1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002 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 2, 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 COPYING. If not, write to
17 the Free Software Foundation, 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
22 #include "coretypes.h"
29 static void dump_file PARAMS ((const char *));
30 static void print_prefix PARAMS ((const char *, unsigned));
31 static void print_usage PARAMS ((void));
32 static void print_version PARAMS ((void));
33 static int tag_function PARAMS ((const char *, unsigned, unsigned));
34 static int tag_blocks PARAMS ((const char *, unsigned, unsigned));
35 static int tag_arcs PARAMS ((const char *, unsigned, unsigned));
36 static int tag_lines PARAMS ((const char *, unsigned, unsigned));
37 static int tag_arc_counts PARAMS ((const char *, unsigned, unsigned));
38 static int tag_summary PARAMS ((const char *, unsigned, unsigned));
39 extern int main PARAMS ((int, char **));
41 typedef struct tag_format
45 int (*proc) (const char *, unsigned, unsigned);
48 static int flag_dump_contents = 0;
50 static const struct option options[] =
52 { "help", no_argument, NULL, 'h' },
53 { "version", no_argument, NULL, 'v' },
54 { "long", no_argument, NULL, 'l' },
57 static const tag_format_t tag_table[] =
61 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function},
62 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks},
63 {GCOV_TAG_ARCS, "ARCS", tag_arcs},
64 {GCOV_TAG_LINES, "LINES", tag_lines},
65 {GCOV_TAG_ARC_COUNTS, "ARC_COUNTS", tag_arc_counts},
66 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary},
67 {GCOV_TAG_PROGRAM_SUMMARY, "PROGRAM_SUMMARY", tag_summary},
68 {GCOV_TAG_PLACEHOLDER_SUMMARY, "PROGRAM_PLACEHOLDER", tag_summary},
69 {GCOV_TAG_INCORRECT_SUMMARY, "PROGRAM_INCORRECT", tag_summary},
74 int argc ATTRIBUTE_UNUSED;
79 while ((opt = getopt_long (argc, argv, "hlv", options, NULL)) != -1)
90 flag_dump_contents = 1;
93 fprintf (stderr, "unknown flag `%c'\n", opt);
98 dump_file (argv[optind++]);
105 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
106 printf ("Print coverage file contents\n");
107 printf (" -h, --help Print this help\n");
108 printf (" -v, --version Print version number\n");
109 printf (" -l, --long Dump record contents too\n");
116 unsigned version = GCOV_VERSION;
119 for (ix = 4; ix--; version >>= 8)
121 printf ("gcov %.4s (GCC %s)\n", v, version_string);
122 printf ("Copyright (C) 2002 Free Software Foundation, Inc.\n");
123 printf ("This is free software; see the source for copying conditions. There is NO\n\
124 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
128 print_prefix (filename, depth)
129 const char *filename;
132 static const char prefix[] = " ";
134 printf ("%s:%.*s", filename, (int) depth, prefix);
139 const char *filename;
143 unsigned magic, version;
144 unsigned tag, length;
146 if (!gcov_open (filename, 1))
148 fprintf (stderr, "%s:cannot open\n", filename);
152 if (gcov_read_unsigned (&magic) || gcov_read_unsigned (&version))
155 printf ("%s:read error at %lu\n", filename, gcov_save_position ());
162 const char *type = NULL;
163 char e[4], v[4], m[4];
164 unsigned expected = GCOV_VERSION;
166 int different = version != GCOV_VERSION;
168 if (magic == GCOV_DATA_MAGIC)
170 else if (magic == GCOV_GRAPH_MAGIC)
174 printf ("%s:not a gcov file\n", filename);
178 for (ix = 4; ix--; expected >>= 8, version >>= 8, magic >>= 8)
185 printf ("%s:%s:magic `%.4s':version `%.4s'\n", filename, type, m, v);
187 printf ("%s:warning:current version is `%.4s'\n", filename, e);
190 while (!gcov_read_unsigned (&tag) && !gcov_read_unsigned (&length))
192 tag_format_t const *format;
196 base = gcov_save_position ();
202 unsigned mask = GCOV_TAG_MASK (tag) >> 1;
204 for (tag_depth = 4; mask; mask >>= 8)
206 if ((mask & 0xff) != 0xff)
208 printf ("%s:tag `%08x' is invalid\n", filename, tag);
214 for (format = tag_table; format->name; format++)
215 if (format->tag == tag)
217 format = &tag_table[1];
221 if (depth && depth < tag_depth)
223 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
224 printf ("%s:tag `%08x' is incorrectly nested\n",
228 tags[depth - 1] = tag;
231 print_prefix (filename, tag_depth);
232 printf ("%08x:%4u:%s", tag, length, format->name);
234 if ((*format->proc) (filename, tag, length))
237 end = gcov_save_position ();
238 gcov_resync (base, length);
239 if (format->proc && end != base + (long)length)
241 if (end > base + (long)length)
242 printf ("%s:record size mismatch %lu bytes overread\n",
243 filename, (end - base) - length);
245 printf ("%s:record size mismatch %lu bytes unread\n",
246 filename, length - (end - base));
255 tag_function (filename, tag, length)
256 const char *filename ATTRIBUTE_UNUSED;
257 unsigned tag ATTRIBUTE_UNUSED;
258 unsigned length ATTRIBUTE_UNUSED;
264 unsigned long pos = gcov_save_position ();
266 if (gcov_read_string (&name)
267 || gcov_read_unsigned (&checksum))
270 if (gcov_save_position () - pos != length
271 && (gcov_read_string (&src)
272 || gcov_read_unsigned (&lineno)))
275 printf (" `%s' checksum=0x%08x", name, checksum);
277 printf (" %s:%u", src, lineno);
285 tag_blocks (filename, tag, length)
286 const char *filename ATTRIBUTE_UNUSED;
287 unsigned tag ATTRIBUTE_UNUSED;
288 unsigned length ATTRIBUTE_UNUSED;
290 unsigned n_blocks = length / 4;
292 printf (" %u blocks", n_blocks);
294 if (flag_dump_contents)
298 for (ix = 0; ix != n_blocks; ix++)
301 if (gcov_read_unsigned (&flags))
304 printf ("\n%s:\t\t%u", filename, ix);
305 printf (" %04x", flags);
310 gcov_skip (n_blocks * 4);
316 tag_arcs (filename, tag, length)
317 const char *filename ATTRIBUTE_UNUSED;
318 unsigned tag ATTRIBUTE_UNUSED;
319 unsigned length ATTRIBUTE_UNUSED;
321 unsigned n_arcs = (length - 4) / 8;
323 printf (" %u arcs", n_arcs);
324 if (flag_dump_contents)
329 if (gcov_read_unsigned (&blockno))
332 for (ix = 0; ix != n_arcs; ix++)
336 if (gcov_read_unsigned (&dst) || gcov_read_unsigned (&flags))
339 printf ("\n%s:\t\t%u:", filename, blockno);
340 printf (" %u:%04x", dst, flags);
344 gcov_skip (4 + n_arcs * 8);
350 tag_lines (filename, tag, length)
351 const char *filename ATTRIBUTE_UNUSED;
352 unsigned tag ATTRIBUTE_UNUSED;
353 unsigned length ATTRIBUTE_UNUSED;
355 if (flag_dump_contents)
359 char const *sep = NULL;
361 if (gcov_read_unsigned (&blockno))
368 if (gcov_read_unsigned (&lineno))
375 if (gcov_read_string (&source))
384 printf ("\n%s:\t\t%u:", filename, blockno);
389 printf ("%s%u", sep, lineno);
394 printf ("%s`%s'", sep, source);
406 tag_arc_counts (filename, tag, length)
407 const char *filename ATTRIBUTE_UNUSED;
408 unsigned tag ATTRIBUTE_UNUSED;
409 unsigned length ATTRIBUTE_UNUSED;
411 unsigned n_counts = length / 8;
413 printf (" %u counts", n_counts);
414 if (flag_dump_contents)
418 for (ix = 0; ix != n_counts; ix++)
422 if (gcov_read_counter (&count))
425 printf ("\n%s:\t\t%u", filename, ix);
427 printf (HOST_WIDEST_INT_PRINT_DEC, count);
431 gcov_skip (n_counts * 8);
437 tag_summary (filename, tag, length)
438 const char *filename ATTRIBUTE_UNUSED;
439 unsigned tag ATTRIBUTE_UNUSED;
440 unsigned length ATTRIBUTE_UNUSED;
442 struct gcov_summary summary;
444 if (gcov_read_summary (&summary))
446 printf (" checksum=0x%08x", summary.checksum);
448 printf ("\n%s:\t\truns=%u, arcs=%u", filename,
449 summary.runs, summary.arcs);
450 printf ("\n%s:\t\tarc_sum=", filename);
451 printf (HOST_WIDEST_INT_PRINT_DEC,
452 (HOST_WIDEST_INT)summary.arc_sum);
453 printf (", arc_max_one=");
454 printf (HOST_WIDEST_INT_PRINT_DEC,
455 (HOST_WIDEST_INT)summary.arc_max_one);
456 printf ("\n%s:\t\tmax_sum=", filename);
457 printf (HOST_WIDEST_INT_PRINT_DEC,
458 (HOST_WIDEST_INT)summary.arc_max_sum);
459 printf (", sum_max=");
460 printf (HOST_WIDEST_INT_PRINT_DEC,
461 (HOST_WIDEST_INT)summary.arc_sum_max);