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