Fix typo in comment in last commit.
[binutils.git] / gprof / gmon_io.c
blob463993229794451609f4ca79fefa3c141883466d
1 /* gmon_io.c - Input and output from/to gmon.out files.
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "gprof.h"
24 #include "search_list.h"
25 #include "source.h"
26 #include "symtab.h"
27 #include "cg_arcs.h"
28 #include "basic_blocks.h"
29 #include "corefile.h"
30 #include "call_graph.h"
31 #include "gmon_io.h"
32 #include "gmon_out.h"
33 #include "gmon.h" /* Fetch header for old format. */
34 #include "hertz.h"
35 #include "hist.h"
36 #include "libiberty.h"
38 enum gmon_ptr_size {
39 ptr_32bit,
40 ptr_64bit
43 enum gmon_ptr_signedness {
44 ptr_signed,
45 ptr_unsigned
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);
54 #endif
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);
60 int gmon_input = 0;
61 int gmon_file_version = 0; /* 0 == old (non-versioned) file format. */
63 static enum gmon_ptr_size
64 gmon_get_ptr_size ()
66 int size;
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);
71 if (size == -1)
72 size = bfd_arch_bits_per_address (core_bfd);
74 switch (size)
76 case 32:
77 return ptr_32bit;
79 case 64:
80 return ptr_64bit;
82 default:
83 fprintf (stderr, _("%s: address size has unexpected value of %u\n"),
84 whoami, size);
85 done (1);
89 static enum gmon_ptr_signedness
90 gmon_get_ptr_signedness ()
92 int sext;
94 /* Figure out whether to sign extend. If BFD doesn't know, assume no. */
95 sext = bfd_get_sign_extend_vma (core_bfd);
96 if (sext == -1)
97 return ptr_unsigned;
98 return (sext ? ptr_signed : ptr_unsigned);
102 gmon_io_read_32 (FILE *ifp, unsigned int *valp)
104 char buf[4];
106 if (fread (buf, 1, 4, ifp) != 4)
107 return 1;
108 *valp = bfd_get_32 (core_bfd, buf);
109 return 0;
112 #ifdef BFD_HOST_U_64_BIT
113 static int
114 gmon_io_read_64 (FILE *ifp, BFD_HOST_U_64_BIT *valp)
116 char buf[8];
118 if (fread (buf, 1, 8, ifp) != 8)
119 return 1;
120 *valp = bfd_get_64 (core_bfd, buf);
121 return 0;
123 #endif
126 gmon_io_read_vma (FILE *ifp, bfd_vma *valp)
128 unsigned int val32;
129 #ifdef BFD_HOST_U_64_BIT
130 BFD_HOST_U_64_BIT val64;
131 #endif
133 switch (gmon_get_ptr_size ())
135 case ptr_32bit:
136 if (gmon_io_read_32 (ifp, &val32))
137 return 1;
138 if (gmon_get_ptr_signedness () == ptr_signed)
139 *valp = (int) val32;
140 else
141 *valp = val32;
142 break;
144 #ifdef BFD_HOST_U_64_BIT
145 case ptr_64bit:
146 if (gmon_io_read_64 (ifp, &val64))
147 return 1;
148 #ifdef BFD_HOST_64_BIT
149 if (gmon_get_ptr_signedness () == ptr_signed)
150 *valp = (BFD_HOST_64_BIT) val64;
151 else
152 #endif
153 *valp = val64;
154 break;
155 #endif
157 return 0;
161 gmon_io_read (FILE *ifp, char *buf, size_t n)
163 if (fread (buf, 1, n, ifp) != n)
164 return 1;
165 return 0;
169 gmon_io_write_32 (FILE *ofp, unsigned int val)
171 char buf[4];
173 bfd_put_32 (core_bfd, (bfd_vma) val, buf);
174 if (fwrite (buf, 1, 4, ofp) != 4)
175 return 1;
176 return 0;
179 #ifdef BFD_HOST_U_64_BIT
180 static int
181 gmon_io_write_64 (FILE *ofp, BFD_HOST_U_64_BIT val)
183 char buf[8];
185 bfd_put_64 (core_bfd, (bfd_vma) val, buf);
186 if (fwrite (buf, 1, 8, ofp) != 8)
187 return 1;
188 return 0;
190 #endif
193 gmon_io_write_vma (FILE *ofp, bfd_vma val)
196 switch (gmon_get_ptr_size ())
198 case ptr_32bit:
199 if (gmon_io_write_32 (ofp, (unsigned int) val))
200 return 1;
201 break;
203 #ifdef BFD_HOST_U_64_BIT
204 case ptr_64bit:
205 if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) val))
206 return 1;
207 break;
208 #endif
210 return 0;
214 gmon_io_write_8 (FILE *ofp, unsigned int val)
216 char buf[1];
218 bfd_put_8 (core_bfd, val, buf);
219 if (fwrite (buf, 1, 1, ofp) != 1)
220 return 1;
221 return 0;
225 gmon_io_write (FILE *ofp, char *buf, size_t n)
227 if (fwrite (buf, 1, n, ofp) != n)
228 return 1;
229 return 0;
232 static int
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;
237 #endif
238 unsigned int cnt32;
240 if (gmon_io_read_vma (ifp, fpc)
241 || gmon_io_read_vma (ifp, spc))
242 return 1;
244 switch (gmon_get_ptr_size ())
246 case ptr_32bit:
247 if (gmon_io_read_32 (ifp, &cnt32))
248 return 1;
249 *cnt = cnt32;
250 break;
252 #ifdef BFD_HOST_U_64_BIT
253 case ptr_64bit:
254 if (gmon_io_read_64 (ifp, &cnt64))
255 return 1;
256 *cnt = cnt64;
257 break;
258 #endif
260 default:
261 return 1;
263 return 0;
266 static int
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))
272 return 1;
274 switch (gmon_get_ptr_size ())
276 case ptr_32bit:
277 if (gmon_io_write_32 (ofp, (unsigned int) cnt))
278 return 1;
279 break;
281 #ifdef BFD_HOST_U_64_BIT
282 case ptr_64bit:
283 if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) cnt))
284 return 1;
285 break;
286 #endif
288 return 0;
291 void
292 gmon_out_read (const char *filename)
294 FILE *ifp;
295 struct gmon_hdr ghdr;
296 unsigned char tag;
297 int nhist = 0, narcs = 0, nbbs = 0;
299 /* Open gmon.out file. */
300 if (strcmp (filename, "-") == 0)
302 ifp = stdin;
303 #ifdef SET_BINARY
304 SET_BINARY (fileno (stdin));
305 #endif
307 else
309 ifp = fopen (filename, FOPEN_RB);
311 if (!ifp)
313 perror (filename);
314 done (1);
318 if (fread (&ghdr, sizeof (struct gmon_hdr), 1, ifp) != 1)
320 fprintf (stderr, _("%s: file too short to be a gmon file\n"),
321 filename);
322 done (1);
325 if ((file_format == FF_MAGIC)
326 || (file_format == FF_AUTO && !strncmp (&ghdr.cookie[0], GMON_MAGIC, 4)))
328 if (file_format == FF_MAGIC && strncmp (&ghdr.cookie[0], GMON_MAGIC, 4))
330 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
331 whoami, filename);
332 done (1);
335 /* Right magic, so it's probably really a new gmon.out file. */
336 gmon_file_version = bfd_get_32 (core_bfd, (bfd_byte *) ghdr.version);
338 if (gmon_file_version != GMON_VERSION && gmon_file_version != 0)
340 fprintf (stderr,
341 _("%s: file `%s' has unsupported version %d\n"),
342 whoami, filename, gmon_file_version);
343 done (1);
346 /* Read in all the records. */
347 while (fread (&tag, sizeof (tag), 1, ifp) == 1)
349 switch (tag)
351 case GMON_TAG_TIME_HIST:
352 ++nhist;
353 gmon_input |= INPUT_HISTOGRAM;
354 hist_read_rec (ifp, filename);
355 break;
357 case GMON_TAG_CG_ARC:
358 ++narcs;
359 gmon_input |= INPUT_CALL_GRAPH;
360 cg_read_rec (ifp, filename);
361 break;
363 case GMON_TAG_BB_COUNT:
364 ++nbbs;
365 gmon_input |= INPUT_BB_COUNTS;
366 bb_read_rec (ifp, filename);
367 break;
369 default:
370 fprintf (stderr,
371 _("%s: %s: found bad tag %d (file corrupted?)\n"),
372 whoami, filename, tag);
373 done (1);
377 else if (file_format == FF_AUTO
378 || file_format == FF_BSD
379 || file_format == FF_BSD44)
381 struct hdr
383 bfd_vma low_pc;
384 bfd_vma high_pc;
385 unsigned int ncnt;
387 unsigned int i;
388 int samp_bytes, header_size = 0;
389 unsigned long count;
390 bfd_vma from_pc, self_pc;
391 UNIT raw_bin_count;
392 struct hdr tmp;
393 unsigned int version;
394 unsigned int hist_num_bins;
396 /* Information from a gmon.out file is in two parts: an array of
397 sampling hits within pc ranges, and the arcs. */
398 gmon_input = INPUT_HISTOGRAM | INPUT_CALL_GRAPH;
400 /* This fseek() ought to work even on stdin as long as it's
401 not an interactive device (heck, is there anybody who would
402 want to type in a gmon.out at the terminal?). */
403 if (fseek (ifp, 0, SEEK_SET) < 0)
405 perror (filename);
406 done (1);
409 /* The beginning of the old BSD header and the 4.4BSD header
410 are the same: lowpc, highpc, ncnt */
411 if (gmon_io_read_vma (ifp, &tmp.low_pc)
412 || gmon_io_read_vma (ifp, &tmp.high_pc)
413 || gmon_io_read_32 (ifp, &tmp.ncnt))
415 bad_gmon_file:
416 fprintf (stderr, _("%s: file too short to be a gmon file\n"),
417 filename);
418 done (1);
421 /* Check to see if this a 4.4BSD-style header. */
422 if (gmon_io_read_32 (ifp, &version))
423 goto bad_gmon_file;
425 if (version == GMONVERSION)
427 unsigned int profrate;
429 /* 4.4BSD format header. */
430 if (gmon_io_read_32 (ifp, &profrate))
431 goto bad_gmon_file;
433 if (!histograms)
434 hz = profrate;
435 else if (hz != (int) profrate)
437 fprintf (stderr,
438 _("%s: profiling rate incompatible with first gmon file\n"),
439 filename);
440 done (1);
443 switch (gmon_get_ptr_size ())
445 case ptr_32bit:
446 header_size = GMON_HDRSIZE_BSD44_32;
447 break;
449 case ptr_64bit:
450 header_size = GMON_HDRSIZE_BSD44_64;
451 break;
454 else
456 /* Old style BSD format. */
457 if (file_format == FF_BSD44)
459 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
460 whoami, filename);
461 done (1);
464 switch (gmon_get_ptr_size ())
466 case ptr_32bit:
467 header_size = GMON_HDRSIZE_OLDBSD_32;
468 break;
470 case ptr_64bit:
471 header_size = GMON_HDRSIZE_OLDBSD_64;
472 break;
476 /* Position the file to after the header. */
477 if (fseek (ifp, header_size, SEEK_SET) < 0)
479 perror (filename);
480 done (1);
483 samp_bytes = tmp.ncnt - header_size;
484 hist_num_bins = samp_bytes / sizeof (UNIT);
485 if (histograms && (tmp.low_pc != histograms->lowpc
486 || tmp.high_pc != histograms->highpc
487 || (hist_num_bins != histograms->num_bins)))
489 fprintf (stderr, _("%s: incompatible with first gmon file\n"),
490 filename);
491 done (1);
494 if (!histograms)
496 histograms = xmalloc (sizeof (struct histogram));
497 histograms->lowpc = tmp.low_pc;
498 histograms->highpc = tmp.high_pc;
499 histograms->num_bins = hist_num_bins;
500 histograms->sample = xmalloc (hist_num_bins * sizeof (int));
501 memset (histograms->sample, 0,
502 hist_num_bins * sizeof (int));
505 DBG (SAMPLEDEBUG,
506 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
507 (unsigned long) tmp.low_pc, (unsigned long) tmp.high_pc,
508 tmp.ncnt);
509 printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
510 samp_bytes, hist_num_bins));
512 /* Make sure that we have sensible values. */
513 if (samp_bytes < 0 || histograms->lowpc > histograms->highpc)
515 fprintf (stderr,
516 _("%s: file '%s' does not appear to be in gmon.out format\n"),
517 whoami, filename);
518 done (1);
521 if (hist_num_bins)
522 ++nhist;
524 for (i = 0; i < hist_num_bins; ++i)
526 if (fread (raw_bin_count, sizeof (raw_bin_count), 1, ifp) != 1)
528 fprintf (stderr,
529 _("%s: unexpected EOF after reading %d/%d bins\n"),
530 whoami, --i, hist_num_bins);
531 done (1);
534 histograms->sample[i]
535 += bfd_get_16 (core_bfd, (bfd_byte *) raw_bin_count);
538 /* The rest of the file consists of a bunch of
539 <from,self,count> tuples. */
540 while (gmon_read_raw_arc (ifp, &from_pc, &self_pc, &count) == 0)
542 ++narcs;
544 DBG (SAMPLEDEBUG,
545 printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
546 (unsigned long) from_pc, (unsigned long) self_pc, count));
548 /* Add this arc. */
549 cg_tally (from_pc, self_pc, count);
552 fclose (ifp);
554 if (hz == HZ_WRONG)
556 /* How many ticks per second? If we can't tell, report
557 time in ticks. */
558 hz = hertz ();
560 if (hz == HZ_WRONG)
562 hz = 1;
563 fprintf (stderr, _("time is in ticks, not seconds\n"));
567 else
569 fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
570 whoami, file_format);
571 done (1);
574 if (output_style & STYLE_GMON_INFO)
576 printf (_("File `%s' (version %d) contains:\n"),
577 filename, gmon_file_version);
578 printf (nhist == 1 ?
579 _("\t%d histogram record\n") :
580 _("\t%d histogram records\n"), nhist);
581 printf (narcs == 1 ?
582 _("\t%d call-graph record\n") :
583 _("\t%d call-graph records\n"), narcs);
584 printf (nbbs == 1 ?
585 _("\t%d basic-block count record\n") :
586 _("\t%d basic-block count records\n"), nbbs);
587 first_output = FALSE;
592 void
593 gmon_out_write (const char *filename)
595 FILE *ofp;
596 struct gmon_hdr ghdr;
598 ofp = fopen (filename, FOPEN_WB);
599 if (!ofp)
601 perror (filename);
602 done (1);
605 if (file_format == FF_AUTO || file_format == FF_MAGIC)
607 /* Write gmon header. */
609 memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
610 bfd_put_32 (core_bfd, (bfd_vma) GMON_VERSION, (bfd_byte *) ghdr.version);
612 if (fwrite (&ghdr, sizeof (ghdr), 1, ofp) != 1)
614 perror (filename);
615 done (1);
618 /* Write execution time histogram if we have one. */
619 if (gmon_input & INPUT_HISTOGRAM)
620 hist_write_hist (ofp, filename);
622 /* Write call graph arcs if we have any. */
623 if (gmon_input & INPUT_CALL_GRAPH)
624 cg_write_arcs (ofp, filename);
626 /* Write basic-block info if we have it. */
627 if (gmon_input & INPUT_BB_COUNTS)
628 bb_write_blocks (ofp, filename);
630 else if (file_format == FF_BSD || file_format == FF_BSD44)
632 UNIT raw_bin_count;
633 unsigned int i, hdrsize;
634 unsigned padsize;
635 char pad[3*4];
636 Arc *arc;
637 Sym *sym;
639 memset (pad, 0, sizeof (pad));
641 hdrsize = 0;
642 /* Decide how large the header will be. Use the 4.4BSD format
643 header if explicitly specified, or if the profiling rate is
644 non-standard. Otherwise, use the old BSD format. */
645 if (file_format == FF_BSD44
646 || hz != hertz())
648 padsize = 3*4;
649 switch (gmon_get_ptr_size ())
651 case ptr_32bit:
652 hdrsize = GMON_HDRSIZE_BSD44_32;
653 break;
655 case ptr_64bit:
656 hdrsize = GMON_HDRSIZE_BSD44_64;
657 break;
660 else
662 padsize = 0;
663 switch (gmon_get_ptr_size ())
665 case ptr_32bit:
666 hdrsize = GMON_HDRSIZE_OLDBSD_32;
667 break;
669 case ptr_64bit:
670 hdrsize = GMON_HDRSIZE_OLDBSD_64;
671 /* FIXME: Checking host compiler defines here means that we can't
672 use a cross gprof alpha OSF. */
673 #if defined(__alpha__) && defined (__osf__)
674 padsize = 4;
675 #endif
676 break;
680 /* Write the parts of the headers that are common to both the
681 old BSD and 4.4BSD formats. */
682 if (gmon_io_write_vma (ofp, histograms->lowpc)
683 || gmon_io_write_vma (ofp, histograms->highpc)
684 || gmon_io_write_32 (ofp, histograms->num_bins
685 * sizeof (UNIT) + hdrsize))
687 perror (filename);
688 done (1);
691 /* Write out the 4.4BSD header bits, if that's what we're using. */
692 if (file_format == FF_BSD44
693 || hz != hertz())
695 if (gmon_io_write_32 (ofp, GMONVERSION)
696 || gmon_io_write_32 (ofp, (unsigned int) hz))
698 perror (filename);
699 done (1);
703 /* Now write out any necessary padding after the meaningful
704 header bits. */
705 if (padsize != 0
706 && fwrite (pad, 1, padsize, ofp) != padsize)
708 perror (filename);
709 done (1);
712 /* Dump the samples. */
713 for (i = 0; i < histograms->num_bins; ++i)
715 bfd_put_16 (core_bfd, (bfd_vma) histograms->sample[i],
716 (bfd_byte *) &raw_bin_count[0]);
717 if (fwrite (&raw_bin_count[0], sizeof (raw_bin_count), 1, ofp) != 1)
719 perror (filename);
720 done (1);
724 /* Dump the normalized raw arc information. */
725 for (sym = symtab.base; sym < symtab.limit; ++sym)
727 for (arc = sym->cg.children; arc; arc = arc->next_child)
729 if (gmon_write_raw_arc (ofp, arc->parent->addr,
730 arc->child->addr, arc->count))
732 perror (filename);
733 done (1);
735 DBG (SAMPLEDEBUG,
736 printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
737 (unsigned long) arc->parent->addr,
738 (unsigned long) arc->child->addr, arc->count));
742 fclose (ofp);
744 else
746 fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
747 whoami, file_format);
748 done (1);