00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "config.h"
00021 #include "system.h"
00022 #include "coretypes.h"
00023 #include "tm.h"
00024 #include "version.h"
00025 #include <getopt.h>
00026 #define IN_GCOV (-1)
00027 #include "gcov-io.h"
00028 #include "gcov-io.c"
00029
00030 static void dump_file (const char *);
00031 static void print_prefix (const char *, unsigned, gcov_position_t);
00032 static void print_usage (void);
00033 static void print_version (void);
00034 static void tag_function (const char *, unsigned, unsigned);
00035 static void tag_blocks (const char *, unsigned, unsigned);
00036 static void tag_arcs (const char *, unsigned, unsigned);
00037 static void tag_lines (const char *, unsigned, unsigned);
00038 static void tag_counters (const char *, unsigned, unsigned);
00039 static void tag_summary (const char *, unsigned, unsigned);
00040 extern int main (int, char **);
00041
00042 typedef struct tag_format
00043 {
00044 unsigned tag;
00045 char const *name;
00046 void (*proc) (const char *, unsigned, unsigned);
00047 } tag_format_t;
00048
00049 static int flag_dump_contents = 0;
00050 static int flag_dump_positions = 0;
00051
00052 static const struct option options[] =
00053 {
00054 { "help", no_argument, NULL, 'h' },
00055 { "version", no_argument, NULL, 'v' },
00056 { "long", no_argument, NULL, 'l' },
00057 { "positions", no_argument, NULL, 'o' },
00058 { 0, 0, 0, 0 }
00059 };
00060
00061 static const tag_format_t tag_table[] =
00062 {
00063 {0, "NOP", NULL},
00064 {0, "UNKNOWN", NULL},
00065 {0, "COUNTERS", tag_counters},
00066 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function},
00067 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks},
00068 {GCOV_TAG_ARCS, "ARCS", tag_arcs},
00069 {GCOV_TAG_LINES, "LINES", tag_lines},
00070 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary},
00071 {GCOV_TAG_PROGRAM_SUMMARY, "PROGRAM_SUMMARY", tag_summary},
00072 {0, NULL, NULL}
00073 };
00074
00075 int
00076 main (int argc ATTRIBUTE_UNUSED, char **argv)
00077 {
00078 int opt;
00079
00080
00081 unlock_std_streams ();
00082
00083 while ((opt = getopt_long (argc, argv, "hlpv", options, NULL)) != -1)
00084 {
00085 switch (opt)
00086 {
00087 case 'h':
00088 print_usage ();
00089 break;
00090 case 'v':
00091 print_version ();
00092 break;
00093 case 'l':
00094 flag_dump_contents = 1;
00095 break;
00096 case 'p':
00097 flag_dump_positions = 1;
00098 break;
00099 default:
00100 fprintf (stderr, "unknown flag `%c'\n", opt);
00101 }
00102 }
00103
00104 while (argv[optind])
00105 dump_file (argv[optind++]);
00106 return 0;
00107 }
00108
00109 static void
00110 print_usage (void)
00111 {
00112 printf ("Usage: gcov-dump [OPTION] ... gcovfiles\n");
00113 printf ("Print coverage file contents\n");
00114 printf (" -h, --help Print this help\n");
00115 printf (" -v, --version Print version number\n");
00116 printf (" -l, --long Dump record contents too\n");
00117 printf (" -p, --positions Dump record positions\n");
00118 }
00119
00120 static void
00121 print_version (void)
00122 {
00123 printf ("gcov-dump (GCC) %s\n", version_string);
00124 printf ("Copyright (C) 2003 Free Software Foundation, Inc.\n");
00125 printf ("This is free software; see the source for copying conditions.\n"
00126 "There is NO warranty; not even for MERCHANTABILITY or \n"
00127 "FITNESS FOR A PARTICULAR PURPOSE.\n\n");
00128 }
00129
00130 static void
00131 print_prefix (const char *filename, unsigned depth, gcov_position_t position)
00132 {
00133 static const char prefix[] = " ";
00134
00135 printf ("%s:", filename);
00136 if (flag_dump_positions)
00137 printf ("%lu:", (unsigned long) position);
00138 printf ("%.*s", (int) depth, prefix);
00139 }
00140
00141 static void
00142 dump_file (const char *filename)
00143 {
00144 unsigned tags[4];
00145 unsigned depth = 0;
00146
00147 if (!gcov_open (filename, 1))
00148 {
00149 fprintf (stderr, "%s:cannot open\n", filename);
00150 return;
00151 }
00152
00153
00154 {
00155 unsigned magic = gcov_read_unsigned ();
00156 unsigned version;
00157 const char *type = NULL;
00158 int endianness = 0;
00159 char m[4], v[4];
00160
00161 if ((endianness = gcov_magic (magic, GCOV_DATA_MAGIC)))
00162 type = "data";
00163 else if ((endianness = gcov_magic (magic, GCOV_NOTE_MAGIC)))
00164 type = "note";
00165 else
00166 {
00167 printf ("%s:not a gcov file\n", filename);
00168 gcov_close ();
00169 return;
00170 }
00171 version = gcov_read_unsigned ();
00172 GCOV_UNSIGNED2STRING (v, version);
00173 GCOV_UNSIGNED2STRING (m, magic);
00174
00175 printf ("%s:%s:magic `%.4s':version `%.4s'%s\n", filename, type,
00176 m, v, endianness < 0 ? " (swapped endianness)" : "");
00177 if (version != GCOV_VERSION)
00178 {
00179 char e[4];
00180
00181 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
00182 printf ("%s:warning:current version is `%.4s'\n", filename, e);
00183 }
00184 }
00185
00186
00187 {
00188 unsigned stamp = gcov_read_unsigned ();
00189
00190 printf ("%s:stamp %lu\n", filename, (unsigned long)stamp);
00191 }
00192
00193 while (1)
00194 {
00195 gcov_position_t base, position = gcov_position ();
00196 unsigned tag, length;
00197 tag_format_t const *format;
00198 unsigned tag_depth;
00199 int error;
00200 unsigned mask;
00201
00202 tag = gcov_read_unsigned ();
00203 if (!tag)
00204 break;
00205 length = gcov_read_unsigned ();
00206 base = gcov_position ();
00207 mask = GCOV_TAG_MASK (tag) >> 1;
00208 for (tag_depth = 4; mask; mask >>= 8)
00209 {
00210 if ((mask & 0xff) != 0xff)
00211 {
00212 printf ("%s:tag `%08x' is invalid\n", filename, tag);
00213 break;
00214 }
00215 tag_depth--;
00216 }
00217 for (format = tag_table; format->name; format++)
00218 if (format->tag == tag)
00219 goto found;
00220 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
00221 found:;
00222 if (tag)
00223 {
00224 if (depth && depth < tag_depth)
00225 {
00226 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
00227 printf ("%s:tag `%08x' is incorrectly nested\n",
00228 filename, tag);
00229 }
00230 depth = tag_depth;
00231 tags[depth - 1] = tag;
00232 }
00233
00234 print_prefix (filename, tag_depth, position);
00235 printf ("%08x:%4u:%s", tag, length, format->name);
00236 if (format->proc)
00237 (*format->proc) (filename, tag, length);
00238
00239 printf ("\n");
00240 if (flag_dump_contents && format->proc)
00241 {
00242 unsigned long actual_length = gcov_position () - base;
00243
00244 if (actual_length > length)
00245 printf ("%s:record size mismatch %lu bytes overread\n",
00246 filename, actual_length - length);
00247 else if (length > actual_length)
00248 printf ("%s:record size mismatch %lu bytes unread\n",
00249 filename, length - actual_length);
00250 }
00251 gcov_sync (base, length);
00252 if ((error = gcov_is_error ()))
00253 {
00254 printf (error < 0 ? "%s:counter overflow at %lu\n" :
00255 "%s:read error at %lu\n", filename,
00256 (long unsigned) gcov_position ());
00257 break;
00258 }
00259 }
00260 gcov_close ();
00261 }
00262
00263 static void
00264 tag_function (const char *filename ATTRIBUTE_UNUSED,
00265 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
00266 {
00267 unsigned long pos = gcov_position ();
00268
00269 printf (" ident=%u", gcov_read_unsigned ());
00270 printf (", checksum=0x%08x", gcov_read_unsigned ());
00271
00272 if (gcov_position () - pos < length)
00273 {
00274 const char *name;
00275
00276 name = gcov_read_string ();
00277 printf (", `%s'", name ? name : "NULL");
00278 name = gcov_read_string ();
00279 printf (" %s", name ? name : "NULL");
00280 printf (":%u", gcov_read_unsigned ());
00281 }
00282 }
00283
00284 static void
00285 tag_blocks (const char *filename ATTRIBUTE_UNUSED,
00286 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
00287 {
00288 unsigned n_blocks = GCOV_TAG_BLOCKS_NUM (length);
00289
00290 printf (" %u blocks", n_blocks);
00291
00292 if (flag_dump_contents)
00293 {
00294 unsigned ix;
00295
00296 for (ix = 0; ix != n_blocks; ix++)
00297 {
00298 if (!(ix & 7))
00299 {
00300 printf ("\n");
00301 print_prefix (filename, 0, gcov_position ());
00302 printf ("\t\t%u", ix);
00303 }
00304 printf (" %04x", gcov_read_unsigned ());
00305 }
00306 }
00307 }
00308
00309 static void
00310 tag_arcs (const char *filename ATTRIBUTE_UNUSED,
00311 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
00312 {
00313 unsigned n_arcs = GCOV_TAG_ARCS_NUM (length);
00314
00315 printf (" %u arcs", n_arcs);
00316 if (flag_dump_contents)
00317 {
00318 unsigned ix;
00319 unsigned blockno = gcov_read_unsigned ();
00320
00321 for (ix = 0; ix != n_arcs; ix++)
00322 {
00323 unsigned dst, flags;
00324
00325 if (!(ix & 3))
00326 {
00327 printf ("\n");
00328 print_prefix (filename, 0, gcov_position ());
00329 printf ("\tblock %u:", blockno);
00330 }
00331 dst = gcov_read_unsigned ();
00332 flags = gcov_read_unsigned ();
00333 printf (" %u:%04x", dst, flags);
00334 }
00335 }
00336 }
00337
00338 static void
00339 tag_lines (const char *filename ATTRIBUTE_UNUSED,
00340 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
00341 {
00342 if (flag_dump_contents)
00343 {
00344 unsigned blockno = gcov_read_unsigned ();
00345 char const *sep = NULL;
00346
00347 while (1)
00348 {
00349 gcov_position_t position = gcov_position ();
00350 const char *source = NULL;
00351 unsigned lineno = gcov_read_unsigned ();
00352
00353 if (!lineno)
00354 {
00355 source = gcov_read_string ();
00356 if (!source)
00357 break;
00358 sep = NULL;
00359 }
00360
00361 if (!sep)
00362 {
00363 printf ("\n");
00364 print_prefix (filename, 0, position);
00365 printf ("\tblock %u:", blockno);
00366 sep = "";
00367 }
00368 if (lineno)
00369 {
00370 printf ("%s%u", sep, lineno);
00371 sep = ", ";
00372 }
00373 else
00374 {
00375 printf ("%s`%s'", sep, source);
00376 sep = ":";
00377 }
00378 }
00379 }
00380 }
00381
00382 static void
00383 tag_counters (const char *filename ATTRIBUTE_UNUSED,
00384 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
00385 {
00386 static const char *const counter_names[] = GCOV_COUNTER_NAMES;
00387 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
00388
00389 printf (" %s %u counts",
00390 counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts);
00391 if (flag_dump_contents)
00392 {
00393 unsigned ix;
00394
00395 for (ix = 0; ix != n_counts; ix++)
00396 {
00397 gcov_type count;
00398
00399 if (!(ix & 7))
00400 {
00401 printf ("\n");
00402 print_prefix (filename, 0, gcov_position ());
00403 printf ("\t\t%u", ix);
00404 }
00405
00406 count = gcov_read_counter ();
00407 printf (" ");
00408 printf (HOST_WIDEST_INT_PRINT_DEC, count);
00409 }
00410 }
00411 }
00412
00413 static void
00414 tag_summary (const char *filename ATTRIBUTE_UNUSED,
00415 unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
00416 {
00417 struct gcov_summary summary;
00418 unsigned ix;
00419
00420 gcov_read_summary (&summary);
00421 printf (" checksum=0x%08x", summary.checksum);
00422
00423 for (ix = 0; ix != GCOV_COUNTERS; ix++)
00424 {
00425 printf ("\n");
00426 print_prefix (filename, 0, 0);
00427 printf ("\t\tcounts=%u, runs=%u",
00428 summary.ctrs[ix].num, summary.ctrs[ix].runs);
00429
00430 printf (", sum_all=" HOST_WIDEST_INT_PRINT_DEC,
00431 (HOST_WIDEST_INT)summary.ctrs[ix].sum_all);
00432 printf (", run_max=" HOST_WIDEST_INT_PRINT_DEC,
00433 (HOST_WIDEST_INT)summary.ctrs[ix].run_max);
00434 printf (", sum_max=" HOST_WIDEST_INT_PRINT_DEC,
00435 (HOST_WIDEST_INT)summary.ctrs[ix].sum_max);
00436 }
00437 }