2005-06-30 19:01:26 +00:00
|
|
|
/*-
|
2007-12-07 08:26:21 +00:00
|
|
|
* Copyright (c) 2005-2007, Joseph Koshy
|
|
|
|
* Copyright (c) 2007 The FreeBSD Foundation
|
2005-06-30 19:01:26 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2007-12-07 08:26:21 +00:00
|
|
|
* Portions of this software were developed by A. Joseph Koshy under
|
|
|
|
* sponsorship from the FreeBSD Foundation and Google, Inc.
|
|
|
|
*
|
2005-06-30 19:01:26 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2006-03-26 12:20:54 +00:00
|
|
|
* Transform a hwpmc(4) log into human readable form, and into
|
|
|
|
* gprof(1) compatible profiles.
|
2005-06-30 19:01:26 +00:00
|
|
|
*/
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/endian.h>
|
2011-08-07 18:37:05 +00:00
|
|
|
#include <sys/cpuset.h>
|
2005-06-30 19:01:26 +00:00
|
|
|
#include <sys/gmon.h>
|
|
|
|
#include <sys/imgact_aout.h>
|
|
|
|
#include <sys/imgact_elf.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/pmc.h>
|
|
|
|
#include <sys/queue.h>
|
2006-04-02 12:52:16 +00:00
|
|
|
#include <sys/socket.h>
|
2005-06-30 19:01:26 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
2010-02-11 22:51:44 +00:00
|
|
|
#include <curses.h>
|
2005-06-30 19:01:26 +00:00
|
|
|
#include <err.h>
|
2006-04-02 12:52:16 +00:00
|
|
|
#include <errno.h>
|
2005-06-30 19:01:26 +00:00
|
|
|
#include <fcntl.h>
|
2007-12-07 08:26:21 +00:00
|
|
|
#include <gelf.h>
|
2005-06-30 19:01:26 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <limits.h>
|
2006-04-02 12:52:16 +00:00
|
|
|
#include <netdb.h>
|
2005-06-30 19:01:26 +00:00
|
|
|
#include <pmc.h>
|
|
|
|
#include <pmclog.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "pmcstat.h"
|
2010-02-11 22:51:44 +00:00
|
|
|
#include "pmcstat_log.h"
|
|
|
|
#include "pmcstat_top.h"
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
#define PMCSTAT_ALLOCATE 1
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
/*
|
2006-03-26 12:20:54 +00:00
|
|
|
* PUBLIC INTERFACES
|
|
|
|
*
|
|
|
|
* pmcstat_initialize_logging() initialize this module, called first
|
|
|
|
* pmcstat_shutdown_logging() orderly shutdown, called last
|
|
|
|
* pmcstat_open_log() open an eventlog for processing
|
|
|
|
* pmcstat_process_log() print/convert an event log
|
2010-02-11 22:51:44 +00:00
|
|
|
* pmcstat_display_log() top mode display for the log
|
2006-03-26 12:20:54 +00:00
|
|
|
* pmcstat_close_log() finish processing an event log
|
|
|
|
*
|
2007-12-07 08:26:21 +00:00
|
|
|
* IMPLEMENTATION NOTES
|
2006-03-26 12:20:54 +00:00
|
|
|
*
|
2007-12-07 08:26:21 +00:00
|
|
|
* We correlate each 'callchain' or 'sample' entry seen in the event
|
|
|
|
* log back to an executable object in the system. Executable objects
|
|
|
|
* include:
|
2006-03-26 12:20:54 +00:00
|
|
|
* - program executables,
|
|
|
|
* - shared libraries loaded by the runtime loader,
|
|
|
|
* - dlopen()'ed objects loaded by the program,
|
|
|
|
* - the runtime loader itself,
|
|
|
|
* - the kernel and kernel modules.
|
|
|
|
*
|
|
|
|
* Each process that we know about is treated as a set of regions that
|
|
|
|
* map to executable objects. Processes are described by
|
|
|
|
* 'pmcstat_process' structures. Executable objects are tracked by
|
|
|
|
* 'pmcstat_image' structures. The kernel and kernel modules are
|
|
|
|
* common to all processes (they reside at the same virtual addresses
|
|
|
|
* for all processes). Individual processes can have their text
|
|
|
|
* segments and shared libraries loaded at process-specific locations.
|
|
|
|
*
|
|
|
|
* A given executable object can be in use by multiple processes
|
|
|
|
* (e.g., libc.so) and loaded at a different address in each.
|
|
|
|
* pmcstat_pcmap structures track per-image mappings.
|
|
|
|
*
|
|
|
|
* The sample log could have samples from multiple PMCs; we
|
|
|
|
* generate one 'gmon.out' profile per PMC.
|
2007-12-07 08:26:21 +00:00
|
|
|
*
|
|
|
|
* IMPLEMENTATION OF GMON OUTPUT
|
|
|
|
*
|
|
|
|
* Each executable object gets one 'gmon.out' profile, per PMC in
|
|
|
|
* use. Creation of 'gmon.out' profiles is done lazily. The
|
|
|
|
* 'gmon.out' profiles generated for a given sampling PMC are
|
|
|
|
* aggregates of all the samples for that particular executable
|
|
|
|
* object.
|
|
|
|
*
|
|
|
|
* IMPLEMENTATION OF SYSTEM-WIDE CALLGRAPH OUTPUT
|
|
|
|
*
|
|
|
|
* Each active pmcid has its own callgraph structure, described by a
|
|
|
|
* 'struct pmcstat_callgraph'. Given a process id and a list of pc
|
|
|
|
* values, we map each pc value to a tuple (image, symbol), where
|
|
|
|
* 'image' denotes an executable object and 'symbol' is the closest
|
|
|
|
* symbol that precedes the pc value. Each pc value in the list is
|
|
|
|
* also given a 'rank' that reflects its depth in the call stack.
|
2005-06-30 19:01:26 +00:00
|
|
|
*/
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_pmcs pmcstat_pmcs = LIST_HEAD_INITIALIZER(pmcstat_pmcs);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
|
|
|
* All image descriptors are kept in a hash table.
|
|
|
|
*/
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_image_hash_list pmcstat_image_hash[PMCSTAT_NHASH];
|
2005-06-30 19:01:26 +00:00
|
|
|
|
|
|
|
/*
|
2010-02-11 22:51:44 +00:00
|
|
|
* All process descriptors are kept in a hash table.
|
2005-06-30 19:01:26 +00:00
|
|
|
*/
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_process_hash_list pmcstat_process_hash[PMCSTAT_NHASH];
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_stats pmcstat_stats; /* statistics */
|
2012-10-19 14:49:42 +00:00
|
|
|
static int ps_samples_period; /* samples count between top refresh. */
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_process *pmcstat_kernproc; /* kernel 'process' */
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
#include "pmcpl_gprof.h"
|
|
|
|
#include "pmcpl_callgraph.h"
|
|
|
|
#include "pmcpl_annotate.h"
|
2014-02-24 02:43:58 +00:00
|
|
|
#include "pmcpl_annotate_cg.h"
|
2010-02-11 22:51:44 +00:00
|
|
|
#include "pmcpl_calltree.h"
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2012-10-19 14:49:42 +00:00
|
|
|
static struct pmc_plugins {
|
2010-02-11 22:51:44 +00:00
|
|
|
const char *pl_name; /* name */
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/* configure */
|
|
|
|
int (*pl_configure)(char *opt);
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/* init and shutdown */
|
|
|
|
int (*pl_init)(void);
|
|
|
|
void (*pl_shutdown)(FILE *mf);
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/* sample processing */
|
|
|
|
void (*pl_process)(struct pmcstat_process *pp,
|
|
|
|
struct pmcstat_pmcrecord *pmcr, uint32_t nsamples,
|
|
|
|
uintfptr_t *cc, int usermode, uint32_t cpu);
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/* image */
|
|
|
|
void (*pl_initimage)(struct pmcstat_image *pi);
|
|
|
|
void (*pl_shutdownimage)(struct pmcstat_image *pi);
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/* pmc */
|
|
|
|
void (*pl_newpmc)(pmcstat_interned_string ps,
|
|
|
|
struct pmcstat_pmcrecord *pr);
|
|
|
|
|
|
|
|
/* top display */
|
|
|
|
void (*pl_topdisplay)(void);
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/* top keypress */
|
|
|
|
int (*pl_topkeypress)(int c, WINDOW *w);
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
} plugins[] = {
|
|
|
|
{
|
|
|
|
.pl_name = "none",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.pl_name = "callgraph",
|
|
|
|
.pl_init = pmcpl_cg_init,
|
|
|
|
.pl_shutdown = pmcpl_cg_shutdown,
|
|
|
|
.pl_process = pmcpl_cg_process,
|
|
|
|
.pl_topkeypress = pmcpl_cg_topkeypress,
|
|
|
|
.pl_topdisplay = pmcpl_cg_topdisplay
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.pl_name = "gprof",
|
|
|
|
.pl_shutdown = pmcpl_gmon_shutdown,
|
|
|
|
.pl_process = pmcpl_gmon_process,
|
|
|
|
.pl_initimage = pmcpl_gmon_initimage,
|
|
|
|
.pl_shutdownimage = pmcpl_gmon_shutdownimage,
|
|
|
|
.pl_newpmc = pmcpl_gmon_newpmc
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.pl_name = "annotate",
|
|
|
|
.pl_process = pmcpl_annotate_process
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.pl_name = "calltree",
|
|
|
|
.pl_configure = pmcpl_ct_configure,
|
|
|
|
.pl_init = pmcpl_ct_init,
|
|
|
|
.pl_shutdown = pmcpl_ct_shutdown,
|
|
|
|
.pl_process = pmcpl_ct_process,
|
|
|
|
.pl_topkeypress = pmcpl_ct_topkeypress,
|
|
|
|
.pl_topdisplay = pmcpl_ct_topdisplay
|
|
|
|
},
|
2014-02-24 02:43:58 +00:00
|
|
|
{
|
|
|
|
.pl_name = "annotate_cg",
|
|
|
|
.pl_process = pmcpl_annotate_cg_process
|
|
|
|
},
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
{
|
|
|
|
.pl_name = NULL
|
|
|
|
}
|
|
|
|
};
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2012-10-19 14:49:42 +00:00
|
|
|
static int pmcstat_mergepmc;
|
2006-03-26 12:20:54 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
int pmcstat_pmcinfilter = 0; /* PMC filter for top mode. */
|
|
|
|
float pmcstat_threshold = 0.5; /* Cost filter for top mode. */
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
/*
|
|
|
|
* Prototypes
|
|
|
|
*/
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
static struct pmcstat_image *pmcstat_image_from_path(pmcstat_interned_string
|
|
|
|
_path, int _iskernelmodule);
|
2010-02-11 22:51:44 +00:00
|
|
|
static void pmcstat_image_get_aout_params(struct pmcstat_image *_image);
|
|
|
|
static void pmcstat_image_get_elf_params(struct pmcstat_image *_image);
|
2005-06-30 19:01:26 +00:00
|
|
|
static void pmcstat_image_link(struct pmcstat_process *_pp,
|
2006-03-26 12:20:54 +00:00
|
|
|
struct pmcstat_image *_i, uintfptr_t _lpc);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
static void pmcstat_pmcid_add(pmc_id_t _pmcid,
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_interned_string _name);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
static void pmcstat_process_aout_exec(struct pmcstat_process *_pp,
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_image *_image, uintfptr_t _entryaddr);
|
2006-03-26 12:20:54 +00:00
|
|
|
static void pmcstat_process_elf_exec(struct pmcstat_process *_pp,
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_image *_image, uintfptr_t _entryaddr);
|
2005-09-14 21:15:50 +00:00
|
|
|
static void pmcstat_process_exec(struct pmcstat_process *_pp,
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_interned_string _path, uintfptr_t _entryaddr);
|
2006-03-26 12:20:54 +00:00
|
|
|
static struct pmcstat_process *pmcstat_process_lookup(pid_t _pid,
|
|
|
|
int _allocate);
|
2005-06-30 19:01:26 +00:00
|
|
|
static int pmcstat_string_compute_hash(const char *_string);
|
2006-03-26 12:20:54 +00:00
|
|
|
static void pmcstat_string_initialize(void);
|
|
|
|
static int pmcstat_string_lookup_hash(pmcstat_interned_string _is);
|
|
|
|
static void pmcstat_string_shutdown(void);
|
2010-08-03 09:23:53 +00:00
|
|
|
static void pmcstat_stats_reset(int _reset_global);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
|
|
|
* A simple implementation of interned strings. Each interned string
|
|
|
|
* is assigned a unique address, so that subsequent string compares
|
2011-12-30 10:58:14 +00:00
|
|
|
* can be done by a simple pointer comparison instead of using
|
2006-03-26 12:20:54 +00:00
|
|
|
* 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(,pmcstat_string) pmcstat_string_hash[PMCSTAT_NHASH];
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/*
|
|
|
|
* PMC count.
|
|
|
|
*/
|
|
|
|
int pmcstat_npmcs;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PMC Top mode pause state.
|
|
|
|
*/
|
2012-10-19 14:49:42 +00:00
|
|
|
static int pmcstat_pause;
|
2010-02-11 22:51:44 +00:00
|
|
|
|
2010-04-02 13:34:28 +00:00
|
|
|
static void
|
2010-08-03 09:23:53 +00:00
|
|
|
pmcstat_stats_reset(int reset_global)
|
2010-04-02 13:34:28 +00:00
|
|
|
{
|
|
|
|
struct pmcstat_pmcrecord *pr;
|
|
|
|
|
|
|
|
/* Flush PMCs stats. */
|
|
|
|
LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) {
|
|
|
|
pr->pr_samples = 0;
|
|
|
|
pr->pr_dubious_frames = 0;
|
|
|
|
}
|
2010-08-03 09:23:53 +00:00
|
|
|
ps_samples_period = 0;
|
2010-04-02 13:34:28 +00:00
|
|
|
|
|
|
|
/* Flush global stats. */
|
2010-08-03 09:23:53 +00:00
|
|
|
if (reset_global)
|
|
|
|
bzero(&pmcstat_stats, sizeof(struct pmcstat_stats));
|
2010-04-02 13:34:28 +00:00
|
|
|
}
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
|
|
|
* Compute a 'hash' value for a string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
pmcstat_string_compute_hash(const char *s)
|
|
|
|
{
|
2014-05-16 03:05:53 +00:00
|
|
|
unsigned hash;
|
2006-03-26 12:20:54 +00:00
|
|
|
|
2014-05-16 03:05:53 +00:00
|
|
|
for (hash = 2166136261; *s; s++)
|
|
|
|
hash = (hash ^ *s) * 16777619;
|
2006-03-26 12:20:54 +00:00
|
|
|
|
|
|
|
return (hash & PMCSTAT_HASH_MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Intern a copy of string 's', and return a pointer to the
|
|
|
|
* interned structure.
|
|
|
|
*/
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_interned_string
|
2006-03-26 12:20:54 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
const char *
|
2006-03-26 12:20:54 +00:00
|
|
|
pmcstat_string_unintern(pmcstat_interned_string str)
|
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
s = ((const struct pmcstat_string *) str)->ps_string;
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_interned_string
|
2006-03-26 12:20:54 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pmcstat_string_lookup_hash(pmcstat_interned_string s)
|
|
|
|
{
|
|
|
|
const struct pmcstat_string *ps;
|
|
|
|
|
|
|
|
ps = (const struct pmcstat_string *) s;
|
|
|
|
return (ps->ps_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the string interning facility.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
pmcstat_string_initialize(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < PMCSTAT_NHASH; i++)
|
|
|
|
LIST_INIT(&pmcstat_string_hash[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy the string table, free'ing up space.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
static void
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_image_get_aout_params(struct pmcstat_image *image)
|
2006-03-26 12:20:54 +00:00
|
|
|
{
|
|
|
|
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)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_SOFTWARE,
|
|
|
|
"ERROR: a.out kernel modules are unsupported \"%s\"", path);
|
2006-03-26 12:20:54 +00:00
|
|
|
|
|
|
|
(void) snprintf(buffer, sizeof(buffer), "%s%s",
|
2010-02-11 22:51:44 +00:00
|
|
|
args.pa_fsroot, path);
|
2006-03-26 12:20:54 +00:00
|
|
|
|
|
|
|
if ((fd = open(buffer, O_RDONLY, 0)) < 0 ||
|
|
|
|
(nbytes = read(fd, &ex, sizeof(ex))) < 0) {
|
2012-03-28 16:23:40 +00:00
|
|
|
if (args.pa_verbosity >= 2)
|
|
|
|
warn("WARNING: Cannot determine type of \"%s\"",
|
|
|
|
path);
|
2006-03-26 12:20:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
/*
|
|
|
|
* Helper function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static 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.
|
|
|
|
*/
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_symbol *
|
2007-12-07 08:26:21 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the list of symbols in the given section to the list associated
|
|
|
|
* with the object.
|
|
|
|
*/
|
|
|
|
static 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 = realloc(image->pi_symbols,
|
|
|
|
sizeof(*symptr) * (image->pi_symcount + nfuncsyms));
|
|
|
|
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;
|
2010-05-06 20:54:19 +00:00
|
|
|
if (sym.st_shndx == STN_UNDEF)
|
|
|
|
continue;
|
2007-12-07 08:26:21 +00:00
|
|
|
|
|
|
|
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;
|
2012-06-06 08:58:31 +00:00
|
|
|
#ifdef __arm__
|
|
|
|
/* Remove spurious ARM function name. */
|
|
|
|
if (fnname[0] == '$' &&
|
|
|
|
(fnname[1] == 'a' || fnname[1] == 't' ||
|
|
|
|
fnname[1] == 'd') &&
|
|
|
|
fnname[2] == '\0')
|
|
|
|
continue;
|
|
|
|
#endif
|
2007-12-07 08:26:21 +00:00
|
|
|
|
|
|
|
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;
|
2012-06-05 20:08:59 +00:00
|
|
|
if (image->pi_symcount == 0)
|
|
|
|
return;
|
2007-12-07 08:26:21 +00:00
|
|
|
|
|
|
|
assert(newsyms <= nfuncsyms);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return space to the system if there were duplicates.
|
|
|
|
*/
|
|
|
|
if (newsyms < nfuncsyms)
|
|
|
|
image->pi_symbols = realloc(image->pi_symbols,
|
|
|
|
sizeof(*symptr) * image->pi_symcount);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_image_get_elf_params(struct pmcstat_image *image)
|
2005-06-30 19:01:26 +00:00
|
|
|
{
|
2007-12-07 08:26:21 +00:00
|
|
|
int fd;
|
|
|
|
size_t i, nph, nsh;
|
|
|
|
const char *path, *elfbase;
|
2010-02-11 22:51:44 +00:00
|
|
|
char *p, *endp;
|
2005-06-30 19:01:26 +00:00
|
|
|
uintfptr_t minva, maxva;
|
2007-12-07 08:26:21 +00:00
|
|
|
Elf *e;
|
|
|
|
Elf_Scn *scn;
|
|
|
|
GElf_Ehdr eh;
|
|
|
|
GElf_Phdr ph;
|
|
|
|
GElf_Shdr sh;
|
2006-03-26 12:20:54 +00:00
|
|
|
enum pmcstat_image_type image_type;
|
2010-08-03 13:46:25 +00:00
|
|
|
char buffer[PATH_MAX];
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2005-09-14 21:15:50 +00:00
|
|
|
assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN);
|
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
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;
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
path = pmcstat_string_unintern(image->pi_execpath);
|
2006-03-26 12:20:54 +00:00
|
|
|
assert(path != NULL);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
|
|
|
* 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",
|
2010-02-11 22:51:44 +00:00
|
|
|
args.pa_fsroot, args.pa_kernel, path);
|
2006-03-26 12:20:54 +00:00
|
|
|
else
|
|
|
|
(void) snprintf(buffer, sizeof(buffer), "%s%s",
|
2010-02-11 22:51:44 +00:00
|
|
|
args.pa_fsroot, path);
|
2006-03-26 12:20:54 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
e = NULL;
|
2006-03-26 12:20:54 +00:00
|
|
|
if ((fd = open(buffer, O_RDONLY, 0)) < 0 ||
|
2007-12-07 08:26:21 +00:00
|
|
|
(e = elf_begin(fd, ELF_C_READ, NULL)) == NULL ||
|
|
|
|
(elf_kind(e) != ELF_K_ELF)) {
|
2012-03-28 16:23:40 +00:00
|
|
|
if (args.pa_verbosity >= 2)
|
|
|
|
warnx("WARNING: Cannot determine the type of \"%s\".",
|
|
|
|
buffer);
|
2007-12-07 08:26:21 +00:00
|
|
|
goto done;
|
2006-03-26 12:20:54 +00:00
|
|
|
}
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
if (gelf_getehdr(e, &eh) != &eh) {
|
2011-11-15 06:44:07 +00:00
|
|
|
warnx(
|
|
|
|
"WARNING: Cannot retrieve the ELF Header for \"%s\": %s.",
|
|
|
|
buffer, elf_errmsg(-1));
|
2007-12-07 08:26:21 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
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;
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
2007-12-07 08:26:21 +00:00
|
|
|
* Determine the virtual address where an executable would be
|
|
|
|
* loaded. Additionally, for dynamically linked executables,
|
|
|
|
* save the pathname to the runtime linker.
|
2006-03-26 12:20:54 +00:00
|
|
|
*/
|
2007-12-07 08:26:21 +00:00
|
|
|
if (eh.e_type == ET_EXEC) {
|
|
|
|
if (elf_getphnum(e, &nph) == 0) {
|
2011-11-15 06:44:07 +00:00
|
|
|
warnx(
|
|
|
|
"WARNING: Could not determine the number of program headers in \"%s\": %s.",
|
|
|
|
buffer,
|
2007-12-07 08:26:21 +00:00
|
|
|
elf_errmsg(-1));
|
|
|
|
goto done;
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
2007-12-07 08:26:21 +00:00
|
|
|
for (i = 0; i < eh.e_phnum; i++) {
|
|
|
|
if (gelf_getphdr(e, i, &ph) != &ph) {
|
2011-11-15 06:44:07 +00:00
|
|
|
warnx(
|
|
|
|
"WARNING: Retrieval of PHDR entry #%ju in \"%s\" failed: %s.",
|
|
|
|
(uintmax_t) i, buffer, elf_errmsg(-1));
|
2007-12-07 08:26:21 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
switch (ph.p_type) {
|
|
|
|
case PT_DYNAMIC:
|
|
|
|
image->pi_isdynamic = 1;
|
|
|
|
break;
|
|
|
|
case PT_INTERP:
|
|
|
|
if ((elfbase = elf_rawfile(e, NULL)) == NULL) {
|
2011-11-15 06:44:07 +00:00
|
|
|
warnx(
|
|
|
|
"WARNING: Cannot retrieve the interpreter for \"%s\": %s.",
|
2007-12-07 08:26:21 +00:00
|
|
|
buffer, elf_errmsg(-1));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
image->pi_dynlinkerpath =
|
2010-08-03 13:46:25 +00:00
|
|
|
pmcstat_string_intern(elfbase +
|
|
|
|
ph.p_offset);
|
2007-12-07 08:26:21 +00:00
|
|
|
break;
|
|
|
|
case PT_LOAD:
|
2015-05-13 18:52:18 +00:00
|
|
|
if ((ph.p_flags & PF_X) != 0 &&
|
|
|
|
(ph.p_offset & (-ph.p_align)) == 0)
|
2012-03-22 17:44:36 +00:00
|
|
|
image->pi_vaddr = ph.p_vaddr & (-ph.p_align);
|
2007-12-07 08:26:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-10-18 05:13:27 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
/*
|
|
|
|
* Get the min and max VA associated with this ELF object.
|
|
|
|
*/
|
|
|
|
if (elf_getshnum(e, &nsh) == 0) {
|
2011-11-15 06:44:07 +00:00
|
|
|
warnx(
|
|
|
|
"WARNING: Could not determine the number of sections for \"%s\": %s.",
|
|
|
|
buffer, elf_errmsg(-1));
|
2007-12-07 08:26:21 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2005-10-18 05:13:27 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
for (i = 0; i < nsh; i++) {
|
|
|
|
if ((scn = elf_getscn(e, i)) == NULL ||
|
|
|
|
gelf_getshdr(scn, &sh) != &sh) {
|
2011-11-15 06:44:07 +00:00
|
|
|
warnx(
|
|
|
|
"WARNING: Could not retrieve section header #%ju in \"%s\": %s.",
|
|
|
|
(uintmax_t) i, buffer, elf_errmsg(-1));
|
2007-12-07 08:26:21 +00:00
|
|
|
goto done;
|
2005-10-18 05:13:27 +00:00
|
|
|
}
|
2007-12-07 08:26:21 +00:00
|
|
|
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);
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
|
2005-10-18 05:13:27 +00:00
|
|
|
image->pi_start = minva;
|
2006-03-26 12:20:54 +00:00
|
|
|
image->pi_end = maxva;
|
|
|
|
image->pi_type = image_type;
|
2007-12-07 08:26:21 +00:00
|
|
|
image->pi_fullpath = pmcstat_string_intern(buffer);
|
2005-10-18 05:13:27 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/* Build display name
|
|
|
|
*/
|
|
|
|
endp = buffer;
|
|
|
|
for (p = buffer; *p; p++)
|
|
|
|
if (*p == '/')
|
|
|
|
endp = p+1;
|
|
|
|
image->pi_name = pmcstat_string_intern(endp);
|
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
done:
|
|
|
|
(void) elf_end(e);
|
|
|
|
if (fd >= 0)
|
|
|
|
(void) close(fd);
|
2006-03-26 12:20:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
|
|
|
* Given an image descriptor, determine whether it is an ELF, or AOUT.
|
|
|
|
* If no handler claims the image, set its type to 'INDETERMINABLE'.
|
|
|
|
*/
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
void
|
|
|
|
pmcstat_image_determine_type(struct pmcstat_image *image)
|
2006-03-26 12:20:54 +00:00
|
|
|
{
|
|
|
|
assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN);
|
|
|
|
|
|
|
|
/* Try each kind of handler in turn */
|
|
|
|
if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_image_get_elf_params(image);
|
2006-03-26 12:20:54 +00:00
|
|
|
if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_image_get_aout_params(image);
|
2006-03-26 12:20:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-09-14 21:15:50 +00:00
|
|
|
* 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.
|
2006-03-26 12:20:54 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2005-06-30 19:01:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static struct pmcstat_image *
|
2006-03-26 12:20:54 +00:00
|
|
|
pmcstat_image_from_path(pmcstat_interned_string internedpath,
|
|
|
|
int iskernelmodule)
|
2005-06-30 19:01:26 +00:00
|
|
|
{
|
2010-02-11 22:51:44 +00:00
|
|
|
int hash;
|
2005-06-30 19:01:26 +00:00
|
|
|
struct pmcstat_image *pi;
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
hash = pmcstat_string_lookup_hash(internedpath);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/* First, look for an existing entry. */
|
2005-06-30 19:01:26 +00:00
|
|
|
LIST_FOREACH(pi, &pmcstat_image_hash[hash], pi_next)
|
2006-03-26 12:20:54 +00:00
|
|
|
if (pi->pi_execpath == internedpath &&
|
2007-12-07 08:26:21 +00:00
|
|
|
pi->pi_iskernelmodule == iskernelmodule)
|
2006-03-26 12:20:54 +00:00
|
|
|
return (pi);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
|
|
|
/*
|
2007-12-07 08:26:21 +00:00
|
|
|
* Allocate a new entry and place it at the head of the hash
|
|
|
|
* and LRU lists.
|
2005-06-30 19:01:26 +00:00
|
|
|
*/
|
|
|
|
pi = malloc(sizeof(*pi));
|
|
|
|
if (pi == NULL)
|
2006-03-26 12:20:54 +00:00
|
|
|
return (NULL);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
|
|
|
pi->pi_type = PMCSTAT_IMAGE_UNKNOWN;
|
2006-03-26 12:20:54 +00:00
|
|
|
pi->pi_execpath = internedpath;
|
2005-06-30 19:01:26 +00:00
|
|
|
pi->pi_start = ~0;
|
|
|
|
pi->pi_end = 0;
|
2007-12-07 08:26:21 +00:00
|
|
|
pi->pi_entry = 0;
|
|
|
|
pi->pi_vaddr = 0;
|
|
|
|
pi->pi_isdynamic = 0;
|
2006-03-26 12:20:54 +00:00
|
|
|
pi->pi_iskernelmodule = iskernelmodule;
|
2007-12-07 08:26:21 +00:00
|
|
|
pi->pi_dynlinkerpath = NULL;
|
|
|
|
pi->pi_symbols = NULL;
|
|
|
|
pi->pi_symcount = 0;
|
2010-02-11 22:51:44 +00:00
|
|
|
pi->pi_addr2line = NULL;
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
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);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&pmcstat_image_hash[hash], pi, pi_next);
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
return (pi);
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
|
|
|
* Record the fact that PC values from 'start' to 'end' come from
|
|
|
|
* image 'image'.
|
|
|
|
*/
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
static 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);
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-03-26 12:20:54 +00:00
|
|
|
* Unmap images in the range [start..end) associated with process
|
|
|
|
* 'pp'.
|
2005-06-30 19:01:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2006-03-26 12:20:54 +00:00
|
|
|
pmcstat_image_unmap(struct pmcstat_process *pp, uintfptr_t start,
|
|
|
|
uintfptr_t end)
|
2005-06-30 19:01:26 +00:00
|
|
|
{
|
2006-03-26 12:20:54 +00:00
|
|
|
struct pmcstat_pcmap *pcm, *pcmtmp, *pcmnew;
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
assert(pp != NULL);
|
|
|
|
assert(start < end);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2007-12-07 08:26:21 +00:00
|
|
|
if (pcm->ppm_lowpc >= end)
|
2006-03-26 12:20:54 +00:00
|
|
|
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)
|
2011-11-15 06:44:07 +00:00
|
|
|
err(EX_OSERR,
|
|
|
|
"ERROR: Cannot split a map entry");
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
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;
|
2007-12-07 08:26:21 +00:00
|
|
|
} 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;
|
2006-03-26 12:20:54 +00:00
|
|
|
else
|
|
|
|
assert(0);
|
|
|
|
}
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/*
|
|
|
|
* Resolve file name and line number for the given address.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pmcstat_image_addr2line(struct pmcstat_image *image, uintfptr_t addr,
|
|
|
|
char *sourcefile, size_t sourcefile_len, unsigned *sourceline,
|
|
|
|
char *funcname, size_t funcname_len)
|
|
|
|
{
|
|
|
|
static int addr2line_warn = 0;
|
|
|
|
|
|
|
|
char *sep, cmdline[PATH_MAX], imagepath[PATH_MAX];
|
2015-10-08 09:46:35 +00:00
|
|
|
unsigned l;
|
2010-02-11 22:51:44 +00:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (image->pi_addr2line == NULL) {
|
2015-10-08 09:46:35 +00:00
|
|
|
/* Try default debug file location. */
|
|
|
|
snprintf(imagepath, sizeof(imagepath),
|
|
|
|
"/usr/lib/debug/%s%s.debug",
|
2010-08-03 09:23:53 +00:00
|
|
|
args.pa_fsroot,
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_string_unintern(image->pi_fullpath));
|
|
|
|
fd = open(imagepath, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2015-10-08 09:46:35 +00:00
|
|
|
/* Old kernel symbol path. */
|
|
|
|
snprintf(imagepath, sizeof(imagepath), "%s%s.symbols",
|
2010-08-03 09:23:53 +00:00
|
|
|
args.pa_fsroot,
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_string_unintern(image->pi_fullpath));
|
2015-10-08 09:46:35 +00:00
|
|
|
fd = open(imagepath, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
snprintf(imagepath, sizeof(imagepath), "%s%s",
|
|
|
|
args.pa_fsroot,
|
|
|
|
pmcstat_string_unintern(
|
|
|
|
image->pi_fullpath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fd >= 0)
|
2010-02-11 22:51:44 +00:00
|
|
|
close(fd);
|
2012-03-28 16:23:40 +00:00
|
|
|
/*
|
|
|
|
* New addr2line support recursive inline function with -i
|
|
|
|
* but the format does not add a marker when no more entries
|
|
|
|
* are available.
|
|
|
|
*/
|
2010-02-11 22:51:44 +00:00
|
|
|
snprintf(cmdline, sizeof(cmdline), "addr2line -Cfe \"%s\"",
|
|
|
|
imagepath);
|
|
|
|
image->pi_addr2line = popen(cmdline, "r+");
|
|
|
|
if (image->pi_addr2line == NULL) {
|
|
|
|
if (!addr2line_warn) {
|
|
|
|
addr2line_warn = 1;
|
2011-11-15 06:44:07 +00:00
|
|
|
warnx(
|
|
|
|
"WARNING: addr2line is needed for source code information."
|
|
|
|
);
|
2010-02-11 22:51:44 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (feof(image->pi_addr2line) || ferror(image->pi_addr2line)) {
|
|
|
|
warnx("WARNING: addr2line pipe error");
|
|
|
|
pclose(image->pi_addr2line);
|
|
|
|
image->pi_addr2line = NULL;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(image->pi_addr2line, "%p\n", (void *)addr);
|
|
|
|
|
|
|
|
if (fgets(funcname, funcname_len, image->pi_addr2line) == NULL) {
|
|
|
|
warnx("WARNING: addr2line function name read error");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
sep = strchr(funcname, '\n');
|
|
|
|
if (sep != NULL)
|
|
|
|
*sep = '\0';
|
|
|
|
|
|
|
|
if (fgets(sourcefile, sourcefile_len, image->pi_addr2line) == NULL) {
|
|
|
|
warnx("WARNING: addr2line source file read error");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
sep = strchr(sourcefile, ':');
|
|
|
|
if (sep == NULL) {
|
|
|
|
warnx("WARNING: addr2line source line separator missing");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
*sep = '\0';
|
2012-03-28 16:23:40 +00:00
|
|
|
l = atoi(sep+1);
|
|
|
|
if (l == 0)
|
2010-02-11 22:51:44 +00:00
|
|
|
return (0);
|
2012-03-28 16:23:40 +00:00
|
|
|
*sourceline = l;
|
2010-02-11 22:51:44 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
/*
|
|
|
|
* Add a {pmcid,name} mapping.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_pmcid_add(pmc_id_t pmcid, pmcstat_interned_string ps)
|
2005-06-30 19:01:26 +00:00
|
|
|
{
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_pmcrecord *pr, *prm;
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
/* Replace an existing name for the PMC. */
|
2010-02-11 22:51:44 +00:00
|
|
|
prm = NULL;
|
2005-06-30 19:01:26 +00:00
|
|
|
LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
|
2010-02-11 22:51:44 +00:00
|
|
|
if (pr->pr_pmcid == pmcid) {
|
|
|
|
pr->pr_pmcname = ps;
|
|
|
|
return;
|
|
|
|
} else if (pr->pr_pmcname == ps)
|
|
|
|
prm = pr;
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
/*
|
2010-02-11 22:51:44 +00:00
|
|
|
* Otherwise, allocate a new descriptor and call the
|
|
|
|
* plugins hook.
|
2007-12-07 08:26:21 +00:00
|
|
|
*/
|
2005-06-30 19:01:26 +00:00
|
|
|
if ((pr = malloc(sizeof(*pr))) == NULL)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot allocate pmc record");
|
|
|
|
|
|
|
|
pr->pr_pmcid = pmcid;
|
2006-03-26 12:20:54 +00:00
|
|
|
pr->pr_pmcname = ps;
|
2010-02-11 22:51:44 +00:00
|
|
|
pr->pr_pmcin = pmcstat_npmcs++;
|
2010-04-02 13:34:28 +00:00
|
|
|
pr->pr_samples = 0;
|
|
|
|
pr->pr_dubious_frames = 0;
|
2010-02-11 22:51:44 +00:00
|
|
|
pr->pr_merge = prm == NULL ? pr : prm;
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
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);
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-03-26 12:20:54 +00:00
|
|
|
* Given a pmcid in use, find its human-readable name.
|
2005-06-30 19:01:26 +00:00
|
|
|
*/
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
const char *
|
2005-06-30 19:01:26 +00:00
|
|
|
pmcstat_pmcid_to_name(pmc_id_t pmcid)
|
|
|
|
{
|
|
|
|
struct pmcstat_pmcrecord *pr;
|
|
|
|
|
|
|
|
LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
|
|
|
|
if (pr->pr_pmcid == pmcid)
|
2006-03-26 12:20:54 +00:00
|
|
|
return (pmcstat_string_unintern(pr->pr_pmcname));
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/*
|
|
|
|
* Convert PMC index to name.
|
|
|
|
*/
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
const char *
|
|
|
|
pmcstat_pmcindex_to_name(int pmcin)
|
|
|
|
{
|
|
|
|
struct pmcstat_pmcrecord *pr;
|
|
|
|
|
|
|
|
LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
|
|
|
|
if (pr->pr_pmcin == pmcin)
|
|
|
|
return pmcstat_string_unintern(pr->pr_pmcname);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return PMC record with given index.
|
|
|
|
*/
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_pmcrecord *
|
|
|
|
pmcstat_pmcindex_to_pmcr(int pmcin)
|
|
|
|
{
|
|
|
|
struct pmcstat_pmcrecord *pr;
|
|
|
|
|
|
|
|
LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
|
|
|
|
if (pr->pr_pmcin == pmcin)
|
|
|
|
return pr;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get PMC record by id, apply merge policy.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct pmcstat_pmcrecord *
|
|
|
|
pmcstat_lookup_pmcid(pmc_id_t pmcid)
|
|
|
|
{
|
|
|
|
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;
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-03-26 12:20:54 +00:00
|
|
|
* Associate an AOUT image with a process.
|
2005-06-30 19:01:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2006-03-26 12:20:54 +00:00
|
|
|
pmcstat_process_aout_exec(struct pmcstat_process *pp,
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_image *image, uintfptr_t entryaddr)
|
2005-06-30 19:01:26 +00:00
|
|
|
{
|
2006-03-26 12:20:54 +00:00
|
|
|
(void) pp;
|
|
|
|
(void) image;
|
|
|
|
(void) entryaddr;
|
|
|
|
/* TODO Implement a.out handling */
|
|
|
|
}
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
|
|
|
* Associate an ELF image with a process.
|
|
|
|
*/
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
static void
|
|
|
|
pmcstat_process_elf_exec(struct pmcstat_process *pp,
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_image *image, uintfptr_t entryaddr)
|
2006-03-26 12:20:54 +00:00
|
|
|
{
|
|
|
|
uintmax_t libstart;
|
|
|
|
struct pmcstat_image *rtldimage;
|
|
|
|
|
|
|
|
assert(image->pi_type == PMCSTAT_IMAGE_ELF32 ||
|
|
|
|
image->pi_type == PMCSTAT_IMAGE_ELF64);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2005-09-14 21:15:50 +00:00
|
|
|
/* Create a map entry for the base executable. */
|
2006-03-26 12:20:54 +00:00
|
|
|
pmcstat_image_link(pp, image, image->pi_vaddr);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2005-09-14 21:15:50 +00:00
|
|
|
/*
|
2007-12-07 08:26:21 +00:00
|
|
|
* 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.
|
2005-09-14 21:15:50 +00:00
|
|
|
*/
|
2006-03-26 12:20:54 +00:00
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
if (image->pi_isdynamic) {
|
|
|
|
|
2005-09-14 21:15:50 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2006-03-26 12:20:54 +00:00
|
|
|
|
2005-09-14 21:15:50 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2010-02-11 22:51:44 +00:00
|
|
|
rtldimage = pmcstat_image_from_path(image->pi_dynlinkerpath, 0);
|
2006-03-26 12:20:54 +00:00
|
|
|
if (rtldimage == NULL) {
|
|
|
|
warnx("WARNING: Cannot find image for \"%s\".",
|
|
|
|
pmcstat_string_unintern(image->pi_dynlinkerpath));
|
|
|
|
pmcstat_stats.ps_exec_errors++;
|
|
|
|
return;
|
|
|
|
}
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
if (rtldimage->pi_type == PMCSTAT_IMAGE_UNKNOWN)
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_image_get_elf_params(rtldimage);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
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;
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
libstart = entryaddr - rtldimage->pi_entry;
|
|
|
|
pmcstat_image_link(pp, rtldimage, libstart);
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static 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)
|
2011-11-15 06:50:10 +00:00
|
|
|
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);
|
|
|
|
}
|
2005-06-30 19:01:26 +00:00
|
|
|
|
|
|
|
if (!allocate)
|
2006-03-26 12:20:54 +00:00
|
|
|
return (NULL);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
|
|
|
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);
|
2006-03-26 12:20:54 +00:00
|
|
|
return (pp);
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Associate an image and a process.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2006-03-26 12:20:54 +00:00
|
|
|
pmcstat_process_exec(struct pmcstat_process *pp,
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_interned_string path, uintfptr_t entryaddr)
|
2005-06-30 19:01:26 +00:00
|
|
|
{
|
|
|
|
struct pmcstat_image *image;
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
if ((image = pmcstat_image_from_path(path, 0)) == NULL) {
|
|
|
|
pmcstat_stats.ps_exec_errors++;
|
2005-06-30 19:01:26 +00:00
|
|
|
return;
|
2006-03-26 12:20:54 +00:00
|
|
|
}
|
2005-06-30 19:01:26 +00:00
|
|
|
|
|
|
|
if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_image_determine_type(image);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN);
|
|
|
|
|
|
|
|
switch (image->pi_type) {
|
|
|
|
case PMCSTAT_IMAGE_ELF32:
|
|
|
|
case PMCSTAT_IMAGE_ELF64:
|
|
|
|
pmcstat_stats.ps_exec_elf++;
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_process_elf_exec(pp, image, entryaddr);
|
2005-06-30 19:01:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PMCSTAT_IMAGE_AOUT:
|
2006-03-26 12:20:54 +00:00
|
|
|
pmcstat_stats.ps_exec_aout++;
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_process_aout_exec(pp, image, entryaddr);
|
2006-03-26 12:20:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PMCSTAT_IMAGE_INDETERMINABLE:
|
|
|
|
pmcstat_stats.ps_exec_indeterminable++;
|
2005-06-30 19:01:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-11-15 06:44:07 +00:00
|
|
|
err(EX_SOFTWARE,
|
|
|
|
"ERROR: Unsupported executable type for \"%s\"",
|
|
|
|
pmcstat_string_unintern(path));
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-14 21:15:50 +00:00
|
|
|
/*
|
|
|
|
* Find the map entry associated with process 'p' at PC value 'pc'.
|
|
|
|
*/
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_pcmap *
|
2005-09-14 21:15:50 +00:00
|
|
|
pmcstat_process_find_map(struct pmcstat_process *p, uintfptr_t pc)
|
|
|
|
{
|
|
|
|
struct pmcstat_pcmap *ppm;
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
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);
|
|
|
|
}
|
2005-09-14 21:15:50 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
return (NULL);
|
2005-09-14 21:15:50 +00:00
|
|
|
}
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
pmcstat_analyze_log(void)
|
2007-12-07 08:26:21 +00:00
|
|
|
{
|
2010-02-11 22:51:44 +00:00
|
|
|
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;
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
assert(args.pa_flags & FLAG_DO_ANALYSIS);
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
if (elf_version(EV_CURRENT) == EV_NONE)
|
2016-05-01 16:40:56 +00:00
|
|
|
err(EX_UNAVAILABLE, "Elf library initialization failed");
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
while (pmclog_read(args.pa_logparser, &ev) == 0) {
|
2005-06-30 19:01:26 +00:00
|
|
|
assert(ev.pl_state == PMCLOG_OK);
|
|
|
|
|
|
|
|
switch (ev.pl_type) {
|
2006-03-26 12:20:54 +00:00
|
|
|
case PMCLOG_TYPE_INITIALIZE:
|
|
|
|
if ((ev.pl_u.pl_i.pl_version & 0xFF000000) !=
|
2010-02-11 22:51:44 +00:00
|
|
|
PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
warnx(
|
|
|
|
"WARNING: Log version 0x%x does not match compiled version 0x%x.",
|
|
|
|
ev.pl_u.pl_i.pl_version, PMC_VERSION_MAJOR);
|
2006-03-26 12:20:54 +00:00
|
|
|
break;
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
case PMCLOG_TYPE_MAP_IN:
|
2005-06-30 19:01:26 +00:00
|
|
|
/*
|
|
|
|
* Introduce an address range mapping for a
|
2006-03-26 12:20:54 +00:00
|
|
|
* 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);
|
|
|
|
if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_image_determine_type(image);
|
2006-03-26 12:20:54 +00:00
|
|
|
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.
|
2005-06-30 19:01:26 +00:00
|
|
|
*/
|
2006-03-26 12:20:54 +00:00
|
|
|
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);
|
2005-06-30 19:01:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PMCLOG_TYPE_PCSAMPLE:
|
2007-12-07 08:26:21 +00:00
|
|
|
/*
|
|
|
|
* Note: the `PCSAMPLE' log entry is not
|
|
|
|
* generated by hpwmc(4) after version 2.
|
|
|
|
*/
|
2005-06-30 19:01:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We bring in the gmon file for the image
|
|
|
|
* currently associated with the PMC & pid
|
|
|
|
* pair and increment the appropriate entry
|
|
|
|
* bin inside this.
|
|
|
|
*/
|
2006-03-26 12:20:54 +00:00
|
|
|
pmcstat_stats.ps_samples_total++;
|
2010-08-03 09:23:53 +00:00
|
|
|
ps_samples_period++;
|
2006-03-26 12:20:54 +00:00
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
pc = ev.pl_u.pl_s.pl_pc;
|
2006-03-26 12:20:54 +00:00
|
|
|
pp = pmcstat_process_lookup(ev.pl_u.pl_s.pl_pid,
|
|
|
|
PMCSTAT_ALLOCATE);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/* Get PMC record. */
|
|
|
|
pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_s.pl_pmcid);
|
|
|
|
assert(pmcr != NULL);
|
2010-04-02 13:34:28 +00:00
|
|
|
pmcr->pr_samples++;
|
2010-02-11 22:51:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Call the plugins processing
|
|
|
|
* TODO: move pmcstat_process_find_map inside plugins
|
|
|
|
*/
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
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);
|
2005-06-30 19:01:26 +00:00
|
|
|
break;
|
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
case PMCLOG_TYPE_CALLCHAIN:
|
|
|
|
pmcstat_stats.ps_samples_total++;
|
2010-08-03 09:23:53 +00:00
|
|
|
ps_samples_period++;
|
2007-12-07 08:26:21 +00:00
|
|
|
|
|
|
|
cpuflags = ev.pl_u.pl_cc.pl_cpuflags;
|
|
|
|
cpu = PMC_CALLCHAIN_CPUFLAGS_TO_CPU(cpuflags);
|
|
|
|
|
|
|
|
/* Filter on the CPU id. */
|
2011-08-07 18:37:05 +00:00
|
|
|
if (!CPU_ISSET(cpu, &(args.pa_cpumask))) {
|
2007-12-07 08:26:21 +00:00
|
|
|
pmcstat_stats.ps_samples_skipped++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pp = pmcstat_process_lookup(ev.pl_u.pl_cc.pl_pid,
|
|
|
|
PMCSTAT_ALLOCATE);
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/* Get PMC record. */
|
|
|
|
pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_cc.pl_pmcid);
|
|
|
|
assert(pmcr != NULL);
|
2010-04-02 13:34:28 +00:00
|
|
|
pmcr->pr_samples++;
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/*
|
|
|
|
* Call the plugins processing
|
|
|
|
*/
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
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);
|
2007-12-07 08:26:21 +00:00
|
|
|
break;
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
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,
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_string_intern(ev.pl_u.pl_a.pl_evname));
|
2005-06-30 19:01:26 +00:00
|
|
|
break;
|
|
|
|
|
2012-03-28 20:58:30 +00:00
|
|
|
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));
|
|
|
|
break;
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
case PMCLOG_TYPE_PROCEXEC:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change the executable image associated with
|
|
|
|
* a process.
|
|
|
|
*/
|
2006-03-26 12:20:54 +00:00
|
|
|
pp = pmcstat_process_lookup(ev.pl_u.pl_x.pl_pid,
|
|
|
|
PMCSTAT_ALLOCATE);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2015-05-09 00:48:44 +00:00
|
|
|
/*
|
|
|
|
* Associate this process image.
|
|
|
|
*/
|
2005-06-30 19:01:26 +00:00
|
|
|
image_path = pmcstat_string_intern(
|
|
|
|
ev.pl_u.pl_x.pl_pathname);
|
2006-03-26 12:20:54 +00:00
|
|
|
assert(image_path != NULL);
|
2005-09-14 21:15:50 +00:00
|
|
|
pmcstat_process_exec(pp, image_path,
|
2010-02-11 22:51:44 +00:00
|
|
|
ev.pl_u.pl_x.pl_entryaddr);
|
2005-06-30 19:01:26 +00:00
|
|
|
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;
|
2006-03-26 12:20:54 +00:00
|
|
|
pp->pp_isactive = 0; /* mark as a zombie */
|
2005-06-30 19:01:26 +00:00
|
|
|
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:
|
|
|
|
|
|
|
|
/*
|
2006-03-26 12:20:54 +00:00
|
|
|
* 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.
|
2005-06-30 19:01:26 +00:00
|
|
|
*/
|
|
|
|
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,
|
2006-03-26 12:20:54 +00:00
|
|
|
ppm->ppm_lowpc);
|
2005-06-30 19:01:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* other types of entries are not relevant */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.pl_state == PMCLOG_EOF)
|
2006-03-26 12:20:54 +00:00
|
|
|
return (PMCSTAT_FINISHED);
|
2005-06-30 19:01:26 +00:00
|
|
|
else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
|
2006-03-26 12:20:54 +00:00
|
|
|
return (PMCSTAT_RUNNING);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2011-11-15 06:44:07 +00:00
|
|
|
err(EX_DATAERR,
|
|
|
|
"ERROR: event parsing failed (record %jd, offset 0x%jx)",
|
|
|
|
(uintmax_t) ev.pl_count + 1, ev.pl_offset);
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print log entries as text.
|
|
|
|
*/
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
static int
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_print_log(void)
|
2005-06-30 19:01:26 +00:00
|
|
|
{
|
|
|
|
struct pmclog_ev ev;
|
2007-12-07 08:26:21 +00:00
|
|
|
uint32_t npc;
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
while (pmclog_read(args.pa_logparser, &ev) == 0) {
|
2005-06-30 19:01:26 +00:00
|
|
|
assert(ev.pl_state == PMCLOG_OK);
|
|
|
|
switch (ev.pl_type) {
|
2007-12-07 08:26:21 +00:00
|
|
|
case PMCLOG_TYPE_CALLCHAIN:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("callchain",
|
2007-12-07 08:26:21 +00:00
|
|
|
"%d 0x%x %d %d %c", ev.pl_u.pl_cc.pl_pid,
|
|
|
|
ev.pl_u.pl_cc.pl_pmcid,
|
|
|
|
PMC_CALLCHAIN_CPUFLAGS_TO_CPU(ev.pl_u.pl_cc. \
|
|
|
|
pl_cpuflags), ev.pl_u.pl_cc.pl_npc,
|
|
|
|
PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(ev.pl_u.pl_cc.\
|
|
|
|
pl_cpuflags) ? 'u' : 's');
|
|
|
|
for (npc = 0; npc < ev.pl_u.pl_cc.pl_npc; npc++)
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("...", "%p",
|
2007-12-07 08:26:21 +00:00
|
|
|
(void *) ev.pl_u.pl_cc.pl_pc[npc]);
|
|
|
|
break;
|
2005-06-30 19:01:26 +00:00
|
|
|
case PMCLOG_TYPE_CLOSELOG:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("closelog",);
|
2005-06-30 19:01:26 +00:00
|
|
|
break;
|
|
|
|
case PMCLOG_TYPE_DROPNOTIFY:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("drop",);
|
2005-06-30 19:01:26 +00:00
|
|
|
break;
|
|
|
|
case PMCLOG_TYPE_INITIALIZE:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("initlog","0x%x \"%s\"",
|
2005-06-30 19:01:26 +00:00
|
|
|
ev.pl_u.pl_i.pl_version,
|
|
|
|
pmc_name_of_cputype(ev.pl_u.pl_i.pl_arch));
|
2007-12-03 10:29:49 +00:00
|
|
|
if ((ev.pl_u.pl_i.pl_version & 0xFF000000) !=
|
2010-02-11 22:51:44 +00:00
|
|
|
PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
warnx(
|
|
|
|
"WARNING: Log version 0x%x != expected version 0x%x.",
|
|
|
|
ev.pl_u.pl_i.pl_version, PMC_VERSION);
|
2005-06-30 19:01:26 +00:00
|
|
|
break;
|
2006-03-26 12:20:54 +00:00
|
|
|
case PMCLOG_TYPE_MAP_IN:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("map-in","%d %p \"%s\"",
|
2006-03-26 12:20:54 +00:00
|
|
|
ev.pl_u.pl_mi.pl_pid,
|
|
|
|
(void *) ev.pl_u.pl_mi.pl_start,
|
|
|
|
ev.pl_u.pl_mi.pl_pathname);
|
|
|
|
break;
|
|
|
|
case PMCLOG_TYPE_MAP_OUT:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("map-out","%d %p %p",
|
2006-03-26 12:20:54 +00:00
|
|
|
ev.pl_u.pl_mo.pl_pid,
|
|
|
|
(void *) ev.pl_u.pl_mo.pl_start,
|
|
|
|
(void *) ev.pl_u.pl_mo.pl_end);
|
2005-06-30 19:01:26 +00:00
|
|
|
break;
|
|
|
|
case PMCLOG_TYPE_PCSAMPLE:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("sample","0x%x %d %p %c",
|
2005-06-30 19:01:26 +00:00
|
|
|
ev.pl_u.pl_s.pl_pmcid,
|
|
|
|
ev.pl_u.pl_s.pl_pid,
|
|
|
|
(void *) ev.pl_u.pl_s.pl_pc,
|
|
|
|
ev.pl_u.pl_s.pl_usermode ? 'u' : 's');
|
|
|
|
break;
|
|
|
|
case PMCLOG_TYPE_PMCALLOCATE:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("allocate","0x%x \"%s\" 0x%x",
|
2005-06-30 19:01:26 +00:00
|
|
|
ev.pl_u.pl_a.pl_pmcid,
|
|
|
|
ev.pl_u.pl_a.pl_evname,
|
|
|
|
ev.pl_u.pl_a.pl_flags);
|
|
|
|
break;
|
2012-03-28 20:58:30 +00:00
|
|
|
case PMCLOG_TYPE_PMCALLOCATEDYN:
|
|
|
|
PMCSTAT_PRINT_ENTRY("allocatedyn","0x%x \"%s\" 0x%x",
|
|
|
|
ev.pl_u.pl_ad.pl_pmcid,
|
|
|
|
ev.pl_u.pl_ad.pl_evname,
|
|
|
|
ev.pl_u.pl_ad.pl_flags);
|
|
|
|
break;
|
2005-06-30 19:01:26 +00:00
|
|
|
case PMCLOG_TYPE_PMCATTACH:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("attach","0x%x %d \"%s\"",
|
2005-06-30 19:01:26 +00:00
|
|
|
ev.pl_u.pl_t.pl_pmcid,
|
|
|
|
ev.pl_u.pl_t.pl_pid,
|
|
|
|
ev.pl_u.pl_t.pl_pathname);
|
|
|
|
break;
|
|
|
|
case PMCLOG_TYPE_PMCDETACH:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("detach","0x%x %d",
|
2005-06-30 19:01:26 +00:00
|
|
|
ev.pl_u.pl_d.pl_pmcid,
|
|
|
|
ev.pl_u.pl_d.pl_pid);
|
|
|
|
break;
|
|
|
|
case PMCLOG_TYPE_PROCCSW:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("cswval","0x%x %d %jd",
|
2005-06-30 19:01:26 +00:00
|
|
|
ev.pl_u.pl_c.pl_pmcid,
|
|
|
|
ev.pl_u.pl_c.pl_pid,
|
|
|
|
ev.pl_u.pl_c.pl_value);
|
|
|
|
break;
|
|
|
|
case PMCLOG_TYPE_PROCEXEC:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("exec","0x%x %d %p \"%s\"",
|
2005-06-30 19:01:26 +00:00
|
|
|
ev.pl_u.pl_x.pl_pmcid,
|
|
|
|
ev.pl_u.pl_x.pl_pid,
|
|
|
|
(void *) ev.pl_u.pl_x.pl_entryaddr,
|
|
|
|
ev.pl_u.pl_x.pl_pathname);
|
|
|
|
break;
|
|
|
|
case PMCLOG_TYPE_PROCEXIT:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("exitval","0x%x %d %jd",
|
2005-06-30 19:01:26 +00:00
|
|
|
ev.pl_u.pl_e.pl_pmcid,
|
|
|
|
ev.pl_u.pl_e.pl_pid,
|
|
|
|
ev.pl_u.pl_e.pl_value);
|
|
|
|
break;
|
|
|
|
case PMCLOG_TYPE_PROCFORK:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("fork","%d %d",
|
2005-06-30 19:01:26 +00:00
|
|
|
ev.pl_u.pl_f.pl_oldpid,
|
|
|
|
ev.pl_u.pl_f.pl_newpid);
|
|
|
|
break;
|
|
|
|
case PMCLOG_TYPE_USERDATA:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("userdata","0x%x",
|
2005-06-30 19:01:26 +00:00
|
|
|
ev.pl_u.pl_u.pl_userdata);
|
|
|
|
break;
|
|
|
|
case PMCLOG_TYPE_SYSEXIT:
|
2010-02-11 22:51:44 +00:00
|
|
|
PMCSTAT_PRINT_ENTRY("exit","%d",
|
2005-06-30 19:01:26 +00:00
|
|
|
ev.pl_u.pl_se.pl_pid);
|
|
|
|
break;
|
|
|
|
default:
|
2010-02-11 22:51:44 +00:00
|
|
|
fprintf(args.pa_printfile, "unknown event (type %d).\n",
|
2005-06-30 19:01:26 +00:00
|
|
|
ev.pl_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.pl_state == PMCLOG_EOF)
|
2006-03-26 12:20:54 +00:00
|
|
|
return (PMCSTAT_FINISHED);
|
2005-06-30 19:01:26 +00:00
|
|
|
else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
|
2006-03-26 12:20:54 +00:00
|
|
|
return (PMCSTAT_RUNNING);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_DATAERR,
|
|
|
|
"ERROR: event parsing failed (record %jd, offset 0x%jx).",
|
2005-06-30 19:01:26 +00:00
|
|
|
(uintmax_t) ev.pl_count + 1, ev.pl_offset);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
|
|
|
* Public Interfaces.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close a logfile, after first flushing all in-module queued data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_close_log(void)
|
2006-03-26 12:20:54 +00:00
|
|
|
{
|
2010-08-03 09:23:53 +00:00
|
|
|
/* 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) {
|
2011-10-18 15:25:43 +00:00
|
|
|
if (pmc_close_logfile() < 0)
|
2010-08-03 09:23:53 +00:00
|
|
|
err(EX_OSERR, "ERROR: logging failed");
|
|
|
|
}
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
return (args.pa_flags & FLAG_HAS_PIPE ? PMCSTAT_EXITING :
|
2006-03-26 12:20:54 +00:00
|
|
|
PMCSTAT_FINISHED);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2010-08-03 09:23:53 +00:00
|
|
|
int error, fd, cfd;
|
2006-04-02 12:52:16 +00:00
|
|
|
size_t hlen;
|
|
|
|
const char *p, *errstr;
|
|
|
|
struct addrinfo hints, *res, *res0;
|
|
|
|
char hostname[MAXHOSTNAMELEN];
|
|
|
|
|
|
|
|
errstr = NULL;
|
|
|
|
fd = -1;
|
2006-03-26 12:20:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If 'path' is "-" then open one of stdin or stdout depending
|
2006-04-02 12:52:16 +00:00
|
|
|
* on the value of 'mode'.
|
2007-12-07 08:26:21 +00:00
|
|
|
*
|
2006-04-02 12:52:16 +00:00
|
|
|
* 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.
|
2006-03-26 12:20:54 +00:00
|
|
|
*/
|
|
|
|
if (path[0] == '-' && path[1] == '\0')
|
|
|
|
fd = (mode == PMCSTAT_OPEN_FOR_READ) ? 0 : 1;
|
2010-08-03 09:23:53 +00:00
|
|
|
else if (path[0] != '/' &&
|
2006-04-02 12:52:16 +00:00
|
|
|
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;
|
|
|
|
}
|
2010-08-03 09:23:53 +00:00
|
|
|
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);
|
2006-04-02 12:52:16 +00:00
|
|
|
(void) close(fd);
|
2010-08-03 09:23:53 +00:00
|
|
|
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;
|
|
|
|
}
|
2006-04-02 12:52:16 +00:00
|
|
|
}
|
|
|
|
errstr = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
|
|
|
|
} else if ((fd = open(path, mode == PMCSTAT_OPEN_FOR_READ ?
|
2006-03-26 12:20:54 +00:00
|
|
|
O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC),
|
2006-04-02 12:52:16 +00:00
|
|
|
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
|
|
|
|
errstr = strerror(errno);
|
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
done:
|
2006-04-02 12:52:16 +00:00
|
|
|
if (errstr)
|
|
|
|
errx(EX_OSERR, "ERROR: Cannot open \"%s\" for %s: %s.", path,
|
|
|
|
(mode == PMCSTAT_OPEN_FOR_READ ? "reading" : "writing"),
|
|
|
|
errstr);
|
2006-03-26 12:20:54 +00:00
|
|
|
|
|
|
|
return (fd);
|
|
|
|
}
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
/*
|
|
|
|
* Process a log file in offline analysis mode.
|
|
|
|
*/
|
|
|
|
|
2005-07-09 17:08:46 +00:00
|
|
|
int
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_process_log(void)
|
2005-06-30 19:01:26 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
2007-12-07 08:26:21 +00:00
|
|
|
* If analysis has not been asked for, just print the log to
|
|
|
|
* the current output file.
|
2005-06-30 19:01:26 +00:00
|
|
|
*/
|
2010-02-11 22:51:44 +00:00
|
|
|
if (args.pa_flags & FLAG_DO_PRINT)
|
|
|
|
return (pmcstat_print_log());
|
2005-06-30 19:01:26 +00:00
|
|
|
else
|
2010-02-11 22:51:44 +00:00
|
|
|
return (pmcstat_analyze_log());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Refresh top display.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
pmcstat_refresh_top(void)
|
|
|
|
{
|
2010-04-02 13:34:28 +00:00
|
|
|
int v_attrs;
|
|
|
|
float v;
|
2010-02-11 22:51:44 +00:00
|
|
|
char pmcname[40];
|
2010-04-02 13:34:28 +00:00
|
|
|
struct pmcstat_pmcrecord *pmcpr;
|
2010-02-11 22:51:44 +00:00
|
|
|
|
|
|
|
/* If in pause mode do not refresh display. */
|
|
|
|
if (pmcstat_pause)
|
|
|
|
return;
|
|
|
|
|
2010-03-28 22:46:56 +00:00
|
|
|
/* Wait until PMC pop in the log. */
|
2010-04-02 13:34:28 +00:00
|
|
|
pmcpr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
|
|
|
|
if (pmcpr == NULL)
|
2010-03-28 22:46:56 +00:00
|
|
|
return;
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/* Format PMC name. */
|
|
|
|
if (pmcstat_mergepmc)
|
2010-04-02 13:34:28 +00:00
|
|
|
snprintf(pmcname, sizeof(pmcname), "[%s]",
|
|
|
|
pmcstat_string_unintern(pmcpr->pr_pmcname));
|
2010-02-11 22:51:44 +00:00
|
|
|
else
|
|
|
|
snprintf(pmcname, sizeof(pmcname), "%s.%d",
|
2010-04-02 13:34:28 +00:00
|
|
|
pmcstat_string_unintern(pmcpr->pr_pmcname),
|
|
|
|
pmcstat_pmcinfilter);
|
|
|
|
|
|
|
|
/* Format samples count. */
|
2010-08-03 09:23:53 +00:00
|
|
|
if (ps_samples_period > 0)
|
|
|
|
v = (pmcpr->pr_samples * 100.0) / ps_samples_period;
|
2010-04-02 13:34:28 +00:00
|
|
|
else
|
|
|
|
v = 0.;
|
|
|
|
v_attrs = PMCSTAT_ATTRPERCENT(v);
|
2010-02-11 22:51:44 +00:00
|
|
|
|
|
|
|
PMCSTAT_PRINTBEGIN();
|
2010-04-02 13:34:28 +00:00
|
|
|
PMCSTAT_PRINTW("PMC: %s Samples: %u ",
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcname,
|
2010-04-02 13:34:28 +00:00
|
|
|
pmcpr->pr_samples);
|
|
|
|
PMCSTAT_ATTRON(v_attrs);
|
|
|
|
PMCSTAT_PRINTW("(%.1f%%) ", v);
|
|
|
|
PMCSTAT_ATTROFF(v_attrs);
|
|
|
|
PMCSTAT_PRINTW(", %u unresolved\n\n",
|
|
|
|
pmcpr->pr_dubious_frames);
|
2010-02-11 22:51:44 +00:00
|
|
|
if (plugins[args.pa_plugin].pl_topdisplay != NULL)
|
|
|
|
plugins[args.pa_plugin].pl_topdisplay();
|
|
|
|
PMCSTAT_PRINTEND();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the next pmc index to display.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
pmcstat_changefilter(void)
|
|
|
|
{
|
|
|
|
int pmcin;
|
|
|
|
struct pmcstat_pmcrecord *pmcr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the next merge target.
|
|
|
|
*/
|
|
|
|
if (pmcstat_mergepmc) {
|
|
|
|
pmcin = pmcstat_pmcinfilter;
|
|
|
|
|
|
|
|
do {
|
|
|
|
pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
|
2010-08-03 09:23:53 +00:00
|
|
|
if (pmcr == NULL || pmcr == pmcr->pr_merge)
|
2010-02-11 22:51:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
pmcstat_pmcinfilter++;
|
|
|
|
if (pmcstat_pmcinfilter >= pmcstat_npmcs)
|
|
|
|
pmcstat_pmcinfilter = 0;
|
|
|
|
|
|
|
|
} while (pmcstat_pmcinfilter != pmcin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Top mode keypress.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
pmcstat_keypress_log(void)
|
|
|
|
{
|
|
|
|
int c, ret = 0;
|
|
|
|
WINDOW *w;
|
|
|
|
|
|
|
|
w = newwin(1, 0, 1, 0);
|
|
|
|
c = wgetch(w);
|
|
|
|
wprintw(w, "Key: %c => ", c);
|
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
|
|
|
wprintw(w, "enter mode 'd' or 'a' => ");
|
|
|
|
c = wgetch(w);
|
|
|
|
if (c == 'd') {
|
|
|
|
args.pa_topmode = PMCSTAT_TOP_DELTA;
|
|
|
|
wprintw(w, "switching to delta mode");
|
|
|
|
} else {
|
|
|
|
args.pa_topmode = PMCSTAT_TOP_ACCUM;
|
|
|
|
wprintw(w, "switching to accumulation mode");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
pmcstat_mergepmc = !pmcstat_mergepmc;
|
|
|
|
/*
|
|
|
|
* Changing merge state require data reset.
|
|
|
|
*/
|
|
|
|
if (plugins[args.pa_plugin].pl_shutdown != NULL)
|
|
|
|
plugins[args.pa_plugin].pl_shutdown(NULL);
|
2010-08-03 09:23:53 +00:00
|
|
|
pmcstat_stats_reset(0);
|
2010-02-11 22:51:44 +00:00
|
|
|
if (plugins[args.pa_plugin].pl_init != NULL)
|
|
|
|
plugins[args.pa_plugin].pl_init();
|
|
|
|
|
|
|
|
/* Update filter to be on a merge target. */
|
|
|
|
pmcstat_changefilter();
|
|
|
|
wprintw(w, "merge PMC %s", pmcstat_mergepmc ? "on" : "off");
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
/* Close current plugin. */
|
|
|
|
if (plugins[args.pa_plugin].pl_shutdown != NULL)
|
|
|
|
plugins[args.pa_plugin].pl_shutdown(NULL);
|
|
|
|
|
|
|
|
/* Find next top display available. */
|
|
|
|
do {
|
|
|
|
args.pa_plugin++;
|
|
|
|
if (plugins[args.pa_plugin].pl_name == NULL)
|
|
|
|
args.pa_plugin = 0;
|
|
|
|
} while (plugins[args.pa_plugin].pl_topdisplay == NULL);
|
|
|
|
|
|
|
|
/* Open new plugin. */
|
2010-08-03 09:23:53 +00:00
|
|
|
pmcstat_stats_reset(0);
|
2010-02-11 22:51:44 +00:00
|
|
|
if (plugins[args.pa_plugin].pl_init != NULL)
|
|
|
|
plugins[args.pa_plugin].pl_init();
|
|
|
|
wprintw(w, "switching to plugin %s",
|
|
|
|
plugins[args.pa_plugin].pl_name);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
pmcstat_pmcinfilter++;
|
|
|
|
if (pmcstat_pmcinfilter >= pmcstat_npmcs)
|
|
|
|
pmcstat_pmcinfilter = 0;
|
|
|
|
pmcstat_changefilter();
|
|
|
|
wprintw(w, "switching to PMC %s.%d",
|
|
|
|
pmcstat_pmcindex_to_name(pmcstat_pmcinfilter),
|
|
|
|
pmcstat_pmcinfilter);
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
pmcstat_pause = !pmcstat_pause;
|
|
|
|
if (pmcstat_pause)
|
|
|
|
wprintw(w, "pause => press space again to continue");
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
wprintw(w, "exiting...");
|
|
|
|
ret = 1;
|
2010-06-05 22:57:53 +00:00
|
|
|
break;
|
2010-02-11 22:51:44 +00:00
|
|
|
default:
|
|
|
|
if (plugins[args.pa_plugin].pl_topkeypress != NULL)
|
|
|
|
if (plugins[args.pa_plugin].pl_topkeypress(c, w))
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
wrefresh(w);
|
|
|
|
delwin(w);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Top mode display.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
pmcstat_display_log(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
pmcstat_refresh_top();
|
|
|
|
|
|
|
|
/* Reset everythings if delta mode. */
|
|
|
|
if (args.pa_topmode == PMCSTAT_TOP_DELTA) {
|
|
|
|
if (plugins[args.pa_plugin].pl_shutdown != NULL)
|
|
|
|
plugins[args.pa_plugin].pl_shutdown(NULL);
|
2010-08-03 09:23:53 +00:00
|
|
|
pmcstat_stats_reset(0);
|
2010-02-11 22:51:44 +00:00
|
|
|
if (plugins[args.pa_plugin].pl_init != NULL)
|
|
|
|
plugins[args.pa_plugin].pl_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Configure a plugins.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
pmcstat_pluginconfigure_log(char *opt)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (strncmp(opt, "threshold=", 10) == 0) {
|
|
|
|
pmcstat_threshold = atof(opt+10);
|
|
|
|
} else {
|
|
|
|
if (plugins[args.pa_plugin].pl_configure != NULL) {
|
|
|
|
if (!plugins[args.pa_plugin].pl_configure(opt))
|
|
|
|
err(EX_USAGE,
|
|
|
|
"ERROR: unknown option <%s>.", opt);
|
|
|
|
}
|
|
|
|
}
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
|
|
|
* Initialize module.
|
|
|
|
*/
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
void
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_initialize_logging(void)
|
2005-06-30 19:01:26 +00:00
|
|
|
{
|
2005-09-14 21:15:50 +00:00
|
|
|
int i;
|
2006-03-26 12:20:54 +00:00
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
/* use a convenient format for 'ldd' output */
|
2005-12-25 05:11:29 +00:00
|
|
|
if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1","%o \"%p\" %x\n",1) != 0)
|
2006-03-26 12:20:54 +00:00
|
|
|
err(EX_OSERR, "ERROR: Cannot setenv");
|
2005-06-30 19:01:26 +00:00
|
|
|
|
|
|
|
/* Initialize hash tables */
|
2006-03-26 12:20:54 +00:00
|
|
|
pmcstat_string_initialize();
|
2005-06-30 19:01:26 +00:00
|
|
|
for (i = 0; i < PMCSTAT_NHASH; i++) {
|
|
|
|
LIST_INIT(&pmcstat_image_hash[i]);
|
|
|
|
LIST_INIT(&pmcstat_process_hash[i]);
|
|
|
|
}
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
|
|
|
* 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_kernproc = pmcstat_process_lookup((pid_t) -1,
|
|
|
|
PMCSTAT_ALLOCATE)) == NULL)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot initialize logging");
|
2010-02-11 22:51:44 +00:00
|
|
|
|
|
|
|
/* 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();
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
|
|
|
* Shutdown module.
|
|
|
|
*/
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
void
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_shutdown_logging(void)
|
2005-06-30 19:01:26 +00:00
|
|
|
{
|
|
|
|
int i;
|
2006-03-26 12:20:54 +00:00
|
|
|
FILE *mf;
|
2005-06-30 19:01:26 +00:00
|
|
|
struct pmcstat_image *pi, *pitmp;
|
|
|
|
struct pmcstat_process *pp, *pptmp;
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_pcmap *ppm, *ppmtmp;
|
2006-03-26 12:20:54 +00:00
|
|
|
|
|
|
|
/* determine where to send the map file */
|
|
|
|
mf = NULL;
|
2010-02-11 22:51:44 +00:00
|
|
|
if (args.pa_mapfilename != NULL)
|
|
|
|
mf = (strcmp(args.pa_mapfilename, "-") == 0) ?
|
|
|
|
args.pa_printfile : fopen(args.pa_mapfilename, "w");
|
2006-03-26 12:20:54 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
if (mf == NULL && args.pa_flags & FLAG_DO_GPROF &&
|
|
|
|
args.pa_verbosity >= 2)
|
|
|
|
mf = args.pa_printfile;
|
2006-03-26 12:20:54 +00:00
|
|
|
|
|
|
|
if (mf)
|
|
|
|
(void) fprintf(mf, "MAP:\n");
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
/*
|
2010-02-11 22:51:44 +00:00
|
|
|
* Shutdown the plugins
|
2007-12-07 08:26:21 +00:00
|
|
|
*/
|
2006-04-05 15:12:25 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
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);
|
2007-12-07 08:26:21 +00:00
|
|
|
|
|
|
|
for (i = 0; i < PMCSTAT_NHASH; i++) {
|
2010-02-11 22:51:44 +00:00
|
|
|
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);
|
2005-06-30 19:01:26 +00:00
|
|
|
LIST_REMOVE(pi, pi_next);
|
|
|
|
free(pi);
|
|
|
|
}
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[i], pp_next,
|
|
|
|
pptmp) {
|
2010-02-11 22:51:44 +00:00
|
|
|
TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
|
|
|
|
TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
|
|
|
|
free(ppm);
|
|
|
|
}
|
2005-06-30 19:01:26 +00:00
|
|
|
LIST_REMOVE(pp, pp_next);
|
|
|
|
free(pp);
|
|
|
|
}
|
|
|
|
}
|
2006-03-26 12:20:54 +00:00
|
|
|
|
|
|
|
pmcstat_string_shutdown();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print errors unless -q was specified. Print all statistics
|
|
|
|
* if verbosity > 1.
|
|
|
|
*/
|
2010-02-11 22:51:44 +00:00
|
|
|
#define PRINT(N,V) do { \
|
|
|
|
if (pmcstat_stats.ps_##V || args.pa_verbosity >= 2) \
|
|
|
|
(void) fprintf(args.pa_printfile, " %-40s %d\n",\
|
2006-03-26 12:20:54 +00:00
|
|
|
N, pmcstat_stats.ps_##V); \
|
|
|
|
} while (0)
|
|
|
|
|
2010-08-03 09:23:53 +00:00
|
|
|
if (args.pa_verbosity >= 1 && (args.pa_flags & FLAG_DO_ANALYSIS)) {
|
2010-02-11 22:51:44 +00:00
|
|
|
(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);
|
2010-09-03 13:54:02 +00:00
|
|
|
PRINT("#samples/unknown-function", samples_unknown_function);
|
2010-02-11 22:51:44 +00:00
|
|
|
PRINT("#callchain/dubious-frames", callchain_dubious_frames);
|
2006-03-26 12:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mf)
|
|
|
|
(void) fclose(mf);
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|