1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
3 2012 Free Software Foundation, Inc.
4 Contributed by Nathan Sidwell <nathan@codesourcery.com>
6 Gcov is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 Gcov is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Gcov; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 #include "diagnostic.h"
32 static void dump_file (const char *);
33 static int dump_aux_modules (const char *);
34 static void print_prefix (const char *, unsigned, gcov_position_t
);
35 static void print_usage (void);
36 static void print_version (void);
37 static void tag_function (const char *, unsigned, unsigned);
38 static void tag_blocks (const char *, unsigned, unsigned);
39 static void tag_arcs (const char *, unsigned, unsigned);
40 static void tag_lines (const char *, unsigned, unsigned);
41 static void tag_counters (const char *, unsigned, unsigned);
42 static void tag_summary (const char *, unsigned, unsigned);
43 static void tag_module_info (const char *, unsigned, unsigned);
44 static void tag_pmu_load_latency_info (const char *, unsigned, unsigned);
45 static void tag_pmu_branch_mispredict_info (const char *, unsigned, unsigned);
46 static void tag_pmu_string_table_entry (const char*, unsigned, unsigned);
47 static void tag_pmu_tool_header (const char *, unsigned, unsigned);
49 extern int main (int, char **);
51 typedef struct tag_format
55 void (*proc
) (const char *, unsigned, unsigned);
58 static int flag_dump_contents
= 0;
59 static int flag_dump_positions
= 0;
60 static int flag_dump_aux_modules_only
= 0;
62 static const struct option options
[] =
64 { "help", no_argument
, NULL
, 'h' },
65 { "version", no_argument
, NULL
, 'v' },
66 { "long", no_argument
, NULL
, 'l' },
67 { "positions", no_argument
, NULL
, 'o' },
71 static const tag_format_t tag_table
[] =
75 {0, "COUNTERS", tag_counters
},
76 {GCOV_TAG_FUNCTION
, "FUNCTION", tag_function
},
77 {GCOV_TAG_BLOCKS
, "BLOCKS", tag_blocks
},
78 {GCOV_TAG_ARCS
, "ARCS", tag_arcs
},
79 {GCOV_TAG_LINES
, "LINES", tag_lines
},
80 {GCOV_TAG_OBJECT_SUMMARY
, "OBJECT_SUMMARY", tag_summary
},
81 {GCOV_TAG_PROGRAM_SUMMARY
, "PROGRAM_SUMMARY", tag_summary
},
82 {GCOV_TAG_MODULE_INFO
, "MODULE INFO", tag_module_info
},
83 {GCOV_TAG_PMU_LOAD_LATENCY_INFO
, "PMU_LOAD_LATENCY_INFO",
84 tag_pmu_load_latency_info
},
85 {GCOV_TAG_PMU_BRANCH_MISPREDICT_INFO
, "PMU_BRANCH_MISPREDICT_INFO",
86 tag_pmu_branch_mispredict_info
},
87 {GCOV_TAG_PMU_TOOL_HEADER
, "PMU_TOOL_HEADER", tag_pmu_tool_header
},
88 {GCOV_TAG_PMU_STRING_TABLE_ENTRY
, "PMU_STRING_TABLE_ENTRY",
89 tag_pmu_string_table_entry
},
94 main (int argc ATTRIBUTE_UNUSED
, char **argv
)
99 p
= argv
[0] + strlen (argv
[0]);
100 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
104 xmalloc_set_program_name (progname
);
106 /* Unlock the stdio streams. */
107 unlock_std_streams ();
111 diagnostic_initialize (global_dc
, 0);
113 while ((opt
= getopt_long (argc
, argv
, "hlpvx", options
, NULL
)) != -1)
124 flag_dump_contents
= 1;
127 flag_dump_positions
= 1;
130 flag_dump_aux_modules_only
= 1;
133 fprintf (stderr
, "unknown flag `%c'\n", opt
);
137 if (flag_dump_aux_modules_only
)
140 if (dump_aux_modules (argv
[optind
++]))
145 dump_file (argv
[optind
++]);
152 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
153 printf ("Print coverage file contents\n");
154 printf (" -h, --help Print this help\n");
155 printf (" -v, --version Print version number\n");
156 printf (" -l, --long Dump record contents too\n");
157 printf (" -p, --positions Dump record positions\n");
158 printf (" -x Dump names of auxiliary modules only\n");
164 printf ("gcov-dump %s%s\n", pkgversion_string
, version_string
);
165 printf ("Copyright (C) 2012 Free Software Foundation, Inc.\n");
166 printf ("This is free software; see the source for copying conditions.\n"
167 "There is NO warranty; not even for MERCHANTABILITY or \n"
168 "FITNESS FOR A PARTICULAR PURPOSE.\n\n");
172 print_prefix (const char *filename
, unsigned depth
, gcov_position_t position
)
174 static const char prefix
[] = " ";
176 printf ("%s:", filename
);
177 if (flag_dump_positions
)
178 printf ("%lu:", (unsigned long) position
);
179 printf ("%.*s", (int) depth
, prefix
);
182 /* Dump auxiliary module information for gcda file with
186 dump_aux_modules (const char *filename
)
188 if (!gcov_open (filename
, 1))
190 fprintf (stderr
, "%s:cannot open\n", filename
);
195 gcov_read_unsigned ();
197 gcov_read_unsigned ();
199 gcov_read_unsigned ();
203 gcov_position_t base
;
204 unsigned tag
, length
;
207 tag
= gcov_read_unsigned ();
210 length
= gcov_read_unsigned ();
211 base
= gcov_position ();
212 if (tag
== GCOV_TAG_MODULE_INFO
)
213 tag_module_info (filename
, tag
, length
);
214 gcov_sync (base
, length
);
215 if ((error
= gcov_is_error ()))
217 printf (error
< 0 ? "%s:counter overflow at %lu\n" :
218 "%s:read error at %lu\n", filename
,
219 (long unsigned) gcov_position ());
229 dump_file (const char *filename
)
234 if (!gcov_open (filename
, 1))
236 fprintf (stderr
, "%s:cannot open\n", filename
);
242 unsigned magic
= gcov_read_unsigned ();
244 const char *type
= NULL
;
248 if ((endianness
= gcov_magic (magic
, GCOV_DATA_MAGIC
)))
250 else if ((endianness
= gcov_magic (magic
, GCOV_NOTE_MAGIC
)))
254 printf ("%s:not a gcov file\n", filename
);
258 version
= gcov_read_unsigned ();
259 GCOV_UNSIGNED2STRING (v
, version
);
260 GCOV_UNSIGNED2STRING (m
, magic
);
262 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename
, type
,
263 m
, v
, endianness
< 0 ? " (swapped endianness)" : "");
264 if (version
!= GCOV_VERSION
)
268 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
269 printf ("%s:warning:current version is `%.4s'\n", filename
, e
);
275 unsigned stamp
= gcov_read_unsigned ();
277 printf ("%s:stamp %lu\n", filename
, (unsigned long)stamp
);
282 gcov_position_t base
, position
= gcov_position ();
283 unsigned tag
, length
;
284 tag_format_t
const *format
;
289 tag
= gcov_read_unsigned ();
292 length
= gcov_read_unsigned ();
293 base
= gcov_position ();
294 mask
= GCOV_TAG_MASK (tag
) >> 1;
295 for (tag_depth
= 4; mask
; mask
>>= 8)
297 if ((mask
& 0xff) != 0xff)
299 printf ("%s:tag `%08x' is invalid\n", filename
, tag
);
304 for (format
= tag_table
; format
->name
; format
++)
305 if (format
->tag
== tag
)
307 format
= &tag_table
[GCOV_TAG_IS_COUNTER (tag
) ? 2 : 1];
311 if (depth
&& depth
< tag_depth
)
313 if (!GCOV_TAG_IS_SUBTAG (tags
[depth
- 1], tag
))
314 printf ("%s:tag `%08x' is incorrectly nested\n",
318 tags
[depth
- 1] = tag
;
321 print_prefix (filename
, tag_depth
, position
);
322 printf ("%08x:%4u:%s", tag
, length
, format
->name
);
324 (*format
->proc
) (filename
, tag
, length
);
327 if (flag_dump_contents
&& format
->proc
)
329 unsigned long actual_length
= gcov_position () - base
;
331 if (actual_length
> length
)
332 printf ("%s:record size mismatch %lu bytes overread\n",
333 filename
, actual_length
- length
);
334 else if (length
> actual_length
)
335 printf ("%s:record size mismatch %lu bytes unread\n",
336 filename
, length
- actual_length
);
338 gcov_sync (base
, length
);
339 if ((error
= gcov_is_error ()))
341 printf (error
< 0 ? "%s:counter overflow at %lu\n" :
342 "%s:read error at %lu\n", filename
,
343 (long unsigned) gcov_position ());
351 tag_function (const char *filename ATTRIBUTE_UNUSED
,
352 unsigned tag ATTRIBUTE_UNUSED
, unsigned length
)
354 unsigned long pos
= gcov_position ();
357 printf (" placeholder");
360 printf (" ident=%u", gcov_read_unsigned ());
361 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
362 printf (", cfg_checksum=0x%08x", gcov_read_unsigned ());
364 if (gcov_position () - pos
< length
)
368 name
= gcov_read_string ();
369 printf (", `%s'", name
? name
: "NULL");
370 name
= gcov_read_string ();
371 printf (" %s", name
? name
: "NULL");
372 printf (":%u", gcov_read_unsigned ());
378 tag_blocks (const char *filename ATTRIBUTE_UNUSED
,
379 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
381 unsigned n_blocks
= GCOV_TAG_BLOCKS_NUM (length
);
383 printf (" %u blocks", n_blocks
);
385 if (flag_dump_contents
)
389 for (ix
= 0; ix
!= n_blocks
; ix
++)
394 print_prefix (filename
, 0, gcov_position ());
395 printf ("\t\t%u", ix
);
397 printf (" %04x", gcov_read_unsigned ());
403 tag_arcs (const char *filename ATTRIBUTE_UNUSED
,
404 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
406 unsigned n_arcs
= GCOV_TAG_ARCS_NUM (length
);
408 printf (" %u arcs", n_arcs
);
409 if (flag_dump_contents
)
412 unsigned blockno
= gcov_read_unsigned ();
414 for (ix
= 0; ix
!= n_arcs
; ix
++)
421 print_prefix (filename
, 0, gcov_position ());
422 printf ("\tblock %u:", blockno
);
424 dst
= gcov_read_unsigned ();
425 flags
= gcov_read_unsigned ();
426 printf (" %u:%04x", dst
, flags
);
431 if (flags
& GCOV_ARC_ON_TREE
)
432 printf ("%ctree", c
), c
= ',';
433 if (flags
& GCOV_ARC_FAKE
)
434 printf ("%cfake", c
), c
= ',';
435 if (flags
& GCOV_ARC_FALLTHROUGH
)
436 printf ("%cfall", c
), c
= ',';
444 tag_lines (const char *filename ATTRIBUTE_UNUSED
,
445 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
447 if (flag_dump_contents
)
449 unsigned blockno
= gcov_read_unsigned ();
450 char const *sep
= NULL
;
454 gcov_position_t position
= gcov_position ();
455 const char *source
= NULL
;
456 unsigned lineno
= gcov_read_unsigned ();
460 source
= gcov_read_string ();
469 print_prefix (filename
, 0, position
);
470 printf ("\tblock %u:", blockno
);
475 printf ("%s%u", sep
, lineno
);
480 printf ("%s`%s'", sep
, source
);
488 tag_counters (const char *filename ATTRIBUTE_UNUSED
,
489 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
491 static const char *const counter_names
[] = GCOV_COUNTER_NAMES
;
492 unsigned n_counts
= GCOV_TAG_COUNTER_NUM (length
);
494 printf (" %s %u counts",
495 counter_names
[GCOV_COUNTER_FOR_TAG (tag
)], n_counts
);
496 if (flag_dump_contents
)
500 for (ix
= 0; ix
!= n_counts
; ix
++)
507 print_prefix (filename
, 0, gcov_position ());
508 printf ("\t\t%u", ix
);
511 count
= gcov_read_counter ();
513 printf (HOST_WIDEST_INT_PRINT_DEC
, count
);
519 tag_summary (const char *filename ATTRIBUTE_UNUSED
,
520 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
522 struct gcov_summary summary
;
524 gcov_bucket_type
*histo_bucket
;
526 gcov_read_summary (&summary
);
527 printf (" checksum=0x%08x", summary
.checksum
);
529 for (ix
= 0; ix
!= GCOV_COUNTERS_SUMMABLE
; ix
++)
532 print_prefix (filename
, 0, 0);
533 printf ("\t\tcounts=%u, runs=%u",
534 summary
.ctrs
[ix
].num
,
535 summary
.ctrs
[ix
].runs
);
537 printf (", sum_all=" HOST_WIDEST_INT_PRINT_DEC
,
538 (HOST_WIDEST_INT
)summary
.ctrs
[ix
].sum_all
);
539 printf (", run_max=" HOST_WIDEST_INT_PRINT_DEC
,
540 (HOST_WIDEST_INT
)summary
.ctrs
[ix
].run_max
);
541 printf (", sum_max=" HOST_WIDEST_INT_PRINT_DEC
,
542 (HOST_WIDEST_INT
)summary
.ctrs
[ix
].sum_max
);
543 if (ix
!= GCOV_COUNTER_ARCS
)
546 print_prefix (filename
, 0, 0);
547 printf ("\t\tcounter histogram:");
548 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
550 histo_bucket
= &summary
.ctrs
[ix
].histogram
[h_ix
];
551 if (!histo_bucket
->num_counters
)
554 print_prefix (filename
, 0, 0);
555 printf ("\t\t%d: num counts=%u, min counter="
556 HOST_WIDEST_INT_PRINT_DEC
", cum_counter="
557 HOST_WIDEST_INT_PRINT_DEC
,
558 h_ix
, histo_bucket
->num_counters
,
559 (HOST_WIDEST_INT
)histo_bucket
->min_value
,
560 (HOST_WIDEST_INT
)histo_bucket
->cum_value
);
566 tag_module_info (const char *filename ATTRIBUTE_UNUSED
,
567 unsigned tag ATTRIBUTE_UNUSED
, unsigned length
)
569 struct gcov_module_info
* mod_info
;
571 mod_info
= (struct gcov_module_info
*)
572 alloca ((length
+ 2) * sizeof (gcov_unsigned_t
));
573 gcov_read_module_info (mod_info
, length
);
574 if (flag_dump_aux_modules_only
)
576 if (!mod_info
->is_primary
)
577 printf ("%s\n", mod_info
->source_filename
);
581 const char *primary_suffix
=
582 mod_info
->is_primary
? "primary" : "auxiliary";
583 const char *export_suffix
= "";
584 const char *include_all_suffix
= "";
586 if (mod_info
->is_primary
)
588 if (MODULE_EXPORTED_FLAG (mod_info
))
589 export_suffix
= ",exported";
590 if (MODULE_INCLUDE_ALL_AUX_FLAG (mod_info
))
591 include_all_suffix
=",include_all";
593 printf (": %s (ident=%u) [%s%s%s]", mod_info
->source_filename
,
594 mod_info
->ident
, primary_suffix
, export_suffix
,
599 /* Read gcov tag GCOV_TAG_PMU_LOAD_LATENCY_INFO from the gcda file and
600 print the contents in a human readable form. */
603 tag_pmu_load_latency_info (const char *filename ATTRIBUTE_UNUSED
,
604 unsigned tag ATTRIBUTE_UNUSED
, unsigned length
)
606 gcov_pmu_ll_info_t ll_info
;
607 gcov_read_pmu_load_latency_info (&ll_info
, length
);
608 print_load_latency_line (stdout
, &ll_info
, no_newline
);
611 /* Read gcov tag GCOV_TAG_PMU_BRANCH_MISPREDICT_INFO from the gcda
612 file and print the contents in a human readable form. */
615 tag_pmu_branch_mispredict_info (const char *filename ATTRIBUTE_UNUSED
,
616 unsigned tag ATTRIBUTE_UNUSED
, unsigned length
)
618 gcov_pmu_brm_info_t brm_info
;
619 gcov_read_pmu_branch_mispredict_info (&brm_info
, length
);
620 print_branch_mispredict_line (stdout
, &brm_info
, no_newline
);
624 tag_pmu_string_table_entry (const char *filename ATTRIBUTE_UNUSED
,
625 unsigned tag ATTRIBUTE_UNUSED
, unsigned length
)
627 gcov_pmu_st_entry_t st_entry
;
628 gcov_read_pmu_string_table_entry(&st_entry
, length
);
629 print_pmu_string_table_entry(stdout
, &st_entry
, no_newline
);
633 /* Read gcov tag GCOV_TAG_PMU_TOOL_HEADER from the gcda file and print
634 the contents in a human readable form. */
637 tag_pmu_tool_header (const char *filename ATTRIBUTE_UNUSED
,
638 unsigned tag ATTRIBUTE_UNUSED
, unsigned length
)
640 gcov_pmu_tool_header_t tool_header
;
641 gcov_read_pmu_tool_header (&tool_header
, length
);
642 print_pmu_tool_header (stdout
, &tool_header
, no_newline
);
643 destroy_pmu_tool_header (&tool_header
);