* gprof.h: Added includes and defines for gettext.
[deliverable/binutils-gdb.git] / gprof / basic_blocks.c
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"
9 #include "core.h"
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 */
21 bool bb_annotate_all_lines = FALSE;
22 int bb_min_calls = 1;
23 int bb_table_length = 10;
24
25 /*
26 * Variables used to compute annotated source listing stats:
27 */
28 static long num_executable_lines;
29 static long num_lines_executed;
30
31
32 /*
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).
36 */
37
38 static int
39 DEFUN (cmp_bb, (lp, rp), const void *lp AND const void *rp)
40 {
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;
51 }
52
53 if (left->line_num != right->line_num)
54 {
55 return left->line_num - right->line_num;
56 }
57 }
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;
70 }
71 }
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 */
78 static int
79 DEFUN (cmp_ncalls, (lp, rp), const void *lp AND const void *rp)
80 {
81 const Sym *left = *(const Sym **) lp;
82 const Sym *right = *(const Sym **) rp;
83
84 if (!left)
85 {
86 return 1;
87 }
88 else if (!right)
89 {
90 return -1;
91 }
92
93 if (right->ncalls != left->ncalls)
94 {
95 return right->ncalls - left->ncalls;
96 }
97
98 return left->line_num - right->line_num;
99 }
100
101
102 /*
103 * Skip over variable length string.
104 */
105 static void
106 DEFUN (fskip_string, (fp), FILE * fp)
107 {
108 int ch;
109
110 while ((ch = fgetc (fp)) != EOF)
111 {
112 if (ch == '\0')
113 {
114 break;
115 }
116 }
117 }
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 */
124 void
125 DEFUN (bb_read_rec, (ifp, filename), FILE * ifp AND const char *filename)
126 {
127 int nblocks, b;
128 bfd_vma addr;
129 long ncalls;
130 Sym *sym;
131
132 if (fread (&nblocks, sizeof (nblocks), 1, ifp) != 1)
133 {
134 fprintf (stderr, _("%s: %s: unexpected end of file\n"), whoami, filename);
135 done (1);
136 }
137
138 nblocks = bfd_get_32 (core_bfd, (bfd_byte *) & nblocks);
139 if (gmon_file_version == 0)
140 {
141 fskip_string (ifp);
142 }
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))
158 {
159 perror (filename);
160 done (1);
161 }
162 }
163 else
164 {
165 if (fread (&addr, sizeof (addr), 1, ifp) != 1
166 || fread (&ncalls, sizeof (ncalls), 1, ifp) != 1)
167 {
168 perror (filename);
169 done (1);
170 }
171 }
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 {
179
180 /* convert from target to host endianness: */
181
182 addr = get_vma (core_bfd, (bfd_byte *) & addr);
183 ncalls = bfd_get_32 (core_bfd, (bfd_byte *) &ncalls);
184
185 sym = sym_lookup (&symtab, addr);
186
187 if (sym)
188 {
189 int i;
190
191 DBG (BBDEBUG,
192 printf ("[bb_read_rec] 0x%lx->0x%lx (%s:%d) cnt=%ld\n",
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 }
205 }
206 else
207 {
208 static bool user_warned = FALSE;
209
210 if (!user_warned)
211 {
212 user_warned = TRUE;
213 fprintf (stderr,
214 _("%s: warning: ignoring basic-block exec counts (use -l or --line)\n"),
215 whoami);
216 }
217 }
218 }
219 return;
220 }
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 */
228 void
229 DEFUN (bb_write_blocks, (ofp, filename), FILE * ofp AND const char *filename)
230 {
231 const unsigned char tag = GMON_TAG_BB_COUNT;
232 int nblocks = 0;
233 bfd_vma addr;
234 long ncalls;
235 Sym *sym;
236 int i;
237
238 /* count how many non-zero blocks with have: */
239
240 for (sym = symtab.base; sym < symtab.limit; ++sym)
241 {
242 for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
243 ;
244 nblocks += i;
245 }
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);
254 }
255
256 /* write counts: */
257 for (sym = symtab.base; sym < symtab.limit; ++sym)
258 {
259 for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
260 {
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);
263
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 }
270 }
271 }
272 }
273
274
275 /*
276 * Output basic-block statistics in a format that is easily parseable.
277 * Current the format is:
278 *
279 * <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls>
280 */
281 void
282 DEFUN_VOID (print_exec_counts)
283 {
284 Sym **sorted_bbs, *sym;
285 int i, j, len;
286
287 if (first_output)
288 {
289 first_output = FALSE;
290 }
291 else
292 {
293 printf ("\f\n");
294 }
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 /*
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.
306 */
307 if (sym_lookup (&syms[INCL_EXEC], sym->addr)
308 || (syms[INCL_EXEC].len == 0
309 && !sym_lookup (&syms[EXCL_EXEC], sym->addr)))
310 {
311 sorted_bbs[len++] = sym;
312 }
313 }
314 qsort (sorted_bbs, len, sizeof (sorted_bbs[0]), cmp_bb);
315
316 /* output basic-blocks: */
317
318 for (i = 0; i < len; ++i)
319 {
320 if (sym->ncalls > 0 || ! ignore_zeros)
321 {
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);
325 }
326 for (j = 0; j < NBBS && sym->bb_addr[j]; j ++)
327 {
328 if (sym->bb_calls[j] > 0 || ! ignore_zeros)
329 {
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]);
333 }
334 }
335 }
336 free (sorted_bbs);
337 }
338
339 /*
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.
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.
349 */
350
351 static void
352 DEFUN (annotate_with_count, (buf, width, line_num, arg),
353 char *buf AND int width AND int line_num AND void *arg)
354 {
355 Source_File *sf = arg;
356 Sym *b;
357 int i;
358 static int last_count;
359 int last_print=-1;
360
361 b = NULL;
362 if (line_num <= sf->num_lines)
363 {
364 b = sf->line[line_num - 1];
365 }
366 if (!b)
367 {
368 for (i = 0; i < width; i++)
369 buf[i] = ' ';
370 buf[width] = '\0';
371 }
372 else
373 {
374 char tmpbuf[NBBS * 30];
375 char *p;
376 int ncalls;
377 int len;
378
379 ++num_executable_lines;
380
381 p = tmpbuf;
382 *p = '\0';
383
384 ncalls = -1;
385
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).
392 */
393
394 if (b->is_func)
395 {
396 sprintf (p, "%d", b->ncalls);
397 p += strlen (p);
398 last_count = b->ncalls;
399 last_print = last_count;
400 ncalls = b->ncalls;
401 }
402 else if (bb_annotate_all_lines
403 && b->bb_addr[0] && b->bb_addr[0] > b->addr)
404 {
405 sprintf (p, "%d", last_count);
406 p += strlen (p);
407 last_print = last_count;
408 ncalls = last_count;
409 }
410
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.
414 */
415
416 for (i = 0; i < NBBS && b->bb_addr[i]; i++)
417 {
418 last_count = b->bb_calls[i];
419 if (ncalls < 0)
420 ncalls = 0;
421 ncalls += last_count;
422
423 if (bb_annotate_all_lines && last_count == last_print)
424 {
425 continue;
426 }
427
428 if (p > tmpbuf)
429 *p++ = ',';
430 sprintf (p, "%d", last_count);
431 p += strlen (p);
432
433 last_print = last_count;
434 }
435
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).
441 */
442
443 if (bb_annotate_all_lines && p == tmpbuf)
444 {
445 sprintf (p, "%d", last_count);
446 p += strlen (p);
447 ncalls = last_count;
448 }
449
450 if (ncalls < 0)
451 {
452 int c;
453
454 for (c = 0; c < width; c++)
455 buf[c] = ' ';
456 buf[width] = '\0';
457 return;
458 }
459
460 ++num_lines_executed;
461
462 if (ncalls < bb_min_calls)
463 {
464 strcpy (tmpbuf, "#####");
465 p = tmpbuf + 5;
466 }
467
468 strcpy (p, " -> ");
469 p += 4;
470
471 len = p - tmpbuf;
472 if (len >= width)
473 {
474 strncpy (buf, tmpbuf, width);
475 buf[width] = '\0';
476 }
477 else
478 {
479 int c;
480
481 strcpy (buf + width - len, tmpbuf);
482 for (c = 0; c < width - len; ++c)
483 buf[c] = ' ';
484 }
485 }
486 }
487
488 /*
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.
492 */
493 void
494 DEFUN_VOID (print_annotated_source)
495 {
496 Sym *sym, *line_stats, *new_line;
497 Source_File *sf;
498 int i, table_len;
499 FILE *ofp;
500
501 /*
502 * Find maximum line number for each source file that user is
503 * interested in:
504 */
505 for (sym = symtab.base; sym < symtab.limit; ++sym)
506 {
507 /*
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.
512 */
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))))
517 {
518 sym->file->num_lines = sym->line_num;
519 }
520 }
521
522 /* allocate line descriptors: */
523
524 for (sf = first_src_file; sf; sf = sf->next)
525 {
526 if (sf->num_lines > 0)
527 {
528 sf->line = (void *) xmalloc (sf->num_lines * sizeof (sf->line[0]));
529 memset (sf->line, 0, sf->num_lines * sizeof (sf->line[0]));
530 }
531 }
532
533 /* count executions per line: */
534
535 for (sym = symtab.base; sym < symtab.limit; ++sym)
536 {
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))))
541 {
542 sym->file->ncalls += sym->ncalls;
543 line_stats = sym->file->line[sym->line_num - 1];
544 if (!line_stats)
545 {
546 /* common case has at most one basic-block per source line: */
547 sym->file->line[sym->line_num - 1] = sym;
548 }
549 else if (!line_stats->addr)
550 {
551 /* sym is the 3rd .. nth basic block for this line: */
552 line_stats->ncalls += sym->ncalls;
553 }
554 else
555 {
556 /* sym is the second basic block for this line */
557 new_line = (Sym *) xmalloc (sizeof (*new_line));
558 *new_line = *line_stats;
559 new_line->addr = 0;
560 new_line->ncalls += sym->ncalls;
561 sym->file->line[sym->line_num - 1] = new_line;
562 }
563 }
564 }
565
566 /* plod over source files, annotating them: */
567
568 for (sf = first_src_file; sf; sf = sf->next)
569 {
570 if (!sf->num_lines || (ignore_zeros && sf->ncalls == 0))
571 {
572 continue;
573 }
574
575 num_executable_lines = num_lines_executed = 0;
576 ofp = annotate_source (sf, 16, annotate_with_count, sf);
577 if (!ofp)
578 {
579 continue;
580 }
581
582 if (bb_table_length > 0)
583 {
584 fprintf (ofp, _("\n\nTop %d Lines:\n\n Line Count\n\n"),
585 bb_table_length);
586
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)
591 {
592 table_len = sf->num_lines;
593 }
594 for (i = 0; i < table_len; ++i)
595 {
596 sym = sf->line[i];
597 if (!sym || sym->ncalls <= 0)
598 {
599 break;
600 }
601 fprintf (ofp, "%9d %10d\n", sym->line_num, sym->ncalls);
602 }
603 }
604
605 free (sf->line);
606 sf->line = 0;
607
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"),
613 num_executable_lines
614 ? 100.0 * num_lines_executed / (double) num_executable_lines
615 : 100.0);
616 fprintf (ofp, _("\n%9d Total number of line executions\n"), sf->ncalls);
617 fprintf (ofp, _("%9.2f Average executions per line\n"),
618 num_executable_lines
619 ? sf->ncalls / (double) num_executable_lines
620 : 0.0);
621 if (ofp != stdout)
622 {
623 fclose (ofp);
624 }
625 }
626 }
This page took 0.042976 seconds and 5 git commands to generate.