If PCI_MAP_FIXUP is defined, following fixes will be applied:
[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 $
dbf2e4fa 28 * $DragonFly: src/usr.bin/ktrdump/ktrdump.c,v 1.6 2005/12/19 17:09:58 dillon 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
HP
38
39#include <err.h>
40#include <fcntl.h>
41#include <kvm.h>
42#include <limits.h>
43#include <nlist.h>
44#include <stdint.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49
50#define SBUFLEN 128
9f815599
HP
51
52extern char *optarg;
53extern int optind;
54
55static void usage(void);
6e8dad34
MD
56static void print_header(FILE *fo, int row);
57static void print_entry(FILE *fo, kvm_t *kd, int n, int i, struct ktr_entry *entry);
58static struct ktr_info *kvm_ktrinfo(kvm_t *kd, void *kptr);
59static const char *kvm_string(kvm_t *kd, const char *kptr);
60static const char *trunc_path(const char *str, int maxlen);
fa9963c9
MD
61static void read_symbols(const char *execfile);
62static const char *address_to_symbol(void *kptr);
9f815599
HP
63
64static struct nlist nl[] = {
65 { "_ktr_version" },
66 { "_ktr_entries" },
67 { "_ktr_idx" },
68 { "_ktr_buf" },
6e8dad34 69 { "_ncpus" },
b8272239 70 { "_tsc_frequency" },
9f815599
HP
71 { NULL }
72};
73
74static int cflag;
9f815599 75static int fflag;
6e8dad34 76static int iflag;
6e8dad34 77static int nflag;
9f815599
HP
78static int qflag;
79static int rflag;
b8272239 80static int sflag;
9f815599 81static int tflag;
6e8dad34
MD
82static int xflag;
83static int pflag;
fa9963c9
MD
84static int Mflag;
85static int Nflag;
6e8dad34 86static int64_t last_timestamp;
b8272239 87static double tsc_frequency;
dbf2e4fa 88static double correction_factor = 0.0;
9f815599
HP
89
90static char corefile[PATH_MAX];
91static char execfile[PATH_MAX];
92
93static char desc[SBUFLEN];
94static char errbuf[_POSIX2_LINE_MAX];
95static char fbuf[PATH_MAX];
96static char obuf[PATH_MAX];
9f815599
HP
97
98/*
99 * Reads the ktr trace buffer from kernel memory and prints the trace entries.
100 */
101int
102main(int ac, char **av)
103{
6e8dad34 104 struct ktr_entry **ktr_buf;
b8272239
MD
105 struct ktr_entry *entry;
106 struct ktr_entry *entryx;
9f815599
HP
107 uintmax_t tlast, tnow;
108 struct stat sb;
109 kvm_t *kd;
6e8dad34 110 FILE *fo;
9f815599 111 char *p;
b8272239
MD
112 int64_t first_timestamp;
113 int64_t tts;
114 int *ktr_start_index;
9f815599
HP
115 int version;
116 int entries;
6e8dad34
MD
117 int *ktr_idx;
118 int ncpus;
fa9963c9 119 int did_display_flag = 0;
9f815599
HP
120 int in;
121 int c;
6e8dad34
MD
122 int i;
123 int n;
b8272239
MD
124 int bestn;
125 int row;
9f815599
HP
126
127 /*
128 * Parse commandline arguments.
129 */
6e8dad34 130 fo = stdout;
dbf2e4fa 131 while ((c = getopt(ac, av, "acfinqrtxpsA:N:M:o:")) != -1) {
9f815599 132 switch (c) {
6e8dad34
MD
133 case 'a':
134 cflag = 1;
135 iflag = 1;
136 tflag = 1;
137 xflag = 1;
138 fflag = 1;
139 pflag = 1;
140 break;
9f815599
HP
141 case 'c':
142 cflag = 1;
143 break;
6e8dad34 144 case 'N':
9f815599
HP
145 if (strlcpy(execfile, optarg, sizeof(execfile))
146 >= sizeof(execfile))
147 errx(1, "%s: File name too long", optarg);
fa9963c9 148 Nflag = 1;
9f815599
HP
149 break;
150 case 'f':
151 fflag = 1;
152 break;
153 case 'i':
154 iflag = 1;
9f815599 155 break;
dbf2e4fa
MD
156 case 'A':
157 correction_factor = strtod(optarg, NULL);
158 break;
6e8dad34 159 case 'M':
9f815599
HP
160 if (strlcpy(corefile, optarg, sizeof(corefile))
161 >= sizeof(corefile))
162 errx(1, "%s: File name too long", optarg);
fa9963c9
MD
163 Mflag = 1;
164 break;
165 case 'n':
166 nflag = 1;
9f815599
HP
167 break;
168 case 'o':
6e8dad34 169 if ((fo = fopen(optarg, "w")) == NULL)
9f815599
HP
170 err(1, "%s", optarg);
171 break;
6e8dad34
MD
172 case 'p':
173 pflag++;
174 break;
9f815599
HP
175 case 'q':
176 qflag++;
177 break;
178 case 'r':
179 rflag = 1;
180 break;
b8272239
MD
181 case 's':
182 sflag = 1; /* sort across the cpus */
183 break;
9f815599
HP
184 case 't':
185 tflag = 1;
186 break;
6e8dad34
MD
187 case 'x':
188 xflag = 1;
189 break;
9f815599
HP
190 case '?':
191 default:
192 usage();
193 }
fa9963c9
MD
194 }
195 if (cflag + iflag + tflag + xflag + fflag + pflag == 0) {
196 cflag = 1;
197 iflag = 1;
198 tflag = 1;
fa9963c9
MD
199 pflag = 1;
200 }
dbf2e4fa
MD
201 if (correction_factor != 0.0 && (rflag == 0 || nflag)) {
202 fprintf(stderr, "Correction factor can only be applied with -r and without -n\n");
203 exit(1);
204 }
9f815599
HP
205 ac -= optind;
206 av += optind;
207 if (ac != 0)
208 usage();
209
210 /*
211 * Open our execfile and corefile, resolve needed symbols and read in
212 * the trace buffer.
213 */
fa9963c9
MD
214 if ((kd = kvm_openfiles(Nflag ? execfile : NULL,
215 Mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL)
9f815599 216 errx(1, "%s", errbuf);
6e8dad34
MD
217 if (kvm_nlist(kd, nl) != 0)
218 errx(1, "%s", kvm_geterr(kd));
219 if (kvm_read(kd, nl[0].n_value, &version, sizeof(version)) == -1)
9f815599 220 errx(1, "%s", kvm_geterr(kd));
6e8dad34
MD
221 if (kvm_read(kd, nl[4].n_value, &ncpus, sizeof(ncpus)) == -1)
222 errx(1, "%s", kvm_geterr(kd));
b8272239
MD
223 ktr_start_index = malloc(sizeof(*ktr_start_index) * ncpus);
224 if (version >= 3) {
225 if (kvm_read(kd, nl[5].n_value, &tts, sizeof(tts)) == -1)
226 errx(1, "%s", kvm_geterr(kd));
227 tsc_frequency = (double)tts;
228 }
229 if (version > KTR_VERSION)
230 errx(1, "ktr version too high for us to handle");
6e8dad34
MD
231 if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries)) == -1)
232 errx(1, "%s", kvm_geterr(kd));
233 ktr_buf = malloc(sizeof(*ktr_buf) * ncpus);
234 ktr_idx = malloc(sizeof(*ktr_idx) * ncpus);
235
fa9963c9
MD
236 if (nflag == 0)
237 read_symbols(Nflag ? execfile : NULL);
238
6e8dad34
MD
239 if (kvm_read(kd, nl[2].n_value, ktr_idx, sizeof(*ktr_idx) * ncpus) == -1)
240 errx(1, "%s", kvm_geterr(kd));
241 if (kvm_read(kd, nl[3].n_value, ktr_buf, sizeof(*ktr_buf) * ncpus) == -1)
242 errx(1, "%s", kvm_geterr(kd));
243 for (n = 0; n < ncpus; ++n) {
244 void *kptr = ktr_buf[n];
245 ktr_buf[n] = malloc(sizeof(**ktr_buf) * entries);
246 if (kvm_read(kd, (uintptr_t)kptr, ktr_buf[n], sizeof(**ktr_buf) * entries) == -1)
b8272239 247 errx(1, "%s", kvm_geterr(kd));
9f815599
HP
248 }
249
250 /*
b8272239
MD
251 * Figure out the lowest numbered timestamp.
252 */
253 first_timestamp = 0;
254 printf("TSC frequency is %6.3f MHz\n", tsc_frequency / 1000000.0);
255 for (n = 0; n < ncpus; ++n) {
256 for (i = 0; i < entries; ++i) {
257 entry = &ktr_buf[n][i];
258 if (entry->ktr_timestamp && (first_timestamp == 0 ||
259 first_timestamp < entry->ktr_timestamp)) {
260 first_timestamp = entry->ktr_timestamp;
261 }
262 }
263 }
264
265 /*
266 * Figure out the starting entry for each cpu
9f815599 267 */
6e8dad34 268 for (n = 0; n < ncpus; ++n) {
b8272239
MD
269 ktr_start_index[n] = 0;
270 tts = 0;
6e8dad34 271 for (i = 0; i < entries; ++i) {
b8272239
MD
272 entry = &ktr_buf[n][i];
273 if (entry->ktr_timestamp == 0)
274 continue;
275 if (tts == 0 || tts > entry->ktr_timestamp) {
276 tts = entry->ktr_timestamp;
277 ktr_start_index[n] = i;
278 }
279 }
280 }
281
282 /*
283 * Now tear through the trace buffer.
284 */
285 if (sflag) {
286 row = 0;
287 for (;;) {
288 bestn = -1;
289 tts = 0;
290 for (n = 0; n < ncpus; ++n) {
291 i = ktr_start_index[n];
292 entry = &ktr_buf[n][i];
293 if (entry->ktr_timestamp == 0)
294 continue;
295 if (tts == 0 || tts >= entry->ktr_timestamp) {
296 tts = entry->ktr_timestamp;
297 bestn = n;
298 }
299 }
300 if (bestn < 0 || tts < last_timestamp)
301 break;
302 print_header(fo, row);
303 print_entry(fo, kd, bestn, ktr_start_index[bestn],
304 &ktr_buf[bestn][ktr_start_index[bestn]]);
305 if (++ktr_start_index[bestn] == entries)
306 ktr_start_index[bestn] = 0;
307 last_timestamp = tts;
308 ++row;
309 }
310 } else {
311 for (n = 0; n < ncpus; ++n) {
312 last_timestamp = first_timestamp;
313 i = ktr_start_index[n];
314 do {
315 entry = &ktr_buf[n][i];
316 print_header(fo, i);
317 print_entry(fo, kd, n, i, &ktr_buf[n][i]);
318 if (++i == entries)
319 i = 0;
320 } while (i != ktr_start_index[n]);
6e8dad34
MD
321 }
322 }
323 return (0);
324}
9f815599 325
6e8dad34
MD
326static void
327print_header(FILE *fo, int row)
328{
329 if (qflag == 0 && row % 20 == 0) {
330 fprintf(fo, "%-6s ", "index");
9f815599 331 if (cflag)
6e8dad34
MD
332 fprintf(fo, "%-3s ", "cpu");
333 if (tflag || rflag)
334 fprintf(fo, "%-16s ", "timestamp");
fa9963c9
MD
335 if (xflag) {
336 if (nflag)
337 fprintf(fo, "%-10s %-10s", "caller2", "caller1");
338 else
339 fprintf(fo, "%-20s %-20s", "caller2", "caller1");
340 }
6e8dad34
MD
341 if (iflag)
342 fprintf(fo, "%-20s ", "ID");
9f815599 343 if (fflag)
6e8dad34
MD
344 fprintf(fo, "%10s%-30s ", "", "file and line");
345 if (pflag)
346 fprintf(fo, "%s", "trace");
347 fprintf(fo, "\n");
9f815599 348 }
6e8dad34 349}
9f815599 350
6e8dad34
MD
351static void
352print_entry(FILE *fo, kvm_t *kd, int n, int i, struct ktr_entry *entry)
353{
354 struct ktr_info *info = NULL;
355
356 fprintf(fo, " %5d ", i);
357 if (cflag)
358 fprintf(fo, "%-3d ", n);
359 if (tflag || rflag) {
b8272239
MD
360 if (rflag && !nflag && tsc_frequency != 0.0) {
361 fprintf(fo, "%13.3f uS ",
dbf2e4fa 362 (double)(entry->ktr_timestamp - last_timestamp) * 1000000.0 / tsc_frequency - correction_factor);
b8272239 363 } else if (rflag) {
6e8dad34
MD
364 fprintf(fo, "%-16lld ", entry->ktr_timestamp -
365 last_timestamp);
b8272239 366 } else {
6e8dad34 367 fprintf(fo, "%-16lld ", entry->ktr_timestamp);
b8272239 368 }
6e8dad34 369 }
fa9963c9
MD
370 if (xflag) {
371 if (nflag) {
372 fprintf(fo, "%p %p ",
373 entry->ktr_caller2, entry->ktr_caller1);
374 } else {
375 fprintf(fo, "%-20s ",
376 address_to_symbol(entry->ktr_caller2));
377 fprintf(fo, "%-20s ",
378 address_to_symbol(entry->ktr_caller1));
379 }
380 }
6e8dad34
MD
381 if (iflag) {
382 info = kvm_ktrinfo(kd, entry->ktr_info);
383 if (info)
384 fprintf(fo, "%-20s ", kvm_string(kd, info->kf_name));
385 else
386 fprintf(fo, "%-20s ", "<empty>");
387 }
388 if (fflag)
389 fprintf(fo, "%34s:%-4d ", trunc_path(kvm_string(kd, entry->ktr_file), 34), entry->ktr_line);
390 if (pflag) {
391 if (info == NULL)
392 info = kvm_ktrinfo(kd, entry->ktr_info);
393 if (info) {
394 fprintf(fo, kvm_string(kd, info->kf_format),
395 entry->ktr_data[0], entry->ktr_data[1],
396 entry->ktr_data[2], entry->ktr_data[3],
397 entry->ktr_data[4], entry->ktr_data[5],
398 entry->ktr_data[6], entry->ktr_data[7],
399 entry->ktr_data[8], entry->ktr_data[9]);
400 } else {
401 fprintf(fo, "");
9f815599 402 }
6e8dad34
MD
403 }
404 fprintf(fo, "\n");
405 last_timestamp = entry->ktr_timestamp;
406}
407
408static
409struct ktr_info *
410kvm_ktrinfo(kvm_t *kd, void *kptr)
411{
412 static struct ktr_info save_info;
413 static void *save_kptr;
414
415 if (kptr == NULL)
416 return(NULL);
417 if (save_kptr != kptr) {
418 if (kvm_read(kd, (uintptr_t)kptr, &save_info, sizeof(save_info)) == -1) {
419 bzero(&save_info, sizeof(save_info));
9f815599 420 } else {
6e8dad34
MD
421 save_kptr = kptr;
422 }
423 }
424 return(&save_info);
425}
426
427static
428const char *
429kvm_string(kvm_t *kd, const char *kptr)
430{
431 static char save_str[128];
432 static const char *save_kptr;
433 int l;
434 int n;
435
436 if (kptr == NULL)
437 return("?");
438 if (save_kptr != kptr) {
439 save_kptr = kptr;
440 l = 0;
441 while (l < sizeof(save_str) - 1) {
442 n = 256 - ((intptr_t)(kptr + l) & 255);
443 if (n > sizeof(save_str) - l - 1)
444 n = sizeof(save_str) - l - 1;
445 if (kvm_read(kd, (uintptr_t)(kptr + l), save_str + l, n) < 0)
9f815599 446 break;
6e8dad34
MD
447 while (l < sizeof(save_str) && n) {
448 if (save_str[l] == 0)
449 break;
450 --n;
451 ++l;
452 }
453 if (n)
454 break;
9f815599 455 }
6e8dad34 456 save_str[l] = 0;
9f815599 457 }
6e8dad34
MD
458 return(save_str);
459}
9f815599 460
6e8dad34
MD
461static
462const char *
463trunc_path(const char *str, int maxlen)
464{
465 int len = strlen(str);
466
467 if (len > maxlen)
468 return(str + len - maxlen);
469 else
470 return(str);
9f815599
HP
471}
472
fa9963c9
MD
473struct symdata {
474 TAILQ_ENTRY(symdata) link;
475 const char *symname;
476 char *symaddr;
477 char symtype;
478};
479
480static TAILQ_HEAD(symlist, symdata) symlist;
481static struct symdata *symcache;
482static char *symbegin;
483static char *symend;
484
485static
486void
487read_symbols(const char *execfile)
488{
489 char buf[256];
490 char cmd[256];
491 int buflen = sizeof(buf);
492 FILE *fp;
493 struct symdata *sym;
494 char *s1;
495 char *s2;
496 char *s3;
497
498 TAILQ_INIT(&symlist);
499
500 if (execfile == NULL) {
501 if (sysctlbyname("kern.bootfile", buf, &buflen, NULL, 0) < 0)
502 execfile = "/kernel";
503 else
504 execfile = buf;
505 }
506 snprintf(cmd, sizeof(cmd), "nm -n %s", execfile);
507 if ((fp = popen(cmd, "r")) != NULL) {
508 while (fgets(buf, sizeof(buf), fp) != NULL) {
509 s1 = strtok(buf, " \t\n");
510 s2 = strtok(NULL, " \t\n");
511 s3 = strtok(NULL, " \t\n");
512 if (s1 && s2 && s3) {
513 sym = malloc(sizeof(struct symdata));
514 sym->symaddr = (char *)strtoul(s1, NULL, 16);
515 sym->symtype = s2[0];
516 sym->symname = strdup(s3);
517 if (strcmp(s3, "kernbase") == 0)
518 symbegin = sym->symaddr;
519 if (strcmp(s3, "end") == 0)
520 symend = sym->symaddr;
521 TAILQ_INSERT_TAIL(&symlist, sym, link);
522 }
523 }
524 pclose(fp);
525 }
526 symcache = TAILQ_FIRST(&symlist);
527}
528
529static
530const char *
531address_to_symbol(void *kptr)
532{
533 static char buf[64];
534
535 if (symcache == NULL ||
536 (char *)kptr < symbegin || (char *)kptr >= symend
537 ) {
538 snprintf(buf, sizeof(buf), "%p", kptr);
539 return(buf);
540 }
541 while ((char *)symcache->symaddr < (char *)kptr) {
542 if (TAILQ_NEXT(symcache, link) == NULL)
543 break;
544 symcache = TAILQ_NEXT(symcache, link);
545 }
546 while ((char *)symcache->symaddr > (char *)kptr) {
547 if (symcache != TAILQ_FIRST(&symlist))
548 symcache = TAILQ_PREV(symcache, symlist, link);
549 }
550 snprintf(buf, sizeof(buf), "%s+%d", symcache->symname,
551 (int)((char *)kptr - symcache->symaddr));
552 return(buf);
553}
554
9f815599
HP
555static void
556usage(void)
557{
fa9963c9 558 fprintf(stderr, "usage: ktrdump [-acfinpqrtx] [-N execfile] "
6e8dad34
MD
559 "[-M corefile] [-o outfile]\n");
560 exit(1);
9f815599 561}