OSDN Git Service

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