OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / gcov-dump.c
1 /* Dump a gcov file, for debugging use.
2    Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
3    2012 Free Software Foundation, Inc.
4    Contributed by Nathan Sidwell <nathan@codesourcery.com>
5
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.
10
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.
15
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/>.  */
19
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"
31
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 **);
43
44 typedef struct tag_format
45 {
46   unsigned tag;
47   char const *name;
48   void (*proc) (const char *, unsigned, unsigned);
49 } tag_format_t;
50
51 static int flag_dump_contents = 0;
52 static int flag_dump_positions = 0;
53
54 static const struct option options[] =
55 {
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 }
61 };
62
63 static const tag_format_t tag_table[] =
64 {
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}
75 };
76
77 int
78 main (int argc ATTRIBUTE_UNUSED, char **argv)
79 {
80   int opt;
81   const char *p;
82
83   p = argv[0] + strlen (argv[0]);
84   while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
85     --p;
86   progname = p;
87
88   xmalloc_set_program_name (progname);
89
90   /* Unlock the stdio streams.  */
91   unlock_std_streams ();
92
93   gcc_init_libintl ();
94
95   diagnostic_initialize (global_dc, 0);
96
97   while ((opt = getopt_long (argc, argv, "hlpv", options, NULL)) != -1)
98     {
99       switch (opt)
100         {
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);
115         }
116     }
117
118   while (argv[optind])
119     dump_file (argv[optind++]);
120   return 0;
121 }
122
123 static void
124 print_usage (void)
125 {
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");
132 }
133
134 static void
135 print_version (void)
136 {
137   printf ("gcov-dump %s%s\n", pkgversion_string, version_string);
138   printf ("Copyright (C) 2012 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");
142 }
143
144 static void
145 print_prefix (const char *filename, unsigned depth, gcov_position_t position)
146 {
147   static const char prefix[] = "    ";
148
149   printf ("%s:", filename);
150   if (flag_dump_positions)
151     printf ("%lu:", (unsigned long) position);
152   printf ("%.*s", (int) depth, prefix);
153 }
154
155 static void
156 dump_file (const char *filename)
157 {
158   unsigned tags[4];
159   unsigned depth = 0;
160
161   if (!gcov_open (filename, 1))
162     {
163       fprintf (stderr, "%s:cannot open\n", filename);
164       return;
165     }
166
167   /* magic */
168   {
169     unsigned magic = gcov_read_unsigned ();
170     unsigned version;
171     const char *type = NULL;
172     int endianness = 0;
173     char m[4], v[4];
174
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
180       {
181         printf ("%s:not a gcov file\n", filename);
182         gcov_close ();
183         return;
184       }
185     version = gcov_read_unsigned ();
186     GCOV_UNSIGNED2STRING (v, version);
187     GCOV_UNSIGNED2STRING (m, magic);
188
189     printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename, type,
190             m, v, endianness < 0 ? " (swapped endianness)" : "");
191     if (version != GCOV_VERSION)
192       {
193         char e[4];
194
195         GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
196         printf ("%s:warning:current version is `%.4s'\n", filename, e);
197       }
198   }
199
200   /* stamp */
201   {
202     unsigned stamp = gcov_read_unsigned ();
203
204     printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
205   }
206
207   while (1)
208     {
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;
215
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)
223         {
224           if ((mask & 0xff) != 0xff)
225             {
226               printf ("%s:tag `%08x' is invalid\n", filename, tag);
227               break;
228             }
229           tag_depth--;
230         }
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)
237         {
238           if (depth && depth < tag_depth)
239             {
240               if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
241                 printf ("%s:tag `%08x' is incorrectly nested\n",
242                         filename, tag);
243             }
244           depth = tag_depth;
245           tags[depth - 1] = tag;
246         }
247
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);
252
253       printf ("\n");
254       if (flag_dump_contents && format->proc)
255         {
256           unsigned long actual_length = gcov_position () - base;
257
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);
264         }
265       gcov_sync (base, length);
266       if ((error = gcov_is_error ()))
267         {
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;
272         }
273     }
274   gcov_close ();
275 }
276
277 static void
278 tag_function (const char *filename ATTRIBUTE_UNUSED,
279               unsigned tag ATTRIBUTE_UNUSED, unsigned length)
280 {
281   unsigned long pos = gcov_position ();
282
283   if (!length)
284     printf (" placeholder");
285   else
286     {
287       printf (" ident=%u", gcov_read_unsigned ());
288       printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
289       printf (", cfg_checksum=0x%08x", gcov_read_unsigned ());
290
291       if (gcov_position () - pos < length)
292         {
293           const char *name;
294           
295           name = gcov_read_string ();
296           printf (", `%s'", name ? name : "NULL");
297           name = gcov_read_string ();
298           printf (" %s", name ? name : "NULL");
299           printf (":%u", gcov_read_unsigned ());
300         }
301     }
302 }
303
304 static void
305 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
306             unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
307 {
308   unsigned n_blocks = GCOV_TAG_BLOCKS_NUM (length);
309
310   printf (" %u blocks", n_blocks);
311
312   if (flag_dump_contents)
313     {
314       unsigned ix;
315
316       for (ix = 0; ix != n_blocks; ix++)
317         {
318           if (!(ix & 7))
319             {
320               printf ("\n");
321               print_prefix (filename, 0, gcov_position ());
322               printf ("\t\t%u", ix);
323             }
324           printf (" %04x", gcov_read_unsigned ());
325         }
326     }
327 }
328
329 static void
330 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
331           unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
332 {
333   unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
334
335   printf (" %u arcs", n_arcs);
336   if (flag_dump_contents)
337     {
338       unsigned ix;
339       unsigned blockno = gcov_read_unsigned ();
340
341       for (ix = 0; ix != n_arcs; ix++)
342         {
343           unsigned dst, flags;
344
345           if (!(ix & 3))
346             {
347               printf ("\n");
348               print_prefix (filename, 0, gcov_position ());
349               printf ("\tblock %u:", blockno);
350             }
351           dst = gcov_read_unsigned ();
352           flags = gcov_read_unsigned ();
353           printf (" %u:%04x", dst, flags);
354           if (flags)
355             {
356               char c = '(';
357               
358               if (flags & GCOV_ARC_ON_TREE)
359                 printf ("%ctree", c), c = ',';
360               if (flags & GCOV_ARC_FAKE)
361                 printf ("%cfake", c), c = ',';
362               if (flags & GCOV_ARC_FALLTHROUGH)
363                 printf ("%cfall", c), c = ',';
364               printf (")");
365             }
366         }
367     }
368 }
369
370 static void
371 tag_lines (const char *filename ATTRIBUTE_UNUSED,
372            unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
373 {
374   if (flag_dump_contents)
375     {
376       unsigned blockno = gcov_read_unsigned ();
377       char const *sep = NULL;
378
379       while (1)
380         {
381           gcov_position_t position = gcov_position ();
382           const char *source = NULL;
383           unsigned lineno = gcov_read_unsigned ();
384
385           if (!lineno)
386             {
387               source = gcov_read_string ();
388               if (!source)
389                 break;
390               sep = NULL;
391             }
392
393           if (!sep)
394             {
395               printf ("\n");
396               print_prefix (filename, 0, position);
397               printf ("\tblock %u:", blockno);
398               sep = "";
399             }
400           if (lineno)
401             {
402               printf ("%s%u", sep, lineno);
403               sep = ", ";
404             }
405           else
406             {
407               printf ("%s`%s'", sep, source);
408               sep = ":";
409             }
410         }
411     }
412 }
413
414 static void
415 tag_counters (const char *filename ATTRIBUTE_UNUSED,
416               unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
417 {
418   static const char *const counter_names[] = GCOV_COUNTER_NAMES;
419   unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
420
421   printf (" %s %u counts",
422           counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts);
423   if (flag_dump_contents)
424     {
425       unsigned ix;
426
427       for (ix = 0; ix != n_counts; ix++)
428         {
429           gcov_type count;
430
431           if (!(ix & 7))
432             {
433               printf ("\n");
434               print_prefix (filename, 0, gcov_position ());
435               printf ("\t\t%u", ix);
436             }
437
438           count = gcov_read_counter ();
439           printf (" ");
440           printf (HOST_WIDEST_INT_PRINT_DEC, count);
441         }
442     }
443 }
444
445 static void
446 tag_summary (const char *filename ATTRIBUTE_UNUSED,
447              unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
448 {
449   struct gcov_summary summary;
450   unsigned ix;
451
452   gcov_read_summary (&summary);
453   printf (" checksum=0x%08x", summary.checksum);
454
455   for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
456     {
457       printf ("\n");
458       print_prefix (filename, 0, 0);
459       printf ("\t\tcounts=%u, runs=%u",
460               summary.ctrs[ix].num, summary.ctrs[ix].runs);
461
462       printf (", sum_all=" HOST_WIDEST_INT_PRINT_DEC,
463               (HOST_WIDEST_INT)summary.ctrs[ix].sum_all);
464       printf (", run_max=" HOST_WIDEST_INT_PRINT_DEC,
465               (HOST_WIDEST_INT)summary.ctrs[ix].run_max);
466       printf (", sum_max=" HOST_WIDEST_INT_PRINT_DEC,
467               (HOST_WIDEST_INT)summary.ctrs[ix].sum_max);
468     }
469 }