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>
54 struct ktr_entry *ents;
57 int beg_idx; /* Beginning index */
58 int end_idx; /* Ending index */
61 static struct nlist nl1[] = {
62 { .n_name = "_ktr_version" },
63 { .n_name = "_ktr_entries" },
64 { .n_name = "_ncpus" },
68 static struct nlist nl2[] = {
69 { .n_name = "_tsc_frequency" },
73 static struct nlist nl_version_ktr_idx[] = {
74 { .n_name = "_ktr_idx" },
75 { .n_name = "_ktr_buf" },
79 static struct nlist nl_version_ktr_cpu[] = {
80 { .n_name = "_ktr_cpu" },
86 const void *save_kptr;
89 typedef void (*ktr_iter_cb_t)(void *, int, int, struct ktr_entry *, uint64_t *);
105 static double tsc_frequency;
106 static double correction_factor = 0.0;
108 static char corefile[PATH_MAX];
109 static char execfile[PATH_MAX];
111 static char errbuf[_POSIX2_LINE_MAX];
114 static int entries_per_buf;
115 static int fifo_mask;
116 static int ktr_version;
118 static void usage(void);
119 static int earliest_ts(struct ktr_buffer *);
120 static void dump_machine_info(evtr_t);
121 static void print_header(FILE *, int);
122 static void print_entry(FILE *, int, int, struct ktr_entry *, u_int64_t *);
123 static void print_callback(void *, int, int, struct ktr_entry *, uint64_t *);
124 static void dump_callback(void *, int, int, struct ktr_entry *, uint64_t *);
125 static struct ktr_info *kvm_ktrinfo(void *, struct save_ctx *);
126 static const char *kvm_string(const char *, struct save_ctx *);
127 static const char *trunc_path(const char *, int);
128 static void read_symbols(const char *);
129 static const char *address_to_symbol(void *, struct save_ctx *);
130 static struct ktr_buffer *ktr_bufs_init(void);
131 static void get_indices(struct ktr_entry **, int *);
132 static void load_bufs(struct ktr_buffer *, struct ktr_entry **, int *);
133 static void iterate_buf(FILE *, struct ktr_buffer *, int, u_int64_t *, ktr_iter_cb_t);
134 static void iterate_bufs_timesorted(FILE *, struct ktr_buffer *, u_int64_t *, ktr_iter_cb_t);
135 static void kvmfprintf(FILE *fp, const char *ctl, va_list va);
138 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
141 main(int ac, char **av)
143 struct ktr_buffer *ktr_bufs;
144 struct ktr_entry **ktr_kbuf;
145 ktr_iter_cb_t callback = &print_callback;
150 int *ktr_start_index;
155 * Parse commandline arguments.
158 while ((c = getopt(ac, av, "acfinqrtxpslA:N:M:o:d")) != -1) {
174 callback = &dump_callback;
177 if (strlcpy(execfile, optarg, sizeof(execfile))
179 errx(1, "%s: File name too long", optarg);
192 correction_factor = strtod(optarg, NULL);
195 if (strlcpy(corefile, optarg, sizeof(corefile))
197 errx(1, "%s: File name too long", optarg);
204 if ((fo = fopen(optarg, "w")) == NULL)
205 err(1, "%s", optarg);
217 sflag = 1; /* sort across the cpus */
232 ctx = evtr_open_write(fo);
234 err(1, "Can't create event stream");
237 if (cflag + iflag + tflag + xflag + fflag + pflag == 0) {
243 if (correction_factor != 0.0 && (rflag == 0 || nflag)) {
244 fprintf(stderr, "Correction factor can only be applied with -r and without -n\n");
253 * Open our execfile and corefile, resolve needed symbols and read in
256 if ((kd = kvm_openfiles(Nflag ? execfile : NULL,
257 Mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
258 errx(1, "%s", errbuf);
259 if (kvm_nlist(kd, nl1) != 0)
260 errx(1, "%s", kvm_geterr(kd));
261 if (kvm_read(kd, nl1[0].n_value, &ktr_version, sizeof(ktr_version)) == -1)
262 errx(1, "%s", kvm_geterr(kd));
263 if (kvm_read(kd, nl1[2].n_value, &ncpus, sizeof(ncpus)) == -1)
264 errx(1, "%s", kvm_geterr(kd));
265 ktr_start_index = malloc(sizeof(*ktr_start_index) * ncpus);
266 if (ktr_version >= KTR_VERSION_WITH_FREQ && kvm_nlist(kd, nl2) == 0) {
267 if (kvm_read(kd, nl2[0].n_value, &tts, sizeof(tts)) == -1)
268 errx(1, "%s", kvm_geterr(kd));
269 tsc_frequency = (double)tts;
271 if (ktr_version > KTR_VERSION)
272 errx(1, "ktr version too high for us to handle");
273 if (kvm_read(kd, nl1[1].n_value, &entries_per_buf,
274 sizeof(entries_per_buf)) == -1)
275 errx(1, "%s", kvm_geterr(kd));
276 fifo_mask = entries_per_buf - 1;
278 printf("TSC frequency is %6.3f MHz\n", tsc_frequency / 1000000.0);
281 dump_machine_info((evtr_t)ctx);
283 ktr_kbuf = calloc(ncpus, sizeof(*ktr_kbuf));
284 ktr_idx = calloc(ncpus, sizeof(*ktr_idx));
287 read_symbols(Nflag ? execfile : NULL);
289 if (ktr_version < KTR_VERSION_KTR_CPU) {
290 if (kvm_nlist(kd, nl_version_ktr_idx))
291 errx(1, "%s", kvm_geterr(kd));
293 if (kvm_nlist(kd, nl_version_ktr_cpu))
294 errx(1, "%s", kvm_geterr(kd));
297 get_indices(ktr_kbuf, ktr_idx);
299 ktr_bufs = ktr_bufs_init();
302 u_int64_t last_timestamp = 0;
304 load_bufs(ktr_bufs, ktr_kbuf, ktr_idx);
305 iterate_bufs_timesorted(ctx, ktr_bufs, &last_timestamp,
308 usleep(1000000 / 10);
311 u_int64_t *last_timestamp = calloc(sizeof(u_int64_t), ncpus);
313 load_bufs(ktr_bufs, ktr_kbuf, ktr_idx);
314 for (n = 0; n < ncpus; ++n)
315 iterate_buf(ctx, ktr_bufs, n, &last_timestamp[n],
318 usleep(1000000 / 10);
328 dump_machine_info(evtr_t evtr)
330 struct evtr_event ev;
332 ev.type = EVTR_TYPE_CPUINFO;
335 evtr_dump_event(evtr, &ev);
339 print_header(FILE *fo, int row)
341 if (qflag == 0 && (u_int32_t)row % 20 == 0) {
342 fprintf(fo, "%-6s ", "index");
344 fprintf(fo, "%-3s ", "cpu");
346 fprintf(fo, "%-16s ", "timestamp");
349 fprintf(fo, "%-10s %-10s", "caller2", "caller1");
351 fprintf(fo, "%-20s %-20s", "caller2", "caller1");
354 fprintf(fo, "%-20s ", "ID");
356 fprintf(fo, "%10s%-30s ", "", "file and line");
358 fprintf(fo, "%s", "trace");
364 print_entry(FILE *fo, int n, int row, struct ktr_entry *entry,
365 u_int64_t *last_timestamp)
367 struct ktr_info *info = NULL;
368 static struct save_ctx nctx, pctx, fmtctx, symctx, infoctx;
370 fprintf(fo, " %06x ", row & 0x00FFFFFF);
372 fprintf(fo, "%-3d ", n);
373 if (tflag || rflag) {
374 if (rflag && !nflag && tsc_frequency != 0.0) {
375 fprintf(fo, "%13.3f uS ",
376 (double)(entry->ktr_timestamp - *last_timestamp) * 1000000.0 / tsc_frequency - correction_factor);
378 fprintf(fo, "%-16ju ",
379 (uintmax_t)(entry->ktr_timestamp - *last_timestamp));
381 fprintf(fo, "%-16ju ",
382 (uintmax_t)entry->ktr_timestamp);
387 fprintf(fo, "%p %p ",
388 entry->ktr_caller2, entry->ktr_caller1);
390 fprintf(fo, "%-25s ",
391 address_to_symbol(entry->ktr_caller2, &symctx));
392 fprintf(fo, "%-25s ",
393 address_to_symbol(entry->ktr_caller1, &symctx));
397 info = kvm_ktrinfo(entry->ktr_info, &infoctx);
399 fprintf(fo, "%-20s ", kvm_string(info->kf_name, &nctx));
401 fprintf(fo, "%-20s ", "<empty>");
404 fprintf(fo, "%34s:%-4d ",
405 trunc_path(kvm_string(entry->ktr_file, &pctx), 34),
409 info = kvm_ktrinfo(entry->ktr_info, &infoctx);
411 kvmfprintf(fo, kvm_string(info->kf_format, &fmtctx),
412 (void *)&entry->ktr_data);
415 *last_timestamp = entry->ktr_timestamp;
420 print_callback(void *ctx, int n, int row, struct ktr_entry *entry, uint64_t *last_ts)
422 FILE *fo = (FILE *)ctx;
423 print_header(fo, row);
424 print_entry(fo, n, row, entry, last_ts);
428 * If free == 0, replace all (kvm) string pointers in fmtdata with pointers
429 * to user-allocated copies of the strings.
430 * If free != 0, free those pointers.
434 mangle_string_ptrs(const char *fmt, uint8_t *fmtdata, int dofree)
437 size_t skipsize, intsz;
438 static struct save_ctx strctx;
441 for (f = fmt; f[0] != '\0'; ++f) {
446 for (p = f; p[0]; ++p) {
449 * Eat flags. Notice this will accept duplicate
465 /* Eat minimum field width, if any */
466 for (; isdigit(p[0]); ++p)
470 /* Eat precision, if any */
471 for (; isdigit(p[0]); ++p)
478 intsz = sizeof(long long);
480 intsz = sizeof(long);
484 intsz = sizeof(intmax_t);
487 intsz = sizeof(ptrdiff_t);
490 intsz = sizeof(size_t);
511 skipsize = sizeof(void *);
515 skipsize = sizeof(double);
517 skipsize = sizeof(float);
521 char *t = ((char **)fmtdata)[0];
523 skipsize = sizeof(char *);
525 char *t = strdup(kvm_string(((char **)fmtdata)[0],
527 ((const char **)fmtdata)[0] = t;
529 skipsize = sizeof(char *);
534 fprintf(stderr, "Unknown conversion specifier %c "
535 "in fmt starting with %s", p[0], f - 1);
545 dump_callback(void *ctx, int n, int row __unused, struct ktr_entry *entry,
546 uint64_t *last_ts __unused)
548 evtr_t evtr = (evtr_t)ctx;
549 struct evtr_event ev;
550 static struct save_ctx pctx, fmtctx, infoctx;
552 int conv = 0; /* pointless */
554 ev.ts = entry->ktr_timestamp;
555 ev.type = EVTR_TYPE_PROBE;
556 ev.line = entry->ktr_line;
557 ev.file = kvm_string(entry->ktr_file, &pctx);
560 if ((ki = kvm_ktrinfo(entry->ktr_info, &infoctx))) {
561 ev.fmt = kvm_string(ki->kf_format, &fmtctx);
562 ev.fmtdata = entry->ktr_data;
563 if ((conv = mangle_string_ptrs(ev.fmt,
564 __DECONST(uint8_t *, ev.fmtdata),
566 errx(1, "Can't parse format string\n");
567 ev.fmtdatalen = ki->kf_data_size;
569 ev.fmt = ev.fmtdata = NULL;
572 if (evtr_dump_event(evtr, &ev)) {
573 err(1, evtr_errmsg(evtr));
575 if (ev.fmtdata && conv) {
576 mangle_string_ptrs(ev.fmt, __DECONST(uint8_t *, ev.fmtdata),
583 kvm_ktrinfo(void *kptr, struct save_ctx *ctx)
585 struct ktr_info *ki = (void *)ctx->save_buf;
589 if (ctx->save_kptr != kptr) {
590 if (kvm_read(kd, (uintptr_t)kptr, ki, sizeof(*ki)) == -1) {
591 bzero(&ki, sizeof(*ki));
593 ctx->save_kptr = kptr;
601 kvm_string(const char *kptr, struct save_ctx *ctx)
608 if (ctx->save_kptr != (const void *)kptr) {
609 ctx->save_kptr = (const void *)kptr;
611 while (l < sizeof(ctx->save_buf) - 1) {
612 n = 256 - ((intptr_t)(kptr + l) & 255);
613 if (n > sizeof(ctx->save_buf) - l - 1)
614 n = sizeof(ctx->save_buf) - l - 1;
615 if (kvm_read(kd, (uintptr_t)(kptr + l), ctx->save_buf + l, n) < 0)
617 while (l < sizeof(ctx->save_buf) && n) {
618 if (ctx->save_buf[l] == 0)
626 ctx->save_buf[l] = 0;
628 return(ctx->save_buf);
633 trunc_path(const char *str, int maxlen)
635 int len = strlen(str);
638 return(str + len - maxlen);
644 TAILQ_ENTRY(symdata) link;
650 static TAILQ_HEAD(symlist, symdata) symlist;
651 static struct symdata *symcache;
652 static char *symbegin;
657 read_symbols(const char *file)
661 size_t buflen = sizeof(buf);
668 TAILQ_INIT(&symlist);
671 if (sysctlbyname("kern.bootfile", buf, &buflen, NULL, 0) < 0)
672 file = "/boot/kernel";
676 snprintf(cmd, sizeof(cmd), "nm -n %s", file);
677 if ((fp = popen(cmd, "r")) != NULL) {
678 while (fgets(buf, sizeof(buf), fp) != NULL) {
679 s1 = strtok(buf, " \t\n");
680 s2 = strtok(NULL, " \t\n");
681 s3 = strtok(NULL, " \t\n");
682 if (s1 && s2 && s3) {
683 sym = malloc(sizeof(struct symdata));
684 sym->symaddr = (char *)strtoul(s1, NULL, 16);
685 sym->symtype = s2[0];
686 sym->symname = strdup(s3);
687 if (strcmp(s3, "kernbase") == 0)
688 symbegin = sym->symaddr;
689 if (strcmp(s3, "end") == 0)
690 symend = sym->symaddr;
691 TAILQ_INSERT_TAIL(&symlist, sym, link);
696 symcache = TAILQ_FIRST(&symlist);
701 address_to_symbol(void *kptr, struct save_ctx *ctx)
703 char *buf = ctx->save_buf;
704 int size = sizeof(ctx->save_buf);
706 if (symcache == NULL ||
707 (char *)kptr < symbegin || (char *)kptr >= symend
709 snprintf(buf, size, "%p", kptr);
712 while ((char *)symcache->symaddr < (char *)kptr) {
713 if (TAILQ_NEXT(symcache, link) == NULL)
715 symcache = TAILQ_NEXT(symcache, link);
717 while ((char *)symcache->symaddr > (char *)kptr) {
718 if (symcache != TAILQ_FIRST(&symlist))
719 symcache = TAILQ_PREV(symcache, symlist, link);
721 snprintf(buf, size, "%s+%d", symcache->symname,
722 (int)((char *)kptr - symcache->symaddr));
730 struct ktr_buffer *ktr_bufs, *it;
733 ktr_bufs = malloc(sizeof(*ktr_bufs) * ncpus);
735 err(1, "can't allocate data structures\n");
736 for (i = 0; i < ncpus; ++i) {
738 it->ents = malloc(sizeof(struct ktr_entry) * entries_per_buf);
739 if (it->ents == NULL)
740 err(1, "can't allocate data structures\n");
750 get_indices(struct ktr_entry **ktr_kbuf, int *ktr_idx)
752 static struct ktr_cpu *ktr_cpus;
755 if (ktr_cpus == NULL)
756 ktr_cpus = malloc(sizeof(*ktr_cpus) * ncpus);
758 if (ktr_version < KTR_VERSION_KTR_CPU) {
759 if (kvm_read(kd, nl_version_ktr_idx[0].n_value, ktr_idx,
760 sizeof(*ktr_idx) * ncpus) == -1) {
761 errx(1, "%s", kvm_geterr(kd));
763 if (ktr_kbuf[0] == NULL) {
764 if (kvm_read(kd, nl_version_ktr_idx[1].n_value,
765 ktr_kbuf, sizeof(*ktr_kbuf) * ncpus) == -1) {
766 errx(1, "%s", kvm_geterr(kd));
770 if (kvm_read(kd, nl_version_ktr_cpu[0].n_value,
771 ktr_cpus, sizeof(*ktr_cpus) * ncpus) == -1) {
772 errx(1, "%s", kvm_geterr(kd));
774 for (i = 0; i < ncpus; ++i) {
775 ktr_idx[i] = ktr_cpus[i].core.ktr_idx;
776 ktr_kbuf[i] = ktr_cpus[i].core.ktr_buf;
782 * Get the trace buffer data from the kernel
786 load_bufs(struct ktr_buffer *ktr_bufs, struct ktr_entry **kbufs, int *ktr_idx)
788 struct ktr_buffer *kbuf;
791 get_indices(kbufs, ktr_idx);
792 for (i = 0; i < ncpus; ++i) {
794 if (ktr_idx[i] == kbuf->end_idx)
796 kbuf->end_idx = ktr_idx[i];
799 * If we do not have a notion of the beginning index, assume
800 * it is entries_per_buf before the ending index. Don't
801 * worry about underflows/negative numbers, the indices will
805 kbuf->beg_idx = kbuf->end_idx - entries_per_buf + 1;
808 if (kvm_read(kd, (uintptr_t)kbufs[i], ktr_bufs[i].ents,
809 sizeof(struct ktr_entry) * entries_per_buf)
811 errx(1, "%s", kvm_geterr(kd));
813 kbuf->beg_idx = earliest_ts(kbuf);
819 * Locate the earliest timestamp iterating backwards from end_idx, but
820 * not going further back then beg_idx. We have to do this because
821 * the kernel uses a circulating buffer.
825 earliest_ts(struct ktr_buffer *buf)
827 struct ktr_entry *save;
828 int count, scan, i, earliest;
831 earliest = buf->end_idx - 1;
832 save = &buf->ents[earliest & fifo_mask];
833 for (scan = buf->end_idx - 1; scan != buf->beg_idx -1; --scan) {
834 i = scan & fifo_mask;
835 if (buf->ents[i].ktr_timestamp <= save->ktr_timestamp &&
836 buf->ents[i].ktr_timestamp > 0)
839 * We may have gotten so far behind that beg_idx wrapped
840 * more then once around the buffer. Just stop
842 if (++count == entries_per_buf)
850 iterate_buf(FILE *fo, struct ktr_buffer *ktr_bufs, int cpu,
851 u_int64_t *last_timestamp, ktr_iter_cb_t cb)
853 struct ktr_buffer *buf = ktr_bufs + cpu;
855 if (buf->modified == 0)
857 if (*last_timestamp == 0) {
859 buf->ents[buf->beg_idx & fifo_mask].ktr_timestamp;
861 while (buf->beg_idx != buf->end_idx) {
862 cb(fo, cpu, buf->beg_idx,
863 &buf->ents[buf->beg_idx & fifo_mask],
872 iterate_bufs_timesorted(FILE *fo, struct ktr_buffer *ktr_bufs,
873 u_int64_t *last_timestamp, ktr_iter_cb_t cb)
875 struct ktr_entry *ent;
876 struct ktr_buffer *buf;
884 for (n = 0; n < ncpus; ++n) {
886 if (buf->beg_idx == buf->end_idx)
888 ent = &buf->ents[buf->beg_idx & fifo_mask];
889 if (ts == 0 || (ts >= ent->ktr_timestamp)) {
890 ts = ent->ktr_timestamp;
894 if ((bestn < 0) || (ts < *last_timestamp))
896 buf = ktr_bufs + bestn;
898 &buf->ents[buf->beg_idx & fifo_mask],
901 *last_timestamp = ts;
908 kvmfprintf(FILE *fp, const char *ctl, va_list va)
914 static struct save_ctx strctx;
918 for (n = 0; ctl[n]; ++n) {
927 while (n < (int)sizeof(fmt)) {
954 va_arg(va, long long));
968 s = kvm_string(va_arg(va, char *), &strctx);
969 fwrite(s, 1, strlen(s), fp);
1014 fprintf(fp, fmt, NULL);
1023 fprintf(stderr, "usage: ktrdump [-acfilnpqrstx] [-A factor] "
1024 "[-N execfile] [-M corefile] [-o outfile]\n");