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(bfd_vma
)) {
27 case 4: return bfd_get_32(abfd
, addr
);
28 case 8: return bfd_get_64(abfd
, addr
);
30 fprintf(stderr
, "%s: bfd_vma has unexpected size of %ld bytes\n",
31 whoami
, (long) sizeof(bfd_vma
));
38 * This probably ought to be in libbfd.
41 DEFUN(put_vma
, (abfd
, val
, addr
), bfd
*abfd AND bfd_vma val AND bfd_byte
*addr
)
43 switch (sizeof(bfd_vma
)) {
44 case 4: bfd_put_32(abfd
, val
, addr
); break;
45 case 8: bfd_put_64(abfd
, val
, addr
); break;
47 fprintf(stderr
, "%s: bfd_vma has unexpected size of %ld bytes\n",
48 whoami
, (long) sizeof(bfd_vma
));
55 DEFUN(gmon_out_read
, (filename
), const char *filename
)
60 int nhist
= 0, narcs
= 0, nbbs
= 0;
62 /* open gmon.out file: */
64 if (strcmp(filename
, "-") == 0) {
67 ifp
= fopen(filename
, FOPEN_RB
);
73 if (fread(&ghdr
, sizeof(struct gmon_hdr
), 1, ifp
) != 1) {
74 fprintf(stderr
, "%s: file too short to be a gmon file\n",
79 if ((file_format
== FF_MAGIC
) ||
80 (file_format
== FF_AUTO
&& !strncmp(&ghdr
.cookie
[0], GMON_MAGIC
, 4)))
82 if (file_format
== FF_MAGIC
&& strncmp(&ghdr
.cookie
[0], GMON_MAGIC
, 4))
84 fprintf(stderr
, "%s: file `%s' has bad magic cookie\n",
89 /* right magic, so it's probably really a new gmon.out file */
91 gmon_file_version
= bfd_get_32(core_bfd
, (bfd_byte
*) ghdr
.version
);
92 if (gmon_file_version
!= GMON_VERSION
&& gmon_file_version
!= 0) {
94 "%s: file `%s' has unsupported version %d\n",
95 whoami
, filename
, gmon_file_version
);
99 /* read in all the records: */
100 while (fread(&tag
, sizeof(tag
), 1, ifp
) == 1) {
102 case GMON_TAG_TIME_HIST
:
104 gmon_input
|= INPUT_HISTOGRAM
;
105 hist_read_rec(ifp
, filename
);
108 case GMON_TAG_CG_ARC
:
110 gmon_input
|= INPUT_CALL_GRAPH
;
111 cg_read_rec(ifp
, filename
);
114 case GMON_TAG_BB_COUNT
:
116 gmon_input
|= INPUT_BB_COUNTS
;
117 bb_read_rec(ifp
, filename
);
122 "%s: %s: found bad tag %d (file corrupted?)\n",
123 whoami
, filename
, tag
);
127 } else if (file_format
== FF_AUTO
|| file_format
== FF_BSD
) {
133 int i
, samp_bytes
, count
;
134 bfd_vma from_pc
, self_pc
;
135 struct raw_arc raw_arc
;
142 * Information from a gmon.out file is in two parts: an array of
143 * sampling hits within pc ranges, and the arcs.
145 gmon_input
= INPUT_HISTOGRAM
| INPUT_CALL_GRAPH
;
148 * This fseek() ought to work even on stdin as long as it's
149 * not an interactive device (heck, is there anybody who would
150 * want to type in a gmon.out at the terminal?).
152 if (fseek(ifp
, 0, SEEK_SET
) < 0) {
156 if (fread(&raw
, 1, sizeof(struct raw_phdr
), ifp
)
157 != sizeof(struct raw_phdr
))
159 fprintf(stderr
, "%s: file too short to be a gmon file\n",
163 tmp
.low_pc
= get_vma(core_bfd
, (bfd_byte
*) &raw
.low_pc
[0]);
164 tmp
.high_pc
= get_vma(core_bfd
, (bfd_byte
*) &raw
.high_pc
[0]);
165 tmp
.ncnt
= bfd_get_32(core_bfd
, (bfd_byte
*) &raw
.ncnt
[0]);
166 if (s_highpc
&& (tmp
.low_pc
!= h
.low_pc
||
167 tmp
.high_pc
!= h
.high_pc
|| tmp
.ncnt
!= h
.ncnt
))
169 fprintf(stderr
, "%s: incompatible with first gmon file\n",
174 s_lowpc
= (bfd_vma
) h
.low_pc
;
175 s_highpc
= (bfd_vma
) h
.high_pc
;
176 lowpc
= (bfd_vma
) h
.low_pc
/ sizeof(UNIT
);
177 highpc
= (bfd_vma
) h
.high_pc
/ sizeof(UNIT
);
178 samp_bytes
= h
.ncnt
- sizeof(struct raw_phdr
);
179 hist_num_bins
= samp_bytes
/ sizeof (UNIT
);
181 printf("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
182 h
.low_pc
, h
.high_pc
, h
.ncnt
);
183 printf("[gmon_out_read] s_lowpc 0x%lx s_highpc 0x%lx\n",
185 printf("[gmon_out_read] lowpc 0x%lx highpc 0x%lx\n",
187 printf("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
188 samp_bytes
, hist_num_bins
));
196 (int*) xmalloc(hist_num_bins
* sizeof(hist_sample
[0]));
197 memset(hist_sample
, 0, hist_num_bins
* sizeof(hist_sample
[0]));
200 for (i
= 0; i
< hist_num_bins
; ++i
) {
201 if (fread(raw_bin_count
, sizeof(raw_bin_count
), 1, ifp
) != 1) {
203 "%s: unexpected EOF after reading %d/%d bins\n",
204 whoami
, --i
, hist_num_bins
);
207 hist_sample
[i
] += bfd_get_16(core_bfd
, (bfd_byte
*) raw_bin_count
);
211 * The rest of the file consists of a bunch of <from,self,count>
214 while (fread(&raw_arc
, sizeof(raw_arc
), 1, ifp
) == 1) {
216 from_pc
= get_vma(core_bfd
, (bfd_byte
*) raw_arc
.from_pc
);
217 self_pc
= get_vma(core_bfd
, (bfd_byte
*) raw_arc
.self_pc
);
218 count
= bfd_get_32(core_bfd
, (bfd_byte
*) raw_arc
.count
);
220 printf("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %d\n",
221 from_pc
, self_pc
, count
));
223 cg_tally(from_pc
, self_pc
, count
);
227 if (hz
== HZ_WRONG
) {
229 * How many ticks per second? If we can't tell, report
233 if (hz
== HZ_WRONG
) {
235 fprintf(stderr
, "time is in ticks, not seconds\n");
239 fprintf(stderr
, "%s: don't know how to deal with file format %d\n",
240 whoami
, file_format
);
244 if (output_style
& STYLE_GMON_INFO
) {
245 printf("File `%s' (version %d) contains:\n",
246 filename
, gmon_file_version
);
247 printf("\t%d histogram record%s\n",
248 nhist
, nhist
== 1 ? "" : "s");
249 printf("\t%d call-graph record%s\n",
250 narcs
, narcs
== 1 ? "" : "s");
251 printf("\t%d basic-block count record%s\n",
252 nbbs
, nbbs
== 1 ? "" : "s");
253 first_output
= FALSE
;
255 } /* gmon_out_read */
259 DEFUN(gmon_out_write
, (filename
), const char *filename
)
262 struct gmon_hdr ghdr
;
264 ofp
= fopen(filename
, FOPEN_WB
);
270 if (file_format
== FF_AUTO
|| file_format
== FF_MAGIC
) {
271 /* write gmon header: */
273 memcpy(&ghdr
.cookie
[0], GMON_MAGIC
, 4);
274 bfd_put_32(core_bfd
, GMON_VERSION
, (bfd_byte
*) ghdr
.version
);
275 if (fwrite(&ghdr
, sizeof(ghdr
), 1, ofp
) != 1) {
280 /* write execution time histogram if we have one: */
281 if (gmon_input
& INPUT_HISTOGRAM
) {
282 hist_write_hist(ofp
, filename
);
285 /* write call graph arcs if we have any: */
286 if (gmon_input
& INPUT_CALL_GRAPH
) {
287 cg_write_arcs(ofp
, filename
);
290 /* write basic-block info if we have it: */
291 if (gmon_input
& INPUT_BB_COUNTS
) {
292 bb_write_blocks(ofp
, filename
);
294 } else if (file_format
== FF_BSD
) {
295 struct raw_arc raw_arc
;
302 put_vma(core_bfd
, s_lowpc
, (bfd_byte
*) &lpc
);
303 put_vma(core_bfd
, s_highpc
, (bfd_byte
*) &hpc
);
305 hist_num_bins
* sizeof(UNIT
) + sizeof(struct raw_phdr
),
309 if (fwrite(&lpc
, sizeof(lpc
), 1, ofp
) != 1
310 || fwrite(&hpc
, sizeof(hpc
), 1, ofp
) != 1
311 || fwrite(&ncnt
, sizeof(ncnt
), 1, ofp
) != 1)
317 /* dump the samples: */
319 for (i
= 0; i
< hist_num_bins
; ++i
) {
320 bfd_put_16(core_bfd
, hist_sample
[i
], (bfd_byte
*)&raw_bin_count
[0]);
321 if (fwrite(&raw_bin_count
[0], sizeof(raw_bin_count
), 1, ofp
) != 1)
328 /* dump the normalized raw arc information: */
330 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
) {
331 for (arc
= sym
->cg
.children
; arc
; arc
= arc
->next_child
) {
332 put_vma(core_bfd
, arc
->parent
->addr
,
333 (bfd_byte
*) raw_arc
.from_pc
);
334 put_vma(core_bfd
, arc
->child
->addr
,
335 (bfd_byte
*) raw_arc
.self_pc
);
336 bfd_put_32(core_bfd
, arc
->count
, (bfd_byte
*) raw_arc
.count
);
337 if (fwrite(&raw_arc
, sizeof(raw_arc
), 1, ofp
) != 1) {
342 printf("[dumpsum] frompc 0x%lx selfpc 0x%lx count %d\n",
343 arc
->parent
->addr
, arc
->child
->addr
, arc
->count
));
348 fprintf(stderr
, "%s: don't know how to deal with file format %d\n",
349 whoami
, file_format
);
352 } /* gmon_out_write */