Home | History | Annotate | Line # | Download | only in ktruss
dump.c revision 1.46
      1 /*	$NetBSD: dump.c,v 1.46 2020/01/04 22:22:34 mlelstv 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
     35  The Regents of the University of California.  All rights reserved.");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)kdump.c	8.4 (Berkeley) 4/28/95";
     41 #endif
     42 __RCSID("$NetBSD: dump.c,v 1.46 2020/01/04 22:22:34 mlelstv Exp $");
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #define	_KERNEL
     47 #include <sys/errno.h>
     48 #undef _KERNEL
     49 #include <sys/ioctl.h>
     50 #include <sys/time.h>
     51 #include <sys/uio.h>
     52 #include <sys/ktrace.h>
     53 #include <sys/ptrace.h>
     54 #include <sys/queue.h>
     55 
     56 #include <err.h>
     57 #include <signal.h>
     58 #include <stdarg.h>
     59 #include <stddef.h>
     60 #include <stdio.h>
     61 #include <stdlib.h>
     62 #include <string.h>
     63 #include <unistd.h>
     64 #include <vis.h>
     65 
     66 #include "ktrace.h"
     67 #include "misc.h"
     68 #include "setemul.h"
     69 
     70 int timestamp, decimal, fancy = 1, tail, maxdata;
     71 
     72 int width;			/* Keep track of current columns. */
     73 
     74 #include <sys/syscall.h>
     75 
     76 static const char *const ptrace_ops[] = {
     77 	"PT_TRACE_ME",	"PT_READ_I",	"PT_READ_D",	"PT_READ_U",
     78 	"PT_WRITE_I",	"PT_WRITE_D",	"PT_WRITE_U",	"PT_CONTINUE",
     79 	"PT_KILL",	"PT_ATTACH",	"PT_DETACH",
     80 };
     81 
     82 struct ktr_entry {
     83 	TAILQ_ENTRY(ktr_entry) kte_list;
     84 	struct ktr_header kte_kth;
     85 };
     86 
     87 TAILQ_HEAD(kteq, ktr_entry) ktependq = TAILQ_HEAD_INITIALIZER(ktependq);
     88 
     89 void	argprint(const char *, register_t **, int *);
     90 void	dumpheader(struct ktr_header *);
     91 int	dumprecord(int, FILE *);
     92 void	flushpendq(struct ktr_entry *);
     93 int	fread_tail(void *, int, int, FILE *);
     94 void	genioprint(struct ktr_header *);
     95 struct ktr_entry *
     96 	getpendq(struct ktr_header *, int, struct kteq *);
     97 struct ktr_entry *
     98 	getrecord(FILE *);
     99 void	indent(int);
    100 void	ioctldecode(u_long);
    101 void	ktrcsw(struct ktr_entry *);
    102 void	ktremul(struct ktr_entry *);
    103 void	ktrgenio(struct ktr_entry *);
    104 void	ktrnamei(struct ktr_entry *);
    105 void	ktrpsig(struct ktr_entry *);
    106 void	ktrsyscall(struct ktr_entry *);
    107 void	ktrsysret(struct ktr_entry *);
    108 void	nameiargprint(const char *, struct ktr_header *, register_t **, int *);
    109 void	nameiprint(struct ktr_header *);
    110 void	newline(void);
    111 void	putpendq(struct ktr_entry *);
    112 void	syscallnameprint(int);
    113 void	syscallprint(struct ktr_header *);
    114 void	sysretprint(struct ktr_header *);
    115 int	xwprintf(const char *, ...) __printflike(1, 2);
    116 void	*xrealloc(void *, size_t *, size_t);
    117 
    118 int
    119 xwprintf(const char *fmt, ...)
    120 {
    121 	va_list ap;
    122 	int w;
    123 
    124 	va_start(ap, fmt);
    125 	w = vprintf(fmt, ap);
    126 	if (w == -1)
    127 		warn("vprintf");
    128 	else
    129 		width += w;
    130 	va_end(ap);
    131 	return (w);
    132 }
    133 
    134 void
    135 newline(void)
    136 {
    137 
    138 	if (width > 0) {
    139 		printf("\n");
    140 		width = 0;
    141 	}
    142 }
    143 
    144 void
    145 indent(int col)
    146 {
    147 
    148 	while (width < col)
    149 		if (xwprintf(" ") < 0)
    150 			break;
    151 }
    152 
    153 void *
    154 xrealloc(void *p, size_t *siz, size_t req)
    155 {
    156 
    157 	if (*siz < req) {
    158 		if (*siz == 0)
    159 			*siz = 1;
    160 		while (*siz < req)
    161 			*siz <<= 1;
    162 		p = realloc(p, *siz);
    163 		if (p == NULL)
    164 			err(EXIT_FAILURE, "realloc: %lu bytes",
    165 			    (u_long)*siz);
    166 	}
    167 	return (p);
    168 }
    169 
    170 struct ktr_entry *
    171 getrecord(FILE *fp)
    172 {
    173 	struct ktr_entry *kte;
    174 	struct ktr_header *kth;
    175 	char *cp;
    176 	size_t siz, len;
    177 
    178 	siz = 0;
    179 	kte = xrealloc(NULL, &siz, sizeof(struct ktr_entry));
    180 	kth = &kte->kte_kth;
    181 	if (fread_tail(kth, sizeof(struct ktr_header), 1, fp) == 0) {
    182 		free(kte);
    183 		return (NULL);
    184 	}
    185 
    186 	if (kth->ktr_len < 0)
    187 		errx(EXIT_FAILURE, "bogus length 0x%x", kth->ktr_len);
    188 	len = kth->ktr_len;
    189 	if (len > 0) {
    190 		/* + 1 to ensure room for NUL terminate */
    191 		kte = xrealloc(kte, &siz, sizeof(struct ktr_entry) + len + 1);
    192 		if (fread_tail(cp = (char *)(&kte->kte_kth + 1),
    193 		    len, 1, fp) == 0)
    194 			errx(EXIT_FAILURE, "data too short");
    195 		cp[len] = 0;
    196 	}
    197 
    198 	return (kte);
    199 }
    200 
    201 #define	KTE_TYPE(kte)		((kte)->kte_kth.ktr_type)
    202 #define	KTE_PID(kte)		((kte)->kte_kth.ktr_pid)
    203 #define	KTE_LID(kte)		((kte)->kte_kth.ktr_lid)
    204 #define	KTE_MATCH(kte, type, pid, lid)				\
    205 	(KTE_TYPE(kte) == (type) && KTE_PID(kte) == (pid) &&	\
    206 	KTE_LID(kte) == (lid))
    207 
    208 void
    209 putpendq(struct ktr_entry *kte)
    210 {
    211 
    212 	TAILQ_INSERT_TAIL(&ktependq, kte, kte_list);
    213 }
    214 
    215 void
    216 flushpendq(struct ktr_entry *us)
    217 {
    218 	struct ktr_entry *kte, *kte_next;
    219 	int pid = KTE_PID(us), lid = KTE_LID(us);
    220 
    221 	for (kte = TAILQ_FIRST(&ktependq); kte != NULL; kte = kte_next) {
    222 		kte_next = TAILQ_NEXT(kte, kte_list);
    223 		if (KTE_PID(kte) == pid || KTE_LID(kte) == lid) {
    224 			TAILQ_REMOVE(&ktependq, kte, kte_list);
    225 			free(kte);
    226 		}
    227 	}
    228 }
    229 
    230 struct ktr_entry *
    231 getpendq(struct ktr_header *us, int type, struct kteq *kteq)
    232 {
    233 	struct ktr_entry *kte, *kte_next;
    234 	int pid = us->ktr_pid, lid = us->ktr_lid;
    235 
    236 	if (kteq != NULL)
    237 		TAILQ_INIT(kteq);
    238 	for (kte = TAILQ_FIRST(&ktependq); kte != NULL; kte = kte_next) {
    239 		kte_next = TAILQ_NEXT(kte, kte_list);
    240 		if (KTE_MATCH(kte, type, pid, lid)) {
    241 			TAILQ_REMOVE(&ktependq, kte, kte_list);
    242 			if (kteq != NULL)
    243 				TAILQ_INSERT_TAIL(kteq, kte, kte_list);
    244 			else
    245 				break;
    246 		}
    247 	}
    248 
    249 	return (kteq ? TAILQ_FIRST(kteq) : kte);
    250 }
    251 
    252 int
    253 dumprecord(int trpoints, FILE *fp)
    254 {
    255 	struct ktr_entry *kte;
    256 	struct ktr_header *kth;
    257 
    258 	kte = getrecord(fp);
    259 	if (kte == NULL)
    260 		return (0);
    261 
    262 	kth = &kte->kte_kth;
    263 	if ((trpoints & (1 << kth->ktr_type)) == 0) {
    264 		free(kte);
    265 		goto out;
    266 	}
    267 
    268 	/* Update context to match currently processed record. */
    269 	ectx_sanify(kth->ktr_pid);
    270 
    271 	switch (kth->ktr_type) {
    272 	case KTR_SYSCALL:
    273 		ktrsyscall(kte);
    274 		break;
    275 	case KTR_SYSRET:
    276 		ktrsysret(kte);
    277 		break;
    278 	case KTR_NAMEI:
    279 		putpendq(kte);
    280 		break;
    281 	case KTR_GENIO:
    282 		putpendq(kte);
    283 		break;
    284 	case KTR_PSIG:
    285 		ktrpsig(kte);
    286 		break;
    287 	case KTR_CSW:
    288 		ktrcsw(kte);
    289 		break;
    290 	case KTR_EMUL:
    291 		putpendq(kte);
    292 		break;
    293 	default:
    294 		/*
    295 		 * XXX: Other types added recently.
    296 		 */
    297 		free(kte);
    298 		break;
    299 	}
    300 	newline();
    301 
    302 out:
    303 	return (1);
    304 }
    305 
    306 void
    307 dumpfile(const char *file, int fd, int trpoints)
    308 {
    309 	FILE *fp;
    310 
    311 	if (file == NULL || *file == 0) {
    312 		if ((fp = fdopen(fd, "r")) == NULL)
    313 			err(EXIT_FAILURE, "fdopen(%d)", fd);
    314 	} else if (strcmp(file, "-") == 0)
    315 		fp = stdin;
    316 	else if ((fp = fopen(file, "r")) == NULL)
    317 		err(EXIT_FAILURE, "fopen: %s", file);
    318 
    319 	for (width = 0; dumprecord(trpoints, fp) != 0;)
    320 		if (tail)
    321 			(void)fflush(stdout);
    322 
    323 	newline();
    324 
    325 	/*
    326 	 * XXX: Dump pending KTR_SYSCALL if any?
    327 	 */
    328 }
    329 
    330 int
    331 fread_tail(void *buf, int size, int num, FILE *fp)
    332 {
    333 	int i;
    334 
    335 	while ((i = fread(buf, size, num, fp)) == 0 && tail) {
    336 		(void)sleep(1);
    337 		clearerr(fp);
    338 	}
    339 	return (i);
    340 }
    341 
    342 void
    343 dumpheader(struct ktr_header *kth)
    344 {
    345 	union timeholder {
    346 		struct timeval tv;
    347 		struct timespec ts;
    348 	};
    349 	static union timeholder prevtime;
    350 	union timeholder temp;
    351 
    352 	temp.tv.tv_sec = temp.tv.tv_usec = 0;
    353 	xwprintf("%6d ", kth->ktr_pid);
    354 	if (kth->ktr_version > KTRFAC_VERSION(KTRFACv0))
    355 		xwprintf("%6d ", kth->ktr_lid);
    356 	xwprintf("%-8.*s ", MAXCOMLEN, kth->ktr_comm);
    357 	if (timestamp) {
    358 		if (timestamp == 2) {
    359 			switch (kth->ktr_version) {
    360 			case KTRFAC_VERSION(KTRFACv0):
    361 				if (prevtime.tv.tv_sec == 0)
    362 					temp.tv.tv_sec = temp.tv.tv_usec = 0;
    363 				else
    364 					timersub(&kth->ktr_otv,
    365 					    &prevtime.tv, &temp.tv);
    366 				prevtime.tv.tv_sec = kth->ktr_otv.tv_sec;
    367 				prevtime.tv.tv_usec = kth->ktr_otv.tv_usec;
    368 				break;
    369 
    370 			case KTRFAC_VERSION(KTRFACv1):
    371 				if (prevtime.ts.tv_sec == 0)
    372 					temp.ts.tv_sec = temp.ts.tv_nsec = 0;
    373 				else
    374 					timespecsub(&kth->ktr_time,
    375 					    &prevtime.ts, &temp.ts);
    376 				prevtime.ts.tv_sec = kth->ktr_ots.tv_sec;
    377 				prevtime.ts.tv_nsec = kth->ktr_ots.tv_nsec;
    378 				break;
    379 
    380 			case KTRFAC_VERSION(KTRFACv2):
    381 				if (prevtime.ts.tv_sec == 0)
    382 					temp.ts.tv_sec = temp.ts.tv_nsec = 0;
    383 				else
    384 					timespecsub(&kth->ktr_time,
    385 					    &prevtime.ts, &temp.ts);
    386 				prevtime.ts.tv_sec = kth->ktr_ts.tv_sec;
    387 				prevtime.ts.tv_nsec = kth->ktr_ts.tv_nsec;
    388 				break;
    389 			}
    390 		} else {
    391 			switch (kth->ktr_version) {
    392 			case KTRFAC_VERSION(KTRFACv0):
    393 				temp.tv.tv_sec = kth->ktr_otv.tv_sec;
    394 				temp.tv.tv_usec = kth->ktr_otv.tv_usec;
    395 				break;
    396 			case KTRFAC_VERSION(KTRFACv1):
    397 				temp.ts.tv_sec = kth->ktr_ots.tv_sec;
    398 				temp.ts.tv_nsec = kth->ktr_ots.tv_nsec;
    399 				break;
    400 			case KTRFAC_VERSION(KTRFACv2):
    401 				temp.ts.tv_sec = kth->ktr_ts.tv_sec;
    402 				temp.ts.tv_nsec = kth->ktr_ts.tv_nsec;
    403 				break;
    404 			}
    405 		}
    406 		if (kth->ktr_version == KTRFAC_VERSION(KTRFACv0))
    407 			xwprintf("%lld.%06ld ",
    408 			    (long long)temp.tv.tv_sec, (long)temp.tv.tv_usec);
    409 		else
    410 			xwprintf("%lld.%09ld ",
    411 			    (long long)temp.ts.tv_sec, (long)temp.ts.tv_nsec);
    412 	}
    413 }
    414 
    415 void
    416 ioctldecode(u_long cmd)
    417 {
    418 	char dirbuf[4], *dir = dirbuf;
    419 
    420 	if (cmd & IOC_OUT)
    421 		*dir++ = 'W';
    422 	if (cmd & IOC_IN)
    423 		*dir++ = 'R';
    424 	*dir = '\0';
    425 
    426 	xwprintf(decimal ? ", _IO%s('%c',%ld" : ", _IO%s('%c',%#lx",
    427 	    dirbuf, (int) ((cmd >> 8) & 0xff), cmd & 0xff);
    428 	if ((cmd & IOC_VOID) == 0)
    429 		xwprintf(decimal ? ",%ld)" : ",%#lx)",
    430 		    (cmd >> 16) & 0xff);
    431 	else
    432 		xwprintf(")");
    433 }
    434 
    435 void
    436 nameiargprint(const char *prefix, struct ktr_header *kth,
    437     register_t **ap, int *argsize)
    438 {
    439 	struct ktr_entry *kte;
    440 
    441 	if (*argsize == 0)
    442 		errx(EXIT_FAILURE, "argument expected");
    443 	/*
    444 	 * XXX: binary emulation mode.
    445 	 */
    446 	kte = getpendq(kth, KTR_NAMEI, NULL);
    447 	if (kte == NULL)
    448 		argprint(prefix, ap, argsize);
    449 	else {
    450 		xwprintf("%s", prefix);
    451 		nameiprint(&kte->kte_kth);
    452 		free(kte);
    453 		(*ap)++;
    454 		*argsize -= sizeof(register_t);
    455 	}
    456 }
    457 
    458 void
    459 syscallnameprint(int code)
    460 {
    461 
    462 	if (code >= cur_emul->nsysnames || code < 0)
    463 		xwprintf("[%d]", code);
    464 	else
    465 		xwprintf("%s", cur_emul->sysnames[code]);
    466 }
    467 
    468 void
    469 argprint(const char *prefix, register_t **ap, int *argsize)
    470 {
    471 
    472 	if (decimal)
    473 		xwprintf("%s%ld", prefix, (long)**ap);
    474 	else
    475 		xwprintf("%s%#lx", prefix, (long)**ap);
    476 	(*ap)++;
    477 	*argsize -= sizeof(register_t);
    478 }
    479 
    480 void
    481 syscallprint(struct ktr_header *kth)
    482 {
    483 	struct ktr_syscall *ktr = (struct ktr_syscall *)(kth + 1);
    484 	register_t *ap;
    485 	const char *s;
    486 	int argsize;
    487 
    488 	syscallnameprint(ktr->ktr_code);
    489 
    490 	/*
    491 	 * Arguments processing.
    492 	 */
    493 	argsize = ktr->ktr_argsize;
    494 	if (argsize == 0) {
    495 		xwprintf("(");
    496 		goto noargument;
    497 	}
    498 
    499 	ap = (register_t *)(ktr + 1);
    500 	if (!fancy)
    501 		goto print_first;
    502 
    503 	switch (ktr->ktr_code) {
    504 	/*
    505 	 * All these have a path as the first param.
    506 	 * The order is same as syscalls.master.
    507 	 */
    508 	case SYS_open:
    509 	case SYS_link:
    510 	case SYS_unlink:
    511 	case SYS_chdir:
    512 	case SYS___mknod50:
    513 	case SYS_chmod:
    514 	case SYS_chown:
    515 	case SYS_unmount:
    516 	case SYS_access:
    517 	case SYS_chflags:
    518 	case SYS_acct:
    519 	case SYS_revoke:
    520 	case SYS_symlink:
    521 	case SYS_readlink:
    522 	case SYS_execve:
    523 	case SYS_chroot:
    524 	case SYS_rename:
    525 	case SYS_mkfifo:
    526 	case SYS_mkdir:
    527 	case SYS_rmdir:
    528 	case SYS___utimes50:
    529 	case SYS_compat_50_quotactl:
    530 	case SYS___quotactl:
    531 	case SYS___statvfs190:
    532 	case SYS_compat_30_getfh:
    533 	case SYS_pathconf:
    534 	case SYS_truncate:
    535 	case SYS_undelete:
    536 	case SYS___posix_rename:
    537 	case SYS_lchmod:
    538 	case SYS_lchown:
    539 	case SYS___lutimes50:
    540 	case SYS___stat50:
    541 	case SYS___lstat50:
    542 	case SYS___posix_chown:
    543 	case SYS___posix_lchown:
    544 	case SYS_lchflags:
    545 	case SYS___getfh30:
    546 		nameiargprint("(", kth, &ap, &argsize);
    547 
    548 		/*
    549 		 * 2nd argument is also pathname.
    550 		 */
    551 		switch (ktr->ktr_code) {
    552 		case SYS_link:
    553 		case SYS_rename:
    554 		case SYS___posix_rename:
    555 			nameiargprint(", ", kth, &ap, &argsize);
    556 			break;
    557 		}
    558 		break;
    559 
    560 	case SYS_compat_16___sigaction14 :
    561 		if ((int)*ap < 0 || (int)*ap >= MAXSIGNALS)
    562 			xwprintf("(%d", (int)*ap);
    563 		else
    564 			xwprintf("(%s", signals[(int)*ap].name);
    565 		ap++;
    566 		argsize -= sizeof(register_t);
    567 		break;
    568 
    569 	case SYS_ioctl :
    570 		argprint("(", &ap, &argsize);
    571 		if ((s = ioctlname(*ap)) != NULL)
    572 			xwprintf(", %s", s);
    573 		else
    574 			ioctldecode(*ap);
    575 		ap++;
    576 		argsize -= sizeof(register_t);
    577 		break;
    578 
    579 	case SYS_ptrace :
    580 		if ((long)*ap >= 0 &&
    581 		    *ap < (register_t)(sizeof(ptrace_ops) / sizeof(ptrace_ops[0])))
    582 			xwprintf("(%s", ptrace_ops[*ap]);
    583 		else
    584 			xwprintf("(%ld", (long)*ap);
    585 		ap++;
    586 		argsize -= sizeof(register_t);
    587 		break;
    588 
    589 	default:
    590 print_first:
    591 		argprint("(", &ap, &argsize);
    592 		break;
    593 	}
    594 
    595 	/* Print rest of argument. */
    596 	while (argsize > 0)
    597 		argprint(", ", &ap, &argsize);
    598 
    599 noargument:
    600 	xwprintf(")");
    601 }
    602 
    603 void
    604 ktrsyscall(struct ktr_entry *kte)
    605 {
    606 	struct ktr_header *kth = &kte->kte_kth;
    607 	struct ktr_syscall *ktr = (struct ktr_syscall *)(kth + 1);
    608 
    609 	switch (ktr->ktr_code) {
    610 	case SYS_exit:
    611 		dumpheader(kth);
    612 		syscallprint(kth);
    613 		break;
    614 	default:
    615 		putpendq(kte);
    616 		return;
    617 	}
    618 
    619 	free(kte);
    620 }
    621 
    622 void
    623 sysretprint(struct ktr_header *kth)
    624 {
    625 	struct ktr_sysret *ktr = (struct ktr_sysret *)(kth + 1);
    626 	register_t ret = ktr->ktr_retval;
    627 	int error = ktr->ktr_error;
    628 
    629 	indent(50);
    630 	if (error == EJUSTRETURN)
    631 		xwprintf(" JUSTRETURN");
    632 	else if (error == ERESTART)
    633 		xwprintf(" RESTART");
    634 	else if (error) {
    635 		xwprintf(" Err#%d", error);
    636 		if (error < MAXERRNOS && error >= 0)
    637 			xwprintf(" %s", errnos[error].name);
    638 	} else
    639 		switch (ktr->ktr_code) {
    640 		case SYS_mremap:
    641 		case SYS_mmap:
    642 			xwprintf(" = %p", (void *)(intptr_t)ret);
    643 			break;
    644 		default:
    645 			xwprintf(" = %ld", (long)ret);
    646 			if (kth->ktr_len > (int)offsetof(struct ktr_sysret,
    647 			    ktr_retval_1) && ktr->ktr_retval_1 != 0)
    648 				xwprintf(", %ld", (long)ktr->ktr_retval_1);
    649 			break;
    650 		}
    651 }
    652 
    653 void
    654 ktrsysret(struct ktr_entry *kte)
    655 {
    656 	struct ktr_header *kth = &kte->kte_kth;
    657 	struct ktr_sysret *ktr = (struct ktr_sysret *)(kth + 1);
    658 	struct ktr_entry *emul;
    659 	struct ktr_entry *genio;
    660 	struct ktr_entry *syscall_ent;
    661 
    662 	dumpheader(kth);
    663 
    664 	/* Print syscall name and arguments. */
    665 	syscall_ent = getpendq(kth, KTR_SYSCALL, NULL);
    666 	if (syscall_ent == NULL) {
    667 		/*
    668 		 * Possibilly a child of fork/vfork, or tracing of
    669 		 * process started during system call.
    670 		 */
    671 		syscallnameprint(ktr->ktr_code);
    672 	} else {
    673 		syscallprint(&syscall_ent->kte_kth);
    674 		free(syscall_ent);
    675 	}
    676 
    677 	/* Print return value and an error if any. */
    678 	sysretprint(kth);
    679 
    680 	genio = getpendq(kth, KTR_GENIO, NULL);
    681 	if (genio != NULL) {
    682 		genioprint(&genio->kte_kth);
    683 		free(genio);
    684 	}
    685 
    686 	emul = getpendq(kth, KTR_EMUL, NULL);
    687 	if (emul != NULL) {
    688 		newline();
    689 		ktremul(emul);
    690 	}
    691 
    692 	flushpendq(kte);
    693 	free(kte);
    694 }
    695 
    696 void
    697 nameiprint(struct ktr_header *kth)
    698 {
    699 
    700 	xwprintf("\"%.*s\"", kth->ktr_len, (char *)(kth + 1));
    701 }
    702 
    703 #ifdef notused
    704 void
    705 ktrnamei(struct ktr_entry *kte)
    706 {
    707 	struct ktr_header *kth = &kte->kte_kth;
    708 
    709 	dumpheader(kth);
    710 	xwprintf("namei(");
    711 	nameiprint(kth);
    712 	xwprintf(")");
    713 
    714 	free(kte);
    715 }
    716 #endif
    717 
    718 void
    719 ktremul(struct ktr_entry *kte)
    720 {
    721 	struct ktr_header *kth = &kte->kte_kth;
    722 	char *emul = (char *)(kth + 1);
    723 
    724 	dumpheader(kth);
    725 	xwprintf("emul(%s)", emul);
    726 	setemul(emul, kth->ktr_pid, 1);
    727 
    728 	free(kte);
    729 }
    730 
    731 void
    732 genioprint(struct ktr_header *kth)
    733 {
    734 	struct ktr_genio *ktr = (struct ktr_genio *)(kth + 1);
    735 	static int screenwidth = 0;
    736 	int datalen = kth->ktr_len - sizeof(struct ktr_genio);
    737 	/*
    738 	 * Need to be unsigned type so that positive value is passed
    739 	 * to vis(), which will call isgraph().
    740 	 */
    741 	unsigned char *dp = (unsigned char *)(ktr + 1);
    742 	int w;
    743 	char visbuf[5];
    744 
    745 	if (screenwidth == 0) {
    746 		struct winsize ws;
    747 
    748 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
    749 		    ws.ws_col > 8)
    750 			screenwidth = ws.ws_col;
    751 		else
    752 			screenwidth = 80;
    753 	}
    754 
    755 	if (maxdata && datalen > maxdata)
    756 		datalen = maxdata;
    757 	newline();
    758 	xwprintf("       \"");
    759 	for (; datalen > 0; datalen--, dp++) {
    760 		(void) vis(visbuf, *dp, VIS_NL|VIS_TAB|VIS_CSTYLE,
    761 		    /* We put NUL at the end of buffer when reading */
    762 		    *(dp + 1));
    763 		visbuf[4] = '\0';
    764 		w = strlen(visbuf);
    765 		if (width + w + 2 >= screenwidth)
    766 			break;
    767 		xwprintf("%s", visbuf);
    768 		if (width + 2 >= screenwidth)
    769 			break;
    770 	}
    771 	xwprintf("\"");
    772 }
    773 
    774 #ifdef notused
    775 void
    776 ktrgenio(struct ktr_entry *kte)
    777 {
    778 	struct ktr_header *kth = &kte->kte_kth;
    779 	struct ktr_genio *ktr = (struct ktr_genio *)(kth + 1);
    780 
    781 	dumpheader(kth);
    782 	xwprintf("genio fd %d %s",
    783 	    ktr->ktr_fd, ktr->ktr_rw ? "write" : "read");
    784 	genioprint(kth);
    785 
    786 	free(kte);
    787 }
    788 #endif
    789 
    790 void
    791 ktrpsig(struct ktr_entry *kte)
    792 {
    793 	struct ktr_header *kth = &kte->kte_kth;
    794 	struct ktr_psig *psig = (struct ktr_psig *)(kth + 1);
    795 
    796 	dumpheader(kth);
    797 	xwprintf("SIG%s ", sys_signame[psig->signo]);
    798 	if (psig->action == SIG_DFL)
    799 		xwprintf("SIG_DFL");
    800 	else {
    801 		xwprintf("caught handler=0x%lx mask=0x%lx code=0x%x",
    802 		    (u_long)psig->action, (unsigned long)psig->mask.__bits[0],
    803 		    psig->code);
    804 	}
    805 
    806 	free(kte);
    807 }
    808 
    809 void
    810 ktrcsw(struct ktr_entry *kte)
    811 {
    812 	struct ktr_header *kth = &kte->kte_kth;
    813 	struct ktr_csw *cs = (struct ktr_csw *)(kth + 1);
    814 
    815 	dumpheader(kth);
    816 	xwprintf("%s %s", cs->out ? "stop" : "resume",
    817 	    cs->user ? "user" : "kernel");
    818 
    819 	free(kte);
    820 }
    821