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