2001-11-12 Hans-Peter Nilsson <hp@bitrange.com>
[binutils.git] / gprof / gmon_io.c
blobcff132cb5aaf82fac194ad860b5e21d1dddafe07
1 /* gmon_io.c - Input and output from/to gmon.out files.
3 Copyright 2000, 2001 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 "cg_arcs.h"
23 #include "basic_blocks.h"
24 #include "bfd.h"
25 #include "corefile.h"
26 #include "call_graph.h"
27 #include "gmon_io.h"
28 #include "gmon_out.h"
29 #include "gmon.h" /* Fetch header for old format. */
30 #include "gprof.h"
31 #include "hertz.h"
32 #include "hist.h"
33 #include "libiberty.h"
35 int gmon_input = 0;
36 int gmon_file_version = 0; /* 0 == old (non-versioned) file format. */
38 int
39 DEFUN (gmon_io_read_vma, (ifp, valp), FILE * ifp AND bfd_vma *valp)
41 char buf[8];
42 bfd_vma val;
44 switch (GMON_PTR_SIZE)
46 case 4:
47 if (fread (buf, 1, 4, ifp) != 4)
48 return 1;
49 val = bfd_get_32 (core_bfd, buf);
50 break;
52 case 8:
53 if (fread (buf, 1, 8, ifp) != 8)
54 return 1;
55 val = bfd_get_64 (core_bfd, buf);
56 break;
58 default:
59 fprintf (stderr, _("%s: GMON_PTR_SIZE has unexpected value of %u\n"),
60 whoami, GMON_PTR_SIZE);
61 done (1);
63 *valp = val;
64 return 0;
67 int
68 DEFUN (gmon_io_read_32, (ifp, valp), FILE * ifp AND unsigned int *valp)
70 char buf[4];
72 if (fread (buf, 1, 4, ifp) != 4)
73 return 1;
74 *valp = bfd_get_32 (core_bfd, buf);
75 return 0;
78 int
79 DEFUN (gmon_io_read, (ifp, buf, n), FILE * ifp AND char *buf AND size_t n)
81 if (fread (buf, 1, n, ifp) != n)
82 return 1;
83 return 0;
86 int
87 DEFUN (gmon_io_write_vma, (ofp, val), FILE * ofp AND bfd_vma val)
89 char buf[8];
91 switch (GMON_PTR_SIZE)
93 case 4:
94 bfd_put_32 (core_bfd, val, buf);
95 if (fwrite (buf, 1, 4, ofp) != 4)
96 return 1;
97 break;
99 case 8:
100 bfd_put_64 (core_bfd, val, buf);
101 if (fwrite (buf, 1, 8, ofp) != 8)
102 return 1;
103 break;
105 default:
106 fprintf (stderr, _("%s: GMON_PTR_SIZE has unexpected value of %u\n"),
107 whoami, GMON_PTR_SIZE);
108 done (1);
110 return 0;
114 DEFUN (gmon_io_write_32, (ofp, val), FILE * ofp AND unsigned int val)
116 char buf[4];
118 bfd_put_32 (core_bfd, val, buf);
119 if (fwrite (buf, 1, 4, ofp) != 4)
120 return 1;
121 return 0;
125 DEFUN (gmon_io_write_8, (ofp, val), FILE * ofp AND unsigned char val)
127 char buf[1];
129 bfd_put_8 (core_bfd, val, buf);
130 if (fwrite (buf, 1, 1, ofp) != 1)
131 return 1;
132 return 0;
136 DEFUN (gmon_io_write, (ofp, buf, n), FILE * ofp AND char *buf AND size_t n)
138 if (fwrite (buf, 1, n, ofp) != n)
139 return 1;
140 return 0;
143 /* get_vma and put_vma are for backwards compatibility only */
144 static bfd_vma
145 DEFUN (get_vma, (abfd, addr), bfd * abfd AND bfd_byte * addr)
147 switch (sizeof (char*))
149 case 4:
150 return bfd_get_32 (abfd, addr);
151 case 8:
152 return bfd_get_64 (abfd, addr);
153 default:
154 fprintf (stderr, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
155 whoami, (long) sizeof (char*));
156 done (1);
160 static void
161 DEFUN (put_vma, (abfd, val, addr), bfd * abfd AND bfd_vma val AND bfd_byte * addr)
163 switch (sizeof (char*))
165 case 4:
166 bfd_put_32 (abfd, val, addr);
167 break;
168 case 8:
169 bfd_put_64 (abfd, val, addr);
170 break;
171 default:
172 fprintf (stderr, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
173 whoami, (long) sizeof (char*));
174 done (1);
178 void
179 DEFUN (gmon_out_read, (filename), const char *filename)
181 FILE *ifp;
182 struct gmon_hdr ghdr;
183 unsigned char tag;
184 int nhist = 0, narcs = 0, nbbs = 0;
186 /* Open gmon.out file. */
187 if (strcmp (filename, "-") == 0)
189 ifp = stdin;
190 #ifdef SET_BINARY
191 SET_BINARY (fileno (stdin));
192 #endif
194 else
196 ifp = fopen (filename, FOPEN_RB);
198 if (!ifp)
200 perror (filename);
201 done (1);
205 if (fread (&ghdr, sizeof (struct gmon_hdr), 1, ifp) != 1)
207 fprintf (stderr, _("%s: file too short to be a gmon file\n"),
208 filename);
209 done (1);
212 if ((file_format == FF_MAGIC)
213 || (file_format == FF_AUTO && !strncmp (&ghdr.cookie[0], GMON_MAGIC, 4)))
215 if (file_format == FF_MAGIC && strncmp (&ghdr.cookie[0], GMON_MAGIC, 4))
217 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
218 whoami, filename);
219 done (1);
222 /* Right magic, so it's probably really a new gmon.out file. */
223 gmon_file_version = bfd_get_32 (core_bfd, (bfd_byte *) ghdr.version);
225 if (gmon_file_version != GMON_VERSION && gmon_file_version != 0)
227 fprintf (stderr,
228 _("%s: file `%s' has unsupported version %d\n"),
229 whoami, filename, gmon_file_version);
230 done (1);
233 /* Read in all the records. */
234 while (fread (&tag, sizeof (tag), 1, ifp) == 1)
236 switch (tag)
238 case GMON_TAG_TIME_HIST:
239 ++nhist;
240 gmon_input |= INPUT_HISTOGRAM;
241 hist_read_rec (ifp, filename);
242 break;
244 case GMON_TAG_CG_ARC:
245 ++narcs;
246 gmon_input |= INPUT_CALL_GRAPH;
247 cg_read_rec (ifp, filename);
248 break;
250 case GMON_TAG_BB_COUNT:
251 ++nbbs;
252 gmon_input |= INPUT_BB_COUNTS;
253 bb_read_rec (ifp, filename);
254 break;
256 default:
257 fprintf (stderr,
258 _("%s: %s: found bad tag %d (file corrupted?)\n"),
259 whoami, filename, tag);
260 done (1);
264 else if (file_format == FF_AUTO
265 || file_format == FF_BSD
266 || file_format == FF_BSD44)
268 struct hdr
270 bfd_vma low_pc;
271 bfd_vma high_pc;
272 int ncnt;
274 int i, samp_bytes, header_size;
275 unsigned long count;
276 bfd_vma from_pc, self_pc;
277 struct raw_arc raw_arc;
278 struct raw_phdr raw;
279 static struct hdr h;
280 UNIT raw_bin_count;
281 struct hdr tmp;
283 /* Information from a gmon.out file is in two parts: an array of
284 sampling hits within pc ranges, and the arcs. */
285 gmon_input = INPUT_HISTOGRAM | INPUT_CALL_GRAPH;
287 /* This fseek() ought to work even on stdin as long as it's
288 not an interactive device (heck, is there anybody who would
289 want to type in a gmon.out at the terminal?). */
290 if (fseek (ifp, 0, SEEK_SET) < 0)
292 perror (filename);
293 done (1);
296 if (fread (&raw, 1, sizeof (struct raw_phdr), ifp)
297 != sizeof (struct raw_phdr))
299 fprintf (stderr, _("%s: file too short to be a gmon file\n"),
300 filename);
301 done (1);
304 tmp.low_pc = get_vma (core_bfd, (bfd_byte *) &raw.low_pc[0]);
305 tmp.high_pc = get_vma (core_bfd, (bfd_byte *) &raw.high_pc[0]);
306 tmp.ncnt = bfd_get_32 (core_bfd, (bfd_byte *) &raw.ncnt[0]);
308 if (bfd_get_32 (core_bfd, (bfd_byte *) &raw.version[0])
309 == GMONVERSION)
311 int profrate;
313 /* 4.4BSD format header. */
314 profrate = bfd_get_32 (core_bfd, (bfd_byte *) &raw.profrate[0]);
316 if (!s_highpc)
317 hz = profrate;
318 else if (hz != profrate)
320 fprintf (stderr,
321 _("%s: profiling rate incompatible with first gmon file\n"),
322 filename);
323 done (1);
326 header_size = sizeof (struct raw_phdr);
328 else
330 /* Old style BSD format. */
331 if (file_format == FF_BSD44)
333 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
334 whoami, filename);
335 done (1);
338 if (fseek (ifp, sizeof (struct old_raw_phdr), SEEK_SET) < 0)
340 perror (filename);
341 done (1);
344 header_size = sizeof (struct old_raw_phdr);
347 if (s_highpc && (tmp.low_pc != h.low_pc
348 || tmp.high_pc != h.high_pc || tmp.ncnt != h.ncnt))
350 fprintf (stderr, _("%s: incompatible with first gmon file\n"),
351 filename);
352 done (1);
355 h = tmp;
356 s_lowpc = (bfd_vma) h.low_pc;
357 s_highpc = (bfd_vma) h.high_pc;
358 lowpc = (bfd_vma) h.low_pc / sizeof (UNIT);
359 highpc = (bfd_vma) h.high_pc / sizeof (UNIT);
360 samp_bytes = h.ncnt - header_size;
361 hist_num_bins = samp_bytes / sizeof (UNIT);
363 DBG (SAMPLEDEBUG,
364 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
365 (unsigned long) h.low_pc, (unsigned long) h.high_pc,
366 h.ncnt);
367 printf ("[gmon_out_read] s_lowpc 0x%lx s_highpc 0x%lx\n",
368 (unsigned long) s_lowpc, (unsigned long) s_highpc);
369 printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx\n",
370 (unsigned long) lowpc, (unsigned long) highpc);
371 printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
372 samp_bytes, hist_num_bins));
374 /* Make sure that we have sensible values. */
375 if (samp_bytes < 0 || lowpc > highpc)
377 fprintf (stderr,
378 _("%s: file '%s' does not appear to be in gmon.out format\n"),
379 whoami, filename);
380 done (1);
383 if (hist_num_bins)
384 ++nhist;
386 if (!hist_sample)
388 hist_sample =
389 (int *) xmalloc (hist_num_bins * sizeof (hist_sample[0]));
391 memset (hist_sample, 0, hist_num_bins * sizeof (hist_sample[0]));
394 for (i = 0; i < hist_num_bins; ++i)
396 if (fread (raw_bin_count, sizeof (raw_bin_count), 1, ifp) != 1)
398 fprintf (stderr,
399 _("%s: unexpected EOF after reading %d/%d bins\n"),
400 whoami, --i, hist_num_bins);
401 done (1);
404 hist_sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) raw_bin_count);
407 /* The rest of the file consists of a bunch of
408 <from,self,count> tuples. */
409 while (fread (&raw_arc, sizeof (raw_arc), 1, ifp) == 1)
411 ++narcs;
412 from_pc = get_vma (core_bfd, (bfd_byte *) raw_arc.from_pc);
413 self_pc = get_vma (core_bfd, (bfd_byte *) raw_arc.self_pc);
414 count = bfd_get_32 (core_bfd, (bfd_byte *) raw_arc.count);
416 DBG (SAMPLEDEBUG,
417 printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
418 (unsigned long) from_pc, (unsigned long) self_pc, count));
420 /* Add this arc. */
421 cg_tally (from_pc, self_pc, count);
424 fclose (ifp);
426 if (hz == HZ_WRONG)
428 /* How many ticks per second? If we can't tell, report
429 time in ticks. */
430 hz = hertz ();
432 if (hz == HZ_WRONG)
434 hz = 1;
435 fprintf (stderr, _("time is in ticks, not seconds\n"));
439 else
441 fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
442 whoami, file_format);
443 done (1);
446 if (output_style & STYLE_GMON_INFO)
448 printf (_("File `%s' (version %d) contains:\n"),
449 filename, gmon_file_version);
450 printf (_("\t%d histogram record%s\n"),
451 nhist, nhist == 1 ? "" : "s");
452 printf (_("\t%d call-graph record%s\n"),
453 narcs, narcs == 1 ? "" : "s");
454 printf (_("\t%d basic-block count record%s\n"),
455 nbbs, nbbs == 1 ? "" : "s");
456 first_output = FALSE;
461 void
462 DEFUN (gmon_out_write, (filename), const char *filename)
464 FILE *ofp;
465 struct gmon_hdr ghdr;
467 ofp = fopen (filename, FOPEN_WB);
468 if (!ofp)
470 perror (filename);
471 done (1);
474 if (file_format == FF_AUTO || file_format == FF_MAGIC)
476 /* Write gmon header. */
478 memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
479 bfd_put_32 (core_bfd, GMON_VERSION, (bfd_byte *) ghdr.version);
481 if (fwrite (&ghdr, sizeof (ghdr), 1, ofp) != 1)
483 perror (filename);
484 done (1);
487 /* Write execution time histogram if we have one. */
488 if (gmon_input & INPUT_HISTOGRAM)
489 hist_write_hist (ofp, filename);
491 /* Write call graph arcs if we have any. */
492 if (gmon_input & INPUT_CALL_GRAPH)
493 cg_write_arcs (ofp, filename);
495 /* Write basic-block info if we have it. */
496 if (gmon_input & INPUT_BB_COUNTS)
497 bb_write_blocks (ofp, filename);
499 else if (file_format == FF_BSD || file_format == FF_BSD44)
501 struct raw_arc raw_arc;
502 UNIT raw_bin_count;
503 struct raw_phdr h;
504 int i;
505 Arc *arc;
506 Sym *sym;
508 memset (&h, 0, sizeof h);
509 put_vma (core_bfd, s_lowpc, (bfd_byte *) &h.low_pc);
510 put_vma (core_bfd, s_highpc, (bfd_byte *) &h.high_pc);
511 bfd_put_32 (core_bfd,
512 hist_num_bins * sizeof (UNIT) + sizeof (struct raw_phdr),
513 (bfd_byte *) &h.ncnt);
515 /* Write header. Use new style BSD format is explicitly
516 specified, or if the profiling rate is non-standard;
517 otherwise, use the old BSD format. */
518 if (file_format == FF_BSD44
519 || hz != hertz ())
521 bfd_put_32 (core_bfd, GMONVERSION, (bfd_byte *) &h.version);
522 bfd_put_32 (core_bfd, hz, (bfd_byte *) &h.profrate);
523 if (fwrite (&h, sizeof (struct raw_phdr), 1, ofp) != 1)
525 perror (filename);
526 done (1);
529 else
531 if (fwrite (&h, sizeof (struct old_raw_phdr), 1, ofp) != 1)
533 perror (filename);
534 done (1);
538 /* Dump the samples. */
539 for (i = 0; i < hist_num_bins; ++i)
541 bfd_put_16 (core_bfd, hist_sample[i], (bfd_byte *) & raw_bin_count[0]);
542 if (fwrite (&raw_bin_count[0], sizeof (raw_bin_count), 1, ofp) != 1)
544 perror (filename);
545 done (1);
549 /* Dump the normalized raw arc information. */
550 for (sym = symtab.base; sym < symtab.limit; ++sym)
552 for (arc = sym->cg.children; arc; arc = arc->next_child)
554 put_vma (core_bfd, arc->parent->addr,
555 (bfd_byte *) raw_arc.from_pc);
556 put_vma (core_bfd, arc->child->addr,
557 (bfd_byte *) raw_arc.self_pc);
558 bfd_put_32 (core_bfd, arc->count, (bfd_byte *) raw_arc.count);
559 if (fwrite (&raw_arc, sizeof (raw_arc), 1, ofp) != 1)
561 perror (filename);
562 done (1);
564 DBG (SAMPLEDEBUG,
565 printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
566 (unsigned long) arc->parent->addr,
567 (unsigned long) arc->child->addr, arc->count));
571 fclose (ofp);
573 else
575 fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
576 whoami, file_format);
577 done (1);