aboutsummaryrefslogtreecommitdiff
path: root/lib/libpmcstat
diff options
context:
space:
mode:
authorRuslan Bukin <br@FreeBSD.org>2017-10-24 16:28:00 +0000
committerRuslan Bukin <br@FreeBSD.org>2017-10-24 16:28:00 +0000
commitd27927f731d6877267b0db84664776d2e62fc443 (patch)
treecd3aca85950d07a00423da65d47330205feed92c /lib/libpmcstat
parent701492a5f6733530ace7c6af70a753bb6c8c4120 (diff)
downloadsrc-d27927f731d6877267b0db84664776d2e62fc443.tar.gz
src-d27927f731d6877267b0db84664776d2e62fc443.zip
Extract a set of pmcstat functions and interfaces to the new internal
library -- libpmcstat. This includes PMC logging module, symbols lookup functions, ELF parsing, process management, PMC attachment, etc. This allows to reuse code while building new hwpmc(4)-based applications. Also add pmcstat_symbol_search_by_name() function that allows to find mapped IP range for a given function name. Reviewed by: kib Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D12718
Notes
Notes: svn path=/head/; revision=324959
Diffstat (limited to 'lib/libpmcstat')
-rw-r--r--lib/libpmcstat/Makefile16
-rw-r--r--lib/libpmcstat/libpmcstat.h382
-rw-r--r--lib/libpmcstat/libpmcstat_event.c72
-rw-r--r--lib/libpmcstat/libpmcstat_image.c517
-rw-r--r--lib/libpmcstat/libpmcstat_logging.c718
-rw-r--r--lib/libpmcstat/libpmcstat_process.c363
-rw-r--r--lib/libpmcstat/libpmcstat_string.c156
-rw-r--r--lib/libpmcstat/libpmcstat_symbol.c148
8 files changed, 2372 insertions, 0 deletions
diff --git a/lib/libpmcstat/Makefile b/lib/libpmcstat/Makefile
new file mode 100644
index 000000000000..948a86767c84
--- /dev/null
+++ b/lib/libpmcstat/Makefile
@@ -0,0 +1,16 @@
+# $FreeBSD$
+
+PACKAGE=lib${LIB}
+LIB= pmcstat
+INTERNALLIB=
+
+SRCS= \
+ libpmcstat_event.c \
+ libpmcstat_image.c \
+ libpmcstat_logging.c \
+ libpmcstat_process.c \
+ libpmcstat_string.c \
+ libpmcstat_symbol.c
+INCS= libpmcstat.h
+
+.include <bsd.lib.mk>
diff --git a/lib/libpmcstat/libpmcstat.h b/lib/libpmcstat/libpmcstat.h
new file mode 100644
index 000000000000..4ead605c88a9
--- /dev/null
+++ b/lib/libpmcstat/libpmcstat.h
@@ -0,0 +1,382 @@
+/*-
+ * Copyright (c) 2005-2007, Joseph Koshy
+ * Copyright (c) 2007 The FreeBSD Foundation
+ * All rights reserved.
+ *
+ * Portions of this software were developed by A. Joseph Koshy under
+ * sponsorship from the FreeBSD Foundation and Google, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _LIBPMCSTAT_H_
+#define _LIBPMCSTAT_H_
+
+#include <sys/_cpuset.h>
+#include <sys/queue.h>
+
+#include <stdio.h>
+#include <gelf.h>
+
+#define PMCSTAT_ALLOCATE 1
+
+#define NSOCKPAIRFD 2
+#define PARENTSOCKET 0
+#define CHILDSOCKET 1
+
+#define PMCSTAT_OPEN_FOR_READ 0
+#define PMCSTAT_OPEN_FOR_WRITE 1
+#define READPIPEFD 0
+#define WRITEPIPEFD 1
+#define NPIPEFD 2
+
+#define PMCSTAT_NHASH 256
+#define PMCSTAT_HASH_MASK 0xFF
+
+typedef const void *pmcstat_interned_string;
+struct pmc_plugins;
+
+enum pmcstat_state {
+ PMCSTAT_FINISHED = 0,
+ PMCSTAT_EXITING = 1,
+ PMCSTAT_RUNNING = 2
+};
+
+struct pmcstat_ev {
+ STAILQ_ENTRY(pmcstat_ev) ev_next;
+ int ev_count; /* associated count if in sampling mode */
+ uint32_t ev_cpu; /* cpus for this event */
+ int ev_cumulative; /* show cumulative counts */
+ int ev_flags; /* PMC_F_* */
+ int ev_fieldskip; /* #leading spaces */
+ int ev_fieldwidth; /* print width */
+ enum pmc_mode ev_mode; /* desired mode */
+ char *ev_name; /* (derived) event name */
+ pmc_id_t ev_pmcid; /* allocated ID */
+ pmc_value_t ev_saved; /* for incremental counts */
+ char *ev_spec; /* event specification */
+};
+
+struct pmcstat_target {
+ SLIST_ENTRY(pmcstat_target) pt_next;
+ pid_t pt_pid;
+};
+
+struct pmcstat_args {
+ int pa_flags; /* argument flags */
+#define FLAG_HAS_TARGET 0x00000001 /* process target */
+#define FLAG_HAS_WAIT_INTERVAL 0x00000002 /* -w secs */
+#define FLAG_HAS_OUTPUT_LOGFILE 0x00000004 /* -O file or pipe */
+#define FLAG_HAS_COMMANDLINE 0x00000008 /* command */
+#define FLAG_HAS_SAMPLING_PMCS 0x00000010 /* -S or -P */
+#define FLAG_HAS_COUNTING_PMCS 0x00000020 /* -s or -p */
+#define FLAG_HAS_PROCESS_PMCS 0x00000040 /* -P or -p */
+#define FLAG_HAS_SYSTEM_PMCS 0x00000080 /* -S or -s */
+#define FLAG_HAS_PIPE 0x00000100 /* implicit log */
+#define FLAG_READ_LOGFILE 0x00000200 /* -R file */
+#define FLAG_DO_GPROF 0x00000400 /* -g */
+#define FLAG_HAS_SAMPLESDIR 0x00000800 /* -D dir */
+#define FLAG_HAS_KERNELPATH 0x00001000 /* -k kernel */
+#define FLAG_DO_PRINT 0x00002000 /* -o */
+#define FLAG_DO_CALLGRAPHS 0x00004000 /* -G or -F */
+#define FLAG_DO_ANNOTATE 0x00008000 /* -m */
+#define FLAG_DO_TOP 0x00010000 /* -T */
+#define FLAG_DO_ANALYSIS 0x00020000 /* -g or -G or -m or -T */
+#define FLAGS_HAS_CPUMASK 0x00040000 /* -c */
+#define FLAG_HAS_DURATION 0x00080000 /* -l secs */
+#define FLAG_DO_WIDE_GPROF_HC 0x00100000 /* -e */
+
+ int pa_required; /* required features */
+ int pa_pplugin; /* pre-processing plugin */
+ int pa_plugin; /* analysis plugin */
+ int pa_verbosity; /* verbosity level */
+ FILE *pa_printfile; /* where to send printed output */
+ int pa_logfd; /* output log file */
+ char *pa_inputpath; /* path to input log */
+ char *pa_outputpath; /* path to output log */
+ void *pa_logparser; /* log file parser */
+ const char *pa_fsroot; /* FS root where executables reside */
+ char *pa_kernel; /* pathname of the kernel */
+ const char *pa_samplesdir; /* directory for profile files */
+ const char *pa_mapfilename;/* mapfile name */
+ FILE *pa_graphfile; /* where to send the callgraph */
+ int pa_graphdepth; /* print depth for callgraphs */
+ double pa_interval; /* printing interval in seconds */
+ cpuset_t pa_cpumask; /* filter for CPUs analysed */
+ int pa_ctdumpinstr; /* dump instructions with calltree */
+ int pa_topmode; /* delta or accumulative */
+ int pa_toptty; /* output to tty or file */
+ int pa_topcolor; /* terminal support color */
+ int pa_mergepmc; /* merge PMC with same name */
+ double pa_duration; /* time duration */
+ int pa_argc;
+ char **pa_argv;
+ STAILQ_HEAD(, pmcstat_ev) pa_events;
+ SLIST_HEAD(, pmcstat_target) pa_targets;
+};
+
+/*
+ * Each function symbol tracked by pmcstat(8).
+ */
+
+struct pmcstat_symbol {
+ pmcstat_interned_string ps_name;
+ uint64_t ps_start;
+ uint64_t ps_end;
+};
+
+/*
+ * A 'pmcstat_image' structure describes an executable program on
+ * disk. 'pi_execpath' is a cookie representing the pathname of
+ * the executable. 'pi_start' and 'pi_end' are the least and greatest
+ * virtual addresses for the text segments in the executable.
+ * 'pi_gmonlist' contains a linked list of gmon.out files associated
+ * with this image.
+ */
+
+enum pmcstat_image_type {
+ PMCSTAT_IMAGE_UNKNOWN = 0, /* never looked at the image */
+ PMCSTAT_IMAGE_INDETERMINABLE, /* can't tell what the image is */
+ PMCSTAT_IMAGE_ELF32, /* ELF 32 bit object */
+ PMCSTAT_IMAGE_ELF64, /* ELF 64 bit object */
+ PMCSTAT_IMAGE_AOUT /* AOUT object */
+};
+
+struct pmcstat_image {
+ LIST_ENTRY(pmcstat_image) pi_next; /* hash link */
+ pmcstat_interned_string pi_execpath; /* cookie */
+ pmcstat_interned_string pi_samplename; /* sample path name */
+ pmcstat_interned_string pi_fullpath; /* path to FS object */
+ pmcstat_interned_string pi_name; /* display name */
+
+ enum pmcstat_image_type pi_type; /* executable type */
+
+ /*
+ * Executables have pi_start and pi_end; these are zero
+ * for shared libraries.
+ */
+ uintfptr_t pi_start; /* start address (inclusive) */
+ uintfptr_t pi_end; /* end address (exclusive) */
+ uintfptr_t pi_entry; /* entry address */
+ uintfptr_t pi_vaddr; /* virtual address where loaded */
+ int pi_isdynamic; /* whether a dynamic object */
+ int pi_iskernelmodule;
+ pmcstat_interned_string pi_dynlinkerpath; /* path in .interp */
+
+ /* All symbols associated with this object. */
+ struct pmcstat_symbol *pi_symbols;
+ size_t pi_symcount;
+
+ /* Handle to addr2line for this image. */
+ FILE *pi_addr2line;
+
+ /*
+ * Plugins private data
+ */
+
+ /* gprof:
+ * An image can be associated with one or more gmon.out files;
+ * one per PMC.
+ */
+ LIST_HEAD(,pmcstat_gmonfile) pi_gmlist;
+};
+
+extern LIST_HEAD(pmcstat_image_hash_list, pmcstat_image) pmcstat_image_hash[PMCSTAT_NHASH];
+
+/*
+ * A simple implementation of interned strings. Each interned string
+ * is assigned a unique address, so that subsequent string compares
+ * can be done by a simple pointer comparison instead of using
+ * strcmp(). This speeds up hash table lookups and saves memory if
+ * duplicate strings are the norm.
+ */
+struct pmcstat_string {
+ LIST_ENTRY(pmcstat_string) ps_next; /* hash link */
+ int ps_len;
+ int ps_hash;
+ char *ps_string;
+};
+
+/*
+ * A 'pmcstat_pcmap' structure maps a virtual address range to an
+ * underlying 'pmcstat_image' descriptor.
+ */
+struct pmcstat_pcmap {
+ TAILQ_ENTRY(pmcstat_pcmap) ppm_next;
+ uintfptr_t ppm_lowpc;
+ uintfptr_t ppm_highpc;
+ struct pmcstat_image *ppm_image;
+};
+
+/*
+ * A 'pmcstat_process' structure models processes. Each process is
+ * associated with a set of pmcstat_pcmap structures that map
+ * addresses inside it to executable objects. This set is implemented
+ * as a list, kept sorted in ascending order of mapped addresses.
+ *
+ * 'pp_pid' holds the pid of the process. When a process exits, the
+ * 'pp_isactive' field is set to zero, but the process structure is
+ * not immediately reclaimed because there may still be samples in the
+ * log for this process.
+ */
+
+struct pmcstat_process {
+ LIST_ENTRY(pmcstat_process) pp_next; /* hash-next */
+ pid_t pp_pid; /* associated pid */
+ int pp_isactive; /* whether active */
+ uintfptr_t pp_entryaddr; /* entry address */
+ TAILQ_HEAD(,pmcstat_pcmap) pp_map; /* address range map */
+};
+extern LIST_HEAD(pmcstat_process_hash_list, pmcstat_process) pmcstat_process_hash[PMCSTAT_NHASH];
+
+/*
+ * 'pmcstat_pmcrecord' is a mapping from PMC ids to human-readable
+ * names.
+ */
+
+struct pmcstat_pmcrecord {
+ LIST_ENTRY(pmcstat_pmcrecord) pr_next;
+ pmc_id_t pr_pmcid;
+ int pr_pmcin;
+ pmcstat_interned_string pr_pmcname;
+ int pr_samples;
+ int pr_dubious_frames;
+ struct pmcstat_pmcrecord *pr_merge;
+};
+extern LIST_HEAD(pmcstat_pmcs, pmcstat_pmcrecord) pmcstat_pmcs; /* PMC list */
+
+struct pmc_plugins {
+ const char *pl_name;
+
+ /* configure */
+ int (*pl_configure)(char *opt);
+
+ /* init and shutdown */
+ int (*pl_init)(void);
+ void (*pl_shutdown)(FILE *mf);
+
+ /* sample processing */
+ void (*pl_process)(struct pmcstat_process *pp,
+ struct pmcstat_pmcrecord *pmcr, uint32_t nsamples,
+ uintfptr_t *cc, int usermode, uint32_t cpu);
+
+ /* image */
+ void (*pl_initimage)(struct pmcstat_image *pi);
+ void (*pl_shutdownimage)(struct pmcstat_image *pi);
+
+ /* pmc */
+ void (*pl_newpmc)(pmcstat_interned_string ps,
+ struct pmcstat_pmcrecord *pr);
+
+ /* top display */
+ void (*pl_topdisplay)(void);
+
+ /* top keypress */
+ int (*pl_topkeypress)(int c, void *w);
+};
+
+/*
+ * Misc. statistics
+ */
+struct pmcstat_stats {
+ int ps_exec_aout; /* # a.out executables seen */
+ int ps_exec_elf; /* # elf executables seen */
+ int ps_exec_errors; /* # errors processing executables */
+ int ps_exec_indeterminable; /* # unknown executables seen */
+ int ps_samples_total; /* total number of samples processed */
+ int ps_samples_skipped; /* #samples filtered out for any reason */
+ int ps_samples_unknown_offset; /* #samples of rank 0 not in a map */
+ int ps_samples_indeterminable; /* #samples in indeterminable images */
+ int ps_samples_unknown_function;/* #samples with unknown function at offset */
+ int ps_callchain_dubious_frames;/* #dubious frame pointers seen */
+};
+
+__BEGIN_DECLS
+int pmcstat_symbol_compare(const void *a, const void *b);
+struct pmcstat_symbol *pmcstat_symbol_search(struct pmcstat_image *image,
+ uintfptr_t addr);
+void pmcstat_image_add_symbols(struct pmcstat_image *image, Elf *e,
+ Elf_Scn *scn, GElf_Shdr *sh);
+
+const char *pmcstat_string_unintern(pmcstat_interned_string _is);
+pmcstat_interned_string pmcstat_string_intern(const char *_s);
+int pmcstat_string_compute_hash(const char *s);
+pmcstat_interned_string pmcstat_string_lookup(const char *_s);
+void pmcstat_image_get_elf_params(struct pmcstat_image *image, struct pmcstat_args *args);
+
+struct pmcstat_image *
+ pmcstat_image_from_path(pmcstat_interned_string internedpath,
+ int iskernelmodule, struct pmcstat_args *args,
+ struct pmc_plugins *plugins);
+int pmcstat_string_lookup_hash(pmcstat_interned_string _is);
+
+void pmcstat_process_elf_exec(struct pmcstat_process *_pp,
+ struct pmcstat_image *_image, uintfptr_t _entryaddr,
+ struct pmcstat_args *args, struct pmc_plugins *plugins,
+ struct pmcstat_stats *pmcstat_stats);
+
+void pmcstat_image_link(struct pmcstat_process *_pp,
+ struct pmcstat_image *_i, uintfptr_t _lpc);
+
+void pmcstat_process_aout_exec(struct pmcstat_process *_pp,
+ struct pmcstat_image *_image, uintfptr_t _entryaddr);
+void pmcstat_process_exec(struct pmcstat_process *_pp,
+ pmcstat_interned_string _path, uintfptr_t _entryaddr,
+ struct pmcstat_args *args, struct pmc_plugins *plugins,
+ struct pmcstat_stats *pmcstat_stats);
+void pmcstat_image_determine_type(struct pmcstat_image *_image, struct pmcstat_args *args);
+void pmcstat_image_get_aout_params(struct pmcstat_image *_image, struct pmcstat_args *args);
+struct pmcstat_pcmap *pmcstat_process_find_map(struct pmcstat_process *_p,
+ uintfptr_t _pc);
+void pmcstat_initialize_logging(struct pmcstat_process **pmcstat_kernproc,
+ struct pmcstat_args *args, struct pmc_plugins *plugins,
+ int *pmcstat_npmcs, int *pmcstat_mergepmc);
+void pmcstat_shutdown_logging(struct pmcstat_args *args,
+ struct pmc_plugins *plugins,
+ struct pmcstat_stats *pmcstat_stats);
+struct pmcstat_process *pmcstat_process_lookup(pid_t _pid, int _allocate);
+void pmcstat_clone_event_descriptor(struct pmcstat_ev *ev, const cpuset_t *cpumask, struct pmcstat_args *args);
+
+void pmcstat_create_process(int *pmcstat_sockpair, struct pmcstat_args *args, int pmcstat_kq);
+void pmcstat_start_process(int *pmcstat_sockpair);
+
+void pmcstat_attach_pmcs(struct pmcstat_args *args);
+struct pmcstat_symbol *pmcstat_symbol_search_by_name(struct pmcstat_process *pp, const char *pi_name, const char *name, uintptr_t *, uintptr_t *);
+
+void pmcstat_string_initialize(void);
+void pmcstat_string_shutdown(void);
+
+int pmcstat_analyze_log(struct pmcstat_args *args,
+ struct pmc_plugins *plugins,
+ struct pmcstat_stats *pmcstat_stats,
+ struct pmcstat_process *pmcstat_kernproc,
+ int pmcstat_mergepmc,
+ int *pmcstat_npmcs,
+ int *ps_samples_period);
+
+int pmcstat_open_log(const char *_p, int _mode);
+int pmcstat_close_log(struct pmcstat_args *args);
+__END_DECLS
+
+#endif /* !_LIBPMCSTAT_H_ */
diff --git a/lib/libpmcstat/libpmcstat_event.c b/lib/libpmcstat/libpmcstat_event.c
new file mode 100644
index 000000000000..623a6f5400df
--- /dev/null
+++ b/lib/libpmcstat/libpmcstat_event.c
@@ -0,0 +1,72 @@
+/*-
+ * Copyright (c) 2003-2008 Joseph Koshy
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/cpuset.h>
+#include <sys/pmc.h>
+
+#include <err.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+
+#include "libpmcstat.h"
+
+void
+pmcstat_clone_event_descriptor(struct pmcstat_ev *ev, const cpuset_t *cpumask,
+ struct pmcstat_args *args)
+{
+ int cpu;
+ struct pmcstat_ev *ev_clone;
+
+ for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
+ if (!CPU_ISSET(cpu, cpumask))
+ continue;
+
+ if ((ev_clone = malloc(sizeof(*ev_clone))) == NULL)
+ errx(EX_SOFTWARE, "ERROR: Out of memory");
+ (void) memset(ev_clone, 0, sizeof(*ev_clone));
+
+ ev_clone->ev_count = ev->ev_count;
+ ev_clone->ev_cpu = cpu;
+ ev_clone->ev_cumulative = ev->ev_cumulative;
+ ev_clone->ev_flags = ev->ev_flags;
+ ev_clone->ev_mode = ev->ev_mode;
+ ev_clone->ev_name = strdup(ev->ev_name);
+ if (ev_clone->ev_name == NULL)
+ errx(EX_SOFTWARE, "ERROR: Out of memory");
+ ev_clone->ev_pmcid = ev->ev_pmcid;
+ ev_clone->ev_saved = ev->ev_saved;
+ ev_clone->ev_spec = strdup(ev->ev_spec);
+ if (ev_clone->ev_spec == NULL)
+ errx(EX_SOFTWARE, "ERROR: Out of memory");
+
+ STAILQ_INSERT_TAIL(&args->pa_events, ev_clone, ev_next);
+ }
+}
diff --git a/lib/libpmcstat/libpmcstat_image.c b/lib/libpmcstat/libpmcstat_image.c
new file mode 100644
index 000000000000..068200462187
--- /dev/null
+++ b/lib/libpmcstat/libpmcstat_image.c
@@ -0,0 +1,517 @@
+/*-
+ * Copyright (c) 2003-2008 Joseph Koshy
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/cpuset.h>
+#include <sys/param.h>
+#include <sys/endian.h>
+#include <sys/pmc.h>
+#include <sys/imgact_aout.h>
+#include <sys/imgact_elf.h>
+
+#include <netinet/in.h>
+
+#include <assert.h>
+#include <err.h>
+#include <fcntl.h>
+#include <pmc.h>
+#include <pmclog.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <unistd.h>
+
+#include "libpmcstat.h"
+
+#define min(A,B) ((A) < (B) ? (A) : (B))
+#define max(A,B) ((A) > (B) ? (A) : (B))
+
+/*
+ * Add the list of symbols in the given section to the list associated
+ * with the object.
+ */
+void
+pmcstat_image_add_symbols(struct pmcstat_image *image, Elf *e,
+ Elf_Scn *scn, GElf_Shdr *sh)
+{
+ int firsttime;
+ size_t n, newsyms, nshsyms, nfuncsyms;
+ struct pmcstat_symbol *symptr;
+ char *fnname;
+ GElf_Sym sym;
+ Elf_Data *data;
+
+ if ((data = elf_getdata(scn, NULL)) == NULL)
+ return;
+
+ /*
+ * Determine the number of functions named in this
+ * section.
+ */
+
+ nshsyms = sh->sh_size / sh->sh_entsize;
+ for (n = nfuncsyms = 0; n < nshsyms; n++) {
+ if (gelf_getsym(data, (int) n, &sym) != &sym)
+ return;
+ if (GELF_ST_TYPE(sym.st_info) == STT_FUNC)
+ nfuncsyms++;
+ }
+
+ if (nfuncsyms == 0)
+ return;
+
+ /*
+ * Allocate space for the new entries.
+ */
+ firsttime = image->pi_symbols == NULL;
+ symptr = reallocarray(image->pi_symbols,
+ image->pi_symcount + nfuncsyms, sizeof(*symptr));
+ if (symptr == image->pi_symbols) /* realloc() failed. */
+ return;
+ image->pi_symbols = symptr;
+
+ /*
+ * Append new symbols to the end of the current table.
+ */
+ symptr += image->pi_symcount;
+
+ for (n = newsyms = 0; n < nshsyms; n++) {
+ if (gelf_getsym(data, (int) n, &sym) != &sym)
+ return;
+ if (GELF_ST_TYPE(sym.st_info) != STT_FUNC)
+ continue;
+
+ if (sym.st_shndx == STN_UNDEF)
+ continue;
+
+ if (!firsttime && pmcstat_symbol_search(image, sym.st_value))
+ continue; /* We've seen this symbol already. */
+
+ if ((fnname = elf_strptr(e, sh->sh_link, sym.st_name))
+ == NULL)
+ continue;
+#ifdef __arm__
+ /* Remove spurious ARM function name. */
+ if (fnname[0] == '$' &&
+ (fnname[1] == 'a' || fnname[1] == 't' ||
+ fnname[1] == 'd') &&
+ fnname[2] == '\0')
+ continue;
+#endif
+
+ symptr->ps_name = pmcstat_string_intern(fnname);
+ symptr->ps_start = sym.st_value - image->pi_vaddr;
+ symptr->ps_end = symptr->ps_start + sym.st_size;
+
+ symptr++;
+ newsyms++;
+ }
+
+ image->pi_symcount += newsyms;
+ if (image->pi_symcount == 0)
+ return;
+
+ assert(newsyms <= nfuncsyms);
+
+ /*
+ * Return space to the system if there were duplicates.
+ */
+ if (newsyms < nfuncsyms)
+ image->pi_symbols = reallocarray(image->pi_symbols,
+ image->pi_symcount, sizeof(*symptr));
+
+ /*
+ * Keep the list of symbols sorted.
+ */
+ qsort(image->pi_symbols, image->pi_symcount, sizeof(*symptr),
+ pmcstat_symbol_compare);
+
+ /*
+ * Deal with function symbols that have a size of 'zero' by
+ * making them extend to the next higher address. These
+ * symbols are usually defined in assembly code.
+ */
+ for (symptr = image->pi_symbols;
+ symptr < image->pi_symbols + (image->pi_symcount - 1);
+ symptr++)
+ if (symptr->ps_start == symptr->ps_end)
+ symptr->ps_end = (symptr+1)->ps_start;
+}
+
+/*
+ * Record the fact that PC values from 'start' to 'end' come from
+ * image 'image'.
+ */
+
+void
+pmcstat_image_link(struct pmcstat_process *pp, struct pmcstat_image *image,
+ uintfptr_t start)
+{
+ struct pmcstat_pcmap *pcm, *pcmnew;
+ uintfptr_t offset;
+
+ assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN &&
+ image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE);
+
+ if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL)
+ err(EX_OSERR, "ERROR: Cannot create a map entry");
+
+ /*
+ * Adjust the map entry to only cover the text portion
+ * of the object.
+ */
+
+ offset = start - image->pi_vaddr;
+ pcmnew->ppm_lowpc = image->pi_start + offset;
+ pcmnew->ppm_highpc = image->pi_end + offset;
+ pcmnew->ppm_image = image;
+
+ assert(pcmnew->ppm_lowpc < pcmnew->ppm_highpc);
+
+ /* Overlapped mmap()'s are assumed to never occur. */
+ TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next)
+ if (pcm->ppm_lowpc >= pcmnew->ppm_highpc)
+ break;
+
+ if (pcm == NULL)
+ TAILQ_INSERT_TAIL(&pp->pp_map, pcmnew, ppm_next);
+ else
+ TAILQ_INSERT_BEFORE(pcm, pcmnew, ppm_next);
+}
+
+/*
+ * Determine whether a given executable image is an A.OUT object, and
+ * if so, fill in its parameters from the text file.
+ * Sets image->pi_type.
+ */
+
+void
+pmcstat_image_get_aout_params(struct pmcstat_image *image,
+ struct pmcstat_args *args)
+{
+ int fd;
+ ssize_t nbytes;
+ struct exec ex;
+ const char *path;
+ char buffer[PATH_MAX];
+
+ path = pmcstat_string_unintern(image->pi_execpath);
+ assert(path != NULL);
+
+ if (image->pi_iskernelmodule)
+ errx(EX_SOFTWARE,
+ "ERROR: a.out kernel modules are unsupported \"%s\"", path);
+
+ (void) snprintf(buffer, sizeof(buffer), "%s%s",
+ args->pa_fsroot, path);
+
+ if ((fd = open(buffer, O_RDONLY, 0)) < 0 ||
+ (nbytes = read(fd, &ex, sizeof(ex))) < 0) {
+ if (args->pa_verbosity >= 2)
+ warn("WARNING: Cannot determine type of \"%s\"",
+ path);
+ image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE;
+ if (fd != -1)
+ (void) close(fd);
+ return;
+ }
+
+ (void) close(fd);
+
+ if ((unsigned) nbytes != sizeof(ex) ||
+ N_BADMAG(ex))
+ return;
+
+ image->pi_type = PMCSTAT_IMAGE_AOUT;
+
+ /* TODO: the rest of a.out processing */
+
+ return;
+}
+
+/*
+ * Examine an ELF file to determine the size of its text segment.
+ * Sets image->pi_type if anything conclusive can be determined about
+ * this image.
+ */
+
+void
+pmcstat_image_get_elf_params(struct pmcstat_image *image,
+ struct pmcstat_args *args)
+{
+ int fd;
+ size_t i, nph, nsh;
+ const char *path, *elfbase;
+ char *p, *endp;
+ uintfptr_t minva, maxva;
+ Elf *e;
+ Elf_Scn *scn;
+ GElf_Ehdr eh;
+ GElf_Phdr ph;
+ GElf_Shdr sh;
+ enum pmcstat_image_type image_type;
+ char buffer[PATH_MAX];
+
+ assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN);
+
+ image->pi_start = minva = ~(uintfptr_t) 0;
+ image->pi_end = maxva = (uintfptr_t) 0;
+ image->pi_type = image_type = PMCSTAT_IMAGE_INDETERMINABLE;
+ image->pi_isdynamic = 0;
+ image->pi_dynlinkerpath = NULL;
+ image->pi_vaddr = 0;
+
+ path = pmcstat_string_unintern(image->pi_execpath);
+ assert(path != NULL);
+
+ /*
+ * Look for kernel modules under FSROOT/KERNELPATH/NAME,
+ * and user mode executable objects under FSROOT/PATHNAME.
+ */
+ if (image->pi_iskernelmodule)
+ (void) snprintf(buffer, sizeof(buffer), "%s%s/%s",
+ args->pa_fsroot, args->pa_kernel, path);
+ else
+ (void) snprintf(buffer, sizeof(buffer), "%s%s",
+ args->pa_fsroot, path);
+
+ e = NULL;
+ if ((fd = open(buffer, O_RDONLY, 0)) < 0) {
+ warnx("WARNING: Cannot open \"%s\".",
+ buffer);
+ goto done;
+ }
+
+ if (elf_version(EV_CURRENT) == EV_NONE) {
+ warnx("WARNING: failed to init elf\n");
+ goto done;
+ }
+
+ if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
+ warnx("WARNING: Cannot read \"%s\".",
+ buffer);
+ goto done;
+ }
+
+ if (elf_kind(e) != ELF_K_ELF) {
+ if (args->pa_verbosity >= 2)
+ warnx("WARNING: Cannot determine the type of \"%s\".",
+ buffer);
+ goto done;
+ }
+
+ if (gelf_getehdr(e, &eh) != &eh) {
+ warnx(
+ "WARNING: Cannot retrieve the ELF Header for \"%s\": %s.",
+ buffer, elf_errmsg(-1));
+ goto done;
+ }
+
+ if (eh.e_type != ET_EXEC && eh.e_type != ET_DYN &&
+ !(image->pi_iskernelmodule && eh.e_type == ET_REL)) {
+ warnx("WARNING: \"%s\" is of an unsupported ELF type.",
+ buffer);
+ goto done;
+ }
+
+ image_type = eh.e_ident[EI_CLASS] == ELFCLASS32 ?
+ PMCSTAT_IMAGE_ELF32 : PMCSTAT_IMAGE_ELF64;
+
+ /*
+ * Determine the virtual address where an executable would be
+ * loaded. Additionally, for dynamically linked executables,
+ * save the pathname to the runtime linker.
+ */
+ if (eh.e_type == ET_EXEC) {
+ if (elf_getphnum(e, &nph) == 0) {
+ warnx(
+"WARNING: Could not determine the number of program headers in \"%s\": %s.",
+ buffer,
+ elf_errmsg(-1));
+ goto done;
+ }
+ for (i = 0; i < eh.e_phnum; i++) {
+ if (gelf_getphdr(e, i, &ph) != &ph) {
+ warnx(
+"WARNING: Retrieval of PHDR entry #%ju in \"%s\" failed: %s.",
+ (uintmax_t) i, buffer, elf_errmsg(-1));
+ goto done;
+ }
+ switch (ph.p_type) {
+ case PT_DYNAMIC:
+ image->pi_isdynamic = 1;
+ break;
+ case PT_INTERP:
+ if ((elfbase = elf_rawfile(e, NULL)) == NULL) {
+ warnx(
+"WARNING: Cannot retrieve the interpreter for \"%s\": %s.",
+ buffer, elf_errmsg(-1));
+ goto done;
+ }
+ image->pi_dynlinkerpath =
+ pmcstat_string_intern(elfbase +
+ ph.p_offset);
+ break;
+ case PT_LOAD:
+ if ((ph.p_flags & PF_X) != 0 &&
+ (ph.p_offset & (-ph.p_align)) == 0)
+ image->pi_vaddr = ph.p_vaddr & (-ph.p_align);
+ break;
+ }
+ }
+ }
+
+ /*
+ * Get the min and max VA associated with this ELF object.
+ */
+ if (elf_getshnum(e, &nsh) == 0) {
+ warnx(
+"WARNING: Could not determine the number of sections for \"%s\": %s.",
+ buffer, elf_errmsg(-1));
+ goto done;
+ }
+
+ for (i = 0; i < nsh; i++) {
+ if ((scn = elf_getscn(e, i)) == NULL ||
+ gelf_getshdr(scn, &sh) != &sh) {
+ warnx(
+"WARNING: Could not retrieve section header #%ju in \"%s\": %s.",
+ (uintmax_t) i, buffer, elf_errmsg(-1));
+ goto done;
+ }
+ if (sh.sh_flags & SHF_EXECINSTR) {
+ minva = min(minva, sh.sh_addr);
+ maxva = max(maxva, sh.sh_addr + sh.sh_size);
+ }
+ if (sh.sh_type == SHT_SYMTAB || sh.sh_type == SHT_DYNSYM)
+ pmcstat_image_add_symbols(image, e, scn, &sh);
+ }
+
+ image->pi_start = minva;
+ image->pi_end = maxva;
+ image->pi_type = image_type;
+ image->pi_fullpath = pmcstat_string_intern(buffer);
+
+ /* Build display name
+ */
+ endp = buffer;
+ for (p = buffer; *p; p++)
+ if (*p == '/')
+ endp = p+1;
+ image->pi_name = pmcstat_string_intern(endp);
+
+ done:
+ (void) elf_end(e);
+ if (fd >= 0)
+ (void) close(fd);
+ return;
+}
+
+/*
+ * Given an image descriptor, determine whether it is an ELF, or AOUT.
+ * If no handler claims the image, set its type to 'INDETERMINABLE'.
+ */
+
+void
+pmcstat_image_determine_type(struct pmcstat_image *image,
+ struct pmcstat_args *args)
+{
+ assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN);
+
+ /* Try each kind of handler in turn */
+ if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
+ pmcstat_image_get_elf_params(image, args);
+ if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
+ pmcstat_image_get_aout_params(image, args);
+
+ /*
+ * Otherwise, remember that we tried to determine
+ * the object's type and had failed.
+ */
+ if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
+ image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE;
+}
+
+/*
+ * Locate an image descriptor given an interned path, adding a fresh
+ * descriptor to the cache if necessary. This function also finds a
+ * suitable name for this image's sample file.
+ *
+ * We defer filling in the file format specific parts of the image
+ * structure till the time we actually see a sample that would fall
+ * into this image.
+ */
+
+struct pmcstat_image *
+pmcstat_image_from_path(pmcstat_interned_string internedpath,
+ int iskernelmodule, struct pmcstat_args *args,
+ struct pmc_plugins *plugins)
+{
+ int hash;
+ struct pmcstat_image *pi;
+
+ hash = pmcstat_string_lookup_hash(internedpath);
+
+ /* First, look for an existing entry. */
+ LIST_FOREACH(pi, &pmcstat_image_hash[hash], pi_next)
+ if (pi->pi_execpath == internedpath &&
+ pi->pi_iskernelmodule == iskernelmodule)
+ return (pi);
+
+ /*
+ * Allocate a new entry and place it at the head of the hash
+ * and LRU lists.
+ */
+ pi = malloc(sizeof(*pi));
+ if (pi == NULL)
+ return (NULL);
+
+ pi->pi_type = PMCSTAT_IMAGE_UNKNOWN;
+ pi->pi_execpath = internedpath;
+ pi->pi_start = ~0;
+ pi->pi_end = 0;
+ pi->pi_entry = 0;
+ pi->pi_vaddr = 0;
+ pi->pi_isdynamic = 0;
+ pi->pi_iskernelmodule = iskernelmodule;
+ pi->pi_dynlinkerpath = NULL;
+ pi->pi_symbols = NULL;
+ pi->pi_symcount = 0;
+ pi->pi_addr2line = NULL;
+
+ if (plugins[args->pa_pplugin].pl_initimage != NULL)
+ plugins[args->pa_pplugin].pl_initimage(pi);
+ if (plugins[args->pa_plugin].pl_initimage != NULL)
+ plugins[args->pa_plugin].pl_initimage(pi);
+
+ LIST_INSERT_HEAD(&pmcstat_image_hash[hash], pi, pi_next);
+
+ return (pi);
+}
diff --git a/lib/libpmcstat/libpmcstat_logging.c b/lib/libpmcstat/libpmcstat_logging.c
new file mode 100644
index 000000000000..d86762f38bb8
--- /dev/null
+++ b/lib/libpmcstat/libpmcstat_logging.c
@@ -0,0 +1,718 @@
+/*-
+ * Copyright (c) 2003-2008 Joseph Koshy
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/cpuset.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/pmc.h>
+
+#include <assert.h>
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <netdb.h>
+#include <pmc.h>
+#include <pmclog.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <sysexits.h>
+#include <unistd.h>
+
+#include "libpmcstat.h"
+
+/*
+ * Get PMC record by id, apply merge policy.
+ */
+
+static struct pmcstat_pmcrecord *
+pmcstat_lookup_pmcid(pmc_id_t pmcid, int pmcstat_mergepmc)
+{
+ struct pmcstat_pmcrecord *pr;
+
+ LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) {
+ if (pr->pr_pmcid == pmcid) {
+ if (pmcstat_mergepmc)
+ return pr->pr_merge;
+ return pr;
+ }
+ }
+
+ return NULL;
+}
+
+/*
+ * Add a {pmcid,name} mapping.
+ */
+
+static void
+pmcstat_pmcid_add(pmc_id_t pmcid, pmcstat_interned_string ps,
+ struct pmcstat_args *args, struct pmc_plugins *plugins,
+ int *pmcstat_npmcs)
+{
+ struct pmcstat_pmcrecord *pr, *prm;
+
+ /* Replace an existing name for the PMC. */
+ prm = NULL;
+ LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
+ if (pr->pr_pmcid == pmcid) {
+ pr->pr_pmcname = ps;
+ return;
+ } else if (pr->pr_pmcname == ps)
+ prm = pr;
+
+ /*
+ * Otherwise, allocate a new descriptor and call the
+ * plugins hook.
+ */
+ if ((pr = malloc(sizeof(*pr))) == NULL)
+ err(EX_OSERR, "ERROR: Cannot allocate pmc record");
+
+ pr->pr_pmcid = pmcid;
+ pr->pr_pmcname = ps;
+ pr->pr_pmcin = *pmcstat_npmcs++;
+ pr->pr_samples = 0;
+ pr->pr_dubious_frames = 0;
+ pr->pr_merge = prm == NULL ? pr : prm;
+
+ LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next);
+
+ if (plugins[args->pa_pplugin].pl_newpmc != NULL)
+ plugins[args->pa_pplugin].pl_newpmc(ps, pr);
+ if (plugins[args->pa_plugin].pl_newpmc != NULL)
+ plugins[args->pa_plugin].pl_newpmc(ps, pr);
+}
+
+/*
+ * Unmap images in the range [start..end) associated with process
+ * 'pp'.
+ */
+
+static void
+pmcstat_image_unmap(struct pmcstat_process *pp, uintfptr_t start,
+ uintfptr_t end)
+{
+ struct pmcstat_pcmap *pcm, *pcmtmp, *pcmnew;
+
+ assert(pp != NULL);
+ assert(start < end);
+
+ /*
+ * Cases:
+ * - we could have the range completely in the middle of an
+ * existing pcmap; in this case we have to split the pcmap
+ * structure into two (i.e., generate a 'hole').
+ * - we could have the range covering multiple pcmaps; these
+ * will have to be removed.
+ * - we could have either 'start' or 'end' falling in the
+ * middle of a pcmap; in this case shorten the entry.
+ */
+ TAILQ_FOREACH_SAFE(pcm, &pp->pp_map, ppm_next, pcmtmp) {
+ assert(pcm->ppm_lowpc < pcm->ppm_highpc);
+ if (pcm->ppm_highpc <= start)
+ continue;
+ if (pcm->ppm_lowpc >= end)
+ return;
+ if (pcm->ppm_lowpc >= start && pcm->ppm_highpc <= end) {
+ /*
+ * The current pcmap is completely inside the
+ * unmapped range: remove it entirely.
+ */
+ TAILQ_REMOVE(&pp->pp_map, pcm, ppm_next);
+ free(pcm);
+ } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc > end) {
+ /*
+ * Split this pcmap into two; curtail the
+ * current map to end at [start-1], and start
+ * the new one at [end].
+ */
+ if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL)
+ err(EX_OSERR,
+ "ERROR: Cannot split a map entry");
+
+ pcmnew->ppm_image = pcm->ppm_image;
+
+ pcmnew->ppm_lowpc = end;
+ pcmnew->ppm_highpc = pcm->ppm_highpc;
+
+ pcm->ppm_highpc = start;
+
+ TAILQ_INSERT_AFTER(&pp->pp_map, pcm, pcmnew, ppm_next);
+
+ return;
+ } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc <= end)
+ pcm->ppm_highpc = start;
+ else if (pcm->ppm_lowpc >= start && pcm->ppm_highpc > end)
+ pcm->ppm_lowpc = end;
+ else
+ assert(0);
+ }
+}
+
+/*
+ * Convert a hwpmc(4) log to profile information. A system-wide
+ * callgraph is generated if FLAG_DO_CALLGRAPHS is set. gmon.out
+ * files usable by gprof(1) are created if FLAG_DO_GPROF is set.
+ */
+int
+pmcstat_analyze_log(struct pmcstat_args *args,
+ struct pmc_plugins *plugins,
+ struct pmcstat_stats *pmcstat_stats,
+ struct pmcstat_process *pmcstat_kernproc,
+ int pmcstat_mergepmc,
+ int *pmcstat_npmcs,
+ int *ps_samples_period)
+{
+ uint32_t cpu, cpuflags;
+ uintfptr_t pc;
+ pid_t pid;
+ struct pmcstat_image *image;
+ struct pmcstat_process *pp, *ppnew;
+ struct pmcstat_pcmap *ppm, *ppmtmp;
+ struct pmclog_ev ev;
+ struct pmcstat_pmcrecord *pmcr;
+ pmcstat_interned_string image_path;
+
+ assert(args->pa_flags & FLAG_DO_ANALYSIS);
+
+ if (elf_version(EV_CURRENT) == EV_NONE)
+ err(EX_UNAVAILABLE, "Elf library initialization failed");
+
+ while (pmclog_read(args->pa_logparser, &ev) == 0) {
+ assert(ev.pl_state == PMCLOG_OK);
+
+ switch (ev.pl_type) {
+ case PMCLOG_TYPE_INITIALIZE:
+ if ((ev.pl_u.pl_i.pl_version & 0xFF000000) !=
+ PMC_VERSION_MAJOR << 24 && args->pa_verbosity > 0)
+ warnx(
+"WARNING: Log version 0x%x does not match compiled version 0x%x.",
+ ev.pl_u.pl_i.pl_version, PMC_VERSION_MAJOR);
+ break;
+
+ case PMCLOG_TYPE_MAP_IN:
+ /*
+ * Introduce an address range mapping for a
+ * userland process or the kernel (pid == -1).
+ *
+ * We always allocate a process descriptor so
+ * that subsequent samples seen for this
+ * address range are mapped to the current
+ * object being mapped in.
+ */
+ pid = ev.pl_u.pl_mi.pl_pid;
+ if (pid == -1)
+ pp = pmcstat_kernproc;
+ else
+ pp = pmcstat_process_lookup(pid,
+ PMCSTAT_ALLOCATE);
+
+ assert(pp != NULL);
+
+ image_path = pmcstat_string_intern(ev.pl_u.pl_mi.
+ pl_pathname);
+ image = pmcstat_image_from_path(image_path, pid == -1,
+ args, plugins);
+ if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
+ pmcstat_image_determine_type(image, args);
+ if (image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE)
+ pmcstat_image_link(pp, image,
+ ev.pl_u.pl_mi.pl_start);
+ break;
+
+ case PMCLOG_TYPE_MAP_OUT:
+ /*
+ * Remove an address map.
+ */
+ pid = ev.pl_u.pl_mo.pl_pid;
+ if (pid == -1)
+ pp = pmcstat_kernproc;
+ else
+ pp = pmcstat_process_lookup(pid, 0);
+
+ if (pp == NULL) /* unknown process */
+ break;
+
+ pmcstat_image_unmap(pp, ev.pl_u.pl_mo.pl_start,
+ ev.pl_u.pl_mo.pl_end);
+ break;
+
+ case PMCLOG_TYPE_PCSAMPLE:
+ /*
+ * Note: the `PCSAMPLE' log entry is not
+ * generated by hpwmc(4) after version 2.
+ */
+
+ /*
+ * We bring in the gmon file for the image
+ * currently associated with the PMC & pid
+ * pair and increment the appropriate entry
+ * bin inside this.
+ */
+ pmcstat_stats->ps_samples_total++;
+ *ps_samples_period += 1;
+
+ pc = ev.pl_u.pl_s.pl_pc;
+ pp = pmcstat_process_lookup(ev.pl_u.pl_s.pl_pid,
+ PMCSTAT_ALLOCATE);
+
+ /* Get PMC record. */
+ pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_s.pl_pmcid, pmcstat_mergepmc);
+ assert(pmcr != NULL);
+ pmcr->pr_samples++;
+
+ /*
+ * Call the plugins processing
+ * TODO: move pmcstat_process_find_map inside plugins
+ */
+
+ if (plugins[args->pa_pplugin].pl_process != NULL)
+ plugins[args->pa_pplugin].pl_process(
+ pp, pmcr, 1, &pc,
+ pmcstat_process_find_map(pp, pc) != NULL, 0);
+ plugins[args->pa_plugin].pl_process(
+ pp, pmcr, 1, &pc,
+ pmcstat_process_find_map(pp, pc) != NULL, 0);
+ break;
+
+ case PMCLOG_TYPE_CALLCHAIN:
+ pmcstat_stats->ps_samples_total++;
+ *ps_samples_period += 1;
+
+ cpuflags = ev.pl_u.pl_cc.pl_cpuflags;
+ cpu = PMC_CALLCHAIN_CPUFLAGS_TO_CPU(cpuflags);
+
+ /* Filter on the CPU id. */
+ if (!CPU_ISSET(cpu, &(args->pa_cpumask))) {
+ pmcstat_stats->ps_samples_skipped++;
+ break;
+ }
+
+ pp = pmcstat_process_lookup(ev.pl_u.pl_cc.pl_pid,
+ PMCSTAT_ALLOCATE);
+
+ /* Get PMC record. */
+ pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_cc.pl_pmcid, pmcstat_mergepmc);
+ assert(pmcr != NULL);
+ pmcr->pr_samples++;
+
+ /*
+ * Call the plugins processing
+ */
+
+ if (plugins[args->pa_pplugin].pl_process != NULL)
+ plugins[args->pa_pplugin].pl_process(
+ pp, pmcr,
+ ev.pl_u.pl_cc.pl_npc,
+ ev.pl_u.pl_cc.pl_pc,
+ PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags),
+ cpu);
+ plugins[args->pa_plugin].pl_process(
+ pp, pmcr,
+ ev.pl_u.pl_cc.pl_npc,
+ ev.pl_u.pl_cc.pl_pc,
+ PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags),
+ cpu);
+ break;
+
+ case PMCLOG_TYPE_PMCALLOCATE:
+ /*
+ * Record the association pmc id between this
+ * PMC and its name.
+ */
+ pmcstat_pmcid_add(ev.pl_u.pl_a.pl_pmcid,
+ pmcstat_string_intern(ev.pl_u.pl_a.pl_evname),
+ args, plugins, pmcstat_npmcs);
+ break;
+
+ case PMCLOG_TYPE_PMCALLOCATEDYN:
+ /*
+ * Record the association pmc id between this
+ * PMC and its name.
+ */
+ pmcstat_pmcid_add(ev.pl_u.pl_ad.pl_pmcid,
+ pmcstat_string_intern(ev.pl_u.pl_ad.pl_evname),
+ args, plugins, pmcstat_npmcs);
+ break;
+
+ case PMCLOG_TYPE_PROCEXEC:
+ /*
+ * Change the executable image associated with
+ * a process.
+ */
+ pp = pmcstat_process_lookup(ev.pl_u.pl_x.pl_pid,
+ PMCSTAT_ALLOCATE);
+
+ /* delete the current process map */
+ TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
+ TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
+ free(ppm);
+ }
+
+ /*
+ * Associate this process image.
+ */
+ image_path = pmcstat_string_intern(
+ ev.pl_u.pl_x.pl_pathname);
+ assert(image_path != NULL);
+ pmcstat_process_exec(pp, image_path,
+ ev.pl_u.pl_x.pl_entryaddr, args,
+ plugins, pmcstat_stats);
+ break;
+
+ case PMCLOG_TYPE_PROCEXIT:
+
+ /*
+ * Due to the way the log is generated, the
+ * last few samples corresponding to a process
+ * may appear in the log after the process
+ * exit event is recorded. Thus we keep the
+ * process' descriptor and associated data
+ * structures around, but mark the process as
+ * having exited.
+ */
+ pp = pmcstat_process_lookup(ev.pl_u.pl_e.pl_pid, 0);
+ if (pp == NULL)
+ break;
+ pp->pp_isactive = 0; /* mark as a zombie */
+ break;
+
+ case PMCLOG_TYPE_SYSEXIT:
+ pp = pmcstat_process_lookup(ev.pl_u.pl_se.pl_pid, 0);
+ if (pp == NULL)
+ break;
+ pp->pp_isactive = 0; /* make a zombie */
+ break;
+
+ case PMCLOG_TYPE_PROCFORK:
+
+ /*
+ * Allocate a process descriptor for the new
+ * (child) process.
+ */
+ ppnew =
+ pmcstat_process_lookup(ev.pl_u.pl_f.pl_newpid,
+ PMCSTAT_ALLOCATE);
+
+ /*
+ * If we had been tracking the parent, clone
+ * its address maps.
+ */
+ pp = pmcstat_process_lookup(ev.pl_u.pl_f.pl_oldpid, 0);
+ if (pp == NULL)
+ break;
+ TAILQ_FOREACH(ppm, &pp->pp_map, ppm_next)
+ pmcstat_image_link(ppnew, ppm->ppm_image,
+ ppm->ppm_lowpc);
+ break;
+
+ default: /* other types of entries are not relevant */
+ break;
+ }
+ }
+
+ if (ev.pl_state == PMCLOG_EOF)
+ return (PMCSTAT_FINISHED);
+ else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
+ return (PMCSTAT_RUNNING);
+
+ err(EX_DATAERR,
+ "ERROR: event parsing failed (record %jd, offset 0x%jx)",
+ (uintmax_t) ev.pl_count + 1, ev.pl_offset);
+}
+
+/*
+ * Open a log file, for reading or writing.
+ *
+ * The function returns the fd of a successfully opened log or -1 in
+ * case of failure.
+ */
+
+int
+pmcstat_open_log(const char *path, int mode)
+{
+ int error, fd, cfd;
+ size_t hlen;
+ const char *p, *errstr;
+ struct addrinfo hints, *res, *res0;
+ char hostname[MAXHOSTNAMELEN];
+
+ errstr = NULL;
+ fd = -1;
+
+ /*
+ * If 'path' is "-" then open one of stdin or stdout depending
+ * on the value of 'mode'.
+ *
+ * If 'path' contains a ':' and does not start with a '/' or '.',
+ * and is being opened for writing, treat it as a "host:port"
+ * specification and open a network socket.
+ *
+ * Otherwise, treat 'path' as a file name and open that.
+ */
+ if (path[0] == '-' && path[1] == '\0')
+ fd = (mode == PMCSTAT_OPEN_FOR_READ) ? 0 : 1;
+ else if (path[0] != '/' &&
+ path[0] != '.' && strchr(path, ':') != NULL) {
+
+ p = strrchr(path, ':');
+ hlen = p - path;
+ if (p == path || hlen >= sizeof(hostname)) {
+ errstr = strerror(EINVAL);
+ goto done;
+ }
+
+ assert(hlen < sizeof(hostname));
+ (void) strncpy(hostname, path, hlen);
+ hostname[hlen] = '\0';
+
+ (void) memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ if ((error = getaddrinfo(hostname, p+1, &hints, &res0)) != 0) {
+ errstr = gai_strerror(error);
+ goto done;
+ }
+
+ fd = -1;
+ for (res = res0; res; res = res->ai_next) {
+ if ((fd = socket(res->ai_family, res->ai_socktype,
+ res->ai_protocol)) < 0) {
+ errstr = strerror(errno);
+ continue;
+ }
+ if (mode == PMCSTAT_OPEN_FOR_READ) {
+ if (bind(fd, res->ai_addr, res->ai_addrlen) < 0) {
+ errstr = strerror(errno);
+ (void) close(fd);
+ fd = -1;
+ continue;
+ }
+ listen(fd, 1);
+ cfd = accept(fd, NULL, NULL);
+ (void) close(fd);
+ if (cfd < 0) {
+ errstr = strerror(errno);
+ fd = -1;
+ break;
+ }
+ fd = cfd;
+ } else {
+ if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
+ errstr = strerror(errno);
+ (void) close(fd);
+ fd = -1;
+ continue;
+ }
+ }
+ errstr = NULL;
+ break;
+ }
+ freeaddrinfo(res0);
+
+ } else if ((fd = open(path, mode == PMCSTAT_OPEN_FOR_READ ?
+ O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC),
+ S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
+ errstr = strerror(errno);
+
+ done:
+ if (errstr)
+ errx(EX_OSERR, "ERROR: Cannot open \"%s\" for %s: %s.", path,
+ (mode == PMCSTAT_OPEN_FOR_READ ? "reading" : "writing"),
+ errstr);
+
+ return (fd);
+}
+
+/*
+ * Close a logfile, after first flushing all in-module queued data.
+ */
+
+int
+pmcstat_close_log(struct pmcstat_args *args)
+{
+ /* If a local logfile is configured ask the kernel to stop
+ * and flush data. Kernel will close the file when data is flushed
+ * so keep the status to EXITING.
+ */
+ if (args->pa_logfd != -1) {
+ if (pmc_close_logfile() < 0)
+ err(EX_OSERR, "ERROR: logging failed");
+ }
+
+ return (args->pa_flags & FLAG_HAS_PIPE ? PMCSTAT_EXITING :
+ PMCSTAT_FINISHED);
+}
+
+/*
+ * Initialize module.
+ */
+
+void
+pmcstat_initialize_logging(struct pmcstat_process **pmcstat_kernproc,
+ struct pmcstat_args *args, struct pmc_plugins *plugins,
+ int *pmcstat_npmcs, int *pmcstat_mergepmc)
+{
+ struct pmcstat_process *pmcstat_kp;
+ int i;
+
+ /* use a convenient format for 'ldd' output */
+ if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1","%o \"%p\" %x\n",1) != 0)
+ err(EX_OSERR, "ERROR: Cannot setenv");
+
+ /* Initialize hash tables */
+ pmcstat_string_initialize();
+ for (i = 0; i < PMCSTAT_NHASH; i++) {
+ LIST_INIT(&pmcstat_image_hash[i]);
+ LIST_INIT(&pmcstat_process_hash[i]);
+ }
+
+ /*
+ * Create a fake 'process' entry for the kernel with pid -1.
+ * hwpmc(4) will subsequently inform us about where the kernel
+ * and any loaded kernel modules are mapped.
+ */
+ if ((pmcstat_kp = pmcstat_process_lookup((pid_t) -1,
+ PMCSTAT_ALLOCATE)) == NULL)
+ err(EX_OSERR, "ERROR: Cannot initialize logging");
+
+ *pmcstat_kernproc = pmcstat_kp;
+
+ /* PMC count. */
+ *pmcstat_npmcs = 0;
+
+ /* Merge PMC with same name. */
+ *pmcstat_mergepmc = args->pa_mergepmc;
+
+ /*
+ * Initialize plugins
+ */
+
+ if (plugins[args->pa_pplugin].pl_init != NULL)
+ plugins[args->pa_pplugin].pl_init();
+ if (plugins[args->pa_plugin].pl_init != NULL)
+ plugins[args->pa_plugin].pl_init();
+}
+
+/*
+ * Shutdown module.
+ */
+
+void
+pmcstat_shutdown_logging(struct pmcstat_args *args,
+ struct pmc_plugins *plugins,
+ struct pmcstat_stats *pmcstat_stats)
+{
+ struct pmcstat_image *pi, *pitmp;
+ struct pmcstat_process *pp, *pptmp;
+ struct pmcstat_pcmap *ppm, *ppmtmp;
+ FILE *mf;
+ int i;
+
+ /* determine where to send the map file */
+ mf = NULL;
+ if (args->pa_mapfilename != NULL)
+ mf = (strcmp(args->pa_mapfilename, "-") == 0) ?
+ args->pa_printfile : fopen(args->pa_mapfilename, "w");
+
+ if (mf == NULL && args->pa_flags & FLAG_DO_GPROF &&
+ args->pa_verbosity >= 2)
+ mf = args->pa_printfile;
+
+ if (mf)
+ (void) fprintf(mf, "MAP:\n");
+
+ /*
+ * Shutdown the plugins
+ */
+
+ if (plugins[args->pa_plugin].pl_shutdown != NULL)
+ plugins[args->pa_plugin].pl_shutdown(mf);
+ if (plugins[args->pa_pplugin].pl_shutdown != NULL)
+ plugins[args->pa_pplugin].pl_shutdown(mf);
+
+ for (i = 0; i < PMCSTAT_NHASH; i++) {
+ LIST_FOREACH_SAFE(pi, &pmcstat_image_hash[i], pi_next,
+ pitmp) {
+ if (plugins[args->pa_plugin].pl_shutdownimage != NULL)
+ plugins[args->pa_plugin].pl_shutdownimage(pi);
+ if (plugins[args->pa_pplugin].pl_shutdownimage != NULL)
+ plugins[args->pa_pplugin].pl_shutdownimage(pi);
+
+ free(pi->pi_symbols);
+ if (pi->pi_addr2line != NULL)
+ pclose(pi->pi_addr2line);
+ LIST_REMOVE(pi, pi_next);
+ free(pi);
+ }
+
+ LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[i], pp_next,
+ pptmp) {
+ TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
+ TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
+ free(ppm);
+ }
+ LIST_REMOVE(pp, pp_next);
+ free(pp);
+ }
+ }
+
+ pmcstat_string_shutdown();
+
+ /*
+ * Print errors unless -q was specified. Print all statistics
+ * if verbosity > 1.
+ */
+#define PRINT(N,V) do { \
+ if (pmcstat_stats->ps_##V || args->pa_verbosity >= 2) \
+ (void) fprintf(args->pa_printfile, " %-40s %d\n",\
+ N, pmcstat_stats->ps_##V); \
+ } while (0)
+
+ if (args->pa_verbosity >= 1 && (args->pa_flags & FLAG_DO_ANALYSIS)) {
+ (void) fprintf(args->pa_printfile, "CONVERSION STATISTICS:\n");
+ PRINT("#exec/a.out", exec_aout);
+ PRINT("#exec/elf", exec_elf);
+ PRINT("#exec/unknown", exec_indeterminable);
+ PRINT("#exec handling errors", exec_errors);
+ PRINT("#samples/total", samples_total);
+ PRINT("#samples/unclaimed", samples_unknown_offset);
+ PRINT("#samples/unknown-object", samples_indeterminable);
+ PRINT("#samples/unknown-function", samples_unknown_function);
+ PRINT("#callchain/dubious-frames", callchain_dubious_frames);
+ }
+
+ if (mf)
+ (void) fclose(mf);
+}
diff --git a/lib/libpmcstat/libpmcstat_process.c b/lib/libpmcstat/libpmcstat_process.c
new file mode 100644
index 000000000000..147eff9ab23e
--- /dev/null
+++ b/lib/libpmcstat/libpmcstat_process.c
@@ -0,0 +1,363 @@
+/*-
+ * Copyright (c) 2003-2008 Joseph Koshy
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/cpuset.h>
+#include <sys/event.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/module.h>
+#include <sys/pmc.h>
+
+#include <assert.h>
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <netdb.h>
+#include <pmc.h>
+#include <pmclog.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <sysexits.h>
+#include <unistd.h>
+
+#include "libpmcstat.h"
+
+/*
+ * Associate an AOUT image with a process.
+ */
+
+void
+pmcstat_process_aout_exec(struct pmcstat_process *pp,
+ struct pmcstat_image *image, uintfptr_t entryaddr)
+{
+ (void) pp;
+ (void) image;
+ (void) entryaddr;
+ /* TODO Implement a.out handling */
+}
+
+/*
+ * Associate an ELF image with a process.
+ */
+
+void
+pmcstat_process_elf_exec(struct pmcstat_process *pp,
+ struct pmcstat_image *image, uintfptr_t entryaddr,
+ struct pmcstat_args *args, struct pmc_plugins *plugins,
+ struct pmcstat_stats *pmcstat_stats)
+{
+ uintmax_t libstart;
+ struct pmcstat_image *rtldimage;
+
+ assert(image->pi_type == PMCSTAT_IMAGE_ELF32 ||
+ image->pi_type == PMCSTAT_IMAGE_ELF64);
+
+ /* Create a map entry for the base executable. */
+ pmcstat_image_link(pp, image, image->pi_vaddr);
+
+ /*
+ * For dynamically linked executables we need to determine
+ * where the dynamic linker was mapped to for this process,
+ * Subsequent executable objects that are mapped in by the
+ * dynamic linker will be tracked by log events of type
+ * PMCLOG_TYPE_MAP_IN.
+ */
+
+ if (image->pi_isdynamic) {
+
+ /*
+ * The runtime loader gets loaded just after the maximum
+ * possible heap address. Like so:
+ *
+ * [ TEXT DATA BSS HEAP -->*RTLD SHLIBS <--STACK]
+ * ^ ^
+ * 0 VM_MAXUSER_ADDRESS
+
+ *
+ * The exact address where the loader gets mapped in
+ * will vary according to the size of the executable
+ * and the limits on the size of the process'es data
+ * segment at the time of exec(). The entry address
+ * recorded at process exec time corresponds to the
+ * 'start' address inside the dynamic linker. From
+ * this we can figure out the address where the
+ * runtime loader's file object had been mapped to.
+ */
+ rtldimage = pmcstat_image_from_path(image->pi_dynlinkerpath,
+ 0, args, plugins);
+ if (rtldimage == NULL) {
+ warnx("WARNING: Cannot find image for \"%s\".",
+ pmcstat_string_unintern(image->pi_dynlinkerpath));
+ pmcstat_stats->ps_exec_errors++;
+ return;
+ }
+
+ if (rtldimage->pi_type == PMCSTAT_IMAGE_UNKNOWN)
+ pmcstat_image_get_elf_params(rtldimage, args);
+
+ if (rtldimage->pi_type != PMCSTAT_IMAGE_ELF32 &&
+ rtldimage->pi_type != PMCSTAT_IMAGE_ELF64) {
+ warnx("WARNING: rtld not an ELF object \"%s\".",
+ pmcstat_string_unintern(image->pi_dynlinkerpath));
+ return;
+ }
+
+ libstart = entryaddr - rtldimage->pi_entry;
+ pmcstat_image_link(pp, rtldimage, libstart);
+ }
+}
+
+/*
+ * Associate an image and a process.
+ */
+
+void
+pmcstat_process_exec(struct pmcstat_process *pp,
+ pmcstat_interned_string path, uintfptr_t entryaddr,
+ struct pmcstat_args *args, struct pmc_plugins *plugins,
+ struct pmcstat_stats *pmcstat_stats)
+{
+ struct pmcstat_image *image;
+
+ if ((image = pmcstat_image_from_path(path, 0,
+ args, plugins)) == NULL) {
+ pmcstat_stats->ps_exec_errors++;
+ return;
+ }
+
+ if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
+ pmcstat_image_determine_type(image, args);
+
+ assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN);
+
+ switch (image->pi_type) {
+ case PMCSTAT_IMAGE_ELF32:
+ case PMCSTAT_IMAGE_ELF64:
+ pmcstat_stats->ps_exec_elf++;
+ pmcstat_process_elf_exec(pp, image, entryaddr,
+ args, plugins, pmcstat_stats);
+ break;
+
+ case PMCSTAT_IMAGE_AOUT:
+ pmcstat_stats->ps_exec_aout++;
+ pmcstat_process_aout_exec(pp, image, entryaddr);
+ break;
+
+ case PMCSTAT_IMAGE_INDETERMINABLE:
+ pmcstat_stats->ps_exec_indeterminable++;
+ break;
+
+ default:
+ err(EX_SOFTWARE,
+ "ERROR: Unsupported executable type for \"%s\"",
+ pmcstat_string_unintern(path));
+ }
+}
+
+/*
+ * Find the map entry associated with process 'p' at PC value 'pc'.
+ */
+
+struct pmcstat_pcmap *
+pmcstat_process_find_map(struct pmcstat_process *p, uintfptr_t pc)
+{
+ struct pmcstat_pcmap *ppm;
+
+ TAILQ_FOREACH(ppm, &p->pp_map, ppm_next) {
+ if (pc >= ppm->ppm_lowpc && pc < ppm->ppm_highpc)
+ return (ppm);
+ if (pc < ppm->ppm_lowpc)
+ return (NULL);
+ }
+
+ return (NULL);
+}
+
+/*
+ * Find the process descriptor corresponding to a PID. If 'allocate'
+ * is zero, we return a NULL if a pid descriptor could not be found or
+ * a process descriptor process. If 'allocate' is non-zero, then we
+ * will attempt to allocate a fresh process descriptor. Zombie
+ * process descriptors are only removed if a fresh allocation for the
+ * same PID is requested.
+ */
+
+struct pmcstat_process *
+pmcstat_process_lookup(pid_t pid, int allocate)
+{
+ uint32_t hash;
+ struct pmcstat_pcmap *ppm, *ppmtmp;
+ struct pmcstat_process *pp, *pptmp;
+
+ hash = (uint32_t) pid & PMCSTAT_HASH_MASK; /* simplicity wins */
+
+ LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[hash], pp_next, pptmp)
+ if (pp->pp_pid == pid) {
+ /* Found a descriptor, check and process zombies */
+ if (allocate && pp->pp_isactive == 0) {
+ /* remove maps */
+ TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next,
+ ppmtmp) {
+ TAILQ_REMOVE(&pp->pp_map, ppm,
+ ppm_next);
+ free(ppm);
+ }
+ /* remove process entry */
+ LIST_REMOVE(pp, pp_next);
+ free(pp);
+ break;
+ }
+ return (pp);
+ }
+
+ if (!allocate)
+ return (NULL);
+
+ if ((pp = malloc(sizeof(*pp))) == NULL)
+ err(EX_OSERR, "ERROR: Cannot allocate pid descriptor");
+
+ pp->pp_pid = pid;
+ pp->pp_isactive = 1;
+
+ TAILQ_INIT(&pp->pp_map);
+
+ LIST_INSERT_HEAD(&pmcstat_process_hash[hash], pp, pp_next);
+ return (pp);
+}
+
+void
+pmcstat_create_process(int *pmcstat_sockpair, struct pmcstat_args *args,
+ int pmcstat_kq)
+{
+ char token;
+ pid_t pid;
+ struct kevent kev;
+ struct pmcstat_target *pt;
+
+ if (socketpair(AF_UNIX, SOCK_STREAM, 0, pmcstat_sockpair) < 0)
+ err(EX_OSERR, "ERROR: cannot create socket pair");
+
+ switch (pid = fork()) {
+ case -1:
+ err(EX_OSERR, "ERROR: cannot fork");
+ /*NOTREACHED*/
+
+ case 0: /* child */
+ (void) close(pmcstat_sockpair[PARENTSOCKET]);
+
+ /* Write a token to tell our parent we've started executing. */
+ if (write(pmcstat_sockpair[CHILDSOCKET], "+", 1) != 1)
+ err(EX_OSERR, "ERROR (child): cannot write token");
+
+ /* Wait for our parent to signal us to start. */
+ if (read(pmcstat_sockpair[CHILDSOCKET], &token, 1) < 0)
+ err(EX_OSERR, "ERROR (child): cannot read token");
+ (void) close(pmcstat_sockpair[CHILDSOCKET]);
+
+ /* exec() the program requested */
+ execvp(*args->pa_argv, args->pa_argv);
+ /* and if that fails, notify the parent */
+ kill(getppid(), SIGCHLD);
+ err(EX_OSERR, "ERROR: execvp \"%s\" failed", *args->pa_argv);
+ /*NOTREACHED*/
+
+ default: /* parent */
+ (void) close(pmcstat_sockpair[CHILDSOCKET]);
+ break;
+ }
+
+ /* Ask to be notified via a kevent when the target process exits. */
+ EV_SET(&kev, pid, EVFILT_PROC, EV_ADD | EV_ONESHOT, NOTE_EXIT, 0,
+ NULL);
+ if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
+ err(EX_OSERR, "ERROR: cannot monitor child process %d", pid);
+
+ if ((pt = malloc(sizeof(*pt))) == NULL)
+ errx(EX_SOFTWARE, "ERROR: Out of memory.");
+
+ pt->pt_pid = pid;
+ SLIST_INSERT_HEAD(&args->pa_targets, pt, pt_next);
+
+ /* Wait for the child to signal that its ready to go. */
+ if (read(pmcstat_sockpair[PARENTSOCKET], &token, 1) < 0)
+ err(EX_OSERR, "ERROR (parent): cannot read token");
+
+ return;
+}
+
+/*
+ * Do process profiling
+ *
+ * If a pid was specified, attach each allocated PMC to the target
+ * process. Otherwise, fork a child and attach the PMCs to the child,
+ * and have the child exec() the target program.
+ */
+
+void
+pmcstat_start_process(int *pmcstat_sockpair)
+{
+ /* Signal the child to proceed. */
+ if (write(pmcstat_sockpair[PARENTSOCKET], "!", 1) != 1)
+ err(EX_OSERR, "ERROR (parent): write of token failed");
+
+ (void) close(pmcstat_sockpair[PARENTSOCKET]);
+}
+
+void
+pmcstat_attach_pmcs(struct pmcstat_args *args)
+{
+ struct pmcstat_ev *ev;
+ struct pmcstat_target *pt;
+ int count;
+
+ /* Attach all process PMCs to target processes. */
+ count = 0;
+ STAILQ_FOREACH(ev, &args->pa_events, ev_next) {
+ if (PMC_IS_SYSTEM_MODE(ev->ev_mode))
+ continue;
+ SLIST_FOREACH(pt, &args->pa_targets, pt_next) {
+ if (pmc_attach(ev->ev_pmcid, pt->pt_pid) == 0)
+ count++;
+ else if (errno != ESRCH)
+ err(EX_OSERR,
+"ERROR: cannot attach pmc \"%s\" to process %d",
+ ev->ev_name, (int)pt->pt_pid);
+ }
+ }
+
+ if (count == 0)
+ errx(EX_DATAERR, "ERROR: No processes were attached to.");
+}
diff --git a/lib/libpmcstat/libpmcstat_string.c b/lib/libpmcstat/libpmcstat_string.c
new file mode 100644
index 000000000000..d7932c680eb4
--- /dev/null
+++ b/lib/libpmcstat/libpmcstat_string.c
@@ -0,0 +1,156 @@
+/*-
+ * Copyright (c) 2003-2008 Joseph Koshy
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/cpuset.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/pmc.h>
+
+#include <assert.h>
+#include <err.h>
+#include <errno.h>
+#include <pmc.h>
+#include <pmclog.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+
+#include "libpmcstat.h"
+
+static LIST_HEAD(,pmcstat_string) pmcstat_string_hash[PMCSTAT_NHASH];
+
+/*
+ * Intern a copy of string 's', and return a pointer to the
+ * interned structure.
+ */
+
+pmcstat_interned_string
+pmcstat_string_intern(const char *s)
+{
+ struct pmcstat_string *ps;
+ const struct pmcstat_string *cps;
+ int hash, len;
+
+ if ((cps = pmcstat_string_lookup(s)) != NULL)
+ return (cps);
+
+ hash = pmcstat_string_compute_hash(s);
+ len = strlen(s);
+
+ if ((ps = malloc(sizeof(*ps))) == NULL)
+ err(EX_OSERR, "ERROR: Could not intern string");
+ ps->ps_len = len;
+ ps->ps_hash = hash;
+ ps->ps_string = strdup(s);
+ LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next);
+ return ((pmcstat_interned_string) ps);
+}
+
+const char *
+pmcstat_string_unintern(pmcstat_interned_string str)
+{
+ const char *s;
+
+ s = ((const struct pmcstat_string *) str)->ps_string;
+ return (s);
+}
+
+/*
+ * Compute a 'hash' value for a string.
+ */
+
+int
+pmcstat_string_compute_hash(const char *s)
+{
+ unsigned hash;
+
+ for (hash = 2166136261; *s; s++)
+ hash = (hash ^ *s) * 16777619;
+
+ return (hash & PMCSTAT_HASH_MASK);
+}
+
+pmcstat_interned_string
+pmcstat_string_lookup(const char *s)
+{
+ struct pmcstat_string *ps;
+ int hash, len;
+
+ hash = pmcstat_string_compute_hash(s);
+ len = strlen(s);
+
+ LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next)
+ if (ps->ps_len == len && ps->ps_hash == hash &&
+ strcmp(ps->ps_string, s) == 0)
+ return (ps);
+ return (NULL);
+}
+
+int
+pmcstat_string_lookup_hash(pmcstat_interned_string s)
+{
+ const struct pmcstat_string *ps;
+
+ ps = (const struct pmcstat_string *) s;
+ return (ps->ps_hash);
+}
+
+/*
+ * Destroy the string table, free'ing up space.
+ */
+
+void
+pmcstat_string_shutdown(void)
+{
+ int i;
+ struct pmcstat_string *ps, *pstmp;
+
+ for (i = 0; i < PMCSTAT_NHASH; i++)
+ LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next,
+ pstmp) {
+ LIST_REMOVE(ps, ps_next);
+ free(ps->ps_string);
+ free(ps);
+ }
+}
+
+/*
+ * Initialize the string interning facility.
+ */
+
+void
+pmcstat_string_initialize(void)
+{
+ int i;
+
+ for (i = 0; i < PMCSTAT_NHASH; i++)
+ LIST_INIT(&pmcstat_string_hash[i]);
+}
diff --git a/lib/libpmcstat/libpmcstat_symbol.c b/lib/libpmcstat/libpmcstat_symbol.c
new file mode 100644
index 000000000000..155582f85f2f
--- /dev/null
+++ b/lib/libpmcstat/libpmcstat_symbol.c
@@ -0,0 +1,148 @@
+/*-
+ * Copyright (c) 2003-2008 Joseph Koshy
+ * Copyright (c) 2017 Ruslan Bukin <br@bsdpad.com>
+ * All rights reserved.
+ *
+ * This software was developed by BAE Systems, the University of Cambridge
+ * Computer Laboratory, and Memorial University under DARPA/AFRL contract
+ * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
+ * (TC) research program.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/cpuset.h>
+#include <sys/event.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/module.h>
+#include <sys/pmc.h>
+
+#include <assert.h>
+#include <err.h>
+#include <pmc.h>
+#include <pmclog.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "libpmcstat.h"
+
+struct pmcstat_symbol *
+pmcstat_symbol_search_by_name(struct pmcstat_process *pp,
+ const char *pi_name, const char *name, uintptr_t *addr_start,
+ uintptr_t *addr_end)
+{
+ struct pmcstat_symbol *sym;
+ struct pmcstat_image *image;
+ struct pmcstat_pcmap *pcm;
+ const char *name1;
+ const char *name2;
+ bool found;
+ size_t i;
+
+ found = 0;
+
+ if (pp == NULL)
+ return (NULL);
+
+ TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next) {
+ image = pcm->ppm_image;
+ if (image->pi_name == NULL)
+ continue;
+ name1 = pmcstat_string_unintern(image->pi_name);
+ if (strcmp(name1, pi_name) == 0) {
+ found = 1;
+ break;
+ }
+ }
+
+ if (!found || image->pi_symbols == NULL)
+ return (NULL);
+
+ found = 0;
+
+ for (i = 0; i < image->pi_symcount; i++) {
+ sym = &image->pi_symbols[i];
+ name2 = pmcstat_string_unintern(sym->ps_name);
+ if (strcmp(name2, name) == 0) {
+ found = 1;
+ break;
+ }
+ }
+
+ if (!found)
+ return (NULL);
+
+ *addr_start = (image->pi_vaddr - image->pi_start +
+ pcm->ppm_lowpc + sym->ps_start);
+ *addr_end = (image->pi_vaddr - image->pi_start +
+ pcm->ppm_lowpc + sym->ps_end);
+
+ return (sym);
+}
+
+/*
+ * Helper function.
+ */
+
+int
+pmcstat_symbol_compare(const void *a, const void *b)
+{
+ const struct pmcstat_symbol *sym1, *sym2;
+
+ sym1 = (const struct pmcstat_symbol *) a;
+ sym2 = (const struct pmcstat_symbol *) b;
+
+ if (sym1->ps_end <= sym2->ps_start)
+ return (-1);
+ if (sym1->ps_start >= sym2->ps_end)
+ return (1);
+ return (0);
+}
+
+/*
+ * Map an address to a symbol in an image.
+ */
+
+struct pmcstat_symbol *
+pmcstat_symbol_search(struct pmcstat_image *image, uintfptr_t addr)
+{
+ struct pmcstat_symbol sym;
+
+ if (image->pi_symbols == NULL)
+ return (NULL);
+
+ sym.ps_name = NULL;
+ sym.ps_start = addr;
+ sym.ps_end = addr + 1;
+
+ return (bsearch((void *) &sym, image->pi_symbols,
+ image->pi_symcount, sizeof(struct pmcstat_symbol),
+ pmcstat_symbol_compare));
+}