Skip various cmp-mem-const tests on lp64 hppa*-*-*
[official-gcc.git] / gcc / gcov-dump.cc
blob167b6f508f6bfff061b60a5cc7f3ef03f4a740ee
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_lines (const char *, unsigned, int, unsigned);
42 static void tag_counters (const char *, unsigned, int, unsigned);
43 static void tag_summary (const char *, unsigned, int, unsigned);
44 extern int main (int, char **);
46 typedef struct tag_format
48 unsigned tag;
49 char const *name;
50 void (*proc) (const char *, unsigned, int, unsigned);
51 } tag_format_t;
53 static int flag_dump_contents = 0;
54 static int flag_dump_positions = 0;
55 static int flag_dump_raw = 0;
56 static int flag_dump_stable = 0;
58 static const struct option options[] =
60 { "help", no_argument, NULL, 'h' },
61 { "version", no_argument, NULL, 'v' },
62 { "long", no_argument, NULL, 'l' },
63 { "positions", no_argument, NULL, 'o' },
64 { "raw", no_argument, NULL, 'r' },
65 { "stable", no_argument, NULL, 's' },
69 #define VALUE_PADDING_PREFIX " "
70 #define VALUE_PREFIX "%2d: "
72 static const tag_format_t tag_table[] =
74 {0, "NOP", NULL},
75 {0, "UNKNOWN", NULL},
76 {0, "COUNTERS", tag_counters},
77 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function},
78 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks},
79 {GCOV_TAG_ARCS, "ARCS", tag_arcs},
80 {GCOV_TAG_LINES, "LINES", tag_lines},
81 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary},
82 {0, NULL, NULL}
85 int
86 main (int argc ATTRIBUTE_UNUSED, char **argv)
88 int opt;
89 const char *p;
91 p = argv[0] + strlen (argv[0]);
92 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
93 --p;
94 progname = p;
96 xmalloc_set_program_name (progname);
98 /* Unlock the stdio streams. */
99 unlock_std_streams ();
101 gcc_init_libintl ();
103 diagnostic_initialize (global_dc, 0);
105 while ((opt = getopt_long (argc, argv, "hlprsvw", options, NULL)) != -1)
107 switch (opt)
109 case 'h':
110 print_usage ();
111 break;
112 case 'v':
113 print_version ();
114 break;
115 case 'l':
116 flag_dump_contents = 1;
117 break;
118 case 'p':
119 flag_dump_positions = 1;
120 break;
121 case 'r':
122 flag_dump_raw = 1;
123 break;
124 case 's':
125 flag_dump_stable = 1;
126 break;
127 default:
128 fprintf (stderr, "unknown flag `%c'\n", opt);
132 while (argv[optind])
133 dump_gcov_file (argv[optind++]);
134 return 0;
137 static void
138 print_usage (void)
140 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
141 printf ("Print coverage file contents\n");
142 printf (" -h, --help Print this help\n");
143 printf (" -l, --long Dump record contents too\n");
144 printf (" -p, --positions Dump record positions\n");
145 printf (" -r, --raw Print content records in raw format\n");
146 printf (" -s, --stable Print content in stable "
147 "format usable for comparison\n");
148 printf (" -v, --version Print version number\n");
149 printf ("\nFor bug reporting instructions, please see:\n%s.\n",
150 bug_report_url);
153 static void
154 print_version (void)
156 printf ("gcov-dump %s%s\n", pkgversion_string, version_string);
157 printf ("Copyright (C) 2024 Free Software Foundation, Inc.\n");
158 printf ("This is free software; see the source for copying conditions. There is NO\n\
159 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
162 static void
163 print_prefix (const char *filename, unsigned depth, gcov_position_t position)
165 static const char prefix[] = " ";
167 printf ("%s:", filename);
168 if (flag_dump_positions)
169 printf ("%5lu:", (unsigned long) position);
170 printf ("%.*s", (int) 2 * depth, prefix);
173 static void
174 dump_gcov_file (const char *filename)
176 unsigned tags[4];
177 unsigned depth = 0;
178 bool is_data_type;
180 if (!gcov_open (filename, 1))
182 fprintf (stderr, "%s:cannot open\n", filename);
183 return;
186 /* magic */
188 unsigned magic = gcov_read_unsigned ();
189 unsigned version;
190 int endianness = 0;
191 char m[4], v[4];
193 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
194 is_data_type = true;
195 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
196 is_data_type = false;
197 else
199 printf ("%s:not a gcov file\n", filename);
200 gcov_close ();
201 return;
203 version = gcov_read_unsigned ();
204 GCOV_UNSIGNED2STRING (v, version);
205 GCOV_UNSIGNED2STRING (m, magic);
207 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename,
208 is_data_type ? "data" : "note",
209 m, v, endianness < 0 ? " (swapped endianness)" : "");
210 if (version != GCOV_VERSION)
212 char e[4];
214 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
215 printf ("%s:warning:current version is `%.4s'\n", filename, e);
219 /* stamp */
220 unsigned stamp = gcov_read_unsigned ();
221 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
223 /* Checksum */
224 unsigned checksum = gcov_read_unsigned ();
225 printf ("%s:checksum %lu\n", filename, (unsigned long)checksum);
227 if (!is_data_type)
229 printf ("%s:cwd: %s\n", filename, gcov_read_string ());
231 /* Support for unexecuted basic blocks. */
232 unsigned support_unexecuted_blocks = gcov_read_unsigned ();
233 if (!support_unexecuted_blocks)
234 printf ("%s: has_unexecuted_block is not supported\n", filename);
237 while (1)
239 gcov_position_t base, position = gcov_position ();
240 int read_length;
241 unsigned tag, length;
242 tag_format_t const *format;
243 unsigned tag_depth;
244 int error;
245 unsigned mask;
247 tag = gcov_read_unsigned ();
248 if (!tag)
249 break;
250 read_length = (int)gcov_read_unsigned ();
251 length = read_length > 0 ? read_length : 0;
252 base = gcov_position ();
253 mask = GCOV_TAG_MASK (tag) >> 1;
254 for (tag_depth = 4; mask; mask >>= 8)
256 if ((mask & 0xff) != 0xff)
258 printf ("%s:tag `%08x' is invalid\n", filename, tag);
259 break;
261 tag_depth--;
263 for (format = tag_table; format->name; format++)
264 if (format->tag == tag)
265 goto found;
266 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
267 found:;
268 if (tag)
270 if (depth && depth < tag_depth)
272 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
273 printf ("%s:tag `%08x' is incorrectly nested\n",
274 filename, tag);
276 depth = tag_depth;
277 tags[depth - 1] = tag;
280 print_prefix (filename, tag_depth, position);
281 printf ("%08x:%4u:%s", tag, abs (read_length), format->name);
282 if (format->proc)
283 (*format->proc) (filename, tag, read_length, depth);
285 printf ("\n");
286 if (flag_dump_contents && format->proc)
288 unsigned long actual_length = gcov_position () - base;
290 if (actual_length > length)
291 printf ("%s:record size mismatch %lu bytes overread\n",
292 filename, actual_length - length);
293 else if (length > actual_length)
294 printf ("%s:record size mismatch %lu bytes unread\n",
295 filename, length - actual_length);
297 gcov_sync (base, length);
298 if ((error = gcov_is_error ()))
300 printf (error < 0 ? "%s:counter overflow at %lu\n" :
301 "%s:read error at %lu\n", filename,
302 (long unsigned) gcov_position ());
303 break;
306 gcov_close ();
309 static void
310 tag_function (const char *filename ATTRIBUTE_UNUSED,
311 unsigned tag ATTRIBUTE_UNUSED, int length,
312 unsigned depth ATTRIBUTE_UNUSED)
314 gcov_position_t pos = gcov_position ();
316 if (!length)
317 printf (" placeholder");
318 else
320 printf (" ident=%u", gcov_read_unsigned ());
321 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
322 printf (", cfg_checksum=0x%08x", gcov_read_unsigned ());
324 if (gcov_position () - pos < (gcov_position_t) length)
326 const char *name;
328 name = gcov_read_string ();
329 printf (", `%s'", name ? name : "NULL");
330 unsigned artificial = gcov_read_unsigned ();
331 name = gcov_read_string ();
332 printf (" %s", name ? name : "NULL");
333 unsigned line_start = gcov_read_unsigned ();
334 unsigned column_start = gcov_read_unsigned ();
335 unsigned line_end = gcov_read_unsigned ();
336 unsigned column_end = gcov_read_unsigned ();
337 printf (":%u:%u-%u:%u", line_start, column_start,
338 line_end, column_end);
339 if (artificial)
340 printf (", artificial");
345 static void
346 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
347 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
348 unsigned depth ATTRIBUTE_UNUSED)
350 printf (" %u blocks", gcov_read_unsigned ());
353 static void
354 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
355 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
356 unsigned depth)
358 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
360 printf (" %u arcs", n_arcs);
361 if (flag_dump_contents)
363 unsigned ix;
364 unsigned blockno = gcov_read_unsigned ();
366 for (ix = 0; ix != n_arcs; ix++)
368 unsigned dst, flags;
370 if (!(ix & 3))
372 printf ("\n");
373 print_prefix (filename, depth, gcov_position ());
374 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
376 dst = gcov_read_unsigned ();
377 flags = gcov_read_unsigned ();
378 printf (" %u:%04x", dst, flags);
379 if (flags)
381 char c = '(';
383 if (flags & GCOV_ARC_ON_TREE)
384 printf ("%ctree", c), c = ',';
385 if (flags & GCOV_ARC_FAKE)
386 printf ("%cfake", c), c = ',';
387 if (flags & GCOV_ARC_FALLTHROUGH)
388 printf ("%cfall", c), c = ',';
389 printf (")");
395 static void
396 tag_lines (const char *filename ATTRIBUTE_UNUSED,
397 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
398 unsigned depth)
400 if (flag_dump_contents)
402 unsigned blockno = gcov_read_unsigned ();
403 char const *sep = NULL;
405 while (1)
407 gcov_position_t position = gcov_position ();
408 const char *source = NULL;
409 unsigned lineno = gcov_read_unsigned ();
411 if (!lineno)
413 source = gcov_read_string ();
414 if (!source)
415 break;
416 sep = NULL;
419 if (!sep)
421 printf ("\n");
422 print_prefix (filename, depth, position);
423 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
424 sep = "";
426 if (lineno)
428 printf ("%s%u", sep, lineno);
429 sep = ", ";
431 else
433 printf ("%s`%s'", sep, source);
434 sep = ":";
440 static void
441 tag_counters (const char *filename ATTRIBUTE_UNUSED,
442 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
443 unsigned depth)
445 #define DEF_GCOV_COUNTER(COUNTER, NAME, MERGE_FN) NAME,
446 static const char *const counter_names[] = {
447 #include "gcov-counter.def"
449 #undef DEF_GCOV_COUNTER
450 int n_counts = GCOV_TAG_COUNTER_NUM (length);
451 bool has_zeros = n_counts < 0;
452 n_counts = abs (n_counts);
453 unsigned counter_idx = GCOV_COUNTER_FOR_TAG (tag);
455 printf (" %s %u counts%s",
456 counter_names[counter_idx], n_counts,
457 has_zeros ? " (all zero)" : "");
458 if (flag_dump_contents)
460 vector<gcov_type> counters;
461 for (int ix = 0; ix != n_counts; ix++)
462 counters.push_back (has_zeros ? 0 : gcov_read_counter ());
464 /* Make stable sort for TOP N counters. */
465 if (flag_dump_stable)
466 if (counter_idx == GCOV_COUNTER_V_INDIR
467 || counter_idx == GCOV_COUNTER_V_TOPN)
469 unsigned start = 0;
470 while (start < counters.size ())
472 unsigned n = counters[start + 1];
474 /* Use bubble sort. */
475 for (unsigned i = 1; i <= n; ++i)
476 for (unsigned j = i; j <= n; ++j)
478 gcov_type key1 = counters[start + 2 * i];
479 gcov_type value1 = counters[start + 2 * i + 1];
480 gcov_type key2 = counters[start + 2 * j];
481 gcov_type value2 = counters[start + 2 * j + 1];
483 if (value1 < value2 || (value1 == value2 && key1 < key2))
485 std::swap (counters[start + 2 * i],
486 counters[start + 2 * j]);
487 std::swap (counters[start + 2 * i + 1],
488 counters[start + 2 * j + 1]);
491 start += 2 * (n + 1);
493 if (start != counters.size ())
494 abort ();
497 for (unsigned ix = 0; ix < counters.size (); ++ix)
499 if (flag_dump_raw)
501 if (ix == 0)
502 printf (": ");
504 else if (!(ix & 7))
506 printf ("\n");
507 print_prefix (filename, depth, gcov_position ());
508 printf (VALUE_PADDING_PREFIX VALUE_PREFIX, ix);
511 printf ("%" PRId64 " ", counters[ix]);
516 static void
517 tag_summary (const char *filename ATTRIBUTE_UNUSED,
518 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
519 unsigned depth ATTRIBUTE_UNUSED)
521 gcov_summary summary;
522 gcov_read_summary (&summary);
523 printf (" runs=%d, sum_max=%" PRId64,
524 summary.runs, summary.sum_max);