pmcstat_log.c revision 147708
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 147708 2005-06-30 19:01:26Z 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 *)((const char *) 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 *)((const char *) 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	if ((nbytes = pread(fd, buffer, sizeof(buffer), 0)) < 0)
459		err(EX_OSERR, "ERROR: Cannot read \"%s\"", path);
460
461	(void) close(fd);
462
463	/* check if its an ELF file */
464	if ((unsigned) nbytes >= sizeof(Elf_Ehdr)) {
465		eh = (Elf_Ehdr *) buffer;
466		if (IS_ELF(*eh))
467			return PMCSTAT_IMAGE_ELF;
468	}
469
470	/* Look for an A.OUT header */
471	if ((unsigned) nbytes >= sizeof(struct exec)) {
472		ex = (struct exec *) buffer;
473		if (!N_BADMAG(*ex))
474			return PMCSTAT_IMAGE_AOUT;
475	}
476
477	return PMCSTAT_IMAGE_UNKNOWN;
478}
479
480/*
481 * Increment the bucket in the gmon.out file corresponding to 'pmcid'
482 * and 'pc'.
483 */
484
485static void
486pmcstat_image_increment_bucket(struct pmcstat_pcmap *map, uintfptr_t pc,
487    pmc_id_t pmcid, struct pmcstat_args *a)
488{
489	struct pmcstat_image *image;
490	struct pmcstat_gmonfile *pgf;
491	uintfptr_t bucket;
492	HISTCOUNTER *hc;
493
494	assert(pc >= map->ppm_lowpc && pc < map->ppm_highpc);
495
496	/*
497	 * Find the gmon file corresponding to 'pmcid', creating it if
498	 * needed.
499	 */
500
501	image = map->ppm_image;
502
503	LIST_FOREACH(pgf, &image->pi_gmlist, pgf_next)
504	    if (pgf->pgf_pmcid == pmcid)
505		    break;
506
507	/* If we don't have a gmon.out file for this PMCid, create one */
508	if (pgf == NULL) {
509		if ((pgf = calloc(1, sizeof(*pgf))) == NULL)
510			err(EX_OSERR, "ERROR:");
511
512		pgf->pgf_gmondata = NULL;	/* mark as unmapped */
513		pgf->pgf_name = pmcstat_gmon_create_name(a->pa_samplesdir,
514		    image, pmcid);
515		pgf->pgf_pmcid = pmcid;
516		pgf->pgf_nsamples = (image->pi_end - image->pi_start) /
517		    FUNCTION_ALIGNMENT;	/* see <machine/profile.h> */
518		pgf->pgf_ndatabytes = sizeof(struct gmonhdr) +
519		    pgf->pgf_nsamples * sizeof(HISTCOUNTER);
520
521		pmcstat_gmon_create_file(pgf, image);
522
523		LIST_INSERT_HEAD(&image->pi_gmlist, pgf, pgf_next);
524	}
525
526	/*
527	 * Map the gmon file in if needed.  It may have been mapped
528	 * out under memory pressure.
529	 */
530	if (pgf->pgf_gmondata == NULL)
531		pmcstat_gmon_map_file(pgf);
532
533	bucket = (pc - map->ppm_lowpc) / FUNCTION_ALIGNMENT;
534
535	assert(bucket < pgf->pgf_nsamples);
536
537	hc = (HISTCOUNTER *) ((char *) pgf->pgf_gmondata +
538	    sizeof(struct gmonhdr));
539	hc[bucket]++;
540
541}
542
543/*
544 * Record the fact that PC values from 'lowpc' to 'highpc' come from
545 * image 'image'.
546 */
547
548static void
549pmcstat_image_link(struct pmcstat_process *pp, struct pmcstat_image *image,
550    uintfptr_t lowpc, uintfptr_t highpc)
551{
552	struct pmcstat_pcmap *pcm, *pcmnew;
553
554	if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL)
555		err(EX_OSERR, "ERROR: ");
556
557	pcmnew->ppm_lowpc  = lowpc;
558	pcmnew->ppm_highpc = highpc;
559	pcmnew->ppm_image  = image;
560
561	TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next)
562	    if (pcm->ppm_lowpc < lowpc)
563		    break;
564
565	if (pcm == NULL)
566		TAILQ_INSERT_TAIL(&pp->pp_map, pcmnew, ppm_next);
567	else
568		TAILQ_INSERT_BEFORE(pcm, pcmnew, ppm_next);
569}
570
571/*
572 * Add a {pmcid,name} mapping.
573 */
574
575static void
576pmcstat_pmcid_add(pmc_id_t pmcid, const char *name, struct pmcstat_args *a)
577{
578	struct pmcstat_pmcrecord *pr;
579	struct stat st;
580	char fullpath[PATH_MAX];
581
582	LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
583	    if (pr->pr_pmcid == pmcid) {
584		    pr->pr_pmcname = name;
585		    return;
586	    }
587
588	if ((pr = malloc(sizeof(*pr))) == NULL)
589		err(EX_OSERR, "ERROR: Cannot allocate pmc record");
590
591	pr->pr_pmcid = pmcid;
592	pr->pr_pmcname = name;
593	LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next);
594
595	(void) snprintf(fullpath, sizeof(fullpath), "%s/%s", a->pa_samplesdir,
596	    name);
597
598	/* If the path name exists, it should be a directory */
599	if (stat(fullpath, &st) == 0 && S_ISDIR(st.st_mode))
600		return;
601
602	if (mkdir(fullpath, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0)
603		err(EX_OSERR, "ERROR: Cannot create directory \"%s\"",
604		    fullpath);
605}
606
607/*
608 * Given a pmcid in use, find its human-readable name, or a
609 */
610
611static const char *
612pmcstat_pmcid_to_name(pmc_id_t pmcid)
613{
614	struct pmcstat_pmcrecord *pr;
615	char fullpath[PATH_MAX];
616
617	LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
618	    if (pr->pr_pmcid == pmcid)
619		    return pr->pr_pmcname;
620
621	/* create a default name and add this entry */
622	if ((pr = malloc(sizeof(*pr))) == NULL)
623		err(EX_OSERR, "ERROR: ");
624	pr->pr_pmcid = pmcid;
625
626	(void) snprintf(fullpath, sizeof(fullpath), "%X", (unsigned int) pmcid);
627	pr->pr_pmcname = pmcstat_string_intern(fullpath);
628
629	LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next);
630
631	return pr->pr_pmcname;
632}
633
634/*
635 * Associate an ELF image with a process.  Argument 'path' names the
636 * executable while 'fd' is an already open descriptor to it.
637 */
638
639static void
640pmcstat_process_add_elf_image(struct pmcstat_process *pp, const char *path)
641{
642	int isdynamic;
643	size_t linelen;
644	FILE *rf;
645	char *line;
646	uintfptr_t minva, maxva;
647	uintmax_t libstart;
648	struct pmcstat_image *image;
649	char libpath[PATH_MAX];
650	char command[PATH_MAX + sizeof(PMCSTAT_LDD_COMMAND) + 1];
651
652	minva = ~ (uintfptr_t) 0;
653	maxva = (uintfptr_t) 0;
654	isdynamic = 0;
655
656	if ((image = pmcstat_image_from_path(path)) == NULL)
657		return;
658
659	if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) {
660
661		pmcstat_image_get_elf_params(image, &minva, &maxva,
662		    &isdynamic);
663
664		image->pi_type = PMCSTAT_IMAGE_ELF;
665		image->pi_start = minva;
666		image->pi_end = maxva;
667		image->pi_isdynamic = isdynamic;
668	}
669
670	/* create a map entry for the base executable */
671	pmcstat_image_link(pp, image, minva, maxva);
672
673	if (image->pi_isdynamic) {
674
675		(void) snprintf(command, sizeof(command), "%s %s",
676		    PMCSTAT_LDD_COMMAND, path);
677
678		if ((rf = popen(command, "r")) == NULL)
679			err(EX_OSERR, "ERROR: Cannot create pipe");
680
681		(void) fgetln(rf, &linelen);
682
683		while (!feof(rf) && !ferror(rf)) {
684
685			if ((line = fgetln(rf, &linelen)) == NULL)
686				continue;
687			line[linelen-1] = '\0';
688
689			if (sscanf(line, "%s %jx",
690				libpath, &libstart) != 2)
691				continue;
692
693			image = pmcstat_image_from_path(
694				pmcstat_string_intern(libpath));
695			if (image == NULL)
696				err(EX_OSERR, "ERROR: Cannot process "
697				    "\"%s\"", libpath);
698
699			if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) {
700
701				pmcstat_image_get_elf_params(image,
702				    &minva, &maxva, &isdynamic);
703
704				image->pi_type = PMCSTAT_IMAGE_ELF;
705				image->pi_start = minva;
706				image->pi_end = maxva;
707				image->pi_isdynamic = isdynamic;
708			}
709
710			pmcstat_image_link(pp, image, libstart + image->pi_start,
711			    libstart + image->pi_end);
712		}
713
714		(void) pclose(rf);
715
716	}
717}
718
719/*
720 * Find the process descriptor corresponding to a PID.  If 'allocate'
721 * is zero, we return a NULL if a pid descriptor could not be found or
722 * a process descriptor process.  If 'allocate' is non-zero, then we
723 * will attempt to allocate a fresh process descriptor.  Zombie
724 * process descriptors are only removed if a fresh allocation for the
725 * same PID is requested.
726 */
727
728static struct pmcstat_process *
729pmcstat_process_lookup(pid_t pid, int allocate)
730{
731	uint32_t hash;
732	struct pmcstat_pcmap *ppm, *ppmtmp;
733	struct pmcstat_process *pp, *pptmp;
734
735	hash = (uint32_t) pid & PMCSTAT_HASH_MASK;	/* simplicity wins */
736
737	LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[hash], pp_next, pptmp)
738	    if (pp->pp_pid == pid) {
739		    /* Found a descriptor, check and process zombies */
740		    if (allocate && !pp->pp_isactive) {
741			    /* remove maps */
742			    TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next,
743				ppmtmp) {
744				    TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
745				    free(ppm);
746			    }
747			    /* remove process entry */
748			    LIST_REMOVE(pp, pp_next);
749			    free(pp);
750			    break;
751		    }
752		    return pp;
753	    }
754
755	if (!allocate)
756		return NULL;
757
758	if ((pp = malloc(sizeof(*pp))) == NULL)
759		err(EX_OSERR, "ERROR: Cannot allocate pid descriptor");
760
761	pp->pp_pid = pid;
762	pp->pp_isactive = 1;
763
764	TAILQ_INIT(&pp->pp_map);
765
766	LIST_INSERT_HEAD(&pmcstat_process_hash[hash], pp, pp_next);
767	return pp;
768}
769
770/*
771 * Find the map entry associated with process 'p' at PC value 'pc'.
772 */
773
774static struct pmcstat_pcmap *
775pmcstat_process_find_map(struct pmcstat_process *p, uintfptr_t pc)
776{
777	struct pmcstat_pcmap *ppm;
778
779	TAILQ_FOREACH(ppm, &p->pp_map, ppm_next)
780	    if (pc >= ppm->ppm_lowpc && pc < ppm->ppm_highpc)
781		    return ppm;
782
783	return NULL;
784}
785
786/*
787 * Associate an image and a process.
788 */
789
790static void
791pmcstat_process_new_image(struct pmcstat_process *pp, const char *path)
792{
793	enum pmcstat_image_type filetype;
794	struct pmcstat_image *image;
795
796	if ((image = pmcstat_image_from_path(path)) == NULL)
797		return;
798
799	if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
800		filetype = pmcstat_image_get_type(path);
801	else
802		filetype = image->pi_type;
803
804	switch (filetype) {
805	case PMCSTAT_IMAGE_ELF:
806		pmcstat_process_add_elf_image(pp, path);
807		break;
808
809	case PMCSTAT_IMAGE_AOUT:
810		break;
811
812	default:
813		err(EX_SOFTWARE, "ERROR: Unsupported executable type \"%s\"",
814		    path);
815	}
816}
817
818
819
820/*
821 * Compute a 'hash' value for a string.
822 */
823
824static int
825pmcstat_string_compute_hash(const char *s)
826{
827	int hash;
828
829	for (hash = 0; *s; s++)
830		hash ^= *s;
831
832	return hash & PMCSTAT_HASH_MASK;
833}
834
835/*
836 * Intern a copy of string 's', and return a pointer to it.
837 */
838
839static const char *
840pmcstat_string_intern(const char *s)
841{
842	struct pmcstat_string *ps;
843	int hash, len;
844
845	hash = pmcstat_string_compute_hash(s);
846	len  = strlen(s);
847
848	if ((ps = pmcstat_string_lookup(s)) != NULL)
849		return ps->ps_string;
850
851	if ((ps = malloc(sizeof(*ps))) == NULL)
852		err(EX_OSERR, "ERROR: Could not intern string");
853	ps->ps_len = len;
854	ps->ps_hash = hash;
855	ps->ps_string = strdup(s);
856	LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next);
857	return ps->ps_string;
858}
859
860static struct pmcstat_string *
861pmcstat_string_lookup(const char *s)
862{
863	struct pmcstat_string *ps;
864	int hash, len;
865
866	hash = pmcstat_string_compute_hash(s);
867	len = strlen(s);
868
869	LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next)
870	    if (ps->ps_len == len && ps->ps_hash == hash &&
871		strcmp(ps->ps_string, s) == 0)
872		    return ps;
873	return NULL;
874}
875
876/*
877 * Public Interfaces.
878 */
879
880/*
881 * Close a logfile, after first flushing all in-module queued data.
882 */
883
884int
885pmcstat_close_log(struct pmcstat_args *a)
886{
887	if (pmc_flush_logfile() < 0 ||
888	    pmc_configure_logfile(-1) < 0)
889		err(EX_OSERR, "ERROR: logging failed");
890	a->pa_flags &= ~(FLAG_HAS_OUTPUT_LOGFILE | FLAG_HAS_PIPE);
891	return a->pa_flags & FLAG_HAS_PIPE ? PMCSTAT_EXITING :
892	    PMCSTAT_FINISHED;
893}
894
895
896int
897pmcstat_convert_log(struct pmcstat_args *a)
898{
899	uintfptr_t pc;
900	struct pmcstat_process *pp, *ppnew;
901	struct pmcstat_pcmap *ppm, *ppmtmp;
902	struct pmclog_ev ev;
903	const char *image_path;
904
905	while (pmclog_read(a->pa_logparser, &ev) == 0) {
906		assert(ev.pl_state == PMCLOG_OK);
907
908		switch (ev.pl_type) {
909		case PMCLOG_TYPE_MAPPINGCHANGE:
910			/*
911			 * Introduce an address range mapping for a
912			 * process.
913			 */
914			break;
915
916		case PMCLOG_TYPE_PCSAMPLE:
917
918			/*
919			 * We bring in the gmon file for the image
920			 * currently associated with the PMC & pid
921			 * pair and increment the appropriate entry
922			 * bin inside this.
923			 */
924			pc = ev.pl_u.pl_s.pl_pc;
925			pp = pmcstat_process_lookup(ev.pl_u.pl_s.pl_pid, 1);
926			if ((ppm = pmcstat_process_find_map(pp, pc)) == NULL &&
927			    (ppm = pmcstat_process_find_map(pmcstat_kernproc,
928				pc)) == NULL) {
929				printf("!%d unknown %jx\n", pp->pp_pid,
930				    (uintmax_t) pc);
931				break; /* unknown process,offset pair */
932			}
933
934			pmcstat_image_increment_bucket(ppm, pc,
935			    ev.pl_u.pl_s.pl_pmcid, a);
936
937			break;
938
939		case PMCLOG_TYPE_PMCALLOCATE:
940			/*
941			 * Record the association pmc id between this
942			 * PMC and its name.
943			 */
944			pmcstat_pmcid_add(ev.pl_u.pl_a.pl_pmcid,
945			    pmcstat_string_intern(ev.pl_u.pl_a.pl_evname), a);
946			break;
947
948		case PMCLOG_TYPE_PROCEXEC:
949
950			/*
951			 * Change the executable image associated with
952			 * a process.
953			 */
954			pp = pmcstat_process_lookup(ev.pl_u.pl_x.pl_pid, 1);
955
956			/* delete the current process map */
957			TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
958				TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
959				free(ppm);
960			}
961
962			/* locate the descriptor for the new 'base' image */
963			image_path = pmcstat_string_intern(
964				ev.pl_u.pl_x.pl_pathname);
965
966			/* link to the new image */
967			pmcstat_process_new_image(pp, image_path);
968			break;
969
970		case PMCLOG_TYPE_PROCEXIT:
971
972			/*
973			 * Due to the way the log is generated, the
974			 * last few samples corresponding to a process
975			 * may appear in the log after the process
976			 * exit event is recorded.  Thus we keep the
977			 * process' descriptor and associated data
978			 * structures around, but mark the process as
979			 * having exited.
980			 */
981			pp = pmcstat_process_lookup(ev.pl_u.pl_e.pl_pid, 0);
982			if (pp == NULL)
983				break;
984			pp->pp_isactive = 0;	/* make a zombie */
985			break;
986
987		case PMCLOG_TYPE_SYSEXIT:
988			pp = pmcstat_process_lookup(ev.pl_u.pl_se.pl_pid, 0);
989			if (pp == NULL)
990				break;
991			pp->pp_isactive = 0;	/* make a zombie */
992			break;
993
994		case PMCLOG_TYPE_PROCFORK:
995
996			/*
997			 * If we had been tracking 'oldpid', then clone
998			 * its pid descriptor.
999			 */
1000			pp = pmcstat_process_lookup(ev.pl_u.pl_f.pl_oldpid, 0);
1001			if (pp == NULL)
1002				break;
1003
1004			ppnew =
1005			    pmcstat_process_lookup(ev.pl_u.pl_f.pl_newpid, 1);
1006
1007			/* copy the old process' address maps */
1008			TAILQ_FOREACH(ppm, &pp->pp_map, ppm_next)
1009			    pmcstat_image_link(ppnew, ppm->ppm_image,
1010				ppm->ppm_lowpc, ppm->ppm_highpc);
1011			break;
1012
1013		default:	/* other types of entries are not relevant */
1014			break;
1015		}
1016	}
1017
1018	if (ev.pl_state == PMCLOG_EOF)
1019		return PMCSTAT_FINISHED;
1020	else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
1021		return PMCSTAT_RUNNING;
1022
1023	err(EX_DATAERR, "ERROR: event parsing failed (record %jd, "
1024	    "offset 0x%jx)", (uintmax_t) ev.pl_count + 1, ev.pl_offset);
1025}
1026
1027
1028/*
1029 * Open a log file, for reading or writing.
1030 *
1031 * The function returns the fd of a successfully opened log or -1 in
1032 * case of failure.
1033 */
1034
1035int
1036pmcstat_open(const char *path, int mode)
1037{
1038	int fd;
1039
1040	/*
1041	 * If 'path' is "-" then open one of stdin or stdout depending
1042	 * on the value of 'mode'.  Otherwise, treat 'path' as a file
1043	 * name and open that.
1044	 */
1045	if (path[0] == '-' && path[1] == '\0')
1046		fd = (mode == PMCSTAT_OPEN_FOR_READ) ? 0 : 1;
1047	else
1048		fd = open(path, mode == PMCSTAT_OPEN_FOR_READ ?
1049		    O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC),
1050		    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
1051
1052	return fd;
1053}
1054
1055/*
1056 * Print log entries as text.
1057 */
1058
1059int
1060pmcstat_print_log(struct pmcstat_args *a)
1061{
1062	struct pmclog_ev ev;
1063
1064	while (pmclog_read(a->pa_logparser, &ev) == 0) {
1065		assert(ev.pl_state == PMCLOG_OK);
1066		switch (ev.pl_type) {
1067		case PMCLOG_TYPE_CLOSELOG:
1068			PMCSTAT_PRINT_ENTRY(a,"closelog",);
1069			break;
1070		case PMCLOG_TYPE_DROPNOTIFY:
1071			PMCSTAT_PRINT_ENTRY(a,"drop",);
1072			break;
1073		case PMCLOG_TYPE_INITIALIZE:
1074			PMCSTAT_PRINT_ENTRY(a,"initlog","0x%x \"%s\"",
1075			    ev.pl_u.pl_i.pl_version,
1076			    pmc_name_of_cputype(ev.pl_u.pl_i.pl_arch));
1077			break;
1078		case PMCLOG_TYPE_MAPPINGCHANGE:
1079			PMCSTAT_PRINT_ENTRY(a,"mapping","%s %d %p %p \"%s\"",
1080			    ev.pl_u.pl_m.pl_type == PMCLOG_MAPPING_INSERT ?
1081			    	"insert" : "delete",
1082			    ev.pl_u.pl_m.pl_pid,
1083			    (void *) ev.pl_u.pl_m.pl_start,
1084			    (void *) ev.pl_u.pl_m.pl_end,
1085			    ev.pl_u.pl_m.pl_pathname);
1086			break;
1087		case PMCLOG_TYPE_PCSAMPLE:
1088			PMCSTAT_PRINT_ENTRY(a,"sample","0x%x %d %p %c",
1089			    ev.pl_u.pl_s.pl_pmcid,
1090			    ev.pl_u.pl_s.pl_pid,
1091			    (void *) ev.pl_u.pl_s.pl_pc,
1092			    ev.pl_u.pl_s.pl_usermode ? 'u' : 's');
1093			break;
1094		case PMCLOG_TYPE_PMCALLOCATE:
1095			PMCSTAT_PRINT_ENTRY(a,"allocate","0x%x \"%s\" 0x%x",
1096			    ev.pl_u.pl_a.pl_pmcid,
1097			    ev.pl_u.pl_a.pl_evname,
1098			    ev.pl_u.pl_a.pl_flags);
1099			break;
1100		case PMCLOG_TYPE_PMCATTACH:
1101			PMCSTAT_PRINT_ENTRY(a,"attach","0x%x %d \"%s\"",
1102			    ev.pl_u.pl_t.pl_pmcid,
1103			    ev.pl_u.pl_t.pl_pid,
1104			    ev.pl_u.pl_t.pl_pathname);
1105			break;
1106		case PMCLOG_TYPE_PMCDETACH:
1107			PMCSTAT_PRINT_ENTRY(a,"detach","0x%x %d",
1108			    ev.pl_u.pl_d.pl_pmcid,
1109			    ev.pl_u.pl_d.pl_pid);
1110			break;
1111		case PMCLOG_TYPE_PROCCSW:
1112			PMCSTAT_PRINT_ENTRY(a,"cswval","0x%x %d %jd",
1113			    ev.pl_u.pl_c.pl_pmcid,
1114			    ev.pl_u.pl_c.pl_pid,
1115			    ev.pl_u.pl_c.pl_value);
1116			break;
1117		case PMCLOG_TYPE_PROCEXEC:
1118			PMCSTAT_PRINT_ENTRY(a,"exec","0x%x %d %p \"%s\"",
1119			    ev.pl_u.pl_x.pl_pmcid,
1120			    ev.pl_u.pl_x.pl_pid,
1121			    (void *) ev.pl_u.pl_x.pl_entryaddr,
1122			    ev.pl_u.pl_x.pl_pathname);
1123			break;
1124		case PMCLOG_TYPE_PROCEXIT:
1125			PMCSTAT_PRINT_ENTRY(a,"exitval","0x%x %d %jd",
1126			    ev.pl_u.pl_e.pl_pmcid,
1127			    ev.pl_u.pl_e.pl_pid,
1128			    ev.pl_u.pl_e.pl_value);
1129			break;
1130		case PMCLOG_TYPE_PROCFORK:
1131			PMCSTAT_PRINT_ENTRY(a,"fork","%d %d",
1132			    ev.pl_u.pl_f.pl_oldpid,
1133			    ev.pl_u.pl_f.pl_newpid);
1134			break;
1135		case PMCLOG_TYPE_USERDATA:
1136			PMCSTAT_PRINT_ENTRY(a,"userdata","0x%x",
1137			    ev.pl_u.pl_u.pl_userdata);
1138			break;
1139		case PMCLOG_TYPE_SYSEXIT:
1140			PMCSTAT_PRINT_ENTRY(a,"exit","%d",
1141			    ev.pl_u.pl_se.pl_pid);
1142			break;
1143		default:
1144			fprintf(a->pa_printfile, "unknown %d",
1145			    ev.pl_type);
1146		}
1147	}
1148
1149	if (ev.pl_state == PMCLOG_EOF)
1150		return PMCSTAT_FINISHED;
1151	else if (ev.pl_state ==  PMCLOG_REQUIRE_DATA)
1152		return PMCSTAT_RUNNING;
1153
1154	err(EX_DATAERR, "ERROR: event parsing failed "
1155	    "(record %jd, offset 0x%jx)",
1156	    (uintmax_t) ev.pl_count + 1, ev.pl_offset);
1157	/*NOTREACHED*/
1158}
1159
1160/*
1161 * Process a log file in offline analysis mode.
1162 */
1163
1164void
1165pmcstat_process_log(struct pmcstat_args *a)
1166{
1167
1168	/*
1169	 * If gprof style profiles haven't been asked for, just print the
1170	 * log to the current output file.
1171	 */
1172	if (a->pa_flags & FLAG_DO_PRINT)
1173		pmcstat_print_log(a);
1174	else
1175		/* convert the log to gprof compatible profiles */
1176		pmcstat_convert_log(a);
1177
1178	return;
1179}
1180
1181void
1182pmcstat_initialize_logging(struct pmcstat_args *a)
1183{
1184	int i, isdynamic;
1185	const char *kernpath;
1186	struct pmcstat_image *img;
1187	uintfptr_t minva, maxva;
1188
1189	/* use a convenient format for 'ldd' output */
1190	if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1","%p %x\n",1) != 0)
1191		goto error;
1192
1193	/* Initialize hash tables */
1194	for (i = 0; i < PMCSTAT_NHASH; i++) {
1195		LIST_INIT(&pmcstat_image_hash[i]);
1196		LIST_INIT(&pmcstat_process_hash[i]);
1197		LIST_INIT(&pmcstat_string_hash[i]);
1198	}
1199
1200	/* create a fake 'process' entry for the kernel with pid == -1 */
1201	if ((pmcstat_kernproc = pmcstat_process_lookup((pid_t) -1, 1)) == NULL)
1202		goto error;
1203
1204	if ((kernpath = pmcstat_string_intern(a->pa_kernel)) == NULL)
1205		goto error;
1206
1207	img = pmcstat_image_from_path(kernpath);
1208
1209	pmcstat_image_get_elf_params(img, &minva, &maxva, &isdynamic);
1210	img->pi_type = PMCSTAT_IMAGE_ELF;
1211	img->pi_start = minva;
1212	img->pi_end = maxva;
1213
1214	pmcstat_image_link(pmcstat_kernproc, img, minva, maxva);
1215
1216	return;
1217
1218 error:
1219	err(EX_OSERR, "ERROR: Cannot initialize logging");
1220}
1221
1222void
1223pmcstat_shutdown_logging(void)
1224{
1225	int i;
1226	struct pmcstat_gmonfile *pgf, *pgftmp;
1227	struct pmcstat_image *pi, *pitmp;
1228	struct pmcstat_process *pp, *pptmp;
1229	struct pmcstat_string *ps, *pstmp;
1230
1231	for (i = 0; i < PMCSTAT_NHASH; i++) {
1232		LIST_FOREACH_SAFE(pi, &pmcstat_image_hash[i], pi_next, pitmp) {
1233			/* flush gmon.out data to disk */
1234			LIST_FOREACH_SAFE(pgf, &pi->pi_gmlist, pgf_next,
1235			    pgftmp) {
1236			    pmcstat_gmon_unmap_file(pgf);
1237			    LIST_REMOVE(pgf, pgf_next);
1238			    free(pgf);
1239			}
1240
1241			LIST_REMOVE(pi, pi_next);
1242			free(pi);
1243		}
1244		LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[i], pp_next,
1245		    pptmp) {
1246			LIST_REMOVE(pp, pp_next);
1247			free(pp);
1248		}
1249		LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next,
1250		    pstmp) {
1251			LIST_REMOVE(ps, ps_next);
1252			free(ps);
1253		}
1254	}
1255}
1256