daily update
[binutils.git] / gprof / gmon_io.c
blob6188631f101de2f7a1902ef2a0e519827cfe4c0a
1 /* gmon_io.c - Input and output from/to gmon.out files.
3 Copyright 2000, 2001, 2002 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
20 02111-1307, USA. */
22 #include "gprof.h"
23 #include "search_list.h"
24 #include "source.h"
25 #include "symtab.h"
26 #include "cg_arcs.h"
27 #include "basic_blocks.h"
28 #include "corefile.h"
29 #include "call_graph.h"
30 #include "gmon_io.h"
31 #include "gmon_out.h"
32 #include "gmon.h" /* Fetch header for old format. */
33 #include "hertz.h"
34 #include "hist.h"
35 #include "libiberty.h"
37 enum gmon_ptr_size {
38 ptr_32bit,
39 ptr_64bit
42 enum gmon_ptr_signedness {
43 ptr_signed,
44 ptr_unsigned
47 static enum gmon_ptr_size gmon_get_ptr_size PARAMS ((void));
48 static enum gmon_ptr_signedness gmon_get_ptr_signedness PARAMS ((void));
50 #ifdef BFD_HOST_U_64_BIT
51 static int gmon_io_read_64 PARAMS ((FILE *, BFD_HOST_U_64_BIT *));
52 static int gmon_io_write_64 PARAMS ((FILE *, BFD_HOST_U_64_BIT));
53 #endif
54 static int gmon_read_raw_arc
55 PARAMS ((FILE *, bfd_vma *, bfd_vma *, unsigned long *));
56 static int gmon_write_raw_arc
57 PARAMS ((FILE *, bfd_vma, bfd_vma, unsigned long));
59 int gmon_input = 0;
60 int gmon_file_version = 0; /* 0 == old (non-versioned) file format. */
62 static enum gmon_ptr_size
63 gmon_get_ptr_size ()
65 int size;
67 /* Pick best size for pointers. Start with the ELF size, and if not
68 elf go with the architecture's address size. */
69 size = bfd_get_arch_size (core_bfd);
70 if (size == -1)
71 size = bfd_arch_bits_per_address (core_bfd);
73 switch (size)
75 case 32:
76 return ptr_32bit;
78 case 64:
79 return ptr_64bit;
81 default:
82 fprintf (stderr, _("%s: address size has unexpected value of %u\n"),
83 whoami, size);
84 done (1);
88 static enum gmon_ptr_signedness
89 gmon_get_ptr_signedness ()
91 int sext;
93 /* Figure out whether to sign extend. If BFD doesn't know, assume no. */
94 sext = bfd_get_sign_extend_vma (core_bfd);
95 if (sext == -1)
96 return ptr_unsigned;
97 return (sext ? ptr_signed : ptr_unsigned);
101 gmon_io_read_32 (ifp, valp)
102 FILE *ifp;
103 unsigned int *valp;
105 char buf[4];
107 if (fread (buf, 1, 4, ifp) != 4)
108 return 1;
109 *valp = bfd_get_32 (core_bfd, buf);
110 return 0;
113 #ifdef BFD_HOST_U_64_BIT
114 static int
115 gmon_io_read_64 (ifp, valp)
116 FILE *ifp;
117 BFD_HOST_U_64_BIT *valp;
119 char buf[8];
121 if (fread (buf, 1, 8, ifp) != 8)
122 return 1;
123 *valp = bfd_get_64 (core_bfd, buf);
124 return 0;
126 #endif
129 gmon_io_read_vma (ifp, valp)
130 FILE *ifp;
131 bfd_vma *valp;
133 unsigned int val32;
134 #ifdef BFD_HOST_U_64_BIT
135 BFD_HOST_U_64_BIT val64;
136 #endif
138 switch (gmon_get_ptr_size ())
140 case ptr_32bit:
141 if (gmon_io_read_32 (ifp, &val32))
142 return 1;
143 if (gmon_get_ptr_signedness () == ptr_signed)
144 *valp = (int) val32;
145 else
146 *valp = val32;
147 break;
149 #ifdef BFD_HOST_U_64_BIT
150 case ptr_64bit:
151 if (gmon_io_read_64 (ifp, &val64))
152 return 1;
153 #ifdef BFD_HOST_64_BIT
154 if (gmon_get_ptr_signedness () == ptr_signed)
155 *valp = (BFD_HOST_64_BIT) val64;
156 else
157 #endif
158 *valp = val64;
159 break;
160 #endif
162 return 0;
166 gmon_io_read (ifp, buf, n)
167 FILE *ifp;
168 char *buf;
169 size_t n;
171 if (fread (buf, 1, n, ifp) != n)
172 return 1;
173 return 0;
177 gmon_io_write_32 (ofp, val)
178 FILE *ofp;
179 unsigned int val;
181 char buf[4];
183 bfd_put_32 (core_bfd, (bfd_vma) val, buf);
184 if (fwrite (buf, 1, 4, ofp) != 4)
185 return 1;
186 return 0;
189 #ifdef BFD_HOST_U_64_BIT
190 static int
191 gmon_io_write_64 (ofp, val)
192 FILE *ofp;
193 BFD_HOST_U_64_BIT val;
195 char buf[8];
197 bfd_put_64 (core_bfd, (bfd_vma) val, buf);
198 if (fwrite (buf, 1, 8, ofp) != 8)
199 return 1;
200 return 0;
202 #endif
205 gmon_io_write_vma (ofp, val)
206 FILE *ofp;
207 bfd_vma val;
210 switch (gmon_get_ptr_size ())
212 case ptr_32bit:
213 if (gmon_io_write_32 (ofp, (unsigned int) val))
214 return 1;
215 break;
217 #ifdef BFD_HOST_U_64_BIT
218 case ptr_64bit:
219 if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) val))
220 return 1;
221 break;
222 #endif
224 return 0;
228 gmon_io_write_8 (ofp, val)
229 FILE *ofp;
230 unsigned int val;
232 char buf[1];
234 bfd_put_8 (core_bfd, val, buf);
235 if (fwrite (buf, 1, 1, ofp) != 1)
236 return 1;
237 return 0;
241 gmon_io_write (ofp, buf, n)
242 FILE *ofp;
243 char *buf;
244 size_t n;
246 if (fwrite (buf, 1, n, ofp) != n)
247 return 1;
248 return 0;
251 static int
252 gmon_read_raw_arc (ifp, fpc, spc, cnt)
253 FILE *ifp;
254 bfd_vma *fpc;
255 bfd_vma *spc;
256 unsigned long *cnt;
258 #ifdef BFD_HOST_U_64_BIT
259 BFD_HOST_U_64_BIT cnt64;
260 #endif
261 unsigned int cnt32;
263 if (gmon_io_read_vma (ifp, fpc)
264 || gmon_io_read_vma (ifp, spc))
265 return 1;
267 switch (gmon_get_ptr_size ())
269 case ptr_32bit:
270 if (gmon_io_read_32 (ifp, &cnt32))
271 return 1;
272 *cnt = cnt32;
273 break;
275 #ifdef BFD_HOST_U_64_BIT
276 case ptr_64bit:
277 if (gmon_io_read_64 (ifp, &cnt64))
278 return 1;
279 *cnt = cnt64;
280 break;
281 #endif
283 return 0;
286 static int
287 gmon_write_raw_arc (ofp, fpc, spc, cnt)
288 FILE *ofp;
289 bfd_vma fpc;
290 bfd_vma spc;
291 unsigned long cnt;
294 if (gmon_io_write_vma (ofp, fpc)
295 || gmon_io_write_vma (ofp, spc))
296 return 1;
298 switch (gmon_get_ptr_size ())
300 case ptr_32bit:
301 if (gmon_io_write_32 (ofp, (unsigned int) cnt))
302 return 1;
303 break;
305 #ifdef BFD_HOST_U_64_BIT
306 case ptr_64bit:
307 if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) cnt))
308 return 1;
309 break;
310 #endif
312 return 0;
315 void
316 gmon_out_read (filename)
317 const char *filename;
319 FILE *ifp;
320 struct gmon_hdr ghdr;
321 unsigned char tag;
322 int nhist = 0, narcs = 0, nbbs = 0;
324 /* Open gmon.out file. */
325 if (strcmp (filename, "-") == 0)
327 ifp = stdin;
328 #ifdef SET_BINARY
329 SET_BINARY (fileno (stdin));
330 #endif
332 else
334 ifp = fopen (filename, FOPEN_RB);
336 if (!ifp)
338 perror (filename);
339 done (1);
343 if (fread (&ghdr, sizeof (struct gmon_hdr), 1, ifp) != 1)
345 fprintf (stderr, _("%s: file too short to be a gmon file\n"),
346 filename);
347 done (1);
350 if ((file_format == FF_MAGIC)
351 || (file_format == FF_AUTO && !strncmp (&ghdr.cookie[0], GMON_MAGIC, 4)))
353 if (file_format == FF_MAGIC && strncmp (&ghdr.cookie[0], GMON_MAGIC, 4))
355 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
356 whoami, filename);
357 done (1);
360 /* Right magic, so it's probably really a new gmon.out file. */
361 gmon_file_version = bfd_get_32 (core_bfd, (bfd_byte *) ghdr.version);
363 if (gmon_file_version != GMON_VERSION && gmon_file_version != 0)
365 fprintf (stderr,
366 _("%s: file `%s' has unsupported version %d\n"),
367 whoami, filename, gmon_file_version);
368 done (1);
371 /* Read in all the records. */
372 while (fread (&tag, sizeof (tag), 1, ifp) == 1)
374 switch (tag)
376 case GMON_TAG_TIME_HIST:
377 ++nhist;
378 gmon_input |= INPUT_HISTOGRAM;
379 hist_read_rec (ifp, filename);
380 break;
382 case GMON_TAG_CG_ARC:
383 ++narcs;
384 gmon_input |= INPUT_CALL_GRAPH;
385 cg_read_rec (ifp, filename);
386 break;
388 case GMON_TAG_BB_COUNT:
389 ++nbbs;
390 gmon_input |= INPUT_BB_COUNTS;
391 bb_read_rec (ifp, filename);
392 break;
394 default:
395 fprintf (stderr,
396 _("%s: %s: found bad tag %d (file corrupted?)\n"),
397 whoami, filename, tag);
398 done (1);
402 else if (file_format == FF_AUTO
403 || file_format == FF_BSD
404 || file_format == FF_BSD44)
406 struct hdr
408 bfd_vma low_pc;
409 bfd_vma high_pc;
410 int ncnt;
412 int i, samp_bytes, header_size = 0;
413 unsigned long count;
414 bfd_vma from_pc, self_pc;
415 static struct hdr h;
416 UNIT raw_bin_count;
417 struct hdr tmp;
418 int version;
420 /* Information from a gmon.out file is in two parts: an array of
421 sampling hits within pc ranges, and the arcs. */
422 gmon_input = INPUT_HISTOGRAM | INPUT_CALL_GRAPH;
424 /* This fseek() ought to work even on stdin as long as it's
425 not an interactive device (heck, is there anybody who would
426 want to type in a gmon.out at the terminal?). */
427 if (fseek (ifp, 0, SEEK_SET) < 0)
429 perror (filename);
430 done (1);
433 /* The beginning of the old BSD header and the 4.4BSD header
434 are the same: lowpc, highpc, ncnt */
435 if (gmon_io_read_vma (ifp, &tmp.low_pc)
436 || gmon_io_read_vma (ifp, &tmp.high_pc)
437 || gmon_io_read_32 (ifp, &tmp.ncnt))
439 bad_gmon_file:
440 fprintf (stderr, _("%s: file too short to be a gmon file\n"),
441 filename);
442 done (1);
445 /* Check to see if this a 4.4BSD-style header. */
446 if (gmon_io_read_32 (ifp, &version))
447 goto bad_gmon_file;
449 if (version == GMONVERSION)
451 int profrate;
453 /* 4.4BSD format header. */
454 if (gmon_io_read_32 (ifp, &profrate))
455 goto bad_gmon_file;
457 if (!s_highpc)
458 hz = profrate;
459 else if (hz != profrate)
461 fprintf (stderr,
462 _("%s: profiling rate incompatible with first gmon file\n"),
463 filename);
464 done (1);
467 switch (gmon_get_ptr_size ())
469 case ptr_32bit:
470 header_size = GMON_HDRSIZE_BSD44_32;
471 break;
473 case ptr_64bit:
474 header_size = GMON_HDRSIZE_BSD44_64;
475 break;
478 else
480 /* Old style BSD format. */
481 if (file_format == FF_BSD44)
483 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
484 whoami, filename);
485 done (1);
488 switch (gmon_get_ptr_size ())
490 case ptr_32bit:
491 header_size = GMON_HDRSIZE_OLDBSD_32;
492 break;
494 case ptr_64bit:
495 header_size = GMON_HDRSIZE_OLDBSD_64;
496 break;
500 /* Position the file to after the header. */
501 if (fseek (ifp, header_size, SEEK_SET) < 0)
503 perror (filename);
504 done (1);
507 if (s_highpc && (tmp.low_pc != h.low_pc
508 || tmp.high_pc != h.high_pc || tmp.ncnt != h.ncnt))
510 fprintf (stderr, _("%s: incompatible with first gmon file\n"),
511 filename);
512 done (1);
515 h = tmp;
516 s_lowpc = (bfd_vma) h.low_pc;
517 s_highpc = (bfd_vma) h.high_pc;
518 lowpc = (bfd_vma) h.low_pc / sizeof (UNIT);
519 highpc = (bfd_vma) h.high_pc / sizeof (UNIT);
520 samp_bytes = h.ncnt - header_size;
521 hist_num_bins = samp_bytes / sizeof (UNIT);
523 DBG (SAMPLEDEBUG,
524 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
525 (unsigned long) h.low_pc, (unsigned long) h.high_pc,
526 h.ncnt);
527 printf ("[gmon_out_read] s_lowpc 0x%lx s_highpc 0x%lx\n",
528 (unsigned long) s_lowpc, (unsigned long) s_highpc);
529 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx\n",
530 (unsigned long) lowpc, (unsigned long) highpc);
531 printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
532 samp_bytes, hist_num_bins));
534 /* Make sure that we have sensible values. */
535 if (samp_bytes < 0 || lowpc > highpc)
537 fprintf (stderr,
538 _("%s: file '%s' does not appear to be in gmon.out format\n"),
539 whoami, filename);
540 done (1);
543 if (hist_num_bins)
544 ++nhist;
546 if (!hist_sample)
548 hist_sample =
549 (int *) xmalloc (hist_num_bins * sizeof (hist_sample[0]));
551 memset (hist_sample, 0, hist_num_bins * sizeof (hist_sample[0]));
554 for (i = 0; i < hist_num_bins; ++i)
556 if (fread (raw_bin_count, sizeof (raw_bin_count), 1, ifp) != 1)
558 fprintf (stderr,
559 _("%s: unexpected EOF after reading %d/%d bins\n"),
560 whoami, --i, hist_num_bins);
561 done (1);
564 hist_sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) raw_bin_count);
567 /* The rest of the file consists of a bunch of
568 <from,self,count> tuples. */
569 while (gmon_read_raw_arc (ifp, &from_pc, &self_pc, &count) == 0)
571 ++narcs;
573 DBG (SAMPLEDEBUG,
574 printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
575 (unsigned long) from_pc, (unsigned long) self_pc, count));
577 /* Add this arc. */
578 cg_tally (from_pc, self_pc, count);
581 fclose (ifp);
583 if (hz == HZ_WRONG)
585 /* How many ticks per second? If we can't tell, report
586 time in ticks. */
587 hz = hertz ();
589 if (hz == HZ_WRONG)
591 hz = 1;
592 fprintf (stderr, _("time is in ticks, not seconds\n"));
596 else
598 fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
599 whoami, file_format);
600 done (1);
603 if (output_style & STYLE_GMON_INFO)
605 printf (_("File `%s' (version %d) contains:\n"),
606 filename, gmon_file_version);
607 printf (nhist == 1 ?
608 _("\t%d histogram record\n") :
609 _("\t%d histogram records\n"), nhist);
610 printf (narcs == 1 ?
611 _("\t%d call-graph record\n") :
612 _("\t%d call-graph records\n"), narcs);
613 printf (nbbs == 1 ?
614 _("\t%d basic-block count record\n") :
615 _("\t%d basic-block count records\n"), nbbs);
616 first_output = FALSE;
621 void
622 gmon_out_write (filename)
623 const char *filename;
625 FILE *ofp;
626 struct gmon_hdr ghdr;
628 ofp = fopen (filename, FOPEN_WB);
629 if (!ofp)
631 perror (filename);
632 done (1);
635 if (file_format == FF_AUTO || file_format == FF_MAGIC)
637 /* Write gmon header. */
639 memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
640 bfd_put_32 (core_bfd, (bfd_vma) GMON_VERSION, (bfd_byte *) ghdr.version);
642 if (fwrite (&ghdr, sizeof (ghdr), 1, ofp) != 1)
644 perror (filename);
645 done (1);
648 /* Write execution time histogram if we have one. */
649 if (gmon_input & INPUT_HISTOGRAM)
650 hist_write_hist (ofp, filename);
652 /* Write call graph arcs if we have any. */
653 if (gmon_input & INPUT_CALL_GRAPH)
654 cg_write_arcs (ofp, filename);
656 /* Write basic-block info if we have it. */
657 if (gmon_input & INPUT_BB_COUNTS)
658 bb_write_blocks (ofp, filename);
660 else if (file_format == FF_BSD || file_format == FF_BSD44)
662 UNIT raw_bin_count;
663 int i, hdrsize;
664 unsigned padsize;
665 char pad[3*4];
666 Arc *arc;
667 Sym *sym;
669 memset (pad, 0, sizeof (pad));
671 hdrsize = 0;
672 /* Decide how large the header will be. Use the 4.4BSD format
673 header if explicitly specified, or if the profiling rate is
674 non-standard. Otherwise, use the old BSD format. */
675 if (file_format == FF_BSD44
676 || hz != hertz())
678 padsize = 3*4;
679 switch (gmon_get_ptr_size ())
681 case ptr_32bit:
682 hdrsize = GMON_HDRSIZE_BSD44_32;
683 break;
685 case ptr_64bit:
686 hdrsize = GMON_HDRSIZE_BSD44_64;
687 break;
690 else
692 padsize = 0;
693 switch (gmon_get_ptr_size ())
695 case ptr_32bit:
696 hdrsize = GMON_HDRSIZE_OLDBSD_32;
697 break;
699 case ptr_64bit:
700 hdrsize = GMON_HDRSIZE_OLDBSD_64;
701 /* FIXME: Checking host compiler defines here means that we can't
702 use a cross gprof alpha OSF. */
703 #if defined(__alpha__) && defined (__osf__)
704 padsize = 4;
705 #endif
706 break;
710 /* Write the parts of the headers that are common to both the
711 old BSD and 4.4BSD formats. */
712 if (gmon_io_write_vma (ofp, s_lowpc)
713 || gmon_io_write_vma (ofp, s_highpc)
714 || gmon_io_write_32 (ofp, hist_num_bins * sizeof (UNIT) + hdrsize))
716 perror (filename);
717 done (1);
720 /* Write out the 4.4BSD header bits, if that's what we're using. */
721 if (file_format == FF_BSD44
722 || hz != hertz())
724 if (gmon_io_write_32 (ofp, GMONVERSION)
725 || gmon_io_write_32 (ofp, (unsigned int) hz))
727 perror (filename);
728 done (1);
732 /* Now write out any necessary padding after the meaningful
733 header bits. */
734 if (padsize != 0
735 && fwrite (pad, 1, padsize, ofp) != padsize)
737 perror (filename);
738 done (1);
741 /* Dump the samples. */
742 for (i = 0; i < hist_num_bins; ++i)
744 bfd_put_16 (core_bfd, (bfd_vma) hist_sample[i],
745 (bfd_byte *) &raw_bin_count[0]);
746 if (fwrite (&raw_bin_count[0], sizeof (raw_bin_count), 1, ofp) != 1)
748 perror (filename);
749 done (1);
753 /* Dump the normalized raw arc information. */
754 for (sym = symtab.base; sym < symtab.limit; ++sym)
756 for (arc = sym->cg.children; arc; arc = arc->next_child)
758 if (gmon_write_raw_arc (ofp, arc->parent->addr,
759 arc->child->addr, arc->count))
761 perror (filename);
762 done (1);
764 DBG (SAMPLEDEBUG,
765 printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
766 (unsigned long) arc->parent->addr,
767 (unsigned long) arc->child->addr, arc->count));
771 fclose (ofp);
773 else
775 fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
776 whoami, file_format);
777 done (1);