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