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