OSDN Git Service

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