1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
3 2012 Free Software Foundation, Inc.
4 Contributed by Nathan Sidwell <nathan@codesourcery.com>
6 Gcov is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 Gcov is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Gcov; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 #include "diagnostic.h"
32 static void dump_file (const char *);
33 static void print_prefix (const char *, unsigned, gcov_position_t);
34 static void print_usage (void);
35 static void print_version (void);
36 static void tag_function (const char *, unsigned, unsigned);
37 static void tag_blocks (const char *, unsigned, unsigned);
38 static void tag_arcs (const char *, unsigned, unsigned);
39 static void tag_lines (const char *, unsigned, unsigned);
40 static void tag_counters (const char *, unsigned, unsigned);
41 static void tag_summary (const char *, unsigned, unsigned);
42 extern int main (int, char **);
44 typedef struct tag_format
48 void (*proc) (const char *, unsigned, unsigned);
51 static int flag_dump_contents = 0;
52 static int flag_dump_positions = 0;
54 static const struct option options[] =
56 { "help", no_argument, NULL, 'h' },
57 { "version", no_argument, NULL, 'v' },
58 { "long", no_argument, NULL, 'l' },
59 { "positions", no_argument, NULL, 'o' },
63 static const tag_format_t tag_table[] =
67 {0, "COUNTERS", tag_counters},
68 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function},
69 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks},
70 {GCOV_TAG_ARCS, "ARCS", tag_arcs},
71 {GCOV_TAG_LINES, "LINES", tag_lines},
72 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary},
73 {GCOV_TAG_PROGRAM_SUMMARY, "PROGRAM_SUMMARY", tag_summary},
78 main (int argc ATTRIBUTE_UNUSED, char **argv)
83 p = argv[0] + strlen (argv[0]);
84 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
88 xmalloc_set_program_name (progname);
90 /* Unlock the stdio streams. */
91 unlock_std_streams ();
95 diagnostic_initialize (global_dc, 0);
97 while ((opt = getopt_long (argc, argv, "hlpv", options, NULL)) != -1)
108 flag_dump_contents = 1;
111 flag_dump_positions = 1;
114 fprintf (stderr, "unknown flag `%c'\n", opt);
119 dump_file (argv[optind++]);
126 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
127 printf ("Print coverage file contents\n");
128 printf (" -h, --help Print this help\n");
129 printf (" -v, --version Print version number\n");
130 printf (" -l, --long Dump record contents too\n");
131 printf (" -p, --positions Dump record positions\n");
137 printf ("gcov-dump %s%s\n", pkgversion_string, version_string);
138 printf ("Copyright (C) 2012 Free Software Foundation, Inc.\n");
139 printf ("This is free software; see the source for copying conditions.\n"
140 "There is NO warranty; not even for MERCHANTABILITY or \n"
141 "FITNESS FOR A PARTICULAR PURPOSE.\n\n");
145 print_prefix (const char *filename, unsigned depth, gcov_position_t position)
147 static const char prefix[] = " ";
149 printf ("%s:", filename);
150 if (flag_dump_positions)
151 printf ("%lu:", (unsigned long) position);
152 printf ("%.*s", (int) depth, prefix);
156 dump_file (const char *filename)
161 if (!gcov_open (filename, 1))
163 fprintf (stderr, "%s:cannot open\n", filename);
169 unsigned magic = gcov_read_unsigned ();
171 const char *type = NULL;
175 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
177 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
181 printf ("%s:not a gcov file\n", filename);
185 version = gcov_read_unsigned ();
186 GCOV_UNSIGNED2STRING (v, version);
187 GCOV_UNSIGNED2STRING (m, magic);
189 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename, type,
190 m, v, endianness < 0 ? " (swapped endianness)" : "");
191 if (version != GCOV_VERSION)
195 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
196 printf ("%s:warning:current version is `%.4s'\n", filename, e);
202 unsigned stamp = gcov_read_unsigned ();
204 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
209 gcov_position_t base, position = gcov_position ();
210 unsigned tag, length;
211 tag_format_t const *format;
216 tag = gcov_read_unsigned ();
219 length = gcov_read_unsigned ();
220 base = gcov_position ();
221 mask = GCOV_TAG_MASK (tag) >> 1;
222 for (tag_depth = 4; mask; mask >>= 8)
224 if ((mask & 0xff) != 0xff)
226 printf ("%s:tag `%08x' is invalid\n", filename, tag);
231 for (format = tag_table; format->name; format++)
232 if (format->tag == tag)
234 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
238 if (depth && depth < tag_depth)
240 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
241 printf ("%s:tag `%08x' is incorrectly nested\n",
245 tags[depth - 1] = tag;
248 print_prefix (filename, tag_depth, position);
249 printf ("%08x:%4u:%s", tag, length, format->name);
251 (*format->proc) (filename, tag, length);
254 if (flag_dump_contents && format->proc)
256 unsigned long actual_length = gcov_position () - base;
258 if (actual_length > length)
259 printf ("%s:record size mismatch %lu bytes overread\n",
260 filename, actual_length - length);
261 else if (length > actual_length)
262 printf ("%s:record size mismatch %lu bytes unread\n",
263 filename, length - actual_length);
265 gcov_sync (base, length);
266 if ((error = gcov_is_error ()))
268 printf (error < 0 ? "%s:counter overflow at %lu\n" :
269 "%s:read error at %lu\n", filename,
270 (long unsigned) gcov_position ());
278 tag_function (const char *filename ATTRIBUTE_UNUSED,
279 unsigned tag ATTRIBUTE_UNUSED, unsigned length)
281 unsigned long pos = gcov_position ();
284 printf (" placeholder");
287 printf (" ident=%u", gcov_read_unsigned ());
288 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
289 printf (", cfg_checksum=0x%08x", gcov_read_unsigned ());
291 if (gcov_position () - pos < length)
295 name = gcov_read_string ();
296 printf (", `%s'", name ? name : "NULL");
297 name = gcov_read_string ();
298 printf (" %s", name ? name : "NULL");
299 printf (":%u", gcov_read_unsigned ());
305 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
306 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
308 unsigned n_blocks = GCOV_TAG_BLOCKS_NUM (length);
310 printf (" %u blocks", n_blocks);
312 if (flag_dump_contents)
316 for (ix = 0; ix != n_blocks; ix++)
321 print_prefix (filename, 0, gcov_position ());
322 printf ("\t\t%u", ix);
324 printf (" %04x", gcov_read_unsigned ());
330 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
331 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
333 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
335 printf (" %u arcs", n_arcs);
336 if (flag_dump_contents)
339 unsigned blockno = gcov_read_unsigned ();
341 for (ix = 0; ix != n_arcs; ix++)
348 print_prefix (filename, 0, gcov_position ());
349 printf ("\tblock %u:", blockno);
351 dst = gcov_read_unsigned ();
352 flags = gcov_read_unsigned ();
353 printf (" %u:%04x", dst, flags);
358 if (flags & GCOV_ARC_ON_TREE)
359 printf ("%ctree", c), c = ',';
360 if (flags & GCOV_ARC_FAKE)
361 printf ("%cfake", c), c = ',';
362 if (flags & GCOV_ARC_FALLTHROUGH)
363 printf ("%cfall", c), c = ',';
371 tag_lines (const char *filename ATTRIBUTE_UNUSED,
372 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
374 if (flag_dump_contents)
376 unsigned blockno = gcov_read_unsigned ();
377 char const *sep = NULL;
381 gcov_position_t position = gcov_position ();
382 const char *source = NULL;
383 unsigned lineno = gcov_read_unsigned ();
387 source = gcov_read_string ();
396 print_prefix (filename, 0, position);
397 printf ("\tblock %u:", blockno);
402 printf ("%s%u", sep, lineno);
407 printf ("%s`%s'", sep, source);
415 tag_counters (const char *filename ATTRIBUTE_UNUSED,
416 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
418 static const char *const counter_names[] = GCOV_COUNTER_NAMES;
419 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
421 printf (" %s %u counts",
422 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts);
423 if (flag_dump_contents)
427 for (ix = 0; ix != n_counts; ix++)
434 print_prefix (filename, 0, gcov_position ());
435 printf ("\t\t%u", ix);
438 count = gcov_read_counter ();
440 printf (HOST_WIDEST_INT_PRINT_DEC, count);
446 tag_summary (const char *filename ATTRIBUTE_UNUSED,
447 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
449 struct gcov_summary summary;
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);