]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gcov-dump.c
collect2.c (main): Unlock the stdio streams.
[thirdparty/gcc.git] / gcc / gcov-dump.c
1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002, 2003, 2004 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 /* Unlock the stdio streams. */
81 unlock_stream (stdin);
82 unlock_stream (stdout);
83 unlock_stream (stderr);
84
85 while ((opt = getopt_long (argc, argv, "hlpv", options, NULL)) != -1)
86 {
87 switch (opt)
88 {
89 case 'h':
90 print_usage ();
91 break;
92 case 'v':
93 print_version ();
94 break;
95 case 'l':
96 flag_dump_contents = 1;
97 break;
98 case 'p':
99 flag_dump_positions = 1;
100 break;
101 default:
102 fprintf (stderr, "unknown flag `%c'\n", opt);
103 }
104 }
105
106 while (argv[optind])
107 dump_file (argv[optind++]);
108 return 0;
109 }
110
111 static void
112 print_usage (void)
113 {
114 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
115 printf ("Print coverage file contents\n");
116 printf (" -h, --help Print this help\n");
117 printf (" -v, --version Print version number\n");
118 printf (" -l, --long Dump record contents too\n");
119 printf (" -p, --positions Dump record positions\n");
120 }
121
122 static void
123 print_version (void)
124 {
125 printf ("gcov-dump (GCC) %s\n", version_string);
126 printf ("Copyright (C) 2003 Free Software Foundation, Inc.\n");
127 printf ("This is free software; see the source for copying conditions.\n"
128 "There is NO warranty; not even for MERCHANTABILITY or \n"
129 "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;
159 const char *type = NULL;
160 int endianness = 0;
161 char m[4], v[4];
162
163 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
164 type = "data";
165 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
166 type = "note";
167 else
168 {
169 printf ("%s:not a gcov file\n", filename);
170 gcov_close ();
171 return;
172 }
173 version = gcov_read_unsigned ();
174 GCOV_UNSIGNED2STRING (v, version);
175 GCOV_UNSIGNED2STRING (m, magic);
176
177 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename, type,
178 m, v, endianness < 0 ? " (swapped endianness)" : "");
179 if (version != GCOV_VERSION)
180 {
181 char e[4];
182
183 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
184 printf ("%s:warning:current version is `%.4s'\n", filename, e);
185 }
186 }
187
188 /* stamp */
189 {
190 unsigned stamp = gcov_read_unsigned ();
191
192 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
193 }
194
195 while (1)
196 {
197 gcov_position_t base, position = gcov_position ();
198 unsigned tag, length;
199 tag_format_t const *format;
200 unsigned tag_depth;
201 int error;
202 unsigned mask;
203
204 tag = gcov_read_unsigned ();
205 if (!tag)
206 break;
207 length = gcov_read_unsigned ();
208 base = gcov_position ();
209 mask = GCOV_TAG_MASK (tag) >> 1;
210 for (tag_depth = 4; mask; mask >>= 8)
211 {
212 if ((mask & 0xff) != 0xff)
213 {
214 printf ("%s:tag `%08x' is invalid\n", filename, tag);
215 break;
216 }
217 tag_depth--;
218 }
219 for (format = tag_table; format->name; format++)
220 if (format->tag == tag)
221 goto found;
222 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
223 found:;
224 if (tag)
225 {
226 if (depth && depth < tag_depth)
227 {
228 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
229 printf ("%s:tag `%08x' is incorrectly nested\n",
230 filename, tag);
231 }
232 depth = tag_depth;
233 tags[depth - 1] = tag;
234 }
235
236 print_prefix (filename, tag_depth, position);
237 printf ("%08x:%4u:%s", tag, length, format->name);
238 if (format->proc)
239 (*format->proc) (filename, tag, length);
240
241 printf ("\n");
242 if (flag_dump_contents && format->proc)
243 {
244 unsigned long actual_length = gcov_position () - base;
245
246 if (actual_length > length)
247 printf ("%s:record size mismatch %lu bytes overread\n",
248 filename, actual_length - length);
249 else if (length > actual_length)
250 printf ("%s:record size mismatch %lu bytes unread\n",
251 filename, length - actual_length);
252 }
253 gcov_sync (base, length);
254 if ((error = gcov_is_error ()))
255 {
256 printf (error < 0 ? "%s:counter overflow at %lu\n" :
257 "%s:read error at %lu\n", filename,
258 (long unsigned) gcov_position ());
259 break;
260 }
261 }
262 gcov_close ();
263 }
264
265 static void
266 tag_function (const char *filename ATTRIBUTE_UNUSED,
267 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
268 {
269 unsigned long pos = gcov_position ();
270
271 printf (" ident=%u", gcov_read_unsigned ());
272 printf (", checksum=0x%08x", gcov_read_unsigned ());
273
274 if (gcov_position () - pos < length)
275 {
276 const char *name;
277
278 name = gcov_read_string ();
279 printf (", `%s'", name ? name : "NULL");
280 name = gcov_read_string ();
281 printf (" %s", name ? name : "NULL");
282 printf (":%u", gcov_read_unsigned ());
283 }
284 }
285
286 static void
287 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
288 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
289 {
290 unsigned n_blocks = GCOV_TAG_BLOCKS_NUM (length);
291
292 printf (" %u blocks", n_blocks);
293
294 if (flag_dump_contents)
295 {
296 unsigned ix;
297
298 for (ix = 0; ix != n_blocks; ix++)
299 {
300 if (!(ix & 7))
301 {
302 printf ("\n");
303 print_prefix (filename, 0, gcov_position ());
304 printf ("\t\t%u", ix);
305 }
306 printf (" %04x", gcov_read_unsigned ());
307 }
308 }
309 }
310
311 static void
312 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
313 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
314 {
315 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
316
317 printf (" %u arcs", n_arcs);
318 if (flag_dump_contents)
319 {
320 unsigned ix;
321 unsigned blockno = gcov_read_unsigned ();
322
323 for (ix = 0; ix != n_arcs; ix++)
324 {
325 unsigned dst, flags;
326
327 if (!(ix & 3))
328 {
329 printf ("\n");
330 print_prefix (filename, 0, gcov_position ());
331 printf ("\tblock %u:", blockno);
332 }
333 dst = gcov_read_unsigned ();
334 flags = gcov_read_unsigned ();
335 printf (" %u:%04x", dst, flags);
336 }
337 }
338 }
339
340 static void
341 tag_lines (const char *filename ATTRIBUTE_UNUSED,
342 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
343 {
344 if (flag_dump_contents)
345 {
346 unsigned blockno = gcov_read_unsigned ();
347 char const *sep = NULL;
348
349 while (1)
350 {
351 gcov_position_t position = gcov_position ();
352 const char *source = NULL;
353 unsigned lineno = gcov_read_unsigned ();
354
355 if (!lineno)
356 {
357 source = gcov_read_string ();
358 if (!source)
359 break;
360 sep = NULL;
361 }
362
363 if (!sep)
364 {
365 printf ("\n");
366 print_prefix (filename, 0, position);
367 printf ("\tblock %u:", blockno);
368 sep = "";
369 }
370 if (lineno)
371 {
372 printf ("%s%u", sep, lineno);
373 sep = ", ";
374 }
375 else
376 {
377 printf ("%s`%s'", sep, source);
378 sep = ":";
379 }
380 }
381 }
382 }
383
384 static void
385 tag_counters (const char *filename ATTRIBUTE_UNUSED,
386 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
387 {
388 static const char *const counter_names[] = GCOV_COUNTER_NAMES;
389 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
390
391 printf (" %s %u counts",
392 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts);
393 if (flag_dump_contents)
394 {
395 unsigned ix;
396
397 for (ix = 0; ix != n_counts; ix++)
398 {
399 gcov_type count;
400
401 if (!(ix & 7))
402 {
403 printf ("\n");
404 print_prefix (filename, 0, gcov_position ());
405 printf ("\t\t%u", ix);
406 }
407
408 count = gcov_read_counter ();
409 printf (" ");
410 printf (HOST_WIDEST_INT_PRINT_DEC, count);
411 }
412 }
413 }
414
415 static void
416 tag_summary (const char *filename ATTRIBUTE_UNUSED,
417 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
418 {
419 struct gcov_summary summary;
420 unsigned ix;
421
422 gcov_read_summary (&summary);
423 printf (" checksum=0x%08x", summary.checksum);
424
425 for (ix = 0; ix != GCOV_COUNTERS; ix++)
426 {
427 printf ("\n");
428 print_prefix (filename, 0, 0);
429 printf ("\t\tcounts=%u, runs=%u",
430 summary.ctrs[ix].num, summary.ctrs[ix].runs);
431
432 printf (", sum_all=" HOST_WIDEST_INT_PRINT_DEC,
433 (HOST_WIDEST_INT)summary.ctrs[ix].sum_all);
434 printf (", run_max=" HOST_WIDEST_INT_PRINT_DEC,
435 (HOST_WIDEST_INT)summary.ctrs[ix].run_max);
436 printf (", sum_max=" HOST_WIDEST_INT_PRINT_DEC,
437 (HOST_WIDEST_INT)summary.ctrs[ix].sum_max);
438 }
439 }