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