Home | History | Annotate | Line # | Download | only in ktruss
dump.c revision 1.10
      1 /*	$NetBSD: dump.c,v 1.10 2000/10/12 19:02:18 is 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.10 2000/10/12 19:02:18 is 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 #include "setemul.h"
     73 
     74 int timestamp, decimal, fancy = 1, tail, maxdata;
     75 
     76 #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
     77 
     78 #include <sys/syscall.h>
     79 
     80 static 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 
     87 void	dumprecord __P((struct ktr_header *, int, int *, void **, FILE *));
     88 void	dumpheader __P((struct ktr_header *, char *, int, int *));
     89 void	dumpfile __P((char *, int, int));
     90 int	fread_tail __P((char *, int, int, FILE *));
     91 void	ioctldecode __P((u_long));
     92 int	ktrsyscall __P((struct ktr_syscall *, int, char *, int, int *));
     93 void	ktrsysret __P((struct ktr_sysret *, char *, int, int *));
     94 void	ktrnamei __P((char *, int, char *, int, int *));
     95 void	ktremul __P((struct ktr_header *, char *, int, char *, int, int *));
     96 void	ktrgenio __P((struct ktr_genio *, int, char *, int, int *));
     97 void	ktrpsig __P((struct ktr_psig *));
     98 void	ktrcsw __P((struct ktr_csw *));
     99 
    100 #define	KTR_BUFSZ	512
    101 #define	BLEFT	(bufsz - (bp - buff))
    102 
    103 
    104 void
    105 dumprecord(ktr, trpoints, sizep, mp, fp)
    106 	register struct ktr_header *ktr;
    107 	int trpoints;
    108 	int *sizep;
    109 	void **mp;
    110 	FILE *fp;
    111 {
    112 	static void *mcopy = NULL;
    113 	static int linelen = 0, iolinelen = 0;
    114 	char buff[KTR_BUFSZ], iobuff[KTR_BUFSZ], *bp;
    115 	int ktrlen, *lenp;
    116 	void *m;
    117 
    118 	if (!ktr) {
    119 		printf("%s\n", buff);
    120 		if (*iobuff)
    121 			printf("%s\n", iobuff);
    122 		return;
    123 	}
    124 
    125 	if (ktr->ktr_type == KTR_GENIO || ktr->ktr_type == KTR_EMUL) {
    126 		bp = iobuff;
    127 		lenp = &iolinelen;
    128 	} else {
    129 		bp = buff;
    130 		lenp = &linelen;
    131 	}
    132 	if (!mcopy && (trpoints & (1<<ktr->ktr_type)))
    133 		dumpheader(ktr, bp, KTR_BUFSZ, lenp);
    134 
    135 	if ((ktrlen = ktr->ktr_len) < 0)
    136 		errx(1, "bogus length 0x%x", ktrlen);
    137 	m = *mp;
    138 	if (ktrlen >= *sizep) {
    139 		while(ktrlen > *sizep) *sizep *= 2;
    140 		*mp = m = (void *)realloc(m, *sizep);
    141 		if (m == NULL)
    142 			errx(1, "realloc: %s", strerror(ENOMEM));
    143 	}
    144 	if (ktrlen && fread_tail(m, ktrlen, 1, fp) == 0)
    145 		errx(1, "data too short");
    146 	if ((trpoints & (1<<ktr->ktr_type)) == 0)
    147 		return;
    148 
    149 	/* update context to match currently processed record */
    150 	ectx_sanify(ktr->ktr_pid);
    151 
    152 	switch (ktr->ktr_type)
    153 	{
    154 	case KTR_SYSCALL:
    155 		if (ktrsyscall((struct ktr_syscall *)m, 0, bp, KTR_BUFSZ,
    156 			       lenp) == 0) {
    157 			mcopy = (void *)malloc(ktrlen + 1);
    158 			bcopy(m, mcopy, ktrlen);
    159 			return;
    160 		}
    161 		break;
    162 	case KTR_SYSRET:
    163 		ktrsysret((struct ktr_sysret *)m, bp, KTR_BUFSZ, lenp);
    164 		if (*iobuff || iolinelen) {
    165 			fputs(iobuff, stdout);
    166 			*iobuff = '\0';
    167 			iolinelen = 0;
    168 		}
    169 		break;
    170 	case KTR_NAMEI:
    171 		ktrnamei(m, ktrlen, bp, sizeof(buff), lenp);
    172 		if (mcopy) {
    173 			(void) ktrsyscall((struct ktr_syscall *)mcopy, 1, bp,
    174 					  KTR_BUFSZ, lenp);
    175 			free(mcopy);
    176 			mcopy = NULL;
    177 		}
    178 		break;
    179 	case KTR_GENIO:
    180 		ktrgenio((struct ktr_genio *)m, ktrlen, bp, KTR_BUFSZ, lenp);
    181 		break;
    182 	case KTR_PSIG:
    183 		ktrpsig((struct ktr_psig *)m);
    184 		break;
    185 	case KTR_CSW:
    186 		ktrcsw((struct ktr_csw *)m);
    187 		break;
    188 	case KTR_EMUL:
    189 		ktremul(ktr, m, ktrlen, bp, sizeof(buff), lenp);
    190 		break;
    191 	}
    192 
    193 	if (mcopy) {
    194 		free(mcopy);
    195 		mcopy = NULL;
    196 	}
    197 }
    198 
    199 void
    200 dumpfile(file, fd, trpoints)
    201 	char *file;
    202 	int fd;
    203 	int trpoints;
    204 {
    205 	struct ktr_header ktr_header;
    206 	void *m;
    207 	FILE *fp;
    208 	int size;
    209 
    210 	m = (void *)malloc(size = 1024);
    211 	if (m == NULL)
    212 		errx(1, "malloc: %s", strerror(ENOMEM));
    213 	if (!file || !*file) {
    214 		if (!(fp = fdopen(fd, "r")))
    215 			err(1, "fdopen(%d)", fd);
    216 	} else if (!strcmp(file, "-"))
    217 		fp = stdin;
    218 	else if (!(fp = fopen(file, "r")))
    219 		err(1, "%s", file);
    220 
    221 	while (fread_tail((char *)&ktr_header,sizeof(struct ktr_header),1,fp)) {
    222 		dumprecord(&ktr_header, trpoints, &size, &m, fp);
    223 		if (tail)
    224 			(void)fflush(stdout);
    225 	}
    226 	dumprecord(NULL, 0, NULL, NULL, fp);
    227 }
    228 
    229 
    230 int
    231 fread_tail(buf, size, num, fp)
    232 	char *buf;
    233 	int num, size;
    234 	FILE *fp;
    235 {
    236 	int i;
    237 
    238 	while ((i = fread(buf, size, num, fp)) == 0 && tail) {
    239 		(void)sleep(1);
    240 		clearerr(fp);
    241 	}
    242 	return (i);
    243 }
    244 
    245 void
    246 dumpheader(kth, buff, buffsz, lenp)
    247 	struct ktr_header *kth;
    248 	char *buff;
    249 	int buffsz, *lenp;
    250 {
    251 	static struct timeval prevtime;
    252 	char *bp = buff + *lenp;
    253 	struct timeval temp;
    254 
    255 	if (kth->ktr_type == KTR_SYSRET || kth->ktr_type == KTR_GENIO)
    256 			return;
    257 	*lenp = 0;
    258 	(void)snprintf(bp, buffsz - *lenp, "%6d %-8.*s ",
    259 		kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
    260 	*lenp += strlen(bp);
    261 	bp = buff + *lenp;
    262 
    263 	if (timestamp) {
    264 		if (timestamp == 2) {
    265 			timersub(&kth->ktr_time, &prevtime, &temp);
    266 			prevtime = kth->ktr_time;
    267 		} else
    268 			temp = kth->ktr_time;
    269 		(void)snprintf(bp, buffsz - *lenp, "%ld.%06ld ",
    270 		    (long int)temp.tv_sec,
    271 		    (long int)temp.tv_usec);
    272 		*lenp += strlen(bp);
    273 	}
    274 }
    275 
    276 void
    277 ioctldecode(cmd)
    278 	u_long cmd;
    279 {
    280 	char dirbuf[4], *dir = dirbuf;
    281 
    282 	if (cmd & IOC_OUT)
    283 		*dir++ = 'W';
    284 	if (cmd & IOC_IN)
    285 		*dir++ = 'R';
    286 	*dir = '\0';
    287 
    288 	printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx",
    289 	    dirbuf, (int) ((cmd >> 8) & 0xff), cmd & 0xff);
    290 	if ((cmd & IOC_VOID) == 0)
    291 		printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff);
    292 	else
    293 		printf(")");
    294 }
    295 
    296 int
    297 ktrsyscall(ktr, nohdr, buff, bufsz, lenp)
    298 	register struct ktr_syscall *ktr;
    299 	int nohdr, bufsz, *lenp;
    300 	char *buff;
    301 {
    302 	register int argsize = ktr->ktr_argsize;
    303 	register register_t *ap;
    304 	char *bp = buff;
    305 	int eol = 1;
    306 
    307 	if (*lenp < bufsz) {
    308 		bp += *lenp;
    309 		bzero(bp, BLEFT);
    310 	}
    311 	if (!nohdr) {
    312 		if (ktr->ktr_code >= current->nsysnames || ktr->ktr_code < 0)
    313 			(void)snprintf(bp, BLEFT, "[%d]", ktr->ktr_code);
    314 		else
    315 			(void)snprintf(bp, BLEFT,
    316 				       "%s", current->sysnames[ktr->ktr_code]);
    317 		bp += strlen(bp);
    318 	}
    319 	ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
    320 	if (argsize) {
    321 		char *s = "(";
    322 		if (fancy && !nohdr) {
    323 			switch (ktr->ktr_code) {
    324 			/*
    325 			 * All these have a path as the first param.
    326 			 */
    327 			case SYS_open : case SYS_chdir :
    328 			case SYS___stat13 : case SYS_chroot :
    329 			case SYS_execve : case SYS_pathconf :
    330 			case SYS_rmdir : case SYS_rename :
    331 			case SYS_symlink : case SYS_chflags :
    332 			case SYS_link : case SYS_mkdir :
    333 			case SYS_mknod : case SYS_mkfifo :
    334 				if (BLEFT > 1)
    335 					*bp++ = '(';
    336 				eol = 0;
    337 				break;
    338 			case SYS___sigaction14 :
    339 				(void)snprintf(bp, BLEFT, "(%s",
    340 					       signals[(int)*ap].name);
    341 				s = ", ";
    342 				argsize -= sizeof(register_t);
    343 				ap++;
    344 				break;
    345 			case SYS_ioctl :
    346 				if (decimal)
    347 					(void)snprintf(bp, BLEFT, "(%ld",
    348 						       (long)*ap);
    349 				else
    350 					(void)snprintf(bp, BLEFT, "(%#lx",
    351 						       (long)*ap);
    352 				bp += strlen(bp);
    353 				ap++;
    354 				argsize -= sizeof(register_t);
    355 				if ((s = ioctlname(*ap)) != NULL)
    356 					(void)snprintf(bp, BLEFT, ", %s", s);
    357 				else
    358 					ioctldecode(*ap);
    359 				s = ", ";
    360 				ap++;
    361 				argsize -= sizeof(register_t);
    362 				break;
    363 			case SYS_ptrace :
    364 				if (*ap >= 0 && *ap <=
    365 				    sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
    366 					(void)snprintf(bp, BLEFT, "(%s",
    367 						       ptrace_ops[*ap]);
    368 				else
    369 					(void)snprintf(bp, BLEFT, "(%ld",
    370 						       (long)*ap);
    371 				s = ", ";
    372 				ap++;
    373 				argsize -= sizeof(register_t);
    374 				break;
    375 			default :
    376 				break;
    377 			}
    378 			bp += strlen(bp);
    379 		}
    380 		if (eol) {
    381 			while (argsize) {
    382 				if (!nohdr || strcmp(s, "(")) {
    383 					if (decimal)
    384 						(void)snprintf(bp, BLEFT,
    385 							       "%s%ld", s,
    386 							       (long)*ap);
    387 					else
    388 						(void)snprintf(bp, BLEFT,
    389 							       "%s%#lx", s,
    390 							       (long)*ap);
    391 					bp += strlen(bp);
    392 				}
    393 				s = ", ";
    394 				ap++;
    395 				argsize -= sizeof(register_t);
    396 			}
    397 			if (BLEFT > 1)
    398 				*bp++ = ')';
    399 		}
    400 	}
    401 	*bp = '\0';
    402 
    403 	*lenp = bp - buff;
    404 	return eol;
    405 }
    406 
    407 void
    408 ktrsysret(ktr, buff, buffsz, lenp)
    409 	struct ktr_sysret *ktr;
    410 	int buffsz, *lenp;
    411 	char *buff;
    412 {
    413 	register register_t ret = ktr->ktr_retval;
    414 	register int error = ktr->ktr_error;
    415 
    416 	while (*lenp < 50)
    417 		buff[(*lenp)++] = ' ';
    418 	if (error == EJUSTRETURN)
    419 		strcpy(buff + *lenp, " JUSTRETURN");
    420 	else if (error == ERESTART)
    421 		strcpy(buff + *lenp, " RESTART");
    422 	else if (error) {
    423 		sprintf(buff + *lenp, " Err#%d", error);
    424 		if (error < MAXERRNOS && error >= -2)
    425 			sprintf(buff + strlen(buff), " %s",errnos[error].name);
    426 	} else
    427 		sprintf(buff + *lenp, " = %ld", (long)ret);
    428 	strcat(buff + *lenp, "\n");
    429 	*lenp = 0;
    430 	fputs(buff, stdout);
    431 	*buff = '\0';
    432 }
    433 
    434 void
    435 ktrnamei(cp, len, buff, buffsz, lenp)
    436 	int buffsz, *lenp;
    437 	char *cp, *buff;
    438 {
    439 	snprintf(buff + *lenp, buffsz - *lenp, "\"%.*s\"", len, cp);
    440 	*lenp += strlen(buff + *lenp);
    441 }
    442 
    443 void
    444 ktremul(ktr_header, cp, len, buff, buffsz, lenp)
    445 	struct ktr_header *ktr_header;
    446 	int buffsz, *lenp;
    447 	char *cp, *buff;
    448 {
    449 	bzero(buff + *lenp, buffsz - *lenp);
    450 	cp[len] = '\0';
    451 	snprintf(buff + *lenp, buffsz - *lenp, "emul(%s)\n", cp);
    452 	*lenp += strlen(buff + *lenp);
    453 
    454 	setemul(cp, ktr_header->ktr_pid, 1);
    455 }
    456 
    457 void
    458 ktrgenio(ktr, len, buff, bufsz, lenp)
    459 	struct ktr_genio *ktr;
    460 	int len;
    461 	char *buff;
    462 	int bufsz, *lenp;
    463 {
    464 	static int screenwidth = 0;
    465 	register int datalen = len - sizeof (struct ktr_genio);
    466 	register char *dp = (char *)ktr + sizeof (struct ktr_genio);
    467 	register int col = 0;
    468 	register int width;
    469 	char visbuf[5], *bp = buff;
    470 
    471 	if (*lenp < bufsz) {
    472 		bp += *lenp;
    473 		bzero(buff, BLEFT);
    474 	} else
    475 		*lenp = 0;
    476 	if (screenwidth == 0) {
    477 		struct winsize ws;
    478 
    479 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
    480 		    ws.ws_col > 8)
    481 			screenwidth = ws.ws_col;
    482 		else
    483 			screenwidth = 80;
    484 	}
    485 
    486 	if (maxdata && datalen > maxdata)
    487 		datalen = maxdata;
    488 	strcpy(bp, "       \"");
    489 	col = *lenp;
    490 	col += 8;
    491 	bp += 8;
    492 	for (; datalen > 0; datalen--, dp++) {
    493 		(void) vis(visbuf, *dp, VIS_NL|VIS_TAB|VIS_CSTYLE, *(dp+1));
    494 		width = strlen(visbuf);
    495 		visbuf[4] = '\0';
    496 		if (col + width + 2 >= screenwidth)
    497 			break;
    498 		col += width;
    499 		strncpy(bp, visbuf, width);
    500 		bp += width;
    501 		if (col + 2 >= screenwidth)
    502 			break;
    503 	}
    504 	strcpy(bp, "\"\n");
    505 	*lenp = col + 2;
    506 }
    507 
    508 void
    509 ktrpsig(psig)
    510 	struct ktr_psig *psig;
    511 {
    512 	(void)printf("SIG%s ", sys_signame[psig->signo]);
    513 	if (psig->action == SIG_DFL)
    514 		(void)printf("SIG_DFL\n");
    515 	else {
    516 		(void)printf("caught handler=0x%lx mask=0x%lx code=0x%x\n",
    517 		    (u_long)psig->action, (unsigned long)psig->mask.__bits[0],
    518 		    psig->code);
    519 	}
    520 }
    521 
    522 void
    523 ktrcsw(cs)
    524 	struct ktr_csw *cs;
    525 {
    526 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
    527 	    cs->user ? "user" : "kernel");
    528 }
    529