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) 2000 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
26 #include "basic_blocks.h"
31 #include "libiberty.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
)
118 unsigned long ncalls
;
121 if (fread (&nblocks
, sizeof (nblocks
), 1, ifp
) != 1)
123 fprintf (stderr
, _("%s: %s: unexpected end of file\n"), whoami
, filename
);
127 nblocks
= bfd_get_32 (core_bfd
, (bfd_byte
*) & nblocks
);
129 if (gmon_file_version
== 0)
132 for (b
= 0; b
< nblocks
; ++b
)
134 if (gmon_file_version
== 0)
138 /* Version 0 had lots of extra stuff that we don't
139 care about anymore. */
140 if ((fread (&ncalls
, sizeof (ncalls
), 1, ifp
) != 1)
141 || (fread (&addr
, sizeof (addr
), 1, ifp
) != 1)
142 || (fskip_string (ifp
), FALSE
)
143 || (fskip_string (ifp
), FALSE
)
144 || (fread (&line_num
, sizeof (line_num
), 1, ifp
) != 1))
152 if (fread (&addr
, sizeof (addr
), 1, ifp
) != 1
153 || fread (&ncalls
, sizeof (ncalls
), 1, ifp
) != 1)
160 /* Basic-block execution counts are meaningful only if we're
161 profiling at the line-by-line level: */
162 if (line_granularity
)
164 /* Convert from target to host endianness: */
165 addr
= get_vma (core_bfd
, (bfd_byte
*) & addr
);
166 ncalls
= bfd_get_32 (core_bfd
, (bfd_byte
*) &ncalls
);
168 sym
= sym_lookup (&symtab
, addr
);
175 printf ("[bb_read_rec] 0x%lx->0x%lx (%s:%d) cnt=%lu\n",
176 (unsigned long) addr
, (unsigned long) sym
->addr
,
177 sym
->name
, sym
->line_num
, ncalls
));
179 for (i
= 0; i
< NBBS
; i
++)
181 if (! sym
->bb_addr
[i
] || sym
->bb_addr
[i
] == addr
)
183 sym
->bb_addr
[i
] = addr
;
184 sym
->bb_calls
[i
] += ncalls
;
192 static bool user_warned
= FALSE
;
198 _("%s: warning: ignoring basic-block exec counts (use -l or --line)\n"),
206 /* Write all basic-blocks with non-zero counts to file OFP. FILENAME
207 is the name of OFP and is provided for producing error-messages
210 DEFUN (bb_write_blocks
, (ofp
, filename
), FILE * ofp AND
const char *filename
)
212 const unsigned char tag
= GMON_TAG_BB_COUNT
;
215 unsigned long ncalls
;
219 /* Count how many non-zero blocks with have: */
220 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
222 for (i
= 0; i
< NBBS
&& sym
->bb_addr
[i
]; i
++)
228 bfd_put_32 (core_bfd
, nblocks
, (bfd_byte
*) & nblocks
);
229 if (fwrite (&tag
, sizeof (tag
), 1, ofp
) != 1
230 || fwrite (&nblocks
, sizeof (nblocks
), 1, ofp
) != 1)
237 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
239 for (i
= 0; i
< NBBS
&& sym
->bb_addr
[i
]; i
++)
241 put_vma (core_bfd
, sym
->bb_addr
[i
], (bfd_byte
*) & addr
);
242 bfd_put_32 (core_bfd
, sym
->bb_calls
[i
], (bfd_byte
*) & ncalls
);
244 if (fwrite (&addr
, sizeof (addr
), 1, ofp
) != 1
245 || fwrite (&ncalls
, sizeof (ncalls
), 1, ofp
) != 1)
254 /* Output basic-block statistics in a format that is easily parseable.
255 Current the format is:
257 <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls> */
260 DEFUN_VOID (print_exec_counts
)
262 Sym
**sorted_bbs
, *sym
;
266 first_output
= FALSE
;
270 /* Sort basic-blocks according to function name and line number: */
271 sorted_bbs
= (Sym
**) xmalloc (symtab
.len
* sizeof (sorted_bbs
[0]));
274 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
276 /* Accept symbol if it's in the INCL_EXEC table
277 or there is no INCL_EXEC table
278 and it does not appear in the EXCL_EXEC table. */
279 if (sym_lookup (&syms
[INCL_EXEC
], sym
->addr
)
280 || (syms
[INCL_EXEC
].len
== 0
281 && !sym_lookup (&syms
[EXCL_EXEC
], sym
->addr
)))
283 sorted_bbs
[len
++] = sym
;
287 qsort (sorted_bbs
, len
, sizeof (sorted_bbs
[0]), cmp_bb
);
289 /* Output basic-blocks: */
291 for (i
= 0; i
< len
; ++i
)
293 if (sym
->ncalls
> 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
->addr
, sym
->ncalls
);
301 for (j
= 0; j
< NBBS
&& sym
->bb_addr
[j
]; j
++)
303 if (sym
->bb_calls
[j
] > 0 || ! ignore_zeros
)
305 /* FIXME: This only works if bfd_vma is unsigned long. */
306 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
307 sym
->file
? sym
->file
->name
: _("<unknown>"), sym
->line_num
,
308 sym
->name
, (unsigned long) sym
->bb_addr
[j
],
316 /* Helper for bb_annotated_source: format annotation containing
317 number of line executions. Depends on being called on each
318 line of a file in sequential order.
320 Global variable bb_annotate_all_lines enables execution count
321 compression (counts are supressed if identical to the last one)
322 and prints counts on all executed lines. Otherwise, print
323 all basic-block execution counts exactly once on the line
324 that starts the basic-block. */
327 DEFUN (annotate_with_count
, (buf
, width
, line_num
, arg
),
328 char *buf AND
int width AND
int line_num AND
void *arg
)
330 Source_File
*sf
= arg
;
333 static unsigned long last_count
;
334 unsigned long last_print
= (unsigned long) -1;
338 if (line_num
<= sf
->num_lines
)
339 b
= sf
->line
[line_num
- 1];
343 for (i
= 0; i
< width
; i
++)
349 char tmpbuf
[NBBS
* 30];
351 unsigned long ncalls
;
355 ++num_executable_lines
;
363 /* If this is a function entry point, label the line no matter what.
364 Otherwise, we're in the middle of a function, so check to see
365 if the first basic-block address is larger than the starting
366 address of the line. If so, then this line begins with a
367 a portion of the previous basic-block, so print that prior
368 execution count (if bb_annotate_all_lines is set). */
371 sprintf (p
, "%lu", b
->ncalls
);
373 last_count
= b
->ncalls
;
374 last_print
= last_count
;
378 else if (bb_annotate_all_lines
379 && b
->bb_addr
[0] && b
->bb_addr
[0] > b
->addr
)
381 sprintf (p
, "%lu", last_count
);
383 last_print
= last_count
;
388 /* Loop through all of this line's basic-blocks. For each one,
389 update last_count, then compress sequential identical counts
390 (if bb_annotate_all_lines) and print the execution count. */
392 for (i
= 0; i
< NBBS
&& b
->bb_addr
[i
]; i
++)
394 last_count
= b
->bb_calls
[i
];
400 ncalls
+= last_count
;
402 if (bb_annotate_all_lines
&& last_count
== last_print
)
407 sprintf (p
, "%lu", last_count
);
410 last_print
= last_count
;
413 /* We're done. If nothing has been printed on this line,
414 print the last execution count (bb_annotate_all_lines),
415 which could be from either a previous line (if there were
416 no BBs on this line), or from this line (if all our BB
417 counts were compressed out because they were identical). */
419 if (bb_annotate_all_lines
&& p
== tmpbuf
)
421 sprintf (p
, "%lu", last_count
);
431 for (c
= 0; c
< width
; c
++)
437 ++num_lines_executed
;
439 if (ncalls
< bb_min_calls
)
441 strcpy (tmpbuf
, "#####");
451 strncpy (buf
, tmpbuf
, width
);
458 strcpy (buf
+ width
- len
, tmpbuf
);
459 for (c
= 0; c
< width
- len
; ++c
)
465 /* Annotate the files named in SOURCE_FILES with basic-block statistics
466 (execution counts). After each source files, a few statistics
467 regarding that source file are printed. */
470 DEFUN_VOID (print_annotated_source
)
472 Sym
*sym
, *line_stats
, *new_line
;
477 /* Find maximum line number for each source file that user is
479 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
481 /* Accept symbol if it's file is known, its line number is
482 bigger than anything we have seen for that file so far and
483 if it's in the INCL_ANNO table or there is no INCL_ANNO
484 table and it does not appear in the EXCL_ANNO table. */
485 if (sym
->file
&& sym
->line_num
> sym
->file
->num_lines
486 && (sym_lookup (&syms
[INCL_ANNO
], sym
->addr
)
487 || (syms
[INCL_ANNO
].len
== 0
488 && !sym_lookup (&syms
[EXCL_ANNO
], sym
->addr
))))
490 sym
->file
->num_lines
= sym
->line_num
;
494 /* Allocate line descriptors: */
495 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
497 if (sf
->num_lines
> 0)
499 sf
->line
= (void *) xmalloc (sf
->num_lines
* sizeof (sf
->line
[0]));
500 memset (sf
->line
, 0, sf
->num_lines
* sizeof (sf
->line
[0]));
504 /* Count executions per line: */
505 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
507 if (sym
->file
&& sym
->file
->num_lines
508 && (sym_lookup (&syms
[INCL_ANNO
], sym
->addr
)
509 || (syms
[INCL_ANNO
].len
== 0
510 && !sym_lookup (&syms
[EXCL_ANNO
], sym
->addr
))))
512 sym
->file
->ncalls
+= sym
->ncalls
;
513 line_stats
= sym
->file
->line
[sym
->line_num
- 1];
517 /* Common case has at most one basic-block per source line: */
518 sym
->file
->line
[sym
->line_num
- 1] = sym
;
520 else if (!line_stats
->addr
)
522 /* sym is the 3rd .. nth basic block for this line: */
523 line_stats
->ncalls
+= sym
->ncalls
;
527 /* sym is the second basic block for this line. */
528 new_line
= (Sym
*) xmalloc (sizeof (*new_line
));
529 *new_line
= *line_stats
;
531 new_line
->ncalls
+= sym
->ncalls
;
532 sym
->file
->line
[sym
->line_num
- 1] = new_line
;
537 /* Plod over source files, annotating them: */
538 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
540 if (!sf
->num_lines
|| (ignore_zeros
&& sf
->ncalls
== 0))
543 num_executable_lines
= num_lines_executed
= 0;
545 ofp
= annotate_source (sf
, 16, annotate_with_count
, sf
);
549 if (bb_table_length
> 0)
551 fprintf (ofp
, _("\n\nTop %d Lines:\n\n Line Count\n\n"),
554 /* Abuse line arrays---it's not needed anymore: */
555 qsort (sf
->line
, sf
->num_lines
, sizeof (sf
->line
[0]), cmp_ncalls
);
556 table_len
= bb_table_length
;
558 if (table_len
> sf
->num_lines
)
559 table_len
= sf
->num_lines
;
561 for (i
= 0; i
< table_len
; ++i
)
565 if (!sym
|| sym
->ncalls
== 0)
568 fprintf (ofp
, "%9d %10lu\n", sym
->line_num
, sym
->ncalls
);
575 fprintf (ofp
, _("\nExecution Summary:\n\n"));
576 fprintf (ofp
, _("%9ld Executable lines in this file\n"),
577 num_executable_lines
);
578 fprintf (ofp
, _("%9ld Lines executed\n"), num_lines_executed
);
579 fprintf (ofp
, _("%9.2f Percent of the file executed\n"),
581 ? 100.0 * num_lines_executed
/ (double) num_executable_lines
583 fprintf (ofp
, _("\n%9lu Total number of line executions\n"),
585 fprintf (ofp
, _("%9.2f Average executions per line\n"),
587 ? (double) sf
->ncalls
/ (double) num_executable_lines
This page took 0.040911 seconds and 4 git commands to generate.