1 /*- 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static char copyright[] = 36 "@(#) Copyright (c) 1988, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)kdump.c 8.1 (Berkeley) 6/6/93"; 42 #endif /* not lint */ 43 44 #define KERNEL 45 extern int errno; 46 #include <sys/errno.h> 47 #undef KERNEL 48 #include <sys/param.h> 49 #include <sys/errno.h> 50 #include <sys/time.h> 51 #include <sys/uio.h> 52 #include <sys/ktrace.h> 53 #include <sys/ioctl.h> 54 #include <sys/ptrace.h> 55 #include <vis.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <locale.h> 60 #include "ktrace.h" 61 62 int timestamp, decimal, fancy = 1, tail, maxdata; 63 char *tracefile = DEF_TRACEFILE; 64 struct ktr_header ktr_header; 65 66 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0) 67 68 main(argc, argv) 69 int argc; 70 char *argv[]; 71 { 72 extern int optind; 73 extern char *optarg; 74 int ch, ktrlen, size; 75 register void *m; 76 int trpoints = ALL_POINTS; 77 78 (void) setlocale(LC_CTYPE, ""); 79 80 while ((ch = getopt(argc,argv,"f:dlm:nRTt:")) != EOF) 81 switch((char)ch) { 82 case 'f': 83 tracefile = optarg; 84 break; 85 case 'd': 86 decimal = 1; 87 break; 88 case 'l': 89 tail = 1; 90 break; 91 case 'm': 92 maxdata = atoi(optarg); 93 break; 94 case 'n': 95 fancy = 0; 96 break; 97 case 'R': 98 timestamp = 2; /* relative timestamp */ 99 break; 100 case 'T': 101 timestamp = 1; 102 break; 103 case 't': 104 trpoints = getpoints(optarg); 105 if (trpoints < 0) { 106 (void)fprintf(stderr, 107 "kdump: unknown trace point in %s\n", 108 optarg); 109 exit(1); 110 } 111 break; 112 default: 113 usage(); 114 } 115 argv += optind; 116 argc -= optind; 117 118 if (argc > 1) 119 usage(); 120 121 m = (void *)malloc(size = 1025); 122 if (m == NULL) { 123 (void)fprintf(stderr, "kdump: %s.\n", strerror(ENOMEM)); 124 exit(1); 125 } 126 if (!freopen(tracefile, "r", stdin)) { 127 (void)fprintf(stderr, 128 "kdump: %s: %s.\n", tracefile, strerror(errno)); 129 exit(1); 130 } 131 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) { 132 if (trpoints & (1<<ktr_header.ktr_type)) 133 dumpheader(&ktr_header); 134 if ((ktrlen = ktr_header.ktr_len) < 0) { 135 (void)fprintf(stderr, 136 "kdump: bogus length 0x%x\n", ktrlen); 137 exit(1); 138 } 139 if (ktrlen > size) { 140 m = (void *)realloc(m, ktrlen+1); 141 if (m == NULL) { 142 (void)fprintf(stderr, 143 "kdump: %s.\n", strerror(ENOMEM)); 144 exit(1); 145 } 146 size = ktrlen; 147 } 148 if (ktrlen && fread_tail(m, ktrlen, 1) == 0) { 149 (void)fprintf(stderr, "kdump: data too short.\n"); 150 exit(1); 151 } 152 if ((trpoints & (1<<ktr_header.ktr_type)) == 0) 153 continue; 154 switch (ktr_header.ktr_type) { 155 case KTR_SYSCALL: 156 ktrsyscall((struct ktr_syscall *)m); 157 break; 158 case KTR_SYSRET: 159 ktrsysret((struct ktr_sysret *)m); 160 break; 161 case KTR_NAMEI: 162 ktrnamei(m, ktrlen); 163 break; 164 case KTR_GENIO: 165 ktrgenio((struct ktr_genio *)m, ktrlen); 166 break; 167 case KTR_PSIG: 168 ktrpsig((struct ktr_psig *)m); 169 break; 170 case KTR_CSW: 171 ktrcsw((struct ktr_csw *)m); 172 break; 173 } 174 if (tail) 175 (void)fflush(stdout); 176 } 177 } 178 179 fread_tail(buf, size, num) 180 char *buf; 181 int num, size; 182 { 183 int i; 184 185 while ((i = fread(buf, size, num, stdin)) == 0 && tail) { 186 (void)sleep(1); 187 clearerr(stdin); 188 } 189 return (i); 190 } 191 192 dumpheader(kth) 193 struct ktr_header *kth; 194 { 195 static char unknown[64]; 196 static struct timeval prevtime, temp; 197 char *type; 198 199 switch (kth->ktr_type) { 200 case KTR_SYSCALL: 201 type = "CALL"; 202 break; 203 case KTR_SYSRET: 204 type = "RET "; 205 break; 206 case KTR_NAMEI: 207 type = "NAMI"; 208 break; 209 case KTR_GENIO: 210 type = "GIO "; 211 break; 212 case KTR_PSIG: 213 type = "PSIG"; 214 break; 215 case KTR_CSW: 216 type = "CSW"; 217 break; 218 default: 219 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type); 220 type = unknown; 221 } 222 223 (void)printf("%6d %-8s ", kth->ktr_pid, kth->ktr_comm); 224 if (timestamp) { 225 if (timestamp == 2) { 226 temp = kth->ktr_time; 227 timevalsub(&kth->ktr_time, &prevtime); 228 prevtime = temp; 229 } 230 (void)printf("%ld.%06ld ", 231 kth->ktr_time.tv_sec, kth->ktr_time.tv_usec); 232 } 233 (void)printf("%s ", type); 234 } 235 236 #include <sys/syscall.h> 237 #define KTRACE 238 #include <sys/kern/syscalls.c> 239 #undef KTRACE 240 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]); 241 242 static char *ptrace_ops[] = { 243 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U", 244 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE", 245 "PT_KILL", "PT_STEP", 246 }; 247 248 ktrsyscall(ktr) 249 register struct ktr_syscall *ktr; 250 { 251 register narg = ktr->ktr_narg; 252 register int *ip; 253 char *ioctlname(); 254 255 if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0) 256 (void)printf("[%d]", ktr->ktr_code); 257 else 258 (void)printf("%s", syscallnames[ktr->ktr_code]); 259 ip = (int *)((char *)ktr + sizeof(struct ktr_syscall)); 260 if (narg) { 261 char c = '('; 262 if (fancy) { 263 if (ktr->ktr_code == SYS_ioctl) { 264 char *cp; 265 if (decimal) 266 (void)printf("(%d", *ip); 267 else 268 (void)printf("(%#x", *ip); 269 ip++; 270 narg--; 271 if ((cp = ioctlname(*ip)) != NULL) 272 (void)printf(",%s", cp); 273 else { 274 if (decimal) 275 (void)printf(",%d", *ip); 276 else 277 (void)printf(",%#x ", *ip); 278 } 279 c = ','; 280 ip++; 281 narg--; 282 } else if (ktr->ktr_code == SYS_ptrace) { 283 if (*ip <= PT_STEP && *ip >= 0) 284 (void)printf("(%s", ptrace_ops[*ip]); 285 else 286 (void)printf("(%d", *ip); 287 c = ','; 288 ip++; 289 narg--; 290 } 291 } 292 while (narg) { 293 if (decimal) 294 (void)printf("%c%d", c, *ip); 295 else 296 (void)printf("%c%#x", c, *ip); 297 c = ','; 298 ip++; 299 narg--; 300 } 301 (void)putchar(')'); 302 } 303 (void)putchar('\n'); 304 } 305 306 ktrsysret(ktr) 307 struct ktr_sysret *ktr; 308 { 309 register int ret = ktr->ktr_retval; 310 register int error = ktr->ktr_error; 311 register int code = ktr->ktr_code; 312 313 if (code >= nsyscalls || code < 0) 314 (void)printf("[%d] ", code); 315 else 316 (void)printf("%s ", syscallnames[code]); 317 318 if (error == 0) { 319 if (fancy) { 320 (void)printf("%d", ret); 321 if (ret < 0 || ret > 9) 322 (void)printf("/%#x", ret); 323 } else { 324 if (decimal) 325 (void)printf("%d", ret); 326 else 327 (void)printf("%#x", ret); 328 } 329 } else if (error == ERESTART) 330 (void)printf("RESTART"); 331 else if (error == EJUSTRETURN) 332 (void)printf("JUSTRETURN"); 333 else { 334 (void)printf("-1 errno %d", ktr->ktr_error); 335 if (fancy) 336 (void)printf(" %s", strerror(ktr->ktr_error)); 337 } 338 (void)putchar('\n'); 339 } 340 341 ktrnamei(cp, len) 342 char *cp; 343 { 344 (void)printf("\"%.*s\"\n", len, cp); 345 } 346 347 ktrgenio(ktr, len) 348 struct ktr_genio *ktr; 349 { 350 register int datalen = len - sizeof (struct ktr_genio); 351 register char *dp = (char *)ktr + sizeof (struct ktr_genio); 352 register char *cp; 353 register int col = 0; 354 register width; 355 char visbuf[5]; 356 static screenwidth = 0; 357 358 if (screenwidth == 0) { 359 struct winsize ws; 360 361 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 && 362 ws.ws_col > 8) 363 screenwidth = ws.ws_col; 364 else 365 screenwidth = 80; 366 } 367 printf("fd %d %s %d bytes\n", ktr->ktr_fd, 368 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen); 369 if (maxdata && datalen > maxdata) 370 datalen = maxdata; 371 (void)printf(" \""); 372 col = 8; 373 for (;datalen > 0; datalen--, dp++) { 374 (void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1)); 375 cp = visbuf; 376 /* 377 * Keep track of printables and 378 * space chars (like fold(1)). 379 */ 380 if (col == 0) { 381 (void)putchar('\t'); 382 col = 8; 383 } 384 switch(*cp) { 385 case '\n': 386 col = 0; 387 (void)putchar('\n'); 388 continue; 389 case '\t': 390 width = 8 - (col&07); 391 break; 392 default: 393 width = strlen(cp); 394 } 395 if (col + width > (screenwidth-2)) { 396 (void)printf("\\\n\t"); 397 col = 8; 398 } 399 col += width; 400 do { 401 (void)putchar(*cp++); 402 } while (*cp); 403 } 404 if (col == 0) 405 (void)printf(" "); 406 (void)printf("\"\n"); 407 } 408 409 char *signames[] = { 410 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */ 411 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */ 412 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */ 413 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */ 414 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */ 415 "USR2", NULL, /* 31 - 32 */ 416 }; 417 418 ktrpsig(psig) 419 struct ktr_psig *psig; 420 { 421 (void)printf("SIG%s ", signames[psig->signo]); 422 if (psig->action == SIG_DFL) 423 (void)printf("SIG_DFL\n"); 424 else 425 (void)printf("caught handler=0x%x mask=0x%x code=0x%x\n", 426 (u_int)psig->action, psig->mask, psig->code); 427 } 428 429 ktrcsw(cs) 430 struct ktr_csw *cs; 431 { 432 (void)printf("%s %s\n", cs->out ? "stop" : "resume", 433 cs->user ? "user" : "kernel"); 434 } 435 436 usage() 437 { 438 (void)fprintf(stderr, 439 "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnis]]\n"); 440 exit(1); 441 } 442