* configure.in (arm-*-netbsdelf*): Enable building java libraries.
[official-gcc.git] / gcc / gcov-dump.c
blobba56fee7e378ec3df7f0caf436014c5655e43e79
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 #define IN_GCOV (-1)
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 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
43 unsigned tag;
44 char const *name;
45 void (*proc) (const char *, 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, (int) depth, prefix);
137 static void
138 dump_file (filename)
139 const char *filename;
141 unsigned tags[4];
142 unsigned depth = 0;
144 if (!gcov_open (filename, 1))
146 fprintf (stderr, "%s:cannot open\n", filename);
147 return;
150 /* magic */
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;
157 unsigned ix;
158 int different = version != GCOV_VERSION;
160 if (magic == GCOV_DATA_MAGIC)
161 type = "data";
162 else if (magic == GCOV_GRAPH_MAGIC)
163 type = "graph";
164 else
166 printf ("%s:not a gcov file\n", filename);
167 gcov_close ();
168 return;
170 for (ix = 4; ix--; expected >>= 8, version >>= 8, magic >>= 8)
172 e[ix] = expected;
173 v[ix] = version;
174 m[ix] = magic;
177 printf ("%s:%s:magic `%.4s':version `%.4s'\n", filename, type, m, v);
178 if (different)
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;
188 unsigned tag_depth;
189 int error;
191 if (!tag)
192 tag_depth = depth;
193 else
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);
202 break;
204 tag_depth--;
207 for (format = tag_table; format->name; format++)
208 if (format->tag == tag)
209 goto found;
210 format = &tag_table[1];
211 found:;
212 if (tag)
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",
218 filename, tag);
220 depth = tag_depth;
221 tags[depth - 1] = tag;
224 print_prefix (filename, tag_depth);
225 printf ("%08x:%4u:%s", tag, length, format->name);
226 if (format->proc)
227 (*format->proc) (filename, tag, length);
229 printf ("\n");
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 ());
246 break;
249 gcov_close ();
252 static void
253 tag_function (filename, tag, length)
254 const char *filename ATTRIBUTE_UNUSED;
255 unsigned tag ATTRIBUTE_UNUSED;
256 unsigned length ATTRIBUTE_UNUSED;
258 const char *name;
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 ());
273 static void
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)
285 unsigned ix;
287 for (ix = 0; ix != n_blocks; ix++)
289 if (!(ix & 7))
290 printf ("\n%s:\t\t%u", filename, ix);
291 printf (" %04x", gcov_read_unsigned ());
296 static void
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)
307 unsigned ix;
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 ();
315 if (!(ix & 3))
316 printf ("\n%s:\tblock %u:", filename, blockno);
317 printf (" %u:%04x", dst, flags);
322 static void
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;
333 while (1)
335 const char *source = NULL;
336 unsigned lineno = gcov_read_unsigned ();
338 if (!lineno)
340 source = gcov_read_string ();
341 if (!source)
342 break;
343 sep = NULL;
346 if (!sep)
348 printf ("\n%s:\tblock %u:", filename, blockno);
349 sep = "";
351 if (lineno)
353 printf ("%s%u", sep, lineno);
354 sep = ", ";
356 else
358 printf ("%s`%s'", sep, source);
359 sep = ":";
365 static void
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)
376 unsigned ix;
378 for (ix = 0; ix != n_counts; ix++)
380 gcov_type count = gcov_read_counter ();
382 if (!(ix & 7))
383 printf ("\n%s:\t\t%u", filename, ix);
384 printf (" ");
385 printf (HOST_WIDEST_INT_PRINT_DEC, count);
390 static void
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);