1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002, 2003, 2004 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 2, 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 COPYING. If not, write to
17 the Free Software Foundation, 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
22 #include "coretypes.h"
30 static void dump_file (const char *);
31 static void print_prefix (const char *, unsigned, gcov_position_t
);
32 static void print_usage (void);
33 static void print_version (void);
34 static void tag_function (const char *, unsigned, unsigned);
35 static void tag_blocks (const char *, unsigned, unsigned);
36 static void tag_arcs (const char *, unsigned, unsigned);
37 static void tag_lines (const char *, unsigned, unsigned);
38 static void tag_counters (const char *, unsigned, unsigned);
39 static void tag_summary (const char *, unsigned, unsigned);
40 extern int main (int, char **);
42 typedef struct tag_format
46 void (*proc
) (const char *, unsigned, unsigned);
49 static int flag_dump_contents
= 0;
50 static int flag_dump_positions
= 0;
52 static const struct option options
[] =
54 { "help", no_argument
, NULL
, 'h' },
55 { "version", no_argument
, NULL
, 'v' },
56 { "long", no_argument
, NULL
, 'l' },
57 { "positions", no_argument
, NULL
, 'o' },
61 static const tag_format_t tag_table
[] =
65 {0, "COUNTERS", tag_counters
},
66 {GCOV_TAG_FUNCTION
, "FUNCTION", tag_function
},
67 {GCOV_TAG_BLOCKS
, "BLOCKS", tag_blocks
},
68 {GCOV_TAG_ARCS
, "ARCS", tag_arcs
},
69 {GCOV_TAG_LINES
, "LINES", tag_lines
},
70 {GCOV_TAG_OBJECT_SUMMARY
, "OBJECT_SUMMARY", tag_summary
},
71 {GCOV_TAG_PROGRAM_SUMMARY
, "PROGRAM_SUMMARY", tag_summary
},
76 main (int argc ATTRIBUTE_UNUSED
, char **argv
)
80 /* Unlock the stdio streams. */
81 unlock_stream (stdin
);
82 unlock_stream (stdout
);
83 unlock_stream (stderr
);
85 while ((opt
= getopt_long (argc
, argv
, "hlpv", options
, NULL
)) != -1)
96 flag_dump_contents
= 1;
99 flag_dump_positions
= 1;
102 fprintf (stderr
, "unknown flag `%c'\n", opt
);
107 dump_file (argv
[optind
++]);
114 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
115 printf ("Print coverage file contents\n");
116 printf (" -h, --help Print this help\n");
117 printf (" -v, --version Print version number\n");
118 printf (" -l, --long Dump record contents too\n");
119 printf (" -p, --positions Dump record positions\n");
125 printf ("gcov-dump (GCC) %s\n", version_string
);
126 printf ("Copyright (C) 2003 Free Software Foundation, Inc.\n");
127 printf ("This is free software; see the source for copying conditions.\n"
128 "There is NO warranty; not even for MERCHANTABILITY or \n"
129 "FITNESS FOR A PARTICULAR PURPOSE.\n\n");
133 print_prefix (const char *filename
, unsigned depth
, gcov_position_t position
)
135 static const char prefix
[] = " ";
137 printf ("%s:", filename
);
138 if (flag_dump_positions
)
139 printf ("%lu:", (unsigned long) position
);
140 printf ("%.*s", (int) depth
, prefix
);
144 dump_file (const char *filename
)
149 if (!gcov_open (filename
, 1))
151 fprintf (stderr
, "%s:cannot open\n", filename
);
157 unsigned magic
= gcov_read_unsigned ();
159 const char *type
= NULL
;
163 if ((endianness
= gcov_magic (magic
, GCOV_DATA_MAGIC
)))
165 else if ((endianness
= gcov_magic (magic
, GCOV_NOTE_MAGIC
)))
169 printf ("%s:not a gcov file\n", filename
);
173 version
= gcov_read_unsigned ();
174 GCOV_UNSIGNED2STRING (v
, version
);
175 GCOV_UNSIGNED2STRING (m
, magic
);
177 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename
, type
,
178 m
, v
, endianness
< 0 ? " (swapped endianness)" : "");
179 if (version
!= GCOV_VERSION
)
183 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
184 printf ("%s:warning:current version is `%.4s'\n", filename
, e
);
190 unsigned stamp
= gcov_read_unsigned ();
192 printf ("%s:stamp %lu\n", filename
, (unsigned long)stamp
);
197 gcov_position_t base
, position
= gcov_position ();
198 unsigned tag
, length
;
199 tag_format_t
const *format
;
204 tag
= gcov_read_unsigned ();
207 length
= gcov_read_unsigned ();
208 base
= gcov_position ();
209 mask
= GCOV_TAG_MASK (tag
) >> 1;
210 for (tag_depth
= 4; mask
; mask
>>= 8)
212 if ((mask
& 0xff) != 0xff)
214 printf ("%s:tag `%08x' is invalid\n", filename
, tag
);
219 for (format
= tag_table
; format
->name
; format
++)
220 if (format
->tag
== tag
)
222 format
= &tag_table
[GCOV_TAG_IS_COUNTER (tag
) ? 2 : 1];
226 if (depth
&& depth
< tag_depth
)
228 if (!GCOV_TAG_IS_SUBTAG (tags
[depth
- 1], tag
))
229 printf ("%s:tag `%08x' is incorrectly nested\n",
233 tags
[depth
- 1] = tag
;
236 print_prefix (filename
, tag_depth
, position
);
237 printf ("%08x:%4u:%s", tag
, length
, format
->name
);
239 (*format
->proc
) (filename
, tag
, length
);
242 if (flag_dump_contents
&& format
->proc
)
244 unsigned long actual_length
= gcov_position () - base
;
246 if (actual_length
> length
)
247 printf ("%s:record size mismatch %lu bytes overread\n",
248 filename
, actual_length
- length
);
249 else if (length
> actual_length
)
250 printf ("%s:record size mismatch %lu bytes unread\n",
251 filename
, length
- actual_length
);
253 gcov_sync (base
, length
);
254 if ((error
= gcov_is_error ()))
256 printf (error
< 0 ? "%s:counter overflow at %lu\n" :
257 "%s:read error at %lu\n", filename
,
258 (long unsigned) gcov_position ());
266 tag_function (const char *filename ATTRIBUTE_UNUSED
,
267 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
269 unsigned long pos
= gcov_position ();
271 printf (" ident=%u", gcov_read_unsigned ());
272 printf (", checksum=0x%08x", gcov_read_unsigned ());
274 if (gcov_position () - pos
< length
)
278 name
= gcov_read_string ();
279 printf (", `%s'", name
? name
: "NULL");
280 name
= gcov_read_string ();
281 printf (" %s", name
? name
: "NULL");
282 printf (":%u", gcov_read_unsigned ());
287 tag_blocks (const char *filename ATTRIBUTE_UNUSED
,
288 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
290 unsigned n_blocks
= GCOV_TAG_BLOCKS_NUM (length
);
292 printf (" %u blocks", n_blocks
);
294 if (flag_dump_contents
)
298 for (ix
= 0; ix
!= n_blocks
; ix
++)
303 print_prefix (filename
, 0, gcov_position ());
304 printf ("\t\t%u", ix
);
306 printf (" %04x", gcov_read_unsigned ());
312 tag_arcs (const char *filename ATTRIBUTE_UNUSED
,
313 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
315 unsigned n_arcs
= GCOV_TAG_ARCS_NUM (length
);
317 printf (" %u arcs", n_arcs
);
318 if (flag_dump_contents
)
321 unsigned blockno
= gcov_read_unsigned ();
323 for (ix
= 0; ix
!= n_arcs
; ix
++)
330 print_prefix (filename
, 0, gcov_position ());
331 printf ("\tblock %u:", blockno
);
333 dst
= gcov_read_unsigned ();
334 flags
= gcov_read_unsigned ();
335 printf (" %u:%04x", dst
, flags
);
341 tag_lines (const char *filename ATTRIBUTE_UNUSED
,
342 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
344 if (flag_dump_contents
)
346 unsigned blockno
= gcov_read_unsigned ();
347 char const *sep
= NULL
;
351 gcov_position_t position
= gcov_position ();
352 const char *source
= NULL
;
353 unsigned lineno
= gcov_read_unsigned ();
357 source
= gcov_read_string ();
366 print_prefix (filename
, 0, position
);
367 printf ("\tblock %u:", blockno
);
372 printf ("%s%u", sep
, lineno
);
377 printf ("%s`%s'", sep
, source
);
385 tag_counters (const char *filename ATTRIBUTE_UNUSED
,
386 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
388 static const char *const counter_names
[] = GCOV_COUNTER_NAMES
;
389 unsigned n_counts
= GCOV_TAG_COUNTER_NUM (length
);
391 printf (" %s %u counts",
392 counter_names
[GCOV_COUNTER_FOR_TAG (tag
)], n_counts
);
393 if (flag_dump_contents
)
397 for (ix
= 0; ix
!= n_counts
; ix
++)
404 print_prefix (filename
, 0, gcov_position ());
405 printf ("\t\t%u", ix
);
408 count
= gcov_read_counter ();
410 printf (HOST_WIDEST_INT_PRINT_DEC
, count
);
416 tag_summary (const char *filename ATTRIBUTE_UNUSED
,
417 unsigned tag ATTRIBUTE_UNUSED
, unsigned length ATTRIBUTE_UNUSED
)
419 struct gcov_summary summary
;
422 gcov_read_summary (&summary
);
423 printf (" checksum=0x%08x", summary
.checksum
);
425 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
428 print_prefix (filename
, 0, 0);
429 printf ("\t\tcounts=%u, runs=%u",
430 summary
.ctrs
[ix
].num
, summary
.ctrs
[ix
].runs
);
432 printf (", sum_all=" HOST_WIDEST_INT_PRINT_DEC
,
433 (HOST_WIDEST_INT
)summary
.ctrs
[ix
].sum_all
);
434 printf (", run_max=" HOST_WIDEST_INT_PRINT_DEC
,
435 (HOST_WIDEST_INT
)summary
.ctrs
[ix
].run_max
);
436 printf (", sum_max=" HOST_WIDEST_INT_PRINT_DEC
,
437 (HOST_WIDEST_INT
)summary
.ctrs
[ix
].sum_max
);