1 /* gmon_io.c - Input and output from/to gmon.out files.
3 Copyright 2000, 2001 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 "search_list.h"
27 #include "basic_blocks.h"
29 #include "call_graph.h"
32 #include "gmon.h" /* Fetch header for old format. */
35 #include "libiberty.h"
38 int gmon_file_version = 0; /* 0 == old (non-versioned) file format. */
41 DEFUN (gmon_io_read_vma, (ifp, valp), FILE * ifp AND bfd_vma *valp)
46 switch (GMON_PTR_SIZE)
49 if (fread (buf, 1, 4, ifp) != 4)
51 val = bfd_get_32 (core_bfd, buf);
55 if (fread (buf, 1, 8, ifp) != 8)
57 val = bfd_get_64 (core_bfd, buf);
61 fprintf (stderr, _("%s: GMON_PTR_SIZE has unexpected value of %u\n"),
62 whoami, GMON_PTR_SIZE);
70 DEFUN (gmon_io_read_32, (ifp, valp), FILE * ifp AND unsigned int *valp)
74 if (fread (buf, 1, 4, ifp) != 4)
76 *valp = bfd_get_32 (core_bfd, buf);
81 DEFUN (gmon_io_read, (ifp, buf, n), FILE * ifp AND char *buf AND size_t n)
83 if (fread (buf, 1, n, ifp) != n)
89 DEFUN (gmon_io_write_vma, (ofp, val), FILE * ofp AND bfd_vma val)
93 switch (GMON_PTR_SIZE)
96 bfd_put_32 (core_bfd, val, buf);
97 if (fwrite (buf, 1, 4, ofp) != 4)
102 bfd_put_64 (core_bfd, val, buf);
103 if (fwrite (buf, 1, 8, ofp) != 8)
108 fprintf (stderr, _("%s: GMON_PTR_SIZE has unexpected value of %u\n"),
109 whoami, GMON_PTR_SIZE);
116 DEFUN (gmon_io_write_32, (ofp, val), FILE * ofp AND unsigned int val)
120 bfd_put_32 (core_bfd, val, buf);
121 if (fwrite (buf, 1, 4, ofp) != 4)
127 DEFUN (gmon_io_write_8, (ofp, val), FILE * ofp AND unsigned char val)
131 bfd_put_8 (core_bfd, val, buf);
132 if (fwrite (buf, 1, 1, ofp) != 1)
138 DEFUN (gmon_io_write, (ofp, buf, n), FILE * ofp AND char *buf AND size_t n)
140 if (fwrite (buf, 1, n, ofp) != n)
145 /* get_vma and put_vma are for backwards compatibility only */
147 DEFUN (get_vma, (abfd, addr), bfd * abfd AND bfd_byte * addr)
149 switch (sizeof (char*))
152 return bfd_get_32 (abfd, addr);
154 return bfd_get_64 (abfd, addr);
156 fprintf (stderr, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
157 whoami, (long) sizeof (char*));
163 DEFUN (put_vma, (abfd, val, addr), bfd * abfd AND bfd_vma val AND bfd_byte * addr)
165 switch (sizeof (char*))
168 bfd_put_32 (abfd, val, addr);
171 bfd_put_64 (abfd, val, addr);
174 fprintf (stderr, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
175 whoami, (long) sizeof (char*));
181 DEFUN (gmon_out_read, (filename), const char *filename)
184 struct gmon_hdr ghdr;
186 int nhist = 0, narcs = 0, nbbs = 0;
188 /* Open gmon.out file. */
189 if (strcmp (filename, "-") == 0)
193 SET_BINARY (fileno (stdin));
198 ifp = fopen (filename, FOPEN_RB);
207 if (fread (&ghdr, sizeof (struct gmon_hdr), 1, ifp) != 1)
209 fprintf (stderr, _("%s: file too short to be a gmon file\n"),
214 if ((file_format == FF_MAGIC)
215 || (file_format == FF_AUTO && !strncmp (&ghdr.cookie[0], GMON_MAGIC, 4)))
217 if (file_format == FF_MAGIC && strncmp (&ghdr.cookie[0], GMON_MAGIC, 4))
219 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
224 /* Right magic, so it's probably really a new gmon.out file. */
225 gmon_file_version = bfd_get_32 (core_bfd, (bfd_byte *) ghdr.version);
227 if (gmon_file_version != GMON_VERSION && gmon_file_version != 0)
230 _("%s: file `%s' has unsupported version %d\n"),
231 whoami, filename, gmon_file_version);
235 /* Read in all the records. */
236 while (fread (&tag, sizeof (tag), 1, ifp) == 1)
240 case GMON_TAG_TIME_HIST:
242 gmon_input |= INPUT_HISTOGRAM;
243 hist_read_rec (ifp, filename);
246 case GMON_TAG_CG_ARC:
248 gmon_input |= INPUT_CALL_GRAPH;
249 cg_read_rec (ifp, filename);
252 case GMON_TAG_BB_COUNT:
254 gmon_input |= INPUT_BB_COUNTS;
255 bb_read_rec (ifp, filename);
260 _("%s: %s: found bad tag %d (file corrupted?)\n"),
261 whoami, filename, tag);
266 else if (file_format == FF_AUTO
267 || file_format == FF_BSD
268 || file_format == FF_BSD44)
276 int i, samp_bytes, header_size;
278 bfd_vma from_pc, self_pc;
279 struct raw_arc raw_arc;
285 /* Information from a gmon.out file is in two parts: an array of
286 sampling hits within pc ranges, and the arcs. */
287 gmon_input = INPUT_HISTOGRAM | INPUT_CALL_GRAPH;
289 /* This fseek() ought to work even on stdin as long as it's
290 not an interactive device (heck, is there anybody who would
291 want to type in a gmon.out at the terminal?). */
292 if (fseek (ifp, 0, SEEK_SET) < 0)
298 if (fread (&raw, 1, sizeof (struct raw_phdr), ifp)
299 != sizeof (struct raw_phdr))
301 fprintf (stderr, _("%s: file too short to be a gmon file\n"),
306 tmp.low_pc = get_vma (core_bfd, (bfd_byte *) &raw.low_pc[0]);
307 tmp.high_pc = get_vma (core_bfd, (bfd_byte *) &raw.high_pc[0]);
308 tmp.ncnt = bfd_get_32 (core_bfd, (bfd_byte *) &raw.ncnt[0]);
310 if (bfd_get_32 (core_bfd, (bfd_byte *) &raw.version[0])
315 /* 4.4BSD format header. */
316 profrate = bfd_get_32 (core_bfd, (bfd_byte *) &raw.profrate[0]);
320 else if (hz != profrate)
323 _("%s: profiling rate incompatible with first gmon file\n"),
328 header_size = sizeof (struct raw_phdr);
332 /* Old style BSD format. */
333 if (file_format == FF_BSD44)
335 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
340 if (fseek (ifp, sizeof (struct old_raw_phdr), SEEK_SET) < 0)
346 header_size = sizeof (struct old_raw_phdr);
349 if (s_highpc && (tmp.low_pc != h.low_pc
350 || tmp.high_pc != h.high_pc || tmp.ncnt != h.ncnt))
352 fprintf (stderr, _("%s: incompatible with first gmon file\n"),
358 s_lowpc = (bfd_vma) h.low_pc;
359 s_highpc = (bfd_vma) h.high_pc;
360 lowpc = (bfd_vma) h.low_pc / sizeof (UNIT);
361 highpc = (bfd_vma) h.high_pc / sizeof (UNIT);
362 samp_bytes = h.ncnt - header_size;
363 hist_num_bins = samp_bytes / sizeof (UNIT);
366 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
367 (unsigned long) h.low_pc, (unsigned long) h.high_pc,
369 printf ("[gmon_out_read] s_lowpc 0x%lx s_highpc 0x%lx\n",
370 (unsigned long) s_lowpc, (unsigned long) s_highpc);
371 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx\n",
372 (unsigned long) lowpc, (unsigned long) highpc);
373 printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
374 samp_bytes, hist_num_bins));
376 /* Make sure that we have sensible values. */
377 if (samp_bytes < 0 || lowpc > highpc)
380 _("%s: file '%s' does not appear to be in gmon.out format\n"),
391 (int *) xmalloc (hist_num_bins * sizeof (hist_sample[0]));
393 memset (hist_sample, 0, hist_num_bins * sizeof (hist_sample[0]));
396 for (i = 0; i < hist_num_bins; ++i)
398 if (fread (raw_bin_count, sizeof (raw_bin_count), 1, ifp) != 1)
401 _("%s: unexpected EOF after reading %d/%d bins\n"),
402 whoami, --i, hist_num_bins);
406 hist_sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) raw_bin_count);
409 /* The rest of the file consists of a bunch of
410 <from,self,count> tuples. */
411 while (fread (&raw_arc, sizeof (raw_arc), 1, ifp) == 1)
414 from_pc = get_vma (core_bfd, (bfd_byte *) raw_arc.from_pc);
415 self_pc = get_vma (core_bfd, (bfd_byte *) raw_arc.self_pc);
416 count = bfd_get_32 (core_bfd, (bfd_byte *) raw_arc.count);
419 printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
420 (unsigned long) from_pc, (unsigned long) self_pc, count));
423 cg_tally (from_pc, self_pc, count);
430 /* How many ticks per second? If we can't tell, report
437 fprintf (stderr, _("time is in ticks, not seconds\n"));
443 fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
444 whoami, file_format);
448 if (output_style & STYLE_GMON_INFO)
450 printf (_("File `%s' (version %d) contains:\n"),
451 filename, gmon_file_version);
453 _("\t%d histogram record\n") :
454 _("\t%d histogram records\n"), nhist);
456 _("\t%d call-graph record\n") :
457 _("\t%d call-graph records\n"), narcs);
459 _("\t%d basic-block count record\n") :
460 _("\t%d basic-block count records\n"), nbbs);
461 first_output = FALSE;
467 DEFUN (gmon_out_write, (filename), const char *filename)
470 struct gmon_hdr ghdr;
472 ofp = fopen (filename, FOPEN_WB);
479 if (file_format == FF_AUTO || file_format == FF_MAGIC)
481 /* Write gmon header. */
483 memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
484 bfd_put_32 (core_bfd, GMON_VERSION, (bfd_byte *) ghdr.version);
486 if (fwrite (&ghdr, sizeof (ghdr), 1, ofp) != 1)
492 /* Write execution time histogram if we have one. */
493 if (gmon_input & INPUT_HISTOGRAM)
494 hist_write_hist (ofp, filename);
496 /* Write call graph arcs if we have any. */
497 if (gmon_input & INPUT_CALL_GRAPH)
498 cg_write_arcs (ofp, filename);
500 /* Write basic-block info if we have it. */
501 if (gmon_input & INPUT_BB_COUNTS)
502 bb_write_blocks (ofp, filename);
504 else if (file_format == FF_BSD || file_format == FF_BSD44)
506 struct raw_arc raw_arc;
513 memset (&h, 0, sizeof h);
514 put_vma (core_bfd, s_lowpc, (bfd_byte *) &h.low_pc);
515 put_vma (core_bfd, s_highpc, (bfd_byte *) &h.high_pc);
516 bfd_put_32 (core_bfd,
517 hist_num_bins * sizeof (UNIT) + sizeof (struct raw_phdr),
518 (bfd_byte *) &h.ncnt);
520 /* Write header. Use new style BSD format is explicitly
521 specified, or if the profiling rate is non-standard;
522 otherwise, use the old BSD format. */
523 if (file_format == FF_BSD44
526 bfd_put_32 (core_bfd, GMONVERSION, (bfd_byte *) &h.version);
527 bfd_put_32 (core_bfd, hz, (bfd_byte *) &h.profrate);
528 if (fwrite (&h, sizeof (struct raw_phdr), 1, ofp) != 1)
536 if (fwrite (&h, sizeof (struct old_raw_phdr), 1, ofp) != 1)
543 /* Dump the samples. */
544 for (i = 0; i < hist_num_bins; ++i)
546 bfd_put_16 (core_bfd, hist_sample[i], (bfd_byte *) & raw_bin_count[0]);
547 if (fwrite (&raw_bin_count[0], sizeof (raw_bin_count), 1, ofp) != 1)
554 /* Dump the normalized raw arc information. */
555 for (sym = symtab.base; sym < symtab.limit; ++sym)
557 for (arc = sym->cg.children; arc; arc = arc->next_child)
559 put_vma (core_bfd, arc->parent->addr,
560 (bfd_byte *) raw_arc.from_pc);
561 put_vma (core_bfd, arc->child->addr,
562 (bfd_byte *) raw_arc.self_pc);
563 bfd_put_32 (core_bfd, arc->count, (bfd_byte *) raw_arc.count);
564 if (fwrite (&raw_arc, sizeof (raw_arc), 1, ofp) != 1)
570 printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
571 (unsigned long) arc->parent->addr,
572 (unsigned long) arc->child->addr, arc->count));
580 fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
581 whoami, file_format);