OSDN Git Service

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