1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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/>. */
21 #include "coretypes.h"
29 static void dump_file (const char *);
30 static void print_prefix (const char *, unsigned, gcov_position_t
);
31 static void print_usage (void);
32 static void print_version (void);
33 static void tag_function (const char *, unsigned, unsigned);
34 static void tag_blocks (const char *, unsigned, unsigned);
35 static void tag_arcs (const char *, unsigned, unsigned);
36 static void tag_lines (const char *, unsigned, unsigned);
37 static void tag_counters (const char *, unsigned, unsigned);
38 static void tag_summary (const char *, unsigned, unsigned);
39 extern int main (int, char **);
41 typedef struct tag_format
45 void (*proc
) (const char *, unsigned, unsigned);
48 static int flag_dump_contents
= 0;
49 static int flag_dump_positions
= 0;
51 static const struct option options
[] =
53 { "help", no_argument
, NULL
, 'h' },
54 { "version", no_argument
, NULL
, 'v' },
55 { "long", no_argument
, NULL
, 'l' },
56 { "positions", no_argument
, NULL
, 'o' },
60 static const tag_format_t tag_table
[] =
64 {0, "COUNTERS", tag_counters
},
65 {GCOV_TAG_FUNCTION
, "FUNCTION", tag_function
},
66 {GCOV_TAG_BLOCKS
, "BLOCKS", tag_blocks
},
67 {GCOV_TAG_ARCS
, "ARCS", tag_arcs
},
68 {GCOV_TAG_LINES
, "LINES", tag_lines
},
69 {GCOV_TAG_OBJECT_SUMMARY
, "OBJECT_SUMMARY", tag_summary
},
70 {GCOV_TAG_PROGRAM_SUMMARY
, "PROGRAM_SUMMARY", tag_summary
},
75 main (int argc ATTRIBUTE_UNUSED
, char **argv
)
79 /* Unlock the stdio streams. */
80 unlock_std_streams ();
82 while ((opt
= getopt_long (argc
, argv
, "hlpv", options
, NULL
)) != -1)
93 flag_dump_contents
= 1;
96 flag_dump_positions
= 1;
99 fprintf (stderr
, "unknown flag `%c'\n", opt
);
104 dump_file (argv
[optind
++]);
111 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
112 printf ("Print coverage file contents\n");
113 printf (" -h, --help Print this help\n");
114 printf (" -v, --version Print version number\n");
115 printf (" -l, --long Dump record contents too\n");
116 printf (" -p, --positions Dump record positions\n");
122 printf ("gcov-dump %s%s\n", pkgversion_string
, version_string
);
123 printf ("Copyright (C) 2007 Free Software Foundation, Inc.\n");
124 printf ("This is free software; see the source for copying conditions.\n"
125 "There is NO warranty; not even for MERCHANTABILITY or \n"
126 "FITNESS FOR A PARTICULAR PURPOSE.\n\n");
130 print_prefix (const char *filename
, unsigned depth
, gcov_position_t position
)
132 static const char prefix
[] = " ";
134 printf ("%s:", filename
);
135 if (flag_dump_positions
)
136 printf ("%lu:", (unsigned long) position
);
137 printf ("%.*s", (int) depth
, prefix
);
141 dump_file (const char *filename
)
146 if (!gcov_open (filename
, 1))
148 fprintf (stderr
, "%s:cannot open\n", filename
);
154 unsigned magic
= gcov_read_unsigned ();
156 const char *type
= NULL
;
160 if ((endianness
= gcov_magic (magic
, GCOV_DATA_MAGIC
)))
162 else if ((endianness
= gcov_magic (magic
, GCOV_NOTE_MAGIC
)))
166 printf ("%s:not a gcov file\n", filename
);
170 version
= gcov_read_unsigned ();
171 GCOV_UNSIGNED2STRING (v
, version
);
172 GCOV_UNSIGNED2STRING (m
, magic
);
174 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename
, type
,
175 m
, v
, endianness
< 0 ? " (swapped endianness)" : "");
176 if (version
!= GCOV_VERSION
)
180 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
181 printf ("%s:warning:current version is `%.4s'\n", filename
, e
);
187 unsigned stamp
= gcov_read_unsigned ();
189 printf ("%s:stamp %lu\n", filename
, (unsigned long)stamp
);
194 gcov_position_t base
, position
= gcov_position ();
195 unsigned tag
, length
;
196 tag_format_t
const *format
;
201 tag
= gcov_read_unsigned ();
204 length
= gcov_read_unsigned ();
205 base
= gcov_position ();
206 mask
= GCOV_TAG_MASK (tag
) >> 1;
207 for (tag_depth
= 4; mask
; mask
>>= 8)
209 if ((mask
& 0xff) != 0xff)
211 printf ("%s:tag `%08x' is invalid\n", filename
, tag
);
216 for (format
= tag_table
; format
->name
; format
++)
217 if (format
->tag
== tag
)
219 format
= &tag_table
[GCOV_TAG_IS_COUNTER (tag
) ? 2 : 1];
223 if (depth
&& depth
< tag_depth
)
225 if (!GCOV_TAG_IS_SUBTAG (tags
[depth
- 1], tag
))
226 printf ("%s:tag `%08x' is incorrectly nested\n",
230 tags
[depth
- 1] = tag
;
233 print_prefix (filename
, tag_depth
, position
);
234 printf ("%08x:%4u:%s", tag
, length
, format
->name
);
236 (*format
->proc
) (filename
, tag
, length
);
239 if (flag_dump_contents
&& format
->proc
)
241 unsigned long actual_length
= gcov_position () - base
;
243 if (actual_length
> length
)
244 printf ("%s:record size mismatch %lu bytes overread\n",
245 filename
, actual_length
- length
);
246 else if (length
> actual_length
)
247 printf ("%s:record size mismatch %lu bytes unread\n",
248 filename
, length
- actual_length
);
250 gcov_sync (base
, length
);
251 if ((error
= gcov_is_error ()))
253 printf (error
< 0 ? "%s:counter overflow at %lu\n" :
254 "%s:read error at %lu\n", filename
,
255 (long unsigned) gcov_position ());
263 tag_function (const char *filename ATTRIBUTE_UNUSED
,
264 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
266 unsigned long pos
= gcov_position ();
268 printf (" ident=%u", gcov_read_unsigned ());
269 printf (", checksum=0x%08x", gcov_read_unsigned ());
271 if (gcov_position () - pos
< length
)
275 name
= gcov_read_string ();
276 printf (", `%s'", name
? name
: "NULL");
277 name
= gcov_read_string ();
278 printf (" %s", name
? name
: "NULL");
279 printf (":%u", gcov_read_unsigned ());
284 tag_blocks (const char *filename ATTRIBUTE_UNUSED
,
285 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
287 unsigned n_blocks
= GCOV_TAG_BLOCKS_NUM (length
);
289 printf (" %u blocks", n_blocks
);
291 if (flag_dump_contents
)
295 for (ix
= 0; ix
!= n_blocks
; ix
++)
300 print_prefix (filename
, 0, gcov_position ());
301 printf ("\t\t%u", ix
);
303 printf (" %04x", gcov_read_unsigned ());
309 tag_arcs (const char *filename ATTRIBUTE_UNUSED
,
310 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
312 unsigned n_arcs
= GCOV_TAG_ARCS_NUM (length
);
314 printf (" %u arcs", n_arcs
);
315 if (flag_dump_contents
)
318 unsigned blockno
= gcov_read_unsigned ();
320 for (ix
= 0; ix
!= n_arcs
; ix
++)
327 print_prefix (filename
, 0, gcov_position ());
328 printf ("\tblock %u:", blockno
);
330 dst
= gcov_read_unsigned ();
331 flags
= gcov_read_unsigned ();
332 printf (" %u:%04x", dst
, flags
);
338 tag_lines (const char *filename ATTRIBUTE_UNUSED
,
339 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
341 if (flag_dump_contents
)
343 unsigned blockno
= gcov_read_unsigned ();
344 char const *sep
= NULL
;
348 gcov_position_t position
= gcov_position ();
349 const char *source
= NULL
;
350 unsigned lineno
= gcov_read_unsigned ();
354 source
= gcov_read_string ();
363 print_prefix (filename
, 0, position
);
364 printf ("\tblock %u:", blockno
);
369 printf ("%s%u", sep
, lineno
);
374 printf ("%s`%s'", sep
, source
);
382 tag_counters (const char *filename ATTRIBUTE_UNUSED
,
383 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
385 static const char *const counter_names
[] = GCOV_COUNTER_NAMES
;
386 unsigned n_counts
= GCOV_TAG_COUNTER_NUM (length
);
388 printf (" %s %u counts",
389 counter_names
[GCOV_COUNTER_FOR_TAG (tag
)], n_counts
);
390 if (flag_dump_contents
)
394 for (ix
= 0; ix
!= n_counts
; ix
++)
401 print_prefix (filename
, 0, gcov_position ());
402 printf ("\t\t%u", ix
);
405 count
= gcov_read_counter ();
407 printf (HOST_WIDEST_INT_PRINT_DEC
, count
);
413 tag_summary (const char *filename ATTRIBUTE_UNUSED
,
414 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
416 struct gcov_summary summary
;
419 gcov_read_summary (&summary
);
420 printf (" checksum=0x%08x", summary
.checksum
);
422 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
425 print_prefix (filename
, 0, 0);
426 printf ("\t\tcounts=%u, runs=%u",
427 summary
.ctrs
[ix
].num
, summary
.ctrs
[ix
].runs
);
429 printf (", sum_all=" HOST_WIDEST_INT_PRINT_DEC
,
430 (HOST_WIDEST_INT
)summary
.ctrs
[ix
].sum_all
);
431 printf (", run_max=" HOST_WIDEST_INT_PRINT_DEC
,
432 (HOST_WIDEST_INT
)summary
.ctrs
[ix
].run_max
);
433 printf (", sum_max=" HOST_WIDEST_INT_PRINT_DEC
,
434 (HOST_WIDEST_INT
)summary
.ctrs
[ix
].sum_max
);