In libobjc/: 2011-06-03 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / gcc / gcov-dump.c
blob200917053fbda419eae4ab926040d65d120d810c
1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Nathan Sidwell <nathan@codesourcery.com>
6 Gcov is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 Gcov is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Gcov; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "version.h"
25 #include "intl.h"
26 #include "diagnostic.h"
27 #include <getopt.h>
28 #define IN_GCOV (-1)
29 #include "gcov-io.h"
30 #include "gcov-io.c"
32 static void dump_file (const char *);
33 static void print_prefix (const char *, unsigned, gcov_position_t);
34 static void print_usage (void);
35 static void print_version (void);
36 static void tag_function (const char *, unsigned, unsigned);
37 static void tag_blocks (const char *, unsigned, unsigned);
38 static void tag_arcs (const char *, unsigned, unsigned);
39 static void tag_lines (const char *, unsigned, unsigned);
40 static void tag_counters (const char *, unsigned, unsigned);
41 static void tag_summary (const char *, unsigned, unsigned);
42 extern int main (int, char **);
44 typedef struct tag_format
46 unsigned tag;
47 char const *name;
48 void (*proc) (const char *, unsigned, unsigned);
49 } tag_format_t;
51 static int flag_dump_contents = 0;
52 static int flag_dump_positions = 0;
54 static const struct option options[] =
56 { "help", no_argument, NULL, 'h' },
57 { "version", no_argument, NULL, 'v' },
58 { "long", no_argument, NULL, 'l' },
59 { "positions", no_argument, NULL, 'o' },
60 { 0, 0, 0, 0 }
63 static const tag_format_t tag_table[] =
65 {0, "NOP", NULL},
66 {0, "UNKNOWN", NULL},
67 {0, "COUNTERS", tag_counters},
68 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function},
69 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks},
70 {GCOV_TAG_ARCS, "ARCS", tag_arcs},
71 {GCOV_TAG_LINES, "LINES", tag_lines},
72 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary},
73 {GCOV_TAG_PROGRAM_SUMMARY, "PROGRAM_SUMMARY", tag_summary},
74 {0, NULL, NULL}
77 int
78 main (int argc ATTRIBUTE_UNUSED, char **argv)
80 int opt;
81 const char *p;
83 p = argv[0] + strlen (argv[0]);
84 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
85 --p;
86 progname = p;
88 xmalloc_set_program_name (progname);
90 /* Unlock the stdio streams. */
91 unlock_std_streams ();
93 gcc_init_libintl ();
95 diagnostic_initialize (global_dc, 0);
97 while ((opt = getopt_long (argc, argv, "hlpv", options, NULL)) != -1)
99 switch (opt)
101 case 'h':
102 print_usage ();
103 break;
104 case 'v':
105 print_version ();
106 break;
107 case 'l':
108 flag_dump_contents = 1;
109 break;
110 case 'p':
111 flag_dump_positions = 1;
112 break;
113 default:
114 fprintf (stderr, "unknown flag `%c'\n", opt);
118 while (argv[optind])
119 dump_file (argv[optind++]);
120 return 0;
123 static void
124 print_usage (void)
126 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
127 printf ("Print coverage file contents\n");
128 printf (" -h, --help Print this help\n");
129 printf (" -v, --version Print version number\n");
130 printf (" -l, --long Dump record contents too\n");
131 printf (" -p, --positions Dump record positions\n");
134 static void
135 print_version (void)
137 printf ("gcov-dump %s%s\n", pkgversion_string, version_string);
138 printf ("Copyright (C) 2011 Free Software Foundation, Inc.\n");
139 printf ("This is free software; see the source for copying conditions.\n"
140 "There is NO warranty; not even for MERCHANTABILITY or \n"
141 "FITNESS FOR A PARTICULAR PURPOSE.\n\n");
144 static void
145 print_prefix (const char *filename, unsigned depth, gcov_position_t position)
147 static const char prefix[] = " ";
149 printf ("%s:", filename);
150 if (flag_dump_positions)
151 printf ("%lu:", (unsigned long) position);
152 printf ("%.*s", (int) depth, prefix);
155 static void
156 dump_file (const char *filename)
158 unsigned tags[4];
159 unsigned depth = 0;
161 if (!gcov_open (filename, 1))
163 fprintf (stderr, "%s:cannot open\n", filename);
164 return;
167 /* magic */
169 unsigned magic = gcov_read_unsigned ();
170 unsigned version;
171 const char *type = NULL;
172 int endianness = 0;
173 char m[4], v[4];
175 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
176 type = "data";
177 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
178 type = "note";
179 else
181 printf ("%s:not a gcov file\n", filename);
182 gcov_close ();
183 return;
185 version = gcov_read_unsigned ();
186 GCOV_UNSIGNED2STRING (v, version);
187 GCOV_UNSIGNED2STRING (m, magic);
189 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename, type,
190 m, v, endianness < 0 ? " (swapped endianness)" : "");
191 if (version != GCOV_VERSION)
193 char e[4];
195 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
196 printf ("%s:warning:current version is `%.4s'\n", filename, e);
200 /* stamp */
202 unsigned stamp = gcov_read_unsigned ();
204 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
207 while (1)
209 gcov_position_t base, position = gcov_position ();
210 unsigned tag, length;
211 tag_format_t const *format;
212 unsigned tag_depth;
213 int error;
214 unsigned mask;
216 tag = gcov_read_unsigned ();
217 if (!tag)
218 break;
219 length = gcov_read_unsigned ();
220 base = gcov_position ();
221 mask = GCOV_TAG_MASK (tag) >> 1;
222 for (tag_depth = 4; mask; mask >>= 8)
224 if ((mask & 0xff) != 0xff)
226 printf ("%s:tag `%08x' is invalid\n", filename, tag);
227 break;
229 tag_depth--;
231 for (format = tag_table; format->name; format++)
232 if (format->tag == tag)
233 goto found;
234 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
235 found:;
236 if (tag)
238 if (depth && depth < tag_depth)
240 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
241 printf ("%s:tag `%08x' is incorrectly nested\n",
242 filename, tag);
244 depth = tag_depth;
245 tags[depth - 1] = tag;
248 print_prefix (filename, tag_depth, position);
249 printf ("%08x:%4u:%s", tag, length, format->name);
250 if (format->proc)
251 (*format->proc) (filename, tag, length);
253 printf ("\n");
254 if (flag_dump_contents && format->proc)
256 unsigned long actual_length = gcov_position () - base;
258 if (actual_length > length)
259 printf ("%s:record size mismatch %lu bytes overread\n",
260 filename, actual_length - length);
261 else if (length > actual_length)
262 printf ("%s:record size mismatch %lu bytes unread\n",
263 filename, length - actual_length);
265 gcov_sync (base, length);
266 if ((error = gcov_is_error ()))
268 printf (error < 0 ? "%s:counter overflow at %lu\n" :
269 "%s:read error at %lu\n", filename,
270 (long unsigned) gcov_position ());
271 break;
274 gcov_close ();
277 static void
278 tag_function (const char *filename ATTRIBUTE_UNUSED,
279 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
281 unsigned long pos = gcov_position ();
283 printf (" ident=%u", gcov_read_unsigned ());
284 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
285 printf (", cfg_checksum_checksum=0x%08x", gcov_read_unsigned ());
287 if (gcov_position () - pos < length)
289 const char *name;
291 name = gcov_read_string ();
292 printf (", `%s'", name ? name : "NULL");
293 name = gcov_read_string ();
294 printf (" %s", name ? name : "NULL");
295 printf (":%u", gcov_read_unsigned ());
299 static void
300 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
301 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
303 unsigned n_blocks = GCOV_TAG_BLOCKS_NUM (length);
305 printf (" %u blocks", n_blocks);
307 if (flag_dump_contents)
309 unsigned ix;
311 for (ix = 0; ix != n_blocks; ix++)
313 if (!(ix & 7))
315 printf ("\n");
316 print_prefix (filename, 0, gcov_position ());
317 printf ("\t\t%u", ix);
319 printf (" %04x", gcov_read_unsigned ());
324 static void
325 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
326 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
328 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
330 printf (" %u arcs", n_arcs);
331 if (flag_dump_contents)
333 unsigned ix;
334 unsigned blockno = gcov_read_unsigned ();
336 for (ix = 0; ix != n_arcs; ix++)
338 unsigned dst, flags;
340 if (!(ix & 3))
342 printf ("\n");
343 print_prefix (filename, 0, gcov_position ());
344 printf ("\tblock %u:", blockno);
346 dst = gcov_read_unsigned ();
347 flags = gcov_read_unsigned ();
348 printf (" %u:%04x", dst, flags);
353 static void
354 tag_lines (const char *filename ATTRIBUTE_UNUSED,
355 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
357 if (flag_dump_contents)
359 unsigned blockno = gcov_read_unsigned ();
360 char const *sep = NULL;
362 while (1)
364 gcov_position_t position = gcov_position ();
365 const char *source = NULL;
366 unsigned lineno = gcov_read_unsigned ();
368 if (!lineno)
370 source = gcov_read_string ();
371 if (!source)
372 break;
373 sep = NULL;
376 if (!sep)
378 printf ("\n");
379 print_prefix (filename, 0, position);
380 printf ("\tblock %u:", blockno);
381 sep = "";
383 if (lineno)
385 printf ("%s%u", sep, lineno);
386 sep = ", ";
388 else
390 printf ("%s`%s'", sep, source);
391 sep = ":";
397 static void
398 tag_counters (const char *filename ATTRIBUTE_UNUSED,
399 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
401 static const char *const counter_names[] = GCOV_COUNTER_NAMES;
402 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
404 printf (" %s %u counts",
405 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts);
406 if (flag_dump_contents)
408 unsigned ix;
410 for (ix = 0; ix != n_counts; ix++)
412 gcov_type count;
414 if (!(ix & 7))
416 printf ("\n");
417 print_prefix (filename, 0, gcov_position ());
418 printf ("\t\t%u", ix);
421 count = gcov_read_counter ();
422 printf (" ");
423 printf (HOST_WIDEST_INT_PRINT_DEC, count);
428 static void
429 tag_summary (const char *filename ATTRIBUTE_UNUSED,
430 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
432 struct gcov_summary summary;
433 unsigned ix;
435 gcov_read_summary (&summary);
436 printf (" checksum=0x%08x", summary.checksum);
438 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
440 printf ("\n");
441 print_prefix (filename, 0, 0);
442 printf ("\t\tcounts=%u, runs=%u",
443 summary.ctrs[ix].num, summary.ctrs[ix].runs);
445 printf (", sum_all=" HOST_WIDEST_INT_PRINT_DEC,
446 (HOST_WIDEST_INT)summary.ctrs[ix].sum_all);
447 printf (", run_max=" HOST_WIDEST_INT_PRINT_DEC,
448 (HOST_WIDEST_INT)summary.ctrs[ix].run_max);
449 printf (", sum_max=" HOST_WIDEST_INT_PRINT_DEC,
450 (HOST_WIDEST_INT)summary.ctrs[ix].sum_max);