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