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;
333 bzero(&ev, sizeof(ev));
334 ev.type = EVTR_TYPE_SYSINFO;
336 evtr_dump_event(evtr, &ev);
337 if (evtr_error(evtr)) {
338 err(1, evtr_errmsg(evtr));
341 for (i = 0; i < ncpus; ++i) {
342 bzero(&ev, sizeof(ev));
343 ev.type = EVTR_TYPE_CPUINFO;
345 ev.cpuinfo.freq = tsc_frequency;
346 evtr_dump_event(evtr, &ev);
347 if (evtr_error(evtr)) {
348 err(1, evtr_errmsg(evtr));
354 print_header(FILE *fo, int row)
356 if (qflag == 0 && (u_int32_t)row % 20 == 0) {
357 fprintf(fo, "%-6s ", "index");
359 fprintf(fo, "%-3s ", "cpu");
361 fprintf(fo, "%-16s ", "timestamp");
364 fprintf(fo, "%-10s %-10s", "caller2", "caller1");
366 fprintf(fo, "%-20s %-20s", "caller2", "caller1");
369 fprintf(fo, "%-20s ", "ID");
371 fprintf(fo, "%10s%-30s ", "", "file and line");
373 fprintf(fo, "%s", "trace");
379 print_entry(FILE *fo, int n, int row, struct ktr_entry *entry,
380 u_int64_t *last_timestamp)
382 struct ktr_info *info = NULL;
383 static struct save_ctx nctx, pctx, fmtctx, symctx, infoctx;
385 fprintf(fo, " %06x ", row & 0x00FFFFFF);
387 fprintf(fo, "%-3d ", n);
388 if (tflag || rflag) {
389 if (rflag && !nflag && tsc_frequency != 0.0) {
390 fprintf(fo, "%13.3f uS ",
391 (double)(entry->ktr_timestamp - *last_timestamp) * 1000000.0 / tsc_frequency - correction_factor);
393 fprintf(fo, "%-16ju ",
394 (uintmax_t)(entry->ktr_timestamp - *last_timestamp));
396 fprintf(fo, "%-16ju ",
397 (uintmax_t)entry->ktr_timestamp);
402 fprintf(fo, "%p %p ",
403 entry->ktr_caller2, entry->ktr_caller1);
405 fprintf(fo, "%-25s ",
406 address_to_symbol(entry->ktr_caller2, &symctx));
407 fprintf(fo, "%-25s ",
408 address_to_symbol(entry->ktr_caller1, &symctx));
412 info = kvm_ktrinfo(entry->ktr_info, &infoctx);
414 fprintf(fo, "%-20s ", kvm_string(info->kf_name, &nctx));
416 fprintf(fo, "%-20s ", "<empty>");
419 fprintf(fo, "%34s:%-4d ",
420 trunc_path(kvm_string(entry->ktr_file, &pctx), 34),
424 info = kvm_ktrinfo(entry->ktr_info, &infoctx);
426 kvmfprintf(fo, kvm_string(info->kf_format, &fmtctx),
427 (void *)&entry->ktr_data);
430 *last_timestamp = entry->ktr_timestamp;
435 print_callback(void *ctx, int n, int row, struct ktr_entry *entry, uint64_t *last_ts)
437 FILE *fo = (FILE *)ctx;
438 print_header(fo, row);
439 print_entry(fo, n, row, entry, last_ts);
443 * If free == 0, replace all (kvm) string pointers in fmtdata with pointers
444 * to user-allocated copies of the strings.
445 * If free != 0, free those pointers.
449 mangle_string_ptrs(const char *fmt, uint8_t *fmtdata, int dofree)
452 size_t skipsize, intsz;
453 static struct save_ctx strctx;
456 for (f = fmt; f[0] != '\0'; ++f) {
461 for (p = f; p[0]; ++p) {
464 * Eat flags. Notice this will accept duplicate
480 /* Eat minimum field width, if any */
481 for (; isdigit(p[0]); ++p)
485 /* Eat precision, if any */
486 for (; isdigit(p[0]); ++p)
493 intsz = sizeof(long long);
495 intsz = sizeof(long);
499 intsz = sizeof(intmax_t);
502 intsz = sizeof(ptrdiff_t);
505 intsz = sizeof(size_t);
526 skipsize = sizeof(void *);
530 skipsize = sizeof(double);
532 skipsize = sizeof(float);
536 char *t = ((char **)fmtdata)[0];
538 skipsize = sizeof(char *);
540 char *t = strdup(kvm_string(((char **)fmtdata)[0],
542 ((const char **)fmtdata)[0] = t;
544 skipsize = sizeof(char *);
549 fprintf(stderr, "Unknown conversion specifier %c "
550 "in fmt starting with %s", p[0], f - 1);
560 dump_callback(void *ctx, int n, int row __unused, struct ktr_entry *entry,
561 uint64_t *last_ts __unused)
563 evtr_t evtr = (evtr_t)ctx;
564 struct evtr_event ev;
565 static struct save_ctx pctx, fmtctx, infoctx;
567 int conv = 0; /* pointless */
569 ev.ts = entry->ktr_timestamp;
570 ev.type = EVTR_TYPE_PROBE;
571 ev.line = entry->ktr_line;
572 ev.file = kvm_string(entry->ktr_file, &pctx);
575 if ((ki = kvm_ktrinfo(entry->ktr_info, &infoctx))) {
576 ev.fmt = kvm_string(ki->kf_format, &fmtctx);
577 ev.fmtdata = entry->ktr_data;
578 if ((conv = mangle_string_ptrs(ev.fmt,
579 __DECONST(uint8_t *, ev.fmtdata),
581 errx(1, "Can't parse format string\n");
582 ev.fmtdatalen = ki->kf_data_size;
584 ev.fmt = ev.fmtdata = NULL;
587 if (evtr_dump_event(evtr, &ev)) {
588 err(1, evtr_errmsg(evtr));
590 if (ev.fmtdata && conv) {
591 mangle_string_ptrs(ev.fmt, __DECONST(uint8_t *, ev.fmtdata),
598 kvm_ktrinfo(void *kptr, struct save_ctx *ctx)
600 struct ktr_info *ki = (void *)ctx->save_buf;
604 if (ctx->save_kptr != kptr) {
605 if (kvm_read(kd, (uintptr_t)kptr, ki, sizeof(*ki)) == -1) {
606 bzero(&ki, sizeof(*ki));
608 ctx->save_kptr = kptr;
616 kvm_string(const char *kptr, struct save_ctx *ctx)
623 if (ctx->save_kptr != (const void *)kptr) {
624 ctx->save_kptr = (const void *)kptr;
626 while (l < sizeof(ctx->save_buf) - 1) {
627 n = 256 - ((intptr_t)(kptr + l) & 255);
628 if (n > sizeof(ctx->save_buf) - l - 1)
629 n = sizeof(ctx->save_buf) - l - 1;
630 if (kvm_read(kd, (uintptr_t)(kptr + l), ctx->save_buf + l, n) < 0)
632 while (l < sizeof(ctx->save_buf) && n) {
633 if (ctx->save_buf[l] == 0)
641 ctx->save_buf[l] = 0;
643 return(ctx->save_buf);
648 trunc_path(const char *str, int maxlen)
650 int len = strlen(str);
653 return(str + len - maxlen);
659 TAILQ_ENTRY(symdata) link;
665 static TAILQ_HEAD(symlist, symdata) symlist;
666 static struct symdata *symcache;
667 static char *symbegin;
672 read_symbols(const char *file)
676 size_t buflen = sizeof(buf);
683 TAILQ_INIT(&symlist);
686 if (sysctlbyname("kern.bootfile", buf, &buflen, NULL, 0) < 0)
687 file = "/boot/kernel";
691 snprintf(cmd, sizeof(cmd), "nm -n %s", file);
692 if ((fp = popen(cmd, "r")) != NULL) {
693 while (fgets(buf, sizeof(buf), fp) != NULL) {
694 s1 = strtok(buf, " \t\n");
695 s2 = strtok(NULL, " \t\n");
696 s3 = strtok(NULL, " \t\n");
697 if (s1 && s2 && s3) {
698 sym = malloc(sizeof(struct symdata));
699 sym->symaddr = (char *)strtoul(s1, NULL, 16);
700 sym->symtype = s2[0];
701 sym->symname = strdup(s3);
702 if (strcmp(s3, "kernbase") == 0)
703 symbegin = sym->symaddr;
704 if (strcmp(s3, "end") == 0)
705 symend = sym->symaddr;
706 TAILQ_INSERT_TAIL(&symlist, sym, link);
711 symcache = TAILQ_FIRST(&symlist);
716 address_to_symbol(void *kptr, struct save_ctx *ctx)
718 char *buf = ctx->save_buf;
719 int size = sizeof(ctx->save_buf);
721 if (symcache == NULL ||
722 (char *)kptr < symbegin || (char *)kptr >= symend
724 snprintf(buf, size, "%p", kptr);
727 while ((char *)symcache->symaddr < (char *)kptr) {
728 if (TAILQ_NEXT(symcache, link) == NULL)
730 symcache = TAILQ_NEXT(symcache, link);
732 while ((char *)symcache->symaddr > (char *)kptr) {
733 if (symcache != TAILQ_FIRST(&symlist))
734 symcache = TAILQ_PREV(symcache, symlist, link);
736 snprintf(buf, size, "%s+%d", symcache->symname,
737 (int)((char *)kptr - symcache->symaddr));
745 struct ktr_buffer *ktr_bufs, *it;
748 ktr_bufs = malloc(sizeof(*ktr_bufs) * ncpus);
750 err(1, "can't allocate data structures\n");
751 for (i = 0; i < ncpus; ++i) {
753 it->ents = malloc(sizeof(struct ktr_entry) * entries_per_buf);
754 if (it->ents == NULL)
755 err(1, "can't allocate data structures\n");
765 get_indices(struct ktr_entry **ktr_kbuf, int *ktr_idx)
767 static struct ktr_cpu *ktr_cpus;
770 if (ktr_cpus == NULL)
771 ktr_cpus = malloc(sizeof(*ktr_cpus) * ncpus);
773 if (ktr_version < KTR_VERSION_KTR_CPU) {
774 if (kvm_read(kd, nl_version_ktr_idx[0].n_value, ktr_idx,
775 sizeof(*ktr_idx) * ncpus) == -1) {
776 errx(1, "%s", kvm_geterr(kd));
778 if (ktr_kbuf[0] == NULL) {
779 if (kvm_read(kd, nl_version_ktr_idx[1].n_value,
780 ktr_kbuf, sizeof(*ktr_kbuf) * ncpus) == -1) {
781 errx(1, "%s", kvm_geterr(kd));
785 if (kvm_read(kd, nl_version_ktr_cpu[0].n_value,
786 ktr_cpus, sizeof(*ktr_cpus) * ncpus) == -1) {
787 errx(1, "%s", kvm_geterr(kd));
789 for (i = 0; i < ncpus; ++i) {
790 ktr_idx[i] = ktr_cpus[i].core.ktr_idx;
791 ktr_kbuf[i] = ktr_cpus[i].core.ktr_buf;
797 * Get the trace buffer data from the kernel
801 load_bufs(struct ktr_buffer *ktr_bufs, struct ktr_entry **kbufs, int *ktr_idx)
803 struct ktr_buffer *kbuf;
806 get_indices(kbufs, ktr_idx);
807 for (i = 0; i < ncpus; ++i) {
809 if (ktr_idx[i] == kbuf->end_idx)
811 kbuf->end_idx = ktr_idx[i];
814 * If we do not have a notion of the beginning index, assume
815 * it is entries_per_buf before the ending index. Don't
816 * worry about underflows/negative numbers, the indices will
820 kbuf->beg_idx = kbuf->end_idx - entries_per_buf + 1;
823 if (kvm_read(kd, (uintptr_t)kbufs[i], ktr_bufs[i].ents,
824 sizeof(struct ktr_entry) * entries_per_buf)
826 errx(1, "%s", kvm_geterr(kd));
828 kbuf->beg_idx = earliest_ts(kbuf);
834 * Locate the earliest timestamp iterating backwards from end_idx, but
835 * not going further back then beg_idx. We have to do this because
836 * the kernel uses a circulating buffer.
840 earliest_ts(struct ktr_buffer *buf)
842 struct ktr_entry *save;
843 int count, scan, i, earliest;
846 earliest = buf->end_idx - 1;
847 save = &buf->ents[earliest & fifo_mask];
848 for (scan = buf->end_idx - 1; scan != buf->beg_idx -1; --scan) {
849 i = scan & fifo_mask;
850 if (buf->ents[i].ktr_timestamp <= save->ktr_timestamp &&
851 buf->ents[i].ktr_timestamp > 0)
854 * We may have gotten so far behind that beg_idx wrapped
855 * more then once around the buffer. Just stop
857 if (++count == entries_per_buf)
865 iterate_buf(FILE *fo, struct ktr_buffer *ktr_bufs, int cpu,
866 u_int64_t *last_timestamp, ktr_iter_cb_t cb)
868 struct ktr_buffer *buf = ktr_bufs + cpu;
870 if (buf->modified == 0)
872 if (*last_timestamp == 0) {
874 buf->ents[buf->beg_idx & fifo_mask].ktr_timestamp;
876 while (buf->beg_idx != buf->end_idx) {
877 cb(fo, cpu, buf->beg_idx,
878 &buf->ents[buf->beg_idx & fifo_mask],
887 iterate_bufs_timesorted(FILE *fo, struct ktr_buffer *ktr_bufs,
888 u_int64_t *last_timestamp, ktr_iter_cb_t cb)
890 struct ktr_entry *ent;
891 struct ktr_buffer *buf;
899 for (n = 0; n < ncpus; ++n) {
901 if (buf->beg_idx == buf->end_idx)
903 ent = &buf->ents[buf->beg_idx & fifo_mask];
904 if (ts == 0 || (ts >= ent->ktr_timestamp)) {
905 ts = ent->ktr_timestamp;
909 if ((bestn < 0) || (ts < *last_timestamp))
911 buf = ktr_bufs + bestn;
913 &buf->ents[buf->beg_idx & fifo_mask],
916 *last_timestamp = ts;
923 kvmfprintf(FILE *fp, const char *ctl, va_list va)
929 static struct save_ctx strctx;
933 for (n = 0; ctl[n]; ++n) {
942 while (n < (int)sizeof(fmt)) {
969 va_arg(va, long long));
983 s = kvm_string(va_arg(va, char *), &strctx);
984 fwrite(s, 1, strlen(s), fp);
1029 fprintf(fp, fmt, NULL);
1038 fprintf(stderr, "usage: ktrdump [-acfilnpqrstx] [-A factor] "
1039 "[-N execfile] [-M corefile] [-o outfile]\n");