2 * Basic-block level related code: reading/writing of basic-block info
3 * to/from gmon.out; computing and formatting of basic-block related
8 #include "basic_blocks.h"
13 #include "libiberty.h"
19 * Default option values:
21 bool bb_annotate_all_lines
= FALSE
;
23 int bb_table_length
= 10;
26 * Variables used to compute annotated source listing stats:
28 static long num_executable_lines
;
29 static long num_lines_executed
;
33 * Helper for sorting. Compares two basic-blocks and returns result
34 * such that sorting will be increasing according to filename, line
35 * number, and basic-block address (in that order).
38 DEFUN (cmp_bb
, (lp
, rp
), const void *lp AND
const void *rp
)
41 const Sym
*left
= *(const Sym
**) lp
;
42 const Sym
*right
= *(const Sym
**) rp
;
44 if (left
->file
&& right
->file
)
46 r
= strcmp (left
->file
->name
, right
->file
->name
);
52 if (left
->line_num
!= right
->line_num
)
54 return left
->line_num
- right
->line_num
;
58 if (left
->addr
< right
->addr
)
62 else if (left
->addr
> right
->addr
)
74 * Helper for sorting. Order basic blocks in decreasing number of
75 * calls, ties are broken in increasing order of line numbers.
78 DEFUN (cmp_ncalls
, (lp
, rp
), const void *lp AND
const void *rp
)
80 const Sym
*left
= *(const Sym
**) lp
;
81 const Sym
*right
= *(const Sym
**) rp
;
92 if (right
->ncalls
!= left
->ncalls
)
94 return right
->ncalls
- left
->ncalls
;
97 return left
->line_num
- right
->line_num
;
102 * Skip over variable length string.
105 DEFUN (fskip_string
, (fp
), FILE * fp
)
109 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 DEFUN (bb_read_rec
, (ifp
, filename
), FILE * ifp AND
const char *filename
)
131 if (fread (&nblocks
, sizeof (nblocks
), 1, ifp
) != 1)
133 fprintf (stderr
, "%s: %s: unexpected end of file\n", whoami
, filename
);
137 nblocks
= bfd_get_32 (core_bfd
, (bfd_byte
*) & nblocks
);
138 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.
152 if ((fread (&ncalls
, sizeof (ncalls
), 1, ifp
) != 1)
153 || (fread (&addr
, sizeof (addr
), 1, ifp
) != 1)
154 || (fskip_string (ifp
), FALSE
)
155 || (fskip_string (ifp
), FALSE
)
156 || (fread (&line_num
, sizeof (line_num
), 1, ifp
) != 1))
164 if (fread (&addr
, sizeof (addr
), 1, ifp
) != 1
165 || fread (&ncalls
, sizeof (ncalls
), 1, ifp
) != 1)
173 * Basic-block execution counts are meaningful only if we're
174 * profiling at the line-by-line level:
176 if (line_granularity
)
179 /* convert from target to host endianness: */
181 addr
= get_vma (core_bfd
, (bfd_byte
*) & addr
);
183 sym
= sym_lookup (&symtab
, addr
);
184 sym
->is_bb_head
= TRUE
;
185 sym
->ncalls
+= bfd_get_32 (core_bfd
, (bfd_byte
*) & ncalls
);
187 DBG (BBDEBUG
, printf ("[bb_read_rec] 0x%lx->0x%lx (%s) cnt=%d\n",
188 addr
, sym
->addr
, sym
->name
, sym
->ncalls
));
192 static bool user_warned
= FALSE
;
198 "%s: warning: ignoring basic-block exec counts (use -l or --line)\n",
208 * Write all basic-blocks with non-zero counts to file OFP. FILENAME
209 * is the name of OFP and is provided for producing error-messages
213 DEFUN (bb_write_blocks
, (ofp
, filename
), FILE * ofp AND
const char *filename
)
215 const unsigned char tag
= GMON_TAG_BB_COUNT
;
221 /* count how many non-zero blocks with have: */
223 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
232 bfd_put_32 (core_bfd
, nblocks
, (bfd_byte
*) & nblocks
);
233 if (fwrite (&tag
, sizeof (tag
), 1, ofp
) != 1
234 || fwrite (&nblocks
, sizeof (nblocks
), 1, ofp
) != 1)
241 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
243 if (sym
->ncalls
== 0)
248 put_vma (core_bfd
, sym
->addr
, (bfd_byte
*) & addr
);
249 bfd_put_32 (core_bfd
, sym
->ncalls
, (bfd_byte
*) & ncalls
);
251 if (fwrite (&addr
, sizeof (addr
), 1, ofp
) != 1
252 || fwrite (&ncalls
, sizeof (ncalls
), 1, ofp
) != 1)
262 * Output basic-block statistics in a format that is easily parseable.
263 * Current the format is:
265 * <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls>
268 DEFUN_VOID (print_exec_counts
)
270 Sym
**sorted_bbs
, *sym
;
275 first_output
= FALSE
;
282 /* sort basic-blocks according to function name and line number: */
284 sorted_bbs
= (Sym
**) xmalloc (symtab
.len
* sizeof (sorted_bbs
[0]));
286 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
289 * Accept symbol if it's the start of a basic-block and it is
290 * called at least bb_min_calls times and if it's in the
291 * INCL_EXEC table or there is no INCL_EXEC table and it does
292 * not appear in the EXCL_EXEC table.
294 if (sym
->is_bb_head
&& sym
->ncalls
>= bb_min_calls
295 && (sym_lookup (&syms
[INCL_EXEC
], sym
->addr
)
296 || (syms
[INCL_EXEC
].len
== 0
297 && !sym_lookup (&syms
[EXCL_EXEC
], sym
->addr
))))
299 sorted_bbs
[len
++] = sym
;
302 qsort (sorted_bbs
, len
, sizeof (sorted_bbs
[0]), cmp_bb
);
304 /* output basic-blocks: */
306 for (i
= 0; i
< len
; ++i
)
309 printf ("%s:%d: (%s:0x%lx) %d executions\n",
310 sym
->file
? sym
->file
->name
: "<unknown>", sym
->line_num
,
311 sym
->name
, sym
->addr
, sym
->ncalls
);
318 * Helper for bb_annotated_source: format annotation containing
319 * number of line executions.
322 DEFUN (annotate_with_count
, (buf
, width
, line_num
, arg
),
323 char *buf AND
int width AND
int line_num AND
void *arg
)
325 Source_File
*sf
= arg
;
328 static long last_count
;
336 if (line_num
<= sf
->num_lines
)
338 b
= sf
->line
[line_num
- 1];
346 ++num_executable_lines
;
351 ++num_lines_executed
;
353 if (cnt
< 0 && bb_annotate_all_lines
)
360 strcpy (buf
, "\t\t");
362 else if (cnt
< bb_min_calls
)
364 strcpy (buf
, " ##### -> ");
368 sprintf (buf
, "%12ld -> ", cnt
);
375 * Annotate the files named in SOURCE_FILES with basic-block statistics
376 * (execution counts). After each source files, a few statistics
377 * regarding that source file are printed.
380 DEFUN_VOID (print_annotated_source
)
382 Sym
*sym
, *line_stats
, *new_line
;
388 * Find maximum line number for each source file that user is
391 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
394 * Accept symbol if it's file is known, its line number is
395 * bigger than anything we have seen for that file so far and
396 * if it's in the INCL_ANNO table or there is no INCL_ANNO
397 * table and it does not appear in the EXCL_ANNO table.
399 if (sym
->file
&& sym
->line_num
> sym
->file
->num_lines
400 && (sym_lookup (&syms
[INCL_ANNO
], sym
->addr
)
401 || (syms
[INCL_ANNO
].len
== 0
402 && !sym_lookup (&syms
[EXCL_ANNO
], sym
->addr
))))
404 sym
->file
->num_lines
= sym
->line_num
;
408 /* allocate line descriptors: */
410 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
412 if (sf
->num_lines
> 0)
414 sf
->line
= (void *) xmalloc (sf
->num_lines
* sizeof (sf
->line
[0]));
415 memset (sf
->line
, 0, sf
->num_lines
* sizeof (sf
->line
[0]));
419 /* count executions per line: */
421 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
423 if (sym
->is_bb_head
&& sym
->file
&& sym
->file
->num_lines
424 && (sym_lookup (&syms
[INCL_ANNO
], sym
->addr
)
425 || (syms
[INCL_ANNO
].len
== 0
426 && !sym_lookup (&syms
[EXCL_ANNO
], sym
->addr
))))
428 sym
->file
->ncalls
+= sym
->ncalls
;
429 line_stats
= sym
->file
->line
[sym
->line_num
- 1];
432 /* common case has at most one basic-block per source line: */
433 sym
->file
->line
[sym
->line_num
- 1] = sym
;
435 else if (!line_stats
->addr
)
437 /* sym is the 3rd .. nth basic block for this line: */
438 line_stats
->ncalls
+= sym
->ncalls
;
442 /* sym is the second basic block for this line */
443 new_line
= (Sym
*) xmalloc (sizeof (*new_line
));
444 *new_line
= *line_stats
;
446 new_line
->ncalls
+= sym
->ncalls
;
447 sym
->file
->line
[sym
->line_num
- 1] = new_line
;
452 /* plod over source files, annotating them: */
454 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
456 if (!sf
->num_lines
|| (ignore_zeros
&& sf
->ncalls
== 0))
461 num_executable_lines
= num_lines_executed
= 0;
462 ofp
= annotate_source (sf
, 16, annotate_with_count
, sf
);
468 if (bb_table_length
> 0)
470 fprintf (ofp
, "\n\nTop %d Lines:\n\n Line Count\n\n",
473 /* abuse line arrays---it's not needed anymore: */
474 qsort (sf
->line
, sf
->num_lines
, sizeof (sf
->line
[0]), cmp_ncalls
);
475 table_len
= bb_table_length
;
476 if (table_len
> sf
->num_lines
)
478 table_len
= sf
->num_lines
;
480 for (i
= 0; i
< table_len
; ++i
)
483 if (!sym
|| sym
->ncalls
<= 0)
487 fprintf (ofp
, "%9d %10d\n", sym
->line_num
, sym
->ncalls
);
494 fprintf (ofp
, "\nExecution Summary:\n\n");
495 fprintf (ofp
, "%9ld Executable lines in this file\n",
496 num_executable_lines
);
497 fprintf (ofp
, "%9ld Lines executed\n", num_lines_executed
);
498 fprintf (ofp
, "%9.2f Percent of the file executed\n",
500 ? 100.0 * num_lines_executed
/ (double) num_executable_lines
502 fprintf (ofp
, "\n%9d Total number of line executions\n", sf
->ncalls
);
503 fprintf (ofp
, "%9.2f Average executions per line\n",
505 ? sf
->ncalls
/ (double) num_executable_lines
This page took 0.038981 seconds and 4 git commands to generate.