Lots of changes from David Mosberger-Tang; see ChangeLog and NOTES for details:
[deliverable/binutils-gdb.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 "core.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(bfd_vma)) {
27 case 4: return bfd_get_32(abfd, addr);
28 case 8: return bfd_get_64(abfd, addr);
29 default:
30 fprintf(stderr, "%s: bfd_vma has unexpected size of %ld bytes\n",
31 whoami, (long) sizeof(bfd_vma));
32 done(1);
33 } /* switch */
34 } /* get_vma */
35
36
37 /*
38 * This probably ought to be in libbfd.
39 */
40 void
41 DEFUN(put_vma, (abfd, val, addr), bfd *abfd AND bfd_vma val AND bfd_byte *addr)
42 {
43 switch (sizeof(bfd_vma)) {
44 case 4: bfd_put_32(abfd, val, addr); break;
45 case 8: bfd_put_64(abfd, val, addr); break;
46 default:
47 fprintf(stderr, "%s: bfd_vma has unexpected size of %ld bytes\n",
48 whoami, (long) sizeof(bfd_vma));
49 done(1);
50 } /* switch */
51 } /* put_vma */
52
53
54 void
55 DEFUN(gmon_out_read, (filename), const char *filename)
56 {
57 FILE *ifp;
58 struct gmon_hdr ghdr;
59 unsigned char tag;
60 int nhist = 0, narcs = 0, nbbs = 0;
61
62 /* open gmon.out file: */
63
64 if (strcmp(filename, "-") == 0) {
65 ifp = stdin;
66 } else {
67 ifp = fopen(filename, FOPEN_RB);
68 if (!ifp) {
69 perror(filename);
70 done(1);
71 } /* if */
72 } /* if */
73 if (fread(&ghdr, sizeof(struct gmon_hdr), 1, ifp) != 1) {
74 fprintf(stderr, "%s: file too short to be a gmon file\n",
75 filename);
76 done(1);
77 } /* if */
78
79 if ((file_format == FF_MAGIC) ||
80 (file_format == FF_AUTO && !strncmp(&ghdr.cookie[0], GMON_MAGIC, 4)))
81 {
82 if (file_format == FF_MAGIC && strncmp(&ghdr.cookie[0], GMON_MAGIC, 4))
83 {
84 fprintf(stderr, "%s: file `%s' has bad magic cookie\n",
85 whoami, filename);
86 done(1);
87 } /* if */
88
89 /* right magic, so it's probably really a new gmon.out file */
90
91 gmon_file_version = bfd_get_32(core_bfd, (bfd_byte *) ghdr.version);
92 if (gmon_file_version != GMON_VERSION && gmon_file_version != 0) {
93 fprintf(stderr,
94 "%s: file `%s' has unsupported version %d\n",
95 whoami, filename, gmon_file_version);
96 done(1);
97 } /* if */
98
99 /* read in all the records: */
100 while (fread(&tag, sizeof(tag), 1, ifp) == 1) {
101 switch (tag) {
102 case GMON_TAG_TIME_HIST:
103 ++nhist;
104 gmon_input |= INPUT_HISTOGRAM;
105 hist_read_rec(ifp, filename);
106 break;
107
108 case GMON_TAG_CG_ARC:
109 ++narcs;
110 gmon_input |= INPUT_CALL_GRAPH;
111 cg_read_rec(ifp, filename);
112 break;
113
114 case GMON_TAG_BB_COUNT:
115 ++nbbs;
116 gmon_input |= INPUT_BB_COUNTS;
117 bb_read_rec(ifp, filename);
118 break;
119
120 default:
121 fprintf(stderr,
122 "%s: %s: found bad tag %d (file corrupted?)\n",
123 whoami, filename, tag);
124 done(1);
125 } /* switch */
126 } /* while */
127 } else if (file_format == FF_AUTO || file_format == FF_BSD) {
128 struct hdr {
129 bfd_vma low_pc;
130 bfd_vma high_pc;
131 int ncnt;
132 };
133 int i, samp_bytes, count;
134 bfd_vma from_pc, self_pc;
135 struct raw_arc raw_arc;
136 struct raw_phdr raw;
137 static struct hdr h;
138 UNIT raw_bin_count;
139 struct hdr tmp;
140
141 /*
142 * Information from a gmon.out file is in two parts: an array of
143 * sampling hits within pc ranges, and the arcs.
144 */
145 gmon_input = INPUT_HISTOGRAM | INPUT_CALL_GRAPH;
146
147 /*
148 * This fseek() ought to work even on stdin as long as it's
149 * not an interactive device (heck, is there anybody who would
150 * want to type in a gmon.out at the terminal?).
151 */
152 if (fseek(ifp, 0, SEEK_SET) < 0) {
153 perror(filename);
154 done(1);
155 } /* if */
156 if (fread(&raw, 1, sizeof(struct raw_phdr), ifp)
157 != sizeof(struct raw_phdr))
158 {
159 fprintf(stderr, "%s: file too short to be a gmon file\n",
160 filename);
161 done(1);
162 } /* if */
163 tmp.low_pc = get_vma(core_bfd, (bfd_byte *) &raw.low_pc[0]);
164 tmp.high_pc = get_vma(core_bfd, (bfd_byte *) &raw.high_pc[0]);
165 tmp.ncnt = bfd_get_32(core_bfd, (bfd_byte *) &raw.ncnt[0]);
166 if (s_highpc && (tmp.low_pc != h.low_pc ||
167 tmp.high_pc != h.high_pc || tmp.ncnt != h.ncnt))
168 {
169 fprintf(stderr, "%s: incompatible with first gmon file\n",
170 filename);
171 done(1);
172 } /* if */
173 h = tmp;
174 s_lowpc = (bfd_vma) h.low_pc;
175 s_highpc = (bfd_vma) h.high_pc;
176 lowpc = (bfd_vma) h.low_pc / sizeof(UNIT);
177 highpc = (bfd_vma) h.high_pc / sizeof(UNIT);
178 samp_bytes = h.ncnt - sizeof(struct raw_phdr);
179 hist_num_bins = samp_bytes / sizeof (UNIT);
180 DBG(SAMPLEDEBUG,
181 printf("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
182 h.low_pc, h.high_pc, h.ncnt);
183 printf("[gmon_out_read] s_lowpc 0x%lx s_highpc 0x%lx\n",
184 s_lowpc, s_highpc);
185 printf("[gmon_out_read] lowpc 0x%lx highpc 0x%lx\n",
186 lowpc, highpc);
187 printf("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
188 samp_bytes, hist_num_bins));
189
190 if (hist_num_bins) {
191 ++nhist;
192 } /* if */
193
194 if (!hist_sample) {
195 hist_sample =
196 (int*) xmalloc(hist_num_bins * sizeof(hist_sample[0]));
197 memset(hist_sample, 0, hist_num_bins * sizeof(hist_sample[0]));
198 } /* if */
199
200 for (i = 0; i < hist_num_bins; ++i) {
201 if (fread(raw_bin_count, sizeof(raw_bin_count), 1, ifp) != 1) {
202 fprintf(stderr,
203 "%s: unexpected EOF after reading %d/%d bins\n",
204 whoami, --i, hist_num_bins);
205 done(1);
206 } /* if */
207 hist_sample[i] += bfd_get_16(core_bfd, (bfd_byte*) raw_bin_count);
208 } /* for */
209
210 /*
211 * The rest of the file consists of a bunch of <from,self,count>
212 * tuples:
213 */
214 while (fread(&raw_arc, sizeof(raw_arc), 1, ifp) == 1) {
215 ++narcs;
216 from_pc = get_vma(core_bfd, (bfd_byte *) raw_arc.from_pc);
217 self_pc = get_vma(core_bfd, (bfd_byte *) raw_arc.self_pc);
218 count = bfd_get_32(core_bfd, (bfd_byte *) raw_arc.count);
219 DBG(SAMPLEDEBUG,
220 printf("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %d\n",
221 from_pc, self_pc, count));
222 /* add this arc: */
223 cg_tally(from_pc, self_pc, count);
224 } /* while */
225 fclose(ifp);
226
227 if (hz == HZ_WRONG) {
228 /*
229 * How many ticks per second? If we can't tell, report
230 * time in ticks.
231 */
232 hz = hertz();
233 if (hz == HZ_WRONG) {
234 hz = 1;
235 fprintf(stderr, "time is in ticks, not seconds\n");
236 } /* if */
237 } /* if */
238 } else {
239 fprintf(stderr, "%s: don't know how to deal with file format %d\n",
240 whoami, file_format);
241 done(1);
242 } /* if */
243
244 if (output_style & STYLE_GMON_INFO) {
245 printf("File `%s' (version %d) contains:\n",
246 filename, gmon_file_version);
247 printf("\t%d histogram record%s\n",
248 nhist, nhist == 1 ? "" : "s");
249 printf("\t%d call-graph record%s\n",
250 narcs, narcs == 1 ? "" : "s");
251 printf("\t%d basic-block count record%s\n",
252 nbbs, nbbs == 1 ? "" : "s");
253 first_output = FALSE;
254 } /* if */
255 } /* gmon_out_read */
256
257
258 void
259 DEFUN(gmon_out_write, (filename), const char *filename)
260 {
261 FILE *ofp;
262 struct gmon_hdr ghdr;
263
264 ofp = fopen(filename, FOPEN_WB);
265 if (!ofp) {
266 perror(filename);
267 done(1);
268 } /* if */
269
270 if (file_format == FF_AUTO || file_format == FF_MAGIC) {
271 /* write gmon header: */
272
273 memcpy(&ghdr.cookie[0], GMON_MAGIC, 4);
274 bfd_put_32(core_bfd, GMON_VERSION, (bfd_byte*) ghdr.version);
275 if (fwrite(&ghdr, sizeof(ghdr), 1, ofp) != 1) {
276 perror(filename);
277 done(1);
278 } /* if */
279
280 /* write execution time histogram if we have one: */
281 if (gmon_input & INPUT_HISTOGRAM) {
282 hist_write_hist(ofp, filename);
283 } /* if */
284
285 /* write call graph arcs if we have any: */
286 if (gmon_input & INPUT_CALL_GRAPH) {
287 cg_write_arcs(ofp, filename);
288 } /* if */
289
290 /* write basic-block info if we have it: */
291 if (gmon_input & INPUT_BB_COUNTS) {
292 bb_write_blocks(ofp, filename);
293 } /* if */
294 } else if (file_format == FF_BSD) {
295 struct raw_arc raw_arc;
296 UNIT raw_bin_count;
297 bfd_vma lpc, hpc;
298 int i, ncnt;
299 Arc *arc;
300 Sym *sym;
301
302 put_vma(core_bfd, s_lowpc, (bfd_byte*) &lpc);
303 put_vma(core_bfd, s_highpc, (bfd_byte*) &hpc);
304 bfd_put_32(core_bfd,
305 hist_num_bins * sizeof(UNIT) + sizeof(struct raw_phdr),
306 (bfd_byte*) &ncnt);
307
308 /* write header: */
309 if (fwrite(&lpc, sizeof(lpc), 1, ofp) != 1
310 || fwrite(&hpc, sizeof(hpc), 1, ofp) != 1
311 || fwrite(&ncnt, sizeof(ncnt), 1, ofp) != 1)
312 {
313 perror(filename);
314 done(1);
315 } /* if */
316
317 /* dump the samples: */
318
319 for (i = 0; i < hist_num_bins; ++i) {
320 bfd_put_16(core_bfd, hist_sample[i], (bfd_byte*)&raw_bin_count[0]);
321 if (fwrite(&raw_bin_count[0], sizeof(raw_bin_count), 1, ofp) != 1)
322 {
323 perror(filename);
324 done(1);
325 } /* if */
326 } /* for */
327
328 /* dump the normalized raw arc information: */
329
330 for (sym = symtab.base; sym < symtab.limit; ++sym) {
331 for (arc = sym->cg.children; arc; arc = arc->next_child) {
332 put_vma(core_bfd, arc->parent->addr,
333 (bfd_byte*) raw_arc.from_pc);
334 put_vma(core_bfd, arc->child->addr,
335 (bfd_byte*) raw_arc.self_pc);
336 bfd_put_32(core_bfd, arc->count, (bfd_byte*) raw_arc.count);
337 if (fwrite(&raw_arc, sizeof(raw_arc), 1, ofp) != 1) {
338 perror(filename);
339 done(1);
340 } /* if */
341 DBG(SAMPLEDEBUG,
342 printf("[dumpsum] frompc 0x%lx selfpc 0x%lx count %d\n",
343 arc->parent->addr, arc->child->addr, arc->count));
344 } /* for */
345 } /* for */
346 fclose(ofp);
347 } else {
348 fprintf(stderr, "%s: don't know how to deal with file format %d\n",
349 whoami, file_format);
350 done(1);
351 } /* if */
352 } /* gmon_out_write */
353
354 /*** gmon_out.c ***/
This page took 0.043334 seconds and 5 git commands to generate.