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