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