This commit was generated by cvs2svn to track changes on a CVS vendor
[platform/upstream/binutils.git] / gprof / gmon_io.c
1 /*
2  * Input and output from/to gmon.out files.
3  */
4 #include "cg_arcs.h"
5 #include "basic_blocks.h"
6 #include "bfd.h"
7 #include "corefile.h"
8 #include "call_graph.h"
9 #include "gmon_io.h"
10 #include "gmon_out.h"
11 #include "gmon.h"               /* fetch header for old format */
12 #include "gprof.h"
13 #include "hertz.h"
14 #include "hist.h"
15 #include "libiberty.h"
16
17 int gmon_input = 0;
18 int gmon_file_version = 0;      /* 0 == old (non-versioned) file format */
19
20 /*
21  * This probably ought to be in libbfd.
22  */
23 bfd_vma
24 DEFUN (get_vma, (abfd, addr), bfd * abfd AND bfd_byte * addr)
25 {
26   switch (sizeof (char*))
27     {
28     case 4:
29       return bfd_get_32 (abfd, addr);
30     case 8:
31       return bfd_get_64 (abfd, addr);
32     default:
33       fprintf (stderr, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
34                whoami, (long) sizeof (char*));
35       done (1);
36     }
37 }
38
39
40 /*
41  * This probably ought to be in libbfd.
42  */
43 void
44 DEFUN (put_vma, (abfd, val, addr), bfd * abfd AND bfd_vma val AND bfd_byte * addr)
45 {
46   switch (sizeof (char*))
47     {
48     case 4:
49       bfd_put_32 (abfd, val, addr);
50       break;
51     case 8:
52       bfd_put_64 (abfd, val, addr);
53       break;
54     default:
55       fprintf (stderr, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
56                whoami, (long) sizeof (char*));
57       done (1);
58     }
59 }
60
61
62 void
63 DEFUN (gmon_out_read, (filename), const char *filename)
64 {
65   FILE *ifp;
66   struct gmon_hdr ghdr;
67   unsigned char tag;
68   int nhist = 0, narcs = 0, nbbs = 0;
69
70   /* open gmon.out file: */
71
72   if (strcmp (filename, "-") == 0)
73     {
74       ifp = stdin;
75 #ifdef SET_BINARY
76       SET_BINARY (fileno (stdin));
77 #endif
78     }
79   else
80     {
81       ifp = fopen (filename, FOPEN_RB);
82       if (!ifp)
83         {
84           perror (filename);
85           done (1);
86         }
87     }
88   if (fread (&ghdr, sizeof (struct gmon_hdr), 1, ifp) != 1)
89     {
90       fprintf (stderr, _("%s: file too short to be a gmon file\n"),
91                filename);
92       done (1);
93     }
94
95   if ((file_format == FF_MAGIC) ||
96       (file_format == FF_AUTO && !strncmp (&ghdr.cookie[0], GMON_MAGIC, 4)))
97     {
98       if (file_format == FF_MAGIC && strncmp (&ghdr.cookie[0], GMON_MAGIC, 4))
99         {
100           fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
101                    whoami, filename);
102           done (1);
103         }
104
105       /* right magic, so it's probably really a new gmon.out file */
106
107       gmon_file_version = bfd_get_32 (core_bfd, (bfd_byte *) ghdr.version);
108       if (gmon_file_version != GMON_VERSION && gmon_file_version != 0)
109         {
110           fprintf (stderr,
111                    _("%s: file `%s' has unsupported version %d\n"),
112                    whoami, filename, gmon_file_version);
113           done (1);
114         }
115
116       /* read in all the records: */
117       while (fread (&tag, sizeof (tag), 1, ifp) == 1)
118         {
119           switch (tag)
120             {
121             case GMON_TAG_TIME_HIST:
122               ++nhist;
123               gmon_input |= INPUT_HISTOGRAM;
124               hist_read_rec (ifp, filename);
125               break;
126
127             case GMON_TAG_CG_ARC:
128               ++narcs;
129               gmon_input |= INPUT_CALL_GRAPH;
130               cg_read_rec (ifp, filename);
131               break;
132
133             case GMON_TAG_BB_COUNT:
134               ++nbbs;
135               gmon_input |= INPUT_BB_COUNTS;
136               bb_read_rec (ifp, filename);
137               break;
138
139             default:
140               fprintf (stderr,
141                        _("%s: %s: found bad tag %d (file corrupted?)\n"),
142                        whoami, filename, tag);
143               done (1);
144             }
145         }
146     }
147   else if (file_format == FF_AUTO
148            || file_format == FF_BSD
149            || file_format == FF_BSD44)
150     {
151       struct hdr
152       {
153         bfd_vma low_pc;
154         bfd_vma high_pc;
155         int ncnt;
156       };
157       int i, samp_bytes, header_size;
158       unsigned long count;
159       bfd_vma from_pc, self_pc;
160       struct raw_arc raw_arc;
161       struct raw_phdr raw;
162       static struct hdr h;
163       UNIT raw_bin_count;
164       struct hdr tmp;
165
166       /*
167        * Information from a gmon.out file is in two parts: an array of
168        * sampling hits within pc ranges, and the arcs.
169        */
170       gmon_input = INPUT_HISTOGRAM | INPUT_CALL_GRAPH;
171
172       /*
173        * This fseek() ought to work even on stdin as long as it's
174        * not an interactive device (heck, is there anybody who would
175        * want to type in a gmon.out at the terminal?).
176        */
177       if (fseek (ifp, 0, SEEK_SET) < 0)
178         {
179           perror (filename);
180           done (1);
181         }
182       if (fread (&raw, 1, sizeof (struct raw_phdr), ifp)
183           != sizeof (struct raw_phdr))
184         {
185           fprintf (stderr, _("%s: file too short to be a gmon file\n"),
186                    filename);
187           done (1);
188         }
189       tmp.low_pc = get_vma (core_bfd, (bfd_byte *) &raw.low_pc[0]);
190       tmp.high_pc = get_vma (core_bfd, (bfd_byte *) &raw.high_pc[0]);
191       tmp.ncnt = bfd_get_32 (core_bfd, (bfd_byte *) &raw.ncnt[0]);
192
193       if (bfd_get_32 (core_bfd, (bfd_byte *) &raw.version[0])
194           == GMONVERSION)
195         {
196           int profrate;
197
198           /* 4.4BSD format header.  */
199
200           profrate = bfd_get_32 (core_bfd, (bfd_byte *) &raw.profrate[0]);
201           if (!s_highpc)
202             hz = profrate;
203           else if (hz != profrate)
204             {
205               fprintf (stderr,
206                        _("%s: profiling rate incompatible with first gmon file\n"),
207                        filename);
208               done (1);
209             }
210
211           header_size = sizeof (struct raw_phdr);
212         }
213       else
214         {
215           /* old style BSD format.  */
216           if (file_format == FF_BSD44)
217             {
218               fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
219                        whoami, filename);
220               done (1);
221             }
222
223           if (fseek (ifp, sizeof (struct old_raw_phdr), SEEK_SET) < 0)
224             {
225               perror (filename);
226               done (1);
227             }
228
229           header_size = sizeof (struct old_raw_phdr);
230         }
231
232       if (s_highpc && (tmp.low_pc != h.low_pc ||
233                        tmp.high_pc != h.high_pc || tmp.ncnt != h.ncnt))
234         {
235           fprintf (stderr, _("%s: incompatible with first gmon file\n"),
236                    filename);
237           done (1);
238         }
239       h = tmp;
240       s_lowpc = (bfd_vma) h.low_pc;
241       s_highpc = (bfd_vma) h.high_pc;
242       lowpc = (bfd_vma) h.low_pc / sizeof (UNIT);
243       highpc = (bfd_vma) h.high_pc / sizeof (UNIT);
244       samp_bytes = h.ncnt - header_size;
245       hist_num_bins = samp_bytes / sizeof (UNIT);
246       DBG (SAMPLEDEBUG,
247            printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
248                    (unsigned long) h.low_pc, (unsigned long) h.high_pc,
249                    h.ncnt);
250            printf ("[gmon_out_read]   s_lowpc 0x%lx   s_highpc 0x%lx\n",
251                    (unsigned long) s_lowpc, (unsigned long) s_highpc);
252            printf ("[gmon_out_read]     lowpc 0x%lx     highpc 0x%lx\n",
253                    (unsigned long) lowpc, (unsigned long) highpc);
254            printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
255                    samp_bytes, hist_num_bins));
256
257       /* Make sure that we have sensible values.  */
258       if (samp_bytes < 0 || lowpc > highpc)
259         {
260           fprintf (stderr, 
261             _("%s: file '%s' does not appear to be in gmon.out format\n"),
262             whoami, filename);
263           done (1);
264         }
265
266       if (hist_num_bins)
267         {
268           ++nhist;
269         }
270
271       if (!hist_sample)
272         {
273           hist_sample =
274             (int *) xmalloc (hist_num_bins * sizeof (hist_sample[0]));
275           memset (hist_sample, 0, hist_num_bins * sizeof (hist_sample[0]));
276         }
277
278       for (i = 0; i < hist_num_bins; ++i)
279         {
280           if (fread (raw_bin_count, sizeof (raw_bin_count), 1, ifp) != 1)
281             {
282               fprintf (stderr,
283                        _("%s: unexpected EOF after reading %d/%d bins\n"),
284                        whoami, --i, hist_num_bins);
285               done (1);
286             }
287           hist_sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) raw_bin_count);
288         }
289
290       /*
291        * The rest of the file consists of a bunch of <from,self,count>
292        * tuples:
293        */
294       while (fread (&raw_arc, sizeof (raw_arc), 1, ifp) == 1)
295         {
296           ++narcs;
297           from_pc = get_vma (core_bfd, (bfd_byte *) raw_arc.from_pc);
298           self_pc = get_vma (core_bfd, (bfd_byte *) raw_arc.self_pc);
299           count = bfd_get_32 (core_bfd, (bfd_byte *) raw_arc.count);
300           DBG (SAMPLEDEBUG,
301              printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
302                      (unsigned long) from_pc, (unsigned long) self_pc, count));
303           /* add this arc: */
304           cg_tally (from_pc, self_pc, count);
305         }
306       fclose (ifp);
307
308       if (hz == HZ_WRONG)
309         {
310           /*
311            * How many ticks per second?  If we can't tell, report
312            * time in ticks.
313            */
314           hz = hertz ();
315           if (hz == HZ_WRONG)
316             {
317               hz = 1;
318               fprintf (stderr, _("time is in ticks, not seconds\n"));
319             }
320         }
321     }
322   else
323     {
324       fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
325                whoami, file_format);
326       done (1);
327     }
328
329   if (output_style & STYLE_GMON_INFO)
330     {
331       printf (_("File `%s' (version %d) contains:\n"),
332               filename, gmon_file_version);
333       printf (_("\t%d histogram record%s\n"),
334               nhist, nhist == 1 ? "" : "s");
335       printf (_("\t%d call-graph record%s\n"),
336               narcs, narcs == 1 ? "" : "s");
337       printf (_("\t%d basic-block count record%s\n"),
338               nbbs, nbbs == 1 ? "" : "s");
339       first_output = FALSE;
340     }
341 }
342
343
344 void
345 DEFUN (gmon_out_write, (filename), const char *filename)
346 {
347   FILE *ofp;
348   struct gmon_hdr ghdr;
349
350   ofp = fopen (filename, FOPEN_WB);
351   if (!ofp)
352     {
353       perror (filename);
354       done (1);
355     }
356
357   if (file_format == FF_AUTO || file_format == FF_MAGIC)
358     {
359       /* write gmon header: */
360
361       memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
362       bfd_put_32 (core_bfd, GMON_VERSION, (bfd_byte *) ghdr.version);
363       if (fwrite (&ghdr, sizeof (ghdr), 1, ofp) != 1)
364         {
365           perror (filename);
366           done (1);
367         }
368
369       /* write execution time histogram if we have one: */
370       if (gmon_input & INPUT_HISTOGRAM)
371         {
372           hist_write_hist (ofp, filename);
373         }
374
375       /* write call graph arcs if we have any: */
376       if (gmon_input & INPUT_CALL_GRAPH)
377         {
378           cg_write_arcs (ofp, filename);
379         }
380
381       /* write basic-block info if we have it: */
382       if (gmon_input & INPUT_BB_COUNTS)
383         {
384           bb_write_blocks (ofp, filename);
385         }
386     }
387   else if (file_format == FF_BSD || file_format == FF_BSD44)
388     {
389       struct raw_arc raw_arc;
390       UNIT raw_bin_count;
391       struct raw_phdr h;
392       int i;
393       Arc *arc;
394       Sym *sym;
395
396       memset (&h, 0, sizeof h);
397       put_vma (core_bfd, s_lowpc, (bfd_byte *) &h.low_pc);
398       put_vma (core_bfd, s_highpc, (bfd_byte *) &h.high_pc);
399       bfd_put_32 (core_bfd,
400                   hist_num_bins * sizeof (UNIT) + sizeof (struct raw_phdr),
401                   (bfd_byte *) &h.ncnt);
402
403       /* Write header.  Use new style BSD format is explicitly
404          specified, or if the profiling rate is non-standard;
405          otherwise, use the old BSD format.  */
406       if (file_format == FF_BSD44
407           || hz != hertz ())
408         {
409           bfd_put_32 (core_bfd, GMONVERSION, (bfd_byte *) &h.version);
410           bfd_put_32 (core_bfd, hz, (bfd_byte *) &h.profrate);
411           if (fwrite (&h, sizeof (struct raw_phdr), 1, ofp) != 1)
412             {
413               perror (filename);
414               done (1);
415             }
416         }
417       else
418         {
419           if (fwrite (&h, sizeof (struct old_raw_phdr), 1, ofp) != 1)
420             {
421               perror (filename);
422               done (1);
423             }
424         }
425
426       /* dump the samples: */
427
428       for (i = 0; i < hist_num_bins; ++i)
429         {
430           bfd_put_16 (core_bfd, hist_sample[i], (bfd_byte *) & raw_bin_count[0]);
431           if (fwrite (&raw_bin_count[0], sizeof (raw_bin_count), 1, ofp) != 1)
432             {
433               perror (filename);
434               done (1);
435             }
436         }
437
438       /* dump the normalized raw arc information: */
439
440       for (sym = symtab.base; sym < symtab.limit; ++sym)
441         {
442           for (arc = sym->cg.children; arc; arc = arc->next_child)
443             {
444               put_vma (core_bfd, arc->parent->addr,
445                        (bfd_byte *) raw_arc.from_pc);
446               put_vma (core_bfd, arc->child->addr,
447                        (bfd_byte *) raw_arc.self_pc);
448               bfd_put_32 (core_bfd, arc->count, (bfd_byte *) raw_arc.count);
449               if (fwrite (&raw_arc, sizeof (raw_arc), 1, ofp) != 1)
450                 {
451                   perror (filename);
452                   done (1);
453                 }
454               DBG (SAMPLEDEBUG,
455                    printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
456                            (unsigned long) arc->parent->addr,
457                            (unsigned long) arc->child->addr, arc->count));
458             }
459         }
460       fclose (ofp);
461     }
462   else
463     {
464       fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
465                whoami, file_format);
466       done (1);
467     }
468 }