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 (C) 1999-2024 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 3 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., 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "libiberty.h"
26 #include "filenames.h"
27 #include "basic_blocks.h"
31 #include "search_list.h"
36 static int cmp_bb (const void *, const void *);
37 static int cmp_ncalls (const void *, const void *);
38 static void fskip_string (FILE *);
39 static void annotate_with_count (char *, unsigned int, int, void *);
41 /* Default option values: */
42 bool bb_annotate_all_lines
= false;
43 unsigned long bb_min_calls
= 1;
44 int bb_table_length
= 10;
46 /* Variables used to compute annotated source listing stats: */
47 static long num_executable_lines
;
48 static long num_lines_executed
;
51 /* Helper for sorting. Compares two symbols and returns result
52 such that sorting will be increasing according to filename, line
53 number, and address (in that order). */
56 cmp_bb (const void *lp
, const void *rp
)
59 const Sym
*left
= *(const Sym
**) lp
;
60 const Sym
*right
= *(const Sym
**) rp
;
62 if (left
->file
&& right
->file
)
64 r
= filename_cmp (left
->file
->name
, right
->file
->name
);
69 if (left
->line_num
!= right
->line_num
)
70 return left
->line_num
- right
->line_num
;
73 if (left
->addr
< right
->addr
)
75 else if (left
->addr
> right
->addr
)
82 /* Helper for sorting. Order basic blocks in decreasing number of
83 calls, ties are broken in increasing order of line numbers. */
85 cmp_ncalls (const void *lp
, const void *rp
)
87 const Sym
*left
= *(const Sym
**) lp
;
88 const Sym
*right
= *(const Sym
**) rp
;
95 if (left
->ncalls
< right
->ncalls
)
97 else if (left
->ncalls
> right
->ncalls
)
100 return left
->line_num
- right
->line_num
;
103 /* Skip over variable length string. */
105 fskip_string (FILE *fp
)
109 while ((ch
= fgetc (fp
)) != EOF
)
116 /* Read a basic-block record from file IFP. FILENAME is the name
117 of file IFP and is provided for formatting error-messages only. */
120 bb_read_rec (FILE *ifp
, const char *filename
)
122 unsigned int nblocks
, b
;
123 bfd_vma addr
, ncalls
;
126 if (gmon_io_read_32 (ifp
, &nblocks
))
128 fprintf (stderr
, _("%s: %s: unexpected end of file\n"),
133 nblocks
= bfd_get_32 (core_bfd
, (bfd_byte
*) & nblocks
);
134 if (gmon_file_version
== 0)
137 for (b
= 0; b
< nblocks
; ++b
)
139 if (gmon_file_version
== 0)
143 /* Version 0 had lots of extra stuff that we don't
144 care about anymore. */
145 if ((fread (&ncalls
, sizeof (ncalls
), 1, ifp
) != 1)
146 || (fread (&addr
, sizeof (addr
), 1, ifp
) != 1)
147 || (fskip_string (ifp
), false)
148 || (fskip_string (ifp
), false)
149 || (fread (&line_num
, sizeof (line_num
), 1, ifp
) != 1))
155 else if (gmon_io_read_vma (ifp
, &addr
)
156 || gmon_io_read_vma (ifp
, &ncalls
))
162 /* Basic-block execution counts are meaningful only if we're
163 profiling at the line-by-line level: */
164 if (line_granularity
)
166 sym
= sym_lookup (&symtab
, addr
);
173 printf ("[bb_read_rec] 0x%lx->0x%lx (%s:%d) cnt=%lu\n",
174 (unsigned long) addr
, (unsigned long) sym
->addr
,
175 sym
->name
, sym
->line_num
, (unsigned long) ncalls
));
177 for (i
= 0; i
< NBBS
; i
++)
179 if (! sym
->bb_addr
[i
] || sym
->bb_addr
[i
] == addr
)
181 sym
->bb_addr
[i
] = addr
;
182 sym
->bb_calls
[i
] += ncalls
;
190 static bool user_warned
= false;
196 _("%s: warning: ignoring basic-block exec counts (use -l or --line)\n"),
204 /* Write all basic-blocks with non-zero counts to file OFP. FILENAME
205 is the name of OFP and is provided for producing error-messages
208 bb_write_blocks (FILE *ofp
, const char *filename
)
210 unsigned int nblocks
= 0;
214 /* Count how many non-zero blocks with have: */
215 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
217 for (i
= 0; i
< NBBS
&& sym
->bb_addr
[i
]; i
++)
223 if (gmon_io_write_8 (ofp
, GMON_TAG_BB_COUNT
)
224 || gmon_io_write_32 (ofp
, nblocks
))
231 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
233 for (i
= 0; i
< NBBS
&& sym
->bb_addr
[i
]; i
++)
235 if (gmon_io_write_vma (ofp
, sym
->bb_addr
[i
])
236 || gmon_io_write_vma (ofp
, (bfd_vma
) sym
->bb_calls
[i
]))
245 /* Output basic-block statistics in a format that is easily parseable.
246 Current the format is:
248 <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls> */
251 print_exec_counts (void)
253 Sym
**sorted_bbs
, *sym
;
254 unsigned int i
, j
, len
;
257 first_output
= false;
261 /* Sort basic-blocks according to function name and line number: */
262 sorted_bbs
= (Sym
**) xmalloc (symtab
.len
* sizeof (sorted_bbs
[0]));
265 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
267 /* Accept symbol if it's in the INCL_EXEC table
268 or there is no INCL_EXEC table
269 and it does not appear in the EXCL_EXEC table. */
270 if (sym_lookup (&syms
[INCL_EXEC
], sym
->addr
)
271 || (syms
[INCL_EXEC
].len
== 0
272 && !sym_lookup (&syms
[EXCL_EXEC
], sym
->addr
)))
274 sorted_bbs
[len
++] = sym
;
278 qsort (sorted_bbs
, len
, sizeof (sorted_bbs
[0]), cmp_bb
);
280 /* Output basic-blocks: */
282 for (i
= 0; i
< len
; ++i
)
284 sym
= sorted_bbs
[i
];
286 if (sym
->ncalls
> 0 || ! ignore_zeros
)
288 /* FIXME: This only works if bfd_vma is unsigned long. */
289 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
290 sym
->file
? sym
->file
->name
: _("<unknown>"), sym
->line_num
,
291 sym
->name
, (unsigned long) sym
->addr
, sym
->ncalls
);
294 for (j
= 0; j
< NBBS
&& sym
->bb_addr
[j
]; j
++)
296 if (sym
->bb_calls
[j
] > 0 || ! ignore_zeros
)
298 /* FIXME: This only works if bfd_vma is unsigned long. */
299 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
300 sym
->file
? sym
->file
->name
: _("<unknown>"), sym
->line_num
,
301 sym
->name
, (unsigned long) sym
->bb_addr
[j
],
309 /* Helper for bb_annotated_source: format annotation containing
310 number of line executions. Depends on being called on each
311 line of a file in sequential order.
313 Global variable bb_annotate_all_lines enables execution count
314 compression (counts are suppressed if identical to the last one)
315 and prints counts on all executed lines. Otherwise, print
316 all basic-block execution counts exactly once on the line
317 that starts the basic-block. */
320 annotate_with_count (char *buf
, unsigned int width
, int line_num
, void *arg
)
322 Source_File
*sf
= (Source_File
*) arg
;
325 static unsigned long last_count
;
326 unsigned long last_print
= (unsigned long) -1;
330 if (line_num
<= sf
->num_lines
)
331 b
= (Sym
*) sf
->line
[line_num
- 1];
335 for (i
= 0; i
< width
; i
++)
341 char tmpbuf
[NBBS
* 30];
343 unsigned long ncalls
;
347 ++num_executable_lines
;
355 /* If this is a function entry point, label the line no matter what.
356 Otherwise, we're in the middle of a function, so check to see
357 if the first basic-block address is larger than the starting
358 address of the line. If so, then this line begins with a
359 a portion of the previous basic-block, so print that prior
360 execution count (if bb_annotate_all_lines is set). */
363 p
+= sprintf (p
, "%lu", b
->ncalls
);
364 last_count
= b
->ncalls
;
365 last_print
= last_count
;
369 else if (bb_annotate_all_lines
370 && b
->bb_addr
[0] && b
->bb_addr
[0] > b
->addr
)
372 p
+= sprintf (p
, "%lu", last_count
);
373 last_print
= last_count
;
378 /* Loop through all of this line's basic-blocks. For each one,
379 update last_count, then compress sequential identical counts
380 (if bb_annotate_all_lines) and print the execution count. */
382 for (i
= 0; i
< NBBS
&& b
->bb_addr
[i
]; i
++)
384 last_count
= b
->bb_calls
[i
];
390 ncalls
+= last_count
;
392 if (bb_annotate_all_lines
&& last_count
== last_print
)
397 p
+= 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 p
+= sprintf (p
, "%lu", last_count
);
419 for (c
= 0; c
< width
; c
++)
425 ++num_lines_executed
;
427 if (ncalls
< bb_min_calls
)
429 strcpy (tmpbuf
, "#####");
439 strncpy (buf
, tmpbuf
, width
);
446 strcpy (buf
+ width
- len
, tmpbuf
);
447 for (c
= 0; c
< width
- len
; ++c
)
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. */
458 print_annotated_source (void)
460 Sym
*sym
, *line_stats
, *new_line
;
465 /* Find maximum line number for each source file that user is
467 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
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
))))
478 sym
->file
->num_lines
= sym
->line_num
;
482 /* Allocate line descriptors: */
483 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
485 if (sf
->num_lines
> 0)
487 sf
->line
= (void **) xmalloc (sf
->num_lines
* sizeof (sf
->line
[0]));
488 memset (sf
->line
, 0, sf
->num_lines
* sizeof (sf
->line
[0]));
492 /* Count executions per line: */
493 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
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
))))
500 sym
->file
->ncalls
+= sym
->ncalls
;
501 line_stats
= (Sym
*) sym
->file
->line
[sym
->line_num
- 1];
505 /* Common case has at most one basic-block per source line: */
506 sym
->file
->line
[sym
->line_num
- 1] = sym
;
508 else if (!line_stats
->addr
)
510 /* sym is the 3rd .. nth basic block for this line: */
511 line_stats
->ncalls
+= sym
->ncalls
;
515 /* sym is the second basic block for this line. */
516 new_line
= (Sym
*) xmalloc (sizeof (*new_line
));
517 *new_line
= *line_stats
;
519 new_line
->ncalls
+= sym
->ncalls
;
520 sym
->file
->line
[sym
->line_num
- 1] = new_line
;
525 /* Plod over source files, annotating them: */
526 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
528 if (!sf
->num_lines
|| (ignore_zeros
&& sf
->ncalls
== 0))
531 num_executable_lines
= num_lines_executed
= 0;
533 ofp
= annotate_source (sf
, 16, annotate_with_count
, sf
);
537 if (bb_table_length
> 0)
539 fprintf (ofp
, _("\n\nTop %d Lines:\n\n Line Count\n\n"),
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
;
546 if (table_len
> sf
->num_lines
)
547 table_len
= sf
->num_lines
;
549 for (i
= 0; i
< table_len
; ++i
)
551 sym
= (Sym
*) sf
->line
[i
];
553 if (!sym
|| sym
->ncalls
== 0)
556 fprintf (ofp
, "%9d %10lu\n", sym
->line_num
, sym
->ncalls
);
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"),
569 ? 100.0 * num_lines_executed
/ (double) num_executable_lines
571 fprintf (ofp
, _("\n%9lu Total number of line executions\n"),
573 fprintf (ofp
, _("%9.2f Average executions per line\n"),
575 ? (double) sf
->ncalls
/ (double) num_executable_lines