1 /* gmon_io.c - Input and output from/to gmon.out files.
3 Copyright (C) 1999-2022 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 3 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., 51 Franklin Street - Fifth Floor, Boston, MA
23 #include "binary-io.h"
24 #include "search_list.h"
28 #include "basic_blocks.h"
30 #include "call_graph.h"
33 #include "gmon.h" /* Fetch header for old format. */
36 #include "libiberty.h"
43 enum gmon_ptr_signedness
{
48 static enum gmon_ptr_size
gmon_get_ptr_size (void);
49 static enum gmon_ptr_signedness
gmon_get_ptr_signedness (void);
51 static int gmon_io_read_64 (FILE *, uint64_t *);
52 static int gmon_io_write_64 (FILE *, uint64_t);
53 static int gmon_read_raw_arc
54 (FILE *, bfd_vma
*, bfd_vma
*, unsigned long *);
55 static int gmon_write_raw_arc
56 (FILE *, bfd_vma
, bfd_vma
, unsigned long);
59 int gmon_file_version
= 0; /* 0 == old (non-versioned) file format. */
61 static enum gmon_ptr_size
62 gmon_get_ptr_size (void)
66 /* Pick best size for pointers. Start with the ELF size, and if not
67 elf go with the architecture's address size. */
68 size
= bfd_get_arch_size (core_bfd
);
70 size
= bfd_arch_bits_per_address (core_bfd
);
81 fprintf (stderr
, _("%s: address size has unexpected value of %u\n"),
87 static enum gmon_ptr_signedness
88 gmon_get_ptr_signedness (void)
92 /* Figure out whether to sign extend. If BFD doesn't know, assume no. */
93 sext
= bfd_get_sign_extend_vma (core_bfd
);
96 return (sext
? ptr_signed
: ptr_unsigned
);
100 gmon_io_read_32 (FILE *ifp
, unsigned int *valp
)
104 if (fread (buf
, 1, 4, ifp
) != 4)
106 *valp
= bfd_get_32 (core_bfd
, buf
);
111 gmon_io_read_64 (FILE *ifp
, uint64_t *valp
)
115 if (fread (buf
, 1, 8, ifp
) != 8)
117 *valp
= bfd_get_64 (core_bfd
, buf
);
122 gmon_io_read_vma (FILE *ifp
, bfd_vma
*valp
)
127 switch (gmon_get_ptr_size ())
130 if (gmon_io_read_32 (ifp
, &val32
))
132 if (gmon_get_ptr_signedness () == ptr_signed
)
139 if (gmon_io_read_64 (ifp
, &val64
))
141 if (gmon_get_ptr_signedness () == ptr_signed
)
142 *valp
= (int64_t) val64
;
151 gmon_io_read (FILE *ifp
, char *buf
, size_t n
)
153 if (fread (buf
, 1, n
, ifp
) != n
)
159 gmon_io_write_32 (FILE *ofp
, unsigned int val
)
163 bfd_put_32 (core_bfd
, (bfd_vma
) val
, buf
);
164 if (fwrite (buf
, 1, 4, ofp
) != 4)
170 gmon_io_write_64 (FILE *ofp
, uint64_t val
)
174 bfd_put_64 (core_bfd
, (bfd_vma
) val
, buf
);
175 if (fwrite (buf
, 1, 8, ofp
) != 8)
181 gmon_io_write_vma (FILE *ofp
, bfd_vma val
)
184 switch (gmon_get_ptr_size ())
187 if (gmon_io_write_32 (ofp
, (unsigned int) val
))
192 if (gmon_io_write_64 (ofp
, (uint64_t) val
))
200 gmon_io_write_8 (FILE *ofp
, unsigned int val
)
204 bfd_put_8 (core_bfd
, val
, buf
);
205 if (fwrite (buf
, 1, 1, ofp
) != 1)
211 gmon_io_write (FILE *ofp
, char *buf
, size_t n
)
213 if (fwrite (buf
, 1, n
, ofp
) != n
)
219 gmon_read_raw_arc (FILE *ifp
, bfd_vma
*fpc
, bfd_vma
*spc
, unsigned long *cnt
)
224 if (gmon_io_read_vma (ifp
, fpc
)
225 || gmon_io_read_vma (ifp
, spc
))
228 switch (gmon_get_ptr_size ())
231 if (gmon_io_read_32 (ifp
, &cnt32
))
237 if (gmon_io_read_64 (ifp
, &cnt64
))
249 gmon_write_raw_arc (FILE *ofp
, bfd_vma fpc
, bfd_vma spc
, unsigned long cnt
)
252 if (gmon_io_write_vma (ofp
, fpc
)
253 || gmon_io_write_vma (ofp
, spc
))
256 switch (gmon_get_ptr_size ())
259 if (gmon_io_write_32 (ofp
, (unsigned int) cnt
))
264 if (gmon_io_write_64 (ofp
, (uint64_t) cnt
))
272 gmon_out_read (const char *filename
)
275 struct gmon_hdr ghdr
;
277 int nhist
= 0, narcs
= 0, nbbs
= 0;
279 /* Open gmon.out file. */
280 if (strcmp (filename
, "-") == 0)
283 SET_BINARY (fileno (stdin
));
287 ifp
= fopen (filename
, FOPEN_RB
);
296 if (fread (&ghdr
, sizeof (struct gmon_hdr
), 1, ifp
) != 1)
298 fprintf (stderr
, _("%s: file too short to be a gmon file\n"),
303 if ((file_format
== FF_MAGIC
)
304 || (file_format
== FF_AUTO
&& !strncmp (&ghdr
.cookie
[0], GMON_MAGIC
, 4)))
306 if (file_format
== FF_MAGIC
&& strncmp (&ghdr
.cookie
[0], GMON_MAGIC
, 4))
308 fprintf (stderr
, _("%s: file `%s' has bad magic cookie\n"),
313 /* Right magic, so it's probably really a new gmon.out file. */
314 gmon_file_version
= bfd_get_32 (core_bfd
, (bfd_byte
*) ghdr
.version
);
316 if (gmon_file_version
!= GMON_VERSION
&& gmon_file_version
!= 0)
319 _("%s: file `%s' has unsupported version %d\n"),
320 whoami
, filename
, gmon_file_version
);
324 /* Read in all the records. */
325 while (fread (&tag
, sizeof (tag
), 1, ifp
) == 1)
329 case GMON_TAG_TIME_HIST
:
331 gmon_input
|= INPUT_HISTOGRAM
;
332 hist_read_rec (ifp
, filename
);
335 case GMON_TAG_CG_ARC
:
337 gmon_input
|= INPUT_CALL_GRAPH
;
338 cg_read_rec (ifp
, filename
);
341 case GMON_TAG_BB_COUNT
:
343 gmon_input
|= INPUT_BB_COUNTS
;
344 bb_read_rec (ifp
, filename
);
349 _("%s: %s: found bad tag %d (file corrupted?)\n"),
350 whoami
, filename
, tag
);
355 else if (file_format
== FF_AUTO
356 || file_format
== FF_BSD
357 || file_format
== FF_BSD44
)
366 int samp_bytes
, header_size
= 0;
368 bfd_vma from_pc
, self_pc
;
371 unsigned int version
;
372 unsigned int hist_num_bins
;
374 /* Information from a gmon.out file is in two parts: an array of
375 sampling hits within pc ranges, and the arcs. */
376 gmon_input
= INPUT_HISTOGRAM
| INPUT_CALL_GRAPH
;
378 /* This fseek() ought to work even on stdin as long as it's
379 not an interactive device (heck, is there anybody who would
380 want to type in a gmon.out at the terminal?). */
381 if (fseek (ifp
, 0, SEEK_SET
) < 0)
387 /* The beginning of the old BSD header and the 4.4BSD header
388 are the same: lowpc, highpc, ncnt */
389 if (gmon_io_read_vma (ifp
, &tmp
.low_pc
)
390 || gmon_io_read_vma (ifp
, &tmp
.high_pc
)
391 || gmon_io_read_32 (ifp
, &tmp
.ncnt
))
394 fprintf (stderr
, _("%s: file too short to be a gmon file\n"),
399 /* Check to see if this a 4.4BSD-style header. */
400 if (gmon_io_read_32 (ifp
, &version
))
403 if (version
== GMONVERSION
)
405 unsigned int profrate
;
407 /* 4.4BSD format header. */
408 if (gmon_io_read_32 (ifp
, &profrate
))
413 else if (hz
!= (int) profrate
)
416 _("%s: profiling rate incompatible with first gmon file\n"),
421 switch (gmon_get_ptr_size ())
424 header_size
= GMON_HDRSIZE_BSD44_32
;
428 header_size
= GMON_HDRSIZE_BSD44_64
;
434 /* Old style BSD format. */
435 if (file_format
== FF_BSD44
)
437 fprintf (stderr
, _("%s: file `%s' has bad magic cookie\n"),
442 switch (gmon_get_ptr_size ())
445 header_size
= GMON_HDRSIZE_OLDBSD_32
;
449 header_size
= GMON_HDRSIZE_OLDBSD_64
;
454 /* Position the file to after the header. */
455 if (fseek (ifp
, header_size
, SEEK_SET
) < 0)
461 samp_bytes
= tmp
.ncnt
- header_size
;
462 hist_num_bins
= samp_bytes
/ sizeof (UNIT
);
463 if (histograms
&& (tmp
.low_pc
!= histograms
->lowpc
464 || tmp
.high_pc
!= histograms
->highpc
465 || (hist_num_bins
!= histograms
->num_bins
)))
467 fprintf (stderr
, _("%s: incompatible with first gmon file\n"),
475 histograms
= (struct histogram
*) xmalloc (sizeof (struct histogram
));
476 histograms
->lowpc
= tmp
.low_pc
;
477 histograms
->highpc
= tmp
.high_pc
;
478 histograms
->num_bins
= hist_num_bins
;
479 hist_scale
= (double)((tmp
.high_pc
- tmp
.low_pc
) / sizeof (UNIT
))
481 histograms
->sample
= (int *) xmalloc (hist_num_bins
* sizeof (int));
482 memset (histograms
->sample
, 0,
483 hist_num_bins
* sizeof (int));
487 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
488 (unsigned long) tmp
.low_pc
, (unsigned long) tmp
.high_pc
,
490 printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
491 samp_bytes
, hist_num_bins
));
493 /* Make sure that we have sensible values. */
494 if (samp_bytes
< 0 || histograms
->lowpc
> histograms
->highpc
)
497 _("%s: file '%s' does not appear to be in gmon.out format\n"),
505 for (i
= 0; i
< hist_num_bins
; ++i
)
507 if (fread (raw_bin_count
, sizeof (raw_bin_count
), 1, ifp
) != 1)
510 _("%s: unexpected EOF after reading %d/%d bins\n"),
511 whoami
, --i
, hist_num_bins
);
515 histograms
->sample
[i
]
516 += bfd_get_16 (core_bfd
, (bfd_byte
*) raw_bin_count
);
519 /* The rest of the file consists of a bunch of
520 <from,self,count> tuples. */
521 while (gmon_read_raw_arc (ifp
, &from_pc
, &self_pc
, &count
) == 0)
526 printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
527 (unsigned long) from_pc
, (unsigned long) self_pc
, count
));
530 cg_tally (from_pc
, self_pc
, count
);
535 /* How many ticks per second? If we can't tell, report
542 fprintf (stderr
, _("time is in ticks, not seconds\n"));
548 fprintf (stderr
, _("%s: don't know how to deal with file format %d\n"),
549 whoami
, file_format
);
556 if (output_style
& STYLE_GMON_INFO
)
558 printf (_("File `%s' (version %d) contains:\n"),
559 filename
, gmon_file_version
);
561 _("\t%d histogram record\n") :
562 _("\t%d histogram records\n"), nhist
);
564 _("\t%d call-graph record\n") :
565 _("\t%d call-graph records\n"), narcs
);
567 _("\t%d basic-block count record\n") :
568 _("\t%d basic-block count records\n"), nbbs
);
569 first_output
= false;
575 gmon_out_write (const char *filename
)
578 struct gmon_hdr ghdr
;
580 ofp
= fopen (filename
, FOPEN_WB
);
587 if (file_format
== FF_AUTO
|| file_format
== FF_MAGIC
)
589 /* Write gmon header. */
591 memcpy (&ghdr
.cookie
[0], GMON_MAGIC
, 4);
592 bfd_put_32 (core_bfd
, (bfd_vma
) GMON_VERSION
, (bfd_byte
*) ghdr
.version
);
594 if (fwrite (&ghdr
, sizeof (ghdr
), 1, ofp
) != 1)
600 /* Write execution time histogram if we have one. */
601 if (gmon_input
& INPUT_HISTOGRAM
)
602 hist_write_hist (ofp
, filename
);
604 /* Write call graph arcs if we have any. */
605 if (gmon_input
& INPUT_CALL_GRAPH
)
606 cg_write_arcs (ofp
, filename
);
608 /* Write basic-block info if we have it. */
609 if (gmon_input
& INPUT_BB_COUNTS
)
610 bb_write_blocks (ofp
, filename
);
612 else if (file_format
== FF_BSD
|| file_format
== FF_BSD44
)
615 unsigned int i
, hdrsize
;
621 memset (pad
, 0, sizeof (pad
));
624 /* Decide how large the header will be. Use the 4.4BSD format
625 header if explicitly specified, or if the profiling rate is
626 non-standard. Otherwise, use the old BSD format. */
627 if (file_format
== FF_BSD44
631 switch (gmon_get_ptr_size ())
634 hdrsize
= GMON_HDRSIZE_BSD44_32
;
638 hdrsize
= GMON_HDRSIZE_BSD44_64
;
645 switch (gmon_get_ptr_size ())
648 hdrsize
= GMON_HDRSIZE_OLDBSD_32
;
652 hdrsize
= GMON_HDRSIZE_OLDBSD_64
;
653 /* FIXME: Checking host compiler defines here means that we can't
654 use a cross gprof alpha OSF. */
655 #if defined(__alpha__) && defined (__osf__)
662 /* Write the parts of the headers that are common to both the
663 old BSD and 4.4BSD formats. */
664 if (gmon_io_write_vma (ofp
, histograms
->lowpc
)
665 || gmon_io_write_vma (ofp
, histograms
->highpc
)
666 || gmon_io_write_32 (ofp
, histograms
->num_bins
667 * sizeof (UNIT
) + hdrsize
))
673 /* Write out the 4.4BSD header bits, if that's what we're using. */
674 if (file_format
== FF_BSD44
677 if (gmon_io_write_32 (ofp
, GMONVERSION
)
678 || gmon_io_write_32 (ofp
, (unsigned int) hz
))
685 /* Now write out any necessary padding after the meaningful
688 && fwrite (pad
, 1, padsize
, ofp
) != padsize
)
694 /* Dump the samples. */
695 for (i
= 0; i
< histograms
->num_bins
; ++i
)
697 bfd_put_16 (core_bfd
, (bfd_vma
) histograms
->sample
[i
],
698 (bfd_byte
*) &raw_bin_count
[0]);
699 if (fwrite (&raw_bin_count
[0], sizeof (raw_bin_count
), 1, ofp
) != 1)
706 /* Dump the normalized raw arc information. */
707 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
709 for (arc
= sym
->cg
.children
; arc
; arc
= arc
->next_child
)
711 if (gmon_write_raw_arc (ofp
, arc
->parent
->addr
,
712 arc
->child
->addr
, arc
->count
))
718 printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
719 (unsigned long) arc
->parent
->addr
,
720 (unsigned long) arc
->child
->addr
, arc
->count
));
728 fprintf (stderr
, _("%s: don't know how to deal with file format %d\n"),
729 whoami
, file_format
);