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 $
30 #include <sys/types.h>
34 #include <sys/queue.h>
52 struct ktr_entry *ents;
55 int beg_idx; /* Beginning index */
56 int end_idx; /* Ending index */
59 static struct nlist nl1[] = {
60 { .n_name = "_ktr_version" },
61 { .n_name = "_ktr_entries" },
62 { .n_name = "_ncpus" },
66 static struct nlist nl2[] = {
67 { .n_name = "_tsc_frequency" },
71 static struct nlist nl_version_ktr_idx[] = {
72 { .n_name = "_ktr_idx" },
73 { .n_name = "_ktr_buf" },
77 static struct nlist nl_version_ktr_cpu[] = {
78 { .n_name = "_ktr_cpu" },
84 const void *save_kptr;
87 typedef void (*ktr_iter_cb_t)(void *, int, int, struct ktr_entry *, uint64_t *);
90 /* defined according to the x86_64 ABI spec */
92 uint32_t gp_offset; /* offset to next available gpr in reg_save_area */
93 uint32_t fp_offset; /* offset to next available fpr in reg_save_area */
94 void *overflow_arg_area; /* args that are passed on the stack */
95 struct reg_save_area *reg_save_area; /* register args */
97 * NOT part of the ABI. ->overflow_arg_area gets advanced when code
98 * iterates over the arguments with va_arg(). That means we need to
99 * keep a copy in order to free the allocated memory (if any)
101 void *overflow_arg_area_save;
102 } __attribute__((packed));
104 typedef struct my_va_list *machine_va_list;
106 struct reg_save_area {
107 uint64_t rdi, rsi, rdx, rcx, r8, r9;
108 /* XMM registers follow, but we don't use them */
126 static double tsc_frequency;
127 static double correction_factor = 0.0;
129 static char corefile[PATH_MAX];
130 static char execfile[PATH_MAX];
132 static char errbuf[_POSIX2_LINE_MAX];
135 static int entries_per_buf;
136 static int fifo_mask;
137 static int ktr_version;
139 static void usage(void);
140 static int earliest_ts(struct ktr_buffer *);
141 static void dump_machine_info(evtr_t);
142 static void dump_device_info(evtr_t);
143 static void print_header(FILE *, int);
144 static void print_entry(FILE *, int, int, struct ktr_entry *, u_int64_t *);
145 static void print_callback(void *, int, int, struct ktr_entry *, uint64_t *);
146 static void dump_callback(void *, int, int, struct ktr_entry *, uint64_t *);
147 static struct ktr_info *kvm_ktrinfo(void *, struct save_ctx *);
148 static const char *kvm_string(const char *, struct save_ctx *);
149 static const char *trunc_path(const char *, int);
150 static void read_symbols(const char *);
151 static const char *address_to_symbol(void *, struct save_ctx *);
152 static struct ktr_buffer *ktr_bufs_init(void);
153 static void get_indices(struct ktr_entry **, int *);
154 static void load_bufs(struct ktr_buffer *, struct ktr_entry **, int *);
155 static void iterate_buf(FILE *, struct ktr_buffer *, int, u_int64_t *, ktr_iter_cb_t);
156 static void iterate_bufs_timesorted(FILE *, struct ktr_buffer *, u_int64_t *, ktr_iter_cb_t);
157 static void kvmfprintf(FILE *fp, const char *ctl, va_list va);
158 static int va_list_from_blob(machine_va_list *valist, const char *fmt, char *blob, size_t blobsize);
159 static void va_list_cleanup(machine_va_list *valist);
161 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
164 main(int ac, char **av)
166 struct ktr_buffer *ktr_bufs;
167 struct ktr_entry **ktr_kbuf;
168 ktr_iter_cb_t callback = &print_callback;
173 int *ktr_start_index;
178 * Parse commandline arguments.
181 while ((c = getopt(ac, av, "acfinqrtxpslA:N:M:o:d")) != -1) {
197 callback = &dump_callback;
200 if (strlcpy(execfile, optarg, sizeof(execfile))
202 errx(1, "%s: File name too long", optarg);
215 correction_factor = strtod(optarg, NULL);
218 if (strlcpy(corefile, optarg, sizeof(corefile))
220 errx(1, "%s: File name too long", optarg);
227 if ((fo = fopen(optarg, "w")) == NULL)
228 err(1, "%s", optarg);
240 sflag = 1; /* sort across the cpus */
255 ctx = evtr_open_write(fo);
257 err(1, "Can't create event stream");
260 if (cflag + iflag + tflag + xflag + fflag + pflag == 0) {
266 if (correction_factor != 0.0 && (rflag == 0 || nflag)) {
267 fprintf(stderr, "Correction factor can only be applied with -r and without -n\n");
276 * Open our execfile and corefile, resolve needed symbols and read in
279 if ((kd = kvm_openfiles(Nflag ? execfile : NULL,
280 Mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
281 errx(1, "%s", errbuf);
282 if (kvm_nlist(kd, nl1) != 0)
283 errx(1, "%s", kvm_geterr(kd));
284 if (kvm_read(kd, nl1[0].n_value, &ktr_version, sizeof(ktr_version)) == -1)
285 errx(1, "%s", kvm_geterr(kd));
286 if (kvm_read(kd, nl1[2].n_value, &ncpus, sizeof(ncpus)) == -1)
287 errx(1, "%s", kvm_geterr(kd));
288 ktr_start_index = malloc(sizeof(*ktr_start_index) * ncpus);
289 if (ktr_version >= KTR_VERSION_WITH_FREQ && kvm_nlist(kd, nl2) == 0) {
290 if (kvm_read(kd, nl2[0].n_value, &tts, sizeof(tts)) == -1)
291 errx(1, "%s", kvm_geterr(kd));
292 tsc_frequency = (double)tts;
294 if (ktr_version > KTR_VERSION)
295 errx(1, "ktr version too high for us to handle");
296 if (kvm_read(kd, nl1[1].n_value, &entries_per_buf,
297 sizeof(entries_per_buf)) == -1)
298 errx(1, "%s", kvm_geterr(kd));
299 fifo_mask = entries_per_buf - 1;
301 printf("TSC frequency is %6.3f MHz\n", tsc_frequency / 1000000.0);
304 dump_machine_info((evtr_t)ctx);
305 dump_device_info((evtr_t)ctx);
307 ktr_kbuf = calloc(ncpus, sizeof(*ktr_kbuf));
308 ktr_idx = calloc(ncpus, sizeof(*ktr_idx));
311 read_symbols(Nflag ? execfile : NULL);
313 if (ktr_version < KTR_VERSION_KTR_CPU) {
314 if (kvm_nlist(kd, nl_version_ktr_idx))
315 errx(1, "%s", kvm_geterr(kd));
317 if (kvm_nlist(kd, nl_version_ktr_cpu))
318 errx(1, "%s", kvm_geterr(kd));
321 get_indices(ktr_kbuf, ktr_idx);
323 ktr_bufs = ktr_bufs_init();
326 u_int64_t last_timestamp = 0;
328 load_bufs(ktr_bufs, ktr_kbuf, ktr_idx);
329 iterate_bufs_timesorted(ctx, ktr_bufs, &last_timestamp,
332 usleep(1000000 / 10);
335 u_int64_t *last_timestamp = calloc(sizeof(u_int64_t), ncpus);
337 load_bufs(ktr_bufs, ktr_kbuf, ktr_idx);
338 for (n = 0; n < ncpus; ++n)
339 iterate_buf(ctx, ktr_bufs, n, &last_timestamp[n],
342 usleep(1000000 / 10);
352 dump_devinfo(struct devinfo_dev *dev, void *arg)
354 struct evtr_event ev;
355 evtr_t evtr = (evtr_t)arg;
356 const char *fmt = "#devicenames[\"%s\"] = %#lx";
357 char fmtdatabuf[sizeof(char *) + sizeof(devinfo_handle_t)];
358 char *fmtdata = fmtdatabuf;
360 if (!dev->dd_name[0])
362 ev.type = EVTR_TYPE_PROBE;
369 ((char **)fmtdata)[0] = &dev->dd_name[0];
370 fmtdata += sizeof(char *);
371 ((devinfo_handle_t *)fmtdata)[0] = dev->dd_handle;
372 ev.fmtdata = fmtdatabuf;
373 ev.fmtdatalen = sizeof(fmtdatabuf);
375 if (evtr_dump_event(evtr, &ev)) {
376 err(1, "%s", evtr_errmsg(evtr));
379 return devinfo_foreach_device_child(dev, dump_devinfo, evtr);
384 dump_device_info(evtr_t evtr)
386 struct devinfo_dev *root;
389 if (!(root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE))) {
390 warn("can't find root device");
393 devinfo_foreach_device_child(root, dump_devinfo, evtr);
398 dump_machine_info(evtr_t evtr)
400 struct evtr_event ev;
403 bzero(&ev, sizeof(ev));
404 ev.type = EVTR_TYPE_SYSINFO;
406 evtr_dump_event(evtr, &ev);
407 if (evtr_error(evtr)) {
408 err(1, "%s", evtr_errmsg(evtr));
411 for (i = 0; i < ncpus; ++i) {
412 bzero(&ev, sizeof(ev));
413 ev.type = EVTR_TYPE_CPUINFO;
415 ev.cpuinfo.freq = tsc_frequency;
416 evtr_dump_event(evtr, &ev);
417 if (evtr_error(evtr)) {
418 err(1, "%s", evtr_errmsg(evtr));
424 print_header(FILE *fo, int row)
426 if (qflag == 0 && (u_int32_t)row % 20 == 0) {
427 fprintf(fo, "%-6s ", "index");
429 fprintf(fo, "%-3s ", "cpu");
431 fprintf(fo, "%-16s ", "timestamp");
434 fprintf(fo, "%-10s %-10s", "caller2", "caller1");
436 fprintf(fo, "%-20s %-20s", "caller2", "caller1");
439 fprintf(fo, "%-20s ", "ID");
441 fprintf(fo, "%10s%-30s ", "", "file and line");
443 fprintf(fo, "%s", "trace");
449 print_entry(FILE *fo, int n, int row, struct ktr_entry *entry,
450 u_int64_t *last_timestamp)
452 struct ktr_info *info = NULL;
453 static struct save_ctx nctx, pctx, fmtctx, symctx, infoctx;
455 fprintf(fo, "%06x ", row & 0x00FFFFFF);
457 fprintf(fo, "%-3d ", n);
458 if (tflag || rflag) {
459 if (rflag && !nflag && tsc_frequency != 0.0) {
460 fprintf(fo, "%13.3f uS ",
461 (double)(entry->ktr_timestamp - *last_timestamp) * 1000000.0 / tsc_frequency - correction_factor);
463 fprintf(fo, "%-16ju ",
464 (uintmax_t)(entry->ktr_timestamp - *last_timestamp));
466 fprintf(fo, "%-16ju ",
467 (uintmax_t)entry->ktr_timestamp);
472 fprintf(fo, "%p %p ",
473 entry->ktr_caller2, entry->ktr_caller1);
475 fprintf(fo, "%-25s ",
476 address_to_symbol(entry->ktr_caller2, &symctx));
477 fprintf(fo, "%-25s ",
478 address_to_symbol(entry->ktr_caller1, &symctx));
482 info = kvm_ktrinfo(entry->ktr_info, &infoctx);
484 fprintf(fo, "%-20s ", kvm_string(info->kf_name, &nctx));
486 fprintf(fo, "%-20s ", "<empty>");
489 fprintf(fo, "%34s:%-4d ",
490 trunc_path(kvm_string(entry->ktr_file, &pctx), 34),
494 info = kvm_ktrinfo(entry->ktr_info, &infoctx);
498 fmt = kvm_string(info->kf_format, &fmtctx);
499 if (va_list_from_blob(&ap, fmt,
500 (char *)&entry->ktr_data,
502 err(2, "Can't generate va_list from %s", fmt);
503 kvmfprintf(fo, kvm_string(info->kf_format, &fmtctx),
505 va_list_cleanup(&ap);
509 *last_timestamp = entry->ktr_timestamp;
514 print_callback(void *ctx, int n, int row, struct ktr_entry *entry, uint64_t *last_ts)
516 FILE *fo = (FILE *)ctx;
517 print_header(fo, row);
518 print_entry(fo, n, row, entry, last_ts);
522 * If free == 0, replace all (kvm) string pointers in fmtdata with pointers
523 * to user-allocated copies of the strings.
524 * If free != 0, free those pointers.
528 mangle_string_ptrs(const char *fmt, uint8_t *fmtdata, int dofree)
531 size_t skipsize, intsz;
532 static struct save_ctx strctx;
535 for (f = fmt; f[0] != '\0'; ++f) {
540 for (p = f; p[0]; ++p) {
543 * Eat flags. Notice this will accept duplicate
559 /* Eat minimum field width, if any */
560 for (; isdigit(p[0]); ++p)
564 /* Eat precision, if any */
565 for (; isdigit(p[0]); ++p)
572 intsz = sizeof(long long);
574 intsz = sizeof(long);
578 intsz = sizeof(intmax_t);
581 intsz = sizeof(ptrdiff_t);
584 intsz = sizeof(size_t);
605 skipsize = sizeof(void *);
609 skipsize = sizeof(double);
611 skipsize = sizeof(float);
615 char *t = ((char **)fmtdata)[0];
617 skipsize = sizeof(char *);
619 char *t = strdup(kvm_string(((char **)fmtdata)[0],
621 ((const char **)fmtdata)[0] = t;
623 skipsize = sizeof(char *);
628 fprintf(stderr, "Unknown conversion specifier %c "
629 "in fmt starting with %s\n", p[0], f - 1);
639 dump_callback(void *ctx, int n, int row __unused, struct ktr_entry *entry,
640 uint64_t *last_ts __unused)
642 evtr_t evtr = (evtr_t)ctx;
643 struct evtr_event ev;
644 static struct save_ctx pctx, fmtctx, infoctx;
646 int conv = 0; /* pointless */
648 ev.ts = entry->ktr_timestamp;
649 ev.type = EVTR_TYPE_PROBE;
650 ev.line = entry->ktr_line;
651 ev.file = kvm_string(entry->ktr_file, &pctx);
654 if ((ki = kvm_ktrinfo(entry->ktr_info, &infoctx))) {
655 ev.fmt = kvm_string(ki->kf_format, &fmtctx);
656 ev.fmtdata = entry->ktr_data;
657 if ((conv = mangle_string_ptrs(ev.fmt,
658 __DECONST(uint8_t *, ev.fmtdata),
660 errx(1, "Can't parse format string");
661 ev.fmtdatalen = ki->kf_data_size;
663 ev.fmt = ev.fmtdata = NULL;
666 if (evtr_dump_event(evtr, &ev)) {
667 err(1, "%s", evtr_errmsg(evtr));
669 if (ev.fmtdata && conv) {
670 mangle_string_ptrs(ev.fmt, __DECONST(uint8_t *, ev.fmtdata),
677 kvm_ktrinfo(void *kptr, struct save_ctx *ctx)
679 struct ktr_info *ki = (void *)ctx->save_buf;
683 if (ctx->save_kptr != kptr) {
684 if (kvm_read(kd, (uintptr_t)kptr, ki, sizeof(*ki)) == -1) {
685 bzero(&ki, sizeof(*ki));
687 ctx->save_kptr = kptr;
695 kvm_string(const char *kptr, struct save_ctx *ctx)
702 if (ctx->save_kptr != (const void *)kptr) {
703 ctx->save_kptr = (const void *)kptr;
705 while (l < sizeof(ctx->save_buf) - 1) {
706 n = 256 - ((intptr_t)(kptr + l) & 255);
707 if (n > sizeof(ctx->save_buf) - l - 1)
708 n = sizeof(ctx->save_buf) - l - 1;
709 if (kvm_read(kd, (uintptr_t)(kptr + l), ctx->save_buf + l, n) < 0)
711 while (l < sizeof(ctx->save_buf) && n) {
712 if (ctx->save_buf[l] == 0)
720 ctx->save_buf[l] = 0;
722 return(ctx->save_buf);
727 trunc_path(const char *str, int maxlen)
729 int len = strlen(str);
732 return(str + len - maxlen);
738 TAILQ_ENTRY(symdata) link;
744 static TAILQ_HEAD(symlist, symdata) symlist;
745 static struct symdata *symcache;
746 static char *symbegin;
751 read_symbols(const char *file)
755 size_t buflen = sizeof(buf);
757 struct symdata *sym = NULL;
762 TAILQ_INIT(&symlist);
765 if (sysctlbyname("kern.bootfile", buf, &buflen, NULL, 0) < 0)
766 file = "/boot/kernel/kernel";
770 snprintf(cmd, sizeof(cmd), "nm -n %s", file);
771 if ((fp = popen(cmd, "r")) != NULL) {
772 while (fgets(buf, sizeof(buf), fp) != NULL) {
773 s1 = strtok(buf, " \t\n");
774 s2 = strtok(NULL, " \t\n");
775 s3 = strtok(NULL, " \t\n");
776 if (s1 && s2 && s3) {
777 sym = malloc(sizeof(struct symdata));
778 sym->symaddr = (char *)strtoul(s1, NULL, 16);
779 sym->symtype = s2[0];
780 sym->symname = strdup(s3);
781 if (strcmp(s3, "kernbase") == 0)
782 symbegin = sym->symaddr;
783 if (strcmp(s3, "end") == 0 || strcmp(s3, "_end") == 0)
784 symend = sym->symaddr;
785 TAILQ_INSERT_TAIL(&symlist, sym, link);
790 if (symend == NULL) {
792 symend = sym->symaddr;
796 symcache = TAILQ_FIRST(&symlist);
801 address_to_symbol(void *kptr, struct save_ctx *ctx)
803 char *buf = ctx->save_buf;
804 int size = sizeof(ctx->save_buf);
806 if (symcache == NULL ||
807 (char *)kptr < symbegin || (char *)kptr >= symend
809 snprintf(buf, size, "%p", kptr);
812 while ((char *)symcache->symaddr < (char *)kptr) {
813 if (TAILQ_NEXT(symcache, link) == NULL)
815 symcache = TAILQ_NEXT(symcache, link);
817 while ((char *)symcache->symaddr > (char *)kptr) {
818 if (symcache != TAILQ_FIRST(&symlist))
819 symcache = TAILQ_PREV(symcache, symlist, link);
821 snprintf(buf, size, "%s+%d", symcache->symname,
822 (int)((char *)kptr - symcache->symaddr));
830 struct ktr_buffer *ktr_bufs, *it;
833 ktr_bufs = malloc(sizeof(*ktr_bufs) * ncpus);
835 err(1, "can't allocate data structures");
836 for (i = 0; i < ncpus; ++i) {
838 it->ents = malloc(sizeof(struct ktr_entry) * entries_per_buf);
839 if (it->ents == NULL)
840 err(1, "can't allocate data structures");
850 get_indices(struct ktr_entry **ktr_kbuf, int *ktr_idx)
852 static struct ktr_cpu *ktr_cpus;
855 if (ktr_cpus == NULL)
856 ktr_cpus = malloc(sizeof(*ktr_cpus) * ncpus);
858 if (ktr_version < KTR_VERSION_KTR_CPU) {
859 if (kvm_read(kd, nl_version_ktr_idx[0].n_value, ktr_idx,
860 sizeof(*ktr_idx) * ncpus) == -1) {
861 errx(1, "%s", kvm_geterr(kd));
863 if (ktr_kbuf[0] == NULL) {
864 if (kvm_read(kd, nl_version_ktr_idx[1].n_value,
865 ktr_kbuf, sizeof(*ktr_kbuf) * ncpus) == -1) {
866 errx(1, "%s", kvm_geterr(kd));
870 if (kvm_read(kd, nl_version_ktr_cpu[0].n_value,
871 ktr_cpus, sizeof(*ktr_cpus) * ncpus) == -1) {
872 errx(1, "%s", kvm_geterr(kd));
874 for (i = 0; i < ncpus; ++i) {
875 ktr_idx[i] = ktr_cpus[i].core.ktr_idx;
876 ktr_kbuf[i] = ktr_cpus[i].core.ktr_buf;
882 * Get the trace buffer data from the kernel
886 load_bufs(struct ktr_buffer *ktr_bufs, struct ktr_entry **kbufs, int *ktr_idx)
888 struct ktr_buffer *kbuf;
891 get_indices(kbufs, ktr_idx);
892 for (i = 0; i < ncpus; ++i) {
894 if (ktr_idx[i] == kbuf->end_idx)
896 kbuf->end_idx = ktr_idx[i];
899 * If we do not have a notion of the beginning index, assume
900 * it is entries_per_buf before the ending index. Don't
901 * worry about underflows/negative numbers, the indices will
905 kbuf->beg_idx = kbuf->end_idx - entries_per_buf + 1;
908 if (kvm_read(kd, (uintptr_t)kbufs[i], ktr_bufs[i].ents,
909 sizeof(struct ktr_entry) * entries_per_buf)
911 errx(1, "%s", kvm_geterr(kd));
913 kbuf->beg_idx = earliest_ts(kbuf);
919 * Locate the earliest timestamp iterating backwards from end_idx, but
920 * not going further back then beg_idx. We have to do this because
921 * the kernel uses a circulating buffer.
925 earliest_ts(struct ktr_buffer *buf)
927 struct ktr_entry *save;
928 int count, scan, i, earliest;
931 earliest = buf->end_idx - 1;
932 save = &buf->ents[earliest & fifo_mask];
933 for (scan = buf->end_idx - 1; scan != buf->beg_idx -1; --scan) {
934 i = scan & fifo_mask;
935 if (buf->ents[i].ktr_timestamp <= save->ktr_timestamp &&
936 buf->ents[i].ktr_timestamp > 0)
939 * We may have gotten so far behind that beg_idx wrapped
940 * more then once around the buffer. Just stop
942 if (++count == entries_per_buf)
950 iterate_buf(FILE *fo, struct ktr_buffer *ktr_bufs, int cpu,
951 u_int64_t *last_timestamp, ktr_iter_cb_t cb)
953 struct ktr_buffer *buf = ktr_bufs + cpu;
955 if (buf->modified == 0)
957 if (*last_timestamp == 0) {
959 buf->ents[buf->beg_idx & fifo_mask].ktr_timestamp;
961 while (buf->beg_idx != buf->end_idx) {
962 cb(fo, cpu, buf->beg_idx,
963 &buf->ents[buf->beg_idx & fifo_mask],
972 iterate_bufs_timesorted(FILE *fo, struct ktr_buffer *ktr_bufs,
973 u_int64_t *last_timestamp, ktr_iter_cb_t cb)
975 struct ktr_entry *ent;
976 struct ktr_buffer *buf;
984 for (n = 0; n < ncpus; ++n) {
986 if (buf->beg_idx == buf->end_idx)
988 ent = &buf->ents[buf->beg_idx & fifo_mask];
989 if (ts == 0 || (ts >= ent->ktr_timestamp)) {
990 ts = ent->ktr_timestamp;
994 if ((bestn < 0) || (ts < *last_timestamp))
996 buf = ktr_bufs + bestn;
998 &buf->ents[buf->beg_idx & fifo_mask],
1001 *last_timestamp = ts;
1008 kvmfprintf(FILE *fp, const char *ctl, va_list va)
1014 static struct save_ctx strctx;
1018 for (n = 0; ctl[n]; ++n) {
1027 while (n < (int)sizeof(fmt)) {
1054 va_arg(va, long long));
1058 va_arg(va, size_t));
1065 fprintf(fp, "%c", va_arg(va, int));
1073 s = kvm_string(va_arg(va, char *), &strctx);
1074 fwrite(s, 1, strlen(s), fp);
1083 va_arg(va, double));
1122 fprintf(fp, fmt, NULL);
1131 fprintf(stderr, "usage: ktrdump [-acfilnpqrstx] [-A factor] "
1132 "[-N execfile] [-M corefile] [-o outfile]\n");
1136 enum argument_class {
1144 conversion_size(const char *fmt, enum argument_class *argclass)
1147 size_t convsize, intsz;
1149 *argclass = ARGCLASS_ERR;
1154 for (p = fmt + 1; p[0]; ++p) {
1157 * Eat flags. Notice this will accept duplicate
1173 /* Eat minimum field width, if any */
1174 for (; isdigit(p[0]); ++p)
1178 /* Eat precision, if any */
1179 for (; isdigit(p[0]); ++p)
1186 intsz = sizeof(char);
1188 intsz = sizeof(short);
1194 intsz = sizeof(long long);
1196 intsz = sizeof(long);
1200 intsz = sizeof(intmax_t);
1203 intsz = sizeof(ptrdiff_t);
1206 intsz = sizeof(size_t);
1209 p--; /* Anticipate the ++p that follows. Yes, I know. Eeek. */
1213 intsz = sizeof(int);
1218 /* for %c, we only store 1 byte in the ktr entry */
1219 convsize = sizeof(char);
1220 *argclass = ARGCLASS_INTEGER;
1229 *argclass = ARGCLASS_INTEGER;
1232 convsize = sizeof(void *);
1233 *argclass = ARGCLASS_INTEGER;
1237 convsize = sizeof(double);
1239 convsize = sizeof(float);
1241 *argclass = ARGCLASS_FP;
1243 convsize = sizeof(char *);
1244 *argclass = ARGCLASS_INTEGER;
1248 *argclass = ARGCLASS_NONE;
1251 fprintf(stderr, "Unknown conversion specifier %c "
1252 "in fmt starting with %s\n", p[0], fmt - 1);
1260 va_list_push_integral(struct my_va_list *valist, void *val, size_t valsize,
1267 r = *(uint8_t *)val; break;
1269 r = *(uint32_t *)val; break;
1271 r = (*(uint32_t *)val); break;
1273 r = *(uint64_t *)val; break;
1277 /* we always need to push the full 8 bytes */
1278 if ((valist->gp_offset + valsize) <= 48) { /* got a free reg */
1280 memcpy(((char *)valist->reg_save_area + valist->gp_offset),
1282 valist->gp_offset += sizeof(r);
1285 /* push to "stack" */
1286 if (!(valist->overflow_arg_area = realloc(valist->overflow_arg_area,
1287 *stacksize + sizeof(r))))
1290 * Keep a pointer to the start of the allocated memory block so
1291 * we can free it later. We need to update it after every realloc().
1293 valist->overflow_arg_area_save = valist->overflow_arg_area;
1294 memcpy((char *)valist->overflow_arg_area + *stacksize, &r, sizeof(r));
1295 *stacksize += sizeof(r);
1300 va_list_rewind(struct my_va_list *valist)
1302 valist->gp_offset = 0;
1306 va_list_cleanup(machine_va_list *_valist)
1308 machine_va_list valist;
1309 if (!_valist || !*_valist)
1312 if (valist->reg_save_area)
1313 free(valist->reg_save_area);
1314 if (valist->overflow_arg_area_save)
1315 free(valist->overflow_arg_area_save);
1320 va_list_from_blob(machine_va_list *_valist, const char *fmt, char *blob, size_t blobsize)
1322 machine_va_list valist;
1323 struct reg_save_area *regs;
1327 if (!(valist = malloc(sizeof(*valist))))
1329 if (!(regs = malloc(sizeof(*regs))))
1331 *valist = (struct my_va_list) {
1334 .overflow_arg_area = NULL,
1335 .reg_save_area = regs,
1336 .overflow_arg_area_save = NULL,
1338 enum argument_class argclass;
1339 size_t stacksize = 0;
1341 for (f = fmt; *f != '\0'; ++f) {
1344 sz = conversion_size(f, &argclass);
1345 if (argclass == ARGCLASS_INTEGER) {
1346 if (blobsize < sz) {
1347 fprintf(stderr, "not enough data available "
1348 "for format: %s\n", fmt);
1351 if (va_list_push_integral(valist, blob, sz, &stacksize))
1355 } else if (argclass != ARGCLASS_NONE)
1357 /* walk past the '%' */
1361 fprintf(stderr, "Couldn't consume all data for format %s "
1362 "(%zd bytes left over)\n", fmt, blobsize);
1365 va_list_rewind(valist);
1369 if (valist->reg_save_area)
1370 free(valist->reg_save_area);
1371 if (valist->overflow_arg_area_save)
1372 free(valist->overflow_arg_area_save);
1380 #error "Don't know how to get a va_list on this platform"