740e23cb3d3e49d4275af0e404118db204b7d17c
[games.git] / usr.bin / ktrdump / ktrdump.c
1 /*-
2  * Copyright (c) 2002 Jake Burkholder
3  * Copyright (c) 2004 Robert Watson
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
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 $
29  */
30
31 #include <sys/cdefs.h>
32
33 #include <sys/types.h>
34 #include <sys/ktr.h>
35 #include <sys/mman.h>
36 #include <sys/stat.h>
37 #include <sys/queue.h>
38
39 #include <ctype.h>
40 #include <devinfo.h>
41 #include <err.h>
42 #include <fcntl.h>
43 #include <kvm.h>
44 #include <limits.h>
45 #include <nlist.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <evtr.h>
52 #include <stdarg.h>
53
54 struct ktr_buffer {
55         struct ktr_entry *ents;
56         int modified;
57         int reset;
58         int beg_idx;            /* Beginning index */
59         int end_idx;            /* Ending index */
60 };
61
62 static struct nlist nl1[] = {
63         { .n_name = "_ktr_version" },
64         { .n_name = "_ktr_entries" },
65         { .n_name = "_ncpus" },
66         { .n_name = NULL }
67 };
68
69 static struct nlist nl2[] = {
70         { .n_name = "_tsc_frequency" },
71         { .n_name = NULL }
72 };
73
74 static struct nlist nl_version_ktr_idx[] = {
75         { .n_name = "_ktr_idx" },
76         { .n_name = "_ktr_buf" },
77         { .n_name = NULL }
78 };
79
80 static struct nlist nl_version_ktr_cpu[] = {
81         { .n_name = "_ktr_cpu" },
82         { .n_name = NULL }
83 };
84
85 struct save_ctx {
86         char save_buf[512];
87         const void *save_kptr;
88 };
89
90 typedef void (*ktr_iter_cb_t)(void *, int, int, struct ktr_entry *, uint64_t *);
91
92 #ifdef __x86_64__
93 /* defined according to the x86_64 ABI spec */
94 struct my_va_list {
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         /*
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)
103          */
104         void *overflow_arg_area_save;
105 } __attribute__((packed));
106
107 typedef struct my_va_list *machine_va_list;
108
109 struct reg_save_area {
110         uint64_t rdi, rsi, rdx, rcx, r8, r9;
111         /* XMM registers follow, but we don't use them */
112 };
113 #elif __i386__
114 typedef void *machine_va_list;
115 #endif
116
117 static int cflag;
118 static int dflag;
119 static int fflag;
120 static int iflag;
121 static int lflag;
122 static int nflag;
123 static int qflag;
124 static int rflag;
125 static int sflag;
126 static int tflag;
127 static int xflag;
128 static int pflag;
129 static int Mflag;
130 static int Nflag;
131 static double tsc_frequency;
132 static double correction_factor = 0.0;
133
134 static char corefile[PATH_MAX];
135 static char execfile[PATH_MAX];
136
137 static char errbuf[_POSIX2_LINE_MAX];
138 static int ncpus;
139 static kvm_t *kd;
140 static int entries_per_buf;
141 static int fifo_mask;
142 static int ktr_version;
143
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);
165 /*
166  * Reads the ktr trace buffer from kernel memory and prints the trace entries.
167  */
168 int
169 main(int ac, char **av)
170 {
171         struct ktr_buffer *ktr_bufs;
172         struct ktr_entry **ktr_kbuf;
173         ktr_iter_cb_t callback = &print_callback;
174         int *ktr_idx;
175         FILE *fo;
176         void *ctx;
177         int64_t tts;
178         int *ktr_start_index;
179         int c;
180         int n;
181
182         /*
183          * Parse commandline arguments.
184          */
185         fo = stdout;
186         while ((c = getopt(ac, av, "acfinqrtxpslA:N:M:o:d")) != -1) {
187                 switch (c) {
188                 case 'a':
189                         cflag = 1;
190                         iflag = 1;
191                         rflag = 1;
192                         xflag = 1;
193                         pflag = 1;
194                         sflag = 1;
195                         break;
196                 case 'c':
197                         cflag = 1;
198                         break;
199                 case 'd':
200                         dflag = 1;
201                         sflag = 1;
202                         callback = &dump_callback;
203                         break;
204                 case 'N':
205                         if (strlcpy(execfile, optarg, sizeof(execfile))
206                             >= sizeof(execfile))
207                                 errx(1, "%s: File name too long", optarg);
208                         Nflag = 1;
209                         break;
210                 case 'f':
211                         fflag = 1;
212                         break;
213                 case 'l':
214                         lflag = 1;
215                         break;
216                 case 'i':
217                         iflag = 1;
218                         break;
219                 case 'A':
220                         correction_factor = strtod(optarg, NULL);
221                         break;
222                 case 'M':
223                         if (strlcpy(corefile, optarg, sizeof(corefile))
224                             >= sizeof(corefile))
225                                 errx(1, "%s: File name too long", optarg);
226                         Mflag = 1;
227                         break;
228                 case 'n':
229                         nflag = 1;
230                         break;
231                 case 'o':
232                         if ((fo = fopen(optarg, "w")) == NULL)
233                                 err(1, "%s", optarg);
234                         break;
235                 case 'p':
236                         pflag++;
237                         break;
238                 case 'q':
239                         qflag++;
240                         break;
241                 case 'r':
242                         rflag = 1;
243                         break;
244                 case 's':
245                         sflag = 1;      /* sort across the cpus */
246                         break;
247                 case 't':
248                         tflag = 1;
249                         break;
250                 case 'x':
251                         xflag = 1;
252                         break;
253                 case '?':
254                 default:
255                         usage();
256                 }
257         }
258         ctx = fo;
259         if (dflag) {
260                 ctx = evtr_open_write(fo);
261                 if (!ctx) {
262                         err(1, "Can't create event stream");
263                 }
264         }
265         if (cflag + iflag + tflag + xflag + fflag + pflag == 0) {
266                 cflag = 1;
267                 iflag = 1;
268                 tflag = 1;
269                 pflag = 1;
270         }
271         if (correction_factor != 0.0 && (rflag == 0 || nflag)) {
272                 fprintf(stderr, "Correction factor can only be applied with -r and without -n\n");
273                 exit(1);
274         }
275         ac -= optind;
276         av += optind;
277         if (ac != 0)
278                 usage();
279
280         /*
281          * Open our execfile and corefile, resolve needed symbols and read in
282          * the trace buffer.
283          */
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;
298         }
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;
305
306         printf("TSC frequency is %6.3f MHz\n", tsc_frequency / 1000000.0);
307
308         if (dflag) {
309                 dump_machine_info((evtr_t)ctx);
310                 dump_device_info((evtr_t)ctx);
311         }
312         ktr_kbuf = calloc(ncpus, sizeof(*ktr_kbuf));
313         ktr_idx = calloc(ncpus, sizeof(*ktr_idx));
314
315         if (nflag == 0)
316                 read_symbols(Nflag ? execfile : NULL);
317
318         if (ktr_version < KTR_VERSION_KTR_CPU) {
319                 if (kvm_nlist(kd, nl_version_ktr_idx))
320                         errx(1, "%s", kvm_geterr(kd));
321         } else {
322                 if (kvm_nlist(kd, nl_version_ktr_cpu))
323                         errx(1, "%s", kvm_geterr(kd));
324         }
325
326         get_indices(ktr_kbuf, ktr_idx);
327
328         ktr_bufs = ktr_bufs_init();
329
330         if (sflag) {
331                 u_int64_t last_timestamp = 0;
332                 do {
333                         load_bufs(ktr_bufs, ktr_kbuf, ktr_idx);
334                         iterate_bufs_timesorted(ctx, ktr_bufs, &last_timestamp,
335                                                 callback);
336                         if (lflag)
337                                 usleep(1000000 / 10);
338                 } while (lflag);
339         } else {
340                 u_int64_t *last_timestamp = calloc(sizeof(u_int64_t), ncpus);
341                 do {
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],
345                                         callback);
346                         if (lflag)
347                                 usleep(1000000 / 10);
348                 } while (lflag);
349         }
350         if (dflag)
351                 evtr_close(ctx);
352         return (0);
353 }
354
355 static
356 int
357 dump_devinfo(struct devinfo_dev *dev, void *arg)
358 {
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;
364
365         if (!dev->dd_name[0])
366                 return 0;
367         ev.type = EVTR_TYPE_PROBE;
368         ev.ts = 0;
369         ev.line = 0;
370         ev.file = NULL;
371         ev.cpu = -1;
372         ev.func = NULL;
373         ev.fmt = fmt;
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);
379
380         if (evtr_dump_event(evtr, &ev)) {
381                 err(1, evtr_errmsg(evtr));
382         }
383
384         return devinfo_foreach_device_child(dev, dump_devinfo, evtr);
385 }
386
387 static
388 void
389 dump_device_info(evtr_t evtr)
390 {
391         struct devinfo_dev *root;
392         if (devinfo_init())
393                 return;
394         if (!(root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE))) {
395                 warn("can't find root device");
396                 return;
397         }
398         devinfo_foreach_device_child(root, dump_devinfo, evtr);
399 }
400
401 static
402 void
403 dump_machine_info(evtr_t evtr)
404 {
405         struct evtr_event ev;
406         int i;
407
408         bzero(&ev, sizeof(ev));
409         ev.type = EVTR_TYPE_SYSINFO;
410         ev.ncpus = ncpus;
411         evtr_dump_event(evtr, &ev);
412         if (evtr_error(evtr)) {
413                 err(1, evtr_errmsg(evtr));
414         }
415
416         for (i = 0; i < ncpus; ++i) {
417                 bzero(&ev, sizeof(ev));
418                 ev.type = EVTR_TYPE_CPUINFO;
419                 ev.cpu = i;
420                 ev.cpuinfo.freq = tsc_frequency;
421                 evtr_dump_event(evtr, &ev);
422                 if (evtr_error(evtr)) {
423                         err(1, evtr_errmsg(evtr));
424                 }
425         }
426 }
427
428 static void
429 print_header(FILE *fo, int row)
430 {
431         if (qflag == 0 && (u_int32_t)row % 20 == 0) {
432                 fprintf(fo, "%-6s ", "index");
433                 if (cflag)
434                         fprintf(fo, "%-3s ", "cpu");
435                 if (tflag || rflag)
436                         fprintf(fo, "%-16s ", "timestamp");
437                 if (xflag) {
438                         if (nflag)
439                             fprintf(fo, "%-10s %-10s", "caller2", "caller1");
440                         else
441                             fprintf(fo, "%-20s %-20s", "caller2", "caller1");
442                 }
443                 if (iflag)
444                         fprintf(fo, "%-20s ", "ID");
445                 if (fflag)
446                         fprintf(fo, "%10s%-30s ", "", "file and line");
447                 if (pflag)
448                         fprintf(fo, "%s", "trace");
449                 fprintf(fo, "\n");
450         }
451 }
452
453 static void
454 print_entry(FILE *fo, int n, int row, struct ktr_entry *entry,
455             u_int64_t *last_timestamp)
456 {
457         struct ktr_info *info = NULL;
458         static struct save_ctx nctx, pctx, fmtctx, symctx, infoctx;
459
460         fprintf(fo, " %06x ", row & 0x00FFFFFF);
461         if (cflag)
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);
467                 } else if (rflag) {
468                         fprintf(fo, "%-16ju ",
469                             (uintmax_t)(entry->ktr_timestamp - *last_timestamp));
470                 } else {
471                         fprintf(fo, "%-16ju ",
472                             (uintmax_t)entry->ktr_timestamp);
473                 }
474         }
475         if (xflag) {
476                 if (nflag) {
477                     fprintf(fo, "%p %p ", 
478                             entry->ktr_caller2, entry->ktr_caller1);
479                 } else {
480                     fprintf(fo, "%-25s ", 
481                             address_to_symbol(entry->ktr_caller2, &symctx));
482                     fprintf(fo, "%-25s ", 
483                             address_to_symbol(entry->ktr_caller1, &symctx));
484                 }
485         }
486         if (iflag) {
487                 info = kvm_ktrinfo(entry->ktr_info, &infoctx);
488                 if (info)
489                         fprintf(fo, "%-20s ", kvm_string(info->kf_name, &nctx));
490                 else
491                         fprintf(fo, "%-20s ", "<empty>");
492         }
493         if (fflag)
494                 fprintf(fo, "%34s:%-4d ",
495                         trunc_path(kvm_string(entry->ktr_file, &pctx), 34),
496                         entry->ktr_line);
497         if (pflag) {
498                 if (info == NULL)
499                         info = kvm_ktrinfo(entry->ktr_info, &infoctx);
500                 if (info) {
501                         machine_va_list ap;
502                         const char *fmt;
503                         fmt = kvm_string(info->kf_format, &fmtctx);
504                         if (va_list_from_blob(&ap, fmt,
505                                               (char *)&entry->ktr_data,
506                                               info->kf_data_size))
507                                 err(2, "Can't generate va_list from %s\n", fmt);
508                         kvmfprintf(fo, kvm_string(info->kf_format, &fmtctx),
509                                    (void *)ap);
510                         va_list_cleanup(&ap);
511                 }
512         }
513         fprintf(fo, "\n");
514         *last_timestamp = entry->ktr_timestamp;
515 }
516
517 static
518 void
519 print_callback(void *ctx, int n, int row, struct ktr_entry *entry, uint64_t *last_ts)
520 {
521         FILE *fo = (FILE *)ctx;
522         print_header(fo, row);
523         print_entry(fo, n, row, entry, last_ts);
524 }
525
526 /*
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.
530  */
531 static
532 int
533 mangle_string_ptrs(const char *fmt, uint8_t *fmtdata, int dofree)
534 {
535         const char *f, *p;
536         size_t skipsize, intsz;
537         static struct save_ctx strctx;
538         int ret = 0;
539
540         for (f = fmt; f[0] != '\0'; ++f) {
541                 if (f[0] != '%')
542                         continue;
543                 ++f;
544                 skipsize = 0;
545                 for (p = f; p[0]; ++p) {
546                         int again = 0;
547                         /*
548                          * Eat flags. Notice this will accept duplicate
549                          * flags.
550                          */
551                         switch (p[0]) {
552                         case '#':
553                         case '0':
554                         case '-':
555                         case ' ':
556                         case '+':
557                         case '\'':
558                                 again = !0;
559                                 break;
560                         }
561                         if (!again)
562                                 break;
563                 }
564                 /* Eat minimum field width, if any */
565                 for (; isdigit(p[0]); ++p)
566                         ;
567                 if (p[0] == '.')
568                         ++p;
569                 /* Eat precision, if any */
570                 for (; isdigit(p[0]); ++p)
571                         ;
572                 intsz = 0;
573                 switch (p[0]) {
574                 case 'l':
575                         if (p[1] == 'l') {
576                                 ++p;
577                                 intsz = sizeof(long long);
578                         } else {
579                                 intsz = sizeof(long);
580                         }
581                         break;
582                 case 'j':
583                         intsz = sizeof(intmax_t);
584                         break;
585                 case 't':
586                         intsz = sizeof(ptrdiff_t);
587                         break;
588                 case 'z':
589                         intsz = sizeof(size_t);
590                         break;
591                 default:
592                         break;
593                 }
594                 if (intsz != 0)
595                         ++p;
596                 else
597                         intsz = sizeof(int);
598
599                 switch (p[0]) {
600                 case 'd':
601                 case 'i':
602                 case 'o':
603                 case 'u':
604                 case 'x':
605                 case 'X':
606                 case 'c':
607                         skipsize = intsz;
608                         break;
609                 case 'p':
610                         skipsize = sizeof(void *);
611                         break;
612                 case 'f':
613                         if (p[-1] == 'l')
614                                 skipsize = sizeof(double);
615                         else
616                                 skipsize = sizeof(float);
617                         break;
618                 case 's':
619                         if (dofree) {
620                           char *t = ((char **)fmtdata)[0];
621                           free(t);
622                           skipsize = sizeof(char *);
623                         } else {
624                           char *t = strdup(kvm_string(((char **)fmtdata)[0],
625                                                           &strctx));
626                           ((const char **)fmtdata)[0] = t;
627                                         
628                                 skipsize = sizeof(char *);
629                         }
630                         ++ret;
631                         break;
632                 default:
633                         fprintf(stderr, "Unknown conversion specifier %c "
634                                 "in fmt starting with %s", p[0], f - 1);
635                         return -1;
636                 }
637                 fmtdata += skipsize;
638         }
639         return ret;
640 }
641
642 static
643 void
644 dump_callback(void *ctx, int n, int row __unused, struct ktr_entry *entry,
645               uint64_t *last_ts __unused)
646 {
647         evtr_t evtr = (evtr_t)ctx;
648         struct evtr_event ev;
649         static struct save_ctx pctx, fmtctx, infoctx;
650         struct ktr_info *ki;
651         int conv = 0;   /* pointless */
652
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);
657         ev.func = NULL;
658         ev.cpu = n;
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),
664                                                0)) < 0)
665                         errx(1, "Can't parse format string\n");
666                 ev.fmtdatalen = ki->kf_data_size;
667         } else {
668                 ev.fmt = ev.fmtdata = NULL;
669                 ev.fmtdatalen = 0;
670         }
671         if (evtr_dump_event(evtr, &ev)) {
672                 err(1, evtr_errmsg(evtr));
673         }
674         if (ev.fmtdata && conv) {
675                 mangle_string_ptrs(ev.fmt, __DECONST(uint8_t *, ev.fmtdata),
676                                    !0);
677         }
678 }
679
680 static
681 struct ktr_info *
682 kvm_ktrinfo(void *kptr, struct save_ctx *ctx)
683 {
684         struct ktr_info *ki = (void *)ctx->save_buf;
685
686         if (kptr == NULL)
687                 return(NULL);
688         if (ctx->save_kptr != kptr) {
689                 if (kvm_read(kd, (uintptr_t)kptr, ki, sizeof(*ki)) == -1) {
690                         bzero(&ki, sizeof(*ki));
691                 } else {
692                         ctx->save_kptr = kptr;
693                 }
694         }
695         return(ki);
696 }
697
698 static
699 const char *
700 kvm_string(const char *kptr, struct save_ctx *ctx)
701 {
702         u_int l;
703         u_int n;
704
705         if (kptr == NULL)
706                 return("?");
707         if (ctx->save_kptr != (const void *)kptr) {
708                 ctx->save_kptr = (const void *)kptr;
709                 l = 0;
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)
715                                 break;
716                         while (l < sizeof(ctx->save_buf) && n) {
717                             if (ctx->save_buf[l] == 0)
718                                     break;
719                             --n;
720                             ++l;
721                         }
722                         if (n)
723                             break;
724                 }
725                 ctx->save_buf[l] = 0;
726         }
727         return(ctx->save_buf);
728 }
729
730 static
731 const char *
732 trunc_path(const char *str, int maxlen)
733 {
734         int len = strlen(str);
735
736         if (len > maxlen)
737                 return(str + len - maxlen);
738         else
739                 return(str);
740 }
741
742 struct symdata {
743         TAILQ_ENTRY(symdata) link;
744         const char *symname;
745         char *symaddr;
746         char symtype;
747 };
748
749 static TAILQ_HEAD(symlist, symdata) symlist;
750 static struct symdata *symcache;
751 static char *symbegin;
752 static char *symend;
753
754 static
755 void
756 read_symbols(const char *file)
757 {
758         char buf[256];
759         char cmd[256];
760         size_t buflen = sizeof(buf);
761         FILE *fp;
762         struct symdata *sym;
763         char *s1;
764         char *s2;
765         char *s3;
766
767         TAILQ_INIT(&symlist);
768
769         if (file == NULL) {
770                 if (sysctlbyname("kern.bootfile", buf, &buflen, NULL, 0) < 0)
771                         file = "/boot/kernel";
772                 else
773                         file = buf;
774         }
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);
791                     }
792                 }
793                 pclose(fp);
794         }
795         symcache = TAILQ_FIRST(&symlist);
796 }
797
798 static
799 const char *
800 address_to_symbol(void *kptr, struct save_ctx *ctx)
801 {
802         char *buf = ctx->save_buf;
803         int size = sizeof(ctx->save_buf);
804
805         if (symcache == NULL ||
806            (char *)kptr < symbegin || (char *)kptr >= symend
807         ) {
808                 snprintf(buf, size, "%p", kptr);
809                 return(buf);
810         }
811         while ((char *)symcache->symaddr < (char *)kptr) {
812                 if (TAILQ_NEXT(symcache, link) == NULL)
813                         break;
814                 symcache = TAILQ_NEXT(symcache, link);
815         }
816         while ((char *)symcache->symaddr > (char *)kptr) {
817                 if (symcache != TAILQ_FIRST(&symlist))
818                         symcache = TAILQ_PREV(symcache, symlist, link);
819         }
820         snprintf(buf, size, "%s+%d", symcache->symname,
821                 (int)((char *)kptr - symcache->symaddr));
822         return(buf);
823 }
824
825 static
826 struct ktr_buffer *
827 ktr_bufs_init(void)
828 {
829         struct ktr_buffer *ktr_bufs, *it;
830         int i;
831
832         ktr_bufs = malloc(sizeof(*ktr_bufs) * ncpus);
833         if (!ktr_bufs)
834                 err(1, "can't allocate data structures\n");
835         for (i = 0; i < ncpus; ++i) {
836                 it = ktr_bufs + 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");
840                 it->reset = 1;
841                 it->beg_idx = -1;
842                 it->end_idx = -1;
843         }
844         return ktr_bufs;
845 }
846
847 static
848 void
849 get_indices(struct ktr_entry **ktr_kbuf, int *ktr_idx)
850 {
851         static struct ktr_cpu *ktr_cpus;
852         int i;
853
854         if (ktr_cpus == NULL)
855                 ktr_cpus = malloc(sizeof(*ktr_cpus) * ncpus);
856
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));
861                 }
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));
866                         }
867                 }
868         } else {
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));
872                 }
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;
876                 }
877         }
878 }
879
880 /*
881  * Get the trace buffer data from the kernel
882  */
883 static
884 void
885 load_bufs(struct ktr_buffer *ktr_bufs, struct ktr_entry **kbufs, int *ktr_idx)
886 {
887         struct ktr_buffer *kbuf;
888         int i;
889
890         get_indices(kbufs, ktr_idx);
891         for (i = 0; i < ncpus; ++i) {
892                 kbuf = &ktr_bufs[i];
893                 if (ktr_idx[i] == kbuf->end_idx)
894                         continue;
895                 kbuf->end_idx = ktr_idx[i];
896
897                 /*
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
901                  * be masked.
902                  */
903                 if (kbuf->reset) {
904                         kbuf->beg_idx = kbuf->end_idx - entries_per_buf + 1;
905                         kbuf->reset = 0;
906                 }
907                 if (kvm_read(kd, (uintptr_t)kbufs[i], ktr_bufs[i].ents,
908                                 sizeof(struct ktr_entry) * entries_per_buf)
909                                                                         == -1)
910                         errx(1, "%s", kvm_geterr(kd));
911                 kbuf->modified = 1;
912                 kbuf->beg_idx = earliest_ts(kbuf);
913         }
914
915 }
916
917 /*
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.
921  */
922 static
923 int
924 earliest_ts(struct ktr_buffer *buf)
925 {
926         struct ktr_entry *save;
927         int count, scan, i, earliest;
928
929         count = 0;
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)
936                         earliest = scan;
937                 /*
938                  * We may have gotten so far behind that beg_idx wrapped
939                  * more then once around the buffer.  Just stop
940                  */
941                 if (++count == entries_per_buf)
942                         break;
943         }
944         return earliest;
945 }
946
947 static
948 void
949 iterate_buf(FILE *fo, struct ktr_buffer *ktr_bufs, int cpu,
950             u_int64_t *last_timestamp, ktr_iter_cb_t cb)
951 {
952         struct ktr_buffer *buf = ktr_bufs + cpu;
953
954         if (buf->modified == 0)
955                 return;
956         if (*last_timestamp == 0) {
957                 *last_timestamp =
958                         buf->ents[buf->beg_idx & fifo_mask].ktr_timestamp;
959         }
960         while (buf->beg_idx != buf->end_idx) {
961                 cb(fo, cpu, buf->beg_idx,
962                    &buf->ents[buf->beg_idx & fifo_mask],
963                    last_timestamp);
964                 ++buf->beg_idx;
965         }
966         buf->modified = 0;
967 }
968
969 static
970 void
971 iterate_bufs_timesorted(FILE *fo, struct ktr_buffer *ktr_bufs,
972                         u_int64_t *last_timestamp, ktr_iter_cb_t cb)
973 {
974         struct ktr_entry *ent;
975         struct ktr_buffer *buf;
976         int n, bestn;
977         u_int64_t ts;
978         static int row = 0;
979
980         for (;;) {
981                 ts = 0;
982                 bestn = -1;
983                 for (n = 0; n < ncpus; ++n) {
984                         buf = ktr_bufs + n;
985                         if (buf->beg_idx == buf->end_idx)
986                                 continue;
987                         ent = &buf->ents[buf->beg_idx & fifo_mask];
988                         if (ts == 0 || (ts >= ent->ktr_timestamp)) {
989                                 ts = ent->ktr_timestamp;
990                                 bestn = n;
991                         }
992                 }
993                 if ((bestn < 0) || (ts < *last_timestamp))
994                         break;
995                 buf = ktr_bufs + bestn;
996                 cb(fo, bestn, row,
997                    &buf->ents[buf->beg_idx & fifo_mask],
998                    last_timestamp);
999                 ++buf->beg_idx;
1000                 *last_timestamp = ts;
1001                 ++row;
1002         }
1003 }
1004
1005 static
1006 void
1007 kvmfprintf(FILE *fp, const char *ctl, va_list va)
1008 {
1009         int n;
1010         int is_long;
1011         int is_done;
1012         char fmt[256];
1013         static struct save_ctx strctx;
1014         const char *s;
1015
1016         while (*ctl) {
1017                 for (n = 0; ctl[n]; ++n) {
1018                         fmt[n] = ctl[n];
1019                         if (ctl[n] == '%')
1020                                 break;
1021                 }
1022                 if (n == 0) {
1023                         is_long = 0;
1024                         is_done = 0;
1025                         n = 1;
1026                         while (n < (int)sizeof(fmt)) {
1027                                 fmt[n] = ctl[n];
1028                                 fmt[n+1] = 0;
1029
1030                                 switch(ctl[n]) {
1031                                 case 'p':
1032                                         is_long = 1;
1033                                         /* fall through */
1034                                 case 'd':
1035                                 case 'u':
1036                                 case 'x':
1037                                 case 'o':
1038                                 case 'X':
1039                                         /*
1040                                          * Integral
1041                                          */
1042                                         switch(is_long) {
1043                                         case 0:
1044                                                 fprintf(fp, fmt,
1045                                                         va_arg(va, int));
1046                                                 break;
1047                                         case 1:
1048                                                 fprintf(fp, fmt,
1049                                                         va_arg(va, long));
1050                                                 break;
1051                                         case 2:
1052                                                 fprintf(fp, fmt,
1053                                                     va_arg(va, long long));
1054                                                 break;
1055                                         case 3:
1056                                                 fprintf(fp, fmt,
1057                                                     va_arg(va, size_t));
1058                                                 break;
1059                                         }
1060                                         ++n;
1061                                         is_done = 1;
1062                                         break;
1063                                 case 'c':
1064                                         fprintf(fp, "%c", va_arg(va, int));
1065                                         ++n;
1066                                         is_done = 1;
1067                                         break;
1068                                 case 's':
1069                                         /*
1070                                          * String
1071                                          */
1072                                         s = kvm_string(va_arg(va, char *), &strctx);
1073                                         fwrite(s, 1, strlen(s), fp);
1074                                         ++n;
1075                                         is_done = 1;
1076                                         break;
1077                                 case 'f':
1078                                         /*
1079                                          * Floating
1080                                          */
1081                                         fprintf(fp, fmt,
1082                                                 va_arg(va, double));
1083                                         ++n;
1084                                         break;
1085                                 case 'j':
1086                                         is_long = 2;
1087                                         break;
1088                                 case 'z':
1089                                         is_long = 3;
1090                                         break;
1091                                 case 'l':
1092                                         if (is_long)
1093                                                 is_long = 2;
1094                                         else
1095                                                 is_long = 1;
1096                                         break;
1097                                 case '.':
1098                                 case '-':
1099                                 case '+':
1100                                 case '0':
1101                                 case '1':
1102                                 case '2':
1103                                 case '3':
1104                                 case '4':
1105                                 case '5':
1106                                 case '6':
1107                                 case '7':
1108                                 case '8':
1109                                 case '9':
1110                                         break;
1111                                 default:
1112                                         is_done = 1;
1113                                         break;
1114                                 }
1115                                 if (is_done)
1116                                         break;
1117                                 ++n;
1118                         }
1119                 } else {
1120                         fmt[n] = 0;
1121                         fprintf(fp, fmt, NULL);
1122                 }
1123                 ctl += n;
1124         }
1125 }
1126
1127 static void
1128 usage(void)
1129 {
1130         fprintf(stderr, "usage: ktrdump [-acfilnpqrstx] [-A factor] "
1131                         "[-N execfile] [-M corefile] [-o outfile]\n");
1132         exit(1);
1133 }
1134
1135 enum argument_class {
1136         ARGCLASS_NONE,
1137         ARGCLASS_INTEGER,
1138         ARGCLASS_FP,
1139         ARGCLASS_MEMORY,
1140         ARGCLASS_ERR,
1141 };
1142 static size_t
1143 conversion_size(const char *fmt, enum argument_class *argclass)
1144 {
1145         const char *p;
1146         size_t convsize, intsz;
1147
1148         *argclass = ARGCLASS_ERR;
1149         if (fmt[0] != '%')
1150                 return -1;
1151
1152         convsize = -1;
1153         for (p = fmt + 1; p[0]; ++p) {
1154                 int again = 0;
1155                 /*
1156                  * Eat flags. Notice this will accept duplicate
1157                  * flags.
1158                  */
1159                 switch (p[0]) {
1160                 case '#':
1161                 case '0':
1162                 case '-':
1163                 case ' ':
1164                 case '+':
1165                 case '\'':
1166                         again = !0;
1167                         break;
1168                 }
1169                 if (!again)
1170                         break;
1171         }
1172         /* Eat minimum field width, if any */
1173         for (; isdigit(p[0]); ++p)
1174                         ;
1175         if (p[0] == '.')
1176                 ++p;
1177         /* Eat precision, if any */
1178         for (; isdigit(p[0]); ++p)
1179                 ;
1180         intsz = 0;
1181         switch (p[0]) {
1182         case 'h':
1183                 if (p[1] == 'h') {
1184                         ++p;
1185                         intsz = sizeof(char);
1186                 } else {
1187                         intsz = sizeof(short);
1188                 }
1189                 break;
1190         case 'l':
1191                 if (p[1] == 'l') {
1192                         ++p;
1193                         intsz = sizeof(long long);
1194                 } else {
1195                         intsz = sizeof(long);
1196                 }
1197                 break;
1198         case 'j':
1199                 intsz = sizeof(intmax_t);
1200                 break;
1201         case 't':
1202                 intsz = sizeof(ptrdiff_t);
1203                 break;
1204         case 'z':
1205                 intsz = sizeof(size_t);
1206                 break;
1207         default:
1208                 p--;    /* Anticipate the ++p that follows. Yes, I know. Eeek. */
1209                 break;
1210         }
1211         if (intsz == 0)
1212                 intsz = sizeof(int);
1213         ++p;
1214
1215         switch (p[0]) {
1216         case 'c':
1217                 /* for %c, we only store 1 byte in the ktr entry */
1218                 convsize = sizeof(char);
1219                 *argclass = ARGCLASS_INTEGER;
1220                 break;
1221         case 'd':
1222         case 'i':
1223         case 'o':
1224         case 'u':
1225         case 'x':
1226         case 'X':
1227                 convsize = intsz;
1228                 *argclass = ARGCLASS_INTEGER;
1229                 break;
1230         case 'p':
1231                 convsize = sizeof(void *);
1232                 *argclass = ARGCLASS_INTEGER;
1233                 break;
1234         case 'f':
1235                 if (p[-1] == 'l')
1236                         convsize = sizeof(double);
1237                 else
1238                         convsize = sizeof(float);
1239                 break;
1240                 *argclass = ARGCLASS_FP;
1241         case 's':
1242                 convsize = sizeof(char *);
1243                 *argclass = ARGCLASS_INTEGER;
1244                 break;
1245         case '%':
1246                 convsize = 0;
1247                 *argclass = ARGCLASS_NONE;
1248                 break;
1249         default:
1250                 fprintf(stderr, "Unknown conversion specifier %c "
1251                         "in fmt starting with %s", p[0], fmt - 1);
1252                 return -2;
1253         }
1254         return convsize;
1255 }
1256
1257 #ifdef __x86_64__
1258 static int
1259 va_list_push_integral(struct my_va_list *valist, void *val, size_t valsize,
1260                      size_t *stacksize)
1261 {
1262         uint64_t r;
1263
1264         switch (valsize) {
1265         case 1:
1266                 r = *(uint8_t *)val; break;
1267         case 2:
1268                 r = *(uint32_t *)val; break;
1269         case 4:
1270                 r = (*(uint32_t *)val); break;
1271         case 8:
1272                 r = *(uint64_t *)val; break;
1273         default:
1274                 err(1, "WTF\n");
1275         }
1276         /* we always need to push the full 8 bytes */
1277         if ((valist->gp_offset + valsize) <= 48) {      /* got a free reg */
1278
1279                 memcpy(((char *)valist->reg_save_area + valist->gp_offset),
1280                        &r, sizeof(r));
1281                 valist->gp_offset += sizeof(r);
1282                 return 0;
1283         }
1284         /* push to "stack" */
1285         if (!(valist->overflow_arg_area = realloc(valist->overflow_arg_area,
1286                                                   *stacksize + sizeof(r))))
1287                 return -1;
1288         /*
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().
1291          */
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);
1295         return 0;
1296 }
1297
1298 static void
1299 va_list_rewind(struct my_va_list *valist)
1300 {
1301         valist->gp_offset = 0;
1302 }
1303
1304 static void
1305 va_list_cleanup(machine_va_list *_valist)
1306 {
1307         machine_va_list valist;
1308         if (!_valist || !*_valist)
1309                 return;
1310         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);
1315         free(valist);
1316 }
1317
1318 static int
1319 va_list_from_blob(machine_va_list *_valist, const char *fmt, char *blob, size_t blobsize)
1320 {
1321         machine_va_list valist;
1322         struct reg_save_area *regs;
1323         const char *f;
1324         size_t sz;
1325
1326         if (!(valist = malloc(sizeof(*valist))))
1327                 return -1;
1328         if (!(regs = malloc(sizeof(*regs))))
1329                 goto free_valist;
1330         *valist = (struct my_va_list) {
1331                 .gp_offset = 0,
1332                 .fp_offset = 0,
1333                 .overflow_arg_area = NULL,
1334                 .reg_save_area = regs,
1335                 .overflow_arg_area_save = NULL,
1336         };
1337         enum argument_class argclass;
1338         size_t stacksize = 0;
1339
1340         for (f = fmt; *f != '\0'; ++f) {
1341                 if (*f != '%')
1342                         continue;
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);
1348                                 goto free_areas;
1349                         }
1350                         if (va_list_push_integral(valist, blob, sz, &stacksize))
1351                                 goto free_areas;
1352                         blob += sz;
1353                         blobsize -= sz;
1354                 } else if (argclass != ARGCLASS_NONE)
1355                         goto free_areas;
1356                 /* walk past the '%' */
1357                 ++f;
1358         }
1359         if (blobsize) {
1360                 fprintf(stderr, "Couldn't consume all data for format %s "
1361                         "(%zd bytes left over)\n", fmt, blobsize);
1362                 goto free_areas;
1363         }
1364         va_list_rewind(valist);
1365         *_valist = valist;
1366         return 0;
1367 free_areas:
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);
1372 free_valist:
1373         free(valist);
1374         *_valist = NULL;
1375         return -1;
1376 }
1377 #elif __i386__
1378
1379 static void
1380 va_list_cleanup(machine_va_list *valist)
1381 {
1382         if (*valist)
1383                 free(*valist);
1384 }
1385
1386 static int
1387 va_list_from_blob(machine_va_list *valist, const char *fmt, char *blob, size_t blobsize)
1388 {
1389         const char *f;
1390         char *n;
1391         size_t bytes, sz;
1392         enum argument_class argclass;
1393
1394         n = NULL;
1395         bytes = 0;
1396         for (f = fmt; *f != '\0'; ++f) {
1397                 if (*f != '%')
1398                         continue;
1399                 sz = conversion_size(f, &argclass);
1400                 if (blobsize < sz) {
1401                         fprintf(stderr, "not enough data available "
1402                                 "for format: %s", fmt);
1403                         goto free_va;
1404                 }
1405                 if ((argclass == ARGCLASS_INTEGER) && (sz < 4)) {
1406                         int i = -1;     /* do C integer promotion */
1407                         if (sz == 1)
1408                                 i = *(char *)blob;
1409                         else
1410                                 i = *(short *)blob;
1411                         if (!(n = realloc(n, bytes + 4)))
1412                                 goto free_va;
1413                         memcpy(n + bytes, &i, sizeof(i));
1414                         bytes += 4;
1415                 } else {
1416                         if (!(n = realloc(n, bytes + sz)))
1417                                 goto free_va;
1418                         memcpy(n + bytes, blob, sz);
1419                         bytes += sz;
1420                 }
1421                 blob += sz;
1422                 blobsize -= sz;
1423
1424         }
1425         if (blobsize) {
1426                 fprintf(stderr, "Couldn't consume all data for format %s "
1427                         "(%zd bytes left over)\n", fmt, blobsize);
1428                 goto free_va;
1429         }
1430         *valist = n;
1431         return 0;
1432 free_va:
1433         if (n)
1434                 free(n);
1435         *valist = NULL;
1436         return -1;
1437 }
1438
1439 #else
1440 #error "Don't know how to get a va_list on this platform"
1441 #endif