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