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