]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gprof/basic_blocks.c
* config/sh/tm-sh.h (BELIEVE_PCC_PROMOTION): Define, so that
[thirdparty/binutils-gdb.git] / gprof / basic_blocks.c
CommitLineData
5489fcc3
KR
1/*
2 * Basic-block level related code: reading/writing of basic-block info
3 * to/from gmon.out; computing and formatting of basic-block related
4 * statistics.
5 */
6#include <stdio.h>
7#include <unistd.h>
8#include "basic_blocks.h"
43870aec 9#include "corefile.h"
5489fcc3
KR
10#include "gmon_io.h"
11#include "gmon_out.h"
12#include "gprof.h"
13#include "libiberty.h"
14#include "source.h"
15#include "sym_ids.h"
16
17
18/*
19 * Default option values:
20 */
21bool bb_annotate_all_lines = FALSE;
8c73afb3 22unsigned long bb_min_calls = 1;
5489fcc3
KR
23int bb_table_length = 10;
24
25/*
26 * Variables used to compute annotated source listing stats:
27 */
28static long num_executable_lines;
29static long num_lines_executed;
30
31
32/*
7862d7d0 33 * Helper for sorting. Compares two symbols and returns result
5489fcc3 34 * such that sorting will be increasing according to filename, line
7862d7d0 35 * number, and address (in that order).
5489fcc3 36 */
7862d7d0 37
5489fcc3 38static int
12516a37 39DEFUN (cmp_bb, (lp, rp), const void *lp AND const void *rp)
5489fcc3 40{
12516a37
KR
41 int r;
42 const Sym *left = *(const Sym **) lp;
43 const Sym *right = *(const Sym **) rp;
44
45 if (left->file && right->file)
46 {
47 r = strcmp (left->file->name, right->file->name);
48 if (r)
49 {
50 return r;
03c35bcb 51 }
12516a37
KR
52
53 if (left->line_num != right->line_num)
54 {
55 return left->line_num - right->line_num;
03c35bcb
KR
56 }
57 }
12516a37
KR
58
59 if (left->addr < right->addr)
60 {
61 return -1;
62 }
63 else if (left->addr > right->addr)
64 {
65 return 1;
66 }
67 else
68 {
69 return 0;
03c35bcb
KR
70 }
71}
5489fcc3
KR
72
73
74/*
75 * Helper for sorting. Order basic blocks in decreasing number of
76 * calls, ties are broken in increasing order of line numbers.
77 */
78static int
12516a37 79DEFUN (cmp_ncalls, (lp, rp), const void *lp AND const void *rp)
5489fcc3 80{
12516a37
KR
81 const Sym *left = *(const Sym **) lp;
82 const Sym *right = *(const Sym **) rp;
5489fcc3 83
12516a37
KR
84 if (!left)
85 {
86 return 1;
87 }
88 else if (!right)
89 {
90 return -1;
03c35bcb 91 }
5489fcc3 92
8c73afb3
ILT
93 if (left->ncalls < right->ncalls)
94 return 1;
95 else if (left->ncalls > right->ncalls)
96 return -1;
5489fcc3 97
12516a37 98 return left->line_num - right->line_num;
03c35bcb 99}
5489fcc3
KR
100
101
102/*
103 * Skip over variable length string.
104 */
105static void
12516a37 106DEFUN (fskip_string, (fp), FILE * fp)
5489fcc3 107{
12516a37 108 int ch;
5489fcc3 109
12516a37
KR
110 while ((ch = fgetc (fp)) != EOF)
111 {
112 if (ch == '\0')
113 {
114 break;
03c35bcb
KR
115 }
116 }
117}
5489fcc3
KR
118
119
120/*
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.
123 */
124void
12516a37 125DEFUN (bb_read_rec, (ifp, filename), FILE * ifp AND const char *filename)
5489fcc3 126{
12516a37
KR
127 int nblocks, b;
128 bfd_vma addr;
8c73afb3 129 unsigned long ncalls;
12516a37
KR
130 Sym *sym;
131
132 if (fread (&nblocks, sizeof (nblocks), 1, ifp) != 1)
133 {
16a02269 134 fprintf (stderr, _("%s: %s: unexpected end of file\n"), whoami, filename);
12516a37 135 done (1);
03c35bcb 136 }
12516a37
KR
137
138 nblocks = bfd_get_32 (core_bfd, (bfd_byte *) & nblocks);
139 if (gmon_file_version == 0)
140 {
141 fskip_string (ifp);
03c35bcb 142 }
12516a37
KR
143
144 for (b = 0; b < nblocks; ++b)
145 {
146 if (gmon_file_version == 0)
147 {
148 int line_num;
149 /*
150 * Version 0 had lots of extra stuff that we don't
151 * care about anymore.
152 */
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))
5489fcc3 158 {
12516a37
KR
159 perror (filename);
160 done (1);
03c35bcb 161 }
12516a37
KR
162 }
163 else
164 {
165 if (fread (&addr, sizeof (addr), 1, ifp) != 1
166 || fread (&ncalls, sizeof (ncalls), 1, ifp) != 1)
5489fcc3 167 {
12516a37
KR
168 perror (filename);
169 done (1);
03c35bcb
KR
170 }
171 }
12516a37
KR
172
173 /*
174 * Basic-block execution counts are meaningful only if we're
175 * profiling at the line-by-line level:
176 */
177 if (line_granularity)
178 {
5489fcc3 179
12516a37 180 /* convert from target to host endianness: */
5489fcc3 181
12516a37 182 addr = get_vma (core_bfd, (bfd_byte *) & addr);
7862d7d0 183 ncalls = bfd_get_32 (core_bfd, (bfd_byte *) &ncalls);
5489fcc3 184
12516a37 185 sym = sym_lookup (&symtab, addr);
5489fcc3 186
7862d7d0
ILT
187 if (sym)
188 {
189 int i;
190
191 DBG (BBDEBUG,
8c73afb3 192 printf ("[bb_read_rec] 0x%lx->0x%lx (%s:%d) cnt=%lu\n",
7862d7d0
ILT
193 addr, sym->addr, sym->name, sym->line_num, ncalls));
194
195 for (i = 0; i < NBBS; i++)
196 {
197 if (! sym->bb_addr[i] || sym->bb_addr[i] == addr)
198 {
199 sym->bb_addr[i] = addr;
200 sym->bb_calls[i] += ncalls;
201 break;
202 }
203 }
204 }
12516a37
KR
205 }
206 else
207 {
208 static bool user_warned = FALSE;
5489fcc3 209
12516a37
KR
210 if (!user_warned)
211 {
212 user_warned = TRUE;
213 fprintf (stderr,
16a02269 214 _("%s: warning: ignoring basic-block exec counts (use -l or --line)\n"),
12516a37 215 whoami);
03c35bcb
KR
216 }
217 }
218 }
12516a37 219 return;
03c35bcb 220}
5489fcc3
KR
221
222
223/*
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
226 * only.
227 */
228void
12516a37 229DEFUN (bb_write_blocks, (ofp, filename), FILE * ofp AND const char *filename)
5489fcc3 230{
12516a37
KR
231 const unsigned char tag = GMON_TAG_BB_COUNT;
232 int nblocks = 0;
233 bfd_vma addr;
8c73afb3 234 unsigned long ncalls;
12516a37 235 Sym *sym;
7862d7d0 236 int i;
12516a37
KR
237
238 /* count how many non-zero blocks with have: */
239
240 for (sym = symtab.base; sym < symtab.limit; ++sym)
5489fcc3 241 {
7862d7d0
ILT
242 for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
243 ;
244 nblocks += i;
03c35bcb 245 }
12516a37
KR
246
247 /* write header: */
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)
251 {
252 perror (filename);
253 done (1);
03c35bcb 254 }
5489fcc3 255
12516a37
KR
256 /* write counts: */
257 for (sym = symtab.base; sym < symtab.limit; ++sym)
258 {
7862d7d0 259 for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
12516a37 260 {
7862d7d0
ILT
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);
5489fcc3 263
7862d7d0
ILT
264 if (fwrite (&addr, sizeof (addr), 1, ofp) != 1
265 || fwrite (&ncalls, sizeof (ncalls), 1, ofp) != 1)
266 {
267 perror (filename);
268 done (1);
269 }
03c35bcb
KR
270 }
271 }
272}
5489fcc3
KR
273
274
275/*
276 * Output basic-block statistics in a format that is easily parseable.
277 * Current the format is:
278 *
12516a37 279 * <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls>
5489fcc3
KR
280 */
281void
12516a37 282DEFUN_VOID (print_exec_counts)
5489fcc3 283{
12516a37 284 Sym **sorted_bbs, *sym;
7862d7d0 285 int i, j, len;
12516a37
KR
286
287 if (first_output)
288 {
289 first_output = FALSE;
290 }
291 else
292 {
293 printf ("\f\n");
03c35bcb 294 }
12516a37
KR
295
296 /* sort basic-blocks according to function name and line number: */
297
298 sorted_bbs = (Sym **) xmalloc (symtab.len * sizeof (sorted_bbs[0]));
299 len = 0;
300 for (sym = symtab.base; sym < symtab.limit; ++sym)
301 {
302 /*
7862d7d0
ILT
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.
12516a37 306 */
7862d7d0
ILT
307 if (sym_lookup (&syms[INCL_EXEC], sym->addr)
308 || (syms[INCL_EXEC].len == 0
309 && !sym_lookup (&syms[EXCL_EXEC], sym->addr)))
5489fcc3 310 {
12516a37 311 sorted_bbs[len++] = sym;
03c35bcb
KR
312 }
313 }
12516a37 314 qsort (sorted_bbs, len, sizeof (sorted_bbs[0]), cmp_bb);
5489fcc3 315
12516a37 316 /* output basic-blocks: */
5489fcc3 317
12516a37
KR
318 for (i = 0; i < len; ++i)
319 {
7862d7d0
ILT
320 if (sym->ncalls > 0 || ! ignore_zeros)
321 {
8c73afb3 322 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
16a02269 323 sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
7862d7d0
ILT
324 sym->name, sym->addr, sym->ncalls);
325 }
326 for (j = 0; j < NBBS && sym->bb_addr[j]; j ++)
327 {
328 if (sym->bb_calls[j] > 0 || ! ignore_zeros)
329 {
8c73afb3 330 printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
16a02269 331 sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
7862d7d0
ILT
332 sym->name, sym->bb_addr[j], sym->bb_calls[j]);
333 }
334 }
03c35bcb 335 }
12516a37 336 free (sorted_bbs);
03c35bcb 337}
5489fcc3 338
5489fcc3
KR
339/*
340 * Helper for bb_annotated_source: format annotation containing
7862d7d0
ILT
341 * number of line executions. Depends on being called on each
342 * line of a file in sequential order.
343 *
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.
5489fcc3 349 */
7862d7d0 350
5489fcc3 351static void
12516a37
KR
352DEFUN (annotate_with_count, (buf, width, line_num, arg),
353 char *buf AND int width AND int line_num AND void *arg)
5489fcc3 354{
12516a37
KR
355 Source_File *sf = arg;
356 Sym *b;
7862d7d0 357 int i;
8c73afb3
ILT
358 static unsigned long last_count;
359 unsigned long last_print = (unsigned long) -1;
12516a37 360
7862d7d0 361 b = NULL;
12516a37
KR
362 if (line_num <= sf->num_lines)
363 {
364 b = sf->line[line_num - 1];
03c35bcb 365 }
12516a37
KR
366 if (!b)
367 {
7862d7d0
ILT
368 for (i = 0; i < width; i++)
369 buf[i] = ' ';
370 buf[width] = '\0';
12516a37
KR
371 }
372 else
373 {
7862d7d0
ILT
374 char tmpbuf[NBBS * 30];
375 char *p;
8c73afb3
ILT
376 unsigned long ncalls;
377 int ncalls_set;
7862d7d0
ILT
378 int len;
379
12516a37 380 ++num_executable_lines;
7862d7d0
ILT
381
382 p = tmpbuf;
383 *p = '\0';
384
8c73afb3
ILT
385 ncalls = 0;
386 ncalls_set = 0;
7862d7d0
ILT
387
388 /* If this is a function entry point, label the line no matter what.
389 * Otherwise, we're in the middle of a function, so check to see
390 * if the first basic-block address is larger than the starting
391 * address of the line. If so, then this line begins with a
392 * a portion of the previous basic-block, so print that prior
393 * execution count (if bb_annotate_all_lines is set).
394 */
395
396 if (b->is_func)
397 {
8c73afb3 398 sprintf (p, "%lu", b->ncalls);
7862d7d0
ILT
399 p += strlen (p);
400 last_count = b->ncalls;
401 last_print = last_count;
402 ncalls = b->ncalls;
8c73afb3 403 ncalls_set = 1;
7862d7d0
ILT
404 }
405 else if (bb_annotate_all_lines
406 && b->bb_addr[0] && b->bb_addr[0] > b->addr)
407 {
8c73afb3 408 sprintf (p, "%lu", last_count);
7862d7d0
ILT
409 p += strlen (p);
410 last_print = last_count;
411 ncalls = last_count;
8c73afb3 412 ncalls_set = 1;
7862d7d0
ILT
413 }
414
415 /* Loop through all of this line's basic-blocks. For each one,
416 * update last_count, then compress sequential identical counts
417 * (if bb_annotate_all_lines) and print the execution count.
418 */
419
420 for (i = 0; i < NBBS && b->bb_addr[i]; i++)
421 {
422 last_count = b->bb_calls[i];
8c73afb3
ILT
423 if (! ncalls_set)
424 {
425 ncalls = 0;
426 ncalls_set = 1;
427 }
7862d7d0
ILT
428 ncalls += last_count;
429
430 if (bb_annotate_all_lines && last_count == last_print)
431 {
432 continue;
433 }
434
435 if (p > tmpbuf)
436 *p++ = ',';
8c73afb3 437 sprintf (p, "%lu", last_count);
7862d7d0
ILT
438 p += strlen (p);
439
440 last_print = last_count;
441 }
442
443 /* We're done. If nothing has been printed on this line,
444 * print the last execution count (bb_annotate_all_lines),
445 * which could be from either a previous line (if there were
446 * no BBs on this line), or from this line (if all our BB
447 * counts were compressed out because they were identical).
448 */
449
450 if (bb_annotate_all_lines && p == tmpbuf)
451 {
8c73afb3 452 sprintf (p, "%lu", last_count);
7862d7d0
ILT
453 p += strlen (p);
454 ncalls = last_count;
8c73afb3 455 ncalls_set = 1;
7862d7d0
ILT
456 }
457
8c73afb3 458 if (! ncalls_set)
7862d7d0
ILT
459 {
460 int c;
461
462 for (c = 0; c < width; c++)
463 buf[c] = ' ';
464 buf[width] = '\0';
465 return;
466 }
467
12516a37 468 ++num_lines_executed;
12516a37 469
7862d7d0
ILT
470 if (ncalls < bb_min_calls)
471 {
472 strcpy (tmpbuf, "#####");
473 p = tmpbuf + 5;
474 }
475
476 strcpy (p, " -> ");
477 p += 4;
478
479 len = p - tmpbuf;
480 if (len >= width)
481 {
482 strncpy (buf, tmpbuf, width);
483 buf[width] = '\0';
484 }
485 else
486 {
487 int c;
488
489 strcpy (buf + width - len, tmpbuf);
490 for (c = 0; c < width - len; ++c)
491 buf[c] = ' ';
492 }
03c35bcb 493 }
03c35bcb 494}
5489fcc3 495
5489fcc3
KR
496/*
497 * Annotate the files named in SOURCE_FILES with basic-block statistics
498 * (execution counts). After each source files, a few statistics
499 * regarding that source file are printed.
500 */
501void
12516a37 502DEFUN_VOID (print_annotated_source)
5489fcc3 503{
12516a37
KR
504 Sym *sym, *line_stats, *new_line;
505 Source_File *sf;
506 int i, table_len;
507 FILE *ofp;
508
509 /*
510 * Find maximum line number for each source file that user is
511 * interested in:
512 */
513 for (sym = symtab.base; sym < symtab.limit; ++sym)
514 {
515 /*
516 * Accept symbol if it's file is known, its line number is
517 * bigger than anything we have seen for that file so far and
518 * if it's in the INCL_ANNO table or there is no INCL_ANNO
519 * table and it does not appear in the EXCL_ANNO table.
520 */
521 if (sym->file && sym->line_num > sym->file->num_lines
522 && (sym_lookup (&syms[INCL_ANNO], sym->addr)
523 || (syms[INCL_ANNO].len == 0
524 && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
525 {
526 sym->file->num_lines = sym->line_num;
03c35bcb
KR
527 }
528 }
12516a37
KR
529
530 /* allocate line descriptors: */
531
532 for (sf = first_src_file; sf; sf = sf->next)
533 {
534 if (sf->num_lines > 0)
535 {
536 sf->line = (void *) xmalloc (sf->num_lines * sizeof (sf->line[0]));
537 memset (sf->line, 0, sf->num_lines * sizeof (sf->line[0]));
03c35bcb
KR
538 }
539 }
12516a37
KR
540
541 /* count executions per line: */
542
543 for (sym = symtab.base; sym < symtab.limit; ++sym)
544 {
7862d7d0 545 if (sym->file && sym->file->num_lines
12516a37
KR
546 && (sym_lookup (&syms[INCL_ANNO], sym->addr)
547 || (syms[INCL_ANNO].len == 0
548 && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
549 {
550 sym->file->ncalls += sym->ncalls;
551 line_stats = sym->file->line[sym->line_num - 1];
552 if (!line_stats)
553 {
554 /* common case has at most one basic-block per source line: */
555 sym->file->line[sym->line_num - 1] = sym;
556 }
557 else if (!line_stats->addr)
558 {
559 /* sym is the 3rd .. nth basic block for this line: */
560 line_stats->ncalls += sym->ncalls;
561 }
562 else
563 {
564 /* sym is the second basic block for this line */
565 new_line = (Sym *) xmalloc (sizeof (*new_line));
566 *new_line = *line_stats;
567 new_line->addr = 0;
568 new_line->ncalls += sym->ncalls;
569 sym->file->line[sym->line_num - 1] = new_line;
03c35bcb
KR
570 }
571 }
572 }
12516a37
KR
573
574 /* plod over source files, annotating them: */
575
576 for (sf = first_src_file; sf; sf = sf->next)
577 {
578 if (!sf->num_lines || (ignore_zeros && sf->ncalls == 0))
5489fcc3 579 {
12516a37 580 continue;
03c35bcb 581 }
12516a37
KR
582
583 num_executable_lines = num_lines_executed = 0;
584 ofp = annotate_source (sf, 16, annotate_with_count, sf);
585 if (!ofp)
5489fcc3 586 {
12516a37 587 continue;
03c35bcb 588 }
12516a37
KR
589
590 if (bb_table_length > 0)
591 {
16a02269 592 fprintf (ofp, _("\n\nTop %d Lines:\n\n Line Count\n\n"),
12516a37
KR
593 bb_table_length);
594
595 /* abuse line arrays---it's not needed anymore: */
596 qsort (sf->line, sf->num_lines, sizeof (sf->line[0]), cmp_ncalls);
597 table_len = bb_table_length;
598 if (table_len > sf->num_lines)
599 {
600 table_len = sf->num_lines;
03c35bcb 601 }
12516a37
KR
602 for (i = 0; i < table_len; ++i)
603 {
604 sym = sf->line[i];
8c73afb3 605 if (!sym || sym->ncalls == 0)
12516a37
KR
606 {
607 break;
03c35bcb 608 }
8c73afb3 609 fprintf (ofp, "%9d %10lu\n", sym->line_num, sym->ncalls);
03c35bcb
KR
610 }
611 }
12516a37
KR
612
613 free (sf->line);
614 sf->line = 0;
615
16a02269
TT
616 fprintf (ofp, _("\nExecution Summary:\n\n"));
617 fprintf (ofp, _("%9ld Executable lines in this file\n"),
12516a37 618 num_executable_lines);
16a02269
TT
619 fprintf (ofp, _("%9ld Lines executed\n"), num_lines_executed);
620 fprintf (ofp, _("%9.2f Percent of the file executed\n"),
12516a37
KR
621 num_executable_lines
622 ? 100.0 * num_lines_executed / (double) num_executable_lines
623 : 100.0);
8c73afb3
ILT
624 fprintf (ofp, _("\n%9lu Total number of line executions\n"),
625 sf->ncalls);
16a02269 626 fprintf (ofp, _("%9.2f Average executions per line\n"),
12516a37 627 num_executable_lines
8c73afb3 628 ? (double) sf->ncalls / (double) num_executable_lines
12516a37
KR
629 : 0.0);
630 if (ofp != stdout)
631 {
632 fclose (ofp);
03c35bcb
KR
633 }
634 }
635}