gdbinit.in: add "break-on-diagnostic" command
[official-gcc.git] / gcc / gcov-dump.c
blobe5e649cb18f1716e0ac027a7b7f98e1cc5de4eb4
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>
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.
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/>. */
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"
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 **);
46 typedef struct tag_format
48 unsigned tag;
49 char const *name;
50 void (*proc) (const char *, unsigned, unsigned, unsigned);
51 } tag_format_t;
53 static int flag_dump_contents = 0;
54 static int flag_dump_positions = 0;
55 static int flag_dump_working_sets = 0;
57 static const struct option options[] =
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 }
67 #define VALUE_PADDING_PREFIX " "
68 #define VALUE_PREFIX "%2d: "
70 static const tag_format_t tag_table[] =
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}
84 int
85 main (int argc ATTRIBUTE_UNUSED, char **argv)
87 int opt;
88 const char *p;
90 p = argv[0] + strlen (argv[0]);
91 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
92 --p;
93 progname = p;
95 xmalloc_set_program_name (progname);
97 /* Unlock the stdio streams. */
98 unlock_std_streams ();
100 gcc_init_libintl ();
102 diagnostic_initialize (global_dc, 0);
104 while ((opt = getopt_long (argc, argv, "hlpvw", options, NULL)) != -1)
106 switch (opt)
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);
128 while (argv[optind])
129 dump_gcov_file (argv[optind++]);
130 return 0;
133 static void
134 print_usage (void)
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);
147 static void
148 print_version (void)
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");
157 static void
158 print_prefix (const char *filename, unsigned depth, gcov_position_t position)
160 static const char prefix[] = " ";
162 printf ("%s:", filename);
163 if (flag_dump_positions)
164 printf ("%5lu:", (unsigned long) position);
165 printf ("%.*s", (int) 2 * depth, prefix);
168 static void
169 dump_gcov_file (const char *filename)
171 unsigned tags[4];
172 unsigned depth = 0;
174 if (!gcov_open (filename, 1))
176 fprintf (stderr, "%s:cannot open\n", filename);
177 return;
180 /* magic */
182 unsigned magic = gcov_read_unsigned ();
183 unsigned version;
184 const char *type = NULL;
185 int endianness = 0;
186 char m[4], v[4];
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
194 printf ("%s:not a gcov file\n", filename);
195 gcov_close ();
196 return;
198 version = gcov_read_unsigned ();
199 GCOV_UNSIGNED2STRING (v, version);
200 GCOV_UNSIGNED2STRING (m, magic);
202 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename, type,
203 m, v, endianness < 0 ? " (swapped endianness)" : "");
204 if (version != GCOV_VERSION)
206 char e[4];
208 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
209 printf ("%s:warning:current version is `%.4s'\n", filename, e);
213 /* stamp */
215 unsigned stamp = gcov_read_unsigned ();
217 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
220 /* Support for unexecuted basic blocks. */
221 unsigned support_unexecuted_blocks = gcov_read_unsigned ();
222 if (!support_unexecuted_blocks)
223 printf ("%s: has_unexecuted_block is not supported\n", filename);
225 while (1)
227 gcov_position_t base, position = gcov_position ();
228 unsigned tag, length;
229 tag_format_t const *format;
230 unsigned tag_depth;
231 int error;
232 unsigned mask;
234 tag = gcov_read_unsigned ();
235 if (!tag)
236 break;
237 length = gcov_read_unsigned ();
238 base = gcov_position ();
239 mask = GCOV_TAG_MASK (tag) >> 1;
240 for (tag_depth = 4; mask; mask >>= 8)
242 if ((mask & 0xff) != 0xff)
244 printf ("%s:tag `%08x' is invalid\n", filename, tag);
245 break;
247 tag_depth--;
249 for (format = tag_table; format->name; format++)
250 if (format->tag == tag)
251 goto found;
252 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
253 found:;
254 if (tag)
256 if (depth && depth < tag_depth)
258 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
259 printf ("%s:tag `%08x' is incorrectly nested\n",
260 filename, tag);
262 depth = tag_depth;
263 tags[depth - 1] = tag;
266 print_prefix (filename, tag_depth, position);
267 printf ("%08x:%4u:%s", tag, length, format->name);
268 if (format->proc)
269 (*format->proc) (filename, tag, length, depth);
271 printf ("\n");
272 if (flag_dump_contents && format->proc)
274 unsigned long actual_length = gcov_position () - base;
276 if (actual_length > length)
277 printf ("%s:record size mismatch %lu bytes overread\n",
278 filename, actual_length - length);
279 else if (length > actual_length)
280 printf ("%s:record size mismatch %lu bytes unread\n",
281 filename, length - actual_length);
283 gcov_sync (base, length);
284 if ((error = gcov_is_error ()))
286 printf (error < 0 ? "%s:counter overflow at %lu\n" :
287 "%s:read error at %lu\n", filename,
288 (long unsigned) gcov_position ());
289 break;
292 gcov_close ();
295 static void
296 tag_function (const char *filename ATTRIBUTE_UNUSED,
297 unsigned tag ATTRIBUTE_UNUSED, unsigned length,
298 unsigned depth ATTRIBUTE_UNUSED)
300 unsigned long pos = gcov_position ();
302 if (!length)
303 printf (" placeholder");
304 else
306 printf (" ident=%u", gcov_read_unsigned ());
307 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
308 printf (", cfg_checksum=0x%08x", gcov_read_unsigned ());
310 if (gcov_position () - pos < length)
312 const char *name;
314 name = gcov_read_string ();
315 printf (", `%s'", name ? name : "NULL");
316 unsigned artificial = gcov_read_unsigned ();
317 name = gcov_read_string ();
318 printf (" %s", name ? name : "NULL");
319 unsigned line_start = gcov_read_unsigned ();
320 unsigned column_start = gcov_read_unsigned ();
321 unsigned line_end = gcov_read_unsigned ();
322 printf (":%u:%u:%u", line_start, column_start, line_end);
323 if (artificial)
324 printf (", artificial");
329 static void
330 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
331 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
332 unsigned depth ATTRIBUTE_UNUSED)
334 printf (" %u blocks", gcov_read_unsigned ());
337 static void
338 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
339 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
340 unsigned depth)
342 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
344 printf (" %u arcs", n_arcs);
345 if (flag_dump_contents)
347 unsigned ix;
348 unsigned blockno = gcov_read_unsigned ();
350 for (ix = 0; ix != n_arcs; ix++)
352 unsigned dst, flags;
354 if (!(ix & 3))
356 printf ("\n");
357 print_prefix (filename, depth, gcov_position ());
358 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
360 dst = gcov_read_unsigned ();
361 flags = gcov_read_unsigned ();
362 printf (" %u:%04x", dst, flags);
363 if (flags)
365 char c = '(';
367 if (flags & GCOV_ARC_ON_TREE)
368 printf ("%ctree", c), c = ',';
369 if (flags & GCOV_ARC_FAKE)
370 printf ("%cfake", c), c = ',';
371 if (flags & GCOV_ARC_FALLTHROUGH)
372 printf ("%cfall", c), c = ',';
373 printf (")");
379 static void
380 tag_lines (const char *filename ATTRIBUTE_UNUSED,
381 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
382 unsigned depth)
384 if (flag_dump_contents)
386 unsigned blockno = gcov_read_unsigned ();
387 char const *sep = NULL;
389 while (1)
391 gcov_position_t position = gcov_position ();
392 const char *source = NULL;
393 unsigned lineno = gcov_read_unsigned ();
395 if (!lineno)
397 source = gcov_read_string ();
398 if (!source)
399 break;
400 sep = NULL;
403 if (!sep)
405 printf ("\n");
406 print_prefix (filename, depth, position);
407 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
408 sep = "";
410 if (lineno)
412 printf ("%s%u", sep, lineno);
413 sep = ", ";
415 else
417 printf ("%s`%s'", sep, source);
418 sep = ":";
424 static void
425 tag_counters (const char *filename ATTRIBUTE_UNUSED,
426 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
427 unsigned depth)
429 #define DEF_GCOV_COUNTER(COUNTER, NAME, MERGE_FN) NAME,
430 static const char *const counter_names[] = {
431 #include "gcov-counter.def"
433 #undef DEF_GCOV_COUNTER
434 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
436 printf (" %s %u counts",
437 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts);
438 if (flag_dump_contents)
440 unsigned ix;
442 for (ix = 0; ix != n_counts; ix++)
444 gcov_type count;
446 if (!(ix & 7))
448 printf ("\n");
449 print_prefix (filename, depth, gcov_position ());
450 printf (VALUE_PADDING_PREFIX VALUE_PREFIX, ix);
453 count = gcov_read_counter ();
454 printf ("%" PRId64 " ", count);
459 static void
460 tag_summary (const char *filename ATTRIBUTE_UNUSED,
461 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
462 unsigned depth)
464 struct gcov_summary summary;
465 unsigned ix, h_ix;
466 gcov_bucket_type *histo_bucket;
468 gcov_read_summary (&summary);
469 printf (" checksum=0x%08x", summary.checksum);
471 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
473 printf ("\n");
474 print_prefix (filename, depth, 0);
475 printf (VALUE_PADDING_PREFIX "counts=%u, runs=%u",
476 summary.ctrs[ix].num, summary.ctrs[ix].runs);
478 printf (", sum_all=%" PRId64,
479 (int64_t)summary.ctrs[ix].sum_all);
480 printf (", run_max=%" PRId64,
481 (int64_t)summary.ctrs[ix].run_max);
482 printf (", sum_max=%" PRId64,
483 (int64_t)summary.ctrs[ix].sum_max);
484 if (ix != GCOV_COUNTER_ARCS)
485 continue;
486 printf ("\n");
487 print_prefix (filename, depth, 0);
488 printf (VALUE_PADDING_PREFIX "counter histogram:");
489 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
491 histo_bucket = &summary.ctrs[ix].histogram[h_ix];
492 if (!histo_bucket->num_counters)
493 continue;
494 printf ("\n");
495 print_prefix (filename, depth, 0);
496 printf (VALUE_PADDING_PREFIX VALUE_PREFIX "num counts=%u, "
497 "min counter=%" PRId64 ", cum_counter=%" PRId64,
498 h_ix, histo_bucket->num_counters,
499 (int64_t)histo_bucket->min_value,
500 (int64_t)histo_bucket->cum_value);
502 if (flag_dump_working_sets)
503 dump_working_sets (filename, &summary.ctrs[ix], depth);
507 static void
508 dump_working_sets (const char *filename ATTRIBUTE_UNUSED,
509 const struct gcov_ctr_summary *summary,
510 unsigned depth)
512 gcov_working_set_t gcov_working_sets[NUM_GCOV_WORKING_SETS];
513 unsigned ws_ix, pctinc, pct;
514 gcov_working_set_t *ws_info;
516 compute_working_sets (summary, gcov_working_sets);
518 printf ("\n");
519 print_prefix (filename, depth, 0);
520 printf (VALUE_PADDING_PREFIX "counter working sets:");
521 /* Multiply the percentage by 100 to avoid float. */
522 pctinc = 100 * 100 / NUM_GCOV_WORKING_SETS;
523 for (ws_ix = 0, pct = pctinc; ws_ix < NUM_GCOV_WORKING_SETS;
524 ws_ix++, pct += pctinc)
526 if (ws_ix == NUM_GCOV_WORKING_SETS - 1)
527 pct = 9990;
528 ws_info = &gcov_working_sets[ws_ix];
529 /* Print out the percentage using int arithmatic to avoid float. */
530 printf ("\n");
531 print_prefix (filename, depth + 1, 0);
532 printf (VALUE_PADDING_PREFIX "%u.%02u%%: num counts=%u, min counter="
533 "%" PRId64,
534 pct / 100, pct - (pct / 100 * 100),
535 ws_info->num_counters,
536 (int64_t)ws_info->min_counter);