1 /* gmon_io.c - Input and output from/to gmon.out files.
3 Copyright 2000, 2001, 2002 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 "search_list.h"
27 #include "basic_blocks.h"
29 #include "call_graph.h"
32 #include "gmon.h" /* Fetch header for old format. */
35 #include "libiberty.h"
37 static int gmon_io_read_64
PARAMS ((FILE *, BFD_HOST_U_64_BIT
*));
38 static int gmon_io_write_64
PARAMS ((FILE *, BFD_HOST_U_64_BIT
));
39 static int gmon_read_raw_arc
40 PARAMS ((FILE *, bfd_vma
*, bfd_vma
*, unsigned long *));
41 static int gmon_write_raw_arc
42 PARAMS ((FILE *, bfd_vma
, bfd_vma
, unsigned long));
45 int gmon_file_version
= 0; /* 0 == old (non-versioned) file format. */
48 gmon_io_read_32 (ifp
, valp
)
54 if (fread (buf
, 1, 4, ifp
) != 4)
56 *valp
= bfd_get_32 (core_bfd
, buf
);
61 gmon_io_read_64 (ifp
, valp
)
63 BFD_HOST_U_64_BIT
*valp
;
67 if (fread (buf
, 1, 8, ifp
) != 8)
69 *valp
= bfd_get_64 (core_bfd
, buf
);
74 gmon_io_read_vma (ifp
, valp
)
79 BFD_HOST_U_64_BIT val64
;
81 switch (bfd_arch_bits_per_address (core_bfd
))
84 if (gmon_io_read_32 (ifp
, &val32
))
90 if (gmon_io_read_64 (ifp
, &val64
))
96 fprintf (stderr
, _("%s: bits per address has unexpected value of %u\n"),
97 whoami
, bfd_arch_bits_per_address (core_bfd
));
104 gmon_io_read (ifp
, buf
, n
)
109 if (fread (buf
, 1, n
, ifp
) != n
)
115 gmon_io_write_32 (ofp
, val
)
121 bfd_put_32 (core_bfd
, (bfd_vma
) val
, buf
);
122 if (fwrite (buf
, 1, 4, ofp
) != 4)
128 gmon_io_write_64 (ofp
, val
)
130 BFD_HOST_U_64_BIT val
;
134 bfd_put_64 (core_bfd
, (bfd_vma
) val
, buf
);
135 if (fwrite (buf
, 1, 8, ofp
) != 8)
141 gmon_io_write_vma (ofp
, val
)
146 switch (bfd_arch_bits_per_address (core_bfd
))
149 if (gmon_io_write_32 (ofp
, (unsigned int) val
))
154 if (gmon_io_write_64 (ofp
, (BFD_HOST_U_64_BIT
) val
))
159 fprintf (stderr
, _("%s: bits per address has unexpected value of %u\n"),
160 whoami
, bfd_arch_bits_per_address (core_bfd
));
167 gmon_io_write_8 (ofp
, val
)
173 bfd_put_8 (core_bfd
, val
, buf
);
174 if (fwrite (buf
, 1, 1, ofp
) != 1)
180 gmon_io_write (ofp
, buf
, n
)
185 if (fwrite (buf
, 1, n
, ofp
) != n
)
191 gmon_read_raw_arc (ifp
, fpc
, spc
, cnt
)
197 BFD_HOST_U_64_BIT cnt64
;
200 if (gmon_io_read_vma (ifp
, fpc
)
201 || gmon_io_read_vma (ifp
, spc
))
204 switch (bfd_arch_bits_per_address (core_bfd
))
207 if (gmon_io_read_32 (ifp
, &cnt32
))
213 if (gmon_io_read_64 (ifp
, &cnt64
))
219 fprintf (stderr
, _("%s: bits per address has unexpected value of %u\n"),
220 whoami
, bfd_arch_bits_per_address (core_bfd
));
227 gmon_write_raw_arc (ofp
, fpc
, spc
, cnt
)
234 if (gmon_io_write_vma (ofp
, fpc
)
235 || gmon_io_write_vma (ofp
, spc
))
238 switch (bfd_arch_bits_per_address (core_bfd
))
241 if (gmon_io_write_32 (ofp
, (unsigned int) cnt
))
246 if (gmon_io_write_64 (ofp
, (BFD_HOST_U_64_BIT
) cnt
))
251 fprintf (stderr
, _("%s: bits per address has unexpected value of %u\n"),
252 whoami
, bfd_arch_bits_per_address (core_bfd
));
259 gmon_out_read (filename
)
260 const char *filename
;
263 struct gmon_hdr ghdr
;
265 int nhist
= 0, narcs
= 0, nbbs
= 0;
267 /* Open gmon.out file. */
268 if (strcmp (filename
, "-") == 0)
272 SET_BINARY (fileno (stdin
));
277 ifp
= fopen (filename
, FOPEN_RB
);
286 if (fread (&ghdr
, sizeof (struct gmon_hdr
), 1, ifp
) != 1)
288 fprintf (stderr
, _("%s: file too short to be a gmon file\n"),
293 if ((file_format
== FF_MAGIC
)
294 || (file_format
== FF_AUTO
&& !strncmp (&ghdr
.cookie
[0], GMON_MAGIC
, 4)))
296 if (file_format
== FF_MAGIC
&& strncmp (&ghdr
.cookie
[0], GMON_MAGIC
, 4))
298 fprintf (stderr
, _("%s: file `%s' has bad magic cookie\n"),
303 /* Right magic, so it's probably really a new gmon.out file. */
304 gmon_file_version
= bfd_get_32 (core_bfd
, (bfd_byte
*) ghdr
.version
);
306 if (gmon_file_version
!= GMON_VERSION
&& gmon_file_version
!= 0)
309 _("%s: file `%s' has unsupported version %d\n"),
310 whoami
, filename
, gmon_file_version
);
314 /* Read in all the records. */
315 while (fread (&tag
, sizeof (tag
), 1, ifp
) == 1)
319 case GMON_TAG_TIME_HIST
:
321 gmon_input
|= INPUT_HISTOGRAM
;
322 hist_read_rec (ifp
, filename
);
325 case GMON_TAG_CG_ARC
:
327 gmon_input
|= INPUT_CALL_GRAPH
;
328 cg_read_rec (ifp
, filename
);
331 case GMON_TAG_BB_COUNT
:
333 gmon_input
|= INPUT_BB_COUNTS
;
334 bb_read_rec (ifp
, filename
);
339 _("%s: %s: found bad tag %d (file corrupted?)\n"),
340 whoami
, filename
, tag
);
345 else if (file_format
== FF_AUTO
346 || file_format
== FF_BSD
347 || file_format
== FF_BSD44
)
355 int i
, samp_bytes
, header_size
= 0;
357 bfd_vma from_pc
, self_pc
;
363 /* Information from a gmon.out file is in two parts: an array of
364 sampling hits within pc ranges, and the arcs. */
365 gmon_input
= INPUT_HISTOGRAM
| INPUT_CALL_GRAPH
;
367 /* This fseek() ought to work even on stdin as long as it's
368 not an interactive device (heck, is there anybody who would
369 want to type in a gmon.out at the terminal?). */
370 if (fseek (ifp
, 0, SEEK_SET
) < 0)
376 /* The beginning of the old BSD header and the 4.4BSD header
377 are the same: lowpc, highpc, ncnt */
378 if (gmon_io_read_vma (ifp
, &tmp
.low_pc
)
379 || gmon_io_read_vma (ifp
, &tmp
.high_pc
)
380 || gmon_io_read_32 (ifp
, &tmp
.ncnt
))
383 fprintf (stderr
, _("%s: file too short to be a gmon file\n"),
388 /* Check to see if this a 4.4BSD-style header. */
389 if (gmon_io_read_32 (ifp
, &version
))
392 if (version
== GMONVERSION
)
396 /* 4.4BSD format header. */
397 if (gmon_io_read_32 (ifp
, &profrate
))
402 else if (hz
!= profrate
)
405 _("%s: profiling rate incompatible with first gmon file\n"),
410 switch (bfd_arch_bits_per_address (core_bfd
))
413 header_size
= GMON_HDRSIZE_BSD44_32
;
417 header_size
= GMON_HDRSIZE_BSD44_64
;
422 _("%s: bits per address has unexpected value of %u\n"),
423 whoami
, bfd_arch_bits_per_address (core_bfd
));
429 /* Old style BSD format. */
430 if (file_format
== FF_BSD44
)
432 fprintf (stderr
, _("%s: file `%s' has bad magic cookie\n"),
437 switch (bfd_arch_bits_per_address (core_bfd
))
440 header_size
= GMON_HDRSIZE_OLDBSD_32
;
444 header_size
= GMON_HDRSIZE_OLDBSD_64
;
449 _("%s: bits per address has unexpected value of %u\n"),
450 whoami
, bfd_arch_bits_per_address (core_bfd
));
455 /* Position the file to after the header. */
456 if (fseek (ifp
, header_size
, SEEK_SET
) < 0)
462 if (s_highpc
&& (tmp
.low_pc
!= h
.low_pc
463 || tmp
.high_pc
!= h
.high_pc
|| tmp
.ncnt
!= h
.ncnt
))
465 fprintf (stderr
, _("%s: incompatible with first gmon file\n"),
471 s_lowpc
= (bfd_vma
) h
.low_pc
;
472 s_highpc
= (bfd_vma
) h
.high_pc
;
473 lowpc
= (bfd_vma
) h
.low_pc
/ sizeof (UNIT
);
474 highpc
= (bfd_vma
) h
.high_pc
/ sizeof (UNIT
);
475 samp_bytes
= h
.ncnt
- header_size
;
476 hist_num_bins
= samp_bytes
/ sizeof (UNIT
);
479 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
480 (unsigned long) h
.low_pc
, (unsigned long) h
.high_pc
,
482 printf ("[gmon_out_read] s_lowpc 0x%lx s_highpc 0x%lx\n",
483 (unsigned long) s_lowpc
, (unsigned long) s_highpc
);
484 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx\n",
485 (unsigned long) lowpc
, (unsigned long) highpc
);
486 printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
487 samp_bytes
, hist_num_bins
));
489 /* Make sure that we have sensible values. */
490 if (samp_bytes
< 0 || lowpc
> highpc
)
493 _("%s: file '%s' does not appear to be in gmon.out format\n"),
504 (int *) xmalloc (hist_num_bins
* sizeof (hist_sample
[0]));
506 memset (hist_sample
, 0, hist_num_bins
* sizeof (hist_sample
[0]));
509 for (i
= 0; i
< hist_num_bins
; ++i
)
511 if (fread (raw_bin_count
, sizeof (raw_bin_count
), 1, ifp
) != 1)
514 _("%s: unexpected EOF after reading %d/%d bins\n"),
515 whoami
, --i
, hist_num_bins
);
519 hist_sample
[i
] += bfd_get_16 (core_bfd
, (bfd_byte
*) raw_bin_count
);
522 /* The rest of the file consists of a bunch of
523 <from,self,count> tuples. */
524 while (gmon_read_raw_arc (ifp
, &from_pc
, &self_pc
, &count
) == 0)
529 printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
530 (unsigned long) from_pc
, (unsigned long) self_pc
, count
));
533 cg_tally (from_pc
, self_pc
, count
);
540 /* How many ticks per second? If we can't tell, report
547 fprintf (stderr
, _("time is in ticks, not seconds\n"));
553 fprintf (stderr
, _("%s: don't know how to deal with file format %d\n"),
554 whoami
, file_format
);
558 if (output_style
& STYLE_GMON_INFO
)
560 printf (_("File `%s' (version %d) contains:\n"),
561 filename
, gmon_file_version
);
563 _("\t%d histogram record\n") :
564 _("\t%d histogram records\n"), nhist
);
566 _("\t%d call-graph record\n") :
567 _("\t%d call-graph records\n"), narcs
);
569 _("\t%d basic-block count record\n") :
570 _("\t%d basic-block count records\n"), nbbs
);
571 first_output
= false;
577 gmon_out_write (filename
)
578 const char *filename
;
581 struct gmon_hdr ghdr
;
583 ofp
= fopen (filename
, FOPEN_WB
);
590 if (file_format
== FF_AUTO
|| file_format
== FF_MAGIC
)
592 /* Write gmon header. */
594 memcpy (&ghdr
.cookie
[0], GMON_MAGIC
, 4);
595 bfd_put_32 (core_bfd
, (bfd_vma
) GMON_VERSION
, (bfd_byte
*) ghdr
.version
);
597 if (fwrite (&ghdr
, sizeof (ghdr
), 1, ofp
) != 1)
603 /* Write execution time histogram if we have one. */
604 if (gmon_input
& INPUT_HISTOGRAM
)
605 hist_write_hist (ofp
, filename
);
607 /* Write call graph arcs if we have any. */
608 if (gmon_input
& INPUT_CALL_GRAPH
)
609 cg_write_arcs (ofp
, filename
);
611 /* Write basic-block info if we have it. */
612 if (gmon_input
& INPUT_BB_COUNTS
)
613 bb_write_blocks (ofp
, filename
);
615 else if (file_format
== FF_BSD
|| file_format
== FF_BSD44
)
624 memset (pad
, 0, sizeof (pad
));
627 /* Decide how large the header will be. Use the 4.4BSD format
628 header if explicitly specified, or if the profiling rate is
629 non-standard. Otherwise, use the old BSD format. */
630 if (file_format
== FF_BSD44
634 switch (bfd_arch_bits_per_address (core_bfd
))
637 hdrsize
= GMON_HDRSIZE_BSD44_32
;
641 hdrsize
= GMON_HDRSIZE_BSD44_64
;
646 _("%s: bits per address has unexpected value of %u\n"),
647 whoami
, bfd_arch_bits_per_address (core_bfd
));
654 switch (bfd_arch_bits_per_address (core_bfd
))
657 hdrsize
= GMON_HDRSIZE_OLDBSD_32
;
661 hdrsize
= GMON_HDRSIZE_OLDBSD_64
;
662 /* FIXME: Checking host compiler defines here means that we can't
663 use a cross gprof alpha OSF. */
664 #if defined(__alpha__) && defined (__osf__)
671 _("%s: bits per address has unexpected value of %u\n"),
672 whoami
, bfd_arch_bits_per_address (core_bfd
));
677 /* Write the parts of the headers that are common to both the
678 old BSD and 4.4BSD formats. */
679 if (gmon_io_write_vma (ofp
, s_lowpc
)
680 || gmon_io_write_vma (ofp
, s_highpc
)
681 || gmon_io_write_32 (ofp
, hist_num_bins
* sizeof (UNIT
) + hdrsize
))
687 /* Write out the 4.4BSD header bits, if that's what we're using. */
688 if (file_format
== FF_BSD44
691 if (gmon_io_write_32 (ofp
, GMONVERSION
)
692 || gmon_io_write_32 (ofp
, (unsigned int) hz
))
699 /* Now write out any necessary padding after the meaningful
702 && fwrite (pad
, 1, padsize
, ofp
) != padsize
)
708 /* Dump the samples. */
709 for (i
= 0; i
< hist_num_bins
; ++i
)
711 bfd_put_16 (core_bfd
, (bfd_vma
) hist_sample
[i
],
712 (bfd_byte
*) &raw_bin_count
[0]);
713 if (fwrite (&raw_bin_count
[0], sizeof (raw_bin_count
), 1, ofp
) != 1)
720 /* Dump the normalized raw arc information. */
721 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
723 for (arc
= sym
->cg
.children
; arc
; arc
= arc
->next_child
)
725 if (gmon_write_raw_arc (ofp
, arc
->parent
->addr
,
726 arc
->child
->addr
, arc
->count
))
732 printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
733 (unsigned long) arc
->parent
->addr
,
734 (unsigned long) arc
->child
->addr
, arc
->count
));
742 fprintf (stderr
, _("%s: don't know how to deal with file format %d\n"),
743 whoami
, file_format
);