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.13 2008/11/10 02:05:31 swildner Exp $
31 #include <sys/cdefs.h>
33 #include <sys/types.h>
37 #include <sys/queue.h>
52 #define SBUFMASK (SBUFLEN - 1)
55 struct ktr_entry *ents;
58 int beg_idx; /* Beginning index */
59 int end_idx; /* Ending index */
62 static struct nlist nl1[] = {
63 { .n_name = "_ktr_version" },
64 { .n_name = "_ktr_entries" },
65 { .n_name = "_ncpus" },
69 static struct nlist nl2[] = {
70 { .n_name = "_tsc_frequency" },
74 static struct nlist nl_version_ktr_idx[] = {
75 { .n_name = "_ktr_idx" },
76 { .n_name = "_ktr_buf" },
80 static struct nlist nl_version_ktr_cpu[] = {
81 { .n_name = "_ktr_cpu" },
98 static double tsc_frequency;
99 static double correction_factor = 0.0;
101 static char corefile[PATH_MAX];
102 static char execfile[PATH_MAX];
104 static char errbuf[_POSIX2_LINE_MAX];
107 static int entries_per_buf;
108 static int fifo_mask;
109 static int ktr_version;
111 static void usage(void);
112 static int earliest_ts(struct ktr_buffer *);
113 static void print_header(FILE *, int);
114 static void print_entry(FILE *, int, int, struct ktr_entry *, u_int64_t *);
115 static struct ktr_info *kvm_ktrinfo(void *);
116 static const char *kvm_string(char *buf, const char *);
117 static const char *trunc_path(const char *, int);
118 static void read_symbols(const char *);
119 static const char *address_to_symbol(void *);
120 static struct ktr_buffer *ktr_bufs_init(void);
121 static void get_indices(struct ktr_entry **, int *);
122 static void load_bufs(struct ktr_buffer *, struct ktr_entry **, int *);
123 static void print_buf(FILE *, struct ktr_buffer *, int, u_int64_t *);
124 static void print_bufs_timesorted(FILE *, struct ktr_buffer *, u_int64_t *);
125 static void kvmfprintf(FILE *fp, const char *ctl, va_list va);
128 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
131 main(int ac, char **av)
133 struct ktr_buffer *ktr_bufs;
134 struct ktr_entry **ktr_kbuf;
138 int *ktr_start_index;
143 * Parse commandline arguments.
146 while ((c = getopt(ac, av, "acfinqrtxpslA:N:M:o:")) != -1) {
161 if (strlcpy(execfile, optarg, sizeof(execfile))
163 errx(1, "%s: File name too long", optarg);
176 correction_factor = strtod(optarg, NULL);
179 if (strlcpy(corefile, optarg, sizeof(corefile))
181 errx(1, "%s: File name too long", optarg);
188 if ((fo = fopen(optarg, "w")) == NULL)
189 err(1, "%s", optarg);
201 sflag = 1; /* sort across the cpus */
214 if (cflag + iflag + tflag + xflag + fflag + pflag == 0) {
220 if (correction_factor != 0.0 && (rflag == 0 || nflag)) {
221 fprintf(stderr, "Correction factor can only be applied with -r and without -n\n");
230 * Open our execfile and corefile, resolve needed symbols and read in
233 if ((kd = kvm_openfiles(Nflag ? execfile : NULL,
234 Mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
235 errx(1, "%s", errbuf);
236 if (kvm_nlist(kd, nl1) != 0)
237 errx(1, "%s", kvm_geterr(kd));
238 if (kvm_read(kd, nl1[0].n_value, &ktr_version, sizeof(ktr_version)) == -1)
239 errx(1, "%s", kvm_geterr(kd));
240 if (kvm_read(kd, nl1[2].n_value, &ncpus, sizeof(ncpus)) == -1)
241 errx(1, "%s", kvm_geterr(kd));
242 ktr_start_index = malloc(sizeof(*ktr_start_index) * ncpus);
243 if (ktr_version >= KTR_VERSION_WITH_FREQ && kvm_nlist(kd, nl2) == 0) {
244 if (kvm_read(kd, nl2[0].n_value, &tts, sizeof(tts)) == -1)
245 errx(1, "%s", kvm_geterr(kd));
246 tsc_frequency = (double)tts;
248 if (ktr_version > KTR_VERSION)
249 errx(1, "ktr version too high for us to handle");
250 if (kvm_read(kd, nl1[1].n_value, &entries_per_buf,
251 sizeof(entries_per_buf)) == -1)
252 errx(1, "%s", kvm_geterr(kd));
253 fifo_mask = entries_per_buf - 1;
255 printf("TSC frequency is %6.3f MHz\n", tsc_frequency / 1000000.0);
257 ktr_kbuf = calloc(ncpus, sizeof(*ktr_kbuf));
258 ktr_idx = calloc(ncpus, sizeof(*ktr_idx));
261 read_symbols(Nflag ? execfile : NULL);
263 if (ktr_version < KTR_VERSION_KTR_CPU) {
264 if (kvm_nlist(kd, nl_version_ktr_idx))
265 errx(1, "%s", kvm_geterr(kd));
267 if (kvm_nlist(kd, nl_version_ktr_cpu))
268 errx(1, "%s", kvm_geterr(kd));
271 get_indices(ktr_kbuf, ktr_idx);
273 ktr_bufs = ktr_bufs_init();
276 u_int64_t last_timestamp = 0;
278 load_bufs(ktr_bufs, ktr_kbuf, ktr_idx);
279 print_bufs_timesorted(fo, ktr_bufs, &last_timestamp);
281 usleep(1000000 / 10);
284 u_int64_t *last_timestamp = calloc(sizeof(u_int64_t), ncpus);
286 load_bufs(ktr_bufs, ktr_kbuf, ktr_idx);
287 for (n = 0; n < ncpus; ++n)
288 print_buf(fo, ktr_bufs, n, &last_timestamp[n]);
290 usleep(1000000 / 10);
297 print_header(FILE *fo, int row)
299 if (qflag == 0 && (u_int32_t)row % 20 == 0) {
300 fprintf(fo, "%-6s ", "index");
302 fprintf(fo, "%-3s ", "cpu");
304 fprintf(fo, "%-16s ", "timestamp");
307 fprintf(fo, "%-10s %-10s", "caller2", "caller1");
309 fprintf(fo, "%-20s %-20s", "caller2", "caller1");
312 fprintf(fo, "%-20s ", "ID");
314 fprintf(fo, "%10s%-30s ", "", "file and line");
316 fprintf(fo, "%s", "trace");
322 print_entry(FILE *fo, int n, int row, struct ktr_entry *entry,
323 u_int64_t *last_timestamp)
325 struct ktr_info *info = NULL;
328 fprintf(fo, " %06x ", row & 0x00FFFFFF);
330 fprintf(fo, "%-3d ", n);
331 if (tflag || rflag) {
332 if (rflag && !nflag && tsc_frequency != 0.0) {
333 fprintf(fo, "%13.3f uS ",
334 (double)(entry->ktr_timestamp - *last_timestamp) * 1000000.0 / tsc_frequency - correction_factor);
336 fprintf(fo, "%-16ju ",
337 (uintmax_t)(entry->ktr_timestamp - *last_timestamp));
339 fprintf(fo, "%-16ju ",
340 (uintmax_t)entry->ktr_timestamp);
345 fprintf(fo, "%p %p ",
346 entry->ktr_caller2, entry->ktr_caller1);
348 fprintf(fo, "%-25s ",
349 address_to_symbol(entry->ktr_caller2));
350 fprintf(fo, "%-25s ",
351 address_to_symbol(entry->ktr_caller1));
355 info = kvm_ktrinfo(entry->ktr_info);
357 fprintf(fo, "%-20s ", kvm_string(buf, info->kf_name));
359 fprintf(fo, "%-20s ", "<empty>");
362 fprintf(fo, "%34s:%-4d ", trunc_path(kvm_string(buf, entry->ktr_file), 34), entry->ktr_line);
365 info = kvm_ktrinfo(entry->ktr_info);
367 kvmfprintf(fo, kvm_string(buf, info->kf_format), (void *)&entry->ktr_data);
370 *last_timestamp = entry->ktr_timestamp;
375 kvm_ktrinfo(void *kptr)
377 static struct ktr_info save_info;
378 static void *save_kptr;
382 if (save_kptr != kptr) {
383 if (kvm_read(kd, (uintptr_t)kptr, &save_info, sizeof(save_info)) == -1) {
384 bzero(&save_info, sizeof(save_info));
394 kvm_string(char *save_str, const char *kptr)
396 static const char *save_kptr;
402 if (save_kptr != kptr) {
405 while (l < SBUFLEN - 1) {
407 ((intptr_t)(kptr + l) & SBUFMASK);
408 if (n > SBUFLEN - l - 1)
410 if (kvm_read(kd, (uintptr_t)(kptr + l), save_str + l, n) < 0)
412 while (l < SBUFLEN && n) {
413 if (save_str[l] == 0)
428 trunc_path(const char *str, int maxlen)
430 int len = strlen(str);
433 return(str + len - maxlen);
439 TAILQ_ENTRY(symdata) link;
445 static TAILQ_HEAD(symlist, symdata) symlist;
446 static struct symdata *symcache;
447 static char *symbegin;
452 read_symbols(const char *file)
456 size_t buflen = sizeof(buf);
463 TAILQ_INIT(&symlist);
466 if (sysctlbyname("kern.bootfile", buf, &buflen, NULL, 0) < 0)
467 file = "/boot/kernel";
471 snprintf(cmd, sizeof(cmd), "nm -n %s", file);
472 if ((fp = popen(cmd, "r")) != NULL) {
473 while (fgets(buf, sizeof(buf), fp) != NULL) {
474 s1 = strtok(buf, " \t\n");
475 s2 = strtok(NULL, " \t\n");
476 s3 = strtok(NULL, " \t\n");
477 if (s1 && s2 && s3) {
478 sym = malloc(sizeof(struct symdata));
479 sym->symaddr = (char *)strtoul(s1, NULL, 16);
480 sym->symtype = s2[0];
481 sym->symname = strdup(s3);
482 if (strcmp(s3, "kernbase") == 0)
483 symbegin = sym->symaddr;
484 if (strcmp(s3, "end") == 0)
485 symend = sym->symaddr;
486 TAILQ_INSERT_TAIL(&symlist, sym, link);
491 symcache = TAILQ_FIRST(&symlist);
496 address_to_symbol(void *kptr)
500 if (symcache == NULL ||
501 (char *)kptr < symbegin || (char *)kptr >= symend
503 snprintf(buf, sizeof(buf), "%p", kptr);
506 while ((char *)symcache->symaddr < (char *)kptr) {
507 if (TAILQ_NEXT(symcache, link) == NULL)
509 symcache = TAILQ_NEXT(symcache, link);
511 while ((char *)symcache->symaddr > (char *)kptr) {
512 if (symcache != TAILQ_FIRST(&symlist))
513 symcache = TAILQ_PREV(symcache, symlist, link);
515 snprintf(buf, sizeof(buf), "%s+%d", symcache->symname,
516 (int)((char *)kptr - symcache->symaddr));
524 struct ktr_buffer *ktr_bufs, *it;
527 ktr_bufs = malloc(sizeof(*ktr_bufs) * ncpus);
529 err(1, "can't allocate data structures\n");
530 for (i = 0; i < ncpus; ++i) {
532 it->ents = malloc(sizeof(struct ktr_entry) * entries_per_buf);
533 if (it->ents == NULL)
534 err(1, "can't allocate data structures\n");
544 get_indices(struct ktr_entry **ktr_kbuf, int *ktr_idx)
546 static struct ktr_cpu *ktr_cpus;
549 if (ktr_cpus == NULL)
550 ktr_cpus = malloc(sizeof(*ktr_cpus) * ncpus);
552 if (ktr_version < KTR_VERSION_KTR_CPU) {
553 if (kvm_read(kd, nl_version_ktr_idx[0].n_value, ktr_idx,
554 sizeof(*ktr_idx) * ncpus) == -1) {
555 errx(1, "%s", kvm_geterr(kd));
557 if (ktr_kbuf[0] == NULL) {
558 if (kvm_read(kd, nl_version_ktr_idx[1].n_value,
559 ktr_kbuf, sizeof(*ktr_kbuf) * ncpus) == -1) {
560 errx(1, "%s", kvm_geterr(kd));
564 if (kvm_read(kd, nl_version_ktr_cpu[0].n_value,
565 ktr_cpus, sizeof(*ktr_cpus) * ncpus) == -1) {
566 errx(1, "%s", kvm_geterr(kd));
568 for (i = 0; i < ncpus; ++i) {
569 ktr_idx[i] = ktr_cpus[i].core.ktr_idx;
570 ktr_kbuf[i] = ktr_cpus[i].core.ktr_buf;
576 * Get the trace buffer data from the kernel
580 load_bufs(struct ktr_buffer *ktr_bufs, struct ktr_entry **kbufs, int *ktr_idx)
582 struct ktr_buffer *kbuf;
585 get_indices(kbufs, ktr_idx);
586 for (i = 0; i < ncpus; ++i) {
588 if (ktr_idx[i] == kbuf->end_idx)
590 kbuf->end_idx = ktr_idx[i];
593 * If we do not have a notion of the beginning index, assume
594 * it is entries_per_buf before the ending index. Don't
595 * worry about underflows/negative numbers, the indices will
599 kbuf->beg_idx = kbuf->end_idx - entries_per_buf + 1;
602 if (kvm_read(kd, (uintptr_t)kbufs[i], ktr_bufs[i].ents,
603 sizeof(struct ktr_entry) * entries_per_buf)
605 errx(1, "%s", kvm_geterr(kd));
607 kbuf->beg_idx = earliest_ts(kbuf);
613 * Locate the earliest timestamp iterating backwards from end_idx, but
614 * not going further back then beg_idx. We have to do this because
615 * the kernel uses a circulating buffer.
619 earliest_ts(struct ktr_buffer *buf)
621 struct ktr_entry *save;
622 int count, scan, i, earliest;
625 earliest = buf->end_idx - 1;
626 save = &buf->ents[earliest & fifo_mask];
627 for (scan = buf->end_idx - 1; scan != buf->beg_idx -1; --scan) {
628 i = scan & fifo_mask;
629 if (buf->ents[i].ktr_timestamp <= save->ktr_timestamp &&
630 buf->ents[i].ktr_timestamp > 0)
633 * We may have gotten so far behind that beg_idx wrapped
634 * more then once around the buffer. Just stop
636 if (++count == entries_per_buf)
644 print_buf(FILE *fo, struct ktr_buffer *ktr_bufs, int cpu,
645 u_int64_t *last_timestamp)
647 struct ktr_buffer *buf = ktr_bufs + cpu;
649 if (buf->modified == 0)
651 if (*last_timestamp == 0) {
653 buf->ents[buf->beg_idx & fifo_mask].ktr_timestamp;
655 while (buf->beg_idx != buf->end_idx) {
656 print_header(fo, buf->beg_idx);
657 print_entry(fo, cpu, buf->beg_idx,
658 &buf->ents[buf->beg_idx & fifo_mask],
667 print_bufs_timesorted(FILE *fo, struct ktr_buffer *ktr_bufs,
668 u_int64_t *last_timestamp)
670 struct ktr_entry *ent;
671 struct ktr_buffer *buf;
679 for (n = 0; n < ncpus; ++n) {
681 if (buf->beg_idx == buf->end_idx)
683 ent = &buf->ents[buf->beg_idx & fifo_mask];
684 if (ts == 0 || (ts >= ent->ktr_timestamp)) {
685 ts = ent->ktr_timestamp;
689 if ((bestn < 0) || (ts < *last_timestamp))
691 buf = ktr_bufs + bestn;
692 print_header(fo, row);
693 print_entry(fo, bestn, row,
694 &buf->ents[buf->beg_idx & fifo_mask],
697 *last_timestamp = ts;
704 kvmfprintf(FILE *fp, const char *ctl, va_list va)
713 for (n = 0; ctl[n]; ++n) {
722 while (n < (int)sizeof(fmt)) {
749 va_arg(va, long long));
763 kvm_string(buf, va_arg(va, char *));
764 fwrite(buf, 1, strlen(buf), fp);
809 fprintf(fp, fmt, NULL);
818 fprintf(stderr, "usage: ktrdump [-acfilnpqrstx] [-A factor] "
819 "[-N execfile] [-M corefile] [-o outfile]\n");