xref: /freebsd/usr.bin/ktrdump/ktrdump.c (revision fba3cde907930eed2adb8a320524bc250338c729)
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/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 [-cfqrtH] [-e execfile] [-i ktrfile] [-m corefile] [-o outfile]\n"
50 
51 static void usage(void);
52 
53 static struct nlist nl[] = {
54 	{ "_ktr_version" },
55 	{ "_ktr_entries" },
56 	{ "_ktr_idx" },
57 	{ "_ktr_buf" },
58 	{ NULL }
59 };
60 
61 static int cflag;
62 static int eflag;
63 static int fflag;
64 static int mflag;
65 static int qflag;
66 static int rflag;
67 static int tflag;
68 static int iflag;
69 static int hflag;
70 
71 static char corefile[PATH_MAX];
72 static char execfile[PATH_MAX];
73 
74 static char desc[SBUFLEN];
75 static char errbuf[_POSIX2_LINE_MAX];
76 static char fbuf[PATH_MAX];
77 static char obuf[PATH_MAX];
78 static char sbuf[KTR_PARMS][SBUFLEN];
79 
80 /*
81  * Reads the ktr trace buffer from kernel memory and prints the trace entries.
82  */
83 int
84 main(int ac, char **av)
85 {
86 	u_long parms[KTR_PARMS];
87 	struct ktr_entry *buf;
88 	uintmax_t tlast, tnow;
89 	unsigned long bufptr;
90 	struct stat sb;
91 	kvm_t *kd;
92 	FILE *out;
93 	char *p;
94 	int version;
95 	int entries;
96 	int index;
97 	int parm;
98 	int in;
99 	int c;
100 	int i = 0;
101 
102 	/*
103 	 * Parse commandline arguments.
104 	 */
105 	out = stdout;
106 	while ((c = getopt(ac, av, "cfqrtHe:i:m:o:")) != -1)
107 		switch (c) {
108 		case 'c':
109 			cflag = 1;
110 			break;
111 		case 'e':
112 			if (strlcpy(execfile, optarg, sizeof(execfile))
113 			    >= sizeof(execfile))
114 				errx(1, "%s: File name too long", optarg);
115 			eflag = 1;
116 			break;
117 		case 'f':
118 			fflag = 1;
119 			break;
120 		case 'i':
121 			iflag = 1;
122 			if ((in = open(optarg, O_RDONLY)) == -1)
123 				err(1, "%s", optarg);
124 			break;
125 		case 'm':
126 			if (strlcpy(corefile, optarg, sizeof(corefile))
127 			    >= sizeof(corefile))
128 				errx(1, "%s: File name too long", optarg);
129 			mflag = 1;
130 			break;
131 		case 'o':
132 			if ((out = fopen(optarg, "w")) == NULL)
133 				err(1, "%s", optarg);
134 			break;
135 		case 'q':
136 			qflag++;
137 			break;
138 		case 'r':
139 			rflag = 1;
140 			break;
141 		case 't':
142 			tflag = 1;
143 			break;
144 		case 'H':
145 			hflag = 1;
146 			break;
147 		case '?':
148 		default:
149 			usage();
150 		}
151 	ac -= optind;
152 	av += optind;
153 	if (ac != 0)
154 		usage();
155 
156 	/*
157 	 * Open our execfile and corefile, resolve needed symbols and read in
158 	 * the trace buffer.
159 	 */
160 	if ((kd = kvm_openfiles(eflag ? execfile : NULL,
161 	    mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
162 		errx(1, "%s", errbuf);
163 	if (kvm_nlist(kd, nl) != 0 ||
164 	    kvm_read(kd, nl[0].n_value, &version, sizeof(version)) == -1)
165 		errx(1, "%s", kvm_geterr(kd));
166 	if (version != KTR_VERSION)
167 		errx(1, "ktr version mismatch");
168 	if (iflag) {
169 		if (fstat(in, &sb) == -1)
170 			errx(1, "stat");
171 		entries = sb.st_size / sizeof(*buf);
172 		index = 0;
173 		buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, in, 0);
174 		if (buf == MAP_FAILED)
175 			errx(1, "mmap");
176 	} else {
177 		if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries))
178 		    == -1)
179 			errx(1, "%s", kvm_geterr(kd));
180 		if ((buf = malloc(sizeof(*buf) * entries)) == NULL)
181 			err(1, NULL);
182 		if (kvm_read(kd, nl[2].n_value, &index, sizeof(index)) == -1 ||
183 		    kvm_read(kd, nl[3].n_value, &bufptr,
184 		    sizeof(bufptr)) == -1 ||
185 		    kvm_read(kd, bufptr, buf, sizeof(*buf) * entries) == -1)
186 			errx(1, "%s", kvm_geterr(kd));
187 	}
188 
189 	/*
190 	 * Print a nice header.
191 	 */
192 	if (!qflag) {
193 		fprintf(out, "%-6s ", "index");
194 		if (cflag)
195 			fprintf(out, "%-3s ", "cpu");
196 		if (tflag)
197 			fprintf(out, "%-16s ", "timestamp");
198 		if (fflag)
199 			fprintf(out, "%-40s ", "file and line");
200 		if (hflag)
201 			fprintf(out, "%-18s ", "tid");
202 		fprintf(out, "%s", "trace");
203 		fprintf(out, "\n");
204 
205 		fprintf(out, "------ ");
206 		if (cflag)
207 			fprintf(out, "--- ");
208 		if (tflag)
209 			fprintf(out, "---------------- ");
210 		if (fflag)
211 			fprintf(out,
212 			    "---------------------------------------- ");
213 		if (hflag)
214 			fprintf(out, "------------------ ");
215 		fprintf(out, "----- ");
216 		fprintf(out, "\n");
217 	}
218 
219 	/*
220 	 * Now tear through the trace buffer.
221 	 */
222 	if (!iflag)
223 		i = (index - 1) % entries;
224 	tlast = -1;
225 	for (;;) {
226 		if (buf[i].ktr_desc == NULL)
227 			break;
228 		if (kvm_read(kd, (u_long)buf[i].ktr_desc, desc,
229 		    sizeof(desc)) == -1)
230 			errx(1, "%s", kvm_geterr(kd));
231 		desc[sizeof(desc) - 1] = '\0';
232 		parm = 0;
233 		for (p = desc; (c = *p++) != '\0';) {
234 			if (c != '%')
235 				continue;
236 next:			if ((c = *p++) == '\0')
237 				break;
238 			if (parm == KTR_PARMS)
239 				errx(1, "too many parameters");
240 			switch (c) {
241 			case '0': case '1': case '2': case '3': case '4':
242 			case '5': case '6': case '7': case '8': case '9':
243 			case '#': case '-': case ' ': case '+': case '\'':
244 			case 'h': case 'l': case 'j': case 't': case 'z':
245 			case 'q': case 'L': case '.':
246 				goto next;
247 			case 's':
248 				if (kvm_read(kd, (u_long)buf[i].ktr_parms[parm],
249 				    sbuf[parm], sizeof(sbuf[parm])) == -1)
250 					strcpy(sbuf[parm], "(null)");
251 				sbuf[parm][sizeof(sbuf[0]) - 1] = '\0';
252 				parms[parm] = (u_long)sbuf[parm];
253 				parm++;
254 				break;
255 			default:
256 				parms[parm] = buf[i].ktr_parms[parm];
257 				parm++;
258 				break;
259 			}
260 		}
261 		fprintf(out, "%6d ", i);
262 		if (cflag)
263 			fprintf(out, "%3d ", buf[i].ktr_cpu);
264 		if (tflag) {
265 			tnow = (uintmax_t)buf[i].ktr_timestamp;
266 			if (rflag) {
267 				if (tlast == -1)
268 					tlast = tnow;
269 				fprintf(out, "%16ju ", !iflag ? tlast - tnow :
270 				    tnow - tlast);
271 				tlast = tnow;
272 			} else
273 				fprintf(out, "%16ju ", tnow);
274 		}
275 		if (fflag) {
276 			if (kvm_read(kd, (u_long)buf[i].ktr_file, fbuf,
277 			    sizeof(fbuf)) == -1)
278 				strcpy(fbuf, "(null)");
279 			snprintf(obuf, sizeof(obuf), "%s:%d", fbuf,
280 			    buf[i].ktr_line);
281 			fprintf(out, "%-40s ", obuf);
282 		}
283 		if (hflag)
284 			fprintf(out, "%p ", buf[i].ktr_thread);
285 		fprintf(out, desc, parms[0], parms[1], parms[2], parms[3],
286 		    parms[4], parms[5]);
287 		fprintf(out, "\n");
288 		if (!iflag) {
289 			if (i == index)
290 				break;
291 			i = (i - 1) % entries;
292 		} else {
293 			if (++i == entries)
294 				break;
295 		}
296 	}
297 
298 	return (0);
299 }
300 
301 static void
302 usage(void)
303 {
304 
305 	fprintf(stderr, USAGE);
306 	exit(1);
307 }
308