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