gcov-dump.c 14 KB

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