Home | History | Annotate | Line # | Download | only in kdump
kdump.c revision 1.116
      1  1.116       dsl /*	$NetBSD: kdump.c,v 1.116 2013/01/02 19:39:04 dsl Exp $	*/
      2   1.17     mikel 
      3    1.1       cgd /*-
      4    1.1       cgd  * Copyright (c) 1988, 1993
      5    1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6    1.1       cgd  *
      7    1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8    1.1       cgd  * modification, are permitted provided that the following conditions
      9    1.1       cgd  * are met:
     10    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.59       agc  * 3. Neither the name of the University nor the names of its contributors
     16    1.1       cgd  *    may be used to endorse or promote products derived from this software
     17    1.1       cgd  *    without specific prior written permission.
     18    1.1       cgd  *
     19    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29    1.1       cgd  * SUCH DAMAGE.
     30    1.1       cgd  */
     31    1.1       cgd 
     32   1.17     mikel #include <sys/cdefs.h>
     33    1.1       cgd #ifndef lint
     34   1.98     lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
     35   1.98     lukem  The Regents of the University of California.  All rights reserved.");
     36    1.1       cgd #endif /* not lint */
     37    1.1       cgd 
     38    1.1       cgd #ifndef lint
     39   1.12       jtc #if 0
     40   1.12       jtc static char sccsid[] = "@(#)kdump.c	8.4 (Berkeley) 4/28/95";
     41   1.17     mikel #else
     42  1.116       dsl __RCSID("$NetBSD: kdump.c,v 1.116 2013/01/02 19:39:04 dsl Exp $");
     43   1.12       jtc #endif
     44    1.1       cgd #endif /* not lint */
     45    1.1       cgd 
     46    1.1       cgd #include <sys/param.h>
     47  1.112     alnsn #include <sys/file.h>
     48  1.116       dsl #define _KMEMUSER        /* To get the pseudo errors defined */
     49    1.1       cgd #include <sys/errno.h>
     50  1.116       dsl #undef _KMEMUSER
     51    1.1       cgd #include <sys/time.h>
     52    1.1       cgd #include <sys/uio.h>
     53    1.1       cgd #include <sys/ktrace.h>
     54    1.1       cgd #include <sys/ioctl.h>
     55    1.1       cgd #include <sys/ptrace.h>
     56  1.114  christos #include <sys/socket.h>
     57    1.4   mycroft 
     58   1.46      manu #include <ctype.h>
     59    1.4   mycroft #include <err.h>
     60    1.4   mycroft #include <signal.h>
     61   1.48       dsl #include <stddef.h>
     62    1.1       cgd #include <stdio.h>
     63    1.1       cgd #include <stdlib.h>
     64    1.1       cgd #include <string.h>
     65    1.4   mycroft #include <unistd.h>
     66    1.4   mycroft #include <vis.h>
     67  1.114  christos #include <util.h>
     68    1.4   mycroft 
     69    1.1       cgd #include "ktrace.h"
     70   1.28  jdolecek #include "setemul.h"
     71   1.28  jdolecek 
     72   1.28  jdolecek #include <sys/syscall.h>
     73    1.1       cgd 
     74   1.81  christos static int timestamp, decimal, plain, tail, maxdata = -1, numeric;
     75   1.81  christos static int word_size = 0;
     76   1.81  christos static pid_t do_pid = -1;
     77   1.81  christos static const char *tracefile = NULL;
     78   1.81  christos static struct ktr_header ktr_header;
     79   1.81  christos static int emul_changed = 0;
     80    1.1       cgd 
     81    1.1       cgd #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
     82   1.73  christos #define small(v)	(((long)(v) >= 0) && ((long)(v) < 10))
     83    1.1       cgd 
     84   1.78      matt static const char * const ptrace_ops[] = {
     85  1.113  christos 	PT_STRINGS
     86   1.11  christos };
     87   1.11  christos 
     88   1.78      matt #ifdef PT_MACHDEP_STRINGS
     89   1.78      matt static const char * const ptrace_machdep_ops[] = { PT_MACHDEP_STRINGS };
     90   1.78      matt #endif
     91   1.78      matt 
     92   1.78      matt static const char * const linux_ptrace_ops[] = {
     93   1.31  jdolecek 	"PTRACE_TRACEME",
     94   1.31  jdolecek 	"PTRACE_PEEKTEXT", "PTRACE_PEEKDATA", "PTRACE_PEEKUSER",
     95   1.31  jdolecek 	"PTRACE_POKETEXT", "PTRACE_POKEDATA", "PTRACE_POKEUSER",
     96   1.31  jdolecek 	"PTRACE_CONT", "PTRACE_KILL", "PTRACE_SINGLESTEP",
     97   1.31  jdolecek 	NULL, NULL,
     98   1.31  jdolecek 	"PTRACE_GETREGS", "PTRACE_SETREGS", "PTRACE_GETFPREGS",
     99   1.31  jdolecek 	"PTRACE_SETFPREGS", "PTRACE_ATTACH", "PTRACE_DETACH",
    100   1.96     njoly 	NULL, NULL, NULL, NULL, NULL, NULL,
    101   1.31  jdolecek 	"PTRACE_SYSCALL",
    102   1.31  jdolecek };
    103   1.31  jdolecek 
    104   1.72       mrg int	main(int, char **);
    105   1.81  christos static int	fread_tail(void *, size_t, size_t);
    106   1.81  christos static int	dumpheader(struct ktr_header *);
    107   1.81  christos static void	output_long(u_long, int);
    108   1.81  christos static void	ioctldecode(u_long);
    109   1.81  christos static void	ktrsyscall(struct ktr_syscall *);
    110   1.81  christos static void	ktrsysret(struct ktr_sysret *, int);
    111   1.81  christos static void	ktrnamei(char *, int);
    112   1.81  christos static void	ktremul(char *, int, int);
    113   1.81  christos static void	ktrgenio(struct ktr_genio *, int);
    114   1.81  christos static void	ktrpsig(void *, int);
    115   1.81  christos static void	ktrcsw(struct ktr_csw *);
    116   1.81  christos static void	ktruser(struct ktr_user *, int);
    117   1.86      manu static void	ktrmib(int *, int);
    118  1.112     alnsn static void	ktrexecfd(struct ktr_execfd *);
    119   1.95     perry static void	usage(void) __dead;
    120   1.81  christos static void	eprint(int);
    121   1.81  christos static void	rprint(register_t);
    122   1.72       mrg static const char *signame(long, int);
    123   1.64      manu static void hexdump_buf(const void *, int, int);
    124   1.58       dsl static void visdump_buf(const void *, int, int);
    125   1.17     mikel 
    126    1.4   mycroft int
    127   1.81  christos main(int argc, char **argv)
    128    1.1       cgd {
    129    1.1       cgd 	int ch, ktrlen, size;
    130   1.18     lukem 	void *m;
    131   1.58       dsl 	int trpoints = 0;
    132   1.58       dsl 	int trset = 0;
    133   1.27  jdolecek 	const char *emul_name = "netbsd";
    134   1.58       dsl 	int col;
    135   1.65       dsl 	char *cp;
    136   1.11  christos 
    137   1.81  christos 	setprogname(argv[0]);
    138  1.104  christos 
    139  1.104  christos 	if (strcmp(getprogname(), "ioctlname") == 0) {
    140  1.104  christos 		int i;
    141  1.104  christos 
    142  1.104  christos 		while ((ch = getopt(argc, argv, "e:")) != -1)
    143  1.104  christos 			switch (ch) {
    144  1.104  christos 			case 'e':
    145  1.104  christos 				emul_name = optarg;
    146  1.104  christos 				break;
    147  1.104  christos 			default:
    148  1.104  christos 				usage();
    149  1.104  christos 				break;
    150  1.104  christos 			}
    151  1.104  christos 		setemul(emul_name, 0, 0);
    152  1.104  christos 		argv += optind;
    153  1.104  christos 		argc -= optind;
    154  1.104  christos 
    155  1.104  christos 		if (argc < 1)
    156  1.104  christos 			usage();
    157  1.104  christos 
    158  1.104  christos 		for (i = 0; i < argc; i++) {
    159  1.104  christos 			ioctldecode(strtoul(argv[i], NULL, 0));
    160  1.104  christos 			(void)putchar('\n');
    161  1.104  christos 		}
    162  1.104  christos 		return 0;
    163  1.104  christos 	}
    164  1.104  christos 
    165   1.67       dsl 	while ((ch = getopt(argc, argv, "e:f:dlm:Nnp:RTt:xX:")) != -1) {
    166    1.4   mycroft 		switch (ch) {
    167   1.11  christos 		case 'e':
    168   1.27  jdolecek 			emul_name = strdup(optarg); /* it's safer to copy it */
    169   1.11  christos 			break;
    170    1.1       cgd 		case 'f':
    171    1.1       cgd 			tracefile = optarg;
    172    1.1       cgd 			break;
    173    1.1       cgd 		case 'd':
    174    1.1       cgd 			decimal = 1;
    175    1.1       cgd 			break;
    176    1.1       cgd 		case 'l':
    177    1.1       cgd 			tail = 1;
    178    1.1       cgd 			break;
    179   1.39    atatat 		case 'p':
    180   1.67       dsl 			do_pid = strtoul(optarg, &cp, 0);
    181   1.67       dsl 			if (*cp != 0)
    182   1.67       dsl 				errx(1,"invalid number %s", optarg);
    183   1.39    atatat 			break;
    184    1.1       cgd 		case 'm':
    185   1.67       dsl 			maxdata = strtoul(optarg, &cp, 0);
    186   1.67       dsl 			if (*cp != 0)
    187   1.67       dsl 				errx(1,"invalid number %s", optarg);
    188    1.1       cgd 			break;
    189   1.43    atatat 		case 'N':
    190   1.43    atatat 			numeric++;
    191   1.43    atatat 			break;
    192    1.1       cgd 		case 'n':
    193   1.36  christos 			plain++;
    194    1.1       cgd 			break;
    195    1.1       cgd 		case 'R':
    196    1.1       cgd 			timestamp = 2;	/* relative timestamp */
    197    1.1       cgd 			break;
    198    1.1       cgd 		case 'T':
    199    1.1       cgd 			timestamp = 1;
    200    1.1       cgd 			break;
    201    1.1       cgd 		case 't':
    202   1.58       dsl 			trset = 1;
    203   1.58       dsl 			trpoints = getpoints(trpoints, optarg);
    204    1.4   mycroft 			if (trpoints < 0)
    205    1.4   mycroft 				errx(1, "unknown trace point in %s", optarg);
    206    1.1       cgd 			break;
    207   1.52       dsl 		case 'x':
    208   1.66      manu 			word_size = 1;
    209   1.66      manu 			break;
    210   1.66      manu 		case 'X':
    211   1.65       dsl 			word_size = strtoul(optarg, &cp, 0);
    212   1.65       dsl 			if (*cp != 0 || word_size & (word_size - 1) ||
    213   1.67       dsl 			    word_size > 16 || word_size <= 0)
    214   1.66      manu 				errx(1, "argument to -X must be "
    215   1.66      manu 				    "1, 2, 4, 8 or 16");
    216   1.52       dsl 			break;
    217    1.1       cgd 		default:
    218    1.1       cgd 			usage();
    219    1.1       cgd 		}
    220   1.67       dsl 	}
    221    1.1       cgd 	argv += optind;
    222    1.1       cgd 	argc -= optind;
    223    1.1       cgd 
    224   1.58       dsl 	if (!trset)
    225   1.58       dsl 		trpoints = ALL_POINTS;
    226   1.58       dsl 
    227   1.41    atatat 	if (tracefile == NULL) {
    228   1.41    atatat 		if (argc == 1) {
    229   1.41    atatat 			tracefile = argv[0];
    230   1.41    atatat 			argv++;
    231   1.41    atatat 			argc--;
    232   1.76     enami 		} else
    233   1.41    atatat 			tracefile = DEF_TRACEFILE;
    234   1.41    atatat 	}
    235   1.41    atatat 
    236   1.41    atatat 	if (argc > 0)
    237    1.1       cgd 		usage();
    238    1.1       cgd 
    239   1.27  jdolecek 	setemul(emul_name, 0, 0);
    240   1.27  jdolecek 
    241   1.27  jdolecek 	m = malloc(size = 1024);
    242    1.4   mycroft 	if (m == NULL)
    243   1.27  jdolecek 		errx(1, "malloc: %s", strerror(ENOMEM));
    244    1.4   mycroft 	if (!freopen(tracefile, "r", stdin))
    245    1.4   mycroft 		err(1, "%s", tracefile);
    246   1.81  christos 	while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
    247   1.76     enami 		if (trpoints & (1 << ktr_header.ktr_type) &&
    248   1.76     enami 		    (do_pid == -1 || ktr_header.ktr_pid == do_pid))
    249   1.58       dsl 			col = dumpheader(&ktr_header);
    250   1.58       dsl 		else
    251   1.58       dsl 			col = -1;
    252    1.4   mycroft 		if ((ktrlen = ktr_header.ktr_len) < 0)
    253    1.4   mycroft 			errx(1, "bogus length 0x%x", ktrlen);
    254    1.1       cgd 		if (ktrlen > size) {
    255   1.58       dsl 			while (ktrlen > size)
    256   1.58       dsl 				size *= 2;
    257   1.58       dsl 			m = realloc(m, size);
    258    1.4   mycroft 			if (m == NULL)
    259   1.27  jdolecek 				errx(1, "realloc: %s", strerror(ENOMEM));
    260    1.1       cgd 		}
    261    1.4   mycroft 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
    262    1.4   mycroft 			errx(1, "data too short");
    263   1.58       dsl 		if (col == -1)
    264    1.1       cgd 			continue;
    265   1.27  jdolecek 
    266   1.27  jdolecek 		/* update context to match currently processed record */
    267   1.27  jdolecek 		ectx_sanify(ktr_header.ktr_pid);
    268   1.27  jdolecek 
    269    1.1       cgd 		switch (ktr_header.ktr_type) {
    270    1.1       cgd 		case KTR_SYSCALL:
    271   1.58       dsl 			ktrsyscall(m);
    272    1.1       cgd 			break;
    273    1.1       cgd 		case KTR_SYSRET:
    274   1.58       dsl 			ktrsysret(m, ktrlen);
    275    1.1       cgd 			break;
    276    1.1       cgd 		case KTR_NAMEI:
    277    1.1       cgd 			ktrnamei(m, ktrlen);
    278    1.1       cgd 			break;
    279    1.1       cgd 		case KTR_GENIO:
    280   1.58       dsl 			ktrgenio(m, ktrlen);
    281    1.1       cgd 			break;
    282    1.1       cgd 		case KTR_PSIG:
    283   1.60  christos 			ktrpsig(m, ktrlen);
    284    1.1       cgd 			break;
    285    1.1       cgd 		case KTR_CSW:
    286   1.58       dsl 			ktrcsw(m);
    287    1.1       cgd 			break;
    288   1.11  christos 		case KTR_EMUL:
    289   1.27  jdolecek 			ktremul(m, ktrlen, size);
    290   1.11  christos 			break;
    291   1.32  jdolecek 		case KTR_USER:
    292   1.58       dsl 			ktruser(m, ktrlen);
    293   1.32  jdolecek 			break;
    294   1.58       dsl 		case KTR_EXEC_ARG:
    295   1.58       dsl 		case KTR_EXEC_ENV:
    296   1.58       dsl 			visdump_buf(m, ktrlen, col);
    297   1.58       dsl 			break;
    298  1.112     alnsn 		case KTR_EXEC_FD:
    299  1.112     alnsn 			ktrexecfd(m);
    300  1.112     alnsn 			break;
    301   1.86      manu 		case KTR_MIB:
    302   1.86      manu 			ktrmib(m, ktrlen);
    303   1.86      manu 			break;
    304   1.58       dsl 		default:
    305   1.72       mrg 			putchar('\n');
    306   1.79       dsl 			hexdump_buf(m, ktrlen, word_size ? word_size : 1);
    307    1.1       cgd 		}
    308    1.1       cgd 		if (tail)
    309    1.1       cgd 			(void)fflush(stdout);
    310    1.1       cgd 	}
    311   1.17     mikel 	return (0);
    312    1.1       cgd }
    313    1.1       cgd 
    314   1.81  christos static int
    315   1.81  christos fread_tail(void *buf, size_t num, size_t size)
    316    1.1       cgd {
    317    1.1       cgd 	int i;
    318    1.1       cgd 
    319    1.1       cgd 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
    320    1.1       cgd 		(void)sleep(1);
    321    1.1       cgd 		clearerr(stdin);
    322    1.1       cgd 	}
    323    1.1       cgd 	return (i);
    324    1.1       cgd }
    325    1.1       cgd 
    326   1.81  christos static int
    327   1.81  christos dumpheader(struct ktr_header *kth)
    328    1.1       cgd {
    329   1.81  christos 	char unknown[64];
    330   1.81  christos 	const char *type;
    331   1.83  christos 	union holdtime {
    332   1.83  christos 		struct timeval tv;
    333   1.83  christos 		struct timespec ts;
    334   1.83  christos 	};
    335   1.83  christos 	static union holdtime prevtime;
    336   1.83  christos 	union holdtime temp;
    337   1.58       dsl 	int col;
    338    1.1       cgd 
    339    1.1       cgd 	switch (kth->ktr_type) {
    340    1.1       cgd 	case KTR_SYSCALL:
    341    1.1       cgd 		type = "CALL";
    342    1.1       cgd 		break;
    343    1.1       cgd 	case KTR_SYSRET:
    344    1.1       cgd 		type = "RET ";
    345    1.1       cgd 		break;
    346    1.1       cgd 	case KTR_NAMEI:
    347    1.1       cgd 		type = "NAMI";
    348    1.1       cgd 		break;
    349    1.1       cgd 	case KTR_GENIO:
    350    1.1       cgd 		type = "GIO ";
    351    1.1       cgd 		break;
    352    1.1       cgd 	case KTR_PSIG:
    353    1.1       cgd 		type = "PSIG";
    354    1.1       cgd 		break;
    355    1.1       cgd 	case KTR_CSW:
    356   1.60  christos 		type = "CSW ";
    357    1.1       cgd 		break;
    358   1.11  christos 	case KTR_EMUL:
    359   1.11  christos 		type = "EMUL";
    360   1.11  christos 		break;
    361   1.32  jdolecek 	case KTR_USER:
    362   1.94       dsl 		type = "MISC";
    363   1.32  jdolecek 		break;
    364   1.58       dsl 	case KTR_EXEC_ENV:
    365   1.58       dsl 		type = "ENV";
    366   1.58       dsl 		break;
    367   1.58       dsl 	case KTR_EXEC_ARG:
    368   1.58       dsl 		type = "ARG";
    369   1.58       dsl 		break;
    370  1.112     alnsn 	case KTR_EXEC_FD:
    371  1.112     alnsn 		type = "FD";
    372  1.112     alnsn 		break;
    373   1.83  christos 	case KTR_SAUPCALL:
    374   1.83  christos 		type = "SAU";
    375   1.83  christos 		break;
    376   1.86      manu 	case KTR_MIB:
    377   1.86      manu 		type = "MIB";
    378   1.86      manu 		break;
    379    1.1       cgd 	default:
    380   1.81  christos 		(void)snprintf(unknown, sizeof(unknown), "UNKNOWN(%d)",
    381   1.81  christos 		    kth->ktr_type);
    382    1.1       cgd 		type = unknown;
    383    1.1       cgd 	}
    384    1.1       cgd 
    385   1.83  christos 	col = printf("%6d ", kth->ktr_pid);
    386   1.83  christos 	if (kth->ktr_version > KTRFACv0)
    387   1.83  christos 		col += printf("%6d ", kth->ktr_lid);
    388   1.83  christos 	col += printf("%-8.*s ", MAXCOMLEN, kth->ktr_comm);
    389    1.1       cgd 	if (timestamp) {
    390  1.101  christos 		(void)&prevtime;
    391    1.1       cgd 		if (timestamp == 2) {
    392  1.101  christos 			switch (kth->ktr_version) {
    393  1.101  christos 			case KTRFAC_VERSION(KTRFACv0):
    394   1.83  christos 				if (prevtime.tv.tv_sec == 0)
    395   1.83  christos 					temp.tv.tv_sec = temp.tv.tv_usec = 0;
    396   1.83  christos 				else
    397  1.101  christos 					timersub(&kth->ktr_otv,
    398   1.83  christos 					    &prevtime.tv, &temp.tv);
    399  1.101  christos 				prevtime.tv.tv_sec = kth->ktr_otv.tv_sec;
    400  1.101  christos 				prevtime.tv.tv_usec = kth->ktr_otv.tv_usec;
    401  1.101  christos 				break;
    402  1.101  christos 			case KTRFAC_VERSION(KTRFACv1):
    403  1.101  christos 				if (prevtime.ts.tv_sec == 0)
    404  1.101  christos 					temp.ts.tv_sec = temp.ts.tv_nsec = 0;
    405  1.101  christos 				else
    406  1.101  christos 					timespecsub(&kth->ktr_ots,
    407  1.101  christos 					    &prevtime.ts, &temp.ts);
    408  1.101  christos 				prevtime.ts.tv_sec = kth->ktr_ots.tv_sec;
    409  1.101  christos 				prevtime.ts.tv_nsec = kth->ktr_ots.tv_nsec;
    410  1.101  christos 				break;
    411  1.101  christos 			case KTRFAC_VERSION(KTRFACv2):
    412   1.83  christos 				if (prevtime.ts.tv_sec == 0)
    413   1.83  christos 					temp.ts.tv_sec = temp.ts.tv_nsec = 0;
    414   1.83  christos 				else
    415  1.101  christos 					timespecsub(&kth->ktr_ts,
    416   1.83  christos 					    &prevtime.ts, &temp.ts);
    417  1.101  christos 				prevtime.ts.tv_sec = kth->ktr_ts.tv_sec;
    418  1.101  christos 				prevtime.ts.tv_nsec = kth->ktr_ts.tv_nsec;
    419  1.101  christos 				break;
    420  1.101  christos 			default:
    421  1.101  christos 				goto badversion;
    422   1.83  christos 			}
    423   1.83  christos 		} else {
    424  1.101  christos 			switch (kth->ktr_version) {
    425  1.101  christos 			case KTRFAC_VERSION(KTRFACv0):
    426  1.101  christos 				temp.tv.tv_sec = kth->ktr_otv.tv_sec;
    427  1.101  christos 				temp.tv.tv_usec = kth->ktr_otv.tv_usec;
    428  1.101  christos 				break;
    429  1.101  christos 			case KTRFAC_VERSION(KTRFACv1):
    430  1.101  christos 				temp.ts.tv_sec = kth->ktr_ots.tv_sec;
    431  1.101  christos 				temp.ts.tv_nsec = kth->ktr_ots.tv_nsec;
    432  1.101  christos 				break;
    433  1.101  christos 			case KTRFAC_VERSION(KTRFACv2):
    434  1.101  christos 				temp.ts.tv_sec = kth->ktr_ts.tv_sec;
    435  1.101  christos 				temp.ts.tv_nsec = kth->ktr_ts.tv_nsec;
    436  1.101  christos 				break;
    437  1.101  christos 			default:
    438  1.101  christos 			badversion:
    439  1.101  christos 				err(1, "Unsupported ktrace version %x\n",
    440  1.101  christos 				    kth->ktr_version);
    441  1.101  christos 			}
    442   1.83  christos 		}
    443   1.83  christos 		if (kth->ktr_version == KTRFACv0)
    444  1.101  christos 			col += printf("%lld.%06ld ",
    445  1.101  christos 			    (long long)temp.tv.tv_sec, (long)temp.tv.tv_usec);
    446   1.83  christos 		else
    447  1.101  christos 			col += printf("%lld.%09ld ",
    448  1.101  christos 			    (long long)temp.ts.tv_sec, (long)temp.ts.tv_nsec);
    449    1.1       cgd 	}
    450   1.58       dsl 	col += printf("%-4s  ", type);
    451   1.58       dsl 	return col;
    452    1.1       cgd }
    453    1.1       cgd 
    454   1.81  christos static void
    455   1.81  christos output_long(u_long it, int as_x)
    456   1.72       mrg {
    457   1.72       mrg 	if (cur_emul->flags & EMUL_FLAG_NETBSD32)
    458   1.72       mrg 		printf(as_x ? "%#x" : "%d", (u_int)it);
    459   1.72       mrg 	else
    460   1.72       mrg 		printf(as_x ? "%#lx" : "%ld", it);
    461   1.72       mrg }
    462   1.72       mrg 
    463   1.81  christos static void
    464   1.81  christos ioctldecode(u_long cmd)
    465   1.13  christos {
    466   1.13  christos 	char dirbuf[4], *dir = dirbuf;
    467   1.90     njoly 	int c;
    468   1.13  christos 
    469   1.15       cgd 	if (cmd & IOC_IN)
    470   1.15       cgd 		*dir++ = 'W';
    471   1.13  christos 	if (cmd & IOC_OUT)
    472   1.13  christos 		*dir++ = 'R';
    473   1.13  christos 	*dir = '\0';
    474   1.13  christos 
    475   1.90     njoly 	c = (cmd >> 8) & 0xff;
    476   1.90     njoly 	if (isprint(c))
    477  1.104  christos 		printf("_IO%s('%c',", dirbuf, c);
    478   1.90     njoly 	else
    479  1.104  christos 		printf("_IO%s(0x%02x,", dirbuf, c);
    480   1.72       mrg 	output_long(cmd & 0xff, decimal == 0);
    481   1.72       mrg 	if ((cmd & IOC_VOID) == 0) {
    482   1.72       mrg 		putchar(',');
    483   1.85  christos 		output_long(IOCPARM_LEN(cmd), decimal == 0);
    484   1.72       mrg 	}
    485   1.72       mrg 	putchar(')');
    486   1.13  christos }
    487    1.1       cgd 
    488   1.81  christos static void
    489   1.81  christos ktrsyscall(struct ktr_syscall *ktr)
    490    1.1       cgd {
    491   1.71       mrg 	int argcount;
    492   1.67       dsl 	const struct emulation *emul = cur_emul;
    493   1.18     lukem 	register_t *ap;
    494   1.67       dsl 	char c;
    495   1.81  christos 	const char *cp;
    496   1.67       dsl 	const char *sys_name;
    497    1.1       cgd 
    498   1.72       mrg 	argcount = ktr->ktr_argsize / sizeof (*ap);
    499   1.72       mrg 
    500   1.67       dsl 	emul_changed = 0;
    501   1.67       dsl 
    502   1.72       mrg 	if (numeric ||
    503  1.110     joerg 	    ((ktr->ktr_code >= emul->nsysnames || ktr->ktr_code < 0))) {
    504   1.67       dsl 		sys_name = "?";
    505    1.1       cgd 		(void)printf("[%d]", ktr->ktr_code);
    506   1.67       dsl 	} else {
    507   1.67       dsl 		sys_name = emul->sysnames[ktr->ktr_code];
    508   1.67       dsl 		(void)printf("%s", sys_name);
    509   1.67       dsl 	}
    510   1.72       mrg #ifdef _LP64
    511   1.72       mrg #define NETBSD32_	"netbsd32_"
    512   1.72       mrg 	if (cur_emul->flags & EMUL_FLAG_NETBSD32) {
    513   1.72       mrg 		size_t len = strlen(NETBSD32_);
    514   1.72       mrg 		if (strncmp(sys_name, NETBSD32_, len) == 0)
    515   1.72       mrg 			sys_name += len;
    516   1.72       mrg 	}
    517   1.72       mrg #undef NETBSD32_
    518   1.72       mrg #endif
    519   1.76     enami 
    520    1.7       cgd 	ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
    521   1.67       dsl 	if (argcount) {
    522   1.67       dsl 		c = '(';
    523   1.67       dsl 		if (plain) {
    524   1.67       dsl 			;
    525   1.67       dsl 
    526  1.105       chs 		} else if (strcmp(sys_name, "exit_group") == 0 ||
    527  1.105       chs 			   (strcmp(emul->name, "linux") != 0 &&
    528  1.105       chs 			    strcmp(emul->name, "linux32") != 0 &&
    529  1.105       chs 			    strcmp(sys_name, "exit") == 0)) {
    530   1.67       dsl 			ectx_delete();
    531   1.67       dsl 
    532   1.72       mrg 		} else if (strcmp(sys_name, "ioctl") == 0 && argcount >= 2) {
    533   1.72       mrg 			(void)putchar('(');
    534   1.73  christos 			output_long((long)*ap, !(decimal || small(*ap)));
    535   1.67       dsl 			ap++;
    536   1.67       dsl 			argcount--;
    537   1.67       dsl 			if ((cp = ioctlname(*ap)) != NULL)
    538   1.67       dsl 				(void)printf(",%s", cp);
    539  1.104  christos 			else {
    540  1.104  christos 				(void)putchar(',');
    541   1.67       dsl 				ioctldecode(*ap);
    542  1.104  christos 			}
    543   1.67       dsl 			ap++;
    544   1.67       dsl 			argcount--;
    545   1.67       dsl 			c = ',';
    546   1.67       dsl 
    547   1.82  christos 		} else if ((strstr(sys_name, "sigaction") != NULL ||
    548   1.82  christos 		    strstr(sys_name, "sigvec") != NULL) && argcount >= 1) {
    549   1.82  christos 			(void)printf("(SIG%s", signame(ap[0], 1));
    550   1.82  christos 			ap += 1;
    551   1.82  christos 			argcount -= 1;
    552   1.82  christos 			c = ',';
    553   1.82  christos 
    554   1.82  christos 		} else if ((strcmp(sys_name, "kill") == 0 ||
    555   1.82  christos 		    strcmp(sys_name, "killpg") == 0) && argcount >= 2) {
    556   1.72       mrg 			putchar('(');
    557   1.73  christos 			output_long((long)ap[0], !(decimal || small(*ap)));
    558   1.72       mrg 			(void)printf(", SIG%s", signame(ap[1], 1));
    559   1.67       dsl 			ap += 2;
    560   1.67       dsl 			argcount -= 2;
    561   1.67       dsl 			c = ',';
    562   1.67       dsl 
    563   1.67       dsl 		} else if (strcmp(sys_name, "ptrace") == 0 && argcount >= 1) {
    564   1.72       mrg 			putchar('(');
    565   1.97     njoly 			if (strcmp(emul->name, "linux") == 0 ||
    566   1.97     njoly 			    strcmp(emul->name, "linux32") == 0) {
    567   1.87       mrg 				if ((long)*ap >= 0 && *ap <
    568  1.103     lukem 				    (register_t)(sizeof(linux_ptrace_ops) /
    569  1.103     lukem 				    sizeof(linux_ptrace_ops[0])))
    570   1.72       mrg 					(void)printf("%s",
    571   1.37  christos 					    linux_ptrace_ops[*ap]);
    572   1.72       mrg 				else
    573   1.72       mrg 					output_long((long)*ap, 1);
    574   1.67       dsl 			} else {
    575  1.113  christos 				if ((long)*ap >= 0 && *ap < (register_t)
    576  1.113  christos 				    __arraycount(ptrace_ops))
    577   1.72       mrg 					(void)printf("%s", ptrace_ops[*ap]);
    578   1.78      matt #ifdef PT_MACHDEP_STRINGS
    579   1.78      matt 				else if (*ap >= PT_FIRSTMACH &&
    580  1.113  christos 				    *ap - PT_FIRSTMACH < (register_t)
    581  1.113  christos 				    __arraycount(ptrace_machdep_ops))
    582   1.78      matt 					(void)printf("%s", ptrace_machdep_ops[*ap - PT_FIRSTMACH]);
    583   1.78      matt #endif
    584   1.75     enami 				else
    585   1.72       mrg 					output_long((long)*ap, 1);
    586   1.67       dsl 			}
    587   1.67       dsl 			ap++;
    588   1.67       dsl 			argcount--;
    589   1.67       dsl 			c = ',';
    590   1.37  christos 
    591    1.1       cgd 		}
    592   1.67       dsl 		while (argcount > 0) {
    593   1.72       mrg 			putchar(c);
    594   1.73  christos 			output_long((long)*ap, !(decimal || small(*ap)));
    595   1.67       dsl 			ap++;
    596   1.67       dsl 			argcount--;
    597    1.1       cgd 			c = ',';
    598    1.1       cgd 		}
    599    1.1       cgd 		(void)putchar(')');
    600    1.1       cgd 	}
    601    1.1       cgd 	(void)putchar('\n');
    602    1.1       cgd }
    603    1.1       cgd 
    604   1.81  christos static void
    605   1.81  christos ktrsysret(struct ktr_sysret *ktr, int len)
    606    1.1       cgd {
    607   1.67       dsl 	const struct emulation *emul;
    608   1.18     lukem 	int error = ktr->ktr_error;
    609   1.18     lukem 	int code = ktr->ktr_code;
    610    1.1       cgd 
    611   1.67       dsl 	if (emul_changed)  {
    612   1.67       dsl 		/* In order to get system call name right in execve return */
    613   1.67       dsl 		emul = prev_emul;
    614   1.67       dsl 		emul_changed = 0;
    615   1.67       dsl 	} else
    616   1.67       dsl 		emul = cur_emul;
    617   1.35      manu 
    618  1.110     joerg 	if (numeric || ((code >= emul->nsysnames || code < 0 || plain > 1)))
    619    1.1       cgd 		(void)printf("[%d] ", code);
    620    1.1       cgd 	else
    621   1.67       dsl 		(void)printf("%s ", emul->sysnames[code]);
    622    1.1       cgd 
    623   1.22  christos 	switch (error) {
    624   1.22  christos 	case 0:
    625   1.48       dsl 		rprint(ktr->ktr_retval);
    626  1.103     lukem 		if (len > (int)offsetof(struct ktr_sysret, ktr_retval_1) &&
    627   1.48       dsl 		    ktr->ktr_retval_1 != 0) {
    628   1.48       dsl 			(void)printf(", ");
    629   1.48       dsl 			rprint(ktr->ktr_retval_1);
    630    1.1       cgd 		}
    631   1.22  christos 		break;
    632   1.22  christos 
    633   1.22  christos 	default:
    634   1.22  christos 		eprint(error);
    635   1.22  christos 		break;
    636   1.22  christos 	}
    637   1.22  christos 	(void)putchar('\n');
    638   1.48       dsl }
    639   1.48       dsl 
    640   1.81  christos static void
    641  1.112     alnsn ktrexecfd(struct ktr_execfd *ktr)
    642  1.112     alnsn {
    643  1.112     alnsn 	static const char *dnames[] = { DTYPE_NAMES };
    644  1.112     alnsn 	if (ktr->ktr_dtype < __arraycount(dnames))
    645  1.112     alnsn 		printf("%s %d\n", dnames[ktr->ktr_dtype], ktr->ktr_fd);
    646  1.112     alnsn 	else
    647  1.112     alnsn 		printf("UNKNOWN(%u) %d\n", ktr->ktr_dtype, ktr->ktr_fd);
    648  1.112     alnsn }
    649  1.112     alnsn 
    650  1.112     alnsn static void
    651   1.48       dsl rprint(register_t ret)
    652   1.48       dsl {
    653   1.76     enami 
    654   1.48       dsl 	if (!plain) {
    655   1.48       dsl 		(void)printf("%ld", (long)ret);
    656   1.73  christos 		if (!small(ret))
    657   1.48       dsl 			(void)printf("/%#lx", (long)ret);
    658   1.48       dsl 	} else {
    659   1.73  christos 		if (decimal || small(ret))
    660   1.48       dsl 			(void)printf("%ld", (long)ret);
    661   1.48       dsl 		else
    662   1.48       dsl 			(void)printf("%#lx", (long)ret);
    663   1.48       dsl 	}
    664   1.22  christos }
    665   1.22  christos 
    666   1.22  christos /*
    667   1.22  christos  * We print the original emulation's error numerically, but we
    668   1.22  christos  * translate it to netbsd to print it symbolically.
    669   1.22  christos  */
    670   1.81  christos static void
    671   1.81  christos eprint(int e)
    672   1.22  christos {
    673   1.22  christos 	int i = e;
    674   1.22  christos 
    675   1.67       dsl 	if (cur_emul->errnomap) {
    676   1.22  christos 
    677   1.22  christos 		/* No remapping for ERESTART and EJUSTRETURN */
    678   1.22  christos 		/* Kludge for linux that has negative error numbers */
    679   1.67       dsl 		if (cur_emul->errnomap[2] > 0 && e < 0)
    680   1.22  christos 			goto normal;
    681   1.22  christos 
    682   1.67       dsl 		for (i = 0; i < cur_emul->nerrnomap; i++)
    683   1.67       dsl 			if (e == cur_emul->errnomap[i])
    684   1.22  christos 				break;
    685   1.22  christos 
    686   1.67       dsl 		if (i == cur_emul->nerrnomap) {
    687   1.22  christos 			printf("-1 unknown errno %d", e);
    688   1.22  christos 			return;
    689   1.22  christos 		}
    690   1.22  christos 	}
    691   1.22  christos 
    692   1.22  christos normal:
    693   1.22  christos 	switch (i) {
    694   1.22  christos 	case ERESTART:
    695    1.1       cgd 		(void)printf("RESTART");
    696   1.22  christos 		break;
    697   1.22  christos 
    698   1.22  christos 	case EJUSTRETURN:
    699    1.1       cgd 		(void)printf("JUSTRETURN");
    700   1.22  christos 		break;
    701   1.22  christos 
    702   1.22  christos 	default:
    703   1.22  christos 		(void)printf("-1 errno %d", e);
    704   1.36  christos 		if (!plain)
    705   1.22  christos 			(void)printf(" %s", strerror(i));
    706    1.1       cgd 	}
    707    1.1       cgd }
    708    1.1       cgd 
    709   1.81  christos static void
    710   1.81  christos ktrnamei(char *cp, int len)
    711    1.1       cgd {
    712   1.17     mikel 
    713    1.1       cgd 	(void)printf("\"%.*s\"\n", len, cp);
    714    1.1       cgd }
    715    1.1       cgd 
    716   1.81  christos static void
    717   1.81  christos ktremul(char *name, int len, int bufsize)
    718   1.11  christos {
    719   1.76     enami 
    720   1.27  jdolecek 	if (len >= bufsize)
    721   1.27  jdolecek 		len = bufsize - 1;
    722   1.11  christos 
    723   1.27  jdolecek 	name[len] = '\0';
    724   1.27  jdolecek 	setemul(name, ktr_header.ktr_pid, 1);
    725   1.35      manu 	emul_changed = 1;
    726   1.11  christos 
    727   1.11  christos 	(void)printf("\"%s\"\n", name);
    728   1.11  christos }
    729   1.11  christos 
    730   1.52       dsl static void
    731   1.81  christos hexdump_buf(const void *vdp, int datalen, int word_sz)
    732   1.52       dsl {
    733   1.65       dsl 	const char hex[] = "0123456789abcdef";
    734   1.77       dsl 	char chars[16], prev[16];
    735   1.65       dsl 	char bytes[16 * 3 + 4];
    736   1.58       dsl 	const unsigned char *dp = vdp;
    737   1.65       dsl 	const unsigned char *datalim = dp + datalen;
    738   1.65       dsl 	const unsigned char *line_end;
    739   1.80     lukem 	int off, l = 0, c;
    740   1.65       dsl 	char *cp, *bp;
    741   1.65       dsl 	int divmask = word_sz - 1;	/* block size in bytes */
    742   1.65       dsl 	int gdelim = 3;			/* gap between blocks */
    743   1.65       dsl 	int bsize = 2;			/* increment for each byte */
    744   1.65       dsl 	int width;
    745   1.77       dsl 	int dupl = 0;
    746   1.65       dsl #if _BYTE_ORDER == _LITTLE_ENDIAN
    747   1.65       dsl 	int bswap = word_sz - 1;
    748   1.65       dsl #else
    749   1.65       dsl #define	bswap 0
    750   1.65       dsl #endif
    751   1.65       dsl 
    752   1.65       dsl 	switch (word_sz) {
    753   1.65       dsl 	case 2:
    754   1.65       dsl 		gdelim = 2;
    755   1.64      manu 		break;
    756   1.64      manu 	case 1:
    757   1.65       dsl 		divmask = 7;
    758   1.65       dsl 		bsize = 3;
    759   1.65       dsl 		gdelim = 1;
    760   1.65       dsl 		break;
    761   1.64      manu 	default:
    762   1.64      manu 		break;
    763   1.64      manu 	}
    764   1.65       dsl 	width = 16 * bsize + (16 / (divmask + 1)) * gdelim;
    765   1.79       dsl 	if (word_sz != 1)
    766   1.65       dsl 		width += 2;
    767   1.65       dsl 
    768   1.65       dsl 	for (off = 0; dp < datalim; off += l) {
    769   1.65       dsl 		memset(bytes, ' ', sizeof bytes);
    770   1.65       dsl 		line_end = dp + 16;
    771   1.77       dsl 		if (line_end >= datalim) {
    772   1.65       dsl 			line_end = datalim;
    773   1.77       dsl 			dupl |= 1;	/* need to print */
    774   1.77       dsl 		} else {
    775   1.77       dsl 			if (dupl == 0 || memcmp(dp, prev, sizeof chars))
    776   1.77       dsl 				dupl |= 1;
    777   1.77       dsl 		}
    778   1.77       dsl 
    779   1.77       dsl 		if (!(dupl & 1)) {
    780   1.77       dsl 			/* This is a duplicate of the line above, count 'em */
    781   1.77       dsl 			dupl += 2;
    782   1.77       dsl 			dp = line_end;
    783   1.77       dsl 			continue;
    784   1.77       dsl 		}
    785   1.77       dsl 
    786   1.77       dsl 		if (dupl > 3) {
    787   1.77       dsl 			/* previous line as a duplicate */
    788   1.77       dsl 			if (dupl == 5)
    789   1.77       dsl 				/* Only one duplicate, print line */
    790   1.77       dsl 				printf("\t%-5.3x%.*s%.*s\n",
    791   1.77       dsl 					off - l, width, bytes, l, chars);
    792   1.77       dsl 			else
    793   1.77       dsl 				printf("\t%.*s\n",
    794   1.77       dsl 					snprintf(NULL, 0, "%3x", off), "*****");
    795   1.77       dsl 		}
    796   1.52       dsl 
    797   1.65       dsl 		for (l = 0, bp = bytes, cp = chars; dp < line_end; l++) {
    798   1.52       dsl 			c = *dp++;
    799   1.77       dsl 			prev[l] = c;
    800   1.65       dsl 			if ((l & divmask) == 0)
    801   1.65       dsl 				bp += gdelim;
    802   1.65       dsl 			bp[(l ^ bswap) * bsize] = hex[c >> 4];
    803   1.65       dsl 			bp[(l ^ bswap) * bsize + 1] = hex[c & 0xf];
    804   1.52       dsl 			*cp++ = isgraph(c) ? c : '.';
    805   1.76     enami 		}
    806   1.64      manu 
    807   1.67       dsl 		printf("\t%-5.3x%.*s%.*s\n", off, width, bytes, l, chars);
    808   1.77       dsl 		dupl = 2;
    809   1.52       dsl 	}
    810   1.52       dsl }
    811   1.52       dsl 
    812   1.58       dsl static void
    813   1.58       dsl visdump_buf(const void *vdp, int datalen, int col)
    814    1.1       cgd {
    815   1.58       dsl 	const unsigned char *dp = vdp;
    816   1.18     lukem 	char *cp;
    817   1.18     lukem 	int width;
    818    1.1       cgd 	char visbuf[5];
    819   1.20       mrg 	static int screenwidth = 0;
    820    1.1       cgd 
    821    1.1       cgd 	if (screenwidth == 0) {
    822    1.1       cgd 		struct winsize ws;
    823    1.1       cgd 
    824   1.36  christos 		if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
    825    1.1       cgd 		    ws.ws_col > 8)
    826    1.1       cgd 			screenwidth = ws.ws_col;
    827    1.1       cgd 		else
    828    1.1       cgd 			screenwidth = 80;
    829    1.1       cgd 	}
    830   1.58       dsl 
    831   1.58       dsl 	(void)printf("\"");
    832   1.58       dsl 	col++;
    833    1.4   mycroft 	for (; datalen > 0; datalen--, dp++) {
    834   1.58       dsl 		(void)svis(visbuf, *dp, VIS_CSTYLE,
    835   1.93       agc 		    datalen > 1 ? *(dp + 1) : 0, "\"\n");
    836    1.1       cgd 		cp = visbuf;
    837    1.1       cgd 		/*
    838    1.1       cgd 		 * Keep track of printables and
    839    1.1       cgd 		 * space chars (like fold(1)).
    840    1.1       cgd 		 */
    841    1.1       cgd 		if (col == 0) {
    842    1.1       cgd 			(void)putchar('\t');
    843    1.1       cgd 			col = 8;
    844    1.1       cgd 		}
    845   1.76     enami 		switch (*cp) {
    846    1.1       cgd 		case '\n':
    847    1.1       cgd 			col = 0;
    848    1.1       cgd 			(void)putchar('\n');
    849    1.1       cgd 			continue;
    850    1.1       cgd 		case '\t':
    851   1.58       dsl 			width = 8 - (col & 07);
    852    1.1       cgd 			break;
    853    1.1       cgd 		default:
    854    1.1       cgd 			width = strlen(cp);
    855    1.1       cgd 		}
    856   1.58       dsl 		if (col + width > (screenwidth - 2)) {
    857    1.1       cgd 			(void)printf("\\\n\t");
    858    1.1       cgd 			col = 8;
    859   1.74     enami 			if (*cp == '\t')
    860   1.74     enami 				width = 8;
    861    1.1       cgd 		}
    862    1.1       cgd 		col += width;
    863    1.1       cgd 		do {
    864    1.1       cgd 			(void)putchar(*cp++);
    865    1.1       cgd 		} while (*cp);
    866    1.1       cgd 	}
    867    1.1       cgd 	if (col == 0)
    868    1.1       cgd 		(void)printf("       ");
    869    1.1       cgd 	(void)printf("\"\n");
    870   1.58       dsl }
    871   1.58       dsl 
    872   1.81  christos static void
    873   1.81  christos ktrgenio(struct ktr_genio *ktr, int len)
    874   1.58       dsl {
    875   1.58       dsl 	int datalen = len - sizeof (struct ktr_genio);
    876   1.58       dsl 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
    877   1.58       dsl 
    878   1.86      manu 	if (ktr->ktr_fd != -1)
    879   1.86      manu 		printf("fd %d ", ktr->ktr_fd);
    880   1.86      manu 	printf("%s %d bytes\n",
    881   1.76     enami 	    ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
    882   1.58       dsl 	if (maxdata == 0)
    883   1.58       dsl 		return;
    884   1.58       dsl 	if (maxdata > 0 && datalen > maxdata)
    885   1.58       dsl 		datalen = maxdata;
    886   1.65       dsl 	if (word_size) {
    887   1.65       dsl 		hexdump_buf(dp, datalen, word_size);
    888   1.58       dsl 		return;
    889   1.58       dsl 	}
    890   1.58       dsl 	(void)printf("       ");
    891   1.58       dsl 	visdump_buf(dp, datalen, 7);
    892    1.1       cgd }
    893    1.1       cgd 
    894   1.81  christos static void
    895   1.81  christos ktrpsig(void *v, int len)
    896    1.1       cgd {
    897   1.21   mycroft 	int signo, first;
    898   1.60  christos 	struct {
    899   1.60  christos 		struct ktr_psig ps;
    900   1.60  christos 		siginfo_t si;
    901   1.60  christos 	} *psig = v;
    902   1.60  christos 	siginfo_t *si = &psig->si;
    903   1.60  christos 	const char *code;
    904   1.60  christos 
    905   1.60  christos 	(void)printf("SIG%s ", signame(psig->ps.signo, 0));
    906   1.60  christos 	if (psig->ps.action == SIG_DFL)
    907   1.60  christos 		(void)printf("SIG_DFL");
    908   1.21   mycroft 	else {
    909   1.60  christos 		(void)printf("caught handler=%p mask=(", psig->ps.action);
    910   1.21   mycroft 		first = 1;
    911   1.21   mycroft 		for (signo = 1; signo < NSIG; signo++) {
    912   1.60  christos 			if (sigismember(&psig->ps.mask, signo)) {
    913   1.21   mycroft 				if (first)
    914   1.21   mycroft 					first = 0;
    915   1.21   mycroft 				else
    916   1.21   mycroft 					(void)printf(",");
    917   1.21   mycroft 				(void)printf("%d", signo);
    918   1.21   mycroft 			}
    919   1.21   mycroft 		}
    920   1.60  christos 		(void)printf(")");
    921   1.60  christos 	}
    922   1.60  christos 	switch (len) {
    923   1.60  christos 	case sizeof(struct ktr_psig):
    924   1.61  christos 		if (psig->ps.code)
    925   1.61  christos 			printf(" code=0x%x", psig->ps.code);
    926   1.61  christos 		printf(psig->ps.action == SIG_DFL ? "\n" : ")\n");
    927   1.60  christos 		return;
    928   1.60  christos 	case sizeof(*psig):
    929   1.60  christos 		if (si->si_code == 0) {
    930   1.61  christos 			printf(": code=SI_USER sent by pid=%d, uid=%d)\n",
    931   1.76     enami 			    si->si_pid, si->si_uid);
    932   1.60  christos 			return;
    933   1.60  christos 		}
    934   1.60  christos 
    935   1.60  christos 		if (si->si_code < 0) {
    936   1.60  christos 			switch (si->si_code) {
    937   1.60  christos 			case SI_TIMER:
    938  1.106  christos 			case SI_QUEUE:
    939  1.106  christos 				printf(": code=%s sent by pid=%d, uid=%d with "
    940  1.106  christos 				    "sigval %p)\n", si->si_code == SI_TIMER ?
    941  1.106  christos 				    "SI_TIMER" : "SI_QUEUE", si->si_pid,
    942  1.106  christos 				    si->si_uid, si->si_value.sival_ptr);
    943   1.60  christos 				return;
    944   1.60  christos 			case SI_ASYNCIO:
    945   1.60  christos 			case SI_MESGQ:
    946  1.107  christos 				printf(": code=%s with sigval %p)\n",
    947  1.107  christos 				    si->si_code == SI_ASYNCIO ?
    948  1.107  christos 				    "SI_ASYNCIO" : "SI_MESGQ",
    949  1.107  christos 				    si->si_value.sival_ptr);
    950  1.107  christos 				return;
    951  1.100     njoly 			case SI_LWP:
    952  1.108  christos 				printf(": code=SI_LWP sent by pid=%d, "
    953  1.109  pgoyette 				    "uid=%d)\n", si->si_pid, si->si_uid);
    954  1.107  christos 				return;
    955   1.60  christos 			default:
    956   1.60  christos 				code = NULL;
    957   1.60  christos 				break;
    958   1.60  christos 			}
    959   1.60  christos 			if (code)
    960   1.61  christos 				printf(": code=%s unimplemented)\n", code);
    961   1.60  christos 			else
    962   1.61  christos 				printf(": code=%d unimplemented)\n",
    963   1.60  christos 				    si->si_code);
    964   1.60  christos 			return;
    965   1.60  christos 		}
    966   1.60  christos 
    967   1.99  christos 		if (si->si_code == SI_NOINFO) {
    968   1.99  christos 			printf(": code=SI_NOINFO\n");
    969   1.99  christos 			return;
    970   1.99  christos 		}
    971   1.99  christos 
    972   1.60  christos 		code = siginfocodename(si->si_signo, si->si_code);
    973   1.60  christos 		switch (si->si_signo) {
    974   1.60  christos 		case SIGCHLD:
    975   1.60  christos 			printf(": code=%s child pid=%d, uid=%d, "
    976   1.76     enami 			    " status=%u, utime=%lu, stime=%lu)\n",
    977   1.60  christos 			    code, si->si_pid,
    978   1.62      matt 			    si->si_uid, si->si_status,
    979   1.62      matt 			    (unsigned long) si->si_utime,
    980   1.76     enami 			    (unsigned long) si->si_stime);
    981   1.60  christos 			return;
    982   1.60  christos 		case SIGILL:
    983   1.60  christos 		case SIGFPE:
    984   1.60  christos 		case SIGSEGV:
    985   1.60  christos 		case SIGBUS:
    986   1.60  christos 		case SIGTRAP:
    987   1.61  christos 			printf(": code=%s, addr=%p, trap=%d)\n",
    988   1.60  christos 			    code, si->si_addr, si->si_trap);
    989   1.60  christos 			return;
    990   1.60  christos 		case SIGIO:
    991   1.61  christos 			printf(": code=%s, fd=%d, band=%lx)\n",
    992   1.60  christos 			    code, si->si_fd, si->si_band);
    993   1.60  christos 			return;
    994   1.60  christos 		default:
    995   1.61  christos 			printf(": code=%s, errno=%d)\n",
    996   1.60  christos 			    code, si->si_errno);
    997   1.60  christos 			return;
    998   1.60  christos 		}
    999   1.60  christos 		/*NOTREACHED*/
   1000   1.60  christos 	default:
   1001   1.60  christos 		warnx("Unhandled size %d for ktrpsig\n", len);
   1002   1.60  christos 		break;
   1003   1.21   mycroft 	}
   1004    1.1       cgd }
   1005    1.1       cgd 
   1006   1.81  christos static void
   1007   1.81  christos ktrcsw(struct ktr_csw *cs)
   1008    1.1       cgd {
   1009   1.17     mikel 
   1010    1.1       cgd 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
   1011    1.4   mycroft 	    cs->user ? "user" : "kernel");
   1012   1.32  jdolecek }
   1013   1.32  jdolecek 
   1014   1.81  christos static void
   1015  1.114  christos ktruser_msghdr(const char *name, const void *buf, size_t len)
   1016  1.114  christos {
   1017  1.114  christos 	struct msghdr m;
   1018  1.114  christos 
   1019  1.114  christos 	if (len != sizeof(m))
   1020  1.114  christos 		warnx("%.*s: len %zu != %zu", KTR_USER_MAXIDLEN, name, len,
   1021  1.114  christos 		    sizeof(m));
   1022  1.114  christos 	memcpy(&m, buf, len);
   1023  1.114  christos 	printf("%.*s: [name=%p, namelen=%zu, iov=%p, iovlen=%zu, control=%p, "
   1024  1.114  christos 	    "controllen=%zu, flags=%x]\n", KTR_USER_MAXIDLEN, name,
   1025  1.114  christos 	    m.msg_name, (size_t)m.msg_namelen, m.msg_iov, (size_t)m.msg_iovlen,
   1026  1.114  christos 	    m.msg_control, (size_t)m.msg_controllen, m.msg_flags);
   1027  1.114  christos }
   1028  1.114  christos 
   1029  1.114  christos static void
   1030  1.114  christos ktruser_soname(const char *name, const void *buf, size_t len)
   1031  1.114  christos {
   1032  1.114  christos 	char fmt[512];
   1033  1.115  christos 	sockaddr_snprintf(fmt, sizeof(fmt), "%a", buf);
   1034  1.114  christos 	printf("%.*s: [%s]\n", KTR_USER_MAXIDLEN, name, fmt);
   1035  1.114  christos }
   1036  1.114  christos 
   1037  1.114  christos static void
   1038  1.114  christos ktruser_control(const char *name, const void *buf, size_t len)
   1039  1.114  christos {
   1040  1.114  christos 	struct cmsghdr m;
   1041  1.114  christos 
   1042  1.114  christos 	if (len < sizeof(m))
   1043  1.114  christos 		warnx("%.*s: len %zu < %zu", KTR_USER_MAXIDLEN, name, len,
   1044  1.114  christos 		    sizeof(m));
   1045  1.114  christos 	memcpy(&m, buf, sizeof(m));
   1046  1.114  christos 	printf("%.*s: [len=%zu, level=%d, type=%d]\n", KTR_USER_MAXIDLEN, name,
   1047  1.114  christos 	    (size_t)m.cmsg_len, m.cmsg_level, m.cmsg_type);
   1048  1.114  christos }
   1049  1.114  christos 
   1050  1.114  christos static void
   1051  1.114  christos ktruser_misc(const char *name, const void *buf, size_t len)
   1052  1.114  christos {
   1053  1.114  christos 	size_t i;
   1054  1.114  christos 	const char *dta = buf;
   1055  1.114  christos 
   1056  1.114  christos 	printf("%.*s: %zu, ", KTR_USER_MAXIDLEN, name, len);
   1057  1.114  christos 	for (i = 0; i < len; i++)
   1058  1.114  christos 		printf("%02x", (unsigned int) dta[i]);
   1059  1.114  christos 	printf("\n");
   1060  1.114  christos }
   1061  1.114  christos 
   1062  1.114  christos static struct {
   1063  1.114  christos 	const char *name;
   1064  1.114  christos 	void (*func)(const char *, const void *, size_t);
   1065  1.114  christos } nv[] = {
   1066  1.114  christos 	{ "msghdr", ktruser_msghdr },
   1067  1.114  christos 	{ "mbsoname", ktruser_soname },
   1068  1.114  christos 	{ "mbcontrol", ktruser_control },
   1069  1.114  christos 	{ NULL,	ktruser_misc },
   1070  1.114  christos };
   1071  1.114  christos 
   1072  1.114  christos static void
   1073   1.81  christos ktruser(struct ktr_user *usr, int len)
   1074   1.32  jdolecek {
   1075   1.40  jdolecek 	unsigned char *dta;
   1076   1.34  jdolecek 
   1077   1.92       dsl 	len -= sizeof(struct ktr_user);
   1078   1.92       dsl 	dta = (unsigned char *)(usr + 1);
   1079   1.92       dsl 	if (word_size) {
   1080  1.114  christos 		printf("%.*s:", KTR_USER_MAXIDLEN, usr->ktr_id);
   1081   1.92       dsl 		printf("\n");
   1082   1.92       dsl 		hexdump_buf(dta, len, word_size);
   1083   1.92       dsl 		return;
   1084   1.92       dsl 	}
   1085  1.114  christos 	for (size_t j = 0; j < __arraycount(nv); j++)
   1086  1.114  christos 		if (nv[j].name == NULL ||
   1087  1.114  christos 		    strncmp(nv[j].name, usr->ktr_id, KTR_USER_MAXIDLEN) == 0) {
   1088  1.114  christos 			(*nv[j].func)(usr->ktr_id, dta, len);
   1089  1.114  christos 			break;
   1090  1.114  christos 		}
   1091   1.37  christos }
   1092   1.37  christos 
   1093   1.81  christos static void
   1094   1.86      manu ktrmib(int *namep, int len)
   1095   1.86      manu {
   1096  1.103     lukem 	size_t i;
   1097   1.86      manu 
   1098   1.86      manu 	for (i = 0; i < (len / sizeof(*namep)); i++)
   1099   1.86      manu 		printf("%s%d", (i == 0) ? "" : ".", namep[i]);
   1100   1.86      manu 	printf("\n");
   1101   1.86      manu }
   1102   1.86      manu 
   1103   1.37  christos static const char *
   1104   1.37  christos signame(long sig, int xlat)
   1105   1.37  christos {
   1106   1.37  christos 	static char buf[64];
   1107   1.76     enami 
   1108   1.44  jdolecek 	if (sig == 0)
   1109   1.44  jdolecek 		return " 0";
   1110   1.44  jdolecek 	else if (sig < 0 || sig >= NSIG) {
   1111   1.37  christos 		(void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
   1112   1.37  christos 		return buf;
   1113   1.37  christos 	} else
   1114   1.67       dsl 		return sys_signame[(xlat && cur_emul->signalmap != NULL) ?
   1115   1.67       dsl 		    cur_emul->signalmap[sig] : sig];
   1116    1.1       cgd }
   1117    1.1       cgd 
   1118   1.81  christos static void
   1119   1.81  christos usage(void)
   1120    1.1       cgd {
   1121  1.104  christos 	if (strcmp(getprogname(), "ioctlname") == 0) {
   1122  1.104  christos 		(void)fprintf(stderr, "Usage: %s [-e emulation] <ioctl> ...\n",
   1123  1.104  christos 		    getprogname());
   1124  1.104  christos 	} else {
   1125  1.104  christos 		(void)fprintf(stderr, "Usage: %s [-dlNnRT] [-e emulation] "
   1126  1.104  christos 		   "[-f file] [-m maxdata] [-p pid]\n             [-t trstr] "
   1127  1.104  christos 		   "[-x | -X size] [file]\n", getprogname());
   1128  1.104  christos 	}
   1129    1.1       cgd 	exit(1);
   1130   1.11  christos }
   1131