2 * Input and output from/to gmon.out files.
5 #include "basic_blocks.h"
8 #include "call_graph.h"
11 #include "gmon.h" /* fetch header for old format */
15 #include "libiberty.h"
18 int gmon_file_version
= 0; /* 0 == old (non-versioned) file format */
21 * This probably ought to be in libbfd.
24 DEFUN (get_vma
, (abfd
, addr
), bfd
* abfd AND bfd_byte
* addr
)
26 switch (sizeof (char*))
29 return bfd_get_32 (abfd
, addr
);
31 return bfd_get_64 (abfd
, addr
);
33 fprintf (stderr
, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
34 whoami
, (long) sizeof (char*));
41 * This probably ought to be in libbfd.
44 DEFUN (put_vma
, (abfd
, val
, addr
), bfd
* abfd AND bfd_vma val AND bfd_byte
* addr
)
46 switch (sizeof (char*))
49 bfd_put_32 (abfd
, val
, addr
);
52 bfd_put_64 (abfd
, val
, addr
);
55 fprintf (stderr
, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
56 whoami
, (long) sizeof (char*));
63 DEFUN (gmon_out_read
, (filename
), const char *filename
)
68 int nhist
= 0, narcs
= 0, nbbs
= 0;
70 /* open gmon.out file: */
72 if (strcmp (filename
, "-") == 0)
78 ifp
= fopen (filename
, FOPEN_RB
);
85 if (fread (&ghdr
, sizeof (struct gmon_hdr
), 1, ifp
) != 1)
87 fprintf (stderr
, _("%s: file too short to be a gmon file\n"),
92 if ((file_format
== FF_MAGIC
) ||
93 (file_format
== FF_AUTO
&& !strncmp (&ghdr
.cookie
[0], GMON_MAGIC
, 4)))
95 if (file_format
== FF_MAGIC
&& strncmp (&ghdr
.cookie
[0], GMON_MAGIC
, 4))
97 fprintf (stderr
, _("%s: file `%s' has bad magic cookie\n"),
102 /* right magic, so it's probably really a new gmon.out file */
104 gmon_file_version
= bfd_get_32 (core_bfd
, (bfd_byte
*) ghdr
.version
);
105 if (gmon_file_version
!= GMON_VERSION
&& gmon_file_version
!= 0)
108 _("%s: file `%s' has unsupported version %d\n"),
109 whoami
, filename
, gmon_file_version
);
113 /* read in all the records: */
114 while (fread (&tag
, sizeof (tag
), 1, ifp
) == 1)
118 case GMON_TAG_TIME_HIST
:
120 gmon_input
|= INPUT_HISTOGRAM
;
121 hist_read_rec (ifp
, filename
);
124 case GMON_TAG_CG_ARC
:
126 gmon_input
|= INPUT_CALL_GRAPH
;
127 cg_read_rec (ifp
, filename
);
130 case GMON_TAG_BB_COUNT
:
132 gmon_input
|= INPUT_BB_COUNTS
;
133 bb_read_rec (ifp
, filename
);
138 _("%s: %s: found bad tag %d (file corrupted?)\n"),
139 whoami
, filename
, tag
);
144 else if (file_format
== FF_AUTO
|| file_format
== FF_BSD
)
154 bfd_vma from_pc
, self_pc
;
155 struct raw_arc raw_arc
;
162 * Information from a gmon.out file is in two parts: an array of
163 * sampling hits within pc ranges, and the arcs.
165 gmon_input
= INPUT_HISTOGRAM
| INPUT_CALL_GRAPH
;
168 * This fseek() ought to work even on stdin as long as it's
169 * not an interactive device (heck, is there anybody who would
170 * want to type in a gmon.out at the terminal?).
172 if (fseek (ifp
, 0, SEEK_SET
) < 0)
177 if (fread (&raw
, 1, sizeof (struct raw_phdr
), ifp
)
178 != sizeof (struct raw_phdr
))
180 fprintf (stderr
, _("%s: file too short to be a gmon file\n"),
184 tmp
.low_pc
= get_vma (core_bfd
, (bfd_byte
*) &raw
.low_pc
[0]);
185 tmp
.high_pc
= get_vma (core_bfd
, (bfd_byte
*) &raw
.high_pc
[0]);
186 tmp
.ncnt
= bfd_get_32 (core_bfd
, (bfd_byte
*) &raw
.ncnt
[0]);
192 profrate
= bfd_get_32 (core_bfd
, (bfd_byte
*) &raw
.profrate
[0]);
195 else if (hz
!= profrate
)
198 _("%s: profiling rate incompatible with first gmon file\n"),
205 if (s_highpc
&& (tmp
.low_pc
!= h
.low_pc
||
206 tmp
.high_pc
!= h
.high_pc
|| tmp
.ncnt
!= h
.ncnt
))
208 fprintf (stderr
, _("%s: incompatible with first gmon file\n"),
213 s_lowpc
= (bfd_vma
) h
.low_pc
;
214 s_highpc
= (bfd_vma
) h
.high_pc
;
215 lowpc
= (bfd_vma
) h
.low_pc
/ sizeof (UNIT
);
216 highpc
= (bfd_vma
) h
.high_pc
/ sizeof (UNIT
);
217 samp_bytes
= h
.ncnt
- sizeof (struct raw_phdr
);
218 hist_num_bins
= samp_bytes
/ sizeof (UNIT
);
220 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
221 h
.low_pc
, h
.high_pc
, h
.ncnt
);
222 printf ("[gmon_out_read] s_lowpc 0x%lx s_highpc 0x%lx\n",
224 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx\n",
226 printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
227 samp_bytes
, hist_num_bins
));
237 (int *) xmalloc (hist_num_bins
* sizeof (hist_sample
[0]));
238 memset (hist_sample
, 0, hist_num_bins
* sizeof (hist_sample
[0]));
241 for (i
= 0; i
< hist_num_bins
; ++i
)
243 if (fread (raw_bin_count
, sizeof (raw_bin_count
), 1, ifp
) != 1)
246 _("%s: unexpected EOF after reading %d/%d bins\n"),
247 whoami
, --i
, hist_num_bins
);
250 hist_sample
[i
] += bfd_get_16 (core_bfd
, (bfd_byte
*) raw_bin_count
);
254 * The rest of the file consists of a bunch of <from,self,count>
257 while (fread (&raw_arc
, sizeof (raw_arc
), 1, ifp
) == 1)
260 from_pc
= get_vma (core_bfd
, (bfd_byte
*) raw_arc
.from_pc
);
261 self_pc
= get_vma (core_bfd
, (bfd_byte
*) raw_arc
.self_pc
);
262 count
= bfd_get_32 (core_bfd
, (bfd_byte
*) raw_arc
.count
);
264 printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
265 from_pc
, self_pc
, count
));
267 cg_tally (from_pc
, self_pc
, count
);
274 * How many ticks per second? If we can't tell, report
281 fprintf (stderr
, _("time is in ticks, not seconds\n"));
287 fprintf (stderr
, _("%s: don't know how to deal with file format %d\n"),
288 whoami
, file_format
);
292 if (output_style
& STYLE_GMON_INFO
)
294 printf (_("File `%s' (version %d) contains:\n"),
295 filename
, gmon_file_version
);
296 printf (_("\t%d histogram record%s\n"),
297 nhist
, nhist
== 1 ? "" : "s");
298 printf (_("\t%d call-graph record%s\n"),
299 narcs
, narcs
== 1 ? "" : "s");
300 printf (_("\t%d basic-block count record%s\n"),
301 nbbs
, nbbs
== 1 ? "" : "s");
302 first_output
= FALSE
;
308 DEFUN (gmon_out_write
, (filename
), const char *filename
)
311 struct gmon_hdr ghdr
;
313 ofp
= fopen (filename
, FOPEN_WB
);
320 if (file_format
== FF_AUTO
|| file_format
== FF_MAGIC
)
322 /* write gmon header: */
324 memcpy (&ghdr
.cookie
[0], GMON_MAGIC
, 4);
325 bfd_put_32 (core_bfd
, GMON_VERSION
, (bfd_byte
*) ghdr
.version
);
326 if (fwrite (&ghdr
, sizeof (ghdr
), 1, ofp
) != 1)
332 /* write execution time histogram if we have one: */
333 if (gmon_input
& INPUT_HISTOGRAM
)
335 hist_write_hist (ofp
, filename
);
338 /* write call graph arcs if we have any: */
339 if (gmon_input
& INPUT_CALL_GRAPH
)
341 cg_write_arcs (ofp
, filename
);
344 /* write basic-block info if we have it: */
345 if (gmon_input
& INPUT_BB_COUNTS
)
347 bb_write_blocks (ofp
, filename
);
350 else if (file_format
== FF_BSD
)
352 struct raw_arc raw_arc
;
359 put_vma (core_bfd
, s_lowpc
, (bfd_byte
*) & lpc
);
360 put_vma (core_bfd
, s_highpc
, (bfd_byte
*) & hpc
);
361 bfd_put_32 (core_bfd
,
362 hist_num_bins
* sizeof (UNIT
) + sizeof (struct raw_phdr
),
363 (bfd_byte
*) & ncnt
);
366 if (fwrite (&lpc
, sizeof (lpc
), 1, ofp
) != 1
367 || fwrite (&hpc
, sizeof (hpc
), 1, ofp
) != 1
368 || fwrite (&ncnt
, sizeof (ncnt
), 1, ofp
) != 1)
374 /* dump the samples: */
376 for (i
= 0; i
< hist_num_bins
; ++i
)
378 bfd_put_16 (core_bfd
, hist_sample
[i
], (bfd_byte
*) & raw_bin_count
[0]);
379 if (fwrite (&raw_bin_count
[0], sizeof (raw_bin_count
), 1, ofp
) != 1)
386 /* dump the normalized raw arc information: */
388 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
390 for (arc
= sym
->cg
.children
; arc
; arc
= arc
->next_child
)
392 put_vma (core_bfd
, arc
->parent
->addr
,
393 (bfd_byte
*) raw_arc
.from_pc
);
394 put_vma (core_bfd
, arc
->child
->addr
,
395 (bfd_byte
*) raw_arc
.self_pc
);
396 bfd_put_32 (core_bfd
, arc
->count
, (bfd_byte
*) raw_arc
.count
);
397 if (fwrite (&raw_arc
, sizeof (raw_arc
), 1, ofp
) != 1)
403 printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
404 arc
->parent
->addr
, arc
->child
->addr
, arc
->count
));
411 fprintf (stderr
, _("%s: don't know how to deal with file format %d\n"),
412 whoami
, file_format
);