gcov-io.h: Add a local time stamp.
[gcc.git] / gcc / gcov-dump.c
1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
4
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.
9
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.
14
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. */
19
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"
28 #include "gcov-io.c"
29
30 static void dump_file (const char *);
31 static void print_prefix (const char *, unsigned, gcov_position_t);
32 static void print_usage (void);
33 static void print_version (void);
34 static void tag_function (const char *, unsigned, unsigned);
35 static void tag_blocks (const char *, unsigned, unsigned);
36 static void tag_arcs (const char *, unsigned, unsigned);
37 static void tag_lines (const char *, unsigned, unsigned);
38 static void tag_counters (const char *, unsigned, unsigned);
39 static void tag_summary (const char *, unsigned, unsigned);
40 extern int main (int, char **);
41
42 typedef struct tag_format
43 {
44 unsigned tag;
45 char const *name;
46 void (*proc) (const char *, unsigned, unsigned);
47 } tag_format_t;
48
49 static int flag_dump_contents = 0;
50 static int flag_dump_positions = 0;
51
52 static const struct option options[] =
53 {
54 { "help", no_argument, NULL, 'h' },
55 { "version", no_argument, NULL, 'v' },
56 { "long", no_argument, NULL, 'l' },
57 { "positions", no_argument, NULL, 'o' },
58 { 0, 0, 0, 0 }
59 };
60
61 static const tag_format_t tag_table[] =
62 {
63 {0, "NOP", NULL},
64 {0, "UNKNOWN", NULL},
65 {0, "COUNTERS", tag_counters},
66 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function},
67 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks},
68 {GCOV_TAG_ARCS, "ARCS", tag_arcs},
69 {GCOV_TAG_LINES, "LINES", tag_lines},
70 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary},
71 {GCOV_TAG_PROGRAM_SUMMARY, "PROGRAM_SUMMARY", tag_summary},
72 {0, NULL, NULL}
73 };
74
75 int
76 main (int argc ATTRIBUTE_UNUSED, char **argv)
77 {
78 int opt;
79
80 while ((opt = getopt_long (argc, argv, "hlpv", options, NULL)) != -1)
81 {
82 switch (opt)
83 {
84 case 'h':
85 print_usage ();
86 break;
87 case 'v':
88 print_version ();
89 break;
90 case 'l':
91 flag_dump_contents = 1;
92 break;
93 case 'p':
94 flag_dump_positions = 1;
95 break;
96 default:
97 fprintf (stderr, "unknown flag `%c'\n", opt);
98 }
99 }
100
101 while (argv[optind])
102 dump_file (argv[optind++]);
103 return 0;
104 }
105
106 static void
107 print_usage (void)
108 {
109 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
110 printf ("Print coverage file contents\n");
111 printf (" -h, --help Print this help\n");
112 printf (" -v, --version Print version number\n");
113 printf (" -l, --long Dump record contents too\n");
114 printf (" -p, --positions Dump record positions\n");
115 }
116
117 static void
118 print_version (void)
119 {
120 char v[4];
121 unsigned version = GCOV_VERSION;
122 unsigned ix;
123
124 for (ix = 4; ix--; version >>= 8)
125 v[ix] = version;
126 printf ("gcov %.4s (GCC %s)\n", v, version_string);
127 printf ("Copyright (C) 2002 Free Software Foundation, Inc.\n");
128 printf ("This is free software; see the source for copying conditions. There is NO\n\
129 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
130 }
131
132 static void
133 print_prefix (const char *filename, unsigned depth, gcov_position_t position)
134 {
135 static const char prefix[] = " ";
136
137 printf ("%s:", filename);
138 if (flag_dump_positions)
139 printf ("%lu:", (unsigned long) position);
140 printf ("%.*s", (int) depth, prefix);
141 }
142
143 static void
144 dump_file (const char *filename)
145 {
146 unsigned tags[4];
147 unsigned depth = 0;
148
149 if (!gcov_open (filename, 1))
150 {
151 fprintf (stderr, "%s:cannot open\n", filename);
152 return;
153 }
154
155 /* magic */
156 {
157 unsigned magic = gcov_read_unsigned ();
158 unsigned version = gcov_read_unsigned ();
159 const char *type = NULL;
160 char e[4], v[4], m[4];
161 unsigned expected = GCOV_VERSION;
162 unsigned ix;
163 int different = version != GCOV_VERSION;
164
165 if (magic == GCOV_DATA_MAGIC)
166 type = "data";
167 else if (magic == GCOV_GRAPH_MAGIC)
168 type = "graph";
169 else
170 {
171 printf ("%s:not a gcov file\n", filename);
172 gcov_close ();
173 return;
174 }
175 for (ix = 4; ix--; expected >>= 8, version >>= 8, magic >>= 8)
176 {
177 e[ix] = expected;
178 v[ix] = version;
179 m[ix] = magic;
180 }
181
182 printf ("%s:%s:magic `%.4s':version `%.4s'\n", filename, type, m, v);
183 if (different)
184 printf ("%s:warning:current version is `%.4s'\n", filename, e);
185 }
186
187 /* stamp */
188 {
189 unsigned stamp = gcov_read_unsigned ();
190
191 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
192 }
193
194 while (1)
195 {
196 gcov_position_t base, position = gcov_position ();
197 unsigned tag, length;
198 tag_format_t const *format;
199 unsigned tag_depth;
200 int error;
201 unsigned mask;
202
203 tag = gcov_read_unsigned ();
204 if (!tag)
205 break;
206 length = gcov_read_unsigned ();
207 base = gcov_position ();
208 mask = GCOV_TAG_MASK (tag) >> 1;
209 for (tag_depth = 4; mask; mask >>= 8)
210 {
211 if ((mask & 0xff) != 0xff)
212 {
213 printf ("%s:tag `%08x' is invalid\n", filename, tag);
214 break;
215 }
216 tag_depth--;
217 }
218 for (format = tag_table; format->name; format++)
219 if (format->tag == tag)
220 goto found;
221 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
222 found:;
223 if (tag)
224 {
225 if (depth && depth < tag_depth)
226 {
227 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
228 printf ("%s:tag `%08x' is incorrectly nested\n",
229 filename, tag);
230 }
231 depth = tag_depth;
232 tags[depth - 1] = tag;
233 }
234
235 print_prefix (filename, tag_depth, position);
236 printf ("%08x:%4u:%s", tag, length, format->name);
237 if (format->proc)
238 (*format->proc) (filename, tag, length);
239
240 printf ("\n");
241 if (flag_dump_contents && format->proc)
242 {
243 unsigned long actual_length = gcov_position () - base;
244
245 if (actual_length > length)
246 printf ("%s:record size mismatch %lu bytes overread\n",
247 filename, actual_length - length);
248 else if (length > actual_length)
249 printf ("%s:record size mismatch %lu bytes unread\n",
250 filename, length - actual_length);
251 }
252 gcov_sync (base, length);
253 if ((error = gcov_is_error ()))
254 {
255 printf (error < 0 ? "%s:counter overflow at %lu\n" :
256 "%s:read error at %lu\n", filename,
257 (long unsigned) gcov_position ());
258 break;
259 }
260 }
261 if (!gcov_is_eof ())
262 printf ("%s:early end of file\n", filename);
263 gcov_close ();
264 }
265
266 static void
267 tag_function (const char *filename ATTRIBUTE_UNUSED,
268 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
269 {
270 unsigned long pos = gcov_position ();
271
272 printf (" ident=%u", gcov_read_unsigned ());
273 printf (", checksum=0x%08x", gcov_read_unsigned ());
274
275 if (gcov_position () - pos < length)
276 {
277 const char *name;
278
279 name = gcov_read_string ();
280 printf (", `%s'", name ? name : "NULL");
281 name = gcov_read_string ();
282 printf (" %s", name ? name : "NULL");
283 printf (":%u", gcov_read_unsigned ());
284 }
285 }
286
287 static void
288 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
289 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
290 {
291 unsigned n_blocks = length / 4;
292
293 printf (" %u blocks", n_blocks);
294
295 if (flag_dump_contents)
296 {
297 unsigned ix;
298
299 for (ix = 0; ix != n_blocks; ix++)
300 {
301 if (!(ix & 7))
302 {
303 printf ("\n");
304 print_prefix (filename, 0, gcov_position ());
305 printf ("\t\t%u", ix);
306 }
307 printf (" %04x", gcov_read_unsigned ());
308 }
309 }
310 }
311
312 static void
313 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
314 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
315 {
316 unsigned n_arcs = (length - 4) / 8;
317
318 printf (" %u arcs", n_arcs);
319 if (flag_dump_contents)
320 {
321 unsigned ix;
322 unsigned blockno = gcov_read_unsigned ();
323
324 for (ix = 0; ix != n_arcs; ix++)
325 {
326 unsigned dst, flags;
327
328 if (!(ix & 3))
329 {
330 printf ("\n");
331 print_prefix (filename, 0, gcov_position ());
332 printf ("\tblock %u:", blockno);
333 }
334 dst = gcov_read_unsigned ();
335 flags = gcov_read_unsigned ();
336 printf (" %u:%04x", dst, flags);
337 }
338 }
339 }
340
341 static void
342 tag_lines (const char *filename ATTRIBUTE_UNUSED,
343 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
344 {
345 if (flag_dump_contents)
346 {
347 unsigned blockno = gcov_read_unsigned ();
348 char const *sep = NULL;
349
350 while (1)
351 {
352 gcov_position_t position = gcov_position ();
353 const char *source = NULL;
354 unsigned lineno = gcov_read_unsigned ();
355
356 if (!lineno)
357 {
358 source = gcov_read_string ();
359 if (!source)
360 break;
361 sep = NULL;
362 }
363
364 if (!sep)
365 {
366 printf ("\n");
367 print_prefix (filename, 0, position);
368 printf ("\tblock %u:", blockno);
369 sep = "";
370 }
371 if (lineno)
372 {
373 printf ("%s%u", sep, lineno);
374 sep = ", ";
375 }
376 else
377 {
378 printf ("%s`%s'", sep, source);
379 sep = ":";
380 }
381 }
382 }
383 }
384
385 static void
386 tag_counters (const char *filename ATTRIBUTE_UNUSED,
387 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
388 {
389 static const char *const counter_names[] = GCOV_COUNTER_NAMES;
390 unsigned n_counts = length / 8;
391
392 printf (" %s %u counts",
393 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts);
394 if (flag_dump_contents)
395 {
396 unsigned ix;
397
398 for (ix = 0; ix != n_counts; ix++)
399 {
400 gcov_type count;
401
402 if (!(ix & 7))
403 {
404 printf ("\n");
405 print_prefix (filename, 0, gcov_position ());
406 printf ("\t\t%u", ix);
407 }
408
409 count = gcov_read_counter ();
410 printf (" ");
411 printf (HOST_WIDEST_INT_PRINT_DEC, count);
412 }
413 }
414 }
415
416 static void
417 tag_summary (const char *filename ATTRIBUTE_UNUSED,
418 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
419 {
420 struct gcov_summary summary;
421 unsigned ix;
422
423 gcov_read_summary (&summary);
424 printf (" checksum=0x%08x", summary.checksum);
425
426 for (ix = 0; ix != GCOV_COUNTERS; ix++)
427 {
428 printf ("\n");
429 print_prefix (filename, 0, 0);
430 printf ("\t\tcounts=%u, runs=%u",
431 summary.ctrs[ix].num, summary.ctrs[ix].runs);
432
433 printf (", sum_all=" HOST_WIDEST_INT_PRINT_DEC,
434 (HOST_WIDEST_INT)summary.ctrs[ix].sum_all);
435 printf (", run_max=" HOST_WIDEST_INT_PRINT_DEC,
436 (HOST_WIDEST_INT)summary.ctrs[ix].run_max);
437 printf (", sum_max=" HOST_WIDEST_INT_PRINT_DEC,
438 (HOST_WIDEST_INT)summary.ctrs[ix].sum_max);
439 }
440 }