Make graph dumps use graphviz format
[official-gcc.git] / gcc / gcov-dump.cc
blob375058c3c7b766b9aa9cf22f4218ea4841b3f4a5
1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002-2024 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 #define INCLUDE_VECTOR
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.cc"
32 using namespace std;
34 static void dump_gcov_file (const char *);
35 static void print_prefix (const char *, unsigned, gcov_position_t);
36 static void print_usage (void);
37 static void print_version (void);
38 static void tag_function (const char *, unsigned, int, unsigned);
39 static void tag_blocks (const char *, unsigned, int, unsigned);
40 static void tag_arcs (const char *, unsigned, int, unsigned);
41 static void tag_conditions (const char *, unsigned, int, unsigned);
42 static void tag_lines (const char *, unsigned, int, unsigned);
43 static void tag_counters (const char *, unsigned, int, unsigned);
44 static void tag_summary (const char *, unsigned, int, unsigned);
45 extern int main (int, char **);
47 typedef struct tag_format
49 unsigned tag;
50 char const *name;
51 void (*proc) (const char *, unsigned, int, unsigned);
52 } tag_format_t;
54 static int flag_dump_contents = 0;
55 static int flag_dump_positions = 0;
56 static int flag_dump_raw = 0;
57 static int flag_dump_stable = 0;
59 static const struct option options[] =
61 { "help", no_argument, NULL, 'h' },
62 { "version", no_argument, NULL, 'v' },
63 { "long", no_argument, NULL, 'l' },
64 { "positions", no_argument, NULL, 'o' },
65 { "raw", no_argument, NULL, 'r' },
66 { "stable", no_argument, NULL, 's' },
70 #define VALUE_PADDING_PREFIX " "
71 #define VALUE_PREFIX "%2d: "
73 static const tag_format_t tag_table[] =
75 {0, "NOP", NULL},
76 {0, "UNKNOWN", NULL},
77 {0, "COUNTERS", tag_counters},
78 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function},
79 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks},
80 {GCOV_TAG_ARCS, "ARCS", tag_arcs},
81 {GCOV_TAG_CONDS, "CONDITIONS", tag_conditions},
82 {GCOV_TAG_LINES, "LINES", tag_lines},
83 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary},
84 {0, NULL, NULL}
87 int
88 main (int argc ATTRIBUTE_UNUSED, char **argv)
90 int opt;
91 const char *p;
93 p = argv[0] + strlen (argv[0]);
94 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
95 --p;
96 progname = p;
98 xmalloc_set_program_name (progname);
100 /* Unlock the stdio streams. */
101 unlock_std_streams ();
103 gcc_init_libintl ();
105 diagnostic_initialize (global_dc, 0);
107 while ((opt = getopt_long (argc, argv, "hlprsvw", options, NULL)) != -1)
109 switch (opt)
111 case 'h':
112 print_usage ();
113 break;
114 case 'v':
115 print_version ();
116 break;
117 case 'l':
118 flag_dump_contents = 1;
119 break;
120 case 'p':
121 flag_dump_positions = 1;
122 break;
123 case 'r':
124 flag_dump_raw = 1;
125 break;
126 case 's':
127 flag_dump_stable = 1;
128 break;
129 default:
130 fprintf (stderr, "unknown flag `%c'\n", opt);
134 while (argv[optind])
135 dump_gcov_file (argv[optind++]);
136 return 0;
139 static void
140 print_usage (void)
142 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
143 printf ("Print coverage file contents\n");
144 printf (" -h, --help Print this help\n");
145 printf (" -l, --long Dump record contents too\n");
146 printf (" -p, --positions Dump record positions\n");
147 printf (" -r, --raw Print content records in raw format\n");
148 printf (" -s, --stable Print content in stable "
149 "format usable for comparison\n");
150 printf (" -v, --version Print version number\n");
151 printf ("\nFor bug reporting instructions, please see:\n%s.\n",
152 bug_report_url);
155 static void
156 print_version (void)
158 printf ("gcov-dump %s%s\n", pkgversion_string, version_string);
159 printf ("Copyright (C) 2024 Free Software Foundation, Inc.\n");
160 printf ("This is free software; see the source for copying conditions. There is NO\n\
161 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
164 static void
165 print_prefix (const char *filename, unsigned depth, gcov_position_t position)
167 static const char prefix[] = " ";
169 printf ("%s:", filename);
170 if (flag_dump_positions)
171 printf ("%5lu:", (unsigned long) position);
172 printf ("%.*s", (int) 2 * depth, prefix);
175 static void
176 dump_gcov_file (const char *filename)
178 unsigned tags[4];
179 unsigned depth = 0;
180 bool is_data_type;
182 if (!gcov_open (filename, 1))
184 fprintf (stderr, "%s:cannot open\n", filename);
185 return;
188 /* magic */
190 unsigned magic = gcov_read_unsigned ();
191 unsigned version;
192 int endianness = 0;
193 char m[4], v[4];
195 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
196 is_data_type = true;
197 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
198 is_data_type = false;
199 else
201 printf ("%s:not a gcov file\n", filename);
202 gcov_close ();
203 return;
205 version = gcov_read_unsigned ();
206 GCOV_UNSIGNED2STRING (v, version);
207 GCOV_UNSIGNED2STRING (m, magic);
209 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename,
210 is_data_type ? "data" : "note",
211 m, v, endianness < 0 ? " (swapped endianness)" : "");
212 if (version != GCOV_VERSION)
214 char e[4];
216 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
217 printf ("%s:warning:current version is `%.4s'\n", filename, e);
221 /* stamp */
222 unsigned stamp = gcov_read_unsigned ();
223 printf ("%s:stamp %u\n", filename, stamp);
225 /* Checksum */
226 unsigned checksum = gcov_read_unsigned ();
227 printf ("%s:checksum %u\n", filename, checksum);
229 if (!is_data_type)
231 printf ("%s:cwd: %s\n", filename, gcov_read_string ());
233 /* Support for unexecuted basic blocks. */
234 unsigned support_unexecuted_blocks = gcov_read_unsigned ();
235 if (!support_unexecuted_blocks)
236 printf ("%s: has_unexecuted_block is not supported\n", filename);
239 while (1)
241 gcov_position_t base, position = gcov_position ();
242 int read_length;
243 unsigned tag, length;
244 tag_format_t const *format;
245 unsigned tag_depth;
246 int error;
247 unsigned mask;
249 tag = gcov_read_unsigned ();
250 if (!tag)
251 break;
252 read_length = (int)gcov_read_unsigned ();
253 length = read_length > 0 ? read_length : 0;
254 base = gcov_position ();
255 mask = GCOV_TAG_MASK (tag) >> 1;
256 for (tag_depth = 4; mask; mask >>= 8)
258 if ((mask & 0xff) != 0xff)
260 printf ("%s:tag `%08x' is invalid\n", filename, tag);
261 break;
263 tag_depth--;
265 for (format = tag_table; format->name; format++)
266 if (format->tag == tag)
267 goto found;
268 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
269 found:;
270 if (tag)
272 if (depth && depth < tag_depth)
274 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
275 printf ("%s:tag `%08x' is incorrectly nested\n",
276 filename, tag);
278 depth = tag_depth;
279 tags[depth - 1] = tag;
282 print_prefix (filename, tag_depth, position);
283 printf ("%08x:%4u:%s", tag, abs (read_length), format->name);
284 if (format->proc)
285 (*format->proc) (filename, tag, read_length, depth);
287 printf ("\n");
288 if (flag_dump_contents && format->proc)
290 unsigned long actual_length = gcov_position () - base;
292 if (actual_length > length)
293 printf ("%s:record size mismatch %lu bytes overread\n",
294 filename, actual_length - length);
295 else if (length > actual_length)
296 printf ("%s:record size mismatch %lu bytes unread\n",
297 filename, length - actual_length);
299 gcov_sync (base, length);
300 if ((error = gcov_is_error ()))
302 printf (error < 0 ? "%s:counter overflow at %lu\n" :
303 "%s:read error at %lu\n", filename,
304 (long unsigned) gcov_position ());
305 break;
308 gcov_close ();
311 static void
312 tag_function (const char *filename ATTRIBUTE_UNUSED,
313 unsigned tag ATTRIBUTE_UNUSED, int length,
314 unsigned depth ATTRIBUTE_UNUSED)
316 gcov_position_t pos = gcov_position ();
318 if (!length)
319 printf (" placeholder");
320 else
322 printf (" ident=%u", gcov_read_unsigned ());
323 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
324 printf (", cfg_checksum=0x%08x", gcov_read_unsigned ());
326 if (gcov_position () - pos < (gcov_position_t) length)
328 const char *name;
330 name = gcov_read_string ();
331 printf (", `%s'", name ? name : "NULL");
332 unsigned artificial = gcov_read_unsigned ();
333 name = gcov_read_string ();
334 printf (" %s", name ? name : "NULL");
335 unsigned line_start = gcov_read_unsigned ();
336 unsigned column_start = gcov_read_unsigned ();
337 unsigned line_end = gcov_read_unsigned ();
338 unsigned column_end = gcov_read_unsigned ();
339 printf (":%u:%u-%u:%u", line_start, column_start,
340 line_end, column_end);
341 if (artificial)
342 printf (", artificial");
347 static void
348 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
349 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
350 unsigned depth ATTRIBUTE_UNUSED)
352 printf (" %u blocks", gcov_read_unsigned ());
355 static void
356 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
357 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
358 unsigned depth)
360 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
362 printf (" %u arcs", n_arcs);
363 if (flag_dump_contents)
365 unsigned ix;
366 unsigned blockno = gcov_read_unsigned ();
368 for (ix = 0; ix != n_arcs; ix++)
370 unsigned dst, flags;
372 if (!(ix & 3))
374 printf ("\n");
375 print_prefix (filename, depth, gcov_position ());
376 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
378 dst = gcov_read_unsigned ();
379 flags = gcov_read_unsigned ();
380 printf (" %u:%04x", dst, flags);
381 if (flags)
383 char c = '(';
385 if (flags & GCOV_ARC_ON_TREE)
386 printf ("%ctree", c), c = ',';
387 if (flags & GCOV_ARC_FAKE)
388 printf ("%cfake", c), c = ',';
389 if (flags & GCOV_ARC_FALLTHROUGH)
390 printf ("%cfall", c), c = ',';
391 printf (")");
397 /* Print number of conditions (not outcomes, i.e. if (x && y) is 2, not 4). */
398 static void
399 tag_conditions (const char *filename, unsigned /* tag */, int length,
400 unsigned depth)
402 unsigned n_conditions = GCOV_TAG_CONDS_NUM (length);
404 printf (" %u conditions", n_conditions);
405 if (flag_dump_contents)
407 for (unsigned ix = 0; ix != n_conditions; ix++)
409 const unsigned blockno = gcov_read_unsigned ();
410 const unsigned nterms = gcov_read_unsigned ();
412 printf ("\n");
413 print_prefix (filename, depth, gcov_position ());
414 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
415 printf (" %u", nterms);
419 static void
420 tag_lines (const char *filename ATTRIBUTE_UNUSED,
421 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
422 unsigned depth)
424 if (flag_dump_contents)
426 unsigned blockno = gcov_read_unsigned ();
427 char const *sep = NULL;
429 while (1)
431 gcov_position_t position = gcov_position ();
432 const char *source = NULL;
433 unsigned lineno = gcov_read_unsigned ();
435 if (!lineno)
437 source = gcov_read_string ();
438 if (!source)
439 break;
440 sep = NULL;
443 if (!sep)
445 printf ("\n");
446 print_prefix (filename, depth, position);
447 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
448 sep = "";
450 if (lineno)
452 printf ("%s%u", sep, lineno);
453 sep = ", ";
455 else
457 printf ("%s`%s'", sep, source);
458 sep = ":";
464 static void
465 tag_counters (const char *filename ATTRIBUTE_UNUSED,
466 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
467 unsigned depth)
469 #define DEF_GCOV_COUNTER(COUNTER, NAME, MERGE_FN) NAME,
470 static const char *const counter_names[] = {
471 #include "gcov-counter.def"
473 #undef DEF_GCOV_COUNTER
474 int n_counts = GCOV_TAG_COUNTER_NUM (length);
475 bool has_zeros = n_counts < 0;
476 n_counts = abs (n_counts);
477 unsigned counter_idx = GCOV_COUNTER_FOR_TAG (tag);
479 printf (" %s %u counts%s",
480 counter_names[counter_idx], n_counts,
481 has_zeros ? " (all zero)" : "");
482 if (flag_dump_contents)
484 vector<gcov_type> counters;
485 for (int ix = 0; ix != n_counts; ix++)
486 counters.push_back (has_zeros ? 0 : gcov_read_counter ());
488 /* Make stable sort for TOP N counters. */
489 if (flag_dump_stable)
490 if (counter_idx == GCOV_COUNTER_V_INDIR
491 || counter_idx == GCOV_COUNTER_V_TOPN)
493 unsigned start = 0;
494 while (start < counters.size ())
496 unsigned n = counters[start + 1];
498 /* Use bubble sort. */
499 for (unsigned i = 1; i <= n; ++i)
500 for (unsigned j = i; j <= n; ++j)
502 gcov_type key1 = counters[start + 2 * i];
503 gcov_type value1 = counters[start + 2 * i + 1];
504 gcov_type key2 = counters[start + 2 * j];
505 gcov_type value2 = counters[start + 2 * j + 1];
507 if (value1 < value2 || (value1 == value2 && key1 < key2))
509 std::swap (counters[start + 2 * i],
510 counters[start + 2 * j]);
511 std::swap (counters[start + 2 * i + 1],
512 counters[start + 2 * j + 1]);
515 start += 2 * (n + 1);
517 if (start != counters.size ())
518 abort ();
521 for (unsigned ix = 0; ix < counters.size (); ++ix)
523 if (flag_dump_raw)
525 if (ix == 0)
526 printf (": ");
528 else if (!(ix & 7))
530 printf ("\n");
531 print_prefix (filename, depth, gcov_position ());
532 printf (VALUE_PADDING_PREFIX VALUE_PREFIX, ix);
535 printf ("%" PRId64 " ", counters[ix]);
540 static void
541 tag_summary (const char *filename ATTRIBUTE_UNUSED,
542 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
543 unsigned depth ATTRIBUTE_UNUSED)
545 gcov_summary summary;
546 gcov_read_summary (&summary);
547 printf (" runs=%d, sum_max=%" PRId64,
548 summary.runs, summary.sum_max);