libevtr: fix thinko
[dragonfly.git] / usr.bin / ktrdump / ktrdump.c
CommitLineData
9f815599
HP
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.
6e8dad34
MD
26 *
27 * $FreeBSD: src/usr.bin/ktrdump/ktrdump.c,v 1.10 2005/05/21 09:55:06 ru Exp $
5b107af3 28 * $DragonFly: src/usr.bin/ktrdump/ktrdump.c,v 1.13 2008/11/10 02:05:31 swildner Exp $
9f815599
HP
29 */
30
6e8dad34
MD
31#include <sys/cdefs.h>
32
33#include <sys/types.h>
9f815599
HP
34#include <sys/ktr.h>
35#include <sys/mman.h>
36#include <sys/stat.h>
fa9963c9 37#include <sys/queue.h>
9f815599 38
e7c0dbba 39#include <ctype.h>
9f815599
HP
40#include <err.h>
41#include <fcntl.h>
42#include <kvm.h>
43#include <limits.h>
44#include <nlist.h>
45#include <stdint.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <unistd.h>
e7c0dbba 50#include <evtr.h>
08cca235 51#include <stdarg.h>
9f815599 52
c2d873bd
MD
53struct ktr_buffer {
54 struct ktr_entry *ents;
55 int modified;
56 int reset;
57 int beg_idx; /* Beginning index */
58 int end_idx; /* Ending index */
59};
9f815599 60
ddca1582 61static struct nlist nl1[] = {
5b107af3
SW
62 { .n_name = "_ktr_version" },
63 { .n_name = "_ktr_entries" },
5b107af3
SW
64 { .n_name = "_ncpus" },
65 { .n_name = NULL }
b85aeba0 66};
ddca1582 67
b85aeba0 68static struct nlist nl2[] = {
5b107af3
SW
69 { .n_name = "_tsc_frequency" },
70 { .n_name = NULL }
9f815599
HP
71};
72
ddca1582
MD
73static struct nlist nl_version_ktr_idx[] = {
74 { .n_name = "_ktr_idx" },
75 { .n_name = "_ktr_buf" },
76 { .n_name = NULL }
77};
78
79static struct nlist nl_version_ktr_cpu[] = {
80 { .n_name = "_ktr_cpu" },
81 { .n_name = NULL }
82};
83
e7c0dbba
AE
84struct save_ctx {
85 char save_buf[512];
86 const void *save_kptr;
87};
88
89typedef void (*ktr_iter_cb_t)(void *, int, int, struct ktr_entry *, uint64_t *);
90
9f815599 91static int cflag;
e7c0dbba 92static int dflag;
9f815599 93static int fflag;
6e8dad34 94static int iflag;
c2d873bd 95static int lflag;
6e8dad34 96static int nflag;
9f815599
HP
97static int qflag;
98static int rflag;
b8272239 99static int sflag;
9f815599 100static int tflag;
6e8dad34
MD
101static int xflag;
102static int pflag;
fa9963c9
MD
103static int Mflag;
104static int Nflag;
b8272239 105static double tsc_frequency;
dbf2e4fa 106static double correction_factor = 0.0;
9f815599
HP
107
108static char corefile[PATH_MAX];
109static char execfile[PATH_MAX];
110
9f815599 111static char errbuf[_POSIX2_LINE_MAX];
c2d873bd
MD
112static int ncpus;
113static kvm_t *kd;
114static int entries_per_buf;
115static int fifo_mask;
ddca1582 116static int ktr_version;
c2d873bd
MD
117
118static void usage(void);
5b107af3 119static int earliest_ts(struct ktr_buffer *);
e7c0dbba 120static void dump_machine_info(evtr_t);
5b107af3
SW
121static void print_header(FILE *, int);
122static void print_entry(FILE *, int, int, struct ktr_entry *, u_int64_t *);
e7c0dbba
AE
123static void print_callback(void *, int, int, struct ktr_entry *, uint64_t *);
124static void dump_callback(void *, int, int, struct ktr_entry *, uint64_t *);
125static struct ktr_info *kvm_ktrinfo(void *, struct save_ctx *);
126static const char *kvm_string(const char *, struct save_ctx *);
5b107af3
SW
127static const char *trunc_path(const char *, int);
128static void read_symbols(const char *);
e7c0dbba 129static const char *address_to_symbol(void *, struct save_ctx *);
5b107af3 130static struct ktr_buffer *ktr_bufs_init(void);
ddca1582
MD
131static void get_indices(struct ktr_entry **, int *);
132static void load_bufs(struct ktr_buffer *, struct ktr_entry **, int *);
e7c0dbba
AE
133static void iterate_buf(FILE *, struct ktr_buffer *, int, u_int64_t *, ktr_iter_cb_t);
134static void iterate_bufs_timesorted(FILE *, struct ktr_buffer *, u_int64_t *, ktr_iter_cb_t);
08cca235 135static void kvmfprintf(FILE *fp, const char *ctl, va_list va);
9f815599
HP
136
137/*
138 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
139 */
140int
141main(int ac, char **av)
142{
c2d873bd
MD
143 struct ktr_buffer *ktr_bufs;
144 struct ktr_entry **ktr_kbuf;
e7c0dbba 145 ktr_iter_cb_t callback = &print_callback;
ddca1582 146 int *ktr_idx;
6e8dad34 147 FILE *fo;
e7c0dbba 148 void *ctx;
b8272239
MD
149 int64_t tts;
150 int *ktr_start_index;
9f815599 151 int c;
6e8dad34 152 int n;
9f815599
HP
153
154 /*
155 * Parse commandline arguments.
156 */
6e8dad34 157 fo = stdout;
e7c0dbba 158 while ((c = getopt(ac, av, "acfinqrtxpslA:N:M:o:d")) != -1) {
9f815599 159 switch (c) {
6e8dad34
MD
160 case 'a':
161 cflag = 1;
162 iflag = 1;
429e7604 163 rflag = 1;
6e8dad34 164 xflag = 1;
6e8dad34 165 pflag = 1;
429e7604
MD
166 rflag = 1;
167 sflag = 1;
6e8dad34 168 break;
9f815599
HP
169 case 'c':
170 cflag = 1;
171 break;
e7c0dbba
AE
172 case 'd':
173 dflag = 1;
174 callback = &dump_callback;
175 break;
6e8dad34 176 case 'N':
9f815599
HP
177 if (strlcpy(execfile, optarg, sizeof(execfile))
178 >= sizeof(execfile))
179 errx(1, "%s: File name too long", optarg);
fa9963c9 180 Nflag = 1;
9f815599
HP
181 break;
182 case 'f':
183 fflag = 1;
184 break;
c2d873bd
MD
185 case 'l':
186 lflag = 1;
187 break;
9f815599
HP
188 case 'i':
189 iflag = 1;
9f815599 190 break;
dbf2e4fa
MD
191 case 'A':
192 correction_factor = strtod(optarg, NULL);
193 break;
6e8dad34 194 case 'M':
9f815599
HP
195 if (strlcpy(corefile, optarg, sizeof(corefile))
196 >= sizeof(corefile))
197 errx(1, "%s: File name too long", optarg);
fa9963c9
MD
198 Mflag = 1;
199 break;
200 case 'n':
201 nflag = 1;
9f815599
HP
202 break;
203 case 'o':
6e8dad34 204 if ((fo = fopen(optarg, "w")) == NULL)
9f815599
HP
205 err(1, "%s", optarg);
206 break;
6e8dad34
MD
207 case 'p':
208 pflag++;
209 break;
9f815599
HP
210 case 'q':
211 qflag++;
212 break;
213 case 'r':
214 rflag = 1;
215 break;
b8272239
MD
216 case 's':
217 sflag = 1; /* sort across the cpus */
218 break;
9f815599
HP
219 case 't':
220 tflag = 1;
221 break;
6e8dad34
MD
222 case 'x':
223 xflag = 1;
224 break;
9f815599
HP
225 case '?':
226 default:
227 usage();
228 }
fa9963c9 229 }
e7c0dbba
AE
230 ctx = fo;
231 if (dflag) {
232 ctx = evtr_open_write(fo);
233 if (!ctx) {
234 err(1, "Can't create event stream");
235 }
236 }
fa9963c9
MD
237 if (cflag + iflag + tflag + xflag + fflag + pflag == 0) {
238 cflag = 1;
239 iflag = 1;
240 tflag = 1;
fa9963c9
MD
241 pflag = 1;
242 }
dbf2e4fa
MD
243 if (correction_factor != 0.0 && (rflag == 0 || nflag)) {
244 fprintf(stderr, "Correction factor can only be applied with -r and without -n\n");
245 exit(1);
246 }
9f815599
HP
247 ac -= optind;
248 av += optind;
249 if (ac != 0)
250 usage();
251
252 /*
253 * Open our execfile and corefile, resolve needed symbols and read in
254 * the trace buffer.
255 */
fa9963c9
MD
256 if ((kd = kvm_openfiles(Nflag ? execfile : NULL,
257 Mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
9f815599 258 errx(1, "%s", errbuf);
ddca1582 259 if (kvm_nlist(kd, nl1) != 0)
6e8dad34 260 errx(1, "%s", kvm_geterr(kd));
ddca1582 261 if (kvm_read(kd, nl1[0].n_value, &ktr_version, sizeof(ktr_version)) == -1)
9f815599 262 errx(1, "%s", kvm_geterr(kd));
ddca1582 263 if (kvm_read(kd, nl1[2].n_value, &ncpus, sizeof(ncpus)) == -1)
6e8dad34 264 errx(1, "%s", kvm_geterr(kd));
b8272239 265 ktr_start_index = malloc(sizeof(*ktr_start_index) * ncpus);
ddca1582 266 if (ktr_version >= KTR_VERSION_WITH_FREQ && kvm_nlist(kd, nl2) == 0) {
b85aeba0 267 if (kvm_read(kd, nl2[0].n_value, &tts, sizeof(tts)) == -1)
b8272239
MD
268 errx(1, "%s", kvm_geterr(kd));
269 tsc_frequency = (double)tts;
270 }
ddca1582 271 if (ktr_version > KTR_VERSION)
b8272239 272 errx(1, "ktr version too high for us to handle");
ddca1582 273 if (kvm_read(kd, nl1[1].n_value, &entries_per_buf,
c2d873bd 274 sizeof(entries_per_buf)) == -1)
6e8dad34 275 errx(1, "%s", kvm_geterr(kd));
c2d873bd
MD
276 fifo_mask = entries_per_buf - 1;
277
278 printf("TSC frequency is %6.3f MHz\n", tsc_frequency / 1000000.0);
279
e7c0dbba
AE
280 if (dflag) {
281 dump_machine_info((evtr_t)ctx);
282 }
ddca1582
MD
283 ktr_kbuf = calloc(ncpus, sizeof(*ktr_kbuf));
284 ktr_idx = calloc(ncpus, sizeof(*ktr_idx));
6e8dad34 285
fa9963c9
MD
286 if (nflag == 0)
287 read_symbols(Nflag ? execfile : NULL);
288
ddca1582
MD
289 if (ktr_version < KTR_VERSION_KTR_CPU) {
290 if (kvm_nlist(kd, nl_version_ktr_idx))
291 errx(1, "%s", kvm_geterr(kd));
292 } else {
293 if (kvm_nlist(kd, nl_version_ktr_cpu))
294 errx(1, "%s", kvm_geterr(kd));
295 }
296
297 get_indices(ktr_kbuf, ktr_idx);
9f815599 298
5b107af3 299 ktr_bufs = ktr_bufs_init();
b8272239 300
b8272239 301 if (sflag) {
c2d873bd
MD
302 u_int64_t last_timestamp = 0;
303 do {
ddca1582 304 load_bufs(ktr_bufs, ktr_kbuf, ktr_idx);
e7c0dbba
AE
305 iterate_bufs_timesorted(ctx, ktr_bufs, &last_timestamp,
306 callback);
c2d873bd
MD
307 if (lflag)
308 usleep(1000000 / 10);
309 } while (lflag);
b8272239 310 } else {
c2d873bd
MD
311 u_int64_t *last_timestamp = calloc(sizeof(u_int64_t), ncpus);
312 do {
ddca1582 313 load_bufs(ktr_bufs, ktr_kbuf, ktr_idx);
c2d873bd 314 for (n = 0; n < ncpus; ++n)
e7c0dbba
AE
315 iterate_buf(ctx, ktr_bufs, n, &last_timestamp[n],
316 callback);
c2d873bd
MD
317 if (lflag)
318 usleep(1000000 / 10);
319 } while (lflag);
6e8dad34 320 }
e7c0dbba
AE
321 if (dflag)
322 evtr_close(ctx);
6e8dad34
MD
323 return (0);
324}
9f815599 325
e7c0dbba
AE
326static
327void
328dump_machine_info(evtr_t evtr)
329{
330 struct evtr_event ev;
331
332 ev.type = EVTR_TYPE_CPUINFO;
333 ev.ncpus = ncpus;
334
335 evtr_dump_event(evtr, &ev);
336}
337
6e8dad34
MD
338static void
339print_header(FILE *fo, int row)
340{
c2d873bd 341 if (qflag == 0 && (u_int32_t)row % 20 == 0) {
6e8dad34 342 fprintf(fo, "%-6s ", "index");
9f815599 343 if (cflag)
6e8dad34
MD
344 fprintf(fo, "%-3s ", "cpu");
345 if (tflag || rflag)
346 fprintf(fo, "%-16s ", "timestamp");
fa9963c9
MD
347 if (xflag) {
348 if (nflag)
349 fprintf(fo, "%-10s %-10s", "caller2", "caller1");
350 else
351 fprintf(fo, "%-20s %-20s", "caller2", "caller1");
352 }
6e8dad34
MD
353 if (iflag)
354 fprintf(fo, "%-20s ", "ID");
9f815599 355 if (fflag)
6e8dad34
MD
356 fprintf(fo, "%10s%-30s ", "", "file and line");
357 if (pflag)
358 fprintf(fo, "%s", "trace");
359 fprintf(fo, "\n");
9f815599 360 }
6e8dad34 361}
9f815599 362
6e8dad34 363static void
5b107af3 364print_entry(FILE *fo, int n, int row, struct ktr_entry *entry,
c2d873bd 365 u_int64_t *last_timestamp)
6e8dad34
MD
366{
367 struct ktr_info *info = NULL;
e7c0dbba 368 static struct save_ctx nctx, pctx, fmtctx, symctx, infoctx;
6e8dad34 369
c2d873bd 370 fprintf(fo, " %06x ", row & 0x00FFFFFF);
6e8dad34
MD
371 if (cflag)
372 fprintf(fo, "%-3d ", n);
373 if (tflag || rflag) {
b8272239
MD
374 if (rflag && !nflag && tsc_frequency != 0.0) {
375 fprintf(fo, "%13.3f uS ",
c2d873bd 376 (double)(entry->ktr_timestamp - *last_timestamp) * 1000000.0 / tsc_frequency - correction_factor);
b8272239 377 } else if (rflag) {
5b107af3
SW
378 fprintf(fo, "%-16ju ",
379 (uintmax_t)(entry->ktr_timestamp - *last_timestamp));
b8272239 380 } else {
5b107af3
SW
381 fprintf(fo, "%-16ju ",
382 (uintmax_t)entry->ktr_timestamp);
b8272239 383 }
6e8dad34 384 }
fa9963c9
MD
385 if (xflag) {
386 if (nflag) {
387 fprintf(fo, "%p %p ",
388 entry->ktr_caller2, entry->ktr_caller1);
389 } else {
e7c0dbba
AE
390 fprintf(fo, "%-25s ",
391 address_to_symbol(entry->ktr_caller2, &symctx));
392 fprintf(fo, "%-25s ",
393 address_to_symbol(entry->ktr_caller1, &symctx));
fa9963c9
MD
394 }
395 }
6e8dad34 396 if (iflag) {
e7c0dbba 397 info = kvm_ktrinfo(entry->ktr_info, &infoctx);
6e8dad34 398 if (info)
e7c0dbba 399 fprintf(fo, "%-20s ", kvm_string(info->kf_name, &nctx));
6e8dad34
MD
400 else
401 fprintf(fo, "%-20s ", "<empty>");
402 }
403 if (fflag)
e7c0dbba
AE
404 fprintf(fo, "%34s:%-4d ",
405 trunc_path(kvm_string(entry->ktr_file, &pctx), 34),
406 entry->ktr_line);
6e8dad34
MD
407 if (pflag) {
408 if (info == NULL)
e7c0dbba 409 info = kvm_ktrinfo(entry->ktr_info, &infoctx);
90e76597 410 if (info)
e7c0dbba
AE
411 kvmfprintf(fo, kvm_string(info->kf_format, &fmtctx),
412 (void *)&entry->ktr_data);
6e8dad34
MD
413 }
414 fprintf(fo, "\n");
c2d873bd 415 *last_timestamp = entry->ktr_timestamp;
6e8dad34
MD
416}
417
e7c0dbba
AE
418static
419void
420print_callback(void *ctx, int n, int row, struct ktr_entry *entry, uint64_t *last_ts)
421{
422 FILE *fo = (FILE *)ctx;
423 print_header(fo, row);
424 print_entry(fo, n, row, entry, last_ts);
425}
426
427/*
428 * If free == 0, replace all (kvm) string pointers in fmtdata with pointers
429 * to user-allocated copies of the strings.
430 * If free != 0, free those pointers.
431 */
432static
433int
434mangle_string_ptrs(const char *fmt, uint8_t *fmtdata, int dofree)
435{
436 const char *f, *p;
437 size_t skipsize, intsz;
438 static struct save_ctx strctx;
439 int ret = 0;
440
441 for (f = fmt; f[0] != '\0'; ++f) {
442 if (f[0] != '%')
443 continue;
444 ++f;
445 skipsize = 0;
446 for (p = f; p[0]; ++p) {
447 int again = 0;
448 /*
449 * Eat flags. Notice this will accept duplicate
450 * flags.
451 */
452 switch (p[0]) {
453 case '#':
454 case '0':
455 case '-':
456 case ' ':
457 case '+':
458 case '\'':
459 again = !0;
460 break;
461 }
462 if (!again)
463 break;
464 }
465 /* Eat minimum field width, if any */
466 for (; isdigit(p[0]); ++p)
467 ;
468 if (p[0] == '.')
469 ++p;
470 /* Eat precision, if any */
471 for (; isdigit(p[0]); ++p)
472 ;
473 intsz = 0;
474 switch (p[0]) {
475 case 'l':
476 if (p[1] == 'l') {
477 ++p;
478 intsz = sizeof(long long);
479 } else {
480 intsz = sizeof(long);
481 }
482 break;
483 case 'j':
484 intsz = sizeof(intmax_t);
485 break;
486 case 't':
487 intsz = sizeof(ptrdiff_t);
488 break;
489 case 'z':
490 intsz = sizeof(size_t);
491 break;
492 default:
493 break;
494 }
495 if (intsz != 0)
496 ++p;
497 else
498 intsz = sizeof(int);
499
500 switch (p[0]) {
501 case 'd':
502 case 'i':
503 case 'o':
504 case 'u':
505 case 'x':
506 case 'X':
507 case 'c':
508 skipsize = intsz;
509 break;
510 case 'p':
511 skipsize = sizeof(void *);
512 break;
513 case 'f':
514 if (p[-1] == 'l')
515 skipsize = sizeof(double);
516 else
517 skipsize = sizeof(float);
518 break;
519 case 's':
520 if (dofree) {
521 char *t = ((char **)fmtdata)[0];
522 free(t);
523 skipsize = sizeof(char *);
524 } else {
525 char *t = strdup(kvm_string(((char **)fmtdata)[0],
526 &strctx));
527 ((const char **)fmtdata)[0] = t;
528
529 skipsize = sizeof(char *);
530 }
531 ++ret;
532 break;
533 default:
534 fprintf(stderr, "Unknown conversion specifier %c "
535 "in fmt starting with %s", p[0], f - 1);
536 return -1;
537 }
538 fmtdata += skipsize;
539 }
540 return ret;
541}
542
543static
544void
545dump_callback(void *ctx, int n, int row __unused, struct ktr_entry *entry,
546 uint64_t *last_ts __unused)
547{
548 evtr_t evtr = (evtr_t)ctx;
549 struct evtr_event ev;
550 static struct save_ctx pctx, fmtctx, infoctx;
551 struct ktr_info *ki;
552 int conv = 0; /* pointless */
553
554 ev.ts = entry->ktr_timestamp;
555 ev.type = EVTR_TYPE_PROBE;
556 ev.line = entry->ktr_line;
557 ev.file = kvm_string(entry->ktr_file, &pctx);
558 ev.func = NULL;
559 ev.cpu = n;
560 if ((ki = kvm_ktrinfo(entry->ktr_info, &infoctx))) {
561 ev.fmt = kvm_string(ki->kf_format, &fmtctx);
562 ev.fmtdata = entry->ktr_data;
563 if ((conv = mangle_string_ptrs(ev.fmt,
564 __DECONST(uint8_t *, ev.fmtdata),
565 0)) < 0)
566 errx(1, "Can't parse format string\n");
567 ev.fmtdatalen = ki->kf_data_size;
568 } else {
569 ev.fmt = ev.fmtdata = NULL;
570 ev.fmtdatalen = 0;
571 }
572 if (evtr_dump_event(evtr, &ev)) {
573 err(1, evtr_errmsg(evtr));
574 }
575 if (ev.fmtdata && conv) {
576 mangle_string_ptrs(ev.fmt, __DECONST(uint8_t *, ev.fmtdata),
577 !0);
578 }
579}
580
6e8dad34
MD
581static
582struct ktr_info *
e7c0dbba 583kvm_ktrinfo(void *kptr, struct save_ctx *ctx)
6e8dad34 584{
e7c0dbba 585 struct ktr_info *ki = (void *)ctx->save_buf;
6e8dad34
MD
586
587 if (kptr == NULL)
588 return(NULL);
e7c0dbba
AE
589 if (ctx->save_kptr != kptr) {
590 if (kvm_read(kd, (uintptr_t)kptr, ki, sizeof(*ki)) == -1) {
591 bzero(&ki, sizeof(*ki));
9f815599 592 } else {
e7c0dbba 593 ctx->save_kptr = kptr;
6e8dad34
MD
594 }
595 }
e7c0dbba 596 return(ki);
6e8dad34
MD
597}
598
599static
600const char *
e7c0dbba 601kvm_string(const char *kptr, struct save_ctx *ctx)
6e8dad34 602{
5b107af3
SW
603 u_int l;
604 u_int n;
6e8dad34
MD
605
606 if (kptr == NULL)
607 return("?");
e7c0dbba
AE
608 if (ctx->save_kptr != (const void *)kptr) {
609 ctx->save_kptr = (const void *)kptr;
6e8dad34 610 l = 0;
e7c0dbba
AE
611 while (l < sizeof(ctx->save_buf) - 1) {
612 n = 256 - ((intptr_t)(kptr + l) & 255);
613 if (n > sizeof(ctx->save_buf) - l - 1)
614 n = sizeof(ctx->save_buf) - l - 1;
615 if (kvm_read(kd, (uintptr_t)(kptr + l), ctx->save_buf + l, n) < 0)
9f815599 616 break;
e7c0dbba
AE
617 while (l < sizeof(ctx->save_buf) && n) {
618 if (ctx->save_buf[l] == 0)
6e8dad34
MD
619 break;
620 --n;
621 ++l;
622 }
623 if (n)
624 break;
9f815599 625 }
e7c0dbba 626 ctx->save_buf[l] = 0;
9f815599 627 }
e7c0dbba 628 return(ctx->save_buf);
6e8dad34 629}
9f815599 630
6e8dad34
MD
631static
632const char *
633trunc_path(const char *str, int maxlen)
634{
635 int len = strlen(str);
636
637 if (len > maxlen)
638 return(str + len - maxlen);
639 else
640 return(str);
9f815599
HP
641}
642
fa9963c9
MD
643struct symdata {
644 TAILQ_ENTRY(symdata) link;
645 const char *symname;
646 char *symaddr;
647 char symtype;
648};
649
650static TAILQ_HEAD(symlist, symdata) symlist;
651static struct symdata *symcache;
652static char *symbegin;
653static char *symend;
654
655static
656void
5b107af3 657read_symbols(const char *file)
fa9963c9
MD
658{
659 char buf[256];
660 char cmd[256];
5b107af3 661 size_t buflen = sizeof(buf);
fa9963c9
MD
662 FILE *fp;
663 struct symdata *sym;
664 char *s1;
665 char *s2;
666 char *s3;
667
668 TAILQ_INIT(&symlist);
669
5b107af3 670 if (file == NULL) {
fa9963c9 671 if (sysctlbyname("kern.bootfile", buf, &buflen, NULL, 0) < 0)
5b107af3 672 file = "/boot/kernel";
fa9963c9 673 else
5b107af3 674 file = buf;
fa9963c9 675 }
5b107af3 676 snprintf(cmd, sizeof(cmd), "nm -n %s", file);
fa9963c9
MD
677 if ((fp = popen(cmd, "r")) != NULL) {
678 while (fgets(buf, sizeof(buf), fp) != NULL) {
679 s1 = strtok(buf, " \t\n");
680 s2 = strtok(NULL, " \t\n");
681 s3 = strtok(NULL, " \t\n");
682 if (s1 && s2 && s3) {
683 sym = malloc(sizeof(struct symdata));
684 sym->symaddr = (char *)strtoul(s1, NULL, 16);
685 sym->symtype = s2[0];
686 sym->symname = strdup(s3);
687 if (strcmp(s3, "kernbase") == 0)
688 symbegin = sym->symaddr;
689 if (strcmp(s3, "end") == 0)
690 symend = sym->symaddr;
691 TAILQ_INSERT_TAIL(&symlist, sym, link);
692 }
693 }
694 pclose(fp);
695 }
696 symcache = TAILQ_FIRST(&symlist);
697}
698
699static
700const char *
e7c0dbba 701address_to_symbol(void *kptr, struct save_ctx *ctx)
fa9963c9 702{
e7c0dbba
AE
703 char *buf = ctx->save_buf;
704 int size = sizeof(ctx->save_buf);
fa9963c9
MD
705
706 if (symcache == NULL ||
707 (char *)kptr < symbegin || (char *)kptr >= symend
708 ) {
e7c0dbba 709 snprintf(buf, size, "%p", kptr);
fa9963c9
MD
710 return(buf);
711 }
712 while ((char *)symcache->symaddr < (char *)kptr) {
713 if (TAILQ_NEXT(symcache, link) == NULL)
714 break;
715 symcache = TAILQ_NEXT(symcache, link);
716 }
717 while ((char *)symcache->symaddr > (char *)kptr) {
718 if (symcache != TAILQ_FIRST(&symlist))
719 symcache = TAILQ_PREV(symcache, symlist, link);
720 }
e7c0dbba 721 snprintf(buf, size, "%s+%d", symcache->symname,
fa9963c9
MD
722 (int)((char *)kptr - symcache->symaddr));
723 return(buf);
724}
725
c2d873bd
MD
726static
727struct ktr_buffer *
5b107af3 728ktr_bufs_init(void)
c2d873bd
MD
729{
730 struct ktr_buffer *ktr_bufs, *it;
731 int i;
732
733 ktr_bufs = malloc(sizeof(*ktr_bufs) * ncpus);
734 if (!ktr_bufs)
735 err(1, "can't allocate data structures\n");
736 for (i = 0; i < ncpus; ++i) {
737 it = ktr_bufs + i;
738 it->ents = malloc(sizeof(struct ktr_entry) * entries_per_buf);
739 if (it->ents == NULL)
740 err(1, "can't allocate data structures\n");
741 it->reset = 1;
742 it->beg_idx = -1;
743 it->end_idx = -1;
744 }
745 return ktr_bufs;
746}
747
748static
749void
ddca1582 750get_indices(struct ktr_entry **ktr_kbuf, int *ktr_idx)
c2d873bd 751{
ddca1582
MD
752 static struct ktr_cpu *ktr_cpus;
753 int i;
754
755 if (ktr_cpus == NULL)
756 ktr_cpus = malloc(sizeof(*ktr_cpus) * ncpus);
757
758 if (ktr_version < KTR_VERSION_KTR_CPU) {
759 if (kvm_read(kd, nl_version_ktr_idx[0].n_value, ktr_idx,
760 sizeof(*ktr_idx) * ncpus) == -1) {
761 errx(1, "%s", kvm_geterr(kd));
762 }
763 if (ktr_kbuf[0] == NULL) {
764 if (kvm_read(kd, nl_version_ktr_idx[1].n_value,
765 ktr_kbuf, sizeof(*ktr_kbuf) * ncpus) == -1) {
766 errx(1, "%s", kvm_geterr(kd));
767 }
768 }
769 } else {
ddca1582
MD
770 if (kvm_read(kd, nl_version_ktr_cpu[0].n_value,
771 ktr_cpus, sizeof(*ktr_cpus) * ncpus) == -1) {
772 errx(1, "%s", kvm_geterr(kd));
773 }
774 for (i = 0; i < ncpus; ++i) {
775 ktr_idx[i] = ktr_cpus[i].core.ktr_idx;
776 ktr_kbuf[i] = ktr_cpus[i].core.ktr_buf;
777 }
778 }
c2d873bd
MD
779}
780
781/*
782 * Get the trace buffer data from the kernel
783 */
784static
785void
ddca1582 786load_bufs(struct ktr_buffer *ktr_bufs, struct ktr_entry **kbufs, int *ktr_idx)
c2d873bd 787{
c2d873bd
MD
788 struct ktr_buffer *kbuf;
789 int i;
790
ddca1582 791 get_indices(kbufs, ktr_idx);
c2d873bd
MD
792 for (i = 0; i < ncpus; ++i) {
793 kbuf = &ktr_bufs[i];
ddca1582 794 if (ktr_idx[i] == kbuf->end_idx)
c2d873bd 795 continue;
ddca1582 796 kbuf->end_idx = ktr_idx[i];
c2d873bd
MD
797
798 /*
799 * If we do not have a notion of the beginning index, assume
800 * it is entries_per_buf before the ending index. Don't
801 * worry about underflows/negative numbers, the indices will
802 * be masked.
803 */
804 if (kbuf->reset) {
805 kbuf->beg_idx = kbuf->end_idx - entries_per_buf + 1;
806 kbuf->reset = 0;
807 }
808 if (kvm_read(kd, (uintptr_t)kbufs[i], ktr_bufs[i].ents,
809 sizeof(struct ktr_entry) * entries_per_buf)
810 == -1)
811 errx(1, "%s", kvm_geterr(kd));
812 kbuf->modified = 1;
813 kbuf->beg_idx = earliest_ts(kbuf);
814 }
815
816}
817
818/*
819 * Locate the earliest timestamp iterating backwards from end_idx, but
820 * not going further back then beg_idx. We have to do this because
821 * the kernel uses a circulating buffer.
822 */
823static
824int
825earliest_ts(struct ktr_buffer *buf)
826{
827 struct ktr_entry *save;
828 int count, scan, i, earliest;
829
830 count = 0;
831 earliest = buf->end_idx - 1;
832 save = &buf->ents[earliest & fifo_mask];
833 for (scan = buf->end_idx - 1; scan != buf->beg_idx -1; --scan) {
834 i = scan & fifo_mask;
10b4dde1
SS
835 if (buf->ents[i].ktr_timestamp <= save->ktr_timestamp &&
836 buf->ents[i].ktr_timestamp > 0)
c2d873bd
MD
837 earliest = scan;
838 /*
839 * We may have gotten so far behind that beg_idx wrapped
840 * more then once around the buffer. Just stop
841 */
842 if (++count == entries_per_buf)
843 break;
844 }
845 return earliest;
846}
847
848static
849void
e7c0dbba
AE
850iterate_buf(FILE *fo, struct ktr_buffer *ktr_bufs, int cpu,
851 u_int64_t *last_timestamp, ktr_iter_cb_t cb)
c2d873bd 852{
c2d873bd
MD
853 struct ktr_buffer *buf = ktr_bufs + cpu;
854
855 if (buf->modified == 0)
856 return;
857 if (*last_timestamp == 0) {
858 *last_timestamp =
859 buf->ents[buf->beg_idx & fifo_mask].ktr_timestamp;
860 }
861 while (buf->beg_idx != buf->end_idx) {
e7c0dbba
AE
862 cb(fo, cpu, buf->beg_idx,
863 &buf->ents[buf->beg_idx & fifo_mask],
864 last_timestamp);
c2d873bd
MD
865 ++buf->beg_idx;
866 }
867 buf->modified = 0;
868}
869
870static
871void
e7c0dbba
AE
872iterate_bufs_timesorted(FILE *fo, struct ktr_buffer *ktr_bufs,
873 u_int64_t *last_timestamp, ktr_iter_cb_t cb)
c2d873bd
MD
874{
875 struct ktr_entry *ent;
876 struct ktr_buffer *buf;
877 int n, bestn;
878 u_int64_t ts;
879 static int row = 0;
880
881 for (;;) {
882 ts = 0;
883 bestn = -1;
884 for (n = 0; n < ncpus; ++n) {
885 buf = ktr_bufs + n;
886 if (buf->beg_idx == buf->end_idx)
887 continue;
888 ent = &buf->ents[buf->beg_idx & fifo_mask];
889 if (ts == 0 || (ts >= ent->ktr_timestamp)) {
890 ts = ent->ktr_timestamp;
891 bestn = n;
892 }
893 }
894 if ((bestn < 0) || (ts < *last_timestamp))
895 break;
896 buf = ktr_bufs + bestn;
e7c0dbba
AE
897 cb(fo, bestn, row,
898 &buf->ents[buf->beg_idx & fifo_mask],
899 last_timestamp);
c2d873bd
MD
900 ++buf->beg_idx;
901 *last_timestamp = ts;
902 ++row;
903 }
904}
905
08cca235
MD
906static
907void
908kvmfprintf(FILE *fp, const char *ctl, va_list va)
909{
910 int n;
911 int is_long;
912 int is_done;
913 char fmt[256];
e7c0dbba
AE
914 static struct save_ctx strctx;
915 const char *s;
08cca235
MD
916
917 while (*ctl) {
918 for (n = 0; ctl[n]; ++n) {
919 fmt[n] = ctl[n];
920 if (ctl[n] == '%')
921 break;
922 }
923 if (n == 0) {
924 is_long = 0;
925 is_done = 0;
926 n = 1;
927 while (n < (int)sizeof(fmt)) {
928 fmt[n] = ctl[n];
929 fmt[n+1] = 0;
930
931 switch(ctl[n]) {
932 case 'p':
933 is_long = 1;
934 /* fall through */
935 case 'd':
936 case 'u':
937 case 'x':
938 case 'o':
939 case 'X':
940 /*
941 * Integral
942 */
943 switch(is_long) {
944 case 0:
945 fprintf(fp, fmt,
946 va_arg(va, int));
947 break;
948 case 1:
949 fprintf(fp, fmt,
950 va_arg(va, long));
951 break;
952 case 2:
953 fprintf(fp, fmt,
954 va_arg(va, long long));
955 break;
956 case 3:
957 fprintf(fp, fmt,
958 va_arg(va, size_t));
959 break;
960 }
961 ++n;
962 is_done = 1;
963 break;
964 case 's':
965 /*
966 * String
967 */
e7c0dbba
AE
968 s = kvm_string(va_arg(va, char *), &strctx);
969 fwrite(s, 1, strlen(s), fp);
08cca235
MD
970 ++n;
971 is_done = 1;
972 break;
973 case 'f':
974 /*
975 * Floating
976 */
977 fprintf(fp, fmt,
978 va_arg(va, double));
979 ++n;
980 break;
981 case 'j':
982 is_long = 3;
983 break;
984 case 'l':
985 if (is_long)
986 is_long = 2;
987 else
988 is_long = 1;
989 break;
990 case '.':
991 case '-':
992 case '+':
993 case '0':
994 case '1':
995 case '2':
996 case '3':
997 case '4':
998 case '5':
999 case '6':
1000 case '7':
1001 case '8':
1002 case '9':
1003 break;
1004 default:
1005 is_done = 1;
1006 break;
1007 }
1008 if (is_done)
1009 break;
1010 ++n;
1011 }
1012 } else {
1013 fmt[n] = 0;
1014 fprintf(fp, fmt, NULL);
1015 }
1016 ctl += n;
1017 }
1018}
1019
9f815599
HP
1020static void
1021usage(void)
1022{
08cca235
MD
1023 fprintf(stderr, "usage: ktrdump [-acfilnpqrstx] [-A factor] "
1024 "[-N execfile] [-M corefile] [-o outfile]\n");
6e8dad34 1025 exit(1);
9f815599 1026}