pmcstat_log.c revision 205809
1/*- 2 * Copyright (c) 2005-2007, Joseph Koshy 3 * Copyright (c) 2007 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by A. Joseph Koshy under 7 * sponsorship from the FreeBSD Foundation and Google, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31/* 32 * Transform a hwpmc(4) log into human readable form, and into 33 * gprof(1) compatible profiles. 34 */ 35 36#include <sys/cdefs.h> 37__FBSDID("$FreeBSD: head/usr.sbin/pmcstat/pmcstat_log.c 205809 2010-03-28 22:46:56Z fabient $"); 38 39#include <sys/param.h> 40#include <sys/endian.h> 41#include <sys/gmon.h> 42#include <sys/imgact_aout.h> 43#include <sys/imgact_elf.h> 44#include <sys/mman.h> 45#include <sys/pmc.h> 46#include <sys/queue.h> 47#include <sys/socket.h> 48#include <sys/stat.h> 49#include <sys/wait.h> 50 51#include <netinet/in.h> 52 53#include <assert.h> 54#include <curses.h> 55#include <err.h> 56#include <errno.h> 57#include <fcntl.h> 58#include <gelf.h> 59#include <libgen.h> 60#include <limits.h> 61#include <netdb.h> 62#include <pmc.h> 63#include <pmclog.h> 64#include <sysexits.h> 65#include <stdint.h> 66#include <stdio.h> 67#include <stdlib.h> 68#include <string.h> 69#include <unistd.h> 70 71#include "pmcstat.h" 72#include "pmcstat_log.h" 73#include "pmcstat_top.h" 74 75#define PMCSTAT_ALLOCATE 1 76 77/* 78 * PUBLIC INTERFACES 79 * 80 * pmcstat_initialize_logging() initialize this module, called first 81 * pmcstat_shutdown_logging() orderly shutdown, called last 82 * pmcstat_open_log() open an eventlog for processing 83 * pmcstat_process_log() print/convert an event log 84 * pmcstat_display_log() top mode display for the log 85 * pmcstat_close_log() finish processing an event log 86 * 87 * IMPLEMENTATION NOTES 88 * 89 * We correlate each 'callchain' or 'sample' entry seen in the event 90 * log back to an executable object in the system. Executable objects 91 * include: 92 * - program executables, 93 * - shared libraries loaded by the runtime loader, 94 * - dlopen()'ed objects loaded by the program, 95 * - the runtime loader itself, 96 * - the kernel and kernel modules. 97 * 98 * Each process that we know about is treated as a set of regions that 99 * map to executable objects. Processes are described by 100 * 'pmcstat_process' structures. Executable objects are tracked by 101 * 'pmcstat_image' structures. The kernel and kernel modules are 102 * common to all processes (they reside at the same virtual addresses 103 * for all processes). Individual processes can have their text 104 * segments and shared libraries loaded at process-specific locations. 105 * 106 * A given executable object can be in use by multiple processes 107 * (e.g., libc.so) and loaded at a different address in each. 108 * pmcstat_pcmap structures track per-image mappings. 109 * 110 * The sample log could have samples from multiple PMCs; we 111 * generate one 'gmon.out' profile per PMC. 112 * 113 * IMPLEMENTATION OF GMON OUTPUT 114 * 115 * Each executable object gets one 'gmon.out' profile, per PMC in 116 * use. Creation of 'gmon.out' profiles is done lazily. The 117 * 'gmon.out' profiles generated for a given sampling PMC are 118 * aggregates of all the samples for that particular executable 119 * object. 120 * 121 * IMPLEMENTATION OF SYSTEM-WIDE CALLGRAPH OUTPUT 122 * 123 * Each active pmcid has its own callgraph structure, described by a 124 * 'struct pmcstat_callgraph'. Given a process id and a list of pc 125 * values, we map each pc value to a tuple (image, symbol), where 126 * 'image' denotes an executable object and 'symbol' is the closest 127 * symbol that precedes the pc value. Each pc value in the list is 128 * also given a 'rank' that reflects its depth in the call stack. 129 */ 130 131struct pmcstat_pmcs pmcstat_pmcs = LIST_HEAD_INITIALIZER(pmcstat_pmcs); 132 133/* 134 * All image descriptors are kept in a hash table. 135 */ 136struct pmcstat_image_hash_list pmcstat_image_hash[PMCSTAT_NHASH]; 137 138/* 139 * All process descriptors are kept in a hash table. 140 */ 141struct pmcstat_process_hash_list pmcstat_process_hash[PMCSTAT_NHASH]; 142 143struct pmcstat_stats pmcstat_stats; /* statistics */ 144 145struct pmcstat_process *pmcstat_kernproc; /* kernel 'process' */ 146 147#include "pmcpl_gprof.h" 148#include "pmcpl_callgraph.h" 149#include "pmcpl_annotate.h" 150#include "pmcpl_calltree.h" 151 152struct pmc_plugins { 153 const char *pl_name; /* name */ 154 155 /* configure */ 156 int (*pl_configure)(char *opt); 157 158 /* init and shutdown */ 159 int (*pl_init)(void); 160 void (*pl_shutdown)(FILE *mf); 161 162 /* sample processing */ 163 void (*pl_process)(struct pmcstat_process *pp, 164 struct pmcstat_pmcrecord *pmcr, uint32_t nsamples, 165 uintfptr_t *cc, int usermode, uint32_t cpu); 166 167 /* image */ 168 void (*pl_initimage)(struct pmcstat_image *pi); 169 void (*pl_shutdownimage)(struct pmcstat_image *pi); 170 171 /* pmc */ 172 void (*pl_newpmc)(pmcstat_interned_string ps, 173 struct pmcstat_pmcrecord *pr); 174 175 /* top display */ 176 void (*pl_topdisplay)(void); 177 178 /* top keypress */ 179 int (*pl_topkeypress)(int c, WINDOW *w); 180 181} plugins[] = { 182 { 183 .pl_name = "none", 184 }, 185 { 186 .pl_name = "callgraph", 187 .pl_init = pmcpl_cg_init, 188 .pl_shutdown = pmcpl_cg_shutdown, 189 .pl_process = pmcpl_cg_process, 190 .pl_topkeypress = pmcpl_cg_topkeypress, 191 .pl_topdisplay = pmcpl_cg_topdisplay 192 }, 193 { 194 .pl_name = "gprof", 195 .pl_shutdown = pmcpl_gmon_shutdown, 196 .pl_process = pmcpl_gmon_process, 197 .pl_initimage = pmcpl_gmon_initimage, 198 .pl_shutdownimage = pmcpl_gmon_shutdownimage, 199 .pl_newpmc = pmcpl_gmon_newpmc 200 }, 201 { 202 .pl_name = "annotate", 203 .pl_process = pmcpl_annotate_process 204 }, 205 { 206 .pl_name = "calltree", 207 .pl_configure = pmcpl_ct_configure, 208 .pl_init = pmcpl_ct_init, 209 .pl_shutdown = pmcpl_ct_shutdown, 210 .pl_process = pmcpl_ct_process, 211 .pl_topkeypress = pmcpl_ct_topkeypress, 212 .pl_topdisplay = pmcpl_ct_topdisplay 213 }, 214 { 215 .pl_name = NULL 216 } 217}; 218 219int pmcstat_mergepmc; 220 221int pmcstat_pmcinfilter = 0; /* PMC filter for top mode. */ 222float pmcstat_threshold = 0.5; /* Cost filter for top mode. */ 223 224/* 225 * Prototypes 226 */ 227 228static struct pmcstat_image *pmcstat_image_from_path(pmcstat_interned_string 229 _path, int _iskernelmodule); 230static void pmcstat_image_get_aout_params(struct pmcstat_image *_image); 231static void pmcstat_image_get_elf_params(struct pmcstat_image *_image); 232static void pmcstat_image_link(struct pmcstat_process *_pp, 233 struct pmcstat_image *_i, uintfptr_t _lpc); 234 235static void pmcstat_pmcid_add(pmc_id_t _pmcid, 236 pmcstat_interned_string _name); 237 238static void pmcstat_process_aout_exec(struct pmcstat_process *_pp, 239 struct pmcstat_image *_image, uintfptr_t _entryaddr); 240static void pmcstat_process_elf_exec(struct pmcstat_process *_pp, 241 struct pmcstat_image *_image, uintfptr_t _entryaddr); 242static void pmcstat_process_exec(struct pmcstat_process *_pp, 243 pmcstat_interned_string _path, uintfptr_t _entryaddr); 244static struct pmcstat_process *pmcstat_process_lookup(pid_t _pid, 245 int _allocate); 246static int pmcstat_string_compute_hash(const char *_string); 247static void pmcstat_string_initialize(void); 248static int pmcstat_string_lookup_hash(pmcstat_interned_string _is); 249static void pmcstat_string_shutdown(void); 250 251/* 252 * A simple implementation of interned strings. Each interned string 253 * is assigned a unique address, so that subsequent string compares 254 * can be done by a simple pointer comparision instead of using 255 * strcmp(). This speeds up hash table lookups and saves memory if 256 * duplicate strings are the norm. 257 */ 258struct pmcstat_string { 259 LIST_ENTRY(pmcstat_string) ps_next; /* hash link */ 260 int ps_len; 261 int ps_hash; 262 char *ps_string; 263}; 264 265static LIST_HEAD(,pmcstat_string) pmcstat_string_hash[PMCSTAT_NHASH]; 266 267/* 268 * PMC count. 269 */ 270int pmcstat_npmcs; 271 272/* 273 * PMC Top mode pause state. 274 */ 275int pmcstat_pause; 276 277/* 278 * Compute a 'hash' value for a string. 279 */ 280 281static int 282pmcstat_string_compute_hash(const char *s) 283{ 284 int hash; 285 286 for (hash = 0; *s; s++) 287 hash ^= *s; 288 289 return (hash & PMCSTAT_HASH_MASK); 290} 291 292/* 293 * Intern a copy of string 's', and return a pointer to the 294 * interned structure. 295 */ 296 297pmcstat_interned_string 298pmcstat_string_intern(const char *s) 299{ 300 struct pmcstat_string *ps; 301 const struct pmcstat_string *cps; 302 int hash, len; 303 304 if ((cps = pmcstat_string_lookup(s)) != NULL) 305 return (cps); 306 307 hash = pmcstat_string_compute_hash(s); 308 len = strlen(s); 309 310 if ((ps = malloc(sizeof(*ps))) == NULL) 311 err(EX_OSERR, "ERROR: Could not intern string"); 312 ps->ps_len = len; 313 ps->ps_hash = hash; 314 ps->ps_string = strdup(s); 315 LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next); 316 return ((pmcstat_interned_string) ps); 317} 318 319const char * 320pmcstat_string_unintern(pmcstat_interned_string str) 321{ 322 const char *s; 323 324 s = ((const struct pmcstat_string *) str)->ps_string; 325 return (s); 326} 327 328pmcstat_interned_string 329pmcstat_string_lookup(const char *s) 330{ 331 struct pmcstat_string *ps; 332 int hash, len; 333 334 hash = pmcstat_string_compute_hash(s); 335 len = strlen(s); 336 337 LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next) 338 if (ps->ps_len == len && ps->ps_hash == hash && 339 strcmp(ps->ps_string, s) == 0) 340 return (ps); 341 return (NULL); 342} 343 344static int 345pmcstat_string_lookup_hash(pmcstat_interned_string s) 346{ 347 const struct pmcstat_string *ps; 348 349 ps = (const struct pmcstat_string *) s; 350 return (ps->ps_hash); 351} 352 353/* 354 * Initialize the string interning facility. 355 */ 356 357static void 358pmcstat_string_initialize(void) 359{ 360 int i; 361 362 for (i = 0; i < PMCSTAT_NHASH; i++) 363 LIST_INIT(&pmcstat_string_hash[i]); 364} 365 366/* 367 * Destroy the string table, free'ing up space. 368 */ 369 370static void 371pmcstat_string_shutdown(void) 372{ 373 int i; 374 struct pmcstat_string *ps, *pstmp; 375 376 for (i = 0; i < PMCSTAT_NHASH; i++) 377 LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next, 378 pstmp) { 379 LIST_REMOVE(ps, ps_next); 380 free(ps->ps_string); 381 free(ps); 382 } 383} 384 385/* 386 * Determine whether a given executable image is an A.OUT object, and 387 * if so, fill in its parameters from the text file. 388 * Sets image->pi_type. 389 */ 390 391static void 392pmcstat_image_get_aout_params(struct pmcstat_image *image) 393{ 394 int fd; 395 ssize_t nbytes; 396 struct exec ex; 397 const char *path; 398 char buffer[PATH_MAX]; 399 400 path = pmcstat_string_unintern(image->pi_execpath); 401 assert(path != NULL); 402 403 if (image->pi_iskernelmodule) 404 errx(EX_SOFTWARE, "ERROR: a.out kernel modules are " 405 "unsupported \"%s\"", path); 406 407 (void) snprintf(buffer, sizeof(buffer), "%s%s", 408 args.pa_fsroot, path); 409 410 if ((fd = open(buffer, O_RDONLY, 0)) < 0 || 411 (nbytes = read(fd, &ex, sizeof(ex))) < 0) { 412 warn("WARNING: Cannot determine type of \"%s\"", path); 413 image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE; 414 if (fd != -1) 415 (void) close(fd); 416 return; 417 } 418 419 (void) close(fd); 420 421 if ((unsigned) nbytes != sizeof(ex) || 422 N_BADMAG(ex)) 423 return; 424 425 image->pi_type = PMCSTAT_IMAGE_AOUT; 426 427 /* TODO: the rest of a.out processing */ 428 429 return; 430} 431 432/* 433 * Helper function. 434 */ 435 436static int 437pmcstat_symbol_compare(const void *a, const void *b) 438{ 439 const struct pmcstat_symbol *sym1, *sym2; 440 441 sym1 = (const struct pmcstat_symbol *) a; 442 sym2 = (const struct pmcstat_symbol *) b; 443 444 if (sym1->ps_end <= sym2->ps_start) 445 return (-1); 446 if (sym1->ps_start >= sym2->ps_end) 447 return (1); 448 return (0); 449} 450 451/* 452 * Map an address to a symbol in an image. 453 */ 454 455struct pmcstat_symbol * 456pmcstat_symbol_search(struct pmcstat_image *image, uintfptr_t addr) 457{ 458 struct pmcstat_symbol sym; 459 460 if (image->pi_symbols == NULL) 461 return (NULL); 462 463 sym.ps_name = NULL; 464 sym.ps_start = addr; 465 sym.ps_end = addr + 1; 466 467 return (bsearch((void *) &sym, image->pi_symbols, 468 image->pi_symcount, sizeof(struct pmcstat_symbol), 469 pmcstat_symbol_compare)); 470} 471 472/* 473 * Add the list of symbols in the given section to the list associated 474 * with the object. 475 */ 476static void 477pmcstat_image_add_symbols(struct pmcstat_image *image, Elf *e, 478 Elf_Scn *scn, GElf_Shdr *sh) 479{ 480 int firsttime; 481 size_t n, newsyms, nshsyms, nfuncsyms; 482 struct pmcstat_symbol *symptr; 483 char *fnname; 484 GElf_Sym sym; 485 Elf_Data *data; 486 487 if ((data = elf_getdata(scn, NULL)) == NULL) 488 return; 489 490 /* 491 * Determine the number of functions named in this 492 * section. 493 */ 494 495 nshsyms = sh->sh_size / sh->sh_entsize; 496 for (n = nfuncsyms = 0; n < nshsyms; n++) { 497 if (gelf_getsym(data, (int) n, &sym) != &sym) 498 return; 499 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC) 500 nfuncsyms++; 501 } 502 503 if (nfuncsyms == 0) 504 return; 505 506 /* 507 * Allocate space for the new entries. 508 */ 509 firsttime = image->pi_symbols == NULL; 510 symptr = realloc(image->pi_symbols, 511 sizeof(*symptr) * (image->pi_symcount + nfuncsyms)); 512 if (symptr == image->pi_symbols) /* realloc() failed. */ 513 return; 514 image->pi_symbols = symptr; 515 516 /* 517 * Append new symbols to the end of the current table. 518 */ 519 symptr += image->pi_symcount; 520 521 for (n = newsyms = 0; n < nshsyms; n++) { 522 if (gelf_getsym(data, (int) n, &sym) != &sym) 523 return; 524 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC) 525 continue; 526 527 if (!firsttime && pmcstat_symbol_search(image, sym.st_value)) 528 continue; /* We've seen this symbol already. */ 529 530 if ((fnname = elf_strptr(e, sh->sh_link, sym.st_name)) 531 == NULL) 532 continue; 533 534 symptr->ps_name = pmcstat_string_intern(fnname); 535 symptr->ps_start = sym.st_value - image->pi_vaddr; 536 symptr->ps_end = symptr->ps_start + sym.st_size; 537 symptr++; 538 539 newsyms++; 540 } 541 542 image->pi_symcount += newsyms; 543 544 assert(newsyms <= nfuncsyms); 545 546 /* 547 * Return space to the system if there were duplicates. 548 */ 549 if (newsyms < nfuncsyms) 550 image->pi_symbols = realloc(image->pi_symbols, 551 sizeof(*symptr) * image->pi_symcount); 552 553 /* 554 * Keep the list of symbols sorted. 555 */ 556 qsort(image->pi_symbols, image->pi_symcount, sizeof(*symptr), 557 pmcstat_symbol_compare); 558 559 /* 560 * Deal with function symbols that have a size of 'zero' by 561 * making them extend to the next higher address. These 562 * symbols are usually defined in assembly code. 563 */ 564 for (symptr = image->pi_symbols; 565 symptr < image->pi_symbols + (image->pi_symcount - 1); 566 symptr++) 567 if (symptr->ps_start == symptr->ps_end) 568 symptr->ps_end = (symptr+1)->ps_start; 569} 570 571/* 572 * Examine an ELF file to determine the size of its text segment. 573 * Sets image->pi_type if anything conclusive can be determined about 574 * this image. 575 */ 576 577static void 578pmcstat_image_get_elf_params(struct pmcstat_image *image) 579{ 580 int fd; 581 size_t i, nph, nsh; 582 const char *path, *elfbase; 583 char *p, *endp; 584 uintfptr_t minva, maxva; 585 Elf *e; 586 Elf_Scn *scn; 587 GElf_Ehdr eh; 588 GElf_Phdr ph; 589 GElf_Shdr sh; 590 enum pmcstat_image_type image_type; 591 char buffer[PATH_MAX]; 592 593 assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN); 594 595 image->pi_start = minva = ~(uintfptr_t) 0; 596 image->pi_end = maxva = (uintfptr_t) 0; 597 image->pi_type = image_type = PMCSTAT_IMAGE_INDETERMINABLE; 598 image->pi_isdynamic = 0; 599 image->pi_dynlinkerpath = NULL; 600 image->pi_vaddr = 0; 601 602 path = pmcstat_string_unintern(image->pi_execpath); 603 assert(path != NULL); 604 605 /* 606 * Look for kernel modules under FSROOT/KERNELPATH/NAME, 607 * and user mode executable objects under FSROOT/PATHNAME. 608 */ 609 if (image->pi_iskernelmodule) 610 (void) snprintf(buffer, sizeof(buffer), "%s%s/%s", 611 args.pa_fsroot, args.pa_kernel, path); 612 else 613 (void) snprintf(buffer, sizeof(buffer), "%s%s", 614 args.pa_fsroot, path); 615 616 e = NULL; 617 if ((fd = open(buffer, O_RDONLY, 0)) < 0 || 618 (e = elf_begin(fd, ELF_C_READ, NULL)) == NULL || 619 (elf_kind(e) != ELF_K_ELF)) { 620 warnx("WARNING: Cannot determine the type of \"%s\".", 621 buffer); 622 goto done; 623 } 624 625 if (gelf_getehdr(e, &eh) != &eh) { 626 warnx("WARNING: Cannot retrieve the ELF Header for " 627 "\"%s\": %s.", buffer, elf_errmsg(-1)); 628 goto done; 629 } 630 631 if (eh.e_type != ET_EXEC && eh.e_type != ET_DYN && 632 !(image->pi_iskernelmodule && eh.e_type == ET_REL)) { 633 warnx("WARNING: \"%s\" is of an unsupported ELF type.", 634 buffer); 635 goto done; 636 } 637 638 image_type = eh.e_ident[EI_CLASS] == ELFCLASS32 ? 639 PMCSTAT_IMAGE_ELF32 : PMCSTAT_IMAGE_ELF64; 640 641 /* 642 * Determine the virtual address where an executable would be 643 * loaded. Additionally, for dynamically linked executables, 644 * save the pathname to the runtime linker. 645 */ 646 if (eh.e_type == ET_EXEC) { 647 if (elf_getphnum(e, &nph) == 0) { 648 warnx("WARNING: Could not determine the number of " 649 "program headers in \"%s\": %s.", buffer, 650 elf_errmsg(-1)); 651 goto done; 652 } 653 for (i = 0; i < eh.e_phnum; i++) { 654 if (gelf_getphdr(e, i, &ph) != &ph) { 655 warnx("WARNING: Retrieval of PHDR entry #%ju " 656 "in \"%s\" failed: %s.", (uintmax_t) i, 657 buffer, elf_errmsg(-1)); 658 goto done; 659 } 660 switch (ph.p_type) { 661 case PT_DYNAMIC: 662 image->pi_isdynamic = 1; 663 break; 664 case PT_INTERP: 665 if ((elfbase = elf_rawfile(e, NULL)) == NULL) { 666 warnx("WARNING: Cannot retrieve the " 667 "interpreter for \"%s\": %s.", 668 buffer, elf_errmsg(-1)); 669 goto done; 670 } 671 image->pi_dynlinkerpath = 672 pmcstat_string_intern(elfbase + 673 ph.p_offset); 674 break; 675 case PT_LOAD: 676 if (ph.p_offset == 0) 677 image->pi_vaddr = ph.p_vaddr; 678 break; 679 } 680 } 681 } 682 683 /* 684 * Get the min and max VA associated with this ELF object. 685 */ 686 if (elf_getshnum(e, &nsh) == 0) { 687 warnx("WARNING: Could not determine the number of sections " 688 "for \"%s\": %s.", buffer, elf_errmsg(-1)); 689 goto done; 690 } 691 692 for (i = 0; i < nsh; i++) { 693 if ((scn = elf_getscn(e, i)) == NULL || 694 gelf_getshdr(scn, &sh) != &sh) { 695 warnx("WARNING: Could not retrieve section header " 696 "#%ju in \"%s\": %s.", (uintmax_t) i, buffer, 697 elf_errmsg(-1)); 698 goto done; 699 } 700 if (sh.sh_flags & SHF_EXECINSTR) { 701 minva = min(minva, sh.sh_addr); 702 maxva = max(maxva, sh.sh_addr + sh.sh_size); 703 } 704 if (sh.sh_type == SHT_SYMTAB || sh.sh_type == SHT_DYNSYM) 705 pmcstat_image_add_symbols(image, e, scn, &sh); 706 } 707 708 image->pi_start = minva; 709 image->pi_end = maxva; 710 image->pi_type = image_type; 711 image->pi_fullpath = pmcstat_string_intern(buffer); 712 713 /* Build display name 714 */ 715 endp = buffer; 716 for (p = buffer; *p; p++) 717 if (*p == '/') 718 endp = p+1; 719 image->pi_name = pmcstat_string_intern(endp); 720 721 done: 722 (void) elf_end(e); 723 if (fd >= 0) 724 (void) close(fd); 725 return; 726} 727 728/* 729 * Given an image descriptor, determine whether it is an ELF, or AOUT. 730 * If no handler claims the image, set its type to 'INDETERMINABLE'. 731 */ 732 733void 734pmcstat_image_determine_type(struct pmcstat_image *image) 735{ 736 assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN); 737 738 /* Try each kind of handler in turn */ 739 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 740 pmcstat_image_get_elf_params(image); 741 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 742 pmcstat_image_get_aout_params(image); 743 744 /* 745 * Otherwise, remember that we tried to determine 746 * the object's type and had failed. 747 */ 748 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 749 image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE; 750} 751 752/* 753 * Locate an image descriptor given an interned path, adding a fresh 754 * descriptor to the cache if necessary. This function also finds a 755 * suitable name for this image's sample file. 756 * 757 * We defer filling in the file format specific parts of the image 758 * structure till the time we actually see a sample that would fall 759 * into this image. 760 */ 761 762static struct pmcstat_image * 763pmcstat_image_from_path(pmcstat_interned_string internedpath, 764 int iskernelmodule) 765{ 766 int hash; 767 struct pmcstat_image *pi; 768 769 hash = pmcstat_string_lookup_hash(internedpath); 770 771 /* First, look for an existing entry. */ 772 LIST_FOREACH(pi, &pmcstat_image_hash[hash], pi_next) 773 if (pi->pi_execpath == internedpath && 774 pi->pi_iskernelmodule == iskernelmodule) 775 return (pi); 776 777 /* 778 * Allocate a new entry and place it at the head of the hash 779 * and LRU lists. 780 */ 781 pi = malloc(sizeof(*pi)); 782 if (pi == NULL) 783 return (NULL); 784 785 pi->pi_type = PMCSTAT_IMAGE_UNKNOWN; 786 pi->pi_execpath = internedpath; 787 pi->pi_start = ~0; 788 pi->pi_end = 0; 789 pi->pi_entry = 0; 790 pi->pi_vaddr = 0; 791 pi->pi_isdynamic = 0; 792 pi->pi_iskernelmodule = iskernelmodule; 793 pi->pi_dynlinkerpath = NULL; 794 pi->pi_symbols = NULL; 795 pi->pi_symcount = 0; 796 pi->pi_addr2line = NULL; 797 798 if (plugins[args.pa_pplugin].pl_initimage != NULL) 799 plugins[args.pa_pplugin].pl_initimage(pi); 800 if (plugins[args.pa_plugin].pl_initimage != NULL) 801 plugins[args.pa_plugin].pl_initimage(pi); 802 803 LIST_INSERT_HEAD(&pmcstat_image_hash[hash], pi, pi_next); 804 805 return (pi); 806} 807 808/* 809 * Record the fact that PC values from 'start' to 'end' come from 810 * image 'image'. 811 */ 812 813static void 814pmcstat_image_link(struct pmcstat_process *pp, struct pmcstat_image *image, 815 uintfptr_t start) 816{ 817 struct pmcstat_pcmap *pcm, *pcmnew; 818 uintfptr_t offset; 819 820 assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN && 821 image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE); 822 823 if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL) 824 err(EX_OSERR, "ERROR: Cannot create a map entry"); 825 826 /* 827 * Adjust the map entry to only cover the text portion 828 * of the object. 829 */ 830 831 offset = start - image->pi_vaddr; 832 pcmnew->ppm_lowpc = image->pi_start + offset; 833 pcmnew->ppm_highpc = image->pi_end + offset; 834 pcmnew->ppm_image = image; 835 836 assert(pcmnew->ppm_lowpc < pcmnew->ppm_highpc); 837 838 /* Overlapped mmap()'s are assumed to never occur. */ 839 TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next) 840 if (pcm->ppm_lowpc >= pcmnew->ppm_highpc) 841 break; 842 843 if (pcm == NULL) 844 TAILQ_INSERT_TAIL(&pp->pp_map, pcmnew, ppm_next); 845 else 846 TAILQ_INSERT_BEFORE(pcm, pcmnew, ppm_next); 847} 848 849/* 850 * Unmap images in the range [start..end) associated with process 851 * 'pp'. 852 */ 853 854static void 855pmcstat_image_unmap(struct pmcstat_process *pp, uintfptr_t start, 856 uintfptr_t end) 857{ 858 struct pmcstat_pcmap *pcm, *pcmtmp, *pcmnew; 859 860 assert(pp != NULL); 861 assert(start < end); 862 863 /* 864 * Cases: 865 * - we could have the range completely in the middle of an 866 * existing pcmap; in this case we have to split the pcmap 867 * structure into two (i.e., generate a 'hole'). 868 * - we could have the range covering multiple pcmaps; these 869 * will have to be removed. 870 * - we could have either 'start' or 'end' falling in the 871 * middle of a pcmap; in this case shorten the entry. 872 */ 873 TAILQ_FOREACH_SAFE(pcm, &pp->pp_map, ppm_next, pcmtmp) { 874 assert(pcm->ppm_lowpc < pcm->ppm_highpc); 875 if (pcm->ppm_highpc <= start) 876 continue; 877 if (pcm->ppm_lowpc >= end) 878 return; 879 if (pcm->ppm_lowpc >= start && pcm->ppm_highpc <= end) { 880 /* 881 * The current pcmap is completely inside the 882 * unmapped range: remove it entirely. 883 */ 884 TAILQ_REMOVE(&pp->pp_map, pcm, ppm_next); 885 free(pcm); 886 } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc > end) { 887 /* 888 * Split this pcmap into two; curtail the 889 * current map to end at [start-1], and start 890 * the new one at [end]. 891 */ 892 if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL) 893 err(EX_OSERR, "ERROR: Cannot split a map " 894 "entry"); 895 896 pcmnew->ppm_image = pcm->ppm_image; 897 898 pcmnew->ppm_lowpc = end; 899 pcmnew->ppm_highpc = pcm->ppm_highpc; 900 901 pcm->ppm_highpc = start; 902 903 TAILQ_INSERT_AFTER(&pp->pp_map, pcm, pcmnew, ppm_next); 904 905 return; 906 } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc <= end) 907 pcm->ppm_highpc = start; 908 else if (pcm->ppm_lowpc >= start && pcm->ppm_highpc > end) 909 pcm->ppm_lowpc = end; 910 else 911 assert(0); 912 } 913} 914 915/* 916 * Resolve file name and line number for the given address. 917 */ 918int 919pmcstat_image_addr2line(struct pmcstat_image *image, uintfptr_t addr, 920 char *sourcefile, size_t sourcefile_len, unsigned *sourceline, 921 char *funcname, size_t funcname_len) 922{ 923 static int addr2line_warn = 0; 924 925 char *sep, cmdline[PATH_MAX], imagepath[PATH_MAX]; 926 int fd; 927 928 if (image->pi_addr2line == NULL) { 929 snprintf(imagepath, sizeof(imagepath), "%s.symbols", 930 pmcstat_string_unintern(image->pi_fullpath)); 931 fd = open(imagepath, O_RDONLY); 932 if (fd < 0) { 933 snprintf(imagepath, sizeof(imagepath), "%s", 934 pmcstat_string_unintern(image->pi_fullpath)); 935 } else 936 close(fd); 937 snprintf(cmdline, sizeof(cmdline), "addr2line -Cfe \"%s\"", 938 imagepath); 939 image->pi_addr2line = popen(cmdline, "r+"); 940 if (image->pi_addr2line == NULL) { 941 if (!addr2line_warn) { 942 addr2line_warn = 1; 943 warnx("WARNING: addr2line is needed" 944 "for source code information."); 945 } 946 return (0); 947 } 948 } 949 950 if (feof(image->pi_addr2line) || ferror(image->pi_addr2line)) { 951 warnx("WARNING: addr2line pipe error"); 952 pclose(image->pi_addr2line); 953 image->pi_addr2line = NULL; 954 return (0); 955 } 956 957 fprintf(image->pi_addr2line, "%p\n", (void *)addr); 958 959 if (fgets(funcname, funcname_len, image->pi_addr2line) == NULL) { 960 warnx("WARNING: addr2line function name read error"); 961 return (0); 962 } 963 sep = strchr(funcname, '\n'); 964 if (sep != NULL) 965 *sep = '\0'; 966 967 if (fgets(sourcefile, sourcefile_len, image->pi_addr2line) == NULL) { 968 warnx("WARNING: addr2line source file read error"); 969 return (0); 970 } 971 sep = strchr(sourcefile, ':'); 972 if (sep == NULL) { 973 warnx("WARNING: addr2line source line separator missing"); 974 return (0); 975 } 976 *sep = '\0'; 977 *sourceline = atoi(sep+1); 978 if (*sourceline == 0) 979 return (0); 980 981 return (1); 982} 983 984/* 985 * Add a {pmcid,name} mapping. 986 */ 987 988static void 989pmcstat_pmcid_add(pmc_id_t pmcid, pmcstat_interned_string ps) 990{ 991 struct pmcstat_pmcrecord *pr, *prm; 992 993 /* Replace an existing name for the PMC. */ 994 prm = NULL; 995 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 996 if (pr->pr_pmcid == pmcid) { 997 pr->pr_pmcname = ps; 998 return; 999 } else if (pr->pr_pmcname == ps) 1000 prm = pr; 1001 1002 /* 1003 * Otherwise, allocate a new descriptor and call the 1004 * plugins hook. 1005 */ 1006 if ((pr = malloc(sizeof(*pr))) == NULL) 1007 err(EX_OSERR, "ERROR: Cannot allocate pmc record"); 1008 1009 pr->pr_pmcid = pmcid; 1010 pr->pr_pmcname = ps; 1011 pr->pr_pmcin = pmcstat_npmcs++; 1012 pr->pr_merge = prm == NULL ? pr : prm; 1013 1014 LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next); 1015 1016 if (plugins[args.pa_pplugin].pl_newpmc != NULL) 1017 plugins[args.pa_pplugin].pl_newpmc(ps, pr); 1018 if (plugins[args.pa_plugin].pl_newpmc != NULL) 1019 plugins[args.pa_plugin].pl_newpmc(ps, pr); 1020} 1021 1022/* 1023 * Given a pmcid in use, find its human-readable name. 1024 */ 1025 1026const char * 1027pmcstat_pmcid_to_name(pmc_id_t pmcid) 1028{ 1029 struct pmcstat_pmcrecord *pr; 1030 1031 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 1032 if (pr->pr_pmcid == pmcid) 1033 return (pmcstat_string_unintern(pr->pr_pmcname)); 1034 1035 err(EX_SOFTWARE, "ERROR: cannot find pmcid"); 1036 return NULL; 1037} 1038 1039/* 1040 * Convert PMC index to name. 1041 */ 1042 1043const char * 1044pmcstat_pmcindex_to_name(int pmcin) 1045{ 1046 struct pmcstat_pmcrecord *pr; 1047 1048 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 1049 if (pr->pr_pmcin == pmcin) 1050 return pmcstat_string_unintern(pr->pr_pmcname); 1051 1052 return NULL; 1053} 1054 1055/* 1056 * Return PMC record with given index. 1057 */ 1058 1059struct pmcstat_pmcrecord * 1060pmcstat_pmcindex_to_pmcr(int pmcin) 1061{ 1062 struct pmcstat_pmcrecord *pr; 1063 1064 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 1065 if (pr->pr_pmcin == pmcin) 1066 return pr; 1067 1068 err(EX_SOFTWARE, "ERROR: invalid pmcindex"); 1069 return NULL; 1070} 1071 1072/* 1073 * Get PMC record by id, apply merge policy. 1074 */ 1075 1076static struct pmcstat_pmcrecord * 1077pmcstat_lookup_pmcid(pmc_id_t pmcid) 1078{ 1079 struct pmcstat_pmcrecord *pr; 1080 1081 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) { 1082 if (pr->pr_pmcid == pmcid) { 1083 if (pmcstat_mergepmc) 1084 return pr->pr_merge; 1085 return pr; 1086 } 1087 } 1088 1089 return NULL; 1090} 1091 1092/* 1093 * Associate an AOUT image with a process. 1094 */ 1095 1096static void 1097pmcstat_process_aout_exec(struct pmcstat_process *pp, 1098 struct pmcstat_image *image, uintfptr_t entryaddr) 1099{ 1100 (void) pp; 1101 (void) image; 1102 (void) entryaddr; 1103 /* TODO Implement a.out handling */ 1104} 1105 1106/* 1107 * Associate an ELF image with a process. 1108 */ 1109 1110static void 1111pmcstat_process_elf_exec(struct pmcstat_process *pp, 1112 struct pmcstat_image *image, uintfptr_t entryaddr) 1113{ 1114 uintmax_t libstart; 1115 struct pmcstat_image *rtldimage; 1116 1117 assert(image->pi_type == PMCSTAT_IMAGE_ELF32 || 1118 image->pi_type == PMCSTAT_IMAGE_ELF64); 1119 1120 /* Create a map entry for the base executable. */ 1121 pmcstat_image_link(pp, image, image->pi_vaddr); 1122 1123 /* 1124 * For dynamically linked executables we need to determine 1125 * where the dynamic linker was mapped to for this process, 1126 * Subsequent executable objects that are mapped in by the 1127 * dynamic linker will be tracked by log events of type 1128 * PMCLOG_TYPE_MAP_IN. 1129 */ 1130 1131 if (image->pi_isdynamic) { 1132 1133 /* 1134 * The runtime loader gets loaded just after the maximum 1135 * possible heap address. Like so: 1136 * 1137 * [ TEXT DATA BSS HEAP -->*RTLD SHLIBS <--STACK] 1138 * ^ ^ 1139 * 0 VM_MAXUSER_ADDRESS 1140 1141 * 1142 * The exact address where the loader gets mapped in 1143 * will vary according to the size of the executable 1144 * and the limits on the size of the process'es data 1145 * segment at the time of exec(). The entry address 1146 * recorded at process exec time corresponds to the 1147 * 'start' address inside the dynamic linker. From 1148 * this we can figure out the address where the 1149 * runtime loader's file object had been mapped to. 1150 */ 1151 rtldimage = pmcstat_image_from_path(image->pi_dynlinkerpath, 0); 1152 if (rtldimage == NULL) { 1153 warnx("WARNING: Cannot find image for \"%s\".", 1154 pmcstat_string_unintern(image->pi_dynlinkerpath)); 1155 pmcstat_stats.ps_exec_errors++; 1156 return; 1157 } 1158 1159 if (rtldimage->pi_type == PMCSTAT_IMAGE_UNKNOWN) 1160 pmcstat_image_get_elf_params(rtldimage); 1161 1162 if (rtldimage->pi_type != PMCSTAT_IMAGE_ELF32 && 1163 rtldimage->pi_type != PMCSTAT_IMAGE_ELF64) { 1164 warnx("WARNING: rtld not an ELF object \"%s\".", 1165 pmcstat_string_unintern(image->pi_dynlinkerpath)); 1166 return; 1167 } 1168 1169 libstart = entryaddr - rtldimage->pi_entry; 1170 pmcstat_image_link(pp, rtldimage, libstart); 1171 } 1172} 1173 1174/* 1175 * Find the process descriptor corresponding to a PID. If 'allocate' 1176 * is zero, we return a NULL if a pid descriptor could not be found or 1177 * a process descriptor process. If 'allocate' is non-zero, then we 1178 * will attempt to allocate a fresh process descriptor. Zombie 1179 * process descriptors are only removed if a fresh allocation for the 1180 * same PID is requested. 1181 */ 1182 1183static struct pmcstat_process * 1184pmcstat_process_lookup(pid_t pid, int allocate) 1185{ 1186 uint32_t hash; 1187 struct pmcstat_pcmap *ppm, *ppmtmp; 1188 struct pmcstat_process *pp, *pptmp; 1189 1190 hash = (uint32_t) pid & PMCSTAT_HASH_MASK; /* simplicity wins */ 1191 1192 LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[hash], pp_next, pptmp) 1193 if (pp->pp_pid == pid) { 1194 /* Found a descriptor, check and process zombies */ 1195 if (allocate && pp->pp_isactive == 0) { 1196 /* remove maps */ 1197 TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, 1198 ppmtmp) { 1199 TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next); 1200 free(ppm); 1201 } 1202 /* remove process entry */ 1203 LIST_REMOVE(pp, pp_next); 1204 free(pp); 1205 break; 1206 } 1207 return (pp); 1208 } 1209 1210 if (!allocate) 1211 return (NULL); 1212 1213 if ((pp = malloc(sizeof(*pp))) == NULL) 1214 err(EX_OSERR, "ERROR: Cannot allocate pid descriptor"); 1215 1216 pp->pp_pid = pid; 1217 pp->pp_isactive = 1; 1218 1219 TAILQ_INIT(&pp->pp_map); 1220 1221 LIST_INSERT_HEAD(&pmcstat_process_hash[hash], pp, pp_next); 1222 return (pp); 1223} 1224 1225/* 1226 * Associate an image and a process. 1227 */ 1228 1229static void 1230pmcstat_process_exec(struct pmcstat_process *pp, 1231 pmcstat_interned_string path, uintfptr_t entryaddr) 1232{ 1233 struct pmcstat_image *image; 1234 1235 if ((image = pmcstat_image_from_path(path, 0)) == NULL) { 1236 pmcstat_stats.ps_exec_errors++; 1237 return; 1238 } 1239 1240 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 1241 pmcstat_image_determine_type(image); 1242 1243 assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN); 1244 1245 switch (image->pi_type) { 1246 case PMCSTAT_IMAGE_ELF32: 1247 case PMCSTAT_IMAGE_ELF64: 1248 pmcstat_stats.ps_exec_elf++; 1249 pmcstat_process_elf_exec(pp, image, entryaddr); 1250 break; 1251 1252 case PMCSTAT_IMAGE_AOUT: 1253 pmcstat_stats.ps_exec_aout++; 1254 pmcstat_process_aout_exec(pp, image, entryaddr); 1255 break; 1256 1257 case PMCSTAT_IMAGE_INDETERMINABLE: 1258 pmcstat_stats.ps_exec_indeterminable++; 1259 break; 1260 1261 default: 1262 err(EX_SOFTWARE, "ERROR: Unsupported executable type for " 1263 "\"%s\"", pmcstat_string_unintern(path)); 1264 } 1265} 1266 1267 1268/* 1269 * Find the map entry associated with process 'p' at PC value 'pc'. 1270 */ 1271 1272struct pmcstat_pcmap * 1273pmcstat_process_find_map(struct pmcstat_process *p, uintfptr_t pc) 1274{ 1275 struct pmcstat_pcmap *ppm; 1276 1277 TAILQ_FOREACH(ppm, &p->pp_map, ppm_next) { 1278 if (pc >= ppm->ppm_lowpc && pc < ppm->ppm_highpc) 1279 return (ppm); 1280 if (pc < ppm->ppm_lowpc) 1281 return (NULL); 1282 } 1283 1284 return (NULL); 1285} 1286 1287/* 1288 * Convert a hwpmc(4) log to profile information. A system-wide 1289 * callgraph is generated if FLAG_DO_CALLGRAPHS is set. gmon.out 1290 * files usable by gprof(1) are created if FLAG_DO_GPROF is set. 1291 */ 1292static int 1293pmcstat_analyze_log(void) 1294{ 1295 uint32_t cpu, cpuflags; 1296 uintfptr_t pc; 1297 pid_t pid; 1298 struct pmcstat_image *image; 1299 struct pmcstat_process *pp, *ppnew; 1300 struct pmcstat_pcmap *ppm, *ppmtmp; 1301 struct pmclog_ev ev; 1302 struct pmcstat_pmcrecord *pmcr; 1303 pmcstat_interned_string image_path; 1304 1305 assert(args.pa_flags & FLAG_DO_ANALYSIS); 1306 1307 if (elf_version(EV_CURRENT) == EV_NONE) 1308 err(EX_UNAVAILABLE, "Elf library intialization failed"); 1309 1310 while (pmclog_read(args.pa_logparser, &ev) == 0) { 1311 assert(ev.pl_state == PMCLOG_OK); 1312 1313 switch (ev.pl_type) { 1314 case PMCLOG_TYPE_INITIALIZE: 1315 if ((ev.pl_u.pl_i.pl_version & 0xFF000000) != 1316 PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0) 1317 warnx("WARNING: Log version 0x%x does not " 1318 "match compiled version 0x%x.", 1319 ev.pl_u.pl_i.pl_version, 1320 PMC_VERSION_MAJOR); 1321 break; 1322 1323 case PMCLOG_TYPE_MAP_IN: 1324 /* 1325 * Introduce an address range mapping for a 1326 * userland process or the kernel (pid == -1). 1327 * 1328 * We always allocate a process descriptor so 1329 * that subsequent samples seen for this 1330 * address range are mapped to the current 1331 * object being mapped in. 1332 */ 1333 pid = ev.pl_u.pl_mi.pl_pid; 1334 if (pid == -1) 1335 pp = pmcstat_kernproc; 1336 else 1337 pp = pmcstat_process_lookup(pid, 1338 PMCSTAT_ALLOCATE); 1339 1340 assert(pp != NULL); 1341 1342 image_path = pmcstat_string_intern(ev.pl_u.pl_mi. 1343 pl_pathname); 1344 image = pmcstat_image_from_path(image_path, pid == -1); 1345 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 1346 pmcstat_image_determine_type(image); 1347 if (image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE) 1348 pmcstat_image_link(pp, image, 1349 ev.pl_u.pl_mi.pl_start); 1350 break; 1351 1352 case PMCLOG_TYPE_MAP_OUT: 1353 /* 1354 * Remove an address map. 1355 */ 1356 pid = ev.pl_u.pl_mo.pl_pid; 1357 if (pid == -1) 1358 pp = pmcstat_kernproc; 1359 else 1360 pp = pmcstat_process_lookup(pid, 0); 1361 1362 if (pp == NULL) /* unknown process */ 1363 break; 1364 1365 pmcstat_image_unmap(pp, ev.pl_u.pl_mo.pl_start, 1366 ev.pl_u.pl_mo.pl_end); 1367 break; 1368 1369 case PMCLOG_TYPE_PCSAMPLE: 1370 /* 1371 * Note: the `PCSAMPLE' log entry is not 1372 * generated by hpwmc(4) after version 2. 1373 */ 1374 1375 /* 1376 * We bring in the gmon file for the image 1377 * currently associated with the PMC & pid 1378 * pair and increment the appropriate entry 1379 * bin inside this. 1380 */ 1381 pmcstat_stats.ps_samples_total++; 1382 1383 pc = ev.pl_u.pl_s.pl_pc; 1384 pp = pmcstat_process_lookup(ev.pl_u.pl_s.pl_pid, 1385 PMCSTAT_ALLOCATE); 1386 1387 /* Get PMC record. */ 1388 pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_s.pl_pmcid); 1389 assert(pmcr != NULL); 1390 1391 /* 1392 * Call the plugins processing 1393 * TODO: move pmcstat_process_find_map inside plugins 1394 */ 1395 1396 if (plugins[args.pa_pplugin].pl_process != NULL) 1397 plugins[args.pa_pplugin].pl_process( 1398 pp, pmcr, 1, &pc, 1399 pmcstat_process_find_map(pp, pc) != NULL, 0); 1400 plugins[args.pa_plugin].pl_process( 1401 pp, pmcr, 1, &pc, 1402 pmcstat_process_find_map(pp, pc) != NULL, 0); 1403 break; 1404 1405 case PMCLOG_TYPE_CALLCHAIN: 1406 pmcstat_stats.ps_samples_total++; 1407 1408 cpuflags = ev.pl_u.pl_cc.pl_cpuflags; 1409 cpu = PMC_CALLCHAIN_CPUFLAGS_TO_CPU(cpuflags); 1410 1411 /* Filter on the CPU id. */ 1412 if ((args.pa_cpumask & (1 << cpu)) == 0) { 1413 pmcstat_stats.ps_samples_skipped++; 1414 break; 1415 } 1416 1417 pp = pmcstat_process_lookup(ev.pl_u.pl_cc.pl_pid, 1418 PMCSTAT_ALLOCATE); 1419 1420 /* Get PMC record. */ 1421 pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_cc.pl_pmcid); 1422 assert(pmcr != NULL); 1423 1424 /* 1425 * Call the plugins processing 1426 */ 1427 1428 if (plugins[args.pa_pplugin].pl_process != NULL) 1429 plugins[args.pa_pplugin].pl_process( 1430 pp, pmcr, 1431 ev.pl_u.pl_cc.pl_npc, 1432 ev.pl_u.pl_cc.pl_pc, 1433 PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags), 1434 cpu); 1435 plugins[args.pa_plugin].pl_process( 1436 pp, pmcr, 1437 ev.pl_u.pl_cc.pl_npc, 1438 ev.pl_u.pl_cc.pl_pc, 1439 PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags), 1440 cpu); 1441 break; 1442 1443 case PMCLOG_TYPE_PMCALLOCATE: 1444 /* 1445 * Record the association pmc id between this 1446 * PMC and its name. 1447 */ 1448 pmcstat_pmcid_add(ev.pl_u.pl_a.pl_pmcid, 1449 pmcstat_string_intern(ev.pl_u.pl_a.pl_evname)); 1450 break; 1451 1452 case PMCLOG_TYPE_PROCEXEC: 1453 1454 /* 1455 * Change the executable image associated with 1456 * a process. 1457 */ 1458 pp = pmcstat_process_lookup(ev.pl_u.pl_x.pl_pid, 1459 PMCSTAT_ALLOCATE); 1460 1461 /* delete the current process map */ 1462 TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) { 1463 TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next); 1464 free(ppm); 1465 } 1466 1467 /* associate this process image */ 1468 image_path = pmcstat_string_intern( 1469 ev.pl_u.pl_x.pl_pathname); 1470 assert(image_path != NULL); 1471 pmcstat_process_exec(pp, image_path, 1472 ev.pl_u.pl_x.pl_entryaddr); 1473 break; 1474 1475 case PMCLOG_TYPE_PROCEXIT: 1476 1477 /* 1478 * Due to the way the log is generated, the 1479 * last few samples corresponding to a process 1480 * may appear in the log after the process 1481 * exit event is recorded. Thus we keep the 1482 * process' descriptor and associated data 1483 * structures around, but mark the process as 1484 * having exited. 1485 */ 1486 pp = pmcstat_process_lookup(ev.pl_u.pl_e.pl_pid, 0); 1487 if (pp == NULL) 1488 break; 1489 pp->pp_isactive = 0; /* mark as a zombie */ 1490 break; 1491 1492 case PMCLOG_TYPE_SYSEXIT: 1493 pp = pmcstat_process_lookup(ev.pl_u.pl_se.pl_pid, 0); 1494 if (pp == NULL) 1495 break; 1496 pp->pp_isactive = 0; /* make a zombie */ 1497 break; 1498 1499 case PMCLOG_TYPE_PROCFORK: 1500 1501 /* 1502 * Allocate a process descriptor for the new 1503 * (child) process. 1504 */ 1505 ppnew = 1506 pmcstat_process_lookup(ev.pl_u.pl_f.pl_newpid, 1507 PMCSTAT_ALLOCATE); 1508 1509 /* 1510 * If we had been tracking the parent, clone 1511 * its address maps. 1512 */ 1513 pp = pmcstat_process_lookup(ev.pl_u.pl_f.pl_oldpid, 0); 1514 if (pp == NULL) 1515 break; 1516 TAILQ_FOREACH(ppm, &pp->pp_map, ppm_next) 1517 pmcstat_image_link(ppnew, ppm->ppm_image, 1518 ppm->ppm_lowpc); 1519 break; 1520 1521 default: /* other types of entries are not relevant */ 1522 break; 1523 } 1524 } 1525 1526 if (ev.pl_state == PMCLOG_EOF) 1527 return (PMCSTAT_FINISHED); 1528 else if (ev.pl_state == PMCLOG_REQUIRE_DATA) 1529 return (PMCSTAT_RUNNING); 1530 1531 err(EX_DATAERR, "ERROR: event parsing failed (record %jd, " 1532 "offset 0x%jx)", (uintmax_t) ev.pl_count + 1, ev.pl_offset); 1533} 1534 1535/* 1536 * Print log entries as text. 1537 */ 1538 1539static int 1540pmcstat_print_log(void) 1541{ 1542 struct pmclog_ev ev; 1543 uint32_t npc; 1544 1545 while (pmclog_read(args.pa_logparser, &ev) == 0) { 1546 assert(ev.pl_state == PMCLOG_OK); 1547 switch (ev.pl_type) { 1548 case PMCLOG_TYPE_CALLCHAIN: 1549 PMCSTAT_PRINT_ENTRY("callchain", 1550 "%d 0x%x %d %d %c", ev.pl_u.pl_cc.pl_pid, 1551 ev.pl_u.pl_cc.pl_pmcid, 1552 PMC_CALLCHAIN_CPUFLAGS_TO_CPU(ev.pl_u.pl_cc. \ 1553 pl_cpuflags), ev.pl_u.pl_cc.pl_npc, 1554 PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(ev.pl_u.pl_cc.\ 1555 pl_cpuflags) ? 'u' : 's'); 1556 for (npc = 0; npc < ev.pl_u.pl_cc.pl_npc; npc++) 1557 PMCSTAT_PRINT_ENTRY("...", "%p", 1558 (void *) ev.pl_u.pl_cc.pl_pc[npc]); 1559 break; 1560 case PMCLOG_TYPE_CLOSELOG: 1561 PMCSTAT_PRINT_ENTRY("closelog",); 1562 break; 1563 case PMCLOG_TYPE_DROPNOTIFY: 1564 PMCSTAT_PRINT_ENTRY("drop",); 1565 break; 1566 case PMCLOG_TYPE_INITIALIZE: 1567 PMCSTAT_PRINT_ENTRY("initlog","0x%x \"%s\"", 1568 ev.pl_u.pl_i.pl_version, 1569 pmc_name_of_cputype(ev.pl_u.pl_i.pl_arch)); 1570 if ((ev.pl_u.pl_i.pl_version & 0xFF000000) != 1571 PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0) 1572 warnx("WARNING: Log version 0x%x != expected " 1573 "version 0x%x.", ev.pl_u.pl_i.pl_version, 1574 PMC_VERSION); 1575 break; 1576 case PMCLOG_TYPE_MAP_IN: 1577 PMCSTAT_PRINT_ENTRY("map-in","%d %p \"%s\"", 1578 ev.pl_u.pl_mi.pl_pid, 1579 (void *) ev.pl_u.pl_mi.pl_start, 1580 ev.pl_u.pl_mi.pl_pathname); 1581 break; 1582 case PMCLOG_TYPE_MAP_OUT: 1583 PMCSTAT_PRINT_ENTRY("map-out","%d %p %p", 1584 ev.pl_u.pl_mo.pl_pid, 1585 (void *) ev.pl_u.pl_mo.pl_start, 1586 (void *) ev.pl_u.pl_mo.pl_end); 1587 break; 1588 case PMCLOG_TYPE_PCSAMPLE: 1589 PMCSTAT_PRINT_ENTRY("sample","0x%x %d %p %c", 1590 ev.pl_u.pl_s.pl_pmcid, 1591 ev.pl_u.pl_s.pl_pid, 1592 (void *) ev.pl_u.pl_s.pl_pc, 1593 ev.pl_u.pl_s.pl_usermode ? 'u' : 's'); 1594 break; 1595 case PMCLOG_TYPE_PMCALLOCATE: 1596 PMCSTAT_PRINT_ENTRY("allocate","0x%x \"%s\" 0x%x", 1597 ev.pl_u.pl_a.pl_pmcid, 1598 ev.pl_u.pl_a.pl_evname, 1599 ev.pl_u.pl_a.pl_flags); 1600 break; 1601 case PMCLOG_TYPE_PMCATTACH: 1602 PMCSTAT_PRINT_ENTRY("attach","0x%x %d \"%s\"", 1603 ev.pl_u.pl_t.pl_pmcid, 1604 ev.pl_u.pl_t.pl_pid, 1605 ev.pl_u.pl_t.pl_pathname); 1606 break; 1607 case PMCLOG_TYPE_PMCDETACH: 1608 PMCSTAT_PRINT_ENTRY("detach","0x%x %d", 1609 ev.pl_u.pl_d.pl_pmcid, 1610 ev.pl_u.pl_d.pl_pid); 1611 break; 1612 case PMCLOG_TYPE_PROCCSW: 1613 PMCSTAT_PRINT_ENTRY("cswval","0x%x %d %jd", 1614 ev.pl_u.pl_c.pl_pmcid, 1615 ev.pl_u.pl_c.pl_pid, 1616 ev.pl_u.pl_c.pl_value); 1617 break; 1618 case PMCLOG_TYPE_PROCEXEC: 1619 PMCSTAT_PRINT_ENTRY("exec","0x%x %d %p \"%s\"", 1620 ev.pl_u.pl_x.pl_pmcid, 1621 ev.pl_u.pl_x.pl_pid, 1622 (void *) ev.pl_u.pl_x.pl_entryaddr, 1623 ev.pl_u.pl_x.pl_pathname); 1624 break; 1625 case PMCLOG_TYPE_PROCEXIT: 1626 PMCSTAT_PRINT_ENTRY("exitval","0x%x %d %jd", 1627 ev.pl_u.pl_e.pl_pmcid, 1628 ev.pl_u.pl_e.pl_pid, 1629 ev.pl_u.pl_e.pl_value); 1630 break; 1631 case PMCLOG_TYPE_PROCFORK: 1632 PMCSTAT_PRINT_ENTRY("fork","%d %d", 1633 ev.pl_u.pl_f.pl_oldpid, 1634 ev.pl_u.pl_f.pl_newpid); 1635 break; 1636 case PMCLOG_TYPE_USERDATA: 1637 PMCSTAT_PRINT_ENTRY("userdata","0x%x", 1638 ev.pl_u.pl_u.pl_userdata); 1639 break; 1640 case PMCLOG_TYPE_SYSEXIT: 1641 PMCSTAT_PRINT_ENTRY("exit","%d", 1642 ev.pl_u.pl_se.pl_pid); 1643 break; 1644 default: 1645 fprintf(args.pa_printfile, "unknown event (type %d).\n", 1646 ev.pl_type); 1647 } 1648 } 1649 1650 if (ev.pl_state == PMCLOG_EOF) 1651 return (PMCSTAT_FINISHED); 1652 else if (ev.pl_state == PMCLOG_REQUIRE_DATA) 1653 return (PMCSTAT_RUNNING); 1654 1655 errx(EX_DATAERR, "ERROR: event parsing failed " 1656 "(record %jd, offset 0x%jx).", 1657 (uintmax_t) ev.pl_count + 1, ev.pl_offset); 1658 /*NOTREACHED*/ 1659} 1660 1661/* 1662 * Public Interfaces. 1663 */ 1664 1665/* 1666 * Close a logfile, after first flushing all in-module queued data. 1667 */ 1668 1669int 1670pmcstat_close_log(void) 1671{ 1672 if (pmc_flush_logfile() < 0) 1673 err(EX_OSERR, "ERROR: logging failed"); 1674 return (args.pa_flags & FLAG_HAS_PIPE ? PMCSTAT_EXITING : 1675 PMCSTAT_FINISHED); 1676} 1677 1678 1679 1680/* 1681 * Open a log file, for reading or writing. 1682 * 1683 * The function returns the fd of a successfully opened log or -1 in 1684 * case of failure. 1685 */ 1686 1687int 1688pmcstat_open_log(const char *path, int mode) 1689{ 1690 int error, fd; 1691 size_t hlen; 1692 const char *p, *errstr; 1693 struct addrinfo hints, *res, *res0; 1694 char hostname[MAXHOSTNAMELEN]; 1695 1696 errstr = NULL; 1697 fd = -1; 1698 1699 /* 1700 * If 'path' is "-" then open one of stdin or stdout depending 1701 * on the value of 'mode'. 1702 * 1703 * If 'path' contains a ':' and does not start with a '/' or '.', 1704 * and is being opened for writing, treat it as a "host:port" 1705 * specification and open a network socket. 1706 * 1707 * Otherwise, treat 'path' as a file name and open that. 1708 */ 1709 if (path[0] == '-' && path[1] == '\0') 1710 fd = (mode == PMCSTAT_OPEN_FOR_READ) ? 0 : 1; 1711 else if (mode == PMCSTAT_OPEN_FOR_WRITE && path[0] != '/' && 1712 path[0] != '.' && strchr(path, ':') != NULL) { 1713 1714 p = strrchr(path, ':'); 1715 hlen = p - path; 1716 if (p == path || hlen >= sizeof(hostname)) { 1717 errstr = strerror(EINVAL); 1718 goto done; 1719 } 1720 1721 assert(hlen < sizeof(hostname)); 1722 (void) strncpy(hostname, path, hlen); 1723 hostname[hlen] = '\0'; 1724 1725 (void) memset(&hints, 0, sizeof(hints)); 1726 hints.ai_family = AF_UNSPEC; 1727 hints.ai_socktype = SOCK_STREAM; 1728 if ((error = getaddrinfo(hostname, p+1, &hints, &res0)) != 0) { 1729 errstr = gai_strerror(error); 1730 goto done; 1731 } 1732 1733 fd = -1; 1734 for (res = res0; res; res = res->ai_next) { 1735 if ((fd = socket(res->ai_family, res->ai_socktype, 1736 res->ai_protocol)) < 0) { 1737 errstr = strerror(errno); 1738 continue; 1739 } 1740 if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) { 1741 errstr = strerror(errno); 1742 (void) close(fd); 1743 fd = -1; 1744 continue; 1745 } 1746 errstr = NULL; 1747 break; 1748 } 1749 freeaddrinfo(res0); 1750 1751 } else if ((fd = open(path, mode == PMCSTAT_OPEN_FOR_READ ? 1752 O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC), 1753 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) 1754 errstr = strerror(errno); 1755 1756 done: 1757 if (errstr) 1758 errx(EX_OSERR, "ERROR: Cannot open \"%s\" for %s: %s.", path, 1759 (mode == PMCSTAT_OPEN_FOR_READ ? "reading" : "writing"), 1760 errstr); 1761 1762 return (fd); 1763} 1764 1765/* 1766 * Process a log file in offline analysis mode. 1767 */ 1768 1769int 1770pmcstat_process_log(void) 1771{ 1772 1773 /* 1774 * If analysis has not been asked for, just print the log to 1775 * the current output file. 1776 */ 1777 if (args.pa_flags & FLAG_DO_PRINT) 1778 return (pmcstat_print_log()); 1779 else 1780 return (pmcstat_analyze_log()); 1781} 1782 1783/* 1784 * Refresh top display. 1785 */ 1786 1787static void 1788pmcstat_refresh_top(void) 1789{ 1790 char pmcname[40]; 1791 const char *s; 1792 1793 /* If in pause mode do not refresh display. */ 1794 if (pmcstat_pause) 1795 return; 1796 1797 /* Wait until PMC pop in the log. */ 1798 s = pmcstat_pmcindex_to_name(pmcstat_pmcinfilter); 1799 if (s == NULL) 1800 return; 1801 1802 /* Format PMC name. */ 1803 if (pmcstat_mergepmc) 1804 snprintf(pmcname, sizeof(pmcname), "[%s]", s); 1805 else 1806 snprintf(pmcname, sizeof(pmcname), "%s.%d", 1807 s, pmcstat_pmcinfilter); 1808 1809 PMCSTAT_PRINTBEGIN(); 1810 PMCSTAT_PRINTW("PMC: %s Samples: %u processed, %u invalid\n\n", 1811 pmcname, 1812 pmcstat_stats.ps_samples_total, 1813 pmcstat_stats.ps_samples_unknown_offset + 1814 pmcstat_stats.ps_samples_indeterminable + 1815 pmcstat_stats.ps_callchain_dubious_frames); 1816 if (plugins[args.pa_plugin].pl_topdisplay != NULL) 1817 plugins[args.pa_plugin].pl_topdisplay(); 1818 PMCSTAT_PRINTEND(); 1819} 1820 1821/* 1822 * Find the next pmc index to display. 1823 */ 1824 1825static void 1826pmcstat_changefilter(void) 1827{ 1828 int pmcin; 1829 struct pmcstat_pmcrecord *pmcr; 1830 1831 /* 1832 * Find the next merge target. 1833 */ 1834 if (pmcstat_mergepmc) { 1835 pmcin = pmcstat_pmcinfilter; 1836 1837 do { 1838 pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter); 1839 if (pmcr == pmcr->pr_merge) 1840 break; 1841 1842 pmcstat_pmcinfilter++; 1843 if (pmcstat_pmcinfilter >= pmcstat_npmcs) 1844 pmcstat_pmcinfilter = 0; 1845 1846 } while (pmcstat_pmcinfilter != pmcin); 1847 } 1848} 1849 1850/* 1851 * Top mode keypress. 1852 */ 1853 1854int 1855pmcstat_keypress_log(void) 1856{ 1857 int c, ret = 0; 1858 WINDOW *w; 1859 1860 w = newwin(1, 0, 1, 0); 1861 c = wgetch(w); 1862 wprintw(w, "Key: %c => ", c); 1863 switch (c) { 1864 case 'c': 1865 wprintw(w, "enter mode 'd' or 'a' => "); 1866 c = wgetch(w); 1867 if (c == 'd') { 1868 args.pa_topmode = PMCSTAT_TOP_DELTA; 1869 wprintw(w, "switching to delta mode"); 1870 } else { 1871 args.pa_topmode = PMCSTAT_TOP_ACCUM; 1872 wprintw(w, "switching to accumulation mode"); 1873 } 1874 break; 1875 case 'm': 1876 pmcstat_mergepmc = !pmcstat_mergepmc; 1877 /* 1878 * Changing merge state require data reset. 1879 */ 1880 if (plugins[args.pa_plugin].pl_shutdown != NULL) 1881 plugins[args.pa_plugin].pl_shutdown(NULL); 1882 bzero(&pmcstat_stats, sizeof(struct pmcstat_stats)); 1883 if (plugins[args.pa_plugin].pl_init != NULL) 1884 plugins[args.pa_plugin].pl_init(); 1885 1886 /* Update filter to be on a merge target. */ 1887 pmcstat_changefilter(); 1888 wprintw(w, "merge PMC %s", pmcstat_mergepmc ? "on" : "off"); 1889 break; 1890 case 'n': 1891 /* Close current plugin. */ 1892 if (plugins[args.pa_plugin].pl_shutdown != NULL) 1893 plugins[args.pa_plugin].pl_shutdown(NULL); 1894 1895 /* Find next top display available. */ 1896 do { 1897 args.pa_plugin++; 1898 if (plugins[args.pa_plugin].pl_name == NULL) 1899 args.pa_plugin = 0; 1900 } while (plugins[args.pa_plugin].pl_topdisplay == NULL); 1901 1902 /* Open new plugin. */ 1903 bzero(&pmcstat_stats, sizeof(struct pmcstat_stats)); 1904 if (plugins[args.pa_plugin].pl_init != NULL) 1905 plugins[args.pa_plugin].pl_init(); 1906 wprintw(w, "switching to plugin %s", 1907 plugins[args.pa_plugin].pl_name); 1908 break; 1909 case 'p': 1910 pmcstat_pmcinfilter++; 1911 if (pmcstat_pmcinfilter >= pmcstat_npmcs) 1912 pmcstat_pmcinfilter = 0; 1913 pmcstat_changefilter(); 1914 wprintw(w, "switching to PMC %s.%d", 1915 pmcstat_pmcindex_to_name(pmcstat_pmcinfilter), 1916 pmcstat_pmcinfilter); 1917 break; 1918 case ' ': 1919 pmcstat_pause = !pmcstat_pause; 1920 if (pmcstat_pause) 1921 wprintw(w, "pause => press space again to continue"); 1922 break; 1923 case 'q': 1924 wprintw(w, "exiting..."); 1925 ret = 1; 1926 default: 1927 if (plugins[args.pa_plugin].pl_topkeypress != NULL) 1928 if (plugins[args.pa_plugin].pl_topkeypress(c, w)) 1929 ret = 1; 1930 } 1931 1932 wrefresh(w); 1933 delwin(w); 1934 return ret; 1935} 1936 1937 1938/* 1939 * Top mode display. 1940 */ 1941 1942void 1943pmcstat_display_log(void) 1944{ 1945 1946 pmcstat_refresh_top(); 1947 1948 /* Reset everythings if delta mode. */ 1949 if (args.pa_topmode == PMCSTAT_TOP_DELTA) { 1950 if (plugins[args.pa_plugin].pl_shutdown != NULL) 1951 plugins[args.pa_plugin].pl_shutdown(NULL); 1952 bzero(&pmcstat_stats, sizeof(struct pmcstat_stats)); 1953 if (plugins[args.pa_plugin].pl_init != NULL) 1954 plugins[args.pa_plugin].pl_init(); 1955 } 1956 1957} 1958 1959/* 1960 * Configure a plugins. 1961 */ 1962 1963void 1964pmcstat_pluginconfigure_log(char *opt) 1965{ 1966 1967 if (strncmp(opt, "threshold=", 10) == 0) { 1968 pmcstat_threshold = atof(opt+10); 1969 } else { 1970 if (plugins[args.pa_plugin].pl_configure != NULL) { 1971 if (!plugins[args.pa_plugin].pl_configure(opt)) 1972 err(EX_USAGE, 1973 "ERROR: unknown option <%s>.", opt); 1974 } 1975 } 1976} 1977 1978/* 1979 * Initialize module. 1980 */ 1981 1982void 1983pmcstat_initialize_logging(void) 1984{ 1985 int i; 1986 1987 /* use a convenient format for 'ldd' output */ 1988 if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1","%o \"%p\" %x\n",1) != 0) 1989 err(EX_OSERR, "ERROR: Cannot setenv"); 1990 1991 /* Initialize hash tables */ 1992 pmcstat_string_initialize(); 1993 for (i = 0; i < PMCSTAT_NHASH; i++) { 1994 LIST_INIT(&pmcstat_image_hash[i]); 1995 LIST_INIT(&pmcstat_process_hash[i]); 1996 } 1997 1998 /* 1999 * Create a fake 'process' entry for the kernel with pid -1. 2000 * hwpmc(4) will subsequently inform us about where the kernel 2001 * and any loaded kernel modules are mapped. 2002 */ 2003 if ((pmcstat_kernproc = pmcstat_process_lookup((pid_t) -1, 2004 PMCSTAT_ALLOCATE)) == NULL) 2005 err(EX_OSERR, "ERROR: Cannot initialize logging"); 2006 2007 /* PMC count. */ 2008 pmcstat_npmcs = 0; 2009 2010 /* Merge PMC with same name. */ 2011 pmcstat_mergepmc = args.pa_mergepmc; 2012 2013 /* 2014 * Initialize plugins 2015 */ 2016 2017 if (plugins[args.pa_pplugin].pl_init != NULL) 2018 plugins[args.pa_pplugin].pl_init(); 2019 if (plugins[args.pa_plugin].pl_init != NULL) 2020 plugins[args.pa_plugin].pl_init(); 2021} 2022 2023/* 2024 * Shutdown module. 2025 */ 2026 2027void 2028pmcstat_shutdown_logging(void) 2029{ 2030 int i; 2031 FILE *mf; 2032 struct pmcstat_image *pi, *pitmp; 2033 struct pmcstat_process *pp, *pptmp; 2034 struct pmcstat_pcmap *ppm, *ppmtmp; 2035 2036 /* determine where to send the map file */ 2037 mf = NULL; 2038 if (args.pa_mapfilename != NULL) 2039 mf = (strcmp(args.pa_mapfilename, "-") == 0) ? 2040 args.pa_printfile : fopen(args.pa_mapfilename, "w"); 2041 2042 if (mf == NULL && args.pa_flags & FLAG_DO_GPROF && 2043 args.pa_verbosity >= 2) 2044 mf = args.pa_printfile; 2045 2046 if (mf) 2047 (void) fprintf(mf, "MAP:\n"); 2048 2049 /* 2050 * Shutdown the plugins 2051 */ 2052 2053 if (plugins[args.pa_plugin].pl_shutdown != NULL) 2054 plugins[args.pa_plugin].pl_shutdown(mf); 2055 if (plugins[args.pa_pplugin].pl_shutdown != NULL) 2056 plugins[args.pa_pplugin].pl_shutdown(mf); 2057 2058 for (i = 0; i < PMCSTAT_NHASH; i++) { 2059 LIST_FOREACH_SAFE(pi, &pmcstat_image_hash[i], pi_next, 2060 pitmp) { 2061 if (plugins[args.pa_plugin].pl_shutdownimage != NULL) 2062 plugins[args.pa_plugin].pl_shutdownimage(pi); 2063 if (plugins[args.pa_pplugin].pl_shutdownimage != NULL) 2064 plugins[args.pa_pplugin].pl_shutdownimage(pi); 2065 2066 free(pi->pi_symbols); 2067 if (pi->pi_addr2line != NULL) 2068 pclose(pi->pi_addr2line); 2069 LIST_REMOVE(pi, pi_next); 2070 free(pi); 2071 } 2072 2073 LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[i], pp_next, 2074 pptmp) { 2075 TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) { 2076 TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next); 2077 free(ppm); 2078 } 2079 LIST_REMOVE(pp, pp_next); 2080 free(pp); 2081 } 2082 } 2083 2084 pmcstat_string_shutdown(); 2085 2086 /* 2087 * Print errors unless -q was specified. Print all statistics 2088 * if verbosity > 1. 2089 */ 2090#define PRINT(N,V) do { \ 2091 if (pmcstat_stats.ps_##V || args.pa_verbosity >= 2) \ 2092 (void) fprintf(args.pa_printfile, " %-40s %d\n",\ 2093 N, pmcstat_stats.ps_##V); \ 2094 } while (0) 2095 2096 if (args.pa_verbosity >= 1 && (args.pa_flags & FLAG_DO_ANALYSIS) && 2097 (args.pa_flags & FLAG_DO_TOP) == 0) { 2098 (void) fprintf(args.pa_printfile, "CONVERSION STATISTICS:\n"); 2099 PRINT("#exec/a.out", exec_aout); 2100 PRINT("#exec/elf", exec_elf); 2101 PRINT("#exec/unknown", exec_indeterminable); 2102 PRINT("#exec handling errors", exec_errors); 2103 PRINT("#samples/total", samples_total); 2104 PRINT("#samples/unclaimed", samples_unknown_offset); 2105 PRINT("#samples/unknown-object", samples_indeterminable); 2106 PRINT("#callchain/dubious-frames", callchain_dubious_frames); 2107 } 2108 2109 if (mf) 2110 (void) fclose(mf); 2111} 2112