1 /* $NetBSD: kdump.c,v 1.35 2001/02/16 23:28:44 manu Exp $ */ 2 3 /*- 4 * Copyright (c) 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)kdump.c 8.4 (Berkeley) 4/28/95"; 45 #else 46 __RCSID("$NetBSD: kdump.c,v 1.35 2001/02/16 23:28:44 manu Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #define _KERNEL 52 #include <sys/errno.h> 53 #undef _KERNEL 54 #include <sys/time.h> 55 #include <sys/uio.h> 56 #include <sys/ktrace.h> 57 #include <sys/ioctl.h> 58 #include <sys/ptrace.h> 59 60 #include <err.h> 61 #include <signal.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <unistd.h> 66 #include <vis.h> 67 68 #include "ktrace.h" 69 #include "setemul.h" 70 71 #include <sys/syscall.h> 72 73 int timestamp, decimal, fancy = 1, tail, maxdata; 74 const char *tracefile = DEF_TRACEFILE; 75 struct ktr_header ktr_header; 76 int emul_changed = 0; 77 78 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0) 79 80 static const char *ptrace_ops[] = { 81 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U", 82 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE", 83 "PT_KILL", "PT_ATTACH", "PT_DETACH", 84 }; 85 86 static const char *linux_ptrace_ops[] = { 87 "PTRACE_TRACEME", 88 "PTRACE_PEEKTEXT", "PTRACE_PEEKDATA", "PTRACE_PEEKUSER", 89 "PTRACE_POKETEXT", "PTRACE_POKEDATA", "PTRACE_POKEUSER", 90 "PTRACE_CONT", "PTRACE_KILL", "PTRACE_SINGLESTEP", 91 NULL, NULL, 92 "PTRACE_GETREGS", "PTRACE_SETREGS", "PTRACE_GETFPREGS", 93 "PTRACE_SETFPREGS", "PTRACE_ATTACH", "PTRACE_DETACH", 94 "PTRACE_SYSCALL", 95 }; 96 97 int main __P((int, char **)); 98 int fread_tail __P((char *, int, int)); 99 void dumpheader __P((struct ktr_header *)); 100 void ioctldecode __P((u_long)); 101 void ktrsyscall __P((struct ktr_syscall *)); 102 void ktrsysret __P((struct ktr_sysret *)); 103 void ktrnamei __P((char *, int)); 104 void ktremul __P((char *, int, int)); 105 void ktrgenio __P((struct ktr_genio *, int)); 106 void ktrpsig __P((struct ktr_psig *)); 107 void ktrcsw __P((struct ktr_csw *)); 108 void ktruser __P((struct ktr_user *, int)); 109 void usage __P((void)); 110 void eprint __P((int)); 111 char *ioctlname __P((long)); 112 113 int 114 main(argc, argv) 115 int argc; 116 char *argv[]; 117 { 118 int ch, ktrlen, size; 119 void *m; 120 int trpoints = ALL_POINTS; 121 const char *emul_name = "netbsd"; 122 123 while ((ch = getopt(argc, argv, "e:f:dlm:nRTt:")) != -1) 124 switch (ch) { 125 case 'e': 126 emul_name = strdup(optarg); /* it's safer to copy it */ 127 break; 128 case 'f': 129 tracefile = optarg; 130 break; 131 case 'd': 132 decimal = 1; 133 break; 134 case 'l': 135 tail = 1; 136 break; 137 case 'm': 138 maxdata = atoi(optarg); 139 break; 140 case 'n': 141 fancy = 0; 142 break; 143 case 'R': 144 timestamp = 2; /* relative timestamp */ 145 break; 146 case 'T': 147 timestamp = 1; 148 break; 149 case 't': 150 trpoints = getpoints(optarg); 151 if (trpoints < 0) 152 errx(1, "unknown trace point in %s", optarg); 153 break; 154 default: 155 usage(); 156 } 157 argv += optind; 158 argc -= optind; 159 160 if (argc > 1) 161 usage(); 162 163 setemul(emul_name, 0, 0); 164 165 m = malloc(size = 1024); 166 if (m == NULL) 167 errx(1, "malloc: %s", strerror(ENOMEM)); 168 if (!freopen(tracefile, "r", stdin)) 169 err(1, "%s", tracefile); 170 while (fread_tail((char *)&ktr_header, sizeof(struct ktr_header), 1)) { 171 if (trpoints & (1<<ktr_header.ktr_type)) 172 dumpheader(&ktr_header); 173 if ((ktrlen = ktr_header.ktr_len) < 0) 174 errx(1, "bogus length 0x%x", ktrlen); 175 if (ktrlen > size) { 176 while(ktrlen > size) size *= 2; 177 m = (void *)realloc(m, size); 178 if (m == NULL) 179 errx(1, "realloc: %s", strerror(ENOMEM)); 180 } 181 if (ktrlen && fread_tail(m, ktrlen, 1) == 0) 182 errx(1, "data too short"); 183 if ((trpoints & (1<<ktr_header.ktr_type)) == 0) 184 continue; 185 186 /* update context to match currently processed record */ 187 ectx_sanify(ktr_header.ktr_pid); 188 189 switch (ktr_header.ktr_type) { 190 case KTR_SYSCALL: 191 ktrsyscall((struct ktr_syscall *)m); 192 break; 193 case KTR_SYSRET: 194 ktrsysret((struct ktr_sysret *)m); 195 break; 196 case KTR_NAMEI: 197 ktrnamei(m, ktrlen); 198 break; 199 case KTR_GENIO: 200 ktrgenio((struct ktr_genio *)m, ktrlen); 201 break; 202 case KTR_PSIG: 203 ktrpsig((struct ktr_psig *)m); 204 break; 205 case KTR_CSW: 206 ktrcsw((struct ktr_csw *)m); 207 break; 208 case KTR_EMUL: 209 ktremul(m, ktrlen, size); 210 break; 211 case KTR_USER: 212 ktruser((struct ktr_user *)m, ktrlen); 213 break; 214 } 215 if (tail) 216 (void)fflush(stdout); 217 } 218 return (0); 219 } 220 221 int 222 fread_tail(buf, size, num) 223 char *buf; 224 int num, size; 225 { 226 int i; 227 228 while ((i = fread(buf, size, num, stdin)) == 0 && tail) { 229 (void)sleep(1); 230 clearerr(stdin); 231 } 232 return (i); 233 } 234 235 void 236 dumpheader(kth) 237 struct ktr_header *kth; 238 { 239 char unknown[64], *type; 240 static struct timeval prevtime; 241 struct timeval temp; 242 243 switch (kth->ktr_type) { 244 case KTR_SYSCALL: 245 type = "CALL"; 246 break; 247 case KTR_SYSRET: 248 type = "RET "; 249 break; 250 case KTR_NAMEI: 251 type = "NAMI"; 252 break; 253 case KTR_GENIO: 254 type = "GIO "; 255 break; 256 case KTR_PSIG: 257 type = "PSIG"; 258 break; 259 case KTR_CSW: 260 type = "CSW"; 261 break; 262 case KTR_EMUL: 263 type = "EMUL"; 264 break; 265 case KTR_USER: 266 type = "USER"; 267 break; 268 default: 269 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type); 270 type = unknown; 271 } 272 273 (void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm); 274 if (timestamp) { 275 if (timestamp == 2) { 276 timersub(&kth->ktr_time, &prevtime, &temp); 277 prevtime = kth->ktr_time; 278 } else 279 temp = kth->ktr_time; 280 (void)printf("%ld.%06ld ", 281 (long int)temp.tv_sec, (long int)temp.tv_usec); 282 } 283 (void)printf("%s ", type); 284 } 285 286 void 287 ioctldecode(cmd) 288 u_long cmd; 289 { 290 char dirbuf[4], *dir = dirbuf; 291 292 if (cmd & IOC_IN) 293 *dir++ = 'W'; 294 if (cmd & IOC_OUT) 295 *dir++ = 'R'; 296 *dir = '\0'; 297 298 printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx", 299 dirbuf, (int) ((cmd >> 8) & 0xff), cmd & 0xff); 300 if ((cmd & IOC_VOID) == 0) 301 printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff); 302 else 303 printf(")"); 304 } 305 306 void 307 ktrsyscall(ktr) 308 struct ktr_syscall *ktr; 309 { 310 int argsize = ktr->ktr_argsize; 311 register_t *ap; 312 313 if (ktr->ktr_code >= current->nsysnames || ktr->ktr_code < 0) 314 (void)printf("[%d]", ktr->ktr_code); 315 else 316 (void)printf("%s", current->sysnames[ktr->ktr_code]); 317 ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall)); 318 if (argsize) { 319 char c = '('; 320 if (fancy) { 321 if (ktr->ktr_code == SYS_ioctl) { 322 char *cp; 323 if (decimal) 324 (void)printf("(%ld", (long)*ap); 325 else 326 (void)printf("(%#lx", (long)*ap); 327 ap++; 328 argsize -= sizeof(register_t); 329 if ((cp = ioctlname(*ap)) != NULL) 330 (void)printf(",%s", cp); 331 else 332 ioctldecode(*ap); 333 c = ','; 334 ap++; 335 argsize -= sizeof(register_t); 336 } else if (ktr->ktr_code == SYS_ptrace) { 337 if (strcmp(current->name, "linux") == 0) { 338 if (*ap >= 0 && *ap <= 339 sizeof(linux_ptrace_ops) / sizeof(linux_ptrace_ops[0])) 340 (void)printf("(%s", linux_ptrace_ops[*ap]); 341 else 342 (void)printf("(%ld", (long)*ap); 343 } else { 344 if (*ap >= 0 && *ap <= 345 sizeof(ptrace_ops) / sizeof(ptrace_ops[0])) 346 (void)printf("(%s", ptrace_ops[*ap]); 347 else 348 (void)printf("(%ld", (long)*ap); 349 } 350 c = ','; 351 ap++; 352 argsize -= sizeof(register_t); 353 } 354 } 355 while (argsize) { 356 if (decimal) 357 (void)printf("%c%ld", c, (long)*ap); 358 else 359 (void)printf("%c%#lx", c, (long)*ap); 360 c = ','; 361 ap++; 362 argsize -= sizeof(register_t); 363 } 364 (void)putchar(')'); 365 } 366 (void)putchar('\n'); 367 } 368 369 void 370 ktrsysret(ktr) 371 struct ktr_sysret *ktr; 372 { 373 const struct emulation *revelant; 374 register_t ret = ktr->ktr_retval; 375 int error = ktr->ktr_error; 376 int code = ktr->ktr_code; 377 378 if (emul_changed) 379 revelant = previous; 380 else 381 revelant = current; 382 emul_changed = 0; 383 384 if (code >= revelant->nsysnames || code < 0) 385 (void)printf("[%d] ", code); 386 else 387 (void)printf("%s ", revelant->sysnames[code]); 388 389 switch (error) { 390 case 0: 391 if (fancy) { 392 (void)printf("%ld", (long)ret); 393 if (ret < 0 || ret > 9) 394 (void)printf("/%#lx", (long)ret); 395 } else { 396 if (decimal) 397 (void)printf("%ld", (long)ret); 398 else 399 (void)printf("%#lx", (long)ret); 400 } 401 break; 402 403 default: 404 eprint(error); 405 break; 406 } 407 (void)putchar('\n'); 408 409 } 410 411 /* 412 * We print the original emulation's error numerically, but we 413 * translate it to netbsd to print it symbolically. 414 */ 415 void 416 eprint(e) 417 int e; 418 { 419 int i = e; 420 421 if (current->errnomap) { 422 423 /* No remapping for ERESTART and EJUSTRETURN */ 424 /* Kludge for linux that has negative error numbers */ 425 if (current->errnomap[2] > 0 && e < 0) 426 goto normal; 427 428 for (i = 0; i < current->nerrnomap; i++) 429 if (e == current->errnomap[i]) 430 break; 431 432 if (i == current->nerrnomap) { 433 printf("-1 unknown errno %d", e); 434 return; 435 } 436 } 437 438 normal: 439 switch (i) { 440 case ERESTART: 441 (void)printf("RESTART"); 442 break; 443 444 case EJUSTRETURN: 445 (void)printf("JUSTRETURN"); 446 break; 447 448 default: 449 (void)printf("-1 errno %d", e); 450 if (fancy) 451 (void)printf(" %s", strerror(i)); 452 } 453 } 454 455 void 456 ktrnamei(cp, len) 457 char *cp; 458 int len; 459 { 460 461 (void)printf("\"%.*s\"\n", len, cp); 462 } 463 464 void 465 ktremul(name, len, bufsize) 466 char *name; 467 int len, bufsize; 468 { 469 if (len >= bufsize) 470 len = bufsize - 1; 471 472 name[len] = '\0'; 473 setemul(name, ktr_header.ktr_pid, 1); 474 emul_changed = 1; 475 476 (void)printf("\"%s\"\n", name); 477 } 478 479 void 480 ktrgenio(ktr, len) 481 struct ktr_genio *ktr; 482 int len; 483 { 484 int datalen = len - sizeof (struct ktr_genio); 485 char *dp = (char *)ktr + sizeof (struct ktr_genio); 486 char *cp; 487 int col = 0; 488 int width; 489 char visbuf[5]; 490 static int screenwidth = 0; 491 492 if (screenwidth == 0) { 493 struct winsize ws; 494 495 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 && 496 ws.ws_col > 8) 497 screenwidth = ws.ws_col; 498 else 499 screenwidth = 80; 500 } 501 printf("fd %d %s %d bytes\n", ktr->ktr_fd, 502 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen); 503 if (maxdata && datalen > maxdata) 504 datalen = maxdata; 505 (void)printf(" \""); 506 col = 8; 507 for (; datalen > 0; datalen--, dp++) { 508 (void) vis(visbuf, *dp, VIS_CSTYLE, datalen>1?*(dp+1):0); 509 cp = visbuf; 510 /* 511 * Keep track of printables and 512 * space chars (like fold(1)). 513 */ 514 if (col == 0) { 515 (void)putchar('\t'); 516 col = 8; 517 } 518 switch(*cp) { 519 case '\n': 520 col = 0; 521 (void)putchar('\n'); 522 continue; 523 case '\t': 524 width = 8 - (col&07); 525 break; 526 default: 527 width = strlen(cp); 528 } 529 if (col + width > (screenwidth-2)) { 530 (void)printf("\\\n\t"); 531 col = 8; 532 } 533 col += width; 534 do { 535 (void)putchar(*cp++); 536 } while (*cp); 537 } 538 if (col == 0) 539 (void)printf(" "); 540 (void)printf("\"\n"); 541 } 542 543 void 544 ktrpsig(psig) 545 struct ktr_psig *psig; 546 { 547 int signo, first; 548 549 (void)printf("SIG%s ", sys_signame[psig->signo]); 550 if (psig->action == SIG_DFL) 551 (void)printf("SIG_DFL\n"); 552 else { 553 (void)printf("caught handler=0x%lx mask=(", 554 (u_long)psig->action); 555 first = 1; 556 for (signo = 1; signo < NSIG; signo++) { 557 if (sigismember(&psig->mask, signo)) { 558 if (first) 559 first = 0; 560 else 561 (void)printf(","); 562 (void)printf("%d", signo); 563 } 564 } 565 (void)printf(") code=0x%x\n", psig->code); 566 } 567 } 568 569 void 570 ktrcsw(cs) 571 struct ktr_csw *cs; 572 { 573 574 (void)printf("%s %s\n", cs->out ? "stop" : "resume", 575 cs->user ? "user" : "kernel"); 576 } 577 578 void 579 ktruser(usr, len) 580 struct ktr_user *usr; 581 int len; 582 { 583 int i; 584 char *dta; 585 586 printf("\"%.*s: %d, ", KTR_USER_MAXIDLEN, usr->ktr_id, len); 587 dta = (char *)usr; 588 for(i=sizeof(struct ktr_user); i < len; i++) 589 printf("%x", dta[i]); 590 printf("\"\n"); 591 } 592 593 void 594 usage() 595 { 596 597 (void)fprintf(stderr, 598 "usage: kdump [-dnlRT] [-e emulation] [-f trfile] [-m maxdata] [-t [cnis]]\n"); 599 exit(1); 600 } 601