pmcstat_log.c revision 147712
1/*-
2 * Copyright (c) 2005, Joseph Koshy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/usr.sbin/pmcstat/pmcstat_log.c 147712 2005-07-01 03:45:01Z jkoshy $");
29
30/*
31 * Transform a hwpmc(4) log into human readable form and into gprof(1)
32 * compatible profiles.
33 */
34
35#include <sys/param.h>
36#include <sys/endian.h>
37#include <sys/gmon.h>
38#include <sys/imgact_aout.h>
39#include <sys/imgact_elf.h>
40#include <sys/mman.h>
41#include <sys/pmc.h>
42#include <sys/queue.h>
43#include <sys/stat.h>
44#include <sys/wait.h>
45
46#include <netinet/in.h>
47
48#include <assert.h>
49#include <err.h>
50#include <fcntl.h>
51#include <libgen.h>
52#include <limits.h>
53#include <pmc.h>
54#include <pmclog.h>
55#include <sysexits.h>
56#include <stdint.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62#include "pmcstat.h"
63
64#define	min(A,B)		((A) < (B) ? (A) : (B))
65#define	max(A,B)		((A) > (B) ? (A) : (B))
66
67/*
68 * A simple implementation to intern strings.  Each interned string is
69 * assigned a unique address, so that subsequent string compares can
70 * be done by a simple pointer comparision.
71 */
72
73struct pmcstat_string {
74	LIST_ENTRY(pmcstat_string)	ps_next;	/* hash link */
75	int		ps_len;
76	int		ps_hash;
77	const char	*ps_string;
78};
79
80static LIST_HEAD(,pmcstat_string)	pmcstat_string_hash[PMCSTAT_NHASH];
81
82/*
83 * 'pmcstat_pmcs' is a mapping for PMC ids to their human-readable
84 * names.
85 */
86
87struct pmcstat_pmcrecord {
88	LIST_ENTRY(pmcstat_pmcrecord)	pr_next;
89	pmc_id_t	pr_pmcid;
90	const char 	*pr_pmcname;
91};
92
93static LIST_HEAD(,pmcstat_pmcrecord)	pmcstat_pmcs =
94	LIST_HEAD_INITIALIZER(&pmcstat_pmcs);
95
96struct pmcstat_gmonfile {
97	LIST_ENTRY(pmcstat_gmonfile)	pgf_next; /* list of entries */
98	pmc_id_t	pgf_pmcid;	/* id of the associated pmc */
99	size_t		pgf_nsamples;	/* number of samples in this gmon.out */
100	const char	*pgf_name;	/* name of gmon.out file */
101	size_t		pgf_ndatabytes;	/* number of bytes mapped */
102	void		*pgf_gmondata;	/* pointer to mmap'ed data */
103};
104
105static TAILQ_HEAD(,pmcstat_gmonfile)	pmcstat_gmonfiles =
106	TAILQ_HEAD_INITIALIZER(pmcstat_gmonfiles);
107
108#define	GM_TO_BUCKETS(GM)	((uint16_t *) ((char *) (GM) + sizeof(*(GM))))
109
110/*
111 * A 'pmcstat_image' structure describes an executable program on
112 * disk.  'pi_internedpath' is a cookie representing the pathname of
113 * the executable.  'pi_start' and 'pi_end' are the least and greatest
114 * virtual addresses for the text segments in the executable.
115 * 'pi_gmonlist' contains a linked list of gmon.out files associated
116 * with this image.
117 */
118
119enum pmcstat_image_type {
120	PMCSTAT_IMAGE_UNKNOWN = 0,
121	PMCSTAT_IMAGE_ELF,
122	PMCSTAT_IMAGE_AOUT
123};
124
125struct pmcstat_image {
126	LIST_ENTRY(pmcstat_image) pi_next;	/* hash link */
127	TAILQ_ENTRY(pmcstat_image) pi_lru;	/* LRU list */
128	const char	*pi_internedpath;	/* cookie */
129	const char	*pi_samplename;		/* sample file name */
130
131	enum pmcstat_image_type pi_type;	/* executable type */
132	uintfptr_t	pi_start;		/* start address (inclusive) */
133	uintfptr_t	pi_end;			/* end address (exclusive) */
134	int		pi_isdynamic;		/* whether a dynamic object */
135
136	LIST_HEAD(,pmcstat_gmonfile) pi_gmlist;
137};
138
139static LIST_HEAD(,pmcstat_image)	pmcstat_image_hash[PMCSTAT_NHASH];
140static TAILQ_HEAD(,pmcstat_image)	pmcstat_image_lru =
141	TAILQ_HEAD_INITIALIZER(pmcstat_image_lru);
142
143struct pmcstat_pcmap {
144	TAILQ_ENTRY(pmcstat_pcmap) ppm_next;
145	uintfptr_t	ppm_lowpc;
146	uintfptr_t	ppm_highpc;
147	struct pmcstat_image *ppm_image;
148};
149
150/*
151 * A 'pmcstat_process' structure tracks processes.
152 */
153
154struct pmcstat_process {
155	LIST_ENTRY(pmcstat_process) pp_next;	/* hash-next */
156	pid_t			pp_pid;		/* associated pid */
157	int			pp_isactive;	/* whether active */
158	TAILQ_HEAD(,pmcstat_pcmap) pp_map;	/* address range map */
159};
160
161static LIST_HEAD(,pmcstat_process) pmcstat_process_hash[PMCSTAT_NHASH];
162
163static struct pmcstat_process *pmcstat_kernproc; /* kernel 'process' */
164
165/*
166 * Prototypes
167 */
168
169static void	pmcstat_gmon_create_file(struct pmcstat_gmonfile *_pgf,
170    struct pmcstat_image *_image);
171static const char *pmcstat_gmon_create_name(const char *_sd,
172    struct pmcstat_image *_img, pmc_id_t _pmcid);
173static void	pmcstat_gmon_map_file(struct pmcstat_gmonfile *_pgf);
174static void	pmcstat_gmon_unmap_file(struct pmcstat_gmonfile *_pgf);
175
176static struct pmcstat_image *pmcstat_image_from_path(const char *_path);
177static enum pmcstat_image_type pmcstat_image_get_type(const char *_p);
178static void	pmcstat_image_get_elf_params(struct pmcstat_image *_image,
179    uintfptr_t *_minp, uintfptr_t *_maxp, int *_isdyn);
180static void	pmcstat_image_increment_bucket(struct pmcstat_pcmap *_pcm,
181    uintfptr_t _pc, pmc_id_t _pmcid, struct pmcstat_args *_a);
182static void	pmcstat_image_link(struct pmcstat_process *_pp,
183    struct pmcstat_image *_i, uintfptr_t _lpc, uintfptr_t _hpc);
184
185static void	pmcstat_pmcid_add(pmc_id_t _pmcid, const char *_name,
186    struct pmcstat_args *_a);
187static const char *pmcstat_pmcid_to_name(pmc_id_t _pmcid);
188
189static void	pmcstat_process_add_elf_image(struct pmcstat_process *_pp,
190    const char *_path);
191static struct pmcstat_process *pmcstat_process_lookup(pid_t _pid, int _allocate);
192static struct pmcstat_pcmap *pmcstat_process_find_map(
193    struct pmcstat_process *_p, uintfptr_t _pc);
194static void	pmcstat_process_new_image(struct pmcstat_process *_pp,
195    const char *_path);
196
197static int	pmcstat_string_compute_hash(const char *_string);
198static const char *pmcstat_string_intern(const char *_s);
199static struct pmcstat_string *pmcstat_string_lookup(const char *_s);
200
201
202/*
203 * Create a gmon.out file and size it.
204 */
205
206static void
207pmcstat_gmon_create_file(struct pmcstat_gmonfile *pgf,
208    struct pmcstat_image *image)
209{
210	int fd;
211	size_t count;
212	struct gmonhdr gm;
213	char buffer[DEFAULT_BUFFER_SIZE];
214
215	if ((fd = open(pgf->pgf_name, O_RDWR|O_NOFOLLOW|O_CREAT,
216		 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
217		err(EX_OSERR, "ERROR: Cannot open \"%s\"", pgf->pgf_name);
218
219	gm.lpc = image->pi_start;
220	gm.hpc = image->pi_end;
221	gm.ncnt = pgf->pgf_nsamples;
222	gm.version = GMONVERSION;
223	gm.profrate = 0;		/* use ticks */
224	gm.histcounter_type = 0;	/* compatibility with moncontrol() */
225	gm.spare[0] = gm.spare[1] = 0;
226
227	/* Write out the gmon header */
228	if (write(fd, &gm, sizeof(gm)) < 0)
229		goto error;
230
231	/* Zero fill the samples[] array */
232	(void) memset(buffer, 0, sizeof(buffer));
233
234	count = pgf->pgf_ndatabytes - sizeof(struct gmonhdr);
235	while (count > sizeof(buffer)) {
236		if (write(fd, &buffer, sizeof(buffer)) < 0)
237			goto error;
238		count -= sizeof(buffer);
239	}
240
241	if (write(fd, &buffer, count) < 0)
242		goto error;
243
244	(void) close(fd);
245
246	return;
247
248 error:
249	err(EX_OSERR, "ERROR: Cannot write \"%s\"", pgf->pgf_name);
250}
251
252const char *
253pmcstat_gmon_create_name(const char *samplesdir, struct pmcstat_image *image,
254    pmc_id_t pmcid)
255{
256	const char *pmcname;
257	char fullpath[PATH_MAX];
258
259	pmcname = pmcstat_pmcid_to_name(pmcid);
260
261	(void) snprintf(fullpath, sizeof(fullpath),
262	    "%s/%s/%s", samplesdir, pmcname, image->pi_samplename);
263
264	return pmcstat_string_intern(fullpath);
265}
266
267
268static void
269pmcstat_gmon_map_file(struct pmcstat_gmonfile *pgf)
270{
271	int fd;
272
273	/* the gmon.out file must already exist */
274	if ((fd = open(pgf->pgf_name, O_RDWR | O_NOFOLLOW, 0)) < 0)
275		err(EX_OSERR, "ERROR: cannot open \"%s\"",
276		    pgf->pgf_name);
277
278	pgf->pgf_gmondata = mmap(NULL, pgf->pgf_ndatabytes,
279	    PROT_READ|PROT_WRITE, MAP_NOSYNC|MAP_SHARED, fd, 0);
280
281	if (pgf->pgf_gmondata == MAP_FAILED)
282		/* XXX unmap a few files and try again? */
283		err(EX_OSERR, "ERROR: cannot map \"%s\"", pgf->pgf_name);
284
285	(void) close(fd);
286}
287
288/*
289 * Unmap the data mapped from a gmon.out file.
290 */
291
292static void
293pmcstat_gmon_unmap_file(struct pmcstat_gmonfile *pgf)
294{
295	(void) msync(pgf->pgf_gmondata, pgf->pgf_ndatabytes,
296	    MS_SYNC);
297	(void) munmap(pgf->pgf_gmondata, pgf->pgf_ndatabytes);
298	pgf->pgf_gmondata = NULL;
299}
300
301static void
302pmcstat_image_get_elf_params(struct pmcstat_image *image, uintfptr_t *minp,
303    uintfptr_t *maxp, int *is_dynamic)
304{
305	int fd, i;
306	struct stat st;
307	void *mapbase;
308	uintfptr_t minva, maxva;
309	const Elf_Ehdr *h;
310	const Elf_Phdr *ph;
311	const Elf_Shdr *sh;
312	const char *path;
313
314	minva = ~(uintfptr_t) 0;
315	maxva = (uintfptr_t) 0;
316	path = image->pi_internedpath;
317
318	if ((fd = open(path, O_RDONLY, 0)) < 0)
319		err(EX_OSERR, "ERROR: Cannot open \"%s\"", path);
320
321	if (fstat(fd, &st) < 0)
322		err(EX_OSERR, "ERROR: Cannot stat \"%s\"", path);
323
324	if ((mapbase = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==
325	    MAP_FAILED)
326		err(EX_OSERR, "ERROR: Cannot mmap \"%s\"", path);
327
328	(void) close(fd);
329
330	h = (const Elf_Ehdr *) mapbase;
331	if (!IS_ELF(*h))
332		err(EX_SOFTWARE, "ERROR: \"%s\" not an ELF file", path);
333
334	sh = (const Elf_Shdr *)((uintptr_t) mapbase + h->e_shoff);
335
336	if (h->e_type == ET_EXEC || h->e_type == ET_DYN) {
337		/*
338		 * Some kind of shared object: find the min,max va for
339		 * its executable sections.
340		 */
341		for (i = 0; i < h->e_shnum; i++)
342			if (sh[i].sh_flags & SHF_EXECINSTR) { /* code */
343				minva = min(minva, sh[i].sh_addr);
344				maxva = max(maxva, sh[i].sh_addr +
345				    sh[i].sh_size);
346			}
347	} else
348		err(EX_DATAERR, "ERROR: Unknown file type for \"%s\"",
349		    image->pi_internedpath);
350
351	*is_dynamic = 0;
352	if (h->e_type == ET_EXEC) {
353		ph = (const Elf_Phdr *)((uintptr_t) mapbase + h->e_phoff);
354		for (i = 0; i < h->e_phnum; i++) {
355			switch (ph[i].p_type) {
356			case PT_DYNAMIC:
357				*is_dynamic = 1;
358				break;
359			}
360		}
361	}
362
363	if (munmap(mapbase, st.st_size) < 0)
364		err(EX_OSERR, "ERROR: Cannot unmap \"%s\"", path);
365
366	*minp = minva;
367	*maxp = maxva;
368
369}
370
371/*
372 * Locate an image descriptor given an interned path.
373 */
374
375static struct pmcstat_image *
376pmcstat_image_from_path(const char *internedpath)
377{
378	int count, hash, nlen;
379	struct pmcstat_image *pi;
380	char *sn;
381	char name[NAME_MAX];
382
383	hash = pmcstat_string_compute_hash(internedpath);
384
385	/* look for an existing entry */
386	LIST_FOREACH(pi, &pmcstat_image_hash[hash], pi_next)
387	    if (pi->pi_internedpath == internedpath) {
388		    /* move descriptor to the head of the lru list */
389		    TAILQ_REMOVE(&pmcstat_image_lru, pi, pi_lru);
390		    TAILQ_INSERT_HEAD(&pmcstat_image_lru, pi, pi_lru);
391		    return pi;
392	    }
393
394	/*
395	 * allocate a new entry and place at the head of the hash and
396	 * LRU lists
397	 */
398	pi = malloc(sizeof(*pi));
399	if (pi == NULL)
400		return NULL;
401
402	pi->pi_type = PMCSTAT_IMAGE_UNKNOWN;
403	pi->pi_internedpath = internedpath;
404	pi->pi_start = ~0;
405	pi->pi_end = 0;
406
407	/* look for a suitable name for the sample files */
408	if ((sn = basename(internedpath)) == NULL)
409		err(EX_OSERR, "ERROR: Cannot process \"%s\"", internedpath);
410
411	nlen = strlen(sn);
412	nlen = min(nlen, (int) sizeof(name) - 6);	/* ".gmon\0" */
413
414	snprintf(name, sizeof(name), "%.*s.gmon",
415	    nlen, sn);
416
417	if (pmcstat_string_lookup(name) == NULL)
418		pi->pi_samplename = pmcstat_string_intern(name);
419	else {
420		nlen = strlen(sn);
421		nlen = min(nlen, (int) sizeof(name)-10); /* "~ddd.gmon\0" */
422		count = 0;
423		do {
424			count++;
425			snprintf(name, sizeof(name), "%.*s~%3.3d",
426			    nlen, sn, count);
427			if (pmcstat_string_lookup(name) == NULL) {
428				pi->pi_samplename = pmcstat_string_intern(name);
429				count = 0;
430			}
431		} while (count > 0);
432	}
433
434	LIST_INIT(&pi->pi_gmlist);
435
436	LIST_INSERT_HEAD(&pmcstat_image_hash[hash], pi, pi_next);
437	TAILQ_INSERT_HEAD(&pmcstat_image_lru, pi, pi_lru);
438
439	return pi;
440}
441
442/*
443 * Given an open file, determine its file type.
444 */
445
446static enum pmcstat_image_type
447pmcstat_image_get_type(const char *path)
448{
449	int fd;
450	Elf_Ehdr eh;
451	struct exec ex;
452	ssize_t nbytes;
453	char buffer[DEFAULT_BUFFER_SIZE];
454
455	if ((fd = open(path, O_RDONLY)) < 0)
456		err(EX_OSERR, "ERROR: Cannot open \"%s\"", path);
457
458	nbytes = max(sizeof(eh), sizeof(ex));
459	if ((nbytes = pread(fd, buffer, nbytes, 0)) < 0)
460		err(EX_OSERR, "ERROR: Cannot read \"%s\"", path);
461
462	(void) close(fd);
463
464	/* check if its an ELF file */
465	if ((unsigned) nbytes >= sizeof(Elf_Ehdr)) {
466		bcopy(buffer, &eh, sizeof(eh));
467		if (IS_ELF(eh))
468			return PMCSTAT_IMAGE_ELF;
469	}
470
471	/* Look for an A.OUT header */
472	if ((unsigned) nbytes >= sizeof(struct exec)) {
473		bcopy(buffer, &ex, sizeof(ex));
474		if (!N_BADMAG(ex))
475			return PMCSTAT_IMAGE_AOUT;
476	}
477
478	return PMCSTAT_IMAGE_UNKNOWN;
479}
480
481/*
482 * Increment the bucket in the gmon.out file corresponding to 'pmcid'
483 * and 'pc'.
484 */
485
486static void
487pmcstat_image_increment_bucket(struct pmcstat_pcmap *map, uintfptr_t pc,
488    pmc_id_t pmcid, struct pmcstat_args *a)
489{
490	struct pmcstat_image *image;
491	struct pmcstat_gmonfile *pgf;
492	uintfptr_t bucket;
493	HISTCOUNTER *hc;
494
495	assert(pc >= map->ppm_lowpc && pc < map->ppm_highpc);
496
497	/*
498	 * Find the gmon file corresponding to 'pmcid', creating it if
499	 * needed.
500	 */
501
502	image = map->ppm_image;
503
504	LIST_FOREACH(pgf, &image->pi_gmlist, pgf_next)
505	    if (pgf->pgf_pmcid == pmcid)
506		    break;
507
508	/* If we don't have a gmon.out file for this PMCid, create one */
509	if (pgf == NULL) {
510		if ((pgf = calloc(1, sizeof(*pgf))) == NULL)
511			err(EX_OSERR, "ERROR:");
512
513		pgf->pgf_gmondata = NULL;	/* mark as unmapped */
514		pgf->pgf_name = pmcstat_gmon_create_name(a->pa_samplesdir,
515		    image, pmcid);
516		pgf->pgf_pmcid = pmcid;
517		pgf->pgf_nsamples = (image->pi_end - image->pi_start) /
518		    FUNCTION_ALIGNMENT;	/* see <machine/profile.h> */
519		pgf->pgf_ndatabytes = sizeof(struct gmonhdr) +
520		    pgf->pgf_nsamples * sizeof(HISTCOUNTER);
521
522		pmcstat_gmon_create_file(pgf, image);
523
524		LIST_INSERT_HEAD(&image->pi_gmlist, pgf, pgf_next);
525	}
526
527	/*
528	 * Map the gmon file in if needed.  It may have been mapped
529	 * out under memory pressure.
530	 */
531	if (pgf->pgf_gmondata == NULL)
532		pmcstat_gmon_map_file(pgf);
533
534	bucket = (pc - map->ppm_lowpc) / FUNCTION_ALIGNMENT;
535
536	assert(bucket < pgf->pgf_nsamples);
537
538	hc = (HISTCOUNTER *) ((uintptr_t) pgf->pgf_gmondata +
539	    sizeof(struct gmonhdr));
540	hc[bucket]++;
541
542}
543
544/*
545 * Record the fact that PC values from 'lowpc' to 'highpc' come from
546 * image 'image'.
547 */
548
549static void
550pmcstat_image_link(struct pmcstat_process *pp, struct pmcstat_image *image,
551    uintfptr_t lowpc, uintfptr_t highpc)
552{
553	struct pmcstat_pcmap *pcm, *pcmnew;
554
555	if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL)
556		err(EX_OSERR, "ERROR: ");
557
558	pcmnew->ppm_lowpc  = lowpc;
559	pcmnew->ppm_highpc = highpc;
560	pcmnew->ppm_image  = image;
561
562	TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next)
563	    if (pcm->ppm_lowpc < lowpc)
564		    break;
565
566	if (pcm == NULL)
567		TAILQ_INSERT_TAIL(&pp->pp_map, pcmnew, ppm_next);
568	else
569		TAILQ_INSERT_BEFORE(pcm, pcmnew, ppm_next);
570}
571
572/*
573 * Add a {pmcid,name} mapping.
574 */
575
576static void
577pmcstat_pmcid_add(pmc_id_t pmcid, const char *name, struct pmcstat_args *a)
578{
579	struct pmcstat_pmcrecord *pr;
580	struct stat st;
581	char fullpath[PATH_MAX];
582
583	LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
584	    if (pr->pr_pmcid == pmcid) {
585		    pr->pr_pmcname = name;
586		    return;
587	    }
588
589	if ((pr = malloc(sizeof(*pr))) == NULL)
590		err(EX_OSERR, "ERROR: Cannot allocate pmc record");
591
592	pr->pr_pmcid = pmcid;
593	pr->pr_pmcname = name;
594	LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next);
595
596	(void) snprintf(fullpath, sizeof(fullpath), "%s/%s", a->pa_samplesdir,
597	    name);
598
599	/* If the path name exists, it should be a directory */
600	if (stat(fullpath, &st) == 0 && S_ISDIR(st.st_mode))
601		return;
602
603	if (mkdir(fullpath, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0)
604		err(EX_OSERR, "ERROR: Cannot create directory \"%s\"",
605		    fullpath);
606}
607
608/*
609 * Given a pmcid in use, find its human-readable name, or a
610 */
611
612static const char *
613pmcstat_pmcid_to_name(pmc_id_t pmcid)
614{
615	struct pmcstat_pmcrecord *pr;
616	char fullpath[PATH_MAX];
617
618	LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
619	    if (pr->pr_pmcid == pmcid)
620		    return pr->pr_pmcname;
621
622	/* create a default name and add this entry */
623	if ((pr = malloc(sizeof(*pr))) == NULL)
624		err(EX_OSERR, "ERROR: ");
625	pr->pr_pmcid = pmcid;
626
627	(void) snprintf(fullpath, sizeof(fullpath), "%X", (unsigned int) pmcid);
628	pr->pr_pmcname = pmcstat_string_intern(fullpath);
629
630	LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next);
631
632	return pr->pr_pmcname;
633}
634
635/*
636 * Associate an ELF image with a process.  Argument 'path' names the
637 * executable while 'fd' is an already open descriptor to it.
638 */
639
640static void
641pmcstat_process_add_elf_image(struct pmcstat_process *pp, const char *path)
642{
643	int isdynamic;
644	size_t linelen;
645	FILE *rf;
646	char *line;
647	uintfptr_t minva, maxva;
648	uintmax_t libstart;
649	struct pmcstat_image *image;
650	char libpath[PATH_MAX];
651	char command[PATH_MAX + sizeof(PMCSTAT_LDD_COMMAND) + 1];
652
653	minva = ~ (uintfptr_t) 0;
654	maxva = (uintfptr_t) 0;
655	isdynamic = 0;
656
657	if ((image = pmcstat_image_from_path(path)) == NULL)
658		return;
659
660	if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) {
661
662		pmcstat_image_get_elf_params(image, &minva, &maxva,
663		    &isdynamic);
664
665		image->pi_type = PMCSTAT_IMAGE_ELF;
666		image->pi_start = minva;
667		image->pi_end = maxva;
668		image->pi_isdynamic = isdynamic;
669	}
670
671	/* create a map entry for the base executable */
672	pmcstat_image_link(pp, image, minva, maxva);
673
674	if (image->pi_isdynamic) {
675
676		(void) snprintf(command, sizeof(command), "%s %s",
677		    PMCSTAT_LDD_COMMAND, path);
678
679		if ((rf = popen(command, "r")) == NULL)
680			err(EX_OSERR, "ERROR: Cannot create pipe");
681
682		(void) fgetln(rf, &linelen);
683
684		while (!feof(rf) && !ferror(rf)) {
685
686			if ((line = fgetln(rf, &linelen)) == NULL)
687				continue;
688			line[linelen-1] = '\0';
689
690			if (sscanf(line, "%s %jx",
691				libpath, &libstart) != 2)
692				continue;
693
694			image = pmcstat_image_from_path(
695				pmcstat_string_intern(libpath));
696			if (image == NULL)
697				err(EX_OSERR, "ERROR: Cannot process "
698				    "\"%s\"", libpath);
699
700			if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) {
701
702				pmcstat_image_get_elf_params(image,
703				    &minva, &maxva, &isdynamic);
704
705				image->pi_type = PMCSTAT_IMAGE_ELF;
706				image->pi_start = minva;
707				image->pi_end = maxva;
708				image->pi_isdynamic = isdynamic;
709			}
710
711			pmcstat_image_link(pp, image, libstart + image->pi_start,
712			    libstart + image->pi_end);
713		}
714
715		(void) pclose(rf);
716
717	}
718}
719
720/*
721 * Find the process descriptor corresponding to a PID.  If 'allocate'
722 * is zero, we return a NULL if a pid descriptor could not be found or
723 * a process descriptor process.  If 'allocate' is non-zero, then we
724 * will attempt to allocate a fresh process descriptor.  Zombie
725 * process descriptors are only removed if a fresh allocation for the
726 * same PID is requested.
727 */
728
729static struct pmcstat_process *
730pmcstat_process_lookup(pid_t pid, int allocate)
731{
732	uint32_t hash;
733	struct pmcstat_pcmap *ppm, *ppmtmp;
734	struct pmcstat_process *pp, *pptmp;
735
736	hash = (uint32_t) pid & PMCSTAT_HASH_MASK;	/* simplicity wins */
737
738	LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[hash], pp_next, pptmp)
739	    if (pp->pp_pid == pid) {
740		    /* Found a descriptor, check and process zombies */
741		    if (allocate && !pp->pp_isactive) {
742			    /* remove maps */
743			    TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next,
744				ppmtmp) {
745				    TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
746				    free(ppm);
747			    }
748			    /* remove process entry */
749			    LIST_REMOVE(pp, pp_next);
750			    free(pp);
751			    break;
752		    }
753		    return pp;
754	    }
755
756	if (!allocate)
757		return NULL;
758
759	if ((pp = malloc(sizeof(*pp))) == NULL)
760		err(EX_OSERR, "ERROR: Cannot allocate pid descriptor");
761
762	pp->pp_pid = pid;
763	pp->pp_isactive = 1;
764
765	TAILQ_INIT(&pp->pp_map);
766
767	LIST_INSERT_HEAD(&pmcstat_process_hash[hash], pp, pp_next);
768	return pp;
769}
770
771/*
772 * Find the map entry associated with process 'p' at PC value 'pc'.
773 */
774
775static struct pmcstat_pcmap *
776pmcstat_process_find_map(struct pmcstat_process *p, uintfptr_t pc)
777{
778	struct pmcstat_pcmap *ppm;
779
780	TAILQ_FOREACH(ppm, &p->pp_map, ppm_next)
781	    if (pc >= ppm->ppm_lowpc && pc < ppm->ppm_highpc)
782		    return ppm;
783
784	return NULL;
785}
786
787/*
788 * Associate an image and a process.
789 */
790
791static void
792pmcstat_process_new_image(struct pmcstat_process *pp, const char *path)
793{
794	enum pmcstat_image_type filetype;
795	struct pmcstat_image *image;
796
797	if ((image = pmcstat_image_from_path(path)) == NULL)
798		return;
799
800	if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
801		filetype = pmcstat_image_get_type(path);
802	else
803		filetype = image->pi_type;
804
805	switch (filetype) {
806	case PMCSTAT_IMAGE_ELF:
807		pmcstat_process_add_elf_image(pp, path);
808		break;
809
810	case PMCSTAT_IMAGE_AOUT:
811		break;
812
813	default:
814		err(EX_SOFTWARE, "ERROR: Unsupported executable type \"%s\"",
815		    path);
816	}
817}
818
819
820
821/*
822 * Compute a 'hash' value for a string.
823 */
824
825static int
826pmcstat_string_compute_hash(const char *s)
827{
828	int hash;
829
830	for (hash = 0; *s; s++)
831		hash ^= *s;
832
833	return hash & PMCSTAT_HASH_MASK;
834}
835
836/*
837 * Intern a copy of string 's', and return a pointer to it.
838 */
839
840static const char *
841pmcstat_string_intern(const char *s)
842{
843	struct pmcstat_string *ps;
844	int hash, len;
845
846	hash = pmcstat_string_compute_hash(s);
847	len  = strlen(s);
848
849	if ((ps = pmcstat_string_lookup(s)) != NULL)
850		return ps->ps_string;
851
852	if ((ps = malloc(sizeof(*ps))) == NULL)
853		err(EX_OSERR, "ERROR: Could not intern string");
854	ps->ps_len = len;
855	ps->ps_hash = hash;
856	ps->ps_string = strdup(s);
857	LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next);
858	return ps->ps_string;
859}
860
861static struct pmcstat_string *
862pmcstat_string_lookup(const char *s)
863{
864	struct pmcstat_string *ps;
865	int hash, len;
866
867	hash = pmcstat_string_compute_hash(s);
868	len = strlen(s);
869
870	LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next)
871	    if (ps->ps_len == len && ps->ps_hash == hash &&
872		strcmp(ps->ps_string, s) == 0)
873		    return ps;
874	return NULL;
875}
876
877/*
878 * Public Interfaces.
879 */
880
881/*
882 * Close a logfile, after first flushing all in-module queued data.
883 */
884
885int
886pmcstat_close_log(struct pmcstat_args *a)
887{
888	if (pmc_flush_logfile() < 0 ||
889	    pmc_configure_logfile(-1) < 0)
890		err(EX_OSERR, "ERROR: logging failed");
891	a->pa_flags &= ~(FLAG_HAS_OUTPUT_LOGFILE | FLAG_HAS_PIPE);
892	return a->pa_flags & FLAG_HAS_PIPE ? PMCSTAT_EXITING :
893	    PMCSTAT_FINISHED;
894}
895
896
897int
898pmcstat_convert_log(struct pmcstat_args *a)
899{
900	uintfptr_t pc;
901	struct pmcstat_process *pp, *ppnew;
902	struct pmcstat_pcmap *ppm, *ppmtmp;
903	struct pmclog_ev ev;
904	const char *image_path;
905
906	while (pmclog_read(a->pa_logparser, &ev) == 0) {
907		assert(ev.pl_state == PMCLOG_OK);
908
909		switch (ev.pl_type) {
910		case PMCLOG_TYPE_MAPPINGCHANGE:
911			/*
912			 * Introduce an address range mapping for a
913			 * process.
914			 */
915			break;
916
917		case PMCLOG_TYPE_PCSAMPLE:
918
919			/*
920			 * We bring in the gmon file for the image
921			 * currently associated with the PMC & pid
922			 * pair and increment the appropriate entry
923			 * bin inside this.
924			 */
925			pc = ev.pl_u.pl_s.pl_pc;
926			pp = pmcstat_process_lookup(ev.pl_u.pl_s.pl_pid, 1);
927			if ((ppm = pmcstat_process_find_map(pp, pc)) == NULL &&
928			    (ppm = pmcstat_process_find_map(pmcstat_kernproc,
929				pc)) == NULL)
930				break; /* unknown process,offset pair */
931
932			pmcstat_image_increment_bucket(ppm, pc,
933			    ev.pl_u.pl_s.pl_pmcid, a);
934
935			break;
936
937		case PMCLOG_TYPE_PMCALLOCATE:
938			/*
939			 * Record the association pmc id between this
940			 * PMC and its name.
941			 */
942			pmcstat_pmcid_add(ev.pl_u.pl_a.pl_pmcid,
943			    pmcstat_string_intern(ev.pl_u.pl_a.pl_evname), a);
944			break;
945
946		case PMCLOG_TYPE_PROCEXEC:
947
948			/*
949			 * Change the executable image associated with
950			 * a process.
951			 */
952			pp = pmcstat_process_lookup(ev.pl_u.pl_x.pl_pid, 1);
953
954			/* delete the current process map */
955			TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
956				TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
957				free(ppm);
958			}
959
960			/* locate the descriptor for the new 'base' image */
961			image_path = pmcstat_string_intern(
962				ev.pl_u.pl_x.pl_pathname);
963
964			/* link to the new image */
965			pmcstat_process_new_image(pp, image_path);
966			break;
967
968		case PMCLOG_TYPE_PROCEXIT:
969
970			/*
971			 * Due to the way the log is generated, the
972			 * last few samples corresponding to a process
973			 * may appear in the log after the process
974			 * exit event is recorded.  Thus we keep the
975			 * process' descriptor and associated data
976			 * structures around, but mark the process as
977			 * having exited.
978			 */
979			pp = pmcstat_process_lookup(ev.pl_u.pl_e.pl_pid, 0);
980			if (pp == NULL)
981				break;
982			pp->pp_isactive = 0;	/* make a zombie */
983			break;
984
985		case PMCLOG_TYPE_SYSEXIT:
986			pp = pmcstat_process_lookup(ev.pl_u.pl_se.pl_pid, 0);
987			if (pp == NULL)
988				break;
989			pp->pp_isactive = 0;	/* make a zombie */
990			break;
991
992		case PMCLOG_TYPE_PROCFORK:
993
994			/*
995			 * If we had been tracking 'oldpid', then clone
996			 * its pid descriptor.
997			 */
998			pp = pmcstat_process_lookup(ev.pl_u.pl_f.pl_oldpid, 0);
999			if (pp == NULL)
1000				break;
1001
1002			ppnew =
1003			    pmcstat_process_lookup(ev.pl_u.pl_f.pl_newpid, 1);
1004
1005			/* copy the old process' address maps */
1006			TAILQ_FOREACH(ppm, &pp->pp_map, ppm_next)
1007			    pmcstat_image_link(ppnew, ppm->ppm_image,
1008				ppm->ppm_lowpc, ppm->ppm_highpc);
1009			break;
1010
1011		default:	/* other types of entries are not relevant */
1012			break;
1013		}
1014	}
1015
1016	if (ev.pl_state == PMCLOG_EOF)
1017		return PMCSTAT_FINISHED;
1018	else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
1019		return PMCSTAT_RUNNING;
1020
1021	err(EX_DATAERR, "ERROR: event parsing failed (record %jd, "
1022	    "offset 0x%jx)", (uintmax_t) ev.pl_count + 1, ev.pl_offset);
1023}
1024
1025
1026/*
1027 * Open a log file, for reading or writing.
1028 *
1029 * The function returns the fd of a successfully opened log or -1 in
1030 * case of failure.
1031 */
1032
1033int
1034pmcstat_open(const char *path, int mode)
1035{
1036	int fd;
1037
1038	/*
1039	 * If 'path' is "-" then open one of stdin or stdout depending
1040	 * on the value of 'mode'.  Otherwise, treat 'path' as a file
1041	 * name and open that.
1042	 */
1043	if (path[0] == '-' && path[1] == '\0')
1044		fd = (mode == PMCSTAT_OPEN_FOR_READ) ? 0 : 1;
1045	else
1046		fd = open(path, mode == PMCSTAT_OPEN_FOR_READ ?
1047		    O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC),
1048		    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
1049
1050	return fd;
1051}
1052
1053/*
1054 * Print log entries as text.
1055 */
1056
1057int
1058pmcstat_print_log(struct pmcstat_args *a)
1059{
1060	struct pmclog_ev ev;
1061
1062	while (pmclog_read(a->pa_logparser, &ev) == 0) {
1063		assert(ev.pl_state == PMCLOG_OK);
1064		switch (ev.pl_type) {
1065		case PMCLOG_TYPE_CLOSELOG:
1066			PMCSTAT_PRINT_ENTRY(a,"closelog",);
1067			break;
1068		case PMCLOG_TYPE_DROPNOTIFY:
1069			PMCSTAT_PRINT_ENTRY(a,"drop",);
1070			break;
1071		case PMCLOG_TYPE_INITIALIZE:
1072			PMCSTAT_PRINT_ENTRY(a,"initlog","0x%x \"%s\"",
1073			    ev.pl_u.pl_i.pl_version,
1074			    pmc_name_of_cputype(ev.pl_u.pl_i.pl_arch));
1075			break;
1076		case PMCLOG_TYPE_MAPPINGCHANGE:
1077			PMCSTAT_PRINT_ENTRY(a,"mapping","%s %d %p %p \"%s\"",
1078			    ev.pl_u.pl_m.pl_type == PMCLOG_MAPPING_INSERT ?
1079			    	"insert" : "delete",
1080			    ev.pl_u.pl_m.pl_pid,
1081			    (void *) ev.pl_u.pl_m.pl_start,
1082			    (void *) ev.pl_u.pl_m.pl_end,
1083			    ev.pl_u.pl_m.pl_pathname);
1084			break;
1085		case PMCLOG_TYPE_PCSAMPLE:
1086			PMCSTAT_PRINT_ENTRY(a,"sample","0x%x %d %p %c",
1087			    ev.pl_u.pl_s.pl_pmcid,
1088			    ev.pl_u.pl_s.pl_pid,
1089			    (void *) ev.pl_u.pl_s.pl_pc,
1090			    ev.pl_u.pl_s.pl_usermode ? 'u' : 's');
1091			break;
1092		case PMCLOG_TYPE_PMCALLOCATE:
1093			PMCSTAT_PRINT_ENTRY(a,"allocate","0x%x \"%s\" 0x%x",
1094			    ev.pl_u.pl_a.pl_pmcid,
1095			    ev.pl_u.pl_a.pl_evname,
1096			    ev.pl_u.pl_a.pl_flags);
1097			break;
1098		case PMCLOG_TYPE_PMCATTACH:
1099			PMCSTAT_PRINT_ENTRY(a,"attach","0x%x %d \"%s\"",
1100			    ev.pl_u.pl_t.pl_pmcid,
1101			    ev.pl_u.pl_t.pl_pid,
1102			    ev.pl_u.pl_t.pl_pathname);
1103			break;
1104		case PMCLOG_TYPE_PMCDETACH:
1105			PMCSTAT_PRINT_ENTRY(a,"detach","0x%x %d",
1106			    ev.pl_u.pl_d.pl_pmcid,
1107			    ev.pl_u.pl_d.pl_pid);
1108			break;
1109		case PMCLOG_TYPE_PROCCSW:
1110			PMCSTAT_PRINT_ENTRY(a,"cswval","0x%x %d %jd",
1111			    ev.pl_u.pl_c.pl_pmcid,
1112			    ev.pl_u.pl_c.pl_pid,
1113			    ev.pl_u.pl_c.pl_value);
1114			break;
1115		case PMCLOG_TYPE_PROCEXEC:
1116			PMCSTAT_PRINT_ENTRY(a,"exec","0x%x %d %p \"%s\"",
1117			    ev.pl_u.pl_x.pl_pmcid,
1118			    ev.pl_u.pl_x.pl_pid,
1119			    (void *) ev.pl_u.pl_x.pl_entryaddr,
1120			    ev.pl_u.pl_x.pl_pathname);
1121			break;
1122		case PMCLOG_TYPE_PROCEXIT:
1123			PMCSTAT_PRINT_ENTRY(a,"exitval","0x%x %d %jd",
1124			    ev.pl_u.pl_e.pl_pmcid,
1125			    ev.pl_u.pl_e.pl_pid,
1126			    ev.pl_u.pl_e.pl_value);
1127			break;
1128		case PMCLOG_TYPE_PROCFORK:
1129			PMCSTAT_PRINT_ENTRY(a,"fork","%d %d",
1130			    ev.pl_u.pl_f.pl_oldpid,
1131			    ev.pl_u.pl_f.pl_newpid);
1132			break;
1133		case PMCLOG_TYPE_USERDATA:
1134			PMCSTAT_PRINT_ENTRY(a,"userdata","0x%x",
1135			    ev.pl_u.pl_u.pl_userdata);
1136			break;
1137		case PMCLOG_TYPE_SYSEXIT:
1138			PMCSTAT_PRINT_ENTRY(a,"exit","%d",
1139			    ev.pl_u.pl_se.pl_pid);
1140			break;
1141		default:
1142			fprintf(a->pa_printfile, "unknown %d",
1143			    ev.pl_type);
1144		}
1145	}
1146
1147	if (ev.pl_state == PMCLOG_EOF)
1148		return PMCSTAT_FINISHED;
1149	else if (ev.pl_state ==  PMCLOG_REQUIRE_DATA)
1150		return PMCSTAT_RUNNING;
1151
1152	err(EX_DATAERR, "ERROR: event parsing failed "
1153	    "(record %jd, offset 0x%jx)",
1154	    (uintmax_t) ev.pl_count + 1, ev.pl_offset);
1155	/*NOTREACHED*/
1156}
1157
1158/*
1159 * Process a log file in offline analysis mode.
1160 */
1161
1162void
1163pmcstat_process_log(struct pmcstat_args *a)
1164{
1165
1166	/*
1167	 * If gprof style profiles haven't been asked for, just print the
1168	 * log to the current output file.
1169	 */
1170	if (a->pa_flags & FLAG_DO_PRINT)
1171		pmcstat_print_log(a);
1172	else
1173		/* convert the log to gprof compatible profiles */
1174		pmcstat_convert_log(a);
1175
1176	return;
1177}
1178
1179void
1180pmcstat_initialize_logging(struct pmcstat_args *a)
1181{
1182	int i, isdynamic;
1183	const char *kernpath;
1184	struct pmcstat_image *img;
1185	uintfptr_t minva, maxva;
1186
1187	/* use a convenient format for 'ldd' output */
1188	if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1","%p %x\n",1) != 0)
1189		goto error;
1190
1191	/* Initialize hash tables */
1192	for (i = 0; i < PMCSTAT_NHASH; i++) {
1193		LIST_INIT(&pmcstat_image_hash[i]);
1194		LIST_INIT(&pmcstat_process_hash[i]);
1195		LIST_INIT(&pmcstat_string_hash[i]);
1196	}
1197
1198	/* create a fake 'process' entry for the kernel with pid == -1 */
1199	if ((pmcstat_kernproc = pmcstat_process_lookup((pid_t) -1, 1)) == NULL)
1200		goto error;
1201
1202	if ((kernpath = pmcstat_string_intern(a->pa_kernel)) == NULL)
1203		goto error;
1204
1205	img = pmcstat_image_from_path(kernpath);
1206
1207	pmcstat_image_get_elf_params(img, &minva, &maxva, &isdynamic);
1208	img->pi_type = PMCSTAT_IMAGE_ELF;
1209	img->pi_start = minva;
1210	img->pi_end = maxva;
1211
1212	pmcstat_image_link(pmcstat_kernproc, img, minva, maxva);
1213
1214	return;
1215
1216 error:
1217	err(EX_OSERR, "ERROR: Cannot initialize logging");
1218}
1219
1220void
1221pmcstat_shutdown_logging(void)
1222{
1223	int i;
1224	struct pmcstat_gmonfile *pgf, *pgftmp;
1225	struct pmcstat_image *pi, *pitmp;
1226	struct pmcstat_process *pp, *pptmp;
1227	struct pmcstat_string *ps, *pstmp;
1228
1229	for (i = 0; i < PMCSTAT_NHASH; i++) {
1230		LIST_FOREACH_SAFE(pi, &pmcstat_image_hash[i], pi_next, pitmp) {
1231			/* flush gmon.out data to disk */
1232			LIST_FOREACH_SAFE(pgf, &pi->pi_gmlist, pgf_next,
1233			    pgftmp) {
1234			    pmcstat_gmon_unmap_file(pgf);
1235			    LIST_REMOVE(pgf, pgf_next);
1236			    free(pgf);
1237			}
1238
1239			LIST_REMOVE(pi, pi_next);
1240			free(pi);
1241		}
1242		LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[i], pp_next,
1243		    pptmp) {
1244			LIST_REMOVE(pp, pp_next);
1245			free(pp);
1246		}
1247		LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next,
1248		    pstmp) {
1249			LIST_REMOVE(ps, ps_next);
1250			free(ps);
1251		}
1252	}
1253}
1254