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 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
PARAMS ((const PTR
, const PTR
));
36 static int cmp_ncalls
PARAMS ((const PTR
, const PTR
));
37 static void fskip_string
PARAMS ((FILE *));
38 static void annotate_with_count
PARAMS ((char *, unsigned int, int, PTR
));
40 /* Default option values: */
41 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). */
60 const Sym
*left
= *(const Sym
**) lp
;
61 const Sym
*right
= *(const Sym
**) rp
;
63 if (left
->file
&& right
->file
)
65 r
= strcmp (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. */
90 const Sym
*left
= *(const Sym
**) lp
;
91 const Sym
*right
= *(const Sym
**) rp
;
98 if (left
->ncalls
< right
->ncalls
)
100 else if (left
->ncalls
> right
->ncalls
)
103 return left
->line_num
- right
->line_num
;
106 /* Skip over variable length string. */
113 while ((ch
= fgetc (fp
)) != EOF
)
120 /* Read a basic-block record from file IFP. FILENAME is the name
121 of file IFP and is provided for formatting error-messages only. */
124 bb_read_rec (ifp
, filename
)
126 const char *filename
;
129 bfd_vma addr
, ncalls
;
132 if (gmon_io_read_32 (ifp
, &nblocks
))
134 fprintf (stderr
, _("%s: %s: unexpected end of file\n"),
139 nblocks
= bfd_get_32 (core_bfd
, (bfd_byte
*) & nblocks
);
140 if (gmon_file_version
== 0)
143 for (b
= 0; b
< nblocks
; ++b
)
145 if (gmon_file_version
== 0)
149 /* Version 0 had lots of extra stuff that we don't
150 care about anymore. */
151 if ((fread (&ncalls
, sizeof (ncalls
), 1, ifp
) != 1)
152 || (fread (&addr
, sizeof (addr
), 1, ifp
) != 1)
153 || (fskip_string (ifp
), false)
154 || (fskip_string (ifp
), false)
155 || (fread (&line_num
, sizeof (line_num
), 1, ifp
) != 1))
161 else if (gmon_io_read_vma (ifp
, &addr
)
162 || gmon_io_read_vma (ifp
, &ncalls
))
168 /* Basic-block execution counts are meaningful only if we're
169 profiling at the line-by-line level: */
170 if (line_granularity
)
172 sym
= sym_lookup (&symtab
, addr
);
179 printf ("[bb_read_rec] 0x%lx->0x%lx (%s:%d) cnt=%lu\n",
180 (unsigned long) addr
, (unsigned long) sym
->addr
,
181 sym
->name
, sym
->line_num
, (unsigned long) ncalls
));
183 for (i
= 0; i
< NBBS
; i
++)
185 if (! sym
->bb_addr
[i
] || sym
->bb_addr
[i
] == addr
)
187 sym
->bb_addr
[i
] = addr
;
188 sym
->bb_calls
[i
] += ncalls
;
196 static boolean user_warned
= false;
202 _("%s: warning: ignoring basic-block exec counts (use -l or --line)\n"),
210 /* Write all basic-blocks with non-zero counts to file OFP. FILENAME
211 is the name of OFP and is provided for producing error-messages
214 bb_write_blocks (ofp
, filename
)
216 const char *filename
;
218 unsigned int nblocks
= 0;
222 /* Count how many non-zero blocks with have: */
223 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
225 for (i
= 0; i
< NBBS
&& sym
->bb_addr
[i
]; i
++)
231 if (gmon_io_write_8 (ofp
, GMON_TAG_BB_COUNT
)
232 || gmon_io_write_32 (ofp
, nblocks
))
239 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
241 for (i
= 0; i
< NBBS
&& sym
->bb_addr
[i
]; i
++)
243 if (gmon_io_write_vma (ofp
, sym
->bb_addr
[i
])
244 || gmon_io_write_vma (ofp
, (bfd_vma
) sym
->bb_calls
[i
]))
253 /* Output basic-block statistics in a format that is easily parseable.
254 Current the format is:
256 <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls> */
261 Sym
**sorted_bbs
, *sym
;
262 unsigned int i
, j
, len
;
265 first_output
= false;
269 /* Sort basic-blocks according to function name and line number: */
270 sorted_bbs
= (Sym
**) xmalloc (symtab
.len
* sizeof (sorted_bbs
[0]));
273 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
275 /* Accept symbol if it's in the INCL_EXEC table
276 or there is no INCL_EXEC table
277 and it does not appear in the EXCL_EXEC table. */
278 if (sym_lookup (&syms
[INCL_EXEC
], sym
->addr
)
279 || (syms
[INCL_EXEC
].len
== 0
280 && !sym_lookup (&syms
[EXCL_EXEC
], sym
->addr
)))
282 sorted_bbs
[len
++] = sym
;
286 qsort (sorted_bbs
, len
, sizeof (sorted_bbs
[0]), cmp_bb
);
288 /* Output basic-blocks: */
290 for (i
= 0; i
< len
; ++i
)
292 if (sym
->ncalls
> 0 || ! ignore_zeros
)
294 /* FIXME: This only works if bfd_vma is unsigned long. */
295 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
296 sym
->file
? sym
->file
->name
: _("<unknown>"), sym
->line_num
,
297 sym
->name
, (unsigned long) sym
->addr
, sym
->ncalls
);
300 for (j
= 0; j
< NBBS
&& sym
->bb_addr
[j
]; j
++)
302 if (sym
->bb_calls
[j
] > 0 || ! ignore_zeros
)
304 /* FIXME: This only works if bfd_vma is unsigned long. */
305 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
306 sym
->file
? sym
->file
->name
: _("<unknown>"), sym
->line_num
,
307 sym
->name
, (unsigned long) sym
->bb_addr
[j
],
315 /* Helper for bb_annotated_source: format annotation containing
316 number of line executions. Depends on being called on each
317 line of a file in sequential order.
319 Global variable bb_annotate_all_lines enables execution count
320 compression (counts are supressed if identical to the last one)
321 and prints counts on all executed lines. Otherwise, print
322 all basic-block execution counts exactly once on the line
323 that starts the basic-block. */
326 annotate_with_count (buf
, width
, line_num
, arg
)
332 Source_File
*sf
= arg
;
335 static unsigned long last_count
;
336 unsigned long last_print
= (unsigned long) -1;
340 if (line_num
<= sf
->num_lines
)
341 b
= sf
->line
[line_num
- 1];
345 for (i
= 0; i
< width
; i
++)
351 char tmpbuf
[NBBS
* 30];
353 unsigned long ncalls
;
357 ++num_executable_lines
;
365 /* If this is a function entry point, label the line no matter what.
366 Otherwise, we're in the middle of a function, so check to see
367 if the first basic-block address is larger than the starting
368 address of the line. If so, then this line begins with a
369 a portion of the previous basic-block, so print that prior
370 execution count (if bb_annotate_all_lines is set). */
373 sprintf (p
, "%lu", b
->ncalls
);
375 last_count
= b
->ncalls
;
376 last_print
= last_count
;
380 else if (bb_annotate_all_lines
381 && b
->bb_addr
[0] && b
->bb_addr
[0] > b
->addr
)
383 sprintf (p
, "%lu", last_count
);
385 last_print
= last_count
;
390 /* Loop through all of this line's basic-blocks. For each one,
391 update last_count, then compress sequential identical counts
392 (if bb_annotate_all_lines) and print the execution count. */
394 for (i
= 0; i
< NBBS
&& b
->bb_addr
[i
]; i
++)
396 last_count
= b
->bb_calls
[i
];
402 ncalls
+= last_count
;
404 if (bb_annotate_all_lines
&& last_count
== last_print
)
409 sprintf (p
, "%lu", last_count
);
412 last_print
= last_count
;
415 /* We're done. If nothing has been printed on this line,
416 print the last execution count (bb_annotate_all_lines),
417 which could be from either a previous line (if there were
418 no BBs on this line), or from this line (if all our BB
419 counts were compressed out because they were identical). */
421 if (bb_annotate_all_lines
&& p
== tmpbuf
)
423 sprintf (p
, "%lu", last_count
);
433 for (c
= 0; c
< width
; c
++)
439 ++num_lines_executed
;
441 if (ncalls
< bb_min_calls
)
443 strcpy (tmpbuf
, "#####");
453 strncpy (buf
, tmpbuf
, width
);
460 strcpy (buf
+ width
- len
, tmpbuf
);
461 for (c
= 0; c
< width
- len
; ++c
)
467 /* Annotate the files named in SOURCE_FILES with basic-block statistics
468 (execution counts). After each source files, a few statistics
469 regarding that source file are printed. */
472 print_annotated_source ()
474 Sym
*sym
, *line_stats
, *new_line
;
479 /* Find maximum line number for each source file that user is
481 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
483 /* Accept symbol if it's file is known, its line number is
484 bigger than anything we have seen for that file so far and
485 if it's in the INCL_ANNO table or there is no INCL_ANNO
486 table and it does not appear in the EXCL_ANNO table. */
487 if (sym
->file
&& sym
->line_num
> sym
->file
->num_lines
488 && (sym_lookup (&syms
[INCL_ANNO
], sym
->addr
)
489 || (syms
[INCL_ANNO
].len
== 0
490 && !sym_lookup (&syms
[EXCL_ANNO
], sym
->addr
))))
492 sym
->file
->num_lines
= sym
->line_num
;
496 /* Allocate line descriptors: */
497 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
499 if (sf
->num_lines
> 0)
501 sf
->line
= (void *) xmalloc (sf
->num_lines
* sizeof (sf
->line
[0]));
502 memset (sf
->line
, 0, sf
->num_lines
* sizeof (sf
->line
[0]));
506 /* Count executions per line: */
507 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
509 if (sym
->file
&& sym
->file
->num_lines
510 && (sym_lookup (&syms
[INCL_ANNO
], sym
->addr
)
511 || (syms
[INCL_ANNO
].len
== 0
512 && !sym_lookup (&syms
[EXCL_ANNO
], sym
->addr
))))
514 sym
->file
->ncalls
+= sym
->ncalls
;
515 line_stats
= sym
->file
->line
[sym
->line_num
- 1];
519 /* Common case has at most one basic-block per source line: */
520 sym
->file
->line
[sym
->line_num
- 1] = sym
;
522 else if (!line_stats
->addr
)
524 /* sym is the 3rd .. nth basic block for this line: */
525 line_stats
->ncalls
+= sym
->ncalls
;
529 /* sym is the second basic block for this line. */
530 new_line
= (Sym
*) xmalloc (sizeof (*new_line
));
531 *new_line
= *line_stats
;
533 new_line
->ncalls
+= sym
->ncalls
;
534 sym
->file
->line
[sym
->line_num
- 1] = new_line
;
539 /* Plod over source files, annotating them: */
540 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
542 if (!sf
->num_lines
|| (ignore_zeros
&& sf
->ncalls
== 0))
545 num_executable_lines
= num_lines_executed
= 0;
547 ofp
= annotate_source (sf
, 16, annotate_with_count
, sf
);
551 if (bb_table_length
> 0)
553 fprintf (ofp
, _("\n\nTop %d Lines:\n\n Line Count\n\n"),
556 /* Abuse line arrays---it's not needed anymore: */
557 qsort (sf
->line
, sf
->num_lines
, sizeof (sf
->line
[0]), cmp_ncalls
);
558 table_len
= bb_table_length
;
560 if (table_len
> sf
->num_lines
)
561 table_len
= sf
->num_lines
;
563 for (i
= 0; i
< table_len
; ++i
)
567 if (!sym
|| sym
->ncalls
== 0)
570 fprintf (ofp
, "%9d %10lu\n", sym
->line_num
, sym
->ncalls
);
577 fprintf (ofp
, _("\nExecution Summary:\n\n"));
578 fprintf (ofp
, _("%9ld Executable lines in this file\n"),
579 num_executable_lines
);
580 fprintf (ofp
, _("%9ld Lines executed\n"), num_lines_executed
);
581 fprintf (ofp
, _("%9.2f Percent of the file executed\n"),
583 ? 100.0 * num_lines_executed
/ (double) num_executable_lines
585 fprintf (ofp
, _("\n%9lu Total number of line executions\n"),
587 fprintf (ofp
, _("%9.2f Average executions per line\n"),
589 ? (double) sf
->ncalls
/ (double) num_executable_lines