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