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
27 * $FreeBSD: src/usr.bin/ktrdump/ktrdump.c,v 1.10 2005/05/21 09:55:06 ru Exp $
28 * $DragonFly: src/usr.bin/ktrdump/ktrdump.c,v 1.6 2005/12/19 17:09:58 dillon Exp $
31 #include <sys/cdefs.h>
33 #include <sys/types.h>
37 #include <sys/queue.h>
55 static void usage(void);
56 static void print_header(FILE *fo, int row);
57 static void print_entry(FILE *fo, kvm_t *kd, int n, int i, struct ktr_entry *entry);
58 static struct ktr_info *kvm_ktrinfo(kvm_t *kd, void *kptr);
59 static const char *kvm_string(kvm_t *kd, const char *kptr);
60 static const char *trunc_path(const char *str, int maxlen);
61 static void read_symbols(const char *execfile);
62 static const char *address_to_symbol(void *kptr);
64 static struct nlist nl[] = {
86 static int64_t last_timestamp;
87 static double tsc_frequency;
88 static double correction_factor = 0.0;
90 static char corefile[PATH_MAX];
91 static char execfile[PATH_MAX];
93 static char desc[SBUFLEN];
94 static char errbuf[_POSIX2_LINE_MAX];
95 static char fbuf[PATH_MAX];
96 static char obuf[PATH_MAX];
99 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
102 main(int ac, char **av)
104 struct ktr_entry **ktr_buf;
105 struct ktr_entry *entry;
106 struct ktr_entry *entryx;
107 uintmax_t tlast, tnow;
112 int64_t first_timestamp;
114 int *ktr_start_index;
119 int did_display_flag = 0;
128 * Parse commandline arguments.
131 while ((c = getopt(ac, av, "acfinqrtxpsA:N:M:o:")) != -1) {
145 if (strlcpy(execfile, optarg, sizeof(execfile))
147 errx(1, "%s: File name too long", optarg);
157 correction_factor = strtod(optarg, NULL);
160 if (strlcpy(corefile, optarg, sizeof(corefile))
162 errx(1, "%s: File name too long", optarg);
169 if ((fo = fopen(optarg, "w")) == NULL)
170 err(1, "%s", optarg);
182 sflag = 1; /* sort across the cpus */
195 if (cflag + iflag + tflag + xflag + fflag + pflag == 0) {
201 if (correction_factor != 0.0 && (rflag == 0 || nflag)) {
202 fprintf(stderr, "Correction factor can only be applied with -r and without -n\n");
211 * Open our execfile and corefile, resolve needed symbols and read in
214 if ((kd = kvm_openfiles(Nflag ? execfile : NULL,
215 Mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
216 errx(1, "%s", errbuf);
217 if (kvm_nlist(kd, nl) != 0)
218 errx(1, "%s", kvm_geterr(kd));
219 if (kvm_read(kd, nl[0].n_value, &version, sizeof(version)) == -1)
220 errx(1, "%s", kvm_geterr(kd));
221 if (kvm_read(kd, nl[4].n_value, &ncpus, sizeof(ncpus)) == -1)
222 errx(1, "%s", kvm_geterr(kd));
223 ktr_start_index = malloc(sizeof(*ktr_start_index) * ncpus);
225 if (kvm_read(kd, nl[5].n_value, &tts, sizeof(tts)) == -1)
226 errx(1, "%s", kvm_geterr(kd));
227 tsc_frequency = (double)tts;
229 if (version > KTR_VERSION)
230 errx(1, "ktr version too high for us to handle");
231 if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries)) == -1)
232 errx(1, "%s", kvm_geterr(kd));
233 ktr_buf = malloc(sizeof(*ktr_buf) * ncpus);
234 ktr_idx = malloc(sizeof(*ktr_idx) * ncpus);
237 read_symbols(Nflag ? execfile : NULL);
239 if (kvm_read(kd, nl[2].n_value, ktr_idx, sizeof(*ktr_idx) * ncpus) == -1)
240 errx(1, "%s", kvm_geterr(kd));
241 if (kvm_read(kd, nl[3].n_value, ktr_buf, sizeof(*ktr_buf) * ncpus) == -1)
242 errx(1, "%s", kvm_geterr(kd));
243 for (n = 0; n < ncpus; ++n) {
244 void *kptr = ktr_buf[n];
245 ktr_buf[n] = malloc(sizeof(**ktr_buf) * entries);
246 if (kvm_read(kd, (uintptr_t)kptr, ktr_buf[n], sizeof(**ktr_buf) * entries) == -1)
247 errx(1, "%s", kvm_geterr(kd));
251 * Figure out the lowest numbered timestamp.
254 printf("TSC frequency is %6.3f MHz\n", tsc_frequency / 1000000.0);
255 for (n = 0; n < ncpus; ++n) {
256 for (i = 0; i < entries; ++i) {
257 entry = &ktr_buf[n][i];
258 if (entry->ktr_timestamp && (first_timestamp == 0 ||
259 first_timestamp < entry->ktr_timestamp)) {
260 first_timestamp = entry->ktr_timestamp;
266 * Figure out the starting entry for each cpu
268 for (n = 0; n < ncpus; ++n) {
269 ktr_start_index[n] = 0;
271 for (i = 0; i < entries; ++i) {
272 entry = &ktr_buf[n][i];
273 if (entry->ktr_timestamp == 0)
275 if (tts == 0 || tts > entry->ktr_timestamp) {
276 tts = entry->ktr_timestamp;
277 ktr_start_index[n] = i;
283 * Now tear through the trace buffer.
290 for (n = 0; n < ncpus; ++n) {
291 i = ktr_start_index[n];
292 entry = &ktr_buf[n][i];
293 if (entry->ktr_timestamp == 0)
295 if (tts == 0 || tts >= entry->ktr_timestamp) {
296 tts = entry->ktr_timestamp;
300 if (bestn < 0 || tts < last_timestamp)
302 print_header(fo, row);
303 print_entry(fo, kd, bestn, ktr_start_index[bestn],
304 &ktr_buf[bestn][ktr_start_index[bestn]]);
305 if (++ktr_start_index[bestn] == entries)
306 ktr_start_index[bestn] = 0;
307 last_timestamp = tts;
311 for (n = 0; n < ncpus; ++n) {
312 last_timestamp = first_timestamp;
313 i = ktr_start_index[n];
315 entry = &ktr_buf[n][i];
317 print_entry(fo, kd, n, i, &ktr_buf[n][i]);
320 } while (i != ktr_start_index[n]);
327 print_header(FILE *fo, int row)
329 if (qflag == 0 && row % 20 == 0) {
330 fprintf(fo, "%-6s ", "index");
332 fprintf(fo, "%-3s ", "cpu");
334 fprintf(fo, "%-16s ", "timestamp");
337 fprintf(fo, "%-10s %-10s", "caller2", "caller1");
339 fprintf(fo, "%-20s %-20s", "caller2", "caller1");
342 fprintf(fo, "%-20s ", "ID");
344 fprintf(fo, "%10s%-30s ", "", "file and line");
346 fprintf(fo, "%s", "trace");
352 print_entry(FILE *fo, kvm_t *kd, int n, int i, struct ktr_entry *entry)
354 struct ktr_info *info = NULL;
356 fprintf(fo, " %5d ", i);
358 fprintf(fo, "%-3d ", n);
359 if (tflag || rflag) {
360 if (rflag && !nflag && tsc_frequency != 0.0) {
361 fprintf(fo, "%13.3f uS ",
362 (double)(entry->ktr_timestamp - last_timestamp) * 1000000.0 / tsc_frequency - correction_factor);
364 fprintf(fo, "%-16lld ", entry->ktr_timestamp -
367 fprintf(fo, "%-16lld ", entry->ktr_timestamp);
372 fprintf(fo, "%p %p ",
373 entry->ktr_caller2, entry->ktr_caller1);
375 fprintf(fo, "%-20s ",
376 address_to_symbol(entry->ktr_caller2));
377 fprintf(fo, "%-20s ",
378 address_to_symbol(entry->ktr_caller1));
382 info = kvm_ktrinfo(kd, entry->ktr_info);
384 fprintf(fo, "%-20s ", kvm_string(kd, info->kf_name));
386 fprintf(fo, "%-20s ", "<empty>");
389 fprintf(fo, "%34s:%-4d ", trunc_path(kvm_string(kd, entry->ktr_file), 34), entry->ktr_line);
392 info = kvm_ktrinfo(kd, entry->ktr_info);
394 fprintf(fo, kvm_string(kd, info->kf_format),
395 entry->ktr_data[0], entry->ktr_data[1],
396 entry->ktr_data[2], entry->ktr_data[3],
397 entry->ktr_data[4], entry->ktr_data[5],
398 entry->ktr_data[6], entry->ktr_data[7],
399 entry->ktr_data[8], entry->ktr_data[9]);
405 last_timestamp = entry->ktr_timestamp;
410 kvm_ktrinfo(kvm_t *kd, void *kptr)
412 static struct ktr_info save_info;
413 static void *save_kptr;
417 if (save_kptr != kptr) {
418 if (kvm_read(kd, (uintptr_t)kptr, &save_info, sizeof(save_info)) == -1) {
419 bzero(&save_info, sizeof(save_info));
429 kvm_string(kvm_t *kd, const char *kptr)
431 static char save_str[128];
432 static const char *save_kptr;
438 if (save_kptr != kptr) {
441 while (l < sizeof(save_str) - 1) {
442 n = 256 - ((intptr_t)(kptr + l) & 255);
443 if (n > sizeof(save_str) - l - 1)
444 n = sizeof(save_str) - l - 1;
445 if (kvm_read(kd, (uintptr_t)(kptr + l), save_str + l, n) < 0)
447 while (l < sizeof(save_str) && n) {
448 if (save_str[l] == 0)
463 trunc_path(const char *str, int maxlen)
465 int len = strlen(str);
468 return(str + len - maxlen);
474 TAILQ_ENTRY(symdata) link;
480 static TAILQ_HEAD(symlist, symdata) symlist;
481 static struct symdata *symcache;
482 static char *symbegin;
487 read_symbols(const char *execfile)
491 int buflen = sizeof(buf);
498 TAILQ_INIT(&symlist);
500 if (execfile == NULL) {
501 if (sysctlbyname("kern.bootfile", buf, &buflen, NULL, 0) < 0)
502 execfile = "/kernel";
506 snprintf(cmd, sizeof(cmd), "nm -n %s", execfile);
507 if ((fp = popen(cmd, "r")) != NULL) {
508 while (fgets(buf, sizeof(buf), fp) != NULL) {
509 s1 = strtok(buf, " \t\n");
510 s2 = strtok(NULL, " \t\n");
511 s3 = strtok(NULL, " \t\n");
512 if (s1 && s2 && s3) {
513 sym = malloc(sizeof(struct symdata));
514 sym->symaddr = (char *)strtoul(s1, NULL, 16);
515 sym->symtype = s2[0];
516 sym->symname = strdup(s3);
517 if (strcmp(s3, "kernbase") == 0)
518 symbegin = sym->symaddr;
519 if (strcmp(s3, "end") == 0)
520 symend = sym->symaddr;
521 TAILQ_INSERT_TAIL(&symlist, sym, link);
526 symcache = TAILQ_FIRST(&symlist);
531 address_to_symbol(void *kptr)
535 if (symcache == NULL ||
536 (char *)kptr < symbegin || (char *)kptr >= symend
538 snprintf(buf, sizeof(buf), "%p", kptr);
541 while ((char *)symcache->symaddr < (char *)kptr) {
542 if (TAILQ_NEXT(symcache, link) == NULL)
544 symcache = TAILQ_NEXT(symcache, link);
546 while ((char *)symcache->symaddr > (char *)kptr) {
547 if (symcache != TAILQ_FIRST(&symlist))
548 symcache = TAILQ_PREV(symcache, symlist, link);
550 snprintf(buf, sizeof(buf), "%s+%d", symcache->symname,
551 (int)((char *)kptr - symcache->symaddr));
558 fprintf(stderr, "usage: ktrdump [-acfinpqrtx] [-N execfile] "
559 "[-M corefile] [-o outfile]\n");