Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | /* |
2 | * Input and output from/to gmon.out files. | |
3 | */ | |
4 | #include "cg_arcs.h" | |
5 | #include "basic_blocks.h" | |
6 | #include "bfd.h" | |
7 | #include "corefile.h" | |
8 | #include "call_graph.h" | |
9 | #include "gmon_io.h" | |
10 | #include "gmon_out.h" | |
11 | #include "gmon.h" /* fetch header for old format */ | |
12 | #include "gprof.h" | |
13 | #include "hertz.h" | |
14 | #include "hist.h" | |
15 | #include "libiberty.h" | |
16 | ||
17 | int gmon_input = 0; | |
18 | int gmon_file_version = 0; /* 0 == old (non-versioned) file format */ | |
19 | ||
20 | /* | |
21 | * This probably ought to be in libbfd. | |
22 | */ | |
23 | bfd_vma | |
24 | DEFUN (get_vma, (abfd, addr), bfd * abfd AND bfd_byte * addr) | |
25 | { | |
26 | switch (sizeof (char*)) | |
27 | { | |
28 | case 4: | |
29 | return bfd_get_32 (abfd, addr); | |
30 | case 8: | |
31 | return bfd_get_64 (abfd, addr); | |
32 | default: | |
33 | fprintf (stderr, _("%s: bfd_vma has unexpected size of %ld bytes\n"), | |
34 | whoami, (long) sizeof (char*)); | |
35 | done (1); | |
36 | } | |
37 | } | |
38 | ||
39 | ||
40 | /* | |
41 | * This probably ought to be in libbfd. | |
42 | */ | |
43 | void | |
44 | DEFUN (put_vma, (abfd, val, addr), bfd * abfd AND bfd_vma val AND bfd_byte * addr) | |
45 | { | |
46 | switch (sizeof (char*)) | |
47 | { | |
48 | case 4: | |
49 | bfd_put_32 (abfd, val, addr); | |
50 | break; | |
51 | case 8: | |
52 | bfd_put_64 (abfd, val, addr); | |
53 | break; | |
54 | default: | |
55 | fprintf (stderr, _("%s: bfd_vma has unexpected size of %ld bytes\n"), | |
56 | whoami, (long) sizeof (char*)); | |
57 | done (1); | |
58 | } | |
59 | } | |
60 | ||
61 | ||
62 | void | |
63 | DEFUN (gmon_out_read, (filename), const char *filename) | |
64 | { | |
65 | FILE *ifp; | |
66 | struct gmon_hdr ghdr; | |
67 | unsigned char tag; | |
68 | int nhist = 0, narcs = 0, nbbs = 0; | |
69 | ||
70 | /* open gmon.out file: */ | |
71 | ||
72 | if (strcmp (filename, "-") == 0) | |
73 | { | |
74 | ifp = stdin; | |
75 | } | |
76 | else | |
77 | { | |
78 | ifp = fopen (filename, FOPEN_RB); | |
79 | if (!ifp) | |
80 | { | |
81 | perror (filename); | |
82 | done (1); | |
83 | } | |
84 | } | |
85 | if (fread (&ghdr, sizeof (struct gmon_hdr), 1, ifp) != 1) | |
86 | { | |
87 | fprintf (stderr, _("%s: file too short to be a gmon file\n"), | |
88 | filename); | |
89 | done (1); | |
90 | } | |
91 | ||
92 | if ((file_format == FF_MAGIC) || | |
93 | (file_format == FF_AUTO && !strncmp (&ghdr.cookie[0], GMON_MAGIC, 4))) | |
94 | { | |
95 | if (file_format == FF_MAGIC && strncmp (&ghdr.cookie[0], GMON_MAGIC, 4)) | |
96 | { | |
97 | fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"), | |
98 | whoami, filename); | |
99 | done (1); | |
100 | } | |
101 | ||
102 | /* right magic, so it's probably really a new gmon.out file */ | |
103 | ||
104 | gmon_file_version = bfd_get_32 (core_bfd, (bfd_byte *) ghdr.version); | |
105 | if (gmon_file_version != GMON_VERSION && gmon_file_version != 0) | |
106 | { | |
107 | fprintf (stderr, | |
108 | _("%s: file `%s' has unsupported version %d\n"), | |
109 | whoami, filename, gmon_file_version); | |
110 | done (1); | |
111 | } | |
112 | ||
113 | /* read in all the records: */ | |
114 | while (fread (&tag, sizeof (tag), 1, ifp) == 1) | |
115 | { | |
116 | switch (tag) | |
117 | { | |
118 | case GMON_TAG_TIME_HIST: | |
119 | ++nhist; | |
120 | gmon_input |= INPUT_HISTOGRAM; | |
121 | hist_read_rec (ifp, filename); | |
122 | break; | |
123 | ||
124 | case GMON_TAG_CG_ARC: | |
125 | ++narcs; | |
126 | gmon_input |= INPUT_CALL_GRAPH; | |
127 | cg_read_rec (ifp, filename); | |
128 | break; | |
129 | ||
130 | case GMON_TAG_BB_COUNT: | |
131 | ++nbbs; | |
132 | gmon_input |= INPUT_BB_COUNTS; | |
133 | bb_read_rec (ifp, filename); | |
134 | break; | |
135 | ||
136 | default: | |
137 | fprintf (stderr, | |
138 | _("%s: %s: found bad tag %d (file corrupted?)\n"), | |
139 | whoami, filename, tag); | |
140 | done (1); | |
141 | } | |
142 | } | |
143 | } | |
144 | else if (file_format == FF_AUTO | |
145 | || file_format == FF_BSD | |
146 | || file_format == FF_BSD44) | |
147 | { | |
148 | struct hdr | |
149 | { | |
150 | bfd_vma low_pc; | |
151 | bfd_vma high_pc; | |
152 | int ncnt; | |
153 | }; | |
154 | int i, samp_bytes, header_size; | |
155 | unsigned long count; | |
156 | bfd_vma from_pc, self_pc; | |
157 | struct raw_arc raw_arc; | |
158 | struct raw_phdr raw; | |
159 | static struct hdr h; | |
160 | UNIT raw_bin_count; | |
161 | struct hdr tmp; | |
162 | ||
163 | /* | |
164 | * Information from a gmon.out file is in two parts: an array of | |
165 | * sampling hits within pc ranges, and the arcs. | |
166 | */ | |
167 | gmon_input = INPUT_HISTOGRAM | INPUT_CALL_GRAPH; | |
168 | ||
169 | /* | |
170 | * This fseek() ought to work even on stdin as long as it's | |
171 | * not an interactive device (heck, is there anybody who would | |
172 | * want to type in a gmon.out at the terminal?). | |
173 | */ | |
174 | if (fseek (ifp, 0, SEEK_SET) < 0) | |
175 | { | |
176 | perror (filename); | |
177 | done (1); | |
178 | } | |
179 | if (fread (&raw, 1, sizeof (struct raw_phdr), ifp) | |
180 | != sizeof (struct raw_phdr)) | |
181 | { | |
182 | fprintf (stderr, _("%s: file too short to be a gmon file\n"), | |
183 | filename); | |
184 | done (1); | |
185 | } | |
186 | tmp.low_pc = get_vma (core_bfd, (bfd_byte *) &raw.low_pc[0]); | |
187 | tmp.high_pc = get_vma (core_bfd, (bfd_byte *) &raw.high_pc[0]); | |
188 | tmp.ncnt = bfd_get_32 (core_bfd, (bfd_byte *) &raw.ncnt[0]); | |
189 | ||
190 | if (bfd_get_32 (core_bfd, (bfd_byte *) &raw.version[0]) | |
191 | == GMONVERSION) | |
192 | { | |
193 | int profrate; | |
194 | ||
195 | /* 4.4BSD format header. */ | |
196 | ||
197 | profrate = bfd_get_32 (core_bfd, (bfd_byte *) &raw.profrate[0]); | |
198 | if (!s_highpc) | |
199 | hz = profrate; | |
200 | else if (hz != profrate) | |
201 | { | |
202 | fprintf (stderr, | |
203 | _("%s: profiling rate incompatible with first gmon file\n"), | |
204 | filename); | |
205 | done (1); | |
206 | } | |
207 | ||
208 | header_size = sizeof (struct raw_phdr); | |
209 | } | |
210 | else | |
211 | { | |
212 | /* old style BSD format. */ | |
213 | if (file_format == FF_BSD44) | |
214 | { | |
215 | fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"), | |
216 | whoami, filename); | |
217 | done (1); | |
218 | } | |
219 | ||
220 | if (fseek (ifp, sizeof (struct old_raw_phdr), SEEK_SET) < 0) | |
221 | { | |
222 | perror (filename); | |
223 | done (1); | |
224 | } | |
225 | ||
226 | header_size = sizeof (struct old_raw_phdr); | |
227 | } | |
228 | ||
229 | if (s_highpc && (tmp.low_pc != h.low_pc || | |
230 | tmp.high_pc != h.high_pc || tmp.ncnt != h.ncnt)) | |
231 | { | |
232 | fprintf (stderr, _("%s: incompatible with first gmon file\n"), | |
233 | filename); | |
234 | done (1); | |
235 | } | |
236 | h = tmp; | |
237 | s_lowpc = (bfd_vma) h.low_pc; | |
238 | s_highpc = (bfd_vma) h.high_pc; | |
239 | lowpc = (bfd_vma) h.low_pc / sizeof (UNIT); | |
240 | highpc = (bfd_vma) h.high_pc / sizeof (UNIT); | |
241 | samp_bytes = h.ncnt - header_size; | |
242 | hist_num_bins = samp_bytes / sizeof (UNIT); | |
243 | DBG (SAMPLEDEBUG, | |
244 | printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n", | |
fdcf7d43 ILT |
245 | (unsigned long) h.low_pc, (unsigned long) h.high_pc, |
246 | h.ncnt); | |
252b5132 | 247 | printf ("[gmon_out_read] s_lowpc 0x%lx s_highpc 0x%lx\n", |
fdcf7d43 | 248 | (unsigned long) s_lowpc, (unsigned long) s_highpc); |
252b5132 | 249 | printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx\n", |
fdcf7d43 | 250 | (unsigned long) lowpc, (unsigned long) highpc); |
252b5132 RH |
251 | printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n", |
252 | samp_bytes, hist_num_bins)); | |
253 | ||
2d35e9f6 NC |
254 | /* Make sure that we have sensible values. */ |
255 | if (samp_bytes < 0 || lowpc > highpc) | |
256 | { | |
257 | fprintf (stderr, | |
258 | _("%s: file '%s' does not appear to be in gmon.out format\n"), | |
259 | whoami, filename); | |
260 | done (1); | |
261 | } | |
262 | ||
252b5132 RH |
263 | if (hist_num_bins) |
264 | { | |
265 | ++nhist; | |
266 | } | |
267 | ||
268 | if (!hist_sample) | |
269 | { | |
270 | hist_sample = | |
271 | (int *) xmalloc (hist_num_bins * sizeof (hist_sample[0])); | |
272 | memset (hist_sample, 0, hist_num_bins * sizeof (hist_sample[0])); | |
273 | } | |
274 | ||
275 | for (i = 0; i < hist_num_bins; ++i) | |
276 | { | |
277 | if (fread (raw_bin_count, sizeof (raw_bin_count), 1, ifp) != 1) | |
278 | { | |
279 | fprintf (stderr, | |
280 | _("%s: unexpected EOF after reading %d/%d bins\n"), | |
281 | whoami, --i, hist_num_bins); | |
282 | done (1); | |
283 | } | |
284 | hist_sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) raw_bin_count); | |
285 | } | |
286 | ||
287 | /* | |
288 | * The rest of the file consists of a bunch of <from,self,count> | |
289 | * tuples: | |
290 | */ | |
291 | while (fread (&raw_arc, sizeof (raw_arc), 1, ifp) == 1) | |
292 | { | |
293 | ++narcs; | |
294 | from_pc = get_vma (core_bfd, (bfd_byte *) raw_arc.from_pc); | |
295 | self_pc = get_vma (core_bfd, (bfd_byte *) raw_arc.self_pc); | |
296 | count = bfd_get_32 (core_bfd, (bfd_byte *) raw_arc.count); | |
297 | DBG (SAMPLEDEBUG, | |
298 | printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n", | |
fdcf7d43 | 299 | (unsigned long) from_pc, (unsigned long) self_pc, count)); |
252b5132 RH |
300 | /* add this arc: */ |
301 | cg_tally (from_pc, self_pc, count); | |
302 | } | |
303 | fclose (ifp); | |
304 | ||
305 | if (hz == HZ_WRONG) | |
306 | { | |
307 | /* | |
308 | * How many ticks per second? If we can't tell, report | |
309 | * time in ticks. | |
310 | */ | |
311 | hz = hertz (); | |
312 | if (hz == HZ_WRONG) | |
313 | { | |
314 | hz = 1; | |
315 | fprintf (stderr, _("time is in ticks, not seconds\n")); | |
316 | } | |
317 | } | |
318 | } | |
319 | else | |
320 | { | |
321 | fprintf (stderr, _("%s: don't know how to deal with file format %d\n"), | |
322 | whoami, file_format); | |
323 | done (1); | |
324 | } | |
325 | ||
326 | if (output_style & STYLE_GMON_INFO) | |
327 | { | |
328 | printf (_("File `%s' (version %d) contains:\n"), | |
329 | filename, gmon_file_version); | |
330 | printf (_("\t%d histogram record%s\n"), | |
331 | nhist, nhist == 1 ? "" : "s"); | |
332 | printf (_("\t%d call-graph record%s\n"), | |
333 | narcs, narcs == 1 ? "" : "s"); | |
334 | printf (_("\t%d basic-block count record%s\n"), | |
335 | nbbs, nbbs == 1 ? "" : "s"); | |
336 | first_output = FALSE; | |
337 | } | |
338 | } | |
339 | ||
340 | ||
341 | void | |
342 | DEFUN (gmon_out_write, (filename), const char *filename) | |
343 | { | |
344 | FILE *ofp; | |
345 | struct gmon_hdr ghdr; | |
346 | ||
347 | ofp = fopen (filename, FOPEN_WB); | |
348 | if (!ofp) | |
349 | { | |
350 | perror (filename); | |
351 | done (1); | |
352 | } | |
353 | ||
354 | if (file_format == FF_AUTO || file_format == FF_MAGIC) | |
355 | { | |
356 | /* write gmon header: */ | |
357 | ||
358 | memcpy (&ghdr.cookie[0], GMON_MAGIC, 4); | |
359 | bfd_put_32 (core_bfd, GMON_VERSION, (bfd_byte *) ghdr.version); | |
360 | if (fwrite (&ghdr, sizeof (ghdr), 1, ofp) != 1) | |
361 | { | |
362 | perror (filename); | |
363 | done (1); | |
364 | } | |
365 | ||
366 | /* write execution time histogram if we have one: */ | |
367 | if (gmon_input & INPUT_HISTOGRAM) | |
368 | { | |
369 | hist_write_hist (ofp, filename); | |
370 | } | |
371 | ||
372 | /* write call graph arcs if we have any: */ | |
373 | if (gmon_input & INPUT_CALL_GRAPH) | |
374 | { | |
375 | cg_write_arcs (ofp, filename); | |
376 | } | |
377 | ||
378 | /* write basic-block info if we have it: */ | |
379 | if (gmon_input & INPUT_BB_COUNTS) | |
380 | { | |
381 | bb_write_blocks (ofp, filename); | |
382 | } | |
383 | } | |
384 | else if (file_format == FF_BSD || file_format == FF_BSD44) | |
385 | { | |
386 | struct raw_arc raw_arc; | |
387 | UNIT raw_bin_count; | |
388 | struct raw_phdr h; | |
389 | int i; | |
390 | Arc *arc; | |
391 | Sym *sym; | |
392 | ||
393 | memset (&h, 0, sizeof h); | |
394 | put_vma (core_bfd, s_lowpc, (bfd_byte *) &h.low_pc); | |
395 | put_vma (core_bfd, s_highpc, (bfd_byte *) &h.high_pc); | |
396 | bfd_put_32 (core_bfd, | |
397 | hist_num_bins * sizeof (UNIT) + sizeof (struct raw_phdr), | |
398 | (bfd_byte *) &h.ncnt); | |
399 | ||
400 | /* Write header. Use new style BSD format is explicitly | |
401 | specified, or if the profiling rate is non-standard; | |
402 | otherwise, use the old BSD format. */ | |
403 | if (file_format == FF_BSD44 | |
404 | || hz != hertz ()) | |
405 | { | |
406 | bfd_put_32 (core_bfd, GMONVERSION, (bfd_byte *) &h.version); | |
407 | bfd_put_32 (core_bfd, hz, (bfd_byte *) &h.profrate); | |
408 | if (fwrite (&h, sizeof (struct raw_phdr), 1, ofp) != 1) | |
409 | { | |
410 | perror (filename); | |
411 | done (1); | |
412 | } | |
413 | } | |
414 | else | |
415 | { | |
416 | if (fwrite (&h, sizeof (struct old_raw_phdr), 1, ofp) != 1) | |
417 | { | |
418 | perror (filename); | |
419 | done (1); | |
420 | } | |
421 | } | |
422 | ||
423 | /* dump the samples: */ | |
424 | ||
425 | for (i = 0; i < hist_num_bins; ++i) | |
426 | { | |
427 | bfd_put_16 (core_bfd, hist_sample[i], (bfd_byte *) & raw_bin_count[0]); | |
428 | if (fwrite (&raw_bin_count[0], sizeof (raw_bin_count), 1, ofp) != 1) | |
429 | { | |
430 | perror (filename); | |
431 | done (1); | |
432 | } | |
433 | } | |
434 | ||
435 | /* dump the normalized raw arc information: */ | |
436 | ||
437 | for (sym = symtab.base; sym < symtab.limit; ++sym) | |
438 | { | |
439 | for (arc = sym->cg.children; arc; arc = arc->next_child) | |
440 | { | |
441 | put_vma (core_bfd, arc->parent->addr, | |
442 | (bfd_byte *) raw_arc.from_pc); | |
443 | put_vma (core_bfd, arc->child->addr, | |
444 | (bfd_byte *) raw_arc.self_pc); | |
445 | bfd_put_32 (core_bfd, arc->count, (bfd_byte *) raw_arc.count); | |
446 | if (fwrite (&raw_arc, sizeof (raw_arc), 1, ofp) != 1) | |
447 | { | |
448 | perror (filename); | |
449 | done (1); | |
450 | } | |
451 | DBG (SAMPLEDEBUG, | |
452 | printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n", | |
fdcf7d43 ILT |
453 | (unsigned long) arc->parent->addr, |
454 | (unsigned long) arc->child->addr, arc->count)); | |
252b5132 RH |
455 | } |
456 | } | |
457 | fclose (ofp); | |
458 | } | |
459 | else | |
460 | { | |
461 | fprintf (stderr, _("%s: don't know how to deal with file format %d\n"), | |
462 | whoami, file_format); | |
463 | done (1); | |
464 | } | |
465 | } |