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