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