* ldlang.c (init_os): Ensure sections mentioned in load_base
[binutils.git] / gprof / gmon_io.c
blobf821b0ca2ef3d5feb03c84ea69f7929e49935775
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 #ifdef BFD_HOST_U_64_BIT
38 static int gmon_io_read_64 PARAMS ((FILE *, BFD_HOST_U_64_BIT *));
39 static int gmon_io_write_64 PARAMS ((FILE *, BFD_HOST_U_64_BIT));
40 #endif
41 static int gmon_read_raw_arc
42 PARAMS ((FILE *, bfd_vma *, bfd_vma *, unsigned long *));
43 static int gmon_write_raw_arc
44 PARAMS ((FILE *, bfd_vma, bfd_vma, unsigned long));
46 int gmon_input = 0;
47 int gmon_file_version = 0; /* 0 == old (non-versioned) file format. */
49 int
50 gmon_io_read_32 (ifp, valp)
51 FILE *ifp;
52 unsigned int *valp;
54 char buf[4];
56 if (fread (buf, 1, 4, ifp) != 4)
57 return 1;
58 *valp = bfd_get_32 (core_bfd, buf);
59 return 0;
62 #ifdef BFD_HOST_U_64_BIT
63 static int
64 gmon_io_read_64 (ifp, valp)
65 FILE *ifp;
66 BFD_HOST_U_64_BIT *valp;
68 char buf[8];
70 if (fread (buf, 1, 8, ifp) != 8)
71 return 1;
72 *valp = bfd_get_64 (core_bfd, buf);
73 return 0;
75 #endif
77 int
78 gmon_io_read_vma (ifp, valp)
79 FILE *ifp;
80 bfd_vma *valp;
82 unsigned int val32;
83 #ifdef BFD_HOST_U_64_BIT
84 BFD_HOST_U_64_BIT val64;
85 #endif
87 switch (bfd_arch_bits_per_address (core_bfd))
89 case 32:
90 if (gmon_io_read_32 (ifp, &val32))
91 return 1;
92 *valp = val32;
93 break;
95 #ifdef BFD_HOST_U_64_BIT
96 case 64:
97 if (gmon_io_read_64 (ifp, &val64))
98 return 1;
99 *valp = val64;
100 break;
101 #endif
103 default:
104 fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
105 whoami, bfd_arch_bits_per_address (core_bfd));
106 done (1);
108 return 0;
112 gmon_io_read (ifp, buf, n)
113 FILE *ifp;
114 char *buf;
115 size_t n;
117 if (fread (buf, 1, n, ifp) != n)
118 return 1;
119 return 0;
123 gmon_io_write_32 (ofp, val)
124 FILE *ofp;
125 unsigned int val;
127 char buf[4];
129 bfd_put_32 (core_bfd, (bfd_vma) val, buf);
130 if (fwrite (buf, 1, 4, ofp) != 4)
131 return 1;
132 return 0;
135 #ifdef BFD_HOST_U_64_BIT
136 static int
137 gmon_io_write_64 (ofp, val)
138 FILE *ofp;
139 BFD_HOST_U_64_BIT val;
141 char buf[8];
143 bfd_put_64 (core_bfd, (bfd_vma) val, buf);
144 if (fwrite (buf, 1, 8, ofp) != 8)
145 return 1;
146 return 0;
148 #endif
151 gmon_io_write_vma (ofp, val)
152 FILE *ofp;
153 bfd_vma val;
156 switch (bfd_arch_bits_per_address (core_bfd))
158 case 32:
159 if (gmon_io_write_32 (ofp, (unsigned int) val))
160 return 1;
161 break;
163 #ifdef BFD_HOST_U_64_BIT
164 case 64:
165 if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) val))
166 return 1;
167 break;
168 #endif
170 default:
171 fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
172 whoami, bfd_arch_bits_per_address (core_bfd));
173 done (1);
175 return 0;
179 gmon_io_write_8 (ofp, val)
180 FILE *ofp;
181 unsigned int val;
183 char buf[1];
185 bfd_put_8 (core_bfd, val, buf);
186 if (fwrite (buf, 1, 1, ofp) != 1)
187 return 1;
188 return 0;
192 gmon_io_write (ofp, buf, n)
193 FILE *ofp;
194 char *buf;
195 size_t n;
197 if (fwrite (buf, 1, n, ofp) != n)
198 return 1;
199 return 0;
202 static int
203 gmon_read_raw_arc (ifp, fpc, spc, cnt)
204 FILE *ifp;
205 bfd_vma *fpc;
206 bfd_vma *spc;
207 unsigned long *cnt;
209 #ifdef BFD_HOST_U_64_BIT
210 BFD_HOST_U_64_BIT cnt64;
211 #endif
212 unsigned int cnt32;
214 if (gmon_io_read_vma (ifp, fpc)
215 || gmon_io_read_vma (ifp, spc))
216 return 1;
218 switch (bfd_arch_bits_per_address (core_bfd))
220 case 32:
221 if (gmon_io_read_32 (ifp, &cnt32))
222 return 1;
223 *cnt = cnt32;
224 break;
226 #ifdef BFD_HOST_U_64_BIT
227 case 64:
228 if (gmon_io_read_64 (ifp, &cnt64))
229 return 1;
230 *cnt = cnt64;
231 break;
232 #endif
234 default:
235 fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
236 whoami, bfd_arch_bits_per_address (core_bfd));
237 done (1);
239 return 0;
242 static int
243 gmon_write_raw_arc (ofp, fpc, spc, cnt)
244 FILE *ofp;
245 bfd_vma fpc;
246 bfd_vma spc;
247 unsigned long cnt;
250 if (gmon_io_write_vma (ofp, fpc)
251 || gmon_io_write_vma (ofp, spc))
252 return 1;
254 switch (bfd_arch_bits_per_address (core_bfd))
256 case 32:
257 if (gmon_io_write_32 (ofp, (unsigned int) cnt))
258 return 1;
259 break;
261 #ifdef BFD_HOST_U_64_BIT
262 case 64:
263 if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) cnt))
264 return 1;
265 break;
266 #endif
268 default:
269 fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
270 whoami, bfd_arch_bits_per_address (core_bfd));
271 done (1);
273 return 0;
276 void
277 gmon_out_read (filename)
278 const char *filename;
280 FILE *ifp;
281 struct gmon_hdr ghdr;
282 unsigned char tag;
283 int nhist = 0, narcs = 0, nbbs = 0;
285 /* Open gmon.out file. */
286 if (strcmp (filename, "-") == 0)
288 ifp = stdin;
289 #ifdef SET_BINARY
290 SET_BINARY (fileno (stdin));
291 #endif
293 else
295 ifp = fopen (filename, FOPEN_RB);
297 if (!ifp)
299 perror (filename);
300 done (1);
304 if (fread (&ghdr, sizeof (struct gmon_hdr), 1, ifp) != 1)
306 fprintf (stderr, _("%s: file too short to be a gmon file\n"),
307 filename);
308 done (1);
311 if ((file_format == FF_MAGIC)
312 || (file_format == FF_AUTO && !strncmp (&ghdr.cookie[0], GMON_MAGIC, 4)))
314 if (file_format == FF_MAGIC && strncmp (&ghdr.cookie[0], GMON_MAGIC, 4))
316 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
317 whoami, filename);
318 done (1);
321 /* Right magic, so it's probably really a new gmon.out file. */
322 gmon_file_version = bfd_get_32 (core_bfd, (bfd_byte *) ghdr.version);
324 if (gmon_file_version != GMON_VERSION && gmon_file_version != 0)
326 fprintf (stderr,
327 _("%s: file `%s' has unsupported version %d\n"),
328 whoami, filename, gmon_file_version);
329 done (1);
332 /* Read in all the records. */
333 while (fread (&tag, sizeof (tag), 1, ifp) == 1)
335 switch (tag)
337 case GMON_TAG_TIME_HIST:
338 ++nhist;
339 gmon_input |= INPUT_HISTOGRAM;
340 hist_read_rec (ifp, filename);
341 break;
343 case GMON_TAG_CG_ARC:
344 ++narcs;
345 gmon_input |= INPUT_CALL_GRAPH;
346 cg_read_rec (ifp, filename);
347 break;
349 case GMON_TAG_BB_COUNT:
350 ++nbbs;
351 gmon_input |= INPUT_BB_COUNTS;
352 bb_read_rec (ifp, filename);
353 break;
355 default:
356 fprintf (stderr,
357 _("%s: %s: found bad tag %d (file corrupted?)\n"),
358 whoami, filename, tag);
359 done (1);
363 else if (file_format == FF_AUTO
364 || file_format == FF_BSD
365 || file_format == FF_BSD44)
367 struct hdr
369 bfd_vma low_pc;
370 bfd_vma high_pc;
371 int ncnt;
373 int i, samp_bytes, header_size = 0;
374 unsigned long count;
375 bfd_vma from_pc, self_pc;
376 static struct hdr h;
377 UNIT raw_bin_count;
378 struct hdr tmp;
379 int version;
381 /* Information from a gmon.out file is in two parts: an array of
382 sampling hits within pc ranges, and the arcs. */
383 gmon_input = INPUT_HISTOGRAM | INPUT_CALL_GRAPH;
385 /* This fseek() ought to work even on stdin as long as it's
386 not an interactive device (heck, is there anybody who would
387 want to type in a gmon.out at the terminal?). */
388 if (fseek (ifp, 0, SEEK_SET) < 0)
390 perror (filename);
391 done (1);
394 /* The beginning of the old BSD header and the 4.4BSD header
395 are the same: lowpc, highpc, ncnt */
396 if (gmon_io_read_vma (ifp, &tmp.low_pc)
397 || gmon_io_read_vma (ifp, &tmp.high_pc)
398 || gmon_io_read_32 (ifp, &tmp.ncnt))
400 bad_gmon_file:
401 fprintf (stderr, _("%s: file too short to be a gmon file\n"),
402 filename);
403 done (1);
406 /* Check to see if this a 4.4BSD-style header. */
407 if (gmon_io_read_32 (ifp, &version))
408 goto bad_gmon_file;
410 if (version == GMONVERSION)
412 int profrate;
414 /* 4.4BSD format header. */
415 if (gmon_io_read_32 (ifp, &profrate))
416 goto bad_gmon_file;
418 if (!s_highpc)
419 hz = profrate;
420 else if (hz != profrate)
422 fprintf (stderr,
423 _("%s: profiling rate incompatible with first gmon file\n"),
424 filename);
425 done (1);
428 switch (bfd_arch_bits_per_address (core_bfd))
430 case 32:
431 header_size = GMON_HDRSIZE_BSD44_32;
432 break;
434 case 64:
435 header_size = GMON_HDRSIZE_BSD44_64;
436 break;
438 default:
439 fprintf (stderr,
440 _("%s: bits per address has unexpected value of %u\n"),
441 whoami, bfd_arch_bits_per_address (core_bfd));
442 done (1);
445 else
447 /* Old style BSD format. */
448 if (file_format == FF_BSD44)
450 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
451 whoami, filename);
452 done (1);
455 switch (bfd_arch_bits_per_address (core_bfd))
457 case 32:
458 header_size = GMON_HDRSIZE_OLDBSD_32;
459 break;
461 case 64:
462 header_size = GMON_HDRSIZE_OLDBSD_64;
463 break;
465 default:
466 fprintf (stderr,
467 _("%s: bits per address has unexpected value of %u\n"),
468 whoami, bfd_arch_bits_per_address (core_bfd));
469 done (1);
473 /* Position the file to after the header. */
474 if (fseek (ifp, header_size, SEEK_SET) < 0)
476 perror (filename);
477 done (1);
480 if (s_highpc && (tmp.low_pc != h.low_pc
481 || tmp.high_pc != h.high_pc || tmp.ncnt != h.ncnt))
483 fprintf (stderr, _("%s: incompatible with first gmon file\n"),
484 filename);
485 done (1);
488 h = tmp;
489 s_lowpc = (bfd_vma) h.low_pc;
490 s_highpc = (bfd_vma) h.high_pc;
491 lowpc = (bfd_vma) h.low_pc / sizeof (UNIT);
492 highpc = (bfd_vma) h.high_pc / sizeof (UNIT);
493 samp_bytes = h.ncnt - header_size;
494 hist_num_bins = samp_bytes / sizeof (UNIT);
496 DBG (SAMPLEDEBUG,
497 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
498 (unsigned long) h.low_pc, (unsigned long) h.high_pc,
499 h.ncnt);
500 printf ("[gmon_out_read] s_lowpc 0x%lx s_highpc 0x%lx\n",
501 (unsigned long) s_lowpc, (unsigned long) s_highpc);
502 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx\n",
503 (unsigned long) lowpc, (unsigned long) highpc);
504 printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
505 samp_bytes, hist_num_bins));
507 /* Make sure that we have sensible values. */
508 if (samp_bytes < 0 || lowpc > highpc)
510 fprintf (stderr,
511 _("%s: file '%s' does not appear to be in gmon.out format\n"),
512 whoami, filename);
513 done (1);
516 if (hist_num_bins)
517 ++nhist;
519 if (!hist_sample)
521 hist_sample =
522 (int *) xmalloc (hist_num_bins * sizeof (hist_sample[0]));
524 memset (hist_sample, 0, hist_num_bins * sizeof (hist_sample[0]));
527 for (i = 0; i < hist_num_bins; ++i)
529 if (fread (raw_bin_count, sizeof (raw_bin_count), 1, ifp) != 1)
531 fprintf (stderr,
532 _("%s: unexpected EOF after reading %d/%d bins\n"),
533 whoami, --i, hist_num_bins);
534 done (1);
537 hist_sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) raw_bin_count);
540 /* The rest of the file consists of a bunch of
541 <from,self,count> tuples. */
542 while (gmon_read_raw_arc (ifp, &from_pc, &self_pc, &count) == 0)
544 ++narcs;
546 DBG (SAMPLEDEBUG,
547 printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
548 (unsigned long) from_pc, (unsigned long) self_pc, count));
550 /* Add this arc. */
551 cg_tally (from_pc, self_pc, count);
554 fclose (ifp);
556 if (hz == HZ_WRONG)
558 /* How many ticks per second? If we can't tell, report
559 time in ticks. */
560 hz = hertz ();
562 if (hz == HZ_WRONG)
564 hz = 1;
565 fprintf (stderr, _("time is in ticks, not seconds\n"));
569 else
571 fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
572 whoami, file_format);
573 done (1);
576 if (output_style & STYLE_GMON_INFO)
578 printf (_("File `%s' (version %d) contains:\n"),
579 filename, gmon_file_version);
580 printf (nhist == 1 ?
581 _("\t%d histogram record\n") :
582 _("\t%d histogram records\n"), nhist);
583 printf (narcs == 1 ?
584 _("\t%d call-graph record\n") :
585 _("\t%d call-graph records\n"), narcs);
586 printf (nbbs == 1 ?
587 _("\t%d basic-block count record\n") :
588 _("\t%d basic-block count records\n"), nbbs);
589 first_output = FALSE;
594 void
595 gmon_out_write (filename)
596 const char *filename;
598 FILE *ofp;
599 struct gmon_hdr ghdr;
601 ofp = fopen (filename, FOPEN_WB);
602 if (!ofp)
604 perror (filename);
605 done (1);
608 if (file_format == FF_AUTO || file_format == FF_MAGIC)
610 /* Write gmon header. */
612 memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
613 bfd_put_32 (core_bfd, (bfd_vma) GMON_VERSION, (bfd_byte *) ghdr.version);
615 if (fwrite (&ghdr, sizeof (ghdr), 1, ofp) != 1)
617 perror (filename);
618 done (1);
621 /* Write execution time histogram if we have one. */
622 if (gmon_input & INPUT_HISTOGRAM)
623 hist_write_hist (ofp, filename);
625 /* Write call graph arcs if we have any. */
626 if (gmon_input & INPUT_CALL_GRAPH)
627 cg_write_arcs (ofp, filename);
629 /* Write basic-block info if we have it. */
630 if (gmon_input & INPUT_BB_COUNTS)
631 bb_write_blocks (ofp, filename);
633 else if (file_format == FF_BSD || file_format == FF_BSD44)
635 UNIT raw_bin_count;
636 int i, hdrsize;
637 unsigned padsize;
638 char pad[3*4];
639 Arc *arc;
640 Sym *sym;
642 memset (pad, 0, sizeof (pad));
644 hdrsize = 0;
645 /* Decide how large the header will be. Use the 4.4BSD format
646 header if explicitly specified, or if the profiling rate is
647 non-standard. Otherwise, use the old BSD format. */
648 if (file_format == FF_BSD44
649 || hz != hertz())
651 padsize = 3*4;
652 switch (bfd_arch_bits_per_address (core_bfd))
654 case 32:
655 hdrsize = GMON_HDRSIZE_BSD44_32;
656 break;
658 case 64:
659 hdrsize = GMON_HDRSIZE_BSD44_64;
660 break;
662 default:
663 fprintf (stderr,
664 _("%s: bits per address has unexpected value of %u\n"),
665 whoami, bfd_arch_bits_per_address (core_bfd));
666 done (1);
669 else
671 padsize = 0;
672 switch (bfd_arch_bits_per_address (core_bfd))
674 case 32:
675 hdrsize = GMON_HDRSIZE_OLDBSD_32;
676 break;
678 case 64:
679 hdrsize = GMON_HDRSIZE_OLDBSD_64;
680 /* FIXME: Checking host compiler defines here means that we can't
681 use a cross gprof alpha OSF. */
682 #if defined(__alpha__) && defined (__osf__)
683 padsize = 4;
684 #endif
685 break;
687 default:
688 fprintf (stderr,
689 _("%s: bits per address has unexpected value of %u\n"),
690 whoami, bfd_arch_bits_per_address (core_bfd));
691 done (1);
695 /* Write the parts of the headers that are common to both the
696 old BSD and 4.4BSD formats. */
697 if (gmon_io_write_vma (ofp, s_lowpc)
698 || gmon_io_write_vma (ofp, s_highpc)
699 || gmon_io_write_32 (ofp, hist_num_bins * sizeof (UNIT) + hdrsize))
701 perror (filename);
702 done (1);
705 /* Write out the 4.4BSD header bits, if that's what we're using. */
706 if (file_format == FF_BSD44
707 || hz != hertz())
709 if (gmon_io_write_32 (ofp, GMONVERSION)
710 || gmon_io_write_32 (ofp, (unsigned int) hz))
712 perror (filename);
713 done (1);
717 /* Now write out any necessary padding after the meaningful
718 header bits. */
719 if (padsize != 0
720 && fwrite (pad, 1, padsize, ofp) != padsize)
722 perror (filename);
723 done (1);
726 /* Dump the samples. */
727 for (i = 0; i < hist_num_bins; ++i)
729 bfd_put_16 (core_bfd, (bfd_vma) hist_sample[i],
730 (bfd_byte *) &raw_bin_count[0]);
731 if (fwrite (&raw_bin_count[0], sizeof (raw_bin_count), 1, ofp) != 1)
733 perror (filename);
734 done (1);
738 /* Dump the normalized raw arc information. */
739 for (sym = symtab.base; sym < symtab.limit; ++sym)
741 for (arc = sym->cg.children; arc; arc = arc->next_child)
743 if (gmon_write_raw_arc (ofp, arc->parent->addr,
744 arc->child->addr, arc->count))
746 perror (filename);
747 done (1);
749 DBG (SAMPLEDEBUG,
750 printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
751 (unsigned long) arc->parent->addr,
752 (unsigned long) arc->child->addr, arc->count));
756 fclose (ofp);
758 else
760 fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
761 whoami, file_format);
762 done (1);