* gmon_io.c (gmon_io_read_64, gmon_io_write_64): Define only if
[deliverable/binutils-gdb.git] / gprof / gmon_io.c
1 /* gmon_io.c - Input and output from/to gmon.out files.
2
3 Copyright 2000, 2001, 2002 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 #ifdef BFD_HOST_U_64_BIT
38 static int gmon_io_read_64 PARAMS ((FILE *, BFD_HOST_U_64_BIT *));
39 static int gmon_io_write_64 PARAMS ((FILE *, BFD_HOST_U_64_BIT));
40 #endif
41 static int gmon_read_raw_arc
42 PARAMS ((FILE *, bfd_vma *, bfd_vma *, unsigned long *));
43 static int gmon_write_raw_arc
44 PARAMS ((FILE *, bfd_vma, bfd_vma, unsigned long));
45
46 int gmon_input = 0;
47 int gmon_file_version = 0; /* 0 == old (non-versioned) file format. */
48
49 int
50 gmon_io_read_32 (ifp, valp)
51 FILE *ifp;
52 unsigned int *valp;
53 {
54 char buf[4];
55
56 if (fread (buf, 1, 4, ifp) != 4)
57 return 1;
58 *valp = bfd_get_32 (core_bfd, buf);
59 return 0;
60 }
61
62 #ifdef BFD_HOST_U_64_BIT
63 static int
64 gmon_io_read_64 (ifp, valp)
65 FILE *ifp;
66 BFD_HOST_U_64_BIT *valp;
67 {
68 char buf[8];
69
70 if (fread (buf, 1, 8, ifp) != 8)
71 return 1;
72 *valp = bfd_get_64 (core_bfd, buf);
73 return 0;
74 }
75 #endif
76
77 int
78 gmon_io_read_vma (ifp, valp)
79 FILE *ifp;
80 bfd_vma *valp;
81 {
82 unsigned int val32;
83 #ifdef BFD_HOST_U_64_BIT
84 BFD_HOST_U_64_BIT val64;
85 #endif
86
87 switch (bfd_arch_bits_per_address (core_bfd))
88 {
89 case 32:
90 if (gmon_io_read_32 (ifp, &val32))
91 return 1;
92 *valp = val32;
93 break;
94
95 #ifdef BFD_HOST_U_64_BIT
96 case 64:
97 if (gmon_io_read_64 (ifp, &val64))
98 return 1;
99 *valp = val64;
100 break;
101 #endif
102
103 default:
104 fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
105 whoami, bfd_arch_bits_per_address (core_bfd));
106 done (1);
107 }
108 return 0;
109 }
110
111 int
112 gmon_io_read (ifp, buf, n)
113 FILE *ifp;
114 char *buf;
115 size_t n;
116 {
117 if (fread (buf, 1, n, ifp) != n)
118 return 1;
119 return 0;
120 }
121
122 int
123 gmon_io_write_32 (ofp, val)
124 FILE *ofp;
125 unsigned int val;
126 {
127 char buf[4];
128
129 bfd_put_32 (core_bfd, (bfd_vma) val, buf);
130 if (fwrite (buf, 1, 4, ofp) != 4)
131 return 1;
132 return 0;
133 }
134
135 #ifdef BFD_HOST_U_64_BIT
136 static int
137 gmon_io_write_64 (ofp, val)
138 FILE *ofp;
139 BFD_HOST_U_64_BIT val;
140 {
141 char buf[8];
142
143 bfd_put_64 (core_bfd, (bfd_vma) val, buf);
144 if (fwrite (buf, 1, 8, ofp) != 8)
145 return 1;
146 return 0;
147 }
148 #endif
149
150 int
151 gmon_io_write_vma (ofp, val)
152 FILE *ofp;
153 bfd_vma val;
154 {
155
156 switch (bfd_arch_bits_per_address (core_bfd))
157 {
158 case 32:
159 if (gmon_io_write_32 (ofp, (unsigned int) val))
160 return 1;
161 break;
162
163 #ifdef BFD_HOST_U_64_BIT
164 case 64:
165 if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) val))
166 return 1;
167 break;
168 #endif
169
170 default:
171 fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
172 whoami, bfd_arch_bits_per_address (core_bfd));
173 done (1);
174 }
175 return 0;
176 }
177
178 int
179 gmon_io_write_8 (ofp, val)
180 FILE *ofp;
181 unsigned int val;
182 {
183 char buf[1];
184
185 bfd_put_8 (core_bfd, val, buf);
186 if (fwrite (buf, 1, 1, ofp) != 1)
187 return 1;
188 return 0;
189 }
190
191 int
192 gmon_io_write (ofp, buf, n)
193 FILE *ofp;
194 char *buf;
195 size_t n;
196 {
197 if (fwrite (buf, 1, n, ofp) != n)
198 return 1;
199 return 0;
200 }
201
202 static int
203 gmon_read_raw_arc (ifp, fpc, spc, cnt)
204 FILE *ifp;
205 bfd_vma *fpc;
206 bfd_vma *spc;
207 unsigned long *cnt;
208 {
209 #ifdef BFD_HOST_U_64_BIT
210 BFD_HOST_U_64_BIT cnt64;
211 #endif
212 unsigned int cnt32;
213
214 if (gmon_io_read_vma (ifp, fpc)
215 || gmon_io_read_vma (ifp, spc))
216 return 1;
217
218 switch (bfd_arch_bits_per_address (core_bfd))
219 {
220 case 32:
221 if (gmon_io_read_32 (ifp, &cnt32))
222 return 1;
223 *cnt = cnt32;
224 break;
225
226 #ifdef BFD_HOST_U_64_BIT
227 case 64:
228 if (gmon_io_read_64 (ifp, &cnt64))
229 return 1;
230 *cnt = cnt64;
231 break;
232 #endif
233
234 default:
235 fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
236 whoami, bfd_arch_bits_per_address (core_bfd));
237 done (1);
238 }
239 return 0;
240 }
241
242 static int
243 gmon_write_raw_arc (ofp, fpc, spc, cnt)
244 FILE *ofp;
245 bfd_vma fpc;
246 bfd_vma spc;
247 unsigned long cnt;
248 {
249
250 if (gmon_io_write_vma (ofp, fpc)
251 || gmon_io_write_vma (ofp, spc))
252 return 1;
253
254 switch (bfd_arch_bits_per_address (core_bfd))
255 {
256 case 32:
257 if (gmon_io_write_32 (ofp, (unsigned int) cnt))
258 return 1;
259 break;
260
261 #ifdef BFD_HOST_U_64_BIT
262 case 64:
263 if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) cnt))
264 return 1;
265 break;
266 #endif
267
268 default:
269 fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
270 whoami, bfd_arch_bits_per_address (core_bfd));
271 done (1);
272 }
273 return 0;
274 }
275
276 void
277 gmon_out_read (filename)
278 const char *filename;
279 {
280 FILE *ifp;
281 struct gmon_hdr ghdr;
282 unsigned char tag;
283 int nhist = 0, narcs = 0, nbbs = 0;
284
285 /* Open gmon.out file. */
286 if (strcmp (filename, "-") == 0)
287 {
288 ifp = stdin;
289 #ifdef SET_BINARY
290 SET_BINARY (fileno (stdin));
291 #endif
292 }
293 else
294 {
295 ifp = fopen (filename, FOPEN_RB);
296
297 if (!ifp)
298 {
299 perror (filename);
300 done (1);
301 }
302 }
303
304 if (fread (&ghdr, sizeof (struct gmon_hdr), 1, ifp) != 1)
305 {
306 fprintf (stderr, _("%s: file too short to be a gmon file\n"),
307 filename);
308 done (1);
309 }
310
311 if ((file_format == FF_MAGIC)
312 || (file_format == FF_AUTO && !strncmp (&ghdr.cookie[0], GMON_MAGIC, 4)))
313 {
314 if (file_format == FF_MAGIC && strncmp (&ghdr.cookie[0], GMON_MAGIC, 4))
315 {
316 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
317 whoami, filename);
318 done (1);
319 }
320
321 /* Right magic, so it's probably really a new gmon.out file. */
322 gmon_file_version = bfd_get_32 (core_bfd, (bfd_byte *) ghdr.version);
323
324 if (gmon_file_version != GMON_VERSION && gmon_file_version != 0)
325 {
326 fprintf (stderr,
327 _("%s: file `%s' has unsupported version %d\n"),
328 whoami, filename, gmon_file_version);
329 done (1);
330 }
331
332 /* Read in all the records. */
333 while (fread (&tag, sizeof (tag), 1, ifp) == 1)
334 {
335 switch (tag)
336 {
337 case GMON_TAG_TIME_HIST:
338 ++nhist;
339 gmon_input |= INPUT_HISTOGRAM;
340 hist_read_rec (ifp, filename);
341 break;
342
343 case GMON_TAG_CG_ARC:
344 ++narcs;
345 gmon_input |= INPUT_CALL_GRAPH;
346 cg_read_rec (ifp, filename);
347 break;
348
349 case GMON_TAG_BB_COUNT:
350 ++nbbs;
351 gmon_input |= INPUT_BB_COUNTS;
352 bb_read_rec (ifp, filename);
353 break;
354
355 default:
356 fprintf (stderr,
357 _("%s: %s: found bad tag %d (file corrupted?)\n"),
358 whoami, filename, tag);
359 done (1);
360 }
361 }
362 }
363 else if (file_format == FF_AUTO
364 || file_format == FF_BSD
365 || file_format == FF_BSD44)
366 {
367 struct hdr
368 {
369 bfd_vma low_pc;
370 bfd_vma high_pc;
371 int ncnt;
372 };
373 int i, samp_bytes, header_size = 0;
374 unsigned long count;
375 bfd_vma from_pc, self_pc;
376 static struct hdr h;
377 UNIT raw_bin_count;
378 struct hdr tmp;
379 int version;
380
381 /* Information from a gmon.out file is in two parts: an array of
382 sampling hits within pc ranges, and the arcs. */
383 gmon_input = INPUT_HISTOGRAM | INPUT_CALL_GRAPH;
384
385 /* This fseek() ought to work even on stdin as long as it's
386 not an interactive device (heck, is there anybody who would
387 want to type in a gmon.out at the terminal?). */
388 if (fseek (ifp, 0, SEEK_SET) < 0)
389 {
390 perror (filename);
391 done (1);
392 }
393
394 /* The beginning of the old BSD header and the 4.4BSD header
395 are the same: lowpc, highpc, ncnt */
396 if (gmon_io_read_vma (ifp, &tmp.low_pc)
397 || gmon_io_read_vma (ifp, &tmp.high_pc)
398 || gmon_io_read_32 (ifp, &tmp.ncnt))
399 {
400 bad_gmon_file:
401 fprintf (stderr, _("%s: file too short to be a gmon file\n"),
402 filename);
403 done (1);
404 }
405
406 /* Check to see if this a 4.4BSD-style header. */
407 if (gmon_io_read_32 (ifp, &version))
408 goto bad_gmon_file;
409
410 if (version == GMONVERSION)
411 {
412 int profrate;
413
414 /* 4.4BSD format header. */
415 if (gmon_io_read_32 (ifp, &profrate))
416 goto bad_gmon_file;
417
418 if (!s_highpc)
419 hz = profrate;
420 else if (hz != profrate)
421 {
422 fprintf (stderr,
423 _("%s: profiling rate incompatible with first gmon file\n"),
424 filename);
425 done (1);
426 }
427
428 switch (bfd_arch_bits_per_address (core_bfd))
429 {
430 case 32:
431 header_size = GMON_HDRSIZE_BSD44_32;
432 break;
433
434 case 64:
435 header_size = GMON_HDRSIZE_BSD44_64;
436 break;
437
438 default:
439 fprintf (stderr,
440 _("%s: bits per address has unexpected value of %u\n"),
441 whoami, bfd_arch_bits_per_address (core_bfd));
442 done (1);
443 }
444 }
445 else
446 {
447 /* Old style BSD format. */
448 if (file_format == FF_BSD44)
449 {
450 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
451 whoami, filename);
452 done (1);
453 }
454
455 switch (bfd_arch_bits_per_address (core_bfd))
456 {
457 case 32:
458 header_size = GMON_HDRSIZE_OLDBSD_32;
459 break;
460
461 case 64:
462 header_size = GMON_HDRSIZE_OLDBSD_64;
463 break;
464
465 default:
466 fprintf (stderr,
467 _("%s: bits per address has unexpected value of %u\n"),
468 whoami, bfd_arch_bits_per_address (core_bfd));
469 done (1);
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 (filename)
596 const char *filename;
597 {
598 FILE *ofp;
599 struct gmon_hdr ghdr;
600
601 ofp = fopen (filename, FOPEN_WB);
602 if (!ofp)
603 {
604 perror (filename);
605 done (1);
606 }
607
608 if (file_format == FF_AUTO || file_format == FF_MAGIC)
609 {
610 /* Write gmon header. */
611
612 memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
613 bfd_put_32 (core_bfd, (bfd_vma) GMON_VERSION, (bfd_byte *) ghdr.version);
614
615 if (fwrite (&ghdr, sizeof (ghdr), 1, ofp) != 1)
616 {
617 perror (filename);
618 done (1);
619 }
620
621 /* Write execution time histogram if we have one. */
622 if (gmon_input & INPUT_HISTOGRAM)
623 hist_write_hist (ofp, filename);
624
625 /* Write call graph arcs if we have any. */
626 if (gmon_input & INPUT_CALL_GRAPH)
627 cg_write_arcs (ofp, filename);
628
629 /* Write basic-block info if we have it. */
630 if (gmon_input & INPUT_BB_COUNTS)
631 bb_write_blocks (ofp, filename);
632 }
633 else if (file_format == FF_BSD || file_format == FF_BSD44)
634 {
635 UNIT raw_bin_count;
636 int i, hdrsize;
637 unsigned padsize;
638 char pad[3*4];
639 Arc *arc;
640 Sym *sym;
641
642 memset (pad, 0, sizeof (pad));
643
644 hdrsize = 0;
645 /* Decide how large the header will be. Use the 4.4BSD format
646 header if explicitly specified, or if the profiling rate is
647 non-standard. Otherwise, use the old BSD format. */
648 if (file_format == FF_BSD44
649 || hz != hertz())
650 {
651 padsize = 3*4;
652 switch (bfd_arch_bits_per_address (core_bfd))
653 {
654 case 32:
655 hdrsize = GMON_HDRSIZE_BSD44_32;
656 break;
657
658 case 64:
659 hdrsize = GMON_HDRSIZE_BSD44_64;
660 break;
661
662 default:
663 fprintf (stderr,
664 _("%s: bits per address has unexpected value of %u\n"),
665 whoami, bfd_arch_bits_per_address (core_bfd));
666 done (1);
667 }
668 }
669 else
670 {
671 padsize = 0;
672 switch (bfd_arch_bits_per_address (core_bfd))
673 {
674 case 32:
675 hdrsize = GMON_HDRSIZE_OLDBSD_32;
676 break;
677
678 case 64:
679 hdrsize = GMON_HDRSIZE_OLDBSD_64;
680 /* FIXME: Checking host compiler defines here means that we can't
681 use a cross gprof alpha OSF. */
682 #if defined(__alpha__) && defined (__osf__)
683 padsize = 4;
684 #endif
685 break;
686
687 default:
688 fprintf (stderr,
689 _("%s: bits per address has unexpected value of %u\n"),
690 whoami, bfd_arch_bits_per_address (core_bfd));
691 done (1);
692 }
693 }
694
695 /* Write the parts of the headers that are common to both the
696 old BSD and 4.4BSD formats. */
697 if (gmon_io_write_vma (ofp, s_lowpc)
698 || gmon_io_write_vma (ofp, s_highpc)
699 || gmon_io_write_32 (ofp, hist_num_bins * sizeof (UNIT) + hdrsize))
700 {
701 perror (filename);
702 done (1);
703 }
704
705 /* Write out the 4.4BSD header bits, if that's what we're using. */
706 if (file_format == FF_BSD44
707 || hz != hertz())
708 {
709 if (gmon_io_write_32 (ofp, GMONVERSION)
710 || gmon_io_write_32 (ofp, (unsigned int) hz))
711 {
712 perror (filename);
713 done (1);
714 }
715 }
716
717 /* Now write out any necessary padding after the meaningful
718 header bits. */
719 if (padsize != 0
720 && fwrite (pad, 1, padsize, ofp) != padsize)
721 {
722 perror (filename);
723 done (1);
724 }
725
726 /* Dump the samples. */
727 for (i = 0; i < hist_num_bins; ++i)
728 {
729 bfd_put_16 (core_bfd, (bfd_vma) hist_sample[i],
730 (bfd_byte *) &raw_bin_count[0]);
731 if (fwrite (&raw_bin_count[0], sizeof (raw_bin_count), 1, ofp) != 1)
732 {
733 perror (filename);
734 done (1);
735 }
736 }
737
738 /* Dump the normalized raw arc information. */
739 for (sym = symtab.base; sym < symtab.limit; ++sym)
740 {
741 for (arc = sym->cg.children; arc; arc = arc->next_child)
742 {
743 if (gmon_write_raw_arc (ofp, arc->parent->addr,
744 arc->child->addr, arc->count))
745 {
746 perror (filename);
747 done (1);
748 }
749 DBG (SAMPLEDEBUG,
750 printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
751 (unsigned long) arc->parent->addr,
752 (unsigned long) arc->child->addr, arc->count));
753 }
754 }
755
756 fclose (ofp);
757 }
758 else
759 {
760 fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
761 whoami, file_format);
762 done (1);
763 }
764 }
This page took 0.080567 seconds and 4 git commands to generate.