Move PREFERRED_DEBUGGING_TYPE define in pa64-hpux.h to pa.h
[official-gcc.git] / gcc / gcov-dump.c
blobbfaf735d2ffb9f17ab846eaecbcd85c1efa34138
1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002-2021 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, int, unsigned);
36 static void tag_blocks (const char *, unsigned, int, unsigned);
37 static void tag_arcs (const char *, unsigned, int, unsigned);
38 static void tag_lines (const char *, unsigned, int, unsigned);
39 static void tag_counters (const char *, unsigned, int, unsigned);
40 static void tag_summary (const char *, unsigned, int, unsigned);
41 extern int main (int, char **);
43 typedef struct tag_format
45 unsigned tag;
46 char const *name;
47 void (*proc) (const char *, unsigned, int, unsigned);
48 } tag_format_t;
50 static int flag_dump_contents = 0;
51 static int flag_dump_positions = 0;
52 static int flag_dump_raw = 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 #define VALUE_PADDING_PREFIX " "
64 #define VALUE_PREFIX "%2d: "
66 static const tag_format_t tag_table[] =
68 {0, "NOP", NULL},
69 {0, "UNKNOWN", NULL},
70 {0, "COUNTERS", tag_counters},
71 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function},
72 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks},
73 {GCOV_TAG_ARCS, "ARCS", tag_arcs},
74 {GCOV_TAG_LINES, "LINES", tag_lines},
75 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary},
76 {0, NULL, NULL}
79 int
80 main (int argc ATTRIBUTE_UNUSED, char **argv)
82 int opt;
83 const char *p;
85 p = argv[0] + strlen (argv[0]);
86 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
87 --p;
88 progname = p;
90 xmalloc_set_program_name (progname);
92 /* Unlock the stdio streams. */
93 unlock_std_streams ();
95 gcc_init_libintl ();
97 diagnostic_initialize (global_dc, 0);
99 while ((opt = getopt_long (argc, argv, "hlprvw", options, NULL)) != -1)
101 switch (opt)
103 case 'h':
104 print_usage ();
105 break;
106 case 'v':
107 print_version ();
108 break;
109 case 'l':
110 flag_dump_contents = 1;
111 break;
112 case 'p':
113 flag_dump_positions = 1;
114 break;
115 case 'r':
116 flag_dump_raw = 1;
117 break;
118 default:
119 fprintf (stderr, "unknown flag `%c'\n", opt);
123 while (argv[optind])
124 dump_gcov_file (argv[optind++]);
125 return 0;
128 static void
129 print_usage (void)
131 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
132 printf ("Print coverage file contents\n");
133 printf (" -h, --help Print this help\n");
134 printf (" -l, --long Dump record contents too\n");
135 printf (" -p, --positions Dump record positions\n");
136 printf (" -r, --raw Print content records in raw format\n");
137 printf (" -v, --version Print version number\n");
138 printf ("\nFor bug reporting instructions, please see:\n%s.\n",
139 bug_report_url);
142 static void
143 print_version (void)
145 printf ("gcov-dump %s%s\n", pkgversion_string, version_string);
146 printf ("Copyright (C) 2021 Free Software Foundation, Inc.\n");
147 printf ("This is free software; see the source for copying conditions. There is NO\n\
148 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
151 static void
152 print_prefix (const char *filename, unsigned depth, gcov_position_t position)
154 static const char prefix[] = " ";
156 printf ("%s:", filename);
157 if (flag_dump_positions)
158 printf ("%5lu:", (unsigned long) position);
159 printf ("%.*s", (int) 2 * depth, prefix);
162 static void
163 dump_gcov_file (const char *filename)
165 unsigned tags[4];
166 unsigned depth = 0;
167 bool is_data_type;
169 if (!gcov_open (filename, 1))
171 fprintf (stderr, "%s:cannot open\n", filename);
172 return;
175 /* magic */
177 unsigned magic = gcov_read_unsigned ();
178 unsigned version;
179 int endianness = 0;
180 char m[4], v[4];
182 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
183 is_data_type = true;
184 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
185 is_data_type = false;
186 else
188 printf ("%s:not a gcov file\n", filename);
189 gcov_close ();
190 return;
192 version = gcov_read_unsigned ();
193 GCOV_UNSIGNED2STRING (v, version);
194 GCOV_UNSIGNED2STRING (m, magic);
196 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename,
197 is_data_type ? "data" : "note",
198 m, v, endianness < 0 ? " (swapped endianness)" : "");
199 if (version != GCOV_VERSION)
201 char e[4];
203 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
204 printf ("%s:warning:current version is `%.4s'\n", filename, e);
208 /* stamp */
209 unsigned stamp = gcov_read_unsigned ();
210 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
212 /* Checksum */
213 unsigned checksum = gcov_read_unsigned ();
214 printf ("%s:checksum %lu\n", filename, (unsigned long)checksum);
216 if (!is_data_type)
218 printf ("%s:cwd: %s\n", filename, gcov_read_string ());
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);
226 while (1)
228 gcov_position_t base, position = gcov_position ();
229 int read_length;
230 unsigned tag, length;
231 tag_format_t const *format;
232 unsigned tag_depth;
233 int error;
234 unsigned mask;
236 tag = gcov_read_unsigned ();
237 if (!tag)
238 break;
239 read_length = (int)gcov_read_unsigned ();
240 length = read_length > 0 ? read_length : 0;
241 base = gcov_position ();
242 mask = GCOV_TAG_MASK (tag) >> 1;
243 for (tag_depth = 4; mask; mask >>= 8)
245 if ((mask & 0xff) != 0xff)
247 printf ("%s:tag `%08x' is invalid\n", filename, tag);
248 break;
250 tag_depth--;
252 for (format = tag_table; format->name; format++)
253 if (format->tag == tag)
254 goto found;
255 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
256 found:;
257 if (tag)
259 if (depth && depth < tag_depth)
261 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
262 printf ("%s:tag `%08x' is incorrectly nested\n",
263 filename, tag);
265 depth = tag_depth;
266 tags[depth - 1] = tag;
269 print_prefix (filename, tag_depth, position);
270 printf ("%08x:%4u:%s", tag, abs (read_length), format->name);
271 if (format->proc)
272 (*format->proc) (filename, tag, read_length, depth);
274 printf ("\n");
275 if (flag_dump_contents && format->proc)
277 unsigned long actual_length = gcov_position () - base;
279 if (actual_length > length)
280 printf ("%s:record size mismatch %lu bytes overread\n",
281 filename, actual_length - length);
282 else if (length > actual_length)
283 printf ("%s:record size mismatch %lu bytes unread\n",
284 filename, length - actual_length);
286 gcov_sync (base, length);
287 if ((error = gcov_is_error ()))
289 printf (error < 0 ? "%s:counter overflow at %lu\n" :
290 "%s:read error at %lu\n", filename,
291 (long unsigned) gcov_position ());
292 break;
295 gcov_close ();
298 static void
299 tag_function (const char *filename ATTRIBUTE_UNUSED,
300 unsigned tag ATTRIBUTE_UNUSED, int length,
301 unsigned depth ATTRIBUTE_UNUSED)
303 gcov_position_t pos = gcov_position ();
305 if (!length)
306 printf (" placeholder");
307 else
309 printf (" ident=%u", gcov_read_unsigned ());
310 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
311 printf (", cfg_checksum=0x%08x", gcov_read_unsigned ());
313 if (gcov_position () - pos < (gcov_position_t) length)
315 const char *name;
317 name = gcov_read_string ();
318 printf (", `%s'", name ? name : "NULL");
319 unsigned artificial = gcov_read_unsigned ();
320 name = gcov_read_string ();
321 printf (" %s", name ? name : "NULL");
322 unsigned line_start = gcov_read_unsigned ();
323 unsigned column_start = gcov_read_unsigned ();
324 unsigned line_end = gcov_read_unsigned ();
325 unsigned column_end = gcov_read_unsigned ();
326 printf (":%u:%u-%u:%u", line_start, column_start,
327 line_end, column_end);
328 if (artificial)
329 printf (", artificial");
334 static void
335 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
336 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
337 unsigned depth ATTRIBUTE_UNUSED)
339 printf (" %u blocks", gcov_read_unsigned ());
342 static void
343 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
344 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
345 unsigned depth)
347 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
349 printf (" %u arcs", n_arcs);
350 if (flag_dump_contents)
352 unsigned ix;
353 unsigned blockno = gcov_read_unsigned ();
355 for (ix = 0; ix != n_arcs; ix++)
357 unsigned dst, flags;
359 if (!(ix & 3))
361 printf ("\n");
362 print_prefix (filename, depth, gcov_position ());
363 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
365 dst = gcov_read_unsigned ();
366 flags = gcov_read_unsigned ();
367 printf (" %u:%04x", dst, flags);
368 if (flags)
370 char c = '(';
372 if (flags & GCOV_ARC_ON_TREE)
373 printf ("%ctree", c), c = ',';
374 if (flags & GCOV_ARC_FAKE)
375 printf ("%cfake", c), c = ',';
376 if (flags & GCOV_ARC_FALLTHROUGH)
377 printf ("%cfall", c), c = ',';
378 printf (")");
384 static void
385 tag_lines (const char *filename ATTRIBUTE_UNUSED,
386 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
387 unsigned depth)
389 if (flag_dump_contents)
391 unsigned blockno = gcov_read_unsigned ();
392 char const *sep = NULL;
394 while (1)
396 gcov_position_t position = gcov_position ();
397 const char *source = NULL;
398 unsigned lineno = gcov_read_unsigned ();
400 if (!lineno)
402 source = gcov_read_string ();
403 if (!source)
404 break;
405 sep = NULL;
408 if (!sep)
410 printf ("\n");
411 print_prefix (filename, depth, position);
412 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
413 sep = "";
415 if (lineno)
417 printf ("%s%u", sep, lineno);
418 sep = ", ";
420 else
422 printf ("%s`%s'", sep, source);
423 sep = ":";
429 static void
430 tag_counters (const char *filename ATTRIBUTE_UNUSED,
431 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
432 unsigned depth)
434 #define DEF_GCOV_COUNTER(COUNTER, NAME, MERGE_FN) NAME,
435 static const char *const counter_names[] = {
436 #include "gcov-counter.def"
438 #undef DEF_GCOV_COUNTER
439 int n_counts = GCOV_TAG_COUNTER_NUM (length);
440 bool has_zeros = n_counts < 0;
441 n_counts = abs (n_counts);
443 printf (" %s %u counts%s",
444 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts,
445 has_zeros ? " (all zero)" : "");
446 if (flag_dump_contents)
448 for (int ix = 0; ix != n_counts; ix++)
450 gcov_type count;
452 if (flag_dump_raw)
454 if (ix == 0)
455 printf (": ");
457 else if (!(ix & 7))
459 printf ("\n");
460 print_prefix (filename, depth, gcov_position ());
461 printf (VALUE_PADDING_PREFIX VALUE_PREFIX, ix);
464 count = has_zeros ? 0 : gcov_read_counter ();
465 printf ("%" PRId64 " ", count);
470 static void
471 tag_summary (const char *filename ATTRIBUTE_UNUSED,
472 unsigned tag ATTRIBUTE_UNUSED, int length ATTRIBUTE_UNUSED,
473 unsigned depth ATTRIBUTE_UNUSED)
475 gcov_summary summary;
476 gcov_read_summary (&summary);
477 printf (" runs=%d, sum_max=%" PRId64,
478 summary.runs, summary.sum_max);