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"
26 typedef HOST_WIDEST_INT gcov_type
;
29 static void dump_file
PARAMS ((const char *));
30 static void print_prefix
PARAMS ((const char *, unsigned));
31 static void print_usage
PARAMS ((void));
32 static void print_version
PARAMS ((void));
33 static int tag_function
PARAMS ((const char *, FILE *, unsigned, unsigned));
34 static int tag_blocks
PARAMS ((const char *, FILE *, unsigned, unsigned));
35 static int tag_arcs
PARAMS ((const char *, FILE *, unsigned, unsigned));
36 static int tag_lines
PARAMS ((const char *, FILE *, unsigned, unsigned));
37 static int tag_arc_counts
PARAMS ((const char *, FILE *, unsigned, unsigned));
38 static int tag_summary
PARAMS ((const char *, FILE *, unsigned, unsigned));
39 extern int main
PARAMS ((int, char **));
41 typedef struct tag_format
45 int (*proc
) (const char *, FILE *, unsigned, unsigned);
48 static int flag_dump_contents
= 0;
50 static const struct option options
[] =
52 { "help", no_argument
, NULL
, 'h' },
53 { "version", no_argument
, NULL
, 'v' },
54 { "long", no_argument
, NULL
, 'l' },
57 static const tag_format_t tag_table
[] =
61 {GCOV_TAG_FUNCTION
, "FUNCTION", tag_function
},
62 {GCOV_TAG_BLOCKS
, "BLOCKS", tag_blocks
},
63 {GCOV_TAG_ARCS
, "ARCS", tag_arcs
},
64 {GCOV_TAG_LINES
, "LINES", tag_lines
},
65 {GCOV_TAG_ARC_COUNTS
, "ARC_COUNTS", tag_arc_counts
},
66 {GCOV_TAG_OBJECT_SUMMARY
, "OBJECT_SUMMARY", tag_summary
},
67 {GCOV_TAG_PROGRAM_SUMMARY
, "PROGRAM_SUMMARY", tag_summary
},
68 {GCOV_TAG_PLACEHOLDER_SUMMARY
, "PROGRAM_PLACEHOLDER", tag_summary
},
69 {GCOV_TAG_INCORRECT_SUMMARY
, "PROGRAM_INCORRECT", tag_summary
},
74 int argc ATTRIBUTE_UNUSED
;
79 while ((opt
= getopt_long (argc
, argv
, "hlv", options
, NULL
)) != -1)
90 flag_dump_contents
= 1;
93 fprintf (stderr
, "unknown flag `%c'\n", opt
);
98 dump_file (argv
[optind
++]);
105 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
106 printf ("Print coverage file contents\n");
107 printf (" -h, --help Print this help\n");
108 printf (" -v, --version Print version number\n");
109 printf (" -l, --long Dump record contents too\n");
116 unsigned version
= GCOV_VERSION
;
119 for (ix
= 4; ix
--; version
>>= 8)
121 printf ("gcov %.4s (GCC %s)\n", v
, version_string
);
122 printf ("Copyright (C) 2002 Free Software Foundation, Inc.\n");
123 printf ("This is free software; see the source for copying conditions. There is NO\n\
124 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
128 print_prefix (filename
, depth
)
129 const char *filename
;
132 static const char prefix
[] = " ";
134 printf ("%s:%.*s", filename
, (int) depth
, prefix
);
139 const char *filename
;
141 FILE *file
= fopen (filename
, "rb");
144 unsigned magic
, version
;
145 unsigned tag
, length
;
149 fprintf (stderr
, "%s:cannot open\n", filename
);
153 if (gcov_read_unsigned (file
, &magic
)
154 || gcov_read_unsigned (file
, &version
))
157 printf ("%s:read error at %ld\n", filename
, ftell (file
));
164 const char *type
= NULL
;
165 char e
[4], v
[4], m
[4];
166 unsigned expected
= GCOV_VERSION
;
168 int different
= version
!= GCOV_VERSION
;
170 if (magic
== GCOV_DATA_MAGIC
)
172 else if (magic
== GCOV_GRAPH_MAGIC
)
176 printf ("%s:not a gcov file\n", filename
);
180 for (ix
= 4; ix
--; expected
>>= 8, version
>>= 8, magic
>>= 8)
187 printf ("%s:%s:magic `%.4s':version `%.4s'\n", filename
, type
, m
, v
);
189 printf ("%s:warning:current version is `%.4s'\n", filename
, e
);
192 while (!gcov_read_unsigned (file
, &tag
)
193 && !gcov_read_unsigned (file
, &length
))
195 tag_format_t
const *format
;
199 base
= gcov_save_position (file
);
205 unsigned 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
);
217 for (format
= tag_table
; format
->name
; format
++)
218 if (format
->tag
== tag
)
220 format
= &tag_table
[1];
224 if (depth
&& depth
< tag_depth
)
226 if (!GCOV_TAG_IS_SUBTAG (tags
[depth
- 1], tag
))
227 printf ("%s:tag `%08x' is incorrectly nested\n",
231 tags
[depth
- 1] = tag
;
234 print_prefix (filename
, tag_depth
);
235 printf ("%08x:%4u:%s", tag
, length
, format
->name
);
237 if ((*format
->proc
) (filename
, file
, tag
, length
))
240 end
= gcov_save_position (file
);
241 gcov_resync (file
, base
, length
);
242 if (format
->proc
&& end
!= base
+ (long)length
)
244 if (end
> base
+ (long)length
)
245 printf ("%s:record size mismatch %lu bytes overread\n",
246 filename
, (end
- base
) - length
);
248 printf ("%s:record size mismatch %lu bytes unread\n",
249 filename
, length
- (end
- base
));
258 tag_function (filename
, file
, tag
, length
)
259 const char *filename ATTRIBUTE_UNUSED
;
260 FILE *file ATTRIBUTE_UNUSED
;
261 unsigned tag ATTRIBUTE_UNUSED
;
262 unsigned length ATTRIBUTE_UNUSED
;
267 if (gcov_read_string (file
, &name
, NULL
)
268 || gcov_read_unsigned (file
, &checksum
))
271 printf (" `%s' checksum=0x%08x", name
, checksum
);
278 tag_blocks (filename
, file
, tag
, length
)
279 const char *filename ATTRIBUTE_UNUSED
;
280 FILE *file ATTRIBUTE_UNUSED
;
281 unsigned tag ATTRIBUTE_UNUSED
;
282 unsigned length ATTRIBUTE_UNUSED
;
284 unsigned n_blocks
= length
/ 4;
286 printf (" %u blocks", n_blocks
);
288 if (flag_dump_contents
)
292 for (ix
= 0; ix
!= n_blocks
; ix
++)
295 if (gcov_read_unsigned (file
, &flags
))
298 printf ("\n%s:\t\t%u", filename
, ix
);
299 printf (" %04x", flags
);
304 gcov_skip (file
, n_blocks
* 4);
310 tag_arcs (filename
, file
, tag
, length
)
311 const char *filename ATTRIBUTE_UNUSED
;
312 FILE *file ATTRIBUTE_UNUSED
;
313 unsigned tag ATTRIBUTE_UNUSED
;
314 unsigned length ATTRIBUTE_UNUSED
;
316 unsigned n_arcs
= (length
- 4) / 8;
318 printf (" %u arcs", n_arcs
);
319 if (flag_dump_contents
)
324 if (gcov_read_unsigned (file
, &blockno
))
327 for (ix
= 0; ix
!= n_arcs
; ix
++)
331 if (gcov_read_unsigned (file
, &dst
)
332 || gcov_read_unsigned (file
, &flags
))
335 printf ("\n%s:\t\t%u:", filename
, blockno
);
336 printf (" %u:%04x", dst
, flags
);
340 gcov_skip (file
, 4 + n_arcs
* 8);
346 tag_lines (filename
, file
, tag
, length
)
347 const char *filename ATTRIBUTE_UNUSED
;
348 FILE *file ATTRIBUTE_UNUSED
;
349 unsigned tag ATTRIBUTE_UNUSED
;
350 unsigned length ATTRIBUTE_UNUSED
;
352 if (flag_dump_contents
)
356 char const *sep
= NULL
;
358 if (gcov_read_unsigned (file
, &blockno
))
365 if (gcov_read_unsigned (file
, &lineno
))
372 if (gcov_read_string (file
, &source
, NULL
))
381 printf ("\n%s:\t\t%u:", filename
, blockno
);
386 printf ("%s%u", sep
, lineno
);
391 printf ("%s`%s'", sep
, source
);
397 gcov_skip (file
, length
);
403 tag_arc_counts (filename
, file
, tag
, length
)
404 const char *filename ATTRIBUTE_UNUSED
;
405 FILE *file ATTRIBUTE_UNUSED
;
406 unsigned tag ATTRIBUTE_UNUSED
;
407 unsigned length ATTRIBUTE_UNUSED
;
409 unsigned n_counts
= length
/ 8;
411 printf (" %u counts", n_counts
);
412 if (flag_dump_contents
)
416 for (ix
= 0; ix
!= n_counts
; ix
++)
420 if (gcov_read_counter (file
, &count
))
423 printf ("\n%s:\t\t%u", filename
, ix
);
425 printf (HOST_WIDEST_INT_PRINT_DEC
, count
);
429 gcov_skip (file
, n_counts
* 8);
435 tag_summary (filename
, file
, tag
, length
)
436 const char *filename ATTRIBUTE_UNUSED
;
437 FILE *file ATTRIBUTE_UNUSED
;
438 unsigned tag ATTRIBUTE_UNUSED
;
439 unsigned length ATTRIBUTE_UNUSED
;
441 struct gcov_summary summary
;
443 if (gcov_read_summary (file
, &summary
))
445 printf (" checksum=0x%08x", summary
.checksum
);
447 printf ("\n%s:\t\truns=%u, arcs=%u", filename
,
448 summary
.runs
, summary
.arcs
);
449 printf ("\n%s:\t\tarc_sum=", filename
);
450 printf (HOST_WIDEST_INT_PRINT_DEC
,
451 (HOST_WIDEST_INT
)summary
.arc_sum
);
452 printf (", arc_max_one=");
453 printf (HOST_WIDEST_INT_PRINT_DEC
,
454 (HOST_WIDEST_INT
)summary
.arc_max_one
);
455 printf ("\n%s:\t\tmax_sum=", filename
);
456 printf (HOST_WIDEST_INT_PRINT_DEC
,
457 (HOST_WIDEST_INT
)summary
.arc_max_sum
);
458 printf (", sum_max=");
459 printf (HOST_WIDEST_INT_PRINT_DEC
,
460 (HOST_WIDEST_INT
)summary
.arc_sum_max
);