1 /* gmon_io.c - Input and output from/to gmon.out files. 2 3 Copyright 2000, 2001 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 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" 34 35 int gmon_input = 0; 36 int gmon_file_version = 0; /* 0 == old (non-versioned) file format. */ 37 38 int 39 DEFUN (gmon_io_read_vma, (ifp, valp), FILE * ifp AND bfd_vma *valp) 40 { 41 char buf[8]; 42 bfd_vma val; 43 44 switch (GMON_PTR_SIZE) 45 { 46 case 4: 47 if (fread (buf, 1, 4, ifp) != 4) 48 return 1; 49 val = bfd_get_32 (core_bfd, buf); 50 break; 51 52 case 8: 53 if (fread (buf, 1, 8, ifp) != 8) 54 return 1; 55 val = bfd_get_64 (core_bfd, buf); 56 break; 57 58 default: 59 fprintf (stderr, _("%s: GMON_PTR_SIZE has unexpected value of %u\n"), 60 whoami, GMON_PTR_SIZE); 61 done (1); 62 } 63 *valp = val; 64 return 0; 65 } 66 67 int 68 DEFUN (gmon_io_read_32, (ifp, valp), FILE * ifp AND unsigned int *valp) 69 { 70 char buf[4]; 71 72 if (fread (buf, 1, 4, ifp) != 4) 73 return 1; 74 *valp = bfd_get_32 (core_bfd, buf); 75 return 0; 76 } 77 78 int 79 DEFUN (gmon_io_read, (ifp, buf, n), FILE * ifp AND char *buf AND size_t n) 80 { 81 if (fread (buf, 1, n, ifp) != n) 82 return 1; 83 return 0; 84 } 85 86 int 87 DEFUN (gmon_io_write_vma, (ofp, val), FILE * ofp AND bfd_vma val) 88 { 89 char buf[8]; 90 91 switch (GMON_PTR_SIZE) 92 { 93 case 4: 94 bfd_put_32 (core_bfd, val, buf); 95 if (fwrite (buf, 1, 4, ofp) != 4) 96 return 1; 97 break; 98 99 case 8: 100 bfd_put_64 (core_bfd, val, buf); 101 if (fwrite (buf, 1, 8, ofp) != 8) 102 return 1; 103 break; 104 105 default: 106 fprintf (stderr, _("%s: GMON_PTR_SIZE has unexpected value of %u\n"), 107 whoami, GMON_PTR_SIZE); 108 done (1); 109 } 110 return 0; 111 } 112 113 int 114 DEFUN (gmon_io_write_32, (ofp, val), FILE * ofp AND unsigned int val) 115 { 116 char buf[4]; 117 118 bfd_put_32 (core_bfd, val, buf); 119 if (fwrite (buf, 1, 4, ofp) != 4) 120 return 1; 121 return 0; 122 } 123 124 int 125 DEFUN (gmon_io_write_8, (ofp, val), FILE * ofp AND unsigned char val) 126 { 127 char buf[1]; 128 129 bfd_put_8 (core_bfd, val, buf); 130 if (fwrite (buf, 1, 1, ofp) != 1) 131 return 1; 132 return 0; 133 } 134 135 int 136 DEFUN (gmon_io_write, (ofp, buf, n), FILE * ofp AND char *buf AND size_t n) 137 { 138 if (fwrite (buf, 1, n, ofp) != n) 139 return 1; 140 return 0; 141 } 142 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) 146 { 147 switch (sizeof (char*)) 148 { 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); 157 } 158 } 159 160 static void 161 DEFUN (put_vma, (abfd, val, addr), bfd * abfd AND bfd_vma val AND bfd_byte * addr) 162 { 163 switch (sizeof (char*)) 164 { 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); 175 } 176 } 177 178 void 179 DEFUN (gmon_out_read, (filename), const char *filename) 180 { 181 FILE *ifp; 182 struct gmon_hdr ghdr; 183 unsigned char tag; 184 int nhist = 0, narcs = 0, nbbs = 0; 185 186 /* Open gmon.out file. */ 187 if (strcmp (filename, "-") == 0) 188 { 189 ifp = stdin; 190 #ifdef SET_BINARY 191 SET_BINARY (fileno (stdin)); 192 #endif 193 } 194 else 195 { 196 ifp = fopen (filename, FOPEN_RB); 197 198 if (!ifp) 199 { 200 perror (filename); 201 done (1); 202 } 203 } 204 205 if (fread (&ghdr, sizeof (struct gmon_hdr), 1, ifp) != 1) 206 { 207 fprintf (stderr, _("%s: file too short to be a gmon file\n"), 208 filename); 209 done (1); 210 } 211 212 if ((file_format == FF_MAGIC) 213 || (file_format == FF_AUTO && !strncmp (&ghdr.cookie[0], GMON_MAGIC, 4))) 214 { 215 if (file_format == FF_MAGIC && strncmp (&ghdr.cookie[0], GMON_MAGIC, 4)) 216 { 217 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"), 218 whoami, filename); 219 done (1); 220 } 221 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); 224 225 if (gmon_file_version != GMON_VERSION && gmon_file_version != 0) 226 { 227 fprintf (stderr, 228 _("%s: file `%s' has unsupported version %d\n"), 229 whoami, filename, gmon_file_version); 230 done (1); 231 } 232 233 /* Read in all the records. */ 234 while (fread (&tag, sizeof (tag), 1, ifp) == 1) 235 { 236 switch (tag) 237 { 238 case GMON_TAG_TIME_HIST: 239 ++nhist; 240 gmon_input |= INPUT_HISTOGRAM; 241 hist_read_rec (ifp, filename); 242 break; 243 244 case GMON_TAG_CG_ARC: 245 ++narcs; 246 gmon_input |= INPUT_CALL_GRAPH; 247 cg_read_rec (ifp, filename); 248 break; 249 250 case GMON_TAG_BB_COUNT: 251 ++nbbs; 252 gmon_input |= INPUT_BB_COUNTS; 253 bb_read_rec (ifp, filename); 254 break; 255 256 default: 257 fprintf (stderr, 258 _("%s: %s: found bad tag %d (file corrupted?)\n"), 259 whoami, filename, tag); 260 done (1); 261 } 262 } 263 } 264 else if (file_format == FF_AUTO 265 || file_format == FF_BSD 266 || file_format == FF_BSD44) 267 { 268 struct hdr 269 { 270 bfd_vma low_pc; 271 bfd_vma high_pc; 272 int ncnt; 273 }; 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; 282 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; 286 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) 291 { 292 perror (filename); 293 done (1); 294 } 295 296 if (fread (&raw, 1, sizeof (struct raw_phdr), ifp) 297 != sizeof (struct raw_phdr)) 298 { 299 fprintf (stderr, _("%s: file too short to be a gmon file\n"), 300 filename); 301 done (1); 302 } 303 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]); 307 308 if (bfd_get_32 (core_bfd, (bfd_byte *) &raw.version[0]) 309 == GMONVERSION) 310 { 311 int profrate; 312 313 /* 4.4BSD format header. */ 314 profrate = bfd_get_32 (core_bfd, (bfd_byte *) &raw.profrate[0]); 315 316 if (!s_highpc) 317 hz = profrate; 318 else if (hz != profrate) 319 { 320 fprintf (stderr, 321 _("%s: profiling rate incompatible with first gmon file\n"), 322 filename); 323 done (1); 324 } 325 326 header_size = sizeof (struct raw_phdr); 327 } 328 else 329 { 330 /* Old style BSD format. */ 331 if (file_format == FF_BSD44) 332 { 333 fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"), 334 whoami, filename); 335 done (1); 336 } 337 338 if (fseek (ifp, sizeof (struct old_raw_phdr), SEEK_SET) < 0) 339 { 340 perror (filename); 341 done (1); 342 } 343 344 header_size = sizeof (struct old_raw_phdr); 345 } 346 347 if (s_highpc && (tmp.low_pc != h.low_pc 348 || tmp.high_pc != h.high_pc || tmp.ncnt != h.ncnt)) 349 { 350 fprintf (stderr, _("%s: incompatible with first gmon file\n"), 351 filename); 352 done (1); 353 } 354 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); 362 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)); 373 374 /* Make sure that we have sensible values. */ 375 if (samp_bytes < 0 || lowpc > highpc) 376 { 377 fprintf (stderr, 378 _("%s: file '%s' does not appear to be in gmon.out format\n"), 379 whoami, filename); 380 done (1); 381 } 382 383 if (hist_num_bins) 384 ++nhist; 385 386 if (!hist_sample) 387 { 388 hist_sample = 389 (int *) xmalloc (hist_num_bins * sizeof (hist_sample[0])); 390 391 memset (hist_sample, 0, hist_num_bins * sizeof (hist_sample[0])); 392 } 393 394 for (i = 0; i < hist_num_bins; ++i) 395 { 396 if (fread (raw_bin_count, sizeof (raw_bin_count), 1, ifp) != 1) 397 { 398 fprintf (stderr, 399 _("%s: unexpected EOF after reading %d/%d bins\n"), 400 whoami, --i, hist_num_bins); 401 done (1); 402 } 403 404 hist_sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) raw_bin_count); 405 } 406 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) 410 { 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); 415 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)); 419 420 /* Add this arc. */ 421 cg_tally (from_pc, self_pc, count); 422 } 423 424 fclose (ifp); 425 426 if (hz == HZ_WRONG) 427 { 428 /* How many ticks per second? If we can't tell, report 429 time in ticks. */ 430 hz = hertz (); 431 432 if (hz == HZ_WRONG) 433 { 434 hz = 1; 435 fprintf (stderr, _("time is in ticks, not seconds\n")); 436 } 437 } 438 } 439 else 440 { 441 fprintf (stderr, _("%s: don't know how to deal with file format %d\n"), 442 whoami, file_format); 443 done (1); 444 } 445 446 if (output_style & STYLE_GMON_INFO) 447 { 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; 457 } 458 } 459 460 461 void 462 DEFUN (gmon_out_write, (filename), const char *filename) 463 { 464 FILE *ofp; 465 struct gmon_hdr ghdr; 466 467 ofp = fopen (filename, FOPEN_WB); 468 if (!ofp) 469 { 470 perror (filename); 471 done (1); 472 } 473 474 if (file_format == FF_AUTO || file_format == FF_MAGIC) 475 { 476 /* Write gmon header. */ 477 478 memcpy (&ghdr.cookie[0], GMON_MAGIC, 4); 479 bfd_put_32 (core_bfd, GMON_VERSION, (bfd_byte *) ghdr.version); 480 481 if (fwrite (&ghdr, sizeof (ghdr), 1, ofp) != 1) 482 { 483 perror (filename); 484 done (1); 485 } 486 487 /* Write execution time histogram if we have one. */ 488 if (gmon_input & INPUT_HISTOGRAM) 489 hist_write_hist (ofp, filename); 490 491 /* Write call graph arcs if we have any. */ 492 if (gmon_input & INPUT_CALL_GRAPH) 493 cg_write_arcs (ofp, filename); 494 495 /* Write basic-block info if we have it. */ 496 if (gmon_input & INPUT_BB_COUNTS) 497 bb_write_blocks (ofp, filename); 498 } 499 else if (file_format == FF_BSD || file_format == FF_BSD44) 500 { 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; 507 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); 514 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 ()) 520 { 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) 524 { 525 perror (filename); 526 done (1); 527 } 528 } 529 else 530 { 531 if (fwrite (&h, sizeof (struct old_raw_phdr), 1, ofp) != 1) 532 { 533 perror (filename); 534 done (1); 535 } 536 } 537 538 /* Dump the samples. */ 539 for (i = 0; i < hist_num_bins; ++i) 540 { 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) 543 { 544 perror (filename); 545 done (1); 546 } 547 } 548 549 /* Dump the normalized raw arc information. */ 550 for (sym = symtab.base; sym < symtab.limit; ++sym) 551 { 552 for (arc = sym->cg.children; arc; arc = arc->next_child) 553 { 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) 560 { 561 perror (filename); 562 done (1); 563 } 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)); 568 } 569 } 570 571 fclose (ofp); 572 } 573 else 574 { 575 fprintf (stderr, _("%s: don't know how to deal with file format %d\n"), 576 whoami, file_format); 577 done (1); 578 } 579 } 580