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)
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/>. */
20 #define INCLUDE_VECTOR
22 #include "coretypes.h"
26 #include "diagnostic.h"
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
50 void (*proc
) (const char *, unsigned, int, unsigned);
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
[] =
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
},
86 main (int argc ATTRIBUTE_UNUSED
, char **argv
)
91 p
= argv
[0] + strlen (argv
[0]);
92 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
96 xmalloc_set_program_name (progname
);
98 /* Unlock the stdio streams. */
99 unlock_std_streams ();
103 diagnostic_initialize (global_dc
, 0);
105 while ((opt
= getopt_long (argc
, argv
, "hlprsvw", options
, NULL
)) != -1)
116 flag_dump_contents
= 1;
119 flag_dump_positions
= 1;
125 flag_dump_stable
= 1;
128 fprintf (stderr
, "unknown flag `%c'\n", opt
);
133 dump_gcov_file (argv
[optind
++]);
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",
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");
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
);
174 dump_gcov_file (const char *filename
)
180 if (!gcov_open (filename
, 1))
182 fprintf (stderr
, "%s:cannot open\n", filename
);
188 unsigned magic
= gcov_read_unsigned ();
193 if ((endianness
= gcov_magic (magic
, GCOV_DATA_MAGIC
)))
195 else if ((endianness
= gcov_magic (magic
, GCOV_NOTE_MAGIC
)))
196 is_data_type
= false;
199 printf ("%s:not a gcov file\n", filename
);
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
)
214 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
215 printf ("%s:warning:current version is `%.4s'\n", filename
, e
);
220 unsigned stamp
= gcov_read_unsigned ();
221 printf ("%s:stamp %lu\n", filename
, (unsigned long)stamp
);
224 unsigned checksum
= gcov_read_unsigned ();
225 printf ("%s:checksum %lu\n", filename
, (unsigned long)checksum
);
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
);
239 gcov_position_t base
, position
= gcov_position ();
241 unsigned tag
, length
;
242 tag_format_t
const *format
;
247 tag
= gcov_read_unsigned ();
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
);
263 for (format
= tag_table
; format
->name
; format
++)
264 if (format
->tag
== tag
)
266 format
= &tag_table
[GCOV_TAG_IS_COUNTER (tag
) ? 2 : 1];
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",
277 tags
[depth
- 1] = tag
;
280 print_prefix (filename
, tag_depth
, position
);
281 printf ("%08x:%4u:%s", tag
, abs (read_length
), format
->name
);
283 (*format
->proc
) (filename
, tag
, read_length
, depth
);
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 ());
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 ();
317 printf (" placeholder");
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
)
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
);
340 printf (", artificial");
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 ());
354 tag_arcs (const char *filename ATTRIBUTE_UNUSED
,
355 unsigned tag ATTRIBUTE_UNUSED
, int length ATTRIBUTE_UNUSED
,
358 unsigned n_arcs
= GCOV_TAG_ARCS_NUM (length
);
360 printf (" %u arcs", n_arcs
);
361 if (flag_dump_contents
)
364 unsigned blockno
= gcov_read_unsigned ();
366 for (ix
= 0; ix
!= n_arcs
; ix
++)
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
);
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
= ',';
396 tag_lines (const char *filename ATTRIBUTE_UNUSED
,
397 unsigned tag ATTRIBUTE_UNUSED
, int length ATTRIBUTE_UNUSED
,
400 if (flag_dump_contents
)
402 unsigned blockno
= gcov_read_unsigned ();
403 char const *sep
= NULL
;
407 gcov_position_t position
= gcov_position ();
408 const char *source
= NULL
;
409 unsigned lineno
= gcov_read_unsigned ();
413 source
= gcov_read_string ();
422 print_prefix (filename
, depth
, position
);
423 printf (VALUE_PADDING_PREFIX
"block %u:", blockno
);
428 printf ("%s%u", sep
, lineno
);
433 printf ("%s`%s'", sep
, source
);
441 tag_counters (const char *filename ATTRIBUTE_UNUSED
,
442 unsigned tag ATTRIBUTE_UNUSED
, int length ATTRIBUTE_UNUSED
,
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
)
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 ())
497 for (unsigned ix
= 0; ix
< counters
.size (); ++ix
)
507 print_prefix (filename
, depth
, gcov_position ());
508 printf (VALUE_PADDING_PREFIX VALUE_PREFIX
, ix
);
511 printf ("%" PRId64
" ", counters
[ix
]);
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
);