FSF GCC merge 02/23/03
[official-gcc.git] / gcc / gcov-dump.c
blob4da05917e75b976bb3ded8e3fc1e23dc1358536d
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)
8 any later version.
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. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "version.h"
25 #include <getopt.h>
26 typedef HOST_WIDEST_INT gcov_type;
27 #include "gcov-io.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 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
43 unsigned tag;
44 char const *name;
45 int (*proc) (const char *, FILE *, unsigned, unsigned);
46 } tag_format_t;
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[] =
59 {0, "NOP", NULL},
60 {0, "UNKNOWN", NULL},
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},
70 {0, NULL, NULL}
73 int main (argc, argv)
74 int argc ATTRIBUTE_UNUSED;
75 char **argv;
77 int opt;
79 while ((opt = getopt_long (argc, argv, "hlv", options, NULL)) != -1)
81 switch (opt)
83 case 'h':
84 print_usage ();
85 break;
86 case 'v':
87 print_version ();
88 break;
89 case 'l':
90 flag_dump_contents = 1;
91 break;
92 default:
93 fprintf (stderr, "unknown flag `%c'\n", opt);
97 while (argv[optind])
98 dump_file (argv[optind++]);
99 return 0;
102 static void
103 print_usage ()
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");
112 static void
113 print_version ()
115 char v[4];
116 unsigned version = GCOV_VERSION;
117 unsigned ix;
119 for (ix = 4; ix--; version >>= 8)
120 v[ix] = version;
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");
127 static void
128 print_prefix (filename, depth)
129 const char *filename;
130 unsigned depth;
132 static const char prefix[] = " ";
134 printf ("%s:%.*s", filename, depth, prefix);
137 static void
138 dump_file (filename)
139 const char *filename;
141 FILE *file = fopen (filename, "rb");
142 unsigned tags[4];
143 unsigned depth = 0;
144 unsigned magic, version;
145 unsigned tag, length;
147 if (!file)
149 fprintf (stderr, "%s:cannot open\n", filename);
150 return;
153 if (gcov_read_unsigned (file, &magic)
154 || gcov_read_unsigned (file, &version))
156 read_error:;
157 printf ("%s:read error at %ld\n", filename, ftell (file));
158 fclose (file);
159 return;
162 /* magic */
164 const char *type = NULL;
165 char e[4], v[4], m[4];
166 unsigned expected = GCOV_VERSION;
167 unsigned ix;
168 int different = version != GCOV_VERSION;
170 if (magic == GCOV_DATA_MAGIC)
171 type = "data";
172 else if (magic == GCOV_GRAPH_MAGIC)
173 type = "graph";
174 else
176 printf ("%s:not a gcov file\n", filename);
177 fclose (file);
178 return;
180 for (ix = 4; ix--; expected >>= 8, version >>= 8, magic >>= 8)
182 e[ix] = expected;
183 v[ix] = version;
184 m[ix] = magic;
187 printf ("%s:%s:magic `%.4s':version `%.4s'\n", filename, type, m, v);
188 if (different)
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;
196 unsigned tag_depth;
197 long base, end;
199 base = gcov_save_position (file);
201 if (!tag)
202 tag_depth = depth;
203 else
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);
212 break;
214 tag_depth--;
217 for (format = tag_table; format->name; format++)
218 if (format->tag == tag)
219 goto found;
220 format = &tag_table[1];
221 found:;
222 if (tag)
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",
228 filename, tag);
230 depth = tag_depth;
231 tags[depth - 1] = tag;
234 print_prefix (filename, tag_depth);
235 printf ("%08x:%4u:%s", tag, length, format->name);
236 if (format->proc)
237 if ((*format->proc) (filename, file, tag, length))
238 goto read_error;
239 printf ("\n");
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);
247 else
248 printf ("%s:record size mismatch %lu bytes unread\n",
249 filename, length - (end - base));
252 if (!feof (file))
253 goto read_error;
254 fclose (file);
257 static int
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;
264 char *name = NULL;
265 unsigned checksum;
267 if (gcov_read_string (file, &name, NULL)
268 || gcov_read_unsigned (file, &checksum))
269 return 1;
271 printf (" `%s' checksum=0x%08x", name, checksum);
272 free (name);
274 return 0;
277 static int
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)
290 unsigned ix;
292 for (ix = 0; ix != n_blocks; ix++)
294 unsigned flags;
295 if (gcov_read_unsigned (file, &flags))
296 return 1;
297 if (!(ix & 7))
298 printf ("\n%s:\t\t%u", filename, ix);
299 printf (" %04x", flags);
303 else
304 gcov_skip (file, n_blocks * 4);
306 return 0;
309 static int
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)
321 unsigned ix;
322 unsigned blockno;
324 if (gcov_read_unsigned (file, &blockno))
325 return 1;
327 for (ix = 0; ix != n_arcs; ix++)
329 unsigned dst, flags;
331 if (gcov_read_unsigned (file, &dst)
332 || gcov_read_unsigned (file, &flags))
333 return 1;
334 if (!(ix & 3))
335 printf ("\n%s:\t\t%u:", filename, blockno);
336 printf (" %u:%04x", dst, flags);
339 else
340 gcov_skip (file, 4 + n_arcs * 8);
342 return 0;
345 static int
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)
354 char *source = NULL;
355 unsigned blockno;
356 char const *sep = NULL;
358 if (gcov_read_unsigned (file, &blockno))
359 return 1;
361 while (1)
363 unsigned lineno;
365 if (gcov_read_unsigned (file, &lineno))
367 free (source);
368 return 1;
370 if (!lineno)
372 if (gcov_read_string (file, &source, NULL))
373 return 1;
374 if (!source)
375 break;
376 sep = NULL;
379 if (!sep)
381 printf ("\n%s:\t\t%u:", filename, blockno);
382 sep = "";
384 if (lineno)
386 printf ("%s%u", sep, lineno);
387 sep = ", ";
389 else
391 printf ("%s`%s'", sep, source);
392 sep = ":";
396 else
397 gcov_skip (file, length);
399 return 0;
402 static int
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)
414 unsigned ix;
416 for (ix = 0; ix != n_counts; ix++)
418 gcov_type count;
420 if (gcov_read_counter (file, &count))
421 return 1;
422 if (!(ix & 7))
423 printf ("\n%s:\t\t%u", filename, ix);
424 printf (" ");
425 printf (HOST_WIDEST_INT_PRINT_DEC, count);
428 else
429 gcov_skip (file, n_counts * 8);
431 return 0;
434 static int
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))
444 return 1;
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);
461 return 0;