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.
5 Copyright 2000, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
7 This file is part of GNU Binutils.
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.
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.
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
24 #include "libiberty.h"
26 #include "basic_blocks.h"
30 #include "search_list.h"
35 static int cmp_bb (const PTR, const PTR);
36 static int cmp_ncalls (const PTR, const PTR);
37 static void fskip_string (FILE *);
38 static void annotate_with_count (char *, unsigned int, int, PTR);
40 /* Default option values: */
41 bfd_boolean bb_annotate_all_lines = FALSE;
42 unsigned long bb_min_calls = 1;
43 int bb_table_length = 10;
45 /* Variables used to compute annotated source listing stats: */
46 static long num_executable_lines;
47 static long num_lines_executed;
50 /* Helper for sorting. Compares two symbols and returns result
51 such that sorting will be increasing according to filename, line
52 number, and address (in that order). */
55 cmp_bb (const PTR lp, const PTR rp)
58 const Sym *left = *(const Sym **) lp;
59 const Sym *right = *(const Sym **) rp;
61 if (left->file && right->file)
63 r = strcmp (left->file->name, right->file->name);
68 if (left->line_num != right->line_num)
69 return left->line_num - right->line_num;
72 if (left->addr < right->addr)
74 else if (left->addr > right->addr)
81 /* Helper for sorting. Order basic blocks in decreasing number of
82 calls, ties are broken in increasing order of line numbers. */
84 cmp_ncalls (const PTR lp, const PTR rp)
86 const Sym *left = *(const Sym **) lp;
87 const Sym *right = *(const Sym **) rp;
94 if (left->ncalls < right->ncalls)
96 else if (left->ncalls > right->ncalls)
99 return left->line_num - right->line_num;
102 /* Skip over variable length string. */
104 fskip_string (FILE *fp)
108 while ((ch = fgetc (fp)) != EOF)
115 /* Read a basic-block record from file IFP. FILENAME is the name
116 of file IFP and is provided for formatting error-messages only. */
119 bb_read_rec (FILE *ifp, const char *filename)
121 unsigned int nblocks, b;
122 bfd_vma addr, ncalls;
125 if (gmon_io_read_32 (ifp, &nblocks))
127 fprintf (stderr, _("%s: %s: unexpected end of file\n"),
132 nblocks = bfd_get_32 (core_bfd, (bfd_byte *) & nblocks);
133 if (gmon_file_version == 0)
136 for (b = 0; b < nblocks; ++b)
138 if (gmon_file_version == 0)
142 /* Version 0 had lots of extra stuff that we don't
143 care about anymore. */
144 if ((fread (&ncalls, sizeof (ncalls), 1, ifp) != 1)
145 || (fread (&addr, sizeof (addr), 1, ifp) != 1)
146 || (fskip_string (ifp), FALSE)
147 || (fskip_string (ifp), FALSE)
148 || (fread (&line_num, sizeof (line_num), 1, ifp) != 1))
154 else if (gmon_io_read_vma (ifp, &addr)
155 || gmon_io_read_vma (ifp, &ncalls))
161 /* Basic-block execution counts are meaningful only if we're
162 profiling at the line-by-line level: */
163 if (line_granularity)
165 sym = sym_lookup (&symtab, addr);
172 printf ("[bb_read_rec] 0x%lx->0x%lx (%s:%d) cnt=%lu\n",
173 (unsigned long) addr, (unsigned long) sym->addr,
174 sym->name, sym->line_num, (unsigned long) ncalls));
176 for (i = 0; i < NBBS; i++)
178 if (! sym->bb_addr[i] || sym->bb_addr[i] == addr)
180 sym->bb_addr[i] = addr;
181 sym->bb_calls[i] += ncalls;
189 static bfd_boolean user_warned = FALSE;
195 _("%s: warning: ignoring basic-block exec counts (use -l or --line)\n"),
203 /* Write all basic-blocks with non-zero counts to file OFP. FILENAME
204 is the name of OFP and is provided for producing error-messages
207 bb_write_blocks (FILE *ofp, const char *filename)
209 unsigned int nblocks = 0;
213 /* Count how many non-zero blocks with have: */
214 for (sym = symtab.base; sym < symtab.limit; ++sym)
216 for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
222 if (gmon_io_write_8 (ofp, GMON_TAG_BB_COUNT)
223 || gmon_io_write_32 (ofp, nblocks))
230 for (sym = symtab.base; sym < symtab.limit; ++sym)
232 for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
234 if (gmon_io_write_vma (ofp, sym->bb_addr[i])
235 || gmon_io_write_vma (ofp, (bfd_vma) sym->bb_calls[i]))
244 /* Output basic-block statistics in a format that is easily parseable.
245 Current the format is:
247 <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls> */
252 Sym **sorted_bbs, *sym;
253 unsigned int i, j, len;
256 first_output = FALSE;
260 /* Sort basic-blocks according to function name and line number: */
261 sorted_bbs = (Sym **) xmalloc (symtab.len * sizeof (sorted_bbs[0]));
264 for (sym = symtab.base; sym < symtab.limit; ++sym)
266 /* Accept symbol if it's in the INCL_EXEC table
267 or there is no INCL_EXEC table
268 and it does not appear in the EXCL_EXEC table. */
269 if (sym_lookup (&syms[INCL_EXEC], sym->addr)
270 || (syms[INCL_EXEC].len == 0
271 && !sym_lookup (&syms[EXCL_EXEC], sym->addr)))
273 sorted_bbs[len++] = sym;
277 qsort (sorted_bbs, len, sizeof (sorted_bbs[0]), cmp_bb);
279 /* Output basic-blocks: */
281 for (i = 0; i < len; ++i)
283 if (sym->ncalls > 0 || ! ignore_zeros)
285 /* FIXME: This only works if bfd_vma is unsigned long. */
286 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
287 sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
288 sym->name, (unsigned long) sym->addr, sym->ncalls);
291 for (j = 0; j < NBBS && sym->bb_addr[j]; j ++)
293 if (sym->bb_calls[j] > 0 || ! ignore_zeros)
295 /* FIXME: This only works if bfd_vma is unsigned long. */
296 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
297 sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
298 sym->name, (unsigned long) sym->bb_addr[j],
306 /* Helper for bb_annotated_source: format annotation containing
307 number of line executions. Depends on being called on each
308 line of a file in sequential order.
310 Global variable bb_annotate_all_lines enables execution count
311 compression (counts are supressed if identical to the last one)
312 and prints counts on all executed lines. Otherwise, print
313 all basic-block execution counts exactly once on the line
314 that starts the basic-block. */
317 annotate_with_count (char *buf, unsigned int width, int line_num, PTR arg)
319 Source_File *sf = arg;
322 static unsigned long last_count;
323 unsigned long last_print = (unsigned long) -1;
327 if (line_num <= sf->num_lines)
328 b = sf->line[line_num - 1];
332 for (i = 0; i < width; i++)
338 char tmpbuf[NBBS * 30];
340 unsigned long ncalls;
344 ++num_executable_lines;
352 /* If this is a function entry point, label the line no matter what.
353 Otherwise, we're in the middle of a function, so check to see
354 if the first basic-block address is larger than the starting
355 address of the line. If so, then this line begins with a
356 a portion of the previous basic-block, so print that prior
357 execution count (if bb_annotate_all_lines is set). */
360 sprintf (p, "%lu", b->ncalls);
362 last_count = b->ncalls;
363 last_print = last_count;
367 else if (bb_annotate_all_lines
368 && b->bb_addr[0] && b->bb_addr[0] > b->addr)
370 sprintf (p, "%lu", last_count);
372 last_print = last_count;
377 /* Loop through all of this line's basic-blocks. For each one,
378 update last_count, then compress sequential identical counts
379 (if bb_annotate_all_lines) and print the execution count. */
381 for (i = 0; i < NBBS && b->bb_addr[i]; i++)
383 last_count = b->bb_calls[i];
389 ncalls += last_count;
391 if (bb_annotate_all_lines && last_count == last_print)
396 sprintf (p, "%lu", last_count);
399 last_print = last_count;
402 /* We're done. If nothing has been printed on this line,
403 print the last execution count (bb_annotate_all_lines),
404 which could be from either a previous line (if there were
405 no BBs on this line), or from this line (if all our BB
406 counts were compressed out because they were identical). */
408 if (bb_annotate_all_lines && p == tmpbuf)
410 sprintf (p, "%lu", last_count);
420 for (c = 0; c < width; c++)
426 ++num_lines_executed;
428 if (ncalls < bb_min_calls)
430 strcpy (tmpbuf, "#####");
440 strncpy (buf, tmpbuf, width);
447 strcpy (buf + width - len, tmpbuf);
448 for (c = 0; c < width - len; ++c)
454 /* Annotate the files named in SOURCE_FILES with basic-block statistics
455 (execution counts). After each source files, a few statistics
456 regarding that source file are printed. */
459 print_annotated_source ()
461 Sym *sym, *line_stats, *new_line;
466 /* Find maximum line number for each source file that user is
468 for (sym = symtab.base; sym < symtab.limit; ++sym)
470 /* Accept symbol if it's file is known, its line number is
471 bigger than anything we have seen for that file so far and
472 if it's in the INCL_ANNO table or there is no INCL_ANNO
473 table and it does not appear in the EXCL_ANNO table. */
474 if (sym->file && sym->line_num > sym->file->num_lines
475 && (sym_lookup (&syms[INCL_ANNO], sym->addr)
476 || (syms[INCL_ANNO].len == 0
477 && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
479 sym->file->num_lines = sym->line_num;
483 /* Allocate line descriptors: */
484 for (sf = first_src_file; sf; sf = sf->next)
486 if (sf->num_lines > 0)
488 sf->line = (void *) xmalloc (sf->num_lines * sizeof (sf->line[0]));
489 memset (sf->line, 0, sf->num_lines * sizeof (sf->line[0]));
493 /* Count executions per line: */
494 for (sym = symtab.base; sym < symtab.limit; ++sym)
496 if (sym->file && sym->file->num_lines
497 && (sym_lookup (&syms[INCL_ANNO], sym->addr)
498 || (syms[INCL_ANNO].len == 0
499 && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
501 sym->file->ncalls += sym->ncalls;
502 line_stats = sym->file->line[sym->line_num - 1];
506 /* Common case has at most one basic-block per source line: */
507 sym->file->line[sym->line_num - 1] = sym;
509 else if (!line_stats->addr)
511 /* sym is the 3rd .. nth basic block for this line: */
512 line_stats->ncalls += sym->ncalls;
516 /* sym is the second basic block for this line. */
517 new_line = (Sym *) xmalloc (sizeof (*new_line));
518 *new_line = *line_stats;
520 new_line->ncalls += sym->ncalls;
521 sym->file->line[sym->line_num - 1] = new_line;
526 /* Plod over source files, annotating them: */
527 for (sf = first_src_file; sf; sf = sf->next)
529 if (!sf->num_lines || (ignore_zeros && sf->ncalls == 0))
532 num_executable_lines = num_lines_executed = 0;
534 ofp = annotate_source (sf, 16, annotate_with_count, sf);
538 if (bb_table_length > 0)
540 fprintf (ofp, _("\n\nTop %d Lines:\n\n Line Count\n\n"),
543 /* Abuse line arrays---it's not needed anymore: */
544 qsort (sf->line, sf->num_lines, sizeof (sf->line[0]), cmp_ncalls);
545 table_len = bb_table_length;
547 if (table_len > sf->num_lines)
548 table_len = sf->num_lines;
550 for (i = 0; i < table_len; ++i)
554 if (!sym || sym->ncalls == 0)
557 fprintf (ofp, "%9d %10lu\n", sym->line_num, sym->ncalls);
564 fprintf (ofp, _("\nExecution Summary:\n\n"));
565 fprintf (ofp, _("%9ld Executable lines in this file\n"),
566 num_executable_lines);
567 fprintf (ofp, _("%9ld Lines executed\n"), num_lines_executed);
568 fprintf (ofp, _("%9.2f Percent of the file executed\n"),
570 ? 100.0 * num_lines_executed / (double) num_executable_lines
572 fprintf (ofp, _("\n%9lu Total number of line executions\n"),
574 fprintf (ofp, _("%9.2f Average executions per line\n"),
576 ? (double) sf->ncalls / (double) num_executable_lines