2 * Copyright (c) 2002 Jake Burkholder
3 * Copyright (c) 2004 Robert Watson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/usr.bin/ktrdump/ktrdump.c,v 1.10 2005/05/21 09:55:06 ru Exp $
28 * $DragonFly: src/usr.bin/ktrdump/ktrdump.c,v 1.13 2008/11/10 02:05:31 swildner Exp $
31 #include <sys/cdefs.h>
33 #include <sys/types.h>
37 #include <sys/queue.h>
53 struct ktr_entry *ents;
56 int beg_idx; /* Beginning index */
57 int end_idx; /* Ending index */
60 static struct nlist nl[] = {
61 { .n_name = "_ktr_version" },
62 { .n_name = "_ktr_entries" },
63 { .n_name = "_ktr_idx" },
64 { .n_name = "_ktr_buf" },
65 { .n_name = "_ncpus" },
68 static struct nlist nl2[] = {
69 { .n_name = "_tsc_frequency" },
86 static double tsc_frequency;
87 static double correction_factor = 0.0;
89 static char corefile[PATH_MAX];
90 static char execfile[PATH_MAX];
92 static char errbuf[_POSIX2_LINE_MAX];
95 static int entries_per_buf;
98 static void usage(void);
99 static int earliest_ts(struct ktr_buffer *);
100 static void print_header(FILE *, int);
101 static void print_entry(FILE *, int, int, struct ktr_entry *, u_int64_t *);
102 static struct ktr_info *kvm_ktrinfo(void *);
103 static const char *kvm_string(const char *);
104 static const char *trunc_path(const char *, int);
105 static void read_symbols(const char *);
106 static const char *address_to_symbol(void *);
107 static struct ktr_buffer *ktr_bufs_init(void);
108 static void get_indices(int *);
109 static void load_bufs(struct ktr_buffer *, struct ktr_entry **);
110 static void print_buf(FILE *, struct ktr_buffer *, int, u_int64_t *);
111 static void print_bufs_timesorted(FILE *, struct ktr_buffer *, u_int64_t *);
115 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
118 main(int ac, char **av)
120 struct ktr_buffer *ktr_bufs;
121 struct ktr_entry **ktr_kbuf;
124 int *ktr_start_index;
130 * Parse commandline arguments.
133 while ((c = getopt(ac, av, "acfinqrtxpslA:N:M:o:")) != -1) {
148 if (strlcpy(execfile, optarg, sizeof(execfile))
150 errx(1, "%s: File name too long", optarg);
163 correction_factor = strtod(optarg, NULL);
166 if (strlcpy(corefile, optarg, sizeof(corefile))
168 errx(1, "%s: File name too long", optarg);
175 if ((fo = fopen(optarg, "w")) == NULL)
176 err(1, "%s", optarg);
188 sflag = 1; /* sort across the cpus */
201 if (cflag + iflag + tflag + xflag + fflag + pflag == 0) {
207 if (correction_factor != 0.0 && (rflag == 0 || nflag)) {
208 fprintf(stderr, "Correction factor can only be applied with -r and without -n\n");
217 * Open our execfile and corefile, resolve needed symbols and read in
220 if ((kd = kvm_openfiles(Nflag ? execfile : NULL,
221 Mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
222 errx(1, "%s", errbuf);
223 if (kvm_nlist(kd, nl) != 0)
224 errx(1, "%s", kvm_geterr(kd));
225 if (kvm_read(kd, nl[0].n_value, &version, sizeof(version)) == -1)
226 errx(1, "%s", kvm_geterr(kd));
227 if (kvm_read(kd, nl[4].n_value, &ncpus, sizeof(ncpus)) == -1)
228 errx(1, "%s", kvm_geterr(kd));
229 ktr_start_index = malloc(sizeof(*ktr_start_index) * ncpus);
230 if (version >= 3 && kvm_nlist(kd, nl2) == 0) {
231 if (kvm_read(kd, nl2[0].n_value, &tts, sizeof(tts)) == -1)
232 errx(1, "%s", kvm_geterr(kd));
233 tsc_frequency = (double)tts;
235 if (version > KTR_VERSION)
236 errx(1, "ktr version too high for us to handle");
237 if (kvm_read(kd, nl[1].n_value, &entries_per_buf,
238 sizeof(entries_per_buf)) == -1)
239 errx(1, "%s", kvm_geterr(kd));
240 fifo_mask = entries_per_buf - 1;
242 printf("TSC frequency is %6.3f MHz\n", tsc_frequency / 1000000.0);
244 ktr_kbuf = malloc(sizeof(*ktr_kbuf) * ncpus);
247 read_symbols(Nflag ? execfile : NULL);
249 if (kvm_read(kd, nl[3].n_value, ktr_kbuf, sizeof(*ktr_kbuf) * ncpus) == -1)
250 errx(1, "%s", kvm_geterr(kd));
252 ktr_bufs = ktr_bufs_init();
255 u_int64_t last_timestamp = 0;
257 load_bufs(ktr_bufs, ktr_kbuf);
258 print_bufs_timesorted(fo, ktr_bufs, &last_timestamp);
260 usleep(1000000 / 10);
263 u_int64_t *last_timestamp = calloc(sizeof(u_int64_t), ncpus);
265 load_bufs(ktr_bufs, ktr_kbuf);
266 for (n = 0; n < ncpus; ++n)
267 print_buf(fo, ktr_bufs, n, &last_timestamp[n]);
269 usleep(1000000 / 10);
276 print_header(FILE *fo, int row)
278 if (qflag == 0 && (u_int32_t)row % 20 == 0) {
279 fprintf(fo, "%-6s ", "index");
281 fprintf(fo, "%-3s ", "cpu");
283 fprintf(fo, "%-16s ", "timestamp");
286 fprintf(fo, "%-10s %-10s", "caller2", "caller1");
288 fprintf(fo, "%-20s %-20s", "caller2", "caller1");
291 fprintf(fo, "%-20s ", "ID");
293 fprintf(fo, "%10s%-30s ", "", "file and line");
295 fprintf(fo, "%s", "trace");
301 print_entry(FILE *fo, int n, int row, struct ktr_entry *entry,
302 u_int64_t *last_timestamp)
304 struct ktr_info *info = NULL;
306 fprintf(fo, " %06x ", row & 0x00FFFFFF);
308 fprintf(fo, "%-3d ", n);
309 if (tflag || rflag) {
310 if (rflag && !nflag && tsc_frequency != 0.0) {
311 fprintf(fo, "%13.3f uS ",
312 (double)(entry->ktr_timestamp - *last_timestamp) * 1000000.0 / tsc_frequency - correction_factor);
314 fprintf(fo, "%-16ju ",
315 (uintmax_t)(entry->ktr_timestamp - *last_timestamp));
317 fprintf(fo, "%-16ju ",
318 (uintmax_t)entry->ktr_timestamp);
323 fprintf(fo, "%p %p ",
324 entry->ktr_caller2, entry->ktr_caller1);
326 fprintf(fo, "%-20s ",
327 address_to_symbol(entry->ktr_caller2));
328 fprintf(fo, "%-20s ",
329 address_to_symbol(entry->ktr_caller1));
333 info = kvm_ktrinfo(entry->ktr_info);
335 fprintf(fo, "%-20s ", kvm_string(info->kf_name));
337 fprintf(fo, "%-20s ", "<empty>");
340 fprintf(fo, "%34s:%-4d ", trunc_path(kvm_string(entry->ktr_file), 34), entry->ktr_line);
343 info = kvm_ktrinfo(entry->ktr_info);
345 fprintf(fo, kvm_string(info->kf_format),
346 entry->ktr_data[0], entry->ktr_data[1],
347 entry->ktr_data[2], entry->ktr_data[3],
348 entry->ktr_data[4], entry->ktr_data[5],
349 entry->ktr_data[6], entry->ktr_data[7],
350 entry->ktr_data[8], entry->ktr_data[9]);
354 *last_timestamp = entry->ktr_timestamp;
359 kvm_ktrinfo(void *kptr)
361 static struct ktr_info save_info;
362 static void *save_kptr;
366 if (save_kptr != kptr) {
367 if (kvm_read(kd, (uintptr_t)kptr, &save_info, sizeof(save_info)) == -1) {
368 bzero(&save_info, sizeof(save_info));
378 kvm_string(const char *kptr)
380 static char save_str[128];
381 static const char *save_kptr;
387 if (save_kptr != kptr) {
390 while (l < sizeof(save_str) - 1) {
391 n = 256 - ((intptr_t)(kptr + l) & 255);
392 if (n > sizeof(save_str) - l - 1)
393 n = sizeof(save_str) - l - 1;
394 if (kvm_read(kd, (uintptr_t)(kptr + l), save_str + l, n) < 0)
396 while (l < sizeof(save_str) && n) {
397 if (save_str[l] == 0)
412 trunc_path(const char *str, int maxlen)
414 int len = strlen(str);
417 return(str + len - maxlen);
423 TAILQ_ENTRY(symdata) link;
429 static TAILQ_HEAD(symlist, symdata) symlist;
430 static struct symdata *symcache;
431 static char *symbegin;
436 read_symbols(const char *file)
440 size_t buflen = sizeof(buf);
447 TAILQ_INIT(&symlist);
450 if (sysctlbyname("kern.bootfile", buf, &buflen, NULL, 0) < 0)
451 file = "/boot/kernel";
455 snprintf(cmd, sizeof(cmd), "nm -n %s", file);
456 if ((fp = popen(cmd, "r")) != NULL) {
457 while (fgets(buf, sizeof(buf), fp) != NULL) {
458 s1 = strtok(buf, " \t\n");
459 s2 = strtok(NULL, " \t\n");
460 s3 = strtok(NULL, " \t\n");
461 if (s1 && s2 && s3) {
462 sym = malloc(sizeof(struct symdata));
463 sym->symaddr = (char *)strtoul(s1, NULL, 16);
464 sym->symtype = s2[0];
465 sym->symname = strdup(s3);
466 if (strcmp(s3, "kernbase") == 0)
467 symbegin = sym->symaddr;
468 if (strcmp(s3, "end") == 0)
469 symend = sym->symaddr;
470 TAILQ_INSERT_TAIL(&symlist, sym, link);
475 symcache = TAILQ_FIRST(&symlist);
480 address_to_symbol(void *kptr)
484 if (symcache == NULL ||
485 (char *)kptr < symbegin || (char *)kptr >= symend
487 snprintf(buf, sizeof(buf), "%p", kptr);
490 while ((char *)symcache->symaddr < (char *)kptr) {
491 if (TAILQ_NEXT(symcache, link) == NULL)
493 symcache = TAILQ_NEXT(symcache, link);
495 while ((char *)symcache->symaddr > (char *)kptr) {
496 if (symcache != TAILQ_FIRST(&symlist))
497 symcache = TAILQ_PREV(symcache, symlist, link);
499 snprintf(buf, sizeof(buf), "%s+%d", symcache->symname,
500 (int)((char *)kptr - symcache->symaddr));
508 struct ktr_buffer *ktr_bufs, *it;
511 ktr_bufs = malloc(sizeof(*ktr_bufs) * ncpus);
513 err(1, "can't allocate data structures\n");
514 for (i = 0; i < ncpus; ++i) {
516 it->ents = malloc(sizeof(struct ktr_entry) * entries_per_buf);
517 if (it->ents == NULL)
518 err(1, "can't allocate data structures\n");
528 get_indices(int *idx)
530 if (kvm_read(kd, nl[2].n_value, idx, sizeof(*idx) * ncpus) == -1)
531 errx(1, "%s", kvm_geterr(kd));
535 * Get the trace buffer data from the kernel
539 load_bufs(struct ktr_buffer *ktr_bufs, struct ktr_entry **kbufs)
541 static int *kern_idx;
542 struct ktr_buffer *kbuf;
546 kern_idx = malloc(sizeof(*kern_idx) * ncpus);
548 err(1, "can't allocate data structures\n");
552 get_indices(kern_idx);
553 for (i = 0; i < ncpus; ++i) {
555 if (kern_idx[i] == kbuf->end_idx)
557 kbuf->end_idx = kern_idx[i];
560 * If we do not have a notion of the beginning index, assume
561 * it is entries_per_buf before the ending index. Don't
562 * worry about underflows/negative numbers, the indices will
566 kbuf->beg_idx = kbuf->end_idx - entries_per_buf + 1;
569 if (kvm_read(kd, (uintptr_t)kbufs[i], ktr_bufs[i].ents,
570 sizeof(struct ktr_entry) * entries_per_buf)
572 errx(1, "%s", kvm_geterr(kd));
574 kbuf->beg_idx = earliest_ts(kbuf);
580 * Locate the earliest timestamp iterating backwards from end_idx, but
581 * not going further back then beg_idx. We have to do this because
582 * the kernel uses a circulating buffer.
586 earliest_ts(struct ktr_buffer *buf)
588 struct ktr_entry *save;
589 int count, scan, i, earliest;
592 earliest = buf->end_idx - 1;
593 save = &buf->ents[earliest & fifo_mask];
594 for (scan = buf->end_idx - 1; scan != buf->beg_idx -1; --scan) {
595 i = scan & fifo_mask;
596 if (buf->ents[i].ktr_timestamp <= save->ktr_timestamp)
599 * We may have gotten so far behind that beg_idx wrapped
600 * more then once around the buffer. Just stop
602 if (++count == entries_per_buf)
610 print_buf(FILE *fo, struct ktr_buffer *ktr_bufs, int cpu,
611 u_int64_t *last_timestamp)
613 struct ktr_buffer *buf = ktr_bufs + cpu;
615 if (buf->modified == 0)
617 if (*last_timestamp == 0) {
619 buf->ents[buf->beg_idx & fifo_mask].ktr_timestamp;
621 while (buf->beg_idx != buf->end_idx) {
622 print_header(fo, buf->beg_idx);
623 print_entry(fo, cpu, buf->beg_idx,
624 &buf->ents[buf->beg_idx & fifo_mask],
633 print_bufs_timesorted(FILE *fo, struct ktr_buffer *ktr_bufs,
634 u_int64_t *last_timestamp)
636 struct ktr_entry *ent;
637 struct ktr_buffer *buf;
645 for (n = 0; n < ncpus; ++n) {
647 if (buf->beg_idx == buf->end_idx)
649 ent = &buf->ents[buf->beg_idx & fifo_mask];
650 if (ts == 0 || (ts >= ent->ktr_timestamp)) {
651 ts = ent->ktr_timestamp;
655 if ((bestn < 0) || (ts < *last_timestamp))
657 buf = ktr_bufs + bestn;
658 print_header(fo, row);
659 print_entry(fo, bestn, row,
660 &buf->ents[buf->beg_idx & fifo_mask],
663 *last_timestamp = ts;
671 fprintf(stderr, "usage: ktrdump [-acfilnpqrstx] [-A factor] [-N execfile] "
672 "[-M corefile] [-o outfile]\n");