]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gcov-dump.c
2019-06-13 Steven G. Kargl <kargl@gcc.gnu.org>
[thirdparty/gcc.git] / gcc / gcov-dump.c
CommitLineData
805e22b2 1/* Dump a gcov file, for debugging use.
fbd26352 2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
805e22b2 3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
4
5Gcov is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
8c4c00c1 7the Free Software Foundation; either version 3, or (at your option)
805e22b2 8any later version.
9
10Gcov is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
8c4c00c1 16along with Gcov; see the file COPYING3. If not see
17<http://www.gnu.org/licenses/>. */
805e22b2 18
19#include "config.h"
20#include "system.h"
21#include "coretypes.h"
22#include "tm.h"
23#include "version.h"
0b4f4daf 24#include "intl.h"
25#include "diagnostic.h"
805e22b2 26#include <getopt.h>
9219dd0a 27#define IN_GCOV (-1)
805e22b2 28#include "gcov-io.h"
44359ced 29#include "gcov-io.c"
805e22b2 30
b5369b7d 31static void dump_gcov_file (const char *);
952f0048 32static void print_prefix (const char *, unsigned, gcov_position_t);
33static void print_usage (void);
34static void print_version (void);
287ac61d 35static void tag_function (const char *, unsigned, unsigned, unsigned);
36static void tag_blocks (const char *, unsigned, unsigned, unsigned);
37static void tag_arcs (const char *, unsigned, unsigned, unsigned);
38static void tag_lines (const char *, unsigned, unsigned, unsigned);
39static void tag_counters (const char *, unsigned, unsigned, unsigned);
40static void tag_summary (const char *, unsigned, unsigned, unsigned);
952f0048 41extern int main (int, char **);
805e22b2 42
43typedef struct tag_format
44{
45 unsigned tag;
46 char const *name;
287ac61d 47 void (*proc) (const char *, unsigned, unsigned, unsigned);
805e22b2 48} tag_format_t;
49
50static int flag_dump_contents = 0;
80abd9e4 51static int flag_dump_positions = 0;
805e22b2 52
53static const struct option options[] =
54{
55 { "help", no_argument, NULL, 'h' },
56 { "version", no_argument, NULL, 'v' },
57 { "long", no_argument, NULL, 'l' },
80abd9e4 58 { "positions", no_argument, NULL, 'o' },
4eb7b61f 59 { 0, 0, 0, 0 }
805e22b2 60};
61
287ac61d 62#define VALUE_PADDING_PREFIX " "
63#define VALUE_PREFIX "%2d: "
64
fd956ee4 65static const tag_format_t tag_table[] =
805e22b2 66{
67 {0, "NOP", NULL},
68 {0, "UNKNOWN", NULL},
ab6a34f2 69 {0, "COUNTERS", tag_counters},
805e22b2 70 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function},
71 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks},
72 {GCOV_TAG_ARCS, "ARCS", tag_arcs},
73 {GCOV_TAG_LINES, "LINES", tag_lines},
805e22b2 74 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary},
805e22b2 75 {0, NULL, NULL}
76};
77
952f0048 78int
79main (int argc ATTRIBUTE_UNUSED, char **argv)
805e22b2 80{
81 int opt;
0b4f4daf 82 const char *p;
83
84 p = argv[0] + strlen (argv[0]);
85 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
86 --p;
87 progname = p;
88
89 xmalloc_set_program_name (progname);
805e22b2 90
4367c81f 91 /* Unlock the stdio streams. */
9c8f076b 92 unlock_std_streams ();
4367c81f 93
0b4f4daf 94 gcc_init_libintl ();
95
96 diagnostic_initialize (global_dc, 0);
97
8515a84d 98 while ((opt = getopt_long (argc, argv, "hlpvw", options, NULL)) != -1)
805e22b2 99 {
100 switch (opt)
101 {
102 case 'h':
103 print_usage ();
104 break;
105 case 'v':
106 print_version ();
107 break;
108 case 'l':
109 flag_dump_contents = 1;
110 break;
80abd9e4 111 case 'p':
112 flag_dump_positions = 1;
113 break;
805e22b2 114 default:
115 fprintf (stderr, "unknown flag `%c'\n", opt);
116 }
117 }
952f0048 118
805e22b2 119 while (argv[optind])
b5369b7d 120 dump_gcov_file (argv[optind++]);
805e22b2 121 return 0;
122}
123
124static void
952f0048 125print_usage (void)
805e22b2 126{
127 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
128 printf ("Print coverage file contents\n");
129 printf (" -h, --help Print this help\n");
805e22b2 130 printf (" -l, --long Dump record contents too\n");
80abd9e4 131 printf (" -p, --positions Dump record positions\n");
b97749b0 132 printf (" -v, --version Print version number\n");
5e01e71b 133 printf ("\nFor bug reporting instructions, please see:\n%s.\n",
134 bug_report_url);
805e22b2 135}
136
137static void
952f0048 138print_version (void)
805e22b2 139{
d25dc80e 140 printf ("gcov-dump %s%s\n", pkgversion_string, version_string);
3d8932fd 141 printf ("Copyright (C) 2019 Free Software Foundation, Inc.\n");
9e0943d4 142 printf ("This is free software; see the source for copying conditions.\n"
143 "There is NO warranty; not even for MERCHANTABILITY or \n"
144 "FITNESS FOR A PARTICULAR PURPOSE.\n\n");
805e22b2 145}
146
147static void
952f0048 148print_prefix (const char *filename, unsigned depth, gcov_position_t position)
805e22b2 149{
150 static const char prefix[] = " ";
952f0048 151
80abd9e4 152 printf ("%s:", filename);
153 if (flag_dump_positions)
287ac61d 154 printf ("%5lu:", (unsigned long) position);
155 printf ("%.*s", (int) 2 * depth, prefix);
805e22b2 156}
157
158static void
b5369b7d 159dump_gcov_file (const char *filename)
805e22b2 160{
805e22b2 161 unsigned tags[4];
162 unsigned depth = 0;
2074ebe7 163 bool is_data_type;
952f0048 164
9219dd0a 165 if (!gcov_open (filename, 1))
805e22b2 166 {
167 fprintf (stderr, "%s:cannot open\n", filename);
168 return;
169 }
952f0048 170
805e22b2 171 /* magic */
172 {
f187042d 173 unsigned magic = gcov_read_unsigned ();
b4d48d67 174 unsigned version;
b4d48d67 175 int endianness = 0;
9e0943d4 176 char m[4], v[4];
48e1416a 177
b4d48d67 178 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
2074ebe7 179 is_data_type = true;
b4d48d67 180 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
2074ebe7 181 is_data_type = false;
805e22b2 182 else
183 {
184 printf ("%s:not a gcov file\n", filename);
9219dd0a 185 gcov_close ();
805e22b2 186 return;
187 }
b4d48d67 188 version = gcov_read_unsigned ();
9e0943d4 189 GCOV_UNSIGNED2STRING (v, version);
190 GCOV_UNSIGNED2STRING (m, magic);
48e1416a 191
2074ebe7 192 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename,
193 is_data_type ? "data" : "note",
9e0943d4 194 m, v, endianness < 0 ? " (swapped endianness)" : "");
b4d48d67 195 if (version != GCOV_VERSION)
805e22b2 196 {
9e0943d4 197 char e[4];
48e1416a 198
9e0943d4 199 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
200 printf ("%s:warning:current version is `%.4s'\n", filename, e);
805e22b2 201 }
805e22b2 202 }
203
42fa384f 204 /* stamp */
205 {
206 unsigned stamp = gcov_read_unsigned ();
207
208 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
209 }
48e1416a 210
2074ebe7 211 if (!is_data_type)
212 {
47e77da0 213 printf ("%s:cwd: %s\n", filename, gcov_read_string ());
214
2074ebe7 215 /* Support for unexecuted basic blocks. */
216 unsigned support_unexecuted_blocks = gcov_read_unsigned ();
217 if (!support_unexecuted_blocks)
218 printf ("%s: has_unexecuted_block is not supported\n", filename);
219 }
fa6f8ae7 220
80abd9e4 221 while (1)
805e22b2 222 {
80abd9e4 223 gcov_position_t base, position = gcov_position ();
224 unsigned tag, length;
805e22b2 225 tag_format_t const *format;
226 unsigned tag_depth;
f187042d 227 int error;
80abd9e4 228 unsigned mask;
229
230 tag = gcov_read_unsigned ();
805e22b2 231 if (!tag)
80abd9e4 232 break;
233 length = gcov_read_unsigned ();
234 base = gcov_position ();
235 mask = GCOV_TAG_MASK (tag) >> 1;
236 for (tag_depth = 4; mask; mask >>= 8)
805e22b2 237 {
80abd9e4 238 if ((mask & 0xff) != 0xff)
805e22b2 239 {
80abd9e4 240 printf ("%s:tag `%08x' is invalid\n", filename, tag);
241 break;
805e22b2 242 }
80abd9e4 243 tag_depth--;
805e22b2 244 }
245 for (format = tag_table; format->name; format++)
246 if (format->tag == tag)
247 goto found;
ab6a34f2 248 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
805e22b2 249 found:;
250 if (tag)
251 {
252 if (depth && depth < tag_depth)
253 {
254 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
255 printf ("%s:tag `%08x' is incorrectly nested\n",
256 filename, tag);
257 }
258 depth = tag_depth;
259 tags[depth - 1] = tag;
260 }
952f0048 261
80abd9e4 262 print_prefix (filename, tag_depth, position);
805e22b2 263 printf ("%08x:%4u:%s", tag, length, format->name);
264 if (format->proc)
287ac61d 265 (*format->proc) (filename, tag, length, depth);
952f0048 266
805e22b2 267 printf ("\n");
f187042d 268 if (flag_dump_contents && format->proc)
805e22b2 269 {
f187042d 270 unsigned long actual_length = gcov_position () - base;
952f0048 271
f187042d 272 if (actual_length > length)
805e22b2 273 printf ("%s:record size mismatch %lu bytes overread\n",
f187042d 274 filename, actual_length - length);
275 else if (length > actual_length)
805e22b2 276 printf ("%s:record size mismatch %lu bytes unread\n",
f187042d 277 filename, length - actual_length);
278 }
3ddf7676 279 gcov_sync (base, length);
f187042d 280 if ((error = gcov_is_error ()))
281 {
282 printf (error < 0 ? "%s:counter overflow at %lu\n" :
834f169c 283 "%s:read error at %lu\n", filename,
284 (long unsigned) gcov_position ());
f187042d 285 break;
805e22b2 286 }
287 }
9219dd0a 288 gcov_close ();
805e22b2 289}
290
f187042d 291static void
952f0048 292tag_function (const char *filename ATTRIBUTE_UNUSED,
287ac61d 293 unsigned tag ATTRIBUTE_UNUSED, unsigned length,
294 unsigned depth ATTRIBUTE_UNUSED)
805e22b2 295{
f187042d 296 unsigned long pos = gcov_position ();
952f0048 297
5014df5d 298 if (!length)
299 printf (" placeholder");
300 else
f187042d 301 {
5014df5d 302 printf (" ident=%u", gcov_read_unsigned ());
303 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
f938168e 304 printf (", cfg_checksum=0x%08x", gcov_read_unsigned ());
952f0048 305
5014df5d 306 if (gcov_position () - pos < length)
307 {
308 const char *name;
309
310 name = gcov_read_string ();
311 printf (", `%s'", name ? name : "NULL");
a82502dd 312 unsigned artificial = gcov_read_unsigned ();
5014df5d 313 name = gcov_read_string ();
314 printf (" %s", name ? name : "NULL");
a82502dd 315 unsigned line_start = gcov_read_unsigned ();
316 unsigned column_start = gcov_read_unsigned ();
317 unsigned line_end = gcov_read_unsigned ();
4e9fa49b 318 unsigned column_end = gcov_read_unsigned ();
319 printf (":%u:%u-%u:%u", line_start, column_start,
320 line_end, column_end);
a82502dd 321 if (artificial)
322 printf (", artificial");
5014df5d 323 }
f187042d 324 }
805e22b2 325}
326
f187042d 327static void
952f0048 328tag_blocks (const char *filename ATTRIBUTE_UNUSED,
287ac61d 329 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
8af8bb9c 330 unsigned depth ATTRIBUTE_UNUSED)
805e22b2 331{
8af8bb9c 332 printf (" %u blocks", gcov_read_unsigned ());
805e22b2 333}
334
f187042d 335static void
952f0048 336tag_arcs (const char *filename ATTRIBUTE_UNUSED,
287ac61d 337 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
338 unsigned depth)
805e22b2 339{
9e0943d4 340 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
805e22b2 341
342 printf (" %u arcs", n_arcs);
343 if (flag_dump_contents)
344 {
345 unsigned ix;
f187042d 346 unsigned blockno = gcov_read_unsigned ();
805e22b2 347
348 for (ix = 0; ix != n_arcs; ix++)
349 {
80abd9e4 350 unsigned dst, flags;
952f0048 351
805e22b2 352 if (!(ix & 3))
80abd9e4 353 {
354 printf ("\n");
287ac61d 355 print_prefix (filename, depth, gcov_position ());
356 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
80abd9e4 357 }
358 dst = gcov_read_unsigned ();
359 flags = gcov_read_unsigned ();
805e22b2 360 printf (" %u:%04x", dst, flags);
87ddc00c 361 if (flags)
362 {
363 char c = '(';
364
365 if (flags & GCOV_ARC_ON_TREE)
366 printf ("%ctree", c), c = ',';
367 if (flags & GCOV_ARC_FAKE)
368 printf ("%cfake", c), c = ',';
369 if (flags & GCOV_ARC_FALLTHROUGH)
370 printf ("%cfall", c), c = ',';
371 printf (")");
372 }
805e22b2 373 }
374 }
805e22b2 375}
376
f187042d 377static void
952f0048 378tag_lines (const char *filename ATTRIBUTE_UNUSED,
287ac61d 379 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
380 unsigned depth)
805e22b2 381{
382 if (flag_dump_contents)
383 {
f187042d 384 unsigned blockno = gcov_read_unsigned ();
805e22b2 385 char const *sep = NULL;
386
805e22b2 387 while (1)
388 {
80abd9e4 389 gcov_position_t position = gcov_position ();
f187042d 390 const char *source = NULL;
391 unsigned lineno = gcov_read_unsigned ();
952f0048 392
805e22b2 393 if (!lineno)
394 {
f187042d 395 source = gcov_read_string ();
805e22b2 396 if (!source)
397 break;
398 sep = NULL;
399 }
952f0048 400
805e22b2 401 if (!sep)
402 {
80abd9e4 403 printf ("\n");
287ac61d 404 print_prefix (filename, depth, position);
405 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
805e22b2 406 sep = "";
407 }
408 if (lineno)
409 {
410 printf ("%s%u", sep, lineno);
411 sep = ", ";
412 }
413 else
414 {
415 printf ("%s`%s'", sep, source);
416 sep = ":";
417 }
418 }
419 }
805e22b2 420}
421
f187042d 422static void
952f0048 423tag_counters (const char *filename ATTRIBUTE_UNUSED,
287ac61d 424 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
425 unsigned depth)
805e22b2 426{
e76f6040 427#define DEF_GCOV_COUNTER(COUNTER, NAME, MERGE_FN) NAME,
428 static const char *const counter_names[] = {
429#include "gcov-counter.def"
430};
431#undef DEF_GCOV_COUNTER
9e0943d4 432 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
952f0048 433
ab6a34f2 434 printf (" %s %u counts",
435 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts);
805e22b2 436 if (flag_dump_contents)
437 {
438 unsigned ix;
439
440 for (ix = 0; ix != n_counts; ix++)
441 {
80abd9e4 442 gcov_type count;
952f0048 443
805e22b2 444 if (!(ix & 7))
80abd9e4 445 {
446 printf ("\n");
287ac61d 447 print_prefix (filename, depth, gcov_position ());
448 printf (VALUE_PADDING_PREFIX VALUE_PREFIX, ix);
80abd9e4 449 }
952f0048 450
80abd9e4 451 count = gcov_read_counter ();
287ac61d 452 printf ("%" PRId64 " ", count);
805e22b2 453 }
454 }
805e22b2 455}
456
f187042d 457static void
952f0048 458tag_summary (const char *filename ATTRIBUTE_UNUSED,
287ac61d 459 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
56621355 460 unsigned depth ATTRIBUTE_UNUSED)
805e22b2 461{
5860b185 462 gcov_summary summary;
ab6a34f2 463 gcov_read_summary (&summary);
56621355 464 printf (" runs=%d, sum_max=%" PRId64,
465 summary.runs, summary.sum_max);
805e22b2 466}