OSDN Git Service

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