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"
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 void tag_function
PARAMS ((const char *, unsigned, unsigned));
34 static void tag_blocks
PARAMS ((const char *, unsigned, unsigned));
35 static void tag_arcs
PARAMS ((const char *, unsigned, unsigned));
36 static void tag_lines
PARAMS ((const char *, unsigned, unsigned));
37 static void tag_arc_counts
PARAMS ((const char *, unsigned, unsigned));
38 static void tag_summary
PARAMS ((const char *, unsigned, unsigned));
39 extern int main
PARAMS ((int, char **));
41 typedef struct tag_format
45 void (*proc
) (const char *, 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
;
144 if (!gcov_open (filename
, 1))
146 fprintf (stderr
, "%s:cannot open\n", filename
);
152 unsigned magic
= gcov_read_unsigned ();
153 unsigned version
= gcov_read_unsigned ();
154 const char *type
= NULL
;
155 char e
[4], v
[4], m
[4];
156 unsigned expected
= GCOV_VERSION
;
158 int different
= version
!= GCOV_VERSION
;
160 if (magic
== GCOV_DATA_MAGIC
)
162 else if (magic
== GCOV_GRAPH_MAGIC
)
166 printf ("%s:not a gcov file\n", filename
);
170 for (ix
= 4; ix
--; expected
>>= 8, version
>>= 8, magic
>>= 8)
177 printf ("%s:%s:magic `%.4s':version `%.4s'\n", filename
, type
, m
, v
);
179 printf ("%s:warning:current version is `%.4s'\n", filename
, e
);
182 while (!gcov_is_eof ())
184 unsigned tag
= gcov_read_unsigned ();
185 unsigned length
= gcov_read_unsigned ();
186 unsigned long base
= gcov_position ();
187 tag_format_t
const *format
;
195 unsigned mask
= GCOV_TAG_MASK (tag
) >> 1;
197 for (tag_depth
= 4; mask
; mask
>>= 8)
199 if ((mask
& 0xff) != 0xff)
201 printf ("%s:tag `%08x' is invalid\n", filename
, tag
);
207 for (format
= tag_table
; format
->name
; format
++)
208 if (format
->tag
== tag
)
210 format
= &tag_table
[1];
214 if (depth
&& depth
< tag_depth
)
216 if (!GCOV_TAG_IS_SUBTAG (tags
[depth
- 1], tag
))
217 printf ("%s:tag `%08x' is incorrectly nested\n",
221 tags
[depth
- 1] = tag
;
224 print_prefix (filename
, tag_depth
);
225 printf ("%08x:%4u:%s", tag
, length
, format
->name
);
227 (*format
->proc
) (filename
, tag
, length
);
230 if (flag_dump_contents
&& format
->proc
)
232 unsigned long actual_length
= gcov_position () - base
;
234 if (actual_length
> length
)
235 printf ("%s:record size mismatch %lu bytes overread\n",
236 filename
, actual_length
- length
);
237 else if (length
> actual_length
)
238 printf ("%s:record size mismatch %lu bytes unread\n",
239 filename
, length
- actual_length
);
241 gcov_seek (base
, length
);
242 if ((error
= gcov_is_error ()))
244 printf (error
< 0 ? "%s:counter overflow at %lu\n" :
245 "%s:read error at %lu\n", filename
, gcov_position ());
253 tag_function (filename
, tag
, length
)
254 const char *filename ATTRIBUTE_UNUSED
;
255 unsigned tag ATTRIBUTE_UNUSED
;
256 unsigned length ATTRIBUTE_UNUSED
;
259 unsigned long pos
= gcov_position ();
261 name
= gcov_read_string ();
262 printf (" `%s'", name
? name
: "NULL");
263 printf (" checksum=0x%08x", gcov_read_unsigned ());
265 if (gcov_position () - pos
< length
)
267 name
= gcov_read_string ();
268 printf (" %s", name
? name
: "NULL");
269 printf (":%u", gcov_read_unsigned ());
274 tag_blocks (filename
, tag
, length
)
275 const char *filename ATTRIBUTE_UNUSED
;
276 unsigned tag ATTRIBUTE_UNUSED
;
277 unsigned length ATTRIBUTE_UNUSED
;
279 unsigned n_blocks
= length
/ 4;
281 printf (" %u blocks", n_blocks
);
283 if (flag_dump_contents
)
287 for (ix
= 0; ix
!= n_blocks
; ix
++)
290 printf ("\n%s:\t\t%u", filename
, ix
);
291 printf (" %04x", gcov_read_unsigned ());
297 tag_arcs (filename
, tag
, length
)
298 const char *filename ATTRIBUTE_UNUSED
;
299 unsigned tag ATTRIBUTE_UNUSED
;
300 unsigned length ATTRIBUTE_UNUSED
;
302 unsigned n_arcs
= (length
- 4) / 8;
304 printf (" %u arcs", n_arcs
);
305 if (flag_dump_contents
)
308 unsigned blockno
= gcov_read_unsigned ();
310 for (ix
= 0; ix
!= n_arcs
; ix
++)
312 unsigned dst
= gcov_read_unsigned ();
313 unsigned flags
= gcov_read_unsigned ();
316 printf ("\n%s:\tblock %u:", filename
, blockno
);
317 printf (" %u:%04x", dst
, flags
);
323 tag_lines (filename
, tag
, length
)
324 const char *filename ATTRIBUTE_UNUSED
;
325 unsigned tag ATTRIBUTE_UNUSED
;
326 unsigned length ATTRIBUTE_UNUSED
;
328 if (flag_dump_contents
)
330 unsigned blockno
= gcov_read_unsigned ();
331 char const *sep
= NULL
;
335 const char *source
= NULL
;
336 unsigned lineno
= gcov_read_unsigned ();
340 source
= gcov_read_string ();
348 printf ("\n%s:\tblock %u:", filename
, blockno
);
353 printf ("%s%u", sep
, lineno
);
358 printf ("%s`%s'", sep
, source
);
366 tag_arc_counts (filename
, tag
, length
)
367 const char *filename ATTRIBUTE_UNUSED
;
368 unsigned tag ATTRIBUTE_UNUSED
;
369 unsigned length ATTRIBUTE_UNUSED
;
371 unsigned n_counts
= length
/ 8;
373 printf (" %u counts", n_counts
);
374 if (flag_dump_contents
)
378 for (ix
= 0; ix
!= n_counts
; ix
++)
380 gcov_type count
= gcov_read_counter ();
383 printf ("\n%s:\t\t%u", filename
, ix
);
385 printf (HOST_WIDEST_INT_PRINT_DEC
, count
);
391 tag_summary (filename
, tag
, length
)
392 const char *filename ATTRIBUTE_UNUSED
;
393 unsigned tag ATTRIBUTE_UNUSED
;
394 unsigned length ATTRIBUTE_UNUSED
;
396 struct gcov_summary summary
;
398 gcov_read_summary (&summary
);
400 printf (" checksum=0x%08x", summary
.checksum
);
402 printf ("\n%s:\t\truns=%u, arcs=%u", filename
,
403 summary
.runs
, summary
.arcs
);
404 printf ("\n%s:\t\tarc_sum=", filename
);
405 printf (HOST_WIDEST_INT_PRINT_DEC
,
406 (HOST_WIDEST_INT
)summary
.arc_sum
);
407 printf (", arc_max_one=");
408 printf (HOST_WIDEST_INT_PRINT_DEC
,
409 (HOST_WIDEST_INT
)summary
.arc_max_one
);
410 printf ("\n%s:\t\tmax_sum=", filename
);
411 printf (HOST_WIDEST_INT_PRINT_DEC
,
412 (HOST_WIDEST_INT
)summary
.arc_max_sum
);
413 printf (", sum_max=");
414 printf (HOST_WIDEST_INT_PRINT_DEC
,
415 (HOST_WIDEST_INT
)summary
.arc_sum_max
);