]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gcov-dump.c
GCOV: support multiple functions per a line (PR gcov-profile/48463)
[thirdparty/gcc.git] / gcc / gcov-dump.c
1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
4
5 Gcov is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 Gcov is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Gcov; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>. */
18
19 #include "config.h"
20 #include "system.h"
21 #include "coretypes.h"
22 #include "tm.h"
23 #include "version.h"
24 #include "intl.h"
25 #include "diagnostic.h"
26 #include <getopt.h>
27 #define IN_GCOV (-1)
28 #include "gcov-io.h"
29 #include "gcov-io.c"
30
31 static void dump_gcov_file (const char *);
32 static void print_prefix (const char *, unsigned, gcov_position_t);
33 static void print_usage (void);
34 static void print_version (void);
35 static void tag_function (const char *, unsigned, unsigned, unsigned);
36 static void tag_blocks (const char *, unsigned, unsigned, unsigned);
37 static void tag_arcs (const char *, unsigned, unsigned, unsigned);
38 static void tag_lines (const char *, unsigned, unsigned, unsigned);
39 static void tag_counters (const char *, unsigned, unsigned, unsigned);
40 static void tag_summary (const char *, unsigned, unsigned, unsigned);
41 static void dump_working_sets (const char *filename ATTRIBUTE_UNUSED,
42 const struct gcov_ctr_summary *summary,
43 unsigned depth);
44 extern int main (int, char **);
45
46 typedef struct tag_format
47 {
48 unsigned tag;
49 char const *name;
50 void (*proc) (const char *, unsigned, unsigned, unsigned);
51 } tag_format_t;
52
53 static int flag_dump_contents = 0;
54 static int flag_dump_positions = 0;
55 static int flag_dump_working_sets = 0;
56
57 static const struct option options[] =
58 {
59 { "help", no_argument, NULL, 'h' },
60 { "version", no_argument, NULL, 'v' },
61 { "long", no_argument, NULL, 'l' },
62 { "positions", no_argument, NULL, 'o' },
63 { "working-sets", no_argument, NULL, 'w' },
64 { 0, 0, 0, 0 }
65 };
66
67 #define VALUE_PADDING_PREFIX " "
68 #define VALUE_PREFIX "%2d: "
69
70 static const tag_format_t tag_table[] =
71 {
72 {0, "NOP", NULL},
73 {0, "UNKNOWN", NULL},
74 {0, "COUNTERS", tag_counters},
75 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function},
76 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks},
77 {GCOV_TAG_ARCS, "ARCS", tag_arcs},
78 {GCOV_TAG_LINES, "LINES", tag_lines},
79 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary},
80 {GCOV_TAG_PROGRAM_SUMMARY, "PROGRAM_SUMMARY", tag_summary},
81 {0, NULL, NULL}
82 };
83
84 int
85 main (int argc ATTRIBUTE_UNUSED, char **argv)
86 {
87 int opt;
88 const char *p;
89
90 p = argv[0] + strlen (argv[0]);
91 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
92 --p;
93 progname = p;
94
95 xmalloc_set_program_name (progname);
96
97 /* Unlock the stdio streams. */
98 unlock_std_streams ();
99
100 gcc_init_libintl ();
101
102 diagnostic_initialize (global_dc, 0);
103
104 while ((opt = getopt_long (argc, argv, "hlpvw", options, NULL)) != -1)
105 {
106 switch (opt)
107 {
108 case 'h':
109 print_usage ();
110 break;
111 case 'v':
112 print_version ();
113 break;
114 case 'l':
115 flag_dump_contents = 1;
116 break;
117 case 'p':
118 flag_dump_positions = 1;
119 break;
120 case 'w':
121 flag_dump_working_sets = 1;
122 break;
123 default:
124 fprintf (stderr, "unknown flag `%c'\n", opt);
125 }
126 }
127
128 while (argv[optind])
129 dump_gcov_file (argv[optind++]);
130 return 0;
131 }
132
133 static void
134 print_usage (void)
135 {
136 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
137 printf ("Print coverage file contents\n");
138 printf (" -h, --help Print this help\n");
139 printf (" -l, --long Dump record contents too\n");
140 printf (" -p, --positions Dump record positions\n");
141 printf (" -v, --version Print version number\n");
142 printf (" -w, --working-sets Dump working set computed from summary\n");
143 printf ("\nFor bug reporting instructions, please see:\n%s.\n",
144 bug_report_url);
145 }
146
147 static void
148 print_version (void)
149 {
150 printf ("gcov-dump %s%s\n", pkgversion_string, version_string);
151 printf ("Copyright (C) 2017 Free Software Foundation, Inc.\n");
152 printf ("This is free software; see the source for copying conditions.\n"
153 "There is NO warranty; not even for MERCHANTABILITY or \n"
154 "FITNESS FOR A PARTICULAR PURPOSE.\n\n");
155 }
156
157 static void
158 print_prefix (const char *filename, unsigned depth, gcov_position_t position)
159 {
160 static const char prefix[] = " ";
161
162 printf ("%s:", filename);
163 if (flag_dump_positions)
164 printf ("%5lu:", (unsigned long) position);
165 printf ("%.*s", (int) 2 * depth, prefix);
166 }
167
168 static void
169 dump_gcov_file (const char *filename)
170 {
171 unsigned tags[4];
172 unsigned depth = 0;
173
174 if (!gcov_open (filename, 1))
175 {
176 fprintf (stderr, "%s:cannot open\n", filename);
177 return;
178 }
179
180 /* magic */
181 {
182 unsigned magic = gcov_read_unsigned ();
183 unsigned version;
184 const char *type = NULL;
185 int endianness = 0;
186 char m[4], v[4];
187
188 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
189 type = "data";
190 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
191 type = "note";
192 else
193 {
194 printf ("%s:not a gcov file\n", filename);
195 gcov_close ();
196 return;
197 }
198 version = gcov_read_unsigned ();
199 GCOV_UNSIGNED2STRING (v, version);
200 GCOV_UNSIGNED2STRING (m, magic);
201
202 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename, type,
203 m, v, endianness < 0 ? " (swapped endianness)" : "");
204 if (version != GCOV_VERSION)
205 {
206 char e[4];
207
208 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
209 printf ("%s:warning:current version is `%.4s'\n", filename, e);
210 }
211 }
212
213 /* stamp */
214 {
215 unsigned stamp = gcov_read_unsigned ();
216
217 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
218 }
219
220 while (1)
221 {
222 gcov_position_t base, position = gcov_position ();
223 unsigned tag, length;
224 tag_format_t const *format;
225 unsigned tag_depth;
226 int error;
227 unsigned mask;
228
229 tag = gcov_read_unsigned ();
230 if (!tag)
231 break;
232 length = gcov_read_unsigned ();
233 base = gcov_position ();
234 mask = GCOV_TAG_MASK (tag) >> 1;
235 for (tag_depth = 4; mask; mask >>= 8)
236 {
237 if ((mask & 0xff) != 0xff)
238 {
239 printf ("%s:tag `%08x' is invalid\n", filename, tag);
240 break;
241 }
242 tag_depth--;
243 }
244 for (format = tag_table; format->name; format++)
245 if (format->tag == tag)
246 goto found;
247 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
248 found:;
249 if (tag)
250 {
251 if (depth && depth < tag_depth)
252 {
253 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
254 printf ("%s:tag `%08x' is incorrectly nested\n",
255 filename, tag);
256 }
257 depth = tag_depth;
258 tags[depth - 1] = tag;
259 }
260
261 print_prefix (filename, tag_depth, position);
262 printf ("%08x:%4u:%s", tag, length, format->name);
263 if (format->proc)
264 (*format->proc) (filename, tag, length, depth);
265
266 printf ("\n");
267 if (flag_dump_contents && format->proc)
268 {
269 unsigned long actual_length = gcov_position () - base;
270
271 if (actual_length > length)
272 printf ("%s:record size mismatch %lu bytes overread\n",
273 filename, actual_length - length);
274 else if (length > actual_length)
275 printf ("%s:record size mismatch %lu bytes unread\n",
276 filename, length - actual_length);
277 }
278 gcov_sync (base, length);
279 if ((error = gcov_is_error ()))
280 {
281 printf (error < 0 ? "%s:counter overflow at %lu\n" :
282 "%s:read error at %lu\n", filename,
283 (long unsigned) gcov_position ());
284 break;
285 }
286 }
287 gcov_close ();
288 }
289
290 static void
291 tag_function (const char *filename ATTRIBUTE_UNUSED,
292 unsigned tag ATTRIBUTE_UNUSED, unsigned length,
293 unsigned depth ATTRIBUTE_UNUSED)
294 {
295 unsigned long pos = gcov_position ();
296
297 if (!length)
298 printf (" placeholder");
299 else
300 {
301 printf (" ident=%u", gcov_read_unsigned ());
302 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
303 printf (", cfg_checksum=0x%08x", gcov_read_unsigned ());
304
305 if (gcov_position () - pos < length)
306 {
307 const char *name;
308
309 name = gcov_read_string ();
310 printf (", `%s'", name ? name : "NULL");
311 unsigned artificial = gcov_read_unsigned ();
312 name = gcov_read_string ();
313 printf (" %s", name ? name : "NULL");
314 unsigned line_start = gcov_read_unsigned ();
315 unsigned column_start = gcov_read_unsigned ();
316 unsigned line_end = gcov_read_unsigned ();
317 printf (":%u:%u:%u", line_start, column_start, line_end);
318 if (artificial)
319 printf (", artificial");
320 }
321 }
322 }
323
324 static void
325 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
326 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
327 unsigned depth ATTRIBUTE_UNUSED)
328 {
329 printf (" %u blocks", gcov_read_unsigned ());
330 }
331
332 static void
333 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
334 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
335 unsigned depth)
336 {
337 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
338
339 printf (" %u arcs", n_arcs);
340 if (flag_dump_contents)
341 {
342 unsigned ix;
343 unsigned blockno = gcov_read_unsigned ();
344
345 for (ix = 0; ix != n_arcs; ix++)
346 {
347 unsigned dst, flags;
348
349 if (!(ix & 3))
350 {
351 printf ("\n");
352 print_prefix (filename, depth, gcov_position ());
353 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
354 }
355 dst = gcov_read_unsigned ();
356 flags = gcov_read_unsigned ();
357 printf (" %u:%04x", dst, flags);
358 if (flags)
359 {
360 char c = '(';
361
362 if (flags & GCOV_ARC_ON_TREE)
363 printf ("%ctree", c), c = ',';
364 if (flags & GCOV_ARC_FAKE)
365 printf ("%cfake", c), c = ',';
366 if (flags & GCOV_ARC_FALLTHROUGH)
367 printf ("%cfall", c), c = ',';
368 printf (")");
369 }
370 }
371 }
372 }
373
374 static void
375 tag_lines (const char *filename ATTRIBUTE_UNUSED,
376 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
377 unsigned depth)
378 {
379 if (flag_dump_contents)
380 {
381 unsigned blockno = gcov_read_unsigned ();
382 char const *sep = NULL;
383
384 while (1)
385 {
386 gcov_position_t position = gcov_position ();
387 const char *source = NULL;
388 unsigned lineno = gcov_read_unsigned ();
389
390 if (!lineno)
391 {
392 source = gcov_read_string ();
393 if (!source)
394 break;
395 sep = NULL;
396 }
397
398 if (!sep)
399 {
400 printf ("\n");
401 print_prefix (filename, depth, position);
402 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
403 sep = "";
404 }
405 if (lineno)
406 {
407 printf ("%s%u", sep, lineno);
408 sep = ", ";
409 }
410 else
411 {
412 printf ("%s`%s'", sep, source);
413 sep = ":";
414 }
415 }
416 }
417 }
418
419 static void
420 tag_counters (const char *filename ATTRIBUTE_UNUSED,
421 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
422 unsigned depth)
423 {
424 #define DEF_GCOV_COUNTER(COUNTER, NAME, MERGE_FN) NAME,
425 static const char *const counter_names[] = {
426 #include "gcov-counter.def"
427 };
428 #undef DEF_GCOV_COUNTER
429 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
430
431 printf (" %s %u counts",
432 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts);
433 if (flag_dump_contents)
434 {
435 unsigned ix;
436
437 for (ix = 0; ix != n_counts; ix++)
438 {
439 gcov_type count;
440
441 if (!(ix & 7))
442 {
443 printf ("\n");
444 print_prefix (filename, depth, gcov_position ());
445 printf (VALUE_PADDING_PREFIX VALUE_PREFIX, ix);
446 }
447
448 count = gcov_read_counter ();
449 printf ("%" PRId64 " ", count);
450 }
451 }
452 }
453
454 static void
455 tag_summary (const char *filename ATTRIBUTE_UNUSED,
456 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
457 unsigned depth)
458 {
459 struct gcov_summary summary;
460 unsigned ix, h_ix;
461 gcov_bucket_type *histo_bucket;
462
463 gcov_read_summary (&summary);
464 printf (" checksum=0x%08x", summary.checksum);
465
466 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
467 {
468 printf ("\n");
469 print_prefix (filename, depth, 0);
470 printf (VALUE_PADDING_PREFIX "counts=%u, runs=%u",
471 summary.ctrs[ix].num, summary.ctrs[ix].runs);
472
473 printf (", sum_all=%" PRId64,
474 (int64_t)summary.ctrs[ix].sum_all);
475 printf (", run_max=%" PRId64,
476 (int64_t)summary.ctrs[ix].run_max);
477 printf (", sum_max=%" PRId64,
478 (int64_t)summary.ctrs[ix].sum_max);
479 if (ix != GCOV_COUNTER_ARCS)
480 continue;
481 printf ("\n");
482 print_prefix (filename, depth, 0);
483 printf (VALUE_PADDING_PREFIX "counter histogram:");
484 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
485 {
486 histo_bucket = &summary.ctrs[ix].histogram[h_ix];
487 if (!histo_bucket->num_counters)
488 continue;
489 printf ("\n");
490 print_prefix (filename, depth, 0);
491 printf (VALUE_PADDING_PREFIX VALUE_PREFIX "num counts=%u, "
492 "min counter=%" PRId64 ", cum_counter=%" PRId64,
493 h_ix, histo_bucket->num_counters,
494 (int64_t)histo_bucket->min_value,
495 (int64_t)histo_bucket->cum_value);
496 }
497 if (flag_dump_working_sets)
498 dump_working_sets (filename, &summary.ctrs[ix], depth);
499 }
500 }
501
502 static void
503 dump_working_sets (const char *filename ATTRIBUTE_UNUSED,
504 const struct gcov_ctr_summary *summary,
505 unsigned depth)
506 {
507 gcov_working_set_t gcov_working_sets[NUM_GCOV_WORKING_SETS];
508 unsigned ws_ix, pctinc, pct;
509 gcov_working_set_t *ws_info;
510
511 compute_working_sets (summary, gcov_working_sets);
512
513 printf ("\n");
514 print_prefix (filename, depth, 0);
515 printf (VALUE_PADDING_PREFIX "counter working sets:");
516 /* Multiply the percentage by 100 to avoid float. */
517 pctinc = 100 * 100 / NUM_GCOV_WORKING_SETS;
518 for (ws_ix = 0, pct = pctinc; ws_ix < NUM_GCOV_WORKING_SETS;
519 ws_ix++, pct += pctinc)
520 {
521 if (ws_ix == NUM_GCOV_WORKING_SETS - 1)
522 pct = 9990;
523 ws_info = &gcov_working_sets[ws_ix];
524 /* Print out the percentage using int arithmatic to avoid float. */
525 printf ("\n");
526 print_prefix (filename, depth + 1, 0);
527 printf (VALUE_PADDING_PREFIX "%u.%02u%%: num counts=%u, min counter="
528 "%" PRId64,
529 pct / 100, pct - (pct / 100 * 100),
530 ws_info->num_counters,
531 (int64_t)ws_info->min_counter);
532 }
533 }