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