Make TOPN counter dynamically allocated.
[gcc.git] / gcc / gcov-dump.c
1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002-2020 Free Software Foundation, Inc.
3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
4
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.
9
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.
14
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/>. */
18
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"
30
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 extern int main (int, char **);
42
43 typedef struct tag_format
44 {
45 unsigned tag;
46 char const *name;
47 void (*proc) (const char *, unsigned, unsigned, unsigned);
48 } tag_format_t;
49
50 static int flag_dump_contents = 0;
51 static int flag_dump_positions = 0;
52 static int flag_dump_raw = 0;
53
54 static const struct option options[] =
55 {
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 }
61 };
62
63 #define VALUE_PADDING_PREFIX " "
64 #define VALUE_PREFIX "%2d: "
65
66 static const tag_format_t tag_table[] =
67 {
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}
77 };
78
79 int
80 main (int argc ATTRIBUTE_UNUSED, char **argv)
81 {
82 int opt;
83 const char *p;
84
85 p = argv[0] + strlen (argv[0]);
86 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
87 --p;
88 progname = p;
89
90 xmalloc_set_program_name (progname);
91
92 /* Unlock the stdio streams. */
93 unlock_std_streams ();
94
95 gcc_init_libintl ();
96
97 diagnostic_initialize (global_dc, 0);
98
99 while ((opt = getopt_long (argc, argv, "hlprvw", options, NULL)) != -1)
100 {
101 switch (opt)
102 {
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);
120 }
121 }
122
123 while (argv[optind])
124 dump_gcov_file (argv[optind++]);
125 return 0;
126 }
127
128 static void
129 print_usage (void)
130 {
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);
140 }
141
142 static void
143 print_version (void)
144 {
145 printf ("gcov-dump %s%s\n", pkgversion_string, version_string);
146 printf ("Copyright (C) 2020 Free Software Foundation, Inc.\n");
147 printf ("This is free software; see the source for copying conditions.\n"
148 "There is NO warranty; not even for MERCHANTABILITY or \n"
149 "FITNESS FOR A PARTICULAR PURPOSE.\n\n");
150 }
151
152 static void
153 print_prefix (const char *filename, unsigned depth, gcov_position_t position)
154 {
155 static const char prefix[] = " ";
156
157 printf ("%s:", filename);
158 if (flag_dump_positions)
159 printf ("%5lu:", (unsigned long) position);
160 printf ("%.*s", (int) 2 * depth, prefix);
161 }
162
163 static void
164 dump_gcov_file (const char *filename)
165 {
166 unsigned tags[4];
167 unsigned depth = 0;
168 bool is_data_type;
169
170 if (!gcov_open (filename, 1))
171 {
172 fprintf (stderr, "%s:cannot open\n", filename);
173 return;
174 }
175
176 /* magic */
177 {
178 unsigned magic = gcov_read_unsigned ();
179 unsigned version;
180 int endianness = 0;
181 char m[4], v[4];
182
183 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
184 is_data_type = true;
185 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
186 is_data_type = false;
187 else
188 {
189 printf ("%s:not a gcov file\n", filename);
190 gcov_close ();
191 return;
192 }
193 version = gcov_read_unsigned ();
194 GCOV_UNSIGNED2STRING (v, version);
195 GCOV_UNSIGNED2STRING (m, magic);
196
197 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename,
198 is_data_type ? "data" : "note",
199 m, v, endianness < 0 ? " (swapped endianness)" : "");
200 if (version != GCOV_VERSION)
201 {
202 char e[4];
203
204 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
205 printf ("%s:warning:current version is `%.4s'\n", filename, e);
206 }
207 }
208
209 /* stamp */
210 {
211 unsigned stamp = gcov_read_unsigned ();
212
213 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
214 }
215
216 if (!is_data_type)
217 {
218 printf ("%s:cwd: %s\n", filename, gcov_read_string ());
219
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);
224 }
225
226 while (1)
227 {
228 gcov_position_t base, position = gcov_position ();
229 unsigned tag, length;
230 tag_format_t const *format;
231 unsigned tag_depth;
232 int error;
233 unsigned mask;
234
235 tag = gcov_read_unsigned ();
236 if (!tag)
237 break;
238 length = gcov_read_unsigned ();
239 base = gcov_position ();
240 mask = GCOV_TAG_MASK (tag) >> 1;
241 for (tag_depth = 4; mask; mask >>= 8)
242 {
243 if ((mask & 0xff) != 0xff)
244 {
245 printf ("%s:tag `%08x' is invalid\n", filename, tag);
246 break;
247 }
248 tag_depth--;
249 }
250 for (format = tag_table; format->name; format++)
251 if (format->tag == tag)
252 goto found;
253 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
254 found:;
255 if (tag)
256 {
257 if (depth && depth < tag_depth)
258 {
259 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
260 printf ("%s:tag `%08x' is incorrectly nested\n",
261 filename, tag);
262 }
263 depth = tag_depth;
264 tags[depth - 1] = tag;
265 }
266
267 print_prefix (filename, tag_depth, position);
268 printf ("%08x:%4u:%s", tag, length, format->name);
269 if (format->proc)
270 (*format->proc) (filename, tag, length, depth);
271
272 printf ("\n");
273 if (flag_dump_contents && format->proc)
274 {
275 unsigned long actual_length = gcov_position () - base;
276
277 if (actual_length > length)
278 printf ("%s:record size mismatch %lu bytes overread\n",
279 filename, actual_length - length);
280 else if (length > actual_length)
281 printf ("%s:record size mismatch %lu bytes unread\n",
282 filename, length - actual_length);
283 }
284 gcov_sync (base, length);
285 if ((error = gcov_is_error ()))
286 {
287 printf (error < 0 ? "%s:counter overflow at %lu\n" :
288 "%s:read error at %lu\n", filename,
289 (long unsigned) gcov_position ());
290 break;
291 }
292 }
293 gcov_close ();
294 }
295
296 static void
297 tag_function (const char *filename ATTRIBUTE_UNUSED,
298 unsigned tag ATTRIBUTE_UNUSED, unsigned length,
299 unsigned depth ATTRIBUTE_UNUSED)
300 {
301 unsigned long pos = gcov_position ();
302
303 if (!length)
304 printf (" placeholder");
305 else
306 {
307 printf (" ident=%u", gcov_read_unsigned ());
308 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
309 printf (", cfg_checksum=0x%08x", gcov_read_unsigned ());
310
311 if (gcov_position () - pos < length)
312 {
313 const char *name;
314
315 name = gcov_read_string ();
316 printf (", `%s'", name ? name : "NULL");
317 unsigned artificial = gcov_read_unsigned ();
318 name = gcov_read_string ();
319 printf (" %s", name ? name : "NULL");
320 unsigned line_start = gcov_read_unsigned ();
321 unsigned column_start = gcov_read_unsigned ();
322 unsigned line_end = gcov_read_unsigned ();
323 unsigned column_end = gcov_read_unsigned ();
324 printf (":%u:%u-%u:%u", line_start, column_start,
325 line_end, column_end);
326 if (artificial)
327 printf (", artificial");
328 }
329 }
330 }
331
332 static void
333 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
334 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
335 unsigned depth ATTRIBUTE_UNUSED)
336 {
337 printf (" %u blocks", gcov_read_unsigned ());
338 }
339
340 static void
341 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
342 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
343 unsigned depth)
344 {
345 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
346
347 printf (" %u arcs", n_arcs);
348 if (flag_dump_contents)
349 {
350 unsigned ix;
351 unsigned blockno = gcov_read_unsigned ();
352
353 for (ix = 0; ix != n_arcs; ix++)
354 {
355 unsigned dst, flags;
356
357 if (!(ix & 3))
358 {
359 printf ("\n");
360 print_prefix (filename, depth, gcov_position ());
361 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
362 }
363 dst = gcov_read_unsigned ();
364 flags = gcov_read_unsigned ();
365 printf (" %u:%04x", dst, flags);
366 if (flags)
367 {
368 char c = '(';
369
370 if (flags & GCOV_ARC_ON_TREE)
371 printf ("%ctree", c), c = ',';
372 if (flags & GCOV_ARC_FAKE)
373 printf ("%cfake", c), c = ',';
374 if (flags & GCOV_ARC_FALLTHROUGH)
375 printf ("%cfall", c), c = ',';
376 printf (")");
377 }
378 }
379 }
380 }
381
382 static void
383 tag_lines (const char *filename ATTRIBUTE_UNUSED,
384 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
385 unsigned depth)
386 {
387 if (flag_dump_contents)
388 {
389 unsigned blockno = gcov_read_unsigned ();
390 char const *sep = NULL;
391
392 while (1)
393 {
394 gcov_position_t position = gcov_position ();
395 const char *source = NULL;
396 unsigned lineno = gcov_read_unsigned ();
397
398 if (!lineno)
399 {
400 source = gcov_read_string ();
401 if (!source)
402 break;
403 sep = NULL;
404 }
405
406 if (!sep)
407 {
408 printf ("\n");
409 print_prefix (filename, depth, position);
410 printf (VALUE_PADDING_PREFIX "block %u:", blockno);
411 sep = "";
412 }
413 if (lineno)
414 {
415 printf ("%s%u", sep, lineno);
416 sep = ", ";
417 }
418 else
419 {
420 printf ("%s`%s'", sep, source);
421 sep = ":";
422 }
423 }
424 }
425 }
426
427 static void
428 tag_counters (const char *filename ATTRIBUTE_UNUSED,
429 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
430 unsigned depth)
431 {
432 #define DEF_GCOV_COUNTER(COUNTER, NAME, MERGE_FN) NAME,
433 static const char *const counter_names[] = {
434 #include "gcov-counter.def"
435 };
436 #undef DEF_GCOV_COUNTER
437 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
438
439 printf (" %s %u counts",
440 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts);
441 if (flag_dump_contents)
442 {
443 unsigned ix;
444
445 for (ix = 0; ix != n_counts; ix++)
446 {
447 gcov_type count;
448
449 if (flag_dump_raw)
450 {
451 if (ix == 0)
452 printf (": ");
453 }
454 else if (!(ix & 7))
455 {
456 printf ("\n");
457 print_prefix (filename, depth, gcov_position ());
458 printf (VALUE_PADDING_PREFIX VALUE_PREFIX, ix);
459 }
460
461 count = gcov_read_counter ();
462 printf ("%" PRId64 " ", count);
463 }
464 }
465 }
466
467 static void
468 tag_summary (const char *filename ATTRIBUTE_UNUSED,
469 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED,
470 unsigned depth ATTRIBUTE_UNUSED)
471 {
472 gcov_summary summary;
473 gcov_read_summary (&summary);
474 printf (" runs=%d, sum_max=%" PRId64,
475 summary.runs, summary.sum_max);
476 }