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