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