OSDN Git Service

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