1 /* gmon_io.c - Input and output from/to gmon.out files.
3 Copyright (C) 2000 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. */
38 /* This probably ought to be in libbfd. */
41 DEFUN (get_vma
, (abfd
, addr
), bfd
* abfd AND bfd_byte
* addr
)
43 switch (sizeof (char*))
46 return bfd_get_32 (abfd
, addr
);
48 return bfd_get_64 (abfd
, addr
);
50 fprintf (stderr
, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
51 whoami
, (long) sizeof (char*));
57 /* This probably ought to be in libbfd. */
60 DEFUN (put_vma
, (abfd
, val
, addr
), bfd
* abfd AND bfd_vma val AND bfd_byte
* addr
)
62 switch (sizeof (char*))
65 bfd_put_32 (abfd
, val
, addr
);
68 bfd_put_64 (abfd
, val
, addr
);
71 fprintf (stderr
, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
72 whoami
, (long) sizeof (char*));
79 DEFUN (gmon_out_read
, (filename
), const char *filename
)
84 int nhist
= 0, narcs
= 0, nbbs
= 0;
86 /* Open gmon.out file. */
87 if (strcmp (filename
, "-") == 0)
91 SET_BINARY (fileno (stdin
));
96 ifp
= fopen (filename
, FOPEN_RB
);
105 if (fread (&ghdr
, sizeof (struct gmon_hdr
), 1, ifp
) != 1)
107 fprintf (stderr
, _("%s: file too short to be a gmon file\n"),
112 if ((file_format
== FF_MAGIC
) ||
113 (file_format
== FF_AUTO
&& !strncmp (&ghdr
.cookie
[0], GMON_MAGIC
, 4)))
115 if (file_format
== FF_MAGIC
&& strncmp (&ghdr
.cookie
[0], GMON_MAGIC
, 4))
117 fprintf (stderr
, _("%s: file `%s' has bad magic cookie\n"),
122 /* Right magic, so it's probably really a new gmon.out file. */
123 gmon_file_version
= bfd_get_32 (core_bfd
, (bfd_byte
*) ghdr
.version
);
125 if (gmon_file_version
!= GMON_VERSION
&& gmon_file_version
!= 0)
128 _("%s: file `%s' has unsupported version %d\n"),
129 whoami
, filename
, gmon_file_version
);
133 /* Read in all the records. */
134 while (fread (&tag
, sizeof (tag
), 1, ifp
) == 1)
138 case GMON_TAG_TIME_HIST
:
140 gmon_input
|= INPUT_HISTOGRAM
;
141 hist_read_rec (ifp
, filename
);
144 case GMON_TAG_CG_ARC
:
146 gmon_input
|= INPUT_CALL_GRAPH
;
147 cg_read_rec (ifp
, filename
);
150 case GMON_TAG_BB_COUNT
:
152 gmon_input
|= INPUT_BB_COUNTS
;
153 bb_read_rec (ifp
, filename
);
158 _("%s: %s: found bad tag %d (file corrupted?)\n"),
159 whoami
, filename
, tag
);
164 else if (file_format
== FF_AUTO
165 || file_format
== FF_BSD
166 || file_format
== FF_BSD44
)
174 int i
, samp_bytes
, header_size
;
176 bfd_vma from_pc
, self_pc
;
177 struct raw_arc raw_arc
;
183 /* Information from a gmon.out file is in two parts: an array of
184 sampling hits within pc ranges, and the arcs. */
185 gmon_input
= INPUT_HISTOGRAM
| INPUT_CALL_GRAPH
;
187 /* This fseek() ought to work even on stdin as long as it's
188 not an interactive device (heck, is there anybody who would
189 want to type in a gmon.out at the terminal?). */
190 if (fseek (ifp
, 0, SEEK_SET
) < 0)
196 if (fread (&raw
, 1, sizeof (struct raw_phdr
), ifp
)
197 != sizeof (struct raw_phdr
))
199 fprintf (stderr
, _("%s: file too short to be a gmon file\n"),
204 tmp
.low_pc
= get_vma (core_bfd
, (bfd_byte
*) &raw
.low_pc
[0]);
205 tmp
.high_pc
= get_vma (core_bfd
, (bfd_byte
*) &raw
.high_pc
[0]);
206 tmp
.ncnt
= bfd_get_32 (core_bfd
, (bfd_byte
*) &raw
.ncnt
[0]);
208 if (bfd_get_32 (core_bfd
, (bfd_byte
*) &raw
.version
[0])
213 /* 4.4BSD format header. */
214 profrate
= bfd_get_32 (core_bfd
, (bfd_byte
*) &raw
.profrate
[0]);
218 else if (hz
!= profrate
)
221 _("%s: profiling rate incompatible with first gmon file\n"),
226 header_size
= sizeof (struct raw_phdr
);
230 /* Old style BSD format. */
231 if (file_format
== FF_BSD44
)
233 fprintf (stderr
, _("%s: file `%s' has bad magic cookie\n"),
238 if (fseek (ifp
, sizeof (struct old_raw_phdr
), SEEK_SET
) < 0)
244 header_size
= sizeof (struct old_raw_phdr
);
247 if (s_highpc
&& (tmp
.low_pc
!= h
.low_pc
||
248 tmp
.high_pc
!= h
.high_pc
|| tmp
.ncnt
!= h
.ncnt
))
250 fprintf (stderr
, _("%s: incompatible with first gmon file\n"),
256 s_lowpc
= (bfd_vma
) h
.low_pc
;
257 s_highpc
= (bfd_vma
) h
.high_pc
;
258 lowpc
= (bfd_vma
) h
.low_pc
/ sizeof (UNIT
);
259 highpc
= (bfd_vma
) h
.high_pc
/ sizeof (UNIT
);
260 samp_bytes
= h
.ncnt
- header_size
;
261 hist_num_bins
= samp_bytes
/ sizeof (UNIT
);
264 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
265 (unsigned long) h
.low_pc
, (unsigned long) h
.high_pc
,
267 printf ("[gmon_out_read] s_lowpc 0x%lx s_highpc 0x%lx\n",
268 (unsigned long) s_lowpc
, (unsigned long) s_highpc
);
269 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx\n",
270 (unsigned long) lowpc
, (unsigned long) highpc
);
271 printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
272 samp_bytes
, hist_num_bins
));
274 /* Make sure that we have sensible values. */
275 if (samp_bytes
< 0 || lowpc
> highpc
)
278 _("%s: file '%s' does not appear to be in gmon.out format\n"),
289 (int *) xmalloc (hist_num_bins
* sizeof (hist_sample
[0]));
291 memset (hist_sample
, 0, hist_num_bins
* sizeof (hist_sample
[0]));
294 for (i
= 0; i
< hist_num_bins
; ++i
)
296 if (fread (raw_bin_count
, sizeof (raw_bin_count
), 1, ifp
) != 1)
299 _("%s: unexpected EOF after reading %d/%d bins\n"),
300 whoami
, --i
, hist_num_bins
);
304 hist_sample
[i
] += bfd_get_16 (core_bfd
, (bfd_byte
*) raw_bin_count
);
307 /* The rest of the file consists of a bunch of
308 <from,self,count> tuples. */
309 while (fread (&raw_arc
, sizeof (raw_arc
), 1, ifp
) == 1)
312 from_pc
= get_vma (core_bfd
, (bfd_byte
*) raw_arc
.from_pc
);
313 self_pc
= get_vma (core_bfd
, (bfd_byte
*) raw_arc
.self_pc
);
314 count
= bfd_get_32 (core_bfd
, (bfd_byte
*) raw_arc
.count
);
317 printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
318 (unsigned long) from_pc
, (unsigned long) self_pc
, count
));
321 cg_tally (from_pc
, self_pc
, count
);
328 /* How many ticks per second? If we can't tell, report
335 fprintf (stderr
, _("time is in ticks, not seconds\n"));
341 fprintf (stderr
, _("%s: don't know how to deal with file format %d\n"),
342 whoami
, file_format
);
346 if (output_style
& STYLE_GMON_INFO
)
348 printf (_("File `%s' (version %d) contains:\n"),
349 filename
, gmon_file_version
);
350 printf (_("\t%d histogram record%s\n"),
351 nhist
, nhist
== 1 ? "" : "s");
352 printf (_("\t%d call-graph record%s\n"),
353 narcs
, narcs
== 1 ? "" : "s");
354 printf (_("\t%d basic-block count record%s\n"),
355 nbbs
, nbbs
== 1 ? "" : "s");
356 first_output
= FALSE
;
362 DEFUN (gmon_out_write
, (filename
), const char *filename
)
365 struct gmon_hdr ghdr
;
367 ofp
= fopen (filename
, FOPEN_WB
);
374 if (file_format
== FF_AUTO
|| file_format
== FF_MAGIC
)
376 /* Write gmon header. */
378 memcpy (&ghdr
.cookie
[0], GMON_MAGIC
, 4);
379 bfd_put_32 (core_bfd
, GMON_VERSION
, (bfd_byte
*) ghdr
.version
);
381 if (fwrite (&ghdr
, sizeof (ghdr
), 1, ofp
) != 1)
387 /* Write execution time histogram if we have one. */
388 if (gmon_input
& INPUT_HISTOGRAM
)
389 hist_write_hist (ofp
, filename
);
391 /* Write call graph arcs if we have any. */
392 if (gmon_input
& INPUT_CALL_GRAPH
)
393 cg_write_arcs (ofp
, filename
);
395 /* Write basic-block info if we have it. */
396 if (gmon_input
& INPUT_BB_COUNTS
)
397 bb_write_blocks (ofp
, filename
);
399 else if (file_format
== FF_BSD
|| file_format
== FF_BSD44
)
401 struct raw_arc raw_arc
;
408 memset (&h
, 0, sizeof h
);
409 put_vma (core_bfd
, s_lowpc
, (bfd_byte
*) &h
.low_pc
);
410 put_vma (core_bfd
, s_highpc
, (bfd_byte
*) &h
.high_pc
);
411 bfd_put_32 (core_bfd
,
412 hist_num_bins
* sizeof (UNIT
) + sizeof (struct raw_phdr
),
413 (bfd_byte
*) &h
.ncnt
);
415 /* Write header. Use new style BSD format is explicitly
416 specified, or if the profiling rate is non-standard;
417 otherwise, use the old BSD format. */
418 if (file_format
== FF_BSD44
421 bfd_put_32 (core_bfd
, GMONVERSION
, (bfd_byte
*) &h
.version
);
422 bfd_put_32 (core_bfd
, hz
, (bfd_byte
*) &h
.profrate
);
423 if (fwrite (&h
, sizeof (struct raw_phdr
), 1, ofp
) != 1)
431 if (fwrite (&h
, sizeof (struct old_raw_phdr
), 1, ofp
) != 1)
438 /* Dump the samples. */
439 for (i
= 0; i
< hist_num_bins
; ++i
)
441 bfd_put_16 (core_bfd
, hist_sample
[i
], (bfd_byte
*) & raw_bin_count
[0]);
442 if (fwrite (&raw_bin_count
[0], sizeof (raw_bin_count
), 1, ofp
) != 1)
449 /* Dump the normalized raw arc information. */
450 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
452 for (arc
= sym
->cg
.children
; arc
; arc
= arc
->next_child
)
454 put_vma (core_bfd
, arc
->parent
->addr
,
455 (bfd_byte
*) raw_arc
.from_pc
);
456 put_vma (core_bfd
, arc
->child
->addr
,
457 (bfd_byte
*) raw_arc
.self_pc
);
458 bfd_put_32 (core_bfd
, arc
->count
, (bfd_byte
*) raw_arc
.count
);
459 if (fwrite (&raw_arc
, sizeof (raw_arc
), 1, ofp
) != 1)
465 printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
466 (unsigned long) arc
->parent
->addr
,
467 (unsigned long) arc
->child
->addr
, arc
->count
));
475 fprintf (stderr
, _("%s: don't know how to deal with file format %d\n"),
476 whoami
, file_format
);