2 * Copyright (c) 2002 Jake Burkholder
3 * Copyright (c) 2004 Robert Watson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
32 #include <sys/capsicum.h>
37 #include <capsicum_helpers.h>
52 "usage: ktrdump [-cfqrtH] [-i ktrfile] [-M core] [-N system] [-o outfile]\n"
54 static void usage(void);
56 static struct nlist nl[] = {
74 static char corefile[PATH_MAX];
75 static char execfile[PATH_MAX];
76 static char outfile[PATH_MAX] = "stdout";
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];
85 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
88 main(int ac, char **av)
90 u_long parms[KTR_PARMS];
91 struct ktr_entry *buf;
92 uintmax_t tlast, tnow;
108 * Parse commandline arguments.
111 while ((c = getopt(ac, av, "cfqrtHe:i:m:M:N:o:")) != -1)
118 if (strlcpy(execfile, optarg, sizeof(execfile))
120 errx(1, "%s: File name too long", optarg);
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 &&
133 err(1, "unable to limit rights for %s",
138 if (strlcpy(corefile, optarg, sizeof(corefile))
140 errx(1, "%s: File name too long", optarg);
144 if ((out = fopen(optarg, "w")) == NULL)
145 err(1, "%s", optarg);
146 strlcpy(outfile, optarg, sizeof(outfile));
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);
174 * Open our execfile and corefile, resolve needed symbols and read in
177 if ((kd = kvm_openfiles(Nflag ? execfile : NULL,
178 Mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
179 errx(1, "%s", errbuf);
182 * Cache NLS data, for strerror, for err(3), before entering capability
185 caph_cache_catpages();
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");
194 * Enter Capsicum sandbox.
196 * kvm_nlist() above uses kldsym(2) for native kernels, and that isn't
197 * allowed in the sandbox.
199 if (cap_enter() < 0 && errno != ENOSYS)
200 err(1, "unable to enter capability mode");
203 if (fstat(in, &sb) == -1)
205 entries = sb.st_size / sizeof(*buf);
207 buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, in, 0);
208 if (buf == MAP_FAILED)
211 if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries))
213 errx(1, "%s", kvm_geterr(kd));
214 if ((buf = malloc(sizeof(*buf) * entries)) == 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));
225 * Print a nice header.
228 fprintf(out, "%-6s ", "index");
230 fprintf(out, "%-3s ", "cpu");
232 fprintf(out, "%-16s ", "timestamp");
234 fprintf(out, "%-40s ", "file and line");
236 fprintf(out, "%-18s ", "tid");
237 fprintf(out, "%s", "trace");
240 fprintf(out, "------ ");
242 fprintf(out, "--- ");
244 fprintf(out, "---------------- ");
247 "---------------------------------------- ");
249 fprintf(out, "------------------ ");
250 fprintf(out, "----- ");
255 * Now tear through the trace buffer.
264 if (buf[i].ktr_desc == NULL)
266 if (kvm_read(kd, (u_long)buf[i].ktr_desc, desc,
268 errx(1, "%s", kvm_geterr(kd));
269 desc[sizeof(desc) - 1] = '\0';
271 for (p = desc; (c = *p++) != '\0';) {
274 next: if ((c = *p++) == '\0')
276 if (parm == KTR_PARMS)
277 errx(1, "too many parameters in \"%s\"", desc);
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 '.':
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];
294 parms[parm] = buf[i].ktr_parms[parm];
299 fprintf(out, "%6d ", i);
301 fprintf(out, "%3d ", buf[i].ktr_cpu);
303 tnow = (uintmax_t)buf[i].ktr_timestamp;
307 fprintf(out, "%16ju ", !iflag ? tlast - tnow :
311 fprintf(out, "%16ju ", tnow);
314 if (kvm_read(kd, (u_long)buf[i].ktr_file, fbuf,
316 strcpy(fbuf, "(null)");
317 snprintf(obuf, sizeof(obuf), "%s:%d", fbuf,
319 fprintf(out, "%-40s ", obuf);
322 fprintf(out, "%p ", buf[i].ktr_thread);
323 fprintf(out, desc, parms[0], parms[1], parms[2], parms[3],
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.
351 fprintf(stderr, USAGE);