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 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 /* Default option values: */
36 bool bb_annotate_all_lines = FALSE;
37 unsigned long bb_min_calls = 1;
38 int bb_table_length = 10;
40 /* Variables used to compute annotated source listing stats: */
41 static long num_executable_lines;
42 static long num_lines_executed;
45 /* Helper for sorting. Compares two symbols and returns result
46 such that sorting will be increasing according to filename, line
47 number, and address (in that order). */
50 DEFUN (cmp_bb, (lp, rp), const void *lp AND const void *rp)
53 const Sym *left = *(const Sym **) lp;
54 const Sym *right = *(const Sym **) rp;
56 if (left->file && right->file)
58 r = strcmp (left->file->name, right->file->name);
63 if (left->line_num != right->line_num)
64 return left->line_num - right->line_num;
67 if (left->addr < right->addr)
69 else if (left->addr > right->addr)
76 /* Helper for sorting. Order basic blocks in decreasing number of
77 calls, ties are broken in increasing order of line numbers. */
79 DEFUN (cmp_ncalls, (lp, rp), const void *lp AND const void *rp)
81 const Sym *left = *(const Sym **) lp;
82 const Sym *right = *(const Sym **) rp;
89 if (left->ncalls < right->ncalls)
91 else if (left->ncalls > right->ncalls)
94 return left->line_num - right->line_num;
97 /* Skip over variable length string. */
99 DEFUN (fskip_string, (fp), FILE * fp)
103 while ((ch = fgetc (fp)) != EOF)
110 /* Read a basic-block record from file IFP. FILENAME is the name
111 of file IFP and is provided for formatting error-messages only. */
114 DEFUN (bb_read_rec, (ifp, filename), FILE * ifp AND const char *filename)
117 bfd_vma addr, ncalls;
120 if (gmon_io_read_32 (ifp, &nblocks))
122 fprintf (stderr, _("%s: %s: unexpected end of file\n"),
127 nblocks = bfd_get_32 (core_bfd, (bfd_byte *) & nblocks);
128 if (gmon_file_version == 0)
131 for (b = 0; b < nblocks; ++b)
133 if (gmon_file_version == 0)
137 /* Version 0 had lots of extra stuff that we don't
138 care about anymore. */
139 if ((fread (&ncalls, sizeof (ncalls), 1, ifp) != 1)
140 || (fread (&addr, sizeof (addr), 1, ifp) != 1)
141 || (fskip_string (ifp), FALSE)
142 || (fskip_string (ifp), FALSE)
143 || (fread (&line_num, sizeof (line_num), 1, ifp) != 1))
149 else if (gmon_io_read_vma (ifp, &addr)
150 || gmon_io_read_vma (ifp, &ncalls))
156 /* Basic-block execution counts are meaningful only if we're
157 profiling at the line-by-line level: */
158 if (line_granularity)
160 sym = sym_lookup (&symtab, addr);
167 printf ("[bb_read_rec] 0x%lx->0x%lx (%s:%d) cnt=%lu\n",
168 (unsigned long) addr, (unsigned long) sym->addr,
169 sym->name, sym->line_num, (unsigned long) ncalls));
171 for (i = 0; i < NBBS; i++)
173 if (! sym->bb_addr[i] || sym->bb_addr[i] == addr)
175 sym->bb_addr[i] = addr;
176 sym->bb_calls[i] += ncalls;
184 static bool user_warned = FALSE;
190 _("%s: warning: ignoring basic-block exec counts (use -l or --line)\n"),
198 /* Write all basic-blocks with non-zero counts to file OFP. FILENAME
199 is the name of OFP and is provided for producing error-messages
202 DEFUN (bb_write_blocks, (ofp, filename), FILE * ofp AND const char *filename)
208 /* Count how many non-zero blocks with have: */
209 for (sym = symtab.base; sym < symtab.limit; ++sym)
211 for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
217 if (gmon_io_write_8 (ofp, GMON_TAG_BB_COUNT)
218 || gmon_io_write_32 (ofp, nblocks))
225 for (sym = symtab.base; sym < symtab.limit; ++sym)
227 for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
229 if (gmon_io_write_vma (ofp, sym->bb_addr[i])
230 || gmon_io_write_vma (ofp, sym->bb_calls[i]))
239 /* Output basic-block statistics in a format that is easily parseable.
240 Current the format is:
242 <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls> */
245 DEFUN_VOID (print_exec_counts)
247 Sym **sorted_bbs, *sym;
251 first_output = FALSE;
255 /* Sort basic-blocks according to function name and line number: */
256 sorted_bbs = (Sym **) xmalloc (symtab.len * sizeof (sorted_bbs[0]));
259 for (sym = symtab.base; sym < symtab.limit; ++sym)
261 /* Accept symbol if it's in the INCL_EXEC table
262 or there is no INCL_EXEC table
263 and it does not appear in the EXCL_EXEC table. */
264 if (sym_lookup (&syms[INCL_EXEC], sym->addr)
265 || (syms[INCL_EXEC].len == 0
266 && !sym_lookup (&syms[EXCL_EXEC], sym->addr)))
268 sorted_bbs[len++] = sym;
272 qsort (sorted_bbs, len, sizeof (sorted_bbs[0]), cmp_bb);
274 /* Output basic-blocks: */
276 for (i = 0; i < len; ++i)
278 if (sym->ncalls > 0 || ! ignore_zeros)
280 /* FIXME: This only works if bfd_vma is unsigned long. */
281 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
282 sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
283 sym->name, (unsigned long) sym->addr, sym->ncalls);
286 for (j = 0; j < NBBS && sym->bb_addr[j]; j ++)
288 if (sym->bb_calls[j] > 0 || ! ignore_zeros)
290 /* FIXME: This only works if bfd_vma is unsigned long. */
291 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
292 sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
293 sym->name, (unsigned long) sym->bb_addr[j],
301 /* Helper for bb_annotated_source: format annotation containing
302 number of line executions. Depends on being called on each
303 line of a file in sequential order.
305 Global variable bb_annotate_all_lines enables execution count
306 compression (counts are supressed if identical to the last one)
307 and prints counts on all executed lines. Otherwise, print
308 all basic-block execution counts exactly once on the line
309 that starts the basic-block. */
312 DEFUN (annotate_with_count, (buf, width, line_num, arg),
313 char *buf AND int width AND int line_num AND void *arg)
315 Source_File *sf = arg;
318 static unsigned long last_count;
319 unsigned long last_print = (unsigned long) -1;
323 if (line_num <= sf->num_lines)
324 b = sf->line[line_num - 1];
328 for (i = 0; i < width; i++)
334 char tmpbuf[NBBS * 30];
336 unsigned long ncalls;
340 ++num_executable_lines;
348 /* If this is a function entry point, label the line no matter what.
349 Otherwise, we're in the middle of a function, so check to see
350 if the first basic-block address is larger than the starting
351 address of the line. If so, then this line begins with a
352 a portion of the previous basic-block, so print that prior
353 execution count (if bb_annotate_all_lines is set). */
356 sprintf (p, "%lu", b->ncalls);
358 last_count = b->ncalls;
359 last_print = last_count;
363 else if (bb_annotate_all_lines
364 && b->bb_addr[0] && b->bb_addr[0] > b->addr)
366 sprintf (p, "%lu", last_count);
368 last_print = last_count;
373 /* Loop through all of this line's basic-blocks. For each one,
374 update last_count, then compress sequential identical counts
375 (if bb_annotate_all_lines) and print the execution count. */
377 for (i = 0; i < NBBS && b->bb_addr[i]; i++)
379 last_count = b->bb_calls[i];
385 ncalls += last_count;
387 if (bb_annotate_all_lines && last_count == last_print)
392 sprintf (p, "%lu", last_count);
395 last_print = last_count;
398 /* We're done. If nothing has been printed on this line,
399 print the last execution count (bb_annotate_all_lines),
400 which could be from either a previous line (if there were
401 no BBs on this line), or from this line (if all our BB
402 counts were compressed out because they were identical). */
404 if (bb_annotate_all_lines && p == tmpbuf)
406 sprintf (p, "%lu", last_count);
416 for (c = 0; c < width; c++)
422 ++num_lines_executed;
424 if (ncalls < bb_min_calls)
426 strcpy (tmpbuf, "#####");
436 strncpy (buf, tmpbuf, width);
443 strcpy (buf + width - len, tmpbuf);
444 for (c = 0; c < width - len; ++c)
450 /* Annotate the files named in SOURCE_FILES with basic-block statistics
451 (execution counts). After each source files, a few statistics
452 regarding that source file are printed. */
455 DEFUN_VOID (print_annotated_source)
457 Sym *sym, *line_stats, *new_line;
462 /* Find maximum line number for each source file that user is
464 for (sym = symtab.base; sym < symtab.limit; ++sym)
466 /* Accept symbol if it's file is known, its line number is
467 bigger than anything we have seen for that file so far and
468 if it's in the INCL_ANNO table or there is no INCL_ANNO
469 table and it does not appear in the EXCL_ANNO table. */
470 if (sym->file && sym->line_num > sym->file->num_lines
471 && (sym_lookup (&syms[INCL_ANNO], sym->addr)
472 || (syms[INCL_ANNO].len == 0
473 && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
475 sym->file->num_lines = sym->line_num;
479 /* Allocate line descriptors: */
480 for (sf = first_src_file; sf; sf = sf->next)
482 if (sf->num_lines > 0)
484 sf->line = (void *) xmalloc (sf->num_lines * sizeof (sf->line[0]));
485 memset (sf->line, 0, sf->num_lines * sizeof (sf->line[0]));
489 /* Count executions per line: */
490 for (sym = symtab.base; sym < symtab.limit; ++sym)
492 if (sym->file && sym->file->num_lines
493 && (sym_lookup (&syms[INCL_ANNO], sym->addr)
494 || (syms[INCL_ANNO].len == 0
495 && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
497 sym->file->ncalls += sym->ncalls;
498 line_stats = sym->file->line[sym->line_num - 1];
502 /* Common case has at most one basic-block per source line: */
503 sym->file->line[sym->line_num - 1] = sym;
505 else if (!line_stats->addr)
507 /* sym is the 3rd .. nth basic block for this line: */
508 line_stats->ncalls += sym->ncalls;
512 /* sym is the second basic block for this line. */
513 new_line = (Sym *) xmalloc (sizeof (*new_line));
514 *new_line = *line_stats;
516 new_line->ncalls += sym->ncalls;
517 sym->file->line[sym->line_num - 1] = new_line;
522 /* Plod over source files, annotating them: */
523 for (sf = first_src_file; sf; sf = sf->next)
525 if (!sf->num_lines || (ignore_zeros && sf->ncalls == 0))
528 num_executable_lines = num_lines_executed = 0;
530 ofp = annotate_source (sf, 16, annotate_with_count, sf);
534 if (bb_table_length > 0)
536 fprintf (ofp, _("\n\nTop %d Lines:\n\n Line Count\n\n"),
539 /* Abuse line arrays---it's not needed anymore: */
540 qsort (sf->line, sf->num_lines, sizeof (sf->line[0]), cmp_ncalls);
541 table_len = bb_table_length;
543 if (table_len > sf->num_lines)
544 table_len = sf->num_lines;
546 for (i = 0; i < table_len; ++i)
550 if (!sym || sym->ncalls == 0)
553 fprintf (ofp, "%9d %10lu\n", sym->line_num, sym->ncalls);
560 fprintf (ofp, _("\nExecution Summary:\n\n"));
561 fprintf (ofp, _("%9ld Executable lines in this file\n"),
562 num_executable_lines);
563 fprintf (ofp, _("%9ld Lines executed\n"), num_lines_executed);
564 fprintf (ofp, _("%9.2f Percent of the file executed\n"),
566 ? 100.0 * num_lines_executed / (double) num_executable_lines
568 fprintf (ofp, _("\n%9lu Total number of line executions\n"),
570 fprintf (ofp, _("%9.2f Average executions per line\n"),
572 ? (double) sf->ncalls / (double) num_executable_lines