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 */ 1382a6d2e9cSJoseph Koshy unsigned int pgf_nsamples; /* #samples in this gmon.out */ 13949874f6eSJoseph Koshy pmcstat_interned_string pgf_name; /* pathname of gmon.out file */ 14015139246SJoseph Koshy size_t pgf_ndatabytes; /* number of bytes mapped */ 14115139246SJoseph Koshy void *pgf_gmondata; /* pointer to mmap'ed data */ 14215139246SJoseph Koshy }; 14315139246SJoseph Koshy 14415139246SJoseph Koshy /* 14515139246SJoseph Koshy * A 'pmcstat_image' structure describes an executable program on 14649874f6eSJoseph Koshy * disk. 'pi_execpath' is a cookie representing the pathname of 14715139246SJoseph Koshy * the executable. 'pi_start' and 'pi_end' are the least and greatest 14815139246SJoseph Koshy * virtual addresses for the text segments in the executable. 14915139246SJoseph Koshy * 'pi_gmonlist' contains a linked list of gmon.out files associated 15015139246SJoseph Koshy * with this image. 15115139246SJoseph Koshy */ 15215139246SJoseph Koshy 15315139246SJoseph Koshy enum pmcstat_image_type { 15449874f6eSJoseph Koshy PMCSTAT_IMAGE_UNKNOWN = 0, /* never looked at the image */ 15549874f6eSJoseph Koshy PMCSTAT_IMAGE_INDETERMINABLE, /* can't tell what the image is */ 15649874f6eSJoseph Koshy PMCSTAT_IMAGE_ELF32, /* ELF 32 bit object */ 15749874f6eSJoseph Koshy PMCSTAT_IMAGE_ELF64, /* ELF 64 bit object */ 15849874f6eSJoseph Koshy PMCSTAT_IMAGE_AOUT /* AOUT object */ 15915139246SJoseph Koshy }; 16015139246SJoseph Koshy 16115139246SJoseph Koshy struct pmcstat_image { 16215139246SJoseph Koshy LIST_ENTRY(pmcstat_image) pi_next; /* hash link */ 16315139246SJoseph Koshy TAILQ_ENTRY(pmcstat_image) pi_lru; /* LRU list */ 16449874f6eSJoseph Koshy pmcstat_interned_string pi_execpath;/* cookie */ 16549874f6eSJoseph Koshy pmcstat_interned_string pi_samplename; /* sample path name */ 16615139246SJoseph Koshy 16715139246SJoseph Koshy enum pmcstat_image_type pi_type; /* executable type */ 16849874f6eSJoseph Koshy 16949874f6eSJoseph Koshy /* 17049874f6eSJoseph Koshy * Executables have pi_start and pi_end; these are zero 17149874f6eSJoseph Koshy * for shared libraries. 17249874f6eSJoseph Koshy */ 17315139246SJoseph Koshy uintfptr_t pi_start; /* start address (inclusive) */ 17415139246SJoseph Koshy uintfptr_t pi_end; /* end address (exclusive) */ 17560f918efSJoseph Koshy uintfptr_t pi_entry; /* entry address */ 17649874f6eSJoseph Koshy uintfptr_t pi_vaddr; /* virtual address where loaded */ 17749874f6eSJoseph Koshy int pi_isdynamic; /* whether a dynamic 17849874f6eSJoseph Koshy * object */ 17949874f6eSJoseph Koshy int pi_iskernelmodule; 18049874f6eSJoseph Koshy pmcstat_interned_string pi_dynlinkerpath; /* path in .interp */ 18115139246SJoseph Koshy 18249874f6eSJoseph Koshy /* 18349874f6eSJoseph Koshy * An image can be associated with one or more gmon.out files; 18449874f6eSJoseph Koshy * one per PMC. 18549874f6eSJoseph Koshy */ 18615139246SJoseph Koshy LIST_HEAD(,pmcstat_gmonfile) pi_gmlist; 18715139246SJoseph Koshy }; 18815139246SJoseph Koshy 18949874f6eSJoseph Koshy /* 19049874f6eSJoseph Koshy * All image descriptors are kept in a hash table. 19149874f6eSJoseph Koshy */ 19215139246SJoseph Koshy static LIST_HEAD(,pmcstat_image) pmcstat_image_hash[PMCSTAT_NHASH]; 19315139246SJoseph Koshy static TAILQ_HEAD(,pmcstat_image) pmcstat_image_lru = 19415139246SJoseph Koshy TAILQ_HEAD_INITIALIZER(pmcstat_image_lru); 19515139246SJoseph Koshy 19649874f6eSJoseph Koshy /* 19749874f6eSJoseph Koshy * A 'pmcstat_pcmap' structure maps a virtual address range to an 19849874f6eSJoseph Koshy * underlying 'pmcstat_image' descriptor. 19949874f6eSJoseph Koshy */ 20015139246SJoseph Koshy struct pmcstat_pcmap { 20115139246SJoseph Koshy TAILQ_ENTRY(pmcstat_pcmap) ppm_next; 20215139246SJoseph Koshy uintfptr_t ppm_lowpc; 20315139246SJoseph Koshy uintfptr_t ppm_highpc; 20415139246SJoseph Koshy struct pmcstat_image *ppm_image; 20515139246SJoseph Koshy }; 20615139246SJoseph Koshy 20715139246SJoseph Koshy /* 20849874f6eSJoseph Koshy * A 'pmcstat_process' structure models processes. Each process is 20949874f6eSJoseph Koshy * associated with a set of pmcstat_pcmap structures that map 21049874f6eSJoseph Koshy * addresses inside it to executable objects. This set is implemented 21149874f6eSJoseph Koshy * as a list, kept sorted in ascending order of mapped addresses. 21249874f6eSJoseph Koshy * 21349874f6eSJoseph Koshy * 'pp_pid' holds the pid of the process. When a process exits, the 21449874f6eSJoseph Koshy * 'pp_isactive' field is set to zero, but the process structure is 21549874f6eSJoseph Koshy * not immediately reclaimed because there may still be samples in the 21649874f6eSJoseph Koshy * log for this process. 21715139246SJoseph Koshy */ 21815139246SJoseph Koshy 21915139246SJoseph Koshy struct pmcstat_process { 22015139246SJoseph Koshy LIST_ENTRY(pmcstat_process) pp_next; /* hash-next */ 22115139246SJoseph Koshy pid_t pp_pid; /* associated pid */ 22215139246SJoseph Koshy int pp_isactive; /* whether active */ 22360f918efSJoseph Koshy uintfptr_t pp_entryaddr; /* entry address */ 22415139246SJoseph Koshy TAILQ_HEAD(,pmcstat_pcmap) pp_map; /* address range map */ 22515139246SJoseph Koshy }; 22615139246SJoseph Koshy 22749874f6eSJoseph Koshy #define PMCSTAT_ALLOCATE 1 22849874f6eSJoseph Koshy 22949874f6eSJoseph Koshy /* 23049874f6eSJoseph Koshy * All process descriptors are kept in a hash table. 23149874f6eSJoseph Koshy */ 23215139246SJoseph Koshy static LIST_HEAD(,pmcstat_process) pmcstat_process_hash[PMCSTAT_NHASH]; 23315139246SJoseph Koshy 23415139246SJoseph Koshy static struct pmcstat_process *pmcstat_kernproc; /* kernel 'process' */ 23515139246SJoseph Koshy 23649874f6eSJoseph Koshy /* Misc. statistics */ 23749874f6eSJoseph Koshy static struct pmcstat_stats { 23849874f6eSJoseph Koshy int ps_exec_aout; /* # a.out executables seen */ 23949874f6eSJoseph Koshy int ps_exec_elf; /* # elf executables seen */ 24049874f6eSJoseph Koshy int ps_exec_errors; /* # errors processing executables */ 24149874f6eSJoseph Koshy int ps_exec_indeterminable; /* # unknown executables seen */ 24249874f6eSJoseph Koshy int ps_samples_total; /* total number of samples processed */ 24349874f6eSJoseph Koshy int ps_samples_unknown_offset; /* #samples not in any map */ 24449874f6eSJoseph Koshy int ps_samples_indeterminable; /* #samples in indeterminable images */ 24549874f6eSJoseph Koshy } pmcstat_stats; 24649874f6eSJoseph Koshy 24715139246SJoseph Koshy /* 24815139246SJoseph Koshy * Prototypes 24915139246SJoseph Koshy */ 25015139246SJoseph Koshy 25115139246SJoseph Koshy static void pmcstat_gmon_create_file(struct pmcstat_gmonfile *_pgf, 25215139246SJoseph Koshy struct pmcstat_image *_image); 25349874f6eSJoseph Koshy static pmcstat_interned_string pmcstat_gmon_create_name(const char *_sd, 25415139246SJoseph Koshy struct pmcstat_image *_img, pmc_id_t _pmcid); 25515139246SJoseph Koshy static void pmcstat_gmon_map_file(struct pmcstat_gmonfile *_pgf); 25615139246SJoseph Koshy static void pmcstat_gmon_unmap_file(struct pmcstat_gmonfile *_pgf); 25715139246SJoseph Koshy 25849874f6eSJoseph Koshy static void pmcstat_image_determine_type(struct pmcstat_image *_image, 25949874f6eSJoseph Koshy struct pmcstat_args *_a); 26049874f6eSJoseph Koshy static struct pmcstat_image *pmcstat_image_from_path(pmcstat_interned_string 26149874f6eSJoseph Koshy _path, int _iskernelmodule); 26249874f6eSJoseph Koshy static void pmcstat_image_get_aout_params(struct pmcstat_image *_image, 26349874f6eSJoseph Koshy struct pmcstat_args *_a); 26449874f6eSJoseph Koshy static void pmcstat_image_get_elf_params(struct pmcstat_image *_image, 26549874f6eSJoseph Koshy struct pmcstat_args *_a); 26615139246SJoseph Koshy static void pmcstat_image_increment_bucket(struct pmcstat_pcmap *_pcm, 26715139246SJoseph Koshy uintfptr_t _pc, pmc_id_t _pmcid, struct pmcstat_args *_a); 26815139246SJoseph Koshy static void pmcstat_image_link(struct pmcstat_process *_pp, 26949874f6eSJoseph Koshy struct pmcstat_image *_i, uintfptr_t _lpc); 27015139246SJoseph Koshy 27149874f6eSJoseph Koshy static void pmcstat_pmcid_add(pmc_id_t _pmcid, 27249874f6eSJoseph Koshy pmcstat_interned_string _name, struct pmcstat_args *_a); 27315139246SJoseph Koshy static const char *pmcstat_pmcid_to_name(pmc_id_t _pmcid); 27415139246SJoseph Koshy 27549874f6eSJoseph Koshy static void pmcstat_process_aout_exec(struct pmcstat_process *_pp, 27649874f6eSJoseph Koshy struct pmcstat_image *_image, uintfptr_t _entryaddr, 27749874f6eSJoseph Koshy struct pmcstat_args *_a); 27849874f6eSJoseph Koshy static void pmcstat_process_elf_exec(struct pmcstat_process *_pp, 27949874f6eSJoseph Koshy struct pmcstat_image *_image, uintfptr_t _entryaddr, 28049874f6eSJoseph Koshy struct pmcstat_args *_a); 28160f918efSJoseph Koshy static void pmcstat_process_exec(struct pmcstat_process *_pp, 28249874f6eSJoseph Koshy pmcstat_interned_string _path, uintfptr_t _entryaddr, 28349874f6eSJoseph Koshy struct pmcstat_args *_ao); 28449874f6eSJoseph Koshy static struct pmcstat_process *pmcstat_process_lookup(pid_t _pid, 28549874f6eSJoseph Koshy int _allocate); 28615139246SJoseph Koshy static struct pmcstat_pcmap *pmcstat_process_find_map( 28715139246SJoseph Koshy struct pmcstat_process *_p, uintfptr_t _pc); 28815139246SJoseph Koshy 28915139246SJoseph Koshy static int pmcstat_string_compute_hash(const char *_string); 29049874f6eSJoseph Koshy static void pmcstat_string_initialize(void); 29149874f6eSJoseph Koshy static pmcstat_interned_string pmcstat_string_intern(const char *_s); 29249874f6eSJoseph Koshy static pmcstat_interned_string pmcstat_string_lookup(const char *_s); 29349874f6eSJoseph Koshy static int pmcstat_string_lookup_hash(pmcstat_interned_string _is); 29449874f6eSJoseph Koshy static void pmcstat_string_shutdown(void); 29549874f6eSJoseph Koshy static const char *pmcstat_string_unintern(pmcstat_interned_string _is); 29615139246SJoseph Koshy 29715139246SJoseph Koshy 29815139246SJoseph Koshy /* 29949874f6eSJoseph Koshy * A simple implementation of interned strings. Each interned string 30049874f6eSJoseph Koshy * is assigned a unique address, so that subsequent string compares 30149874f6eSJoseph Koshy * can be done by a simple pointer comparision instead of using 30249874f6eSJoseph Koshy * strcmp(). This speeds up hash table lookups and saves memory if 30349874f6eSJoseph Koshy * duplicate strings are the norm. 30449874f6eSJoseph Koshy */ 30549874f6eSJoseph Koshy struct pmcstat_string { 30649874f6eSJoseph Koshy LIST_ENTRY(pmcstat_string) ps_next; /* hash link */ 30749874f6eSJoseph Koshy int ps_len; 30849874f6eSJoseph Koshy int ps_hash; 30949874f6eSJoseph Koshy char *ps_string; 31049874f6eSJoseph Koshy }; 31149874f6eSJoseph Koshy 31249874f6eSJoseph Koshy static LIST_HEAD(,pmcstat_string) pmcstat_string_hash[PMCSTAT_NHASH]; 31349874f6eSJoseph Koshy 31449874f6eSJoseph Koshy /* 31549874f6eSJoseph Koshy * Compute a 'hash' value for a string. 31649874f6eSJoseph Koshy */ 31749874f6eSJoseph Koshy 31849874f6eSJoseph Koshy static int 31949874f6eSJoseph Koshy pmcstat_string_compute_hash(const char *s) 32049874f6eSJoseph Koshy { 32149874f6eSJoseph Koshy int hash; 32249874f6eSJoseph Koshy 32349874f6eSJoseph Koshy for (hash = 0; *s; s++) 32449874f6eSJoseph Koshy hash ^= *s; 32549874f6eSJoseph Koshy 32649874f6eSJoseph Koshy return (hash & PMCSTAT_HASH_MASK); 32749874f6eSJoseph Koshy } 32849874f6eSJoseph Koshy 32949874f6eSJoseph Koshy /* 33049874f6eSJoseph Koshy * Intern a copy of string 's', and return a pointer to the 33149874f6eSJoseph Koshy * interned structure. 33249874f6eSJoseph Koshy */ 33349874f6eSJoseph Koshy 33449874f6eSJoseph Koshy static pmcstat_interned_string 33549874f6eSJoseph Koshy pmcstat_string_intern(const char *s) 33649874f6eSJoseph Koshy { 33749874f6eSJoseph Koshy struct pmcstat_string *ps; 33849874f6eSJoseph Koshy const struct pmcstat_string *cps; 33949874f6eSJoseph Koshy int hash, len; 34049874f6eSJoseph Koshy 34149874f6eSJoseph Koshy if ((cps = pmcstat_string_lookup(s)) != NULL) 34249874f6eSJoseph Koshy return (cps); 34349874f6eSJoseph Koshy 34449874f6eSJoseph Koshy hash = pmcstat_string_compute_hash(s); 34549874f6eSJoseph Koshy len = strlen(s); 34649874f6eSJoseph Koshy 34749874f6eSJoseph Koshy if ((ps = malloc(sizeof(*ps))) == NULL) 34849874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Could not intern string"); 34949874f6eSJoseph Koshy ps->ps_len = len; 35049874f6eSJoseph Koshy ps->ps_hash = hash; 35149874f6eSJoseph Koshy ps->ps_string = strdup(s); 35249874f6eSJoseph Koshy LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next); 35349874f6eSJoseph Koshy return ((pmcstat_interned_string) ps); 35449874f6eSJoseph Koshy } 35549874f6eSJoseph Koshy 35649874f6eSJoseph Koshy static const char * 35749874f6eSJoseph Koshy pmcstat_string_unintern(pmcstat_interned_string str) 35849874f6eSJoseph Koshy { 35949874f6eSJoseph Koshy const char *s; 36049874f6eSJoseph Koshy 36149874f6eSJoseph Koshy s = ((const struct pmcstat_string *) str)->ps_string; 36249874f6eSJoseph Koshy return (s); 36349874f6eSJoseph Koshy } 36449874f6eSJoseph Koshy 36549874f6eSJoseph Koshy static pmcstat_interned_string 36649874f6eSJoseph Koshy pmcstat_string_lookup(const char *s) 36749874f6eSJoseph Koshy { 36849874f6eSJoseph Koshy struct pmcstat_string *ps; 36949874f6eSJoseph Koshy int hash, len; 37049874f6eSJoseph Koshy 37149874f6eSJoseph Koshy hash = pmcstat_string_compute_hash(s); 37249874f6eSJoseph Koshy len = strlen(s); 37349874f6eSJoseph Koshy 37449874f6eSJoseph Koshy LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next) 37549874f6eSJoseph Koshy if (ps->ps_len == len && ps->ps_hash == hash && 37649874f6eSJoseph Koshy strcmp(ps->ps_string, s) == 0) 37749874f6eSJoseph Koshy return (ps); 37849874f6eSJoseph Koshy return (NULL); 37949874f6eSJoseph Koshy } 38049874f6eSJoseph Koshy 38149874f6eSJoseph Koshy static int 38249874f6eSJoseph Koshy pmcstat_string_lookup_hash(pmcstat_interned_string s) 38349874f6eSJoseph Koshy { 38449874f6eSJoseph Koshy const struct pmcstat_string *ps; 38549874f6eSJoseph Koshy 38649874f6eSJoseph Koshy ps = (const struct pmcstat_string *) s; 38749874f6eSJoseph Koshy return (ps->ps_hash); 38849874f6eSJoseph Koshy } 38949874f6eSJoseph Koshy 39049874f6eSJoseph Koshy /* 39149874f6eSJoseph Koshy * Initialize the string interning facility. 39249874f6eSJoseph Koshy */ 39349874f6eSJoseph Koshy 39449874f6eSJoseph Koshy static void 39549874f6eSJoseph Koshy pmcstat_string_initialize(void) 39649874f6eSJoseph Koshy { 39749874f6eSJoseph Koshy int i; 39849874f6eSJoseph Koshy 39949874f6eSJoseph Koshy for (i = 0; i < PMCSTAT_NHASH; i++) 40049874f6eSJoseph Koshy LIST_INIT(&pmcstat_string_hash[i]); 40149874f6eSJoseph Koshy } 40249874f6eSJoseph Koshy 40349874f6eSJoseph Koshy /* 40449874f6eSJoseph Koshy * Destroy the string table, free'ing up space. 40549874f6eSJoseph Koshy */ 40649874f6eSJoseph Koshy 40749874f6eSJoseph Koshy static void 40849874f6eSJoseph Koshy pmcstat_string_shutdown(void) 40949874f6eSJoseph Koshy { 41049874f6eSJoseph Koshy int i; 41149874f6eSJoseph Koshy struct pmcstat_string *ps, *pstmp; 41249874f6eSJoseph Koshy 41349874f6eSJoseph Koshy for (i = 0; i < PMCSTAT_NHASH; i++) 41449874f6eSJoseph Koshy LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next, 41549874f6eSJoseph Koshy pstmp) { 41649874f6eSJoseph Koshy LIST_REMOVE(ps, ps_next); 41749874f6eSJoseph Koshy free(ps->ps_string); 41849874f6eSJoseph Koshy free(ps); 41949874f6eSJoseph Koshy } 42049874f6eSJoseph Koshy } 42149874f6eSJoseph Koshy 42249874f6eSJoseph Koshy /* 42315139246SJoseph Koshy * Create a gmon.out file and size it. 42415139246SJoseph Koshy */ 42515139246SJoseph Koshy 42615139246SJoseph Koshy static void 42715139246SJoseph Koshy pmcstat_gmon_create_file(struct pmcstat_gmonfile *pgf, 42815139246SJoseph Koshy struct pmcstat_image *image) 42915139246SJoseph Koshy { 43015139246SJoseph Koshy int fd; 43115139246SJoseph Koshy size_t count; 43215139246SJoseph Koshy struct gmonhdr gm; 43349874f6eSJoseph Koshy const char *pathname; 43415139246SJoseph Koshy char buffer[DEFAULT_BUFFER_SIZE]; 43515139246SJoseph Koshy 43649874f6eSJoseph Koshy pathname = pmcstat_string_unintern(pgf->pgf_name); 43749874f6eSJoseph Koshy if ((fd = open(pathname, O_RDWR|O_NOFOLLOW|O_CREAT, 43815139246SJoseph Koshy S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) 43949874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Cannot open \"%s\"", pathname); 44015139246SJoseph Koshy 44115139246SJoseph Koshy gm.lpc = image->pi_start; 44215139246SJoseph Koshy gm.hpc = image->pi_end; 443a566429aSJoseph Koshy gm.ncnt = (pgf->pgf_nbuckets * sizeof(HISTCOUNTER)) + 444a566429aSJoseph Koshy sizeof(struct gmonhdr); 44515139246SJoseph Koshy gm.version = GMONVERSION; 44615139246SJoseph Koshy gm.profrate = 0; /* use ticks */ 44715139246SJoseph Koshy gm.histcounter_type = 0; /* compatibility with moncontrol() */ 44815139246SJoseph Koshy gm.spare[0] = gm.spare[1] = 0; 44915139246SJoseph Koshy 45015139246SJoseph Koshy /* Write out the gmon header */ 45115139246SJoseph Koshy if (write(fd, &gm, sizeof(gm)) < 0) 45215139246SJoseph Koshy goto error; 45315139246SJoseph Koshy 45415139246SJoseph Koshy /* Zero fill the samples[] array */ 45515139246SJoseph Koshy (void) memset(buffer, 0, sizeof(buffer)); 45615139246SJoseph Koshy 45715139246SJoseph Koshy count = pgf->pgf_ndatabytes - sizeof(struct gmonhdr); 45815139246SJoseph Koshy while (count > sizeof(buffer)) { 45915139246SJoseph Koshy if (write(fd, &buffer, sizeof(buffer)) < 0) 46015139246SJoseph Koshy goto error; 46115139246SJoseph Koshy count -= sizeof(buffer); 46215139246SJoseph Koshy } 46315139246SJoseph Koshy 46415139246SJoseph Koshy if (write(fd, &buffer, count) < 0) 46515139246SJoseph Koshy goto error; 46615139246SJoseph Koshy 46749874f6eSJoseph Koshy /* TODO size the arc table */ 46849874f6eSJoseph Koshy 46915139246SJoseph Koshy (void) close(fd); 47015139246SJoseph Koshy 47115139246SJoseph Koshy return; 47215139246SJoseph Koshy 47315139246SJoseph Koshy error: 47449874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Cannot write \"%s\"", pathname); 47515139246SJoseph Koshy } 47615139246SJoseph Koshy 47749874f6eSJoseph Koshy /* 47849874f6eSJoseph Koshy * Determine the full pathname of a gmon.out file for a given 47949874f6eSJoseph Koshy * (image,pmcid) combination. Return the interned string. 48049874f6eSJoseph Koshy */ 48149874f6eSJoseph Koshy 48249874f6eSJoseph Koshy pmcstat_interned_string 48315139246SJoseph Koshy pmcstat_gmon_create_name(const char *samplesdir, struct pmcstat_image *image, 48415139246SJoseph Koshy pmc_id_t pmcid) 48515139246SJoseph Koshy { 48615139246SJoseph Koshy const char *pmcname; 48715139246SJoseph Koshy char fullpath[PATH_MAX]; 48815139246SJoseph Koshy 48915139246SJoseph Koshy pmcname = pmcstat_pmcid_to_name(pmcid); 49015139246SJoseph Koshy 49115139246SJoseph Koshy (void) snprintf(fullpath, sizeof(fullpath), 49249874f6eSJoseph Koshy "%s/%s/%s", samplesdir, pmcname, 49349874f6eSJoseph Koshy pmcstat_string_unintern(image->pi_samplename)); 49415139246SJoseph Koshy 49549874f6eSJoseph Koshy return (pmcstat_string_intern(fullpath)); 49615139246SJoseph Koshy } 49715139246SJoseph Koshy 49815139246SJoseph Koshy 49949874f6eSJoseph Koshy /* 50049874f6eSJoseph Koshy * Mmap in a gmon.out file for processing. 50149874f6eSJoseph Koshy */ 50249874f6eSJoseph Koshy 50315139246SJoseph Koshy static void 50415139246SJoseph Koshy pmcstat_gmon_map_file(struct pmcstat_gmonfile *pgf) 50515139246SJoseph Koshy { 50615139246SJoseph Koshy int fd; 50749874f6eSJoseph Koshy const char *pathname; 50849874f6eSJoseph Koshy 50949874f6eSJoseph Koshy pathname = pmcstat_string_unintern(pgf->pgf_name); 51015139246SJoseph Koshy 51115139246SJoseph Koshy /* the gmon.out file must already exist */ 51249874f6eSJoseph Koshy if ((fd = open(pathname, O_RDWR | O_NOFOLLOW, 0)) < 0) 51349874f6eSJoseph Koshy err(EX_OSERR, "ERROR: cannot open \"%s\"", pathname); 51415139246SJoseph Koshy 51515139246SJoseph Koshy pgf->pgf_gmondata = mmap(NULL, pgf->pgf_ndatabytes, 51615139246SJoseph Koshy PROT_READ|PROT_WRITE, MAP_NOSYNC|MAP_SHARED, fd, 0); 51715139246SJoseph Koshy 51815139246SJoseph Koshy if (pgf->pgf_gmondata == MAP_FAILED) 51949874f6eSJoseph Koshy err(EX_OSERR, "ERROR: cannot map \"%s\"", pathname); 52015139246SJoseph Koshy 52115139246SJoseph Koshy (void) close(fd); 52215139246SJoseph Koshy } 52315139246SJoseph Koshy 52415139246SJoseph Koshy /* 52549874f6eSJoseph Koshy * Unmap a gmon.out file after sync'ing its data to disk. 52615139246SJoseph Koshy */ 52715139246SJoseph Koshy 52815139246SJoseph Koshy static void 52915139246SJoseph Koshy pmcstat_gmon_unmap_file(struct pmcstat_gmonfile *pgf) 53015139246SJoseph Koshy { 53115139246SJoseph Koshy (void) msync(pgf->pgf_gmondata, pgf->pgf_ndatabytes, 53215139246SJoseph Koshy MS_SYNC); 53315139246SJoseph Koshy (void) munmap(pgf->pgf_gmondata, pgf->pgf_ndatabytes); 53415139246SJoseph Koshy pgf->pgf_gmondata = NULL; 53515139246SJoseph Koshy } 53615139246SJoseph Koshy 53749874f6eSJoseph Koshy /* 53849874f6eSJoseph Koshy * Determine whether a given executable image is an A.OUT object, and 53949874f6eSJoseph Koshy * if so, fill in its parameters from the text file. 54049874f6eSJoseph Koshy * Sets image->pi_type. 54149874f6eSJoseph Koshy */ 54249874f6eSJoseph Koshy 54315139246SJoseph Koshy static void 54449874f6eSJoseph Koshy pmcstat_image_get_aout_params(struct pmcstat_image *image, 54549874f6eSJoseph Koshy struct pmcstat_args *a) 54649874f6eSJoseph Koshy { 54749874f6eSJoseph Koshy int fd; 54849874f6eSJoseph Koshy ssize_t nbytes; 54949874f6eSJoseph Koshy struct exec ex; 55049874f6eSJoseph Koshy const char *path; 55149874f6eSJoseph Koshy char buffer[PATH_MAX]; 55249874f6eSJoseph Koshy 55349874f6eSJoseph Koshy path = pmcstat_string_unintern(image->pi_execpath); 55449874f6eSJoseph Koshy assert(path != NULL); 55549874f6eSJoseph Koshy 55649874f6eSJoseph Koshy if (image->pi_iskernelmodule) 55749874f6eSJoseph Koshy errx(EX_SOFTWARE, "ERROR: a.out kernel modules are " 55849874f6eSJoseph Koshy "unsupported \"%s\"", path); 55949874f6eSJoseph Koshy 56049874f6eSJoseph Koshy (void) snprintf(buffer, sizeof(buffer), "%s%s", 56149874f6eSJoseph Koshy a->pa_fsroot, path); 56249874f6eSJoseph Koshy 56349874f6eSJoseph Koshy if ((fd = open(buffer, O_RDONLY, 0)) < 0 || 56449874f6eSJoseph Koshy (nbytes = read(fd, &ex, sizeof(ex))) < 0) { 56549874f6eSJoseph Koshy warn("WARNING: Cannot determine type of \"%s\"", path); 56649874f6eSJoseph Koshy image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE; 56749874f6eSJoseph Koshy if (fd != -1) 56849874f6eSJoseph Koshy (void) close(fd); 56949874f6eSJoseph Koshy return; 57049874f6eSJoseph Koshy } 57149874f6eSJoseph Koshy 57249874f6eSJoseph Koshy (void) close(fd); 57349874f6eSJoseph Koshy 57449874f6eSJoseph Koshy if ((unsigned) nbytes != sizeof(ex) || 57549874f6eSJoseph Koshy N_BADMAG(ex)) 57649874f6eSJoseph Koshy return; 57749874f6eSJoseph Koshy 57849874f6eSJoseph Koshy image->pi_type = PMCSTAT_IMAGE_AOUT; 57949874f6eSJoseph Koshy 58049874f6eSJoseph Koshy /* TODO: the rest of a.out processing */ 58149874f6eSJoseph Koshy 58249874f6eSJoseph Koshy return; 58349874f6eSJoseph Koshy } 58449874f6eSJoseph Koshy 58549874f6eSJoseph Koshy /* 58649874f6eSJoseph Koshy * Examine an ELF file to determine the size of its text segment. 58749874f6eSJoseph Koshy * Sets image->pi_type if anything conclusive can be determined about 58849874f6eSJoseph Koshy * this image. 58949874f6eSJoseph Koshy */ 59049874f6eSJoseph Koshy 59149874f6eSJoseph Koshy static void 59249874f6eSJoseph Koshy pmcstat_image_get_elf_params(struct pmcstat_image *image, 59349874f6eSJoseph Koshy struct pmcstat_args *a) 59415139246SJoseph Koshy { 59515139246SJoseph Koshy int fd, i; 59649874f6eSJoseph Koshy const char *path; 59715139246SJoseph Koshy void *mapbase; 59815139246SJoseph Koshy uintfptr_t minva, maxva; 59915139246SJoseph Koshy const Elf_Ehdr *h; 60015139246SJoseph Koshy const Elf_Phdr *ph; 60115139246SJoseph Koshy const Elf_Shdr *sh; 602887a8d04SJoseph Koshy #if defined(__amd64__) 603887a8d04SJoseph Koshy const Elf32_Ehdr *h32; 604887a8d04SJoseph Koshy const Elf32_Phdr *ph32; 605887a8d04SJoseph Koshy const Elf32_Shdr *sh32; 606887a8d04SJoseph Koshy #endif 60749874f6eSJoseph Koshy enum pmcstat_image_type image_type; 60849874f6eSJoseph Koshy struct stat st; 60949874f6eSJoseph Koshy char buffer[PATH_MAX]; 61015139246SJoseph Koshy 61160f918efSJoseph Koshy assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN); 61260f918efSJoseph Koshy 61315139246SJoseph Koshy minva = ~(uintfptr_t) 0; 61415139246SJoseph Koshy maxva = (uintfptr_t) 0; 61549874f6eSJoseph Koshy path = pmcstat_string_unintern(image->pi_execpath); 61615139246SJoseph Koshy 61749874f6eSJoseph Koshy assert(path != NULL); 61815139246SJoseph Koshy 61949874f6eSJoseph Koshy /* 62049874f6eSJoseph Koshy * Look for kernel modules under FSROOT/KERNELPATH/NAME, 62149874f6eSJoseph Koshy * and user mode executable objects under FSROOT/PATHNAME. 62249874f6eSJoseph Koshy */ 62349874f6eSJoseph Koshy if (image->pi_iskernelmodule) 62449874f6eSJoseph Koshy (void) snprintf(buffer, sizeof(buffer), "%s%s/%s", 62549874f6eSJoseph Koshy a->pa_fsroot, a->pa_kernel, path); 62649874f6eSJoseph Koshy else 62749874f6eSJoseph Koshy (void) snprintf(buffer, sizeof(buffer), "%s%s", 62849874f6eSJoseph Koshy a->pa_fsroot, path); 62915139246SJoseph Koshy 63049874f6eSJoseph Koshy if ((fd = open(buffer, O_RDONLY, 0)) < 0 || 63149874f6eSJoseph Koshy fstat(fd, &st) < 0 || 63249874f6eSJoseph Koshy (mapbase = mmap(0, st.st_size, PROT_READ, MAP_SHARED, 63349874f6eSJoseph Koshy fd, 0)) == MAP_FAILED) { 63449874f6eSJoseph Koshy warn("WARNING: Cannot determine type of \"%s\"", buffer); 63549874f6eSJoseph Koshy image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE; 63649874f6eSJoseph Koshy if (fd != -1) 63749874f6eSJoseph Koshy (void) close(fd); 63849874f6eSJoseph Koshy return; 63949874f6eSJoseph Koshy } 64015139246SJoseph Koshy 64115139246SJoseph Koshy (void) close(fd); 64215139246SJoseph Koshy 64349874f6eSJoseph Koshy /* Punt on non-ELF objects */ 64415139246SJoseph Koshy h = (const Elf_Ehdr *) mapbase; 64515139246SJoseph Koshy if (!IS_ELF(*h)) 64649874f6eSJoseph Koshy return; 64715139246SJoseph Koshy 64849874f6eSJoseph Koshy /* 64949874f6eSJoseph Koshy * We only handle executable ELF objects and kernel 65049874f6eSJoseph Koshy * modules. 65149874f6eSJoseph Koshy */ 65249874f6eSJoseph Koshy if (h->e_type != ET_EXEC && h->e_type != ET_DYN && 65349874f6eSJoseph Koshy !(image->pi_iskernelmodule && h->e_type == ET_REL)) 65449874f6eSJoseph Koshy return; 65549874f6eSJoseph Koshy 65649874f6eSJoseph Koshy image->pi_isdynamic = 0; 65749874f6eSJoseph Koshy image->pi_dynlinkerpath = NULL; 65849874f6eSJoseph Koshy image->pi_vaddr = 0; 65915139246SJoseph Koshy 660887a8d04SJoseph Koshy #define GET_VA(H, SH, MINVA, MAXVA) do { \ 661887a8d04SJoseph Koshy for (i = 0; i < (H)->e_shnum; i++) \ 662887a8d04SJoseph Koshy if ((SH)[i].sh_flags & SHF_EXECINSTR) { \ 663887a8d04SJoseph Koshy (MINVA) = min((MINVA),(SH)[i].sh_addr); \ 664887a8d04SJoseph Koshy (MAXVA) = max((MAXVA),(SH)[i].sh_addr + \ 665887a8d04SJoseph Koshy (SH)[i].sh_size); \ 666887a8d04SJoseph Koshy } \ 667887a8d04SJoseph Koshy } while (0) 668887a8d04SJoseph Koshy 669887a8d04SJoseph Koshy 670887a8d04SJoseph Koshy #define GET_PHDR_INFO(H, PH, IMAGE) do { \ 671887a8d04SJoseph Koshy for (i = 0; i < (H)->e_phnum; i++) { \ 672887a8d04SJoseph Koshy switch ((PH)[i].p_type) { \ 673887a8d04SJoseph Koshy case PT_DYNAMIC: \ 674887a8d04SJoseph Koshy image->pi_isdynamic = 1; \ 675887a8d04SJoseph Koshy break; \ 676887a8d04SJoseph Koshy case PT_INTERP: \ 677887a8d04SJoseph Koshy image->pi_dynlinkerpath = \ 678887a8d04SJoseph Koshy pmcstat_string_intern( \ 679887a8d04SJoseph Koshy (char *) mapbase + \ 680887a8d04SJoseph Koshy (PH)[i].p_offset); \ 681887a8d04SJoseph Koshy break; \ 68249874f6eSJoseph Koshy case PT_LOAD: \ 68349874f6eSJoseph Koshy if ((PH)[i].p_offset == 0) \ 68449874f6eSJoseph Koshy image->pi_vaddr = \ 68549874f6eSJoseph Koshy (PH)[i].p_vaddr; \ 68649874f6eSJoseph Koshy break; \ 687887a8d04SJoseph Koshy } \ 688887a8d04SJoseph Koshy } \ 689887a8d04SJoseph Koshy } while (0) 690887a8d04SJoseph Koshy 691887a8d04SJoseph Koshy switch (h->e_machine) { 692887a8d04SJoseph Koshy case EM_386: 693887a8d04SJoseph Koshy case EM_486: 694887a8d04SJoseph Koshy #if defined(__amd64__) 695887a8d04SJoseph Koshy /* a 32 bit executable */ 696887a8d04SJoseph Koshy h32 = (const Elf32_Ehdr *) h; 697887a8d04SJoseph Koshy sh32 = (const Elf32_Shdr *)((uintptr_t) mapbase + h32->e_shoff); 698887a8d04SJoseph Koshy 699887a8d04SJoseph Koshy GET_VA(h32, sh32, minva, maxva); 700887a8d04SJoseph Koshy 701887a8d04SJoseph Koshy image->pi_entry = h32->e_entry; 702887a8d04SJoseph Koshy 703887a8d04SJoseph Koshy if (h32->e_type == ET_EXEC) { 704887a8d04SJoseph Koshy ph32 = (const Elf32_Phdr *)((uintptr_t) mapbase + 705887a8d04SJoseph Koshy h32->e_phoff); 706887a8d04SJoseph Koshy GET_PHDR_INFO(h32, ph32, image); 707887a8d04SJoseph Koshy } 70849874f6eSJoseph Koshy image_type = PMCSTAT_IMAGE_ELF32; 709887a8d04SJoseph Koshy break; 710887a8d04SJoseph Koshy #endif 711887a8d04SJoseph Koshy default: 712887a8d04SJoseph Koshy sh = (const Elf_Shdr *)((uintptr_t) mapbase + h->e_shoff); 713887a8d04SJoseph Koshy 714887a8d04SJoseph Koshy GET_VA(h, sh, minva, maxva); 715887a8d04SJoseph Koshy 716887a8d04SJoseph Koshy image->pi_entry = h->e_entry; 71760f918efSJoseph Koshy 71815139246SJoseph Koshy if (h->e_type == ET_EXEC) { 719887a8d04SJoseph Koshy ph = (const Elf_Phdr *)((uintptr_t) mapbase + 720887a8d04SJoseph Koshy h->e_phoff); 721887a8d04SJoseph Koshy GET_PHDR_INFO(h, ph, image); 722887a8d04SJoseph Koshy } 72349874f6eSJoseph Koshy image_type = PMCSTAT_IMAGE_ELF64; 72415139246SJoseph Koshy break; 72515139246SJoseph Koshy } 726887a8d04SJoseph Koshy 727887a8d04SJoseph Koshy #undef GET_PHDR_INFO 728887a8d04SJoseph Koshy #undef GET_VA 729887a8d04SJoseph Koshy 730887a8d04SJoseph Koshy image->pi_start = minva; 731887a8d04SJoseph Koshy image->pi_end = maxva; 73249874f6eSJoseph Koshy image->pi_type = image_type; 73315139246SJoseph Koshy 73415139246SJoseph Koshy if (munmap(mapbase, st.st_size) < 0) 73515139246SJoseph Koshy err(EX_OSERR, "ERROR: Cannot unmap \"%s\"", path); 73649874f6eSJoseph Koshy return; 73749874f6eSJoseph Koshy } 73815139246SJoseph Koshy 73949874f6eSJoseph Koshy /* 74049874f6eSJoseph Koshy * Given an image descriptor, determine whether it is an ELF, or AOUT. 74149874f6eSJoseph Koshy * If no handler claims the image, set its type to 'INDETERMINABLE'. 74249874f6eSJoseph Koshy */ 74349874f6eSJoseph Koshy 74449874f6eSJoseph Koshy static void 74549874f6eSJoseph Koshy pmcstat_image_determine_type(struct pmcstat_image *image, 74649874f6eSJoseph Koshy struct pmcstat_args *a) 74749874f6eSJoseph Koshy { 74849874f6eSJoseph Koshy assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN); 74949874f6eSJoseph Koshy 75049874f6eSJoseph Koshy /* Try each kind of handler in turn */ 75149874f6eSJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 75249874f6eSJoseph Koshy pmcstat_image_get_elf_params(image, a); 75349874f6eSJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 75449874f6eSJoseph Koshy pmcstat_image_get_aout_params(image, a); 75549874f6eSJoseph Koshy 75649874f6eSJoseph Koshy /* 75749874f6eSJoseph Koshy * Otherwise, remember that we tried to determine 75849874f6eSJoseph Koshy * the object's type and had failed. 75949874f6eSJoseph Koshy */ 76049874f6eSJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 76149874f6eSJoseph Koshy image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE; 76215139246SJoseph Koshy } 76315139246SJoseph Koshy 76415139246SJoseph Koshy /* 76560f918efSJoseph Koshy * Locate an image descriptor given an interned path, adding a fresh 76660f918efSJoseph Koshy * descriptor to the cache if necessary. This function also finds a 76760f918efSJoseph Koshy * suitable name for this image's sample file. 76849874f6eSJoseph Koshy * 76949874f6eSJoseph Koshy * We defer filling in the file format specific parts of the image 77049874f6eSJoseph Koshy * structure till the time we actually see a sample that would fall 77149874f6eSJoseph Koshy * into this image. 77215139246SJoseph Koshy */ 77315139246SJoseph Koshy 77415139246SJoseph Koshy static struct pmcstat_image * 77549874f6eSJoseph Koshy pmcstat_image_from_path(pmcstat_interned_string internedpath, 77649874f6eSJoseph Koshy int iskernelmodule) 77715139246SJoseph Koshy { 77815139246SJoseph Koshy int count, hash, nlen; 77915139246SJoseph Koshy struct pmcstat_image *pi; 78015139246SJoseph Koshy char *sn; 78115139246SJoseph Koshy char name[NAME_MAX]; 78215139246SJoseph Koshy 78349874f6eSJoseph Koshy hash = pmcstat_string_lookup_hash(internedpath); 78415139246SJoseph Koshy 78549874f6eSJoseph Koshy /* First, look for an existing entry. */ 78615139246SJoseph Koshy LIST_FOREACH(pi, &pmcstat_image_hash[hash], pi_next) 78749874f6eSJoseph Koshy if (pi->pi_execpath == internedpath && 78849874f6eSJoseph Koshy pi->pi_iskernelmodule == iskernelmodule) { 78915139246SJoseph Koshy /* move descriptor to the head of the lru list */ 79015139246SJoseph Koshy TAILQ_REMOVE(&pmcstat_image_lru, pi, pi_lru); 79115139246SJoseph Koshy TAILQ_INSERT_HEAD(&pmcstat_image_lru, pi, pi_lru); 79249874f6eSJoseph Koshy return (pi); 79315139246SJoseph Koshy } 79415139246SJoseph Koshy 79515139246SJoseph Koshy /* 79660f918efSJoseph Koshy * Allocate a new entry and place at the head of the hash and 79760f918efSJoseph Koshy * LRU lists. 79815139246SJoseph Koshy */ 79915139246SJoseph Koshy pi = malloc(sizeof(*pi)); 80015139246SJoseph Koshy if (pi == NULL) 80149874f6eSJoseph Koshy return (NULL); 80215139246SJoseph Koshy 80315139246SJoseph Koshy pi->pi_type = PMCSTAT_IMAGE_UNKNOWN; 80449874f6eSJoseph Koshy pi->pi_execpath = internedpath; 80515139246SJoseph Koshy pi->pi_start = ~0; 80660f918efSJoseph Koshy pi->pi_entry = ~0; 80715139246SJoseph Koshy pi->pi_end = 0; 80849874f6eSJoseph Koshy pi->pi_iskernelmodule = iskernelmodule; 80915139246SJoseph Koshy 81060f918efSJoseph Koshy /* 81160f918efSJoseph Koshy * Look for a suitable name for the sample files associated 81260f918efSJoseph Koshy * with this image: if `basename(path)`+".gmon" is available, 81360f918efSJoseph Koshy * we use that, otherwise we try iterating through 81460f918efSJoseph Koshy * `basename(path)`+ "~" + NNN + ".gmon" till we get a free 81560f918efSJoseph Koshy * entry. 81660f918efSJoseph Koshy */ 81749874f6eSJoseph Koshy if ((sn = basename(pmcstat_string_unintern(internedpath))) == NULL) 81849874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Cannot process \"%s\"", 81949874f6eSJoseph Koshy pmcstat_string_unintern(internedpath)); 82015139246SJoseph Koshy 82115139246SJoseph Koshy nlen = strlen(sn); 82249874f6eSJoseph Koshy nlen = min(nlen, (int) (sizeof(name) - sizeof(".gmon"))); 82315139246SJoseph Koshy 82460f918efSJoseph Koshy snprintf(name, sizeof(name), "%.*s.gmon", nlen, sn); 82515139246SJoseph Koshy 82649874f6eSJoseph Koshy /* try use the unabridged name first */ 82715139246SJoseph Koshy if (pmcstat_string_lookup(name) == NULL) 82815139246SJoseph Koshy pi->pi_samplename = pmcstat_string_intern(name); 82915139246SJoseph Koshy else { 83049874f6eSJoseph Koshy /* 83149874f6eSJoseph Koshy * Otherwise use a prefix from the original name and 83249874f6eSJoseph Koshy * upto 3 digits. 83349874f6eSJoseph Koshy */ 83415139246SJoseph Koshy nlen = strlen(sn); 83549874f6eSJoseph Koshy nlen = min(nlen, (int) (sizeof(name)-sizeof("~NNN.gmon"))); 83615139246SJoseph Koshy count = 0; 83715139246SJoseph Koshy do { 83849874f6eSJoseph Koshy if (++count > 999) 83949874f6eSJoseph Koshy errx(EX_CANTCREAT, "ERROR: cannot create a gmon " 84049874f6eSJoseph Koshy "file for \"%s\"", name); 84149874f6eSJoseph Koshy snprintf(name, sizeof(name), "%.*s~%3.3d.gmon", 84215139246SJoseph Koshy nlen, sn, count); 84315139246SJoseph Koshy if (pmcstat_string_lookup(name) == NULL) { 84415139246SJoseph Koshy pi->pi_samplename = pmcstat_string_intern(name); 84515139246SJoseph Koshy count = 0; 84615139246SJoseph Koshy } 84715139246SJoseph Koshy } while (count > 0); 84815139246SJoseph Koshy } 84915139246SJoseph Koshy 85049874f6eSJoseph Koshy 85115139246SJoseph Koshy LIST_INIT(&pi->pi_gmlist); 85215139246SJoseph Koshy 85315139246SJoseph Koshy LIST_INSERT_HEAD(&pmcstat_image_hash[hash], pi, pi_next); 85415139246SJoseph Koshy TAILQ_INSERT_HEAD(&pmcstat_image_lru, pi, pi_lru); 85515139246SJoseph Koshy 85649874f6eSJoseph Koshy return (pi); 85715139246SJoseph Koshy } 85815139246SJoseph Koshy 85915139246SJoseph Koshy /* 86015139246SJoseph Koshy * Increment the bucket in the gmon.out file corresponding to 'pmcid' 86115139246SJoseph Koshy * and 'pc'. 86215139246SJoseph Koshy */ 86315139246SJoseph Koshy 86415139246SJoseph Koshy static void 86515139246SJoseph Koshy pmcstat_image_increment_bucket(struct pmcstat_pcmap *map, uintfptr_t pc, 86615139246SJoseph Koshy pmc_id_t pmcid, struct pmcstat_args *a) 86715139246SJoseph Koshy { 86815139246SJoseph Koshy struct pmcstat_image *image; 86915139246SJoseph Koshy struct pmcstat_gmonfile *pgf; 87015139246SJoseph Koshy uintfptr_t bucket; 87115139246SJoseph Koshy HISTCOUNTER *hc; 87215139246SJoseph Koshy 87315139246SJoseph Koshy assert(pc >= map->ppm_lowpc && pc < map->ppm_highpc); 87415139246SJoseph Koshy 87549874f6eSJoseph Koshy image = map->ppm_image; 87649874f6eSJoseph Koshy 87749874f6eSJoseph Koshy /* 87849874f6eSJoseph Koshy * If this is the first time we are seeing a sample for 87949874f6eSJoseph Koshy * this executable image, try determine its parameters. 88049874f6eSJoseph Koshy */ 88149874f6eSJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 88249874f6eSJoseph Koshy pmcstat_image_determine_type(image, a); 88349874f6eSJoseph Koshy 88449874f6eSJoseph Koshy assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN); 88549874f6eSJoseph Koshy 88649874f6eSJoseph Koshy /* Ignore samples in images that we know nothing about. */ 88749874f6eSJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_INDETERMINABLE) { 88849874f6eSJoseph Koshy pmcstat_stats.ps_samples_indeterminable++; 88949874f6eSJoseph Koshy return; 89049874f6eSJoseph Koshy } 89149874f6eSJoseph Koshy 89215139246SJoseph Koshy /* 89315139246SJoseph Koshy * Find the gmon file corresponding to 'pmcid', creating it if 89415139246SJoseph Koshy * needed. 89515139246SJoseph Koshy */ 89615139246SJoseph Koshy LIST_FOREACH(pgf, &image->pi_gmlist, pgf_next) 89715139246SJoseph Koshy if (pgf->pgf_pmcid == pmcid) 89815139246SJoseph Koshy break; 89915139246SJoseph Koshy 90015139246SJoseph Koshy /* If we don't have a gmon.out file for this PMCid, create one */ 90115139246SJoseph Koshy if (pgf == NULL) { 90215139246SJoseph Koshy if ((pgf = calloc(1, sizeof(*pgf))) == NULL) 90315139246SJoseph Koshy err(EX_OSERR, "ERROR:"); 90415139246SJoseph Koshy 90515139246SJoseph Koshy pgf->pgf_gmondata = NULL; /* mark as unmapped */ 90615139246SJoseph Koshy pgf->pgf_name = pmcstat_gmon_create_name(a->pa_samplesdir, 90715139246SJoseph Koshy image, pmcid); 90815139246SJoseph Koshy pgf->pgf_pmcid = pmcid; 90960f918efSJoseph Koshy assert(image->pi_end > image->pi_start); 910a566429aSJoseph Koshy pgf->pgf_nbuckets = (image->pi_end - image->pi_start) / 91115139246SJoseph Koshy FUNCTION_ALIGNMENT; /* see <machine/profile.h> */ 91215139246SJoseph Koshy pgf->pgf_ndatabytes = sizeof(struct gmonhdr) + 913a566429aSJoseph Koshy pgf->pgf_nbuckets * sizeof(HISTCOUNTER); 9142a6d2e9cSJoseph Koshy pgf->pgf_nsamples = 0; 91515139246SJoseph Koshy 91615139246SJoseph Koshy pmcstat_gmon_create_file(pgf, image); 91715139246SJoseph Koshy 91815139246SJoseph Koshy LIST_INSERT_HEAD(&image->pi_gmlist, pgf, pgf_next); 91915139246SJoseph Koshy } 92015139246SJoseph Koshy 92115139246SJoseph Koshy /* 92215139246SJoseph Koshy * Map the gmon file in if needed. It may have been mapped 92315139246SJoseph Koshy * out under memory pressure. 92415139246SJoseph Koshy */ 92515139246SJoseph Koshy if (pgf->pgf_gmondata == NULL) 92615139246SJoseph Koshy pmcstat_gmon_map_file(pgf); 92715139246SJoseph Koshy 92849874f6eSJoseph Koshy assert(pgf->pgf_gmondata != NULL); 92949874f6eSJoseph Koshy 93049874f6eSJoseph Koshy /* 93149874f6eSJoseph Koshy * 93249874f6eSJoseph Koshy */ 93349874f6eSJoseph Koshy 93415139246SJoseph Koshy bucket = (pc - map->ppm_lowpc) / FUNCTION_ALIGNMENT; 93515139246SJoseph Koshy 936a566429aSJoseph Koshy assert(bucket < pgf->pgf_nbuckets); 93715139246SJoseph Koshy 938bf840f4aSJoseph Koshy hc = (HISTCOUNTER *) ((uintptr_t) pgf->pgf_gmondata + 93915139246SJoseph Koshy sizeof(struct gmonhdr)); 940a566429aSJoseph Koshy 941a566429aSJoseph Koshy /* saturating add */ 94249874f6eSJoseph Koshy if (hc[bucket] < 0xFFFFU) /* XXX tie this to sizeof(HISTCOUNTER) */ 94315139246SJoseph Koshy hc[bucket]++; 94449874f6eSJoseph Koshy else /* mark that an overflow occurred */ 94549874f6eSJoseph Koshy pgf->pgf_overflow = 1; 9462a6d2e9cSJoseph Koshy 9472a6d2e9cSJoseph Koshy pgf->pgf_nsamples++; 94815139246SJoseph Koshy } 94915139246SJoseph Koshy 95015139246SJoseph Koshy /* 95149874f6eSJoseph Koshy * Record the fact that PC values from 'start' to 'end' come from 95215139246SJoseph Koshy * image 'image'. 95315139246SJoseph Koshy */ 95415139246SJoseph Koshy 95515139246SJoseph Koshy static void 95615139246SJoseph Koshy pmcstat_image_link(struct pmcstat_process *pp, struct pmcstat_image *image, 95749874f6eSJoseph Koshy uintfptr_t start) 95815139246SJoseph Koshy { 95915139246SJoseph Koshy struct pmcstat_pcmap *pcm, *pcmnew; 96049874f6eSJoseph Koshy uintfptr_t offset; 96149874f6eSJoseph Koshy 96249874f6eSJoseph Koshy assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN && 96349874f6eSJoseph Koshy image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE); 96415139246SJoseph Koshy 96515139246SJoseph Koshy if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL) 96649874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Cannot create a map entry"); 96715139246SJoseph Koshy 96849874f6eSJoseph Koshy /* 96949874f6eSJoseph Koshy * Adjust the map entry to only cover the text portion 97049874f6eSJoseph Koshy * of the object. 97149874f6eSJoseph Koshy */ 97249874f6eSJoseph Koshy 97349874f6eSJoseph Koshy offset = start - image->pi_vaddr; 97449874f6eSJoseph Koshy pcmnew->ppm_lowpc = image->pi_start + offset; 97549874f6eSJoseph Koshy pcmnew->ppm_highpc = image->pi_end + offset; 97615139246SJoseph Koshy pcmnew->ppm_image = image; 97715139246SJoseph Koshy 97849874f6eSJoseph Koshy assert(pcmnew->ppm_lowpc < pcmnew->ppm_highpc); 97949874f6eSJoseph Koshy 98049874f6eSJoseph Koshy /* Overlapped mmap()'s are assumed to never occur. */ 98115139246SJoseph Koshy TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next) 98249874f6eSJoseph Koshy if (pcm->ppm_lowpc >= pcmnew->ppm_highpc) 98315139246SJoseph Koshy break; 98415139246SJoseph Koshy 98515139246SJoseph Koshy if (pcm == NULL) 98615139246SJoseph Koshy TAILQ_INSERT_TAIL(&pp->pp_map, pcmnew, ppm_next); 98715139246SJoseph Koshy else 98815139246SJoseph Koshy TAILQ_INSERT_BEFORE(pcm, pcmnew, ppm_next); 98915139246SJoseph Koshy } 99015139246SJoseph Koshy 99115139246SJoseph Koshy /* 99249874f6eSJoseph Koshy * Unmap images in the range [start..end) associated with process 99349874f6eSJoseph Koshy * 'pp'. 99449874f6eSJoseph Koshy */ 99549874f6eSJoseph Koshy 99649874f6eSJoseph Koshy static void 99749874f6eSJoseph Koshy pmcstat_image_unmap(struct pmcstat_process *pp, uintfptr_t start, 99849874f6eSJoseph Koshy uintfptr_t end) 99949874f6eSJoseph Koshy { 100049874f6eSJoseph Koshy struct pmcstat_pcmap *pcm, *pcmtmp, *pcmnew; 100149874f6eSJoseph Koshy 100249874f6eSJoseph Koshy assert(pp != NULL); 100349874f6eSJoseph Koshy assert(start < end); 100449874f6eSJoseph Koshy 100549874f6eSJoseph Koshy /* 100649874f6eSJoseph Koshy * Cases: 100749874f6eSJoseph Koshy * - we could have the range completely in the middle of an 100849874f6eSJoseph Koshy * existing pcmap; in this case we have to split the pcmap 100949874f6eSJoseph Koshy * structure into two (i.e., generate a 'hole'). 101049874f6eSJoseph Koshy * - we could have the range covering multiple pcmaps; these 101149874f6eSJoseph Koshy * will have to be removed. 101249874f6eSJoseph Koshy * - we could have either 'start' or 'end' falling in the 101349874f6eSJoseph Koshy * middle of a pcmap; in this case shorten the entry. 101449874f6eSJoseph Koshy */ 101549874f6eSJoseph Koshy 101649874f6eSJoseph Koshy TAILQ_FOREACH_SAFE(pcm, &pp->pp_map, ppm_next, pcmtmp) { 101749874f6eSJoseph Koshy assert(pcm->ppm_lowpc < pcm->ppm_highpc); 101849874f6eSJoseph Koshy if (pcm->ppm_highpc <= start) 101949874f6eSJoseph Koshy continue; 102049874f6eSJoseph Koshy if (pcm->ppm_lowpc > end) 102149874f6eSJoseph Koshy return; 102249874f6eSJoseph Koshy if (pcm->ppm_lowpc >= start && pcm->ppm_highpc <= end) { 102349874f6eSJoseph Koshy /* 102449874f6eSJoseph Koshy * The current pcmap is completely inside the 102549874f6eSJoseph Koshy * unmapped range: remove it entirely. 102649874f6eSJoseph Koshy */ 102749874f6eSJoseph Koshy TAILQ_REMOVE(&pp->pp_map, pcm, ppm_next); 102849874f6eSJoseph Koshy free(pcm); 102949874f6eSJoseph Koshy } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc > end) { 103049874f6eSJoseph Koshy /* 103149874f6eSJoseph Koshy * Split this pcmap into two; curtail the 103249874f6eSJoseph Koshy * current map to end at [start-1], and start 103349874f6eSJoseph Koshy * the new one at [end]. 103449874f6eSJoseph Koshy */ 103549874f6eSJoseph Koshy if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL) 103649874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Cannot split a map " 103749874f6eSJoseph Koshy "entry"); 103849874f6eSJoseph Koshy 103949874f6eSJoseph Koshy pcmnew->ppm_image = pcm->ppm_image; 104049874f6eSJoseph Koshy 104149874f6eSJoseph Koshy pcmnew->ppm_lowpc = end; 104249874f6eSJoseph Koshy pcmnew->ppm_highpc = pcm->ppm_highpc; 104349874f6eSJoseph Koshy 104449874f6eSJoseph Koshy pcm->ppm_highpc = start; 104549874f6eSJoseph Koshy 104649874f6eSJoseph Koshy TAILQ_INSERT_AFTER(&pp->pp_map, pcm, pcmnew, ppm_next); 104749874f6eSJoseph Koshy 104849874f6eSJoseph Koshy return; 104949874f6eSJoseph Koshy } else if (pcm->ppm_lowpc < start) 105049874f6eSJoseph Koshy pcm->ppm_lowpc = start; 105149874f6eSJoseph Koshy else if (pcm->ppm_highpc > end) 105249874f6eSJoseph Koshy pcm->ppm_highpc = end; 105349874f6eSJoseph Koshy else 105449874f6eSJoseph Koshy assert(0); 105549874f6eSJoseph Koshy } 105649874f6eSJoseph Koshy } 105749874f6eSJoseph Koshy 105849874f6eSJoseph Koshy /* 105915139246SJoseph Koshy * Add a {pmcid,name} mapping. 106015139246SJoseph Koshy */ 106115139246SJoseph Koshy 106215139246SJoseph Koshy static void 106349874f6eSJoseph Koshy pmcstat_pmcid_add(pmc_id_t pmcid, pmcstat_interned_string ps, 106449874f6eSJoseph Koshy struct pmcstat_args *a) 106515139246SJoseph Koshy { 106615139246SJoseph Koshy struct pmcstat_pmcrecord *pr; 106715139246SJoseph Koshy struct stat st; 106815139246SJoseph Koshy char fullpath[PATH_MAX]; 106915139246SJoseph Koshy 107015139246SJoseph Koshy LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 107115139246SJoseph Koshy if (pr->pr_pmcid == pmcid) { 107249874f6eSJoseph Koshy pr->pr_pmcname = ps; 107315139246SJoseph Koshy return; 107415139246SJoseph Koshy } 107515139246SJoseph Koshy 107615139246SJoseph Koshy if ((pr = malloc(sizeof(*pr))) == NULL) 107715139246SJoseph Koshy err(EX_OSERR, "ERROR: Cannot allocate pmc record"); 107815139246SJoseph Koshy 107915139246SJoseph Koshy pr->pr_pmcid = pmcid; 108049874f6eSJoseph Koshy pr->pr_pmcname = ps; 108115139246SJoseph Koshy LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next); 108215139246SJoseph Koshy 108315139246SJoseph Koshy (void) snprintf(fullpath, sizeof(fullpath), "%s/%s", a->pa_samplesdir, 108449874f6eSJoseph Koshy pmcstat_string_unintern(ps)); 108515139246SJoseph Koshy 108615139246SJoseph Koshy /* If the path name exists, it should be a directory */ 108715139246SJoseph Koshy if (stat(fullpath, &st) == 0 && S_ISDIR(st.st_mode)) 108815139246SJoseph Koshy return; 108915139246SJoseph Koshy 109015139246SJoseph Koshy if (mkdir(fullpath, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0) 109115139246SJoseph Koshy err(EX_OSERR, "ERROR: Cannot create directory \"%s\"", 109215139246SJoseph Koshy fullpath); 109315139246SJoseph Koshy } 109415139246SJoseph Koshy 109515139246SJoseph Koshy /* 109649874f6eSJoseph Koshy * Given a pmcid in use, find its human-readable name. 109715139246SJoseph Koshy */ 109815139246SJoseph Koshy 109915139246SJoseph Koshy static const char * 110015139246SJoseph Koshy pmcstat_pmcid_to_name(pmc_id_t pmcid) 110115139246SJoseph Koshy { 110215139246SJoseph Koshy struct pmcstat_pmcrecord *pr; 110315139246SJoseph Koshy char fullpath[PATH_MAX]; 110415139246SJoseph Koshy 110515139246SJoseph Koshy LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 110615139246SJoseph Koshy if (pr->pr_pmcid == pmcid) 110749874f6eSJoseph Koshy return (pmcstat_string_unintern(pr->pr_pmcname)); 110815139246SJoseph Koshy 110915139246SJoseph Koshy /* create a default name and add this entry */ 111015139246SJoseph Koshy if ((pr = malloc(sizeof(*pr))) == NULL) 111115139246SJoseph Koshy err(EX_OSERR, "ERROR: "); 111215139246SJoseph Koshy pr->pr_pmcid = pmcid; 111315139246SJoseph Koshy 111415139246SJoseph Koshy (void) snprintf(fullpath, sizeof(fullpath), "%X", (unsigned int) pmcid); 111515139246SJoseph Koshy pr->pr_pmcname = pmcstat_string_intern(fullpath); 111615139246SJoseph Koshy 111715139246SJoseph Koshy LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next); 111815139246SJoseph Koshy 111949874f6eSJoseph Koshy return (pmcstat_string_unintern(pr->pr_pmcname)); 112015139246SJoseph Koshy } 112115139246SJoseph Koshy 112215139246SJoseph Koshy /* 112349874f6eSJoseph Koshy * Associate an AOUT image with a process. 112415139246SJoseph Koshy */ 112515139246SJoseph Koshy 112615139246SJoseph Koshy static void 112749874f6eSJoseph Koshy pmcstat_process_aout_exec(struct pmcstat_process *pp, 112849874f6eSJoseph Koshy struct pmcstat_image *image, uintfptr_t entryaddr, 112949874f6eSJoseph Koshy struct pmcstat_args *a) 113015139246SJoseph Koshy { 113149874f6eSJoseph Koshy (void) pp; 113249874f6eSJoseph Koshy (void) image; 113349874f6eSJoseph Koshy (void) entryaddr; 113449874f6eSJoseph Koshy (void) a; 113549874f6eSJoseph Koshy /* TODO Implement a.out handling */ 113649874f6eSJoseph Koshy } 113749874f6eSJoseph Koshy 113849874f6eSJoseph Koshy /* 113949874f6eSJoseph Koshy * Associate an ELF image with a process. 114049874f6eSJoseph Koshy */ 114149874f6eSJoseph Koshy 114249874f6eSJoseph Koshy static void 114349874f6eSJoseph Koshy pmcstat_process_elf_exec(struct pmcstat_process *pp, 114449874f6eSJoseph Koshy struct pmcstat_image *image, uintfptr_t entryaddr, 114549874f6eSJoseph Koshy struct pmcstat_args *a) 114649874f6eSJoseph Koshy { 114715139246SJoseph Koshy uintmax_t libstart; 114849874f6eSJoseph Koshy struct pmcstat_image *rtldimage; 114915139246SJoseph Koshy 115049874f6eSJoseph Koshy assert(image->pi_type == PMCSTAT_IMAGE_ELF32 || 115149874f6eSJoseph Koshy image->pi_type == PMCSTAT_IMAGE_ELF64); 115215139246SJoseph Koshy 115360f918efSJoseph Koshy /* Create a map entry for the base executable. */ 115449874f6eSJoseph Koshy pmcstat_image_link(pp, image, image->pi_vaddr); 115515139246SJoseph Koshy 115660f918efSJoseph Koshy /* 115760f918efSJoseph Koshy * For dynamically linked executables we need to: 115860f918efSJoseph Koshy * (a) find where the dynamic linker was mapped to for this 115960f918efSJoseph Koshy * process, 116060f918efSJoseph Koshy * (b) find all the executable objects that the dynamic linker 116160f918efSJoseph Koshy * brought in. 116260f918efSJoseph Koshy */ 116349874f6eSJoseph Koshy 116415139246SJoseph Koshy if (image->pi_isdynamic) { 116515139246SJoseph Koshy 116660f918efSJoseph Koshy /* 116760f918efSJoseph Koshy * The runtime loader gets loaded just after the maximum 116860f918efSJoseph Koshy * possible heap address. Like so: 116960f918efSJoseph Koshy * 117060f918efSJoseph Koshy * [ TEXT DATA BSS HEAP -->*RTLD SHLIBS <--STACK] 117160f918efSJoseph Koshy * ^ ^ 117260f918efSJoseph Koshy * 0 VM_MAXUSER_ADDRESS 117349874f6eSJoseph Koshy 117460f918efSJoseph Koshy * 117560f918efSJoseph Koshy * The exact address where the loader gets mapped in 117660f918efSJoseph Koshy * will vary according to the size of the executable 117760f918efSJoseph Koshy * and the limits on the size of the process'es data 117860f918efSJoseph Koshy * segment at the time of exec(). The entry address 117960f918efSJoseph Koshy * recorded at process exec time corresponds to the 118060f918efSJoseph Koshy * 'start' address inside the dynamic linker. From 118160f918efSJoseph Koshy * this we can figure out the address where the 118260f918efSJoseph Koshy * runtime loader's file object had been mapped to. 118360f918efSJoseph Koshy */ 118449874f6eSJoseph Koshy rtldimage = pmcstat_image_from_path(image->pi_dynlinkerpath, 118549874f6eSJoseph Koshy 0); 118649874f6eSJoseph Koshy if (rtldimage == NULL) { 118749874f6eSJoseph Koshy warnx("WARNING: Cannot find image for \"%s\".", 118849874f6eSJoseph Koshy pmcstat_string_unintern(image->pi_dynlinkerpath)); 118949874f6eSJoseph Koshy pmcstat_stats.ps_exec_errors++; 119049874f6eSJoseph Koshy return; 119149874f6eSJoseph Koshy } 119249874f6eSJoseph Koshy 119360f918efSJoseph Koshy if (rtldimage->pi_type == PMCSTAT_IMAGE_UNKNOWN) 119449874f6eSJoseph Koshy pmcstat_image_get_elf_params(rtldimage, a); 119549874f6eSJoseph Koshy 119649874f6eSJoseph Koshy if (rtldimage->pi_type != PMCSTAT_IMAGE_ELF32 && 119749874f6eSJoseph Koshy rtldimage->pi_type != PMCSTAT_IMAGE_ELF64) { 119849874f6eSJoseph Koshy warnx("WARNING: rtld not an ELF object \"%s\".", 119949874f6eSJoseph Koshy pmcstat_string_unintern(image->pi_dynlinkerpath)); 120049874f6eSJoseph Koshy return; 120149874f6eSJoseph Koshy } 120260f918efSJoseph Koshy 120360f918efSJoseph Koshy libstart = entryaddr - rtldimage->pi_entry; 120449874f6eSJoseph Koshy pmcstat_image_link(pp, rtldimage, libstart); 120515139246SJoseph Koshy } 120615139246SJoseph Koshy } 120715139246SJoseph Koshy 120815139246SJoseph Koshy /* 120915139246SJoseph Koshy * Find the process descriptor corresponding to a PID. If 'allocate' 121015139246SJoseph Koshy * is zero, we return a NULL if a pid descriptor could not be found or 121115139246SJoseph Koshy * a process descriptor process. If 'allocate' is non-zero, then we 121215139246SJoseph Koshy * will attempt to allocate a fresh process descriptor. Zombie 121315139246SJoseph Koshy * process descriptors are only removed if a fresh allocation for the 121415139246SJoseph Koshy * same PID is requested. 121515139246SJoseph Koshy */ 121615139246SJoseph Koshy 121715139246SJoseph Koshy static struct pmcstat_process * 121815139246SJoseph Koshy pmcstat_process_lookup(pid_t pid, int allocate) 121915139246SJoseph Koshy { 122015139246SJoseph Koshy uint32_t hash; 122115139246SJoseph Koshy struct pmcstat_pcmap *ppm, *ppmtmp; 122215139246SJoseph Koshy struct pmcstat_process *pp, *pptmp; 122315139246SJoseph Koshy 122415139246SJoseph Koshy hash = (uint32_t) pid & PMCSTAT_HASH_MASK; /* simplicity wins */ 122515139246SJoseph Koshy 122615139246SJoseph Koshy LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[hash], pp_next, pptmp) 122715139246SJoseph Koshy if (pp->pp_pid == pid) { 122815139246SJoseph Koshy /* Found a descriptor, check and process zombies */ 122949874f6eSJoseph Koshy if (allocate && pp->pp_isactive == 0) { 123015139246SJoseph Koshy /* remove maps */ 123115139246SJoseph Koshy TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, 123215139246SJoseph Koshy ppmtmp) { 123315139246SJoseph Koshy TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next); 123415139246SJoseph Koshy free(ppm); 123515139246SJoseph Koshy } 123615139246SJoseph Koshy /* remove process entry */ 123715139246SJoseph Koshy LIST_REMOVE(pp, pp_next); 123815139246SJoseph Koshy free(pp); 123915139246SJoseph Koshy break; 124015139246SJoseph Koshy } 124149874f6eSJoseph Koshy return (pp); 124215139246SJoseph Koshy } 124315139246SJoseph Koshy 124415139246SJoseph Koshy if (!allocate) 124549874f6eSJoseph Koshy return (NULL); 124615139246SJoseph Koshy 124715139246SJoseph Koshy if ((pp = malloc(sizeof(*pp))) == NULL) 124815139246SJoseph Koshy err(EX_OSERR, "ERROR: Cannot allocate pid descriptor"); 124915139246SJoseph Koshy 125015139246SJoseph Koshy pp->pp_pid = pid; 125115139246SJoseph Koshy pp->pp_isactive = 1; 125215139246SJoseph Koshy 125315139246SJoseph Koshy TAILQ_INIT(&pp->pp_map); 125415139246SJoseph Koshy 125515139246SJoseph Koshy LIST_INSERT_HEAD(&pmcstat_process_hash[hash], pp, pp_next); 125649874f6eSJoseph Koshy return (pp); 125715139246SJoseph Koshy } 125815139246SJoseph Koshy 125915139246SJoseph Koshy /* 126015139246SJoseph Koshy * Associate an image and a process. 126115139246SJoseph Koshy */ 126215139246SJoseph Koshy 126315139246SJoseph Koshy static void 126449874f6eSJoseph Koshy pmcstat_process_exec(struct pmcstat_process *pp, 126549874f6eSJoseph Koshy pmcstat_interned_string path, uintfptr_t entryaddr, 126649874f6eSJoseph Koshy struct pmcstat_args *a) 126715139246SJoseph Koshy { 126815139246SJoseph Koshy struct pmcstat_image *image; 126915139246SJoseph Koshy 127049874f6eSJoseph Koshy if ((image = pmcstat_image_from_path(path, 0)) == NULL) { 127149874f6eSJoseph Koshy pmcstat_stats.ps_exec_errors++; 127215139246SJoseph Koshy return; 127349874f6eSJoseph Koshy } 127415139246SJoseph Koshy 127515139246SJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 127649874f6eSJoseph Koshy pmcstat_image_determine_type(image, a); 127715139246SJoseph Koshy 127849874f6eSJoseph Koshy assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN); 127949874f6eSJoseph Koshy 128049874f6eSJoseph Koshy switch (image->pi_type) { 128149874f6eSJoseph Koshy case PMCSTAT_IMAGE_ELF32: 128249874f6eSJoseph Koshy case PMCSTAT_IMAGE_ELF64: 128349874f6eSJoseph Koshy pmcstat_stats.ps_exec_elf++; 128449874f6eSJoseph Koshy pmcstat_process_elf_exec(pp, image, entryaddr, a); 128515139246SJoseph Koshy break; 128615139246SJoseph Koshy 128715139246SJoseph Koshy case PMCSTAT_IMAGE_AOUT: 128849874f6eSJoseph Koshy pmcstat_stats.ps_exec_aout++; 128949874f6eSJoseph Koshy pmcstat_process_aout_exec(pp, image, entryaddr, a); 129049874f6eSJoseph Koshy break; 129149874f6eSJoseph Koshy 129249874f6eSJoseph Koshy case PMCSTAT_IMAGE_INDETERMINABLE: 129349874f6eSJoseph Koshy pmcstat_stats.ps_exec_indeterminable++; 129415139246SJoseph Koshy break; 129515139246SJoseph Koshy 129615139246SJoseph Koshy default: 129760f918efSJoseph Koshy err(EX_SOFTWARE, "ERROR: Unsupported executable type for " 129849874f6eSJoseph Koshy "\"%s\"", pmcstat_string_unintern(path)); 129915139246SJoseph Koshy } 130015139246SJoseph Koshy } 130115139246SJoseph Koshy 130215139246SJoseph Koshy 130360f918efSJoseph Koshy /* 130460f918efSJoseph Koshy * Find the map entry associated with process 'p' at PC value 'pc'. 130560f918efSJoseph Koshy */ 130660f918efSJoseph Koshy 130760f918efSJoseph Koshy static struct pmcstat_pcmap * 130860f918efSJoseph Koshy pmcstat_process_find_map(struct pmcstat_process *p, uintfptr_t pc) 130960f918efSJoseph Koshy { 131060f918efSJoseph Koshy struct pmcstat_pcmap *ppm; 131160f918efSJoseph Koshy 131249874f6eSJoseph Koshy TAILQ_FOREACH(ppm, &p->pp_map, ppm_next) { 131360f918efSJoseph Koshy if (pc >= ppm->ppm_lowpc && pc < ppm->ppm_highpc) 131449874f6eSJoseph Koshy return (ppm); 131549874f6eSJoseph Koshy if (pc < ppm->ppm_lowpc) 131649874f6eSJoseph Koshy return (NULL); 131749874f6eSJoseph Koshy } 131860f918efSJoseph Koshy 131949874f6eSJoseph Koshy return (NULL); 132060f918efSJoseph Koshy } 132160f918efSJoseph Koshy 132215139246SJoseph Koshy 132315139246SJoseph Koshy 132415139246SJoseph Koshy static int 132515139246SJoseph Koshy pmcstat_convert_log(struct pmcstat_args *a) 132615139246SJoseph Koshy { 132715139246SJoseph Koshy uintfptr_t pc; 132849874f6eSJoseph Koshy pid_t pid; 132949874f6eSJoseph Koshy struct pmcstat_image *image; 133015139246SJoseph Koshy struct pmcstat_process *pp, *ppnew; 133115139246SJoseph Koshy struct pmcstat_pcmap *ppm, *ppmtmp; 133215139246SJoseph Koshy struct pmclog_ev ev; 133349874f6eSJoseph Koshy pmcstat_interned_string image_path; 133415139246SJoseph Koshy 133515139246SJoseph Koshy while (pmclog_read(a->pa_logparser, &ev) == 0) { 133615139246SJoseph Koshy assert(ev.pl_state == PMCLOG_OK); 133715139246SJoseph Koshy 133815139246SJoseph Koshy switch (ev.pl_type) { 133949874f6eSJoseph Koshy case PMCLOG_TYPE_INITIALIZE: 134049874f6eSJoseph Koshy if ((ev.pl_u.pl_i.pl_version & 0xFF000000) != 134149874f6eSJoseph Koshy PMC_VERSION_MAJOR << 24 && a->pa_verbosity > 0) 134249874f6eSJoseph Koshy warnx("WARNING: Log version 0x%x does not " 134349874f6eSJoseph Koshy "match compiled version 0x%x.", 134449874f6eSJoseph Koshy ev.pl_u.pl_i.pl_version, 134549874f6eSJoseph Koshy PMC_VERSION_MAJOR); 134649874f6eSJoseph Koshy break; 134749874f6eSJoseph Koshy case PMCLOG_TYPE_MAP_IN: 134815139246SJoseph Koshy /* 134915139246SJoseph Koshy * Introduce an address range mapping for a 135049874f6eSJoseph Koshy * userland process or the kernel (pid == -1). 135149874f6eSJoseph Koshy * 135249874f6eSJoseph Koshy * We always allocate a process descriptor so 135349874f6eSJoseph Koshy * that subsequent samples seen for this 135449874f6eSJoseph Koshy * address range are mapped to the current 135549874f6eSJoseph Koshy * object being mapped in. 135615139246SJoseph Koshy */ 135749874f6eSJoseph Koshy pid = ev.pl_u.pl_mi.pl_pid; 135849874f6eSJoseph Koshy if (pid == -1) 135949874f6eSJoseph Koshy pp = pmcstat_kernproc; 136049874f6eSJoseph Koshy else 136149874f6eSJoseph Koshy pp = pmcstat_process_lookup(pid, 136249874f6eSJoseph Koshy PMCSTAT_ALLOCATE); 136349874f6eSJoseph Koshy 136449874f6eSJoseph Koshy assert(pp != NULL); 136549874f6eSJoseph Koshy 136649874f6eSJoseph Koshy image_path = pmcstat_string_intern(ev.pl_u.pl_mi. 136749874f6eSJoseph Koshy pl_pathname); 136849874f6eSJoseph Koshy image = pmcstat_image_from_path(image_path, pid == -1); 136949874f6eSJoseph Koshy if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 137049874f6eSJoseph Koshy pmcstat_image_determine_type(image, a); 137149874f6eSJoseph Koshy if (image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE) 137249874f6eSJoseph Koshy pmcstat_image_link(pp, image, 137349874f6eSJoseph Koshy ev.pl_u.pl_mi.pl_start); 137449874f6eSJoseph Koshy break; 137549874f6eSJoseph Koshy 137649874f6eSJoseph Koshy case PMCLOG_TYPE_MAP_OUT: 137749874f6eSJoseph Koshy /* 137849874f6eSJoseph Koshy * Remove an address map. 137949874f6eSJoseph Koshy */ 138049874f6eSJoseph Koshy pid = ev.pl_u.pl_mo.pl_pid; 138149874f6eSJoseph Koshy if (pid == -1) 138249874f6eSJoseph Koshy pp = pmcstat_kernproc; 138349874f6eSJoseph Koshy else 138449874f6eSJoseph Koshy pp = pmcstat_process_lookup(pid, 0); 138549874f6eSJoseph Koshy 138649874f6eSJoseph Koshy if (pp == NULL) /* unknown process */ 138749874f6eSJoseph Koshy break; 138849874f6eSJoseph Koshy 138949874f6eSJoseph Koshy pmcstat_image_unmap(pp, ev.pl_u.pl_mo.pl_start, 139049874f6eSJoseph Koshy ev.pl_u.pl_mo.pl_end); 139115139246SJoseph Koshy break; 139215139246SJoseph Koshy 139315139246SJoseph Koshy case PMCLOG_TYPE_PCSAMPLE: 139415139246SJoseph Koshy 139515139246SJoseph Koshy /* 139615139246SJoseph Koshy * We bring in the gmon file for the image 139715139246SJoseph Koshy * currently associated with the PMC & pid 139815139246SJoseph Koshy * pair and increment the appropriate entry 139915139246SJoseph Koshy * bin inside this. 140015139246SJoseph Koshy */ 140149874f6eSJoseph Koshy pmcstat_stats.ps_samples_total++; 140249874f6eSJoseph Koshy 140315139246SJoseph Koshy pc = ev.pl_u.pl_s.pl_pc; 140449874f6eSJoseph Koshy pp = pmcstat_process_lookup(ev.pl_u.pl_s.pl_pid, 140549874f6eSJoseph Koshy PMCSTAT_ALLOCATE); 140615139246SJoseph Koshy if ((ppm = pmcstat_process_find_map(pp, pc)) == NULL && 140715139246SJoseph Koshy (ppm = pmcstat_process_find_map(pmcstat_kernproc, 140849874f6eSJoseph Koshy pc)) == NULL) { /* unknown process,offset pair */ 140949874f6eSJoseph Koshy pmcstat_stats.ps_samples_unknown_offset++; 141049874f6eSJoseph Koshy break; 141149874f6eSJoseph Koshy } 141215139246SJoseph Koshy 141315139246SJoseph Koshy pmcstat_image_increment_bucket(ppm, pc, 141415139246SJoseph Koshy ev.pl_u.pl_s.pl_pmcid, a); 141515139246SJoseph Koshy 141615139246SJoseph Koshy break; 141715139246SJoseph Koshy 141815139246SJoseph Koshy case PMCLOG_TYPE_PMCALLOCATE: 141915139246SJoseph Koshy /* 142015139246SJoseph Koshy * Record the association pmc id between this 142115139246SJoseph Koshy * PMC and its name. 142215139246SJoseph Koshy */ 142315139246SJoseph Koshy pmcstat_pmcid_add(ev.pl_u.pl_a.pl_pmcid, 142415139246SJoseph Koshy pmcstat_string_intern(ev.pl_u.pl_a.pl_evname), a); 142515139246SJoseph Koshy break; 142615139246SJoseph Koshy 142715139246SJoseph Koshy case PMCLOG_TYPE_PROCEXEC: 142815139246SJoseph Koshy 142915139246SJoseph Koshy /* 143015139246SJoseph Koshy * Change the executable image associated with 143115139246SJoseph Koshy * a process. 143215139246SJoseph Koshy */ 143349874f6eSJoseph Koshy pp = pmcstat_process_lookup(ev.pl_u.pl_x.pl_pid, 143449874f6eSJoseph Koshy PMCSTAT_ALLOCATE); 143515139246SJoseph Koshy 143615139246SJoseph Koshy /* delete the current process map */ 143715139246SJoseph Koshy TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) { 143815139246SJoseph Koshy TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next); 143915139246SJoseph Koshy free(ppm); 144015139246SJoseph Koshy } 144115139246SJoseph Koshy 144249874f6eSJoseph Koshy /* associate this process image */ 144315139246SJoseph Koshy image_path = pmcstat_string_intern( 144415139246SJoseph Koshy ev.pl_u.pl_x.pl_pathname); 144549874f6eSJoseph Koshy assert(image_path != NULL); 144660f918efSJoseph Koshy pmcstat_process_exec(pp, image_path, 144749874f6eSJoseph Koshy ev.pl_u.pl_x.pl_entryaddr, a); 144815139246SJoseph Koshy break; 144915139246SJoseph Koshy 145015139246SJoseph Koshy case PMCLOG_TYPE_PROCEXIT: 145115139246SJoseph Koshy 145215139246SJoseph Koshy /* 145315139246SJoseph Koshy * Due to the way the log is generated, the 145415139246SJoseph Koshy * last few samples corresponding to a process 145515139246SJoseph Koshy * may appear in the log after the process 145615139246SJoseph Koshy * exit event is recorded. Thus we keep the 145715139246SJoseph Koshy * process' descriptor and associated data 145815139246SJoseph Koshy * structures around, but mark the process as 145915139246SJoseph Koshy * having exited. 146015139246SJoseph Koshy */ 146115139246SJoseph Koshy pp = pmcstat_process_lookup(ev.pl_u.pl_e.pl_pid, 0); 146215139246SJoseph Koshy if (pp == NULL) 146315139246SJoseph Koshy break; 146449874f6eSJoseph Koshy pp->pp_isactive = 0; /* mark as a zombie */ 146515139246SJoseph Koshy break; 146615139246SJoseph Koshy 146715139246SJoseph Koshy case PMCLOG_TYPE_SYSEXIT: 146815139246SJoseph Koshy pp = pmcstat_process_lookup(ev.pl_u.pl_se.pl_pid, 0); 146915139246SJoseph Koshy if (pp == NULL) 147015139246SJoseph Koshy break; 147115139246SJoseph Koshy pp->pp_isactive = 0; /* make a zombie */ 147215139246SJoseph Koshy break; 147315139246SJoseph Koshy 147415139246SJoseph Koshy case PMCLOG_TYPE_PROCFORK: 147515139246SJoseph Koshy 147615139246SJoseph Koshy /* 147749874f6eSJoseph Koshy * Allocate a process descriptor for the new 147849874f6eSJoseph Koshy * (child) process. 147949874f6eSJoseph Koshy */ 148049874f6eSJoseph Koshy ppnew = 148149874f6eSJoseph Koshy pmcstat_process_lookup(ev.pl_u.pl_f.pl_newpid, 148249874f6eSJoseph Koshy PMCSTAT_ALLOCATE); 148349874f6eSJoseph Koshy 148449874f6eSJoseph Koshy /* 148549874f6eSJoseph Koshy * If we had been tracking the parent, clone 148649874f6eSJoseph Koshy * its address maps. 148715139246SJoseph Koshy */ 148815139246SJoseph Koshy pp = pmcstat_process_lookup(ev.pl_u.pl_f.pl_oldpid, 0); 148915139246SJoseph Koshy if (pp == NULL) 149015139246SJoseph Koshy break; 149115139246SJoseph Koshy TAILQ_FOREACH(ppm, &pp->pp_map, ppm_next) 149215139246SJoseph Koshy pmcstat_image_link(ppnew, ppm->ppm_image, 149349874f6eSJoseph Koshy ppm->ppm_lowpc); 149415139246SJoseph Koshy break; 149515139246SJoseph Koshy 149615139246SJoseph Koshy default: /* other types of entries are not relevant */ 149715139246SJoseph Koshy break; 149815139246SJoseph Koshy } 149915139246SJoseph Koshy } 150015139246SJoseph Koshy 150115139246SJoseph Koshy if (ev.pl_state == PMCLOG_EOF) 150249874f6eSJoseph Koshy return (PMCSTAT_FINISHED); 150315139246SJoseph Koshy else if (ev.pl_state == PMCLOG_REQUIRE_DATA) 150449874f6eSJoseph Koshy return (PMCSTAT_RUNNING); 150515139246SJoseph Koshy 150615139246SJoseph Koshy err(EX_DATAERR, "ERROR: event parsing failed (record %jd, " 150715139246SJoseph Koshy "offset 0x%jx)", (uintmax_t) ev.pl_count + 1, ev.pl_offset); 150815139246SJoseph Koshy } 150915139246SJoseph Koshy 151015139246SJoseph Koshy /* 151115139246SJoseph Koshy * Print log entries as text. 151215139246SJoseph Koshy */ 151315139246SJoseph Koshy 151449874f6eSJoseph Koshy static int 151515139246SJoseph Koshy pmcstat_print_log(struct pmcstat_args *a) 151615139246SJoseph Koshy { 151715139246SJoseph Koshy struct pmclog_ev ev; 151815139246SJoseph Koshy 151915139246SJoseph Koshy while (pmclog_read(a->pa_logparser, &ev) == 0) { 152015139246SJoseph Koshy assert(ev.pl_state == PMCLOG_OK); 152115139246SJoseph Koshy switch (ev.pl_type) { 152215139246SJoseph Koshy case PMCLOG_TYPE_CLOSELOG: 152315139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"closelog",); 152415139246SJoseph Koshy break; 152515139246SJoseph Koshy case PMCLOG_TYPE_DROPNOTIFY: 152615139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"drop",); 152715139246SJoseph Koshy break; 152815139246SJoseph Koshy case PMCLOG_TYPE_INITIALIZE: 152915139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"initlog","0x%x \"%s\"", 153015139246SJoseph Koshy ev.pl_u.pl_i.pl_version, 153115139246SJoseph Koshy pmc_name_of_cputype(ev.pl_u.pl_i.pl_arch)); 15323ab065b6SJoseph Koshy if ((ev.pl_u.pl_i.pl_version & 0xFF000000) != 15333ab065b6SJoseph Koshy PMC_VERSION_MAJOR << 24 && a->pa_verbosity > 0) 15343ab065b6SJoseph Koshy warnx("WARNING: Log version 0x%x != expected " 15353ab065b6SJoseph Koshy "version 0x%x.", ev.pl_u.pl_i.pl_version, 15363ab065b6SJoseph Koshy PMC_VERSION); 153715139246SJoseph Koshy break; 153849874f6eSJoseph Koshy case PMCLOG_TYPE_MAP_IN: 153949874f6eSJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"map-in","%d %p \"%s\"", 154049874f6eSJoseph Koshy ev.pl_u.pl_mi.pl_pid, 154149874f6eSJoseph Koshy (void *) ev.pl_u.pl_mi.pl_start, 154249874f6eSJoseph Koshy ev.pl_u.pl_mi.pl_pathname); 154349874f6eSJoseph Koshy break; 154449874f6eSJoseph Koshy case PMCLOG_TYPE_MAP_OUT: 154549874f6eSJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"map-out","%d %p %p", 154649874f6eSJoseph Koshy ev.pl_u.pl_mo.pl_pid, 154749874f6eSJoseph Koshy (void *) ev.pl_u.pl_mo.pl_start, 154849874f6eSJoseph Koshy (void *) ev.pl_u.pl_mo.pl_end); 154915139246SJoseph Koshy break; 155015139246SJoseph Koshy case PMCLOG_TYPE_PCSAMPLE: 155115139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"sample","0x%x %d %p %c", 155215139246SJoseph Koshy ev.pl_u.pl_s.pl_pmcid, 155315139246SJoseph Koshy ev.pl_u.pl_s.pl_pid, 155415139246SJoseph Koshy (void *) ev.pl_u.pl_s.pl_pc, 155515139246SJoseph Koshy ev.pl_u.pl_s.pl_usermode ? 'u' : 's'); 155615139246SJoseph Koshy break; 155715139246SJoseph Koshy case PMCLOG_TYPE_PMCALLOCATE: 155815139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"allocate","0x%x \"%s\" 0x%x", 155915139246SJoseph Koshy ev.pl_u.pl_a.pl_pmcid, 156015139246SJoseph Koshy ev.pl_u.pl_a.pl_evname, 156115139246SJoseph Koshy ev.pl_u.pl_a.pl_flags); 156215139246SJoseph Koshy break; 156315139246SJoseph Koshy case PMCLOG_TYPE_PMCATTACH: 156415139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"attach","0x%x %d \"%s\"", 156515139246SJoseph Koshy ev.pl_u.pl_t.pl_pmcid, 156615139246SJoseph Koshy ev.pl_u.pl_t.pl_pid, 156715139246SJoseph Koshy ev.pl_u.pl_t.pl_pathname); 156815139246SJoseph Koshy break; 156915139246SJoseph Koshy case PMCLOG_TYPE_PMCDETACH: 157015139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"detach","0x%x %d", 157115139246SJoseph Koshy ev.pl_u.pl_d.pl_pmcid, 157215139246SJoseph Koshy ev.pl_u.pl_d.pl_pid); 157315139246SJoseph Koshy break; 157415139246SJoseph Koshy case PMCLOG_TYPE_PROCCSW: 157515139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"cswval","0x%x %d %jd", 157615139246SJoseph Koshy ev.pl_u.pl_c.pl_pmcid, 157715139246SJoseph Koshy ev.pl_u.pl_c.pl_pid, 157815139246SJoseph Koshy ev.pl_u.pl_c.pl_value); 157915139246SJoseph Koshy break; 158015139246SJoseph Koshy case PMCLOG_TYPE_PROCEXEC: 158115139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"exec","0x%x %d %p \"%s\"", 158215139246SJoseph Koshy ev.pl_u.pl_x.pl_pmcid, 158315139246SJoseph Koshy ev.pl_u.pl_x.pl_pid, 158415139246SJoseph Koshy (void *) ev.pl_u.pl_x.pl_entryaddr, 158515139246SJoseph Koshy ev.pl_u.pl_x.pl_pathname); 158615139246SJoseph Koshy break; 158715139246SJoseph Koshy case PMCLOG_TYPE_PROCEXIT: 158815139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"exitval","0x%x %d %jd", 158915139246SJoseph Koshy ev.pl_u.pl_e.pl_pmcid, 159015139246SJoseph Koshy ev.pl_u.pl_e.pl_pid, 159115139246SJoseph Koshy ev.pl_u.pl_e.pl_value); 159215139246SJoseph Koshy break; 159315139246SJoseph Koshy case PMCLOG_TYPE_PROCFORK: 159415139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"fork","%d %d", 159515139246SJoseph Koshy ev.pl_u.pl_f.pl_oldpid, 159615139246SJoseph Koshy ev.pl_u.pl_f.pl_newpid); 159715139246SJoseph Koshy break; 159815139246SJoseph Koshy case PMCLOG_TYPE_USERDATA: 159915139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"userdata","0x%x", 160015139246SJoseph Koshy ev.pl_u.pl_u.pl_userdata); 160115139246SJoseph Koshy break; 160215139246SJoseph Koshy case PMCLOG_TYPE_SYSEXIT: 160315139246SJoseph Koshy PMCSTAT_PRINT_ENTRY(a,"exit","%d", 160415139246SJoseph Koshy ev.pl_u.pl_se.pl_pid); 160515139246SJoseph Koshy break; 160615139246SJoseph Koshy default: 16073ab065b6SJoseph Koshy fprintf(a->pa_printfile, "unknown event (type %d).\n", 160815139246SJoseph Koshy ev.pl_type); 160915139246SJoseph Koshy } 161015139246SJoseph Koshy } 161115139246SJoseph Koshy 161215139246SJoseph Koshy if (ev.pl_state == PMCLOG_EOF) 161349874f6eSJoseph Koshy return (PMCSTAT_FINISHED); 161415139246SJoseph Koshy else if (ev.pl_state == PMCLOG_REQUIRE_DATA) 161549874f6eSJoseph Koshy return (PMCSTAT_RUNNING); 161615139246SJoseph Koshy 16173ab065b6SJoseph Koshy errx(EX_DATAERR, "ERROR: event parsing failed " 16183ab065b6SJoseph Koshy "(record %jd, offset 0x%jx).", 161915139246SJoseph Koshy (uintmax_t) ev.pl_count + 1, ev.pl_offset); 162015139246SJoseph Koshy /*NOTREACHED*/ 162115139246SJoseph Koshy } 162215139246SJoseph Koshy 162315139246SJoseph Koshy /* 162449874f6eSJoseph Koshy * Public Interfaces. 162549874f6eSJoseph Koshy */ 162649874f6eSJoseph Koshy 162749874f6eSJoseph Koshy /* 162849874f6eSJoseph Koshy * Close a logfile, after first flushing all in-module queued data. 162949874f6eSJoseph Koshy */ 163049874f6eSJoseph Koshy 163149874f6eSJoseph Koshy int 163249874f6eSJoseph Koshy pmcstat_close_log(struct pmcstat_args *a) 163349874f6eSJoseph Koshy { 163449874f6eSJoseph Koshy if (pmc_flush_logfile() < 0 || 163549874f6eSJoseph Koshy pmc_configure_logfile(-1) < 0) 163649874f6eSJoseph Koshy err(EX_OSERR, "ERROR: logging failed"); 163749874f6eSJoseph Koshy a->pa_flags &= ~(FLAG_HAS_OUTPUT_LOGFILE | FLAG_HAS_PIPE); 163849874f6eSJoseph Koshy return (a->pa_flags & FLAG_HAS_PIPE ? PMCSTAT_EXITING : 163949874f6eSJoseph Koshy PMCSTAT_FINISHED); 164049874f6eSJoseph Koshy } 164149874f6eSJoseph Koshy 164249874f6eSJoseph Koshy 164349874f6eSJoseph Koshy 164449874f6eSJoseph Koshy /* 164549874f6eSJoseph Koshy * Open a log file, for reading or writing. 164649874f6eSJoseph Koshy * 164749874f6eSJoseph Koshy * The function returns the fd of a successfully opened log or -1 in 164849874f6eSJoseph Koshy * case of failure. 164949874f6eSJoseph Koshy */ 165049874f6eSJoseph Koshy 165149874f6eSJoseph Koshy int 165249874f6eSJoseph Koshy pmcstat_open_log(const char *path, int mode) 165349874f6eSJoseph Koshy { 1654302cbb90SJoseph Koshy int error, fd; 1655302cbb90SJoseph Koshy size_t hlen; 1656302cbb90SJoseph Koshy const char *p, *errstr; 1657302cbb90SJoseph Koshy struct addrinfo hints, *res, *res0; 1658302cbb90SJoseph Koshy char hostname[MAXHOSTNAMELEN]; 1659302cbb90SJoseph Koshy 1660302cbb90SJoseph Koshy errstr = NULL; 1661302cbb90SJoseph Koshy fd = -1; 166249874f6eSJoseph Koshy 166349874f6eSJoseph Koshy /* 166449874f6eSJoseph Koshy * If 'path' is "-" then open one of stdin or stdout depending 1665302cbb90SJoseph Koshy * on the value of 'mode'. 1666302cbb90SJoseph Koshy * 1667302cbb90SJoseph Koshy * If 'path' contains a ':' and does not start with a '/' or '.', 1668302cbb90SJoseph Koshy * and is being opened for writing, treat it as a "host:port" 1669302cbb90SJoseph Koshy * specification and open a network socket. 1670302cbb90SJoseph Koshy * 1671302cbb90SJoseph Koshy * Otherwise, treat 'path' as a file name and open that. 167249874f6eSJoseph Koshy */ 167349874f6eSJoseph Koshy if (path[0] == '-' && path[1] == '\0') 167449874f6eSJoseph Koshy fd = (mode == PMCSTAT_OPEN_FOR_READ) ? 0 : 1; 1675302cbb90SJoseph Koshy else if (mode == PMCSTAT_OPEN_FOR_WRITE && path[0] != '/' && 1676302cbb90SJoseph Koshy path[0] != '.' && strchr(path, ':') != NULL) { 1677302cbb90SJoseph Koshy 1678302cbb90SJoseph Koshy p = strrchr(path, ':'); 1679302cbb90SJoseph Koshy hlen = p - path; 1680302cbb90SJoseph Koshy if (p == path || hlen >= sizeof(hostname)) { 1681302cbb90SJoseph Koshy errstr = strerror(EINVAL); 1682302cbb90SJoseph Koshy goto done; 1683302cbb90SJoseph Koshy } 1684302cbb90SJoseph Koshy 1685302cbb90SJoseph Koshy assert(hlen < sizeof(hostname)); 1686302cbb90SJoseph Koshy (void) strncpy(hostname, path, hlen); 1687302cbb90SJoseph Koshy hostname[hlen] = '\0'; 1688302cbb90SJoseph Koshy 1689302cbb90SJoseph Koshy (void) memset(&hints, 0, sizeof(hints)); 1690302cbb90SJoseph Koshy hints.ai_family = AF_UNSPEC; 1691302cbb90SJoseph Koshy hints.ai_socktype = SOCK_STREAM; 1692302cbb90SJoseph Koshy if ((error = getaddrinfo(hostname, p+1, &hints, &res0)) != 0) { 1693302cbb90SJoseph Koshy errstr = gai_strerror(error); 1694302cbb90SJoseph Koshy goto done; 1695302cbb90SJoseph Koshy } 1696302cbb90SJoseph Koshy 1697302cbb90SJoseph Koshy fd = -1; 1698302cbb90SJoseph Koshy for (res = res0; res; res = res->ai_next) { 1699302cbb90SJoseph Koshy if ((fd = socket(res->ai_family, res->ai_socktype, 1700302cbb90SJoseph Koshy res->ai_protocol)) < 0) { 1701302cbb90SJoseph Koshy errstr = strerror(errno); 1702302cbb90SJoseph Koshy continue; 1703302cbb90SJoseph Koshy } 1704302cbb90SJoseph Koshy if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) { 1705302cbb90SJoseph Koshy errstr = strerror(errno); 1706302cbb90SJoseph Koshy (void) close(fd); 1707302cbb90SJoseph Koshy fd = -1; 1708302cbb90SJoseph Koshy continue; 1709302cbb90SJoseph Koshy } 1710302cbb90SJoseph Koshy errstr = NULL; 1711302cbb90SJoseph Koshy break; 1712302cbb90SJoseph Koshy } 1713302cbb90SJoseph Koshy freeaddrinfo(res0); 1714302cbb90SJoseph Koshy 1715302cbb90SJoseph Koshy } else if ((fd = open(path, mode == PMCSTAT_OPEN_FOR_READ ? 171649874f6eSJoseph Koshy O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC), 1717302cbb90SJoseph Koshy S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) 1718302cbb90SJoseph Koshy errstr = strerror(errno); 1719302cbb90SJoseph Koshy 1720302cbb90SJoseph Koshy done: 1721302cbb90SJoseph Koshy if (errstr) 1722302cbb90SJoseph Koshy errx(EX_OSERR, "ERROR: Cannot open \"%s\" for %s: %s.", path, 1723302cbb90SJoseph Koshy (mode == PMCSTAT_OPEN_FOR_READ ? "reading" : "writing"), 1724302cbb90SJoseph Koshy errstr); 172549874f6eSJoseph Koshy 172649874f6eSJoseph Koshy return (fd); 172749874f6eSJoseph Koshy } 172849874f6eSJoseph Koshy 172949874f6eSJoseph Koshy /* 173015139246SJoseph Koshy * Process a log file in offline analysis mode. 173115139246SJoseph Koshy */ 173215139246SJoseph Koshy 1733dc1d9d2eSJoseph Koshy int 173415139246SJoseph Koshy pmcstat_process_log(struct pmcstat_args *a) 173515139246SJoseph Koshy { 173615139246SJoseph Koshy 173715139246SJoseph Koshy /* 173815139246SJoseph Koshy * If gprof style profiles haven't been asked for, just print the 173915139246SJoseph Koshy * log to the current output file. 174015139246SJoseph Koshy */ 174115139246SJoseph Koshy if (a->pa_flags & FLAG_DO_PRINT) 174249874f6eSJoseph Koshy return (pmcstat_print_log(a)); 174315139246SJoseph Koshy else 174415139246SJoseph Koshy /* convert the log to gprof compatible profiles */ 174549874f6eSJoseph Koshy return (pmcstat_convert_log(a)); 174615139246SJoseph Koshy } 174715139246SJoseph Koshy 174849874f6eSJoseph Koshy /* 174949874f6eSJoseph Koshy * Initialize module. 175049874f6eSJoseph Koshy */ 175149874f6eSJoseph Koshy 175215139246SJoseph Koshy void 175315139246SJoseph Koshy pmcstat_initialize_logging(struct pmcstat_args *a) 175415139246SJoseph Koshy { 175560f918efSJoseph Koshy int i; 175649874f6eSJoseph Koshy 175749874f6eSJoseph Koshy (void) a; 175815139246SJoseph Koshy 175915139246SJoseph Koshy /* use a convenient format for 'ldd' output */ 1760f3c8200eSJoseph Koshy if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1","%o \"%p\" %x\n",1) != 0) 176149874f6eSJoseph Koshy err(EX_OSERR, "ERROR: Cannot setenv"); 176215139246SJoseph Koshy 176315139246SJoseph Koshy /* Initialize hash tables */ 176449874f6eSJoseph Koshy pmcstat_string_initialize(); 176515139246SJoseph Koshy for (i = 0; i < PMCSTAT_NHASH; i++) { 176615139246SJoseph Koshy LIST_INIT(&pmcstat_image_hash[i]); 176715139246SJoseph Koshy LIST_INIT(&pmcstat_process_hash[i]); 176815139246SJoseph Koshy } 176915139246SJoseph Koshy 177049874f6eSJoseph Koshy /* 177149874f6eSJoseph Koshy * Create a fake 'process' entry for the kernel with pid -1. 177249874f6eSJoseph Koshy * hwpmc(4) will subsequently inform us about where the kernel 177349874f6eSJoseph Koshy * and any loaded kernel modules are mapped. 177449874f6eSJoseph Koshy */ 177549874f6eSJoseph Koshy if ((pmcstat_kernproc = pmcstat_process_lookup((pid_t) -1, 177649874f6eSJoseph Koshy PMCSTAT_ALLOCATE)) == NULL) 177715139246SJoseph Koshy err(EX_OSERR, "ERROR: Cannot initialize logging"); 177815139246SJoseph Koshy } 177915139246SJoseph Koshy 178049874f6eSJoseph Koshy /* 178149874f6eSJoseph Koshy * Shutdown module. 178249874f6eSJoseph Koshy */ 178349874f6eSJoseph Koshy 178415139246SJoseph Koshy void 178549874f6eSJoseph Koshy pmcstat_shutdown_logging(struct pmcstat_args *a) 178615139246SJoseph Koshy { 178715139246SJoseph Koshy int i; 178849874f6eSJoseph Koshy FILE *mf; 178915139246SJoseph Koshy struct pmcstat_gmonfile *pgf, *pgftmp; 179015139246SJoseph Koshy struct pmcstat_image *pi, *pitmp; 179115139246SJoseph Koshy struct pmcstat_process *pp, *pptmp; 179249874f6eSJoseph Koshy 179349874f6eSJoseph Koshy /* determine where to send the map file */ 179449874f6eSJoseph Koshy mf = NULL; 179549874f6eSJoseph Koshy if (a->pa_mapfilename != NULL) 179649874f6eSJoseph Koshy mf = (strcmp(a->pa_mapfilename, "-") == 0) ? 179749874f6eSJoseph Koshy a->pa_printfile : fopen(a->pa_mapfilename, "w"); 179849874f6eSJoseph Koshy 179949874f6eSJoseph Koshy if (mf == NULL && a->pa_flags & FLAG_DO_GPROF && 180049874f6eSJoseph Koshy a->pa_verbosity >= 2) 180149874f6eSJoseph Koshy mf = a->pa_printfile; 180249874f6eSJoseph Koshy 180349874f6eSJoseph Koshy if (mf) 180449874f6eSJoseph Koshy (void) fprintf(mf, "MAP:\n"); 180515139246SJoseph Koshy 180615139246SJoseph Koshy for (i = 0; i < PMCSTAT_NHASH; i++) { 180715139246SJoseph Koshy LIST_FOREACH_SAFE(pi, &pmcstat_image_hash[i], pi_next, pitmp) { 18082a6d2e9cSJoseph Koshy 18092a6d2e9cSJoseph Koshy if (mf) 18102a6d2e9cSJoseph Koshy (void) fprintf(mf, " \"%s\" => \"%s\"", 18112a6d2e9cSJoseph Koshy pmcstat_string_unintern(pi->pi_execpath), 18122a6d2e9cSJoseph Koshy pmcstat_string_unintern(pi->pi_samplename)); 18132a6d2e9cSJoseph Koshy 181415139246SJoseph Koshy /* flush gmon.out data to disk */ 181515139246SJoseph Koshy LIST_FOREACH_SAFE(pgf, &pi->pi_gmlist, pgf_next, 181615139246SJoseph Koshy pgftmp) { 181715139246SJoseph Koshy pmcstat_gmon_unmap_file(pgf); 181815139246SJoseph Koshy LIST_REMOVE(pgf, pgf_next); 18192a6d2e9cSJoseph Koshy if (mf) 18202a6d2e9cSJoseph Koshy (void) fprintf(mf, " %s/%d", 18212a6d2e9cSJoseph Koshy pmcstat_pmcid_to_name(pgf->pgf_pmcid), 18222a6d2e9cSJoseph Koshy pgf->pgf_nsamples); 182349874f6eSJoseph Koshy if (pgf->pgf_overflow && a->pa_verbosity >= 1) 182449874f6eSJoseph Koshy warnx("WARNING: profile \"%s\" " 182549874f6eSJoseph Koshy "overflowed.", 18262a6d2e9cSJoseph Koshy pmcstat_string_unintern( 18272a6d2e9cSJoseph Koshy pgf->pgf_name)); 182815139246SJoseph Koshy free(pgf); 182915139246SJoseph Koshy } 18302a6d2e9cSJoseph Koshy 183149874f6eSJoseph Koshy if (mf) 18322a6d2e9cSJoseph Koshy (void) fprintf(mf, "\n"); 183315139246SJoseph Koshy 183415139246SJoseph Koshy LIST_REMOVE(pi, pi_next); 183515139246SJoseph Koshy free(pi); 183615139246SJoseph Koshy } 183715139246SJoseph Koshy LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[i], pp_next, 183815139246SJoseph Koshy pptmp) { 183915139246SJoseph Koshy LIST_REMOVE(pp, pp_next); 184015139246SJoseph Koshy free(pp); 184115139246SJoseph Koshy } 184215139246SJoseph Koshy } 184349874f6eSJoseph Koshy 184449874f6eSJoseph Koshy pmcstat_string_shutdown(); 184549874f6eSJoseph Koshy 184649874f6eSJoseph Koshy /* 184749874f6eSJoseph Koshy * Print errors unless -q was specified. Print all statistics 184849874f6eSJoseph Koshy * if verbosity > 1. 184949874f6eSJoseph Koshy */ 185049874f6eSJoseph Koshy #define PRINT(N,V,A) do { \ 185149874f6eSJoseph Koshy if (pmcstat_stats.ps_##V || (A)->pa_verbosity >= 2) \ 185249874f6eSJoseph Koshy (void) fprintf((A)->pa_printfile, " %-40s %d\n",\ 185349874f6eSJoseph Koshy N, pmcstat_stats.ps_##V); \ 185449874f6eSJoseph Koshy } while (0) 185549874f6eSJoseph Koshy 185649874f6eSJoseph Koshy if (a->pa_verbosity >= 1 && a->pa_flags & FLAG_DO_GPROF) { 185749874f6eSJoseph Koshy (void) fprintf(a->pa_printfile, "CONVERSION STATISTICS:\n"); 185849874f6eSJoseph Koshy PRINT("#exec/a.out", exec_aout, a); 185949874f6eSJoseph Koshy PRINT("#exec/elf", exec_elf, a); 186049874f6eSJoseph Koshy PRINT("#exec/unknown", exec_indeterminable, a); 186149874f6eSJoseph Koshy PRINT("#exec handling errors", exec_errors, a); 186249874f6eSJoseph Koshy PRINT("#samples/total", samples_total, a); 186349874f6eSJoseph Koshy PRINT("#samples/unclaimed", samples_unknown_offset, a); 186449874f6eSJoseph Koshy PRINT("#samples/unknown-object", samples_indeterminable, a); 186515139246SJoseph Koshy } 186649874f6eSJoseph Koshy 186749874f6eSJoseph Koshy if (mf) 186849874f6eSJoseph Koshy (void) fclose(mf); 186915139246SJoseph Koshy } 1870