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>
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" },
87 const void *save_kptr;
90 typedef void (*ktr_iter_cb_t)(void *, int, int, struct ktr_entry *, uint64_t *);
93 /* defined according to the x86_64 ABI spec */
95 uint32_t gp_offset; /* offset to next available gpr in reg_save_area */
96 uint32_t fp_offset; /* offset to next available fpr in reg_save_area */
97 void *overflow_arg_area; /* args that are passed on the stack */
98 struct reg_save_area *reg_save_area; /* register args */
100 * NOT part of the ABI. ->overflow_arg_area gets advanced when code
101 * iterates over the arguments with va_arg(). That means we need to
102 * keep a copy in order to free the allocated memory (if any)
104 void *overflow_arg_area_save;
105 } __attribute__((packed));
107 typedef struct my_va_list *machine_va_list;
109 struct reg_save_area {
110 uint64_t rdi, rsi, rdx, rcx, r8, r9;
111 /* XMM registers follow, but we don't use them */
114 typedef void *machine_va_list;
131 static double tsc_frequency;
132 static double correction_factor = 0.0;
134 static char corefile[PATH_MAX];
135 static char execfile[PATH_MAX];
137 static char errbuf[_POSIX2_LINE_MAX];
140 static int entries_per_buf;
141 static int fifo_mask;
142 static int ktr_version;
144 static void usage(void);
145 static int earliest_ts(struct ktr_buffer *);
146 static void dump_machine_info(evtr_t);
147 static void dump_device_info(evtr_t);
148 static void print_header(FILE *, int);
149 static void print_entry(FILE *, int, int, struct ktr_entry *, u_int64_t *);
150 static void print_callback(void *, int, int, struct ktr_entry *, uint64_t *);
151 static void dump_callback(void *, int, int, struct ktr_entry *, uint64_t *);
152 static struct ktr_info *kvm_ktrinfo(void *, struct save_ctx *);
153 static const char *kvm_string(const char *, struct save_ctx *);
154 static const char *trunc_path(const char *, int);
155 static void read_symbols(const char *);
156 static const char *address_to_symbol(void *, struct save_ctx *);
157 static struct ktr_buffer *ktr_bufs_init(void);
158 static void get_indices(struct ktr_entry **, int *);
159 static void load_bufs(struct ktr_buffer *, struct ktr_entry **, int *);
160 static void iterate_buf(FILE *, struct ktr_buffer *, int, u_int64_t *, ktr_iter_cb_t);
161 static void iterate_bufs_timesorted(FILE *, struct ktr_buffer *, u_int64_t *, ktr_iter_cb_t);
162 static void kvmfprintf(FILE *fp, const char *ctl, va_list va);
163 static int va_list_from_blob(machine_va_list *valist, const char *fmt, char *blob, size_t blobsize);
164 static void va_list_cleanup(machine_va_list *valist);
166 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
169 main(int ac, char **av)
171 struct ktr_buffer *ktr_bufs;
172 struct ktr_entry **ktr_kbuf;
173 ktr_iter_cb_t callback = &print_callback;
178 int *ktr_start_index;
183 * Parse commandline arguments.
186 while ((c = getopt(ac, av, "acfinqrtxpslA:N:M:o:d")) != -1) {
202 callback = &dump_callback;
205 if (strlcpy(execfile, optarg, sizeof(execfile))
207 errx(1, "%s: File name too long", optarg);
220 correction_factor = strtod(optarg, NULL);
223 if (strlcpy(corefile, optarg, sizeof(corefile))
225 errx(1, "%s: File name too long", optarg);
232 if ((fo = fopen(optarg, "w")) == NULL)
233 err(1, "%s", optarg);
245 sflag = 1; /* sort across the cpus */
260 ctx = evtr_open_write(fo);
262 err(1, "Can't create event stream");
265 if (cflag + iflag + tflag + xflag + fflag + pflag == 0) {
271 if (correction_factor != 0.0 && (rflag == 0 || nflag)) {
272 fprintf(stderr, "Correction factor can only be applied with -r and without -n\n");
281 * Open our execfile and corefile, resolve needed symbols and read in
284 if ((kd = kvm_openfiles(Nflag ? execfile : NULL,
285 Mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
286 errx(1, "%s", errbuf);
287 if (kvm_nlist(kd, nl1) != 0)
288 errx(1, "%s", kvm_geterr(kd));
289 if (kvm_read(kd, nl1[0].n_value, &ktr_version, sizeof(ktr_version)) == -1)
290 errx(1, "%s", kvm_geterr(kd));
291 if (kvm_read(kd, nl1[2].n_value, &ncpus, sizeof(ncpus)) == -1)
292 errx(1, "%s", kvm_geterr(kd));
293 ktr_start_index = malloc(sizeof(*ktr_start_index) * ncpus);
294 if (ktr_version >= KTR_VERSION_WITH_FREQ && kvm_nlist(kd, nl2) == 0) {
295 if (kvm_read(kd, nl2[0].n_value, &tts, sizeof(tts)) == -1)
296 errx(1, "%s", kvm_geterr(kd));
297 tsc_frequency = (double)tts;
299 if (ktr_version > KTR_VERSION)
300 errx(1, "ktr version too high for us to handle");
301 if (kvm_read(kd, nl1[1].n_value, &entries_per_buf,
302 sizeof(entries_per_buf)) == -1)
303 errx(1, "%s", kvm_geterr(kd));
304 fifo_mask = entries_per_buf - 1;
306 printf("TSC frequency is %6.3f MHz\n", tsc_frequency / 1000000.0);
309 dump_machine_info((evtr_t)ctx);
310 dump_device_info((evtr_t)ctx);
312 ktr_kbuf = calloc(ncpus, sizeof(*ktr_kbuf));
313 ktr_idx = calloc(ncpus, sizeof(*ktr_idx));
316 read_symbols(Nflag ? execfile : NULL);
318 if (ktr_version < KTR_VERSION_KTR_CPU) {
319 if (kvm_nlist(kd, nl_version_ktr_idx))
320 errx(1, "%s", kvm_geterr(kd));
322 if (kvm_nlist(kd, nl_version_ktr_cpu))
323 errx(1, "%s", kvm_geterr(kd));
326 get_indices(ktr_kbuf, ktr_idx);
328 ktr_bufs = ktr_bufs_init();
331 u_int64_t last_timestamp = 0;
333 load_bufs(ktr_bufs, ktr_kbuf, ktr_idx);
334 iterate_bufs_timesorted(ctx, ktr_bufs, &last_timestamp,
337 usleep(1000000 / 10);
340 u_int64_t *last_timestamp = calloc(sizeof(u_int64_t), ncpus);
342 load_bufs(ktr_bufs, ktr_kbuf, ktr_idx);
343 for (n = 0; n < ncpus; ++n)
344 iterate_buf(ctx, ktr_bufs, n, &last_timestamp[n],
347 usleep(1000000 / 10);
357 dump_devinfo(struct devinfo_dev *dev, void *arg)
359 struct evtr_event ev;
360 evtr_t evtr = (evtr_t)arg;
361 const char *fmt = "#devicenames[\"%s\"] = %#lx";
362 char fmtdatabuf[sizeof(char *) + sizeof(devinfo_handle_t)];
363 char *fmtdata = fmtdatabuf;
365 if (!dev->dd_name[0])
367 ev.type = EVTR_TYPE_PROBE;
374 ((char **)fmtdata)[0] = &dev->dd_name[0];
375 fmtdata += sizeof(char *);
376 ((devinfo_handle_t *)fmtdata)[0] = dev->dd_handle;
377 ev.fmtdata = fmtdatabuf;
378 ev.fmtdatalen = sizeof(fmtdatabuf);
380 if (evtr_dump_event(evtr, &ev)) {
381 err(1, evtr_errmsg(evtr));
384 return devinfo_foreach_device_child(dev, dump_devinfo, evtr);
389 dump_device_info(evtr_t evtr)
391 struct devinfo_dev *root;
394 if (!(root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE))) {
395 warn("can't find root device");
398 devinfo_foreach_device_child(root, dump_devinfo, evtr);
403 dump_machine_info(evtr_t evtr)
405 struct evtr_event ev;
408 bzero(&ev, sizeof(ev));
409 ev.type = EVTR_TYPE_SYSINFO;
411 evtr_dump_event(evtr, &ev);
412 if (evtr_error(evtr)) {
413 err(1, evtr_errmsg(evtr));
416 for (i = 0; i < ncpus; ++i) {
417 bzero(&ev, sizeof(ev));
418 ev.type = EVTR_TYPE_CPUINFO;
420 ev.cpuinfo.freq = tsc_frequency;
421 evtr_dump_event(evtr, &ev);
422 if (evtr_error(evtr)) {
423 err(1, evtr_errmsg(evtr));
429 print_header(FILE *fo, int row)
431 if (qflag == 0 && (u_int32_t)row % 20 == 0) {
432 fprintf(fo, "%-6s ", "index");
434 fprintf(fo, "%-3s ", "cpu");
436 fprintf(fo, "%-16s ", "timestamp");
439 fprintf(fo, "%-10s %-10s", "caller2", "caller1");
441 fprintf(fo, "%-20s %-20s", "caller2", "caller1");
444 fprintf(fo, "%-20s ", "ID");
446 fprintf(fo, "%10s%-30s ", "", "file and line");
448 fprintf(fo, "%s", "trace");
454 print_entry(FILE *fo, int n, int row, struct ktr_entry *entry,
455 u_int64_t *last_timestamp)
457 struct ktr_info *info = NULL;
458 static struct save_ctx nctx, pctx, fmtctx, symctx, infoctx;
460 fprintf(fo, " %06x ", row & 0x00FFFFFF);
462 fprintf(fo, "%-3d ", n);
463 if (tflag || rflag) {
464 if (rflag && !nflag && tsc_frequency != 0.0) {
465 fprintf(fo, "%13.3f uS ",
466 (double)(entry->ktr_timestamp - *last_timestamp) * 1000000.0 / tsc_frequency - correction_factor);
468 fprintf(fo, "%-16ju ",
469 (uintmax_t)(entry->ktr_timestamp - *last_timestamp));
471 fprintf(fo, "%-16ju ",
472 (uintmax_t)entry->ktr_timestamp);
477 fprintf(fo, "%p %p ",
478 entry->ktr_caller2, entry->ktr_caller1);
480 fprintf(fo, "%-25s ",
481 address_to_symbol(entry->ktr_caller2, &symctx));
482 fprintf(fo, "%-25s ",
483 address_to_symbol(entry->ktr_caller1, &symctx));
487 info = kvm_ktrinfo(entry->ktr_info, &infoctx);
489 fprintf(fo, "%-20s ", kvm_string(info->kf_name, &nctx));
491 fprintf(fo, "%-20s ", "<empty>");
494 fprintf(fo, "%34s:%-4d ",
495 trunc_path(kvm_string(entry->ktr_file, &pctx), 34),
499 info = kvm_ktrinfo(entry->ktr_info, &infoctx);
503 fmt = kvm_string(info->kf_format, &fmtctx);
504 if (va_list_from_blob(&ap, fmt,
505 (char *)&entry->ktr_data,
507 err(2, "Can't generate va_list from %s\n", fmt);
508 kvmfprintf(fo, kvm_string(info->kf_format, &fmtctx),
510 va_list_cleanup(&ap);
514 *last_timestamp = entry->ktr_timestamp;
519 print_callback(void *ctx, int n, int row, struct ktr_entry *entry, uint64_t *last_ts)
521 FILE *fo = (FILE *)ctx;
522 print_header(fo, row);
523 print_entry(fo, n, row, entry, last_ts);
527 * If free == 0, replace all (kvm) string pointers in fmtdata with pointers
528 * to user-allocated copies of the strings.
529 * If free != 0, free those pointers.
533 mangle_string_ptrs(const char *fmt, uint8_t *fmtdata, int dofree)
536 size_t skipsize, intsz;
537 static struct save_ctx strctx;
540 for (f = fmt; f[0] != '\0'; ++f) {
545 for (p = f; p[0]; ++p) {
548 * Eat flags. Notice this will accept duplicate
564 /* Eat minimum field width, if any */
565 for (; isdigit(p[0]); ++p)
569 /* Eat precision, if any */
570 for (; isdigit(p[0]); ++p)
577 intsz = sizeof(long long);
579 intsz = sizeof(long);
583 intsz = sizeof(intmax_t);
586 intsz = sizeof(ptrdiff_t);
589 intsz = sizeof(size_t);
610 skipsize = sizeof(void *);
614 skipsize = sizeof(double);
616 skipsize = sizeof(float);
620 char *t = ((char **)fmtdata)[0];
622 skipsize = sizeof(char *);
624 char *t = strdup(kvm_string(((char **)fmtdata)[0],
626 ((const char **)fmtdata)[0] = t;
628 skipsize = sizeof(char *);
633 fprintf(stderr, "Unknown conversion specifier %c "
634 "in fmt starting with %s", p[0], f - 1);
644 dump_callback(void *ctx, int n, int row __unused, struct ktr_entry *entry,
645 uint64_t *last_ts __unused)
647 evtr_t evtr = (evtr_t)ctx;
648 struct evtr_event ev;
649 static struct save_ctx pctx, fmtctx, infoctx;
651 int conv = 0; /* pointless */
653 ev.ts = entry->ktr_timestamp;
654 ev.type = EVTR_TYPE_PROBE;
655 ev.line = entry->ktr_line;
656 ev.file = kvm_string(entry->ktr_file, &pctx);
659 if ((ki = kvm_ktrinfo(entry->ktr_info, &infoctx))) {
660 ev.fmt = kvm_string(ki->kf_format, &fmtctx);
661 ev.fmtdata = entry->ktr_data;
662 if ((conv = mangle_string_ptrs(ev.fmt,
663 __DECONST(uint8_t *, ev.fmtdata),
665 errx(1, "Can't parse format string\n");
666 ev.fmtdatalen = ki->kf_data_size;
668 ev.fmt = ev.fmtdata = NULL;
671 if (evtr_dump_event(evtr, &ev)) {
672 err(1, evtr_errmsg(evtr));
674 if (ev.fmtdata && conv) {
675 mangle_string_ptrs(ev.fmt, __DECONST(uint8_t *, ev.fmtdata),
682 kvm_ktrinfo(void *kptr, struct save_ctx *ctx)
684 struct ktr_info *ki = (void *)ctx->save_buf;
688 if (ctx->save_kptr != kptr) {
689 if (kvm_read(kd, (uintptr_t)kptr, ki, sizeof(*ki)) == -1) {
690 bzero(&ki, sizeof(*ki));
692 ctx->save_kptr = kptr;
700 kvm_string(const char *kptr, struct save_ctx *ctx)
707 if (ctx->save_kptr != (const void *)kptr) {
708 ctx->save_kptr = (const void *)kptr;
710 while (l < sizeof(ctx->save_buf) - 1) {
711 n = 256 - ((intptr_t)(kptr + l) & 255);
712 if (n > sizeof(ctx->save_buf) - l - 1)
713 n = sizeof(ctx->save_buf) - l - 1;
714 if (kvm_read(kd, (uintptr_t)(kptr + l), ctx->save_buf + l, n) < 0)
716 while (l < sizeof(ctx->save_buf) && n) {
717 if (ctx->save_buf[l] == 0)
725 ctx->save_buf[l] = 0;
727 return(ctx->save_buf);
732 trunc_path(const char *str, int maxlen)
734 int len = strlen(str);
737 return(str + len - maxlen);
743 TAILQ_ENTRY(symdata) link;
749 static TAILQ_HEAD(symlist, symdata) symlist;
750 static struct symdata *symcache;
751 static char *symbegin;
756 read_symbols(const char *file)
760 size_t buflen = sizeof(buf);
767 TAILQ_INIT(&symlist);
770 if (sysctlbyname("kern.bootfile", buf, &buflen, NULL, 0) < 0)
771 file = "/boot/kernel";
775 snprintf(cmd, sizeof(cmd), "nm -n %s", file);
776 if ((fp = popen(cmd, "r")) != NULL) {
777 while (fgets(buf, sizeof(buf), fp) != NULL) {
778 s1 = strtok(buf, " \t\n");
779 s2 = strtok(NULL, " \t\n");
780 s3 = strtok(NULL, " \t\n");
781 if (s1 && s2 && s3) {
782 sym = malloc(sizeof(struct symdata));
783 sym->symaddr = (char *)strtoul(s1, NULL, 16);
784 sym->symtype = s2[0];
785 sym->symname = strdup(s3);
786 if (strcmp(s3, "kernbase") == 0)
787 symbegin = sym->symaddr;
788 if (strcmp(s3, "end") == 0)
789 symend = sym->symaddr;
790 TAILQ_INSERT_TAIL(&symlist, sym, link);
795 symcache = TAILQ_FIRST(&symlist);
800 address_to_symbol(void *kptr, struct save_ctx *ctx)
802 char *buf = ctx->save_buf;
803 int size = sizeof(ctx->save_buf);
805 if (symcache == NULL ||
806 (char *)kptr < symbegin || (char *)kptr >= symend
808 snprintf(buf, size, "%p", kptr);
811 while ((char *)symcache->symaddr < (char *)kptr) {
812 if (TAILQ_NEXT(symcache, link) == NULL)
814 symcache = TAILQ_NEXT(symcache, link);
816 while ((char *)symcache->symaddr > (char *)kptr) {
817 if (symcache != TAILQ_FIRST(&symlist))
818 symcache = TAILQ_PREV(symcache, symlist, link);
820 snprintf(buf, size, "%s+%d", symcache->symname,
821 (int)((char *)kptr - symcache->symaddr));
829 struct ktr_buffer *ktr_bufs, *it;
832 ktr_bufs = malloc(sizeof(*ktr_bufs) * ncpus);
834 err(1, "can't allocate data structures\n");
835 for (i = 0; i < ncpus; ++i) {
837 it->ents = malloc(sizeof(struct ktr_entry) * entries_per_buf);
838 if (it->ents == NULL)
839 err(1, "can't allocate data structures\n");
849 get_indices(struct ktr_entry **ktr_kbuf, int *ktr_idx)
851 static struct ktr_cpu *ktr_cpus;
854 if (ktr_cpus == NULL)
855 ktr_cpus = malloc(sizeof(*ktr_cpus) * ncpus);
857 if (ktr_version < KTR_VERSION_KTR_CPU) {
858 if (kvm_read(kd, nl_version_ktr_idx[0].n_value, ktr_idx,
859 sizeof(*ktr_idx) * ncpus) == -1) {
860 errx(1, "%s", kvm_geterr(kd));
862 if (ktr_kbuf[0] == NULL) {
863 if (kvm_read(kd, nl_version_ktr_idx[1].n_value,
864 ktr_kbuf, sizeof(*ktr_kbuf) * ncpus) == -1) {
865 errx(1, "%s", kvm_geterr(kd));
869 if (kvm_read(kd, nl_version_ktr_cpu[0].n_value,
870 ktr_cpus, sizeof(*ktr_cpus) * ncpus) == -1) {
871 errx(1, "%s", kvm_geterr(kd));
873 for (i = 0; i < ncpus; ++i) {
874 ktr_idx[i] = ktr_cpus[i].core.ktr_idx;
875 ktr_kbuf[i] = ktr_cpus[i].core.ktr_buf;
881 * Get the trace buffer data from the kernel
885 load_bufs(struct ktr_buffer *ktr_bufs, struct ktr_entry **kbufs, int *ktr_idx)
887 struct ktr_buffer *kbuf;
890 get_indices(kbufs, ktr_idx);
891 for (i = 0; i < ncpus; ++i) {
893 if (ktr_idx[i] == kbuf->end_idx)
895 kbuf->end_idx = ktr_idx[i];
898 * If we do not have a notion of the beginning index, assume
899 * it is entries_per_buf before the ending index. Don't
900 * worry about underflows/negative numbers, the indices will
904 kbuf->beg_idx = kbuf->end_idx - entries_per_buf + 1;
907 if (kvm_read(kd, (uintptr_t)kbufs[i], ktr_bufs[i].ents,
908 sizeof(struct ktr_entry) * entries_per_buf)
910 errx(1, "%s", kvm_geterr(kd));
912 kbuf->beg_idx = earliest_ts(kbuf);
918 * Locate the earliest timestamp iterating backwards from end_idx, but
919 * not going further back then beg_idx. We have to do this because
920 * the kernel uses a circulating buffer.
924 earliest_ts(struct ktr_buffer *buf)
926 struct ktr_entry *save;
927 int count, scan, i, earliest;
930 earliest = buf->end_idx - 1;
931 save = &buf->ents[earliest & fifo_mask];
932 for (scan = buf->end_idx - 1; scan != buf->beg_idx -1; --scan) {
933 i = scan & fifo_mask;
934 if (buf->ents[i].ktr_timestamp <= save->ktr_timestamp &&
935 buf->ents[i].ktr_timestamp > 0)
938 * We may have gotten so far behind that beg_idx wrapped
939 * more then once around the buffer. Just stop
941 if (++count == entries_per_buf)
949 iterate_buf(FILE *fo, struct ktr_buffer *ktr_bufs, int cpu,
950 u_int64_t *last_timestamp, ktr_iter_cb_t cb)
952 struct ktr_buffer *buf = ktr_bufs + cpu;
954 if (buf->modified == 0)
956 if (*last_timestamp == 0) {
958 buf->ents[buf->beg_idx & fifo_mask].ktr_timestamp;
960 while (buf->beg_idx != buf->end_idx) {
961 cb(fo, cpu, buf->beg_idx,
962 &buf->ents[buf->beg_idx & fifo_mask],
971 iterate_bufs_timesorted(FILE *fo, struct ktr_buffer *ktr_bufs,
972 u_int64_t *last_timestamp, ktr_iter_cb_t cb)
974 struct ktr_entry *ent;
975 struct ktr_buffer *buf;
983 for (n = 0; n < ncpus; ++n) {
985 if (buf->beg_idx == buf->end_idx)
987 ent = &buf->ents[buf->beg_idx & fifo_mask];
988 if (ts == 0 || (ts >= ent->ktr_timestamp)) {
989 ts = ent->ktr_timestamp;
993 if ((bestn < 0) || (ts < *last_timestamp))
995 buf = ktr_bufs + bestn;
997 &buf->ents[buf->beg_idx & fifo_mask],
1000 *last_timestamp = ts;
1007 kvmfprintf(FILE *fp, const char *ctl, va_list va)
1013 static struct save_ctx strctx;
1017 for (n = 0; ctl[n]; ++n) {
1026 while (n < (int)sizeof(fmt)) {
1053 va_arg(va, long long));
1057 va_arg(va, size_t));
1064 fprintf(fp, "%c", va_arg(va, int));
1072 s = kvm_string(va_arg(va, char *), &strctx);
1073 fwrite(s, 1, strlen(s), fp);
1082 va_arg(va, double));
1121 fprintf(fp, fmt, NULL);
1130 fprintf(stderr, "usage: ktrdump [-acfilnpqrstx] [-A factor] "
1131 "[-N execfile] [-M corefile] [-o outfile]\n");
1135 enum argument_class {
1143 conversion_size(const char *fmt, enum argument_class *argclass)
1146 size_t convsize, intsz;
1148 *argclass = ARGCLASS_ERR;
1153 for (p = fmt + 1; p[0]; ++p) {
1156 * Eat flags. Notice this will accept duplicate
1172 /* Eat minimum field width, if any */
1173 for (; isdigit(p[0]); ++p)
1177 /* Eat precision, if any */
1178 for (; isdigit(p[0]); ++p)
1185 intsz = sizeof(char);
1187 intsz = sizeof(short);
1193 intsz = sizeof(long long);
1195 intsz = sizeof(long);
1199 intsz = sizeof(intmax_t);
1202 intsz = sizeof(ptrdiff_t);
1205 intsz = sizeof(size_t);
1208 p--; /* Anticipate the ++p that follows. Yes, I know. Eeek. */
1212 intsz = sizeof(int);
1217 /* for %c, we only store 1 byte in the ktr entry */
1218 convsize = sizeof(char);
1219 *argclass = ARGCLASS_INTEGER;
1228 *argclass = ARGCLASS_INTEGER;
1231 convsize = sizeof(void *);
1232 *argclass = ARGCLASS_INTEGER;
1236 convsize = sizeof(double);
1238 convsize = sizeof(float);
1240 *argclass = ARGCLASS_FP;
1242 convsize = sizeof(char *);
1243 *argclass = ARGCLASS_INTEGER;
1247 *argclass = ARGCLASS_NONE;
1250 fprintf(stderr, "Unknown conversion specifier %c "
1251 "in fmt starting with %s", p[0], fmt - 1);
1259 va_list_push_integral(struct my_va_list *valist, void *val, size_t valsize,
1266 r = *(uint8_t *)val; break;
1268 r = *(uint32_t *)val; break;
1270 r = (*(uint32_t *)val); break;
1272 r = *(uint64_t *)val; break;
1276 /* we always need to push the full 8 bytes */
1277 if ((valist->gp_offset + valsize) <= 48) { /* got a free reg */
1279 memcpy(((char *)valist->reg_save_area + valist->gp_offset),
1281 valist->gp_offset += sizeof(r);
1284 /* push to "stack" */
1285 if (!(valist->overflow_arg_area = realloc(valist->overflow_arg_area,
1286 *stacksize + sizeof(r))))
1289 * Keep a pointer to the start of the allocated memory block so
1290 * we can free it later. We need to update it after every realloc().
1292 valist->overflow_arg_area_save = valist->overflow_arg_area;
1293 memcpy((char *)valist->overflow_arg_area + *stacksize, &r, sizeof(r));
1294 *stacksize += sizeof(r);
1299 va_list_rewind(struct my_va_list *valist)
1301 valist->gp_offset = 0;
1305 va_list_cleanup(machine_va_list *_valist)
1307 machine_va_list valist;
1308 if (!_valist || !*_valist)
1311 if (valist->reg_save_area)
1312 free(valist->reg_save_area);
1313 if (valist->overflow_arg_area_save)
1314 free(valist->overflow_arg_area_save);
1319 va_list_from_blob(machine_va_list *_valist, const char *fmt, char *blob, size_t blobsize)
1321 machine_va_list valist;
1322 struct reg_save_area *regs;
1326 if (!(valist = malloc(sizeof(*valist))))
1328 if (!(regs = malloc(sizeof(*regs))))
1330 *valist = (struct my_va_list) {
1333 .overflow_arg_area = NULL,
1334 .reg_save_area = regs,
1335 .overflow_arg_area_save = NULL,
1337 enum argument_class argclass;
1338 size_t stacksize = 0;
1340 for (f = fmt; *f != '\0'; ++f) {
1343 sz = conversion_size(f, &argclass);
1344 if (argclass == ARGCLASS_INTEGER) {
1345 if (blobsize < sz) {
1346 fprintf(stderr, "not enough data available "
1347 "for format: %s", fmt);
1350 if (va_list_push_integral(valist, blob, sz, &stacksize))
1354 } else if (argclass != ARGCLASS_NONE)
1356 /* walk past the '%' */
1360 fprintf(stderr, "Couldn't consume all data for format %s "
1361 "(%zd bytes left over)\n", fmt, blobsize);
1364 va_list_rewind(valist);
1368 if (valist->reg_save_area)
1369 free(valist->reg_save_area);
1370 if (valist->overflow_arg_area_save)
1371 free(valist->overflow_arg_area_save);
1380 va_list_cleanup(machine_va_list *valist)
1387 va_list_from_blob(machine_va_list *valist, const char *fmt, char *blob, size_t blobsize)
1392 enum argument_class argclass;
1396 for (f = fmt; *f != '\0'; ++f) {
1399 sz = conversion_size(f, &argclass);
1400 if (blobsize < sz) {
1401 fprintf(stderr, "not enough data available "
1402 "for format: %s", fmt);
1405 if ((argclass == ARGCLASS_INTEGER) && (sz < 4)) {
1406 int i = -1; /* do C integer promotion */
1411 if (!(n = realloc(n, bytes + 4)))
1413 memcpy(n + bytes, &i, sizeof(i));
1416 if (!(n = realloc(n, bytes + sz)))
1418 memcpy(n + bytes, blob, sz);
1426 fprintf(stderr, "Couldn't consume all data for format %s "
1427 "(%zd bytes left over)\n", fmt, blobsize);
1440 #error "Don't know how to get a va_list on this platform"