]>
Commit | Line | Data |
---|---|---|
5489fcc3 KR |
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 "core.h" | |
8 | #include "call_graph.h" | |
9 | #include "gmon_io.h" | |
10 | #include "gmon_out.h" | |
12516a37 | 11 | #include "gmon.h" /* fetch header for old format */ |
5489fcc3 KR |
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 | |
12516a37 | 24 | DEFUN (get_vma, (abfd, addr), bfd * abfd AND bfd_byte * addr) |
5489fcc3 | 25 | { |
03c35bcb | 26 | switch (sizeof (char*)) |
12516a37 KR |
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", | |
03c35bcb | 34 | whoami, (long) sizeof (char*)); |
12516a37 | 35 | done (1); |
03c35bcb KR |
36 | } |
37 | } | |
5489fcc3 KR |
38 | |
39 | ||
40 | /* | |
41 | * This probably ought to be in libbfd. | |
42 | */ | |
43 | void | |
12516a37 | 44 | DEFUN (put_vma, (abfd, val, addr), bfd * abfd AND bfd_vma val AND bfd_byte * addr) |
5489fcc3 | 45 | { |
03c35bcb | 46 | switch (sizeof (char*)) |
12516a37 KR |
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", | |
03c35bcb | 56 | whoami, (long) sizeof (char*)); |
12516a37 | 57 | done (1); |
03c35bcb KR |
58 | } |
59 | } | |
5489fcc3 KR |
60 | |
61 | ||
62 | void | |
12516a37 | 63 | DEFUN (gmon_out_read, (filename), const char *filename) |
5489fcc3 | 64 | { |
12516a37 KR |
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); | |
03c35bcb KR |
83 | } |
84 | } | |
12516a37 KR |
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); | |
03c35bcb | 90 | } |
12516a37 KR |
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); | |
03c35bcb | 100 | } |
12516a37 KR |
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); | |
03c35bcb | 111 | } |
12516a37 KR |
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); | |
03c35bcb KR |
141 | } |
142 | } | |
12516a37 KR |
143 | } |
144 | else if (file_format == FF_AUTO || file_format == FF_BSD) | |
5489fcc3 | 145 | { |
12516a37 KR |
146 | struct hdr |
147 | { | |
148 | bfd_vma low_pc; | |
149 | bfd_vma high_pc; | |
150 | int ncnt; | |
151 | }; | |
152 | int i, samp_bytes, count; | |
153 | bfd_vma from_pc, self_pc; | |
154 | struct raw_arc raw_arc; | |
155 | struct raw_phdr raw; | |
156 | static struct hdr h; | |
157 | UNIT raw_bin_count; | |
158 | struct hdr tmp; | |
159 | ||
160 | /* | |
161 | * Information from a gmon.out file is in two parts: an array of | |
162 | * sampling hits within pc ranges, and the arcs. | |
163 | */ | |
164 | gmon_input = INPUT_HISTOGRAM | INPUT_CALL_GRAPH; | |
165 | ||
166 | /* | |
167 | * This fseek() ought to work even on stdin as long as it's | |
168 | * not an interactive device (heck, is there anybody who would | |
169 | * want to type in a gmon.out at the terminal?). | |
170 | */ | |
171 | if (fseek (ifp, 0, SEEK_SET) < 0) | |
5489fcc3 | 172 | { |
12516a37 KR |
173 | perror (filename); |
174 | done (1); | |
03c35bcb | 175 | } |
12516a37 KR |
176 | if (fread (&raw, 1, sizeof (struct raw_phdr), ifp) |
177 | != sizeof (struct raw_phdr)) | |
5489fcc3 | 178 | { |
12516a37 KR |
179 | fprintf (stderr, "%s: file too short to be a gmon file\n", |
180 | filename); | |
181 | done (1); | |
03c35bcb | 182 | } |
12516a37 KR |
183 | tmp.low_pc = get_vma (core_bfd, (bfd_byte *) & raw.low_pc[0]); |
184 | tmp.high_pc = get_vma (core_bfd, (bfd_byte *) & raw.high_pc[0]); | |
185 | tmp.ncnt = bfd_get_32 (core_bfd, (bfd_byte *) & raw.ncnt[0]); | |
186 | if (s_highpc && (tmp.low_pc != h.low_pc || | |
187 | tmp.high_pc != h.high_pc || tmp.ncnt != h.ncnt)) | |
5489fcc3 | 188 | { |
12516a37 KR |
189 | fprintf (stderr, "%s: incompatible with first gmon file\n", |
190 | filename); | |
191 | done (1); | |
03c35bcb | 192 | } |
12516a37 KR |
193 | h = tmp; |
194 | s_lowpc = (bfd_vma) h.low_pc; | |
195 | s_highpc = (bfd_vma) h.high_pc; | |
196 | lowpc = (bfd_vma) h.low_pc / sizeof (UNIT); | |
197 | highpc = (bfd_vma) h.high_pc / sizeof (UNIT); | |
198 | samp_bytes = h.ncnt - sizeof (struct raw_phdr); | |
199 | hist_num_bins = samp_bytes / sizeof (UNIT); | |
200 | DBG (SAMPLEDEBUG, | |
201 | printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n", | |
5489fcc3 | 202 | h.low_pc, h.high_pc, h.ncnt); |
12516a37 | 203 | printf ("[gmon_out_read] s_lowpc 0x%lx s_highpc 0x%lx\n", |
5489fcc3 | 204 | s_lowpc, s_highpc); |
12516a37 | 205 | printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx\n", |
5489fcc3 | 206 | lowpc, highpc); |
12516a37 | 207 | printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n", |
5489fcc3 KR |
208 | samp_bytes, hist_num_bins)); |
209 | ||
12516a37 KR |
210 | if (hist_num_bins) |
211 | { | |
212 | ++nhist; | |
03c35bcb | 213 | } |
12516a37 KR |
214 | |
215 | if (!hist_sample) | |
216 | { | |
217 | hist_sample = | |
218 | (int *) xmalloc (hist_num_bins * sizeof (hist_sample[0])); | |
219 | memset (hist_sample, 0, hist_num_bins * sizeof (hist_sample[0])); | |
03c35bcb | 220 | } |
12516a37 KR |
221 | |
222 | for (i = 0; i < hist_num_bins; ++i) | |
223 | { | |
224 | if (fread (raw_bin_count, sizeof (raw_bin_count), 1, ifp) != 1) | |
225 | { | |
226 | fprintf (stderr, | |
227 | "%s: unexpected EOF after reading %d/%d bins\n", | |
228 | whoami, --i, hist_num_bins); | |
229 | done (1); | |
03c35bcb | 230 | } |
12516a37 | 231 | hist_sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) raw_bin_count); |
03c35bcb | 232 | } |
12516a37 KR |
233 | |
234 | /* | |
235 | * The rest of the file consists of a bunch of <from,self,count> | |
236 | * tuples: | |
237 | */ | |
238 | while (fread (&raw_arc, sizeof (raw_arc), 1, ifp) == 1) | |
239 | { | |
240 | ++narcs; | |
241 | from_pc = get_vma (core_bfd, (bfd_byte *) raw_arc.from_pc); | |
242 | self_pc = get_vma (core_bfd, (bfd_byte *) raw_arc.self_pc); | |
243 | count = bfd_get_32 (core_bfd, (bfd_byte *) raw_arc.count); | |
244 | DBG (SAMPLEDEBUG, | |
245 | printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %d\n", | |
246 | from_pc, self_pc, count)); | |
247 | /* add this arc: */ | |
248 | cg_tally (from_pc, self_pc, count); | |
03c35bcb | 249 | } |
12516a37 KR |
250 | fclose (ifp); |
251 | ||
252 | if (hz == HZ_WRONG) | |
253 | { | |
254 | /* | |
255 | * How many ticks per second? If we can't tell, report | |
256 | * time in ticks. | |
257 | */ | |
258 | hz = hertz (); | |
259 | if (hz == HZ_WRONG) | |
260 | { | |
261 | hz = 1; | |
262 | fprintf (stderr, "time is in ticks, not seconds\n"); | |
03c35bcb KR |
263 | } |
264 | } | |
12516a37 KR |
265 | } |
266 | else | |
267 | { | |
268 | fprintf (stderr, "%s: don't know how to deal with file format %d\n", | |
269 | whoami, file_format); | |
270 | done (1); | |
03c35bcb | 271 | } |
12516a37 KR |
272 | |
273 | if (output_style & STYLE_GMON_INFO) | |
274 | { | |
275 | printf ("File `%s' (version %d) contains:\n", | |
276 | filename, gmon_file_version); | |
277 | printf ("\t%d histogram record%s\n", | |
278 | nhist, nhist == 1 ? "" : "s"); | |
279 | printf ("\t%d call-graph record%s\n", | |
280 | narcs, narcs == 1 ? "" : "s"); | |
281 | printf ("\t%d basic-block count record%s\n", | |
282 | nbbs, nbbs == 1 ? "" : "s"); | |
283 | first_output = FALSE; | |
03c35bcb KR |
284 | } |
285 | } | |
5489fcc3 KR |
286 | |
287 | ||
288 | void | |
12516a37 | 289 | DEFUN (gmon_out_write, (filename), const char *filename) |
5489fcc3 | 290 | { |
12516a37 KR |
291 | FILE *ofp; |
292 | struct gmon_hdr ghdr; | |
293 | ||
294 | ofp = fopen (filename, FOPEN_WB); | |
295 | if (!ofp) | |
296 | { | |
297 | perror (filename); | |
298 | done (1); | |
03c35bcb | 299 | } |
12516a37 KR |
300 | |
301 | if (file_format == FF_AUTO || file_format == FF_MAGIC) | |
302 | { | |
303 | /* write gmon header: */ | |
304 | ||
305 | memcpy (&ghdr.cookie[0], GMON_MAGIC, 4); | |
306 | bfd_put_32 (core_bfd, GMON_VERSION, (bfd_byte *) ghdr.version); | |
307 | if (fwrite (&ghdr, sizeof (ghdr), 1, ofp) != 1) | |
308 | { | |
309 | perror (filename); | |
310 | done (1); | |
03c35bcb | 311 | } |
12516a37 KR |
312 | |
313 | /* write execution time histogram if we have one: */ | |
314 | if (gmon_input & INPUT_HISTOGRAM) | |
315 | { | |
316 | hist_write_hist (ofp, filename); | |
03c35bcb | 317 | } |
12516a37 KR |
318 | |
319 | /* write call graph arcs if we have any: */ | |
320 | if (gmon_input & INPUT_CALL_GRAPH) | |
321 | { | |
322 | cg_write_arcs (ofp, filename); | |
03c35bcb | 323 | } |
12516a37 KR |
324 | |
325 | /* write basic-block info if we have it: */ | |
326 | if (gmon_input & INPUT_BB_COUNTS) | |
327 | { | |
328 | bb_write_blocks (ofp, filename); | |
03c35bcb | 329 | } |
12516a37 KR |
330 | } |
331 | else if (file_format == FF_BSD) | |
332 | { | |
333 | struct raw_arc raw_arc; | |
334 | UNIT raw_bin_count; | |
335 | bfd_vma lpc, hpc; | |
336 | int i, ncnt; | |
337 | Arc *arc; | |
338 | Sym *sym; | |
339 | ||
340 | put_vma (core_bfd, s_lowpc, (bfd_byte *) & lpc); | |
341 | put_vma (core_bfd, s_highpc, (bfd_byte *) & hpc); | |
342 | bfd_put_32 (core_bfd, | |
343 | hist_num_bins * sizeof (UNIT) + sizeof (struct raw_phdr), | |
344 | (bfd_byte *) & ncnt); | |
345 | ||
346 | /* write header: */ | |
347 | if (fwrite (&lpc, sizeof (lpc), 1, ofp) != 1 | |
348 | || fwrite (&hpc, sizeof (hpc), 1, ofp) != 1 | |
349 | || fwrite (&ncnt, sizeof (ncnt), 1, ofp) != 1) | |
350 | { | |
351 | perror (filename); | |
352 | done (1); | |
03c35bcb | 353 | } |
12516a37 KR |
354 | |
355 | /* dump the samples: */ | |
356 | ||
357 | for (i = 0; i < hist_num_bins; ++i) | |
5489fcc3 | 358 | { |
12516a37 KR |
359 | bfd_put_16 (core_bfd, hist_sample[i], (bfd_byte *) & raw_bin_count[0]); |
360 | if (fwrite (&raw_bin_count[0], sizeof (raw_bin_count), 1, ofp) != 1) | |
361 | { | |
362 | perror (filename); | |
363 | done (1); | |
03c35bcb KR |
364 | } |
365 | } | |
5489fcc3 | 366 | |
12516a37 | 367 | /* dump the normalized raw arc information: */ |
5489fcc3 | 368 | |
12516a37 KR |
369 | for (sym = symtab.base; sym < symtab.limit; ++sym) |
370 | { | |
371 | for (arc = sym->cg.children; arc; arc = arc->next_child) | |
5489fcc3 | 372 | { |
12516a37 KR |
373 | put_vma (core_bfd, arc->parent->addr, |
374 | (bfd_byte *) raw_arc.from_pc); | |
375 | put_vma (core_bfd, arc->child->addr, | |
376 | (bfd_byte *) raw_arc.self_pc); | |
377 | bfd_put_32 (core_bfd, arc->count, (bfd_byte *) raw_arc.count); | |
378 | if (fwrite (&raw_arc, sizeof (raw_arc), 1, ofp) != 1) | |
379 | { | |
380 | perror (filename); | |
381 | done (1); | |
03c35bcb | 382 | } |
12516a37 KR |
383 | DBG (SAMPLEDEBUG, |
384 | printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %d\n", | |
5489fcc3 | 385 | arc->parent->addr, arc->child->addr, arc->count)); |
03c35bcb KR |
386 | } |
387 | } | |
12516a37 KR |
388 | fclose (ofp); |
389 | } | |
390 | else | |
391 | { | |
392 | fprintf (stderr, "%s: don't know how to deal with file format %d\n", | |
393 | whoami, file_format); | |
394 | done (1); | |
03c35bcb KR |
395 | } |
396 | } |