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