Commit | Line | Data |
---|---|---|
ef368dac NC |
1 | /* gmon_io.c - Input and output from/to gmon.out files. |
2 | ||
e7e2dd92 | 3 | Copyright 2000, 2001, 2002 Free Software Foundation, Inc. |
ef368dac NC |
4 | |
5 | This file is part of GNU Binutils. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
21 | \f | |
6d9c411a AM |
22 | #include "gprof.h" |
23 | #include "search_list.h" | |
24 | #include "source.h" | |
25 | #include "symtab.h" | |
252b5132 RH |
26 | #include "cg_arcs.h" |
27 | #include "basic_blocks.h" | |
252b5132 RH |
28 | #include "corefile.h" |
29 | #include "call_graph.h" | |
30 | #include "gmon_io.h" | |
31 | #include "gmon_out.h" | |
ef368dac | 32 | #include "gmon.h" /* Fetch header for old format. */ |
252b5132 RH |
33 | #include "hertz.h" |
34 | #include "hist.h" | |
35 | #include "libiberty.h" | |
36 | ||
e4c79bb2 | 37 | #ifdef BFD_HOST_U_64_BIT |
1355568a AM |
38 | static int gmon_io_read_64 PARAMS ((FILE *, BFD_HOST_U_64_BIT *)); |
39 | static int gmon_io_write_64 PARAMS ((FILE *, BFD_HOST_U_64_BIT)); | |
e4c79bb2 | 40 | #endif |
1355568a AM |
41 | static int gmon_read_raw_arc |
42 | PARAMS ((FILE *, bfd_vma *, bfd_vma *, unsigned long *)); | |
43 | static int gmon_write_raw_arc | |
44 | PARAMS ((FILE *, bfd_vma, bfd_vma, unsigned long)); | |
45 | ||
252b5132 | 46 | int gmon_input = 0; |
ef368dac NC |
47 | int gmon_file_version = 0; /* 0 == old (non-versioned) file format. */ |
48 | ||
0eee5820 | 49 | int |
1355568a AM |
50 | gmon_io_read_32 (ifp, valp) |
51 | FILE *ifp; | |
52 | unsigned int *valp; | |
e7e2dd92 NC |
53 | { |
54 | char buf[4]; | |
55 | ||
56 | if (fread (buf, 1, 4, ifp) != 4) | |
57 | return 1; | |
58 | *valp = bfd_get_32 (core_bfd, buf); | |
59 | return 0; | |
60 | } | |
61 | ||
e4c79bb2 | 62 | #ifdef BFD_HOST_U_64_BIT |
1355568a AM |
63 | static int |
64 | gmon_io_read_64 (ifp, valp) | |
65 | FILE *ifp; | |
66 | BFD_HOST_U_64_BIT *valp; | |
0eee5820 AM |
67 | { |
68 | char buf[8]; | |
0eee5820 | 69 | |
e7e2dd92 NC |
70 | if (fread (buf, 1, 8, ifp) != 8) |
71 | return 1; | |
72 | *valp = bfd_get_64 (core_bfd, buf); | |
73 | return 0; | |
74 | } | |
e4c79bb2 | 75 | #endif |
e7e2dd92 NC |
76 | |
77 | int | |
1355568a AM |
78 | gmon_io_read_vma (ifp, valp) |
79 | FILE *ifp; | |
80 | bfd_vma *valp; | |
e7e2dd92 NC |
81 | { |
82 | unsigned int val32; | |
e4c79bb2 | 83 | #ifdef BFD_HOST_U_64_BIT |
e7e2dd92 | 84 | BFD_HOST_U_64_BIT val64; |
e4c79bb2 | 85 | #endif |
e7e2dd92 NC |
86 | |
87 | switch (bfd_arch_bits_per_address (core_bfd)) | |
0eee5820 | 88 | { |
e7e2dd92 NC |
89 | case 32: |
90 | if (gmon_io_read_32 (ifp, &val32)) | |
0eee5820 | 91 | return 1; |
e7e2dd92 | 92 | *valp = val32; |
0eee5820 AM |
93 | break; |
94 | ||
e4c79bb2 | 95 | #ifdef BFD_HOST_U_64_BIT |
e7e2dd92 NC |
96 | case 64: |
97 | if (gmon_io_read_64 (ifp, &val64)) | |
0eee5820 | 98 | return 1; |
e7e2dd92 | 99 | *valp = val64; |
0eee5820 | 100 | break; |
e4c79bb2 | 101 | #endif |
0eee5820 AM |
102 | |
103 | default: | |
e7e2dd92 NC |
104 | fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"), |
105 | whoami, bfd_arch_bits_per_address (core_bfd)); | |
0eee5820 AM |
106 | done (1); |
107 | } | |
0eee5820 AM |
108 | return 0; |
109 | } | |
252b5132 | 110 | |
0eee5820 | 111 | int |
1355568a AM |
112 | gmon_io_read (ifp, buf, n) |
113 | FILE *ifp; | |
114 | char *buf; | |
115 | size_t n; | |
e7e2dd92 NC |
116 | { |
117 | if (fread (buf, 1, n, ifp) != n) | |
118 | return 1; | |
119 | return 0; | |
120 | } | |
121 | ||
122 | int | |
1355568a AM |
123 | gmon_io_write_32 (ofp, val) |
124 | FILE *ofp; | |
125 | unsigned int val; | |
0eee5820 AM |
126 | { |
127 | char buf[4]; | |
128 | ||
1355568a | 129 | bfd_put_32 (core_bfd, (bfd_vma) val, buf); |
e7e2dd92 | 130 | if (fwrite (buf, 1, 4, ofp) != 4) |
0eee5820 | 131 | return 1; |
0eee5820 AM |
132 | return 0; |
133 | } | |
134 | ||
e4c79bb2 | 135 | #ifdef BFD_HOST_U_64_BIT |
1355568a AM |
136 | static int |
137 | gmon_io_write_64 (ofp, val) | |
138 | FILE *ofp; | |
139 | BFD_HOST_U_64_BIT val; | |
0eee5820 | 140 | { |
e7e2dd92 NC |
141 | char buf[8]; |
142 | ||
1355568a | 143 | bfd_put_64 (core_bfd, (bfd_vma) val, buf); |
e7e2dd92 | 144 | if (fwrite (buf, 1, 8, ofp) != 8) |
0eee5820 AM |
145 | return 1; |
146 | return 0; | |
147 | } | |
e4c79bb2 | 148 | #endif |
0eee5820 AM |
149 | |
150 | int | |
1355568a AM |
151 | gmon_io_write_vma (ofp, val) |
152 | FILE *ofp; | |
153 | bfd_vma val; | |
0eee5820 | 154 | { |
0eee5820 | 155 | |
e7e2dd92 | 156 | switch (bfd_arch_bits_per_address (core_bfd)) |
0eee5820 | 157 | { |
e7e2dd92 NC |
158 | case 32: |
159 | if (gmon_io_write_32 (ofp, (unsigned int) val)) | |
0eee5820 AM |
160 | return 1; |
161 | break; | |
162 | ||
e4c79bb2 | 163 | #ifdef BFD_HOST_U_64_BIT |
e7e2dd92 NC |
164 | case 64: |
165 | if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) val)) | |
0eee5820 AM |
166 | return 1; |
167 | break; | |
e4c79bb2 | 168 | #endif |
0eee5820 AM |
169 | |
170 | default: | |
e7e2dd92 NC |
171 | fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"), |
172 | whoami, bfd_arch_bits_per_address (core_bfd)); | |
0eee5820 AM |
173 | done (1); |
174 | } | |
175 | return 0; | |
176 | } | |
177 | ||
0eee5820 | 178 | int |
1355568a AM |
179 | gmon_io_write_8 (ofp, val) |
180 | FILE *ofp; | |
181 | unsigned int val; | |
0eee5820 AM |
182 | { |
183 | char buf[1]; | |
184 | ||
185 | bfd_put_8 (core_bfd, val, buf); | |
186 | if (fwrite (buf, 1, 1, ofp) != 1) | |
187 | return 1; | |
188 | return 0; | |
189 | } | |
190 | ||
191 | int | |
1355568a AM |
192 | gmon_io_write (ofp, buf, n) |
193 | FILE *ofp; | |
194 | char *buf; | |
195 | size_t n; | |
0eee5820 AM |
196 | { |
197 | if (fwrite (buf, 1, n, ofp) != n) | |
198 | return 1; | |
199 | return 0; | |
200 | } | |
201 | ||
1355568a AM |
202 | static int |
203 | gmon_read_raw_arc (ifp, fpc, spc, cnt) | |
204 | FILE *ifp; | |
205 | bfd_vma *fpc; | |
206 | bfd_vma *spc; | |
207 | unsigned long *cnt; | |
252b5132 | 208 | { |
e4c79bb2 | 209 | #ifdef BFD_HOST_U_64_BIT |
e7e2dd92 | 210 | BFD_HOST_U_64_BIT cnt64; |
e4c79bb2 | 211 | #endif |
e7e2dd92 NC |
212 | unsigned int cnt32; |
213 | ||
214 | if (gmon_io_read_vma (ifp, fpc) | |
215 | || gmon_io_read_vma (ifp, spc)) | |
216 | return 1; | |
217 | ||
218 | switch (bfd_arch_bits_per_address (core_bfd)) | |
252b5132 | 219 | { |
e7e2dd92 NC |
220 | case 32: |
221 | if (gmon_io_read_32 (ifp, &cnt32)) | |
222 | return 1; | |
223 | *cnt = cnt32; | |
224 | break; | |
225 | ||
e4c79bb2 | 226 | #ifdef BFD_HOST_U_64_BIT |
e7e2dd92 NC |
227 | case 64: |
228 | if (gmon_io_read_64 (ifp, &cnt64)) | |
229 | return 1; | |
230 | *cnt = cnt64; | |
231 | break; | |
e4c79bb2 | 232 | #endif |
e7e2dd92 | 233 | |
252b5132 | 234 | default: |
e7e2dd92 NC |
235 | fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"), |
236 | whoami, bfd_arch_bits_per_address (core_bfd)); | |
252b5132 RH |
237 | done (1); |
238 | } | |
e7e2dd92 | 239 | return 0; |
252b5132 RH |
240 | } |
241 | ||
1355568a AM |
242 | static int |
243 | gmon_write_raw_arc (ofp, fpc, spc, cnt) | |
244 | FILE *ofp; | |
245 | bfd_vma fpc; | |
246 | bfd_vma spc; | |
247 | unsigned long cnt; | |
252b5132 | 248 | { |
e7e2dd92 NC |
249 | |
250 | if (gmon_io_write_vma (ofp, fpc) | |
251 | || gmon_io_write_vma (ofp, spc)) | |
252 | return 1; | |
253 | ||
254 | switch (bfd_arch_bits_per_address (core_bfd)) | |
252b5132 | 255 | { |
e7e2dd92 NC |
256 | case 32: |
257 | if (gmon_io_write_32 (ofp, (unsigned int) cnt)) | |
258 | return 1; | |
252b5132 | 259 | break; |
e7e2dd92 | 260 | |
e4c79bb2 | 261 | #ifdef BFD_HOST_U_64_BIT |
e7e2dd92 NC |
262 | case 64: |
263 | if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) cnt)) | |
264 | return 1; | |
252b5132 | 265 | break; |
e4c79bb2 | 266 | #endif |
e7e2dd92 | 267 | |
252b5132 | 268 | default: |
e7e2dd92 NC |
269 | fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"), |
270 | whoami, bfd_arch_bits_per_address (core_bfd)); | |
252b5132 RH |
271 | done (1); |
272 | } | |
e7e2dd92 | 273 | return 0; |
252b5132 RH |
274 | } |
275 | ||
252b5132 | 276 | void |
1355568a AM |
277 | gmon_out_read (filename) |
278 | const char *filename; | |
252b5132 RH |
279 | { |
280 | FILE *ifp; | |
281 | struct gmon_hdr ghdr; | |
282 | unsigned char tag; | |
283 | int nhist = 0, narcs = 0, nbbs = 0; | |
284 | ||
ef368dac | 285 | /* Open gmon.out file. */ |
252b5132 RH |
286 | if (strcmp (filename, "-") == 0) |
287 | { | |
288 | ifp = stdin; | |
5af11cab AM |
289 | #ifdef SET_BINARY |
290 | SET_BINARY (fileno (stdin)); | |
291 | #endif | |
252b5132 RH |
292 | } |
293 | else | |
294 | { | |
295 | ifp = fopen (filename, FOPEN_RB); | |
0eee5820 | 296 | |
252b5132 RH |
297 | if (!ifp) |
298 | { | |
299 | perror (filename); | |
300 | done (1); | |
301 | } | |
302 | } | |
0eee5820 | 303 | |
252b5132 RH |
304 | if (fread (&ghdr, sizeof (struct gmon_hdr), 1, ifp) != 1) |
305 | { | |
306 | fprintf (stderr, _("%s: file too short to be a gmon file\n"), | |
307 | filename); | |
308 | done (1); | |
309 | } | |
310 | ||
0eee5820 AM |
311 | if ((file_format == FF_MAGIC) |
312 | || (file_format == FF_AUTO && !strncmp (&ghdr.cookie[0], GMON_MAGIC, 4))) | |
252b5132 RH |
313 | { |
314 | if (file_format == FF_MAGIC && strncmp (&ghdr.cookie[0], GMON_MAGIC, 4)) | |
315 | { | |
316 | fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"), | |
317 | whoami, filename); | |
318 | done (1); | |
319 | } | |
320 | ||
ef368dac | 321 | /* Right magic, so it's probably really a new gmon.out file. */ |
252b5132 | 322 | gmon_file_version = bfd_get_32 (core_bfd, (bfd_byte *) ghdr.version); |
0eee5820 | 323 | |
252b5132 RH |
324 | if (gmon_file_version != GMON_VERSION && gmon_file_version != 0) |
325 | { | |
326 | fprintf (stderr, | |
327 | _("%s: file `%s' has unsupported version %d\n"), | |
328 | whoami, filename, gmon_file_version); | |
329 | done (1); | |
330 | } | |
331 | ||
ef368dac | 332 | /* Read in all the records. */ |
252b5132 RH |
333 | while (fread (&tag, sizeof (tag), 1, ifp) == 1) |
334 | { | |
335 | switch (tag) | |
336 | { | |
337 | case GMON_TAG_TIME_HIST: | |
338 | ++nhist; | |
339 | gmon_input |= INPUT_HISTOGRAM; | |
340 | hist_read_rec (ifp, filename); | |
341 | break; | |
342 | ||
343 | case GMON_TAG_CG_ARC: | |
344 | ++narcs; | |
345 | gmon_input |= INPUT_CALL_GRAPH; | |
346 | cg_read_rec (ifp, filename); | |
347 | break; | |
348 | ||
349 | case GMON_TAG_BB_COUNT: | |
350 | ++nbbs; | |
351 | gmon_input |= INPUT_BB_COUNTS; | |
352 | bb_read_rec (ifp, filename); | |
353 | break; | |
354 | ||
355 | default: | |
356 | fprintf (stderr, | |
357 | _("%s: %s: found bad tag %d (file corrupted?)\n"), | |
358 | whoami, filename, tag); | |
359 | done (1); | |
360 | } | |
361 | } | |
362 | } | |
363 | else if (file_format == FF_AUTO | |
364 | || file_format == FF_BSD | |
365 | || file_format == FF_BSD44) | |
366 | { | |
367 | struct hdr | |
368 | { | |
369 | bfd_vma low_pc; | |
370 | bfd_vma high_pc; | |
371 | int ncnt; | |
372 | }; | |
e7e2dd92 | 373 | int i, samp_bytes, header_size = 0; |
252b5132 RH |
374 | unsigned long count; |
375 | bfd_vma from_pc, self_pc; | |
252b5132 RH |
376 | static struct hdr h; |
377 | UNIT raw_bin_count; | |
378 | struct hdr tmp; | |
e7e2dd92 | 379 | int version; |
252b5132 | 380 | |
ef368dac | 381 | /* Information from a gmon.out file is in two parts: an array of |
0eee5820 | 382 | sampling hits within pc ranges, and the arcs. */ |
252b5132 RH |
383 | gmon_input = INPUT_HISTOGRAM | INPUT_CALL_GRAPH; |
384 | ||
ef368dac | 385 | /* This fseek() ought to work even on stdin as long as it's |
0eee5820 AM |
386 | not an interactive device (heck, is there anybody who would |
387 | want to type in a gmon.out at the terminal?). */ | |
252b5132 RH |
388 | if (fseek (ifp, 0, SEEK_SET) < 0) |
389 | { | |
390 | perror (filename); | |
391 | done (1); | |
392 | } | |
0eee5820 | 393 | |
e7e2dd92 NC |
394 | /* The beginning of the old BSD header and the 4.4BSD header |
395 | are the same: lowpc, highpc, ncnt */ | |
396 | if (gmon_io_read_vma (ifp, &tmp.low_pc) | |
397 | || gmon_io_read_vma (ifp, &tmp.high_pc) | |
398 | || gmon_io_read_32 (ifp, &tmp.ncnt)) | |
252b5132 | 399 | { |
e7e2dd92 NC |
400 | bad_gmon_file: |
401 | fprintf (stderr, _("%s: file too short to be a gmon file\n"), | |
252b5132 RH |
402 | filename); |
403 | done (1); | |
404 | } | |
0eee5820 | 405 | |
e7e2dd92 NC |
406 | /* Check to see if this a 4.4BSD-style header. */ |
407 | if (gmon_io_read_32 (ifp, &version)) | |
408 | goto bad_gmon_file; | |
252b5132 | 409 | |
e7e2dd92 | 410 | if (version == GMONVERSION) |
252b5132 RH |
411 | { |
412 | int profrate; | |
413 | ||
414 | /* 4.4BSD format header. */ | |
e7e2dd92 NC |
415 | if (gmon_io_read_32 (ifp, &profrate)) |
416 | goto bad_gmon_file; | |
0eee5820 | 417 | |
252b5132 RH |
418 | if (!s_highpc) |
419 | hz = profrate; | |
420 | else if (hz != profrate) | |
421 | { | |
422 | fprintf (stderr, | |
423 | _("%s: profiling rate incompatible with first gmon file\n"), | |
424 | filename); | |
425 | done (1); | |
426 | } | |
427 | ||
e7e2dd92 NC |
428 | switch (bfd_arch_bits_per_address (core_bfd)) |
429 | { | |
430 | case 32: | |
431 | header_size = GMON_HDRSIZE_BSD44_32; | |
432 | break; | |
433 | ||
434 | case 64: | |
435 | header_size = GMON_HDRSIZE_BSD44_64; | |
436 | break; | |
437 | ||
438 | default: | |
439 | fprintf (stderr, | |
440 | _("%s: bits per address has unexpected value of %u\n"), | |
441 | whoami, bfd_arch_bits_per_address (core_bfd)); | |
442 | done (1); | |
443 | } | |
252b5132 RH |
444 | } |
445 | else | |
446 | { | |
ef368dac | 447 | /* Old style BSD format. */ |
252b5132 RH |
448 | if (file_format == FF_BSD44) |
449 | { | |
450 | fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"), | |
451 | whoami, filename); | |
452 | done (1); | |
453 | } | |
454 | ||
e7e2dd92 | 455 | switch (bfd_arch_bits_per_address (core_bfd)) |
252b5132 | 456 | { |
e7e2dd92 NC |
457 | case 32: |
458 | header_size = GMON_HDRSIZE_OLDBSD_32; | |
459 | break; | |
460 | ||
461 | case 64: | |
462 | header_size = GMON_HDRSIZE_OLDBSD_64; | |
463 | break; | |
464 | ||
465 | default: | |
466 | fprintf (stderr, | |
467 | _("%s: bits per address has unexpected value of %u\n"), | |
468 | whoami, bfd_arch_bits_per_address (core_bfd)); | |
469 | done (1); | |
252b5132 | 470 | } |
e7e2dd92 | 471 | } |
252b5132 | 472 | |
e7e2dd92 NC |
473 | /* Position the file to after the header. */ |
474 | if (fseek (ifp, header_size, SEEK_SET) < 0) | |
475 | { | |
476 | perror (filename); | |
477 | done (1); | |
252b5132 RH |
478 | } |
479 | ||
0eee5820 AM |
480 | if (s_highpc && (tmp.low_pc != h.low_pc |
481 | || tmp.high_pc != h.high_pc || tmp.ncnt != h.ncnt)) | |
252b5132 RH |
482 | { |
483 | fprintf (stderr, _("%s: incompatible with first gmon file\n"), | |
484 | filename); | |
485 | done (1); | |
486 | } | |
0eee5820 | 487 | |
252b5132 RH |
488 | h = tmp; |
489 | s_lowpc = (bfd_vma) h.low_pc; | |
490 | s_highpc = (bfd_vma) h.high_pc; | |
491 | lowpc = (bfd_vma) h.low_pc / sizeof (UNIT); | |
492 | highpc = (bfd_vma) h.high_pc / sizeof (UNIT); | |
493 | samp_bytes = h.ncnt - header_size; | |
494 | hist_num_bins = samp_bytes / sizeof (UNIT); | |
0eee5820 | 495 | |
252b5132 RH |
496 | DBG (SAMPLEDEBUG, |
497 | printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n", | |
fdcf7d43 ILT |
498 | (unsigned long) h.low_pc, (unsigned long) h.high_pc, |
499 | h.ncnt); | |
252b5132 | 500 | printf ("[gmon_out_read] s_lowpc 0x%lx s_highpc 0x%lx\n", |
fdcf7d43 | 501 | (unsigned long) s_lowpc, (unsigned long) s_highpc); |
252b5132 | 502 | printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx\n", |
fdcf7d43 | 503 | (unsigned long) lowpc, (unsigned long) highpc); |
252b5132 RH |
504 | printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n", |
505 | samp_bytes, hist_num_bins)); | |
506 | ||
2d35e9f6 NC |
507 | /* Make sure that we have sensible values. */ |
508 | if (samp_bytes < 0 || lowpc > highpc) | |
0eee5820 AM |
509 | { |
510 | fprintf (stderr, | |
2d35e9f6 NC |
511 | _("%s: file '%s' does not appear to be in gmon.out format\n"), |
512 | whoami, filename); | |
0eee5820 AM |
513 | done (1); |
514 | } | |
2d35e9f6 | 515 | |
252b5132 | 516 | if (hist_num_bins) |
ef368dac | 517 | ++nhist; |
252b5132 RH |
518 | |
519 | if (!hist_sample) | |
520 | { | |
521 | hist_sample = | |
522 | (int *) xmalloc (hist_num_bins * sizeof (hist_sample[0])); | |
0eee5820 | 523 | |
252b5132 RH |
524 | memset (hist_sample, 0, hist_num_bins * sizeof (hist_sample[0])); |
525 | } | |
526 | ||
527 | for (i = 0; i < hist_num_bins; ++i) | |
528 | { | |
529 | if (fread (raw_bin_count, sizeof (raw_bin_count), 1, ifp) != 1) | |
530 | { | |
531 | fprintf (stderr, | |
532 | _("%s: unexpected EOF after reading %d/%d bins\n"), | |
533 | whoami, --i, hist_num_bins); | |
534 | done (1); | |
535 | } | |
0eee5820 | 536 | |
252b5132 RH |
537 | hist_sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) raw_bin_count); |
538 | } | |
539 | ||
ef368dac NC |
540 | /* The rest of the file consists of a bunch of |
541 | <from,self,count> tuples. */ | |
e7e2dd92 | 542 | while (gmon_read_raw_arc (ifp, &from_pc, &self_pc, &count) == 0) |
252b5132 RH |
543 | { |
544 | ++narcs; | |
0eee5820 | 545 | |
252b5132 RH |
546 | DBG (SAMPLEDEBUG, |
547 | printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n", | |
fdcf7d43 | 548 | (unsigned long) from_pc, (unsigned long) self_pc, count)); |
0eee5820 | 549 | |
ef368dac | 550 | /* Add this arc. */ |
252b5132 RH |
551 | cg_tally (from_pc, self_pc, count); |
552 | } | |
0eee5820 | 553 | |
252b5132 RH |
554 | fclose (ifp); |
555 | ||
556 | if (hz == HZ_WRONG) | |
557 | { | |
ef368dac NC |
558 | /* How many ticks per second? If we can't tell, report |
559 | time in ticks. */ | |
252b5132 | 560 | hz = hertz (); |
0eee5820 | 561 | |
252b5132 RH |
562 | if (hz == HZ_WRONG) |
563 | { | |
564 | hz = 1; | |
565 | fprintf (stderr, _("time is in ticks, not seconds\n")); | |
566 | } | |
567 | } | |
568 | } | |
569 | else | |
570 | { | |
571 | fprintf (stderr, _("%s: don't know how to deal with file format %d\n"), | |
572 | whoami, file_format); | |
573 | done (1); | |
574 | } | |
575 | ||
576 | if (output_style & STYLE_GMON_INFO) | |
577 | { | |
578 | printf (_("File `%s' (version %d) contains:\n"), | |
579 | filename, gmon_file_version); | |
741247bf NC |
580 | printf (nhist == 1 ? |
581 | _("\t%d histogram record\n") : | |
582 | _("\t%d histogram records\n"), nhist); | |
583 | printf (narcs == 1 ? | |
584 | _("\t%d call-graph record\n") : | |
585 | _("\t%d call-graph records\n"), narcs); | |
586 | printf (nbbs == 1 ? | |
587 | _("\t%d basic-block count record\n") : | |
588 | _("\t%d basic-block count records\n"), nbbs); | |
b34976b6 | 589 | first_output = FALSE; |
252b5132 RH |
590 | } |
591 | } | |
592 | ||
593 | ||
594 | void | |
1355568a AM |
595 | gmon_out_write (filename) |
596 | const char *filename; | |
252b5132 RH |
597 | { |
598 | FILE *ofp; | |
599 | struct gmon_hdr ghdr; | |
600 | ||
601 | ofp = fopen (filename, FOPEN_WB); | |
602 | if (!ofp) | |
603 | { | |
604 | perror (filename); | |
605 | done (1); | |
606 | } | |
607 | ||
608 | if (file_format == FF_AUTO || file_format == FF_MAGIC) | |
609 | { | |
ef368dac | 610 | /* Write gmon header. */ |
252b5132 RH |
611 | |
612 | memcpy (&ghdr.cookie[0], GMON_MAGIC, 4); | |
1355568a | 613 | bfd_put_32 (core_bfd, (bfd_vma) GMON_VERSION, (bfd_byte *) ghdr.version); |
0eee5820 | 614 | |
252b5132 RH |
615 | if (fwrite (&ghdr, sizeof (ghdr), 1, ofp) != 1) |
616 | { | |
617 | perror (filename); | |
618 | done (1); | |
619 | } | |
620 | ||
ef368dac | 621 | /* Write execution time histogram if we have one. */ |
252b5132 | 622 | if (gmon_input & INPUT_HISTOGRAM) |
ef368dac | 623 | hist_write_hist (ofp, filename); |
252b5132 | 624 | |
ef368dac | 625 | /* Write call graph arcs if we have any. */ |
252b5132 | 626 | if (gmon_input & INPUT_CALL_GRAPH) |
ef368dac | 627 | cg_write_arcs (ofp, filename); |
252b5132 | 628 | |
ef368dac | 629 | /* Write basic-block info if we have it. */ |
252b5132 | 630 | if (gmon_input & INPUT_BB_COUNTS) |
ef368dac | 631 | bb_write_blocks (ofp, filename); |
252b5132 RH |
632 | } |
633 | else if (file_format == FF_BSD || file_format == FF_BSD44) | |
634 | { | |
252b5132 | 635 | UNIT raw_bin_count; |
e7e2dd92 NC |
636 | int i, hdrsize; |
637 | unsigned padsize; | |
638 | char pad[3*4]; | |
252b5132 RH |
639 | Arc *arc; |
640 | Sym *sym; | |
641 | ||
e7e2dd92 | 642 | memset (pad, 0, sizeof (pad)); |
252b5132 | 643 | |
e7e2dd92 NC |
644 | hdrsize = 0; |
645 | /* Decide how large the header will be. Use the 4.4BSD format | |
646 | header if explicitly specified, or if the profiling rate is | |
647 | non-standard. Otherwise, use the old BSD format. */ | |
252b5132 | 648 | if (file_format == FF_BSD44 |
e7e2dd92 | 649 | || hz != hertz()) |
252b5132 | 650 | { |
e7e2dd92 NC |
651 | padsize = 3*4; |
652 | switch (bfd_arch_bits_per_address (core_bfd)) | |
252b5132 | 653 | { |
e7e2dd92 NC |
654 | case 32: |
655 | hdrsize = GMON_HDRSIZE_BSD44_32; | |
656 | break; | |
657 | ||
658 | case 64: | |
659 | hdrsize = GMON_HDRSIZE_BSD44_64; | |
660 | break; | |
661 | ||
662 | default: | |
663 | fprintf (stderr, | |
664 | _("%s: bits per address has unexpected value of %u\n"), | |
665 | whoami, bfd_arch_bits_per_address (core_bfd)); | |
666 | done (1); | |
252b5132 RH |
667 | } |
668 | } | |
669 | else | |
670 | { | |
e7e2dd92 NC |
671 | padsize = 0; |
672 | switch (bfd_arch_bits_per_address (core_bfd)) | |
673 | { | |
674 | case 32: | |
675 | hdrsize = GMON_HDRSIZE_OLDBSD_32; | |
676 | break; | |
677 | ||
678 | case 64: | |
679 | hdrsize = GMON_HDRSIZE_OLDBSD_64; | |
680 | /* FIXME: Checking host compiler defines here means that we can't | |
681 | use a cross gprof alpha OSF. */ | |
682 | #if defined(__alpha__) && defined (__osf__) | |
683 | padsize = 4; | |
684 | #endif | |
685 | break; | |
686 | ||
687 | default: | |
688 | fprintf (stderr, | |
689 | _("%s: bits per address has unexpected value of %u\n"), | |
690 | whoami, bfd_arch_bits_per_address (core_bfd)); | |
691 | done (1); | |
692 | } | |
693 | } | |
694 | ||
695 | /* Write the parts of the headers that are common to both the | |
696 | old BSD and 4.4BSD formats. */ | |
697 | if (gmon_io_write_vma (ofp, s_lowpc) | |
698 | || gmon_io_write_vma (ofp, s_highpc) | |
699 | || gmon_io_write_32 (ofp, hist_num_bins * sizeof (UNIT) + hdrsize)) | |
700 | { | |
701 | perror (filename); | |
702 | done (1); | |
703 | } | |
704 | ||
705 | /* Write out the 4.4BSD header bits, if that's what we're using. */ | |
706 | if (file_format == FF_BSD44 | |
707 | || hz != hertz()) | |
708 | { | |
709 | if (gmon_io_write_32 (ofp, GMONVERSION) | |
1355568a | 710 | || gmon_io_write_32 (ofp, (unsigned int) hz)) |
252b5132 RH |
711 | { |
712 | perror (filename); | |
713 | done (1); | |
714 | } | |
715 | } | |
716 | ||
e7e2dd92 NC |
717 | /* Now write out any necessary padding after the meaningful |
718 | header bits. */ | |
719 | if (padsize != 0 | |
720 | && fwrite (pad, 1, padsize, ofp) != padsize) | |
721 | { | |
722 | perror (filename); | |
723 | done (1); | |
724 | } | |
725 | ||
ef368dac | 726 | /* Dump the samples. */ |
252b5132 RH |
727 | for (i = 0; i < hist_num_bins; ++i) |
728 | { | |
1355568a AM |
729 | bfd_put_16 (core_bfd, (bfd_vma) hist_sample[i], |
730 | (bfd_byte *) &raw_bin_count[0]); | |
252b5132 RH |
731 | if (fwrite (&raw_bin_count[0], sizeof (raw_bin_count), 1, ofp) != 1) |
732 | { | |
733 | perror (filename); | |
734 | done (1); | |
735 | } | |
736 | } | |
737 | ||
ef368dac | 738 | /* Dump the normalized raw arc information. */ |
252b5132 RH |
739 | for (sym = symtab.base; sym < symtab.limit; ++sym) |
740 | { | |
741 | for (arc = sym->cg.children; arc; arc = arc->next_child) | |
742 | { | |
e7e2dd92 NC |
743 | if (gmon_write_raw_arc (ofp, arc->parent->addr, |
744 | arc->child->addr, arc->count)) | |
252b5132 RH |
745 | { |
746 | perror (filename); | |
747 | done (1); | |
748 | } | |
749 | DBG (SAMPLEDEBUG, | |
750 | printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n", | |
fdcf7d43 ILT |
751 | (unsigned long) arc->parent->addr, |
752 | (unsigned long) arc->child->addr, arc->count)); | |
252b5132 RH |
753 | } |
754 | } | |
0eee5820 | 755 | |
252b5132 RH |
756 | fclose (ofp); |
757 | } | |
758 | else | |
759 | { | |
760 | fprintf (stderr, _("%s: don't know how to deal with file format %d\n"), | |
761 | whoami, file_format); | |
762 | done (1); | |
763 | } | |
764 | } |