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 symbols and returns result
34 * such that sorting will be increasing according to filename, line
35 * number, and address (in that order).
39 DEFUN (cmp_bb
, (lp
, rp
), const void *lp AND
const void *rp
)
42 const Sym
*left
= *(const Sym
**) lp
;
43 const Sym
*right
= *(const Sym
**) rp
;
45 if (left
->file
&& right
->file
)
47 r
= strcmp (left
->file
->name
, right
->file
->name
);
53 if (left
->line_num
!= right
->line_num
)
55 return left
->line_num
- right
->line_num
;
59 if (left
->addr
< right
->addr
)
63 else if (left
->addr
> right
->addr
)
75 * Helper for sorting. Order basic blocks in decreasing number of
76 * 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
;
93 if (right
->ncalls
!= left
->ncalls
)
95 return right
->ncalls
- left
->ncalls
;
98 return left
->line_num
- right
->line_num
;
103 * Skip over variable length string.
106 DEFUN (fskip_string
, (fp
), FILE * fp
)
110 while ((ch
= fgetc (fp
)) != EOF
)
121 * Read a basic-block record from file IFP. FILENAME is the name
122 * of file IFP and is provided for formatting error-messages only.
125 DEFUN (bb_read_rec
, (ifp
, filename
), FILE * ifp AND
const char *filename
)
132 if (fread (&nblocks
, sizeof (nblocks
), 1, ifp
) != 1)
134 fprintf (stderr
, "%s: %s: unexpected end of file\n", whoami
, filename
);
138 nblocks
= bfd_get_32 (core_bfd
, (bfd_byte
*) & nblocks
);
139 if (gmon_file_version
== 0)
144 for (b
= 0; b
< nblocks
; ++b
)
146 if (gmon_file_version
== 0)
150 * Version 0 had lots of extra stuff that we don't
151 * care about anymore.
153 if ((fread (&ncalls
, sizeof (ncalls
), 1, ifp
) != 1)
154 || (fread (&addr
, sizeof (addr
), 1, ifp
) != 1)
155 || (fskip_string (ifp
), FALSE
)
156 || (fskip_string (ifp
), FALSE
)
157 || (fread (&line_num
, sizeof (line_num
), 1, ifp
) != 1))
165 if (fread (&addr
, sizeof (addr
), 1, ifp
) != 1
166 || fread (&ncalls
, sizeof (ncalls
), 1, ifp
) != 1)
174 * Basic-block execution counts are meaningful only if we're
175 * profiling at the line-by-line level:
177 if (line_granularity
)
180 /* convert from target to host endianness: */
182 addr
= get_vma (core_bfd
, (bfd_byte
*) & addr
);
183 ncalls
= bfd_get_32 (core_bfd
, (bfd_byte
*) &ncalls
);
185 sym
= sym_lookup (&symtab
, addr
);
192 printf ("[bb_read_rec] 0x%lx->0x%lx (%s:%d) cnt=%ld\n",
193 addr
, sym
->addr
, sym
->name
, sym
->line_num
, ncalls
));
195 for (i
= 0; i
< NBBS
; i
++)
197 if (! sym
->bb_addr
[i
] || sym
->bb_addr
[i
] == addr
)
199 sym
->bb_addr
[i
] = addr
;
200 sym
->bb_calls
[i
] += ncalls
;
208 static bool user_warned
= FALSE
;
214 "%s: warning: ignoring basic-block exec counts (use -l or --line)\n",
224 * Write all basic-blocks with non-zero counts to file OFP. FILENAME
225 * is the name of OFP and is provided for producing error-messages
229 DEFUN (bb_write_blocks
, (ofp
, filename
), FILE * ofp AND
const char *filename
)
231 const unsigned char tag
= GMON_TAG_BB_COUNT
;
238 /* count how many non-zero blocks with have: */
240 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
242 for (i
= 0; i
< NBBS
&& sym
->bb_addr
[i
]; i
++)
248 bfd_put_32 (core_bfd
, nblocks
, (bfd_byte
*) & nblocks
);
249 if (fwrite (&tag
, sizeof (tag
), 1, ofp
) != 1
250 || fwrite (&nblocks
, sizeof (nblocks
), 1, ofp
) != 1)
257 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
259 for (i
= 0; i
< NBBS
&& sym
->bb_addr
[i
]; i
++)
261 put_vma (core_bfd
, sym
->bb_addr
[i
], (bfd_byte
*) & addr
);
262 bfd_put_32 (core_bfd
, sym
->bb_calls
[i
], (bfd_byte
*) & ncalls
);
264 if (fwrite (&addr
, sizeof (addr
), 1, ofp
) != 1
265 || fwrite (&ncalls
, sizeof (ncalls
), 1, ofp
) != 1)
276 * Output basic-block statistics in a format that is easily parseable.
277 * Current the format is:
279 * <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls>
282 DEFUN_VOID (print_exec_counts
)
284 Sym
**sorted_bbs
, *sym
;
289 first_output
= FALSE
;
296 /* sort basic-blocks according to function name and line number: */
298 sorted_bbs
= (Sym
**) xmalloc (symtab
.len
* sizeof (sorted_bbs
[0]));
300 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
303 * Accept symbol if it's in the INCL_EXEC table
304 * or there is no INCL_EXEC table
305 * and it does not appear in the EXCL_EXEC table.
307 if (sym_lookup (&syms
[INCL_EXEC
], sym
->addr
)
308 || (syms
[INCL_EXEC
].len
== 0
309 && !sym_lookup (&syms
[EXCL_EXEC
], sym
->addr
)))
311 sorted_bbs
[len
++] = sym
;
314 qsort (sorted_bbs
, len
, sizeof (sorted_bbs
[0]), cmp_bb
);
316 /* output basic-blocks: */
318 for (i
= 0; i
< len
; ++i
)
320 if (sym
->ncalls
> 0 || ! ignore_zeros
)
322 printf ("%s:%d: (%s:0x%lx) %d executions\n",
323 sym
->file
? sym
->file
->name
: "<unknown>", sym
->line_num
,
324 sym
->name
, sym
->addr
, sym
->ncalls
);
326 for (j
= 0; j
< NBBS
&& sym
->bb_addr
[j
]; j
++)
328 if (sym
->bb_calls
[j
] > 0 || ! ignore_zeros
)
330 printf ("%s:%d: (%s:0x%lx) %d executions\n",
331 sym
->file
? sym
->file
->name
: "<unknown>", sym
->line_num
,
332 sym
->name
, sym
->bb_addr
[j
], sym
->bb_calls
[j
]);
340 * Helper for bb_annotated_source: format annotation containing
341 * number of line executions. Depends on being called on each
342 * line of a file in sequential order.
344 * Global variable bb_annotate_all_lines enables execution count
345 * compression (counts are supressed if identical to the last one)
346 * and prints counts on all executed lines. Otherwise, print
347 * all basic-block execution counts exactly once on the line
348 * that starts the basic-block.
352 DEFUN (annotate_with_count
, (buf
, width
, line_num
, arg
),
353 char *buf AND
int width AND
int line_num AND
void *arg
)
355 Source_File
*sf
= arg
;
358 static int last_count
;
362 if (line_num
<= sf
->num_lines
)
364 b
= sf
->line
[line_num
- 1];
368 for (i
= 0; i
< width
; i
++)
374 char tmpbuf
[NBBS
* 30];
379 ++num_executable_lines
;
386 /* If this is a function entry point, label the line no matter what.
387 * Otherwise, we're in the middle of a function, so check to see
388 * if the first basic-block address is larger than the starting
389 * address of the line. If so, then this line begins with a
390 * a portion of the previous basic-block, so print that prior
391 * execution count (if bb_annotate_all_lines is set).
396 sprintf (p
, "%d", b
->ncalls
);
398 last_count
= b
->ncalls
;
399 last_print
= last_count
;
402 else if (bb_annotate_all_lines
403 && b
->bb_addr
[0] && b
->bb_addr
[0] > b
->addr
)
405 sprintf (p
, "%d", last_count
);
407 last_print
= last_count
;
411 /* Loop through all of this line's basic-blocks. For each one,
412 * update last_count, then compress sequential identical counts
413 * (if bb_annotate_all_lines) and print the execution count.
416 for (i
= 0; i
< NBBS
&& b
->bb_addr
[i
]; i
++)
418 last_count
= b
->bb_calls
[i
];
421 ncalls
+= last_count
;
423 if (bb_annotate_all_lines
&& last_count
== last_print
)
430 sprintf (p
, "%d", last_count
);
433 last_print
= last_count
;
436 /* We're done. If nothing has been printed on this line,
437 * print the last execution count (bb_annotate_all_lines),
438 * which could be from either a previous line (if there were
439 * no BBs on this line), or from this line (if all our BB
440 * counts were compressed out because they were identical).
443 if (bb_annotate_all_lines
&& p
== tmpbuf
)
445 sprintf (p
, "%d", last_count
);
454 for (c
= 0; c
< width
; c
++)
460 ++num_lines_executed
;
462 if (ncalls
< bb_min_calls
)
464 strcpy (tmpbuf
, "#####");
474 strncpy (buf
, tmpbuf
, width
);
481 strcpy (buf
+ width
- len
, tmpbuf
);
482 for (c
= 0; c
< width
- len
; ++c
)
489 * Annotate the files named in SOURCE_FILES with basic-block statistics
490 * (execution counts). After each source files, a few statistics
491 * regarding that source file are printed.
494 DEFUN_VOID (print_annotated_source
)
496 Sym
*sym
, *line_stats
, *new_line
;
502 * Find maximum line number for each source file that user is
505 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
508 * Accept symbol if it's file is known, its line number is
509 * bigger than anything we have seen for that file so far and
510 * if it's in the INCL_ANNO table or there is no INCL_ANNO
511 * table and it does not appear in the EXCL_ANNO table.
513 if (sym
->file
&& sym
->line_num
> sym
->file
->num_lines
514 && (sym_lookup (&syms
[INCL_ANNO
], sym
->addr
)
515 || (syms
[INCL_ANNO
].len
== 0
516 && !sym_lookup (&syms
[EXCL_ANNO
], sym
->addr
))))
518 sym
->file
->num_lines
= sym
->line_num
;
522 /* allocate line descriptors: */
524 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
526 if (sf
->num_lines
> 0)
528 sf
->line
= (void *) xmalloc (sf
->num_lines
* sizeof (sf
->line
[0]));
529 memset (sf
->line
, 0, sf
->num_lines
* sizeof (sf
->line
[0]));
533 /* count executions per line: */
535 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
537 if (sym
->file
&& sym
->file
->num_lines
538 && (sym_lookup (&syms
[INCL_ANNO
], sym
->addr
)
539 || (syms
[INCL_ANNO
].len
== 0
540 && !sym_lookup (&syms
[EXCL_ANNO
], sym
->addr
))))
542 sym
->file
->ncalls
+= sym
->ncalls
;
543 line_stats
= sym
->file
->line
[sym
->line_num
- 1];
546 /* common case has at most one basic-block per source line: */
547 sym
->file
->line
[sym
->line_num
- 1] = sym
;
549 else if (!line_stats
->addr
)
551 /* sym is the 3rd .. nth basic block for this line: */
552 line_stats
->ncalls
+= sym
->ncalls
;
556 /* sym is the second basic block for this line */
557 new_line
= (Sym
*) xmalloc (sizeof (*new_line
));
558 *new_line
= *line_stats
;
560 new_line
->ncalls
+= sym
->ncalls
;
561 sym
->file
->line
[sym
->line_num
- 1] = new_line
;
566 /* plod over source files, annotating them: */
568 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
570 if (!sf
->num_lines
|| (ignore_zeros
&& sf
->ncalls
== 0))
575 num_executable_lines
= num_lines_executed
= 0;
576 ofp
= annotate_source (sf
, 16, annotate_with_count
, sf
);
582 if (bb_table_length
> 0)
584 fprintf (ofp
, "\n\nTop %d Lines:\n\n Line Count\n\n",
587 /* abuse line arrays---it's not needed anymore: */
588 qsort (sf
->line
, sf
->num_lines
, sizeof (sf
->line
[0]), cmp_ncalls
);
589 table_len
= bb_table_length
;
590 if (table_len
> sf
->num_lines
)
592 table_len
= sf
->num_lines
;
594 for (i
= 0; i
< table_len
; ++i
)
597 if (!sym
|| sym
->ncalls
<= 0)
601 fprintf (ofp
, "%9d %10d\n", sym
->line_num
, sym
->ncalls
);
608 fprintf (ofp
, "\nExecution Summary:\n\n");
609 fprintf (ofp
, "%9ld Executable lines in this file\n",
610 num_executable_lines
);
611 fprintf (ofp
, "%9ld Lines executed\n", num_lines_executed
);
612 fprintf (ofp
, "%9.2f Percent of the file executed\n",
614 ? 100.0 * num_lines_executed
/ (double) num_executable_lines
616 fprintf (ofp
, "\n%9d Total number of line executions\n", sf
->ncalls
);
617 fprintf (ofp
, "%9.2f Average executions per line\n",
619 ? sf
->ncalls
/ (double) num_executable_lines
This page took 0.05161 seconds and 4 git commands to generate.