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