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>
49 "usage: ktrdump [-cfqrtH] [-e execfile] [-i ktrfile] [-m corefile] [-o outfile]\n"
51 static void usage(void);
53 static struct nlist nl[] = {
71 static char corefile[PATH_MAX];
72 static char execfile[PATH_MAX];
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];
81 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
84 main(int ac, char **av)
86 u_long parms[KTR_PARMS];
87 struct ktr_entry *buf;
88 uintmax_t tlast, tnow;
103 * Parse commandline arguments.
106 while ((c = getopt(ac, av, "cfqrtHe:i:m:o:")) != -1)
112 if (strlcpy(execfile, optarg, sizeof(execfile))
114 errx(1, "%s: File name too long", optarg);
122 if ((in = open(optarg, O_RDONLY)) == -1)
123 err(1, "%s", optarg);
126 if (strlcpy(corefile, optarg, sizeof(corefile))
128 errx(1, "%s: File name too long", optarg);
132 if ((out = fopen(optarg, "w")) == NULL)
133 err(1, "%s", optarg);
157 * Open our execfile and corefile, resolve needed symbols and read in
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");
169 if (fstat(in, &sb) == -1)
171 entries = sb.st_size / sizeof(*buf);
173 buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, in, 0);
174 if (buf == MAP_FAILED)
177 if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries))
179 errx(1, "%s", kvm_geterr(kd));
180 if ((buf = malloc(sizeof(*buf) * entries)) == 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 kvm_read(kd, nl[2].n_value, &index2, sizeof(index2)) == -1)
187 errx(1, "%s", kvm_geterr(kd));
191 * Print a nice header.
194 fprintf(out, "%-6s ", "index");
196 fprintf(out, "%-3s ", "cpu");
198 fprintf(out, "%-16s ", "timestamp");
200 fprintf(out, "%-40s ", "file and line");
202 fprintf(out, "%-18s ", "tid");
203 fprintf(out, "%s", "trace");
206 fprintf(out, "------ ");
208 fprintf(out, "--- ");
210 fprintf(out, "---------------- ");
213 "---------------------------------------- ");
215 fprintf(out, "------------------ ");
216 fprintf(out, "----- ");
221 * Now tear through the trace buffer.
230 if (buf[i].ktr_desc == NULL)
232 if (kvm_read(kd, (u_long)buf[i].ktr_desc, desc,
234 errx(1, "%s", kvm_geterr(kd));
235 desc[sizeof(desc) - 1] = '\0';
237 for (p = desc; (c = *p++) != '\0';) {
240 next: if ((c = *p++) == '\0')
242 if (parm == KTR_PARMS)
243 errx(1, "too many parameters in \"%s\"", desc);
245 case '0': case '1': case '2': case '3': case '4':
246 case '5': case '6': case '7': case '8': case '9':
247 case '#': case '-': case ' ': case '+': case '\'':
248 case 'h': case 'l': case 'j': case 't': case 'z':
249 case 'q': case 'L': case '.':
252 if (kvm_read(kd, (u_long)buf[i].ktr_parms[parm],
253 sbuf[parm], sizeof(sbuf[parm])) == -1)
254 strcpy(sbuf[parm], "(null)");
255 sbuf[parm][sizeof(sbuf[0]) - 1] = '\0';
256 parms[parm] = (u_long)sbuf[parm];
260 parms[parm] = buf[i].ktr_parms[parm];
265 fprintf(out, "%6d ", i);
267 fprintf(out, "%3d ", buf[i].ktr_cpu);
269 tnow = (uintmax_t)buf[i].ktr_timestamp;
273 fprintf(out, "%16ju ", !iflag ? tlast - tnow :
277 fprintf(out, "%16ju ", tnow);
280 if (kvm_read(kd, (u_long)buf[i].ktr_file, fbuf,
282 strcpy(fbuf, "(null)");
283 snprintf(obuf, sizeof(obuf), "%s:%d", fbuf,
285 fprintf(out, "%-40s ", obuf);
288 fprintf(out, "%p ", buf[i].ktr_thread);
289 fprintf(out, desc, parms[0], parms[1], parms[2], parms[3],
294 * 'index' and 'index2' are the values of 'ktr_idx'
295 * before and after the KTR buffer was copied into
296 * 'buf'. Since the KTR entries between 'index' and
297 * 'index2' were in flux while the KTR buffer was
298 * being copied to userspace we don't dump them.
317 fprintf(stderr, USAGE);