Merge trunk version 190524 into gupc branch.
[official-gcc.git] / gcc / gcov-dump.c
blob59b8380f299f39bde5850a3ee6c37b063dd1d48f
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)
9 any later version.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "version.h"
25 #include "intl.h"
26 #include "diagnostic.h"
27 #include <getopt.h>
28 #define IN_GCOV (-1)
29 #include "gcov-io.h"
30 #include "gcov-io.c"
32 static void dump_gcov_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
46 unsigned tag;
47 char const *name;
48 void (*proc) (const char *, unsigned, unsigned);
49 } tag_format_t;
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' },
60 { 0, 0, 0, 0 }
63 static const tag_format_t tag_table[] =
65 {0, "NOP", NULL},
66 {0, "UNKNOWN", NULL},
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},
74 {0, NULL, NULL}
77 int
78 main (int argc ATTRIBUTE_UNUSED, char **argv)
80 int opt;
81 const char *p;
83 p = argv[0] + strlen (argv[0]);
84 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
85 --p;
86 progname = p;
88 xmalloc_set_program_name (progname);
90 /* Unlock the stdio streams. */
91 unlock_std_streams ();
93 gcc_init_libintl ();
95 diagnostic_initialize (global_dc, 0);
97 while ((opt = getopt_long (argc, argv, "hlpv", options, NULL)) != -1)
99 switch (opt)
101 case 'h':
102 print_usage ();
103 break;
104 case 'v':
105 print_version ();
106 break;
107 case 'l':
108 flag_dump_contents = 1;
109 break;
110 case 'p':
111 flag_dump_positions = 1;
112 break;
113 default:
114 fprintf (stderr, "unknown flag `%c'\n", opt);
118 while (argv[optind])
119 dump_gcov_file (argv[optind++]);
120 return 0;
123 static void
124 print_usage (void)
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");
134 static void
135 print_version (void)
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");
144 static void
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);
155 static void
156 dump_gcov_file (const char *filename)
158 unsigned tags[4];
159 unsigned depth = 0;
161 if (!gcov_open (filename, 1))
163 fprintf (stderr, "%s:cannot open\n", filename);
164 return;
167 /* magic */
169 unsigned magic = gcov_read_unsigned ();
170 unsigned version;
171 const char *type = NULL;
172 int endianness = 0;
173 char m[4], v[4];
175 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
176 type = "data";
177 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
178 type = "note";
179 else
181 printf ("%s:not a gcov file\n", filename);
182 gcov_close ();
183 return;
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)
193 char e[4];
195 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
196 printf ("%s:warning:current version is `%.4s'\n", filename, e);
200 /* stamp */
202 unsigned stamp = gcov_read_unsigned ();
204 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
207 while (1)
209 gcov_position_t base, position = gcov_position ();
210 unsigned tag, length;
211 tag_format_t const *format;
212 unsigned tag_depth;
213 int error;
214 unsigned mask;
216 tag = gcov_read_unsigned ();
217 if (!tag)
218 break;
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);
227 break;
229 tag_depth--;
231 for (format = tag_table; format->name; format++)
232 if (format->tag == tag)
233 goto found;
234 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
235 found:;
236 if (tag)
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",
242 filename, tag);
244 depth = tag_depth;
245 tags[depth - 1] = tag;
248 print_prefix (filename, tag_depth, position);
249 printf ("%08x:%4u:%s", tag, length, format->name);
250 if (format->proc)
251 (*format->proc) (filename, tag, length);
253 printf ("\n");
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 ());
271 break;
274 gcov_close ();
277 static void
278 tag_function (const char *filename ATTRIBUTE_UNUSED,
279 unsigned tag ATTRIBUTE_UNUSED, unsigned length)
281 unsigned long pos = gcov_position ();
283 if (!length)
284 printf (" placeholder");
285 else
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)
293 const char *name;
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 ());
304 static void
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)
314 unsigned ix;
316 for (ix = 0; ix != n_blocks; ix++)
318 if (!(ix & 7))
320 printf ("\n");
321 print_prefix (filename, 0, gcov_position ());
322 printf ("\t\t%u", ix);
324 printf (" %04x", gcov_read_unsigned ());
329 static void
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)
338 unsigned ix;
339 unsigned blockno = gcov_read_unsigned ();
341 for (ix = 0; ix != n_arcs; ix++)
343 unsigned dst, flags;
345 if (!(ix & 3))
347 printf ("\n");
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);
354 if (flags)
356 char c = '(';
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 = ',';
364 printf (")");
370 static void
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;
379 while (1)
381 gcov_position_t position = gcov_position ();
382 const char *source = NULL;
383 unsigned lineno = gcov_read_unsigned ();
385 if (!lineno)
387 source = gcov_read_string ();
388 if (!source)
389 break;
390 sep = NULL;
393 if (!sep)
395 printf ("\n");
396 print_prefix (filename, 0, position);
397 printf ("\tblock %u:", blockno);
398 sep = "";
400 if (lineno)
402 printf ("%s%u", sep, lineno);
403 sep = ", ";
405 else
407 printf ("%s`%s'", sep, source);
408 sep = ":";
414 static void
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)
425 unsigned ix;
427 for (ix = 0; ix != n_counts; ix++)
429 gcov_type count;
431 if (!(ix & 7))
433 printf ("\n");
434 print_prefix (filename, 0, gcov_position ());
435 printf ("\t\t%u", ix);
438 count = gcov_read_counter ();
439 printf (" ");
440 printf (HOST_WIDEST_INT_PRINT_DEC, count);
445 static void
446 tag_summary (const char *filename ATTRIBUTE_UNUSED,
447 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
449 struct gcov_summary summary;
450 unsigned ix;
452 gcov_read_summary (&summary);
453 printf (" checksum=0x%08x", summary.checksum);
455 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
457 printf ("\n");
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);