Disable anchors and msdata for ASAN test-case (PR sanirizer/85174).
[official-gcc.git] / gcc / gcov-dump.c
blobba432db51c73daf6f8d532565fd07d88261ea84b
1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002-2018 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) 2018 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;
173 bool is_data_type;
175 if (!gcov_open (filename, 1))
177 fprintf (stderr, "%s:cannot open\n", filename);
178 return;
181 /* magic */
183 unsigned magic = gcov_read_unsigned ();
184 unsigned version;
185 int endianness = 0;
186 char m[4], v[4];
188 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
189 is_data_type = true;
190 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
191 is_data_type = false;
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,
203 is_data_type ? "data" : "note",
204 m, v, endianness < 0 ? " (swapped endianness)" : "");
205 if (version != GCOV_VERSION)
207 char e[4];
209 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
210 printf ("%s:warning:current version is `%.4s'\n", filename, e);
214 /* stamp */
216 unsigned stamp = gcov_read_unsigned ();
218 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
221 if (!is_data_type)
223 /* Support for unexecuted basic blocks. */
224 unsigned support_unexecuted_blocks = gcov_read_unsigned ();
225 if (!support_unexecuted_blocks)
226 printf ("%s: has_unexecuted_block is not supported\n", filename);
229 while (1)
231 gcov_position_t base, position = gcov_position ();
232 unsigned tag, length;
233 tag_format_t const *format;
234 unsigned tag_depth;
235 int error;
236 unsigned mask;
238 tag = gcov_read_unsigned ();
239 if (!tag)
240 break;
241 length = gcov_read_unsigned ();
242 base = gcov_position ();
243 mask = GCOV_TAG_MASK (tag) >> 1;
244 for (tag_depth = 4; mask; mask >>= 8)
246 if ((mask & 0xff) != 0xff)
248 printf ("%s:tag `%08x' is invalid\n", filename, tag);
249 break;
251 tag_depth--;
253 for (format = tag_table; format->name; format++)
254 if (format->tag == tag)
255 goto found;
256 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
257 found:;
258 if (tag)
260 if (depth && depth < tag_depth)
262 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
263 printf ("%s:tag `%08x' is incorrectly nested\n",
264 filename, tag);
266 depth = tag_depth;
267 tags[depth - 1] = tag;
270 print_prefix (filename, tag_depth, position);
271 printf ("%08x:%4u:%s", tag, length, format->name);
272 if (format->proc)
273 (*format->proc) (filename, tag, length, depth);
275 printf ("\n");
276 if (flag_dump_contents && format->proc)
278 unsigned long actual_length = gcov_position () - base;
280 if (actual_length > length)
281 printf ("%s:record size mismatch %lu bytes overread\n",
282 filename, actual_length - length);
283 else if (length > actual_length)
284 printf ("%s:record size mismatch %lu bytes unread\n",
285 filename, length - actual_length);
287 gcov_sync (base, length);
288 if ((error = gcov_is_error ()))
290 printf (error < 0 ? "%s:counter overflow at %lu\n" :
291 "%s:read error at %lu\n", filename,
292 (long unsigned) gcov_position ());
293 break;
296 gcov_close ();
299 static void
300 tag_function (const char *filename ATTRIBUTE_UNUSED,
301 unsigned tag ATTRIBUTE_UNUSED, unsigned length,
302 unsigned depth ATTRIBUTE_UNUSED)
304 unsigned long pos = gcov_position ();
306 if (!length)
307 printf (" placeholder");
308 else
310 printf (" ident=%u", gcov_read_unsigned ());
311 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
312 printf (", cfg_checksum=0x%08x", gcov_read_unsigned ());
314 if (gcov_position () - pos < length)
316 const char *name;
318 name = gcov_read_string ();
319 printf (", `%s'", name ? name : "NULL");
320 unsigned artificial = gcov_read_unsigned ();
321 name = gcov_read_string ();
322 printf (" %s", name ? name : "NULL");
323 unsigned line_start = gcov_read_unsigned ();
324 unsigned column_start = gcov_read_unsigned ();
325 unsigned line_end = gcov_read_unsigned ();
326 printf (":%u:%u:%u", line_start, column_start, line_end);
327 if (artificial)
328 printf (", artificial");
333 static void
334 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
335 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
336 unsigned depth ATTRIBUTE_UNUSED)
338 printf (" %u blocks", gcov_read_unsigned ());
341 static void
342 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
343 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
344 unsigned depth)
346 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
348 printf (" %u arcs", n_arcs);
349 if (flag_dump_contents)
351 unsigned ix;
352 unsigned blockno = gcov_read_unsigned ();
354 for (ix = 0; ix != n_arcs; ix++)
356 unsigned dst, flags;
358 if (!(ix & 3))
360 printf ("\n");
361 print_prefix (filename, depth, gcov_position ());
362 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
364 dst = gcov_read_unsigned ();
365 flags = gcov_read_unsigned ();
366 printf (" %u:%04x", dst, flags);
367 if (flags)
369 char c = '(';
371 if (flags & GCOV_ARC_ON_TREE)
372 printf ("%ctree", c), c = ',';
373 if (flags & GCOV_ARC_FAKE)
374 printf ("%cfake", c), c = ',';
375 if (flags & GCOV_ARC_FALLTHROUGH)
376 printf ("%cfall", c), c = ',';
377 printf (")");
383 static void
384 tag_lines (const char *filename ATTRIBUTE_UNUSED,
385 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
386 unsigned depth)
388 if (flag_dump_contents)
390 unsigned blockno = gcov_read_unsigned ();
391 char const *sep = NULL;
393 while (1)
395 gcov_position_t position = gcov_position ();
396 const char *source = NULL;
397 unsigned lineno = gcov_read_unsigned ();
399 if (!lineno)
401 source = gcov_read_string ();
402 if (!source)
403 break;
404 sep = NULL;
407 if (!sep)
409 printf ("\n");
410 print_prefix (filename, depth, position);
411 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
412 sep = "";
414 if (lineno)
416 printf ("%s%u", sep, lineno);
417 sep = ", ";
419 else
421 printf ("%s`%s'", sep, source);
422 sep = ":";
428 static void
429 tag_counters (const char *filename ATTRIBUTE_UNUSED,
430 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
431 unsigned depth)
433 #define DEF_GCOV_COUNTER(COUNTER, NAME, MERGE_FN) NAME,
434 static const char *const counter_names[] = {
435 #include "gcov-counter.def"
437 #undef DEF_GCOV_COUNTER
438 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
440 printf (" %s %u counts",
441 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts);
442 if (flag_dump_contents)
444 unsigned ix;
446 for (ix = 0; ix != n_counts; ix++)
448 gcov_type count;
450 if (!(ix & 7))
452 printf ("\n");
453 print_prefix (filename, depth, gcov_position ());
454 printf (VALUE_PADDING_PREFIX VALUE_PREFIX, ix);
457 count = gcov_read_counter ();
458 printf ("%" PRId64 " ", count);
463 static void
464 tag_summary (const char *filename ATTRIBUTE_UNUSED,
465 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
466 unsigned depth)
468 struct gcov_summary summary;
469 unsigned ix, h_ix;
470 gcov_bucket_type *histo_bucket;
472 gcov_read_summary (&summary);
473 printf (" checksum=0x%08x", summary.checksum);
475 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
477 printf ("\n");
478 print_prefix (filename, depth, 0);
479 printf (VALUE_PADDING_PREFIX "counts=%u, runs=%u",
480 summary.ctrs[ix].num, summary.ctrs[ix].runs);
482 printf (", sum_all=%" PRId64,
483 (int64_t)summary.ctrs[ix].sum_all);
484 printf (", run_max=%" PRId64,
485 (int64_t)summary.ctrs[ix].run_max);
486 printf (", sum_max=%" PRId64,
487 (int64_t)summary.ctrs[ix].sum_max);
488 if (ix != GCOV_COUNTER_ARCS)
489 continue;
490 printf ("\n");
491 print_prefix (filename, depth, 0);
492 printf (VALUE_PADDING_PREFIX "counter histogram:");
493 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
495 histo_bucket = &summary.ctrs[ix].histogram[h_ix];
496 if (!histo_bucket->num_counters)
497 continue;
498 printf ("\n");
499 print_prefix (filename, depth, 0);
500 printf (VALUE_PADDING_PREFIX VALUE_PREFIX "num counts=%u, "
501 "min counter=%" PRId64 ", cum_counter=%" PRId64,
502 h_ix, histo_bucket->num_counters,
503 (int64_t)histo_bucket->min_value,
504 (int64_t)histo_bucket->cum_value);
506 if (flag_dump_working_sets)
507 dump_working_sets (filename, &summary.ctrs[ix], depth);
511 static void
512 dump_working_sets (const char *filename ATTRIBUTE_UNUSED,
513 const struct gcov_ctr_summary *summary,
514 unsigned depth)
516 gcov_working_set_t gcov_working_sets[NUM_GCOV_WORKING_SETS];
517 unsigned ws_ix, pctinc, pct;
518 gcov_working_set_t *ws_info;
520 compute_working_sets (summary, gcov_working_sets);
522 printf ("\n");
523 print_prefix (filename, depth, 0);
524 printf (VALUE_PADDING_PREFIX "counter working sets:");
525 /* Multiply the percentage by 100 to avoid float. */
526 pctinc = 100 * 100 / NUM_GCOV_WORKING_SETS;
527 for (ws_ix = 0, pct = pctinc; ws_ix < NUM_GCOV_WORKING_SETS;
528 ws_ix++, pct += pctinc)
530 if (ws_ix == NUM_GCOV_WORKING_SETS - 1)
531 pct = 9990;
532 ws_info = &gcov_working_sets[ws_ix];
533 /* Print out the percentage using int arithmatic to avoid float. */
534 printf ("\n");
535 print_prefix (filename, depth + 1, 0);
536 printf (VALUE_PADDING_PREFIX "%u.%02u%%: num counts=%u, min counter="
537 "%" PRId64,
538 pct / 100, pct - (pct / 100 * 100),
539 ws_info->num_counters,
540 (int64_t)ws_info->min_counter);