ktrdump.c revision 103791
193504Sjake/*-
293504Sjake * Copyright (c) 2002 Jake Burkholder
393504Sjake * All rights reserved.
493504Sjake *
593504Sjake * Redistribution and use in source and binary forms, with or without
693504Sjake * modification, are permitted provided that the following conditions
793504Sjake * are met:
893504Sjake * 1. Redistributions of source code must retain the above copyright
993504Sjake *    notice, this list of conditions and the following disclaimer.
1093504Sjake * 2. Redistributions in binary form must reproduce the above copyright
1193504Sjake *    notice, this list of conditions and the following disclaimer in the
1293504Sjake *    documentation and/or other materials provided with the distribution.
1393504Sjake *
1493504Sjake * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1593504Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1693504Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1793504Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1893504Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1993504Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2093504Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2193504Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2293504Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2393504Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2493504Sjake * SUCH DAMAGE.
2593504Sjake */
2693504Sjake
2793504Sjake#include <sys/cdefs.h>
2893504Sjake__FBSDID("$FreeBSD: head/usr.bin/ktrdump/ktrdump.c 103791 2002-09-22 07:21:28Z jeff $");
2993504Sjake
3093504Sjake#include <sys/types.h>
3193504Sjake#include <sys/ktr.h>
32103791Sjeff#include <sys/mman.h>
33103791Sjeff#include <sys/stat.h>
3493504Sjake
3593504Sjake#include <err.h>
3693504Sjake#include <fcntl.h>
3793504Sjake#include <kvm.h>
3893504Sjake#include <limits.h>
3993504Sjake#include <nlist.h>
4093617Sjake#include <stdint.h>
4193504Sjake#include <stdio.h>
4293504Sjake#include <stdlib.h>
4393504Sjake#include <string.h>
4493504Sjake#include <unistd.h>
4593504Sjake
4693504Sjake#define	SBUFLEN	128
4793504Sjake#define	USAGE \
48103791Sjeff	"usage: ktrdump [-c] [-f] [-t] [-e execfile] [-i ktrfile ] [-m corefile] [-o outfile]"
4993504Sjake
5093504Sjakeextern char *optarg;
5193504Sjakeextern int optind;
5293504Sjake
5393504Sjakestatic void usage(void);
5493504Sjake
5593504Sjakestatic struct nlist nl[] = {
5693504Sjake	{ "_ktr_version" },
5793504Sjake	{ "_ktr_entries" },
5893504Sjake	{ "_ktr_idx" },
5993504Sjake	{ "_ktr_buf" },
6093504Sjake	{ NULL }
6193504Sjake};
6293504Sjake
6393504Sjakestatic int cflag;
6493504Sjakestatic int eflag;
6593504Sjakestatic int fflag;
6693504Sjakestatic int mflag;
6793504Sjakestatic int tflag;
68103791Sjeffstatic int iflag;
6993504Sjake
7093504Sjakestatic char corefile[PATH_MAX];
7193504Sjakestatic char execfile[PATH_MAX];
7293504Sjake
7393504Sjakestatic char desc[SBUFLEN];
7493504Sjakestatic char errbuf[_POSIX2_LINE_MAX];
7593504Sjakestatic char fbuf[PATH_MAX];
7693504Sjakestatic char obuf[PATH_MAX];
7793504Sjakestatic char sbuf[KTR_PARMS][SBUFLEN];
7893504Sjake
7993504Sjake/*
8093504Sjake * Reads the ktr trace buffer from kernel memory and prints the trace entries.
8193504Sjake */
8293504Sjakeint
8393504Sjakemain(int ac, char **av)
8493504Sjake{
8593504Sjake	u_long parms[KTR_PARMS];
8693504Sjake	struct ktr_entry *buf;
87103791Sjeff	struct stat sb;
8893504Sjake	kvm_t *kd;
8993504Sjake	FILE *out;
9093504Sjake	char *p;
9193504Sjake	int version;
9293504Sjake	int entries;
9393504Sjake	int index;
9493504Sjake	int parm;
95103791Sjeff	int in;
9693504Sjake	int c;
9793504Sjake	int i;
9893504Sjake	int n;
9993504Sjake
10093504Sjake	/*
10193504Sjake	 * Parse commandline arguments.
10293504Sjake	 */
10393504Sjake	out = stdout;
104103791Sjeff	while ((c = getopt(ac, av, "cfte:i:m:o:")) != -1)
10593504Sjake		switch (c) {
10693504Sjake		case 'c':
10793504Sjake			cflag = 1;
10893504Sjake			break;
10993504Sjake		case 'e':
11093617Sjake			strcpy(execfile, optarg);
11193504Sjake			eflag = 1;
11293504Sjake			break;
11393504Sjake		case 'f':
11493504Sjake			fflag = 1;
11593504Sjake			break;
116103791Sjeff		case 'i':
117103791Sjeff			iflag = 1;
118103791Sjeff			if ((in = open(optarg, O_RDONLY)) == -1)
119103791Sjeff				err(1, "%s", optarg);
120103791Sjeff			break;
12193504Sjake		case 'm':
12293617Sjake			strcpy(corefile, optarg);
12393504Sjake			mflag = 1;
12493504Sjake			break;
12593504Sjake		case 'o':
12693504Sjake			if ((out = fopen(optarg, "w")) == NULL)
12793504Sjake				err(1, "%s", optarg);
12893504Sjake			break;
12993504Sjake		case 't':
13093504Sjake			tflag = 1;
13193504Sjake			break;
13293504Sjake		case '?':
13393504Sjake		default:
13493504Sjake			usage();
13593504Sjake		}
13693504Sjake	ac -= optind;
13793504Sjake	av += optind;
13893504Sjake	if (ac != 0)
13993504Sjake		usage();
14093504Sjake
14193504Sjake	/*
14293504Sjake	 * Open our execfile and corefile, resolve needed symbols and read in
14393504Sjake	 * the trace buffer.
14493504Sjake	 */
14593504Sjake	if ((kd = kvm_openfiles(eflag ? execfile : NULL,
14693504Sjake	    mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
14793504Sjake		errx(1, "%s", errbuf);
14893504Sjake	if (kvm_nlist(kd, nl) != 0 ||
14993504Sjake	    kvm_read(kd, nl[0].n_value, &version, sizeof(version)) == -1)
15093504Sjake		errx(1, "%s", kvm_geterr(kd));
15193504Sjake	if (version != KTR_VERSION)
15293504Sjake		errx(1, "ktr version mismatch");
153103791Sjeff	if (iflag) {
154103791Sjeff		if (fstat(in, &sb) == -1)
155103791Sjeff			errx(1, "stat");
156103791Sjeff		entries = sb.st_size / sizeof(*buf);
157103791Sjeff		index = 0;
158103791Sjeff		buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, in, 0);
159103791Sjeff		if (buf == MAP_FAILED)
160103791Sjeff			errx(1, "mmap");
161103791Sjeff	} else {
162103791Sjeff		if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries))
163103791Sjeff		    == -1)
164103791Sjeff			errx(1, "%s", kvm_geterr(kd));
165103791Sjeff		if ((buf = malloc(sizeof(*buf) * entries)) == NULL)
166103791Sjeff			err(1, NULL);
167103791Sjeff		if (kvm_read(kd, nl[2].n_value, &index, sizeof(index)) == -1 ||
168103791Sjeff		    kvm_read(kd, nl[3].n_value, buf, sizeof(*buf) * entries)
169103791Sjeff		    == -1)
170103791Sjeff			errx(1, "%s", kvm_geterr(kd));
171103791Sjeff	}
17293504Sjake
17393504Sjake	/*
17493504Sjake	 * Print a nice header.
17593504Sjake	 */
17693504Sjake	fprintf(out, "%-6s ", "index");
17793504Sjake	if (cflag)
17893504Sjake		fprintf(out, "%-3s ", "cpu");
17993504Sjake	if (tflag)
18093504Sjake		fprintf(out, "%-16s ", "timestamp");
18193504Sjake	if (fflag)
18293504Sjake		fprintf(out, "%-32s ", "file and line");
18393504Sjake	fprintf(out, "%s", "trace");
18493504Sjake	fprintf(out, "\n");
18593504Sjake
18693504Sjake	fprintf(out, "------ ");
18793504Sjake	if (cflag)
18893504Sjake		fprintf(out, "--- ");
18993504Sjake	if (tflag)
19093504Sjake		fprintf(out, "---------------- ");
19193504Sjake	if (fflag)
19293504Sjake		fprintf(out, "---------------------------------------- ");
19393504Sjake	fprintf(out, "----- ");
19493504Sjake	fprintf(out, "\n");
19593504Sjake
19693504Sjake	/*
19793504Sjake	 * Now tear through the trace buffer.
19893504Sjake	 */
199103791Sjeff	if (!iflag)
200103791Sjeff		i = (index - 1) & (entries - 1);
20193504Sjake	for (;;) {
20293504Sjake		if (buf[i].ktr_desc == NULL)
20393504Sjake			break;
20493504Sjake		if (kvm_read(kd, (u_long)buf[i].ktr_desc, desc,
20593504Sjake		    sizeof(desc)) == -1)
20693504Sjake			errx(1, "%s", kvm_geterr(kd));
20793504Sjake		desc[sizeof(desc) - 1] = '\0';
20893504Sjake		parm = 0;
20993504Sjake		for (p = desc; (c = *p++) != '\0';) {
21093504Sjake			if (c != '%')
21193504Sjake				continue;
21293504Sjake			if ((c = *p++) == '\0')
21393504Sjake				break;
21493504Sjake			if (parm == KTR_PARMS)
21593504Sjake				errx(1, "too many parameters");
21693504Sjake			switch (c) {
21793504Sjake			case 's':
21893504Sjake				if (kvm_read(kd, (u_long)buf[i].ktr_parms[parm],
21993504Sjake				    sbuf[parm], sizeof(sbuf[parm])) == -1)
22093504Sjake					strcpy(sbuf[parm], "(null)");
22193504Sjake				sbuf[parm][sizeof(sbuf[0]) - 1] = '\0';
22293504Sjake				parms[parm] = (u_long)sbuf[parm];
22393504Sjake				parm++;
22493504Sjake				break;
22593504Sjake			default:
22693504Sjake				parms[parm] = buf[i].ktr_parms[parm];
22793504Sjake				parm++;
22893504Sjake				break;
22993504Sjake			}
23093504Sjake		}
23193504Sjake		fprintf(out, "%6d ", i);
23293504Sjake		if (cflag)
23393504Sjake			fprintf(out, "%3d ", buf[i].ktr_cpu);
23493504Sjake		if (tflag)
23593504Sjake			fprintf(out, "%16ju ",
23693504Sjake			    (uintmax_t)buf[i].ktr_timestamp);
23793504Sjake		if (fflag) {
23893504Sjake			if (kvm_read(kd, (u_long)buf[i].ktr_file, fbuf,
23993504Sjake			    sizeof(fbuf)) == -1)
24093504Sjake				strcpy(fbuf, "(null)");
24193504Sjake			snprintf(obuf, sizeof(obuf), "%s:%d", fbuf,
24293504Sjake			    buf[i].ktr_line);
24393504Sjake			fprintf(out, "%-40s ", obuf);
24493504Sjake		}
24593504Sjake		fprintf(out, desc, parms[0], parms[1], parms[2], parms[3],
24693504Sjake		    parms[4], parms[5]);
24793504Sjake		fprintf(out, "\n");
248103791Sjeff		if (!iflag) {
249103791Sjeff			if (i == index)
250103791Sjeff				break;
251103791Sjeff			i = (i - 1) & (entries - 1);
252103791Sjeff		} else {
253103791Sjeff			if (++i == entries)
254103791Sjeff				break;
255103791Sjeff		}
25693504Sjake	}
25793504Sjake
25893504Sjake	return (0);
25993504Sjake}
26093504Sjake
26193504Sjakestatic void
26293504Sjakeusage(void)
26393504Sjake{
26493504Sjake	errx(1, USAGE);
26593504Sjake}
266