]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gprof/gmon_io.c
1 /* gmon_io.c - Input and output from/to gmon.out files.
3 Copyright (C) 1999-2017 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 #ifdef BFD_HOST_U_64_BIT
52 static int gmon_io_read_64 (FILE *, BFD_HOST_U_64_BIT
*);
53 static int gmon_io_write_64 (FILE *, BFD_HOST_U_64_BIT
);
55 static int gmon_read_raw_arc
56 (FILE *, bfd_vma
*, bfd_vma
*, unsigned long *);
57 static int gmon_write_raw_arc
58 (FILE *, bfd_vma
, bfd_vma
, unsigned long);
61 int gmon_file_version
= 0; /* 0 == old (non-versioned) file format. */
63 static enum gmon_ptr_size
64 gmon_get_ptr_size (void)
68 /* Pick best size for pointers. Start with the ELF size, and if not
69 elf go with the architecture's address size. */
70 size
= bfd_get_arch_size (core_bfd
);
72 size
= bfd_arch_bits_per_address (core_bfd
);
83 fprintf (stderr
, _("%s: address size has unexpected value of %u\n"),
89 static enum gmon_ptr_signedness
90 gmon_get_ptr_signedness (void)
94 /* Figure out whether to sign extend. If BFD doesn't know, assume no. */
95 sext
= bfd_get_sign_extend_vma (core_bfd
);
98 return (sext
? ptr_signed
: ptr_unsigned
);
102 gmon_io_read_32 (FILE *ifp
, unsigned int *valp
)
106 if (fread (buf
, 1, 4, ifp
) != 4)
108 *valp
= bfd_get_32 (core_bfd
, buf
);
112 #ifdef BFD_HOST_U_64_BIT
114 gmon_io_read_64 (FILE *ifp
, BFD_HOST_U_64_BIT
*valp
)
118 if (fread (buf
, 1, 8, ifp
) != 8)
120 *valp
= bfd_get_64 (core_bfd
, buf
);
126 gmon_io_read_vma (FILE *ifp
, bfd_vma
*valp
)
129 #ifdef BFD_HOST_U_64_BIT
130 BFD_HOST_U_64_BIT val64
;
133 switch (gmon_get_ptr_size ())
136 if (gmon_io_read_32 (ifp
, &val32
))
138 if (gmon_get_ptr_signedness () == ptr_signed
)
144 #ifdef BFD_HOST_U_64_BIT
146 if (gmon_io_read_64 (ifp
, &val64
))
148 #ifdef BFD_HOST_64_BIT
149 if (gmon_get_ptr_signedness () == ptr_signed
)
150 *valp
= (BFD_HOST_64_BIT
) val64
;
161 gmon_io_read (FILE *ifp
, char *buf
, size_t n
)
163 if (fread (buf
, 1, n
, ifp
) != n
)
169 gmon_io_write_32 (FILE *ofp
, unsigned int val
)
173 bfd_put_32 (core_bfd
, (bfd_vma
) val
, buf
);
174 if (fwrite (buf
, 1, 4, ofp
) != 4)
179 #ifdef BFD_HOST_U_64_BIT
181 gmon_io_write_64 (FILE *ofp
, BFD_HOST_U_64_BIT val
)
185 bfd_put_64 (core_bfd
, (bfd_vma
) val
, buf
);
186 if (fwrite (buf
, 1, 8, ofp
) != 8)
193 gmon_io_write_vma (FILE *ofp
, bfd_vma val
)
196 switch (gmon_get_ptr_size ())
199 if (gmon_io_write_32 (ofp
, (unsigned int) val
))
203 #ifdef BFD_HOST_U_64_BIT
205 if (gmon_io_write_64 (ofp
, (BFD_HOST_U_64_BIT
) val
))
214 gmon_io_write_8 (FILE *ofp
, unsigned int val
)
218 bfd_put_8 (core_bfd
, val
, buf
);
219 if (fwrite (buf
, 1, 1, ofp
) != 1)
225 gmon_io_write (FILE *ofp
, char *buf
, size_t n
)
227 if (fwrite (buf
, 1, n
, ofp
) != n
)
233 gmon_read_raw_arc (FILE *ifp
, bfd_vma
*fpc
, bfd_vma
*spc
, unsigned long *cnt
)
235 #ifdef BFD_HOST_U_64_BIT
236 BFD_HOST_U_64_BIT cnt64
;
240 if (gmon_io_read_vma (ifp
, fpc
)
241 || gmon_io_read_vma (ifp
, spc
))
244 switch (gmon_get_ptr_size ())
247 if (gmon_io_read_32 (ifp
, &cnt32
))
252 #ifdef BFD_HOST_U_64_BIT
254 if (gmon_io_read_64 (ifp
, &cnt64
))
267 gmon_write_raw_arc (FILE *ofp
, bfd_vma fpc
, bfd_vma spc
, unsigned long cnt
)
270 if (gmon_io_write_vma (ofp
, fpc
)
271 || gmon_io_write_vma (ofp
, spc
))
274 switch (gmon_get_ptr_size ())
277 if (gmon_io_write_32 (ofp
, (unsigned int) cnt
))
281 #ifdef BFD_HOST_U_64_BIT
283 if (gmon_io_write_64 (ofp
, (BFD_HOST_U_64_BIT
) cnt
))
292 gmon_out_read (const char *filename
)
295 struct gmon_hdr ghdr
;
297 int nhist
= 0, narcs
= 0, nbbs
= 0;
299 /* Open gmon.out file. */
300 if (strcmp (filename
, "-") == 0)
303 SET_BINARY (fileno (stdin
));
307 ifp
= fopen (filename
, FOPEN_RB
);
316 if (fread (&ghdr
, sizeof (struct gmon_hdr
), 1, ifp
) != 1)
318 fprintf (stderr
, _("%s: file too short to be a gmon file\n"),
323 if ((file_format
== FF_MAGIC
)
324 || (file_format
== FF_AUTO
&& !strncmp (&ghdr
.cookie
[0], GMON_MAGIC
, 4)))
326 if (file_format
== FF_MAGIC
&& strncmp (&ghdr
.cookie
[0], GMON_MAGIC
, 4))
328 fprintf (stderr
, _("%s: file `%s' has bad magic cookie\n"),
333 /* Right magic, so it's probably really a new gmon.out file. */
334 gmon_file_version
= bfd_get_32 (core_bfd
, (bfd_byte
*) ghdr
.version
);
336 if (gmon_file_version
!= GMON_VERSION
&& gmon_file_version
!= 0)
339 _("%s: file `%s' has unsupported version %d\n"),
340 whoami
, filename
, gmon_file_version
);
344 /* Read in all the records. */
345 while (fread (&tag
, sizeof (tag
), 1, ifp
) == 1)
349 case GMON_TAG_TIME_HIST
:
351 gmon_input
|= INPUT_HISTOGRAM
;
352 hist_read_rec (ifp
, filename
);
355 case GMON_TAG_CG_ARC
:
357 gmon_input
|= INPUT_CALL_GRAPH
;
358 cg_read_rec (ifp
, filename
);
361 case GMON_TAG_BB_COUNT
:
363 gmon_input
|= INPUT_BB_COUNTS
;
364 bb_read_rec (ifp
, filename
);
369 _("%s: %s: found bad tag %d (file corrupted?)\n"),
370 whoami
, filename
, tag
);
375 else if (file_format
== FF_AUTO
376 || file_format
== FF_BSD
377 || file_format
== FF_BSD44
)
386 int samp_bytes
, header_size
= 0;
388 bfd_vma from_pc
, self_pc
;
391 unsigned int version
;
392 unsigned int hist_num_bins
;
394 /* Information from a gmon.out file is in two parts: an array of
395 sampling hits within pc ranges, and the arcs. */
396 gmon_input
= INPUT_HISTOGRAM
| INPUT_CALL_GRAPH
;
398 /* This fseek() ought to work even on stdin as long as it's
399 not an interactive device (heck, is there anybody who would
400 want to type in a gmon.out at the terminal?). */
401 if (fseek (ifp
, 0, SEEK_SET
) < 0)
407 /* The beginning of the old BSD header and the 4.4BSD header
408 are the same: lowpc, highpc, ncnt */
409 if (gmon_io_read_vma (ifp
, &tmp
.low_pc
)
410 || gmon_io_read_vma (ifp
, &tmp
.high_pc
)
411 || gmon_io_read_32 (ifp
, &tmp
.ncnt
))
414 fprintf (stderr
, _("%s: file too short to be a gmon file\n"),
419 /* Check to see if this a 4.4BSD-style header. */
420 if (gmon_io_read_32 (ifp
, &version
))
423 if (version
== GMONVERSION
)
425 unsigned int profrate
;
427 /* 4.4BSD format header. */
428 if (gmon_io_read_32 (ifp
, &profrate
))
433 else if (hz
!= (int) profrate
)
436 _("%s: profiling rate incompatible with first gmon file\n"),
441 switch (gmon_get_ptr_size ())
444 header_size
= GMON_HDRSIZE_BSD44_32
;
448 header_size
= GMON_HDRSIZE_BSD44_64
;
454 /* Old style BSD format. */
455 if (file_format
== FF_BSD44
)
457 fprintf (stderr
, _("%s: file `%s' has bad magic cookie\n"),
462 switch (gmon_get_ptr_size ())
465 header_size
= GMON_HDRSIZE_OLDBSD_32
;
469 header_size
= GMON_HDRSIZE_OLDBSD_64
;
474 /* Position the file to after the header. */
475 if (fseek (ifp
, header_size
, SEEK_SET
) < 0)
481 samp_bytes
= tmp
.ncnt
- header_size
;
482 hist_num_bins
= samp_bytes
/ sizeof (UNIT
);
483 if (histograms
&& (tmp
.low_pc
!= histograms
->lowpc
484 || tmp
.high_pc
!= histograms
->highpc
485 || (hist_num_bins
!= histograms
->num_bins
)))
487 fprintf (stderr
, _("%s: incompatible with first gmon file\n"),
495 histograms
= (struct histogram
*) xmalloc (sizeof (struct histogram
));
496 histograms
->lowpc
= tmp
.low_pc
;
497 histograms
->highpc
= tmp
.high_pc
;
498 histograms
->num_bins
= hist_num_bins
;
499 hist_scale
= (double)((tmp
.high_pc
- tmp
.low_pc
) / sizeof (UNIT
))
501 histograms
->sample
= (int *) xmalloc (hist_num_bins
* sizeof (int));
502 memset (histograms
->sample
, 0,
503 hist_num_bins
* sizeof (int));
507 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
508 (unsigned long) tmp
.low_pc
, (unsigned long) tmp
.high_pc
,
510 printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
511 samp_bytes
, hist_num_bins
));
513 /* Make sure that we have sensible values. */
514 if (samp_bytes
< 0 || histograms
->lowpc
> histograms
->highpc
)
517 _("%s: file '%s' does not appear to be in gmon.out format\n"),
525 for (i
= 0; i
< hist_num_bins
; ++i
)
527 if (fread (raw_bin_count
, sizeof (raw_bin_count
), 1, ifp
) != 1)
530 _("%s: unexpected EOF after reading %d/%d bins\n"),
531 whoami
, --i
, hist_num_bins
);
535 histograms
->sample
[i
]
536 += bfd_get_16 (core_bfd
, (bfd_byte
*) raw_bin_count
);
539 /* The rest of the file consists of a bunch of
540 <from,self,count> tuples. */
541 while (gmon_read_raw_arc (ifp
, &from_pc
, &self_pc
, &count
) == 0)
546 printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
547 (unsigned long) from_pc
, (unsigned long) self_pc
, count
));
550 cg_tally (from_pc
, self_pc
, count
);
555 /* How many ticks per second? If we can't tell, report
562 fprintf (stderr
, _("time is in ticks, not seconds\n"));
568 fprintf (stderr
, _("%s: don't know how to deal with file format %d\n"),
569 whoami
, file_format
);
576 if (output_style
& STYLE_GMON_INFO
)
578 printf (_("File `%s' (version %d) contains:\n"),
579 filename
, gmon_file_version
);
581 _("\t%d histogram record\n") :
582 _("\t%d histogram records\n"), nhist
);
584 _("\t%d call-graph record\n") :
585 _("\t%d call-graph records\n"), narcs
);
587 _("\t%d basic-block count record\n") :
588 _("\t%d basic-block count records\n"), nbbs
);
589 first_output
= FALSE
;
595 gmon_out_write (const char *filename
)
598 struct gmon_hdr ghdr
;
600 ofp
= fopen (filename
, FOPEN_WB
);
607 if (file_format
== FF_AUTO
|| file_format
== FF_MAGIC
)
609 /* Write gmon header. */
611 memcpy (&ghdr
.cookie
[0], GMON_MAGIC
, 4);
612 bfd_put_32 (core_bfd
, (bfd_vma
) GMON_VERSION
, (bfd_byte
*) ghdr
.version
);
614 if (fwrite (&ghdr
, sizeof (ghdr
), 1, ofp
) != 1)
620 /* Write execution time histogram if we have one. */
621 if (gmon_input
& INPUT_HISTOGRAM
)
622 hist_write_hist (ofp
, filename
);
624 /* Write call graph arcs if we have any. */
625 if (gmon_input
& INPUT_CALL_GRAPH
)
626 cg_write_arcs (ofp
, filename
);
628 /* Write basic-block info if we have it. */
629 if (gmon_input
& INPUT_BB_COUNTS
)
630 bb_write_blocks (ofp
, filename
);
632 else if (file_format
== FF_BSD
|| file_format
== FF_BSD44
)
635 unsigned int i
, hdrsize
;
641 memset (pad
, 0, sizeof (pad
));
644 /* Decide how large the header will be. Use the 4.4BSD format
645 header if explicitly specified, or if the profiling rate is
646 non-standard. Otherwise, use the old BSD format. */
647 if (file_format
== FF_BSD44
651 switch (gmon_get_ptr_size ())
654 hdrsize
= GMON_HDRSIZE_BSD44_32
;
658 hdrsize
= GMON_HDRSIZE_BSD44_64
;
665 switch (gmon_get_ptr_size ())
668 hdrsize
= GMON_HDRSIZE_OLDBSD_32
;
672 hdrsize
= GMON_HDRSIZE_OLDBSD_64
;
673 /* FIXME: Checking host compiler defines here means that we can't
674 use a cross gprof alpha OSF. */
675 #if defined(__alpha__) && defined (__osf__)
682 /* Write the parts of the headers that are common to both the
683 old BSD and 4.4BSD formats. */
684 if (gmon_io_write_vma (ofp
, histograms
->lowpc
)
685 || gmon_io_write_vma (ofp
, histograms
->highpc
)
686 || gmon_io_write_32 (ofp
, histograms
->num_bins
687 * sizeof (UNIT
) + hdrsize
))
693 /* Write out the 4.4BSD header bits, if that's what we're using. */
694 if (file_format
== FF_BSD44
697 if (gmon_io_write_32 (ofp
, GMONVERSION
)
698 || gmon_io_write_32 (ofp
, (unsigned int) hz
))
705 /* Now write out any necessary padding after the meaningful
708 && fwrite (pad
, 1, padsize
, ofp
) != padsize
)
714 /* Dump the samples. */
715 for (i
= 0; i
< histograms
->num_bins
; ++i
)
717 bfd_put_16 (core_bfd
, (bfd_vma
) histograms
->sample
[i
],
718 (bfd_byte
*) &raw_bin_count
[0]);
719 if (fwrite (&raw_bin_count
[0], sizeof (raw_bin_count
), 1, ofp
) != 1)
726 /* Dump the normalized raw arc information. */
727 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
729 for (arc
= sym
->cg
.children
; arc
; arc
= arc
->next_child
)
731 if (gmon_write_raw_arc (ofp
, arc
->parent
->addr
,
732 arc
->child
->addr
, arc
->count
))
738 printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
739 (unsigned long) arc
->parent
->addr
,
740 (unsigned long) arc
->child
->addr
, arc
->count
));
748 fprintf (stderr
, _("%s: don't know how to deal with file format %d\n"),
749 whoami
, file_format
);