]>
Commit | Line | Data |
---|---|---|
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" | |
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 | |
12516a37 | 38 | DEFUN (cmp_bb, (lp, rp), const void *lp AND const void *rp) |
5489fcc3 | 39 | { |
12516a37 KR |
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; | |
03c35bcb | 50 | } |
12516a37 KR |
51 | |
52 | if (left->line_num != right->line_num) | |
53 | { | |
54 | return left->line_num - right->line_num; | |
03c35bcb KR |
55 | } |
56 | } | |
12516a37 KR |
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; | |
03c35bcb KR |
69 | } |
70 | } | |
5489fcc3 KR |
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 | |
12516a37 | 78 | DEFUN (cmp_ncalls, (lp, rp), const void *lp AND const void *rp) |
5489fcc3 | 79 | { |
12516a37 KR |
80 | const Sym *left = *(const Sym **) lp; |
81 | const Sym *right = *(const Sym **) rp; | |
5489fcc3 | 82 | |
12516a37 KR |
83 | if (!left) |
84 | { | |
85 | return 1; | |
86 | } | |
87 | else if (!right) | |
88 | { | |
89 | return -1; | |
03c35bcb | 90 | } |
5489fcc3 | 91 | |
12516a37 KR |
92 | if (right->ncalls != left->ncalls) |
93 | { | |
94 | return right->ncalls - left->ncalls; | |
03c35bcb | 95 | } |
5489fcc3 | 96 | |
12516a37 | 97 | return left->line_num - right->line_num; |
03c35bcb | 98 | } |
5489fcc3 KR |
99 | |
100 | ||
101 | /* | |
102 | * Skip over variable length string. | |
103 | */ | |
104 | static void | |
12516a37 | 105 | DEFUN (fskip_string, (fp), FILE * fp) |
5489fcc3 | 106 | { |
12516a37 | 107 | int ch; |
5489fcc3 | 108 | |
12516a37 KR |
109 | while ((ch = fgetc (fp)) != EOF) |
110 | { | |
111 | if (ch == '\0') | |
112 | { | |
113 | break; | |
03c35bcb KR |
114 | } |
115 | } | |
116 | } | |
5489fcc3 KR |
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 | |
12516a37 | 124 | DEFUN (bb_read_rec, (ifp, filename), FILE * ifp AND const char *filename) |
5489fcc3 | 125 | { |
12516a37 KR |
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); | |
03c35bcb | 135 | } |
12516a37 KR |
136 | |
137 | nblocks = bfd_get_32 (core_bfd, (bfd_byte *) & nblocks); | |
138 | if (gmon_file_version == 0) | |
139 | { | |
140 | fskip_string (ifp); | |
03c35bcb | 141 | } |
12516a37 KR |
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)) | |
5489fcc3 | 157 | { |
12516a37 KR |
158 | perror (filename); |
159 | done (1); | |
03c35bcb | 160 | } |
12516a37 KR |
161 | } |
162 | else | |
163 | { | |
164 | if (fread (&addr, sizeof (addr), 1, ifp) != 1 | |
165 | || fread (&ncalls, sizeof (ncalls), 1, ifp) != 1) | |
5489fcc3 | 166 | { |
12516a37 KR |
167 | perror (filename); |
168 | done (1); | |
03c35bcb KR |
169 | } |
170 | } | |
12516a37 KR |
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 | { | |
5489fcc3 | 178 | |
12516a37 | 179 | /* convert from target to host endianness: */ |
5489fcc3 | 180 | |
12516a37 | 181 | addr = get_vma (core_bfd, (bfd_byte *) & addr); |
5489fcc3 | 182 | |
12516a37 KR |
183 | sym = sym_lookup (&symtab, addr); |
184 | sym->is_bb_head = TRUE; | |
185 | sym->ncalls += bfd_get_32 (core_bfd, (bfd_byte *) & ncalls); | |
5489fcc3 | 186 | |
12516a37 | 187 | DBG (BBDEBUG, printf ("[bb_read_rec] 0x%lx->0x%lx (%s) cnt=%d\n", |
5489fcc3 | 188 | addr, sym->addr, sym->name, sym->ncalls)); |
12516a37 KR |
189 | } |
190 | else | |
191 | { | |
192 | static bool user_warned = FALSE; | |
5489fcc3 | 193 | |
12516a37 KR |
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); | |
03c35bcb KR |
200 | } |
201 | } | |
202 | } | |
12516a37 | 203 | return; |
03c35bcb | 204 | } |
5489fcc3 KR |
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 | |
12516a37 | 213 | DEFUN (bb_write_blocks, (ofp, filename), FILE * ofp AND const char *filename) |
5489fcc3 | 214 | { |
12516a37 KR |
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) | |
5489fcc3 | 224 | { |
12516a37 KR |
225 | if (sym->ncalls > 0) |
226 | { | |
227 | ++nblocks; | |
03c35bcb KR |
228 | } |
229 | } | |
12516a37 KR |
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); | |
03c35bcb | 238 | } |
5489fcc3 | 239 | |
12516a37 KR |
240 | /* write counts: */ |
241 | for (sym = symtab.base; sym < symtab.limit; ++sym) | |
242 | { | |
243 | if (sym->ncalls == 0) | |
244 | { | |
245 | continue; | |
03c35bcb | 246 | } |
5489fcc3 | 247 | |
12516a37 KR |
248 | put_vma (core_bfd, sym->addr, (bfd_byte *) & addr); |
249 | bfd_put_32 (core_bfd, sym->ncalls, (bfd_byte *) & ncalls); | |
5489fcc3 | 250 | |
12516a37 KR |
251 | if (fwrite (&addr, sizeof (addr), 1, ofp) != 1 |
252 | || fwrite (&ncalls, sizeof (ncalls), 1, ofp) != 1) | |
5489fcc3 | 253 | { |
12516a37 KR |
254 | perror (filename); |
255 | done (1); | |
03c35bcb KR |
256 | } |
257 | } | |
258 | } | |
5489fcc3 KR |
259 | |
260 | ||
261 | /* | |
262 | * Output basic-block statistics in a format that is easily parseable. | |
263 | * Current the format is: | |
264 | * | |
12516a37 | 265 | * <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls> |
5489fcc3 KR |
266 | */ |
267 | void | |
12516a37 | 268 | DEFUN_VOID (print_exec_counts) |
5489fcc3 | 269 | { |
12516a37 KR |
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"); | |
03c35bcb | 280 | } |
12516a37 KR |
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)))) | |
5489fcc3 | 298 | { |
12516a37 | 299 | sorted_bbs[len++] = sym; |
03c35bcb KR |
300 | } |
301 | } | |
12516a37 | 302 | qsort (sorted_bbs, len, sizeof (sorted_bbs[0]), cmp_bb); |
5489fcc3 | 303 | |
12516a37 | 304 | /* output basic-blocks: */ |
5489fcc3 | 305 | |
12516a37 KR |
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); | |
03c35bcb | 312 | } |
12516a37 | 313 | free (sorted_bbs); |
03c35bcb | 314 | } |
5489fcc3 KR |
315 | |
316 | ||
317 | /* | |
318 | * Helper for bb_annotated_source: format annotation containing | |
319 | * number of line executions. | |
320 | */ | |
321 | static void | |
12516a37 KR |
322 | DEFUN (annotate_with_count, (buf, width, line_num, arg), |
323 | char *buf AND int width AND int line_num AND void *arg) | |
5489fcc3 | 324 | { |
12516a37 KR |
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; | |
03c35bcb | 333 | } |
12516a37 KR |
334 | |
335 | b = 0; | |
336 | if (line_num <= sf->num_lines) | |
337 | { | |
338 | b = sf->line[line_num - 1]; | |
03c35bcb | 339 | } |
12516a37 KR |
340 | if (!b) |
341 | { | |
342 | cnt = -1; | |
343 | } | |
344 | else | |
345 | { | |
346 | ++num_executable_lines; | |
347 | cnt = b->ncalls; | |
03c35bcb | 348 | } |
12516a37 KR |
349 | if (cnt > 0) |
350 | { | |
351 | ++num_lines_executed; | |
03c35bcb | 352 | } |
12516a37 KR |
353 | if (cnt < 0 && bb_annotate_all_lines) |
354 | { | |
355 | cnt = last_count; | |
03c35bcb | 356 | } |
12516a37 KR |
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); | |
03c35bcb | 369 | } |
12516a37 | 370 | last_count = cnt; |
03c35bcb | 371 | } |
5489fcc3 KR |
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 | |
12516a37 | 380 | DEFUN_VOID (print_annotated_source) |
5489fcc3 | 381 | { |
12516a37 KR |
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; | |
03c35bcb KR |
405 | } |
406 | } | |
12516a37 KR |
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])); | |
03c35bcb KR |
416 | } |
417 | } | |
12516a37 KR |
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; | |
03c35bcb KR |
448 | } |
449 | } | |
450 | } | |
12516a37 KR |
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)) | |
5489fcc3 | 457 | { |
12516a37 | 458 | continue; |
03c35bcb | 459 | } |
12516a37 KR |
460 | |
461 | num_executable_lines = num_lines_executed = 0; | |
462 | ofp = annotate_source (sf, 16, annotate_with_count, sf); | |
463 | if (!ofp) | |
5489fcc3 | 464 | { |
12516a37 | 465 | continue; |
03c35bcb | 466 | } |
12516a37 KR |
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; | |
03c35bcb | 479 | } |
12516a37 KR |
480 | for (i = 0; i < table_len; ++i) |
481 | { | |
482 | sym = sf->line[i]; | |
483 | if (!sym || sym->ncalls <= 0) | |
484 | { | |
485 | break; | |
03c35bcb | 486 | } |
12516a37 | 487 | fprintf (ofp, "%9d %10d\n", sym->line_num, sym->ncalls); |
03c35bcb KR |
488 | } |
489 | } | |
12516a37 KR |
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); | |
03c35bcb KR |
510 | } |
511 | } | |
512 | } |