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 [-cfqrt] [-e execfile] [-i ktrfile] [-m corefile] [-o outfile]\n"
51 static void usage(void);
53 static struct nlist nl[] = {
70 static char corefile[PATH_MAX];
71 static char execfile[PATH_MAX];
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];
80 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
83 main(int ac, char **av)
85 u_long parms[KTR_PARMS];
86 struct ktr_entry *buf;
87 uintmax_t tlast, tnow;
101 * Parse commandline arguments.
104 while ((c = getopt(ac, av, "cfqrte:i:m:o:")) != -1)
110 if (strlcpy(execfile, optarg, sizeof(execfile))
112 errx(1, "%s: File name too long", optarg);
120 if ((in = open(optarg, O_RDONLY)) == -1)
121 err(1, "%s", optarg);
124 if (strlcpy(corefile, optarg, sizeof(corefile))
126 errx(1, "%s: File name too long", optarg);
130 if ((out = fopen(optarg, "w")) == NULL)
131 err(1, "%s", optarg);
152 * Open our execfile and corefile, resolve needed symbols and read in
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");
164 if (fstat(in, &sb) == -1)
166 entries = sb.st_size / sizeof(*buf);
168 buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, in, 0);
169 if (buf == MAP_FAILED)
172 if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries))
174 errx(1, "%s", kvm_geterr(kd));
175 if ((buf = malloc(sizeof(*buf) * entries)) == 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)
180 errx(1, "%s", kvm_geterr(kd));
184 * Print a nice header.
187 fprintf(out, "%-6s ", "index");
189 fprintf(out, "%-3s ", "cpu");
191 fprintf(out, "%-16s ", "timestamp");
193 fprintf(out, "%-40s ", "file and line");
194 fprintf(out, "%s", "trace");
197 fprintf(out, "------ ");
199 fprintf(out, "--- ");
201 fprintf(out, "---------------- ");
204 "---------------------------------------- ");
205 fprintf(out, "----- ");
210 * Now tear through the trace buffer.
213 i = (index - 1) & (entries - 1);
216 if (buf[i].ktr_desc == NULL)
218 if (kvm_read(kd, (u_long)buf[i].ktr_desc, desc,
220 errx(1, "%s", kvm_geterr(kd));
221 desc[sizeof(desc) - 1] = '\0';
223 for (p = desc; (c = *p++) != '\0';) {
226 next: if ((c = *p++) == '\0')
228 if (parm == KTR_PARMS)
229 errx(1, "too many parameters");
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 '.':
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];
246 parms[parm] = buf[i].ktr_parms[parm];
251 fprintf(out, "%6d ", i);
253 fprintf(out, "%3d ", buf[i].ktr_cpu);
255 tnow = (uintmax_t)buf[i].ktr_timestamp;
259 fprintf(out, "%16ju ", !iflag ? tlast - tnow :
263 fprintf(out, "%16ju ", tnow);
266 if (kvm_read(kd, (u_long)buf[i].ktr_file, fbuf,
268 strcpy(fbuf, "(null)");
269 snprintf(obuf, sizeof(obuf), "%s:%d", fbuf,
271 fprintf(out, "%-40s ", obuf);
273 fprintf(out, desc, parms[0], parms[1], parms[2], parms[3],
279 i = (i - 1) & (entries - 1);
293 fprintf(stderr, USAGE);