OSDN Git Service

Change gcov file interface to single file at a time.
[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 int tag_function PARAMS ((const char *, unsigned, unsigned));
34 static int tag_blocks PARAMS ((const char *, unsigned, unsigned));
35 static int tag_arcs PARAMS ((const char *, unsigned, unsigned));
36 static int tag_lines PARAMS ((const char *, unsigned, unsigned));
37 static int tag_arc_counts PARAMS ((const char *, unsigned, unsigned));
38 static int 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   int (*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   unsigned magic, version;
144   unsigned tag, length;
145   
146   if (!gcov_open (filename, 1))
147     {
148       fprintf (stderr, "%s:cannot open\n", filename);
149       return;
150     }
151   
152   if (gcov_read_unsigned (&magic) || gcov_read_unsigned (&version))
153     {
154     read_error:;
155       printf ("%s:read error at %lu\n", filename, gcov_save_position ());
156       gcov_close ();
157       return;
158     }
159
160   /* magic */
161   {
162     const char *type = NULL;
163     char e[4], v[4], m[4];
164     unsigned expected = GCOV_VERSION;
165     unsigned ix;
166     int different = version != GCOV_VERSION;
167     
168     if (magic == GCOV_DATA_MAGIC)
169       type = "data";
170     else if (magic == GCOV_GRAPH_MAGIC)
171       type = "graph";
172     else
173       {
174         printf ("%s:not a gcov file\n", filename);
175         gcov_close ();
176         return;
177       }
178     for (ix = 4; ix--; expected >>= 8, version >>= 8, magic >>= 8)
179       {
180         e[ix] = expected;
181         v[ix] = version;
182         m[ix] = magic;
183       }
184     
185     printf ("%s:%s:magic `%.4s':version `%.4s'\n", filename, type, m, v);
186     if (different)
187       printf ("%s:warning:current version is `%.4s'\n", filename, e);
188   }
189
190   while (!gcov_read_unsigned (&tag) && !gcov_read_unsigned (&length))
191     {
192       tag_format_t const *format;
193       unsigned tag_depth;
194       long base, end;
195       
196       base = gcov_save_position ();
197       
198       if (!tag)
199         tag_depth = depth;
200       else
201         {
202           unsigned mask = GCOV_TAG_MASK (tag) >> 1;
203           
204           for (tag_depth = 4; mask; mask >>= 8)
205             {
206               if ((mask & 0xff) != 0xff)
207                 {
208                   printf ("%s:tag `%08x' is invalid\n", filename, tag);
209                   break;
210                 }
211               tag_depth--;
212             }
213         }
214       for (format = tag_table; format->name; format++)
215         if (format->tag == tag)
216           goto found;
217       format = &tag_table[1];
218     found:;
219       if (tag)
220         {
221           if (depth && depth < tag_depth)
222             {
223               if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
224                 printf ("%s:tag `%08x' is incorrectly nested\n",
225                         filename, tag);
226             }
227           depth = tag_depth;
228           tags[depth - 1] = tag;
229         }
230       
231       print_prefix (filename, tag_depth);
232       printf ("%08x:%4u:%s", tag, length, format->name);
233       if (format->proc)
234         if ((*format->proc) (filename, tag, length))
235           goto read_error;
236       printf ("\n");
237       end = gcov_save_position ();
238       gcov_resync (base, length);
239       if (format->proc && end != base + (long)length)
240         {
241           if (end > base + (long)length)
242             printf ("%s:record size mismatch %lu bytes overread\n",
243                     filename, (end - base) - length);
244           else
245             printf ("%s:record size mismatch %lu bytes unread\n",
246                     filename, length - (end - base));
247         }
248     }
249   if (!gcov_eof ())
250     goto read_error;
251   gcov_close ();
252 }
253
254 static int
255 tag_function (filename, tag, length)
256      const char *filename ATTRIBUTE_UNUSED;
257      unsigned tag ATTRIBUTE_UNUSED;
258      unsigned length ATTRIBUTE_UNUSED;
259 {
260   char *name = NULL;
261   unsigned checksum;
262   char *src = NULL;
263   unsigned lineno = 0;
264   unsigned long pos = gcov_save_position ();
265   
266   if (gcov_read_string (&name)
267       || gcov_read_unsigned (&checksum))
268     return 1;
269
270   if (gcov_save_position () - pos != length
271       && (gcov_read_string (&src)
272           || gcov_read_unsigned (&lineno)))
273     return 1;
274
275   printf (" `%s' checksum=0x%08x", name, checksum);
276   if (src)
277     printf (" %s:%u", src, lineno);
278   free (name);
279   free (src);
280   
281   return 0;
282 }
283
284 static int
285 tag_blocks (filename, tag, length)
286      const char *filename ATTRIBUTE_UNUSED;
287      unsigned tag ATTRIBUTE_UNUSED;
288      unsigned length ATTRIBUTE_UNUSED;
289 {
290   unsigned n_blocks = length / 4;
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           unsigned flags;
301           if (gcov_read_unsigned (&flags))
302             return 1;
303           if (!(ix & 7))
304             printf ("\n%s:\t\t%u", filename, ix);
305           printf (" %04x", flags);
306         }
307       
308     }
309   else
310     gcov_skip (n_blocks * 4);
311   
312   return 0;
313 }
314
315 static int
316 tag_arcs (filename, tag, length)
317      const char *filename ATTRIBUTE_UNUSED;
318      unsigned tag ATTRIBUTE_UNUSED;
319      unsigned length ATTRIBUTE_UNUSED;
320 {
321   unsigned n_arcs = (length - 4) / 8;
322
323   printf (" %u arcs", n_arcs);
324   if (flag_dump_contents)
325     {
326       unsigned ix;
327       unsigned blockno;
328
329       if (gcov_read_unsigned (&blockno))
330         return 1;
331
332       for (ix = 0; ix != n_arcs; ix++)
333         {
334           unsigned dst, flags;
335           
336           if (gcov_read_unsigned (&dst) || gcov_read_unsigned (&flags))
337             return 1;
338           if (!(ix & 3))
339             printf ("\n%s:\t\t%u:", filename, blockno);
340           printf (" %u:%04x", dst, flags);
341         }
342     }
343   else
344     gcov_skip (4 + n_arcs * 8);
345   
346   return 0;
347 }
348
349 static int
350 tag_lines (filename, tag, length)
351      const char *filename ATTRIBUTE_UNUSED;
352      unsigned tag ATTRIBUTE_UNUSED;
353      unsigned length ATTRIBUTE_UNUSED;
354 {
355   if (flag_dump_contents)
356     {
357       char *source = NULL;
358       unsigned blockno;
359       char const *sep = NULL;
360
361       if (gcov_read_unsigned (&blockno))
362         return 1;
363       
364       while (1)
365         {
366           unsigned lineno;
367           
368           if (gcov_read_unsigned (&lineno))
369             {
370               free (source);
371               return 1;
372             }
373           if (!lineno)
374             {
375               if (gcov_read_string (&source))
376                 return 1;
377               if (!source)
378                 break;
379               sep = NULL;
380             }
381           
382           if (!sep)
383             {
384               printf ("\n%s:\t\t%u:", filename, blockno);
385               sep = "";
386             }
387           if (lineno)
388             {
389               printf ("%s%u", sep, lineno);
390               sep = ", ";
391             }
392           else
393             {
394               printf ("%s`%s'", sep, source);
395               sep = ":";
396             }
397         }
398     }
399   else
400     gcov_skip (length);
401   
402   return 0;
403 }
404
405 static int
406 tag_arc_counts (filename, tag, length)
407      const char *filename ATTRIBUTE_UNUSED;
408      unsigned tag ATTRIBUTE_UNUSED;
409      unsigned length ATTRIBUTE_UNUSED;
410 {
411   unsigned n_counts = length / 8;
412   
413   printf (" %u counts", n_counts);
414   if (flag_dump_contents)
415     {
416       unsigned ix;
417
418       for (ix = 0; ix != n_counts; ix++)
419         {
420           gcov_type count;
421           
422           if (gcov_read_counter (&count))
423             return 1;
424           if (!(ix & 7))
425             printf ("\n%s:\t\t%u", filename, ix);
426           printf (" ");
427           printf (HOST_WIDEST_INT_PRINT_DEC, count);
428         }
429     }
430   else
431     gcov_skip (n_counts * 8);
432   
433   return 0;
434 }
435
436 static int
437 tag_summary (filename, tag, length)
438      const char *filename ATTRIBUTE_UNUSED;
439      unsigned tag ATTRIBUTE_UNUSED;
440      unsigned length ATTRIBUTE_UNUSED;
441 {
442   struct gcov_summary summary;
443
444   if (gcov_read_summary (&summary))
445     return 1;
446   printf (" checksum=0x%08x", summary.checksum);
447   
448   printf ("\n%s:\t\truns=%u, arcs=%u", filename,
449           summary.runs, summary.arcs);
450   printf ("\n%s:\t\tarc_sum=", filename);
451   printf (HOST_WIDEST_INT_PRINT_DEC, 
452           (HOST_WIDEST_INT)summary.arc_sum);
453   printf (", arc_max_one=");
454   printf (HOST_WIDEST_INT_PRINT_DEC, 
455           (HOST_WIDEST_INT)summary.arc_max_one);
456   printf ("\n%s:\t\tmax_sum=", filename);
457   printf (HOST_WIDEST_INT_PRINT_DEC, 
458           (HOST_WIDEST_INT)summary.arc_max_sum);
459   printf (", sum_max=");
460   printf (HOST_WIDEST_INT_PRINT_DEC, 
461           (HOST_WIDEST_INT)summary.arc_sum_max);
462   return 0;
463 }