xref: /freebsd/usr.bin/ktrdump/ktrdump.c (revision d6b92ffa)
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$");
30 
31 #include <sys/types.h>
32 #include <sys/capsicum.h>
33 #include <sys/ktr.h>
34 #include <sys/mman.h>
35 #include <sys/stat.h>
36 
37 #include <capsicum_helpers.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <kvm.h>
42 #include <limits.h>
43 #include <nlist.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #define	SBUFLEN	128
51 #define	USAGE \
52 	"usage: ktrdump [-cfqrtH] [-i ktrfile] [-M core] [-N system] [-o outfile]\n"
53 
54 static void usage(void);
55 
56 static struct nlist nl[] = {
57 	{ "_ktr_version" },
58 	{ "_ktr_entries" },
59 	{ "_ktr_idx" },
60 	{ "_ktr_buf" },
61 	{ NULL }
62 };
63 
64 static int cflag;
65 static int fflag;
66 static int Mflag;
67 static int Nflag;
68 static int qflag;
69 static int rflag;
70 static int tflag;
71 static int iflag;
72 static int hflag;
73 
74 static char corefile[PATH_MAX];
75 static char execfile[PATH_MAX];
76 static char outfile[PATH_MAX] = "stdout";
77 
78 static char desc[SBUFLEN];
79 static char errbuf[_POSIX2_LINE_MAX];
80 static char fbuf[PATH_MAX];
81 static char obuf[PATH_MAX];
82 static char sbuf[KTR_PARMS][SBUFLEN];
83 
84 /*
85  * Reads the ktr trace buffer from kernel memory and prints the trace entries.
86  */
87 int
88 main(int ac, char **av)
89 {
90 	u_long parms[KTR_PARMS];
91 	struct ktr_entry *buf;
92 	uintmax_t tlast, tnow;
93 	unsigned long bufptr;
94 	cap_rights_t rights;
95 	struct stat sb;
96 	kvm_t *kd;
97 	FILE *out;
98 	char *p;
99 	int version;
100 	int entries;
101 	int index, index2;
102 	int parm;
103 	int in;
104 	int c;
105 	int i = 0;
106 
107 	/*
108 	 * Parse commandline arguments.
109 	 */
110 	out = stdout;
111 	while ((c = getopt(ac, av, "cfqrtHe:i:m:M:N:o:")) != -1)
112 		switch (c) {
113 		case 'c':
114 			cflag = 1;
115 			break;
116 		case 'N':
117 		case 'e':
118 			if (strlcpy(execfile, optarg, sizeof(execfile))
119 			    >= sizeof(execfile))
120 				errx(1, "%s: File name too long", optarg);
121 			Nflag = 1;
122 			break;
123 		case 'f':
124 			fflag = 1;
125 			break;
126 		case 'i':
127 			iflag = 1;
128 			if ((in = open(optarg, O_RDONLY)) == -1)
129 				err(1, "%s", optarg);
130 			cap_rights_init(&rights, CAP_FSTAT, CAP_MMAP_R);
131 			if (cap_rights_limit(in, &rights) < 0 &&
132 			    errno != ENOSYS)
133 				err(1, "unable to limit rights for %s",
134 				    optarg);
135 			break;
136 		case 'M':
137 		case 'm':
138 			if (strlcpy(corefile, optarg, sizeof(corefile))
139 			    >= sizeof(corefile))
140 				errx(1, "%s: File name too long", optarg);
141 			Mflag = 1;
142 			break;
143 		case 'o':
144 			if ((out = fopen(optarg, "w")) == NULL)
145 				err(1, "%s", optarg);
146 			strlcpy(outfile, optarg, sizeof(outfile));
147 			break;
148 		case 'q':
149 			qflag++;
150 			break;
151 		case 'r':
152 			rflag = 1;
153 			break;
154 		case 't':
155 			tflag = 1;
156 			break;
157 		case 'H':
158 			hflag = 1;
159 			break;
160 		case '?':
161 		default:
162 			usage();
163 		}
164 	ac -= optind;
165 	av += optind;
166 	if (ac != 0)
167 		usage();
168 
169 	cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
170 	if (cap_rights_limit(fileno(out), &rights) < 0 && errno != ENOSYS)
171 		err(1, "unable to limit rights for %s", outfile);
172 
173 	/*
174 	 * Open our execfile and corefile, resolve needed symbols and read in
175 	 * the trace buffer.
176 	 */
177 	if ((kd = kvm_openfiles(Nflag ? execfile : NULL,
178 	    Mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
179 		errx(1, "%s", errbuf);
180 
181 	/*
182 	 * Cache NLS data, for strerror, for err(3), before entering capability
183 	 * mode.
184 	 */
185 	caph_cache_catpages();
186 
187 	if (kvm_nlist(kd, nl) != 0 ||
188 	    kvm_read(kd, nl[0].n_value, &version, sizeof(version)) == -1)
189 		errx(1, "%s", kvm_geterr(kd));
190 	if (version != KTR_VERSION)
191 		errx(1, "ktr version mismatch");
192 
193 	/*
194 	 * Enter Capsicum sandbox.
195 	 *
196 	 * kvm_nlist() above uses kldsym(2) for native kernels, and that isn't
197 	 * allowed in the sandbox.
198 	 */
199 	if (cap_enter() < 0 && errno != ENOSYS)
200 		err(1, "unable to enter capability mode");
201 
202 	if (iflag) {
203 		if (fstat(in, &sb) == -1)
204 			errx(1, "stat");
205 		entries = sb.st_size / sizeof(*buf);
206 		index = 0;
207 		buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, in, 0);
208 		if (buf == MAP_FAILED)
209 			errx(1, "mmap");
210 	} else {
211 		if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries))
212 		    == -1)
213 			errx(1, "%s", kvm_geterr(kd));
214 		if ((buf = malloc(sizeof(*buf) * entries)) == NULL)
215 			err(1, NULL);
216 		if (kvm_read(kd, nl[2].n_value, &index, sizeof(index)) == -1 ||
217 		    kvm_read(kd, nl[3].n_value, &bufptr,
218 		    sizeof(bufptr)) == -1 ||
219 		    kvm_read(kd, bufptr, buf, sizeof(*buf) * entries) == -1 ||
220 		    kvm_read(kd, nl[2].n_value, &index2, sizeof(index2)) == -1)
221 			errx(1, "%s", kvm_geterr(kd));
222 	}
223 
224 	/*
225 	 * Print a nice header.
226 	 */
227 	if (!qflag) {
228 		fprintf(out, "%-6s ", "index");
229 		if (cflag)
230 			fprintf(out, "%-3s ", "cpu");
231 		if (tflag)
232 			fprintf(out, "%-16s ", "timestamp");
233 		if (fflag)
234 			fprintf(out, "%-40s ", "file and line");
235 		if (hflag)
236 			fprintf(out, "%-18s ", "tid");
237 		fprintf(out, "%s", "trace");
238 		fprintf(out, "\n");
239 
240 		fprintf(out, "------ ");
241 		if (cflag)
242 			fprintf(out, "--- ");
243 		if (tflag)
244 			fprintf(out, "---------------- ");
245 		if (fflag)
246 			fprintf(out,
247 			    "---------------------------------------- ");
248 		if (hflag)
249 			fprintf(out, "------------------ ");
250 		fprintf(out, "----- ");
251 		fprintf(out, "\n");
252 	}
253 
254 	/*
255 	 * Now tear through the trace buffer.
256 	 */
257 	if (!iflag) {
258 		i = index - 1;
259 		if (i < 0)
260 			i = entries - 1;
261 	}
262 	tlast = -1;
263 	for (;;) {
264 		if (buf[i].ktr_desc == NULL)
265 			break;
266 		if (kvm_read(kd, (u_long)buf[i].ktr_desc, desc,
267 		    sizeof(desc)) == -1)
268 			errx(1, "%s", kvm_geterr(kd));
269 		desc[sizeof(desc) - 1] = '\0';
270 		parm = 0;
271 		for (p = desc; (c = *p++) != '\0';) {
272 			if (c != '%')
273 				continue;
274 next:			if ((c = *p++) == '\0')
275 				break;
276 			if (parm == KTR_PARMS)
277 				errx(1, "too many parameters in \"%s\"", desc);
278 			switch (c) {
279 			case '0': case '1': case '2': case '3': case '4':
280 			case '5': case '6': case '7': case '8': case '9':
281 			case '#': case '-': case ' ': case '+': case '\'':
282 			case 'h': case 'l': case 'j': case 't': case 'z':
283 			case 'q': case 'L': case '.':
284 				goto next;
285 			case 's':
286 				if (kvm_read(kd, (u_long)buf[i].ktr_parms[parm],
287 				    sbuf[parm], sizeof(sbuf[parm])) == -1)
288 					strcpy(sbuf[parm], "(null)");
289 				sbuf[parm][sizeof(sbuf[0]) - 1] = '\0';
290 				parms[parm] = (u_long)sbuf[parm];
291 				parm++;
292 				break;
293 			default:
294 				parms[parm] = buf[i].ktr_parms[parm];
295 				parm++;
296 				break;
297 			}
298 		}
299 		fprintf(out, "%6d ", i);
300 		if (cflag)
301 			fprintf(out, "%3d ", buf[i].ktr_cpu);
302 		if (tflag) {
303 			tnow = (uintmax_t)buf[i].ktr_timestamp;
304 			if (rflag) {
305 				if (tlast == -1)
306 					tlast = tnow;
307 				fprintf(out, "%16ju ", !iflag ? tlast - tnow :
308 				    tnow - tlast);
309 				tlast = tnow;
310 			} else
311 				fprintf(out, "%16ju ", tnow);
312 		}
313 		if (fflag) {
314 			if (kvm_read(kd, (u_long)buf[i].ktr_file, fbuf,
315 			    sizeof(fbuf)) == -1)
316 				strcpy(fbuf, "(null)");
317 			snprintf(obuf, sizeof(obuf), "%s:%d", fbuf,
318 			    buf[i].ktr_line);
319 			fprintf(out, "%-40s ", obuf);
320 		}
321 		if (hflag)
322 			fprintf(out, "%p ", buf[i].ktr_thread);
323 		fprintf(out, desc, parms[0], parms[1], parms[2], parms[3],
324 		    parms[4], parms[5]);
325 		fprintf(out, "\n");
326 		if (!iflag) {
327 			/*
328 			 * 'index' and 'index2' are the values of 'ktr_idx'
329 			 * before and after the KTR buffer was copied into
330 			 * 'buf'. Since the KTR entries between 'index' and
331 			 * 'index2' were in flux while the KTR buffer was
332 			 * being copied to userspace we don't dump them.
333 			 */
334 			if (i == index2)
335 				break;
336 			if (--i < 0)
337 				i = entries - 1;
338 		} else {
339 			if (++i == entries)
340 				break;
341 		}
342 	}
343 
344 	return (0);
345 }
346 
347 static void
348 usage(void)
349 {
350 
351 	fprintf(stderr, USAGE);
352 	exit(1);
353 }
354