ktrdump.c revision 135842
1/*-
2 * Copyright (c) 2002 Jake Burkholder
3 * Copyright (c) 2004 Robert Watson
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/usr.bin/ktrdump/ktrdump.c 135842 2004-09-27 05:56:57Z julian $");
30
31#include <sys/types.h>
32#include <sys/ktr.h>
33#include <sys/mman.h>
34#include <sys/stat.h>
35
36#include <err.h>
37#include <fcntl.h>
38#include <kvm.h>
39#include <limits.h>
40#include <nlist.h>
41#include <stdint.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
47#define	SBUFLEN	128
48#define	USAGE \
49	"usage: ktrdump [-c] [-f] [-q] [-r] [-t] [-e execfile] [-i ktrfile ] [-m corefile] [-o outfile]"
50
51extern char *optarg;
52extern int optind;
53
54static void usage(void);
55
56static struct nlist nl[] = {
57	{ "_ktr_version" },
58	{ "_ktr_entries" },
59	{ "_ktr_idx" },
60	{ "_ktr_buf" },
61	{ NULL }
62};
63
64static int cflag;
65static int eflag;
66static int fflag;
67static int mflag;
68static int qflag;
69static int rflag;
70static int tflag;
71static int iflag;
72
73static char corefile[PATH_MAX];
74static char execfile[PATH_MAX];
75
76static char desc[SBUFLEN];
77static char errbuf[_POSIX2_LINE_MAX];
78static char fbuf[PATH_MAX];
79static char obuf[PATH_MAX];
80static char sbuf[KTR_PARMS][SBUFLEN];
81
82/*
83 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
84 */
85int
86main(int ac, char **av)
87{
88	u_long parms[KTR_PARMS];
89	struct ktr_entry *buf;
90	uintmax_t tlast, tnow;
91	struct stat sb;
92	kvm_t *kd;
93	FILE *out;
94	char *p;
95	int version;
96	int entries;
97	int index;
98	int parm;
99	int in;
100	int c;
101	int i = 0;
102
103	/*
104	 * Parse commandline arguments.
105	 */
106	out = stdout;
107	while ((c = getopt(ac, av, "cfqrte:i:m:o:")) != -1)
108		switch (c) {
109		case 'c':
110			cflag = 1;
111			break;
112		case 'e':
113			if (strlcpy(execfile, optarg, sizeof(execfile))
114			    >= sizeof(execfile))
115				errx(1, "%s: File name too long", optarg);
116			eflag = 1;
117			break;
118		case 'f':
119			fflag = 1;
120			break;
121		case 'i':
122			iflag = 1;
123			if ((in = open(optarg, O_RDONLY)) == -1)
124				err(1, "%s", optarg);
125			break;
126		case 'm':
127			if (strlcpy(corefile, optarg, sizeof(corefile))
128			    >= sizeof(corefile))
129				errx(1, "%s: File name too long", optarg);
130			mflag = 1;
131			break;
132		case 'o':
133			if ((out = fopen(optarg, "w")) == NULL)
134				err(1, "%s", optarg);
135			break;
136		case 'q':
137			qflag++;
138			break;
139		case 'r':
140			rflag = 1;
141			break;
142		case 't':
143			tflag = 1;
144			break;
145		case '?':
146		default:
147			usage();
148		}
149	ac -= optind;
150	av += optind;
151	if (ac != 0)
152		usage();
153
154	/*
155	 * Open our execfile and corefile, resolve needed symbols and read in
156	 * the trace buffer.
157	 */
158	if ((kd = kvm_openfiles(eflag ? execfile : NULL,
159	    mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
160		errx(1, "%s", errbuf);
161	if (kvm_nlist(kd, nl) != 0 ||
162	    kvm_read(kd, nl[0].n_value, &version, sizeof(version)) == -1)
163		errx(1, "%s", kvm_geterr(kd));
164	if (version != KTR_VERSION)
165		errx(1, "ktr version mismatch");
166	if (iflag) {
167		if (fstat(in, &sb) == -1)
168			errx(1, "stat");
169		entries = sb.st_size / sizeof(*buf);
170		index = 0;
171		buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, in, 0);
172		if (buf == MAP_FAILED)
173			errx(1, "mmap");
174	} else {
175		if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries))
176		    == -1)
177			errx(1, "%s", kvm_geterr(kd));
178		if ((buf = malloc(sizeof(*buf) * entries)) == NULL)
179			err(1, NULL);
180		if (kvm_read(kd, nl[2].n_value, &index, sizeof(index)) == -1 ||
181		    kvm_read(kd, nl[3].n_value, buf, sizeof(*buf) * entries)
182		    == -1)
183			errx(1, "%s", kvm_geterr(kd));
184	}
185
186	/*
187	 * Print a nice header.
188	 */
189	if (!qflag) {
190		fprintf(out, "%-6s ", "index");
191		if (cflag)
192			fprintf(out, "%-3s ", "cpu");
193		if (tflag)
194			fprintf(out, "%-16s ", "timestamp");
195		if (fflag)
196			fprintf(out, "%-40s ", "file and line");
197		fprintf(out, "%s", "trace");
198		fprintf(out, "\n");
199
200		fprintf(out, "------ ");
201		if (cflag)
202			fprintf(out, "--- ");
203		if (tflag)
204			fprintf(out, "---------------- ");
205		if (fflag)
206			fprintf(out,
207			    "---------------------------------------- ");
208		fprintf(out, "----- ");
209		fprintf(out, "\n");
210	}
211
212	/*
213	 * Now tear through the trace buffer.
214	 */
215	if (!iflag)
216		i = (index - 1) & (entries - 1);
217	tlast = -1;
218	for (;;) {
219		if (buf[i].ktr_desc == NULL)
220			break;
221		if (kvm_read(kd, (u_long)buf[i].ktr_desc, desc,
222		    sizeof(desc)) == -1)
223			errx(1, "%s", kvm_geterr(kd));
224		desc[sizeof(desc) - 1] = '\0';
225		parm = 0;
226		for (p = desc; (c = *p++) != '\0';) {
227			if (c != '%')
228				continue;
229			if ((c = *p++) == '\0')
230				break;
231			if (parm == KTR_PARMS)
232				errx(1, "too many parameters");
233			switch (c) {
234			case 's':
235				if (kvm_read(kd, (u_long)buf[i].ktr_parms[parm],
236				    sbuf[parm], sizeof(sbuf[parm])) == -1)
237					strcpy(sbuf[parm], "(null)");
238				sbuf[parm][sizeof(sbuf[0]) - 1] = '\0';
239				parms[parm] = (u_long)sbuf[parm];
240				parm++;
241				break;
242			default:
243				parms[parm] = buf[i].ktr_parms[parm];
244				parm++;
245				break;
246			}
247		}
248		fprintf(out, "%6d ", i);
249		if (cflag)
250			fprintf(out, "%3d ", buf[i].ktr_cpu);
251		if (tflag) {
252			tnow = (uintmax_t)buf[i].ktr_timestamp;
253			if (rflag) {
254				if (tlast == -1)
255					tlast = tnow;
256				fprintf(out, "%16ju ", tlast - tnow);
257				tlast = tnow;
258			} else
259				fprintf(out, "%16ju ", tnow);
260		}
261		if (fflag) {
262			if (kvm_read(kd, (u_long)buf[i].ktr_file, fbuf,
263			    sizeof(fbuf)) == -1)
264				strcpy(fbuf, "(null)");
265			snprintf(obuf, sizeof(obuf), "%s:%d", fbuf,
266			    buf[i].ktr_line);
267			fprintf(out, "%-40s ", obuf);
268		}
269		fprintf(out, desc, parms[0], parms[1], parms[2], parms[3],
270		    parms[4], parms[5]);
271		fprintf(out, "\n");
272		if (!iflag) {
273			if (i == index)
274				break;
275			i = (i - 1) & (entries - 1);
276		} else {
277			if (++i == entries)
278				break;
279		}
280	}
281
282	return (0);
283}
284
285static void
286usage(void)
287{
288	errx(1, USAGE);
289}
290