source: trunk/src/binutils/gprof/basic_blocks.c@ 597

Last change on this file since 597 was 10, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 14.5 KB
Line 
1/* basic_blocks.c - Basic-block level related code: reading/writing
2 of basic-block info to/from gmon.out; computing and formatting of
3 basic-block related statistics.
4
5 Copyright 2000, 2001 Free Software Foundation, Inc.
6
7 This file is part of GNU Binutils.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
23
24
25#include <stdio.h>
26#include "basic_blocks.h"
27#include "corefile.h"
28#include "gmon_io.h"
29#include "gmon_out.h"
30#include "gprof.h"
31#include "libiberty.h"
32#include "source.h"
33#include "sym_ids.h"
34#ifdef HAVE_UNISTD_H
35#include <unistd.h>
36#endif
37
38/* Default option values: */
39bool bb_annotate_all_lines = FALSE;
40unsigned long bb_min_calls = 1;
41int bb_table_length = 10;
42
43/* Variables used to compute annotated source listing stats: */
44static long num_executable_lines;
45static long num_lines_executed;
46
47
48/* Helper for sorting. Compares two symbols and returns result
49 such that sorting will be increasing according to filename, line
50 number, and address (in that order). */
51
52static int
53DEFUN (cmp_bb, (lp, rp), const void *lp AND const void *rp)
54{
55 int r;
56 const Sym *left = *(const Sym **) lp;
57 const Sym *right = *(const Sym **) rp;
58
59 if (left->file && right->file)
60 {
61 r = strcmp (left->file->name, right->file->name);
62
63 if (r)
64 return r;
65
66 if (left->line_num != right->line_num)
67 return left->line_num - right->line_num;
68 }
69
70 if (left->addr < right->addr)
71 return -1;
72 else if (left->addr > right->addr)
73 return 1;
74 else
75 return 0;
76}
77
78
79/* Helper for sorting. Order basic blocks in decreasing number of
80 calls, ties are broken in increasing order of line numbers. */
81static int
82DEFUN (cmp_ncalls, (lp, rp), const void *lp AND const void *rp)
83{
84 const Sym *left = *(const Sym **) lp;
85 const Sym *right = *(const Sym **) rp;
86
87 if (!left)
88 return 1;
89 else if (!right)
90 return -1;
91
92 if (left->ncalls < right->ncalls)
93 return 1;
94 else if (left->ncalls > right->ncalls)
95 return -1;
96
97 return left->line_num - right->line_num;
98}
99
100/* Skip over variable length string. */
101static void
102DEFUN (fskip_string, (fp), FILE * fp)
103{
104 int ch;
105
106 while ((ch = fgetc (fp)) != EOF)
107 {
108 if (ch == '\0')
109 break;
110 }
111}
112
113/* Read a basic-block record from file IFP. FILENAME is the name
114 of file IFP and is provided for formatting error-messages only. */
115
116void
117DEFUN (bb_read_rec, (ifp, filename), FILE * ifp AND const char *filename)
118{
119 int nblocks, b;
120 bfd_vma addr, ncalls;
121 Sym *sym;
122
123 if (gmon_io_read_32 (ifp, &nblocks))
124 {
125 fprintf (stderr, _("%s: %s: unexpected end of file\n"),
126 whoami, filename);
127 done (1);
128 }
129
130 nblocks = bfd_get_32 (core_bfd, (bfd_byte *) & nblocks);
131 if (gmon_file_version == 0)
132 fskip_string (ifp);
133
134 for (b = 0; b < nblocks; ++b)
135 {
136 if (gmon_file_version == 0)
137 {
138 int line_num;
139
140 /* Version 0 had lots of extra stuff that we don't
141 care about anymore. */
142 if ((fread (&ncalls, sizeof (ncalls), 1, ifp) != 1)
143 || (fread (&addr, sizeof (addr), 1, ifp) != 1)
144 || (fskip_string (ifp), FALSE)
145 || (fskip_string (ifp), FALSE)
146 || (fread (&line_num, sizeof (line_num), 1, ifp) != 1))
147 {
148 perror (filename);
149 done (1);
150 }
151 }
152 else if (gmon_io_read_vma (ifp, &addr)
153 || gmon_io_read_vma (ifp, &ncalls))
154 {
155 perror (filename);
156 done (1);
157 }
158
159 /* Basic-block execution counts are meaningful only if we're
160 profiling at the line-by-line level: */
161 if (line_granularity)
162 {
163 sym = sym_lookup (&symtab, addr);
164
165 if (sym)
166 {
167 int i;
168
169 DBG (BBDEBUG,
170 printf ("[bb_read_rec] 0x%lx->0x%lx (%s:%d) cnt=%lu\n",
171 (unsigned long) addr, (unsigned long) sym->addr,
172 sym->name, sym->line_num, (unsigned long) ncalls));
173
174 for (i = 0; i < NBBS; i++)
175 {
176 if (! sym->bb_addr[i] || sym->bb_addr[i] == addr)
177 {
178 sym->bb_addr[i] = addr;
179 sym->bb_calls[i] += ncalls;
180 break;
181 }
182 }
183 }
184 }
185 else
186 {
187 static bool user_warned = FALSE;
188
189 if (!user_warned)
190 {
191 user_warned = TRUE;
192 fprintf (stderr,
193 _("%s: warning: ignoring basic-block exec counts (use -l or --line)\n"),
194 whoami);
195 }
196 }
197 }
198 return;
199}
200
201/* Write all basic-blocks with non-zero counts to file OFP. FILENAME
202 is the name of OFP and is provided for producing error-messages
203 only. */
204void
205DEFUN (bb_write_blocks, (ofp, filename), FILE * ofp AND const char *filename)
206{
207 int nblocks = 0;
208 Sym *sym;
209 int i;
210
211 /* Count how many non-zero blocks with have: */
212 for (sym = symtab.base; sym < symtab.limit; ++sym)
213 {
214 for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
215 ;
216 nblocks += i;
217 }
218
219 /* Write header: */
220 if (gmon_io_write_8 (ofp, GMON_TAG_BB_COUNT)
221 || gmon_io_write_32 (ofp, nblocks))
222 {
223 perror (filename);
224 done (1);
225 }
226
227 /* Write counts: */
228 for (sym = symtab.base; sym < symtab.limit; ++sym)
229 {
230 for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
231 {
232 if (gmon_io_write_vma (ofp, sym->bb_addr[i])
233 || gmon_io_write_vma (ofp, sym->bb_calls[i]))
234 {
235 perror (filename);
236 done (1);
237 }
238 }
239 }
240}
241
242/* Output basic-block statistics in a format that is easily parseable.
243 Current the format is:
244
245 <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls> */
246
247void
248DEFUN_VOID (print_exec_counts)
249{
250 Sym **sorted_bbs, *sym;
251 int i, j, len;
252
253 if (first_output)
254 first_output = FALSE;
255 else
256 printf ("\f\n");
257
258 /* Sort basic-blocks according to function name and line number: */
259 sorted_bbs = (Sym **) xmalloc (symtab.len * sizeof (sorted_bbs[0]));
260 len = 0;
261
262 for (sym = symtab.base; sym < symtab.limit; ++sym)
263 {
264 /* Accept symbol if it's in the INCL_EXEC table
265 or there is no INCL_EXEC table
266 and it does not appear in the EXCL_EXEC table. */
267 if (sym_lookup (&syms[INCL_EXEC], sym->addr)
268 || (syms[INCL_EXEC].len == 0
269 && !sym_lookup (&syms[EXCL_EXEC], sym->addr)))
270 {
271 sorted_bbs[len++] = sym;
272 }
273 }
274
275 qsort (sorted_bbs, len, sizeof (sorted_bbs[0]), cmp_bb);
276
277 /* Output basic-blocks: */
278
279 for (i = 0; i < len; ++i)
280 {
281 if (sym->ncalls > 0 || ! ignore_zeros)
282 {
283 /* FIXME: This only works if bfd_vma is unsigned long. */
284 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
285 sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
286 sym->name, (unsigned long) sym->addr, sym->ncalls);
287 }
288
289 for (j = 0; j < NBBS && sym->bb_addr[j]; j ++)
290 {
291 if (sym->bb_calls[j] > 0 || ! ignore_zeros)
292 {
293 /* FIXME: This only works if bfd_vma is unsigned long. */
294 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
295 sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
296 sym->name, (unsigned long) sym->bb_addr[j],
297 sym->bb_calls[j]);
298 }
299 }
300 }
301 free (sorted_bbs);
302}
303
304/* Helper for bb_annotated_source: format annotation containing
305 number of line executions. Depends on being called on each
306 line of a file in sequential order.
307
308 Global variable bb_annotate_all_lines enables execution count
309 compression (counts are supressed if identical to the last one)
310 and prints counts on all executed lines. Otherwise, print
311 all basic-block execution counts exactly once on the line
312 that starts the basic-block. */
313
314static void
315DEFUN (annotate_with_count, (buf, width, line_num, arg),
316 char *buf AND int width AND int line_num AND void *arg)
317{
318 Source_File *sf = arg;
319 Sym *b;
320 int i;
321 static unsigned long last_count;
322 unsigned long last_print = (unsigned long) -1;
323
324 b = NULL;
325
326 if (line_num <= sf->num_lines)
327 b = sf->line[line_num - 1];
328
329 if (!b)
330 {
331 for (i = 0; i < width; i++)
332 buf[i] = ' ';
333 buf[width] = '\0';
334 }
335 else
336 {
337 char tmpbuf[NBBS * 30];
338 char *p;
339 unsigned long ncalls;
340 int ncalls_set;
341 int len;
342
343 ++num_executable_lines;
344
345 p = tmpbuf;
346 *p = '\0';
347
348 ncalls = 0;
349 ncalls_set = 0;
350
351 /* If this is a function entry point, label the line no matter what.
352 Otherwise, we're in the middle of a function, so check to see
353 if the first basic-block address is larger than the starting
354 address of the line. If so, then this line begins with a
355 a portion of the previous basic-block, so print that prior
356 execution count (if bb_annotate_all_lines is set). */
357 if (b->is_func)
358 {
359 sprintf (p, "%lu", b->ncalls);
360 p += strlen (p);
361 last_count = b->ncalls;
362 last_print = last_count;
363 ncalls = b->ncalls;
364 ncalls_set = 1;
365 }
366 else if (bb_annotate_all_lines
367 && b->bb_addr[0] && b->bb_addr[0] > b->addr)
368 {
369 sprintf (p, "%lu", last_count);
370 p += strlen (p);
371 last_print = last_count;
372 ncalls = last_count;
373 ncalls_set = 1;
374 }
375
376 /* Loop through all of this line's basic-blocks. For each one,
377 update last_count, then compress sequential identical counts
378 (if bb_annotate_all_lines) and print the execution count. */
379
380 for (i = 0; i < NBBS && b->bb_addr[i]; i++)
381 {
382 last_count = b->bb_calls[i];
383 if (! ncalls_set)
384 {
385 ncalls = 0;
386 ncalls_set = 1;
387 }
388 ncalls += last_count;
389
390 if (bb_annotate_all_lines && last_count == last_print)
391 continue;
392
393 if (p > tmpbuf)
394 *p++ = ',';
395 sprintf (p, "%lu", last_count);
396 p += strlen (p);
397
398 last_print = last_count;
399 }
400
401 /* We're done. If nothing has been printed on this line,
402 print the last execution count (bb_annotate_all_lines),
403 which could be from either a previous line (if there were
404 no BBs on this line), or from this line (if all our BB
405 counts were compressed out because they were identical). */
406
407 if (bb_annotate_all_lines && p == tmpbuf)
408 {
409 sprintf (p, "%lu", last_count);
410 p += strlen (p);
411 ncalls = last_count;
412 ncalls_set = 1;
413 }
414
415 if (! ncalls_set)
416 {
417 int c;
418
419 for (c = 0; c < width; c++)
420 buf[c] = ' ';
421 buf[width] = '\0';
422 return;
423 }
424
425 ++num_lines_executed;
426
427 if (ncalls < bb_min_calls)
428 {
429 strcpy (tmpbuf, "#####");
430 p = tmpbuf + 5;
431 }
432
433 strcpy (p, " -> ");
434 p += 4;
435
436 len = p - tmpbuf;
437 if (len >= width)
438 {
439 strncpy (buf, tmpbuf, width);
440 buf[width] = '\0';
441 }
442 else
443 {
444 int c;
445
446 strcpy (buf + width - len, tmpbuf);
447 for (c = 0; c < width - len; ++c)
448 buf[c] = ' ';
449 }
450 }
451}
452
453/* Annotate the files named in SOURCE_FILES with basic-block statistics
454 (execution counts). After each source files, a few statistics
455 regarding that source file are printed. */
456
457void
458DEFUN_VOID (print_annotated_source)
459{
460 Sym *sym, *line_stats, *new_line;
461 Source_File *sf;
462 int i, table_len;
463 FILE *ofp;
464
465 /* Find maximum line number for each source file that user is
466 interested in: */
467 for (sym = symtab.base; sym < symtab.limit; ++sym)
468 {
469 /* Accept symbol if it's file is known, its line number is
470 bigger than anything we have seen for that file so far and
471 if it's in the INCL_ANNO table or there is no INCL_ANNO
472 table and it does not appear in the EXCL_ANNO table. */
473 if (sym->file && sym->line_num > sym->file->num_lines
474 && (sym_lookup (&syms[INCL_ANNO], sym->addr)
475 || (syms[INCL_ANNO].len == 0
476 && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
477 {
478 sym->file->num_lines = sym->line_num;
479 }
480 }
481
482 /* Allocate line descriptors: */
483 for (sf = first_src_file; sf; sf = sf->next)
484 {
485 if (sf->num_lines > 0)
486 {
487 sf->line = (void *) xmalloc (sf->num_lines * sizeof (sf->line[0]));
488 memset (sf->line, 0, sf->num_lines * sizeof (sf->line[0]));
489 }
490 }
491
492 /* Count executions per line: */
493 for (sym = symtab.base; sym < symtab.limit; ++sym)
494 {
495 if (sym->file && sym->file->num_lines
496 && (sym_lookup (&syms[INCL_ANNO], sym->addr)
497 || (syms[INCL_ANNO].len == 0
498 && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
499 {
500 sym->file->ncalls += sym->ncalls;
501 line_stats = sym->file->line[sym->line_num - 1];
502
503 if (!line_stats)
504 {
505 /* Common case has at most one basic-block per source line: */
506 sym->file->line[sym->line_num - 1] = sym;
507 }
508 else if (!line_stats->addr)
509 {
510 /* sym is the 3rd .. nth basic block for this line: */
511 line_stats->ncalls += sym->ncalls;
512 }
513 else
514 {
515 /* sym is the second basic block for this line. */
516 new_line = (Sym *) xmalloc (sizeof (*new_line));
517 *new_line = *line_stats;
518 new_line->addr = 0;
519 new_line->ncalls += sym->ncalls;
520 sym->file->line[sym->line_num - 1] = new_line;
521 }
522 }
523 }
524
525 /* Plod over source files, annotating them: */
526 for (sf = first_src_file; sf; sf = sf->next)
527 {
528 if (!sf->num_lines || (ignore_zeros && sf->ncalls == 0))
529 continue;
530
531 num_executable_lines = num_lines_executed = 0;
532
533 ofp = annotate_source (sf, 16, annotate_with_count, sf);
534 if (!ofp)
535 continue;
536
537 if (bb_table_length > 0)
538 {
539 fprintf (ofp, _("\n\nTop %d Lines:\n\n Line Count\n\n"),
540 bb_table_length);
541
542 /* Abuse line arrays---it's not needed anymore: */
543 qsort (sf->line, sf->num_lines, sizeof (sf->line[0]), cmp_ncalls);
544 table_len = bb_table_length;
545
546 if (table_len > sf->num_lines)
547 table_len = sf->num_lines;
548
549 for (i = 0; i < table_len; ++i)
550 {
551 sym = sf->line[i];
552
553 if (!sym || sym->ncalls == 0)
554 break;
555
556 fprintf (ofp, "%9d %10lu\n", sym->line_num, sym->ncalls);
557 }
558 }
559
560 free (sf->line);
561 sf->line = 0;
562
563 fprintf (ofp, _("\nExecution Summary:\n\n"));
564 fprintf (ofp, _("%9ld Executable lines in this file\n"),
565 num_executable_lines);
566 fprintf (ofp, _("%9ld Lines executed\n"), num_lines_executed);
567 fprintf (ofp, _("%9.2f Percent of the file executed\n"),
568 num_executable_lines
569 ? 100.0 * num_lines_executed / (double) num_executable_lines
570 : 100.0);
571 fprintf (ofp, _("\n%9lu Total number of line executions\n"),
572 sf->ncalls);
573 fprintf (ofp, _("%9.2f Average executions per line\n"),
574 num_executable_lines
575 ? (double) sf->ncalls / (double) num_executable_lines
576 : 0.0);
577
578 if (ofp != stdout)
579 fclose (ofp);
580 }
581}
Note: See TracBrowser for help on using the repository browser.