1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002 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
PARAMS ((const char *));
31 static void print_prefix
PARAMS ((const char *, unsigned));
32 static void print_usage
PARAMS ((void));
33 static void print_version
PARAMS ((void));
34 static void tag_function
PARAMS ((const char *, unsigned, unsigned));
35 static void tag_blocks
PARAMS ((const char *, unsigned, unsigned));
36 static void tag_arcs
PARAMS ((const char *, unsigned, unsigned));
37 static void tag_lines
PARAMS ((const char *, unsigned, unsigned));
38 static void tag_counters
PARAMS ((const char *, unsigned, unsigned));
39 static void tag_summary
PARAMS ((const char *, unsigned, unsigned));
40 extern int main
PARAMS ((int, char **));
42 typedef struct tag_format
46 void (*proc
) (const char *, unsigned, unsigned);
49 static int flag_dump_contents
= 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' },
58 static const tag_format_t tag_table
[] =
62 {0, "COUNTERS", tag_counters
},
63 {GCOV_TAG_FUNCTION
, "FUNCTION", tag_function
},
64 {GCOV_TAG_BLOCKS
, "BLOCKS", tag_blocks
},
65 {GCOV_TAG_ARCS
, "ARCS", tag_arcs
},
66 {GCOV_TAG_LINES
, "LINES", tag_lines
},
67 {GCOV_TAG_OBJECT_SUMMARY
, "OBJECT_SUMMARY", tag_summary
},
68 {GCOV_TAG_PROGRAM_SUMMARY
, "PROGRAM_SUMMARY", tag_summary
},
73 int argc ATTRIBUTE_UNUSED
;
78 while ((opt
= getopt_long (argc
, argv
, "hlv", options
, NULL
)) != -1)
89 flag_dump_contents
= 1;
92 fprintf (stderr
, "unknown flag `%c'\n", opt
);
97 dump_file (argv
[optind
++]);
104 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
105 printf ("Print coverage file contents\n");
106 printf (" -h, --help Print this help\n");
107 printf (" -v, --version Print version number\n");
108 printf (" -l, --long Dump record contents too\n");
115 unsigned version
= GCOV_VERSION
;
118 for (ix
= 4; ix
--; version
>>= 8)
120 printf ("gcov %.4s (GCC %s)\n", v
, version_string
);
121 printf ("Copyright (C) 2002 Free Software Foundation, Inc.\n");
122 printf ("This is free software; see the source for copying conditions. There is NO\n\
123 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
127 print_prefix (filename
, depth
)
128 const char *filename
;
131 static const char prefix
[] = " ";
133 printf ("%s:%.*s", filename
, (int) depth
, prefix
);
138 const char *filename
;
143 if (!gcov_open (filename
, 1))
145 fprintf (stderr
, "%s:cannot open\n", filename
);
151 unsigned magic
= gcov_read_unsigned ();
152 unsigned version
= gcov_read_unsigned ();
153 const char *type
= NULL
;
154 char e
[4], v
[4], m
[4];
155 unsigned expected
= GCOV_VERSION
;
157 int different
= version
!= GCOV_VERSION
;
159 if (magic
== GCOV_DATA_MAGIC
)
161 else if (magic
== GCOV_GRAPH_MAGIC
)
165 printf ("%s:not a gcov file\n", filename
);
169 for (ix
= 4; ix
--; expected
>>= 8, version
>>= 8, magic
>>= 8)
176 printf ("%s:%s:magic `%.4s':version `%.4s'\n", filename
, type
, m
, v
);
178 printf ("%s:warning:current version is `%.4s'\n", filename
, e
);
181 while (!gcov_is_eof ())
183 unsigned tag
= gcov_read_unsigned ();
184 unsigned length
= gcov_read_unsigned ();
185 unsigned long base
= gcov_position ();
186 tag_format_t
const *format
;
194 unsigned mask
= GCOV_TAG_MASK (tag
) >> 1;
196 for (tag_depth
= 4; mask
; mask
>>= 8)
198 if ((mask
& 0xff) != 0xff)
200 printf ("%s:tag `%08x' is invalid\n", filename
, tag
);
206 for (format
= tag_table
; format
->name
; format
++)
207 if (format
->tag
== tag
)
209 format
= &tag_table
[GCOV_TAG_IS_COUNTER (tag
) ? 2 : 1];
213 if (depth
&& depth
< tag_depth
)
215 if (!GCOV_TAG_IS_SUBTAG (tags
[depth
- 1], tag
))
216 printf ("%s:tag `%08x' is incorrectly nested\n",
220 tags
[depth
- 1] = tag
;
223 print_prefix (filename
, tag_depth
);
224 printf ("%08x:%4u:%s", tag
, length
, format
->name
);
226 (*format
->proc
) (filename
, tag
, length
);
229 if (flag_dump_contents
&& format
->proc
)
231 unsigned long actual_length
= gcov_position () - base
;
233 if (actual_length
> length
)
234 printf ("%s:record size mismatch %lu bytes overread\n",
235 filename
, actual_length
- length
);
236 else if (length
> actual_length
)
237 printf ("%s:record size mismatch %lu bytes unread\n",
238 filename
, length
- actual_length
);
240 gcov_seek (base
, length
);
241 if ((error
= gcov_is_error ()))
243 printf (error
< 0 ? "%s:counter overflow at %lu\n" :
244 "%s:read error at %lu\n", filename
, gcov_position ());
252 tag_function (filename
, tag
, length
)
253 const char *filename ATTRIBUTE_UNUSED
;
254 unsigned tag ATTRIBUTE_UNUSED
;
255 unsigned length ATTRIBUTE_UNUSED
;
257 unsigned long pos
= gcov_position ();
259 printf (" ident=%u", gcov_read_unsigned ());
260 printf (", checksum=0x%08x", gcov_read_unsigned ());
262 if (gcov_position () - pos
< length
)
266 name
= gcov_read_string ();
267 printf (", `%s'", name
? name
: "NULL");
268 name
= gcov_read_string ();
269 printf (" %s", name
? name
: "NULL");
270 printf (":%u", gcov_read_unsigned ());
275 tag_blocks (filename
, tag
, length
)
276 const char *filename ATTRIBUTE_UNUSED
;
277 unsigned tag ATTRIBUTE_UNUSED
;
278 unsigned length ATTRIBUTE_UNUSED
;
280 unsigned n_blocks
= length
/ 4;
282 printf (" %u blocks", n_blocks
);
284 if (flag_dump_contents
)
288 for (ix
= 0; ix
!= n_blocks
; ix
++)
291 printf ("\n%s:\t\t%u", filename
, ix
);
292 printf (" %04x", gcov_read_unsigned ());
298 tag_arcs (filename
, tag
, length
)
299 const char *filename ATTRIBUTE_UNUSED
;
300 unsigned tag ATTRIBUTE_UNUSED
;
301 unsigned length ATTRIBUTE_UNUSED
;
303 unsigned n_arcs
= (length
- 4) / 8;
305 printf (" %u arcs", n_arcs
);
306 if (flag_dump_contents
)
309 unsigned blockno
= gcov_read_unsigned ();
311 for (ix
= 0; ix
!= n_arcs
; ix
++)
313 unsigned dst
= gcov_read_unsigned ();
314 unsigned flags
= gcov_read_unsigned ();
317 printf ("\n%s:\tblock %u:", filename
, blockno
);
318 printf (" %u:%04x", dst
, flags
);
324 tag_lines (filename
, tag
, length
)
325 const char *filename ATTRIBUTE_UNUSED
;
326 unsigned tag ATTRIBUTE_UNUSED
;
327 unsigned length ATTRIBUTE_UNUSED
;
329 if (flag_dump_contents
)
331 unsigned blockno
= gcov_read_unsigned ();
332 char const *sep
= NULL
;
336 const char *source
= NULL
;
337 unsigned lineno
= gcov_read_unsigned ();
341 source
= gcov_read_string ();
349 printf ("\n%s:\tblock %u:", filename
, blockno
);
354 printf ("%s%u", sep
, lineno
);
359 printf ("%s`%s'", sep
, source
);
367 tag_counters (filename
, tag
, length
)
368 const char *filename ATTRIBUTE_UNUSED
;
369 unsigned tag ATTRIBUTE_UNUSED
;
370 unsigned length ATTRIBUTE_UNUSED
;
372 static const char *const counter_names
[] = GCOV_COUNTER_NAMES
;
373 unsigned n_counts
= length
/ 8;
375 printf (" %s %u counts",
376 counter_names
[GCOV_COUNTER_FOR_TAG (tag
)], n_counts
);
377 if (flag_dump_contents
)
381 for (ix
= 0; ix
!= n_counts
; ix
++)
383 gcov_type count
= gcov_read_counter ();
386 printf ("\n%s:\t\t%u", filename
, ix
);
388 printf (HOST_WIDEST_INT_PRINT_DEC
, count
);
394 tag_summary (filename
, tag
, length
)
395 const char *filename ATTRIBUTE_UNUSED
;
396 unsigned tag ATTRIBUTE_UNUSED
;
397 unsigned length ATTRIBUTE_UNUSED
;
399 struct gcov_summary summary
;
402 gcov_read_summary (&summary
);
403 printf (" checksum=0x%08x", summary
.checksum
);
405 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
407 printf ("\n%sL\t\tcounts=%u, runs=%u", filename
,
408 summary
.ctrs
[ix
].num
, summary
.ctrs
[ix
].runs
);
410 printf (", sum_all=" HOST_WIDEST_INT_PRINT_DEC
,
411 (HOST_WIDEST_INT
)summary
.ctrs
[ix
].sum_all
);
412 printf (", run_max=" HOST_WIDEST_INT_PRINT_DEC
,
413 (HOST_WIDEST_INT
)summary
.ctrs
[ix
].run_max
);
414 printf (", sum_max=" HOST_WIDEST_INT_PRINT_DEC
,
415 (HOST_WIDEST_INT
)summary
.ctrs
[ix
].sum_max
);