ran "indent -gnu"; have not fixed block comment style
[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 basic-blocks and returns result
34 * such that sorting will be increasing according to filename, line
35 * number, and basic-block address (in that order).
36 */
37 static int
38 DEFUN (cmp_bb, (lp, rp), const void *lp AND const void *rp)
39 {
40 int r;
41 const Sym *left = *(const Sym **) lp;
42 const Sym *right = *(const Sym **) rp;
43
44 if (left->file && right->file)
45 {
46 r = strcmp (left->file->name, right->file->name);
47 if (r)
48 {
49 return r;
50 } /* if */
51
52 if (left->line_num != right->line_num)
53 {
54 return left->line_num - right->line_num;
55 } /* if */
56 } /* if */
57
58 if (left->addr < right->addr)
59 {
60 return -1;
61 }
62 else if (left->addr > right->addr)
63 {
64 return 1;
65 }
66 else
67 {
68 return 0;
69 } /* if */
70 } /* cmp_bb */
71
72
73 /*
74 * Helper for sorting. Order basic blocks in decreasing number of
75 * calls, ties are broken in increasing order of line numbers.
76 */
77 static int
78 DEFUN (cmp_ncalls, (lp, rp), const void *lp AND const void *rp)
79 {
80 const Sym *left = *(const Sym **) lp;
81 const Sym *right = *(const Sym **) rp;
82
83 if (!left)
84 {
85 return 1;
86 }
87 else if (!right)
88 {
89 return -1;
90 } /* if */
91
92 if (right->ncalls != left->ncalls)
93 {
94 return right->ncalls - left->ncalls;
95 } /* if */
96
97 return left->line_num - right->line_num;
98 } /* cmp_ncalls */
99
100
101 /*
102 * Skip over variable length string.
103 */
104 static void
105 DEFUN (fskip_string, (fp), FILE * fp)
106 {
107 int ch;
108
109 while ((ch = fgetc (fp)) != EOF)
110 {
111 if (ch == '\0')
112 {
113 break;
114 } /* if */
115 } /* if */
116 } /* fskip_string */
117
118
119 /*
120 * Read a basic-block record from file IFP. FILENAME is the name
121 * of file IFP and is provided for formatting error-messages only.
122 */
123 void
124 DEFUN (bb_read_rec, (ifp, filename), FILE * ifp AND const char *filename)
125 {
126 int nblocks, b;
127 bfd_vma addr;
128 long ncalls;
129 Sym *sym;
130
131 if (fread (&nblocks, sizeof (nblocks), 1, ifp) != 1)
132 {
133 fprintf (stderr, "%s: %s: unexpected end of file\n", whoami, filename);
134 done (1);
135 } /* if */
136
137 nblocks = bfd_get_32 (core_bfd, (bfd_byte *) & nblocks);
138 if (gmon_file_version == 0)
139 {
140 fskip_string (ifp);
141 } /* if */
142
143 for (b = 0; b < nblocks; ++b)
144 {
145 if (gmon_file_version == 0)
146 {
147 int line_num;
148 /*
149 * Version 0 had lots of extra stuff that we don't
150 * care about anymore.
151 */
152 if ((fread (&ncalls, sizeof (ncalls), 1, ifp) != 1)
153 || (fread (&addr, sizeof (addr), 1, ifp) != 1)
154 || (fskip_string (ifp), FALSE)
155 || (fskip_string (ifp), FALSE)
156 || (fread (&line_num, sizeof (line_num), 1, ifp) != 1))
157 {
158 perror (filename);
159 done (1);
160 } /* if */
161 }
162 else
163 {
164 if (fread (&addr, sizeof (addr), 1, ifp) != 1
165 || fread (&ncalls, sizeof (ncalls), 1, ifp) != 1)
166 {
167 perror (filename);
168 done (1);
169 } /* if */
170 } /* if */
171
172 /*
173 * Basic-block execution counts are meaningful only if we're
174 * profiling at the line-by-line level:
175 */
176 if (line_granularity)
177 {
178
179 /* convert from target to host endianness: */
180
181 addr = get_vma (core_bfd, (bfd_byte *) & addr);
182
183 sym = sym_lookup (&symtab, addr);
184 sym->is_bb_head = TRUE;
185 sym->ncalls += bfd_get_32 (core_bfd, (bfd_byte *) & ncalls);
186
187 DBG (BBDEBUG, printf ("[bb_read_rec] 0x%lx->0x%lx (%s) cnt=%d\n",
188 addr, sym->addr, sym->name, sym->ncalls));
189 }
190 else
191 {
192 static bool user_warned = FALSE;
193
194 if (!user_warned)
195 {
196 user_warned = TRUE;
197 fprintf (stderr,
198 "%s: warning: ignoring basic-block exec counts (use -l or --line)\n",
199 whoami);
200 } /* if */
201 } /* if */
202 } /* for */
203 return;
204 } /* bb_read_rec */
205
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 */
212 void
213 DEFUN (bb_write_blocks, (ofp, filename), FILE * ofp AND const char *filename)
214 {
215 const unsigned char tag = GMON_TAG_BB_COUNT;
216 int nblocks = 0;
217 bfd_vma addr;
218 long ncalls;
219 Sym *sym;
220
221 /* count how many non-zero blocks with have: */
222
223 for (sym = symtab.base; sym < symtab.limit; ++sym)
224 {
225 if (sym->ncalls > 0)
226 {
227 ++nblocks;
228 } /* if */
229 } /* for */
230
231 /* write header: */
232 bfd_put_32 (core_bfd, nblocks, (bfd_byte *) & nblocks);
233 if (fwrite (&tag, sizeof (tag), 1, ofp) != 1
234 || fwrite (&nblocks, sizeof (nblocks), 1, ofp) != 1)
235 {
236 perror (filename);
237 done (1);
238 } /* if */
239
240 /* write counts: */
241 for (sym = symtab.base; sym < symtab.limit; ++sym)
242 {
243 if (sym->ncalls == 0)
244 {
245 continue;
246 } /* if */
247
248 put_vma (core_bfd, sym->addr, (bfd_byte *) & addr);
249 bfd_put_32 (core_bfd, sym->ncalls, (bfd_byte *) & ncalls);
250
251 if (fwrite (&addr, sizeof (addr), 1, ofp) != 1
252 || fwrite (&ncalls, sizeof (ncalls), 1, ofp) != 1)
253 {
254 perror (filename);
255 done (1);
256 } /* if */
257 } /* for */
258 } /* bb_write_blocks */
259
260
261 /*
262 * Output basic-block statistics in a format that is easily parseable.
263 * Current the format is:
264 *
265 * <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls>
266 */
267 void
268 DEFUN_VOID (print_exec_counts)
269 {
270 Sym **sorted_bbs, *sym;
271 int i, len;
272
273 if (first_output)
274 {
275 first_output = FALSE;
276 }
277 else
278 {
279 printf ("\f\n");
280 } /* if */
281
282 /* sort basic-blocks according to function name and line number: */
283
284 sorted_bbs = (Sym **) xmalloc (symtab.len * sizeof (sorted_bbs[0]));
285 len = 0;
286 for (sym = symtab.base; sym < symtab.limit; ++sym)
287 {
288 /*
289 * Accept symbol if it's the start of a basic-block and it is
290 * called at least bb_min_calls times and if it's in the
291 * INCL_EXEC table or there is no INCL_EXEC table and it does
292 * not appear in the EXCL_EXEC table.
293 */
294 if (sym->is_bb_head && sym->ncalls >= bb_min_calls
295 && (sym_lookup (&syms[INCL_EXEC], sym->addr)
296 || (syms[INCL_EXEC].len == 0
297 && !sym_lookup (&syms[EXCL_EXEC], sym->addr))))
298 {
299 sorted_bbs[len++] = sym;
300 } /* if */
301 } /* for */
302 qsort (sorted_bbs, len, sizeof (sorted_bbs[0]), cmp_bb);
303
304 /* output basic-blocks: */
305
306 for (i = 0; i < len; ++i)
307 {
308 sym = sorted_bbs[i];
309 printf ("%s:%d: (%s:0x%lx) %d executions\n",
310 sym->file ? sym->file->name : "<unknown>", sym->line_num,
311 sym->name, sym->addr, sym->ncalls);
312 } /* for */
313 free (sorted_bbs);
314 } /* print_exec_counts */
315
316
317 /*
318 * Helper for bb_annotated_source: format annotation containing
319 * number of line executions.
320 */
321 static void
322 DEFUN (annotate_with_count, (buf, width, line_num, arg),
323 char *buf AND int width AND int line_num AND void *arg)
324 {
325 Source_File *sf = arg;
326 Sym *b;
327 long cnt;
328 static long last_count;
329
330 if (line_num == 1)
331 {
332 last_count = -1;
333 } /* if */
334
335 b = 0;
336 if (line_num <= sf->num_lines)
337 {
338 b = sf->line[line_num - 1];
339 } /* if */
340 if (!b)
341 {
342 cnt = -1;
343 }
344 else
345 {
346 ++num_executable_lines;
347 cnt = b->ncalls;
348 } /* if */
349 if (cnt > 0)
350 {
351 ++num_lines_executed;
352 } /* if */
353 if (cnt < 0 && bb_annotate_all_lines)
354 {
355 cnt = last_count;
356 } /* if */
357
358 if (cnt < 0)
359 {
360 strcpy (buf, "\t\t");
361 }
362 else if (cnt < bb_min_calls)
363 {
364 strcpy (buf, " ##### -> ");
365 }
366 else
367 {
368 sprintf (buf, "%12ld -> ", cnt);
369 } /* if */
370 last_count = cnt;
371 } /* annotate_with_count */
372
373
374 /*
375 * Annotate the files named in SOURCE_FILES with basic-block statistics
376 * (execution counts). After each source files, a few statistics
377 * regarding that source file are printed.
378 */
379 void
380 DEFUN_VOID (print_annotated_source)
381 {
382 Sym *sym, *line_stats, *new_line;
383 Source_File *sf;
384 int i, table_len;
385 FILE *ofp;
386
387 /*
388 * Find maximum line number for each source file that user is
389 * interested in:
390 */
391 for (sym = symtab.base; sym < symtab.limit; ++sym)
392 {
393 /*
394 * Accept symbol if it's file is known, its line number is
395 * bigger than anything we have seen for that file so far and
396 * if it's in the INCL_ANNO table or there is no INCL_ANNO
397 * table and it does not appear in the EXCL_ANNO table.
398 */
399 if (sym->file && sym->line_num > sym->file->num_lines
400 && (sym_lookup (&syms[INCL_ANNO], sym->addr)
401 || (syms[INCL_ANNO].len == 0
402 && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
403 {
404 sym->file->num_lines = sym->line_num;
405 } /* if */
406 } /* for */
407
408 /* allocate line descriptors: */
409
410 for (sf = first_src_file; sf; sf = sf->next)
411 {
412 if (sf->num_lines > 0)
413 {
414 sf->line = (void *) xmalloc (sf->num_lines * sizeof (sf->line[0]));
415 memset (sf->line, 0, sf->num_lines * sizeof (sf->line[0]));
416 } /* if */
417 } /* for */
418
419 /* count executions per line: */
420
421 for (sym = symtab.base; sym < symtab.limit; ++sym)
422 {
423 if (sym->is_bb_head && sym->file && sym->file->num_lines
424 && (sym_lookup (&syms[INCL_ANNO], sym->addr)
425 || (syms[INCL_ANNO].len == 0
426 && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
427 {
428 sym->file->ncalls += sym->ncalls;
429 line_stats = sym->file->line[sym->line_num - 1];
430 if (!line_stats)
431 {
432 /* common case has at most one basic-block per source line: */
433 sym->file->line[sym->line_num - 1] = sym;
434 }
435 else if (!line_stats->addr)
436 {
437 /* sym is the 3rd .. nth basic block for this line: */
438 line_stats->ncalls += sym->ncalls;
439 }
440 else
441 {
442 /* sym is the second basic block for this line */
443 new_line = (Sym *) xmalloc (sizeof (*new_line));
444 *new_line = *line_stats;
445 new_line->addr = 0;
446 new_line->ncalls += sym->ncalls;
447 sym->file->line[sym->line_num - 1] = new_line;
448 } /* if */
449 } /* if */
450 } /* for */
451
452 /* plod over source files, annotating them: */
453
454 for (sf = first_src_file; sf; sf = sf->next)
455 {
456 if (!sf->num_lines || (ignore_zeros && sf->ncalls == 0))
457 {
458 continue;
459 } /* if */
460
461 num_executable_lines = num_lines_executed = 0;
462 ofp = annotate_source (sf, 16, annotate_with_count, sf);
463 if (!ofp)
464 {
465 continue;
466 } /* if */
467
468 if (bb_table_length > 0)
469 {
470 fprintf (ofp, "\n\nTop %d Lines:\n\n Line Count\n\n",
471 bb_table_length);
472
473 /* abuse line arrays---it's not needed anymore: */
474 qsort (sf->line, sf->num_lines, sizeof (sf->line[0]), cmp_ncalls);
475 table_len = bb_table_length;
476 if (table_len > sf->num_lines)
477 {
478 table_len = sf->num_lines;
479 } /* if */
480 for (i = 0; i < table_len; ++i)
481 {
482 sym = sf->line[i];
483 if (!sym || sym->ncalls <= 0)
484 {
485 break;
486 } /* if */
487 fprintf (ofp, "%9d %10d\n", sym->line_num, sym->ncalls);
488 } /* for */
489 } /* if */
490
491 free (sf->line);
492 sf->line = 0;
493
494 fprintf (ofp, "\nExecution Summary:\n\n");
495 fprintf (ofp, "%9ld Executable lines in this file\n",
496 num_executable_lines);
497 fprintf (ofp, "%9ld Lines executed\n", num_lines_executed);
498 fprintf (ofp, "%9.2f Percent of the file executed\n",
499 num_executable_lines
500 ? 100.0 * num_lines_executed / (double) num_executable_lines
501 : 100.0);
502 fprintf (ofp, "\n%9d Total number of line executions\n", sf->ncalls);
503 fprintf (ofp, "%9.2f Average executions per line\n",
504 num_executable_lines
505 ? sf->ncalls / (double) num_executable_lines
506 : 0.0);
507 if (ofp != stdout)
508 {
509 fclose (ofp);
510 } /* if */
511 } /* for */
512 } /* print_annotated_source */
513
514 /*** end of basic_block.c ***/
This page took 0.049733 seconds and 5 git commands to generate.