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