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.7 2007/06/08 18:24:22 dillon Exp $
31 #include <sys/cdefs.h>
33 #include <sys/types.h>
37 #include <sys/queue.h>
56 struct ktr_entry
*ents
;
59 int beg_idx
; /* Beginning index */
60 int end_idx
; /* Ending index */
63 static struct nlist nl
[] = {
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
*buf
);
100 static void print_header(FILE *fo
, int row
);
101 static void print_entry(FILE *fo
, kvm_t
*kd
, int n
, int i
, struct ktr_entry
*entry
, u_int64_t
*last_timestamp
);
102 static struct ktr_info
*kvm_ktrinfo(kvm_t
*kd
, void *kptr
);
103 static const char *kvm_string(kvm_t
*kd
, const char *kptr
);
104 static const char *trunc_path(const char *str
, int maxlen
);
105 static void read_symbols(const char *execfile
);
106 static const char *address_to_symbol(void *kptr
);
107 static struct ktr_buffer
*ktr_bufs_init(int);
108 static void load_bufs(struct ktr_buffer
*, struct ktr_entry
**);
109 static void print_buf(FILE *, struct ktr_buffer
*, int, u_int64_t
*);
110 static void print_bufs_timesorted(FILE *, struct ktr_buffer
*, u_int64_t
*);
114 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
117 main(int ac
, char **av
)
119 struct ktr_buffer
*ktr_bufs
;
120 struct ktr_entry
**ktr_kbuf
;
123 int *ktr_start_index
;
129 * Parse commandline arguments.
132 while ((c
= getopt(ac
, av
, "acfinqrtxpslA:N:M:o:")) != -1) {
146 if (strlcpy(execfile
, optarg
, sizeof(execfile
))
148 errx(1, "%s: File name too long", optarg
);
161 correction_factor
= strtod(optarg
, NULL
);
164 if (strlcpy(corefile
, optarg
, sizeof(corefile
))
166 errx(1, "%s: File name too long", optarg
);
173 if ((fo
= fopen(optarg
, "w")) == NULL
)
174 err(1, "%s", optarg
);
186 sflag
= 1; /* sort across the cpus */
199 if (cflag
+ iflag
+ tflag
+ xflag
+ fflag
+ pflag
== 0) {
205 if (correction_factor
!= 0.0 && (rflag
== 0 || nflag
)) {
206 fprintf(stderr
, "Correction factor can only be applied with -r and without -n\n");
215 * Open our execfile and corefile, resolve needed symbols and read in
218 if ((kd
= kvm_openfiles(Nflag
? execfile
: NULL
,
219 Mflag
? corefile
: NULL
, NULL
, O_RDONLY
, errbuf
)) == NULL
)
220 errx(1, "%s", errbuf
);
221 if (kvm_nlist(kd
, nl
) != 0)
222 errx(1, "%s", kvm_geterr(kd
));
223 if (kvm_read(kd
, nl
[0].n_value
, &version
, sizeof(version
)) == -1)
224 errx(1, "%s", kvm_geterr(kd
));
225 if (kvm_read(kd
, nl
[4].n_value
, &ncpus
, sizeof(ncpus
)) == -1)
226 errx(1, "%s", kvm_geterr(kd
));
227 ktr_start_index
= malloc(sizeof(*ktr_start_index
) * ncpus
);
229 if (kvm_read(kd
, nl
[5].n_value
, &tts
, sizeof(tts
)) == -1)
230 errx(1, "%s", kvm_geterr(kd
));
231 tsc_frequency
= (double)tts
;
233 if (version
> KTR_VERSION
)
234 errx(1, "ktr version too high for us to handle");
235 if (kvm_read(kd
, nl
[1].n_value
, &entries_per_buf
,
236 sizeof(entries_per_buf
)) == -1)
237 errx(1, "%s", kvm_geterr(kd
));
238 fifo_mask
= entries_per_buf
- 1;
240 printf("TSC frequency is %6.3f MHz\n", tsc_frequency
/ 1000000.0);
242 ktr_kbuf
= malloc(sizeof(*ktr_kbuf
) * ncpus
);
245 read_symbols(Nflag
? execfile
: NULL
);
247 if (kvm_read(kd
, nl
[3].n_value
, ktr_kbuf
, sizeof(*ktr_kbuf
) * ncpus
) == -1)
248 errx(1, "%s", kvm_geterr(kd
));
250 ktr_bufs
= ktr_bufs_init(ncpus
);
253 u_int64_t last_timestamp
= 0;
255 load_bufs(ktr_bufs
, ktr_kbuf
);
256 print_bufs_timesorted(fo
, ktr_bufs
, &last_timestamp
);
258 usleep(1000000 / 10);
261 u_int64_t
*last_timestamp
= calloc(sizeof(u_int64_t
), ncpus
);
263 load_bufs(ktr_bufs
, ktr_kbuf
);
264 for (n
= 0; n
< ncpus
; ++n
)
265 print_buf(fo
, ktr_bufs
, n
, &last_timestamp
[n
]);
267 usleep(1000000 / 10);
274 print_header(FILE *fo
, int row
)
276 if (qflag
== 0 && (u_int32_t
)row
% 20 == 0) {
277 fprintf(fo
, "%-6s ", "index");
279 fprintf(fo
, "%-3s ", "cpu");
281 fprintf(fo
, "%-16s ", "timestamp");
284 fprintf(fo
, "%-10s %-10s", "caller2", "caller1");
286 fprintf(fo
, "%-20s %-20s", "caller2", "caller1");
289 fprintf(fo
, "%-20s ", "ID");
291 fprintf(fo
, "%10s%-30s ", "", "file and line");
293 fprintf(fo
, "%s", "trace");
299 print_entry(FILE *fo
, kvm_t
*kd
, int n
, int row
, struct ktr_entry
*entry
,
300 u_int64_t
*last_timestamp
)
302 struct ktr_info
*info
= NULL
;
304 fprintf(fo
, " %06x ", row
& 0x00FFFFFF);
306 fprintf(fo
, "%-3d ", n
);
307 if (tflag
|| rflag
) {
308 if (rflag
&& !nflag
&& tsc_frequency
!= 0.0) {
309 fprintf(fo
, "%13.3f uS ",
310 (double)(entry
->ktr_timestamp
- *last_timestamp
) * 1000000.0 / tsc_frequency
- correction_factor
);
312 fprintf(fo
, "%-16lld ", entry
->ktr_timestamp
-
315 fprintf(fo
, "%-16lld ", entry
->ktr_timestamp
);
320 fprintf(fo
, "%p %p ",
321 entry
->ktr_caller2
, entry
->ktr_caller1
);
323 fprintf(fo
, "%-20s ",
324 address_to_symbol(entry
->ktr_caller2
));
325 fprintf(fo
, "%-20s ",
326 address_to_symbol(entry
->ktr_caller1
));
330 info
= kvm_ktrinfo(kd
, entry
->ktr_info
);
332 fprintf(fo
, "%-20s ", kvm_string(kd
, info
->kf_name
));
334 fprintf(fo
, "%-20s ", "<empty>");
337 fprintf(fo
, "%34s:%-4d ", trunc_path(kvm_string(kd
, entry
->ktr_file
), 34), entry
->ktr_line
);
340 info
= kvm_ktrinfo(kd
, entry
->ktr_info
);
342 fprintf(fo
, kvm_string(kd
, info
->kf_format
),
343 entry
->ktr_data
[0], entry
->ktr_data
[1],
344 entry
->ktr_data
[2], entry
->ktr_data
[3],
345 entry
->ktr_data
[4], entry
->ktr_data
[5],
346 entry
->ktr_data
[6], entry
->ktr_data
[7],
347 entry
->ktr_data
[8], entry
->ktr_data
[9]);
353 *last_timestamp
= entry
->ktr_timestamp
;
358 kvm_ktrinfo(kvm_t
*kd
, void *kptr
)
360 static struct ktr_info save_info
;
361 static void *save_kptr
;
365 if (save_kptr
!= kptr
) {
366 if (kvm_read(kd
, (uintptr_t)kptr
, &save_info
, sizeof(save_info
)) == -1) {
367 bzero(&save_info
, sizeof(save_info
));
377 kvm_string(kvm_t
*kd
, const char *kptr
)
379 static char save_str
[128];
380 static const char *save_kptr
;
386 if (save_kptr
!= kptr
) {
389 while (l
< sizeof(save_str
) - 1) {
390 n
= 256 - ((intptr_t)(kptr
+ l
) & 255);
391 if (n
> sizeof(save_str
) - l
- 1)
392 n
= sizeof(save_str
) - l
- 1;
393 if (kvm_read(kd
, (uintptr_t)(kptr
+ l
), save_str
+ l
, n
) < 0)
395 while (l
< sizeof(save_str
) && n
) {
396 if (save_str
[l
] == 0)
411 trunc_path(const char *str
, int maxlen
)
413 int len
= strlen(str
);
416 return(str
+ len
- maxlen
);
422 TAILQ_ENTRY(symdata
) link
;
428 static TAILQ_HEAD(symlist
, symdata
) symlist
;
429 static struct symdata
*symcache
;
430 static char *symbegin
;
435 read_symbols(const char *execfile
)
439 int buflen
= sizeof(buf
);
446 TAILQ_INIT(&symlist
);
448 if (execfile
== NULL
) {
449 if (sysctlbyname("kern.bootfile", buf
, &buflen
, NULL
, 0) < 0)
450 execfile
= "/kernel";
454 snprintf(cmd
, sizeof(cmd
), "nm -n %s", execfile
);
455 if ((fp
= popen(cmd
, "r")) != NULL
) {
456 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
457 s1
= strtok(buf
, " \t\n");
458 s2
= strtok(NULL
, " \t\n");
459 s3
= strtok(NULL
, " \t\n");
460 if (s1
&& s2
&& s3
) {
461 sym
= malloc(sizeof(struct symdata
));
462 sym
->symaddr
= (char *)strtoul(s1
, NULL
, 16);
463 sym
->symtype
= s2
[0];
464 sym
->symname
= strdup(s3
);
465 if (strcmp(s3
, "kernbase") == 0)
466 symbegin
= sym
->symaddr
;
467 if (strcmp(s3
, "end") == 0)
468 symend
= sym
->symaddr
;
469 TAILQ_INSERT_TAIL(&symlist
, sym
, link
);
474 symcache
= TAILQ_FIRST(&symlist
);
479 address_to_symbol(void *kptr
)
483 if (symcache
== NULL
||
484 (char *)kptr
< symbegin
|| (char *)kptr
>= symend
486 snprintf(buf
, sizeof(buf
), "%p", kptr
);
489 while ((char *)symcache
->symaddr
< (char *)kptr
) {
490 if (TAILQ_NEXT(symcache
, link
) == NULL
)
492 symcache
= TAILQ_NEXT(symcache
, link
);
494 while ((char *)symcache
->symaddr
> (char *)kptr
) {
495 if (symcache
!= TAILQ_FIRST(&symlist
))
496 symcache
= TAILQ_PREV(symcache
, symlist
, link
);
498 snprintf(buf
, sizeof(buf
), "%s+%d", symcache
->symname
,
499 (int)((char *)kptr
- symcache
->symaddr
));
505 ktr_bufs_init(int ncpus
)
507 struct ktr_buffer
*ktr_bufs
, *it
;
510 ktr_bufs
= malloc(sizeof(*ktr_bufs
) * ncpus
);
512 err(1, "can't allocate data structures\n");
513 for (i
= 0; i
< ncpus
; ++i
) {
515 it
->ents
= malloc(sizeof(struct ktr_entry
) * entries_per_buf
);
516 if (it
->ents
== NULL
)
517 err(1, "can't allocate data structures\n");
527 get_indices(kvm_t
*kd
, int *idx
)
529 if (kvm_read(kd
, nl
[2].n_value
, idx
, sizeof(*idx
) * ncpus
) == -1)
530 errx(1, "%s", kvm_geterr(kd
));
534 * Get the trace buffer data from the kernel
538 load_bufs(struct ktr_buffer
*ktr_bufs
, struct ktr_entry
**kbufs
)
540 static int *kern_idx
;
541 struct ktr_buffer
*kbuf
;
545 kern_idx
= malloc(sizeof(*kern_idx
) * ncpus
);
547 err(1, "can't allocate data structures\n");
551 get_indices(kd
, kern_idx
);
552 for (i
= 0; i
< ncpus
; ++i
) {
554 if (kern_idx
[i
] == kbuf
->end_idx
)
556 kbuf
->end_idx
= kern_idx
[i
];
559 * If we do not have a notion of the beginning index, assume
560 * it is entries_per_buf before the ending index. Don't
561 * worry about underflows/negative numbers, the indices will
565 kbuf
->beg_idx
= kbuf
->end_idx
- entries_per_buf
+ 1;
568 if (kvm_read(kd
, (uintptr_t)kbufs
[i
], ktr_bufs
[i
].ents
,
569 sizeof(struct ktr_entry
) * entries_per_buf
)
571 errx(1, "%s", kvm_geterr(kd
));
573 kbuf
->beg_idx
= earliest_ts(kbuf
);
579 * Locate the earliest timestamp iterating backwards from end_idx, but
580 * not going further back then beg_idx. We have to do this because
581 * the kernel uses a circulating buffer.
585 earliest_ts(struct ktr_buffer
*buf
)
587 struct ktr_entry
*save
;
588 int count
, scan
, i
, earliest
;
591 earliest
= buf
->end_idx
- 1;
592 save
= &buf
->ents
[earliest
& fifo_mask
];
593 for (scan
= buf
->end_idx
- 1; scan
!= buf
->beg_idx
-1; --scan
) {
594 i
= scan
& fifo_mask
;
595 if (buf
->ents
[i
].ktr_timestamp
< save
->ktr_timestamp
)
598 * We may have gotten so far behind that beg_idx wrapped
599 * more then once around the buffer. Just stop
601 if (++count
== entries_per_buf
)
609 print_buf(FILE *fo
, struct ktr_buffer
*ktr_bufs
, int cpu
,
610 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
, kd
, 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
, kd
, bestn
, row
,
660 &buf
->ents
[buf
->beg_idx
& fifo_mask
],
663 *last_timestamp
= ts
;
671 fprintf(stderr
, "usage: ktrdump [-acfinpqrtx] [-N execfile] "
672 "[-M corefile] [-o outfile]\n");