1 /* Dump a gcov file, for debugging use. 2 Copyright (C) 2002-2015 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 3, 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 COPYING3. If not see 17 <http://www.gnu.org/licenses/>. */ 18 19 #include "config.h" 20 #include "system.h" 21 #include "coretypes.h" 22 #include "tm.h" 23 #include "version.h" 24 #include "intl.h" 25 #include "diagnostic.h" 26 #include <getopt.h> 27 #define IN_GCOV (-1) 28 #include "gcov-io.h" 29 #include "gcov-io.c" 30 31 static void dump_gcov_file (const char *); 32 static void print_prefix (const char *, unsigned, gcov_position_t); 33 static void print_usage (void); 34 static void print_version (void); 35 static void tag_function (const char *, unsigned, unsigned); 36 static void tag_blocks (const char *, unsigned, unsigned); 37 static void tag_arcs (const char *, unsigned, unsigned); 38 static void tag_lines (const char *, unsigned, unsigned); 39 static void tag_counters (const char *, unsigned, unsigned); 40 static void tag_summary (const char *, unsigned, unsigned); 41 static void dump_working_sets (const char *filename ATTRIBUTE_UNUSED, 42 const struct gcov_ctr_summary *summary); 43 extern int main (int, char **); 44 45 typedef struct tag_format 46 { 47 unsigned tag; 48 char const *name; 49 void (*proc) (const char *, unsigned, unsigned); 50 } tag_format_t; 51 52 static int flag_dump_contents = 0; 53 static int flag_dump_positions = 0; 54 static int flag_dump_working_sets = 0; 55 56 static const struct option options[] = 57 { 58 { "help", no_argument, NULL, 'h' }, 59 { "version", no_argument, NULL, 'v' }, 60 { "long", no_argument, NULL, 'l' }, 61 { "positions", no_argument, NULL, 'o' }, 62 { "working-sets", no_argument, NULL, 'w' }, 63 { 0, 0, 0, 0 } 64 }; 65 66 static const tag_format_t tag_table[] = 67 { 68 {0, "NOP", NULL}, 69 {0, "UNKNOWN", NULL}, 70 {0, "COUNTERS", tag_counters}, 71 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function}, 72 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks}, 73 {GCOV_TAG_ARCS, "ARCS", tag_arcs}, 74 {GCOV_TAG_LINES, "LINES", tag_lines}, 75 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary}, 76 {GCOV_TAG_PROGRAM_SUMMARY, "PROGRAM_SUMMARY", tag_summary}, 77 {0, NULL, NULL} 78 }; 79 80 int 81 main (int argc ATTRIBUTE_UNUSED, char **argv) 82 { 83 int opt; 84 const char *p; 85 86 p = argv[0] + strlen (argv[0]); 87 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1])) 88 --p; 89 progname = p; 90 91 xmalloc_set_program_name (progname); 92 93 /* Unlock the stdio streams. */ 94 unlock_std_streams (); 95 96 gcc_init_libintl (); 97 98 diagnostic_initialize (global_dc, 0); 99 100 while ((opt = getopt_long (argc, argv, "hlpvw", options, NULL)) != -1) 101 { 102 switch (opt) 103 { 104 case 'h': 105 print_usage (); 106 break; 107 case 'v': 108 print_version (); 109 break; 110 case 'l': 111 flag_dump_contents = 1; 112 break; 113 case 'p': 114 flag_dump_positions = 1; 115 break; 116 case 'w': 117 flag_dump_working_sets = 1; 118 break; 119 default: 120 fprintf (stderr, "unknown flag `%c'\n", opt); 121 } 122 } 123 124 while (argv[optind]) 125 dump_gcov_file (argv[optind++]); 126 return 0; 127 } 128 129 static void 130 print_usage (void) 131 { 132 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n"); 133 printf ("Print coverage file contents\n"); 134 printf (" -h, --help Print this help\n"); 135 printf (" -l, --long Dump record contents too\n"); 136 printf (" -p, --positions Dump record positions\n"); 137 printf (" -v, --version Print version number\n"); 138 printf (" -w, --working-sets Dump working set computed from summary\n"); 139 printf ("\nFor bug reporting instructions, please see:\n%s.\n", 140 bug_report_url); 141 } 142 143 static void 144 print_version (void) 145 { 146 printf ("gcov-dump %s%s\n", pkgversion_string, version_string); 147 printf ("Copyright (C) 2015 Free Software Foundation, Inc.\n"); 148 printf ("This is free software; see the source for copying conditions.\n" 149 "There is NO warranty; not even for MERCHANTABILITY or \n" 150 "FITNESS FOR A PARTICULAR PURPOSE.\n\n"); 151 } 152 153 static void 154 print_prefix (const char *filename, unsigned depth, gcov_position_t position) 155 { 156 static const char prefix[] = " "; 157 158 printf ("%s:", filename); 159 if (flag_dump_positions) 160 printf ("%lu:", (unsigned long) position); 161 printf ("%.*s", (int) depth, prefix); 162 } 163 164 static void 165 dump_gcov_file (const char *filename) 166 { 167 unsigned tags[4]; 168 unsigned depth = 0; 169 170 if (!gcov_open (filename, 1)) 171 { 172 fprintf (stderr, "%s:cannot open\n", filename); 173 return; 174 } 175 176 /* magic */ 177 { 178 unsigned magic = gcov_read_unsigned (); 179 unsigned version; 180 const char *type = NULL; 181 int endianness = 0; 182 char m[4], v[4]; 183 184 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC))) 185 type = "data"; 186 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC))) 187 type = "note"; 188 else 189 { 190 printf ("%s:not a gcov file\n", filename); 191 gcov_close (); 192 return; 193 } 194 version = gcov_read_unsigned (); 195 GCOV_UNSIGNED2STRING (v, version); 196 GCOV_UNSIGNED2STRING (m, magic); 197 198 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename, type, 199 m, v, endianness < 0 ? " (swapped endianness)" : ""); 200 if (version != GCOV_VERSION) 201 { 202 char e[4]; 203 204 GCOV_UNSIGNED2STRING (e, GCOV_VERSION); 205 printf ("%s:warning:current version is `%.4s'\n", filename, e); 206 } 207 } 208 209 /* stamp */ 210 { 211 unsigned stamp = gcov_read_unsigned (); 212 213 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp); 214 } 215 216 while (1) 217 { 218 gcov_position_t base, position = gcov_position (); 219 unsigned tag, length; 220 tag_format_t const *format; 221 unsigned tag_depth; 222 int error; 223 unsigned mask; 224 225 tag = gcov_read_unsigned (); 226 if (!tag) 227 break; 228 length = gcov_read_unsigned (); 229 base = gcov_position (); 230 mask = GCOV_TAG_MASK (tag) >> 1; 231 for (tag_depth = 4; mask; mask >>= 8) 232 { 233 if ((mask & 0xff) != 0xff) 234 { 235 printf ("%s:tag `%08x' is invalid\n", filename, tag); 236 break; 237 } 238 tag_depth--; 239 } 240 for (format = tag_table; format->name; format++) 241 if (format->tag == tag) 242 goto found; 243 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1]; 244 found:; 245 if (tag) 246 { 247 if (depth && depth < tag_depth) 248 { 249 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag)) 250 printf ("%s:tag `%08x' is incorrectly nested\n", 251 filename, tag); 252 } 253 depth = tag_depth; 254 tags[depth - 1] = tag; 255 } 256 257 print_prefix (filename, tag_depth, position); 258 printf ("%08x:%4u:%s", tag, length, format->name); 259 if (format->proc) 260 (*format->proc) (filename, tag, length); 261 262 printf ("\n"); 263 if (flag_dump_contents && format->proc) 264 { 265 unsigned long actual_length = gcov_position () - base; 266 267 if (actual_length > length) 268 printf ("%s:record size mismatch %lu bytes overread\n", 269 filename, actual_length - length); 270 else if (length > actual_length) 271 printf ("%s:record size mismatch %lu bytes unread\n", 272 filename, length - actual_length); 273 } 274 gcov_sync (base, length); 275 if ((error = gcov_is_error ())) 276 { 277 printf (error < 0 ? "%s:counter overflow at %lu\n" : 278 "%s:read error at %lu\n", filename, 279 (long unsigned) gcov_position ()); 280 break; 281 } 282 } 283 gcov_close (); 284 } 285 286 static void 287 tag_function (const char *filename ATTRIBUTE_UNUSED, 288 unsigned tag ATTRIBUTE_UNUSED, unsigned length) 289 { 290 unsigned long pos = gcov_position (); 291 292 if (!length) 293 printf (" placeholder"); 294 else 295 { 296 printf (" ident=%u", gcov_read_unsigned ()); 297 printf (", lineno_checksum=0x%08x", gcov_read_unsigned ()); 298 printf (", cfg_checksum=0x%08x", gcov_read_unsigned ()); 299 300 if (gcov_position () - pos < length) 301 { 302 const char *name; 303 304 name = gcov_read_string (); 305 printf (", `%s'", name ? name : "NULL"); 306 name = gcov_read_string (); 307 printf (" %s", name ? name : "NULL"); 308 printf (":%u", gcov_read_unsigned ()); 309 } 310 } 311 } 312 313 static void 314 tag_blocks (const char *filename ATTRIBUTE_UNUSED, 315 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED) 316 { 317 unsigned n_blocks = GCOV_TAG_BLOCKS_NUM (length); 318 319 printf (" %u blocks", n_blocks); 320 321 if (flag_dump_contents) 322 { 323 unsigned ix; 324 325 for (ix = 0; ix != n_blocks; ix++) 326 { 327 if (!(ix & 7)) 328 { 329 printf ("\n"); 330 print_prefix (filename, 0, gcov_position ()); 331 printf ("\t\t%u", ix); 332 } 333 printf (" %04x", gcov_read_unsigned ()); 334 } 335 } 336 } 337 338 static void 339 tag_arcs (const char *filename ATTRIBUTE_UNUSED, 340 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED) 341 { 342 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length); 343 344 printf (" %u arcs", n_arcs); 345 if (flag_dump_contents) 346 { 347 unsigned ix; 348 unsigned blockno = gcov_read_unsigned (); 349 350 for (ix = 0; ix != n_arcs; ix++) 351 { 352 unsigned dst, flags; 353 354 if (!(ix & 3)) 355 { 356 printf ("\n"); 357 print_prefix (filename, 0, gcov_position ()); 358 printf ("\tblock %u:", blockno); 359 } 360 dst = gcov_read_unsigned (); 361 flags = gcov_read_unsigned (); 362 printf (" %u:%04x", dst, flags); 363 if (flags) 364 { 365 char c = '('; 366 367 if (flags & GCOV_ARC_ON_TREE) 368 printf ("%ctree", c), c = ','; 369 if (flags & GCOV_ARC_FAKE) 370 printf ("%cfake", c), c = ','; 371 if (flags & GCOV_ARC_FALLTHROUGH) 372 printf ("%cfall", c), c = ','; 373 printf (")"); 374 } 375 } 376 } 377 } 378 379 static void 380 tag_lines (const char *filename ATTRIBUTE_UNUSED, 381 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED) 382 { 383 if (flag_dump_contents) 384 { 385 unsigned blockno = gcov_read_unsigned (); 386 char const *sep = NULL; 387 388 while (1) 389 { 390 gcov_position_t position = gcov_position (); 391 const char *source = NULL; 392 unsigned lineno = gcov_read_unsigned (); 393 394 if (!lineno) 395 { 396 source = gcov_read_string (); 397 if (!source) 398 break; 399 sep = NULL; 400 } 401 402 if (!sep) 403 { 404 printf ("\n"); 405 print_prefix (filename, 0, position); 406 printf ("\tblock %u:", blockno); 407 sep = ""; 408 } 409 if (lineno) 410 { 411 printf ("%s%u", sep, lineno); 412 sep = ", "; 413 } 414 else 415 { 416 printf ("%s`%s'", sep, source); 417 sep = ":"; 418 } 419 } 420 } 421 } 422 423 static void 424 tag_counters (const char *filename ATTRIBUTE_UNUSED, 425 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED) 426 { 427 #define DEF_GCOV_COUNTER(COUNTER, NAME, MERGE_FN) NAME, 428 static const char *const counter_names[] = { 429 #include "gcov-counter.def" 430 }; 431 #undef DEF_GCOV_COUNTER 432 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length); 433 434 printf (" %s %u counts", 435 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts); 436 if (flag_dump_contents) 437 { 438 unsigned ix; 439 440 for (ix = 0; ix != n_counts; ix++) 441 { 442 gcov_type count; 443 444 if (!(ix & 7)) 445 { 446 printf ("\n"); 447 print_prefix (filename, 0, gcov_position ()); 448 printf ("\t\t%u", ix); 449 } 450 451 count = gcov_read_counter (); 452 printf (" "); 453 printf ("%"PRId64, count); 454 } 455 } 456 } 457 458 static void 459 tag_summary (const char *filename ATTRIBUTE_UNUSED, 460 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED) 461 { 462 struct gcov_summary summary; 463 unsigned ix, h_ix; 464 gcov_bucket_type *histo_bucket; 465 466 gcov_read_summary (&summary); 467 printf (" checksum=0x%08x", summary.checksum); 468 469 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++) 470 { 471 printf ("\n"); 472 print_prefix (filename, 0, 0); 473 printf ("\t\tcounts=%u, runs=%u", 474 summary.ctrs[ix].num, summary.ctrs[ix].runs); 475 476 printf (", sum_all=%"PRId64, 477 (int64_t)summary.ctrs[ix].sum_all); 478 printf (", run_max=%"PRId64, 479 (int64_t)summary.ctrs[ix].run_max); 480 printf (", sum_max=%"PRId64, 481 (int64_t)summary.ctrs[ix].sum_max); 482 if (ix != GCOV_COUNTER_ARCS) 483 continue; 484 printf ("\n"); 485 print_prefix (filename, 0, 0); 486 printf ("\t\tcounter histogram:"); 487 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++) 488 { 489 histo_bucket = &summary.ctrs[ix].histogram[h_ix]; 490 if (!histo_bucket->num_counters) 491 continue; 492 printf ("\n"); 493 print_prefix (filename, 0, 0); 494 printf ("\t\t%d: num counts=%u, min counter=" 495 "%"PRId64 ", cum_counter=" 496 "%"PRId64, 497 h_ix, histo_bucket->num_counters, 498 (int64_t)histo_bucket->min_value, 499 (int64_t)histo_bucket->cum_value); 500 } 501 if (flag_dump_working_sets) 502 dump_working_sets (filename, &summary.ctrs[ix]); 503 } 504 } 505 506 static void 507 dump_working_sets (const char *filename ATTRIBUTE_UNUSED, 508 const struct gcov_ctr_summary *summary) 509 { 510 gcov_working_set_t gcov_working_sets[NUM_GCOV_WORKING_SETS]; 511 unsigned ws_ix, pctinc, pct; 512 gcov_working_set_t *ws_info; 513 514 compute_working_sets (summary, gcov_working_sets); 515 516 printf ("\n"); 517 print_prefix (filename, 0, 0); 518 printf ("\t\tcounter working sets:"); 519 /* Multiply the percentage by 100 to avoid float. */ 520 pctinc = 100 * 100 / NUM_GCOV_WORKING_SETS; 521 for (ws_ix = 0, pct = pctinc; ws_ix < NUM_GCOV_WORKING_SETS; 522 ws_ix++, pct += pctinc) 523 { 524 if (ws_ix == NUM_GCOV_WORKING_SETS - 1) 525 pct = 9990; 526 ws_info = &gcov_working_sets[ws_ix]; 527 /* Print out the percentage using int arithmatic to avoid float. */ 528 printf ("\n"); 529 print_prefix (filename, 0, 0); 530 printf ("\t\t%u.%02u%%: num counts=%u, min counter=" 531 "%"PRId64, 532 pct / 100, pct - (pct / 100 * 100), 533 ws_info->num_counters, 534 (int64_t)ws_info->min_counter); 535 } 536 } 537