OSDN Git Service

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