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