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 nl1
[] = {
61 { .n_name
= "_ktr_version" },
62 { .n_name
= "_ktr_entries" },
63 { .n_name
= "_ncpus" },
67 static struct nlist nl2
[] = {
68 { .n_name
= "_tsc_frequency" },
72 static struct nlist nl_version_ktr_idx
[] = {
73 { .n_name
= "_ktr_idx" },
74 { .n_name
= "_ktr_buf" },
78 static struct nlist nl_version_ktr_cpu
[] = {
79 { .n_name
= "_ktr_cpu" },
96 static double tsc_frequency
;
97 static double correction_factor
= 0.0;
99 static char corefile
[PATH_MAX
];
100 static char execfile
[PATH_MAX
];
102 static char errbuf
[_POSIX2_LINE_MAX
];
105 static int entries_per_buf
;
106 static int fifo_mask
;
107 static int ktr_version
;
109 static void usage(void);
110 static int earliest_ts(struct ktr_buffer
*);
111 static void print_header(FILE *, int);
112 static void print_entry(FILE *, int, int, struct ktr_entry
*, u_int64_t
*);
113 static struct ktr_info
*kvm_ktrinfo(void *);
114 static const char *kvm_string(const char *);
115 static const char *trunc_path(const char *, int);
116 static void read_symbols(const char *);
117 static const char *address_to_symbol(void *);
118 static struct ktr_buffer
*ktr_bufs_init(void);
119 static void get_indices(struct ktr_entry
**, int *);
120 static void load_bufs(struct ktr_buffer
*, struct ktr_entry
**, int *);
121 static void print_buf(FILE *, struct ktr_buffer
*, int, u_int64_t
*);
122 static void print_bufs_timesorted(FILE *, struct ktr_buffer
*, u_int64_t
*);
126 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
129 main(int ac
, char **av
)
131 struct ktr_buffer
*ktr_bufs
;
132 struct ktr_entry
**ktr_kbuf
;
136 int *ktr_start_index
;
141 * Parse commandline arguments.
144 while ((c
= getopt(ac
, av
, "acfinqrtxpslA:N:M:o:")) != -1) {
159 if (strlcpy(execfile
, optarg
, sizeof(execfile
))
161 errx(1, "%s: File name too long", optarg
);
174 correction_factor
= strtod(optarg
, NULL
);
177 if (strlcpy(corefile
, optarg
, sizeof(corefile
))
179 errx(1, "%s: File name too long", optarg
);
186 if ((fo
= fopen(optarg
, "w")) == NULL
)
187 err(1, "%s", optarg
);
199 sflag
= 1; /* sort across the cpus */
212 if (cflag
+ iflag
+ tflag
+ xflag
+ fflag
+ pflag
== 0) {
218 if (correction_factor
!= 0.0 && (rflag
== 0 || nflag
)) {
219 fprintf(stderr
, "Correction factor can only be applied with -r and without -n\n");
228 * Open our execfile and corefile, resolve needed symbols and read in
231 if ((kd
= kvm_openfiles(Nflag
? execfile
: NULL
,
232 Mflag
? corefile
: NULL
, NULL
, O_RDONLY
, errbuf
)) == NULL
)
233 errx(1, "%s", errbuf
);
234 if (kvm_nlist(kd
, nl1
) != 0)
235 errx(1, "%s", kvm_geterr(kd
));
236 if (kvm_read(kd
, nl1
[0].n_value
, &ktr_version
, sizeof(ktr_version
)) == -1)
237 errx(1, "%s", kvm_geterr(kd
));
238 if (kvm_read(kd
, nl1
[2].n_value
, &ncpus
, sizeof(ncpus
)) == -1)
239 errx(1, "%s", kvm_geterr(kd
));
240 ktr_start_index
= malloc(sizeof(*ktr_start_index
) * ncpus
);
241 if (ktr_version
>= KTR_VERSION_WITH_FREQ
&& kvm_nlist(kd
, nl2
) == 0) {
242 if (kvm_read(kd
, nl2
[0].n_value
, &tts
, sizeof(tts
)) == -1)
243 errx(1, "%s", kvm_geterr(kd
));
244 tsc_frequency
= (double)tts
;
246 if (ktr_version
> KTR_VERSION
)
247 errx(1, "ktr version too high for us to handle");
248 if (kvm_read(kd
, nl1
[1].n_value
, &entries_per_buf
,
249 sizeof(entries_per_buf
)) == -1)
250 errx(1, "%s", kvm_geterr(kd
));
251 fifo_mask
= entries_per_buf
- 1;
253 printf("TSC frequency is %6.3f MHz\n", tsc_frequency
/ 1000000.0);
255 ktr_kbuf
= calloc(ncpus
, sizeof(*ktr_kbuf
));
256 ktr_idx
= calloc(ncpus
, sizeof(*ktr_idx
));
259 read_symbols(Nflag
? execfile
: NULL
);
261 if (ktr_version
< KTR_VERSION_KTR_CPU
) {
262 if (kvm_nlist(kd
, nl_version_ktr_idx
))
263 errx(1, "%s", kvm_geterr(kd
));
265 if (kvm_nlist(kd
, nl_version_ktr_cpu
))
266 errx(1, "%s", kvm_geterr(kd
));
269 get_indices(ktr_kbuf
, ktr_idx
);
271 ktr_bufs
= ktr_bufs_init();
274 u_int64_t last_timestamp
= 0;
276 load_bufs(ktr_bufs
, ktr_kbuf
, ktr_idx
);
277 print_bufs_timesorted(fo
, ktr_bufs
, &last_timestamp
);
279 usleep(1000000 / 10);
282 u_int64_t
*last_timestamp
= calloc(sizeof(u_int64_t
), ncpus
);
284 load_bufs(ktr_bufs
, ktr_kbuf
, ktr_idx
);
285 for (n
= 0; n
< ncpus
; ++n
)
286 print_buf(fo
, ktr_bufs
, n
, &last_timestamp
[n
]);
288 usleep(1000000 / 10);
295 print_header(FILE *fo
, int row
)
297 if (qflag
== 0 && (u_int32_t
)row
% 20 == 0) {
298 fprintf(fo
, "%-6s ", "index");
300 fprintf(fo
, "%-3s ", "cpu");
302 fprintf(fo
, "%-16s ", "timestamp");
305 fprintf(fo
, "%-10s %-10s", "caller2", "caller1");
307 fprintf(fo
, "%-20s %-20s", "caller2", "caller1");
310 fprintf(fo
, "%-20s ", "ID");
312 fprintf(fo
, "%10s%-30s ", "", "file and line");
314 fprintf(fo
, "%s", "trace");
320 print_entry(FILE *fo
, int n
, int row
, struct ktr_entry
*entry
,
321 u_int64_t
*last_timestamp
)
323 struct ktr_info
*info
= NULL
;
325 fprintf(fo
, " %06x ", row
& 0x00FFFFFF);
327 fprintf(fo
, "%-3d ", n
);
328 if (tflag
|| rflag
) {
329 if (rflag
&& !nflag
&& tsc_frequency
!= 0.0) {
330 fprintf(fo
, "%13.3f uS ",
331 (double)(entry
->ktr_timestamp
- *last_timestamp
) * 1000000.0 / tsc_frequency
- correction_factor
);
333 fprintf(fo
, "%-16ju ",
334 (uintmax_t)(entry
->ktr_timestamp
- *last_timestamp
));
336 fprintf(fo
, "%-16ju ",
337 (uintmax_t)entry
->ktr_timestamp
);
342 fprintf(fo
, "%p %p ",
343 entry
->ktr_caller2
, entry
->ktr_caller1
);
345 fprintf(fo
, "%-20s ",
346 address_to_symbol(entry
->ktr_caller2
));
347 fprintf(fo
, "%-20s ",
348 address_to_symbol(entry
->ktr_caller1
));
352 info
= kvm_ktrinfo(entry
->ktr_info
);
354 fprintf(fo
, "%-20s ", kvm_string(info
->kf_name
));
356 fprintf(fo
, "%-20s ", "<empty>");
359 fprintf(fo
, "%34s:%-4d ", trunc_path(kvm_string(entry
->ktr_file
), 34), entry
->ktr_line
);
362 info
= kvm_ktrinfo(entry
->ktr_info
);
364 vfprintf(fo
, kvm_string(info
->kf_format
), (void *)&entry
->ktr_data
);
367 *last_timestamp
= entry
->ktr_timestamp
;
372 kvm_ktrinfo(void *kptr
)
374 static struct ktr_info save_info
;
375 static void *save_kptr
;
379 if (save_kptr
!= kptr
) {
380 if (kvm_read(kd
, (uintptr_t)kptr
, &save_info
, sizeof(save_info
)) == -1) {
381 bzero(&save_info
, sizeof(save_info
));
391 kvm_string(const char *kptr
)
393 static char save_str
[128];
394 static const char *save_kptr
;
400 if (save_kptr
!= kptr
) {
403 while (l
< sizeof(save_str
) - 1) {
404 n
= 256 - ((intptr_t)(kptr
+ l
) & 255);
405 if (n
> sizeof(save_str
) - l
- 1)
406 n
= sizeof(save_str
) - l
- 1;
407 if (kvm_read(kd
, (uintptr_t)(kptr
+ l
), save_str
+ l
, n
) < 0)
409 while (l
< sizeof(save_str
) && n
) {
410 if (save_str
[l
] == 0)
425 trunc_path(const char *str
, int maxlen
)
427 int len
= strlen(str
);
430 return(str
+ len
- maxlen
);
436 TAILQ_ENTRY(symdata
) link
;
442 static TAILQ_HEAD(symlist
, symdata
) symlist
;
443 static struct symdata
*symcache
;
444 static char *symbegin
;
449 read_symbols(const char *file
)
453 size_t buflen
= sizeof(buf
);
460 TAILQ_INIT(&symlist
);
463 if (sysctlbyname("kern.bootfile", buf
, &buflen
, NULL
, 0) < 0)
464 file
= "/boot/kernel";
468 snprintf(cmd
, sizeof(cmd
), "nm -n %s", file
);
469 if ((fp
= popen(cmd
, "r")) != NULL
) {
470 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
471 s1
= strtok(buf
, " \t\n");
472 s2
= strtok(NULL
, " \t\n");
473 s3
= strtok(NULL
, " \t\n");
474 if (s1
&& s2
&& s3
) {
475 sym
= malloc(sizeof(struct symdata
));
476 sym
->symaddr
= (char *)strtoul(s1
, NULL
, 16);
477 sym
->symtype
= s2
[0];
478 sym
->symname
= strdup(s3
);
479 if (strcmp(s3
, "kernbase") == 0)
480 symbegin
= sym
->symaddr
;
481 if (strcmp(s3
, "end") == 0)
482 symend
= sym
->symaddr
;
483 TAILQ_INSERT_TAIL(&symlist
, sym
, link
);
488 symcache
= TAILQ_FIRST(&symlist
);
493 address_to_symbol(void *kptr
)
497 if (symcache
== NULL
||
498 (char *)kptr
< symbegin
|| (char *)kptr
>= symend
500 snprintf(buf
, sizeof(buf
), "%p", kptr
);
503 while ((char *)symcache
->symaddr
< (char *)kptr
) {
504 if (TAILQ_NEXT(symcache
, link
) == NULL
)
506 symcache
= TAILQ_NEXT(symcache
, link
);
508 while ((char *)symcache
->symaddr
> (char *)kptr
) {
509 if (symcache
!= TAILQ_FIRST(&symlist
))
510 symcache
= TAILQ_PREV(symcache
, symlist
, link
);
512 snprintf(buf
, sizeof(buf
), "%s+%d", symcache
->symname
,
513 (int)((char *)kptr
- symcache
->symaddr
));
521 struct ktr_buffer
*ktr_bufs
, *it
;
524 ktr_bufs
= malloc(sizeof(*ktr_bufs
) * ncpus
);
526 err(1, "can't allocate data structures\n");
527 for (i
= 0; i
< ncpus
; ++i
) {
529 it
->ents
= malloc(sizeof(struct ktr_entry
) * entries_per_buf
);
530 if (it
->ents
== NULL
)
531 err(1, "can't allocate data structures\n");
541 get_indices(struct ktr_entry
**ktr_kbuf
, int *ktr_idx
)
543 static struct ktr_cpu
*ktr_cpus
;
546 if (ktr_cpus
== NULL
)
547 ktr_cpus
= malloc(sizeof(*ktr_cpus
) * ncpus
);
549 if (ktr_version
< KTR_VERSION_KTR_CPU
) {
550 if (kvm_read(kd
, nl_version_ktr_idx
[0].n_value
, ktr_idx
,
551 sizeof(*ktr_idx
) * ncpus
) == -1) {
552 errx(1, "%s", kvm_geterr(kd
));
554 if (ktr_kbuf
[0] == NULL
) {
555 if (kvm_read(kd
, nl_version_ktr_idx
[1].n_value
,
556 ktr_kbuf
, sizeof(*ktr_kbuf
) * ncpus
) == -1) {
557 errx(1, "%s", kvm_geterr(kd
));
561 if (kvm_read(kd
, nl_version_ktr_cpu
[0].n_value
,
562 ktr_cpus
, sizeof(*ktr_cpus
) * ncpus
) == -1) {
563 errx(1, "%s", kvm_geterr(kd
));
565 for (i
= 0; i
< ncpus
; ++i
) {
566 ktr_idx
[i
] = ktr_cpus
[i
].core
.ktr_idx
;
567 ktr_kbuf
[i
] = ktr_cpus
[i
].core
.ktr_buf
;
573 * Get the trace buffer data from the kernel
577 load_bufs(struct ktr_buffer
*ktr_bufs
, struct ktr_entry
**kbufs
, int *ktr_idx
)
579 struct ktr_buffer
*kbuf
;
582 get_indices(kbufs
, ktr_idx
);
583 for (i
= 0; i
< ncpus
; ++i
) {
585 if (ktr_idx
[i
] == kbuf
->end_idx
)
587 kbuf
->end_idx
= ktr_idx
[i
];
590 * If we do not have a notion of the beginning index, assume
591 * it is entries_per_buf before the ending index. Don't
592 * worry about underflows/negative numbers, the indices will
596 kbuf
->beg_idx
= kbuf
->end_idx
- entries_per_buf
+ 1;
599 if (kvm_read(kd
, (uintptr_t)kbufs
[i
], ktr_bufs
[i
].ents
,
600 sizeof(struct ktr_entry
) * entries_per_buf
)
602 errx(1, "%s", kvm_geterr(kd
));
604 kbuf
->beg_idx
= earliest_ts(kbuf
);
610 * Locate the earliest timestamp iterating backwards from end_idx, but
611 * not going further back then beg_idx. We have to do this because
612 * the kernel uses a circulating buffer.
616 earliest_ts(struct ktr_buffer
*buf
)
618 struct ktr_entry
*save
;
619 int count
, scan
, i
, earliest
;
622 earliest
= buf
->end_idx
- 1;
623 save
= &buf
->ents
[earliest
& fifo_mask
];
624 for (scan
= buf
->end_idx
- 1; scan
!= buf
->beg_idx
-1; --scan
) {
625 i
= scan
& fifo_mask
;
626 if (buf
->ents
[i
].ktr_timestamp
<= save
->ktr_timestamp
&&
627 buf
->ents
[i
].ktr_timestamp
> 0)
630 * We may have gotten so far behind that beg_idx wrapped
631 * more then once around the buffer. Just stop
633 if (++count
== entries_per_buf
)
641 print_buf(FILE *fo
, struct ktr_buffer
*ktr_bufs
, int cpu
,
642 u_int64_t
*last_timestamp
)
644 struct ktr_buffer
*buf
= ktr_bufs
+ cpu
;
646 if (buf
->modified
== 0)
648 if (*last_timestamp
== 0) {
650 buf
->ents
[buf
->beg_idx
& fifo_mask
].ktr_timestamp
;
652 while (buf
->beg_idx
!= buf
->end_idx
) {
653 print_header(fo
, buf
->beg_idx
);
654 print_entry(fo
, cpu
, buf
->beg_idx
,
655 &buf
->ents
[buf
->beg_idx
& fifo_mask
],
664 print_bufs_timesorted(FILE *fo
, struct ktr_buffer
*ktr_bufs
,
665 u_int64_t
*last_timestamp
)
667 struct ktr_entry
*ent
;
668 struct ktr_buffer
*buf
;
676 for (n
= 0; n
< ncpus
; ++n
) {
678 if (buf
->beg_idx
== buf
->end_idx
)
680 ent
= &buf
->ents
[buf
->beg_idx
& fifo_mask
];
681 if (ts
== 0 || (ts
>= ent
->ktr_timestamp
)) {
682 ts
= ent
->ktr_timestamp
;
686 if ((bestn
< 0) || (ts
< *last_timestamp
))
688 buf
= ktr_bufs
+ bestn
;
689 print_header(fo
, row
);
690 print_entry(fo
, bestn
, row
,
691 &buf
->ents
[buf
->beg_idx
& fifo_mask
],
694 *last_timestamp
= ts
;
702 fprintf(stderr
, "usage: ktrdump [-acfilnpqrstx] [-A factor] [-N execfile] "
703 "[-M corefile] [-o outfile]\n");