xref: /openbsd-src/gnu/usr.bin/binutils-2.17/gprof/gmon_io.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* gmon_io.c - Input and output from/to gmon.out files.
2*3d8817e4Smiod 
3*3d8817e4Smiod    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005
4*3d8817e4Smiod    Free Software Foundation, Inc.
5*3d8817e4Smiod 
6*3d8817e4Smiod    This file is part of GNU Binutils.
7*3d8817e4Smiod 
8*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
9*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
10*3d8817e4Smiod    the Free Software Foundation; either version 2 of the License, or
11*3d8817e4Smiod    (at your option) any later version.
12*3d8817e4Smiod 
13*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
14*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*3d8817e4Smiod    GNU General Public License for more details.
17*3d8817e4Smiod 
18*3d8817e4Smiod    You should have received a copy of the GNU General Public License
19*3d8817e4Smiod    along with this program; if not, write to the Free Software
20*3d8817e4Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21*3d8817e4Smiod    02110-1301, USA.  */
22*3d8817e4Smiod 
23*3d8817e4Smiod #include "gprof.h"
24*3d8817e4Smiod #include "search_list.h"
25*3d8817e4Smiod #include "source.h"
26*3d8817e4Smiod #include "symtab.h"
27*3d8817e4Smiod #include "cg_arcs.h"
28*3d8817e4Smiod #include "basic_blocks.h"
29*3d8817e4Smiod #include "corefile.h"
30*3d8817e4Smiod #include "call_graph.h"
31*3d8817e4Smiod #include "gmon_io.h"
32*3d8817e4Smiod #include "gmon_out.h"
33*3d8817e4Smiod #include "gmon.h"		/* Fetch header for old format.  */
34*3d8817e4Smiod #include "hertz.h"
35*3d8817e4Smiod #include "hist.h"
36*3d8817e4Smiod #include "libiberty.h"
37*3d8817e4Smiod 
38*3d8817e4Smiod enum gmon_ptr_size {
39*3d8817e4Smiod   ptr_32bit,
40*3d8817e4Smiod   ptr_64bit
41*3d8817e4Smiod };
42*3d8817e4Smiod 
43*3d8817e4Smiod enum gmon_ptr_signedness {
44*3d8817e4Smiod   ptr_signed,
45*3d8817e4Smiod   ptr_unsigned
46*3d8817e4Smiod };
47*3d8817e4Smiod 
48*3d8817e4Smiod static enum gmon_ptr_size gmon_get_ptr_size (void);
49*3d8817e4Smiod static enum gmon_ptr_signedness gmon_get_ptr_signedness (void);
50*3d8817e4Smiod 
51*3d8817e4Smiod #ifdef BFD_HOST_U_64_BIT
52*3d8817e4Smiod static int gmon_io_read_64 (FILE *, BFD_HOST_U_64_BIT *);
53*3d8817e4Smiod static int gmon_io_write_64 (FILE *, BFD_HOST_U_64_BIT);
54*3d8817e4Smiod #endif
55*3d8817e4Smiod static int gmon_read_raw_arc
56*3d8817e4Smiod   (FILE *, bfd_vma *, bfd_vma *, unsigned long *);
57*3d8817e4Smiod static int gmon_write_raw_arc
58*3d8817e4Smiod   (FILE *, bfd_vma, bfd_vma, unsigned long);
59*3d8817e4Smiod 
60*3d8817e4Smiod int gmon_input = 0;
61*3d8817e4Smiod int gmon_file_version = 0;	/* 0 == old (non-versioned) file format.  */
62*3d8817e4Smiod 
63*3d8817e4Smiod static enum gmon_ptr_size
gmon_get_ptr_size()64*3d8817e4Smiod gmon_get_ptr_size ()
65*3d8817e4Smiod {
66*3d8817e4Smiod   int size;
67*3d8817e4Smiod 
68*3d8817e4Smiod   /* Pick best size for pointers.  Start with the ELF size, and if not
69*3d8817e4Smiod      elf go with the architecture's address size.  */
70*3d8817e4Smiod   size = bfd_get_arch_size (core_bfd);
71*3d8817e4Smiod   if (size == -1)
72*3d8817e4Smiod     size = bfd_arch_bits_per_address (core_bfd);
73*3d8817e4Smiod 
74*3d8817e4Smiod   switch (size)
75*3d8817e4Smiod     {
76*3d8817e4Smiod     case 32:
77*3d8817e4Smiod       return ptr_32bit;
78*3d8817e4Smiod 
79*3d8817e4Smiod     case 64:
80*3d8817e4Smiod       return ptr_64bit;
81*3d8817e4Smiod 
82*3d8817e4Smiod     default:
83*3d8817e4Smiod       fprintf (stderr, _("%s: address size has unexpected value of %u\n"),
84*3d8817e4Smiod 	       whoami, size);
85*3d8817e4Smiod       done (1);
86*3d8817e4Smiod     }
87*3d8817e4Smiod }
88*3d8817e4Smiod 
89*3d8817e4Smiod static enum gmon_ptr_signedness
gmon_get_ptr_signedness()90*3d8817e4Smiod gmon_get_ptr_signedness ()
91*3d8817e4Smiod {
92*3d8817e4Smiod   int sext;
93*3d8817e4Smiod 
94*3d8817e4Smiod   /* Figure out whether to sign extend.  If BFD doesn't know, assume no.  */
95*3d8817e4Smiod   sext = bfd_get_sign_extend_vma (core_bfd);
96*3d8817e4Smiod   if (sext == -1)
97*3d8817e4Smiod     return ptr_unsigned;
98*3d8817e4Smiod   return (sext ? ptr_signed : ptr_unsigned);
99*3d8817e4Smiod }
100*3d8817e4Smiod 
101*3d8817e4Smiod int
gmon_io_read_32(FILE * ifp,unsigned int * valp)102*3d8817e4Smiod gmon_io_read_32 (FILE *ifp, unsigned int *valp)
103*3d8817e4Smiod {
104*3d8817e4Smiod   char buf[4];
105*3d8817e4Smiod 
106*3d8817e4Smiod   if (fread (buf, 1, 4, ifp) != 4)
107*3d8817e4Smiod     return 1;
108*3d8817e4Smiod   *valp = bfd_get_32 (core_bfd, buf);
109*3d8817e4Smiod   return 0;
110*3d8817e4Smiod }
111*3d8817e4Smiod 
112*3d8817e4Smiod #ifdef BFD_HOST_U_64_BIT
113*3d8817e4Smiod static int
gmon_io_read_64(FILE * ifp,BFD_HOST_U_64_BIT * valp)114*3d8817e4Smiod gmon_io_read_64 (FILE *ifp, BFD_HOST_U_64_BIT *valp)
115*3d8817e4Smiod {
116*3d8817e4Smiod   char buf[8];
117*3d8817e4Smiod 
118*3d8817e4Smiod   if (fread (buf, 1, 8, ifp) != 8)
119*3d8817e4Smiod     return 1;
120*3d8817e4Smiod   *valp = bfd_get_64 (core_bfd, buf);
121*3d8817e4Smiod   return 0;
122*3d8817e4Smiod }
123*3d8817e4Smiod #endif
124*3d8817e4Smiod 
125*3d8817e4Smiod int
gmon_io_read_vma(FILE * ifp,bfd_vma * valp)126*3d8817e4Smiod gmon_io_read_vma (FILE *ifp, bfd_vma *valp)
127*3d8817e4Smiod {
128*3d8817e4Smiod   unsigned int val32;
129*3d8817e4Smiod #ifdef BFD_HOST_U_64_BIT
130*3d8817e4Smiod   BFD_HOST_U_64_BIT val64;
131*3d8817e4Smiod #endif
132*3d8817e4Smiod 
133*3d8817e4Smiod   switch (gmon_get_ptr_size ())
134*3d8817e4Smiod     {
135*3d8817e4Smiod     case ptr_32bit:
136*3d8817e4Smiod       if (gmon_io_read_32 (ifp, &val32))
137*3d8817e4Smiod 	return 1;
138*3d8817e4Smiod       if (gmon_get_ptr_signedness () == ptr_signed)
139*3d8817e4Smiod         *valp = (int) val32;
140*3d8817e4Smiod       else
141*3d8817e4Smiod         *valp = val32;
142*3d8817e4Smiod       break;
143*3d8817e4Smiod 
144*3d8817e4Smiod #ifdef BFD_HOST_U_64_BIT
145*3d8817e4Smiod     case ptr_64bit:
146*3d8817e4Smiod       if (gmon_io_read_64 (ifp, &val64))
147*3d8817e4Smiod 	return 1;
148*3d8817e4Smiod #ifdef BFD_HOST_64_BIT
149*3d8817e4Smiod       if (gmon_get_ptr_signedness () == ptr_signed)
150*3d8817e4Smiod         *valp = (BFD_HOST_64_BIT) val64;
151*3d8817e4Smiod       else
152*3d8817e4Smiod #endif
153*3d8817e4Smiod         *valp = val64;
154*3d8817e4Smiod       break;
155*3d8817e4Smiod #endif
156*3d8817e4Smiod     }
157*3d8817e4Smiod   return 0;
158*3d8817e4Smiod }
159*3d8817e4Smiod 
160*3d8817e4Smiod int
gmon_io_read(FILE * ifp,char * buf,size_t n)161*3d8817e4Smiod gmon_io_read (FILE *ifp, char *buf, size_t n)
162*3d8817e4Smiod {
163*3d8817e4Smiod   if (fread (buf, 1, n, ifp) != n)
164*3d8817e4Smiod     return 1;
165*3d8817e4Smiod   return 0;
166*3d8817e4Smiod }
167*3d8817e4Smiod 
168*3d8817e4Smiod int
gmon_io_write_32(FILE * ofp,unsigned int val)169*3d8817e4Smiod gmon_io_write_32 (FILE *ofp, unsigned int val)
170*3d8817e4Smiod {
171*3d8817e4Smiod   char buf[4];
172*3d8817e4Smiod 
173*3d8817e4Smiod   bfd_put_32 (core_bfd, (bfd_vma) val, buf);
174*3d8817e4Smiod   if (fwrite (buf, 1, 4, ofp) != 4)
175*3d8817e4Smiod     return 1;
176*3d8817e4Smiod   return 0;
177*3d8817e4Smiod }
178*3d8817e4Smiod 
179*3d8817e4Smiod #ifdef BFD_HOST_U_64_BIT
180*3d8817e4Smiod static int
gmon_io_write_64(FILE * ofp,BFD_HOST_U_64_BIT val)181*3d8817e4Smiod gmon_io_write_64 (FILE *ofp, BFD_HOST_U_64_BIT val)
182*3d8817e4Smiod {
183*3d8817e4Smiod   char buf[8];
184*3d8817e4Smiod 
185*3d8817e4Smiod   bfd_put_64 (core_bfd, (bfd_vma) val, buf);
186*3d8817e4Smiod   if (fwrite (buf, 1, 8, ofp) != 8)
187*3d8817e4Smiod     return 1;
188*3d8817e4Smiod   return 0;
189*3d8817e4Smiod }
190*3d8817e4Smiod #endif
191*3d8817e4Smiod 
192*3d8817e4Smiod int
gmon_io_write_vma(FILE * ofp,bfd_vma val)193*3d8817e4Smiod gmon_io_write_vma (FILE *ofp, bfd_vma val)
194*3d8817e4Smiod {
195*3d8817e4Smiod 
196*3d8817e4Smiod   switch (gmon_get_ptr_size ())
197*3d8817e4Smiod     {
198*3d8817e4Smiod     case ptr_32bit:
199*3d8817e4Smiod       if (gmon_io_write_32 (ofp, (unsigned int) val))
200*3d8817e4Smiod 	return 1;
201*3d8817e4Smiod       break;
202*3d8817e4Smiod 
203*3d8817e4Smiod #ifdef BFD_HOST_U_64_BIT
204*3d8817e4Smiod     case ptr_64bit:
205*3d8817e4Smiod       if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) val))
206*3d8817e4Smiod 	return 1;
207*3d8817e4Smiod       break;
208*3d8817e4Smiod #endif
209*3d8817e4Smiod     }
210*3d8817e4Smiod   return 0;
211*3d8817e4Smiod }
212*3d8817e4Smiod 
213*3d8817e4Smiod int
gmon_io_write_8(FILE * ofp,unsigned int val)214*3d8817e4Smiod gmon_io_write_8 (FILE *ofp, unsigned int val)
215*3d8817e4Smiod {
216*3d8817e4Smiod   char buf[1];
217*3d8817e4Smiod 
218*3d8817e4Smiod   bfd_put_8 (core_bfd, val, buf);
219*3d8817e4Smiod   if (fwrite (buf, 1, 1, ofp) != 1)
220*3d8817e4Smiod     return 1;
221*3d8817e4Smiod   return 0;
222*3d8817e4Smiod }
223*3d8817e4Smiod 
224*3d8817e4Smiod int
gmon_io_write(FILE * ofp,char * buf,size_t n)225*3d8817e4Smiod gmon_io_write (FILE *ofp, char *buf, size_t n)
226*3d8817e4Smiod {
227*3d8817e4Smiod   if (fwrite (buf, 1, n, ofp) != n)
228*3d8817e4Smiod     return 1;
229*3d8817e4Smiod   return 0;
230*3d8817e4Smiod }
231*3d8817e4Smiod 
232*3d8817e4Smiod static int
gmon_read_raw_arc(FILE * ifp,bfd_vma * fpc,bfd_vma * spc,unsigned long * cnt)233*3d8817e4Smiod gmon_read_raw_arc (FILE *ifp, bfd_vma *fpc, bfd_vma *spc, unsigned long *cnt)
234*3d8817e4Smiod {
235*3d8817e4Smiod #ifdef BFD_HOST_U_64_BIT
236*3d8817e4Smiod   BFD_HOST_U_64_BIT cnt64;
237*3d8817e4Smiod #endif
238*3d8817e4Smiod   unsigned int cnt32;
239*3d8817e4Smiod 
240*3d8817e4Smiod   if (gmon_io_read_vma (ifp, fpc)
241*3d8817e4Smiod       || gmon_io_read_vma (ifp, spc))
242*3d8817e4Smiod     return 1;
243*3d8817e4Smiod 
244*3d8817e4Smiod   switch (gmon_get_ptr_size ())
245*3d8817e4Smiod     {
246*3d8817e4Smiod     case ptr_32bit:
247*3d8817e4Smiod       if (gmon_io_read_32 (ifp, &cnt32))
248*3d8817e4Smiod 	return 1;
249*3d8817e4Smiod       *cnt = cnt32;
250*3d8817e4Smiod       break;
251*3d8817e4Smiod 
252*3d8817e4Smiod #ifdef BFD_HOST_U_64_BIT
253*3d8817e4Smiod     case ptr_64bit:
254*3d8817e4Smiod       if (gmon_io_read_64 (ifp, &cnt64))
255*3d8817e4Smiod 	return 1;
256*3d8817e4Smiod       *cnt = cnt64;
257*3d8817e4Smiod       break;
258*3d8817e4Smiod #endif
259*3d8817e4Smiod 
260*3d8817e4Smiod     default:
261*3d8817e4Smiod       return 1;
262*3d8817e4Smiod     }
263*3d8817e4Smiod   return 0;
264*3d8817e4Smiod }
265*3d8817e4Smiod 
266*3d8817e4Smiod static int
gmon_write_raw_arc(FILE * ofp,bfd_vma fpc,bfd_vma spc,unsigned long cnt)267*3d8817e4Smiod gmon_write_raw_arc (FILE *ofp, bfd_vma fpc, bfd_vma spc, unsigned long cnt)
268*3d8817e4Smiod {
269*3d8817e4Smiod 
270*3d8817e4Smiod   if (gmon_io_write_vma (ofp, fpc)
271*3d8817e4Smiod       || gmon_io_write_vma (ofp, spc))
272*3d8817e4Smiod     return 1;
273*3d8817e4Smiod 
274*3d8817e4Smiod   switch (gmon_get_ptr_size ())
275*3d8817e4Smiod     {
276*3d8817e4Smiod     case ptr_32bit:
277*3d8817e4Smiod       if (gmon_io_write_32 (ofp, (unsigned int) cnt))
278*3d8817e4Smiod 	return 1;
279*3d8817e4Smiod       break;
280*3d8817e4Smiod 
281*3d8817e4Smiod #ifdef BFD_HOST_U_64_BIT
282*3d8817e4Smiod     case ptr_64bit:
283*3d8817e4Smiod       if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) cnt))
284*3d8817e4Smiod 	return 1;
285*3d8817e4Smiod       break;
286*3d8817e4Smiod #endif
287*3d8817e4Smiod     }
288*3d8817e4Smiod   return 0;
289*3d8817e4Smiod }
290*3d8817e4Smiod 
291*3d8817e4Smiod void
gmon_out_read(const char * filename)292*3d8817e4Smiod gmon_out_read (const char *filename)
293*3d8817e4Smiod {
294*3d8817e4Smiod   FILE *ifp;
295*3d8817e4Smiod   struct gmon_hdr ghdr;
296*3d8817e4Smiod   unsigned char tag;
297*3d8817e4Smiod   int nhist = 0, narcs = 0, nbbs = 0;
298*3d8817e4Smiod 
299*3d8817e4Smiod   /* Open gmon.out file.  */
300*3d8817e4Smiod   if (strcmp (filename, "-") == 0)
301*3d8817e4Smiod     {
302*3d8817e4Smiod       ifp = stdin;
303*3d8817e4Smiod #ifdef SET_BINARY
304*3d8817e4Smiod       SET_BINARY (fileno (stdin));
305*3d8817e4Smiod #endif
306*3d8817e4Smiod     }
307*3d8817e4Smiod   else
308*3d8817e4Smiod     {
309*3d8817e4Smiod       ifp = fopen (filename, FOPEN_RB);
310*3d8817e4Smiod 
311*3d8817e4Smiod       if (!ifp)
312*3d8817e4Smiod 	{
313*3d8817e4Smiod 	  perror (filename);
314*3d8817e4Smiod 	  done (1);
315*3d8817e4Smiod 	}
316*3d8817e4Smiod     }
317*3d8817e4Smiod 
318*3d8817e4Smiod   if (fread (&ghdr, sizeof (struct gmon_hdr), 1, ifp) != 1)
319*3d8817e4Smiod     {
320*3d8817e4Smiod       fprintf (stderr, _("%s: file too short to be a gmon file\n"),
321*3d8817e4Smiod 	       filename);
322*3d8817e4Smiod       done (1);
323*3d8817e4Smiod     }
324*3d8817e4Smiod 
325*3d8817e4Smiod   if ((file_format == FF_MAGIC)
326*3d8817e4Smiod       || (file_format == FF_AUTO && !strncmp (&ghdr.cookie[0], GMON_MAGIC, 4)))
327*3d8817e4Smiod     {
328*3d8817e4Smiod       if (file_format == FF_MAGIC && strncmp (&ghdr.cookie[0], GMON_MAGIC, 4))
329*3d8817e4Smiod 	{
330*3d8817e4Smiod 	  fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
331*3d8817e4Smiod 		   whoami, filename);
332*3d8817e4Smiod 	  done (1);
333*3d8817e4Smiod 	}
334*3d8817e4Smiod 
335*3d8817e4Smiod       /* Right magic, so it's probably really a new gmon.out file.  */
336*3d8817e4Smiod       gmon_file_version = bfd_get_32 (core_bfd, (bfd_byte *) ghdr.version);
337*3d8817e4Smiod 
338*3d8817e4Smiod       if (gmon_file_version != GMON_VERSION && gmon_file_version != 0)
339*3d8817e4Smiod 	{
340*3d8817e4Smiod 	  fprintf (stderr,
341*3d8817e4Smiod 		   _("%s: file `%s' has unsupported version %d\n"),
342*3d8817e4Smiod 		   whoami, filename, gmon_file_version);
343*3d8817e4Smiod 	  done (1);
344*3d8817e4Smiod 	}
345*3d8817e4Smiod 
346*3d8817e4Smiod       /* Read in all the records.  */
347*3d8817e4Smiod       while (fread (&tag, sizeof (tag), 1, ifp) == 1)
348*3d8817e4Smiod 	{
349*3d8817e4Smiod 	  switch (tag)
350*3d8817e4Smiod 	    {
351*3d8817e4Smiod 	    case GMON_TAG_TIME_HIST:
352*3d8817e4Smiod 	      ++nhist;
353*3d8817e4Smiod 	      gmon_input |= INPUT_HISTOGRAM;
354*3d8817e4Smiod 	      hist_read_rec (ifp, filename);
355*3d8817e4Smiod 	      break;
356*3d8817e4Smiod 
357*3d8817e4Smiod 	    case GMON_TAG_CG_ARC:
358*3d8817e4Smiod 	      ++narcs;
359*3d8817e4Smiod 	      gmon_input |= INPUT_CALL_GRAPH;
360*3d8817e4Smiod 	      cg_read_rec (ifp, filename);
361*3d8817e4Smiod 	      break;
362*3d8817e4Smiod 
363*3d8817e4Smiod 	    case GMON_TAG_BB_COUNT:
364*3d8817e4Smiod 	      ++nbbs;
365*3d8817e4Smiod 	      gmon_input |= INPUT_BB_COUNTS;
366*3d8817e4Smiod 	      bb_read_rec (ifp, filename);
367*3d8817e4Smiod 	      break;
368*3d8817e4Smiod 
369*3d8817e4Smiod 	    default:
370*3d8817e4Smiod 	      fprintf (stderr,
371*3d8817e4Smiod 		       _("%s: %s: found bad tag %d (file corrupted?)\n"),
372*3d8817e4Smiod 		       whoami, filename, tag);
373*3d8817e4Smiod 	      done (1);
374*3d8817e4Smiod 	    }
375*3d8817e4Smiod 	}
376*3d8817e4Smiod     }
377*3d8817e4Smiod   else if (file_format == FF_AUTO
378*3d8817e4Smiod 	   || file_format == FF_BSD
379*3d8817e4Smiod 	   || file_format == FF_BSD44)
380*3d8817e4Smiod     {
381*3d8817e4Smiod       struct hdr
382*3d8817e4Smiod       {
383*3d8817e4Smiod 	bfd_vma low_pc;
384*3d8817e4Smiod 	bfd_vma high_pc;
385*3d8817e4Smiod 	unsigned int ncnt;
386*3d8817e4Smiod       };
387*3d8817e4Smiod       unsigned int i;
388*3d8817e4Smiod       int samp_bytes, header_size = 0;
389*3d8817e4Smiod       unsigned long count;
390*3d8817e4Smiod       bfd_vma from_pc, self_pc;
391*3d8817e4Smiod       static struct hdr h;
392*3d8817e4Smiod       UNIT raw_bin_count;
393*3d8817e4Smiod       struct hdr tmp;
394*3d8817e4Smiod       unsigned int version;
395*3d8817e4Smiod 
396*3d8817e4Smiod       /* Information from a gmon.out file is in two parts: an array of
397*3d8817e4Smiod 	 sampling hits within pc ranges, and the arcs.  */
398*3d8817e4Smiod       gmon_input = INPUT_HISTOGRAM | INPUT_CALL_GRAPH;
399*3d8817e4Smiod 
400*3d8817e4Smiod       /* This fseek() ought to work even on stdin as long as it's
401*3d8817e4Smiod 	 not an interactive device (heck, is there anybody who would
402*3d8817e4Smiod 	 want to type in a gmon.out at the terminal?).  */
403*3d8817e4Smiod       if (fseek (ifp, 0, SEEK_SET) < 0)
404*3d8817e4Smiod 	{
405*3d8817e4Smiod 	  perror (filename);
406*3d8817e4Smiod 	  done (1);
407*3d8817e4Smiod 	}
408*3d8817e4Smiod 
409*3d8817e4Smiod       /* The beginning of the old BSD header and the 4.4BSD header
410*3d8817e4Smiod 	 are the same: lowpc, highpc, ncnt  */
411*3d8817e4Smiod       if (gmon_io_read_vma (ifp, &tmp.low_pc)
412*3d8817e4Smiod           || gmon_io_read_vma (ifp, &tmp.high_pc)
413*3d8817e4Smiod           || gmon_io_read_32 (ifp, &tmp.ncnt))
414*3d8817e4Smiod 	{
415*3d8817e4Smiod  bad_gmon_file:
416*3d8817e4Smiod           fprintf (stderr, _("%s: file too short to be a gmon file\n"),
417*3d8817e4Smiod 		   filename);
418*3d8817e4Smiod 	  done (1);
419*3d8817e4Smiod 	}
420*3d8817e4Smiod 
421*3d8817e4Smiod       /* Check to see if this a 4.4BSD-style header.  */
422*3d8817e4Smiod       if (gmon_io_read_32 (ifp, &version))
423*3d8817e4Smiod 	goto bad_gmon_file;
424*3d8817e4Smiod 
425*3d8817e4Smiod       if (version == GMONVERSION)
426*3d8817e4Smiod 	{
427*3d8817e4Smiod 	  unsigned int profrate;
428*3d8817e4Smiod 
429*3d8817e4Smiod 	  /* 4.4BSD format header.  */
430*3d8817e4Smiod           if (gmon_io_read_32 (ifp, &profrate))
431*3d8817e4Smiod 	    goto bad_gmon_file;
432*3d8817e4Smiod 
433*3d8817e4Smiod 	  if (!s_highpc)
434*3d8817e4Smiod 	    hz = profrate;
435*3d8817e4Smiod 	  else if (hz != (int) profrate)
436*3d8817e4Smiod 	    {
437*3d8817e4Smiod 	      fprintf (stderr,
438*3d8817e4Smiod 		       _("%s: profiling rate incompatible with first gmon file\n"),
439*3d8817e4Smiod 		       filename);
440*3d8817e4Smiod 	      done (1);
441*3d8817e4Smiod 	    }
442*3d8817e4Smiod 
443*3d8817e4Smiod 	  switch (gmon_get_ptr_size ())
444*3d8817e4Smiod 	    {
445*3d8817e4Smiod 	    case ptr_32bit:
446*3d8817e4Smiod 	      header_size = GMON_HDRSIZE_BSD44_32;
447*3d8817e4Smiod 	      break;
448*3d8817e4Smiod 
449*3d8817e4Smiod 	    case ptr_64bit:
450*3d8817e4Smiod 	      header_size = GMON_HDRSIZE_BSD44_64;
451*3d8817e4Smiod 	      break;
452*3d8817e4Smiod 	    }
453*3d8817e4Smiod 	}
454*3d8817e4Smiod       else
455*3d8817e4Smiod 	{
456*3d8817e4Smiod 	  /* Old style BSD format.  */
457*3d8817e4Smiod 	  if (file_format == FF_BSD44)
458*3d8817e4Smiod 	    {
459*3d8817e4Smiod 	      fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
460*3d8817e4Smiod 		       whoami, filename);
461*3d8817e4Smiod 	      done (1);
462*3d8817e4Smiod 	    }
463*3d8817e4Smiod 
464*3d8817e4Smiod 	  switch (gmon_get_ptr_size ())
465*3d8817e4Smiod 	    {
466*3d8817e4Smiod 	    case ptr_32bit:
467*3d8817e4Smiod 	      header_size = GMON_HDRSIZE_OLDBSD_32;
468*3d8817e4Smiod 	      break;
469*3d8817e4Smiod 
470*3d8817e4Smiod 	    case ptr_64bit:
471*3d8817e4Smiod 	      header_size = GMON_HDRSIZE_OLDBSD_64;
472*3d8817e4Smiod 	      break;
473*3d8817e4Smiod 	    }
474*3d8817e4Smiod 	}
475*3d8817e4Smiod 
476*3d8817e4Smiod       /* Position the file to after the header.  */
477*3d8817e4Smiod       if (fseek (ifp, header_size, SEEK_SET) < 0)
478*3d8817e4Smiod 	{
479*3d8817e4Smiod 	  perror (filename);
480*3d8817e4Smiod 	  done (1);
481*3d8817e4Smiod 	}
482*3d8817e4Smiod 
483*3d8817e4Smiod       if (s_highpc && (tmp.low_pc != h.low_pc
484*3d8817e4Smiod 		       || tmp.high_pc != h.high_pc || tmp.ncnt != h.ncnt))
485*3d8817e4Smiod 	{
486*3d8817e4Smiod 	  fprintf (stderr, _("%s: incompatible with first gmon file\n"),
487*3d8817e4Smiod 		   filename);
488*3d8817e4Smiod 	  done (1);
489*3d8817e4Smiod 	}
490*3d8817e4Smiod 
491*3d8817e4Smiod       h = tmp;
492*3d8817e4Smiod       s_lowpc = (bfd_vma) h.low_pc;
493*3d8817e4Smiod       s_highpc = (bfd_vma) h.high_pc;
494*3d8817e4Smiod       lowpc = (bfd_vma) h.low_pc / sizeof (UNIT);
495*3d8817e4Smiod       highpc = (bfd_vma) h.high_pc / sizeof (UNIT);
496*3d8817e4Smiod       samp_bytes = h.ncnt - header_size;
497*3d8817e4Smiod       hist_num_bins = samp_bytes / sizeof (UNIT);
498*3d8817e4Smiod 
499*3d8817e4Smiod       DBG (SAMPLEDEBUG,
500*3d8817e4Smiod 	   printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
501*3d8817e4Smiod 		   (unsigned long) h.low_pc, (unsigned long) h.high_pc,
502*3d8817e4Smiod 		   h.ncnt);
503*3d8817e4Smiod 	   printf ("[gmon_out_read]   s_lowpc 0x%lx   s_highpc 0x%lx\n",
504*3d8817e4Smiod 		   (unsigned long) s_lowpc, (unsigned long) s_highpc);
505*3d8817e4Smiod 	   printf ("[gmon_out_read]     lowpc 0x%lx     highpc 0x%lx\n",
506*3d8817e4Smiod 		   (unsigned long) lowpc, (unsigned long) highpc);
507*3d8817e4Smiod 	   printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
508*3d8817e4Smiod 		   samp_bytes, hist_num_bins));
509*3d8817e4Smiod 
510*3d8817e4Smiod       /* Make sure that we have sensible values.  */
511*3d8817e4Smiod       if (samp_bytes < 0 || lowpc > highpc)
512*3d8817e4Smiod 	{
513*3d8817e4Smiod 	  fprintf (stderr,
514*3d8817e4Smiod 	    _("%s: file '%s' does not appear to be in gmon.out format\n"),
515*3d8817e4Smiod 	    whoami, filename);
516*3d8817e4Smiod 	  done (1);
517*3d8817e4Smiod 	}
518*3d8817e4Smiod 
519*3d8817e4Smiod       if (hist_num_bins)
520*3d8817e4Smiod 	++nhist;
521*3d8817e4Smiod 
522*3d8817e4Smiod       if (!hist_sample)
523*3d8817e4Smiod 	{
524*3d8817e4Smiod 	  hist_sample =
525*3d8817e4Smiod 	    (int *) xmalloc (hist_num_bins * sizeof (hist_sample[0]));
526*3d8817e4Smiod 
527*3d8817e4Smiod 	  memset (hist_sample, 0, hist_num_bins * sizeof (hist_sample[0]));
528*3d8817e4Smiod 	}
529*3d8817e4Smiod 
530*3d8817e4Smiod       for (i = 0; i < hist_num_bins; ++i)
531*3d8817e4Smiod 	{
532*3d8817e4Smiod 	  if (fread (raw_bin_count, sizeof (raw_bin_count), 1, ifp) != 1)
533*3d8817e4Smiod 	    {
534*3d8817e4Smiod 	      fprintf (stderr,
535*3d8817e4Smiod 		       _("%s: unexpected EOF after reading %d/%d bins\n"),
536*3d8817e4Smiod 		       whoami, --i, hist_num_bins);
537*3d8817e4Smiod 	      done (1);
538*3d8817e4Smiod 	    }
539*3d8817e4Smiod 
540*3d8817e4Smiod 	  hist_sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) raw_bin_count);
541*3d8817e4Smiod 	}
542*3d8817e4Smiod 
543*3d8817e4Smiod       /* The rest of the file consists of a bunch of
544*3d8817e4Smiod 	 <from,self,count> tuples.  */
545*3d8817e4Smiod       while (gmon_read_raw_arc (ifp, &from_pc, &self_pc, &count) == 0)
546*3d8817e4Smiod 	{
547*3d8817e4Smiod 	  ++narcs;
548*3d8817e4Smiod 
549*3d8817e4Smiod 	  DBG (SAMPLEDEBUG,
550*3d8817e4Smiod 	     printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
551*3d8817e4Smiod 		     (unsigned long) from_pc, (unsigned long) self_pc, count));
552*3d8817e4Smiod 
553*3d8817e4Smiod 	  /* Add this arc.  */
554*3d8817e4Smiod 	  cg_tally (from_pc, self_pc, count);
555*3d8817e4Smiod 	}
556*3d8817e4Smiod 
557*3d8817e4Smiod       fclose (ifp);
558*3d8817e4Smiod 
559*3d8817e4Smiod       if (hz == HZ_WRONG)
560*3d8817e4Smiod 	{
561*3d8817e4Smiod 	  /* How many ticks per second?  If we can't tell, report
562*3d8817e4Smiod 	     time in ticks.  */
563*3d8817e4Smiod 	  hz = hertz ();
564*3d8817e4Smiod 
565*3d8817e4Smiod 	  if (hz == HZ_WRONG)
566*3d8817e4Smiod 	    {
567*3d8817e4Smiod 	      hz = 1;
568*3d8817e4Smiod 	      fprintf (stderr, _("time is in ticks, not seconds\n"));
569*3d8817e4Smiod 	    }
570*3d8817e4Smiod 	}
571*3d8817e4Smiod     }
572*3d8817e4Smiod   else
573*3d8817e4Smiod     {
574*3d8817e4Smiod       fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
575*3d8817e4Smiod 	       whoami, file_format);
576*3d8817e4Smiod       done (1);
577*3d8817e4Smiod     }
578*3d8817e4Smiod 
579*3d8817e4Smiod   if (output_style & STYLE_GMON_INFO)
580*3d8817e4Smiod     {
581*3d8817e4Smiod       printf (_("File `%s' (version %d) contains:\n"),
582*3d8817e4Smiod 	      filename, gmon_file_version);
583*3d8817e4Smiod       printf (nhist == 1 ?
584*3d8817e4Smiod 	      _("\t%d histogram record\n") :
585*3d8817e4Smiod 	      _("\t%d histogram records\n"), nhist);
586*3d8817e4Smiod       printf (narcs == 1 ?
587*3d8817e4Smiod 	      _("\t%d call-graph record\n") :
588*3d8817e4Smiod 	      _("\t%d call-graph records\n"), narcs);
589*3d8817e4Smiod       printf (nbbs == 1 ?
590*3d8817e4Smiod 	      _("\t%d basic-block count record\n") :
591*3d8817e4Smiod 	      _("\t%d basic-block count records\n"), nbbs);
592*3d8817e4Smiod       first_output = FALSE;
593*3d8817e4Smiod     }
594*3d8817e4Smiod }
595*3d8817e4Smiod 
596*3d8817e4Smiod 
597*3d8817e4Smiod void
gmon_out_write(const char * filename)598*3d8817e4Smiod gmon_out_write (const char *filename)
599*3d8817e4Smiod {
600*3d8817e4Smiod   FILE *ofp;
601*3d8817e4Smiod   struct gmon_hdr ghdr;
602*3d8817e4Smiod 
603*3d8817e4Smiod   ofp = fopen (filename, FOPEN_WB);
604*3d8817e4Smiod   if (!ofp)
605*3d8817e4Smiod     {
606*3d8817e4Smiod       perror (filename);
607*3d8817e4Smiod       done (1);
608*3d8817e4Smiod     }
609*3d8817e4Smiod 
610*3d8817e4Smiod   if (file_format == FF_AUTO || file_format == FF_MAGIC)
611*3d8817e4Smiod     {
612*3d8817e4Smiod       /* Write gmon header.  */
613*3d8817e4Smiod 
614*3d8817e4Smiod       memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
615*3d8817e4Smiod       bfd_put_32 (core_bfd, (bfd_vma) GMON_VERSION, (bfd_byte *) ghdr.version);
616*3d8817e4Smiod 
617*3d8817e4Smiod       if (fwrite (&ghdr, sizeof (ghdr), 1, ofp) != 1)
618*3d8817e4Smiod 	{
619*3d8817e4Smiod 	  perror (filename);
620*3d8817e4Smiod 	  done (1);
621*3d8817e4Smiod 	}
622*3d8817e4Smiod 
623*3d8817e4Smiod       /* Write execution time histogram if we have one.  */
624*3d8817e4Smiod       if (gmon_input & INPUT_HISTOGRAM)
625*3d8817e4Smiod 	hist_write_hist (ofp, filename);
626*3d8817e4Smiod 
627*3d8817e4Smiod       /* Write call graph arcs if we have any.  */
628*3d8817e4Smiod       if (gmon_input & INPUT_CALL_GRAPH)
629*3d8817e4Smiod 	cg_write_arcs (ofp, filename);
630*3d8817e4Smiod 
631*3d8817e4Smiod       /* Write basic-block info if we have it.  */
632*3d8817e4Smiod       if (gmon_input & INPUT_BB_COUNTS)
633*3d8817e4Smiod 	bb_write_blocks (ofp, filename);
634*3d8817e4Smiod     }
635*3d8817e4Smiod   else if (file_format == FF_BSD || file_format == FF_BSD44)
636*3d8817e4Smiod     {
637*3d8817e4Smiod       UNIT raw_bin_count;
638*3d8817e4Smiod       unsigned int i, hdrsize;
639*3d8817e4Smiod       unsigned padsize;
640*3d8817e4Smiod       char pad[3*4];
641*3d8817e4Smiod       Arc *arc;
642*3d8817e4Smiod       Sym *sym;
643*3d8817e4Smiod 
644*3d8817e4Smiod       memset (pad, 0, sizeof (pad));
645*3d8817e4Smiod 
646*3d8817e4Smiod       hdrsize = 0;
647*3d8817e4Smiod       /* Decide how large the header will be.  Use the 4.4BSD format
648*3d8817e4Smiod          header if explicitly specified, or if the profiling rate is
649*3d8817e4Smiod          non-standard.  Otherwise, use the old BSD format.  */
650*3d8817e4Smiod       if (file_format == FF_BSD44
651*3d8817e4Smiod 	  || hz != hertz())
652*3d8817e4Smiod 	{
653*3d8817e4Smiod 	  padsize = 3*4;
654*3d8817e4Smiod 	  switch (gmon_get_ptr_size ())
655*3d8817e4Smiod 	    {
656*3d8817e4Smiod 	    case ptr_32bit:
657*3d8817e4Smiod 	      hdrsize = GMON_HDRSIZE_BSD44_32;
658*3d8817e4Smiod 	      break;
659*3d8817e4Smiod 
660*3d8817e4Smiod 	    case ptr_64bit:
661*3d8817e4Smiod 	      hdrsize = GMON_HDRSIZE_BSD44_64;
662*3d8817e4Smiod 	      break;
663*3d8817e4Smiod 	    }
664*3d8817e4Smiod 	}
665*3d8817e4Smiod       else
666*3d8817e4Smiod 	{
667*3d8817e4Smiod 	  padsize = 0;
668*3d8817e4Smiod 	  switch (gmon_get_ptr_size ())
669*3d8817e4Smiod 	    {
670*3d8817e4Smiod 	    case ptr_32bit:
671*3d8817e4Smiod 	      hdrsize = GMON_HDRSIZE_OLDBSD_32;
672*3d8817e4Smiod 	      break;
673*3d8817e4Smiod 
674*3d8817e4Smiod 	    case ptr_64bit:
675*3d8817e4Smiod 	      hdrsize = GMON_HDRSIZE_OLDBSD_64;
676*3d8817e4Smiod 	      /* FIXME: Checking host compiler defines here means that we can't
677*3d8817e4Smiod 		 use a cross gprof alpha OSF.  */
678*3d8817e4Smiod #if defined(__alpha__) && defined (__osf__)
679*3d8817e4Smiod 	      padsize = 4;
680*3d8817e4Smiod #endif
681*3d8817e4Smiod 	      break;
682*3d8817e4Smiod 	    }
683*3d8817e4Smiod 	}
684*3d8817e4Smiod 
685*3d8817e4Smiod       /* Write the parts of the headers that are common to both the
686*3d8817e4Smiod 	 old BSD and 4.4BSD formats.  */
687*3d8817e4Smiod       if (gmon_io_write_vma (ofp, s_lowpc)
688*3d8817e4Smiod           || gmon_io_write_vma (ofp, s_highpc)
689*3d8817e4Smiod           || gmon_io_write_32 (ofp, hist_num_bins * sizeof (UNIT) + hdrsize))
690*3d8817e4Smiod 	{
691*3d8817e4Smiod 	  perror (filename);
692*3d8817e4Smiod 	  done (1);
693*3d8817e4Smiod 	}
694*3d8817e4Smiod 
695*3d8817e4Smiod       /* Write out the 4.4BSD header bits, if that's what we're using.  */
696*3d8817e4Smiod       if (file_format == FF_BSD44
697*3d8817e4Smiod 	  || hz != hertz())
698*3d8817e4Smiod 	{
699*3d8817e4Smiod           if (gmon_io_write_32 (ofp, GMONVERSION)
700*3d8817e4Smiod 	      || gmon_io_write_32 (ofp, (unsigned int) hz))
701*3d8817e4Smiod 	    {
702*3d8817e4Smiod 	      perror (filename);
703*3d8817e4Smiod 	      done (1);
704*3d8817e4Smiod 	    }
705*3d8817e4Smiod 	}
706*3d8817e4Smiod 
707*3d8817e4Smiod       /* Now write out any necessary padding after the meaningful
708*3d8817e4Smiod 	 header bits.  */
709*3d8817e4Smiod       if (padsize != 0
710*3d8817e4Smiod           && fwrite (pad, 1, padsize, ofp) != padsize)
711*3d8817e4Smiod         {
712*3d8817e4Smiod           perror (filename);
713*3d8817e4Smiod 	  done (1);
714*3d8817e4Smiod 	}
715*3d8817e4Smiod 
716*3d8817e4Smiod       /* Dump the samples.  */
717*3d8817e4Smiod       for (i = 0; i < hist_num_bins; ++i)
718*3d8817e4Smiod 	{
719*3d8817e4Smiod 	  bfd_put_16 (core_bfd, (bfd_vma) hist_sample[i],
720*3d8817e4Smiod 		      (bfd_byte *) &raw_bin_count[0]);
721*3d8817e4Smiod 	  if (fwrite (&raw_bin_count[0], sizeof (raw_bin_count), 1, ofp) != 1)
722*3d8817e4Smiod 	    {
723*3d8817e4Smiod 	      perror (filename);
724*3d8817e4Smiod 	      done (1);
725*3d8817e4Smiod 	    }
726*3d8817e4Smiod 	}
727*3d8817e4Smiod 
728*3d8817e4Smiod       /* Dump the normalized raw arc information.  */
729*3d8817e4Smiod       for (sym = symtab.base; sym < symtab.limit; ++sym)
730*3d8817e4Smiod 	{
731*3d8817e4Smiod 	  for (arc = sym->cg.children; arc; arc = arc->next_child)
732*3d8817e4Smiod 	    {
733*3d8817e4Smiod 	      if (gmon_write_raw_arc (ofp, arc->parent->addr,
734*3d8817e4Smiod 				      arc->child->addr, arc->count))
735*3d8817e4Smiod 		{
736*3d8817e4Smiod 		  perror (filename);
737*3d8817e4Smiod 		  done (1);
738*3d8817e4Smiod 		}
739*3d8817e4Smiod 	      DBG (SAMPLEDEBUG,
740*3d8817e4Smiod 		   printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
741*3d8817e4Smiod 			   (unsigned long) arc->parent->addr,
742*3d8817e4Smiod 			   (unsigned long) arc->child->addr, arc->count));
743*3d8817e4Smiod 	    }
744*3d8817e4Smiod 	}
745*3d8817e4Smiod 
746*3d8817e4Smiod       fclose (ofp);
747*3d8817e4Smiod     }
748*3d8817e4Smiod   else
749*3d8817e4Smiod     {
750*3d8817e4Smiod       fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
751*3d8817e4Smiod 	       whoami, file_format);
752*3d8817e4Smiod       done (1);
753*3d8817e4Smiod     }
754*3d8817e4Smiod }
755