1 /* gmon_io.c - Input and output from/to gmon.out files.
3 Copyright 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
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.
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.
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
23 #include "basic_blocks.h"
26 #include "call_graph.h"
29 #include "gmon.h" /* Fetch header for old format. */
33 #include "libiberty.h"
36 int gmon_file_version
= 0; /* 0 == old (non-versioned) file format. */
39 DEFUN (gmon_io_read_vma
, (ifp
, valp
), FILE * ifp AND bfd_vma
*valp
)
44 switch (GMON_PTR_SIZE
)
47 if (fread (buf
, 1, 4, ifp
) != 4)
49 val
= bfd_get_32 (core_bfd
, buf
);
53 if (fread (buf
, 1, 8, ifp
) != 8)
55 val
= bfd_get_64 (core_bfd
, buf
);
59 fprintf (stderr
, _("%s: GMON_PTR_SIZE has unexpected value of %u\n"),
60 whoami
, GMON_PTR_SIZE
);
68 DEFUN (gmon_io_read_32
, (ifp
, valp
), FILE * ifp AND
unsigned int *valp
)
72 if (fread (buf
, 1, 4, ifp
) != 4)
74 *valp
= bfd_get_32 (core_bfd
, buf
);
79 DEFUN (gmon_io_read
, (ifp
, buf
, n
), FILE * ifp AND
char *buf AND
size_t n
)
81 if (fread (buf
, 1, n
, ifp
) != n
)
87 DEFUN (gmon_io_write_vma
, (ofp
, val
), FILE * ofp AND bfd_vma val
)
91 switch (GMON_PTR_SIZE
)
94 bfd_put_32 (core_bfd
, val
, buf
);
95 if (fwrite (buf
, 1, 4, ofp
) != 4)
100 bfd_put_64 (core_bfd
, val
, buf
);
101 if (fwrite (buf
, 1, 8, ofp
) != 8)
106 fprintf (stderr
, _("%s: GMON_PTR_SIZE has unexpected value of %u\n"),
107 whoami
, GMON_PTR_SIZE
);
114 DEFUN (gmon_io_write_32
, (ofp
, val
), FILE * ofp AND
unsigned int val
)
118 bfd_put_32 (core_bfd
, val
, buf
);
119 if (fwrite (buf
, 1, 4, ofp
) != 4)
125 DEFUN (gmon_io_write_8
, (ofp
, val
), FILE * ofp AND
unsigned char val
)
129 bfd_put_8 (core_bfd
, val
, buf
);
130 if (fwrite (buf
, 1, 1, ofp
) != 1)
136 DEFUN (gmon_io_write
, (ofp
, buf
, n
), FILE * ofp AND
char *buf AND
size_t n
)
138 if (fwrite (buf
, 1, n
, ofp
) != n
)
143 /* get_vma and put_vma are for backwards compatibility only */
145 DEFUN (get_vma
, (abfd
, addr
), bfd
* abfd AND bfd_byte
* addr
)
147 switch (sizeof (char*))
150 return bfd_get_32 (abfd
, addr
);
152 return bfd_get_64 (abfd
, addr
);
154 fprintf (stderr
, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
155 whoami
, (long) sizeof (char*));
161 DEFUN (put_vma
, (abfd
, val
, addr
), bfd
* abfd AND bfd_vma val AND bfd_byte
* addr
)
163 switch (sizeof (char*))
166 bfd_put_32 (abfd
, val
, addr
);
169 bfd_put_64 (abfd
, val
, addr
);
172 fprintf (stderr
, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
173 whoami
, (long) sizeof (char*));
179 DEFUN (gmon_out_read
, (filename
), const char *filename
)
182 struct gmon_hdr ghdr
;
184 int nhist
= 0, narcs
= 0, nbbs
= 0;
186 /* Open gmon.out file. */
187 if (strcmp (filename
, "-") == 0)
191 SET_BINARY (fileno (stdin
));
196 ifp
= fopen (filename
, FOPEN_RB
);
205 if (fread (&ghdr
, sizeof (struct gmon_hdr
), 1, ifp
) != 1)
207 fprintf (stderr
, _("%s: file too short to be a gmon file\n"),
212 if ((file_format
== FF_MAGIC
)
213 || (file_format
== FF_AUTO
&& !strncmp (&ghdr
.cookie
[0], GMON_MAGIC
, 4)))
215 if (file_format
== FF_MAGIC
&& strncmp (&ghdr
.cookie
[0], GMON_MAGIC
, 4))
217 fprintf (stderr
, _("%s: file `%s' has bad magic cookie\n"),
222 /* Right magic, so it's probably really a new gmon.out file. */
223 gmon_file_version
= bfd_get_32 (core_bfd
, (bfd_byte
*) ghdr
.version
);
225 if (gmon_file_version
!= GMON_VERSION
&& gmon_file_version
!= 0)
228 _("%s: file `%s' has unsupported version %d\n"),
229 whoami
, filename
, gmon_file_version
);
233 /* Read in all the records. */
234 while (fread (&tag
, sizeof (tag
), 1, ifp
) == 1)
238 case GMON_TAG_TIME_HIST
:
240 gmon_input
|= INPUT_HISTOGRAM
;
241 hist_read_rec (ifp
, filename
);
244 case GMON_TAG_CG_ARC
:
246 gmon_input
|= INPUT_CALL_GRAPH
;
247 cg_read_rec (ifp
, filename
);
250 case GMON_TAG_BB_COUNT
:
252 gmon_input
|= INPUT_BB_COUNTS
;
253 bb_read_rec (ifp
, filename
);
258 _("%s: %s: found bad tag %d (file corrupted?)\n"),
259 whoami
, filename
, tag
);
264 else if (file_format
== FF_AUTO
265 || file_format
== FF_BSD
266 || file_format
== FF_BSD44
)
274 int i
, samp_bytes
, header_size
;
276 bfd_vma from_pc
, self_pc
;
277 struct raw_arc raw_arc
;
283 /* Information from a gmon.out file is in two parts: an array of
284 sampling hits within pc ranges, and the arcs. */
285 gmon_input
= INPUT_HISTOGRAM
| INPUT_CALL_GRAPH
;
287 /* This fseek() ought to work even on stdin as long as it's
288 not an interactive device (heck, is there anybody who would
289 want to type in a gmon.out at the terminal?). */
290 if (fseek (ifp
, 0, SEEK_SET
) < 0)
296 if (fread (&raw
, 1, sizeof (struct raw_phdr
), ifp
)
297 != sizeof (struct raw_phdr
))
299 fprintf (stderr
, _("%s: file too short to be a gmon file\n"),
304 tmp
.low_pc
= get_vma (core_bfd
, (bfd_byte
*) &raw
.low_pc
[0]);
305 tmp
.high_pc
= get_vma (core_bfd
, (bfd_byte
*) &raw
.high_pc
[0]);
306 tmp
.ncnt
= bfd_get_32 (core_bfd
, (bfd_byte
*) &raw
.ncnt
[0]);
308 if (bfd_get_32 (core_bfd
, (bfd_byte
*) &raw
.version
[0])
313 /* 4.4BSD format header. */
314 profrate
= bfd_get_32 (core_bfd
, (bfd_byte
*) &raw
.profrate
[0]);
318 else if (hz
!= profrate
)
321 _("%s: profiling rate incompatible with first gmon file\n"),
326 header_size
= sizeof (struct raw_phdr
);
330 /* Old style BSD format. */
331 if (file_format
== FF_BSD44
)
333 fprintf (stderr
, _("%s: file `%s' has bad magic cookie\n"),
338 if (fseek (ifp
, sizeof (struct old_raw_phdr
), SEEK_SET
) < 0)
344 header_size
= sizeof (struct old_raw_phdr
);
347 if (s_highpc
&& (tmp
.low_pc
!= h
.low_pc
348 || tmp
.high_pc
!= h
.high_pc
|| tmp
.ncnt
!= h
.ncnt
))
350 fprintf (stderr
, _("%s: incompatible with first gmon file\n"),
356 s_lowpc
= (bfd_vma
) h
.low_pc
;
357 s_highpc
= (bfd_vma
) h
.high_pc
;
358 lowpc
= (bfd_vma
) h
.low_pc
/ sizeof (UNIT
);
359 highpc
= (bfd_vma
) h
.high_pc
/ sizeof (UNIT
);
360 samp_bytes
= h
.ncnt
- header_size
;
361 hist_num_bins
= samp_bytes
/ sizeof (UNIT
);
364 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
365 (unsigned long) h
.low_pc
, (unsigned long) h
.high_pc
,
367 printf ("[gmon_out_read] s_lowpc 0x%lx s_highpc 0x%lx\n",
368 (unsigned long) s_lowpc
, (unsigned long) s_highpc
);
369 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx\n",
370 (unsigned long) lowpc
, (unsigned long) highpc
);
371 printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
372 samp_bytes
, hist_num_bins
));
374 /* Make sure that we have sensible values. */
375 if (samp_bytes
< 0 || lowpc
> highpc
)
378 _("%s: file '%s' does not appear to be in gmon.out format\n"),
389 (int *) xmalloc (hist_num_bins
* sizeof (hist_sample
[0]));
391 memset (hist_sample
, 0, hist_num_bins
* sizeof (hist_sample
[0]));
394 for (i
= 0; i
< hist_num_bins
; ++i
)
396 if (fread (raw_bin_count
, sizeof (raw_bin_count
), 1, ifp
) != 1)
399 _("%s: unexpected EOF after reading %d/%d bins\n"),
400 whoami
, --i
, hist_num_bins
);
404 hist_sample
[i
] += bfd_get_16 (core_bfd
, (bfd_byte
*) raw_bin_count
);
407 /* The rest of the file consists of a bunch of
408 <from,self,count> tuples. */
409 while (fread (&raw_arc
, sizeof (raw_arc
), 1, ifp
) == 1)
412 from_pc
= get_vma (core_bfd
, (bfd_byte
*) raw_arc
.from_pc
);
413 self_pc
= get_vma (core_bfd
, (bfd_byte
*) raw_arc
.self_pc
);
414 count
= bfd_get_32 (core_bfd
, (bfd_byte
*) raw_arc
.count
);
417 printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
418 (unsigned long) from_pc
, (unsigned long) self_pc
, count
));
421 cg_tally (from_pc
, self_pc
, count
);
428 /* How many ticks per second? If we can't tell, report
435 fprintf (stderr
, _("time is in ticks, not seconds\n"));
441 fprintf (stderr
, _("%s: don't know how to deal with file format %d\n"),
442 whoami
, file_format
);
446 if (output_style
& STYLE_GMON_INFO
)
448 printf (_("File `%s' (version %d) contains:\n"),
449 filename
, gmon_file_version
);
450 printf (_("\t%d histogram record%s\n"),
451 nhist
, nhist
== 1 ? "" : "s");
452 printf (_("\t%d call-graph record%s\n"),
453 narcs
, narcs
== 1 ? "" : "s");
454 printf (_("\t%d basic-block count record%s\n"),
455 nbbs
, nbbs
== 1 ? "" : "s");
456 first_output
= FALSE
;
462 DEFUN (gmon_out_write
, (filename
), const char *filename
)
465 struct gmon_hdr ghdr
;
467 ofp
= fopen (filename
, FOPEN_WB
);
474 if (file_format
== FF_AUTO
|| file_format
== FF_MAGIC
)
476 /* Write gmon header. */
478 memcpy (&ghdr
.cookie
[0], GMON_MAGIC
, 4);
479 bfd_put_32 (core_bfd
, GMON_VERSION
, (bfd_byte
*) ghdr
.version
);
481 if (fwrite (&ghdr
, sizeof (ghdr
), 1, ofp
) != 1)
487 /* Write execution time histogram if we have one. */
488 if (gmon_input
& INPUT_HISTOGRAM
)
489 hist_write_hist (ofp
, filename
);
491 /* Write call graph arcs if we have any. */
492 if (gmon_input
& INPUT_CALL_GRAPH
)
493 cg_write_arcs (ofp
, filename
);
495 /* Write basic-block info if we have it. */
496 if (gmon_input
& INPUT_BB_COUNTS
)
497 bb_write_blocks (ofp
, filename
);
499 else if (file_format
== FF_BSD
|| file_format
== FF_BSD44
)
501 struct raw_arc raw_arc
;
508 memset (&h
, 0, sizeof h
);
509 put_vma (core_bfd
, s_lowpc
, (bfd_byte
*) &h
.low_pc
);
510 put_vma (core_bfd
, s_highpc
, (bfd_byte
*) &h
.high_pc
);
511 bfd_put_32 (core_bfd
,
512 hist_num_bins
* sizeof (UNIT
) + sizeof (struct raw_phdr
),
513 (bfd_byte
*) &h
.ncnt
);
515 /* Write header. Use new style BSD format is explicitly
516 specified, or if the profiling rate is non-standard;
517 otherwise, use the old BSD format. */
518 if (file_format
== FF_BSD44
521 bfd_put_32 (core_bfd
, GMONVERSION
, (bfd_byte
*) &h
.version
);
522 bfd_put_32 (core_bfd
, hz
, (bfd_byte
*) &h
.profrate
);
523 if (fwrite (&h
, sizeof (struct raw_phdr
), 1, ofp
) != 1)
531 if (fwrite (&h
, sizeof (struct old_raw_phdr
), 1, ofp
) != 1)
538 /* Dump the samples. */
539 for (i
= 0; i
< hist_num_bins
; ++i
)
541 bfd_put_16 (core_bfd
, hist_sample
[i
], (bfd_byte
*) & raw_bin_count
[0]);
542 if (fwrite (&raw_bin_count
[0], sizeof (raw_bin_count
), 1, ofp
) != 1)
549 /* Dump the normalized raw arc information. */
550 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
552 for (arc
= sym
->cg
.children
; arc
; arc
= arc
->next_child
)
554 put_vma (core_bfd
, arc
->parent
->addr
,
555 (bfd_byte
*) raw_arc
.from_pc
);
556 put_vma (core_bfd
, arc
->child
->addr
,
557 (bfd_byte
*) raw_arc
.self_pc
);
558 bfd_put_32 (core_bfd
, arc
->count
, (bfd_byte
*) raw_arc
.count
);
559 if (fwrite (&raw_arc
, sizeof (raw_arc
), 1, ofp
) != 1)
565 printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
566 (unsigned long) arc
->parent
->addr
,
567 (unsigned long) arc
->child
->addr
, arc
->count
));
575 fprintf (stderr
, _("%s: don't know how to deal with file format %d\n"),
576 whoami
, file_format
);