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 */
99 } __attribute__((packed));
101 typedef struct my_va_list machine_va_list;
103 struct reg_save_area {
104 uint64_t rdi, rsi, rdx, rcx, r8, r9;
105 /* XMM registers follow, but we don't use them */
108 typedef void *machine_va_list;
125 static double tsc_frequency;
126 static double correction_factor = 0.0;
128 static char corefile[PATH_MAX];
129 static char execfile[PATH_MAX];
131 static char errbuf[_POSIX2_LINE_MAX];
134 static int entries_per_buf;
135 static int fifo_mask;
136 static int ktr_version;
138 static void usage(void);
139 static int earliest_ts(struct ktr_buffer *);
140 static void dump_machine_info(evtr_t);
141 static void dump_device_info(evtr_t);
142 static void print_header(FILE *, int);
143 static void print_entry(FILE *, int, int, struct ktr_entry *, u_int64_t *);
144 static void print_callback(void *, int, int, struct ktr_entry *, uint64_t *);
145 static void dump_callback(void *, int, int, struct ktr_entry *, uint64_t *);
146 static struct ktr_info *kvm_ktrinfo(void *, struct save_ctx *);
147 static const char *kvm_string(const char *, struct save_ctx *);
148 static const char *trunc_path(const char *, int);
149 static void read_symbols(const char *);
150 static const char *address_to_symbol(void *, struct save_ctx *);
151 static struct ktr_buffer *ktr_bufs_init(void);
152 static void get_indices(struct ktr_entry **, int *);
153 static void load_bufs(struct ktr_buffer *, struct ktr_entry **, int *);
154 static void iterate_buf(FILE *, struct ktr_buffer *, int, u_int64_t *, ktr_iter_cb_t);
155 static void iterate_bufs_timesorted(FILE *, struct ktr_buffer *, u_int64_t *, ktr_iter_cb_t);
156 static void kvmfprintf(FILE *fp, const char *ctl, va_list va);
157 static int va_list_from_blob(machine_va_list *valist, const char *fmt, char *blob, size_t blobsize);
160 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
163 main(int ac, char **av)
165 struct ktr_buffer *ktr_bufs;
166 struct ktr_entry **ktr_kbuf;
167 ktr_iter_cb_t callback = &print_callback;
172 int *ktr_start_index;
177 * Parse commandline arguments.
180 while ((c = getopt(ac, av, "acfinqrtxpslA:N:M:o:d")) != -1) {
196 callback = &dump_callback;
199 if (strlcpy(execfile, optarg, sizeof(execfile))
201 errx(1, "%s: File name too long", optarg);
214 correction_factor = strtod(optarg, NULL);
217 if (strlcpy(corefile, optarg, sizeof(corefile))
219 errx(1, "%s: File name too long", optarg);
226 if ((fo = fopen(optarg, "w")) == NULL)
227 err(1, "%s", optarg);
239 sflag = 1; /* sort across the cpus */
254 ctx = evtr_open_write(fo);
256 err(1, "Can't create event stream");
259 if (cflag + iflag + tflag + xflag + fflag + pflag == 0) {
265 if (correction_factor != 0.0 && (rflag == 0 || nflag)) {
266 fprintf(stderr, "Correction factor can only be applied with -r and without -n\n");
275 * Open our execfile and corefile, resolve needed symbols and read in
278 if ((kd = kvm_openfiles(Nflag ? execfile : NULL,
279 Mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
280 errx(1, "%s", errbuf);
281 if (kvm_nlist(kd, nl1) != 0)
282 errx(1, "%s", kvm_geterr(kd));
283 if (kvm_read(kd, nl1[0].n_value, &ktr_version, sizeof(ktr_version)) == -1)
284 errx(1, "%s", kvm_geterr(kd));
285 if (kvm_read(kd, nl1[2].n_value, &ncpus, sizeof(ncpus)) == -1)
286 errx(1, "%s", kvm_geterr(kd));
287 ktr_start_index = malloc(sizeof(*ktr_start_index) * ncpus);
288 if (ktr_version >= KTR_VERSION_WITH_FREQ && kvm_nlist(kd, nl2) == 0) {
289 if (kvm_read(kd, nl2[0].n_value, &tts, sizeof(tts)) == -1)
290 errx(1, "%s", kvm_geterr(kd));
291 tsc_frequency = (double)tts;
293 if (ktr_version > KTR_VERSION)
294 errx(1, "ktr version too high for us to handle");
295 if (kvm_read(kd, nl1[1].n_value, &entries_per_buf,
296 sizeof(entries_per_buf)) == -1)
297 errx(1, "%s", kvm_geterr(kd));
298 fifo_mask = entries_per_buf - 1;
300 printf("TSC frequency is %6.3f MHz\n", tsc_frequency / 1000000.0);
303 dump_machine_info((evtr_t)ctx);
304 dump_device_info((evtr_t)ctx);
306 ktr_kbuf = calloc(ncpus, sizeof(*ktr_kbuf));
307 ktr_idx = calloc(ncpus, sizeof(*ktr_idx));
310 read_symbols(Nflag ? execfile : NULL);
312 if (ktr_version < KTR_VERSION_KTR_CPU) {
313 if (kvm_nlist(kd, nl_version_ktr_idx))
314 errx(1, "%s", kvm_geterr(kd));
316 if (kvm_nlist(kd, nl_version_ktr_cpu))
317 errx(1, "%s", kvm_geterr(kd));
320 get_indices(ktr_kbuf, ktr_idx);
322 ktr_bufs = ktr_bufs_init();
325 u_int64_t last_timestamp = 0;
327 load_bufs(ktr_bufs, ktr_kbuf, ktr_idx);
328 iterate_bufs_timesorted(ctx, ktr_bufs, &last_timestamp,
331 usleep(1000000 / 10);
334 u_int64_t *last_timestamp = calloc(sizeof(u_int64_t), ncpus);
336 load_bufs(ktr_bufs, ktr_kbuf, ktr_idx);
337 for (n = 0; n < ncpus; ++n)
338 iterate_buf(ctx, ktr_bufs, n, &last_timestamp[n],
341 usleep(1000000 / 10);
351 dump_devinfo(struct devinfo_dev *dev, void *arg)
353 struct evtr_event ev;
354 evtr_t evtr = (evtr_t)arg;
355 const char *fmt = "#devicenames[\"%s\"] = %#lx";
356 char fmtdatabuf[sizeof(char *) + sizeof(devinfo_handle_t)];
357 char *fmtdata = fmtdatabuf;
359 if (!dev->dd_name[0])
361 ev.type = EVTR_TYPE_PROBE;
368 ((char **)fmtdata)[0] = &dev->dd_name[0];
369 fmtdata += sizeof(char *);
370 ((devinfo_handle_t *)fmtdata)[0] = dev->dd_handle;
371 ev.fmtdata = fmtdatabuf;
372 ev.fmtdatalen = sizeof(fmtdatabuf);
374 if (evtr_dump_event(evtr, &ev)) {
375 err(1, evtr_errmsg(evtr));
378 return devinfo_foreach_device_child(dev, dump_devinfo, evtr);
383 dump_device_info(evtr_t evtr)
385 struct devinfo_dev *root;
388 if (!(root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE))) {
389 warn("can't find root device");
392 devinfo_foreach_device_child(root, dump_devinfo, evtr);
397 dump_machine_info(evtr_t evtr)
399 struct evtr_event ev;
402 bzero(&ev, sizeof(ev));
403 ev.type = EVTR_TYPE_SYSINFO;
405 evtr_dump_event(evtr, &ev);
406 if (evtr_error(evtr)) {
407 err(1, evtr_errmsg(evtr));
410 for (i = 0; i < ncpus; ++i) {
411 bzero(&ev, sizeof(ev));
412 ev.type = EVTR_TYPE_CPUINFO;
414 ev.cpuinfo.freq = tsc_frequency;
415 evtr_dump_event(evtr, &ev);
416 if (evtr_error(evtr)) {
417 err(1, evtr_errmsg(evtr));
423 print_header(FILE *fo, int row)
425 if (qflag == 0 && (u_int32_t)row % 20 == 0) {
426 fprintf(fo, "%-6s ", "index");
428 fprintf(fo, "%-3s ", "cpu");
430 fprintf(fo, "%-16s ", "timestamp");
433 fprintf(fo, "%-10s %-10s", "caller2", "caller1");
435 fprintf(fo, "%-20s %-20s", "caller2", "caller1");
438 fprintf(fo, "%-20s ", "ID");
440 fprintf(fo, "%10s%-30s ", "", "file and line");
442 fprintf(fo, "%s", "trace");
448 print_entry(FILE *fo, int n, int row, struct ktr_entry *entry,
449 u_int64_t *last_timestamp)
451 struct ktr_info *info = NULL;
452 static struct save_ctx nctx, pctx, fmtctx, symctx, infoctx;
454 fprintf(fo, " %06x ", row & 0x00FFFFFF);
456 fprintf(fo, "%-3d ", n);
457 if (tflag || rflag) {
458 if (rflag && !nflag && tsc_frequency != 0.0) {
459 fprintf(fo, "%13.3f uS ",
460 (double)(entry->ktr_timestamp - *last_timestamp) * 1000000.0 / tsc_frequency - correction_factor);
462 fprintf(fo, "%-16ju ",
463 (uintmax_t)(entry->ktr_timestamp - *last_timestamp));
465 fprintf(fo, "%-16ju ",
466 (uintmax_t)entry->ktr_timestamp);
471 fprintf(fo, "%p %p ",
472 entry->ktr_caller2, entry->ktr_caller1);
474 fprintf(fo, "%-25s ",
475 address_to_symbol(entry->ktr_caller2, &symctx));
476 fprintf(fo, "%-25s ",
477 address_to_symbol(entry->ktr_caller1, &symctx));
481 info = kvm_ktrinfo(entry->ktr_info, &infoctx);
483 fprintf(fo, "%-20s ", kvm_string(info->kf_name, &nctx));
485 fprintf(fo, "%-20s ", "<empty>");
488 fprintf(fo, "%34s:%-4d ",
489 trunc_path(kvm_string(entry->ktr_file, &pctx), 34),
493 info = kvm_ktrinfo(entry->ktr_info, &infoctx);
497 fmt = kvm_string(info->kf_format, &fmtctx);
498 if (va_list_from_blob(&ap, fmt,
499 (char *)&entry->ktr_data,
501 err(2, "Can't generate va_list from %s\n", fmt);
502 kvmfprintf(fo, kvm_string(info->kf_format, &fmtctx),
507 *last_timestamp = entry->ktr_timestamp;
512 print_callback(void *ctx, int n, int row, struct ktr_entry *entry, uint64_t *last_ts)
514 FILE *fo = (FILE *)ctx;
515 print_header(fo, row);
516 print_entry(fo, n, row, entry, last_ts);
520 * If free == 0, replace all (kvm) string pointers in fmtdata with pointers
521 * to user-allocated copies of the strings.
522 * If free != 0, free those pointers.
526 mangle_string_ptrs(const char *fmt, uint8_t *fmtdata, int dofree)
529 size_t skipsize, intsz;
530 static struct save_ctx strctx;
533 for (f = fmt; f[0] != '\0'; ++f) {
538 for (p = f; p[0]; ++p) {
541 * Eat flags. Notice this will accept duplicate
557 /* Eat minimum field width, if any */
558 for (; isdigit(p[0]); ++p)
562 /* Eat precision, if any */
563 for (; isdigit(p[0]); ++p)
570 intsz = sizeof(long long);
572 intsz = sizeof(long);
576 intsz = sizeof(intmax_t);
579 intsz = sizeof(ptrdiff_t);
582 intsz = sizeof(size_t);
603 skipsize = sizeof(void *);
607 skipsize = sizeof(double);
609 skipsize = sizeof(float);
613 char *t = ((char **)fmtdata)[0];
615 skipsize = sizeof(char *);
617 char *t = strdup(kvm_string(((char **)fmtdata)[0],
619 ((const char **)fmtdata)[0] = t;
621 skipsize = sizeof(char *);
626 fprintf(stderr, "Unknown conversion specifier %c "
627 "in fmt starting with %s", p[0], f - 1);
637 dump_callback(void *ctx, int n, int row __unused, struct ktr_entry *entry,
638 uint64_t *last_ts __unused)
640 evtr_t evtr = (evtr_t)ctx;
641 struct evtr_event ev;
642 static struct save_ctx pctx, fmtctx, infoctx;
644 int conv = 0; /* pointless */
646 ev.ts = entry->ktr_timestamp;
647 ev.type = EVTR_TYPE_PROBE;
648 ev.line = entry->ktr_line;
649 ev.file = kvm_string(entry->ktr_file, &pctx);
652 if ((ki = kvm_ktrinfo(entry->ktr_info, &infoctx))) {
653 ev.fmt = kvm_string(ki->kf_format, &fmtctx);
654 ev.fmtdata = entry->ktr_data;
655 if ((conv = mangle_string_ptrs(ev.fmt,
656 __DECONST(uint8_t *, ev.fmtdata),
658 errx(1, "Can't parse format string\n");
659 ev.fmtdatalen = ki->kf_data_size;
661 ev.fmt = ev.fmtdata = NULL;
664 if (evtr_dump_event(evtr, &ev)) {
665 err(1, evtr_errmsg(evtr));
667 if (ev.fmtdata && conv) {
668 mangle_string_ptrs(ev.fmt, __DECONST(uint8_t *, ev.fmtdata),
675 kvm_ktrinfo(void *kptr, struct save_ctx *ctx)
677 struct ktr_info *ki = (void *)ctx->save_buf;
681 if (ctx->save_kptr != kptr) {
682 if (kvm_read(kd, (uintptr_t)kptr, ki, sizeof(*ki)) == -1) {
683 bzero(&ki, sizeof(*ki));
685 ctx->save_kptr = kptr;
693 kvm_string(const char *kptr, struct save_ctx *ctx)
700 if (ctx->save_kptr != (const void *)kptr) {
701 ctx->save_kptr = (const void *)kptr;
703 while (l < sizeof(ctx->save_buf) - 1) {
704 n = 256 - ((intptr_t)(kptr + l) & 255);
705 if (n > sizeof(ctx->save_buf) - l - 1)
706 n = sizeof(ctx->save_buf) - l - 1;
707 if (kvm_read(kd, (uintptr_t)(kptr + l), ctx->save_buf + l, n) < 0)
709 while (l < sizeof(ctx->save_buf) && n) {
710 if (ctx->save_buf[l] == 0)
718 ctx->save_buf[l] = 0;
720 return(ctx->save_buf);
725 trunc_path(const char *str, int maxlen)
727 int len = strlen(str);
730 return(str + len - maxlen);
736 TAILQ_ENTRY(symdata) link;
742 static TAILQ_HEAD(symlist, symdata) symlist;
743 static struct symdata *symcache;
744 static char *symbegin;
749 read_symbols(const char *file)
753 size_t buflen = sizeof(buf);
760 TAILQ_INIT(&symlist);
763 if (sysctlbyname("kern.bootfile", buf, &buflen, NULL, 0) < 0)
764 file = "/boot/kernel";
768 snprintf(cmd, sizeof(cmd), "nm -n %s", file);
769 if ((fp = popen(cmd, "r")) != NULL) {
770 while (fgets(buf, sizeof(buf), fp) != NULL) {
771 s1 = strtok(buf, " \t\n");
772 s2 = strtok(NULL, " \t\n");
773 s3 = strtok(NULL, " \t\n");
774 if (s1 && s2 && s3) {
775 sym = malloc(sizeof(struct symdata));
776 sym->symaddr = (char *)strtoul(s1, NULL, 16);
777 sym->symtype = s2[0];
778 sym->symname = strdup(s3);
779 if (strcmp(s3, "kernbase") == 0)
780 symbegin = sym->symaddr;
781 if (strcmp(s3, "end") == 0)
782 symend = sym->symaddr;
783 TAILQ_INSERT_TAIL(&symlist, sym, link);
788 symcache = TAILQ_FIRST(&symlist);
793 address_to_symbol(void *kptr, struct save_ctx *ctx)
795 char *buf = ctx->save_buf;
796 int size = sizeof(ctx->save_buf);
798 if (symcache == NULL ||
799 (char *)kptr < symbegin || (char *)kptr >= symend
801 snprintf(buf, size, "%p", kptr);
804 while ((char *)symcache->symaddr < (char *)kptr) {
805 if (TAILQ_NEXT(symcache, link) == NULL)
807 symcache = TAILQ_NEXT(symcache, link);
809 while ((char *)symcache->symaddr > (char *)kptr) {
810 if (symcache != TAILQ_FIRST(&symlist))
811 symcache = TAILQ_PREV(symcache, symlist, link);
813 snprintf(buf, size, "%s+%d", symcache->symname,
814 (int)((char *)kptr - symcache->symaddr));
822 struct ktr_buffer *ktr_bufs, *it;
825 ktr_bufs = malloc(sizeof(*ktr_bufs) * ncpus);
827 err(1, "can't allocate data structures\n");
828 for (i = 0; i < ncpus; ++i) {
830 it->ents = malloc(sizeof(struct ktr_entry) * entries_per_buf);
831 if (it->ents == NULL)
832 err(1, "can't allocate data structures\n");
842 get_indices(struct ktr_entry **ktr_kbuf, int *ktr_idx)
844 static struct ktr_cpu *ktr_cpus;
847 if (ktr_cpus == NULL)
848 ktr_cpus = malloc(sizeof(*ktr_cpus) * ncpus);
850 if (ktr_version < KTR_VERSION_KTR_CPU) {
851 if (kvm_read(kd, nl_version_ktr_idx[0].n_value, ktr_idx,
852 sizeof(*ktr_idx) * ncpus) == -1) {
853 errx(1, "%s", kvm_geterr(kd));
855 if (ktr_kbuf[0] == NULL) {
856 if (kvm_read(kd, nl_version_ktr_idx[1].n_value,
857 ktr_kbuf, sizeof(*ktr_kbuf) * ncpus) == -1) {
858 errx(1, "%s", kvm_geterr(kd));
862 if (kvm_read(kd, nl_version_ktr_cpu[0].n_value,
863 ktr_cpus, sizeof(*ktr_cpus) * ncpus) == -1) {
864 errx(1, "%s", kvm_geterr(kd));
866 for (i = 0; i < ncpus; ++i) {
867 ktr_idx[i] = ktr_cpus[i].core.ktr_idx;
868 ktr_kbuf[i] = ktr_cpus[i].core.ktr_buf;
874 * Get the trace buffer data from the kernel
878 load_bufs(struct ktr_buffer *ktr_bufs, struct ktr_entry **kbufs, int *ktr_idx)
880 struct ktr_buffer *kbuf;
883 get_indices(kbufs, ktr_idx);
884 for (i = 0; i < ncpus; ++i) {
886 if (ktr_idx[i] == kbuf->end_idx)
888 kbuf->end_idx = ktr_idx[i];
891 * If we do not have a notion of the beginning index, assume
892 * it is entries_per_buf before the ending index. Don't
893 * worry about underflows/negative numbers, the indices will
897 kbuf->beg_idx = kbuf->end_idx - entries_per_buf + 1;
900 if (kvm_read(kd, (uintptr_t)kbufs[i], ktr_bufs[i].ents,
901 sizeof(struct ktr_entry) * entries_per_buf)
903 errx(1, "%s", kvm_geterr(kd));
905 kbuf->beg_idx = earliest_ts(kbuf);
911 * Locate the earliest timestamp iterating backwards from end_idx, but
912 * not going further back then beg_idx. We have to do this because
913 * the kernel uses a circulating buffer.
917 earliest_ts(struct ktr_buffer *buf)
919 struct ktr_entry *save;
920 int count, scan, i, earliest;
923 earliest = buf->end_idx - 1;
924 save = &buf->ents[earliest & fifo_mask];
925 for (scan = buf->end_idx - 1; scan != buf->beg_idx -1; --scan) {
926 i = scan & fifo_mask;
927 if (buf->ents[i].ktr_timestamp <= save->ktr_timestamp &&
928 buf->ents[i].ktr_timestamp > 0)
931 * We may have gotten so far behind that beg_idx wrapped
932 * more then once around the buffer. Just stop
934 if (++count == entries_per_buf)
942 iterate_buf(FILE *fo, struct ktr_buffer *ktr_bufs, int cpu,
943 u_int64_t *last_timestamp, ktr_iter_cb_t cb)
945 struct ktr_buffer *buf = ktr_bufs + cpu;
947 if (buf->modified == 0)
949 if (*last_timestamp == 0) {
951 buf->ents[buf->beg_idx & fifo_mask].ktr_timestamp;
953 while (buf->beg_idx != buf->end_idx) {
954 cb(fo, cpu, buf->beg_idx,
955 &buf->ents[buf->beg_idx & fifo_mask],
964 iterate_bufs_timesorted(FILE *fo, struct ktr_buffer *ktr_bufs,
965 u_int64_t *last_timestamp, ktr_iter_cb_t cb)
967 struct ktr_entry *ent;
968 struct ktr_buffer *buf;
976 for (n = 0; n < ncpus; ++n) {
978 if (buf->beg_idx == buf->end_idx)
980 ent = &buf->ents[buf->beg_idx & fifo_mask];
981 if (ts == 0 || (ts >= ent->ktr_timestamp)) {
982 ts = ent->ktr_timestamp;
986 if ((bestn < 0) || (ts < *last_timestamp))
988 buf = ktr_bufs + bestn;
990 &buf->ents[buf->beg_idx & fifo_mask],
993 *last_timestamp = ts;
1000 kvmfprintf(FILE *fp, const char *ctl, va_list va)
1006 static struct save_ctx strctx;
1010 for (n = 0; ctl[n]; ++n) {
1019 while (n < (int)sizeof(fmt)) {
1046 va_arg(va, long long));
1050 va_arg(va, size_t));
1057 fprintf(fp, "%c", va_arg(va, int));
1065 s = kvm_string(va_arg(va, char *), &strctx);
1066 fwrite(s, 1, strlen(s), fp);
1075 va_arg(va, double));
1114 fprintf(fp, fmt, NULL);
1123 fprintf(stderr, "usage: ktrdump [-acfilnpqrstx] [-A factor] "
1124 "[-N execfile] [-M corefile] [-o outfile]\n");
1128 enum argument_class {
1136 conversion_size(const char *fmt, enum argument_class *argclass)
1139 size_t convsize, intsz;
1141 *argclass = ARGCLASS_ERR;
1146 for (p = fmt + 1; p[0]; ++p) {
1149 * Eat flags. Notice this will accept duplicate
1165 /* Eat minimum field width, if any */
1166 for (; isdigit(p[0]); ++p)
1170 /* Eat precision, if any */
1171 for (; isdigit(p[0]); ++p)
1178 intsz = sizeof(char);
1180 intsz = sizeof(short);
1186 intsz = sizeof(long long);
1188 intsz = sizeof(long);
1192 intsz = sizeof(intmax_t);
1195 intsz = sizeof(ptrdiff_t);
1198 intsz = sizeof(size_t);
1201 p--; /* Anticipate the ++p that follows. Yes, I know. Eeek. */
1205 intsz = sizeof(int);
1210 /* for %c, we only store 1 byte in the ktr entry */
1211 convsize = sizeof(char);
1212 *argclass = ARGCLASS_INTEGER;
1221 *argclass = ARGCLASS_INTEGER;
1224 convsize = sizeof(void *);
1225 *argclass = ARGCLASS_INTEGER;
1229 convsize = sizeof(double);
1231 convsize = sizeof(float);
1233 *argclass = ARGCLASS_FP;
1235 convsize = sizeof(char *);
1236 *argclass = ARGCLASS_INTEGER;
1240 *argclass = ARGCLASS_NONE;
1243 fprintf(stderr, "Unknown conversion specifier %c "
1244 "in fmt starting with %s", p[0], fmt - 1);
1252 va_list_push_integral(struct my_va_list *valist, void *val, size_t valsize,
1259 r = *(uint8_t *)val; break;
1261 r = *(uint32_t *)val; break;
1263 r = (*(uint32_t *)val); break;
1265 r = *(uint64_t *)val; break;
1269 /* we always need to push the full 8 bytes */
1270 if ((valist->gp_offset + valsize) <= 48) { /* got a free reg */
1272 memcpy(((char *)valist->reg_save_area + valist->gp_offset),
1274 valist->gp_offset += sizeof(r);
1277 /* push to "stack" */
1278 if (!(valist->overflow_arg_area = realloc(valist->overflow_arg_area,
1279 *stacksize + sizeof(r))))
1281 memcpy((char *)valist->overflow_arg_area + *stacksize, &r, sizeof(r));
1282 *stacksize += sizeof(r);
1287 va_list_rewind(struct my_va_list *valist)
1289 valist->gp_offset = 0;
1293 va_list_from_blob(machine_va_list *valist, const char *fmt, char *blob, size_t blobsize)
1295 struct reg_save_area *regs;
1299 if (!(regs = malloc(sizeof(*regs))))
1301 *valist = (struct my_va_list) {
1304 .overflow_arg_area = NULL,
1305 .reg_save_area = regs,
1307 enum argument_class argclass;
1308 size_t stacksize = 0;
1310 for (f = fmt; *f != '\0'; ++f) {
1313 sz = conversion_size(f, &argclass);
1314 if (argclass == ARGCLASS_INTEGER) {
1315 if (blobsize < sz) {
1316 fprintf(stderr, "not enough data available "
1317 "for format: %s", fmt);
1320 if (va_list_push_integral(valist, blob, sz, &stacksize))
1324 } else if (argclass != ARGCLASS_NONE)
1326 /* walk past the '%' */
1330 fprintf(stderr, "Couldn't consume all data for format %s "
1331 "(%zd bytes left over)\n", fmt, blobsize);
1334 va_list_rewind(valist);
1339 va_list_from_blob(machine_va_list *valist, const char *fmt, char *blob, size_t blobsize)
1344 enum argument_class argclass;
1348 for (f = fmt; *f != '\0'; ++f) {
1351 sz = conversion_size(f, &argclass);
1352 if (blobsize < sz) {
1353 fprintf(stderr, "not enough data available "
1354 "for format: %s", fmt);
1357 if ((argclass == ARGCLASS_INTEGER) && (sz < 4)) {
1358 int i = -1; /* do C integer promotion */
1363 if (!(n = realloc(n, bytes + 4)))
1365 memcpy(n + bytes, &i, sizeof(i));
1368 if (!(n = realloc(n, bytes + sz)))
1370 memcpy(n + bytes, blob, sz);
1378 fprintf(stderr, "Couldn't consume all data for format %s "
1379 "(%zd bytes left over)\n", fmt, blobsize);
1386 #error "Don't know how to get a va_list on this platform"