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