2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Jake Burkholder
5 * Copyright (c) 2004 Robert Watson
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
51 "usage: ktrdump [-cflqrtH] [-i ktrfile] [-M core] [-N system] [-o outfile]\n"
53 static void usage(void);
55 static struct nlist nl[] = {
74 static char corefile[PATH_MAX];
75 static char execfile[PATH_MAX];
77 static char desc[SBUFLEN];
78 static char errbuf[_POSIX2_LINE_MAX];
79 static char fbuf[PATH_MAX];
80 static char obuf[PATH_MAX];
81 static char sbuf[KTR_PARMS][SBUFLEN];
84 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
87 main(int ac, char **av)
89 u_long parms[KTR_PARMS];
90 struct ktr_entry *buf;
91 uintmax_t tlast, tnow;
107 * Parse commandline arguments.
110 while ((c = getopt(ac, av, "cflqrtHe:i:m:M:N:o:")) != -1)
117 if (strlcpy(execfile, optarg, sizeof(execfile))
119 errx(1, "%s: File name too long", optarg);
127 if ((in = open(optarg, O_RDONLY)) == -1)
128 err(1, "%s", optarg);
135 if (strlcpy(corefile, optarg, sizeof(corefile))
137 errx(1, "%s: File name too long", optarg);
141 if ((out = fopen(optarg, "w")) == NULL)
142 err(1, "%s", optarg);
166 * Open our execfile and corefile, resolve needed symbols and read in
169 if ((kd = kvm_openfiles(Nflag ? execfile : NULL,
170 Mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
171 errx(1, "%s", errbuf);
172 count = kvm_nlist(kd, nl);
174 errx(1, "%s", kvm_geterr(kd));
176 errx(1, "failed to resolve ktr symbols");
177 if (kvm_read(kd, nl[0].n_value, &version, sizeof(version)) == -1)
178 errx(1, "%s", kvm_geterr(kd));
179 if (version != KTR_VERSION)
180 errx(1, "ktr version mismatch");
182 if (fstat(in, &sb) == -1)
184 entries = sb.st_size / sizeof(*buf);
186 buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, in, 0);
187 if (buf == MAP_FAILED)
190 if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries))
192 errx(1, "%s", kvm_geterr(kd));
193 if ((buf = malloc(sizeof(*buf) * entries)) == NULL)
195 if (kvm_read(kd, nl[2].n_value, &index, sizeof(index)) == -1 ||
196 kvm_read(kd, nl[3].n_value, &bufptr,
197 sizeof(bufptr)) == -1 ||
198 kvm_read(kd, bufptr, buf, sizeof(*buf) * entries) == -1 ||
199 kvm_read(kd, nl[2].n_value, &index2, sizeof(index2)) == -1)
200 errx(1, "%s", kvm_geterr(kd));
204 * Print a nice header.
207 fprintf(out, "%-6s ", "index");
209 fprintf(out, "%-3s ", "cpu");
211 fprintf(out, "%-16s ", "timestamp");
213 fprintf(out, "%-40s ", "file and line");
215 fprintf(out, "%-18s ", "tid");
216 fprintf(out, "%s", "trace");
219 fprintf(out, "------ ");
221 fprintf(out, "--- ");
223 fprintf(out, "---------------- ");
226 "---------------------------------------- ");
228 fprintf(out, "------------------ ");
229 fprintf(out, "----- ");
235 * Now tear through the trace buffer.
237 * In "live" mode, find the oldest entry (first non-NULL entry
238 * after index2) and walk forward. Otherwise, start with the
239 * most recent entry and walk backwards.
243 i = index2 + 1 % entries;
244 while (buf[i].ktr_desc == NULL && i != index) {
257 if (buf[i].ktr_desc == NULL)
259 if (kvm_read(kd, (u_long)buf[i].ktr_desc, desc,
261 errx(1, "%s", kvm_geterr(kd));
262 desc[sizeof(desc) - 1] = '\0';
264 for (p = desc; (c = *p++) != '\0';) {
267 next: if ((c = *p++) == '\0')
269 if (parm == KTR_PARMS)
270 errx(1, "too many parameters in \"%s\"", desc);
272 case '0': case '1': case '2': case '3': case '4':
273 case '5': case '6': case '7': case '8': case '9':
274 case '#': case '-': case ' ': case '+': case '\'':
275 case 'h': case 'l': case 'j': case 't': case 'z':
276 case 'q': case 'L': case '.':
279 if (kvm_read(kd, (u_long)buf[i].ktr_parms[parm],
280 sbuf[parm], sizeof(sbuf[parm])) == -1)
281 strcpy(sbuf[parm], "(null)");
282 sbuf[parm][sizeof(sbuf[0]) - 1] = '\0';
283 parms[parm] = (u_long)sbuf[parm];
287 parms[parm] = buf[i].ktr_parms[parm];
292 fprintf(out, "%6d ", i);
294 fprintf(out, "%3d ", buf[i].ktr_cpu);
296 tnow = (uintmax_t)buf[i].ktr_timestamp;
300 fprintf(out, "%16ju ", !iflag ? tlast - tnow :
304 fprintf(out, "%16ju ", tnow);
307 if (kvm_read(kd, (u_long)buf[i].ktr_file, fbuf,
309 strcpy(fbuf, "(null)");
310 snprintf(obuf, sizeof(obuf), "%s:%d", fbuf,
312 fprintf(out, "%-40s ", obuf);
315 fprintf(out, "%p ", buf[i].ktr_thread);
316 fprintf(out, desc, parms[0], parms[1], parms[2], parms[3],
321 * 'index' and 'index2' are the values of 'ktr_idx'
322 * before and after the KTR buffer was copied into
323 * 'buf'. Since the KTR entries between 'index' and
324 * 'index2' were in flux while the KTR buffer was
325 * being copied to userspace we don't dump them.
345 * In "live" mode, poll 'ktr_idx' periodically and dump any
346 * new entries since our last pass through the ring.
348 if (lflag && !iflag) {
349 while (index == index2) {
351 if (kvm_read(kd, nl[2].n_value, &index2,
352 sizeof(index2)) == -1)
353 errx(1, "%s", kvm_geterr(kd));
357 if (kvm_read(kd, bufptr, buf, sizeof(*buf) * entries) == -1 ||
358 kvm_read(kd, nl[2].n_value, &index2, sizeof(index2)) == -1)
359 errx(1, "%s", kvm_geterr(kd));
370 fprintf(stderr, USAGE);