193504Sjake/*-
293504Sjake * Copyright (c) 2002 Jake Burkholder
3129574Srwatson * Copyright (c) 2004 Robert Watson
493504Sjake * All rights reserved.
593504Sjake *
693504Sjake * Redistribution and use in source and binary forms, with or without
793504Sjake * modification, are permitted provided that the following conditions
893504Sjake * are met:
993504Sjake * 1. Redistributions of source code must retain the above copyright
1093504Sjake *    notice, this list of conditions and the following disclaimer.
1193504Sjake * 2. Redistributions in binary form must reproduce the above copyright
1293504Sjake *    notice, this list of conditions and the following disclaimer in the
1393504Sjake *    documentation and/or other materials provided with the distribution.
1493504Sjake *
1593504Sjake * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1693504Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1793504Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1893504Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1993504Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2093504Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2193504Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2293504Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2393504Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2493504Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2593504Sjake * SUCH DAMAGE.
2693504Sjake */
2793504Sjake
2893504Sjake#include <sys/cdefs.h>
2993504Sjake__FBSDID("$FreeBSD$");
3093504Sjake
3193504Sjake#include <sys/types.h>
3293504Sjake#include <sys/ktr.h>
33103791Sjeff#include <sys/mman.h>
34103791Sjeff#include <sys/stat.h>
3593504Sjake
3693504Sjake#include <err.h>
3793504Sjake#include <fcntl.h>
3893504Sjake#include <kvm.h>
3993504Sjake#include <limits.h>
4093504Sjake#include <nlist.h>
4193617Sjake#include <stdint.h>
4293504Sjake#include <stdio.h>
4393504Sjake#include <stdlib.h>
4493504Sjake#include <string.h>
4593504Sjake#include <unistd.h>
4693504Sjake
4793504Sjake#define	SBUFLEN	128
4893504Sjake#define	USAGE \
49217873Sdchagin	"usage: ktrdump [-cfqrtH] [-e execfile] [-i ktrfile] [-m corefile] [-o outfile]\n"
5093504Sjake
5193504Sjakestatic void usage(void);
5293504Sjake
5393504Sjakestatic struct nlist nl[] = {
5493504Sjake	{ "_ktr_version" },
5593504Sjake	{ "_ktr_entries" },
5693504Sjake	{ "_ktr_idx" },
5793504Sjake	{ "_ktr_buf" },
5893504Sjake	{ NULL }
5993504Sjake};
6093504Sjake
6193504Sjakestatic int cflag;
6293504Sjakestatic int eflag;
6393504Sjakestatic int fflag;
6493504Sjakestatic int mflag;
65129559Srwatsonstatic int qflag;
66129574Srwatsonstatic int rflag;
6793504Sjakestatic int tflag;
68103791Sjeffstatic int iflag;
69217873Sdchaginstatic int hflag;
7093504Sjake
7193504Sjakestatic char corefile[PATH_MAX];
7293504Sjakestatic char execfile[PATH_MAX];
7393504Sjake
7493504Sjakestatic char desc[SBUFLEN];
7593504Sjakestatic char errbuf[_POSIX2_LINE_MAX];
7693504Sjakestatic char fbuf[PATH_MAX];
7793504Sjakestatic char obuf[PATH_MAX];
7893504Sjakestatic char sbuf[KTR_PARMS][SBUFLEN];
7993504Sjake
8093504Sjake/*
8193504Sjake * Reads the ktr trace buffer from kernel memory and prints the trace entries.
8293504Sjake */
8393504Sjakeint
8493504Sjakemain(int ac, char **av)
8593504Sjake{
8693504Sjake	u_long parms[KTR_PARMS];
8793504Sjake	struct ktr_entry *buf;
88129574Srwatson	uintmax_t tlast, tnow;
89243046Sjeff	unsigned long bufptr;
90103791Sjeff	struct stat sb;
9193504Sjake	kvm_t *kd;
9293504Sjake	FILE *out;
9393504Sjake	char *p;
9493504Sjake	int version;
9593504Sjake	int entries;
9693504Sjake	int index;
9793504Sjake	int parm;
98103791Sjeff	int in;
9993504Sjake	int c;
100135842Sjulian	int i = 0;
10193504Sjake
10293504Sjake	/*
10393504Sjake	 * Parse commandline arguments.
10493504Sjake	 */
10593504Sjake	out = stdout;
106217873Sdchagin	while ((c = getopt(ac, av, "cfqrtHe:i:m:o:")) != -1)
10793504Sjake		switch (c) {
10893504Sjake		case 'c':
10993504Sjake			cflag = 1;
11093504Sjake			break;
11193504Sjake		case 'e':
112104586Skris			if (strlcpy(execfile, optarg, sizeof(execfile))
113104586Skris			    >= sizeof(execfile))
114104586Skris				errx(1, "%s: File name too long", optarg);
11593504Sjake			eflag = 1;
11693504Sjake			break;
11793504Sjake		case 'f':
11893504Sjake			fflag = 1;
11993504Sjake			break;
120103791Sjeff		case 'i':
121103791Sjeff			iflag = 1;
122103791Sjeff			if ((in = open(optarg, O_RDONLY)) == -1)
123103791Sjeff				err(1, "%s", optarg);
124103791Sjeff			break;
12593504Sjake		case 'm':
126104586Skris			if (strlcpy(corefile, optarg, sizeof(corefile))
127104586Skris			    >= sizeof(corefile))
128104586Skris				errx(1, "%s: File name too long", optarg);
12993504Sjake			mflag = 1;
13093504Sjake			break;
13193504Sjake		case 'o':
13293504Sjake			if ((out = fopen(optarg, "w")) == NULL)
13393504Sjake				err(1, "%s", optarg);
13493504Sjake			break;
135129559Srwatson		case 'q':
136129559Srwatson			qflag++;
137129559Srwatson			break;
138129574Srwatson		case 'r':
139129574Srwatson			rflag = 1;
140129574Srwatson			break;
14193504Sjake		case 't':
14293504Sjake			tflag = 1;
14393504Sjake			break;
144217873Sdchagin		case 'H':
145217873Sdchagin			hflag = 1;
146217873Sdchagin			break;
14793504Sjake		case '?':
14893504Sjake		default:
14993504Sjake			usage();
15093504Sjake		}
15193504Sjake	ac -= optind;
15293504Sjake	av += optind;
15393504Sjake	if (ac != 0)
15493504Sjake		usage();
15593504Sjake
15693504Sjake	/*
15793504Sjake	 * Open our execfile and corefile, resolve needed symbols and read in
15893504Sjake	 * the trace buffer.
15993504Sjake	 */
16093504Sjake	if ((kd = kvm_openfiles(eflag ? execfile : NULL,
16193504Sjake	    mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
16293504Sjake		errx(1, "%s", errbuf);
16393504Sjake	if (kvm_nlist(kd, nl) != 0 ||
16493504Sjake	    kvm_read(kd, nl[0].n_value, &version, sizeof(version)) == -1)
16593504Sjake		errx(1, "%s", kvm_geterr(kd));
16693504Sjake	if (version != KTR_VERSION)
16793504Sjake		errx(1, "ktr version mismatch");
168103791Sjeff	if (iflag) {
169103791Sjeff		if (fstat(in, &sb) == -1)
170103791Sjeff			errx(1, "stat");
171103791Sjeff		entries = sb.st_size / sizeof(*buf);
172103791Sjeff		index = 0;
173103791Sjeff		buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, in, 0);
174103791Sjeff		if (buf == MAP_FAILED)
175103791Sjeff			errx(1, "mmap");
176103791Sjeff	} else {
177103791Sjeff		if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries))
178103791Sjeff		    == -1)
179103791Sjeff			errx(1, "%s", kvm_geterr(kd));
180103791Sjeff		if ((buf = malloc(sizeof(*buf) * entries)) == NULL)
181103791Sjeff			err(1, NULL);
182103791Sjeff		if (kvm_read(kd, nl[2].n_value, &index, sizeof(index)) == -1 ||
183243046Sjeff		    kvm_read(kd, nl[3].n_value, &bufptr,
184243046Sjeff		    sizeof(bufptr)) == -1 ||
185243046Sjeff		    kvm_read(kd, bufptr, buf, sizeof(*buf) * entries) == -1)
186103791Sjeff			errx(1, "%s", kvm_geterr(kd));
187103791Sjeff	}
18893504Sjake
18993504Sjake	/*
19093504Sjake	 * Print a nice header.
19193504Sjake	 */
192129559Srwatson	if (!qflag) {
193129559Srwatson		fprintf(out, "%-6s ", "index");
194129559Srwatson		if (cflag)
195129559Srwatson			fprintf(out, "%-3s ", "cpu");
196129559Srwatson		if (tflag)
197129559Srwatson			fprintf(out, "%-16s ", "timestamp");
198129559Srwatson		if (fflag)
199129559Srwatson			fprintf(out, "%-40s ", "file and line");
200217873Sdchagin		if (hflag)
201217873Sdchagin			fprintf(out, "%-18s ", "tid");
202129559Srwatson		fprintf(out, "%s", "trace");
203129559Srwatson		fprintf(out, "\n");
20493504Sjake
205129559Srwatson		fprintf(out, "------ ");
206129559Srwatson		if (cflag)
207129559Srwatson			fprintf(out, "--- ");
208129559Srwatson		if (tflag)
209129559Srwatson			fprintf(out, "---------------- ");
210129559Srwatson		if (fflag)
211129559Srwatson			fprintf(out,
212129559Srwatson			    "---------------------------------------- ");
213217873Sdchagin		if (hflag)
214217873Sdchagin			fprintf(out, "------------------ ");
215129559Srwatson		fprintf(out, "----- ");
216129559Srwatson		fprintf(out, "\n");
217129559Srwatson	}
21893504Sjake
21993504Sjake	/*
22093504Sjake	 * Now tear through the trace buffer.
22193504Sjake	 */
222103791Sjeff	if (!iflag)
223242378Snp		i = (index - 1) % entries;
224129574Srwatson	tlast = -1;
22593504Sjake	for (;;) {
22693504Sjake		if (buf[i].ktr_desc == NULL)
22793504Sjake			break;
22893504Sjake		if (kvm_read(kd, (u_long)buf[i].ktr_desc, desc,
22993504Sjake		    sizeof(desc)) == -1)
23093504Sjake			errx(1, "%s", kvm_geterr(kd));
23193504Sjake		desc[sizeof(desc) - 1] = '\0';
23293504Sjake		parm = 0;
23393504Sjake		for (p = desc; (c = *p++) != '\0';) {
23493504Sjake			if (c != '%')
23593504Sjake				continue;
236154274Sglebiusnext:			if ((c = *p++) == '\0')
23793504Sjake				break;
23893504Sjake			if (parm == KTR_PARMS)
23993504Sjake				errx(1, "too many parameters");
24093504Sjake			switch (c) {
241154274Sglebius			case '0': case '1': case '2': case '3': case '4':
242154274Sglebius			case '5': case '6': case '7': case '8': case '9':
243154274Sglebius			case '#': case '-': case ' ': case '+': case '\'':
244154274Sglebius			case 'h': case 'l': case 'j': case 't': case 'z':
245154274Sglebius			case 'q': case 'L': case '.':
246154274Sglebius				goto next;
24793504Sjake			case 's':
24893504Sjake				if (kvm_read(kd, (u_long)buf[i].ktr_parms[parm],
24993504Sjake				    sbuf[parm], sizeof(sbuf[parm])) == -1)
25093504Sjake					strcpy(sbuf[parm], "(null)");
25193504Sjake				sbuf[parm][sizeof(sbuf[0]) - 1] = '\0';
25293504Sjake				parms[parm] = (u_long)sbuf[parm];
25393504Sjake				parm++;
25493504Sjake				break;
25593504Sjake			default:
25693504Sjake				parms[parm] = buf[i].ktr_parms[parm];
25793504Sjake				parm++;
25893504Sjake				break;
25993504Sjake			}
26093504Sjake		}
26193504Sjake		fprintf(out, "%6d ", i);
26293504Sjake		if (cflag)
26393504Sjake			fprintf(out, "%3d ", buf[i].ktr_cpu);
264129574Srwatson		if (tflag) {
265129574Srwatson			tnow = (uintmax_t)buf[i].ktr_timestamp;
266129574Srwatson			if (rflag) {
267129574Srwatson				if (tlast == -1)
268129574Srwatson					tlast = tnow;
269153270Snjl				fprintf(out, "%16ju ", !iflag ? tlast - tnow :
270153270Snjl				    tnow - tlast);
271129574Srwatson				tlast = tnow;
272129574Srwatson			} else
273129574Srwatson				fprintf(out, "%16ju ", tnow);
274129574Srwatson		}
27593504Sjake		if (fflag) {
27693504Sjake			if (kvm_read(kd, (u_long)buf[i].ktr_file, fbuf,
27793504Sjake			    sizeof(fbuf)) == -1)
27893504Sjake				strcpy(fbuf, "(null)");
27993504Sjake			snprintf(obuf, sizeof(obuf), "%s:%d", fbuf,
28093504Sjake			    buf[i].ktr_line);
28193504Sjake			fprintf(out, "%-40s ", obuf);
28293504Sjake		}
283217873Sdchagin		if (hflag)
284217873Sdchagin			fprintf(out, "%p ", buf[i].ktr_thread);
28593504Sjake		fprintf(out, desc, parms[0], parms[1], parms[2], parms[3],
28693504Sjake		    parms[4], parms[5]);
28793504Sjake		fprintf(out, "\n");
288103791Sjeff		if (!iflag) {
289103791Sjeff			if (i == index)
290103791Sjeff				break;
291242378Snp			i = (i - 1) % entries;
292103791Sjeff		} else {
293103791Sjeff			if (++i == entries)
294103791Sjeff				break;
295103791Sjeff		}
29693504Sjake	}
29793504Sjake
29893504Sjake	return (0);
29993504Sjake}
30093504Sjake
30193504Sjakestatic void
30293504Sjakeusage(void)
30393504Sjake{
304146466Sru
305146466Sru	fprintf(stderr, USAGE);
306146466Sru	exit(1);
30793504Sjake}
308