115139246SJoseph Koshy /*- 249874f6eSJoseph Koshy * Copyright (c) 2005-2006, Joseph Koshy 315139246SJoseph Koshy * All rights reserved. 415139246SJoseph Koshy * 515139246SJoseph Koshy * Redistribution and use in source and binary forms, with or without 615139246SJoseph Koshy * modification, are permitted provided that the following conditions 715139246SJoseph Koshy * are met: 815139246SJoseph Koshy * 1. Redistributions of source code must retain the above copyright 915139246SJoseph Koshy * notice, this list of conditions and the following disclaimer. 1015139246SJoseph Koshy * 2. Redistributions in binary form must reproduce the above copyright 1115139246SJoseph Koshy * notice, this list of conditions and the following disclaimer in the 1215139246SJoseph Koshy * documentation and/or other materials provided with the distribution. 1315139246SJoseph Koshy * 1415139246SJoseph Koshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1515139246SJoseph Koshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1615139246SJoseph Koshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1715139246SJoseph Koshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1815139246SJoseph Koshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1915139246SJoseph Koshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2015139246SJoseph Koshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2115139246SJoseph Koshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2215139246SJoseph Koshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2315139246SJoseph Koshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2415139246SJoseph Koshy * SUCH DAMAGE. 2515139246SJoseph Koshy */ 2615139246SJoseph Koshy 2749874f6eSJoseph Koshy /* 2849874f6eSJoseph Koshy * Transform a hwpmc(4) log into human readable form, and into 2949874f6eSJoseph Koshy * gprof(1) compatible profiles. 3049874f6eSJoseph Koshy */ 3149874f6eSJoseph Koshy 3215139246SJoseph Koshy #include <sys/cdefs.h> 3315139246SJoseph Koshy __FBSDID("$FreeBSD$"); 3415139246SJoseph Koshy 3515139246SJoseph Koshy #include <sys/param.h> 3615139246SJoseph Koshy #include <sys/endian.h> 3715139246SJoseph Koshy #include <sys/gmon.h> 3815139246SJoseph Koshy #include <sys/imgact_aout.h> 3915139246SJoseph Koshy #include <sys/imgact_elf.h> 4015139246SJoseph Koshy #include <sys/mman.h> 4115139246SJoseph Koshy #include <sys/pmc.h> 4215139246SJoseph Koshy #include <sys/queue.h> 43302cbb90SJoseph Koshy #include <sys/socket.h> 4415139246SJoseph Koshy #include <sys/stat.h> 4515139246SJoseph Koshy #include <sys/wait.h> 4615139246SJoseph Koshy 4715139246SJoseph Koshy #include <netinet/in.h> 4815139246SJoseph Koshy 4915139246SJoseph Koshy #include <assert.h> 5015139246SJoseph Koshy #include <err.h> 51302cbb90SJoseph Koshy #include <errno.h> 5215139246SJoseph Koshy #include <fcntl.h> 5315139246SJoseph Koshy #include <libgen.h> 5415139246SJoseph Koshy #include <limits.h> 55302cbb90SJoseph Koshy #include <netdb.h> 5615139246SJoseph Koshy #include <pmc.h> 5715139246SJoseph Koshy #include <pmclog.h> 5815139246SJoseph Koshy #include <sysexits.h> 5915139246SJoseph Koshy #include <stdint.h> 6015139246SJoseph Koshy #include <stdio.h> 6115139246SJoseph Koshy #include <stdlib.h> 6215139246SJoseph Koshy #include <string.h> 6315139246SJoseph Koshy #include <unistd.h> 6415139246SJoseph Koshy 6515139246SJoseph Koshy #include "pmcstat.h" 6615139246SJoseph Koshy 6715139246SJoseph Koshy #define min(A,B) ((A) < (B) ? (A) : (B)) 6815139246SJoseph Koshy #define max(A,B) ((A) > (B) ? (A) : (B)) 6915139246SJoseph Koshy 7015139246SJoseph Koshy /* 7149874f6eSJoseph Koshy * PUBLIC INTERFACES 7249874f6eSJoseph Koshy * 7349874f6eSJoseph Koshy * pmcstat_initialize_logging() initialize this module, called first 7449874f6eSJoseph Koshy * pmcstat_shutdown_logging() orderly shutdown, called last 7549874f6eSJoseph Koshy * pmcstat_open_log() open an eventlog for processing 7649874f6eSJoseph Koshy * pmcstat_process_log() print/convert an event log 7749874f6eSJoseph Koshy * pmcstat_close_log() finish processing an event log 7849874f6eSJoseph Koshy * 7949874f6eSJoseph Koshy * IMPLEMENTATION OF GMON OUTPUT 8049874f6eSJoseph Koshy * 8149874f6eSJoseph Koshy * We correlate each 'sample' seen in the event log back to an 8249874f6eSJoseph Koshy * executable object in the system. Executable objects include: 8349874f6eSJoseph Koshy * - program executables, 8449874f6eSJoseph Koshy * - shared libraries loaded by the runtime loader, 8549874f6eSJoseph Koshy * - dlopen()'ed objects loaded by the program, 8649874f6eSJoseph Koshy * - the runtime loader itself, 8749874f6eSJoseph Koshy * - the kernel and kernel modules. 8849874f6eSJoseph Koshy * 8949874f6eSJoseph Koshy * Each such executable object gets one 'gmon.out' profile, per PMC in 9049874f6eSJoseph Koshy * use. Creation of 'gmon.out' profiles is done lazily. The 9149874f6eSJoseph Koshy * 'gmon.out' profiles generated for a given sampling PMC are 9249874f6eSJoseph Koshy * aggregates of all the samples for that particular executable 9349874f6eSJoseph Koshy * object. 9449874f6eSJoseph Koshy * 9549874f6eSJoseph Koshy * Each process that we know about is treated as a set of regions that 9649874f6eSJoseph Koshy * map to executable objects. Processes are described by 9749874f6eSJoseph Koshy * 'pmcstat_process' structures. Executable objects are tracked by 9849874f6eSJoseph Koshy * 'pmcstat_image' structures. The kernel and kernel modules are 9949874f6eSJoseph Koshy * common to all processes (they reside at the same virtual addresses 10049874f6eSJoseph Koshy * for all processes). Individual processes can have their text 10149874f6eSJoseph Koshy * segments and shared libraries loaded at process-specific locations. 10249874f6eSJoseph Koshy * 10349874f6eSJoseph Koshy * A given executable object can be in use by multiple processes 10449874f6eSJoseph Koshy * (e.g., libc.so) and loaded at a different address in each. 10549874f6eSJoseph Koshy * pmcstat_pcmap structures track per-image mappings. 10649874f6eSJoseph Koshy * 10749874f6eSJoseph Koshy * The sample log could have samples from multiple PMCs; we 10849874f6eSJoseph Koshy * generate one 'gmon.out' profile per PMC. 10915139246SJoseph Koshy */ 11015139246SJoseph Koshy 11149874f6eSJoseph Koshy typedef const void *pmcstat_interned_string; 11215139246SJoseph Koshy 11315139246SJoseph Koshy /* 114a566429aSJoseph Koshy * 'pmcstat_pmcrecord' is a mapping from PMC ids to human-readable 11515139246SJoseph Koshy * names. 11615139246SJoseph Koshy */ 11715139246SJoseph Koshy 11815139246SJoseph Koshy struct pmcstat_pmcrecord { 11915139246SJoseph Koshy LIST_ENTRY(pmcstat_pmcrecord) pr_next; 12015139246SJoseph Koshy pmc_id_t pr_pmcid; 12149874f6eSJoseph Koshy pmcstat_interned_string pr_pmcname; 12215139246SJoseph Koshy }; 12315139246SJoseph Koshy 12415139246SJoseph Koshy static LIST_HEAD(,pmcstat_pmcrecord) pmcstat_pmcs = 12515139246SJoseph Koshy LIST_HEAD_INITIALIZER(&pmcstat_pmcs); 12615139246SJoseph Koshy 127a566429aSJoseph Koshy 128a566429aSJoseph Koshy /* 129a566429aSJoseph Koshy * struct pmcstat_gmonfile tracks a given 'gmon.out' file. These 130a566429aSJoseph Koshy * files are mmap()'ed in as needed. 131a566429aSJoseph Koshy */ 132a566429aSJoseph Koshy 13315139246SJoseph Koshy struct pmcstat_gmonfile { 13415139246SJoseph Koshy LIST_ENTRY(pmcstat_gmonfile) pgf_next; /* list of entries */ 13549874f6eSJoseph Koshy int pgf_overflow; /* whether a count overflowed */ 13615139246SJoseph Koshy pmc_id_t pgf_pmcid; /* id of the associated pmc */ 137a566429aSJoseph Koshy size_t pgf_nbuckets; /* #buckets in this gmon.out */ 13849874f6eSJoseph Koshy pmcstat_interned_string pgf_name; /* pathname of gmon.out file */ 13915139246SJoseph Koshy size_t pgf_ndatabytes; /* number of bytes mapped */ 14015139246SJoseph Koshy void *pgf_gmondata; /* pointer to mmap'ed data */ 14115139246SJoseph Koshy }; 14215139246SJoseph Koshy 14315139246SJoseph Koshy /* 14415139246SJoseph Koshy * A 'pmcstat_image' structure describes an executable program on 14549874f6eSJoseph Koshy * disk. 'pi_execpath' is a cookie representing the pathname of 14615139246SJoseph Koshy * the executable. 'pi_start' and 'pi_end' are the least and greatest 14715139246SJoseph Koshy * virtual addresses for the text segments in the executable. 14815139246SJoseph Koshy * 'pi_gmonlist' contains a linked list of gmon.out files associated 14915139246SJoseph Koshy * with this image. 15015139246SJoseph Koshy */ 15115139246SJoseph Koshy 15215139246SJoseph Koshy enum pmcstat_image_type { 15349874f6eSJoseph Koshy PMCSTAT_IMAGE_UNKNOWN = 0, /* never looked at the image */ 15449874f6eSJoseph Koshy PMCSTAT_IMAGE_INDETERMINABLE, /* can't tell what the image is */ 15549874f6eSJoseph Koshy PMCSTAT_IMAGE_ELF32, /* ELF 32 bit object */ 15649874f6eSJoseph Koshy PMCSTAT_IMAGE_ELF64, /* ELF 64 bit object */ 15749874f6eSJoseph Koshy PMCSTAT_IMAGE_AOUT /* AOUT object */ 15815139246SJoseph Koshy }; 15915139246SJoseph Koshy 16015139246SJoseph Koshy struct pmcstat_image { 16115139246SJoseph Koshy LIST_ENTRY(pmcstat_image) pi_next; /* hash link */ 16215139246SJoseph Koshy TAILQ_ENTRY(pmcstat_image) pi_lru; /* LRU list */ 16349874f6eSJoseph Koshy pmcstat_interned_string pi_execpath;/* cookie */ 16449874f6eSJoseph Koshy pmcstat_interned_string pi_samplename; /* sample path name */ 16515139246SJoseph Koshy 16615139246SJoseph Koshy enum pmcstat_image_type pi_type; /* executable type */ 16749874f6eSJoseph Koshy 16849874f6eSJoseph Koshy /* 16949874f6eSJoseph Koshy * Executables have pi_start and pi_end; these are zero 17049874f6eSJoseph Koshy * for shared libraries. 17149874f6eSJoseph Koshy */ 17215139246SJoseph Koshy uintfptr_t pi_start; /* start address (inclusive) */ 17315139246SJoseph Koshy uintfptr_t pi_end; /* end address (exclusive) */ 17460f918efSJoseph Koshy uintfptr_t pi_entry; /* entry address */ 17549874f6eSJoseph Koshy uintfptr_t pi_vaddr; /* virtual address where loaded */ 17649874f6eSJoseph Koshy int pi_isdynamic; /* whether a dynamic 17749874f6eSJoseph Koshy * object */ 17849874f6eSJoseph Koshy int pi_iskernelmodule; 17949874f6eSJoseph Koshy pmcstat_interned_string pi_dynlinkerpath; /* path in .interp */ 18015139246SJoseph Koshy 18149874f6eSJoseph Koshy /* 18249874f6eSJoseph Koshy * An image can be associated with one or more gmon.out files; 18349874f6eSJoseph Koshy * one per PMC. 18449874f6eSJoseph Koshy */ 18515139246SJoseph Koshy LIST_HEAD(,pmcstat_gmonfile) pi_gmlist; 18615139246SJoseph Koshy }; 18715139246SJoseph Koshy 18849874f6eSJoseph Koshy /* 18949874f6eSJoseph Koshy * All image descriptors are kept in a hash table. 19049874f6eSJoseph Koshy */ 19115139246SJoseph Koshy static LIST_HEAD(,pmcstat_image) pmcstat_image_hash[PMCSTAT_NHASH]; 19215139246SJoseph Koshy static TAILQ_HEAD(,pmcstat_image) pmcstat_image_lru = 19315139246SJoseph Koshy TAILQ_HEAD_INITIALIZER(pmcstat_image_lru); 19415139246SJoseph Koshy 19549874f6eSJoseph Koshy /* 19649874f6eSJoseph Koshy * A 'pmcstat_pcmap' structure maps a virtual address range to an 19749874f6eSJoseph Koshy * underlying 'pmcstat_image' descriptor. 19849874f6eSJoseph Koshy */ 19915139246SJoseph Koshy struct pmcstat_pcmap { 20015139246SJoseph Koshy TAILQ_ENTRY(pmcstat_pcmap) ppm_next; 20115139246SJoseph Koshy uintfptr_t ppm_lowpc; 20215139246SJoseph Koshy uintfptr_t ppm_highpc; 20315139246SJoseph Koshy struct pmcstat_image *ppm_image; 20415139246SJoseph Koshy }; 20515139246SJoseph Koshy 20615139246SJoseph Koshy /* 20749874f6eSJoseph Koshy * A 'pmcstat_process' structure models processes. Each process is 20849874f6eSJoseph Koshy * associated with a set of pmcstat_pcmap structures that map 20949874f6eSJoseph Koshy * addresses inside it to executable objects. This set is implemented 21049874f6eSJoseph Koshy * as a list, kept sorted in ascending order of mapped addresses. 21149874f6eSJoseph Koshy * 21249874f6eSJoseph Koshy * 'pp_pid' holds the pid of the process. When a process exits, the 21349874f6eSJoseph Koshy * 'pp_isactive' field is set to zero, but the process structure is 21449874f6eSJoseph Koshy * not immediately reclaimed because there may still be samples in the 21549874f6eSJoseph Koshy * log for this process. 21615139246SJoseph Koshy */ 21715139246SJoseph Koshy 21815139246SJoseph Koshy struct pmcstat_process { 21915139246SJoseph Koshy LIST_ENTRY(pmcstat_process) pp_next; /* hash-next */ 22015139246SJoseph Koshy pid_t pp_pid; /* associated pid */ 22115139246SJoseph Koshy int pp_isactive; /* whether active */ 22260f918efSJoseph Koshy uintfptr_t pp_entryaddr; /* entry address */ 22315139246SJoseph Koshy TAILQ_HEAD(,pmcstat_pcmap) pp_map; /* address range map */ 22415139246SJoseph Koshy }; 22515139246SJoseph Koshy 22649874f6eSJoseph Koshy #define PMCSTAT_ALLOCATE 1 22749874f6eSJoseph Koshy 22849874f6eSJoseph Koshy /* 22949874f6eSJoseph Koshy * All process descriptors are kept in a hash table. 23049874f6eSJoseph Koshy */ 23115139246SJoseph Koshy static LIST_HEAD(,pmcstat_process) pmcstat_process_hash[PMCSTAT_NHASH]; 23215139246SJoseph Koshy 23315139246SJoseph Koshy static struct pmcstat_process *pmcstat_kernproc; /* kernel 'process' */ 23415139246SJoseph Koshy 23549874f6eSJoseph Koshy /* Misc. statistics */ 23649874f6eSJoseph Koshy static struct pmcstat_stats { 23749874f6eSJoseph Koshy int ps_exec_aout; /* # a.out executables seen */ 23849874f6eSJoseph Koshy int ps_exec_elf; /* # elf executables seen */ 23949874f6eSJoseph Koshy int ps_exec_errors; /* # errors processing executables */ 24049874f6eSJoseph Koshy int ps_exec_indeterminable; /* # unknown executables seen */ 24149874f6eSJoseph Koshy int ps_samples_total; /* total number of samples processed */ 24249874f6eSJoseph Koshy int ps_samples_unknown_offset; /* #samples not in any map */ 24349874f6eSJoseph Koshy int ps_samples_indeterminable; /* #samples in indeterminable images */ 24449874f6eSJoseph Koshy } pmcstat_stats; 24549874f6eSJoseph Koshy 24615139246SJoseph Koshy /* 24715139246SJoseph Koshy * Prototypes 24815139246SJoseph Koshy */ 24915139246SJoseph Koshy 25015139246SJoseph Koshy static void pmcstat_gmon_create_file(struct pmcstat_gmonfile *_pgf, 25115139246SJoseph Koshy struct pmcstat_image *_image); 25249874f6eSJoseph Koshy static pmcstat_interned_string pmcstat_gmon_create_name(const char *_sd, 25315139246SJoseph Koshy struct pmcstat_image *_img, pmc_id_t _pmcid); 25415139246SJoseph Koshy static void pmcstat_gmon_map_file(struct pmcstat_gmonfile *_pgf); 25515139246SJoseph Koshy static void pmcstat_gmon_unmap_file(struct pmcstat_gmonfile *_pgf); 25615139246SJoseph Koshy 25749874f6eSJoseph Koshy static void pmcstat_image_determine_type(struct pmcstat_image *_image, 25849874f6eSJoseph Koshy struct pmcstat_args *_a); 25949874f6eSJoseph Koshy static struct pmcstat_image *pmcstat_image_from_path(pmcstat_interned_string 26049874f6eSJoseph Koshy _path, int _iskernelmodule); 26149874f6eSJoseph Koshy static void pmcstat_image_get_aout_params(struct pmcstat_image *_image, 26249874f6eSJoseph Koshy struct pmcstat_args *_a); 26349874f6eSJoseph Koshy static void pmcstat_image_get_elf_params(struct pmcstat_image *_image, 26449874f6eSJoseph Koshy struct pmcstat_args *_a); 26515139246SJoseph Koshy static void pmcstat_image_increment_bucket(struct pmcstat_pcmap *_pcm, 26615139246SJoseph Koshy uintfptr_t _pc, pmc_id_t _pmcid, struct pmcstat_args *_a); 26715139246SJoseph Koshy static void pmcstat_image_link(struct pmcstat_process *_pp, 26849874f6eSJoseph Koshy struct pmcstat_image *_i, uintfptr_t _lpc); 26915139246SJoseph Koshy 27049874f6eSJoseph Koshy static void pmcstat_pmcid_add(pmc_id_t _pmcid, 27149874f6eSJoseph Koshy pmcstat_interned_string _name, struct pmcstat_args *_a); 27215139246SJoseph Koshy static const char *pmcstat_pmcid_to_name(pmc_id_t _pmcid); 27315139246SJoseph Koshy 27449874f6eSJoseph Koshy static void pmcstat_process_aout_exec(struct pmcstat_process *_pp, 27549874f6eSJoseph Koshy struct pmcstat_image *_image, uintfptr_t _entryaddr, 27649874f6eSJoseph Koshy struct pmcstat_args *_a); 27749874f6eSJoseph Koshy static void pmcstat_process_elf_exec(struct pmcstat_process *_pp, 27849874f6eSJoseph Koshy struct pmcstat_image *_image, uintfptr_t _entryaddr, 27949874f6eSJoseph Koshy struct pmcstat_args *_a); 28060f918efSJoseph Koshy static void pmcstat_process_exec(struct pmcstat_process *_pp, 28149874f6eSJoseph Koshy pmcstat_interned_string _path, uintfptr_t _entryaddr, 28249874f6eSJoseph Koshy struct pmcstat_args *_ao); 28349874f6eSJoseph Koshy static struct pmcstat_process *pmcstat_process_lookup(pid_t _pid, 28449874f6eSJoseph Koshy int _allocate); 28515139246SJoseph Koshy static struct pmcstat_pcmap *pmcstat_process_find_map( 28615139246SJoseph Koshy struct pmcstat_process *_p, uintfptr_t _pc); 28715139246SJoseph Koshy 28815139246SJoseph Koshy static int pmcstat_string_compute_hash(const char *_string); 28949874f6eSJoseph Koshy static void pmcstat_string_initialize(void); 29049874f6eSJoseph Koshy static pmcstat_interned_string pmcstat_string_intern(const char *_s); 29149874f6eSJoseph Koshy static pmcstat_interned_string pmcstat_string_lookup(const char *_s); 29249874f6eSJoseph Koshy static int pmcstat_string_lookup_hash(pmcstat_interned_string _is); 29349874f6eSJoseph Koshy static void pmcstat_string_shutdown(void); 29449874f6eSJoseph Koshy static const char *pmcstat_string_unintern(pmcstat_interned_string _is); 29515139246SJoseph Koshy 29615139246SJoseph Koshy 29715139246SJoseph Koshy /* 29849874f6eSJoseph Koshy * A simple implementation of interned strings. Each interned string 29949874f6eSJoseph Koshy * is assigned a unique address, so that subsequent string compares 30049874f6eSJoseph Koshy * can be done by a simple pointer comparision instead of using 30149874f6eSJoseph Koshy * strcmp(). This speeds up hash table lookups and saves memory if 30249874f6eSJoseph Koshy * duplicate strings are the norm. 30349874f6eSJoseph Koshy */ 30449874f6eSJoseph Koshy struct pmcstat_string { 30549874f6eSJoseph Koshy LIST_ENTRY(pmcstat_string) ps_next; /* hash link */ 30649874f6eSJoseph Koshy int ps_len; 30749874f6eSJoseph Koshy int ps_hash; 30849874f6eSJoseph Koshy char *ps_string; 30949874f6eSJoseph Koshy }; 31049874f6eSJoseph Koshy 31149874f6eSJoseph Koshy static LIST_HEAD(,pmcstat_string) pmcstat_string_hash[PMCSTAT_NHASH]; 31249874f6eSJoseph Koshy 31349874f6eSJoseph Koshy /* 31449874f6eSJoseph Koshy * Compute a 'hash' value for a string. 31549874f6eSJoseph Koshy */ 31649874f6eSJoseph Koshy 31749874f6eSJoseph Koshy static int 31849874f6eSJoseph Koshy pmcstat_string_compute_hash(const char *s) 31949874f6eSJoseph Koshy { 32049874f6eSJoseph Koshy int hash; 32149874f6eSJoseph Koshy 32249874f6eSJoseph Koshy for (hash = 0; *s; s++) 32349874f6eSJoseph Koshy hash ^= *s; 32449874f6eSJoseph Koshy 32549874f6eSJoseph Koshy return (hash & PMCSTAT_HASH_MASK); 32649874f6eSJoseph Koshy } 32749874f6eSJoseph Koshy 32849874f6eSJoseph Koshy /* 32949874f6eSJoseph Koshy * Intern a copy of string 's', and return a pointer to the 33049874f6eSJoseph Koshy * interned structure. 33149874f6eSJoseph Koshy */ 33249874f6eSJoseph Koshy 33349874f6eSJoseph Koshy static pmcstat_interned_string 33449874f6eSJoseph Koshy pmcstat_string_intern(const char *s) 33549874f6eSJoseph Koshy { 33649874f6eSJoseph Koshy struct pmcstat_string *ps; 33749874f6eSJoseph Koshy const struct pmcstat_string *cps; 33849874f6eSJoseph Koshy int hash, len; 33949874f6eSJoseph Koshy 34049874f6eSJoseph Koshy if ((cps = pmcstat_string_lookup(s)) != NULL) 34149874f6eSJoseph Koshy return (cps); 34249874f6eSJoseph Koshy 34349874f6eSJoseph Koshy hash = pmcstat_string_compute_hash(s); 34449874f6eSJoseph Koshy len = strlen(s); 34549874f6eSJoseph Koshy 34649874f6eSJoseph Koshy if ((ps = malloc(sizeof(*ps))) == NULL) 34749874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Could not intern string"); 34849874f6eSJoseph Koshy ps->ps_len = len; 34949874f6eSJoseph Koshy ps->ps_hash = hash; 35049874f6eSJoseph Koshy ps->ps_string = strdup(s); 35149874f6eSJoseph Koshy LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next); 35249874f6eSJoseph Koshy return ((pmcstat_interned_string) ps); 35349874f6eSJoseph Koshy } 35449874f6eSJoseph Koshy 35549874f6eSJoseph Koshy static const char * 35649874f6eSJoseph Koshy pmcstat_string_unintern(pmcstat_interned_string str) 35749874f6eSJoseph Koshy { 35849874f6eSJoseph Koshy const char *s; 35949874f6eSJoseph Koshy 36049874f6eSJoseph Koshy s = ((const struct pmcstat_string *) str)->ps_string; 36149874f6eSJoseph Koshy return (s); 36249874f6eSJoseph Koshy } 36349874f6eSJoseph Koshy 36449874f6eSJoseph Koshy static pmcstat_interned_string 36549874f6eSJoseph Koshy pmcstat_string_lookup(const char *s) 36649874f6eSJoseph Koshy { 36749874f6eSJoseph Koshy struct pmcstat_string *ps; 36849874f6eSJoseph Koshy int hash, len; 36949874f6eSJoseph Koshy 37049874f6eSJoseph Koshy hash = pmcstat_string_compute_hash(s); 37149874f6eSJoseph Koshy len = strlen(s); 37249874f6eSJoseph Koshy 37349874f6eSJoseph Koshy LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next) 37449874f6eSJoseph Koshy if (ps->ps_len == len && ps->ps_hash == hash && 37549874f6eSJoseph Koshy strcmp(ps->ps_string, s) == 0) 37649874f6eSJoseph Koshy return (ps); 37749874f6eSJoseph Koshy return (NULL); 37849874f6eSJoseph Koshy } 37949874f6eSJoseph Koshy 38049874f6eSJoseph Koshy static int 38149874f6eSJoseph Koshy pmcstat_string_lookup_hash(pmcstat_interned_string s) 38249874f6eSJoseph Koshy { 38349874f6eSJoseph Koshy const struct pmcstat_string *ps; 38449874f6eSJoseph Koshy 38549874f6eSJoseph Koshy ps = (const struct pmcstat_string *) s; 38649874f6eSJoseph Koshy return (ps->ps_hash); 38749874f6eSJoseph Koshy } 38849874f6eSJoseph Koshy 38949874f6eSJoseph Koshy /* 39049874f6eSJoseph Koshy * Initialize the string interning facility. 39149874f6eSJoseph Koshy */ 39249874f6eSJoseph Koshy 39349874f6eSJoseph Koshy static void 39449874f6eSJoseph Koshy pmcstat_string_initialize(void) 39549874f6eSJoseph Koshy { 39649874f6eSJoseph Koshy int i; 39749874f6eSJoseph Koshy 39849874f6eSJoseph Koshy for (i = 0; i < PMCSTAT_NHASH; i++) 39949874f6eSJoseph Koshy LIST_INIT(&pmcstat_string_hash[i]); 40049874f6eSJoseph Koshy } 40149874f6eSJoseph Koshy 40249874f6eSJoseph Koshy /* 40349874f6eSJoseph Koshy * Destroy the string table, free'ing up space. 40449874f6eSJoseph Koshy */ 40549874f6eSJoseph Koshy 40649874f6eSJoseph Koshy static void 40749874f6eSJoseph Koshy pmcstat_string_shutdown(void) 40849874f6eSJoseph Koshy { 40949874f6eSJoseph Koshy int i; 41049874f6eSJoseph Koshy struct pmcstat_string *ps, *pstmp; 41149874f6eSJoseph Koshy 41249874f6eSJoseph Koshy for (i = 0; i < PMCSTAT_NHASH; i++) 41349874f6eSJoseph Koshy LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next, 41449874f6eSJoseph Koshy pstmp) { 41549874f6eSJoseph Koshy LIST_REMOVE(ps, ps_next); 41649874f6eSJoseph Koshy free(ps->ps_string); 41749874f6eSJoseph Koshy free(ps); 41849874f6eSJoseph Koshy } 41949874f6eSJoseph Koshy } 42049874f6eSJoseph Koshy 42149874f6eSJoseph Koshy /* 42215139246SJoseph Koshy * Create a gmon.out file and size it. 42315139246SJoseph Koshy */ 42415139246SJoseph Koshy 42515139246SJoseph Koshy static void 42615139246SJoseph Koshy pmcstat_gmon_create_file(struct pmcstat_gmonfile *pgf, 42715139246SJoseph Koshy struct pmcstat_image *image) 42815139246SJoseph Koshy { 42915139246SJoseph Koshy int fd; 43015139246SJoseph Koshy size_t count; 43115139246SJoseph Koshy struct gmonhdr gm; 43249874f6eSJoseph Koshy const char *pathname; 43315139246SJoseph Koshy char buffer[DEFAULT_BUFFER_SIZE]; 43415139246SJoseph Koshy 43549874f6eSJoseph Koshy pathname = pmcstat_string_unintern(pgf->pgf_name); 43649874f6eSJoseph Koshy if ((fd = open(pathname, O_RDWR|O_NOFOLLOW|O_CREAT, 43715139246SJoseph Koshy S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) 43849874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Cannot open \"%s\"", pathname); 43915139246SJoseph Koshy 44015139246SJoseph Koshy gm.lpc = image->pi_start; 44115139246SJoseph Koshy gm.hpc = image->pi_end; 442a566429aSJoseph Koshy gm.ncnt = (pgf->pgf_nbuckets * sizeof(HISTCOUNTER)) + 443a566429aSJoseph Koshy sizeof(struct gmonhdr); 44415139246SJoseph Koshy gm.version = GMONVERSION; 44515139246SJoseph Koshy gm.profrate = 0; /* use ticks */ 44615139246SJoseph Koshy gm.histcounter_type = 0; /* compatibility with moncontrol() */ 44715139246SJoseph Koshy gm.spare[0] = gm.spare[1] = 0; 44815139246SJoseph Koshy 44915139246SJoseph Koshy /* Write out the gmon header */ 45015139246SJoseph Koshy if (write(fd, &gm, sizeof(gm)) < 0) 45115139246SJoseph Koshy goto error; 45215139246SJoseph Koshy 45315139246SJoseph Koshy /* Zero fill the samples[] array */ 45415139246SJoseph Koshy (void) memset(buffer, 0, sizeof(buffer)); 45515139246SJoseph Koshy 45615139246SJoseph Koshy count = pgf->pgf_ndatabytes - sizeof(struct gmonhdr); 45715139246SJoseph Koshy while (count > sizeof(buffer)) { 45815139246SJoseph Koshy if (write(fd, &buffer, sizeof(buffer)) < 0) 45915139246SJoseph Koshy goto error; 46015139246SJoseph Koshy count -= sizeof(buffer); 46115139246SJoseph Koshy } 46215139246SJoseph Koshy 46315139246SJoseph Koshy if (write(fd, &buffer, count) < 0) 46415139246SJoseph Koshy goto error; 46515139246SJoseph Koshy 46649874f6eSJoseph Koshy /* TODO size the arc table */ 46749874f6eSJoseph Koshy 46815139246SJoseph Koshy (void) close(fd); 46915139246SJoseph Koshy 47015139246SJoseph Koshy return; 47115139246SJoseph Koshy 47215139246SJoseph Koshy error: 47349874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Cannot write \"%s\"", pathname); 47415139246SJoseph Koshy } 47515139246SJoseph Koshy 47649874f6eSJoseph Koshy /* 47749874f6eSJoseph Koshy * Determine the full pathname of a gmon.out file for a given 47849874f6eSJoseph Koshy * (image,pmcid) combination. Return the interned string. 47949874f6eSJoseph Koshy */ 48049874f6eSJoseph Koshy 48149874f6eSJoseph Koshy pmcstat_interned_string 48215139246SJoseph Koshy pmcstat_gmon_create_name(const char *samplesdir, struct pmcstat_image *image, 48315139246SJoseph Koshy pmc_id_t pmcid) 48415139246SJoseph Koshy { 48515139246SJoseph Koshy const char *pmcname; 48615139246SJoseph Koshy char fullpath[PATH_MAX]; 48715139246SJoseph Koshy 48815139246SJoseph Koshy pmcname = pmcstat_pmcid_to_name(pmcid); 48915139246SJoseph Koshy 49015139246SJoseph Koshy (void) snprintf(fullpath, sizeof(fullpath), 49149874f6eSJoseph Koshy "%s/%s/%s", samplesdir, pmcname, 49249874f6eSJoseph Koshy pmcstat_string_unintern(image->pi_samplename)); 49315139246SJoseph Koshy 49449874f6eSJoseph Koshy return (pmcstat_string_intern(fullpath)); 49515139246SJoseph Koshy } 49615139246SJoseph Koshy 49715139246SJoseph Koshy 49849874f6eSJoseph Koshy /* 49949874f6eSJoseph Koshy * Mmap in a gmon.out file for processing. 50049874f6eSJoseph Koshy */ 50149874f6eSJoseph Koshy 50215139246SJoseph Koshy static void 50315139246SJoseph Koshy pmcstat_gmon_map_file(struct pmcstat_gmonfile *pgf) 50415139246SJoseph Koshy { 50515139246SJoseph Koshy int fd; 50649874f6eSJoseph Koshy const char *pathname; 50749874f6eSJoseph Koshy 50849874f6eSJoseph Koshy pathname = pmcstat_string_unintern(pgf->pgf_name); 50915139246SJoseph Koshy 51015139246SJoseph Koshy /* the gmon.out file must already exist */ 51149874f6eSJoseph Koshy if ((fd = open(pathname, O_RDWR | O_NOFOLLOW, 0)) < 0) 51249874f6eSJoseph Koshy err(EX_OSERR, "ERROR: cannot open \"%s\"", pathname); 51315139246SJoseph Koshy 51415139246SJoseph Koshy pgf->pgf_gmondata = mmap(NULL, pgf->pgf_ndatabytes, 51515139246SJoseph Koshy PROT_READ|PROT_WRITE, MAP_NOSYNC|MAP_SHARED, fd, 0); 51615139246SJoseph Koshy 51715139246SJoseph Koshy if (pgf->pgf_gmondata == MAP_FAILED) 51849874f6eSJoseph Koshy err(EX_OSERR, "ERROR: cannot map \"%s\"", pathname); 51915139246SJoseph Koshy 52015139246SJoseph Koshy (void) close(fd); 52115139246SJoseph Koshy } 52215139246SJoseph Koshy 52315139246SJoseph Koshy /* 52449874f6eSJoseph Koshy * Unmap a gmon.out file after sync'ing its data to disk. 52515139246SJoseph Koshy */ 52615139246SJoseph Koshy 52715139246SJoseph Koshy static void 52815139246SJoseph Koshy pmcstat_gmon_unmap_file(struct pmcstat_gmonfile *pgf) 52915139246SJoseph Koshy { 53015139246SJoseph Koshy (void) msync(pgf->pgf_gmondata, pgf->pgf_ndatabytes, 53115139246SJoseph Koshy MS_SYNC); 53215139246SJoseph Koshy (void) munmap(pgf->pgf_gmondata, pgf->pgf_ndatabytes); 53315139246SJoseph Koshy pgf->pgf_gmondata = NULL; 53415139246SJoseph Koshy } 53515139246SJoseph Koshy 53649874f6eSJoseph Koshy /* 53749874f6eSJoseph Koshy * Determine whether a given executable image is an A.OUT object, and 53849874f6eSJoseph Koshy * if so, fill in its parameters from the text file. 53949874f6eSJoseph Koshy * Sets image->pi_type. 54049874f6eSJoseph Koshy */ 54149874f6eSJoseph Koshy 54215139246SJoseph Koshy static void 54349874f6eSJoseph Koshy pmcstat_image_get_aout_params(struct pmcstat_image *image, 54449874f6eSJoseph Koshy struct pmcstat_args *a) 54549874f6eSJoseph Koshy { 54649874f6eSJoseph Koshy int fd; 54749874f6eSJoseph Koshy ssize_t nbytes; 54849874f6eSJoseph Koshy struct exec ex; 54949874f6eSJoseph Koshy const char *path; 55049874f6eSJoseph Koshy char buffer[PATH_MAX]; 55149874f6eSJoseph Koshy 55249874f6eSJoseph Koshy path = pmcstat_string_unintern(image->pi_execpath); 55349874f6eSJoseph Koshy assert(path != NULL); 55449874f6eSJoseph Koshy 55549874f6eSJoseph Koshy if (image->pi_iskernelmodule) 55649874f6eSJoseph Koshy errx(EX_SOFTWARE, "ERROR: a.out kernel modules are " 55749874f6eSJoseph Koshy "unsupported \"%s\"", path); 55849874f6eSJoseph Koshy 55949874f6eSJoseph Koshy (void) snprintf(buffer, sizeof(buffer), "%s%s", 56049874f6eSJoseph Koshy a->pa_fsroot, path); 56149874f6eSJoseph Koshy 56249874f6eSJoseph Koshy if ((fd = open(buffer, O_RDONLY, 0)) < 0 || 56349874f6eSJoseph Koshy (nbytes = read(fd, &ex, sizeof(ex))) < 0) { 56449874f6eSJoseph Koshy warn("WARNING: Cannot determine type of \"%s\"", path); 56549874f6eSJoseph Koshy image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE; 56649874f6eSJoseph Koshy if (fd != -1) 56749874f6eSJoseph Koshy (void) close(fd); 56849874f6eSJoseph Koshy return; 56949874f6eSJoseph Koshy } 57049874f6eSJoseph Koshy 57149874f6eSJoseph Koshy (void) close(fd); 57249874f6eSJoseph Koshy 57349874f6eSJoseph Koshy if ((unsigned) nbytes != sizeof(ex) || 57449874f6eSJoseph Koshy N_BADMAG(ex)) 57549874f6eSJoseph Koshy return; 57649874f6eSJoseph Koshy 57749874f6eSJoseph Koshy image->pi_type = PMCSTAT_IMAGE_AOUT; 57849874f6eSJoseph Koshy 57949874f6eSJoseph Koshy /* TODO: the rest of a.out processing */ 58049874f6eSJoseph Koshy 58149874f6eSJoseph Koshy return; 58249874f6eSJoseph Koshy } 58349874f6eSJoseph Koshy 58449874f6eSJoseph Koshy /* 58549874f6eSJoseph Koshy * Examine an ELF file to determine the size of its text segment. 58649874f6eSJoseph Koshy * Sets image->pi_type if anything conclusive can be determined about 58749874f6eSJoseph Koshy * this image. 58849874f6eSJoseph Koshy */ 58949874f6eSJoseph Koshy 59049874f6eSJoseph Koshy static void 59149874f6eSJoseph Koshy pmcstat_image_get_elf_params(struct pmcstat_image *image, 59249874f6eSJoseph Koshy struct pmcstat_args *a) 59315139246SJoseph Koshy { 59415139246SJoseph Koshy int fd, i; 59549874f6eSJoseph Koshy const char *path; 59615139246SJoseph Koshy void *mapbase; 59715139246SJoseph Koshy uintfptr_t minva, maxva; 59815139246SJoseph Koshy const Elf_Ehdr *h; 59915139246SJoseph Koshy const Elf_Phdr *ph; 60015139246SJoseph Koshy const Elf_Shdr *sh; 601887a8d04SJoseph Koshy #if defined(__amd64__) 602887a8d04SJoseph Koshy const Elf32_Ehdr *h32; 603887a8d04SJoseph Koshy const Elf32_Phdr *ph32; 604887a8d04SJoseph Koshy const Elf32_Shdr *sh32; 605887a8d04SJoseph Koshy #endif 60649874f6eSJoseph Koshy enum pmcstat_image_type image_type; 60749874f6eSJoseph Koshy struct stat st; 60849874f6eSJoseph Koshy char buffer[PATH_MAX]; 60915139246SJoseph Koshy 61060f918efSJoseph Koshy assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN); 61160f918efSJoseph Koshy 61215139246SJoseph Koshy minva = ~(uintfptr_t) 0; 61315139246SJoseph Koshy maxva = (uintfptr_t) 0; 61449874f6eSJoseph Koshy path = pmcstat_string_unintern(image->pi_execpath); 61515139246SJoseph Koshy 61649874f6eSJoseph Koshy assert(path != NULL); 61715139246SJoseph Koshy 61849874f6eSJoseph Koshy /* 61949874f6eSJoseph Koshy * Look for kernel modules under FSROOT/KERNELPATH/NAME, 62049874f6eSJoseph Koshy * and user mode executable objects under FSROOT/PATHNAME. 62149874f6eSJoseph Koshy */ 62249874f6eSJoseph Koshy if (image->pi_iskernelmodule) 62349874f6eSJoseph Koshy (void) snprintf(buffer, sizeof(buffer), "%s%s/%s", 62449874f6eSJoseph Koshy a->pa_fsroot, a->pa_kernel, path); 62549874f6eSJoseph Koshy else 62649874f6eSJoseph Koshy (void) snprintf(buffer, sizeof(buffer), "%s%s", 62749874f6eSJoseph Koshy a->pa_fsroot, path); 62815139246SJoseph Koshy 62949874f6eSJoseph Koshy if ((fd = open(buffer, O_RDONLY, 0)) < 0 || 63049874f6eSJoseph Koshy fstat(fd, &st) < 0 || 63149874f6eSJoseph Koshy (mapbase = mmap(0, st.st_size, PROT_READ, MAP_SHARED, 63249874f6eSJoseph Koshy fd, 0)) == MAP_FAILED) { 63349874f6eSJoseph Koshy warn("WARNING: Cannot determine type of \"%s\"", buffer); 63449874f6eSJoseph Koshy image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE; 63549874f6eSJoseph Koshy if (fd != -1) 63649874f6eSJoseph Koshy (void) close(fd); 63749874f6eSJoseph Koshy return; 63849874f6eSJoseph Koshy } 63915139246SJoseph Koshy 64015139246SJoseph Koshy (void) close(fd); 64115139246SJoseph Koshy 64249874f6eSJoseph Koshy /* Punt on non-ELF objects */ 64315139246SJoseph Koshy h = (const Elf_Ehdr *) mapbase; 64415139246SJoseph Koshy if (!IS_ELF(*h)) 64549874f6eSJoseph Koshy return; 64615139246SJoseph Koshy 64749874f6eSJoseph Koshy /* 64849874f6eSJoseph Koshy * We only handle executable ELF objects and kernel 64949874f6eSJoseph Koshy * modules. 65049874f6eSJoseph Koshy */ 65149874f6eSJoseph Koshy if (h->e_type != ET_EXEC && h->e_type != ET_DYN && 65249874f6eSJoseph Koshy !(image->pi_iskernelmodule && h->e_type == ET_REL)) 65349874f6eSJoseph Koshy return; 65449874f6eSJoseph Koshy 65549874f6eSJoseph Koshy image->pi_isdynamic = 0; 65649874f6eSJoseph Koshy image->pi_dynlinkerpath = NULL; 65749874f6eSJoseph Koshy image->pi_vaddr = 0; 65815139246SJoseph Koshy 659887a8d04SJoseph Koshy #define GET_VA(H, SH, MINVA, MAXVA) do { \ 660887a8d04SJoseph Koshy for (i = 0; i < (H)->e_shnum; i++) \ 661887a8d04SJoseph Koshy if ((SH)[i].sh_flags & SHF_EXECINSTR) { \ 662887a8d04SJoseph Koshy (MINVA) = min((MINVA),(SH)[i].sh_addr); \ 663887a8d04SJoseph Koshy (MAXVA) = max((MAXVA),(SH)[i].sh_addr + \ 664887a8d04SJoseph Koshy (SH)[i].sh_size); \ 665887a8d04SJoseph Koshy } \ 666887a8d04SJoseph Koshy } while (0) 667887a8d04SJoseph Koshy 668887a8d04SJoseph Koshy 669887a8d04SJoseph Koshy #define GET_PHDR_INFO(H, PH, IMAGE) do { \ 670887a8d04SJoseph Koshy for (i = 0; i < (H)->e_phnum; i++) { \ 671887a8d04SJoseph Koshy switch ((PH)[i].p_type) { \ 672887a8d04SJoseph Koshy case PT_DYNAMIC: \ 673887a8d04SJoseph Koshy image->pi_isdynamic = 1; \ 674887a8d04SJoseph Koshy break; \ 675887a8d04SJoseph Koshy case PT_INTERP: \ 676887a8d04SJoseph Koshy image->pi_dynlinkerpath = \ 677887a8d04SJoseph Koshy pmcstat_string_intern( \ 678887a8d04SJoseph Koshy (char *) mapbase + \ 679887a8d04SJoseph Koshy (PH)[i].p_offset); \ 680887a8d04SJoseph Koshy break; \ 68149874f6eSJoseph Koshy case PT_LOAD: \ 68249874f6eSJoseph Koshy if ((PH)[i].p_offset == 0) \ 68349874f6eSJoseph Koshy image->pi_vaddr = \ 68449874f6eSJoseph Koshy (PH)[i].p_vaddr; \ 68549874f6eSJoseph Koshy break; \ 686887a8d04SJoseph Koshy } \ 687887a8d04SJoseph Koshy } \ 688887a8d04SJoseph Koshy } while (0) 689887a8d04SJoseph Koshy 690887a8d04SJoseph Koshy switch (h->e_machine) { 691887a8d04SJoseph Koshy case EM_386: 692887a8d04SJoseph Koshy case EM_486: 693887a8d04SJoseph Koshy #if defined(__amd64__) 694887a8d04SJoseph Koshy /* a 32 bit executable */ 695887a8d04SJoseph Koshy h32 = (const Elf32_Ehdr *) h; 696887a8d04SJoseph Koshy sh32 = (const Elf32_Shdr *)((uintptr_t) mapbase + h32->e_shoff); 697887a8d04SJoseph Koshy 698887a8d04SJoseph Koshy GET_VA(h32, sh32, minva, maxva); 699887a8d04SJoseph Koshy 700887a8d04SJoseph Koshy image->pi_entry = h32->e_entry; 701887a8d04SJoseph Koshy 702887a8d04SJoseph Koshy if (h32->e_type == ET_EXEC) { 703887a8d04SJoseph Koshy ph32 = (const Elf32_Phdr *)((uintptr_t) mapbase + 704887a8d04SJoseph Koshy h32->e_phoff); 705887a8d04SJoseph Koshy GET_PHDR_INFO(h32, ph32, image); 706887a8d04SJoseph Koshy } 70749874f6eSJoseph Koshy image_type = PMCSTAT_IMAGE_ELF32; 708887a8d04SJoseph Koshy break; 709887a8d04SJoseph Koshy #endif 710887a8d04SJoseph Koshy default: 711887a8d04SJoseph Koshy sh = (const Elf_Shdr *)((uintptr_t) mapbase + h->e_shoff); 712887a8d04SJoseph Koshy 713887a8d04SJoseph Koshy GET_VA(h, sh, minva, maxva); 714887a8d04SJoseph Koshy 715887a8d04SJoseph Koshy image->pi_entry = h->e_entry; 71660f918efSJoseph Koshy 71715139246SJoseph Koshy if (h->e_type == ET_EXEC) { 718887a8d04SJoseph Koshy ph = (const Elf_Phdr *)((uintptr_t) mapbase + 719887a8d04SJoseph Koshy h->e_phoff); 720887a8d04SJoseph Koshy GET_PHDR_INFO(h, ph, image); 721887a8d04SJoseph Koshy } 72249874f6eSJoseph Koshy image_type = PMCSTAT_IMAGE_ELF64; 72315139246SJoseph Koshy break; 72415139246SJoseph Koshy } 725887a8d04SJoseph Koshy 726887a8d04SJoseph Koshy #undef GET_PHDR_INFO 727887a8d04SJoseph Koshy #undef GET_VA 728887a8d04SJoseph Koshy 729887a8d04SJoseph Koshy image->pi_start = minva; 730887a8d04SJoseph Koshy image->pi_end = maxva; 73149874f6eSJoseph Koshy image->pi_type = image_type; 73215139246SJoseph Koshy 73315139246SJoseph Koshy if (munmap(mapbase, st.st_size) < 0) 73415139246SJoseph Koshy err(EX_OSERR, "ERROR: Cannot unmap \"%s\"", path); 73549874f6eSJoseph Koshy return; 73649874f6eSJoseph Koshy } 73715139246SJoseph Koshy 73849874f6eSJoseph Koshy /* 73949874f6eSJoseph Koshy * Given an image descriptor, determine whether it is an ELF, or AOUT. 74049874f6eSJoseph Koshy * If no handler claims the image, set its type to 'INDETERMINABLE'. 74149874f6eSJoseph Koshy */ 74249874f6eSJoseph Koshy 74349874f6eSJoseph Koshy static void 74449874f6eSJoseph Koshy pmcstat_image_determine_type(struct pmcstat_image *image, 74549874f6eSJoseph Koshy struct pmcstat_args *a) 74649874f6eSJoseph Koshy { 74749874f6eSJoseph Koshy assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN); 74849874f6eSJoseph Koshy 74949874f6eSJoseph Koshy /* Try each kind of handler in turn */ 75049874f6eSJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 75149874f6eSJoseph Koshy pmcstat_image_get_elf_params(image, a); 75249874f6eSJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 75349874f6eSJoseph Koshy pmcstat_image_get_aout_params(image, a); 75449874f6eSJoseph Koshy 75549874f6eSJoseph Koshy /* 75649874f6eSJoseph Koshy * Otherwise, remember that we tried to determine 75749874f6eSJoseph Koshy * the object's type and had failed. 75849874f6eSJoseph Koshy */ 75949874f6eSJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 76049874f6eSJoseph Koshy image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE; 76115139246SJoseph Koshy } 76215139246SJoseph Koshy 76315139246SJoseph Koshy /* 76460f918efSJoseph Koshy * Locate an image descriptor given an interned path, adding a fresh 76560f918efSJoseph Koshy * descriptor to the cache if necessary. This function also finds a 76660f918efSJoseph Koshy * suitable name for this image's sample file. 76749874f6eSJoseph Koshy * 76849874f6eSJoseph Koshy * We defer filling in the file format specific parts of the image 76949874f6eSJoseph Koshy * structure till the time we actually see a sample that would fall 77049874f6eSJoseph Koshy * into this image. 77115139246SJoseph Koshy */ 77215139246SJoseph Koshy 77315139246SJoseph Koshy static struct pmcstat_image * 77449874f6eSJoseph Koshy pmcstat_image_from_path(pmcstat_interned_string internedpath, 77549874f6eSJoseph Koshy int iskernelmodule) 77615139246SJoseph Koshy { 77715139246SJoseph Koshy int count, hash, nlen; 77815139246SJoseph Koshy struct pmcstat_image *pi; 77915139246SJoseph Koshy char *sn; 78015139246SJoseph Koshy char name[NAME_MAX]; 78115139246SJoseph Koshy 78249874f6eSJoseph Koshy hash = pmcstat_string_lookup_hash(internedpath); 78315139246SJoseph Koshy 78449874f6eSJoseph Koshy /* First, look for an existing entry. */ 78515139246SJoseph Koshy LIST_FOREACH(pi, &pmcstat_image_hash[hash], pi_next) 78649874f6eSJoseph Koshy if (pi->pi_execpath == internedpath && 78749874f6eSJoseph Koshy pi->pi_iskernelmodule == iskernelmodule) { 78815139246SJoseph Koshy /* move descriptor to the head of the lru list */ 78915139246SJoseph Koshy TAILQ_REMOVE(&pmcstat_image_lru, pi, pi_lru); 79015139246SJoseph Koshy TAILQ_INSERT_HEAD(&pmcstat_image_lru, pi, pi_lru); 79149874f6eSJoseph Koshy return (pi); 79215139246SJoseph Koshy } 79315139246SJoseph Koshy 79415139246SJoseph Koshy /* 79560f918efSJoseph Koshy * Allocate a new entry and place at the head of the hash and 79660f918efSJoseph Koshy * LRU lists. 79715139246SJoseph Koshy */ 79815139246SJoseph Koshy pi = malloc(sizeof(*pi)); 79915139246SJoseph Koshy if (pi == NULL) 80049874f6eSJoseph Koshy return (NULL); 80115139246SJoseph Koshy 80215139246SJoseph Koshy pi->pi_type = PMCSTAT_IMAGE_UNKNOWN; 80349874f6eSJoseph Koshy pi->pi_execpath = internedpath; 80415139246SJoseph Koshy pi->pi_start = ~0; 80560f918efSJoseph Koshy pi->pi_entry = ~0; 80615139246SJoseph Koshy pi->pi_end = 0; 80749874f6eSJoseph Koshy pi->pi_iskernelmodule = iskernelmodule; 80815139246SJoseph Koshy 80960f918efSJoseph Koshy /* 81060f918efSJoseph Koshy * Look for a suitable name for the sample files associated 81160f918efSJoseph Koshy * with this image: if `basename(path)`+".gmon" is available, 81260f918efSJoseph Koshy * we use that, otherwise we try iterating through 81360f918efSJoseph Koshy * `basename(path)`+ "~" + NNN + ".gmon" till we get a free 81460f918efSJoseph Koshy * entry. 81560f918efSJoseph Koshy */ 81649874f6eSJoseph Koshy if ((sn = basename(pmcstat_string_unintern(internedpath))) == NULL) 81749874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Cannot process \"%s\"", 81849874f6eSJoseph Koshy pmcstat_string_unintern(internedpath)); 81915139246SJoseph Koshy 82015139246SJoseph Koshy nlen = strlen(sn); 82149874f6eSJoseph Koshy nlen = min(nlen, (int) (sizeof(name) - sizeof(".gmon"))); 82215139246SJoseph Koshy 82360f918efSJoseph Koshy snprintf(name, sizeof(name), "%.*s.gmon", nlen, sn); 82415139246SJoseph Koshy 82549874f6eSJoseph Koshy /* try use the unabridged name first */ 82615139246SJoseph Koshy if (pmcstat_string_lookup(name) == NULL) 82715139246SJoseph Koshy pi->pi_samplename = pmcstat_string_intern(name); 82815139246SJoseph Koshy else { 82949874f6eSJoseph Koshy /* 83049874f6eSJoseph Koshy * Otherwise use a prefix from the original name and 83149874f6eSJoseph Koshy * upto 3 digits. 83249874f6eSJoseph Koshy */ 83315139246SJoseph Koshy nlen = strlen(sn); 83449874f6eSJoseph Koshy nlen = min(nlen, (int) (sizeof(name)-sizeof("~NNN.gmon"))); 83515139246SJoseph Koshy count = 0; 83615139246SJoseph Koshy do { 83749874f6eSJoseph Koshy if (++count > 999) 83849874f6eSJoseph Koshy errx(EX_CANTCREAT, "ERROR: cannot create a gmon " 83949874f6eSJoseph Koshy "file for \"%s\"", name); 84049874f6eSJoseph Koshy snprintf(name, sizeof(name), "%.*s~%3.3d.gmon", 84115139246SJoseph Koshy nlen, sn, count); 84215139246SJoseph Koshy if (pmcstat_string_lookup(name) == NULL) { 84315139246SJoseph Koshy pi->pi_samplename = pmcstat_string_intern(name); 84415139246SJoseph Koshy count = 0; 84515139246SJoseph Koshy } 84615139246SJoseph Koshy } while (count > 0); 84715139246SJoseph Koshy } 84815139246SJoseph Koshy 84949874f6eSJoseph Koshy 85015139246SJoseph Koshy LIST_INIT(&pi->pi_gmlist); 85115139246SJoseph Koshy 85215139246SJoseph Koshy LIST_INSERT_HEAD(&pmcstat_image_hash[hash], pi, pi_next); 85315139246SJoseph Koshy TAILQ_INSERT_HEAD(&pmcstat_image_lru, pi, pi_lru); 85415139246SJoseph Koshy 85549874f6eSJoseph Koshy return (pi); 85615139246SJoseph Koshy } 85715139246SJoseph Koshy 85815139246SJoseph Koshy /* 85915139246SJoseph Koshy * Increment the bucket in the gmon.out file corresponding to 'pmcid' 86015139246SJoseph Koshy * and 'pc'. 86115139246SJoseph Koshy */ 86215139246SJoseph Koshy 86315139246SJoseph Koshy static void 86415139246SJoseph Koshy pmcstat_image_increment_bucket(struct pmcstat_pcmap *map, uintfptr_t pc, 86515139246SJoseph Koshy pmc_id_t pmcid, struct pmcstat_args *a) 86615139246SJoseph Koshy { 86715139246SJoseph Koshy struct pmcstat_image *image; 86815139246SJoseph Koshy struct pmcstat_gmonfile *pgf; 86915139246SJoseph Koshy uintfptr_t bucket; 87015139246SJoseph Koshy HISTCOUNTER *hc; 87115139246SJoseph Koshy 87215139246SJoseph Koshy assert(pc >= map->ppm_lowpc && pc < map->ppm_highpc); 87315139246SJoseph Koshy 87449874f6eSJoseph Koshy image = map->ppm_image; 87549874f6eSJoseph Koshy 87649874f6eSJoseph Koshy /* 87749874f6eSJoseph Koshy * If this is the first time we are seeing a sample for 87849874f6eSJoseph Koshy * this executable image, try determine its parameters. 87949874f6eSJoseph Koshy */ 88049874f6eSJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 88149874f6eSJoseph Koshy pmcstat_image_determine_type(image, a); 88249874f6eSJoseph Koshy 88349874f6eSJoseph Koshy assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN); 88449874f6eSJoseph Koshy 88549874f6eSJoseph Koshy /* Ignore samples in images that we know nothing about. */ 88649874f6eSJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_INDETERMINABLE) { 88749874f6eSJoseph Koshy pmcstat_stats.ps_samples_indeterminable++; 88849874f6eSJoseph Koshy return; 88949874f6eSJoseph Koshy } 89049874f6eSJoseph Koshy 89115139246SJoseph Koshy /* 89215139246SJoseph Koshy * Find the gmon file corresponding to 'pmcid', creating it if 89315139246SJoseph Koshy * needed. 89415139246SJoseph Koshy */ 89515139246SJoseph Koshy LIST_FOREACH(pgf, &image->pi_gmlist, pgf_next) 89615139246SJoseph Koshy if (pgf->pgf_pmcid == pmcid) 89715139246SJoseph Koshy break; 89815139246SJoseph Koshy 89915139246SJoseph Koshy /* If we don't have a gmon.out file for this PMCid, create one */ 90015139246SJoseph Koshy if (pgf == NULL) { 90115139246SJoseph Koshy if ((pgf = calloc(1, sizeof(*pgf))) == NULL) 90215139246SJoseph Koshy err(EX_OSERR, "ERROR:"); 90315139246SJoseph Koshy 90415139246SJoseph Koshy pgf->pgf_gmondata = NULL; /* mark as unmapped */ 90515139246SJoseph Koshy pgf->pgf_name = pmcstat_gmon_create_name(a->pa_samplesdir, 90615139246SJoseph Koshy image, pmcid); 90715139246SJoseph Koshy pgf->pgf_pmcid = pmcid; 90860f918efSJoseph Koshy assert(image->pi_end > image->pi_start); 909a566429aSJoseph Koshy pgf->pgf_nbuckets = (image->pi_end - image->pi_start) / 91015139246SJoseph Koshy FUNCTION_ALIGNMENT; /* see <machine/profile.h> */ 91115139246SJoseph Koshy pgf->pgf_ndatabytes = sizeof(struct gmonhdr) + 912a566429aSJoseph Koshy pgf->pgf_nbuckets * sizeof(HISTCOUNTER); 91315139246SJoseph Koshy 91415139246SJoseph Koshy pmcstat_gmon_create_file(pgf, image); 91515139246SJoseph Koshy 91615139246SJoseph Koshy LIST_INSERT_HEAD(&image->pi_gmlist, pgf, pgf_next); 91715139246SJoseph Koshy } 91815139246SJoseph Koshy 91915139246SJoseph Koshy /* 92015139246SJoseph Koshy * Map the gmon file in if needed. It may have been mapped 92115139246SJoseph Koshy * out under memory pressure. 92215139246SJoseph Koshy */ 92315139246SJoseph Koshy if (pgf->pgf_gmondata == NULL) 92415139246SJoseph Koshy pmcstat_gmon_map_file(pgf); 92515139246SJoseph Koshy 92649874f6eSJoseph Koshy assert(pgf->pgf_gmondata != NULL); 92749874f6eSJoseph Koshy 92849874f6eSJoseph Koshy /* 92949874f6eSJoseph Koshy * 93049874f6eSJoseph Koshy */ 93149874f6eSJoseph Koshy 93215139246SJoseph Koshy bucket = (pc - map->ppm_lowpc) / FUNCTION_ALIGNMENT; 93315139246SJoseph Koshy 934a566429aSJoseph Koshy assert(bucket < pgf->pgf_nbuckets); 93515139246SJoseph Koshy 936bf840f4aSJoseph Koshy hc = (HISTCOUNTER *) ((uintptr_t) pgf->pgf_gmondata + 93715139246SJoseph Koshy sizeof(struct gmonhdr)); 938a566429aSJoseph Koshy 939a566429aSJoseph Koshy /* saturating add */ 94049874f6eSJoseph Koshy if (hc[bucket] < 0xFFFFU) /* XXX tie this to sizeof(HISTCOUNTER) */ 94115139246SJoseph Koshy hc[bucket]++; 94249874f6eSJoseph Koshy else /* mark that an overflow occurred */ 94349874f6eSJoseph Koshy pgf->pgf_overflow = 1; 94415139246SJoseph Koshy } 94515139246SJoseph Koshy 94615139246SJoseph Koshy /* 94749874f6eSJoseph Koshy * Record the fact that PC values from 'start' to 'end' come from 94815139246SJoseph Koshy * image 'image'. 94915139246SJoseph Koshy */ 95015139246SJoseph Koshy 95115139246SJoseph Koshy static void 95215139246SJoseph Koshy pmcstat_image_link(struct pmcstat_process *pp, struct pmcstat_image *image, 95349874f6eSJoseph Koshy uintfptr_t start) 95415139246SJoseph Koshy { 95515139246SJoseph Koshy struct pmcstat_pcmap *pcm, *pcmnew; 95649874f6eSJoseph Koshy uintfptr_t offset; 95749874f6eSJoseph Koshy 95849874f6eSJoseph Koshy assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN && 95949874f6eSJoseph Koshy image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE); 96015139246SJoseph Koshy 96115139246SJoseph Koshy if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL) 96249874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Cannot create a map entry"); 96315139246SJoseph Koshy 96449874f6eSJoseph Koshy /* 96549874f6eSJoseph Koshy * Adjust the map entry to only cover the text portion 96649874f6eSJoseph Koshy * of the object. 96749874f6eSJoseph Koshy */ 96849874f6eSJoseph Koshy 96949874f6eSJoseph Koshy offset = start - image->pi_vaddr; 97049874f6eSJoseph Koshy pcmnew->ppm_lowpc = image->pi_start + offset; 97149874f6eSJoseph Koshy pcmnew->ppm_highpc = image->pi_end + offset; 97215139246SJoseph Koshy pcmnew->ppm_image = image; 97315139246SJoseph Koshy 97449874f6eSJoseph Koshy assert(pcmnew->ppm_lowpc < pcmnew->ppm_highpc); 97549874f6eSJoseph Koshy 97649874f6eSJoseph Koshy /* Overlapped mmap()'s are assumed to never occur. */ 97715139246SJoseph Koshy TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next) 97849874f6eSJoseph Koshy if (pcm->ppm_lowpc >= pcmnew->ppm_highpc) 97915139246SJoseph Koshy break; 98015139246SJoseph Koshy 98115139246SJoseph Koshy if (pcm == NULL) 98215139246SJoseph Koshy TAILQ_INSERT_TAIL(&pp->pp_map, pcmnew, ppm_next); 98315139246SJoseph Koshy else 98415139246SJoseph Koshy TAILQ_INSERT_BEFORE(pcm, pcmnew, ppm_next); 98515139246SJoseph Koshy } 98615139246SJoseph Koshy 98715139246SJoseph Koshy /* 98849874f6eSJoseph Koshy * Unmap images in the range [start..end) associated with process 98949874f6eSJoseph Koshy * 'pp'. 99049874f6eSJoseph Koshy */ 99149874f6eSJoseph Koshy 99249874f6eSJoseph Koshy static void 99349874f6eSJoseph Koshy pmcstat_image_unmap(struct pmcstat_process *pp, uintfptr_t start, 99449874f6eSJoseph Koshy uintfptr_t end) 99549874f6eSJoseph Koshy { 99649874f6eSJoseph Koshy struct pmcstat_pcmap *pcm, *pcmtmp, *pcmnew; 99749874f6eSJoseph Koshy 99849874f6eSJoseph Koshy assert(pp != NULL); 99949874f6eSJoseph Koshy assert(start < end); 100049874f6eSJoseph Koshy 100149874f6eSJoseph Koshy /* 100249874f6eSJoseph Koshy * Cases: 100349874f6eSJoseph Koshy * - we could have the range completely in the middle of an 100449874f6eSJoseph Koshy * existing pcmap; in this case we have to split the pcmap 100549874f6eSJoseph Koshy * structure into two (i.e., generate a 'hole'). 100649874f6eSJoseph Koshy * - we could have the range covering multiple pcmaps; these 100749874f6eSJoseph Koshy * will have to be removed. 100849874f6eSJoseph Koshy * - we could have either 'start' or 'end' falling in the 100949874f6eSJoseph Koshy * middle of a pcmap; in this case shorten the entry. 101049874f6eSJoseph Koshy */ 101149874f6eSJoseph Koshy 101249874f6eSJoseph Koshy TAILQ_FOREACH_SAFE(pcm, &pp->pp_map, ppm_next, pcmtmp) { 101349874f6eSJoseph Koshy assert(pcm->ppm_lowpc < pcm->ppm_highpc); 101449874f6eSJoseph Koshy if (pcm->ppm_highpc <= start) 101549874f6eSJoseph Koshy continue; 101649874f6eSJoseph Koshy if (pcm->ppm_lowpc > end) 101749874f6eSJoseph Koshy return; 101849874f6eSJoseph Koshy if (pcm->ppm_lowpc >= start && pcm->ppm_highpc <= end) { 101949874f6eSJoseph Koshy /* 102049874f6eSJoseph Koshy * The current pcmap is completely inside the 102149874f6eSJoseph Koshy * unmapped range: remove it entirely. 102249874f6eSJoseph Koshy */ 102349874f6eSJoseph Koshy TAILQ_REMOVE(&pp->pp_map, pcm, ppm_next); 102449874f6eSJoseph Koshy free(pcm); 102549874f6eSJoseph Koshy } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc > end) { 102649874f6eSJoseph Koshy /* 102749874f6eSJoseph Koshy * Split this pcmap into two; curtail the 102849874f6eSJoseph Koshy * current map to end at [start-1], and start 102949874f6eSJoseph Koshy * the new one at [end]. 103049874f6eSJoseph Koshy */ 103149874f6eSJoseph Koshy if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL) 103249874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Cannot split a map " 103349874f6eSJoseph Koshy "entry"); 103449874f6eSJoseph Koshy 103549874f6eSJoseph Koshy pcmnew->ppm_image = pcm->ppm_image; 103649874f6eSJoseph Koshy 103749874f6eSJoseph Koshy pcmnew->ppm_lowpc = end; 103849874f6eSJoseph Koshy pcmnew->ppm_highpc = pcm->ppm_highpc; 103949874f6eSJoseph Koshy 104049874f6eSJoseph Koshy pcm->ppm_highpc = start; 104149874f6eSJoseph Koshy 104249874f6eSJoseph Koshy TAILQ_INSERT_AFTER(&pp->pp_map, pcm, pcmnew, ppm_next); 104349874f6eSJoseph Koshy 104449874f6eSJoseph Koshy return; 104549874f6eSJoseph Koshy } else if (pcm->ppm_lowpc < start) 104649874f6eSJoseph Koshy pcm->ppm_lowpc = start; 104749874f6eSJoseph Koshy else if (pcm->ppm_highpc > end) 104849874f6eSJoseph Koshy pcm->ppm_highpc = end; 104949874f6eSJoseph Koshy else 105049874f6eSJoseph Koshy assert(0); 105149874f6eSJoseph Koshy } 105249874f6eSJoseph Koshy } 105349874f6eSJoseph Koshy 105449874f6eSJoseph Koshy /* 105515139246SJoseph Koshy * Add a {pmcid,name} mapping. 105615139246SJoseph Koshy */ 105715139246SJoseph Koshy 105815139246SJoseph Koshy static void 105949874f6eSJoseph Koshy pmcstat_pmcid_add(pmc_id_t pmcid, pmcstat_interned_string ps, 106049874f6eSJoseph Koshy struct pmcstat_args *a) 106115139246SJoseph Koshy { 106215139246SJoseph Koshy struct pmcstat_pmcrecord *pr; 106315139246SJoseph Koshy struct stat st; 106415139246SJoseph Koshy char fullpath[PATH_MAX]; 106515139246SJoseph Koshy 106615139246SJoseph Koshy LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 106715139246SJoseph Koshy if (pr->pr_pmcid == pmcid) { 106849874f6eSJoseph Koshy pr->pr_pmcname = ps; 106915139246SJoseph Koshy return; 107015139246SJoseph Koshy } 107115139246SJoseph Koshy 107215139246SJoseph Koshy if ((pr = malloc(sizeof(*pr))) == NULL) 107315139246SJoseph Koshy err(EX_OSERR, "ERROR: Cannot allocate pmc record"); 107415139246SJoseph Koshy 107515139246SJoseph Koshy pr->pr_pmcid = pmcid; 107649874f6eSJoseph Koshy pr->pr_pmcname = ps; 107715139246SJoseph Koshy LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next); 107815139246SJoseph Koshy 107915139246SJoseph Koshy (void) snprintf(fullpath, sizeof(fullpath), "%s/%s", a->pa_samplesdir, 108049874f6eSJoseph Koshy pmcstat_string_unintern(ps)); 108115139246SJoseph Koshy 108215139246SJoseph Koshy /* If the path name exists, it should be a directory */ 108315139246SJoseph Koshy if (stat(fullpath, &st) == 0 && S_ISDIR(st.st_mode)) 108415139246SJoseph Koshy return; 108515139246SJoseph Koshy 108615139246SJoseph Koshy if (mkdir(fullpath, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0) 108715139246SJoseph Koshy err(EX_OSERR, "ERROR: Cannot create directory \"%s\"", 108815139246SJoseph Koshy fullpath); 108915139246SJoseph Koshy } 109015139246SJoseph Koshy 109115139246SJoseph Koshy /* 109249874f6eSJoseph Koshy * Given a pmcid in use, find its human-readable name. 109315139246SJoseph Koshy */ 109415139246SJoseph Koshy 109515139246SJoseph Koshy static const char * 109615139246SJoseph Koshy pmcstat_pmcid_to_name(pmc_id_t pmcid) 109715139246SJoseph Koshy { 109815139246SJoseph Koshy struct pmcstat_pmcrecord *pr; 109915139246SJoseph Koshy char fullpath[PATH_MAX]; 110015139246SJoseph Koshy 110115139246SJoseph Koshy LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 110215139246SJoseph Koshy if (pr->pr_pmcid == pmcid) 110349874f6eSJoseph Koshy return (pmcstat_string_unintern(pr->pr_pmcname)); 110415139246SJoseph Koshy 110515139246SJoseph Koshy /* create a default name and add this entry */ 110615139246SJoseph Koshy if ((pr = malloc(sizeof(*pr))) == NULL) 110715139246SJoseph Koshy err(EX_OSERR, "ERROR: "); 110815139246SJoseph Koshy pr->pr_pmcid = pmcid; 110915139246SJoseph Koshy 111015139246SJoseph Koshy (void) snprintf(fullpath, sizeof(fullpath), "%X", (unsigned int) pmcid); 111115139246SJoseph Koshy pr->pr_pmcname = pmcstat_string_intern(fullpath); 111215139246SJoseph Koshy 111315139246SJoseph Koshy LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next); 111415139246SJoseph Koshy 111549874f6eSJoseph Koshy return (pmcstat_string_unintern(pr->pr_pmcname)); 111615139246SJoseph Koshy } 111715139246SJoseph Koshy 111815139246SJoseph Koshy /* 111949874f6eSJoseph Koshy * Associate an AOUT image with a process. 112015139246SJoseph Koshy */ 112115139246SJoseph Koshy 112215139246SJoseph Koshy static void 112349874f6eSJoseph Koshy pmcstat_process_aout_exec(struct pmcstat_process *pp, 112449874f6eSJoseph Koshy struct pmcstat_image *image, uintfptr_t entryaddr, 112549874f6eSJoseph Koshy struct pmcstat_args *a) 112615139246SJoseph Koshy { 112749874f6eSJoseph Koshy (void) pp; 112849874f6eSJoseph Koshy (void) image; 112949874f6eSJoseph Koshy (void) entryaddr; 113049874f6eSJoseph Koshy (void) a; 113149874f6eSJoseph Koshy /* TODO Implement a.out handling */ 113249874f6eSJoseph Koshy } 113349874f6eSJoseph Koshy 113449874f6eSJoseph Koshy /* 113549874f6eSJoseph Koshy * Associate an ELF image with a process. 113649874f6eSJoseph Koshy */ 113749874f6eSJoseph Koshy 113849874f6eSJoseph Koshy static void 113949874f6eSJoseph Koshy pmcstat_process_elf_exec(struct pmcstat_process *pp, 114049874f6eSJoseph Koshy struct pmcstat_image *image, uintfptr_t entryaddr, 114149874f6eSJoseph Koshy struct pmcstat_args *a) 114249874f6eSJoseph Koshy { 114315139246SJoseph Koshy uintmax_t libstart; 114449874f6eSJoseph Koshy struct pmcstat_image *rtldimage; 114515139246SJoseph Koshy 114649874f6eSJoseph Koshy assert(image->pi_type == PMCSTAT_IMAGE_ELF32 || 114749874f6eSJoseph Koshy image->pi_type == PMCSTAT_IMAGE_ELF64); 114815139246SJoseph Koshy 114960f918efSJoseph Koshy /* Create a map entry for the base executable. */ 115049874f6eSJoseph Koshy pmcstat_image_link(pp, image, image->pi_vaddr); 115115139246SJoseph Koshy 115260f918efSJoseph Koshy /* 115360f918efSJoseph Koshy * For dynamically linked executables we need to: 115460f918efSJoseph Koshy * (a) find where the dynamic linker was mapped to for this 115560f918efSJoseph Koshy * process, 115660f918efSJoseph Koshy * (b) find all the executable objects that the dynamic linker 115760f918efSJoseph Koshy * brought in. 115860f918efSJoseph Koshy */ 115949874f6eSJoseph Koshy 116015139246SJoseph Koshy if (image->pi_isdynamic) { 116115139246SJoseph Koshy 116260f918efSJoseph Koshy /* 116360f918efSJoseph Koshy * The runtime loader gets loaded just after the maximum 116460f918efSJoseph Koshy * possible heap address. Like so: 116560f918efSJoseph Koshy * 116660f918efSJoseph Koshy * [ TEXT DATA BSS HEAP -->*RTLD SHLIBS <--STACK] 116760f918efSJoseph Koshy * ^ ^ 116860f918efSJoseph Koshy * 0 VM_MAXUSER_ADDRESS 116949874f6eSJoseph Koshy 117060f918efSJoseph Koshy * 117160f918efSJoseph Koshy * The exact address where the loader gets mapped in 117260f918efSJoseph Koshy * will vary according to the size of the executable 117360f918efSJoseph Koshy * and the limits on the size of the process'es data 117460f918efSJoseph Koshy * segment at the time of exec(). The entry address 117560f918efSJoseph Koshy * recorded at process exec time corresponds to the 117660f918efSJoseph Koshy * 'start' address inside the dynamic linker. From 117760f918efSJoseph Koshy * this we can figure out the address where the 117860f918efSJoseph Koshy * runtime loader's file object had been mapped to. 117960f918efSJoseph Koshy */ 118049874f6eSJoseph Koshy rtldimage = pmcstat_image_from_path(image->pi_dynlinkerpath, 118149874f6eSJoseph Koshy 0); 118249874f6eSJoseph Koshy if (rtldimage == NULL) { 118349874f6eSJoseph Koshy warnx("WARNING: Cannot find image for \"%s\".", 118449874f6eSJoseph Koshy pmcstat_string_unintern(image->pi_dynlinkerpath)); 118549874f6eSJoseph Koshy pmcstat_stats.ps_exec_errors++; 118649874f6eSJoseph Koshy return; 118749874f6eSJoseph Koshy } 118849874f6eSJoseph Koshy 118960f918efSJoseph Koshy if (rtldimage->pi_type == PMCSTAT_IMAGE_UNKNOWN) 119049874f6eSJoseph Koshy pmcstat_image_get_elf_params(rtldimage, a); 119149874f6eSJoseph Koshy 119249874f6eSJoseph Koshy if (rtldimage->pi_type != PMCSTAT_IMAGE_ELF32 && 119349874f6eSJoseph Koshy rtldimage->pi_type != PMCSTAT_IMAGE_ELF64) { 119449874f6eSJoseph Koshy warnx("WARNING: rtld not an ELF object \"%s\".", 119549874f6eSJoseph Koshy pmcstat_string_unintern(image->pi_dynlinkerpath)); 119649874f6eSJoseph Koshy return; 119749874f6eSJoseph Koshy } 119860f918efSJoseph Koshy 119960f918efSJoseph Koshy libstart = entryaddr - rtldimage->pi_entry; 120049874f6eSJoseph Koshy pmcstat_image_link(pp, rtldimage, libstart); 120115139246SJoseph Koshy } 120215139246SJoseph Koshy } 120315139246SJoseph Koshy 120415139246SJoseph Koshy /* 120515139246SJoseph Koshy * Find the process descriptor corresponding to a PID. If 'allocate' 120615139246SJoseph Koshy * is zero, we return a NULL if a pid descriptor could not be found or 120715139246SJoseph Koshy * a process descriptor process. If 'allocate' is non-zero, then we 120815139246SJoseph Koshy * will attempt to allocate a fresh process descriptor. Zombie 120915139246SJoseph Koshy * process descriptors are only removed if a fresh allocation for the 121015139246SJoseph Koshy * same PID is requested. 121115139246SJoseph Koshy */ 121215139246SJoseph Koshy 121315139246SJoseph Koshy static struct pmcstat_process * 121415139246SJoseph Koshy pmcstat_process_lookup(pid_t pid, int allocate) 121515139246SJoseph Koshy { 121615139246SJoseph Koshy uint32_t hash; 121715139246SJoseph Koshy struct pmcstat_pcmap *ppm, *ppmtmp; 121815139246SJoseph Koshy struct pmcstat_process *pp, *pptmp; 121915139246SJoseph Koshy 122015139246SJoseph Koshy hash = (uint32_t) pid & PMCSTAT_HASH_MASK; /* simplicity wins */ 122115139246SJoseph Koshy 122215139246SJoseph Koshy LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[hash], pp_next, pptmp) 122315139246SJoseph Koshy if (pp->pp_pid == pid) { 122415139246SJoseph Koshy /* Found a descriptor, check and process zombies */ 122549874f6eSJoseph Koshy if (allocate && pp->pp_isactive == 0) { 122615139246SJoseph Koshy /* remove maps */ 122715139246SJoseph Koshy TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, 122815139246SJoseph Koshy ppmtmp) { 122915139246SJoseph Koshy TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next); 123015139246SJoseph Koshy free(ppm); 123115139246SJoseph Koshy } 123215139246SJoseph Koshy /* remove process entry */ 123315139246SJoseph Koshy LIST_REMOVE(pp, pp_next); 123415139246SJoseph Koshy free(pp); 123515139246SJoseph Koshy break; 123615139246SJoseph Koshy } 123749874f6eSJoseph Koshy return (pp); 123815139246SJoseph Koshy } 123915139246SJoseph Koshy 124015139246SJoseph Koshy if (!allocate) 124149874f6eSJoseph Koshy return (NULL); 124215139246SJoseph Koshy 124315139246SJoseph Koshy if ((pp = malloc(sizeof(*pp))) == NULL) 124415139246SJoseph Koshy err(EX_OSERR, "ERROR: Cannot allocate pid descriptor"); 124515139246SJoseph Koshy 124615139246SJoseph Koshy pp->pp_pid = pid; 124715139246SJoseph Koshy pp->pp_isactive = 1; 124815139246SJoseph Koshy 124915139246SJoseph Koshy TAILQ_INIT(&pp->pp_map); 125015139246SJoseph Koshy 125115139246SJoseph Koshy LIST_INSERT_HEAD(&pmcstat_process_hash[hash], pp, pp_next); 125249874f6eSJoseph Koshy return (pp); 125315139246SJoseph Koshy } 125415139246SJoseph Koshy 125515139246SJoseph Koshy /* 125615139246SJoseph Koshy * Associate an image and a process. 125715139246SJoseph Koshy */ 125815139246SJoseph Koshy 125915139246SJoseph Koshy static void 126049874f6eSJoseph Koshy pmcstat_process_exec(struct pmcstat_process *pp, 126149874f6eSJoseph Koshy pmcstat_interned_string path, uintfptr_t entryaddr, 126249874f6eSJoseph Koshy struct pmcstat_args *a) 126315139246SJoseph Koshy { 126415139246SJoseph Koshy struct pmcstat_image *image; 126515139246SJoseph Koshy 126649874f6eSJoseph Koshy if ((image = pmcstat_image_from_path(path, 0)) == NULL) { 126749874f6eSJoseph Koshy pmcstat_stats.ps_exec_errors++; 126815139246SJoseph Koshy return; 126949874f6eSJoseph Koshy } 127015139246SJoseph Koshy 127115139246SJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 127249874f6eSJoseph Koshy pmcstat_image_determine_type(image, a); 127315139246SJoseph Koshy 127449874f6eSJoseph Koshy assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN); 127549874f6eSJoseph Koshy 127649874f6eSJoseph Koshy switch (image->pi_type) { 127749874f6eSJoseph Koshy case PMCSTAT_IMAGE_ELF32: 127849874f6eSJoseph Koshy case PMCSTAT_IMAGE_ELF64: 127949874f6eSJoseph Koshy pmcstat_stats.ps_exec_elf++; 128049874f6eSJoseph Koshy pmcstat_process_elf_exec(pp, image, entryaddr, a); 128115139246SJoseph Koshy break; 128215139246SJoseph Koshy 128315139246SJoseph Koshy case PMCSTAT_IMAGE_AOUT: 128449874f6eSJoseph Koshy pmcstat_stats.ps_exec_aout++; 128549874f6eSJoseph Koshy pmcstat_process_aout_exec(pp, image, entryaddr, a); 128649874f6eSJoseph Koshy break; 128749874f6eSJoseph Koshy 128849874f6eSJoseph Koshy case PMCSTAT_IMAGE_INDETERMINABLE: 128949874f6eSJoseph Koshy pmcstat_stats.ps_exec_indeterminable++; 129015139246SJoseph Koshy break; 129115139246SJoseph Koshy 129215139246SJoseph Koshy default: 129360f918efSJoseph Koshy err(EX_SOFTWARE, "ERROR: Unsupported executable type for " 129449874f6eSJoseph Koshy "\"%s\"", pmcstat_string_unintern(path)); 129515139246SJoseph Koshy } 129615139246SJoseph Koshy } 129715139246SJoseph Koshy 129815139246SJoseph Koshy 129960f918efSJoseph Koshy /* 130060f918efSJoseph Koshy * Find the map entry associated with process 'p' at PC value 'pc'. 130160f918efSJoseph Koshy */ 130260f918efSJoseph Koshy 130360f918efSJoseph Koshy static struct pmcstat_pcmap * 130460f918efSJoseph Koshy pmcstat_process_find_map(struct pmcstat_process *p, uintfptr_t pc) 130560f918efSJoseph Koshy { 130660f918efSJoseph Koshy struct pmcstat_pcmap *ppm; 130760f918efSJoseph Koshy 130849874f6eSJoseph Koshy TAILQ_FOREACH(ppm, &p->pp_map, ppm_next) { 130960f918efSJoseph Koshy if (pc >= ppm->ppm_lowpc && pc < ppm->ppm_highpc) 131049874f6eSJoseph Koshy return (ppm); 131149874f6eSJoseph Koshy if (pc < ppm->ppm_lowpc) 131249874f6eSJoseph Koshy return (NULL); 131349874f6eSJoseph Koshy } 131460f918efSJoseph Koshy 131549874f6eSJoseph Koshy return (NULL); 131660f918efSJoseph Koshy } 131760f918efSJoseph Koshy 131815139246SJoseph Koshy 131915139246SJoseph Koshy 132015139246SJoseph Koshy static int 132115139246SJoseph Koshy pmcstat_convert_log(struct pmcstat_args *a) 132215139246SJoseph Koshy { 132315139246SJoseph Koshy uintfptr_t pc; 132449874f6eSJoseph Koshy pid_t pid; 132549874f6eSJoseph Koshy struct pmcstat_image *image; 132615139246SJoseph Koshy struct pmcstat_process *pp, *ppnew; 132715139246SJoseph Koshy struct pmcstat_pcmap *ppm, *ppmtmp; 132815139246SJoseph Koshy struct pmclog_ev ev; 132949874f6eSJoseph Koshy pmcstat_interned_string image_path; 133015139246SJoseph Koshy 133115139246SJoseph Koshy while (pmclog_read(a->pa_logparser, &ev) == 0) { 133215139246SJoseph Koshy assert(ev.pl_state == PMCLOG_OK); 133315139246SJoseph Koshy 133415139246SJoseph Koshy switch (ev.pl_type) { 133549874f6eSJoseph Koshy case PMCLOG_TYPE_INITIALIZE: 133649874f6eSJoseph Koshy if ((ev.pl_u.pl_i.pl_version & 0xFF000000) != 133749874f6eSJoseph Koshy PMC_VERSION_MAJOR << 24 && a->pa_verbosity > 0) 133849874f6eSJoseph Koshy warnx("WARNING: Log version 0x%x does not " 133949874f6eSJoseph Koshy "match compiled version 0x%x.", 134049874f6eSJoseph Koshy ev.pl_u.pl_i.pl_version, 134149874f6eSJoseph Koshy PMC_VERSION_MAJOR); 134249874f6eSJoseph Koshy break; 134349874f6eSJoseph Koshy case PMCLOG_TYPE_MAP_IN: 134415139246SJoseph Koshy /* 134515139246SJoseph Koshy * Introduce an address range mapping for a 134649874f6eSJoseph Koshy * userland process or the kernel (pid == -1). 134749874f6eSJoseph Koshy * 134849874f6eSJoseph Koshy * We always allocate a process descriptor so 134949874f6eSJoseph Koshy * that subsequent samples seen for this 135049874f6eSJoseph Koshy * address range are mapped to the current 135149874f6eSJoseph Koshy * object being mapped in. 135215139246SJoseph Koshy */ 135349874f6eSJoseph Koshy pid = ev.pl_u.pl_mi.pl_pid; 135449874f6eSJoseph Koshy if (pid == -1) 135549874f6eSJoseph Koshy pp = pmcstat_kernproc; 135649874f6eSJoseph Koshy else 135749874f6eSJoseph Koshy pp = pmcstat_process_lookup(pid, 135849874f6eSJoseph Koshy PMCSTAT_ALLOCATE); 135949874f6eSJoseph Koshy 136049874f6eSJoseph Koshy assert(pp != NULL); 136149874f6eSJoseph Koshy 136249874f6eSJoseph Koshy image_path = pmcstat_string_intern(ev.pl_u.pl_mi. 136349874f6eSJoseph Koshy pl_pathname); 136449874f6eSJoseph Koshy image = pmcstat_image_from_path(image_path, pid == -1); 136549874f6eSJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 136649874f6eSJoseph Koshy pmcstat_image_determine_type(image, a); 136749874f6eSJoseph Koshy if (image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE) 136849874f6eSJoseph Koshy pmcstat_image_link(pp, image, 136949874f6eSJoseph Koshy ev.pl_u.pl_mi.pl_start); 137049874f6eSJoseph Koshy break; 137149874f6eSJoseph Koshy 137249874f6eSJoseph Koshy case PMCLOG_TYPE_MAP_OUT: 137349874f6eSJoseph Koshy /* 137449874f6eSJoseph Koshy * Remove an address map. 137549874f6eSJoseph Koshy */ 137649874f6eSJoseph Koshy pid = ev.pl_u.pl_mo.pl_pid; 137749874f6eSJoseph Koshy if (pid == -1) 137849874f6eSJoseph Koshy pp = pmcstat_kernproc; 137949874f6eSJoseph Koshy else 138049874f6eSJoseph Koshy pp = pmcstat_process_lookup(pid, 0); 138149874f6eSJoseph Koshy 138249874f6eSJoseph Koshy if (pp == NULL) /* unknown process */ 138349874f6eSJoseph Koshy break; 138449874f6eSJoseph Koshy 138549874f6eSJoseph Koshy pmcstat_image_unmap(pp, ev.pl_u.pl_mo.pl_start, 138649874f6eSJoseph Koshy ev.pl_u.pl_mo.pl_end); 138715139246SJoseph Koshy break; 138815139246SJoseph Koshy 138915139246SJoseph Koshy case PMCLOG_TYPE_PCSAMPLE: 139015139246SJoseph Koshy 139115139246SJoseph Koshy /* 139215139246SJoseph Koshy * We bring in the gmon file for the image 139315139246SJoseph Koshy * currently associated with the PMC & pid 139415139246SJoseph Koshy * pair and increment the appropriate entry 139515139246SJoseph Koshy * bin inside this. 139615139246SJoseph Koshy */ 139749874f6eSJoseph Koshy pmcstat_stats.ps_samples_total++; 139849874f6eSJoseph Koshy 139915139246SJoseph Koshy pc = ev.pl_u.pl_s.pl_pc; 140049874f6eSJoseph Koshy pp = pmcstat_process_lookup(ev.pl_u.pl_s.pl_pid, 140149874f6eSJoseph Koshy PMCSTAT_ALLOCATE); 140215139246SJoseph Koshy if ((ppm = pmcstat_process_find_map(pp, pc)) == NULL && 140315139246SJoseph Koshy (ppm = pmcstat_process_find_map(pmcstat_kernproc, 140449874f6eSJoseph Koshy pc)) == NULL) { /* unknown process,offset pair */ 140549874f6eSJoseph Koshy pmcstat_stats.ps_samples_unknown_offset++; 140649874f6eSJoseph Koshy break; 140749874f6eSJoseph Koshy } 140815139246SJoseph Koshy 140915139246SJoseph Koshy pmcstat_image_increment_bucket(ppm, pc, 141015139246SJoseph Koshy ev.pl_u.pl_s.pl_pmcid, a); 141115139246SJoseph Koshy 141215139246SJoseph Koshy break; 141315139246SJoseph Koshy 141415139246SJoseph Koshy case PMCLOG_TYPE_PMCALLOCATE: 141515139246SJoseph Koshy /* 141615139246SJoseph Koshy * Record the association pmc id between this 141715139246SJoseph Koshy * PMC and its name. 141815139246SJoseph Koshy */ 141915139246SJoseph Koshy pmcstat_pmcid_add(ev.pl_u.pl_a.pl_pmcid, 142015139246SJoseph Koshy pmcstat_string_intern(ev.pl_u.pl_a.pl_evname), a); 142115139246SJoseph Koshy break; 142215139246SJoseph Koshy 142315139246SJoseph Koshy case PMCLOG_TYPE_PROCEXEC: 142415139246SJoseph Koshy 142515139246SJoseph Koshy /* 142615139246SJoseph Koshy * Change the executable image associated with 142715139246SJoseph Koshy * a process. 142815139246SJoseph Koshy */ 142949874f6eSJoseph Koshy pp = pmcstat_process_lookup(ev.pl_u.pl_x.pl_pid, 143049874f6eSJoseph Koshy PMCSTAT_ALLOCATE); 143115139246SJoseph Koshy 143215139246SJoseph Koshy /* delete the current process map */ 143315139246SJoseph Koshy TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) { 143415139246SJoseph Koshy TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next); 143515139246SJoseph Koshy free(ppm); 143615139246SJoseph Koshy } 143715139246SJoseph Koshy 143849874f6eSJoseph Koshy /* associate this process image */ 143915139246SJoseph Koshy image_path = pmcstat_string_intern( 144015139246SJoseph Koshy ev.pl_u.pl_x.pl_pathname); 144149874f6eSJoseph Koshy assert(image_path != NULL); 144260f918efSJoseph Koshy pmcstat_process_exec(pp, image_path, 144349874f6eSJoseph Koshy ev.pl_u.pl_x.pl_entryaddr, a); 144415139246SJoseph Koshy break; 144515139246SJoseph Koshy 144615139246SJoseph Koshy case PMCLOG_TYPE_PROCEXIT: 144715139246SJoseph Koshy 144815139246SJoseph Koshy /* 144915139246SJoseph Koshy * Due to the way the log is generated, the 145015139246SJoseph Koshy * last few samples corresponding to a process 145115139246SJoseph Koshy * may appear in the log after the process 145215139246SJoseph Koshy * exit event is recorded. Thus we keep the 145315139246SJoseph Koshy * process' descriptor and associated data 145415139246SJoseph Koshy * structures around, but mark the process as 145515139246SJoseph Koshy * having exited. 145615139246SJoseph Koshy */ 145715139246SJoseph Koshy pp = pmcstat_process_lookup(ev.pl_u.pl_e.pl_pid, 0); 145815139246SJoseph Koshy if (pp == NULL) 145915139246SJoseph Koshy break; 146049874f6eSJoseph Koshy pp->pp_isactive = 0; /* mark as a zombie */ 146115139246SJoseph Koshy break; 146215139246SJoseph Koshy 146315139246SJoseph Koshy case PMCLOG_TYPE_SYSEXIT: 146415139246SJoseph Koshy pp = pmcstat_process_lookup(ev.pl_u.pl_se.pl_pid, 0); 146515139246SJoseph Koshy if (pp == NULL) 146615139246SJoseph Koshy break; 146715139246SJoseph Koshy pp->pp_isactive = 0; /* make a zombie */ 146815139246SJoseph Koshy break; 146915139246SJoseph Koshy 147015139246SJoseph Koshy case PMCLOG_TYPE_PROCFORK: 147115139246SJoseph Koshy 147215139246SJoseph Koshy /* 147349874f6eSJoseph Koshy * Allocate a process descriptor for the new 147449874f6eSJoseph Koshy * (child) process. 147549874f6eSJoseph Koshy */ 147649874f6eSJoseph Koshy ppnew = 147749874f6eSJoseph Koshy pmcstat_process_lookup(ev.pl_u.pl_f.pl_newpid, 147849874f6eSJoseph Koshy PMCSTAT_ALLOCATE); 147949874f6eSJoseph Koshy 148049874f6eSJoseph Koshy /* 148149874f6eSJoseph Koshy * If we had been tracking the parent, clone 148249874f6eSJoseph Koshy * its address maps. 148315139246SJoseph Koshy */ 148415139246SJoseph Koshy pp = pmcstat_process_lookup(ev.pl_u.pl_f.pl_oldpid, 0); 148515139246SJoseph Koshy if (pp == NULL) 148615139246SJoseph Koshy break; 148715139246SJoseph Koshy TAILQ_FOREACH(ppm, &pp->pp_map, ppm_next) 148815139246SJoseph Koshy pmcstat_image_link(ppnew, ppm->ppm_image, 148949874f6eSJoseph Koshy ppm->ppm_lowpc); 149015139246SJoseph Koshy break; 149115139246SJoseph Koshy 149215139246SJoseph Koshy default: /* other types of entries are not relevant */ 149315139246SJoseph Koshy break; 149415139246SJoseph Koshy } 149515139246SJoseph Koshy } 149615139246SJoseph Koshy 149715139246SJoseph Koshy if (ev.pl_state == PMCLOG_EOF) 149849874f6eSJoseph Koshy return (PMCSTAT_FINISHED); 149915139246SJoseph Koshy else if (ev.pl_state == PMCLOG_REQUIRE_DATA) 150049874f6eSJoseph Koshy return (PMCSTAT_RUNNING); 150115139246SJoseph Koshy 150215139246SJoseph Koshy err(EX_DATAERR, "ERROR: event parsing failed (record %jd, " 150315139246SJoseph Koshy "offset 0x%jx)", (uintmax_t) ev.pl_count + 1, ev.pl_offset); 150415139246SJoseph Koshy } 150515139246SJoseph Koshy 150615139246SJoseph Koshy /* 150715139246SJoseph Koshy * Print log entries as text. 150815139246SJoseph Koshy */ 150915139246SJoseph Koshy 151049874f6eSJoseph Koshy static int 151115139246SJoseph Koshy pmcstat_print_log(struct pmcstat_args *a) 151215139246SJoseph Koshy { 151315139246SJoseph Koshy struct pmclog_ev ev; 151415139246SJoseph Koshy 151515139246SJoseph Koshy while (pmclog_read(a->pa_logparser, &ev) == 0) { 151615139246SJoseph Koshy assert(ev.pl_state == PMCLOG_OK); 151715139246SJoseph Koshy switch (ev.pl_type) { 151815139246SJoseph Koshy case PMCLOG_TYPE_CLOSELOG: 151915139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"closelog",); 152015139246SJoseph Koshy break; 152115139246SJoseph Koshy case PMCLOG_TYPE_DROPNOTIFY: 152215139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"drop",); 152315139246SJoseph Koshy break; 152415139246SJoseph Koshy case PMCLOG_TYPE_INITIALIZE: 152515139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"initlog","0x%x \"%s\"", 152615139246SJoseph Koshy ev.pl_u.pl_i.pl_version, 152715139246SJoseph Koshy pmc_name_of_cputype(ev.pl_u.pl_i.pl_arch)); 152815139246SJoseph Koshy break; 152949874f6eSJoseph Koshy case PMCLOG_TYPE_MAP_IN: 153049874f6eSJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"map-in","%d %p \"%s\"", 153149874f6eSJoseph Koshy ev.pl_u.pl_mi.pl_pid, 153249874f6eSJoseph Koshy (void *) ev.pl_u.pl_mi.pl_start, 153349874f6eSJoseph Koshy ev.pl_u.pl_mi.pl_pathname); 153449874f6eSJoseph Koshy break; 153549874f6eSJoseph Koshy case PMCLOG_TYPE_MAP_OUT: 153649874f6eSJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"map-out","%d %p %p", 153749874f6eSJoseph Koshy ev.pl_u.pl_mo.pl_pid, 153849874f6eSJoseph Koshy (void *) ev.pl_u.pl_mo.pl_start, 153949874f6eSJoseph Koshy (void *) ev.pl_u.pl_mo.pl_end); 154015139246SJoseph Koshy break; 154115139246SJoseph Koshy case PMCLOG_TYPE_PCSAMPLE: 154215139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"sample","0x%x %d %p %c", 154315139246SJoseph Koshy ev.pl_u.pl_s.pl_pmcid, 154415139246SJoseph Koshy ev.pl_u.pl_s.pl_pid, 154515139246SJoseph Koshy (void *) ev.pl_u.pl_s.pl_pc, 154615139246SJoseph Koshy ev.pl_u.pl_s.pl_usermode ? 'u' : 's'); 154715139246SJoseph Koshy break; 154815139246SJoseph Koshy case PMCLOG_TYPE_PMCALLOCATE: 154915139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"allocate","0x%x \"%s\" 0x%x", 155015139246SJoseph Koshy ev.pl_u.pl_a.pl_pmcid, 155115139246SJoseph Koshy ev.pl_u.pl_a.pl_evname, 155215139246SJoseph Koshy ev.pl_u.pl_a.pl_flags); 155315139246SJoseph Koshy break; 155415139246SJoseph Koshy case PMCLOG_TYPE_PMCATTACH: 155515139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"attach","0x%x %d \"%s\"", 155615139246SJoseph Koshy ev.pl_u.pl_t.pl_pmcid, 155715139246SJoseph Koshy ev.pl_u.pl_t.pl_pid, 155815139246SJoseph Koshy ev.pl_u.pl_t.pl_pathname); 155915139246SJoseph Koshy break; 156015139246SJoseph Koshy case PMCLOG_TYPE_PMCDETACH: 156115139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"detach","0x%x %d", 156215139246SJoseph Koshy ev.pl_u.pl_d.pl_pmcid, 156315139246SJoseph Koshy ev.pl_u.pl_d.pl_pid); 156415139246SJoseph Koshy break; 156515139246SJoseph Koshy case PMCLOG_TYPE_PROCCSW: 156615139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"cswval","0x%x %d %jd", 156715139246SJoseph Koshy ev.pl_u.pl_c.pl_pmcid, 156815139246SJoseph Koshy ev.pl_u.pl_c.pl_pid, 156915139246SJoseph Koshy ev.pl_u.pl_c.pl_value); 157015139246SJoseph Koshy break; 157115139246SJoseph Koshy case PMCLOG_TYPE_PROCEXEC: 157215139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"exec","0x%x %d %p \"%s\"", 157315139246SJoseph Koshy ev.pl_u.pl_x.pl_pmcid, 157415139246SJoseph Koshy ev.pl_u.pl_x.pl_pid, 157515139246SJoseph Koshy (void *) ev.pl_u.pl_x.pl_entryaddr, 157615139246SJoseph Koshy ev.pl_u.pl_x.pl_pathname); 157715139246SJoseph Koshy break; 157815139246SJoseph Koshy case PMCLOG_TYPE_PROCEXIT: 157915139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"exitval","0x%x %d %jd", 158015139246SJoseph Koshy ev.pl_u.pl_e.pl_pmcid, 158115139246SJoseph Koshy ev.pl_u.pl_e.pl_pid, 158215139246SJoseph Koshy ev.pl_u.pl_e.pl_value); 158315139246SJoseph Koshy break; 158415139246SJoseph Koshy case PMCLOG_TYPE_PROCFORK: 158515139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"fork","%d %d", 158615139246SJoseph Koshy ev.pl_u.pl_f.pl_oldpid, 158715139246SJoseph Koshy ev.pl_u.pl_f.pl_newpid); 158815139246SJoseph Koshy break; 158915139246SJoseph Koshy case PMCLOG_TYPE_USERDATA: 159015139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"userdata","0x%x", 159115139246SJoseph Koshy ev.pl_u.pl_u.pl_userdata); 159215139246SJoseph Koshy break; 159315139246SJoseph Koshy case PMCLOG_TYPE_SYSEXIT: 159415139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"exit","%d", 159515139246SJoseph Koshy ev.pl_u.pl_se.pl_pid); 159615139246SJoseph Koshy break; 159715139246SJoseph Koshy default: 159815139246SJoseph Koshy fprintf(a->pa_printfile, "unknown %d", 159915139246SJoseph Koshy ev.pl_type); 160015139246SJoseph Koshy } 160115139246SJoseph Koshy } 160215139246SJoseph Koshy 160315139246SJoseph Koshy if (ev.pl_state == PMCLOG_EOF) 160449874f6eSJoseph Koshy return (PMCSTAT_FINISHED); 160515139246SJoseph Koshy else if (ev.pl_state == PMCLOG_REQUIRE_DATA) 160649874f6eSJoseph Koshy return (PMCSTAT_RUNNING); 160715139246SJoseph Koshy 160815139246SJoseph Koshy err(EX_DATAERR, "ERROR: event parsing failed " 160915139246SJoseph Koshy "(record %jd, offset 0x%jx)", 161015139246SJoseph Koshy (uintmax_t) ev.pl_count + 1, ev.pl_offset); 161115139246SJoseph Koshy /*NOTREACHED*/ 161215139246SJoseph Koshy } 161315139246SJoseph Koshy 161415139246SJoseph Koshy /* 161549874f6eSJoseph Koshy * Public Interfaces. 161649874f6eSJoseph Koshy */ 161749874f6eSJoseph Koshy 161849874f6eSJoseph Koshy /* 161949874f6eSJoseph Koshy * Close a logfile, after first flushing all in-module queued data. 162049874f6eSJoseph Koshy */ 162149874f6eSJoseph Koshy 162249874f6eSJoseph Koshy int 162349874f6eSJoseph Koshy pmcstat_close_log(struct pmcstat_args *a) 162449874f6eSJoseph Koshy { 162549874f6eSJoseph Koshy if (pmc_flush_logfile() < 0 || 162649874f6eSJoseph Koshy pmc_configure_logfile(-1) < 0) 162749874f6eSJoseph Koshy err(EX_OSERR, "ERROR: logging failed"); 162849874f6eSJoseph Koshy a->pa_flags &= ~(FLAG_HAS_OUTPUT_LOGFILE | FLAG_HAS_PIPE); 162949874f6eSJoseph Koshy return (a->pa_flags & FLAG_HAS_PIPE ? PMCSTAT_EXITING : 163049874f6eSJoseph Koshy PMCSTAT_FINISHED); 163149874f6eSJoseph Koshy } 163249874f6eSJoseph Koshy 163349874f6eSJoseph Koshy 163449874f6eSJoseph Koshy 163549874f6eSJoseph Koshy /* 163649874f6eSJoseph Koshy * Open a log file, for reading or writing. 163749874f6eSJoseph Koshy * 163849874f6eSJoseph Koshy * The function returns the fd of a successfully opened log or -1 in 163949874f6eSJoseph Koshy * case of failure. 164049874f6eSJoseph Koshy */ 164149874f6eSJoseph Koshy 164249874f6eSJoseph Koshy int 164349874f6eSJoseph Koshy pmcstat_open_log(const char *path, int mode) 164449874f6eSJoseph Koshy { 1645302cbb90SJoseph Koshy int error, fd; 1646302cbb90SJoseph Koshy size_t hlen; 1647302cbb90SJoseph Koshy const char *p, *errstr; 1648302cbb90SJoseph Koshy struct addrinfo hints, *res, *res0; 1649302cbb90SJoseph Koshy char hostname[MAXHOSTNAMELEN]; 1650302cbb90SJoseph Koshy 1651302cbb90SJoseph Koshy errstr = NULL; 1652302cbb90SJoseph Koshy fd = -1; 165349874f6eSJoseph Koshy 165449874f6eSJoseph Koshy /* 165549874f6eSJoseph Koshy * If 'path' is "-" then open one of stdin or stdout depending 1656302cbb90SJoseph Koshy * on the value of 'mode'. 1657302cbb90SJoseph Koshy * 1658302cbb90SJoseph Koshy * If 'path' contains a ':' and does not start with a '/' or '.', 1659302cbb90SJoseph Koshy * and is being opened for writing, treat it as a "host:port" 1660302cbb90SJoseph Koshy * specification and open a network socket. 1661302cbb90SJoseph Koshy * 1662302cbb90SJoseph Koshy * Otherwise, treat 'path' as a file name and open that. 166349874f6eSJoseph Koshy */ 166449874f6eSJoseph Koshy if (path[0] == '-' && path[1] == '\0') 166549874f6eSJoseph Koshy fd = (mode == PMCSTAT_OPEN_FOR_READ) ? 0 : 1; 1666302cbb90SJoseph Koshy else if (mode == PMCSTAT_OPEN_FOR_WRITE && path[0] != '/' && 1667302cbb90SJoseph Koshy path[0] != '.' && strchr(path, ':') != NULL) { 1668302cbb90SJoseph Koshy 1669302cbb90SJoseph Koshy p = strrchr(path, ':'); 1670302cbb90SJoseph Koshy hlen = p - path; 1671302cbb90SJoseph Koshy if (p == path || hlen >= sizeof(hostname)) { 1672302cbb90SJoseph Koshy errstr = strerror(EINVAL); 1673302cbb90SJoseph Koshy goto done; 1674302cbb90SJoseph Koshy } 1675302cbb90SJoseph Koshy 1676302cbb90SJoseph Koshy assert(hlen < sizeof(hostname)); 1677302cbb90SJoseph Koshy (void) strncpy(hostname, path, hlen); 1678302cbb90SJoseph Koshy hostname[hlen] = '\0'; 1679302cbb90SJoseph Koshy 1680302cbb90SJoseph Koshy (void) memset(&hints, 0, sizeof(hints)); 1681302cbb90SJoseph Koshy hints.ai_family = AF_UNSPEC; 1682302cbb90SJoseph Koshy hints.ai_socktype = SOCK_STREAM; 1683302cbb90SJoseph Koshy if ((error = getaddrinfo(hostname, p+1, &hints, &res0)) != 0) { 1684302cbb90SJoseph Koshy errstr = gai_strerror(error); 1685302cbb90SJoseph Koshy goto done; 1686302cbb90SJoseph Koshy } 1687302cbb90SJoseph Koshy 1688302cbb90SJoseph Koshy fd = -1; 1689302cbb90SJoseph Koshy for (res = res0; res; res = res->ai_next) { 1690302cbb90SJoseph Koshy if ((fd = socket(res->ai_family, res->ai_socktype, 1691302cbb90SJoseph Koshy res->ai_protocol)) < 0) { 1692302cbb90SJoseph Koshy errstr = strerror(errno); 1693302cbb90SJoseph Koshy continue; 1694302cbb90SJoseph Koshy } 1695302cbb90SJoseph Koshy if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) { 1696302cbb90SJoseph Koshy errstr = strerror(errno); 1697302cbb90SJoseph Koshy (void) close(fd); 1698302cbb90SJoseph Koshy fd = -1; 1699302cbb90SJoseph Koshy continue; 1700302cbb90SJoseph Koshy } 1701302cbb90SJoseph Koshy errstr = NULL; 1702302cbb90SJoseph Koshy break; 1703302cbb90SJoseph Koshy } 1704302cbb90SJoseph Koshy freeaddrinfo(res0); 1705302cbb90SJoseph Koshy 1706302cbb90SJoseph Koshy } else if ((fd = open(path, mode == PMCSTAT_OPEN_FOR_READ ? 170749874f6eSJoseph Koshy O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC), 1708302cbb90SJoseph Koshy S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) 1709302cbb90SJoseph Koshy errstr = strerror(errno); 1710302cbb90SJoseph Koshy 1711302cbb90SJoseph Koshy done: 1712302cbb90SJoseph Koshy if (errstr) 1713302cbb90SJoseph Koshy errx(EX_OSERR, "ERROR: Cannot open \"%s\" for %s: %s.", path, 1714302cbb90SJoseph Koshy (mode == PMCSTAT_OPEN_FOR_READ ? "reading" : "writing"), 1715302cbb90SJoseph Koshy errstr); 171649874f6eSJoseph Koshy 171749874f6eSJoseph Koshy return (fd); 171849874f6eSJoseph Koshy } 171949874f6eSJoseph Koshy 172049874f6eSJoseph Koshy /* 172115139246SJoseph Koshy * Process a log file in offline analysis mode. 172215139246SJoseph Koshy */ 172315139246SJoseph Koshy 1724dc1d9d2eSJoseph Koshy int 172515139246SJoseph Koshy pmcstat_process_log(struct pmcstat_args *a) 172615139246SJoseph Koshy { 172715139246SJoseph Koshy 172815139246SJoseph Koshy /* 172915139246SJoseph Koshy * If gprof style profiles haven't been asked for, just print the 173015139246SJoseph Koshy * log to the current output file. 173115139246SJoseph Koshy */ 173215139246SJoseph Koshy if (a->pa_flags & FLAG_DO_PRINT) 173349874f6eSJoseph Koshy return (pmcstat_print_log(a)); 173415139246SJoseph Koshy else 173515139246SJoseph Koshy /* convert the log to gprof compatible profiles */ 173649874f6eSJoseph Koshy return (pmcstat_convert_log(a)); 173715139246SJoseph Koshy } 173815139246SJoseph Koshy 173949874f6eSJoseph Koshy /* 174049874f6eSJoseph Koshy * Initialize module. 174149874f6eSJoseph Koshy */ 174249874f6eSJoseph Koshy 174315139246SJoseph Koshy void 174415139246SJoseph Koshy pmcstat_initialize_logging(struct pmcstat_args *a) 174515139246SJoseph Koshy { 174660f918efSJoseph Koshy int i; 174749874f6eSJoseph Koshy 174849874f6eSJoseph Koshy (void) a; 174915139246SJoseph Koshy 175015139246SJoseph Koshy /* use a convenient format for 'ldd' output */ 1751f3c8200eSJoseph Koshy if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1","%o \"%p\" %x\n",1) != 0) 175249874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Cannot setenv"); 175315139246SJoseph Koshy 175415139246SJoseph Koshy /* Initialize hash tables */ 175549874f6eSJoseph Koshy pmcstat_string_initialize(); 175615139246SJoseph Koshy for (i = 0; i < PMCSTAT_NHASH; i++) { 175715139246SJoseph Koshy LIST_INIT(&pmcstat_image_hash[i]); 175815139246SJoseph Koshy LIST_INIT(&pmcstat_process_hash[i]); 175915139246SJoseph Koshy } 176015139246SJoseph Koshy 176149874f6eSJoseph Koshy /* 176249874f6eSJoseph Koshy * Create a fake 'process' entry for the kernel with pid -1. 176349874f6eSJoseph Koshy * hwpmc(4) will subsequently inform us about where the kernel 176449874f6eSJoseph Koshy * and any loaded kernel modules are mapped. 176549874f6eSJoseph Koshy */ 176649874f6eSJoseph Koshy if ((pmcstat_kernproc = pmcstat_process_lookup((pid_t) -1, 176749874f6eSJoseph Koshy PMCSTAT_ALLOCATE)) == NULL) 176815139246SJoseph Koshy err(EX_OSERR, "ERROR: Cannot initialize logging"); 176915139246SJoseph Koshy } 177015139246SJoseph Koshy 177149874f6eSJoseph Koshy /* 177249874f6eSJoseph Koshy * Shutdown module. 177349874f6eSJoseph Koshy */ 177449874f6eSJoseph Koshy 177515139246SJoseph Koshy void 177649874f6eSJoseph Koshy pmcstat_shutdown_logging(struct pmcstat_args *a) 177715139246SJoseph Koshy { 177815139246SJoseph Koshy int i; 177949874f6eSJoseph Koshy FILE *mf; 178015139246SJoseph Koshy struct pmcstat_gmonfile *pgf, *pgftmp; 178115139246SJoseph Koshy struct pmcstat_image *pi, *pitmp; 178215139246SJoseph Koshy struct pmcstat_process *pp, *pptmp; 178349874f6eSJoseph Koshy 178449874f6eSJoseph Koshy /* determine where to send the map file */ 178549874f6eSJoseph Koshy mf = NULL; 178649874f6eSJoseph Koshy if (a->pa_mapfilename != NULL) 178749874f6eSJoseph Koshy mf = (strcmp(a->pa_mapfilename, "-") == 0) ? 178849874f6eSJoseph Koshy a->pa_printfile : fopen(a->pa_mapfilename, "w"); 178949874f6eSJoseph Koshy 179049874f6eSJoseph Koshy if (mf == NULL && a->pa_flags & FLAG_DO_GPROF && 179149874f6eSJoseph Koshy a->pa_verbosity >= 2) 179249874f6eSJoseph Koshy mf = a->pa_printfile; 179349874f6eSJoseph Koshy 179449874f6eSJoseph Koshy if (mf) 179549874f6eSJoseph Koshy (void) fprintf(mf, "MAP:\n"); 179615139246SJoseph Koshy 179715139246SJoseph Koshy for (i = 0; i < PMCSTAT_NHASH; i++) { 179815139246SJoseph Koshy LIST_FOREACH_SAFE(pi, &pmcstat_image_hash[i], pi_next, pitmp) { 179915139246SJoseph Koshy /* flush gmon.out data to disk */ 180015139246SJoseph Koshy LIST_FOREACH_SAFE(pgf, &pi->pi_gmlist, pgf_next, 180115139246SJoseph Koshy pgftmp) { 180215139246SJoseph Koshy pmcstat_gmon_unmap_file(pgf); 180315139246SJoseph Koshy LIST_REMOVE(pgf, pgf_next); 180449874f6eSJoseph Koshy 180549874f6eSJoseph Koshy if (pgf->pgf_overflow && a->pa_verbosity >= 1) 180649874f6eSJoseph Koshy warnx("WARNING: profile \"%s\" " 180749874f6eSJoseph Koshy "overflowed.", 180849874f6eSJoseph Koshy pmcstat_string_unintern(pgf->pgf_name)); 180915139246SJoseph Koshy free(pgf); 181015139246SJoseph Koshy } 181149874f6eSJoseph Koshy if (mf) 181249874f6eSJoseph Koshy (void) fprintf(mf, " \"%s\" -> \"%s\"\n", 181349874f6eSJoseph Koshy pmcstat_string_unintern(pi->pi_execpath), 181449874f6eSJoseph Koshy pmcstat_string_unintern(pi->pi_samplename)); 181515139246SJoseph Koshy 181615139246SJoseph Koshy LIST_REMOVE(pi, pi_next); 181715139246SJoseph Koshy free(pi); 181815139246SJoseph Koshy } 181915139246SJoseph Koshy LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[i], pp_next, 182015139246SJoseph Koshy pptmp) { 182115139246SJoseph Koshy LIST_REMOVE(pp, pp_next); 182215139246SJoseph Koshy free(pp); 182315139246SJoseph Koshy } 182415139246SJoseph Koshy } 182549874f6eSJoseph Koshy 182649874f6eSJoseph Koshy pmcstat_string_shutdown(); 182749874f6eSJoseph Koshy 182849874f6eSJoseph Koshy /* 182949874f6eSJoseph Koshy * Print errors unless -q was specified. Print all statistics 183049874f6eSJoseph Koshy * if verbosity > 1. 183149874f6eSJoseph Koshy */ 183249874f6eSJoseph Koshy #define PRINT(N,V,A) do { \ 183349874f6eSJoseph Koshy if (pmcstat_stats.ps_##V || (A)->pa_verbosity >= 2) \ 183449874f6eSJoseph Koshy (void) fprintf((A)->pa_printfile, " %-40s %d\n",\ 183549874f6eSJoseph Koshy N, pmcstat_stats.ps_##V); \ 183649874f6eSJoseph Koshy } while (0) 183749874f6eSJoseph Koshy 183849874f6eSJoseph Koshy if (a->pa_verbosity >= 1 && a->pa_flags & FLAG_DO_GPROF) { 183949874f6eSJoseph Koshy (void) fprintf(a->pa_printfile, "CONVERSION STATISTICS:\n"); 184049874f6eSJoseph Koshy PRINT("#exec/a.out", exec_aout, a); 184149874f6eSJoseph Koshy PRINT("#exec/elf", exec_elf, a); 184249874f6eSJoseph Koshy PRINT("#exec/unknown", exec_indeterminable, a); 184349874f6eSJoseph Koshy PRINT("#exec handling errors", exec_errors, a); 184449874f6eSJoseph Koshy PRINT("#samples/total", samples_total, a); 184549874f6eSJoseph Koshy PRINT("#samples/unclaimed", samples_unknown_offset, a); 184649874f6eSJoseph Koshy PRINT("#samples/unknown-object", samples_indeterminable, a); 184715139246SJoseph Koshy } 184849874f6eSJoseph Koshy 184949874f6eSJoseph Koshy if (mf) 185049874f6eSJoseph Koshy (void) fclose(mf); 185115139246SJoseph Koshy } 1852