1147708Sjkoshy/*- 2174396Sjkoshy * Copyright (c) 2005-2007, Joseph Koshy 3174396Sjkoshy * Copyright (c) 2007 The FreeBSD Foundation 4147708Sjkoshy * All rights reserved. 5147708Sjkoshy * 6174396Sjkoshy * Portions of this software were developed by A. Joseph Koshy under 7174396Sjkoshy * sponsorship from the FreeBSD Foundation and Google, Inc. 8174396Sjkoshy * 9147708Sjkoshy * Redistribution and use in source and binary forms, with or without 10147708Sjkoshy * modification, are permitted provided that the following conditions 11147708Sjkoshy * are met: 12147708Sjkoshy * 1. Redistributions of source code must retain the above copyright 13147708Sjkoshy * notice, this list of conditions and the following disclaimer. 14147708Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright 15147708Sjkoshy * notice, this list of conditions and the following disclaimer in the 16147708Sjkoshy * documentation and/or other materials provided with the distribution. 17147708Sjkoshy * 18147708Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19147708Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20147708Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21147708Sjkoshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22147708Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23147708Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24147708Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25147708Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26147708Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27147708Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28147708Sjkoshy * SUCH DAMAGE. 29147708Sjkoshy */ 30147708Sjkoshy 31157144Sjkoshy/* 32157144Sjkoshy * Transform a hwpmc(4) log into human readable form, and into 33157144Sjkoshy * gprof(1) compatible profiles. 34157144Sjkoshy */ 35157144Sjkoshy 36147708Sjkoshy#include <sys/cdefs.h> 37147708Sjkoshy__FBSDID("$FreeBSD$"); 38147708Sjkoshy 39147708Sjkoshy#include <sys/param.h> 40147708Sjkoshy#include <sys/endian.h> 41224698Sattilio#include <sys/cpuset.h> 42147708Sjkoshy#include <sys/gmon.h> 43147708Sjkoshy#include <sys/imgact_aout.h> 44147708Sjkoshy#include <sys/imgact_elf.h> 45147708Sjkoshy#include <sys/mman.h> 46147708Sjkoshy#include <sys/pmc.h> 47147708Sjkoshy#include <sys/queue.h> 48157406Sjkoshy#include <sys/socket.h> 49147708Sjkoshy#include <sys/stat.h> 50147708Sjkoshy#include <sys/wait.h> 51147708Sjkoshy 52147708Sjkoshy#include <netinet/in.h> 53147708Sjkoshy 54147708Sjkoshy#include <assert.h> 55203790Sfabient#include <curses.h> 56147708Sjkoshy#include <err.h> 57157406Sjkoshy#include <errno.h> 58147708Sjkoshy#include <fcntl.h> 59174396Sjkoshy#include <gelf.h> 60147708Sjkoshy#include <libgen.h> 61147708Sjkoshy#include <limits.h> 62157406Sjkoshy#include <netdb.h> 63147708Sjkoshy#include <pmc.h> 64147708Sjkoshy#include <pmclog.h> 65147708Sjkoshy#include <sysexits.h> 66147708Sjkoshy#include <stdint.h> 67147708Sjkoshy#include <stdio.h> 68147708Sjkoshy#include <stdlib.h> 69147708Sjkoshy#include <string.h> 70147708Sjkoshy#include <unistd.h> 71147708Sjkoshy 72147708Sjkoshy#include "pmcstat.h" 73203790Sfabient#include "pmcstat_log.h" 74203790Sfabient#include "pmcstat_top.h" 75147708Sjkoshy 76174396Sjkoshy#define PMCSTAT_ALLOCATE 1 77174396Sjkoshy 78147708Sjkoshy/* 79157144Sjkoshy * PUBLIC INTERFACES 80157144Sjkoshy * 81157144Sjkoshy * pmcstat_initialize_logging() initialize this module, called first 82157144Sjkoshy * pmcstat_shutdown_logging() orderly shutdown, called last 83157144Sjkoshy * pmcstat_open_log() open an eventlog for processing 84157144Sjkoshy * pmcstat_process_log() print/convert an event log 85203790Sfabient * pmcstat_display_log() top mode display for the log 86157144Sjkoshy * pmcstat_close_log() finish processing an event log 87157144Sjkoshy * 88174396Sjkoshy * IMPLEMENTATION NOTES 89157144Sjkoshy * 90174396Sjkoshy * We correlate each 'callchain' or 'sample' entry seen in the event 91174396Sjkoshy * log back to an executable object in the system. Executable objects 92174396Sjkoshy * include: 93157144Sjkoshy * - program executables, 94157144Sjkoshy * - shared libraries loaded by the runtime loader, 95157144Sjkoshy * - dlopen()'ed objects loaded by the program, 96157144Sjkoshy * - the runtime loader itself, 97157144Sjkoshy * - the kernel and kernel modules. 98157144Sjkoshy * 99157144Sjkoshy * Each process that we know about is treated as a set of regions that 100157144Sjkoshy * map to executable objects. Processes are described by 101157144Sjkoshy * 'pmcstat_process' structures. Executable objects are tracked by 102157144Sjkoshy * 'pmcstat_image' structures. The kernel and kernel modules are 103157144Sjkoshy * common to all processes (they reside at the same virtual addresses 104157144Sjkoshy * for all processes). Individual processes can have their text 105157144Sjkoshy * segments and shared libraries loaded at process-specific locations. 106157144Sjkoshy * 107157144Sjkoshy * A given executable object can be in use by multiple processes 108157144Sjkoshy * (e.g., libc.so) and loaded at a different address in each. 109157144Sjkoshy * pmcstat_pcmap structures track per-image mappings. 110157144Sjkoshy * 111157144Sjkoshy * The sample log could have samples from multiple PMCs; we 112157144Sjkoshy * generate one 'gmon.out' profile per PMC. 113174396Sjkoshy * 114174396Sjkoshy * IMPLEMENTATION OF GMON OUTPUT 115174396Sjkoshy * 116174396Sjkoshy * Each executable object gets one 'gmon.out' profile, per PMC in 117174396Sjkoshy * use. Creation of 'gmon.out' profiles is done lazily. The 118174396Sjkoshy * 'gmon.out' profiles generated for a given sampling PMC are 119174396Sjkoshy * aggregates of all the samples for that particular executable 120174396Sjkoshy * object. 121174396Sjkoshy * 122174396Sjkoshy * IMPLEMENTATION OF SYSTEM-WIDE CALLGRAPH OUTPUT 123174396Sjkoshy * 124174396Sjkoshy * Each active pmcid has its own callgraph structure, described by a 125174396Sjkoshy * 'struct pmcstat_callgraph'. Given a process id and a list of pc 126174396Sjkoshy * values, we map each pc value to a tuple (image, symbol), where 127174396Sjkoshy * 'image' denotes an executable object and 'symbol' is the closest 128174396Sjkoshy * symbol that precedes the pc value. Each pc value in the list is 129174396Sjkoshy * also given a 'rank' that reflects its depth in the call stack. 130147708Sjkoshy */ 131147708Sjkoshy 132203790Sfabientstruct pmcstat_pmcs pmcstat_pmcs = LIST_HEAD_INITIALIZER(pmcstat_pmcs); 133147708Sjkoshy 134147708Sjkoshy/* 135203790Sfabient * All image descriptors are kept in a hash table. 136147708Sjkoshy */ 137203790Sfabientstruct pmcstat_image_hash_list pmcstat_image_hash[PMCSTAT_NHASH]; 138147708Sjkoshy 139150069Sjkoshy/* 140203790Sfabient * All process descriptors are kept in a hash table. 141150069Sjkoshy */ 142203790Sfabientstruct pmcstat_process_hash_list pmcstat_process_hash[PMCSTAT_NHASH]; 143150069Sjkoshy 144203790Sfabientstruct pmcstat_stats pmcstat_stats; /* statistics */ 145210794Sfabientint ps_samples_period; /* samples count between top refresh. */ 146147708Sjkoshy 147203790Sfabientstruct pmcstat_process *pmcstat_kernproc; /* kernel 'process' */ 148147708Sjkoshy 149203790Sfabient#include "pmcpl_gprof.h" 150203790Sfabient#include "pmcpl_callgraph.h" 151203790Sfabient#include "pmcpl_annotate.h" 152203790Sfabient#include "pmcpl_calltree.h" 153147708Sjkoshy 154203790Sfabientstruct pmc_plugins { 155203790Sfabient const char *pl_name; /* name */ 156147708Sjkoshy 157203790Sfabient /* configure */ 158203790Sfabient int (*pl_configure)(char *opt); 159157144Sjkoshy 160203790Sfabient /* init and shutdown */ 161203790Sfabient int (*pl_init)(void); 162203790Sfabient void (*pl_shutdown)(FILE *mf); 163147708Sjkoshy 164203790Sfabient /* sample processing */ 165203790Sfabient void (*pl_process)(struct pmcstat_process *pp, 166203790Sfabient struct pmcstat_pmcrecord *pmcr, uint32_t nsamples, 167203790Sfabient uintfptr_t *cc, int usermode, uint32_t cpu); 168174396Sjkoshy 169203790Sfabient /* image */ 170203790Sfabient void (*pl_initimage)(struct pmcstat_image *pi); 171203790Sfabient void (*pl_shutdownimage)(struct pmcstat_image *pi); 172147708Sjkoshy 173203790Sfabient /* pmc */ 174203790Sfabient void (*pl_newpmc)(pmcstat_interned_string ps, 175203790Sfabient struct pmcstat_pmcrecord *pr); 176203790Sfabient 177203790Sfabient /* top display */ 178203790Sfabient void (*pl_topdisplay)(void); 179147708Sjkoshy 180203790Sfabient /* top keypress */ 181203790Sfabient int (*pl_topkeypress)(int c, WINDOW *w); 182147708Sjkoshy 183203790Sfabient} plugins[] = { 184203790Sfabient { 185203790Sfabient .pl_name = "none", 186203790Sfabient }, 187203790Sfabient { 188203790Sfabient .pl_name = "callgraph", 189203790Sfabient .pl_init = pmcpl_cg_init, 190203790Sfabient .pl_shutdown = pmcpl_cg_shutdown, 191203790Sfabient .pl_process = pmcpl_cg_process, 192203790Sfabient .pl_topkeypress = pmcpl_cg_topkeypress, 193203790Sfabient .pl_topdisplay = pmcpl_cg_topdisplay 194203790Sfabient }, 195203790Sfabient { 196203790Sfabient .pl_name = "gprof", 197203790Sfabient .pl_shutdown = pmcpl_gmon_shutdown, 198203790Sfabient .pl_process = pmcpl_gmon_process, 199203790Sfabient .pl_initimage = pmcpl_gmon_initimage, 200203790Sfabient .pl_shutdownimage = pmcpl_gmon_shutdownimage, 201203790Sfabient .pl_newpmc = pmcpl_gmon_newpmc 202203790Sfabient }, 203203790Sfabient { 204203790Sfabient .pl_name = "annotate", 205203790Sfabient .pl_process = pmcpl_annotate_process 206203790Sfabient }, 207203790Sfabient { 208203790Sfabient .pl_name = "calltree", 209203790Sfabient .pl_configure = pmcpl_ct_configure, 210203790Sfabient .pl_init = pmcpl_ct_init, 211203790Sfabient .pl_shutdown = pmcpl_ct_shutdown, 212203790Sfabient .pl_process = pmcpl_ct_process, 213203790Sfabient .pl_topkeypress = pmcpl_ct_topkeypress, 214203790Sfabient .pl_topdisplay = pmcpl_ct_topdisplay 215203790Sfabient }, 216203790Sfabient { 217203790Sfabient .pl_name = NULL 218203790Sfabient } 219147708Sjkoshy}; 220147708Sjkoshy 221203790Sfabientint pmcstat_mergepmc; 222147708Sjkoshy 223203790Sfabientint pmcstat_pmcinfilter = 0; /* PMC filter for top mode. */ 224203790Sfabientfloat pmcstat_threshold = 0.5; /* Cost filter for top mode. */ 225147708Sjkoshy 226174396Sjkoshy/* 227147708Sjkoshy * Prototypes 228147708Sjkoshy */ 229147708Sjkoshy 230157144Sjkoshystatic struct pmcstat_image *pmcstat_image_from_path(pmcstat_interned_string 231157144Sjkoshy _path, int _iskernelmodule); 232203790Sfabientstatic void pmcstat_image_get_aout_params(struct pmcstat_image *_image); 233203790Sfabientstatic void pmcstat_image_get_elf_params(struct pmcstat_image *_image); 234147708Sjkoshystatic void pmcstat_image_link(struct pmcstat_process *_pp, 235157144Sjkoshy struct pmcstat_image *_i, uintfptr_t _lpc); 236147708Sjkoshy 237157144Sjkoshystatic void pmcstat_pmcid_add(pmc_id_t _pmcid, 238203790Sfabient pmcstat_interned_string _name); 239147708Sjkoshy 240157144Sjkoshystatic void pmcstat_process_aout_exec(struct pmcstat_process *_pp, 241203790Sfabient struct pmcstat_image *_image, uintfptr_t _entryaddr); 242157144Sjkoshystatic void pmcstat_process_elf_exec(struct pmcstat_process *_pp, 243203790Sfabient struct pmcstat_image *_image, uintfptr_t _entryaddr); 244150139Sjkoshystatic void pmcstat_process_exec(struct pmcstat_process *_pp, 245203790Sfabient pmcstat_interned_string _path, uintfptr_t _entryaddr); 246157144Sjkoshystatic struct pmcstat_process *pmcstat_process_lookup(pid_t _pid, 247157144Sjkoshy int _allocate); 248147708Sjkoshystatic int pmcstat_string_compute_hash(const char *_string); 249157144Sjkoshystatic void pmcstat_string_initialize(void); 250157144Sjkoshystatic int pmcstat_string_lookup_hash(pmcstat_interned_string _is); 251157144Sjkoshystatic void pmcstat_string_shutdown(void); 252210794Sfabientstatic void pmcstat_stats_reset(int _reset_global); 253147708Sjkoshy 254147708Sjkoshy/* 255157144Sjkoshy * A simple implementation of interned strings. Each interned string 256157144Sjkoshy * is assigned a unique address, so that subsequent string compares 257157144Sjkoshy * can be done by a simple pointer comparision instead of using 258157144Sjkoshy * strcmp(). This speeds up hash table lookups and saves memory if 259157144Sjkoshy * duplicate strings are the norm. 260157144Sjkoshy */ 261157144Sjkoshystruct pmcstat_string { 262157144Sjkoshy LIST_ENTRY(pmcstat_string) ps_next; /* hash link */ 263157144Sjkoshy int ps_len; 264157144Sjkoshy int ps_hash; 265157144Sjkoshy char *ps_string; 266157144Sjkoshy}; 267157144Sjkoshy 268157144Sjkoshystatic LIST_HEAD(,pmcstat_string) pmcstat_string_hash[PMCSTAT_NHASH]; 269157144Sjkoshy 270157144Sjkoshy/* 271203790Sfabient * PMC count. 272203790Sfabient */ 273203790Sfabientint pmcstat_npmcs; 274203790Sfabient 275203790Sfabient/* 276203790Sfabient * PMC Top mode pause state. 277203790Sfabient */ 278203790Sfabientint pmcstat_pause; 279203790Sfabient 280206090Sfabientstatic void 281210794Sfabientpmcstat_stats_reset(int reset_global) 282206090Sfabient{ 283206090Sfabient struct pmcstat_pmcrecord *pr; 284206090Sfabient 285206090Sfabient /* Flush PMCs stats. */ 286206090Sfabient LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) { 287206090Sfabient pr->pr_samples = 0; 288206090Sfabient pr->pr_dubious_frames = 0; 289206090Sfabient } 290210794Sfabient ps_samples_period = 0; 291206090Sfabient 292206090Sfabient /* Flush global stats. */ 293210794Sfabient if (reset_global) 294210794Sfabient bzero(&pmcstat_stats, sizeof(struct pmcstat_stats)); 295206090Sfabient} 296206090Sfabient 297203790Sfabient/* 298157144Sjkoshy * Compute a 'hash' value for a string. 299157144Sjkoshy */ 300157144Sjkoshy 301157144Sjkoshystatic int 302157144Sjkoshypmcstat_string_compute_hash(const char *s) 303157144Sjkoshy{ 304267415Semaste unsigned hash; 305157144Sjkoshy 306267415Semaste for (hash = 2166136261; *s; s++) 307267415Semaste hash = (hash ^ *s) * 16777619; 308157144Sjkoshy 309157144Sjkoshy return (hash & PMCSTAT_HASH_MASK); 310157144Sjkoshy} 311157144Sjkoshy 312157144Sjkoshy/* 313157144Sjkoshy * Intern a copy of string 's', and return a pointer to the 314157144Sjkoshy * interned structure. 315157144Sjkoshy */ 316157144Sjkoshy 317203790Sfabientpmcstat_interned_string 318157144Sjkoshypmcstat_string_intern(const char *s) 319157144Sjkoshy{ 320157144Sjkoshy struct pmcstat_string *ps; 321157144Sjkoshy const struct pmcstat_string *cps; 322157144Sjkoshy int hash, len; 323157144Sjkoshy 324157144Sjkoshy if ((cps = pmcstat_string_lookup(s)) != NULL) 325157144Sjkoshy return (cps); 326157144Sjkoshy 327157144Sjkoshy hash = pmcstat_string_compute_hash(s); 328157144Sjkoshy len = strlen(s); 329157144Sjkoshy 330157144Sjkoshy if ((ps = malloc(sizeof(*ps))) == NULL) 331157144Sjkoshy err(EX_OSERR, "ERROR: Could not intern string"); 332157144Sjkoshy ps->ps_len = len; 333157144Sjkoshy ps->ps_hash = hash; 334157144Sjkoshy ps->ps_string = strdup(s); 335157144Sjkoshy LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next); 336157144Sjkoshy return ((pmcstat_interned_string) ps); 337157144Sjkoshy} 338157144Sjkoshy 339203790Sfabientconst char * 340157144Sjkoshypmcstat_string_unintern(pmcstat_interned_string str) 341157144Sjkoshy{ 342157144Sjkoshy const char *s; 343157144Sjkoshy 344157144Sjkoshy s = ((const struct pmcstat_string *) str)->ps_string; 345157144Sjkoshy return (s); 346157144Sjkoshy} 347157144Sjkoshy 348203790Sfabientpmcstat_interned_string 349157144Sjkoshypmcstat_string_lookup(const char *s) 350157144Sjkoshy{ 351157144Sjkoshy struct pmcstat_string *ps; 352157144Sjkoshy int hash, len; 353157144Sjkoshy 354157144Sjkoshy hash = pmcstat_string_compute_hash(s); 355157144Sjkoshy len = strlen(s); 356157144Sjkoshy 357157144Sjkoshy LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next) 358157144Sjkoshy if (ps->ps_len == len && ps->ps_hash == hash && 359157144Sjkoshy strcmp(ps->ps_string, s) == 0) 360157144Sjkoshy return (ps); 361157144Sjkoshy return (NULL); 362157144Sjkoshy} 363157144Sjkoshy 364157144Sjkoshystatic int 365157144Sjkoshypmcstat_string_lookup_hash(pmcstat_interned_string s) 366157144Sjkoshy{ 367157144Sjkoshy const struct pmcstat_string *ps; 368157144Sjkoshy 369157144Sjkoshy ps = (const struct pmcstat_string *) s; 370157144Sjkoshy return (ps->ps_hash); 371157144Sjkoshy} 372157144Sjkoshy 373157144Sjkoshy/* 374157144Sjkoshy * Initialize the string interning facility. 375157144Sjkoshy */ 376157144Sjkoshy 377157144Sjkoshystatic void 378157144Sjkoshypmcstat_string_initialize(void) 379157144Sjkoshy{ 380157144Sjkoshy int i; 381157144Sjkoshy 382157144Sjkoshy for (i = 0; i < PMCSTAT_NHASH; i++) 383157144Sjkoshy LIST_INIT(&pmcstat_string_hash[i]); 384157144Sjkoshy} 385157144Sjkoshy 386157144Sjkoshy/* 387157144Sjkoshy * Destroy the string table, free'ing up space. 388157144Sjkoshy */ 389157144Sjkoshy 390157144Sjkoshystatic void 391157144Sjkoshypmcstat_string_shutdown(void) 392157144Sjkoshy{ 393157144Sjkoshy int i; 394157144Sjkoshy struct pmcstat_string *ps, *pstmp; 395157144Sjkoshy 396157144Sjkoshy for (i = 0; i < PMCSTAT_NHASH; i++) 397157144Sjkoshy LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next, 398157144Sjkoshy pstmp) { 399157144Sjkoshy LIST_REMOVE(ps, ps_next); 400157144Sjkoshy free(ps->ps_string); 401157144Sjkoshy free(ps); 402157144Sjkoshy } 403157144Sjkoshy} 404157144Sjkoshy 405157144Sjkoshy/* 406157144Sjkoshy * Determine whether a given executable image is an A.OUT object, and 407157144Sjkoshy * if so, fill in its parameters from the text file. 408157144Sjkoshy * Sets image->pi_type. 409157144Sjkoshy */ 410157144Sjkoshy 411147708Sjkoshystatic void 412203790Sfabientpmcstat_image_get_aout_params(struct pmcstat_image *image) 413147708Sjkoshy{ 414157144Sjkoshy int fd; 415157144Sjkoshy ssize_t nbytes; 416157144Sjkoshy struct exec ex; 417157144Sjkoshy const char *path; 418157144Sjkoshy char buffer[PATH_MAX]; 419157144Sjkoshy 420157144Sjkoshy path = pmcstat_string_unintern(image->pi_execpath); 421157144Sjkoshy assert(path != NULL); 422157144Sjkoshy 423157144Sjkoshy if (image->pi_iskernelmodule) 424237969Sobrien errx(EX_SOFTWARE, 425237969Sobrien "ERROR: a.out kernel modules are unsupported \"%s\"", path); 426157144Sjkoshy 427157144Sjkoshy (void) snprintf(buffer, sizeof(buffer), "%s%s", 428203790Sfabient args.pa_fsroot, path); 429157144Sjkoshy 430157144Sjkoshy if ((fd = open(buffer, O_RDONLY, 0)) < 0 || 431157144Sjkoshy (nbytes = read(fd, &ex, sizeof(ex))) < 0) { 432235977Sfabient if (args.pa_verbosity >= 2) 433235977Sfabient warn("WARNING: Cannot determine type of \"%s\"", 434235977Sfabient path); 435157144Sjkoshy image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE; 436157144Sjkoshy if (fd != -1) 437157144Sjkoshy (void) close(fd); 438157144Sjkoshy return; 439157144Sjkoshy } 440157144Sjkoshy 441157144Sjkoshy (void) close(fd); 442157144Sjkoshy 443157144Sjkoshy if ((unsigned) nbytes != sizeof(ex) || 444157144Sjkoshy N_BADMAG(ex)) 445157144Sjkoshy return; 446157144Sjkoshy 447157144Sjkoshy image->pi_type = PMCSTAT_IMAGE_AOUT; 448157144Sjkoshy 449157144Sjkoshy /* TODO: the rest of a.out processing */ 450157144Sjkoshy 451157144Sjkoshy return; 452157144Sjkoshy} 453157144Sjkoshy 454157144Sjkoshy/* 455174396Sjkoshy * Helper function. 456174396Sjkoshy */ 457174396Sjkoshy 458174396Sjkoshystatic int 459174396Sjkoshypmcstat_symbol_compare(const void *a, const void *b) 460174396Sjkoshy{ 461174396Sjkoshy const struct pmcstat_symbol *sym1, *sym2; 462174396Sjkoshy 463174396Sjkoshy sym1 = (const struct pmcstat_symbol *) a; 464174396Sjkoshy sym2 = (const struct pmcstat_symbol *) b; 465174396Sjkoshy 466174396Sjkoshy if (sym1->ps_end <= sym2->ps_start) 467174396Sjkoshy return (-1); 468174396Sjkoshy if (sym1->ps_start >= sym2->ps_end) 469174396Sjkoshy return (1); 470174396Sjkoshy return (0); 471174396Sjkoshy} 472174396Sjkoshy 473174396Sjkoshy/* 474174396Sjkoshy * Map an address to a symbol in an image. 475174396Sjkoshy */ 476174396Sjkoshy 477203790Sfabientstruct pmcstat_symbol * 478174396Sjkoshypmcstat_symbol_search(struct pmcstat_image *image, uintfptr_t addr) 479174396Sjkoshy{ 480174396Sjkoshy struct pmcstat_symbol sym; 481174396Sjkoshy 482174396Sjkoshy if (image->pi_symbols == NULL) 483174396Sjkoshy return (NULL); 484174396Sjkoshy 485174396Sjkoshy sym.ps_name = NULL; 486174396Sjkoshy sym.ps_start = addr; 487174396Sjkoshy sym.ps_end = addr + 1; 488174396Sjkoshy 489174396Sjkoshy return (bsearch((void *) &sym, image->pi_symbols, 490174396Sjkoshy image->pi_symcount, sizeof(struct pmcstat_symbol), 491174396Sjkoshy pmcstat_symbol_compare)); 492174396Sjkoshy} 493174396Sjkoshy 494174396Sjkoshy/* 495174396Sjkoshy * Add the list of symbols in the given section to the list associated 496174396Sjkoshy * with the object. 497174396Sjkoshy */ 498174396Sjkoshystatic void 499174396Sjkoshypmcstat_image_add_symbols(struct pmcstat_image *image, Elf *e, 500174396Sjkoshy Elf_Scn *scn, GElf_Shdr *sh) 501174396Sjkoshy{ 502174396Sjkoshy int firsttime; 503174396Sjkoshy size_t n, newsyms, nshsyms, nfuncsyms; 504174396Sjkoshy struct pmcstat_symbol *symptr; 505174396Sjkoshy char *fnname; 506174396Sjkoshy GElf_Sym sym; 507174396Sjkoshy Elf_Data *data; 508174396Sjkoshy 509174396Sjkoshy if ((data = elf_getdata(scn, NULL)) == NULL) 510174396Sjkoshy return; 511174396Sjkoshy 512174396Sjkoshy /* 513174396Sjkoshy * Determine the number of functions named in this 514174396Sjkoshy * section. 515174396Sjkoshy */ 516174396Sjkoshy 517174396Sjkoshy nshsyms = sh->sh_size / sh->sh_entsize; 518174396Sjkoshy for (n = nfuncsyms = 0; n < nshsyms; n++) { 519174396Sjkoshy if (gelf_getsym(data, (int) n, &sym) != &sym) 520174396Sjkoshy return; 521174396Sjkoshy if (GELF_ST_TYPE(sym.st_info) == STT_FUNC) 522174396Sjkoshy nfuncsyms++; 523174396Sjkoshy } 524174396Sjkoshy 525174396Sjkoshy if (nfuncsyms == 0) 526174396Sjkoshy return; 527174396Sjkoshy 528174396Sjkoshy /* 529174396Sjkoshy * Allocate space for the new entries. 530174396Sjkoshy */ 531174396Sjkoshy firsttime = image->pi_symbols == NULL; 532174396Sjkoshy symptr = realloc(image->pi_symbols, 533174396Sjkoshy sizeof(*symptr) * (image->pi_symcount + nfuncsyms)); 534174396Sjkoshy if (symptr == image->pi_symbols) /* realloc() failed. */ 535174396Sjkoshy return; 536174396Sjkoshy image->pi_symbols = symptr; 537174396Sjkoshy 538174396Sjkoshy /* 539174396Sjkoshy * Append new symbols to the end of the current table. 540174396Sjkoshy */ 541174396Sjkoshy symptr += image->pi_symcount; 542174396Sjkoshy 543174396Sjkoshy for (n = newsyms = 0; n < nshsyms; n++) { 544174396Sjkoshy if (gelf_getsym(data, (int) n, &sym) != &sym) 545174396Sjkoshy return; 546174396Sjkoshy if (GELF_ST_TYPE(sym.st_info) != STT_FUNC) 547174396Sjkoshy continue; 548207731Sfabient if (sym.st_shndx == STN_UNDEF) 549207731Sfabient continue; 550174396Sjkoshy 551174396Sjkoshy if (!firsttime && pmcstat_symbol_search(image, sym.st_value)) 552174396Sjkoshy continue; /* We've seen this symbol already. */ 553174396Sjkoshy 554174396Sjkoshy if ((fnname = elf_strptr(e, sh->sh_link, sym.st_name)) 555174396Sjkoshy == NULL) 556174396Sjkoshy continue; 557236998Sfabient#ifdef __arm__ 558236998Sfabient /* Remove spurious ARM function name. */ 559236998Sfabient if (fnname[0] == '$' && 560236998Sfabient (fnname[1] == 'a' || fnname[1] == 't' || 561236998Sfabient fnname[1] == 'd') && 562236998Sfabient fnname[2] == '\0') 563236998Sfabient continue; 564236998Sfabient#endif 565174396Sjkoshy 566174396Sjkoshy symptr->ps_name = pmcstat_string_intern(fnname); 567174396Sjkoshy symptr->ps_start = sym.st_value - image->pi_vaddr; 568174396Sjkoshy symptr->ps_end = symptr->ps_start + sym.st_size; 569174396Sjkoshy symptr++; 570174396Sjkoshy 571174396Sjkoshy newsyms++; 572174396Sjkoshy } 573174396Sjkoshy 574174396Sjkoshy image->pi_symcount += newsyms; 575174396Sjkoshy 576174396Sjkoshy assert(newsyms <= nfuncsyms); 577174396Sjkoshy 578174396Sjkoshy /* 579174396Sjkoshy * Return space to the system if there were duplicates. 580174396Sjkoshy */ 581174396Sjkoshy if (newsyms < nfuncsyms) 582174396Sjkoshy image->pi_symbols = realloc(image->pi_symbols, 583174396Sjkoshy sizeof(*symptr) * image->pi_symcount); 584174396Sjkoshy 585174396Sjkoshy /* 586174396Sjkoshy * Keep the list of symbols sorted. 587174396Sjkoshy */ 588174396Sjkoshy qsort(image->pi_symbols, image->pi_symcount, sizeof(*symptr), 589174396Sjkoshy pmcstat_symbol_compare); 590174396Sjkoshy 591174396Sjkoshy /* 592174396Sjkoshy * Deal with function symbols that have a size of 'zero' by 593174396Sjkoshy * making them extend to the next higher address. These 594174396Sjkoshy * symbols are usually defined in assembly code. 595174396Sjkoshy */ 596174396Sjkoshy for (symptr = image->pi_symbols; 597174396Sjkoshy symptr < image->pi_symbols + (image->pi_symcount - 1); 598174396Sjkoshy symptr++) 599174396Sjkoshy if (symptr->ps_start == symptr->ps_end) 600174396Sjkoshy symptr->ps_end = (symptr+1)->ps_start; 601174396Sjkoshy} 602174396Sjkoshy 603174396Sjkoshy/* 604157144Sjkoshy * Examine an ELF file to determine the size of its text segment. 605157144Sjkoshy * Sets image->pi_type if anything conclusive can be determined about 606157144Sjkoshy * this image. 607157144Sjkoshy */ 608157144Sjkoshy 609157144Sjkoshystatic void 610203790Sfabientpmcstat_image_get_elf_params(struct pmcstat_image *image) 611157144Sjkoshy{ 612174396Sjkoshy int fd; 613174396Sjkoshy size_t i, nph, nsh; 614174396Sjkoshy const char *path, *elfbase; 615203790Sfabient char *p, *endp; 616147708Sjkoshy uintfptr_t minva, maxva; 617174396Sjkoshy Elf *e; 618174396Sjkoshy Elf_Scn *scn; 619174396Sjkoshy GElf_Ehdr eh; 620174396Sjkoshy GElf_Phdr ph; 621174396Sjkoshy GElf_Shdr sh; 622157144Sjkoshy enum pmcstat_image_type image_type; 623210797Sfabient char buffer[PATH_MAX]; 624147708Sjkoshy 625150139Sjkoshy assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN); 626150139Sjkoshy 627174396Sjkoshy image->pi_start = minva = ~(uintfptr_t) 0; 628174396Sjkoshy image->pi_end = maxva = (uintfptr_t) 0; 629174396Sjkoshy image->pi_type = image_type = PMCSTAT_IMAGE_INDETERMINABLE; 630174396Sjkoshy image->pi_isdynamic = 0; 631174396Sjkoshy image->pi_dynlinkerpath = NULL; 632174396Sjkoshy image->pi_vaddr = 0; 633174396Sjkoshy 634157144Sjkoshy path = pmcstat_string_unintern(image->pi_execpath); 635157144Sjkoshy assert(path != NULL); 636147708Sjkoshy 637157144Sjkoshy /* 638157144Sjkoshy * Look for kernel modules under FSROOT/KERNELPATH/NAME, 639157144Sjkoshy * and user mode executable objects under FSROOT/PATHNAME. 640157144Sjkoshy */ 641157144Sjkoshy if (image->pi_iskernelmodule) 642157144Sjkoshy (void) snprintf(buffer, sizeof(buffer), "%s%s/%s", 643203790Sfabient args.pa_fsroot, args.pa_kernel, path); 644157144Sjkoshy else 645157144Sjkoshy (void) snprintf(buffer, sizeof(buffer), "%s%s", 646203790Sfabient args.pa_fsroot, path); 647147708Sjkoshy 648174396Sjkoshy e = NULL; 649157144Sjkoshy if ((fd = open(buffer, O_RDONLY, 0)) < 0 || 650174396Sjkoshy (e = elf_begin(fd, ELF_C_READ, NULL)) == NULL || 651174396Sjkoshy (elf_kind(e) != ELF_K_ELF)) { 652235977Sfabient if (args.pa_verbosity >= 2) 653235977Sfabient warnx("WARNING: Cannot determine the type of \"%s\".", 654235977Sfabient buffer); 655174396Sjkoshy goto done; 656157144Sjkoshy } 657147708Sjkoshy 658174396Sjkoshy if (gelf_getehdr(e, &eh) != &eh) { 659237969Sobrien warnx( 660237969Sobrien "WARNING: Cannot retrieve the ELF Header for \"%s\": %s.", 661237969Sobrien buffer, elf_errmsg(-1)); 662174396Sjkoshy goto done; 663174396Sjkoshy } 664147708Sjkoshy 665174396Sjkoshy if (eh.e_type != ET_EXEC && eh.e_type != ET_DYN && 666174396Sjkoshy !(image->pi_iskernelmodule && eh.e_type == ET_REL)) { 667174396Sjkoshy warnx("WARNING: \"%s\" is of an unsupported ELF type.", 668174396Sjkoshy buffer); 669174396Sjkoshy goto done; 670174396Sjkoshy } 671147708Sjkoshy 672174396Sjkoshy image_type = eh.e_ident[EI_CLASS] == ELFCLASS32 ? 673174396Sjkoshy PMCSTAT_IMAGE_ELF32 : PMCSTAT_IMAGE_ELF64; 674174396Sjkoshy 675157144Sjkoshy /* 676174396Sjkoshy * Determine the virtual address where an executable would be 677174396Sjkoshy * loaded. Additionally, for dynamically linked executables, 678174396Sjkoshy * save the pathname to the runtime linker. 679157144Sjkoshy */ 680174396Sjkoshy if (eh.e_type == ET_EXEC) { 681174396Sjkoshy if (elf_getphnum(e, &nph) == 0) { 682237969Sobrien warnx( 683237969Sobrien"WARNING: Could not determine the number of program headers in \"%s\": %s.", 684237969Sobrien buffer, 685174396Sjkoshy elf_errmsg(-1)); 686174396Sjkoshy goto done; 687147708Sjkoshy } 688174396Sjkoshy for (i = 0; i < eh.e_phnum; i++) { 689174396Sjkoshy if (gelf_getphdr(e, i, &ph) != &ph) { 690237969Sobrien warnx( 691237969Sobrien"WARNING: Retrieval of PHDR entry #%ju in \"%s\" failed: %s.", 692237969Sobrien (uintmax_t) i, buffer, elf_errmsg(-1)); 693174396Sjkoshy goto done; 694174396Sjkoshy } 695174396Sjkoshy switch (ph.p_type) { 696174396Sjkoshy case PT_DYNAMIC: 697174396Sjkoshy image->pi_isdynamic = 1; 698174396Sjkoshy break; 699174396Sjkoshy case PT_INTERP: 700174396Sjkoshy if ((elfbase = elf_rawfile(e, NULL)) == NULL) { 701237969Sobrien warnx( 702237969Sobrien"WARNING: Cannot retrieve the interpreter for \"%s\": %s.", 703174396Sjkoshy buffer, elf_errmsg(-1)); 704174396Sjkoshy goto done; 705174396Sjkoshy } 706174396Sjkoshy image->pi_dynlinkerpath = 707210797Sfabient pmcstat_string_intern(elfbase + 708210797Sfabient ph.p_offset); 709174396Sjkoshy break; 710174396Sjkoshy case PT_LOAD: 711174396Sjkoshy if (ph.p_offset == 0) 712174396Sjkoshy image->pi_vaddr = ph.p_vaddr; 713174396Sjkoshy break; 714174396Sjkoshy } 715174396Sjkoshy } 716174396Sjkoshy } 717151434Sjkoshy 718174396Sjkoshy /* 719174396Sjkoshy * Get the min and max VA associated with this ELF object. 720174396Sjkoshy */ 721174396Sjkoshy if (elf_getshnum(e, &nsh) == 0) { 722237969Sobrien warnx( 723237969Sobrien"WARNING: Could not determine the number of sections for \"%s\": %s.", 724237969Sobrien buffer, elf_errmsg(-1)); 725174396Sjkoshy goto done; 726174396Sjkoshy } 727151434Sjkoshy 728174396Sjkoshy for (i = 0; i < nsh; i++) { 729174396Sjkoshy if ((scn = elf_getscn(e, i)) == NULL || 730174396Sjkoshy gelf_getshdr(scn, &sh) != &sh) { 731237969Sobrien warnx( 732237969Sobrien"WARNING: Could not retrieve section header #%ju in \"%s\": %s.", 733237969Sobrien (uintmax_t) i, buffer, elf_errmsg(-1)); 734174396Sjkoshy goto done; 735151434Sjkoshy } 736174396Sjkoshy if (sh.sh_flags & SHF_EXECINSTR) { 737174396Sjkoshy minva = min(minva, sh.sh_addr); 738174396Sjkoshy maxva = max(maxva, sh.sh_addr + sh.sh_size); 739174396Sjkoshy } 740174396Sjkoshy if (sh.sh_type == SHT_SYMTAB || sh.sh_type == SHT_DYNSYM) 741174396Sjkoshy pmcstat_image_add_symbols(image, e, scn, &sh); 742147708Sjkoshy } 743147708Sjkoshy 744151434Sjkoshy image->pi_start = minva; 745157144Sjkoshy image->pi_end = maxva; 746157144Sjkoshy image->pi_type = image_type; 747174396Sjkoshy image->pi_fullpath = pmcstat_string_intern(buffer); 748151434Sjkoshy 749203790Sfabient /* Build display name 750203790Sfabient */ 751203790Sfabient endp = buffer; 752203790Sfabient for (p = buffer; *p; p++) 753203790Sfabient if (*p == '/') 754203790Sfabient endp = p+1; 755203790Sfabient image->pi_name = pmcstat_string_intern(endp); 756203790Sfabient 757174396Sjkoshy done: 758174396Sjkoshy (void) elf_end(e); 759174396Sjkoshy if (fd >= 0) 760174396Sjkoshy (void) close(fd); 761157144Sjkoshy return; 762157144Sjkoshy} 763147708Sjkoshy 764157144Sjkoshy/* 765157144Sjkoshy * Given an image descriptor, determine whether it is an ELF, or AOUT. 766157144Sjkoshy * If no handler claims the image, set its type to 'INDETERMINABLE'. 767157144Sjkoshy */ 768157144Sjkoshy 769203790Sfabientvoid 770203790Sfabientpmcstat_image_determine_type(struct pmcstat_image *image) 771157144Sjkoshy{ 772157144Sjkoshy assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN); 773157144Sjkoshy 774157144Sjkoshy /* Try each kind of handler in turn */ 775157144Sjkoshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 776203790Sfabient pmcstat_image_get_elf_params(image); 777157144Sjkoshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 778203790Sfabient pmcstat_image_get_aout_params(image); 779157144Sjkoshy 780157144Sjkoshy /* 781157144Sjkoshy * Otherwise, remember that we tried to determine 782157144Sjkoshy * the object's type and had failed. 783157144Sjkoshy */ 784157144Sjkoshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 785157144Sjkoshy image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE; 786147708Sjkoshy} 787147708Sjkoshy 788147708Sjkoshy/* 789150139Sjkoshy * Locate an image descriptor given an interned path, adding a fresh 790150139Sjkoshy * descriptor to the cache if necessary. This function also finds a 791150139Sjkoshy * suitable name for this image's sample file. 792157144Sjkoshy * 793157144Sjkoshy * We defer filling in the file format specific parts of the image 794157144Sjkoshy * structure till the time we actually see a sample that would fall 795157144Sjkoshy * into this image. 796147708Sjkoshy */ 797147708Sjkoshy 798147708Sjkoshystatic struct pmcstat_image * 799157144Sjkoshypmcstat_image_from_path(pmcstat_interned_string internedpath, 800157144Sjkoshy int iskernelmodule) 801147708Sjkoshy{ 802203790Sfabient int hash; 803147708Sjkoshy struct pmcstat_image *pi; 804147708Sjkoshy 805157144Sjkoshy hash = pmcstat_string_lookup_hash(internedpath); 806147708Sjkoshy 807157144Sjkoshy /* First, look for an existing entry. */ 808147708Sjkoshy LIST_FOREACH(pi, &pmcstat_image_hash[hash], pi_next) 809157144Sjkoshy if (pi->pi_execpath == internedpath && 810174396Sjkoshy pi->pi_iskernelmodule == iskernelmodule) 811157144Sjkoshy return (pi); 812147708Sjkoshy 813147708Sjkoshy /* 814174396Sjkoshy * Allocate a new entry and place it at the head of the hash 815174396Sjkoshy * and LRU lists. 816147708Sjkoshy */ 817147708Sjkoshy pi = malloc(sizeof(*pi)); 818147708Sjkoshy if (pi == NULL) 819157144Sjkoshy return (NULL); 820147708Sjkoshy 821147708Sjkoshy pi->pi_type = PMCSTAT_IMAGE_UNKNOWN; 822157144Sjkoshy pi->pi_execpath = internedpath; 823147708Sjkoshy pi->pi_start = ~0; 824147708Sjkoshy pi->pi_end = 0; 825174396Sjkoshy pi->pi_entry = 0; 826174396Sjkoshy pi->pi_vaddr = 0; 827174396Sjkoshy pi->pi_isdynamic = 0; 828157144Sjkoshy pi->pi_iskernelmodule = iskernelmodule; 829174396Sjkoshy pi->pi_dynlinkerpath = NULL; 830174396Sjkoshy pi->pi_symbols = NULL; 831174396Sjkoshy pi->pi_symcount = 0; 832203790Sfabient pi->pi_addr2line = NULL; 833147708Sjkoshy 834203790Sfabient if (plugins[args.pa_pplugin].pl_initimage != NULL) 835203790Sfabient plugins[args.pa_pplugin].pl_initimage(pi); 836203790Sfabient if (plugins[args.pa_plugin].pl_initimage != NULL) 837203790Sfabient plugins[args.pa_plugin].pl_initimage(pi); 838147708Sjkoshy 839147708Sjkoshy LIST_INSERT_HEAD(&pmcstat_image_hash[hash], pi, pi_next); 840147708Sjkoshy 841157144Sjkoshy return (pi); 842147708Sjkoshy} 843147708Sjkoshy 844147708Sjkoshy/* 845157144Sjkoshy * Record the fact that PC values from 'start' to 'end' come from 846147708Sjkoshy * image 'image'. 847147708Sjkoshy */ 848147708Sjkoshy 849147708Sjkoshystatic void 850147708Sjkoshypmcstat_image_link(struct pmcstat_process *pp, struct pmcstat_image *image, 851157144Sjkoshy uintfptr_t start) 852147708Sjkoshy{ 853147708Sjkoshy struct pmcstat_pcmap *pcm, *pcmnew; 854157144Sjkoshy uintfptr_t offset; 855147708Sjkoshy 856157144Sjkoshy assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN && 857157144Sjkoshy image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE); 858157144Sjkoshy 859147708Sjkoshy if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL) 860157144Sjkoshy err(EX_OSERR, "ERROR: Cannot create a map entry"); 861147708Sjkoshy 862157144Sjkoshy /* 863157144Sjkoshy * Adjust the map entry to only cover the text portion 864157144Sjkoshy * of the object. 865157144Sjkoshy */ 866157144Sjkoshy 867157144Sjkoshy offset = start - image->pi_vaddr; 868157144Sjkoshy pcmnew->ppm_lowpc = image->pi_start + offset; 869157144Sjkoshy pcmnew->ppm_highpc = image->pi_end + offset; 870147708Sjkoshy pcmnew->ppm_image = image; 871147708Sjkoshy 872157144Sjkoshy assert(pcmnew->ppm_lowpc < pcmnew->ppm_highpc); 873157144Sjkoshy 874157144Sjkoshy /* Overlapped mmap()'s are assumed to never occur. */ 875147708Sjkoshy TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next) 876157144Sjkoshy if (pcm->ppm_lowpc >= pcmnew->ppm_highpc) 877147708Sjkoshy break; 878147708Sjkoshy 879147708Sjkoshy if (pcm == NULL) 880147708Sjkoshy TAILQ_INSERT_TAIL(&pp->pp_map, pcmnew, ppm_next); 881147708Sjkoshy else 882147708Sjkoshy TAILQ_INSERT_BEFORE(pcm, pcmnew, ppm_next); 883147708Sjkoshy} 884147708Sjkoshy 885147708Sjkoshy/* 886157144Sjkoshy * Unmap images in the range [start..end) associated with process 887157144Sjkoshy * 'pp'. 888157144Sjkoshy */ 889157144Sjkoshy 890157144Sjkoshystatic void 891157144Sjkoshypmcstat_image_unmap(struct pmcstat_process *pp, uintfptr_t start, 892157144Sjkoshy uintfptr_t end) 893157144Sjkoshy{ 894157144Sjkoshy struct pmcstat_pcmap *pcm, *pcmtmp, *pcmnew; 895157144Sjkoshy 896157144Sjkoshy assert(pp != NULL); 897157144Sjkoshy assert(start < end); 898157144Sjkoshy 899157144Sjkoshy /* 900157144Sjkoshy * Cases: 901157144Sjkoshy * - we could have the range completely in the middle of an 902157144Sjkoshy * existing pcmap; in this case we have to split the pcmap 903157144Sjkoshy * structure into two (i.e., generate a 'hole'). 904157144Sjkoshy * - we could have the range covering multiple pcmaps; these 905157144Sjkoshy * will have to be removed. 906157144Sjkoshy * - we could have either 'start' or 'end' falling in the 907157144Sjkoshy * middle of a pcmap; in this case shorten the entry. 908157144Sjkoshy */ 909157144Sjkoshy TAILQ_FOREACH_SAFE(pcm, &pp->pp_map, ppm_next, pcmtmp) { 910157144Sjkoshy assert(pcm->ppm_lowpc < pcm->ppm_highpc); 911157144Sjkoshy if (pcm->ppm_highpc <= start) 912157144Sjkoshy continue; 913174396Sjkoshy if (pcm->ppm_lowpc >= end) 914157144Sjkoshy return; 915157144Sjkoshy if (pcm->ppm_lowpc >= start && pcm->ppm_highpc <= end) { 916157144Sjkoshy /* 917157144Sjkoshy * The current pcmap is completely inside the 918157144Sjkoshy * unmapped range: remove it entirely. 919157144Sjkoshy */ 920157144Sjkoshy TAILQ_REMOVE(&pp->pp_map, pcm, ppm_next); 921157144Sjkoshy free(pcm); 922157144Sjkoshy } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc > end) { 923157144Sjkoshy /* 924157144Sjkoshy * Split this pcmap into two; curtail the 925157144Sjkoshy * current map to end at [start-1], and start 926157144Sjkoshy * the new one at [end]. 927157144Sjkoshy */ 928157144Sjkoshy if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL) 929237969Sobrien err(EX_OSERR, 930237969Sobrien "ERROR: Cannot split a map entry"); 931157144Sjkoshy 932157144Sjkoshy pcmnew->ppm_image = pcm->ppm_image; 933157144Sjkoshy 934157144Sjkoshy pcmnew->ppm_lowpc = end; 935157144Sjkoshy pcmnew->ppm_highpc = pcm->ppm_highpc; 936157144Sjkoshy 937157144Sjkoshy pcm->ppm_highpc = start; 938157144Sjkoshy 939157144Sjkoshy TAILQ_INSERT_AFTER(&pp->pp_map, pcm, pcmnew, ppm_next); 940157144Sjkoshy 941157144Sjkoshy return; 942174396Sjkoshy } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc <= end) 943174396Sjkoshy pcm->ppm_highpc = start; 944174396Sjkoshy else if (pcm->ppm_lowpc >= start && pcm->ppm_highpc > end) 945174396Sjkoshy pcm->ppm_lowpc = end; 946157144Sjkoshy else 947157144Sjkoshy assert(0); 948157144Sjkoshy } 949157144Sjkoshy} 950157144Sjkoshy 951157144Sjkoshy/* 952203790Sfabient * Resolve file name and line number for the given address. 953203790Sfabient */ 954203790Sfabientint 955203790Sfabientpmcstat_image_addr2line(struct pmcstat_image *image, uintfptr_t addr, 956203790Sfabient char *sourcefile, size_t sourcefile_len, unsigned *sourceline, 957203790Sfabient char *funcname, size_t funcname_len) 958203790Sfabient{ 959203790Sfabient static int addr2line_warn = 0; 960235977Sfabient unsigned l; 961203790Sfabient 962203790Sfabient char *sep, cmdline[PATH_MAX], imagepath[PATH_MAX]; 963203790Sfabient int fd; 964203790Sfabient 965203790Sfabient if (image->pi_addr2line == NULL) { 966210794Sfabient snprintf(imagepath, sizeof(imagepath), "%s%s.symbols", 967210794Sfabient args.pa_fsroot, 968203790Sfabient pmcstat_string_unintern(image->pi_fullpath)); 969203790Sfabient fd = open(imagepath, O_RDONLY); 970203790Sfabient if (fd < 0) { 971210794Sfabient snprintf(imagepath, sizeof(imagepath), "%s%s", 972210794Sfabient args.pa_fsroot, 973203790Sfabient pmcstat_string_unintern(image->pi_fullpath)); 974203790Sfabient } else 975203790Sfabient close(fd); 976235977Sfabient /* 977235977Sfabient * New addr2line support recursive inline function with -i 978235977Sfabient * but the format does not add a marker when no more entries 979235977Sfabient * are available. 980235977Sfabient */ 981203790Sfabient snprintf(cmdline, sizeof(cmdline), "addr2line -Cfe \"%s\"", 982203790Sfabient imagepath); 983203790Sfabient image->pi_addr2line = popen(cmdline, "r+"); 984203790Sfabient if (image->pi_addr2line == NULL) { 985203790Sfabient if (!addr2line_warn) { 986203790Sfabient addr2line_warn = 1; 987237969Sobrien warnx( 988237969Sobrien"WARNING: addr2line is needed for source code information." 989237969Sobrien ); 990203790Sfabient } 991203790Sfabient return (0); 992203790Sfabient } 993203790Sfabient } 994203790Sfabient 995203790Sfabient if (feof(image->pi_addr2line) || ferror(image->pi_addr2line)) { 996203790Sfabient warnx("WARNING: addr2line pipe error"); 997203790Sfabient pclose(image->pi_addr2line); 998203790Sfabient image->pi_addr2line = NULL; 999203790Sfabient return (0); 1000203790Sfabient } 1001203790Sfabient 1002203790Sfabient fprintf(image->pi_addr2line, "%p\n", (void *)addr); 1003203790Sfabient 1004203790Sfabient if (fgets(funcname, funcname_len, image->pi_addr2line) == NULL) { 1005203790Sfabient warnx("WARNING: addr2line function name read error"); 1006203790Sfabient return (0); 1007203790Sfabient } 1008203790Sfabient sep = strchr(funcname, '\n'); 1009203790Sfabient if (sep != NULL) 1010203790Sfabient *sep = '\0'; 1011203790Sfabient 1012203790Sfabient if (fgets(sourcefile, sourcefile_len, image->pi_addr2line) == NULL) { 1013203790Sfabient warnx("WARNING: addr2line source file read error"); 1014203790Sfabient return (0); 1015203790Sfabient } 1016203790Sfabient sep = strchr(sourcefile, ':'); 1017203790Sfabient if (sep == NULL) { 1018203790Sfabient warnx("WARNING: addr2line source line separator missing"); 1019203790Sfabient return (0); 1020203790Sfabient } 1021203790Sfabient *sep = '\0'; 1022235977Sfabient l = atoi(sep+1); 1023235977Sfabient if (l == 0) 1024203790Sfabient return (0); 1025235977Sfabient *sourceline = l; 1026203790Sfabient return (1); 1027203790Sfabient} 1028203790Sfabient 1029203790Sfabient/* 1030147708Sjkoshy * Add a {pmcid,name} mapping. 1031147708Sjkoshy */ 1032147708Sjkoshy 1033147708Sjkoshystatic void 1034203790Sfabientpmcstat_pmcid_add(pmc_id_t pmcid, pmcstat_interned_string ps) 1035147708Sjkoshy{ 1036203790Sfabient struct pmcstat_pmcrecord *pr, *prm; 1037147708Sjkoshy 1038174396Sjkoshy /* Replace an existing name for the PMC. */ 1039203790Sfabient prm = NULL; 1040147708Sjkoshy LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 1041203790Sfabient if (pr->pr_pmcid == pmcid) { 1042203790Sfabient pr->pr_pmcname = ps; 1043203790Sfabient return; 1044203790Sfabient } else if (pr->pr_pmcname == ps) 1045203790Sfabient prm = pr; 1046147708Sjkoshy 1047174396Sjkoshy /* 1048203790Sfabient * Otherwise, allocate a new descriptor and call the 1049203790Sfabient * plugins hook. 1050174396Sjkoshy */ 1051147708Sjkoshy if ((pr = malloc(sizeof(*pr))) == NULL) 1052147708Sjkoshy err(EX_OSERR, "ERROR: Cannot allocate pmc record"); 1053147708Sjkoshy 1054147708Sjkoshy pr->pr_pmcid = pmcid; 1055157144Sjkoshy pr->pr_pmcname = ps; 1056203790Sfabient pr->pr_pmcin = pmcstat_npmcs++; 1057206090Sfabient pr->pr_samples = 0; 1058206090Sfabient pr->pr_dubious_frames = 0; 1059203790Sfabient pr->pr_merge = prm == NULL ? pr : prm; 1060203790Sfabient 1061147708Sjkoshy LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next); 1062147708Sjkoshy 1063203790Sfabient if (plugins[args.pa_pplugin].pl_newpmc != NULL) 1064203790Sfabient plugins[args.pa_pplugin].pl_newpmc(ps, pr); 1065203790Sfabient if (plugins[args.pa_plugin].pl_newpmc != NULL) 1066203790Sfabient plugins[args.pa_plugin].pl_newpmc(ps, pr); 1067147708Sjkoshy} 1068147708Sjkoshy 1069147708Sjkoshy/* 1070157144Sjkoshy * Given a pmcid in use, find its human-readable name. 1071147708Sjkoshy */ 1072147708Sjkoshy 1073203790Sfabientconst char * 1074147708Sjkoshypmcstat_pmcid_to_name(pmc_id_t pmcid) 1075147708Sjkoshy{ 1076147708Sjkoshy struct pmcstat_pmcrecord *pr; 1077147708Sjkoshy 1078147708Sjkoshy LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 1079147708Sjkoshy if (pr->pr_pmcid == pmcid) 1080157144Sjkoshy return (pmcstat_string_unintern(pr->pr_pmcname)); 1081147708Sjkoshy 1082203790Sfabient return NULL; 1083203790Sfabient} 1084147708Sjkoshy 1085203790Sfabient/* 1086203790Sfabient * Convert PMC index to name. 1087203790Sfabient */ 1088147708Sjkoshy 1089203790Sfabientconst char * 1090203790Sfabientpmcstat_pmcindex_to_name(int pmcin) 1091203790Sfabient{ 1092203790Sfabient struct pmcstat_pmcrecord *pr; 1093147708Sjkoshy 1094203790Sfabient LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 1095203790Sfabient if (pr->pr_pmcin == pmcin) 1096203790Sfabient return pmcstat_string_unintern(pr->pr_pmcname); 1097203790Sfabient 1098203790Sfabient return NULL; 1099147708Sjkoshy} 1100147708Sjkoshy 1101147708Sjkoshy/* 1102203790Sfabient * Return PMC record with given index. 1103203790Sfabient */ 1104203790Sfabient 1105203790Sfabientstruct pmcstat_pmcrecord * 1106203790Sfabientpmcstat_pmcindex_to_pmcr(int pmcin) 1107203790Sfabient{ 1108203790Sfabient struct pmcstat_pmcrecord *pr; 1109203790Sfabient 1110203790Sfabient LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 1111203790Sfabient if (pr->pr_pmcin == pmcin) 1112203790Sfabient return pr; 1113203790Sfabient 1114203790Sfabient return NULL; 1115203790Sfabient} 1116203790Sfabient 1117203790Sfabient/* 1118203790Sfabient * Get PMC record by id, apply merge policy. 1119203790Sfabient */ 1120203790Sfabient 1121203790Sfabientstatic struct pmcstat_pmcrecord * 1122203790Sfabientpmcstat_lookup_pmcid(pmc_id_t pmcid) 1123203790Sfabient{ 1124203790Sfabient struct pmcstat_pmcrecord *pr; 1125203790Sfabient 1126203790Sfabient LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) { 1127203790Sfabient if (pr->pr_pmcid == pmcid) { 1128203790Sfabient if (pmcstat_mergepmc) 1129203790Sfabient return pr->pr_merge; 1130203790Sfabient return pr; 1131203790Sfabient } 1132203790Sfabient } 1133203790Sfabient 1134203790Sfabient return NULL; 1135203790Sfabient} 1136203790Sfabient 1137203790Sfabient/* 1138157144Sjkoshy * Associate an AOUT image with a process. 1139147708Sjkoshy */ 1140147708Sjkoshy 1141147708Sjkoshystatic void 1142157144Sjkoshypmcstat_process_aout_exec(struct pmcstat_process *pp, 1143203790Sfabient struct pmcstat_image *image, uintfptr_t entryaddr) 1144147708Sjkoshy{ 1145157144Sjkoshy (void) pp; 1146157144Sjkoshy (void) image; 1147157144Sjkoshy (void) entryaddr; 1148157144Sjkoshy /* TODO Implement a.out handling */ 1149157144Sjkoshy} 1150157144Sjkoshy 1151157144Sjkoshy/* 1152157144Sjkoshy * Associate an ELF image with a process. 1153157144Sjkoshy */ 1154157144Sjkoshy 1155157144Sjkoshystatic void 1156157144Sjkoshypmcstat_process_elf_exec(struct pmcstat_process *pp, 1157203790Sfabient struct pmcstat_image *image, uintfptr_t entryaddr) 1158157144Sjkoshy{ 1159147708Sjkoshy uintmax_t libstart; 1160157144Sjkoshy struct pmcstat_image *rtldimage; 1161147708Sjkoshy 1162157144Sjkoshy assert(image->pi_type == PMCSTAT_IMAGE_ELF32 || 1163157144Sjkoshy image->pi_type == PMCSTAT_IMAGE_ELF64); 1164147708Sjkoshy 1165150139Sjkoshy /* Create a map entry for the base executable. */ 1166157144Sjkoshy pmcstat_image_link(pp, image, image->pi_vaddr); 1167147708Sjkoshy 1168150139Sjkoshy /* 1169174396Sjkoshy * For dynamically linked executables we need to determine 1170174396Sjkoshy * where the dynamic linker was mapped to for this process, 1171174396Sjkoshy * Subsequent executable objects that are mapped in by the 1172174396Sjkoshy * dynamic linker will be tracked by log events of type 1173174396Sjkoshy * PMCLOG_TYPE_MAP_IN. 1174150139Sjkoshy */ 1175157144Sjkoshy 1176150139Sjkoshy if (image->pi_isdynamic) { 1177147708Sjkoshy 1178150139Sjkoshy /* 1179150139Sjkoshy * The runtime loader gets loaded just after the maximum 1180150139Sjkoshy * possible heap address. Like so: 1181150139Sjkoshy * 1182150139Sjkoshy * [ TEXT DATA BSS HEAP -->*RTLD SHLIBS <--STACK] 1183150139Sjkoshy * ^ ^ 1184150139Sjkoshy * 0 VM_MAXUSER_ADDRESS 1185157144Sjkoshy 1186150139Sjkoshy * 1187150139Sjkoshy * The exact address where the loader gets mapped in 1188150139Sjkoshy * will vary according to the size of the executable 1189150139Sjkoshy * and the limits on the size of the process'es data 1190150139Sjkoshy * segment at the time of exec(). The entry address 1191150139Sjkoshy * recorded at process exec time corresponds to the 1192150139Sjkoshy * 'start' address inside the dynamic linker. From 1193150139Sjkoshy * this we can figure out the address where the 1194150139Sjkoshy * runtime loader's file object had been mapped to. 1195150139Sjkoshy */ 1196203790Sfabient rtldimage = pmcstat_image_from_path(image->pi_dynlinkerpath, 0); 1197157144Sjkoshy if (rtldimage == NULL) { 1198157144Sjkoshy warnx("WARNING: Cannot find image for \"%s\".", 1199157144Sjkoshy pmcstat_string_unintern(image->pi_dynlinkerpath)); 1200157144Sjkoshy pmcstat_stats.ps_exec_errors++; 1201157144Sjkoshy return; 1202157144Sjkoshy } 1203157144Sjkoshy 1204150139Sjkoshy if (rtldimage->pi_type == PMCSTAT_IMAGE_UNKNOWN) 1205203790Sfabient pmcstat_image_get_elf_params(rtldimage); 1206147708Sjkoshy 1207157144Sjkoshy if (rtldimage->pi_type != PMCSTAT_IMAGE_ELF32 && 1208157144Sjkoshy rtldimage->pi_type != PMCSTAT_IMAGE_ELF64) { 1209157144Sjkoshy warnx("WARNING: rtld not an ELF object \"%s\".", 1210157144Sjkoshy pmcstat_string_unintern(image->pi_dynlinkerpath)); 1211157144Sjkoshy return; 1212147708Sjkoshy } 1213147708Sjkoshy 1214157144Sjkoshy libstart = entryaddr - rtldimage->pi_entry; 1215157144Sjkoshy pmcstat_image_link(pp, rtldimage, libstart); 1216147708Sjkoshy } 1217147708Sjkoshy} 1218147708Sjkoshy 1219147708Sjkoshy/* 1220147708Sjkoshy * Find the process descriptor corresponding to a PID. If 'allocate' 1221147708Sjkoshy * is zero, we return a NULL if a pid descriptor could not be found or 1222147708Sjkoshy * a process descriptor process. If 'allocate' is non-zero, then we 1223147708Sjkoshy * will attempt to allocate a fresh process descriptor. Zombie 1224147708Sjkoshy * process descriptors are only removed if a fresh allocation for the 1225147708Sjkoshy * same PID is requested. 1226147708Sjkoshy */ 1227147708Sjkoshy 1228147708Sjkoshystatic struct pmcstat_process * 1229147708Sjkoshypmcstat_process_lookup(pid_t pid, int allocate) 1230147708Sjkoshy{ 1231147708Sjkoshy uint32_t hash; 1232147708Sjkoshy struct pmcstat_pcmap *ppm, *ppmtmp; 1233147708Sjkoshy struct pmcstat_process *pp, *pptmp; 1234147708Sjkoshy 1235147708Sjkoshy hash = (uint32_t) pid & PMCSTAT_HASH_MASK; /* simplicity wins */ 1236147708Sjkoshy 1237147708Sjkoshy LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[hash], pp_next, pptmp) 1238237970Sobrien if (pp->pp_pid == pid) { 1239237970Sobrien /* Found a descriptor, check and process zombies */ 1240237970Sobrien if (allocate && pp->pp_isactive == 0) { 1241237970Sobrien /* remove maps */ 1242237970Sobrien TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, 1243237970Sobrien ppmtmp) { 1244237970Sobrien TAILQ_REMOVE(&pp->pp_map, ppm, 1245237970Sobrien ppm_next); 1246237970Sobrien free(ppm); 1247237970Sobrien } 1248237970Sobrien /* remove process entry */ 1249237970Sobrien LIST_REMOVE(pp, pp_next); 1250237970Sobrien free(pp); 1251237970Sobrien break; 1252237970Sobrien } 1253237970Sobrien return (pp); 1254237970Sobrien } 1255147708Sjkoshy 1256147708Sjkoshy if (!allocate) 1257157144Sjkoshy return (NULL); 1258147708Sjkoshy 1259147708Sjkoshy if ((pp = malloc(sizeof(*pp))) == NULL) 1260147708Sjkoshy err(EX_OSERR, "ERROR: Cannot allocate pid descriptor"); 1261147708Sjkoshy 1262147708Sjkoshy pp->pp_pid = pid; 1263147708Sjkoshy pp->pp_isactive = 1; 1264147708Sjkoshy 1265147708Sjkoshy TAILQ_INIT(&pp->pp_map); 1266147708Sjkoshy 1267147708Sjkoshy LIST_INSERT_HEAD(&pmcstat_process_hash[hash], pp, pp_next); 1268157144Sjkoshy return (pp); 1269147708Sjkoshy} 1270147708Sjkoshy 1271147708Sjkoshy/* 1272147708Sjkoshy * Associate an image and a process. 1273147708Sjkoshy */ 1274147708Sjkoshy 1275147708Sjkoshystatic void 1276157144Sjkoshypmcstat_process_exec(struct pmcstat_process *pp, 1277203790Sfabient pmcstat_interned_string path, uintfptr_t entryaddr) 1278147708Sjkoshy{ 1279147708Sjkoshy struct pmcstat_image *image; 1280147708Sjkoshy 1281157144Sjkoshy if ((image = pmcstat_image_from_path(path, 0)) == NULL) { 1282157144Sjkoshy pmcstat_stats.ps_exec_errors++; 1283147708Sjkoshy return; 1284157144Sjkoshy } 1285147708Sjkoshy 1286147708Sjkoshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 1287203790Sfabient pmcstat_image_determine_type(image); 1288147708Sjkoshy 1289157144Sjkoshy assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN); 1290157144Sjkoshy 1291157144Sjkoshy switch (image->pi_type) { 1292157144Sjkoshy case PMCSTAT_IMAGE_ELF32: 1293157144Sjkoshy case PMCSTAT_IMAGE_ELF64: 1294157144Sjkoshy pmcstat_stats.ps_exec_elf++; 1295203790Sfabient pmcstat_process_elf_exec(pp, image, entryaddr); 1296147708Sjkoshy break; 1297147708Sjkoshy 1298147708Sjkoshy case PMCSTAT_IMAGE_AOUT: 1299157144Sjkoshy pmcstat_stats.ps_exec_aout++; 1300203790Sfabient pmcstat_process_aout_exec(pp, image, entryaddr); 1301147708Sjkoshy break; 1302147708Sjkoshy 1303157144Sjkoshy case PMCSTAT_IMAGE_INDETERMINABLE: 1304157144Sjkoshy pmcstat_stats.ps_exec_indeterminable++; 1305157144Sjkoshy break; 1306157144Sjkoshy 1307147708Sjkoshy default: 1308237969Sobrien err(EX_SOFTWARE, 1309237969Sobrien "ERROR: Unsupported executable type for \"%s\"", 1310237969Sobrien pmcstat_string_unintern(path)); 1311147708Sjkoshy } 1312147708Sjkoshy} 1313147708Sjkoshy 1314147708Sjkoshy 1315150139Sjkoshy/* 1316150139Sjkoshy * Find the map entry associated with process 'p' at PC value 'pc'. 1317150139Sjkoshy */ 1318147708Sjkoshy 1319203790Sfabientstruct pmcstat_pcmap * 1320150139Sjkoshypmcstat_process_find_map(struct pmcstat_process *p, uintfptr_t pc) 1321150139Sjkoshy{ 1322150139Sjkoshy struct pmcstat_pcmap *ppm; 1323150139Sjkoshy 1324157144Sjkoshy TAILQ_FOREACH(ppm, &p->pp_map, ppm_next) { 1325157144Sjkoshy if (pc >= ppm->ppm_lowpc && pc < ppm->ppm_highpc) 1326157144Sjkoshy return (ppm); 1327157144Sjkoshy if (pc < ppm->ppm_lowpc) 1328157144Sjkoshy return (NULL); 1329157144Sjkoshy } 1330150139Sjkoshy 1331157144Sjkoshy return (NULL); 1332150139Sjkoshy} 1333150139Sjkoshy 1334174396Sjkoshy/* 1335174396Sjkoshy * Convert a hwpmc(4) log to profile information. A system-wide 1336174396Sjkoshy * callgraph is generated if FLAG_DO_CALLGRAPHS is set. gmon.out 1337174396Sjkoshy * files usable by gprof(1) are created if FLAG_DO_GPROF is set. 1338174396Sjkoshy */ 1339174396Sjkoshystatic int 1340203790Sfabientpmcstat_analyze_log(void) 1341174396Sjkoshy{ 1342174396Sjkoshy uint32_t cpu, cpuflags; 1343203790Sfabient uintfptr_t pc; 1344157144Sjkoshy pid_t pid; 1345157144Sjkoshy struct pmcstat_image *image; 1346147708Sjkoshy struct pmcstat_process *pp, *ppnew; 1347147708Sjkoshy struct pmcstat_pcmap *ppm, *ppmtmp; 1348147708Sjkoshy struct pmclog_ev ev; 1349203790Sfabient struct pmcstat_pmcrecord *pmcr; 1350157144Sjkoshy pmcstat_interned_string image_path; 1351147708Sjkoshy 1352203790Sfabient assert(args.pa_flags & FLAG_DO_ANALYSIS); 1353174396Sjkoshy 1354174396Sjkoshy if (elf_version(EV_CURRENT) == EV_NONE) 1355174396Sjkoshy err(EX_UNAVAILABLE, "Elf library intialization failed"); 1356174396Sjkoshy 1357203790Sfabient while (pmclog_read(args.pa_logparser, &ev) == 0) { 1358147708Sjkoshy assert(ev.pl_state == PMCLOG_OK); 1359147708Sjkoshy 1360147708Sjkoshy switch (ev.pl_type) { 1361157144Sjkoshy case PMCLOG_TYPE_INITIALIZE: 1362157144Sjkoshy if ((ev.pl_u.pl_i.pl_version & 0xFF000000) != 1363203790Sfabient PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0) 1364237969Sobrien warnx( 1365237969Sobrien"WARNING: Log version 0x%x does not match compiled version 0x%x.", 1366237969Sobrien ev.pl_u.pl_i.pl_version, PMC_VERSION_MAJOR); 1367157144Sjkoshy break; 1368174396Sjkoshy 1369157144Sjkoshy case PMCLOG_TYPE_MAP_IN: 1370147708Sjkoshy /* 1371147708Sjkoshy * Introduce an address range mapping for a 1372157144Sjkoshy * userland process or the kernel (pid == -1). 1373157144Sjkoshy * 1374157144Sjkoshy * We always allocate a process descriptor so 1375157144Sjkoshy * that subsequent samples seen for this 1376157144Sjkoshy * address range are mapped to the current 1377157144Sjkoshy * object being mapped in. 1378147708Sjkoshy */ 1379157144Sjkoshy pid = ev.pl_u.pl_mi.pl_pid; 1380157144Sjkoshy if (pid == -1) 1381157144Sjkoshy pp = pmcstat_kernproc; 1382157144Sjkoshy else 1383157144Sjkoshy pp = pmcstat_process_lookup(pid, 1384157144Sjkoshy PMCSTAT_ALLOCATE); 1385157144Sjkoshy 1386157144Sjkoshy assert(pp != NULL); 1387157144Sjkoshy 1388157144Sjkoshy image_path = pmcstat_string_intern(ev.pl_u.pl_mi. 1389157144Sjkoshy pl_pathname); 1390157144Sjkoshy image = pmcstat_image_from_path(image_path, pid == -1); 1391157144Sjkoshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 1392203790Sfabient pmcstat_image_determine_type(image); 1393157144Sjkoshy if (image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE) 1394157144Sjkoshy pmcstat_image_link(pp, image, 1395157144Sjkoshy ev.pl_u.pl_mi.pl_start); 1396147708Sjkoshy break; 1397147708Sjkoshy 1398157144Sjkoshy case PMCLOG_TYPE_MAP_OUT: 1399157144Sjkoshy /* 1400157144Sjkoshy * Remove an address map. 1401157144Sjkoshy */ 1402157144Sjkoshy pid = ev.pl_u.pl_mo.pl_pid; 1403157144Sjkoshy if (pid == -1) 1404157144Sjkoshy pp = pmcstat_kernproc; 1405157144Sjkoshy else 1406157144Sjkoshy pp = pmcstat_process_lookup(pid, 0); 1407157144Sjkoshy 1408157144Sjkoshy if (pp == NULL) /* unknown process */ 1409157144Sjkoshy break; 1410157144Sjkoshy 1411157144Sjkoshy pmcstat_image_unmap(pp, ev.pl_u.pl_mo.pl_start, 1412157144Sjkoshy ev.pl_u.pl_mo.pl_end); 1413157144Sjkoshy break; 1414157144Sjkoshy 1415147708Sjkoshy case PMCLOG_TYPE_PCSAMPLE: 1416174396Sjkoshy /* 1417174396Sjkoshy * Note: the `PCSAMPLE' log entry is not 1418174396Sjkoshy * generated by hpwmc(4) after version 2. 1419174396Sjkoshy */ 1420147708Sjkoshy 1421147708Sjkoshy /* 1422147708Sjkoshy * We bring in the gmon file for the image 1423147708Sjkoshy * currently associated with the PMC & pid 1424147708Sjkoshy * pair and increment the appropriate entry 1425147708Sjkoshy * bin inside this. 1426147708Sjkoshy */ 1427157144Sjkoshy pmcstat_stats.ps_samples_total++; 1428210794Sfabient ps_samples_period++; 1429157144Sjkoshy 1430147708Sjkoshy pc = ev.pl_u.pl_s.pl_pc; 1431157144Sjkoshy pp = pmcstat_process_lookup(ev.pl_u.pl_s.pl_pid, 1432157144Sjkoshy PMCSTAT_ALLOCATE); 1433147708Sjkoshy 1434203790Sfabient /* Get PMC record. */ 1435203790Sfabient pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_s.pl_pmcid); 1436203790Sfabient assert(pmcr != NULL); 1437206090Sfabient pmcr->pr_samples++; 1438147708Sjkoshy 1439203790Sfabient /* 1440203790Sfabient * Call the plugins processing 1441203790Sfabient * TODO: move pmcstat_process_find_map inside plugins 1442203790Sfabient */ 1443203790Sfabient 1444203790Sfabient if (plugins[args.pa_pplugin].pl_process != NULL) 1445203790Sfabient plugins[args.pa_pplugin].pl_process( 1446203790Sfabient pp, pmcr, 1, &pc, 1447203790Sfabient pmcstat_process_find_map(pp, pc) != NULL, 0); 1448203790Sfabient plugins[args.pa_plugin].pl_process( 1449203790Sfabient pp, pmcr, 1, &pc, 1450203790Sfabient pmcstat_process_find_map(pp, pc) != NULL, 0); 1451147708Sjkoshy break; 1452147708Sjkoshy 1453174396Sjkoshy case PMCLOG_TYPE_CALLCHAIN: 1454174396Sjkoshy pmcstat_stats.ps_samples_total++; 1455210794Sfabient ps_samples_period++; 1456174396Sjkoshy 1457174396Sjkoshy cpuflags = ev.pl_u.pl_cc.pl_cpuflags; 1458174396Sjkoshy cpu = PMC_CALLCHAIN_CPUFLAGS_TO_CPU(cpuflags); 1459174396Sjkoshy 1460174396Sjkoshy /* Filter on the CPU id. */ 1461224698Sattilio if (!CPU_ISSET(cpu, &(args.pa_cpumask))) { 1462174396Sjkoshy pmcstat_stats.ps_samples_skipped++; 1463174396Sjkoshy break; 1464174396Sjkoshy } 1465174396Sjkoshy 1466174396Sjkoshy pp = pmcstat_process_lookup(ev.pl_u.pl_cc.pl_pid, 1467174396Sjkoshy PMCSTAT_ALLOCATE); 1468174396Sjkoshy 1469203790Sfabient /* Get PMC record. */ 1470203790Sfabient pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_cc.pl_pmcid); 1471203790Sfabient assert(pmcr != NULL); 1472206090Sfabient pmcr->pr_samples++; 1473174396Sjkoshy 1474203790Sfabient /* 1475203790Sfabient * Call the plugins processing 1476203790Sfabient */ 1477174396Sjkoshy 1478203790Sfabient if (plugins[args.pa_pplugin].pl_process != NULL) 1479203790Sfabient plugins[args.pa_pplugin].pl_process( 1480203790Sfabient pp, pmcr, 1481203790Sfabient ev.pl_u.pl_cc.pl_npc, 1482203790Sfabient ev.pl_u.pl_cc.pl_pc, 1483203790Sfabient PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags), 1484203790Sfabient cpu); 1485203790Sfabient plugins[args.pa_plugin].pl_process( 1486203790Sfabient pp, pmcr, 1487203790Sfabient ev.pl_u.pl_cc.pl_npc, 1488203790Sfabient ev.pl_u.pl_cc.pl_pc, 1489203790Sfabient PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags), 1490203790Sfabient cpu); 1491174396Sjkoshy break; 1492174396Sjkoshy 1493147708Sjkoshy case PMCLOG_TYPE_PMCALLOCATE: 1494147708Sjkoshy /* 1495147708Sjkoshy * Record the association pmc id between this 1496147708Sjkoshy * PMC and its name. 1497147708Sjkoshy */ 1498147708Sjkoshy pmcstat_pmcid_add(ev.pl_u.pl_a.pl_pmcid, 1499203790Sfabient pmcstat_string_intern(ev.pl_u.pl_a.pl_evname)); 1500147708Sjkoshy break; 1501147708Sjkoshy 1502236238Sfabient case PMCLOG_TYPE_PMCALLOCATEDYN: 1503236238Sfabient /* 1504236238Sfabient * Record the association pmc id between this 1505236238Sfabient * PMC and its name. 1506236238Sfabient */ 1507236238Sfabient pmcstat_pmcid_add(ev.pl_u.pl_ad.pl_pmcid, 1508236238Sfabient pmcstat_string_intern(ev.pl_u.pl_ad.pl_evname)); 1509236238Sfabient break; 1510236238Sfabient 1511147708Sjkoshy case PMCLOG_TYPE_PROCEXEC: 1512147708Sjkoshy 1513147708Sjkoshy /* 1514147708Sjkoshy * Change the executable image associated with 1515147708Sjkoshy * a process. 1516147708Sjkoshy */ 1517157144Sjkoshy pp = pmcstat_process_lookup(ev.pl_u.pl_x.pl_pid, 1518157144Sjkoshy PMCSTAT_ALLOCATE); 1519147708Sjkoshy 1520147708Sjkoshy /* delete the current process map */ 1521147708Sjkoshy TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) { 1522147708Sjkoshy TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next); 1523147708Sjkoshy free(ppm); 1524147708Sjkoshy } 1525147708Sjkoshy 1526157144Sjkoshy /* associate this process image */ 1527147708Sjkoshy image_path = pmcstat_string_intern( 1528147708Sjkoshy ev.pl_u.pl_x.pl_pathname); 1529157144Sjkoshy assert(image_path != NULL); 1530150139Sjkoshy pmcstat_process_exec(pp, image_path, 1531203790Sfabient ev.pl_u.pl_x.pl_entryaddr); 1532147708Sjkoshy break; 1533147708Sjkoshy 1534147708Sjkoshy case PMCLOG_TYPE_PROCEXIT: 1535147708Sjkoshy 1536147708Sjkoshy /* 1537147708Sjkoshy * Due to the way the log is generated, the 1538147708Sjkoshy * last few samples corresponding to a process 1539147708Sjkoshy * may appear in the log after the process 1540147708Sjkoshy * exit event is recorded. Thus we keep the 1541147708Sjkoshy * process' descriptor and associated data 1542147708Sjkoshy * structures around, but mark the process as 1543147708Sjkoshy * having exited. 1544147708Sjkoshy */ 1545147708Sjkoshy pp = pmcstat_process_lookup(ev.pl_u.pl_e.pl_pid, 0); 1546147708Sjkoshy if (pp == NULL) 1547147708Sjkoshy break; 1548157144Sjkoshy pp->pp_isactive = 0; /* mark as a zombie */ 1549147708Sjkoshy break; 1550147708Sjkoshy 1551147708Sjkoshy case PMCLOG_TYPE_SYSEXIT: 1552147708Sjkoshy pp = pmcstat_process_lookup(ev.pl_u.pl_se.pl_pid, 0); 1553147708Sjkoshy if (pp == NULL) 1554147708Sjkoshy break; 1555147708Sjkoshy pp->pp_isactive = 0; /* make a zombie */ 1556147708Sjkoshy break; 1557147708Sjkoshy 1558147708Sjkoshy case PMCLOG_TYPE_PROCFORK: 1559147708Sjkoshy 1560147708Sjkoshy /* 1561157144Sjkoshy * Allocate a process descriptor for the new 1562157144Sjkoshy * (child) process. 1563147708Sjkoshy */ 1564157144Sjkoshy ppnew = 1565157144Sjkoshy pmcstat_process_lookup(ev.pl_u.pl_f.pl_newpid, 1566157144Sjkoshy PMCSTAT_ALLOCATE); 1567157144Sjkoshy 1568157144Sjkoshy /* 1569157144Sjkoshy * If we had been tracking the parent, clone 1570157144Sjkoshy * its address maps. 1571157144Sjkoshy */ 1572147708Sjkoshy pp = pmcstat_process_lookup(ev.pl_u.pl_f.pl_oldpid, 0); 1573147708Sjkoshy if (pp == NULL) 1574147708Sjkoshy break; 1575147708Sjkoshy TAILQ_FOREACH(ppm, &pp->pp_map, ppm_next) 1576147708Sjkoshy pmcstat_image_link(ppnew, ppm->ppm_image, 1577157144Sjkoshy ppm->ppm_lowpc); 1578147708Sjkoshy break; 1579147708Sjkoshy 1580147708Sjkoshy default: /* other types of entries are not relevant */ 1581147708Sjkoshy break; 1582147708Sjkoshy } 1583147708Sjkoshy } 1584147708Sjkoshy 1585147708Sjkoshy if (ev.pl_state == PMCLOG_EOF) 1586157144Sjkoshy return (PMCSTAT_FINISHED); 1587147708Sjkoshy else if (ev.pl_state == PMCLOG_REQUIRE_DATA) 1588157144Sjkoshy return (PMCSTAT_RUNNING); 1589147708Sjkoshy 1590237969Sobrien err(EX_DATAERR, 1591237969Sobrien "ERROR: event parsing failed (record %jd, offset 0x%jx)", 1592237969Sobrien (uintmax_t) ev.pl_count + 1, ev.pl_offset); 1593147708Sjkoshy} 1594147708Sjkoshy 1595147708Sjkoshy/* 1596147708Sjkoshy * Print log entries as text. 1597147708Sjkoshy */ 1598147708Sjkoshy 1599157144Sjkoshystatic int 1600203790Sfabientpmcstat_print_log(void) 1601147708Sjkoshy{ 1602147708Sjkoshy struct pmclog_ev ev; 1603174396Sjkoshy uint32_t npc; 1604147708Sjkoshy 1605203790Sfabient while (pmclog_read(args.pa_logparser, &ev) == 0) { 1606147708Sjkoshy assert(ev.pl_state == PMCLOG_OK); 1607147708Sjkoshy switch (ev.pl_type) { 1608174396Sjkoshy case PMCLOG_TYPE_CALLCHAIN: 1609203790Sfabient PMCSTAT_PRINT_ENTRY("callchain", 1610174396Sjkoshy "%d 0x%x %d %d %c", ev.pl_u.pl_cc.pl_pid, 1611174396Sjkoshy ev.pl_u.pl_cc.pl_pmcid, 1612174396Sjkoshy PMC_CALLCHAIN_CPUFLAGS_TO_CPU(ev.pl_u.pl_cc. \ 1613174396Sjkoshy pl_cpuflags), ev.pl_u.pl_cc.pl_npc, 1614174396Sjkoshy PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(ev.pl_u.pl_cc.\ 1615174396Sjkoshy pl_cpuflags) ? 'u' : 's'); 1616174396Sjkoshy for (npc = 0; npc < ev.pl_u.pl_cc.pl_npc; npc++) 1617203790Sfabient PMCSTAT_PRINT_ENTRY("...", "%p", 1618174396Sjkoshy (void *) ev.pl_u.pl_cc.pl_pc[npc]); 1619174396Sjkoshy break; 1620147708Sjkoshy case PMCLOG_TYPE_CLOSELOG: 1621203790Sfabient PMCSTAT_PRINT_ENTRY("closelog",); 1622147708Sjkoshy break; 1623147708Sjkoshy case PMCLOG_TYPE_DROPNOTIFY: 1624203790Sfabient PMCSTAT_PRINT_ENTRY("drop",); 1625147708Sjkoshy break; 1626147708Sjkoshy case PMCLOG_TYPE_INITIALIZE: 1627203790Sfabient PMCSTAT_PRINT_ENTRY("initlog","0x%x \"%s\"", 1628147708Sjkoshy ev.pl_u.pl_i.pl_version, 1629147708Sjkoshy pmc_name_of_cputype(ev.pl_u.pl_i.pl_arch)); 1630174209Sjkoshy if ((ev.pl_u.pl_i.pl_version & 0xFF000000) != 1631203790Sfabient PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0) 1632237969Sobrien warnx( 1633237969Sobrien"WARNING: Log version 0x%x != expected version 0x%x.", 1634237969Sobrien ev.pl_u.pl_i.pl_version, PMC_VERSION); 1635147708Sjkoshy break; 1636157144Sjkoshy case PMCLOG_TYPE_MAP_IN: 1637203790Sfabient PMCSTAT_PRINT_ENTRY("map-in","%d %p \"%s\"", 1638157144Sjkoshy ev.pl_u.pl_mi.pl_pid, 1639157144Sjkoshy (void *) ev.pl_u.pl_mi.pl_start, 1640157144Sjkoshy ev.pl_u.pl_mi.pl_pathname); 1641147708Sjkoshy break; 1642157144Sjkoshy case PMCLOG_TYPE_MAP_OUT: 1643203790Sfabient PMCSTAT_PRINT_ENTRY("map-out","%d %p %p", 1644157144Sjkoshy ev.pl_u.pl_mo.pl_pid, 1645157144Sjkoshy (void *) ev.pl_u.pl_mo.pl_start, 1646157144Sjkoshy (void *) ev.pl_u.pl_mo.pl_end); 1647157144Sjkoshy break; 1648147708Sjkoshy case PMCLOG_TYPE_PCSAMPLE: 1649203790Sfabient PMCSTAT_PRINT_ENTRY("sample","0x%x %d %p %c", 1650147708Sjkoshy ev.pl_u.pl_s.pl_pmcid, 1651147708Sjkoshy ev.pl_u.pl_s.pl_pid, 1652147708Sjkoshy (void *) ev.pl_u.pl_s.pl_pc, 1653147708Sjkoshy ev.pl_u.pl_s.pl_usermode ? 'u' : 's'); 1654147708Sjkoshy break; 1655147708Sjkoshy case PMCLOG_TYPE_PMCALLOCATE: 1656203790Sfabient PMCSTAT_PRINT_ENTRY("allocate","0x%x \"%s\" 0x%x", 1657147708Sjkoshy ev.pl_u.pl_a.pl_pmcid, 1658147708Sjkoshy ev.pl_u.pl_a.pl_evname, 1659147708Sjkoshy ev.pl_u.pl_a.pl_flags); 1660147708Sjkoshy break; 1661236238Sfabient case PMCLOG_TYPE_PMCALLOCATEDYN: 1662236238Sfabient PMCSTAT_PRINT_ENTRY("allocatedyn","0x%x \"%s\" 0x%x", 1663236238Sfabient ev.pl_u.pl_ad.pl_pmcid, 1664236238Sfabient ev.pl_u.pl_ad.pl_evname, 1665236238Sfabient ev.pl_u.pl_ad.pl_flags); 1666236238Sfabient break; 1667147708Sjkoshy case PMCLOG_TYPE_PMCATTACH: 1668203790Sfabient PMCSTAT_PRINT_ENTRY("attach","0x%x %d \"%s\"", 1669147708Sjkoshy ev.pl_u.pl_t.pl_pmcid, 1670147708Sjkoshy ev.pl_u.pl_t.pl_pid, 1671147708Sjkoshy ev.pl_u.pl_t.pl_pathname); 1672147708Sjkoshy break; 1673147708Sjkoshy case PMCLOG_TYPE_PMCDETACH: 1674203790Sfabient PMCSTAT_PRINT_ENTRY("detach","0x%x %d", 1675147708Sjkoshy ev.pl_u.pl_d.pl_pmcid, 1676147708Sjkoshy ev.pl_u.pl_d.pl_pid); 1677147708Sjkoshy break; 1678147708Sjkoshy case PMCLOG_TYPE_PROCCSW: 1679203790Sfabient PMCSTAT_PRINT_ENTRY("cswval","0x%x %d %jd", 1680147708Sjkoshy ev.pl_u.pl_c.pl_pmcid, 1681147708Sjkoshy ev.pl_u.pl_c.pl_pid, 1682147708Sjkoshy ev.pl_u.pl_c.pl_value); 1683147708Sjkoshy break; 1684147708Sjkoshy case PMCLOG_TYPE_PROCEXEC: 1685203790Sfabient PMCSTAT_PRINT_ENTRY("exec","0x%x %d %p \"%s\"", 1686147708Sjkoshy ev.pl_u.pl_x.pl_pmcid, 1687147708Sjkoshy ev.pl_u.pl_x.pl_pid, 1688147708Sjkoshy (void *) ev.pl_u.pl_x.pl_entryaddr, 1689147708Sjkoshy ev.pl_u.pl_x.pl_pathname); 1690147708Sjkoshy break; 1691147708Sjkoshy case PMCLOG_TYPE_PROCEXIT: 1692203790Sfabient PMCSTAT_PRINT_ENTRY("exitval","0x%x %d %jd", 1693147708Sjkoshy ev.pl_u.pl_e.pl_pmcid, 1694147708Sjkoshy ev.pl_u.pl_e.pl_pid, 1695147708Sjkoshy ev.pl_u.pl_e.pl_value); 1696147708Sjkoshy break; 1697147708Sjkoshy case PMCLOG_TYPE_PROCFORK: 1698203790Sfabient PMCSTAT_PRINT_ENTRY("fork","%d %d", 1699147708Sjkoshy ev.pl_u.pl_f.pl_oldpid, 1700147708Sjkoshy ev.pl_u.pl_f.pl_newpid); 1701147708Sjkoshy break; 1702147708Sjkoshy case PMCLOG_TYPE_USERDATA: 1703203790Sfabient PMCSTAT_PRINT_ENTRY("userdata","0x%x", 1704147708Sjkoshy ev.pl_u.pl_u.pl_userdata); 1705147708Sjkoshy break; 1706147708Sjkoshy case PMCLOG_TYPE_SYSEXIT: 1707203790Sfabient PMCSTAT_PRINT_ENTRY("exit","%d", 1708147708Sjkoshy ev.pl_u.pl_se.pl_pid); 1709147708Sjkoshy break; 1710147708Sjkoshy default: 1711203790Sfabient fprintf(args.pa_printfile, "unknown event (type %d).\n", 1712147708Sjkoshy ev.pl_type); 1713147708Sjkoshy } 1714147708Sjkoshy } 1715147708Sjkoshy 1716147708Sjkoshy if (ev.pl_state == PMCLOG_EOF) 1717157144Sjkoshy return (PMCSTAT_FINISHED); 1718147708Sjkoshy else if (ev.pl_state == PMCLOG_REQUIRE_DATA) 1719157144Sjkoshy return (PMCSTAT_RUNNING); 1720147708Sjkoshy 1721237969Sobrien errx(EX_DATAERR, 1722237969Sobrien "ERROR: event parsing failed (record %jd, offset 0x%jx).", 1723147708Sjkoshy (uintmax_t) ev.pl_count + 1, ev.pl_offset); 1724147708Sjkoshy /*NOTREACHED*/ 1725147708Sjkoshy} 1726147708Sjkoshy 1727147708Sjkoshy/* 1728157144Sjkoshy * Public Interfaces. 1729157144Sjkoshy */ 1730157144Sjkoshy 1731157144Sjkoshy/* 1732157144Sjkoshy * Close a logfile, after first flushing all in-module queued data. 1733157144Sjkoshy */ 1734157144Sjkoshy 1735157144Sjkoshyint 1736203790Sfabientpmcstat_close_log(void) 1737157144Sjkoshy{ 1738210794Sfabient /* If a local logfile is configured ask the kernel to stop 1739210794Sfabient * and flush data. Kernel will close the file when data is flushed 1740210794Sfabient * so keep the status to EXITING. 1741210794Sfabient */ 1742210794Sfabient if (args.pa_logfd != -1) { 1743229392Sfabient if (pmc_close_logfile() < 0) 1744210794Sfabient err(EX_OSERR, "ERROR: logging failed"); 1745210794Sfabient } 1746210794Sfabient 1747203790Sfabient return (args.pa_flags & FLAG_HAS_PIPE ? PMCSTAT_EXITING : 1748157144Sjkoshy PMCSTAT_FINISHED); 1749157144Sjkoshy} 1750157144Sjkoshy 1751157144Sjkoshy 1752157144Sjkoshy 1753157144Sjkoshy/* 1754157144Sjkoshy * Open a log file, for reading or writing. 1755157144Sjkoshy * 1756157144Sjkoshy * The function returns the fd of a successfully opened log or -1 in 1757157144Sjkoshy * case of failure. 1758157144Sjkoshy */ 1759157144Sjkoshy 1760157144Sjkoshyint 1761157144Sjkoshypmcstat_open_log(const char *path, int mode) 1762157144Sjkoshy{ 1763210794Sfabient int error, fd, cfd; 1764157406Sjkoshy size_t hlen; 1765157406Sjkoshy const char *p, *errstr; 1766157406Sjkoshy struct addrinfo hints, *res, *res0; 1767157406Sjkoshy char hostname[MAXHOSTNAMELEN]; 1768157144Sjkoshy 1769157406Sjkoshy errstr = NULL; 1770157406Sjkoshy fd = -1; 1771157406Sjkoshy 1772157144Sjkoshy /* 1773157144Sjkoshy * If 'path' is "-" then open one of stdin or stdout depending 1774157406Sjkoshy * on the value of 'mode'. 1775174396Sjkoshy * 1776157406Sjkoshy * If 'path' contains a ':' and does not start with a '/' or '.', 1777157406Sjkoshy * and is being opened for writing, treat it as a "host:port" 1778157406Sjkoshy * specification and open a network socket. 1779157406Sjkoshy * 1780157406Sjkoshy * Otherwise, treat 'path' as a file name and open that. 1781157144Sjkoshy */ 1782157144Sjkoshy if (path[0] == '-' && path[1] == '\0') 1783157144Sjkoshy fd = (mode == PMCSTAT_OPEN_FOR_READ) ? 0 : 1; 1784210794Sfabient else if (path[0] != '/' && 1785157406Sjkoshy path[0] != '.' && strchr(path, ':') != NULL) { 1786157406Sjkoshy 1787157406Sjkoshy p = strrchr(path, ':'); 1788157406Sjkoshy hlen = p - path; 1789157406Sjkoshy if (p == path || hlen >= sizeof(hostname)) { 1790157406Sjkoshy errstr = strerror(EINVAL); 1791157406Sjkoshy goto done; 1792157406Sjkoshy } 1793157406Sjkoshy 1794157406Sjkoshy assert(hlen < sizeof(hostname)); 1795157406Sjkoshy (void) strncpy(hostname, path, hlen); 1796157406Sjkoshy hostname[hlen] = '\0'; 1797157406Sjkoshy 1798157406Sjkoshy (void) memset(&hints, 0, sizeof(hints)); 1799157406Sjkoshy hints.ai_family = AF_UNSPEC; 1800157406Sjkoshy hints.ai_socktype = SOCK_STREAM; 1801157406Sjkoshy if ((error = getaddrinfo(hostname, p+1, &hints, &res0)) != 0) { 1802157406Sjkoshy errstr = gai_strerror(error); 1803157406Sjkoshy goto done; 1804157406Sjkoshy } 1805157406Sjkoshy 1806157406Sjkoshy fd = -1; 1807157406Sjkoshy for (res = res0; res; res = res->ai_next) { 1808157406Sjkoshy if ((fd = socket(res->ai_family, res->ai_socktype, 1809157406Sjkoshy res->ai_protocol)) < 0) { 1810157406Sjkoshy errstr = strerror(errno); 1811157406Sjkoshy continue; 1812157406Sjkoshy } 1813210794Sfabient if (mode == PMCSTAT_OPEN_FOR_READ) { 1814210794Sfabient if (bind(fd, res->ai_addr, res->ai_addrlen) < 0) { 1815210794Sfabient errstr = strerror(errno); 1816210794Sfabient (void) close(fd); 1817210794Sfabient fd = -1; 1818210794Sfabient continue; 1819210794Sfabient } 1820210794Sfabient listen(fd, 1); 1821210794Sfabient cfd = accept(fd, NULL, NULL); 1822157406Sjkoshy (void) close(fd); 1823210794Sfabient if (cfd < 0) { 1824210794Sfabient errstr = strerror(errno); 1825210794Sfabient fd = -1; 1826210794Sfabient break; 1827210794Sfabient } 1828210794Sfabient fd = cfd; 1829210794Sfabient } else { 1830210794Sfabient if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) { 1831210794Sfabient errstr = strerror(errno); 1832210794Sfabient (void) close(fd); 1833210794Sfabient fd = -1; 1834210794Sfabient continue; 1835210794Sfabient } 1836157406Sjkoshy } 1837157406Sjkoshy errstr = NULL; 1838157406Sjkoshy break; 1839157406Sjkoshy } 1840157406Sjkoshy freeaddrinfo(res0); 1841157406Sjkoshy 1842157406Sjkoshy } else if ((fd = open(path, mode == PMCSTAT_OPEN_FOR_READ ? 1843157144Sjkoshy O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC), 1844157406Sjkoshy S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) 1845157406Sjkoshy errstr = strerror(errno); 1846157144Sjkoshy 1847174396Sjkoshy done: 1848157406Sjkoshy if (errstr) 1849157406Sjkoshy errx(EX_OSERR, "ERROR: Cannot open \"%s\" for %s: %s.", path, 1850157406Sjkoshy (mode == PMCSTAT_OPEN_FOR_READ ? "reading" : "writing"), 1851157406Sjkoshy errstr); 1852157406Sjkoshy 1853157144Sjkoshy return (fd); 1854157144Sjkoshy} 1855157144Sjkoshy 1856157144Sjkoshy/* 1857147708Sjkoshy * Process a log file in offline analysis mode. 1858147708Sjkoshy */ 1859147708Sjkoshy 1860147863Sjkoshyint 1861203790Sfabientpmcstat_process_log(void) 1862147708Sjkoshy{ 1863147708Sjkoshy 1864147708Sjkoshy /* 1865174396Sjkoshy * If analysis has not been asked for, just print the log to 1866174396Sjkoshy * the current output file. 1867147708Sjkoshy */ 1868203790Sfabient if (args.pa_flags & FLAG_DO_PRINT) 1869203790Sfabient return (pmcstat_print_log()); 1870147708Sjkoshy else 1871203790Sfabient return (pmcstat_analyze_log()); 1872147708Sjkoshy} 1873147708Sjkoshy 1874157144Sjkoshy/* 1875203790Sfabient * Refresh top display. 1876203790Sfabient */ 1877203790Sfabient 1878203790Sfabientstatic void 1879203790Sfabientpmcstat_refresh_top(void) 1880203790Sfabient{ 1881206090Sfabient int v_attrs; 1882206090Sfabient float v; 1883203790Sfabient char pmcname[40]; 1884206090Sfabient struct pmcstat_pmcrecord *pmcpr; 1885203790Sfabient 1886203790Sfabient /* If in pause mode do not refresh display. */ 1887203790Sfabient if (pmcstat_pause) 1888203790Sfabient return; 1889203790Sfabient 1890205809Sfabient /* Wait until PMC pop in the log. */ 1891206090Sfabient pmcpr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter); 1892206090Sfabient if (pmcpr == NULL) 1893205809Sfabient return; 1894205809Sfabient 1895203790Sfabient /* Format PMC name. */ 1896203790Sfabient if (pmcstat_mergepmc) 1897206090Sfabient snprintf(pmcname, sizeof(pmcname), "[%s]", 1898206090Sfabient pmcstat_string_unintern(pmcpr->pr_pmcname)); 1899203790Sfabient else 1900203790Sfabient snprintf(pmcname, sizeof(pmcname), "%s.%d", 1901206090Sfabient pmcstat_string_unintern(pmcpr->pr_pmcname), 1902206090Sfabient pmcstat_pmcinfilter); 1903203790Sfabient 1904206090Sfabient /* Format samples count. */ 1905210794Sfabient if (ps_samples_period > 0) 1906210794Sfabient v = (pmcpr->pr_samples * 100.0) / ps_samples_period; 1907206090Sfabient else 1908206090Sfabient v = 0.; 1909206090Sfabient v_attrs = PMCSTAT_ATTRPERCENT(v); 1910206090Sfabient 1911203790Sfabient PMCSTAT_PRINTBEGIN(); 1912206090Sfabient PMCSTAT_PRINTW("PMC: %s Samples: %u ", 1913203790Sfabient pmcname, 1914206090Sfabient pmcpr->pr_samples); 1915206090Sfabient PMCSTAT_ATTRON(v_attrs); 1916206090Sfabient PMCSTAT_PRINTW("(%.1f%%) ", v); 1917206090Sfabient PMCSTAT_ATTROFF(v_attrs); 1918206090Sfabient PMCSTAT_PRINTW(", %u unresolved\n\n", 1919206090Sfabient pmcpr->pr_dubious_frames); 1920203790Sfabient if (plugins[args.pa_plugin].pl_topdisplay != NULL) 1921203790Sfabient plugins[args.pa_plugin].pl_topdisplay(); 1922203790Sfabient PMCSTAT_PRINTEND(); 1923203790Sfabient} 1924203790Sfabient 1925203790Sfabient/* 1926203790Sfabient * Find the next pmc index to display. 1927203790Sfabient */ 1928203790Sfabient 1929203790Sfabientstatic void 1930203790Sfabientpmcstat_changefilter(void) 1931203790Sfabient{ 1932203790Sfabient int pmcin; 1933203790Sfabient struct pmcstat_pmcrecord *pmcr; 1934203790Sfabient 1935203790Sfabient /* 1936203790Sfabient * Find the next merge target. 1937203790Sfabient */ 1938203790Sfabient if (pmcstat_mergepmc) { 1939203790Sfabient pmcin = pmcstat_pmcinfilter; 1940203790Sfabient 1941203790Sfabient do { 1942203790Sfabient pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter); 1943210794Sfabient if (pmcr == NULL || pmcr == pmcr->pr_merge) 1944203790Sfabient break; 1945203790Sfabient 1946203790Sfabient pmcstat_pmcinfilter++; 1947203790Sfabient if (pmcstat_pmcinfilter >= pmcstat_npmcs) 1948203790Sfabient pmcstat_pmcinfilter = 0; 1949203790Sfabient 1950203790Sfabient } while (pmcstat_pmcinfilter != pmcin); 1951203790Sfabient } 1952203790Sfabient} 1953203790Sfabient 1954203790Sfabient/* 1955203790Sfabient * Top mode keypress. 1956203790Sfabient */ 1957203790Sfabient 1958203790Sfabientint 1959203790Sfabientpmcstat_keypress_log(void) 1960203790Sfabient{ 1961203790Sfabient int c, ret = 0; 1962203790Sfabient WINDOW *w; 1963203790Sfabient 1964203790Sfabient w = newwin(1, 0, 1, 0); 1965203790Sfabient c = wgetch(w); 1966203790Sfabient wprintw(w, "Key: %c => ", c); 1967203790Sfabient switch (c) { 1968203790Sfabient case 'c': 1969203790Sfabient wprintw(w, "enter mode 'd' or 'a' => "); 1970203790Sfabient c = wgetch(w); 1971203790Sfabient if (c == 'd') { 1972203790Sfabient args.pa_topmode = PMCSTAT_TOP_DELTA; 1973203790Sfabient wprintw(w, "switching to delta mode"); 1974203790Sfabient } else { 1975203790Sfabient args.pa_topmode = PMCSTAT_TOP_ACCUM; 1976203790Sfabient wprintw(w, "switching to accumulation mode"); 1977203790Sfabient } 1978203790Sfabient break; 1979203790Sfabient case 'm': 1980203790Sfabient pmcstat_mergepmc = !pmcstat_mergepmc; 1981203790Sfabient /* 1982203790Sfabient * Changing merge state require data reset. 1983203790Sfabient */ 1984203790Sfabient if (plugins[args.pa_plugin].pl_shutdown != NULL) 1985203790Sfabient plugins[args.pa_plugin].pl_shutdown(NULL); 1986210794Sfabient pmcstat_stats_reset(0); 1987203790Sfabient if (plugins[args.pa_plugin].pl_init != NULL) 1988203790Sfabient plugins[args.pa_plugin].pl_init(); 1989203790Sfabient 1990203790Sfabient /* Update filter to be on a merge target. */ 1991203790Sfabient pmcstat_changefilter(); 1992203790Sfabient wprintw(w, "merge PMC %s", pmcstat_mergepmc ? "on" : "off"); 1993203790Sfabient break; 1994203790Sfabient case 'n': 1995203790Sfabient /* Close current plugin. */ 1996203790Sfabient if (plugins[args.pa_plugin].pl_shutdown != NULL) 1997203790Sfabient plugins[args.pa_plugin].pl_shutdown(NULL); 1998203790Sfabient 1999203790Sfabient /* Find next top display available. */ 2000203790Sfabient do { 2001203790Sfabient args.pa_plugin++; 2002203790Sfabient if (plugins[args.pa_plugin].pl_name == NULL) 2003203790Sfabient args.pa_plugin = 0; 2004203790Sfabient } while (plugins[args.pa_plugin].pl_topdisplay == NULL); 2005203790Sfabient 2006203790Sfabient /* Open new plugin. */ 2007210794Sfabient pmcstat_stats_reset(0); 2008203790Sfabient if (plugins[args.pa_plugin].pl_init != NULL) 2009203790Sfabient plugins[args.pa_plugin].pl_init(); 2010203790Sfabient wprintw(w, "switching to plugin %s", 2011203790Sfabient plugins[args.pa_plugin].pl_name); 2012203790Sfabient break; 2013203790Sfabient case 'p': 2014203790Sfabient pmcstat_pmcinfilter++; 2015203790Sfabient if (pmcstat_pmcinfilter >= pmcstat_npmcs) 2016203790Sfabient pmcstat_pmcinfilter = 0; 2017203790Sfabient pmcstat_changefilter(); 2018203790Sfabient wprintw(w, "switching to PMC %s.%d", 2019203790Sfabient pmcstat_pmcindex_to_name(pmcstat_pmcinfilter), 2020203790Sfabient pmcstat_pmcinfilter); 2021203790Sfabient break; 2022203790Sfabient case ' ': 2023203790Sfabient pmcstat_pause = !pmcstat_pause; 2024203790Sfabient if (pmcstat_pause) 2025203790Sfabient wprintw(w, "pause => press space again to continue"); 2026203790Sfabient break; 2027203790Sfabient case 'q': 2028203790Sfabient wprintw(w, "exiting..."); 2029203790Sfabient ret = 1; 2030208858Sfabient break; 2031203790Sfabient default: 2032203790Sfabient if (plugins[args.pa_plugin].pl_topkeypress != NULL) 2033203790Sfabient if (plugins[args.pa_plugin].pl_topkeypress(c, w)) 2034203790Sfabient ret = 1; 2035203790Sfabient } 2036203790Sfabient 2037203790Sfabient wrefresh(w); 2038203790Sfabient delwin(w); 2039203790Sfabient return ret; 2040203790Sfabient} 2041203790Sfabient 2042203790Sfabient 2043203790Sfabient/* 2044203790Sfabient * Top mode display. 2045203790Sfabient */ 2046203790Sfabient 2047203790Sfabientvoid 2048203790Sfabientpmcstat_display_log(void) 2049203790Sfabient{ 2050203790Sfabient 2051203790Sfabient pmcstat_refresh_top(); 2052203790Sfabient 2053203790Sfabient /* Reset everythings if delta mode. */ 2054203790Sfabient if (args.pa_topmode == PMCSTAT_TOP_DELTA) { 2055203790Sfabient if (plugins[args.pa_plugin].pl_shutdown != NULL) 2056203790Sfabient plugins[args.pa_plugin].pl_shutdown(NULL); 2057210794Sfabient pmcstat_stats_reset(0); 2058203790Sfabient if (plugins[args.pa_plugin].pl_init != NULL) 2059203790Sfabient plugins[args.pa_plugin].pl_init(); 2060203790Sfabient } 2061203790Sfabient 2062203790Sfabient} 2063203790Sfabient 2064203790Sfabient/* 2065203790Sfabient * Configure a plugins. 2066203790Sfabient */ 2067203790Sfabient 2068203790Sfabientvoid 2069203790Sfabientpmcstat_pluginconfigure_log(char *opt) 2070203790Sfabient{ 2071203790Sfabient 2072203790Sfabient if (strncmp(opt, "threshold=", 10) == 0) { 2073203790Sfabient pmcstat_threshold = atof(opt+10); 2074203790Sfabient } else { 2075203790Sfabient if (plugins[args.pa_plugin].pl_configure != NULL) { 2076203790Sfabient if (!plugins[args.pa_plugin].pl_configure(opt)) 2077203790Sfabient err(EX_USAGE, 2078203790Sfabient "ERROR: unknown option <%s>.", opt); 2079203790Sfabient } 2080203790Sfabient } 2081203790Sfabient} 2082203790Sfabient 2083203790Sfabient/* 2084157144Sjkoshy * Initialize module. 2085157144Sjkoshy */ 2086157144Sjkoshy 2087147708Sjkoshyvoid 2088203790Sfabientpmcstat_initialize_logging(void) 2089147708Sjkoshy{ 2090150139Sjkoshy int i; 2091147708Sjkoshy 2092147708Sjkoshy /* use a convenient format for 'ldd' output */ 2093153710Sjkoshy if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1","%o \"%p\" %x\n",1) != 0) 2094157144Sjkoshy err(EX_OSERR, "ERROR: Cannot setenv"); 2095147708Sjkoshy 2096147708Sjkoshy /* Initialize hash tables */ 2097157144Sjkoshy pmcstat_string_initialize(); 2098147708Sjkoshy for (i = 0; i < PMCSTAT_NHASH; i++) { 2099147708Sjkoshy LIST_INIT(&pmcstat_image_hash[i]); 2100147708Sjkoshy LIST_INIT(&pmcstat_process_hash[i]); 2101147708Sjkoshy } 2102147708Sjkoshy 2103157144Sjkoshy /* 2104157144Sjkoshy * Create a fake 'process' entry for the kernel with pid -1. 2105157144Sjkoshy * hwpmc(4) will subsequently inform us about where the kernel 2106157144Sjkoshy * and any loaded kernel modules are mapped. 2107157144Sjkoshy */ 2108157144Sjkoshy if ((pmcstat_kernproc = pmcstat_process_lookup((pid_t) -1, 2109157144Sjkoshy PMCSTAT_ALLOCATE)) == NULL) 2110157144Sjkoshy err(EX_OSERR, "ERROR: Cannot initialize logging"); 2111203790Sfabient 2112203790Sfabient /* PMC count. */ 2113203790Sfabient pmcstat_npmcs = 0; 2114203790Sfabient 2115203790Sfabient /* Merge PMC with same name. */ 2116203790Sfabient pmcstat_mergepmc = args.pa_mergepmc; 2117203790Sfabient 2118203790Sfabient /* 2119203790Sfabient * Initialize plugins 2120203790Sfabient */ 2121203790Sfabient 2122203790Sfabient if (plugins[args.pa_pplugin].pl_init != NULL) 2123203790Sfabient plugins[args.pa_pplugin].pl_init(); 2124203790Sfabient if (plugins[args.pa_plugin].pl_init != NULL) 2125203790Sfabient plugins[args.pa_plugin].pl_init(); 2126157144Sjkoshy} 2127147708Sjkoshy 2128157144Sjkoshy/* 2129157144Sjkoshy * Shutdown module. 2130157144Sjkoshy */ 2131147708Sjkoshy 2132147708Sjkoshyvoid 2133203790Sfabientpmcstat_shutdown_logging(void) 2134147708Sjkoshy{ 2135147708Sjkoshy int i; 2136157144Sjkoshy FILE *mf; 2137147708Sjkoshy struct pmcstat_image *pi, *pitmp; 2138147708Sjkoshy struct pmcstat_process *pp, *pptmp; 2139203790Sfabient struct pmcstat_pcmap *ppm, *ppmtmp; 2140147708Sjkoshy 2141157144Sjkoshy /* determine where to send the map file */ 2142157144Sjkoshy mf = NULL; 2143203790Sfabient if (args.pa_mapfilename != NULL) 2144203790Sfabient mf = (strcmp(args.pa_mapfilename, "-") == 0) ? 2145203790Sfabient args.pa_printfile : fopen(args.pa_mapfilename, "w"); 2146157144Sjkoshy 2147203790Sfabient if (mf == NULL && args.pa_flags & FLAG_DO_GPROF && 2148203790Sfabient args.pa_verbosity >= 2) 2149203790Sfabient mf = args.pa_printfile; 2150157144Sjkoshy 2151157144Sjkoshy if (mf) 2152157144Sjkoshy (void) fprintf(mf, "MAP:\n"); 2153157144Sjkoshy 2154174396Sjkoshy /* 2155203790Sfabient * Shutdown the plugins 2156174396Sjkoshy */ 2157157536Sjkoshy 2158203790Sfabient if (plugins[args.pa_plugin].pl_shutdown != NULL) 2159203790Sfabient plugins[args.pa_plugin].pl_shutdown(mf); 2160203790Sfabient if (plugins[args.pa_pplugin].pl_shutdown != NULL) 2161203790Sfabient plugins[args.pa_pplugin].pl_shutdown(mf); 2162157536Sjkoshy 2163174396Sjkoshy for (i = 0; i < PMCSTAT_NHASH; i++) { 2164203790Sfabient LIST_FOREACH_SAFE(pi, &pmcstat_image_hash[i], pi_next, 2165203790Sfabient pitmp) { 2166203790Sfabient if (plugins[args.pa_plugin].pl_shutdownimage != NULL) 2167203790Sfabient plugins[args.pa_plugin].pl_shutdownimage(pi); 2168203790Sfabient if (plugins[args.pa_pplugin].pl_shutdownimage != NULL) 2169203790Sfabient plugins[args.pa_pplugin].pl_shutdownimage(pi); 2170174396Sjkoshy 2171203790Sfabient free(pi->pi_symbols); 2172203790Sfabient if (pi->pi_addr2line != NULL) 2173203790Sfabient pclose(pi->pi_addr2line); 2174147708Sjkoshy LIST_REMOVE(pi, pi_next); 2175147708Sjkoshy free(pi); 2176147708Sjkoshy } 2177174396Sjkoshy 2178147708Sjkoshy LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[i], pp_next, 2179147708Sjkoshy pptmp) { 2180203790Sfabient TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) { 2181203790Sfabient TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next); 2182203790Sfabient free(ppm); 2183203790Sfabient } 2184147708Sjkoshy LIST_REMOVE(pp, pp_next); 2185147708Sjkoshy free(pp); 2186147708Sjkoshy } 2187147708Sjkoshy } 2188157144Sjkoshy 2189157144Sjkoshy pmcstat_string_shutdown(); 2190157144Sjkoshy 2191157144Sjkoshy /* 2192157144Sjkoshy * Print errors unless -q was specified. Print all statistics 2193157144Sjkoshy * if verbosity > 1. 2194157144Sjkoshy */ 2195203790Sfabient#define PRINT(N,V) do { \ 2196203790Sfabient if (pmcstat_stats.ps_##V || args.pa_verbosity >= 2) \ 2197203790Sfabient (void) fprintf(args.pa_printfile, " %-40s %d\n",\ 2198157144Sjkoshy N, pmcstat_stats.ps_##V); \ 2199157144Sjkoshy } while (0) 2200157144Sjkoshy 2201210794Sfabient if (args.pa_verbosity >= 1 && (args.pa_flags & FLAG_DO_ANALYSIS)) { 2202203790Sfabient (void) fprintf(args.pa_printfile, "CONVERSION STATISTICS:\n"); 2203203790Sfabient PRINT("#exec/a.out", exec_aout); 2204203790Sfabient PRINT("#exec/elf", exec_elf); 2205203790Sfabient PRINT("#exec/unknown", exec_indeterminable); 2206203790Sfabient PRINT("#exec handling errors", exec_errors); 2207203790Sfabient PRINT("#samples/total", samples_total); 2208203790Sfabient PRINT("#samples/unclaimed", samples_unknown_offset); 2209203790Sfabient PRINT("#samples/unknown-object", samples_indeterminable); 2210212176Sfabient PRINT("#samples/unknown-function", samples_unknown_function); 2211203790Sfabient PRINT("#callchain/dubious-frames", callchain_dubious_frames); 2212157144Sjkoshy } 2213157144Sjkoshy 2214157144Sjkoshy if (mf) 2215157144Sjkoshy (void) fclose(mf); 2216147708Sjkoshy} 2217