1/*-
2 * Copyright (c) 2005-2007, Joseph Koshy
3 * Copyright (c) 2007 The FreeBSD Foundation
4 * All rights reserved.
5 *
6 * Portions of this software were developed by A. Joseph Koshy under
7 * sponsorship from the FreeBSD Foundation and Google, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31/*
32 * Transform a hwpmc(4) log into human readable form, and into
33 * gprof(1) compatible profiles.
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD$");
38
39#include <sys/param.h>
40#include <sys/endian.h>
41#include <sys/cpuset.h>
42#include <sys/gmon.h>
43#include <sys/imgact_aout.h>
44#include <sys/imgact_elf.h>
45#include <sys/mman.h>
46#include <sys/pmc.h>
47#include <sys/queue.h>
48#include <sys/socket.h>
49#include <sys/stat.h>
50#include <sys/wait.h>
51
52#include <netinet/in.h>
53
54#include <assert.h>
55#include <curses.h>
56#include <err.h>
57#include <errno.h>
58#include <fcntl.h>
59#include <gelf.h>
60#include <libgen.h>
61#include <limits.h>
62#include <netdb.h>
63#include <pmc.h>
64#include <pmclog.h>
65#include <sysexits.h>
66#include <stdint.h>
67#include <stdio.h>
68#include <stdlib.h>
69#include <string.h>
70#include <unistd.h>
71
72#include "pmcstat.h"
73#include "pmcstat_log.h"
74#include "pmcstat_top.h"
75
76#define	PMCSTAT_ALLOCATE		1
77
78/*
79 * PUBLIC INTERFACES
80 *
81 * pmcstat_initialize_logging()	initialize this module, called first
82 * pmcstat_shutdown_logging()		orderly shutdown, called last
83 * pmcstat_open_log()			open an eventlog for processing
84 * pmcstat_process_log()		print/convert an event log
85 * pmcstat_display_log()		top mode display for the log
86 * pmcstat_close_log()			finish processing an event log
87 *
88 * IMPLEMENTATION NOTES
89 *
90 * We correlate each 'callchain' or 'sample' entry seen in the event
91 * log back to an executable object in the system. Executable objects
92 * include:
93 * 	- program executables,
94 *	- shared libraries loaded by the runtime loader,
95 *	- dlopen()'ed objects loaded by the program,
96 *	- the runtime loader itself,
97 *	- the kernel and kernel modules.
98 *
99 * Each process that we know about is treated as a set of regions that
100 * map to executable objects.  Processes are described by
101 * 'pmcstat_process' structures.  Executable objects are tracked by
102 * 'pmcstat_image' structures.  The kernel and kernel modules are
103 * common to all processes (they reside at the same virtual addresses
104 * for all processes).  Individual processes can have their text
105 * segments and shared libraries loaded at process-specific locations.
106 *
107 * A given executable object can be in use by multiple processes
108 * (e.g., libc.so) and loaded at a different address in each.
109 * pmcstat_pcmap structures track per-image mappings.
110 *
111 * The sample log could have samples from multiple PMCs; we
112 * generate one 'gmon.out' profile per PMC.
113 *
114 * IMPLEMENTATION OF GMON OUTPUT
115 *
116 * Each executable object gets one 'gmon.out' profile, per PMC in
117 * use.  Creation of 'gmon.out' profiles is done lazily.  The
118 * 'gmon.out' profiles generated for a given sampling PMC are
119 * aggregates of all the samples for that particular executable
120 * object.
121 *
122 * IMPLEMENTATION OF SYSTEM-WIDE CALLGRAPH OUTPUT
123 *
124 * Each active pmcid has its own callgraph structure, described by a
125 * 'struct pmcstat_callgraph'.  Given a process id and a list of pc
126 * values, we map each pc value to a tuple (image, symbol), where
127 * 'image' denotes an executable object and 'symbol' is the closest
128 * symbol that precedes the pc value.  Each pc value in the list is
129 * also given a 'rank' that reflects its depth in the call stack.
130 */
131
132struct pmcstat_pmcs pmcstat_pmcs = LIST_HEAD_INITIALIZER(pmcstat_pmcs);
133
134/*
135 * All image descriptors are kept in a hash table.
136 */
137struct pmcstat_image_hash_list pmcstat_image_hash[PMCSTAT_NHASH];
138
139/*
140 * All process descriptors are kept in a hash table.
141 */
142struct pmcstat_process_hash_list pmcstat_process_hash[PMCSTAT_NHASH];
143
144struct pmcstat_stats pmcstat_stats; /* statistics */
145int ps_samples_period; /* samples count between top refresh. */
146
147struct pmcstat_process *pmcstat_kernproc; /* kernel 'process' */
148
149#include "pmcpl_gprof.h"
150#include "pmcpl_callgraph.h"
151#include "pmcpl_annotate.h"
152#include "pmcpl_calltree.h"
153
154struct pmc_plugins  {
155	const char 	*pl_name;	/* name */
156
157	/* configure */
158	int (*pl_configure)(char *opt);
159
160	/* init and shutdown */
161	int (*pl_init)(void);
162	void (*pl_shutdown)(FILE *mf);
163
164	/* sample processing */
165	void (*pl_process)(struct pmcstat_process *pp,
166	    struct pmcstat_pmcrecord *pmcr, uint32_t nsamples,
167	    uintfptr_t *cc, int usermode, uint32_t cpu);
168
169	/* image */
170	void (*pl_initimage)(struct pmcstat_image *pi);
171	void (*pl_shutdownimage)(struct pmcstat_image *pi);
172
173	/* pmc */
174	void (*pl_newpmc)(pmcstat_interned_string ps,
175		struct pmcstat_pmcrecord *pr);
176
177	/* top display */
178	void (*pl_topdisplay)(void);
179
180	/* top keypress */
181	int (*pl_topkeypress)(int c, WINDOW *w);
182
183} plugins[] = {
184	{
185		.pl_name		= "none",
186	},
187	{
188		.pl_name		= "callgraph",
189		.pl_init		= pmcpl_cg_init,
190		.pl_shutdown		= pmcpl_cg_shutdown,
191		.pl_process		= pmcpl_cg_process,
192		.pl_topkeypress		= pmcpl_cg_topkeypress,
193		.pl_topdisplay		= pmcpl_cg_topdisplay
194	},
195	{
196		.pl_name		= "gprof",
197		.pl_shutdown		= pmcpl_gmon_shutdown,
198		.pl_process		= pmcpl_gmon_process,
199		.pl_initimage		= pmcpl_gmon_initimage,
200		.pl_shutdownimage	= pmcpl_gmon_shutdownimage,
201		.pl_newpmc		= pmcpl_gmon_newpmc
202	},
203	{
204		.pl_name		= "annotate",
205		.pl_process		= pmcpl_annotate_process
206	},
207	{
208		.pl_name		= "calltree",
209		.pl_configure		= pmcpl_ct_configure,
210		.pl_init		= pmcpl_ct_init,
211		.pl_shutdown		= pmcpl_ct_shutdown,
212		.pl_process		= pmcpl_ct_process,
213		.pl_topkeypress		= pmcpl_ct_topkeypress,
214		.pl_topdisplay		= pmcpl_ct_topdisplay
215	},
216	{
217		.pl_name		= NULL
218	}
219};
220
221int pmcstat_mergepmc;
222
223int pmcstat_pmcinfilter = 0; /* PMC filter for top mode. */
224float pmcstat_threshold = 0.5; /* Cost filter for top mode. */
225
226/*
227 * Prototypes
228 */
229
230static struct pmcstat_image *pmcstat_image_from_path(pmcstat_interned_string
231    _path, int _iskernelmodule);
232static void pmcstat_image_get_aout_params(struct pmcstat_image *_image);
233static void pmcstat_image_get_elf_params(struct pmcstat_image *_image);
234static void	pmcstat_image_link(struct pmcstat_process *_pp,
235    struct pmcstat_image *_i, uintfptr_t _lpc);
236
237static void	pmcstat_pmcid_add(pmc_id_t _pmcid,
238    pmcstat_interned_string _name);
239
240static void	pmcstat_process_aout_exec(struct pmcstat_process *_pp,
241    struct pmcstat_image *_image, uintfptr_t _entryaddr);
242static void	pmcstat_process_elf_exec(struct pmcstat_process *_pp,
243    struct pmcstat_image *_image, uintfptr_t _entryaddr);
244static void	pmcstat_process_exec(struct pmcstat_process *_pp,
245    pmcstat_interned_string _path, uintfptr_t _entryaddr);
246static struct pmcstat_process *pmcstat_process_lookup(pid_t _pid,
247    int _allocate);
248static int	pmcstat_string_compute_hash(const char *_string);
249static void pmcstat_string_initialize(void);
250static int	pmcstat_string_lookup_hash(pmcstat_interned_string _is);
251static void pmcstat_string_shutdown(void);
252static void pmcstat_stats_reset(int _reset_global);
253
254/*
255 * A simple implementation of interned strings.  Each interned string
256 * is assigned a unique address, so that subsequent string compares
257 * can be done by a simple pointer comparision instead of using
258 * strcmp().  This speeds up hash table lookups and saves memory if
259 * duplicate strings are the norm.
260 */
261struct pmcstat_string {
262	LIST_ENTRY(pmcstat_string)	ps_next;	/* hash link */
263	int		ps_len;
264	int		ps_hash;
265	char		*ps_string;
266};
267
268static LIST_HEAD(,pmcstat_string)	pmcstat_string_hash[PMCSTAT_NHASH];
269
270/*
271 * PMC count.
272 */
273int pmcstat_npmcs;
274
275/*
276 * PMC Top mode pause state.
277 */
278int pmcstat_pause;
279
280static void
281pmcstat_stats_reset(int reset_global)
282{
283	struct pmcstat_pmcrecord *pr;
284
285	/* Flush PMCs stats. */
286	LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) {
287		pr->pr_samples = 0;
288		pr->pr_dubious_frames = 0;
289	}
290	ps_samples_period = 0;
291
292	/* Flush global stats. */
293	if (reset_global)
294		bzero(&pmcstat_stats, sizeof(struct pmcstat_stats));
295}
296
297/*
298 * Compute a 'hash' value for a string.
299 */
300
301static int
302pmcstat_string_compute_hash(const char *s)
303{
304	unsigned hash;
305
306	for (hash = 2166136261; *s; s++)
307		hash = (hash ^ *s) * 16777619;
308
309	return (hash & PMCSTAT_HASH_MASK);
310}
311
312/*
313 * Intern a copy of string 's', and return a pointer to the
314 * interned structure.
315 */
316
317pmcstat_interned_string
318pmcstat_string_intern(const char *s)
319{
320	struct pmcstat_string *ps;
321	const struct pmcstat_string *cps;
322	int hash, len;
323
324	if ((cps = pmcstat_string_lookup(s)) != NULL)
325		return (cps);
326
327	hash = pmcstat_string_compute_hash(s);
328	len  = strlen(s);
329
330	if ((ps = malloc(sizeof(*ps))) == NULL)
331		err(EX_OSERR, "ERROR: Could not intern string");
332	ps->ps_len = len;
333	ps->ps_hash = hash;
334	ps->ps_string = strdup(s);
335	LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next);
336	return ((pmcstat_interned_string) ps);
337}
338
339const char *
340pmcstat_string_unintern(pmcstat_interned_string str)
341{
342	const char *s;
343
344	s = ((const struct pmcstat_string *) str)->ps_string;
345	return (s);
346}
347
348pmcstat_interned_string
349pmcstat_string_lookup(const char *s)
350{
351	struct pmcstat_string *ps;
352	int hash, len;
353
354	hash = pmcstat_string_compute_hash(s);
355	len = strlen(s);
356
357	LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next)
358	    if (ps->ps_len == len && ps->ps_hash == hash &&
359		strcmp(ps->ps_string, s) == 0)
360		    return (ps);
361	return (NULL);
362}
363
364static int
365pmcstat_string_lookup_hash(pmcstat_interned_string s)
366{
367	const struct pmcstat_string *ps;
368
369	ps = (const struct pmcstat_string *) s;
370	return (ps->ps_hash);
371}
372
373/*
374 * Initialize the string interning facility.
375 */
376
377static void
378pmcstat_string_initialize(void)
379{
380	int i;
381
382	for (i = 0; i < PMCSTAT_NHASH; i++)
383		LIST_INIT(&pmcstat_string_hash[i]);
384}
385
386/*
387 * Destroy the string table, free'ing up space.
388 */
389
390static void
391pmcstat_string_shutdown(void)
392{
393	int i;
394	struct pmcstat_string *ps, *pstmp;
395
396	for (i = 0; i < PMCSTAT_NHASH; i++)
397		LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next,
398		    pstmp) {
399			LIST_REMOVE(ps, ps_next);
400			free(ps->ps_string);
401			free(ps);
402		}
403}
404
405/*
406 * Determine whether a given executable image is an A.OUT object, and
407 * if so, fill in its parameters from the text file.
408 * Sets image->pi_type.
409 */
410
411static void
412pmcstat_image_get_aout_params(struct pmcstat_image *image)
413{
414	int fd;
415	ssize_t nbytes;
416	struct exec ex;
417	const char *path;
418	char buffer[PATH_MAX];
419
420	path = pmcstat_string_unintern(image->pi_execpath);
421	assert(path != NULL);
422
423	if (image->pi_iskernelmodule)
424		errx(EX_SOFTWARE,
425		    "ERROR: a.out kernel modules are unsupported \"%s\"", path);
426
427	(void) snprintf(buffer, sizeof(buffer), "%s%s",
428	    args.pa_fsroot, path);
429
430	if ((fd = open(buffer, O_RDONLY, 0)) < 0 ||
431	    (nbytes = read(fd, &ex, sizeof(ex))) < 0) {
432		if (args.pa_verbosity >= 2)
433			warn("WARNING: Cannot determine type of \"%s\"",
434			    path);
435		image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE;
436		if (fd != -1)
437			(void) close(fd);
438		return;
439	}
440
441	(void) close(fd);
442
443	if ((unsigned) nbytes != sizeof(ex) ||
444	    N_BADMAG(ex))
445		return;
446
447	image->pi_type = PMCSTAT_IMAGE_AOUT;
448
449	/* TODO: the rest of a.out processing */
450
451	return;
452}
453
454/*
455 * Helper function.
456 */
457
458static int
459pmcstat_symbol_compare(const void *a, const void *b)
460{
461	const struct pmcstat_symbol *sym1, *sym2;
462
463	sym1 = (const struct pmcstat_symbol *) a;
464	sym2 = (const struct pmcstat_symbol *) b;
465
466	if (sym1->ps_end <= sym2->ps_start)
467		return (-1);
468	if (sym1->ps_start >= sym2->ps_end)
469		return (1);
470	return (0);
471}
472
473/*
474 * Map an address to a symbol in an image.
475 */
476
477struct pmcstat_symbol *
478pmcstat_symbol_search(struct pmcstat_image *image, uintfptr_t addr)
479{
480	struct pmcstat_symbol sym;
481
482	if (image->pi_symbols == NULL)
483		return (NULL);
484
485	sym.ps_name  = NULL;
486	sym.ps_start = addr;
487	sym.ps_end   = addr + 1;
488
489	return (bsearch((void *) &sym, image->pi_symbols,
490		    image->pi_symcount, sizeof(struct pmcstat_symbol),
491		    pmcstat_symbol_compare));
492}
493
494/*
495 * Add the list of symbols in the given section to the list associated
496 * with the object.
497 */
498static void
499pmcstat_image_add_symbols(struct pmcstat_image *image, Elf *e,
500    Elf_Scn *scn, GElf_Shdr *sh)
501{
502	int firsttime;
503	size_t n, newsyms, nshsyms, nfuncsyms;
504	struct pmcstat_symbol *symptr;
505	char *fnname;
506	GElf_Sym sym;
507	Elf_Data *data;
508
509	if ((data = elf_getdata(scn, NULL)) == NULL)
510		return;
511
512	/*
513	 * Determine the number of functions named in this
514	 * section.
515	 */
516
517	nshsyms = sh->sh_size / sh->sh_entsize;
518	for (n = nfuncsyms = 0; n < nshsyms; n++) {
519		if (gelf_getsym(data, (int) n, &sym) != &sym)
520			return;
521		if (GELF_ST_TYPE(sym.st_info) == STT_FUNC)
522			nfuncsyms++;
523	}
524
525	if (nfuncsyms == 0)
526		return;
527
528	/*
529	 * Allocate space for the new entries.
530	 */
531	firsttime = image->pi_symbols == NULL;
532	symptr = realloc(image->pi_symbols,
533	    sizeof(*symptr) * (image->pi_symcount + nfuncsyms));
534	if (symptr == image->pi_symbols) /* realloc() failed. */
535		return;
536	image->pi_symbols = symptr;
537
538	/*
539	 * Append new symbols to the end of the current table.
540	 */
541	symptr += image->pi_symcount;
542
543	for (n = newsyms = 0; n < nshsyms; n++) {
544		if (gelf_getsym(data, (int) n, &sym) != &sym)
545			return;
546		if (GELF_ST_TYPE(sym.st_info) != STT_FUNC)
547			continue;
548		if (sym.st_shndx == STN_UNDEF)
549			continue;
550
551		if (!firsttime && pmcstat_symbol_search(image, sym.st_value))
552			continue; /* We've seen this symbol already. */
553
554		if ((fnname = elf_strptr(e, sh->sh_link, sym.st_name))
555		    == NULL)
556			continue;
557#ifdef __arm__
558		/* Remove spurious ARM function name. */
559		if (fnname[0] == '$' &&
560		    (fnname[1] == 'a' || fnname[1] == 't' ||
561		    fnname[1] == 'd') &&
562		    fnname[2] == '\0')
563			continue;
564#endif
565
566		symptr->ps_name  = pmcstat_string_intern(fnname);
567		symptr->ps_start = sym.st_value - image->pi_vaddr;
568		symptr->ps_end   = symptr->ps_start + sym.st_size;
569		symptr++;
570
571		newsyms++;
572	}
573
574	image->pi_symcount += newsyms;
575
576	assert(newsyms <= nfuncsyms);
577
578	/*
579	 * Return space to the system if there were duplicates.
580	 */
581	if (newsyms < nfuncsyms)
582		image->pi_symbols = realloc(image->pi_symbols,
583		    sizeof(*symptr) * image->pi_symcount);
584
585	/*
586	 * Keep the list of symbols sorted.
587	 */
588	qsort(image->pi_symbols, image->pi_symcount, sizeof(*symptr),
589	    pmcstat_symbol_compare);
590
591	/*
592	 * Deal with function symbols that have a size of 'zero' by
593	 * making them extend to the next higher address.  These
594	 * symbols are usually defined in assembly code.
595	 */
596	for (symptr = image->pi_symbols;
597	     symptr < image->pi_symbols + (image->pi_symcount - 1);
598	     symptr++)
599		if (symptr->ps_start == symptr->ps_end)
600			symptr->ps_end = (symptr+1)->ps_start;
601}
602
603/*
604 * Examine an ELF file to determine the size of its text segment.
605 * Sets image->pi_type if anything conclusive can be determined about
606 * this image.
607 */
608
609static void
610pmcstat_image_get_elf_params(struct pmcstat_image *image)
611{
612	int fd;
613	size_t i, nph, nsh;
614	const char *path, *elfbase;
615	char *p, *endp;
616	uintfptr_t minva, maxva;
617	Elf *e;
618	Elf_Scn *scn;
619	GElf_Ehdr eh;
620	GElf_Phdr ph;
621	GElf_Shdr sh;
622	enum pmcstat_image_type image_type;
623	char buffer[PATH_MAX];
624
625	assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN);
626
627	image->pi_start = minva = ~(uintfptr_t) 0;
628	image->pi_end = maxva = (uintfptr_t) 0;
629	image->pi_type = image_type = PMCSTAT_IMAGE_INDETERMINABLE;
630	image->pi_isdynamic = 0;
631	image->pi_dynlinkerpath = NULL;
632	image->pi_vaddr = 0;
633
634	path = pmcstat_string_unintern(image->pi_execpath);
635	assert(path != NULL);
636
637	/*
638	 * Look for kernel modules under FSROOT/KERNELPATH/NAME,
639	 * and user mode executable objects under FSROOT/PATHNAME.
640	 */
641	if (image->pi_iskernelmodule)
642		(void) snprintf(buffer, sizeof(buffer), "%s%s/%s",
643		    args.pa_fsroot, args.pa_kernel, path);
644	else
645		(void) snprintf(buffer, sizeof(buffer), "%s%s",
646		    args.pa_fsroot, path);
647
648	e = NULL;
649	if ((fd = open(buffer, O_RDONLY, 0)) < 0 ||
650	    (e = elf_begin(fd, ELF_C_READ, NULL)) == NULL ||
651	    (elf_kind(e) != ELF_K_ELF)) {
652		if (args.pa_verbosity >= 2)
653			warnx("WARNING: Cannot determine the type of \"%s\".",
654			    buffer);
655		goto done;
656	}
657
658	if (gelf_getehdr(e, &eh) != &eh) {
659		warnx(
660		    "WARNING: Cannot retrieve the ELF Header for \"%s\": %s.",
661		    buffer, elf_errmsg(-1));
662		goto done;
663	}
664
665	if (eh.e_type != ET_EXEC && eh.e_type != ET_DYN &&
666	    !(image->pi_iskernelmodule && eh.e_type == ET_REL)) {
667		warnx("WARNING: \"%s\" is of an unsupported ELF type.",
668		    buffer);
669		goto done;
670	}
671
672	image_type = eh.e_ident[EI_CLASS] == ELFCLASS32 ?
673	    PMCSTAT_IMAGE_ELF32 : PMCSTAT_IMAGE_ELF64;
674
675	/*
676	 * Determine the virtual address where an executable would be
677	 * loaded.  Additionally, for dynamically linked executables,
678	 * save the pathname to the runtime linker.
679	 */
680	if (eh.e_type == ET_EXEC) {
681		if (elf_getphnum(e, &nph) == 0) {
682			warnx(
683"WARNING: Could not determine the number of program headers in \"%s\": %s.",
684			    buffer,
685			    elf_errmsg(-1));
686			goto done;
687		}
688		for (i = 0; i < eh.e_phnum; i++) {
689			if (gelf_getphdr(e, i, &ph) != &ph) {
690				warnx(
691"WARNING: Retrieval of PHDR entry #%ju in \"%s\" failed: %s.",
692				    (uintmax_t) i, buffer, elf_errmsg(-1));
693				goto done;
694			}
695			switch (ph.p_type) {
696			case PT_DYNAMIC:
697				image->pi_isdynamic = 1;
698				break;
699			case PT_INTERP:
700				if ((elfbase = elf_rawfile(e, NULL)) == NULL) {
701					warnx(
702"WARNING: Cannot retrieve the interpreter for \"%s\": %s.",
703					    buffer, elf_errmsg(-1));
704					goto done;
705				}
706				image->pi_dynlinkerpath =
707				    pmcstat_string_intern(elfbase +
708				        ph.p_offset);
709				break;
710			case PT_LOAD:
711				if (ph.p_offset == 0)
712					image->pi_vaddr = ph.p_vaddr;
713				break;
714			}
715		}
716	}
717
718	/*
719	 * Get the min and max VA associated with this ELF object.
720	 */
721	if (elf_getshnum(e, &nsh) == 0) {
722		warnx(
723"WARNING: Could not determine the number of sections for \"%s\": %s.",
724		    buffer, elf_errmsg(-1));
725		goto done;
726	}
727
728	for (i = 0; i < nsh; i++) {
729		if ((scn = elf_getscn(e, i)) == NULL ||
730		    gelf_getshdr(scn, &sh) != &sh) {
731			warnx(
732"WARNING: Could not retrieve section header #%ju in \"%s\": %s.",
733			    (uintmax_t) i, buffer, elf_errmsg(-1));
734			goto done;
735		}
736		if (sh.sh_flags & SHF_EXECINSTR) {
737			minva = min(minva, sh.sh_addr);
738			maxva = max(maxva, sh.sh_addr + sh.sh_size);
739		}
740		if (sh.sh_type == SHT_SYMTAB || sh.sh_type == SHT_DYNSYM)
741			pmcstat_image_add_symbols(image, e, scn, &sh);
742	}
743
744	image->pi_start = minva;
745	image->pi_end   = maxva;
746	image->pi_type  = image_type;
747	image->pi_fullpath = pmcstat_string_intern(buffer);
748
749	/* Build display name
750	 */
751	endp = buffer;
752	for (p = buffer; *p; p++)
753		if (*p == '/')
754			endp = p+1;
755	image->pi_name = pmcstat_string_intern(endp);
756
757 done:
758	(void) elf_end(e);
759	if (fd >= 0)
760		(void) close(fd);
761	return;
762}
763
764/*
765 * Given an image descriptor, determine whether it is an ELF, or AOUT.
766 * If no handler claims the image, set its type to 'INDETERMINABLE'.
767 */
768
769void
770pmcstat_image_determine_type(struct pmcstat_image *image)
771{
772	assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN);
773
774	/* Try each kind of handler in turn */
775	if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
776		pmcstat_image_get_elf_params(image);
777	if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
778		pmcstat_image_get_aout_params(image);
779
780	/*
781	 * Otherwise, remember that we tried to determine
782	 * the object's type and had failed.
783	 */
784	if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
785		image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE;
786}
787
788/*
789 * Locate an image descriptor given an interned path, adding a fresh
790 * descriptor to the cache if necessary.  This function also finds a
791 * suitable name for this image's sample file.
792 *
793 * We defer filling in the file format specific parts of the image
794 * structure till the time we actually see a sample that would fall
795 * into this image.
796 */
797
798static struct pmcstat_image *
799pmcstat_image_from_path(pmcstat_interned_string internedpath,
800    int iskernelmodule)
801{
802	int hash;
803	struct pmcstat_image *pi;
804
805	hash = pmcstat_string_lookup_hash(internedpath);
806
807	/* First, look for an existing entry. */
808	LIST_FOREACH(pi, &pmcstat_image_hash[hash], pi_next)
809	    if (pi->pi_execpath == internedpath &&
810		  pi->pi_iskernelmodule == iskernelmodule)
811		    return (pi);
812
813	/*
814	 * Allocate a new entry and place it at the head of the hash
815	 * and LRU lists.
816	 */
817	pi = malloc(sizeof(*pi));
818	if (pi == NULL)
819		return (NULL);
820
821	pi->pi_type = PMCSTAT_IMAGE_UNKNOWN;
822	pi->pi_execpath = internedpath;
823	pi->pi_start = ~0;
824	pi->pi_end = 0;
825	pi->pi_entry = 0;
826	pi->pi_vaddr = 0;
827	pi->pi_isdynamic = 0;
828	pi->pi_iskernelmodule = iskernelmodule;
829	pi->pi_dynlinkerpath = NULL;
830	pi->pi_symbols = NULL;
831	pi->pi_symcount = 0;
832	pi->pi_addr2line = NULL;
833
834	if (plugins[args.pa_pplugin].pl_initimage != NULL)
835		plugins[args.pa_pplugin].pl_initimage(pi);
836	if (plugins[args.pa_plugin].pl_initimage != NULL)
837		plugins[args.pa_plugin].pl_initimage(pi);
838
839	LIST_INSERT_HEAD(&pmcstat_image_hash[hash], pi, pi_next);
840
841	return (pi);
842}
843
844/*
845 * Record the fact that PC values from 'start' to 'end' come from
846 * image 'image'.
847 */
848
849static void
850pmcstat_image_link(struct pmcstat_process *pp, struct pmcstat_image *image,
851    uintfptr_t start)
852{
853	struct pmcstat_pcmap *pcm, *pcmnew;
854	uintfptr_t offset;
855
856	assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN &&
857	    image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE);
858
859	if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL)
860		err(EX_OSERR, "ERROR: Cannot create a map entry");
861
862	/*
863	 * Adjust the map entry to only cover the text portion
864	 * of the object.
865	 */
866
867	offset = start - image->pi_vaddr;
868	pcmnew->ppm_lowpc  = image->pi_start + offset;
869	pcmnew->ppm_highpc = image->pi_end + offset;
870	pcmnew->ppm_image  = image;
871
872	assert(pcmnew->ppm_lowpc < pcmnew->ppm_highpc);
873
874	/* Overlapped mmap()'s are assumed to never occur. */
875	TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next)
876	    if (pcm->ppm_lowpc >= pcmnew->ppm_highpc)
877		    break;
878
879	if (pcm == NULL)
880		TAILQ_INSERT_TAIL(&pp->pp_map, pcmnew, ppm_next);
881	else
882		TAILQ_INSERT_BEFORE(pcm, pcmnew, ppm_next);
883}
884
885/*
886 * Unmap images in the range [start..end) associated with process
887 * 'pp'.
888 */
889
890static void
891pmcstat_image_unmap(struct pmcstat_process *pp, uintfptr_t start,
892    uintfptr_t end)
893{
894	struct pmcstat_pcmap *pcm, *pcmtmp, *pcmnew;
895
896	assert(pp != NULL);
897	assert(start < end);
898
899	/*
900	 * Cases:
901	 * - we could have the range completely in the middle of an
902	 *   existing pcmap; in this case we have to split the pcmap
903	 *   structure into two (i.e., generate a 'hole').
904	 * - we could have the range covering multiple pcmaps; these
905	 *   will have to be removed.
906	 * - we could have either 'start' or 'end' falling in the
907	 *   middle of a pcmap; in this case shorten the entry.
908	 */
909	TAILQ_FOREACH_SAFE(pcm, &pp->pp_map, ppm_next, pcmtmp) {
910		assert(pcm->ppm_lowpc < pcm->ppm_highpc);
911		if (pcm->ppm_highpc <= start)
912			continue;
913		if (pcm->ppm_lowpc >= end)
914			return;
915		if (pcm->ppm_lowpc >= start && pcm->ppm_highpc <= end) {
916			/*
917			 * The current pcmap is completely inside the
918			 * unmapped range: remove it entirely.
919			 */
920			TAILQ_REMOVE(&pp->pp_map, pcm, ppm_next);
921			free(pcm);
922		} else if (pcm->ppm_lowpc < start && pcm->ppm_highpc > end) {
923			/*
924			 * Split this pcmap into two; curtail the
925			 * current map to end at [start-1], and start
926			 * the new one at [end].
927			 */
928			if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL)
929				err(EX_OSERR,
930				    "ERROR: Cannot split a map entry");
931
932			pcmnew->ppm_image = pcm->ppm_image;
933
934			pcmnew->ppm_lowpc = end;
935			pcmnew->ppm_highpc = pcm->ppm_highpc;
936
937			pcm->ppm_highpc = start;
938
939			TAILQ_INSERT_AFTER(&pp->pp_map, pcm, pcmnew, ppm_next);
940
941			return;
942		} else if (pcm->ppm_lowpc < start && pcm->ppm_highpc <= end)
943			pcm->ppm_highpc = start;
944		else if (pcm->ppm_lowpc >= start && pcm->ppm_highpc > end)
945			pcm->ppm_lowpc = end;
946		else
947			assert(0);
948	}
949}
950
951/*
952 * Resolve file name and line number for the given address.
953 */
954int
955pmcstat_image_addr2line(struct pmcstat_image *image, uintfptr_t addr,
956    char *sourcefile, size_t sourcefile_len, unsigned *sourceline,
957    char *funcname, size_t funcname_len)
958{
959	static int addr2line_warn = 0;
960	unsigned l;
961
962	char *sep, cmdline[PATH_MAX], imagepath[PATH_MAX];
963	int fd;
964
965	if (image->pi_addr2line == NULL) {
966		snprintf(imagepath, sizeof(imagepath), "%s%s.symbols",
967		    args.pa_fsroot,
968		    pmcstat_string_unintern(image->pi_fullpath));
969		fd = open(imagepath, O_RDONLY);
970		if (fd < 0) {
971			snprintf(imagepath, sizeof(imagepath), "%s%s",
972			    args.pa_fsroot,
973			    pmcstat_string_unintern(image->pi_fullpath));
974		} else
975			close(fd);
976		/*
977		 * New addr2line support recursive inline function with -i
978		 * but the format does not add a marker when no more entries
979		 * are available.
980		 */
981		snprintf(cmdline, sizeof(cmdline), "addr2line -Cfe \"%s\"",
982		    imagepath);
983		image->pi_addr2line = popen(cmdline, "r+");
984		if (image->pi_addr2line == NULL) {
985			if (!addr2line_warn) {
986				addr2line_warn = 1;
987				warnx(
988"WARNING: addr2line is needed for source code information."
989				    );
990			}
991			return (0);
992		}
993	}
994
995	if (feof(image->pi_addr2line) || ferror(image->pi_addr2line)) {
996		warnx("WARNING: addr2line pipe error");
997		pclose(image->pi_addr2line);
998		image->pi_addr2line = NULL;
999		return (0);
1000	}
1001
1002	fprintf(image->pi_addr2line, "%p\n", (void *)addr);
1003
1004	if (fgets(funcname, funcname_len, image->pi_addr2line) == NULL) {
1005		warnx("WARNING: addr2line function name read error");
1006		return (0);
1007	}
1008	sep = strchr(funcname, '\n');
1009	if (sep != NULL)
1010		*sep = '\0';
1011
1012	if (fgets(sourcefile, sourcefile_len, image->pi_addr2line) == NULL) {
1013		warnx("WARNING: addr2line source file read error");
1014		return (0);
1015	}
1016	sep = strchr(sourcefile, ':');
1017	if (sep == NULL) {
1018		warnx("WARNING: addr2line source line separator missing");
1019		return (0);
1020	}
1021	*sep = '\0';
1022	l = atoi(sep+1);
1023	if (l == 0)
1024		return (0);
1025	*sourceline = l;
1026	return (1);
1027}
1028
1029/*
1030 * Add a {pmcid,name} mapping.
1031 */
1032
1033static void
1034pmcstat_pmcid_add(pmc_id_t pmcid, pmcstat_interned_string ps)
1035{
1036	struct pmcstat_pmcrecord *pr, *prm;
1037
1038	/* Replace an existing name for the PMC. */
1039	prm = NULL;
1040	LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
1041		if (pr->pr_pmcid == pmcid) {
1042			pr->pr_pmcname = ps;
1043			return;
1044		} else if (pr->pr_pmcname == ps)
1045			prm = pr;
1046
1047	/*
1048	 * Otherwise, allocate a new descriptor and call the
1049	 * plugins hook.
1050	 */
1051	if ((pr = malloc(sizeof(*pr))) == NULL)
1052		err(EX_OSERR, "ERROR: Cannot allocate pmc record");
1053
1054	pr->pr_pmcid = pmcid;
1055	pr->pr_pmcname = ps;
1056	pr->pr_pmcin = pmcstat_npmcs++;
1057	pr->pr_samples = 0;
1058	pr->pr_dubious_frames = 0;
1059	pr->pr_merge = prm == NULL ? pr : prm;
1060
1061	LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next);
1062
1063	if (plugins[args.pa_pplugin].pl_newpmc != NULL)
1064		plugins[args.pa_pplugin].pl_newpmc(ps, pr);
1065	if (plugins[args.pa_plugin].pl_newpmc != NULL)
1066		plugins[args.pa_plugin].pl_newpmc(ps, pr);
1067}
1068
1069/*
1070 * Given a pmcid in use, find its human-readable name.
1071 */
1072
1073const char *
1074pmcstat_pmcid_to_name(pmc_id_t pmcid)
1075{
1076	struct pmcstat_pmcrecord *pr;
1077
1078	LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
1079	    if (pr->pr_pmcid == pmcid)
1080		    return (pmcstat_string_unintern(pr->pr_pmcname));
1081
1082	return NULL;
1083}
1084
1085/*
1086 * Convert PMC index to name.
1087 */
1088
1089const char *
1090pmcstat_pmcindex_to_name(int pmcin)
1091{
1092	struct pmcstat_pmcrecord *pr;
1093
1094	LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
1095		if (pr->pr_pmcin == pmcin)
1096			return pmcstat_string_unintern(pr->pr_pmcname);
1097
1098	return NULL;
1099}
1100
1101/*
1102 * Return PMC record with given index.
1103 */
1104
1105struct pmcstat_pmcrecord *
1106pmcstat_pmcindex_to_pmcr(int pmcin)
1107{
1108	struct pmcstat_pmcrecord *pr;
1109
1110	LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
1111		if (pr->pr_pmcin == pmcin)
1112			return pr;
1113
1114	return NULL;
1115}
1116
1117/*
1118 * Get PMC record by id, apply merge policy.
1119 */
1120
1121static struct pmcstat_pmcrecord *
1122pmcstat_lookup_pmcid(pmc_id_t pmcid)
1123{
1124	struct pmcstat_pmcrecord *pr;
1125
1126	LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) {
1127		if (pr->pr_pmcid == pmcid) {
1128			if (pmcstat_mergepmc)
1129				return pr->pr_merge;
1130			return pr;
1131		}
1132	}
1133
1134	return NULL;
1135}
1136
1137/*
1138 * Associate an AOUT image with a process.
1139 */
1140
1141static void
1142pmcstat_process_aout_exec(struct pmcstat_process *pp,
1143    struct pmcstat_image *image, uintfptr_t entryaddr)
1144{
1145	(void) pp;
1146	(void) image;
1147	(void) entryaddr;
1148	/* TODO Implement a.out handling */
1149}
1150
1151/*
1152 * Associate an ELF image with a process.
1153 */
1154
1155static void
1156pmcstat_process_elf_exec(struct pmcstat_process *pp,
1157    struct pmcstat_image *image, uintfptr_t entryaddr)
1158{
1159	uintmax_t libstart;
1160	struct pmcstat_image *rtldimage;
1161
1162	assert(image->pi_type == PMCSTAT_IMAGE_ELF32 ||
1163	    image->pi_type == PMCSTAT_IMAGE_ELF64);
1164
1165	/* Create a map entry for the base executable. */
1166	pmcstat_image_link(pp, image, image->pi_vaddr);
1167
1168	/*
1169	 * For dynamically linked executables we need to determine
1170	 * where the dynamic linker was mapped to for this process,
1171	 * Subsequent executable objects that are mapped in by the
1172	 * dynamic linker will be tracked by log events of type
1173	 * PMCLOG_TYPE_MAP_IN.
1174	 */
1175
1176	if (image->pi_isdynamic) {
1177
1178		/*
1179		 * The runtime loader gets loaded just after the maximum
1180		 * possible heap address.  Like so:
1181		 *
1182		 * [  TEXT DATA BSS HEAP -->*RTLD  SHLIBS   <--STACK]
1183		 * ^					            ^
1184		 * 0				   VM_MAXUSER_ADDRESS
1185
1186		 *
1187		 * The exact address where the loader gets mapped in
1188		 * will vary according to the size of the executable
1189		 * and the limits on the size of the process'es data
1190		 * segment at the time of exec().  The entry address
1191		 * recorded at process exec time corresponds to the
1192		 * 'start' address inside the dynamic linker.  From
1193		 * this we can figure out the address where the
1194		 * runtime loader's file object had been mapped to.
1195		 */
1196		rtldimage = pmcstat_image_from_path(image->pi_dynlinkerpath, 0);
1197		if (rtldimage == NULL) {
1198			warnx("WARNING: Cannot find image for \"%s\".",
1199			    pmcstat_string_unintern(image->pi_dynlinkerpath));
1200			pmcstat_stats.ps_exec_errors++;
1201			return;
1202		}
1203
1204		if (rtldimage->pi_type == PMCSTAT_IMAGE_UNKNOWN)
1205			pmcstat_image_get_elf_params(rtldimage);
1206
1207		if (rtldimage->pi_type != PMCSTAT_IMAGE_ELF32 &&
1208		    rtldimage->pi_type != PMCSTAT_IMAGE_ELF64) {
1209			warnx("WARNING: rtld not an ELF object \"%s\".",
1210			    pmcstat_string_unintern(image->pi_dynlinkerpath));
1211			return;
1212		}
1213
1214		libstart = entryaddr - rtldimage->pi_entry;
1215		pmcstat_image_link(pp, rtldimage, libstart);
1216	}
1217}
1218
1219/*
1220 * Find the process descriptor corresponding to a PID.  If 'allocate'
1221 * is zero, we return a NULL if a pid descriptor could not be found or
1222 * a process descriptor process.  If 'allocate' is non-zero, then we
1223 * will attempt to allocate a fresh process descriptor.  Zombie
1224 * process descriptors are only removed if a fresh allocation for the
1225 * same PID is requested.
1226 */
1227
1228static struct pmcstat_process *
1229pmcstat_process_lookup(pid_t pid, int allocate)
1230{
1231	uint32_t hash;
1232	struct pmcstat_pcmap *ppm, *ppmtmp;
1233	struct pmcstat_process *pp, *pptmp;
1234
1235	hash = (uint32_t) pid & PMCSTAT_HASH_MASK;	/* simplicity wins */
1236
1237	LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[hash], pp_next, pptmp)
1238		if (pp->pp_pid == pid) {
1239			/* Found a descriptor, check and process zombies */
1240			if (allocate && pp->pp_isactive == 0) {
1241				/* remove maps */
1242				TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next,
1243				    ppmtmp) {
1244					TAILQ_REMOVE(&pp->pp_map, ppm,
1245					    ppm_next);
1246					free(ppm);
1247				}
1248				/* remove process entry */
1249				LIST_REMOVE(pp, pp_next);
1250				free(pp);
1251				break;
1252			}
1253			return (pp);
1254		}
1255
1256	if (!allocate)
1257		return (NULL);
1258
1259	if ((pp = malloc(sizeof(*pp))) == NULL)
1260		err(EX_OSERR, "ERROR: Cannot allocate pid descriptor");
1261
1262	pp->pp_pid = pid;
1263	pp->pp_isactive = 1;
1264
1265	TAILQ_INIT(&pp->pp_map);
1266
1267	LIST_INSERT_HEAD(&pmcstat_process_hash[hash], pp, pp_next);
1268	return (pp);
1269}
1270
1271/*
1272 * Associate an image and a process.
1273 */
1274
1275static void
1276pmcstat_process_exec(struct pmcstat_process *pp,
1277    pmcstat_interned_string path, uintfptr_t entryaddr)
1278{
1279	struct pmcstat_image *image;
1280
1281	if ((image = pmcstat_image_from_path(path, 0)) == NULL) {
1282		pmcstat_stats.ps_exec_errors++;
1283		return;
1284	}
1285
1286	if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
1287		pmcstat_image_determine_type(image);
1288
1289	assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN);
1290
1291	switch (image->pi_type) {
1292	case PMCSTAT_IMAGE_ELF32:
1293	case PMCSTAT_IMAGE_ELF64:
1294		pmcstat_stats.ps_exec_elf++;
1295		pmcstat_process_elf_exec(pp, image, entryaddr);
1296		break;
1297
1298	case PMCSTAT_IMAGE_AOUT:
1299		pmcstat_stats.ps_exec_aout++;
1300		pmcstat_process_aout_exec(pp, image, entryaddr);
1301		break;
1302
1303	case PMCSTAT_IMAGE_INDETERMINABLE:
1304		pmcstat_stats.ps_exec_indeterminable++;
1305		break;
1306
1307	default:
1308		err(EX_SOFTWARE,
1309		    "ERROR: Unsupported executable type for \"%s\"",
1310		    pmcstat_string_unintern(path));
1311	}
1312}
1313
1314
1315/*
1316 * Find the map entry associated with process 'p' at PC value 'pc'.
1317 */
1318
1319struct pmcstat_pcmap *
1320pmcstat_process_find_map(struct pmcstat_process *p, uintfptr_t pc)
1321{
1322	struct pmcstat_pcmap *ppm;
1323
1324	TAILQ_FOREACH(ppm, &p->pp_map, ppm_next) {
1325		if (pc >= ppm->ppm_lowpc && pc < ppm->ppm_highpc)
1326			return (ppm);
1327		if (pc < ppm->ppm_lowpc)
1328			return (NULL);
1329	}
1330
1331	return (NULL);
1332}
1333
1334/*
1335 * Convert a hwpmc(4) log to profile information.  A system-wide
1336 * callgraph is generated if FLAG_DO_CALLGRAPHS is set.  gmon.out
1337 * files usable by gprof(1) are created if FLAG_DO_GPROF is set.
1338 */
1339static int
1340pmcstat_analyze_log(void)
1341{
1342	uint32_t cpu, cpuflags;
1343	uintfptr_t pc;
1344	pid_t pid;
1345	struct pmcstat_image *image;
1346	struct pmcstat_process *pp, *ppnew;
1347	struct pmcstat_pcmap *ppm, *ppmtmp;
1348	struct pmclog_ev ev;
1349	struct pmcstat_pmcrecord *pmcr;
1350	pmcstat_interned_string image_path;
1351
1352	assert(args.pa_flags & FLAG_DO_ANALYSIS);
1353
1354	if (elf_version(EV_CURRENT) == EV_NONE)
1355		err(EX_UNAVAILABLE, "Elf library intialization failed");
1356
1357	while (pmclog_read(args.pa_logparser, &ev) == 0) {
1358		assert(ev.pl_state == PMCLOG_OK);
1359
1360		switch (ev.pl_type) {
1361		case PMCLOG_TYPE_INITIALIZE:
1362			if ((ev.pl_u.pl_i.pl_version & 0xFF000000) !=
1363			    PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0)
1364				warnx(
1365"WARNING: Log version 0x%x does not match compiled version 0x%x.",
1366				    ev.pl_u.pl_i.pl_version, PMC_VERSION_MAJOR);
1367			break;
1368
1369		case PMCLOG_TYPE_MAP_IN:
1370			/*
1371			 * Introduce an address range mapping for a
1372			 * userland process or the kernel (pid == -1).
1373			 *
1374			 * We always allocate a process descriptor so
1375			 * that subsequent samples seen for this
1376			 * address range are mapped to the current
1377			 * object being mapped in.
1378			 */
1379			pid = ev.pl_u.pl_mi.pl_pid;
1380			if (pid == -1)
1381				pp = pmcstat_kernproc;
1382			else
1383				pp = pmcstat_process_lookup(pid,
1384				    PMCSTAT_ALLOCATE);
1385
1386			assert(pp != NULL);
1387
1388			image_path = pmcstat_string_intern(ev.pl_u.pl_mi.
1389			    pl_pathname);
1390			image = pmcstat_image_from_path(image_path, pid == -1);
1391			if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
1392				pmcstat_image_determine_type(image);
1393			if (image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE)
1394				pmcstat_image_link(pp, image,
1395				    ev.pl_u.pl_mi.pl_start);
1396			break;
1397
1398		case PMCLOG_TYPE_MAP_OUT:
1399			/*
1400			 * Remove an address map.
1401			 */
1402			pid = ev.pl_u.pl_mo.pl_pid;
1403			if (pid == -1)
1404				pp = pmcstat_kernproc;
1405			else
1406				pp = pmcstat_process_lookup(pid, 0);
1407
1408			if (pp == NULL)	/* unknown process */
1409				break;
1410
1411			pmcstat_image_unmap(pp, ev.pl_u.pl_mo.pl_start,
1412			    ev.pl_u.pl_mo.pl_end);
1413			break;
1414
1415		case PMCLOG_TYPE_PCSAMPLE:
1416			/*
1417			 * Note: the `PCSAMPLE' log entry is not
1418			 * generated by hpwmc(4) after version 2.
1419			 */
1420
1421			/*
1422			 * We bring in the gmon file for the image
1423			 * currently associated with the PMC & pid
1424			 * pair and increment the appropriate entry
1425			 * bin inside this.
1426			 */
1427			pmcstat_stats.ps_samples_total++;
1428			ps_samples_period++;
1429
1430			pc = ev.pl_u.pl_s.pl_pc;
1431			pp = pmcstat_process_lookup(ev.pl_u.pl_s.pl_pid,
1432			    PMCSTAT_ALLOCATE);
1433
1434			/* Get PMC record. */
1435			pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_s.pl_pmcid);
1436			assert(pmcr != NULL);
1437			pmcr->pr_samples++;
1438
1439			/*
1440			 * Call the plugins processing
1441			 * TODO: move pmcstat_process_find_map inside plugins
1442			 */
1443
1444			if (plugins[args.pa_pplugin].pl_process != NULL)
1445				plugins[args.pa_pplugin].pl_process(
1446				    pp, pmcr, 1, &pc,
1447				    pmcstat_process_find_map(pp, pc) != NULL, 0);
1448			plugins[args.pa_plugin].pl_process(
1449			    pp, pmcr, 1, &pc,
1450			    pmcstat_process_find_map(pp, pc) != NULL, 0);
1451			break;
1452
1453		case PMCLOG_TYPE_CALLCHAIN:
1454			pmcstat_stats.ps_samples_total++;
1455			ps_samples_period++;
1456
1457			cpuflags = ev.pl_u.pl_cc.pl_cpuflags;
1458			cpu = PMC_CALLCHAIN_CPUFLAGS_TO_CPU(cpuflags);
1459
1460			/* Filter on the CPU id. */
1461			if (!CPU_ISSET(cpu, &(args.pa_cpumask))) {
1462				pmcstat_stats.ps_samples_skipped++;
1463				break;
1464			}
1465
1466			pp = pmcstat_process_lookup(ev.pl_u.pl_cc.pl_pid,
1467			    PMCSTAT_ALLOCATE);
1468
1469			/* Get PMC record. */
1470			pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_cc.pl_pmcid);
1471			assert(pmcr != NULL);
1472			pmcr->pr_samples++;
1473
1474			/*
1475			 * Call the plugins processing
1476			 */
1477
1478			if (plugins[args.pa_pplugin].pl_process != NULL)
1479				plugins[args.pa_pplugin].pl_process(
1480				    pp, pmcr,
1481				    ev.pl_u.pl_cc.pl_npc,
1482				    ev.pl_u.pl_cc.pl_pc,
1483				    PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags),
1484				    cpu);
1485			plugins[args.pa_plugin].pl_process(
1486			    pp, pmcr,
1487			    ev.pl_u.pl_cc.pl_npc,
1488			    ev.pl_u.pl_cc.pl_pc,
1489			    PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags),
1490			    cpu);
1491			break;
1492
1493		case PMCLOG_TYPE_PMCALLOCATE:
1494			/*
1495			 * Record the association pmc id between this
1496			 * PMC and its name.
1497			 */
1498			pmcstat_pmcid_add(ev.pl_u.pl_a.pl_pmcid,
1499			    pmcstat_string_intern(ev.pl_u.pl_a.pl_evname));
1500			break;
1501
1502		case PMCLOG_TYPE_PMCALLOCATEDYN:
1503			/*
1504			 * Record the association pmc id between this
1505			 * PMC and its name.
1506			 */
1507			pmcstat_pmcid_add(ev.pl_u.pl_ad.pl_pmcid,
1508			    pmcstat_string_intern(ev.pl_u.pl_ad.pl_evname));
1509			break;
1510
1511		case PMCLOG_TYPE_PROCEXEC:
1512
1513			/*
1514			 * Change the executable image associated with
1515			 * a process.
1516			 */
1517			pp = pmcstat_process_lookup(ev.pl_u.pl_x.pl_pid,
1518			    PMCSTAT_ALLOCATE);
1519
1520			/* delete the current process map */
1521			TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
1522				TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
1523				free(ppm);
1524			}
1525
1526			/* associate this process  image */
1527			image_path = pmcstat_string_intern(
1528				ev.pl_u.pl_x.pl_pathname);
1529			assert(image_path != NULL);
1530			pmcstat_process_exec(pp, image_path,
1531			    ev.pl_u.pl_x.pl_entryaddr);
1532			break;
1533
1534		case PMCLOG_TYPE_PROCEXIT:
1535
1536			/*
1537			 * Due to the way the log is generated, the
1538			 * last few samples corresponding to a process
1539			 * may appear in the log after the process
1540			 * exit event is recorded.  Thus we keep the
1541			 * process' descriptor and associated data
1542			 * structures around, but mark the process as
1543			 * having exited.
1544			 */
1545			pp = pmcstat_process_lookup(ev.pl_u.pl_e.pl_pid, 0);
1546			if (pp == NULL)
1547				break;
1548			pp->pp_isactive = 0;	/* mark as a zombie */
1549			break;
1550
1551		case PMCLOG_TYPE_SYSEXIT:
1552			pp = pmcstat_process_lookup(ev.pl_u.pl_se.pl_pid, 0);
1553			if (pp == NULL)
1554				break;
1555			pp->pp_isactive = 0;	/* make a zombie */
1556			break;
1557
1558		case PMCLOG_TYPE_PROCFORK:
1559
1560			/*
1561			 * Allocate a process descriptor for the new
1562			 * (child) process.
1563			 */
1564			ppnew =
1565			    pmcstat_process_lookup(ev.pl_u.pl_f.pl_newpid,
1566				PMCSTAT_ALLOCATE);
1567
1568			/*
1569			 * If we had been tracking the parent, clone
1570			 * its address maps.
1571			 */
1572			pp = pmcstat_process_lookup(ev.pl_u.pl_f.pl_oldpid, 0);
1573			if (pp == NULL)
1574				break;
1575			TAILQ_FOREACH(ppm, &pp->pp_map, ppm_next)
1576			    pmcstat_image_link(ppnew, ppm->ppm_image,
1577				ppm->ppm_lowpc);
1578			break;
1579
1580		default:	/* other types of entries are not relevant */
1581			break;
1582		}
1583	}
1584
1585	if (ev.pl_state == PMCLOG_EOF)
1586		return (PMCSTAT_FINISHED);
1587	else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
1588		return (PMCSTAT_RUNNING);
1589
1590	err(EX_DATAERR,
1591	    "ERROR: event parsing failed (record %jd, offset 0x%jx)",
1592	    (uintmax_t) ev.pl_count + 1, ev.pl_offset);
1593}
1594
1595/*
1596 * Print log entries as text.
1597 */
1598
1599static int
1600pmcstat_print_log(void)
1601{
1602	struct pmclog_ev ev;
1603	uint32_t npc;
1604
1605	while (pmclog_read(args.pa_logparser, &ev) == 0) {
1606		assert(ev.pl_state == PMCLOG_OK);
1607		switch (ev.pl_type) {
1608		case PMCLOG_TYPE_CALLCHAIN:
1609			PMCSTAT_PRINT_ENTRY("callchain",
1610			    "%d 0x%x %d %d %c", ev.pl_u.pl_cc.pl_pid,
1611			    ev.pl_u.pl_cc.pl_pmcid,
1612			    PMC_CALLCHAIN_CPUFLAGS_TO_CPU(ev.pl_u.pl_cc. \
1613				pl_cpuflags), ev.pl_u.pl_cc.pl_npc,
1614			    PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(ev.pl_u.pl_cc.\
1615			        pl_cpuflags) ? 'u' : 's');
1616			for (npc = 0; npc < ev.pl_u.pl_cc.pl_npc; npc++)
1617				PMCSTAT_PRINT_ENTRY("...", "%p",
1618				    (void *) ev.pl_u.pl_cc.pl_pc[npc]);
1619			break;
1620		case PMCLOG_TYPE_CLOSELOG:
1621			PMCSTAT_PRINT_ENTRY("closelog",);
1622			break;
1623		case PMCLOG_TYPE_DROPNOTIFY:
1624			PMCSTAT_PRINT_ENTRY("drop",);
1625			break;
1626		case PMCLOG_TYPE_INITIALIZE:
1627			PMCSTAT_PRINT_ENTRY("initlog","0x%x \"%s\"",
1628			    ev.pl_u.pl_i.pl_version,
1629			    pmc_name_of_cputype(ev.pl_u.pl_i.pl_arch));
1630			if ((ev.pl_u.pl_i.pl_version & 0xFF000000) !=
1631			    PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0)
1632				warnx(
1633"WARNING: Log version 0x%x != expected version 0x%x.",
1634				    ev.pl_u.pl_i.pl_version, PMC_VERSION);
1635			break;
1636		case PMCLOG_TYPE_MAP_IN:
1637			PMCSTAT_PRINT_ENTRY("map-in","%d %p \"%s\"",
1638			    ev.pl_u.pl_mi.pl_pid,
1639			    (void *) ev.pl_u.pl_mi.pl_start,
1640			    ev.pl_u.pl_mi.pl_pathname);
1641			break;
1642		case PMCLOG_TYPE_MAP_OUT:
1643			PMCSTAT_PRINT_ENTRY("map-out","%d %p %p",
1644			    ev.pl_u.pl_mo.pl_pid,
1645			    (void *) ev.pl_u.pl_mo.pl_start,
1646			    (void *) ev.pl_u.pl_mo.pl_end);
1647			break;
1648		case PMCLOG_TYPE_PCSAMPLE:
1649			PMCSTAT_PRINT_ENTRY("sample","0x%x %d %p %c",
1650			    ev.pl_u.pl_s.pl_pmcid,
1651			    ev.pl_u.pl_s.pl_pid,
1652			    (void *) ev.pl_u.pl_s.pl_pc,
1653			    ev.pl_u.pl_s.pl_usermode ? 'u' : 's');
1654			break;
1655		case PMCLOG_TYPE_PMCALLOCATE:
1656			PMCSTAT_PRINT_ENTRY("allocate","0x%x \"%s\" 0x%x",
1657			    ev.pl_u.pl_a.pl_pmcid,
1658			    ev.pl_u.pl_a.pl_evname,
1659			    ev.pl_u.pl_a.pl_flags);
1660			break;
1661		case PMCLOG_TYPE_PMCALLOCATEDYN:
1662			PMCSTAT_PRINT_ENTRY("allocatedyn","0x%x \"%s\" 0x%x",
1663			    ev.pl_u.pl_ad.pl_pmcid,
1664			    ev.pl_u.pl_ad.pl_evname,
1665			    ev.pl_u.pl_ad.pl_flags);
1666			break;
1667		case PMCLOG_TYPE_PMCATTACH:
1668			PMCSTAT_PRINT_ENTRY("attach","0x%x %d \"%s\"",
1669			    ev.pl_u.pl_t.pl_pmcid,
1670			    ev.pl_u.pl_t.pl_pid,
1671			    ev.pl_u.pl_t.pl_pathname);
1672			break;
1673		case PMCLOG_TYPE_PMCDETACH:
1674			PMCSTAT_PRINT_ENTRY("detach","0x%x %d",
1675			    ev.pl_u.pl_d.pl_pmcid,
1676			    ev.pl_u.pl_d.pl_pid);
1677			break;
1678		case PMCLOG_TYPE_PROCCSW:
1679			PMCSTAT_PRINT_ENTRY("cswval","0x%x %d %jd",
1680			    ev.pl_u.pl_c.pl_pmcid,
1681			    ev.pl_u.pl_c.pl_pid,
1682			    ev.pl_u.pl_c.pl_value);
1683			break;
1684		case PMCLOG_TYPE_PROCEXEC:
1685			PMCSTAT_PRINT_ENTRY("exec","0x%x %d %p \"%s\"",
1686			    ev.pl_u.pl_x.pl_pmcid,
1687			    ev.pl_u.pl_x.pl_pid,
1688			    (void *) ev.pl_u.pl_x.pl_entryaddr,
1689			    ev.pl_u.pl_x.pl_pathname);
1690			break;
1691		case PMCLOG_TYPE_PROCEXIT:
1692			PMCSTAT_PRINT_ENTRY("exitval","0x%x %d %jd",
1693			    ev.pl_u.pl_e.pl_pmcid,
1694			    ev.pl_u.pl_e.pl_pid,
1695			    ev.pl_u.pl_e.pl_value);
1696			break;
1697		case PMCLOG_TYPE_PROCFORK:
1698			PMCSTAT_PRINT_ENTRY("fork","%d %d",
1699			    ev.pl_u.pl_f.pl_oldpid,
1700			    ev.pl_u.pl_f.pl_newpid);
1701			break;
1702		case PMCLOG_TYPE_USERDATA:
1703			PMCSTAT_PRINT_ENTRY("userdata","0x%x",
1704			    ev.pl_u.pl_u.pl_userdata);
1705			break;
1706		case PMCLOG_TYPE_SYSEXIT:
1707			PMCSTAT_PRINT_ENTRY("exit","%d",
1708			    ev.pl_u.pl_se.pl_pid);
1709			break;
1710		default:
1711			fprintf(args.pa_printfile, "unknown event (type %d).\n",
1712			    ev.pl_type);
1713		}
1714	}
1715
1716	if (ev.pl_state == PMCLOG_EOF)
1717		return (PMCSTAT_FINISHED);
1718	else if (ev.pl_state ==  PMCLOG_REQUIRE_DATA)
1719		return (PMCSTAT_RUNNING);
1720
1721	errx(EX_DATAERR,
1722	    "ERROR: event parsing failed (record %jd, offset 0x%jx).",
1723	    (uintmax_t) ev.pl_count + 1, ev.pl_offset);
1724	/*NOTREACHED*/
1725}
1726
1727/*
1728 * Public Interfaces.
1729 */
1730
1731/*
1732 * Close a logfile, after first flushing all in-module queued data.
1733 */
1734
1735int
1736pmcstat_close_log(void)
1737{
1738	/* If a local logfile is configured ask the kernel to stop
1739	 * and flush data. Kernel will close the file when data is flushed
1740	 * so keep the status to EXITING.
1741	 */
1742	if (args.pa_logfd != -1) {
1743		if (pmc_close_logfile() < 0)
1744			err(EX_OSERR, "ERROR: logging failed");
1745	}
1746
1747	return (args.pa_flags & FLAG_HAS_PIPE ? PMCSTAT_EXITING :
1748	    PMCSTAT_FINISHED);
1749}
1750
1751
1752
1753/*
1754 * Open a log file, for reading or writing.
1755 *
1756 * The function returns the fd of a successfully opened log or -1 in
1757 * case of failure.
1758 */
1759
1760int
1761pmcstat_open_log(const char *path, int mode)
1762{
1763	int error, fd, cfd;
1764	size_t hlen;
1765	const char *p, *errstr;
1766	struct addrinfo hints, *res, *res0;
1767	char hostname[MAXHOSTNAMELEN];
1768
1769	errstr = NULL;
1770	fd = -1;
1771
1772	/*
1773	 * If 'path' is "-" then open one of stdin or stdout depending
1774	 * on the value of 'mode'.
1775	 *
1776	 * If 'path' contains a ':' and does not start with a '/' or '.',
1777	 * and is being opened for writing, treat it as a "host:port"
1778	 * specification and open a network socket.
1779	 *
1780	 * Otherwise, treat 'path' as a file name and open that.
1781	 */
1782	if (path[0] == '-' && path[1] == '\0')
1783		fd = (mode == PMCSTAT_OPEN_FOR_READ) ? 0 : 1;
1784	else if (path[0] != '/' &&
1785	    path[0] != '.' && strchr(path, ':') != NULL) {
1786
1787		p = strrchr(path, ':');
1788		hlen = p - path;
1789		if (p == path || hlen >= sizeof(hostname)) {
1790			errstr = strerror(EINVAL);
1791			goto done;
1792		}
1793
1794		assert(hlen < sizeof(hostname));
1795		(void) strncpy(hostname, path, hlen);
1796		hostname[hlen] = '\0';
1797
1798		(void) memset(&hints, 0, sizeof(hints));
1799		hints.ai_family = AF_UNSPEC;
1800		hints.ai_socktype = SOCK_STREAM;
1801		if ((error = getaddrinfo(hostname, p+1, &hints, &res0)) != 0) {
1802			errstr = gai_strerror(error);
1803			goto done;
1804		}
1805
1806		fd = -1;
1807		for (res = res0; res; res = res->ai_next) {
1808			if ((fd = socket(res->ai_family, res->ai_socktype,
1809			    res->ai_protocol)) < 0) {
1810				errstr = strerror(errno);
1811				continue;
1812			}
1813			if (mode == PMCSTAT_OPEN_FOR_READ) {
1814				if (bind(fd, res->ai_addr, res->ai_addrlen) < 0) {
1815					errstr = strerror(errno);
1816					(void) close(fd);
1817					fd = -1;
1818					continue;
1819				}
1820				listen(fd, 1);
1821				cfd = accept(fd, NULL, NULL);
1822				(void) close(fd);
1823				if (cfd < 0) {
1824					errstr = strerror(errno);
1825					fd = -1;
1826					break;
1827				}
1828				fd = cfd;
1829			} else {
1830				if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
1831					errstr = strerror(errno);
1832					(void) close(fd);
1833					fd = -1;
1834					continue;
1835				}
1836			}
1837			errstr = NULL;
1838			break;
1839		}
1840		freeaddrinfo(res0);
1841
1842	} else if ((fd = open(path, mode == PMCSTAT_OPEN_FOR_READ ?
1843		    O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC),
1844		    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
1845			errstr = strerror(errno);
1846
1847  done:
1848	if (errstr)
1849		errx(EX_OSERR, "ERROR: Cannot open \"%s\" for %s: %s.", path,
1850		    (mode == PMCSTAT_OPEN_FOR_READ ? "reading" : "writing"),
1851		    errstr);
1852
1853	return (fd);
1854}
1855
1856/*
1857 * Process a log file in offline analysis mode.
1858 */
1859
1860int
1861pmcstat_process_log(void)
1862{
1863
1864	/*
1865	 * If analysis has not been asked for, just print the log to
1866	 * the current output file.
1867	 */
1868	if (args.pa_flags & FLAG_DO_PRINT)
1869		return (pmcstat_print_log());
1870	else
1871		return (pmcstat_analyze_log());
1872}
1873
1874/*
1875 * Refresh top display.
1876 */
1877
1878static void
1879pmcstat_refresh_top(void)
1880{
1881	int v_attrs;
1882	float v;
1883	char pmcname[40];
1884	struct pmcstat_pmcrecord *pmcpr;
1885
1886	/* If in pause mode do not refresh display. */
1887	if (pmcstat_pause)
1888		return;
1889
1890	/* Wait until PMC pop in the log. */
1891	pmcpr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
1892	if (pmcpr == NULL)
1893		return;
1894
1895	/* Format PMC name. */
1896	if (pmcstat_mergepmc)
1897		snprintf(pmcname, sizeof(pmcname), "[%s]",
1898		    pmcstat_string_unintern(pmcpr->pr_pmcname));
1899	else
1900		snprintf(pmcname, sizeof(pmcname), "%s.%d",
1901		    pmcstat_string_unintern(pmcpr->pr_pmcname),
1902		    pmcstat_pmcinfilter);
1903
1904	/* Format samples count. */
1905	if (ps_samples_period > 0)
1906		v = (pmcpr->pr_samples * 100.0) / ps_samples_period;
1907	else
1908		v = 0.;
1909	v_attrs = PMCSTAT_ATTRPERCENT(v);
1910
1911	PMCSTAT_PRINTBEGIN();
1912	PMCSTAT_PRINTW("PMC: %s Samples: %u ",
1913	    pmcname,
1914	    pmcpr->pr_samples);
1915	PMCSTAT_ATTRON(v_attrs);
1916	PMCSTAT_PRINTW("(%.1f%%) ", v);
1917	PMCSTAT_ATTROFF(v_attrs);
1918	PMCSTAT_PRINTW(", %u unresolved\n\n",
1919	    pmcpr->pr_dubious_frames);
1920	if (plugins[args.pa_plugin].pl_topdisplay != NULL)
1921		plugins[args.pa_plugin].pl_topdisplay();
1922	PMCSTAT_PRINTEND();
1923}
1924
1925/*
1926 * Find the next pmc index to display.
1927 */
1928
1929static void
1930pmcstat_changefilter(void)
1931{
1932	int pmcin;
1933	struct pmcstat_pmcrecord *pmcr;
1934
1935	/*
1936	 * Find the next merge target.
1937	 */
1938	if (pmcstat_mergepmc) {
1939		pmcin = pmcstat_pmcinfilter;
1940
1941		do {
1942			pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
1943			if (pmcr == NULL || pmcr == pmcr->pr_merge)
1944				break;
1945
1946			pmcstat_pmcinfilter++;
1947			if (pmcstat_pmcinfilter >= pmcstat_npmcs)
1948				pmcstat_pmcinfilter = 0;
1949
1950		} while (pmcstat_pmcinfilter != pmcin);
1951	}
1952}
1953
1954/*
1955 * Top mode keypress.
1956 */
1957
1958int
1959pmcstat_keypress_log(void)
1960{
1961	int c, ret = 0;
1962	WINDOW *w;
1963
1964	w = newwin(1, 0, 1, 0);
1965	c = wgetch(w);
1966	wprintw(w, "Key: %c => ", c);
1967	switch (c) {
1968	case 'c':
1969		wprintw(w, "enter mode 'd' or 'a' => ");
1970		c = wgetch(w);
1971		if (c == 'd') {
1972			args.pa_topmode = PMCSTAT_TOP_DELTA;
1973			wprintw(w, "switching to delta mode");
1974		} else {
1975			args.pa_topmode = PMCSTAT_TOP_ACCUM;
1976			wprintw(w, "switching to accumulation mode");
1977		}
1978		break;
1979	case 'm':
1980		pmcstat_mergepmc = !pmcstat_mergepmc;
1981		/*
1982		 * Changing merge state require data reset.
1983		 */
1984		if (plugins[args.pa_plugin].pl_shutdown != NULL)
1985			plugins[args.pa_plugin].pl_shutdown(NULL);
1986		pmcstat_stats_reset(0);
1987		if (plugins[args.pa_plugin].pl_init != NULL)
1988			plugins[args.pa_plugin].pl_init();
1989
1990		/* Update filter to be on a merge target. */
1991		pmcstat_changefilter();
1992		wprintw(w, "merge PMC %s", pmcstat_mergepmc ? "on" : "off");
1993		break;
1994	case 'n':
1995		/* Close current plugin. */
1996		if (plugins[args.pa_plugin].pl_shutdown != NULL)
1997			plugins[args.pa_plugin].pl_shutdown(NULL);
1998
1999		/* Find next top display available. */
2000		do {
2001			args.pa_plugin++;
2002			if (plugins[args.pa_plugin].pl_name == NULL)
2003				args.pa_plugin = 0;
2004		} while (plugins[args.pa_plugin].pl_topdisplay == NULL);
2005
2006		/* Open new plugin. */
2007		pmcstat_stats_reset(0);
2008		if (plugins[args.pa_plugin].pl_init != NULL)
2009			plugins[args.pa_plugin].pl_init();
2010		wprintw(w, "switching to plugin %s",
2011		    plugins[args.pa_plugin].pl_name);
2012		break;
2013	case 'p':
2014		pmcstat_pmcinfilter++;
2015		if (pmcstat_pmcinfilter >= pmcstat_npmcs)
2016			pmcstat_pmcinfilter = 0;
2017		pmcstat_changefilter();
2018		wprintw(w, "switching to PMC %s.%d",
2019		    pmcstat_pmcindex_to_name(pmcstat_pmcinfilter),
2020		    pmcstat_pmcinfilter);
2021		break;
2022	case ' ':
2023		pmcstat_pause = !pmcstat_pause;
2024		if (pmcstat_pause)
2025			wprintw(w, "pause => press space again to continue");
2026		break;
2027	case 'q':
2028		wprintw(w, "exiting...");
2029		ret = 1;
2030		break;
2031	default:
2032		if (plugins[args.pa_plugin].pl_topkeypress != NULL)
2033			if (plugins[args.pa_plugin].pl_topkeypress(c, w))
2034				ret = 1;
2035	}
2036
2037	wrefresh(w);
2038	delwin(w);
2039	return ret;
2040}
2041
2042
2043/*
2044 * Top mode display.
2045 */
2046
2047void
2048pmcstat_display_log(void)
2049{
2050
2051	pmcstat_refresh_top();
2052
2053	/* Reset everythings if delta mode. */
2054	if (args.pa_topmode == PMCSTAT_TOP_DELTA) {
2055		if (plugins[args.pa_plugin].pl_shutdown != NULL)
2056			plugins[args.pa_plugin].pl_shutdown(NULL);
2057		pmcstat_stats_reset(0);
2058		if (plugins[args.pa_plugin].pl_init != NULL)
2059			plugins[args.pa_plugin].pl_init();
2060	}
2061
2062}
2063
2064/*
2065 * Configure a plugins.
2066 */
2067
2068void
2069pmcstat_pluginconfigure_log(char *opt)
2070{
2071
2072	if (strncmp(opt, "threshold=", 10) == 0) {
2073		pmcstat_threshold = atof(opt+10);
2074	} else {
2075		if (plugins[args.pa_plugin].pl_configure != NULL) {
2076			if (!plugins[args.pa_plugin].pl_configure(opt))
2077				err(EX_USAGE,
2078				    "ERROR: unknown option <%s>.", opt);
2079		}
2080	}
2081}
2082
2083/*
2084 * Initialize module.
2085 */
2086
2087void
2088pmcstat_initialize_logging(void)
2089{
2090	int i;
2091
2092	/* use a convenient format for 'ldd' output */
2093	if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1","%o \"%p\" %x\n",1) != 0)
2094		err(EX_OSERR, "ERROR: Cannot setenv");
2095
2096	/* Initialize hash tables */
2097	pmcstat_string_initialize();
2098	for (i = 0; i < PMCSTAT_NHASH; i++) {
2099		LIST_INIT(&pmcstat_image_hash[i]);
2100		LIST_INIT(&pmcstat_process_hash[i]);
2101	}
2102
2103	/*
2104	 * Create a fake 'process' entry for the kernel with pid -1.
2105	 * hwpmc(4) will subsequently inform us about where the kernel
2106	 * and any loaded kernel modules are mapped.
2107	 */
2108	if ((pmcstat_kernproc = pmcstat_process_lookup((pid_t) -1,
2109		 PMCSTAT_ALLOCATE)) == NULL)
2110		err(EX_OSERR, "ERROR: Cannot initialize logging");
2111
2112	/* PMC count. */
2113	pmcstat_npmcs = 0;
2114
2115	/* Merge PMC with same name. */
2116	pmcstat_mergepmc = args.pa_mergepmc;
2117
2118	/*
2119	 * Initialize plugins
2120	 */
2121
2122	if (plugins[args.pa_pplugin].pl_init != NULL)
2123		plugins[args.pa_pplugin].pl_init();
2124	if (plugins[args.pa_plugin].pl_init != NULL)
2125		plugins[args.pa_plugin].pl_init();
2126}
2127
2128/*
2129 * Shutdown module.
2130 */
2131
2132void
2133pmcstat_shutdown_logging(void)
2134{
2135	int i;
2136	FILE *mf;
2137	struct pmcstat_image *pi, *pitmp;
2138	struct pmcstat_process *pp, *pptmp;
2139	struct pmcstat_pcmap *ppm, *ppmtmp;
2140
2141	/* determine where to send the map file */
2142	mf = NULL;
2143	if (args.pa_mapfilename != NULL)
2144		mf = (strcmp(args.pa_mapfilename, "-") == 0) ?
2145		    args.pa_printfile : fopen(args.pa_mapfilename, "w");
2146
2147	if (mf == NULL && args.pa_flags & FLAG_DO_GPROF &&
2148	    args.pa_verbosity >= 2)
2149		mf = args.pa_printfile;
2150
2151	if (mf)
2152		(void) fprintf(mf, "MAP:\n");
2153
2154	/*
2155	 * Shutdown the plugins
2156	 */
2157
2158	if (plugins[args.pa_plugin].pl_shutdown != NULL)
2159		plugins[args.pa_plugin].pl_shutdown(mf);
2160	if (plugins[args.pa_pplugin].pl_shutdown != NULL)
2161		plugins[args.pa_pplugin].pl_shutdown(mf);
2162
2163	for (i = 0; i < PMCSTAT_NHASH; i++) {
2164		LIST_FOREACH_SAFE(pi, &pmcstat_image_hash[i], pi_next,
2165		    pitmp) {
2166			if (plugins[args.pa_plugin].pl_shutdownimage != NULL)
2167				plugins[args.pa_plugin].pl_shutdownimage(pi);
2168			if (plugins[args.pa_pplugin].pl_shutdownimage != NULL)
2169				plugins[args.pa_pplugin].pl_shutdownimage(pi);
2170
2171			free(pi->pi_symbols);
2172			if (pi->pi_addr2line != NULL)
2173				pclose(pi->pi_addr2line);
2174			LIST_REMOVE(pi, pi_next);
2175			free(pi);
2176		}
2177
2178		LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[i], pp_next,
2179		    pptmp) {
2180			TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
2181				TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
2182				free(ppm);
2183			}
2184			LIST_REMOVE(pp, pp_next);
2185			free(pp);
2186		}
2187	}
2188
2189	pmcstat_string_shutdown();
2190
2191	/*
2192	 * Print errors unless -q was specified.  Print all statistics
2193	 * if verbosity > 1.
2194	 */
2195#define	PRINT(N,V) do {							\
2196		if (pmcstat_stats.ps_##V || args.pa_verbosity >= 2)	\
2197			(void) fprintf(args.pa_printfile, " %-40s %d\n",\
2198			    N, pmcstat_stats.ps_##V);			\
2199	} while (0)
2200
2201	if (args.pa_verbosity >= 1 && (args.pa_flags & FLAG_DO_ANALYSIS)) {
2202		(void) fprintf(args.pa_printfile, "CONVERSION STATISTICS:\n");
2203		PRINT("#exec/a.out", exec_aout);
2204		PRINT("#exec/elf", exec_elf);
2205		PRINT("#exec/unknown", exec_indeterminable);
2206		PRINT("#exec handling errors", exec_errors);
2207		PRINT("#samples/total", samples_total);
2208		PRINT("#samples/unclaimed", samples_unknown_offset);
2209		PRINT("#samples/unknown-object", samples_indeterminable);
2210		PRINT("#samples/unknown-function", samples_unknown_function);
2211		PRINT("#callchain/dubious-frames", callchain_dubious_frames);
2212	}
2213
2214	if (mf)
2215		(void) fclose(mf);
2216}
2217