1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
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)
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.
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/>. */
21 #include "coretypes.h"
25 #include "diagnostic.h"
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);
36 static void tag_blocks (const char *, unsigned, unsigned);
37 static void tag_arcs (const char *, unsigned, unsigned);
38 static void tag_lines (const char *, unsigned, unsigned);
39 static void tag_counters (const char *, unsigned, unsigned);
40 static void tag_summary (const char *, unsigned, unsigned);
41 extern int main (int, char **);
43 typedef struct tag_format
47 void (*proc) (const char *, unsigned, unsigned);
50 static int flag_dump_contents = 0;
51 static int flag_dump_positions = 0;
53 static const struct option options[] =
55 { "help", no_argument, NULL, 'h' },
56 { "version", no_argument, NULL, 'v' },
57 { "long", no_argument, NULL, 'l' },
58 { "positions", no_argument, NULL, 'o' },
62 static const tag_format_t tag_table[] =
66 {0, "COUNTERS", tag_counters},
67 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function},
68 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks},
69 {GCOV_TAG_ARCS, "ARCS", tag_arcs},
70 {GCOV_TAG_LINES, "LINES", tag_lines},
71 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary},
72 {GCOV_TAG_PROGRAM_SUMMARY, "PROGRAM_SUMMARY", tag_summary},
77 main (int argc ATTRIBUTE_UNUSED, char **argv)
82 p = argv[0] + strlen (argv[0]);
83 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
87 xmalloc_set_program_name (progname);
89 /* Unlock the stdio streams. */
90 unlock_std_streams ();
94 diagnostic_initialize (global_dc, 0);
96 while ((opt = getopt_long (argc, argv, "hlpv", options, NULL)) != -1)
107 flag_dump_contents = 1;
110 flag_dump_positions = 1;
113 fprintf (stderr, "unknown flag `%c'\n", opt);
118 dump_gcov_file (argv[optind++]);
125 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
126 printf ("Print coverage file contents\n");
127 printf (" -h, --help Print this help\n");
128 printf (" -v, --version Print version number\n");
129 printf (" -l, --long Dump record contents too\n");
130 printf (" -p, --positions Dump record positions\n");
136 printf ("gcov-dump %s%s\n", pkgversion_string, version_string);
137 printf ("Copyright (C) 2013 Free Software Foundation, Inc.\n");
138 printf ("This is free software; see the source for copying conditions.\n"
139 "There is NO warranty; not even for MERCHANTABILITY or \n"
140 "FITNESS FOR A PARTICULAR PURPOSE.\n\n");
144 print_prefix (const char *filename, unsigned depth, gcov_position_t position)
146 static const char prefix[] = " ";
148 printf ("%s:", filename);
149 if (flag_dump_positions)
150 printf ("%lu:", (unsigned long) position);
151 printf ("%.*s", (int) depth, prefix);
155 dump_gcov_file (const char *filename)
160 if (!gcov_open (filename, 1))
162 fprintf (stderr, "%s:cannot open\n", filename);
168 unsigned magic = gcov_read_unsigned ();
170 const char *type = NULL;
174 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
176 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
180 printf ("%s:not a gcov file\n", filename);
184 version = gcov_read_unsigned ();
185 GCOV_UNSIGNED2STRING (v, version);
186 GCOV_UNSIGNED2STRING (m, magic);
188 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename, type,
189 m, v, endianness < 0 ? " (swapped endianness)" : "");
190 if (version != GCOV_VERSION)
194 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
195 printf ("%s:warning:current version is `%.4s'\n", filename, e);
201 unsigned stamp = gcov_read_unsigned ();
203 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
208 gcov_position_t base, position = gcov_position ();
209 unsigned tag, length;
210 tag_format_t const *format;
215 tag = gcov_read_unsigned ();
218 length = gcov_read_unsigned ();
219 base = gcov_position ();
220 mask = GCOV_TAG_MASK (tag) >> 1;
221 for (tag_depth = 4; mask; mask >>= 8)
223 if ((mask & 0xff) != 0xff)
225 printf ("%s:tag `%08x' is invalid\n", filename, tag);
230 for (format = tag_table; format->name; format++)
231 if (format->tag == tag)
233 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
237 if (depth && depth < tag_depth)
239 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
240 printf ("%s:tag `%08x' is incorrectly nested\n",
244 tags[depth - 1] = tag;
247 print_prefix (filename, tag_depth, position);
248 printf ("%08x:%4u:%s", tag, length, format->name);
250 (*format->proc) (filename, tag, length);
253 if (flag_dump_contents && format->proc)
255 unsigned long actual_length = gcov_position () - base;
257 if (actual_length > length)
258 printf ("%s:record size mismatch %lu bytes overread\n",
259 filename, actual_length - length);
260 else if (length > actual_length)
261 printf ("%s:record size mismatch %lu bytes unread\n",
262 filename, length - actual_length);
264 gcov_sync (base, length);
265 if ((error = gcov_is_error ()))
267 printf (error < 0 ? "%s:counter overflow at %lu\n" :
268 "%s:read error at %lu\n", filename,
269 (long unsigned) gcov_position ());
277 tag_function (const char *filename ATTRIBUTE_UNUSED,
278 unsigned tag ATTRIBUTE_UNUSED, unsigned length)
280 unsigned long pos = gcov_position ();
283 printf (" placeholder");
286 printf (" ident=%u", gcov_read_unsigned ());
287 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
288 printf (", cfg_checksum=0x%08x", gcov_read_unsigned ());
290 if (gcov_position () - pos < length)
294 name = gcov_read_string ();
295 printf (", `%s'", name ? name : "NULL");
296 name = gcov_read_string ();
297 printf (" %s", name ? name : "NULL");
298 printf (":%u", gcov_read_unsigned ());
304 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
305 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
307 unsigned n_blocks = GCOV_TAG_BLOCKS_NUM (length);
309 printf (" %u blocks", n_blocks);
311 if (flag_dump_contents)
315 for (ix = 0; ix != n_blocks; ix++)
320 print_prefix (filename, 0, gcov_position ());
321 printf ("\t\t%u", ix);
323 printf (" %04x", gcov_read_unsigned ());
329 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
330 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
332 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
334 printf (" %u arcs", n_arcs);
335 if (flag_dump_contents)
338 unsigned blockno = gcov_read_unsigned ();
340 for (ix = 0; ix != n_arcs; ix++)
347 print_prefix (filename, 0, gcov_position ());
348 printf ("\tblock %u:", blockno);
350 dst = gcov_read_unsigned ();
351 flags = gcov_read_unsigned ();
352 printf (" %u:%04x", dst, flags);
357 if (flags & GCOV_ARC_ON_TREE)
358 printf ("%ctree", c), c = ',';
359 if (flags & GCOV_ARC_FAKE)
360 printf ("%cfake", c), c = ',';
361 if (flags & GCOV_ARC_FALLTHROUGH)
362 printf ("%cfall", c), c = ',';
370 tag_lines (const char *filename ATTRIBUTE_UNUSED,
371 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
373 if (flag_dump_contents)
375 unsigned blockno = gcov_read_unsigned ();
376 char const *sep = NULL;
380 gcov_position_t position = gcov_position ();
381 const char *source = NULL;
382 unsigned lineno = gcov_read_unsigned ();
386 source = gcov_read_string ();
395 print_prefix (filename, 0, position);
396 printf ("\tblock %u:", blockno);
401 printf ("%s%u", sep, lineno);
406 printf ("%s`%s'", sep, source);
414 tag_counters (const char *filename ATTRIBUTE_UNUSED,
415 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
417 static const char *const counter_names[] = GCOV_COUNTER_NAMES;
418 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
420 printf (" %s %u counts",
421 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts);
422 if (flag_dump_contents)
426 for (ix = 0; ix != n_counts; ix++)
433 print_prefix (filename, 0, gcov_position ());
434 printf ("\t\t%u", ix);
437 count = gcov_read_counter ();
439 printf (HOST_WIDEST_INT_PRINT_DEC, count);
445 tag_summary (const char *filename ATTRIBUTE_UNUSED,
446 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
448 struct gcov_summary summary;
450 gcov_bucket_type *histo_bucket;
452 gcov_read_summary (&summary);
453 printf (" checksum=0x%08x", summary.checksum);
455 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
458 print_prefix (filename, 0, 0);
459 printf ("\t\tcounts=%u, runs=%u",
460 summary.ctrs[ix].num, summary.ctrs[ix].runs);
462 printf (", sum_all=" HOST_WIDEST_INT_PRINT_DEC,
463 (HOST_WIDEST_INT)summary.ctrs[ix].sum_all);
464 printf (", run_max=" HOST_WIDEST_INT_PRINT_DEC,
465 (HOST_WIDEST_INT)summary.ctrs[ix].run_max);
466 printf (", sum_max=" HOST_WIDEST_INT_PRINT_DEC,
467 (HOST_WIDEST_INT)summary.ctrs[ix].sum_max);
468 if (ix != GCOV_COUNTER_ARCS)
471 print_prefix (filename, 0, 0);
472 printf ("\t\tcounter histogram:");
473 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
475 histo_bucket = &summary.ctrs[ix].histogram[h_ix];
476 if (!histo_bucket->num_counters)
479 print_prefix (filename, 0, 0);
480 printf ("\t\t%d: num counts=%u, min counter="
481 HOST_WIDEST_INT_PRINT_DEC ", cum_counter="
482 HOST_WIDEST_INT_PRINT_DEC,
483 h_ix, histo_bucket->num_counters,
484 (HOST_WIDEST_INT)histo_bucket->min_value,
485 (HOST_WIDEST_INT)histo_bucket->cum_value);