This patch suppresses the messages printed when the primary module is not found.
[official-gcc.git] / gcc-4_7 / gcc / gcov-dump.c
blob12a79b49d425cb567b1d1971ff3c95f2048c982f
1 /* Dump a gcov file, for debugging use.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
3 2012 Free Software Foundation, Inc.
4 Contributed by Nathan Sidwell <nathan@codesourcery.com>
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.
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.
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/>. */
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"
32 static void dump_file (const char *);
33 static int dump_aux_modules (const char *);
34 static void print_prefix (const char *, unsigned, gcov_position_t);
35 static void print_usage (void);
36 static void print_version (void);
37 static void tag_function (const char *, unsigned, unsigned);
38 static void tag_blocks (const char *, unsigned, unsigned);
39 static void tag_arcs (const char *, unsigned, unsigned);
40 static void tag_lines (const char *, unsigned, unsigned);
41 static void tag_counters (const char *, unsigned, unsigned);
42 static void tag_summary (const char *, unsigned, unsigned);
43 static void tag_module_info (const char *, unsigned, unsigned);
44 static void tag_pmu_load_latency_info (const char *, unsigned, unsigned);
45 static void tag_pmu_branch_mispredict_info (const char *, unsigned, unsigned);
46 static void tag_pmu_string_table_entry (const char*, unsigned, unsigned);
47 static void tag_pmu_tool_header (const char *, unsigned, unsigned);
49 extern int main (int, char **);
51 typedef struct tag_format
53 unsigned tag;
54 char const *name;
55 void (*proc) (const char *, unsigned, unsigned);
56 } tag_format_t;
58 static int flag_dump_contents = 0;
59 static int flag_dump_positions = 0;
60 static int flag_dump_aux_modules_only = 0;
62 static const struct option options[] =
64 { "help", no_argument, NULL, 'h' },
65 { "version", no_argument, NULL, 'v' },
66 { "long", no_argument, NULL, 'l' },
67 { "positions", no_argument, NULL, 'o' },
68 { 0, 0, 0, 0 }
71 static const tag_format_t tag_table[] =
73 {0, "NOP", NULL},
74 {0, "UNKNOWN", NULL},
75 {0, "COUNTERS", tag_counters},
76 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function},
77 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks},
78 {GCOV_TAG_ARCS, "ARCS", tag_arcs},
79 {GCOV_TAG_LINES, "LINES", tag_lines},
80 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary},
81 {GCOV_TAG_PROGRAM_SUMMARY, "PROGRAM_SUMMARY", tag_summary},
82 {GCOV_TAG_MODULE_INFO, "MODULE INFO", tag_module_info},
83 {GCOV_TAG_PMU_LOAD_LATENCY_INFO, "PMU_LOAD_LATENCY_INFO",
84 tag_pmu_load_latency_info},
85 {GCOV_TAG_PMU_BRANCH_MISPREDICT_INFO, "PMU_BRANCH_MISPREDICT_INFO",
86 tag_pmu_branch_mispredict_info},
87 {GCOV_TAG_PMU_TOOL_HEADER, "PMU_TOOL_HEADER", tag_pmu_tool_header},
88 {GCOV_TAG_PMU_STRING_TABLE_ENTRY, "PMU_STRING_TABLE_ENTRY",
89 tag_pmu_string_table_entry},
90 {0, NULL, NULL}
93 int
94 main (int argc ATTRIBUTE_UNUSED, char **argv)
96 int opt;
97 const char *p;
99 p = argv[0] + strlen (argv[0]);
100 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
101 --p;
102 progname = p;
104 xmalloc_set_program_name (progname);
106 /* Unlock the stdio streams. */
107 unlock_std_streams ();
109 gcc_init_libintl ();
111 diagnostic_initialize (global_dc, 0);
113 while ((opt = getopt_long (argc, argv, "hlpvx", options, NULL)) != -1)
115 switch (opt)
117 case 'h':
118 print_usage ();
119 break;
120 case 'v':
121 print_version ();
122 break;
123 case 'l':
124 flag_dump_contents = 1;
125 break;
126 case 'p':
127 flag_dump_positions = 1;
128 break;
129 case 'x':
130 flag_dump_aux_modules_only = 1;
131 break;
132 default:
133 fprintf (stderr, "unknown flag `%c'\n", opt);
137 if (flag_dump_aux_modules_only)
139 while (argv[optind])
140 if (dump_aux_modules (argv[optind++]))
141 return 1;
143 else
144 while (argv[optind])
145 dump_file (argv[optind++]);
146 return 0;
149 static void
150 print_usage (void)
152 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
153 printf ("Print coverage file contents\n");
154 printf (" -h, --help Print this help\n");
155 printf (" -v, --version Print version number\n");
156 printf (" -l, --long Dump record contents too\n");
157 printf (" -p, --positions Dump record positions\n");
158 printf (" -x Dump names of auxiliary modules only\n");
161 static void
162 print_version (void)
164 printf ("gcov-dump %s%s\n", pkgversion_string, version_string);
165 printf ("Copyright (C) 2012 Free Software Foundation, Inc.\n");
166 printf ("This is free software; see the source for copying conditions.\n"
167 "There is NO warranty; not even for MERCHANTABILITY or \n"
168 "FITNESS FOR A PARTICULAR PURPOSE.\n\n");
171 static void
172 print_prefix (const char *filename, unsigned depth, gcov_position_t position)
174 static const char prefix[] = " ";
176 printf ("%s:", filename);
177 if (flag_dump_positions)
178 printf ("%lu:", (unsigned long) position);
179 printf ("%.*s", (int) depth, prefix);
182 /* Dump auxiliary module information for gcda file with
183 name FILENAME. */
185 static int
186 dump_aux_modules (const char *filename)
188 if (!gcov_open (filename, 1))
190 fprintf (stderr, "%s:cannot open\n", filename);
191 return 1;
194 /* magic */
195 gcov_read_unsigned ();
196 /* version */
197 gcov_read_unsigned ();
198 /* stamp */
199 gcov_read_unsigned ();
201 while (1)
203 gcov_position_t base;
204 unsigned tag, length;
205 int error;
207 tag = gcov_read_unsigned ();
208 if (!tag)
209 break;
210 length = gcov_read_unsigned ();
211 base = gcov_position ();
212 if (tag == GCOV_TAG_MODULE_INFO)
213 tag_module_info (filename, tag, length);
214 gcov_sync (base, length);
215 if ((error = gcov_is_error ()))
217 printf (error < 0 ? "%s:counter overflow at %lu\n" :
218 "%s:read error at %lu\n", filename,
219 (long unsigned) gcov_position ());
220 return 1;
224 gcov_close ();
225 return 0;
228 static void
229 dump_file (const char *filename)
231 unsigned tags[4];
232 unsigned depth = 0;
234 if (!gcov_open (filename, 1))
236 fprintf (stderr, "%s:cannot open\n", filename);
237 return;
240 /* magic */
242 unsigned magic = gcov_read_unsigned ();
243 unsigned version;
244 const char *type = NULL;
245 int endianness = 0;
246 char m[4], v[4];
248 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
249 type = "data";
250 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
251 type = "note";
252 else
254 printf ("%s:not a gcov file\n", filename);
255 gcov_close ();
256 return;
258 version = gcov_read_unsigned ();
259 GCOV_UNSIGNED2STRING (v, version);
260 GCOV_UNSIGNED2STRING (m, magic);
262 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename, type,
263 m, v, endianness < 0 ? " (swapped endianness)" : "");
264 if (version != GCOV_VERSION)
266 char e[4];
268 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
269 printf ("%s:warning:current version is `%.4s'\n", filename, e);
273 /* stamp */
275 unsigned stamp = gcov_read_unsigned ();
277 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
280 while (1)
282 gcov_position_t base, position = gcov_position ();
283 unsigned tag, length;
284 tag_format_t const *format;
285 unsigned tag_depth;
286 int error;
287 unsigned mask;
289 tag = gcov_read_unsigned ();
290 if (!tag)
291 break;
292 length = gcov_read_unsigned ();
293 base = gcov_position ();
294 mask = GCOV_TAG_MASK (tag) >> 1;
295 for (tag_depth = 4; mask; mask >>= 8)
297 if ((mask & 0xff) != 0xff)
299 printf ("%s:tag `%08x' is invalid\n", filename, tag);
300 break;
302 tag_depth--;
304 for (format = tag_table; format->name; format++)
305 if (format->tag == tag)
306 goto found;
307 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
308 found:;
309 if (tag)
311 if (depth && depth < tag_depth)
313 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
314 printf ("%s:tag `%08x' is incorrectly nested\n",
315 filename, tag);
317 depth = tag_depth;
318 tags[depth - 1] = tag;
321 print_prefix (filename, tag_depth, position);
322 printf ("%08x:%4u:%s", tag, length, format->name);
323 if (format->proc)
324 (*format->proc) (filename, tag, length);
326 printf ("\n");
327 if (flag_dump_contents && format->proc)
329 unsigned long actual_length = gcov_position () - base;
331 if (actual_length > length)
332 printf ("%s:record size mismatch %lu bytes overread\n",
333 filename, actual_length - length);
334 else if (length > actual_length)
335 printf ("%s:record size mismatch %lu bytes unread\n",
336 filename, length - actual_length);
338 gcov_sync (base, length);
339 if ((error = gcov_is_error ()))
341 printf (error < 0 ? "%s:counter overflow at %lu\n" :
342 "%s:read error at %lu\n", filename,
343 (long unsigned) gcov_position ());
344 break;
347 gcov_close ();
350 static void
351 tag_function (const char *filename ATTRIBUTE_UNUSED,
352 unsigned tag ATTRIBUTE_UNUSED, unsigned length)
354 unsigned long pos = gcov_position ();
356 if (!length)
357 printf (" placeholder");
358 else
360 printf (" ident=%u", gcov_read_unsigned ());
361 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ());
362 printf (", cfg_checksum=0x%08x", gcov_read_unsigned ());
364 if (gcov_position () - pos < length)
366 const char *name;
368 name = gcov_read_string ();
369 printf (", `%s'", name ? name : "NULL");
370 name = gcov_read_string ();
371 printf (" %s", name ? name : "NULL");
372 printf (":%u", gcov_read_unsigned ());
377 static void
378 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
379 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
381 unsigned n_blocks = GCOV_TAG_BLOCKS_NUM (length);
383 printf (" %u blocks", n_blocks);
385 if (flag_dump_contents)
387 unsigned ix;
389 for (ix = 0; ix != n_blocks; ix++)
391 if (!(ix & 7))
393 printf ("\n");
394 print_prefix (filename, 0, gcov_position ());
395 printf ("\t\t%u", ix);
397 printf (" %04x", gcov_read_unsigned ());
402 static void
403 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
404 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
406 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
408 printf (" %u arcs", n_arcs);
409 if (flag_dump_contents)
411 unsigned ix;
412 unsigned blockno = gcov_read_unsigned ();
414 for (ix = 0; ix != n_arcs; ix++)
416 unsigned dst, flags;
418 if (!(ix & 3))
420 printf ("\n");
421 print_prefix (filename, 0, gcov_position ());
422 printf ("\tblock %u:", blockno);
424 dst = gcov_read_unsigned ();
425 flags = gcov_read_unsigned ();
426 printf (" %u:%04x", dst, flags);
427 if (flags)
429 char c = '(';
431 if (flags & GCOV_ARC_ON_TREE)
432 printf ("%ctree", c), c = ',';
433 if (flags & GCOV_ARC_FAKE)
434 printf ("%cfake", c), c = ',';
435 if (flags & GCOV_ARC_FALLTHROUGH)
436 printf ("%cfall", c), c = ',';
437 printf (")");
443 static void
444 tag_lines (const char *filename ATTRIBUTE_UNUSED,
445 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
447 if (flag_dump_contents)
449 unsigned blockno = gcov_read_unsigned ();
450 char const *sep = NULL;
452 while (1)
454 gcov_position_t position = gcov_position ();
455 const char *source = NULL;
456 unsigned lineno = gcov_read_unsigned ();
458 if (!lineno)
460 source = gcov_read_string ();
461 if (!source)
462 break;
463 sep = NULL;
466 if (!sep)
468 printf ("\n");
469 print_prefix (filename, 0, position);
470 printf ("\tblock %u:", blockno);
471 sep = "";
473 if (lineno)
475 printf ("%s%u", sep, lineno);
476 sep = ", ";
478 else
480 printf ("%s`%s'", sep, source);
481 sep = ":";
487 static void
488 tag_counters (const char *filename ATTRIBUTE_UNUSED,
489 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
491 static const char *const counter_names[] = GCOV_COUNTER_NAMES;
492 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
494 printf (" %s %u counts",
495 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts);
496 if (flag_dump_contents)
498 unsigned ix;
500 for (ix = 0; ix != n_counts; ix++)
502 gcov_type count;
504 if (!(ix & 7))
506 printf ("\n");
507 print_prefix (filename, 0, gcov_position ());
508 printf ("\t\t%u", ix);
511 count = gcov_read_counter ();
512 printf (" ");
513 printf (HOST_WIDEST_INT_PRINT_DEC, count);
518 static void
519 tag_summary (const char *filename ATTRIBUTE_UNUSED,
520 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
522 struct gcov_summary summary;
523 unsigned ix, h_ix;
524 gcov_bucket_type *histo_bucket;
526 gcov_read_summary (&summary);
527 printf (" checksum=0x%08x", summary.checksum);
529 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
531 printf ("\n");
532 print_prefix (filename, 0, 0);
533 printf ("\t\tcounts=%u, runs=%u",
534 summary.ctrs[ix].num,
535 summary.ctrs[ix].runs);
537 printf (", sum_all=" HOST_WIDEST_INT_PRINT_DEC,
538 (HOST_WIDEST_INT)summary.ctrs[ix].sum_all);
539 printf (", run_max=" HOST_WIDEST_INT_PRINT_DEC,
540 (HOST_WIDEST_INT)summary.ctrs[ix].run_max);
541 printf (", sum_max=" HOST_WIDEST_INT_PRINT_DEC,
542 (HOST_WIDEST_INT)summary.ctrs[ix].sum_max);
543 if (ix != GCOV_COUNTER_ARCS)
544 continue;
545 printf ("\n");
546 print_prefix (filename, 0, 0);
547 printf ("\t\tcounter histogram:");
548 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
550 histo_bucket = &summary.ctrs[ix].histogram[h_ix];
551 if (!histo_bucket->num_counters)
552 continue;
553 printf ("\n");
554 print_prefix (filename, 0, 0);
555 printf ("\t\t%d: num counts=%u, min counter="
556 HOST_WIDEST_INT_PRINT_DEC ", cum_counter="
557 HOST_WIDEST_INT_PRINT_DEC,
558 h_ix, histo_bucket->num_counters,
559 (HOST_WIDEST_INT)histo_bucket->min_value,
560 (HOST_WIDEST_INT)histo_bucket->cum_value);
565 static void
566 tag_module_info (const char *filename ATTRIBUTE_UNUSED,
567 unsigned tag ATTRIBUTE_UNUSED, unsigned length)
569 struct gcov_module_info* mod_info;
571 mod_info = (struct gcov_module_info *)
572 alloca ((length + 2) * sizeof (gcov_unsigned_t));
573 gcov_read_module_info (mod_info, length);
574 if (flag_dump_aux_modules_only)
576 if (!mod_info->is_primary)
577 printf ("%s\n", mod_info->source_filename);
579 else
581 const char *primary_suffix =
582 mod_info->is_primary ? "primary" : "auxiliary";
583 const char *export_suffix = "";
584 const char *include_all_suffix = "";
586 if (mod_info->is_primary)
588 if (MODULE_EXPORTED_FLAG (mod_info))
589 export_suffix = ",exported";
590 if (MODULE_INCLUDE_ALL_AUX_FLAG (mod_info))
591 include_all_suffix =",include_all";
593 printf (": %s (ident=%u) [%s%s%s]", mod_info->source_filename,
594 mod_info->ident, primary_suffix, export_suffix,
595 include_all_suffix);
599 /* Read gcov tag GCOV_TAG_PMU_LOAD_LATENCY_INFO from the gcda file and
600 print the contents in a human readable form. */
602 static void
603 tag_pmu_load_latency_info (const char *filename ATTRIBUTE_UNUSED,
604 unsigned tag ATTRIBUTE_UNUSED, unsigned length)
606 gcov_pmu_ll_info_t ll_info;
607 gcov_read_pmu_load_latency_info (&ll_info, length);
608 print_load_latency_line (stdout, &ll_info, no_newline);
611 /* Read gcov tag GCOV_TAG_PMU_BRANCH_MISPREDICT_INFO from the gcda
612 file and print the contents in a human readable form. */
614 static void
615 tag_pmu_branch_mispredict_info (const char *filename ATTRIBUTE_UNUSED,
616 unsigned tag ATTRIBUTE_UNUSED, unsigned length)
618 gcov_pmu_brm_info_t brm_info;
619 gcov_read_pmu_branch_mispredict_info (&brm_info, length);
620 print_branch_mispredict_line (stdout, &brm_info, no_newline);
623 static void
624 tag_pmu_string_table_entry (const char *filename ATTRIBUTE_UNUSED,
625 unsigned tag ATTRIBUTE_UNUSED, unsigned length)
627 gcov_pmu_st_entry_t st_entry;
628 gcov_read_pmu_string_table_entry(&st_entry, length);
629 print_pmu_string_table_entry(stdout, &st_entry, no_newline);
630 free(st_entry.str);
633 /* Read gcov tag GCOV_TAG_PMU_TOOL_HEADER from the gcda file and print
634 the contents in a human readable form. */
636 static void
637 tag_pmu_tool_header (const char *filename ATTRIBUTE_UNUSED,
638 unsigned tag ATTRIBUTE_UNUSED, unsigned length)
640 gcov_pmu_tool_header_t tool_header;
641 gcov_read_pmu_tool_header (&tool_header, length);
642 print_pmu_tool_header (stdout, &tool_header, no_newline);
643 destroy_pmu_tool_header (&tool_header);