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 1999, 2000, 2001, 2002, 2004, 2005, 2007
6 Free Software Foundation, Inc.
8 This file is part of GNU Binutils.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
26 #include "libiberty.h"
27 #include "filenames.h"
28 #include "basic_blocks.h"
32 #include "search_list.h"
37 static int cmp_bb (const PTR, const PTR);
38 static int cmp_ncalls (const PTR, const PTR);
39 static void fskip_string (FILE *);
40 static void annotate_with_count (char *, unsigned int, int, PTR);
42 /* Default option values: */
43 bfd_boolean bb_annotate_all_lines = FALSE;
44 unsigned long bb_min_calls = 1;
45 int bb_table_length = 10;
47 /* Variables used to compute annotated source listing stats: */
48 static long num_executable_lines;
49 static long num_lines_executed;
52 /* Helper for sorting. Compares two symbols and returns result
53 such that sorting will be increasing according to filename, line
54 number, and address (in that order). */
57 cmp_bb (const PTR lp, const PTR rp)
60 const Sym *left = *(const Sym **) lp;
61 const Sym *right = *(const Sym **) rp;
63 if (left->file && right->file)
65 r = filename_cmp (left->file->name, right->file->name);
70 if (left->line_num != right->line_num)
71 return left->line_num - right->line_num;
74 if (left->addr < right->addr)
76 else if (left->addr > right->addr)
83 /* Helper for sorting. Order basic blocks in decreasing number of
84 calls, ties are broken in increasing order of line numbers. */
86 cmp_ncalls (const PTR lp, const PTR rp)
88 const Sym *left = *(const Sym **) lp;
89 const Sym *right = *(const Sym **) rp;
96 if (left->ncalls < right->ncalls)
98 else if (left->ncalls > right->ncalls)
101 return left->line_num - right->line_num;
104 /* Skip over variable length string. */
106 fskip_string (FILE *fp)
110 while ((ch = fgetc (fp)) != EOF)
117 /* Read a basic-block record from file IFP. FILENAME is the name
118 of file IFP and is provided for formatting error-messages only. */
121 bb_read_rec (FILE *ifp, const char *filename)
123 unsigned int nblocks, b;
124 bfd_vma addr, ncalls;
127 if (gmon_io_read_32 (ifp, &nblocks))
129 fprintf (stderr, _("%s: %s: unexpected end of file\n"),
134 nblocks = bfd_get_32 (core_bfd, (bfd_byte *) & nblocks);
135 if (gmon_file_version == 0)
138 for (b = 0; b < nblocks; ++b)
140 if (gmon_file_version == 0)
144 /* Version 0 had lots of extra stuff that we don't
145 care about anymore. */
146 if ((fread (&ncalls, sizeof (ncalls), 1, ifp) != 1)
147 || (fread (&addr, sizeof (addr), 1, ifp) != 1)
148 || (fskip_string (ifp), FALSE)
149 || (fskip_string (ifp), FALSE)
150 || (fread (&line_num, sizeof (line_num), 1, ifp) != 1))
156 else if (gmon_io_read_vma (ifp, &addr)
157 || gmon_io_read_vma (ifp, &ncalls))
163 /* Basic-block execution counts are meaningful only if we're
164 profiling at the line-by-line level: */
165 if (line_granularity)
167 sym = sym_lookup (&symtab, addr);
174 printf ("[bb_read_rec] 0x%lx->0x%lx (%s:%d) cnt=%lu\n",
175 (unsigned long) addr, (unsigned long) sym->addr,
176 sym->name, sym->line_num, (unsigned long) ncalls));
178 for (i = 0; i < NBBS; i++)
180 if (! sym->bb_addr[i] || sym->bb_addr[i] == addr)
182 sym->bb_addr[i] = addr;
183 sym->bb_calls[i] += ncalls;
191 static bfd_boolean user_warned = FALSE;
197 _("%s: warning: ignoring basic-block exec counts (use -l or --line)\n"),
205 /* Write all basic-blocks with non-zero counts to file OFP. FILENAME
206 is the name of OFP and is provided for producing error-messages
209 bb_write_blocks (FILE *ofp, const char *filename)
211 unsigned int nblocks = 0;
215 /* Count how many non-zero blocks with have: */
216 for (sym = symtab.base; sym < symtab.limit; ++sym)
218 for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
224 if (gmon_io_write_8 (ofp, GMON_TAG_BB_COUNT)
225 || gmon_io_write_32 (ofp, nblocks))
232 for (sym = symtab.base; sym < symtab.limit; ++sym)
234 for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
236 if (gmon_io_write_vma (ofp, sym->bb_addr[i])
237 || gmon_io_write_vma (ofp, (bfd_vma) sym->bb_calls[i]))
246 /* Output basic-block statistics in a format that is easily parseable.
247 Current the format is:
249 <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls> */
254 Sym **sorted_bbs, *sym;
255 unsigned int i, j, len;
258 first_output = FALSE;
262 /* Sort basic-blocks according to function name and line number: */
263 sorted_bbs = (Sym **) xmalloc (symtab.len * sizeof (sorted_bbs[0]));
266 for (sym = symtab.base; sym < symtab.limit; ++sym)
268 /* Accept symbol if it's in the INCL_EXEC table
269 or there is no INCL_EXEC table
270 and it does not appear in the EXCL_EXEC table. */
271 if (sym_lookup (&syms[INCL_EXEC], sym->addr)
272 || (syms[INCL_EXEC].len == 0
273 && !sym_lookup (&syms[EXCL_EXEC], sym->addr)))
275 sorted_bbs[len++] = sym;
279 qsort (sorted_bbs, len, sizeof (sorted_bbs[0]), cmp_bb);
281 /* Output basic-blocks: */
283 for (i = 0; i < len; ++i)
285 sym = sorted_bbs [i];
287 if (sym->ncalls > 0 || ! ignore_zeros)
289 /* FIXME: This only works if bfd_vma is unsigned long. */
290 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
291 sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
292 sym->name, (unsigned long) sym->addr, sym->ncalls);
295 for (j = 0; j < NBBS && sym->bb_addr[j]; j ++)
297 if (sym->bb_calls[j] > 0 || ! ignore_zeros)
299 /* FIXME: This only works if bfd_vma is unsigned long. */
300 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
301 sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
302 sym->name, (unsigned long) sym->bb_addr[j],
310 /* Helper for bb_annotated_source: format annotation containing
311 number of line executions. Depends on being called on each
312 line of a file in sequential order.
314 Global variable bb_annotate_all_lines enables execution count
315 compression (counts are supressed if identical to the last one)
316 and prints counts on all executed lines. Otherwise, print
317 all basic-block execution counts exactly once on the line
318 that starts the basic-block. */
321 annotate_with_count (char *buf, unsigned int width, int line_num, PTR arg)
323 Source_File *sf = (Source_File *) arg;
326 static unsigned long last_count;
327 unsigned long last_print = (unsigned long) -1;
331 if (line_num <= sf->num_lines)
332 b = (Sym *) sf->line[line_num - 1];
336 for (i = 0; i < width; i++)
342 char tmpbuf[NBBS * 30];
344 unsigned long ncalls;
348 ++num_executable_lines;
356 /* If this is a function entry point, label the line no matter what.
357 Otherwise, we're in the middle of a function, so check to see
358 if the first basic-block address is larger than the starting
359 address of the line. If so, then this line begins with a
360 a portion of the previous basic-block, so print that prior
361 execution count (if bb_annotate_all_lines is set). */
364 sprintf (p, "%lu", b->ncalls);
366 last_count = b->ncalls;
367 last_print = last_count;
371 else if (bb_annotate_all_lines
372 && b->bb_addr[0] && b->bb_addr[0] > b->addr)
374 sprintf (p, "%lu", last_count);
376 last_print = last_count;
381 /* Loop through all of this line's basic-blocks. For each one,
382 update last_count, then compress sequential identical counts
383 (if bb_annotate_all_lines) and print the execution count. */
385 for (i = 0; i < NBBS && b->bb_addr[i]; i++)
387 last_count = b->bb_calls[i];
393 ncalls += last_count;
395 if (bb_annotate_all_lines && last_count == last_print)
400 sprintf (p, "%lu", last_count);
403 last_print = last_count;
406 /* We're done. If nothing has been printed on this line,
407 print the last execution count (bb_annotate_all_lines),
408 which could be from either a previous line (if there were
409 no BBs on this line), or from this line (if all our BB
410 counts were compressed out because they were identical). */
412 if (bb_annotate_all_lines && p == tmpbuf)
414 sprintf (p, "%lu", last_count);
424 for (c = 0; c < width; c++)
430 ++num_lines_executed;
432 if (ncalls < bb_min_calls)
434 strcpy (tmpbuf, "#####");
444 strncpy (buf, tmpbuf, width);
451 strcpy (buf + width - len, tmpbuf);
452 for (c = 0; c < width - len; ++c)
458 /* Annotate the files named in SOURCE_FILES with basic-block statistics
459 (execution counts). After each source files, a few statistics
460 regarding that source file are printed. */
463 print_annotated_source ()
465 Sym *sym, *line_stats, *new_line;
470 /* Find maximum line number for each source file that user is
472 for (sym = symtab.base; sym < symtab.limit; ++sym)
474 /* Accept symbol if it's file is known, its line number is
475 bigger than anything we have seen for that file so far and
476 if it's in the INCL_ANNO table or there is no INCL_ANNO
477 table and it does not appear in the EXCL_ANNO table. */
478 if (sym->file && sym->line_num > sym->file->num_lines
479 && (sym_lookup (&syms[INCL_ANNO], sym->addr)
480 || (syms[INCL_ANNO].len == 0
481 && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
483 sym->file->num_lines = sym->line_num;
487 /* Allocate line descriptors: */
488 for (sf = first_src_file; sf; sf = sf->next)
490 if (sf->num_lines > 0)
492 sf->line = (void **) xmalloc (sf->num_lines * sizeof (sf->line[0]));
493 memset (sf->line, 0, sf->num_lines * sizeof (sf->line[0]));
497 /* Count executions per line: */
498 for (sym = symtab.base; sym < symtab.limit; ++sym)
500 if (sym->file && sym->file->num_lines
501 && (sym_lookup (&syms[INCL_ANNO], sym->addr)
502 || (syms[INCL_ANNO].len == 0
503 && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
505 sym->file->ncalls += sym->ncalls;
506 line_stats = (Sym *) sym->file->line[sym->line_num - 1];
510 /* Common case has at most one basic-block per source line: */
511 sym->file->line[sym->line_num - 1] = sym;
513 else if (!line_stats->addr)
515 /* sym is the 3rd .. nth basic block for this line: */
516 line_stats->ncalls += sym->ncalls;
520 /* sym is the second basic block for this line. */
521 new_line = (Sym *) xmalloc (sizeof (*new_line));
522 *new_line = *line_stats;
524 new_line->ncalls += sym->ncalls;
525 sym->file->line[sym->line_num - 1] = new_line;
530 /* Plod over source files, annotating them: */
531 for (sf = first_src_file; sf; sf = sf->next)
533 if (!sf->num_lines || (ignore_zeros && sf->ncalls == 0))
536 num_executable_lines = num_lines_executed = 0;
538 ofp = annotate_source (sf, 16, annotate_with_count, sf);
542 if (bb_table_length > 0)
544 fprintf (ofp, _("\n\nTop %d Lines:\n\n Line Count\n\n"),
547 /* Abuse line arrays---it's not needed anymore: */
548 qsort (sf->line, sf->num_lines, sizeof (sf->line[0]), cmp_ncalls);
549 table_len = bb_table_length;
551 if (table_len > sf->num_lines)
552 table_len = sf->num_lines;
554 for (i = 0; i < table_len; ++i)
556 sym = (Sym *) sf->line[i];
558 if (!sym || sym->ncalls == 0)
561 fprintf (ofp, "%9d %10lu\n", sym->line_num, sym->ncalls);
568 fprintf (ofp, _("\nExecution Summary:\n\n"));
569 fprintf (ofp, _("%9ld Executable lines in this file\n"),
570 num_executable_lines);
571 fprintf (ofp, _("%9ld Lines executed\n"), num_lines_executed);
572 fprintf (ofp, _("%9.2f Percent of the file executed\n"),
574 ? 100.0 * num_lines_executed / (double) num_executable_lines
576 fprintf (ofp, _("\n%9lu Total number of line executions\n"),
578 fprintf (ofp, _("%9.2f Average executions per line\n"),
580 ? (double) sf->ncalls / (double) num_executable_lines