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