Home | History | Annotate | Line # | Download | only in ktruss
dump.c revision 1.6
      1 /*	$NetBSD: dump.c,v 1.6 1999/08/06 00:11:03 thorpej 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 #endif
     46 __RCSID("$NetBSD: dump.c,v 1.6 1999/08/06 00:11:03 thorpej Exp $");
     47 #endif /* not lint */
     48 
     49 #include <sys/param.h>
     50 #define	_KERNEL
     51 #include <sys/errno.h>
     52 #undef _KERNEL
     53 #include <sys/time.h>
     54 #include <sys/uio.h>
     55 #include <sys/ktrace.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/ptrace.h>
     58 #define _KERNEL
     59 #include <sys/errno.h>
     60 #undef _KERNEL
     61 
     62 #include <err.h>
     63 #include <signal.h>
     64 #include <stdio.h>
     65 #include <stdlib.h>
     66 #include <string.h>
     67 #include <unistd.h>
     68 #include <vis.h>
     69 
     70 #include "ktrace.h"
     71 #include "misc.h"
     72 
     73 int timestamp, decimal, fancy = 1, tail, maxdata;
     74 
     75 #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
     76 
     77 #include <sys/syscall.h>
     78 
     79 #include "../../sys/compat/hpux/hpux_syscall.h"
     80 #include "../../sys/compat/ibcs2/ibcs2_syscall.h"
     81 #include "../../sys/compat/linux/linux_syscall.h"
     82 #include "../../sys/compat/osf1/osf1_syscall.h"
     83 #include "../../sys/compat/sunos/sunos_syscall.h"
     84 #include "../../sys/compat/svr4/svr4_syscall.h"
     85 #include "../../sys/compat/ultrix/ultrix_syscall.h"
     86 
     87 #define KTRACE
     88 #include "../../sys/kern/syscalls.c"
     89 
     90 #include "../../sys/compat/hpux/hpux_syscalls.c"
     91 #include "../../sys/compat/ibcs2/ibcs2_syscalls.c"
     92 #include "../../sys/compat/linux/linux_syscalls.c"
     93 #include "../../sys/compat/osf1/osf1_syscalls.c"
     94 #include "../../sys/compat/sunos/sunos_syscalls.c"
     95 #include "../../sys/compat/svr4/svr4_syscalls.c"
     96 #include "../../sys/compat/ultrix/ultrix_syscalls.c"
     97 #undef KTRACE
     98 
     99 struct emulation {
    100 	char *name;		/* Emulation name */
    101 	char **sysnames;	/* Array of system call names */
    102 	int  nsysnames;		/* Number of */
    103 };
    104 
    105 static struct emulation emulations[] = {
    106 	{ "netbsd",	     syscallnames,        SYS_MAXSYSCALL },
    107 	{ "hpux",	hpux_syscallnames,   HPUX_SYS_MAXSYSCALL },
    108 	{ "ibcs2",     ibcs2_syscallnames,  IBCS2_SYS_MAXSYSCALL },
    109 	{ "linux",     linux_syscallnames,  LINUX_SYS_MAXSYSCALL },
    110 	{ "osf1",       osf1_syscallnames,   OSF1_SYS_MAXSYSCALL },
    111 	{ "sunos",     sunos_syscallnames,  SUNOS_SYS_MAXSYSCALL },
    112 	{ "svr4",       svr4_syscallnames,   SVR4_SYS_MAXSYSCALL },
    113 	{ "ultrix",   ultrix_syscallnames, ULTRIX_SYS_MAXSYSCALL },
    114 	{ NULL,			     NULL,		    NULL }
    115 };
    116 
    117 struct emulation *current = &emulations[0];	/* NetBSD */
    118 
    119 
    120 static char *ptrace_ops[] = {
    121 	"PT_TRACE_ME",	"PT_READ_I",	"PT_READ_D",	"PT_READ_U",
    122 	"PT_WRITE_I",	"PT_WRITE_D",	"PT_WRITE_U",	"PT_CONTINUE",
    123 	"PT_KILL",	"PT_ATTACH",	"PT_DETACH",
    124 };
    125 
    126 
    127 void	dumprecord __P((struct ktr_header *, int, int *, void **, FILE *));
    128 void	dumpheader __P((struct ktr_header *, char *, int, int *));
    129 void	dumpfile __P((char *, int, int));
    130 int	fread_tail __P((char *, int, int, FILE *));
    131 void	ioctldecode __P((u_long));
    132 int	ktrsyscall __P((struct ktr_syscall *, int, char *, int, int *));
    133 void	ktrsysret __P((struct ktr_sysret *, char *, int, int *));
    134 void	ktrnamei __P((char *, int, char *, int, int *));
    135 void	ktremul __P((char *, int, char *, int, int *));
    136 void	ktrgenio __P((struct ktr_genio *, int, char *, int, int *));
    137 void	ktrpsig __P((struct ktr_psig *));
    138 void	ktrcsw __P((struct ktr_csw *));
    139 void	setemul __P((char *));
    140 
    141 #define	KTR_BUFSZ	512
    142 #define	BLEFT	(bufsz - (bp - buff))
    143 
    144 
    145 void
    146 dumprecord(ktr, trpoints, sizep, mp, fp)
    147 	register struct ktr_header *ktr;
    148 	int trpoints;
    149 	int *sizep;
    150 	void **mp;
    151 	FILE *fp;
    152 {
    153 	static void *mcopy = NULL;
    154 	static int linelen = 0, iolinelen = 0;
    155 	char buff[KTR_BUFSZ], iobuff[KTR_BUFSZ], *bp;
    156 	int ktrlen, *lenp;
    157 	void *m;
    158 
    159 	if (!ktr) {
    160 		printf("%s\n", buff);
    161 		if (*iobuff)
    162 			printf("%s\n", iobuff);
    163 		return;
    164 	}
    165 
    166 	if (ktr->ktr_type == KTR_GENIO || ktr->ktr_type == KTR_EMUL) {
    167 		bp = iobuff;
    168 		lenp = &iolinelen;
    169 	} else {
    170 		bp = buff;
    171 		lenp = &linelen;
    172 	}
    173 	if (!mcopy && (trpoints & (1<<ktr->ktr_type)))
    174 		dumpheader(ktr, bp, KTR_BUFSZ, lenp);
    175 
    176 	if ((ktrlen = ktr->ktr_len) < 0)
    177 		errx(1, "bogus length 0x%x", ktrlen);
    178 	m = *mp;
    179 	if (ktrlen >= *sizep) {
    180 		*mp = m = (void *)realloc(m, *sizep = ktrlen+1);
    181 		if (m == NULL)
    182 			errx(1, "%s", strerror(ENOMEM));
    183 	}
    184 	if (ktrlen && fread_tail(m, ktrlen, 1, fp) == 0)
    185 		errx(1, "data too short");
    186 	if ((trpoints & (1<<ktr->ktr_type)) == 0)
    187 		return;
    188 	switch (ktr->ktr_type)
    189 	{
    190 	case KTR_SYSCALL:
    191 		if (ktrsyscall((struct ktr_syscall *)m, 0, bp, KTR_BUFSZ,
    192 			       lenp) == 0) {
    193 			mcopy = (void *)malloc(ktrlen + 1);
    194 			bcopy(m, mcopy, ktrlen);
    195 			return;
    196 		}
    197 		break;
    198 	case KTR_SYSRET:
    199 		ktrsysret((struct ktr_sysret *)m, bp, KTR_BUFSZ, lenp);
    200 		if (*iobuff || iolinelen) {
    201 			fputs(iobuff, stdout);
    202 			*iobuff = '\0';
    203 			iolinelen = 0;
    204 		}
    205 		break;
    206 	case KTR_NAMEI:
    207 		ktrnamei(m, ktrlen, bp, sizeof(buff), lenp);
    208 		if (mcopy) {
    209 			(void) ktrsyscall((struct ktr_syscall *)mcopy, 1, bp,
    210 					  KTR_BUFSZ, lenp);
    211 			free(mcopy);
    212 			mcopy = NULL;
    213 		}
    214 		break;
    215 	case KTR_GENIO:
    216 		ktrgenio((struct ktr_genio *)m, ktrlen, bp, KTR_BUFSZ, lenp);
    217 		break;
    218 	case KTR_PSIG:
    219 		ktrpsig((struct ktr_psig *)m);
    220 		break;
    221 	case KTR_CSW:
    222 		ktrcsw((struct ktr_csw *)m);
    223 		break;
    224 	case KTR_EMUL:
    225 		ktremul(m, ktrlen, bp, sizeof(buff), lenp);
    226 		break;
    227 	}
    228 
    229 	if (mcopy)
    230 		free(mcopy);
    231 }
    232 
    233 void
    234 dumpfile(file, fd, trpoints)
    235 	char *file;
    236 	int fd;
    237 	int trpoints;
    238 {
    239 	struct ktr_header ktr_header;
    240 	void *m;
    241 	FILE *fp;
    242 	int size;
    243 
    244 	m = (void *)malloc(size = 1025);
    245 	if (m == NULL)
    246 		errx(1, "%s", strerror(ENOMEM));
    247 	if (!file || !*file) {
    248 		if (!(fp = fdopen(fd, "r")))
    249 			err(1, "fdopen(%d)", fd);
    250 	} else if (!strcmp(file, "-"))
    251 		fp = stdin;
    252 	else if (!(fp = fopen(file, "r")))
    253 		err(1, "%s", file);
    254 
    255 	while (fread_tail((char *)&ktr_header,sizeof(struct ktr_header),1,fp)) {
    256 		dumprecord(&ktr_header, trpoints, &size, &m, fp);
    257 		if (tail)
    258 			(void)fflush(stdout);
    259 	}
    260 	dumprecord(NULL, 0, NULL, NULL, fp);
    261 }
    262 
    263 
    264 int
    265 fread_tail(buf, size, num, fp)
    266 	char *buf;
    267 	int num, size;
    268 	FILE *fp;
    269 {
    270 	int i;
    271 
    272 	while ((i = fread(buf, size, num, fp)) == 0 && tail) {
    273 		(void)sleep(1);
    274 		clearerr(fp);
    275 	}
    276 	return (i);
    277 }
    278 
    279 void
    280 dumpheader(kth, buff, buffsz, lenp)
    281 	struct ktr_header *kth;
    282 	char *buff;
    283 	int buffsz, *lenp;
    284 {
    285 	static struct timeval prevtime;
    286 	char *bp = buff + *lenp;
    287 	struct timeval temp;
    288 
    289 	if (kth->ktr_type == KTR_SYSRET || kth->ktr_type == KTR_GENIO)
    290 			return;
    291 	*lenp = 0;
    292 	(void)snprintf(bp, buffsz - *lenp, "%6d %-8.*s ",
    293 		kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
    294 	*lenp += strlen(bp);
    295 	bp = buff + *lenp;
    296 
    297 	if (timestamp) {
    298 		if (timestamp == 2) {
    299 			timersub(&kth->ktr_time, &prevtime, &temp);
    300 			prevtime = kth->ktr_time;
    301 		} else
    302 			temp = kth->ktr_time;
    303 		(void)snprintf(bp, buffsz - *lenp, "%ld.%06ld ", temp.tv_sec,
    304 			temp.tv_usec);
    305 		*lenp += strlen(bp);
    306 	}
    307 }
    308 
    309 void
    310 ioctldecode(cmd)
    311 	u_long cmd;
    312 {
    313 	char dirbuf[4], *dir = dirbuf;
    314 
    315 	if (cmd & IOC_OUT)
    316 		*dir++ = 'W';
    317 	if (cmd & IOC_IN)
    318 		*dir++ = 'R';
    319 	*dir = '\0';
    320 
    321 	printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx",
    322 	    dirbuf, (cmd >> 8) & 0xff, cmd & 0xff);
    323 	if ((cmd & IOC_VOID) == 0)
    324 		printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff);
    325 	else
    326 		printf(")");
    327 }
    328 
    329 int
    330 ktrsyscall(ktr, nohdr, buff, bufsz, lenp)
    331 	register struct ktr_syscall *ktr;
    332 	int nohdr, bufsz, *lenp;
    333 	char *buff;
    334 {
    335 	register int argsize = ktr->ktr_argsize;
    336 	register register_t *ap;
    337 	char *bp = buff;
    338 	int eol = 1;
    339 
    340 	if (*lenp < bufsz) {
    341 		bp += *lenp;
    342 		bzero(bp, BLEFT);
    343 	}
    344 	if (!nohdr) {
    345 		if (ktr->ktr_code >= current->nsysnames || ktr->ktr_code < 0)
    346 			(void)snprintf(bp, BLEFT, "[%d]", ktr->ktr_code);
    347 		else
    348 			(void)snprintf(bp, BLEFT,
    349 				       "%s", current->sysnames[ktr->ktr_code]);
    350 		bp += strlen(bp);
    351 	}
    352 	ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
    353 	if (argsize) {
    354 		char *s = "(";
    355 		if (fancy && !nohdr) {
    356 			switch (ktr->ktr_code) {
    357 			/*
    358 			 * All these have a path as the first param.
    359 			 */
    360 			case SYS_open : case SYS_chdir :
    361 			case SYS___stat13 : case SYS_chroot :
    362 			case SYS_execve : case SYS_pathconf :
    363 			case SYS_rmdir : case SYS_rename :
    364 			case SYS_symlink : case SYS_chflags :
    365 			case SYS_link : case SYS_mkdir :
    366 			case SYS_mknod : case SYS_mkfifo :
    367 				if (BLEFT > 1)
    368 					*bp++ = '(';
    369 				eol = 0;
    370 				break;
    371 			case SYS___sigaction14 :
    372 				(void)snprintf(bp, BLEFT, "(%s",
    373 					       signals[(int)*ap].name);
    374 				s = ", ";
    375 				argsize -= sizeof(register_t);
    376 				ap++;
    377 				break;
    378 			case SYS_ioctl :
    379 				if (decimal)
    380 					(void)snprintf(bp, BLEFT, "(%ld",
    381 						       (long)*ap);
    382 				else
    383 					(void)snprintf(bp, BLEFT, "(%#lx",
    384 						       (long)*ap);
    385 				bp += strlen(bp);
    386 				ap++;
    387 				argsize -= sizeof(register_t);
    388 				if ((s = ioctlname(*ap)) != NULL)
    389 					(void)snprintf(bp, BLEFT, ", %s", s);
    390 				else
    391 					ioctldecode(*ap);
    392 				s = ", ";
    393 				ap++;
    394 				argsize -= sizeof(register_t);
    395 				break;
    396 			case SYS_ptrace :
    397 				if (*ap >= 0 && *ap <=
    398 				    sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
    399 					(void)snprintf(bp, BLEFT, "(%s",
    400 						       ptrace_ops[*ap]);
    401 				else
    402 					(void)snprintf(bp, BLEFT, "(%ld",
    403 						       (long)*ap);
    404 				s = ", ";
    405 				ap++;
    406 				argsize -= sizeof(register_t);
    407 				break;
    408 			default :
    409 				break;
    410 			}
    411 			bp += strlen(bp);
    412 		}
    413 		if (eol) {
    414 			while (argsize) {
    415 				if (!nohdr || strcmp(s, "(")) {
    416 					if (decimal)
    417 						(void)snprintf(bp, BLEFT,
    418 							       "%s%ld", s,
    419 							       (long)*ap);
    420 					else
    421 						(void)snprintf(bp, BLEFT,
    422 							       "%s%#lx", s,
    423 							       (long)*ap);
    424 					bp += strlen(bp);
    425 				}
    426 				s = ", ";
    427 				ap++;
    428 				argsize -= sizeof(register_t);
    429 			}
    430 			if (BLEFT > 1)
    431 				*bp++ = ')';
    432 		}
    433 	}
    434 	*bp = '\0';
    435 
    436 	*lenp = bp - buff;
    437 	return eol;
    438 }
    439 
    440 void
    441 ktrsysret(ktr, buff, buffsz, lenp)
    442 	struct ktr_sysret *ktr;
    443 	int buffsz, *lenp;
    444 	char *buff;
    445 {
    446 	register register_t ret = ktr->ktr_retval;
    447 	register int error = ktr->ktr_error;
    448 
    449 	while (*lenp < 50)
    450 		buff[(*lenp)++] = ' ';
    451 	if (error == EJUSTRETURN)
    452 		strcpy(buff + *lenp, " JUSTRETURN");
    453 	else if (error == ERESTART)
    454 		strcpy(buff + *lenp, " RESTART");
    455 	else if (error) {
    456 		sprintf(buff + *lenp, " Err#%d", error);
    457 		if (error < MAXERRNOS && error >= -2)
    458 			sprintf(buff + strlen(buff), " %s",errnos[error].name);
    459 	} else
    460 		sprintf(buff + *lenp, " = %ld", (long)ret);
    461 	strcat(buff + *lenp, "\n");
    462 	*lenp = 0;
    463 	fputs(buff, stdout);
    464 	*buff = '\0';
    465 }
    466 
    467 void
    468 ktrnamei(cp, len, buff, buffsz, lenp)
    469 	int buffsz, *lenp;
    470 	char *cp, *buff;
    471 {
    472 	snprintf(buff + *lenp, buffsz - *lenp, "\"%.*s\"", len, cp);
    473 	*lenp += strlen(buff + *lenp);
    474 }
    475 
    476 void
    477 ktremul(cp, len, buff, buffsz, lenp)
    478 	int buffsz, *lenp;
    479 	char *cp, *buff;
    480 {
    481 	bzero(buff + *lenp, buffsz - *lenp);
    482 	cp[len] = '\0';
    483 	snprintf(buff + *lenp, buffsz - *lenp, "emul(%s)\n", cp);
    484 	*lenp += strlen(buff + *lenp);
    485 
    486 	setemul(cp);
    487 }
    488 
    489 void
    490 ktrgenio(ktr, len, buff, bufsz, lenp)
    491 	struct ktr_genio *ktr;
    492 	int len;
    493 	char *buff;
    494 	int bufsz, *lenp;
    495 {
    496 	static int screenwidth = 0;
    497 	register int datalen = len - sizeof (struct ktr_genio);
    498 	register char *dp = (char *)ktr + sizeof (struct ktr_genio);
    499 	register int col = 0;
    500 	register int width;
    501 	char visbuf[5], *bp = buff;
    502 
    503 	if (*lenp < bufsz) {
    504 		bp += *lenp;
    505 		bzero(buff, BLEFT);
    506 	} else
    507 		*lenp = 0;
    508 	if (screenwidth == 0) {
    509 		struct winsize ws;
    510 
    511 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
    512 		    ws.ws_col > 8)
    513 			screenwidth = ws.ws_col;
    514 		else
    515 			screenwidth = 80;
    516 	}
    517 
    518 	if (maxdata && datalen > maxdata)
    519 		datalen = maxdata;
    520 	strcpy(bp, "       \"");
    521 	col = *lenp;
    522 	col += 8;
    523 	bp += 8;
    524 	for (; datalen > 0; datalen--, dp++) {
    525 		(void) vis(visbuf, *dp, VIS_NL|VIS_TAB|VIS_CSTYLE, *(dp+1));
    526 		width = strlen(visbuf);
    527 		visbuf[4] = '\0';
    528 		if (col + width + 2 >= screenwidth)
    529 			break;
    530 		col += width;
    531 		strncpy(bp, visbuf, width);
    532 		bp += width;
    533 		if (col + 2 >= screenwidth)
    534 			break;
    535 	}
    536 	strcpy(bp, "\"\n");
    537 	*lenp = col + 2;
    538 }
    539 
    540 void
    541 ktrpsig(psig)
    542 	struct ktr_psig *psig;
    543 {
    544 	u_long msk;
    545 
    546 	(void)printf("SIG%s ", sys_signame[psig->signo]);
    547 	if (psig->action == SIG_DFL)
    548 		(void)printf("SIG_DFL\n");
    549 	else {
    550 		msk = 0;
    551 		bcopy((char *)&psig->mask, (char *)&msk, sizeof(psig->mask));
    552 		(void)printf("caught handler=0x%lx mask=0x%lx code=0x%x\n",
    553 		    (u_long)psig->action, msk, psig->code);
    554 	}
    555 }
    556 
    557 void
    558 ktrcsw(cs)
    559 	struct ktr_csw *cs;
    560 {
    561 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
    562 	    cs->user ? "user" : "kernel");
    563 }
    564 
    565 void
    566 setemul(name)
    567 	char *name;
    568 {
    569 	int i;
    570 	for (i = 0; emulations[i].name != NULL; i++)
    571 		if (strcmp(emulations[i].name, name) == 0) {
    572 			current = &emulations[i];
    573 			return;
    574 		}
    575 	warnx("Emulation `%s' unknown", name);
    576 }
    577