Home | History | Annotate | Line # | Download | only in kdump
kdump.c revision 1.146
      1  1.146    rillig /*	$NetBSD: kdump.c,v 1.146 2025/03/06 06:36:25 rillig 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.146    rillig __RCSID("$NetBSD: kdump.c,v 1.146 2025/03/06 06:36:25 rillig 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.138   thorpej #include <sys/futex.h>
     59    1.4   mycroft 
     60   1.46      manu #include <ctype.h>
     61    1.4   mycroft #include <err.h>
     62  1.133     kamil #include <inttypes.h>
     63    1.4   mycroft #include <signal.h>
     64   1.48       dsl #include <stddef.h>
     65    1.1       cgd #include <stdio.h>
     66    1.1       cgd #include <stdlib.h>
     67    1.1       cgd #include <string.h>
     68    1.4   mycroft #include <unistd.h>
     69    1.4   mycroft #include <vis.h>
     70  1.114  christos #include <util.h>
     71    1.4   mycroft 
     72  1.143  christos #include <netinet/in.h>
     73  1.143  christos #include <netinet/tcp.h>
     74  1.143  christos 
     75    1.1       cgd #include "ktrace.h"
     76   1.28  jdolecek #include "setemul.h"
     77   1.28  jdolecek 
     78   1.28  jdolecek #include <sys/syscall.h>
     79    1.1       cgd 
     80  1.143  christos #define	CASERETURN(a)	case a:	return # a
     81  1.143  christos 
     82  1.127       uwe #define TIMESTAMP_NONE		0x0
     83  1.127       uwe #define TIMESTAMP_ABSOLUTE	0x1
     84  1.127       uwe #define TIMESTAMP_ELAPSED	0x2
     85  1.127       uwe #define TIMESTAMP_RELATIVE	0x4
     86  1.127       uwe 
     87   1.81  christos static int timestamp, decimal, plain, tail, maxdata = -1, numeric;
     88   1.81  christos static int word_size = 0;
     89   1.81  christos static pid_t do_pid = -1;
     90   1.81  christos static const char *tracefile = NULL;
     91   1.81  christos static struct ktr_header ktr_header;
     92   1.81  christos static int emul_changed = 0;
     93    1.1       cgd 
     94    1.1       cgd #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
     95   1.73  christos #define small(v)	(((long)(v) >= 0) && ((long)(v) < 10))
     96    1.1       cgd 
     97   1.78      matt static const char * const ptrace_ops[] = {
     98  1.113  christos 	PT_STRINGS
     99   1.11  christos };
    100   1.11  christos 
    101   1.78      matt #ifdef PT_MACHDEP_STRINGS
    102   1.78      matt static const char * const ptrace_machdep_ops[] = { PT_MACHDEP_STRINGS };
    103   1.78      matt #endif
    104   1.78      matt 
    105   1.78      matt static const char * const linux_ptrace_ops[] = {
    106   1.31  jdolecek 	"PTRACE_TRACEME",
    107   1.31  jdolecek 	"PTRACE_PEEKTEXT", "PTRACE_PEEKDATA", "PTRACE_PEEKUSER",
    108   1.31  jdolecek 	"PTRACE_POKETEXT", "PTRACE_POKEDATA", "PTRACE_POKEUSER",
    109   1.31  jdolecek 	"PTRACE_CONT", "PTRACE_KILL", "PTRACE_SINGLESTEP",
    110   1.31  jdolecek 	NULL, NULL,
    111   1.31  jdolecek 	"PTRACE_GETREGS", "PTRACE_SETREGS", "PTRACE_GETFPREGS",
    112   1.31  jdolecek 	"PTRACE_SETFPREGS", "PTRACE_ATTACH", "PTRACE_DETACH",
    113   1.96     njoly 	NULL, NULL, NULL, NULL, NULL, NULL,
    114   1.31  jdolecek 	"PTRACE_SYSCALL",
    115   1.31  jdolecek };
    116   1.31  jdolecek 
    117  1.134  christos static const char default_format[] = { "%n\t%E\t%x\n" };
    118  1.133     kamil 
    119  1.134  christos static void	fmtprint(const char *, const struct ioctlinfo *ii);
    120   1.81  christos static int	fread_tail(void *, size_t, size_t);
    121   1.81  christos static int	dumpheader(struct ktr_header *);
    122  1.128       uwe static int	output_ts(const struct timespec *);
    123   1.81  christos static void	output_long(u_long, int);
    124   1.81  christos static void	ioctldecode(u_long);
    125   1.81  christos static void	ktrsyscall(struct ktr_syscall *);
    126   1.81  christos static void	ktrsysret(struct ktr_sysret *, int);
    127   1.81  christos static void	ktrnamei(char *, int);
    128  1.123     alnsn static void	ktremul(char *, size_t, size_t);
    129   1.81  christos static void	ktrgenio(struct ktr_genio *, int);
    130   1.81  christos static void	ktrpsig(void *, int);
    131   1.81  christos static void	ktrcsw(struct ktr_csw *);
    132   1.81  christos static void	ktruser(struct ktr_user *, int);
    133   1.86      manu static void	ktrmib(int *, int);
    134  1.112     alnsn static void	ktrexecfd(struct ktr_execfd *);
    135   1.95     perry static void	usage(void) __dead;
    136   1.81  christos static void	eprint(int);
    137   1.81  christos static void	rprint(register_t);
    138   1.72       mrg static const char *signame(long, int);
    139   1.64      manu static void hexdump_buf(const void *, int, int);
    140   1.58       dsl static void visdump_buf(const void *, int, int);
    141  1.134  christos static const struct ioctlinfo *find_ioctl(const char *);
    142   1.17     mikel 
    143    1.4   mycroft int
    144   1.81  christos main(int argc, char **argv)
    145    1.1       cgd {
    146  1.123     alnsn 	unsigned int ktrlen, size;
    147  1.123     alnsn 	int ch;
    148   1.18     lukem 	void *m;
    149   1.58       dsl 	int trpoints = 0;
    150   1.58       dsl 	int trset = 0;
    151   1.27  jdolecek 	const char *emul_name = "netbsd";
    152  1.133     kamil 	const char *format = default_format;
    153   1.58       dsl 	int col;
    154   1.65       dsl 	char *cp;
    155   1.11  christos 
    156   1.81  christos 	setprogname(argv[0]);
    157  1.104  christos 
    158  1.133     kamil 	if (strcmp(getprogname(), "ioctlprint") == 0) {
    159  1.134  christos 		const struct ioctlinfo *ii;
    160  1.134  christos 		int list = 0;
    161  1.104  christos 		int i;
    162  1.104  christos 
    163  1.134  christos 		while ((ch = getopt(argc, argv, "e:f:l")) != -1)
    164  1.104  christos 			switch (ch) {
    165  1.104  christos 			case 'e':
    166  1.104  christos 				emul_name = optarg;
    167  1.104  christos 				break;
    168  1.133     kamil 			case 'f':
    169  1.133     kamil 				if (format != default_format)
    170  1.133     kamil 					errx(1, "Too many formats");
    171  1.133     kamil 				format = optarg;
    172  1.133     kamil 				break;
    173  1.134  christos 			case 'l':
    174  1.134  christos 				list = 1;
    175  1.134  christos 				break;
    176  1.104  christos 			default:
    177  1.104  christos 				usage();
    178  1.104  christos 				break;
    179  1.104  christos 			}
    180  1.134  christos 
    181  1.104  christos 		setemul(emul_name, 0, 0);
    182  1.104  christos 		argv += optind;
    183  1.104  christos 		argc -= optind;
    184  1.104  christos 
    185  1.134  christos 		if (argc < 1 && !list)
    186  1.104  christos 			usage();
    187  1.104  christos 
    188  1.134  christos 		if (list) {
    189  1.134  christos 			for (i = 0; ioctlinfo[i].name != NULL; i++) {
    190  1.134  christos 				fmtprint(format, &ioctlinfo[i]);
    191  1.134  christos 			}
    192  1.134  christos 			return 0;
    193  1.134  christos 		}
    194  1.134  christos 
    195  1.104  christos 		for (i = 0; i < argc; i++) {
    196  1.134  christos 			if ((ii = find_ioctl(argv[i])) == NULL) {
    197  1.134  christos 				warnx("Can't find ioctl `%s'", argv[i]);
    198  1.134  christos 				continue;
    199  1.134  christos 			}
    200  1.134  christos 			fmtprint(format, ii);
    201  1.104  christos 		}
    202  1.104  christos 		return 0;
    203  1.104  christos 	}
    204  1.127       uwe 
    205  1.127       uwe 	timestamp = TIMESTAMP_NONE;
    206  1.127       uwe 
    207  1.127       uwe 	while ((ch = getopt(argc, argv, "Ee:f:dlm:Nnp:RTt:xX:")) != -1) {
    208    1.4   mycroft 		switch (ch) {
    209  1.127       uwe 		case 'E':
    210  1.127       uwe 			timestamp |= TIMESTAMP_ELAPSED;
    211  1.127       uwe 			break;
    212   1.11  christos 		case 'e':
    213   1.27  jdolecek 			emul_name = strdup(optarg); /* it's safer to copy it */
    214   1.11  christos 			break;
    215    1.1       cgd 		case 'f':
    216    1.1       cgd 			tracefile = optarg;
    217    1.1       cgd 			break;
    218    1.1       cgd 		case 'd':
    219    1.1       cgd 			decimal = 1;
    220    1.1       cgd 			break;
    221    1.1       cgd 		case 'l':
    222    1.1       cgd 			tail = 1;
    223    1.1       cgd 			break;
    224   1.39    atatat 		case 'p':
    225   1.67       dsl 			do_pid = strtoul(optarg, &cp, 0);
    226   1.67       dsl 			if (*cp != 0)
    227   1.67       dsl 				errx(1,"invalid number %s", optarg);
    228   1.39    atatat 			break;
    229    1.1       cgd 		case 'm':
    230   1.67       dsl 			maxdata = strtoul(optarg, &cp, 0);
    231   1.67       dsl 			if (*cp != 0)
    232   1.67       dsl 				errx(1,"invalid number %s", optarg);
    233    1.1       cgd 			break;
    234   1.43    atatat 		case 'N':
    235   1.43    atatat 			numeric++;
    236   1.43    atatat 			break;
    237    1.1       cgd 		case 'n':
    238   1.36  christos 			plain++;
    239    1.1       cgd 			break;
    240    1.1       cgd 		case 'R':
    241  1.127       uwe 			timestamp |= TIMESTAMP_RELATIVE;
    242    1.1       cgd 			break;
    243    1.1       cgd 		case 'T':
    244  1.127       uwe 			timestamp |= TIMESTAMP_ABSOLUTE;
    245    1.1       cgd 			break;
    246    1.1       cgd 		case 't':
    247   1.58       dsl 			trset = 1;
    248   1.58       dsl 			trpoints = getpoints(trpoints, optarg);
    249    1.4   mycroft 			if (trpoints < 0)
    250    1.4   mycroft 				errx(1, "unknown trace point in %s", optarg);
    251    1.1       cgd 			break;
    252   1.52       dsl 		case 'x':
    253   1.66      manu 			word_size = 1;
    254   1.66      manu 			break;
    255   1.66      manu 		case 'X':
    256   1.65       dsl 			word_size = strtoul(optarg, &cp, 0);
    257   1.65       dsl 			if (*cp != 0 || word_size & (word_size - 1) ||
    258   1.67       dsl 			    word_size > 16 || word_size <= 0)
    259   1.66      manu 				errx(1, "argument to -X must be "
    260   1.66      manu 				    "1, 2, 4, 8 or 16");
    261   1.52       dsl 			break;
    262    1.1       cgd 		default:
    263    1.1       cgd 			usage();
    264    1.1       cgd 		}
    265   1.67       dsl 	}
    266    1.1       cgd 	argv += optind;
    267    1.1       cgd 	argc -= optind;
    268    1.1       cgd 
    269   1.58       dsl 	if (!trset)
    270   1.58       dsl 		trpoints = ALL_POINTS;
    271   1.58       dsl 
    272   1.41    atatat 	if (tracefile == NULL) {
    273   1.41    atatat 		if (argc == 1) {
    274   1.41    atatat 			tracefile = argv[0];
    275   1.41    atatat 			argv++;
    276   1.41    atatat 			argc--;
    277   1.76     enami 		} else
    278   1.41    atatat 			tracefile = DEF_TRACEFILE;
    279   1.41    atatat 	}
    280   1.41    atatat 
    281   1.41    atatat 	if (argc > 0)
    282    1.1       cgd 		usage();
    283    1.1       cgd 
    284   1.27  jdolecek 	setemul(emul_name, 0, 0);
    285   1.27  jdolecek 
    286   1.27  jdolecek 	m = malloc(size = 1024);
    287    1.4   mycroft 	if (m == NULL)
    288   1.27  jdolecek 		errx(1, "malloc: %s", strerror(ENOMEM));
    289    1.4   mycroft 	if (!freopen(tracefile, "r", stdin))
    290    1.4   mycroft 		err(1, "%s", tracefile);
    291   1.81  christos 	while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
    292   1.76     enami 		if (trpoints & (1 << ktr_header.ktr_type) &&
    293   1.76     enami 		    (do_pid == -1 || ktr_header.ktr_pid == do_pid))
    294   1.58       dsl 			col = dumpheader(&ktr_header);
    295   1.58       dsl 		else
    296   1.58       dsl 			col = -1;
    297  1.123     alnsn 		if ((ktrlen = ktr_header.ktr_len) > INT_MAX)
    298    1.4   mycroft 			errx(1, "bogus length 0x%x", ktrlen);
    299    1.1       cgd 		if (ktrlen > size) {
    300   1.58       dsl 			while (ktrlen > size)
    301   1.58       dsl 				size *= 2;
    302   1.58       dsl 			m = realloc(m, size);
    303    1.4   mycroft 			if (m == NULL)
    304   1.27  jdolecek 				errx(1, "realloc: %s", strerror(ENOMEM));
    305    1.1       cgd 		}
    306    1.4   mycroft 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
    307    1.4   mycroft 			errx(1, "data too short");
    308   1.58       dsl 		if (col == -1)
    309    1.1       cgd 			continue;
    310   1.27  jdolecek 
    311   1.27  jdolecek 		/* update context to match currently processed record */
    312   1.27  jdolecek 		ectx_sanify(ktr_header.ktr_pid);
    313   1.27  jdolecek 
    314    1.1       cgd 		switch (ktr_header.ktr_type) {
    315    1.1       cgd 		case KTR_SYSCALL:
    316   1.58       dsl 			ktrsyscall(m);
    317    1.1       cgd 			break;
    318    1.1       cgd 		case KTR_SYSRET:
    319   1.58       dsl 			ktrsysret(m, ktrlen);
    320    1.1       cgd 			break;
    321    1.1       cgd 		case KTR_NAMEI:
    322    1.1       cgd 			ktrnamei(m, ktrlen);
    323    1.1       cgd 			break;
    324    1.1       cgd 		case KTR_GENIO:
    325   1.58       dsl 			ktrgenio(m, ktrlen);
    326    1.1       cgd 			break;
    327    1.1       cgd 		case KTR_PSIG:
    328   1.60  christos 			ktrpsig(m, ktrlen);
    329    1.1       cgd 			break;
    330    1.1       cgd 		case KTR_CSW:
    331   1.58       dsl 			ktrcsw(m);
    332    1.1       cgd 			break;
    333   1.11  christos 		case KTR_EMUL:
    334   1.27  jdolecek 			ktremul(m, ktrlen, size);
    335   1.11  christos 			break;
    336   1.32  jdolecek 		case KTR_USER:
    337   1.58       dsl 			ktruser(m, ktrlen);
    338   1.32  jdolecek 			break;
    339   1.58       dsl 		case KTR_EXEC_ARG:
    340   1.58       dsl 		case KTR_EXEC_ENV:
    341   1.58       dsl 			visdump_buf(m, ktrlen, col);
    342   1.58       dsl 			break;
    343  1.112     alnsn 		case KTR_EXEC_FD:
    344  1.112     alnsn 			ktrexecfd(m);
    345  1.112     alnsn 			break;
    346   1.86      manu 		case KTR_MIB:
    347   1.86      manu 			ktrmib(m, ktrlen);
    348   1.86      manu 			break;
    349   1.58       dsl 		default:
    350   1.72       mrg 			putchar('\n');
    351   1.79       dsl 			hexdump_buf(m, ktrlen, word_size ? word_size : 1);
    352    1.1       cgd 		}
    353    1.1       cgd 		if (tail)
    354    1.1       cgd 			(void)fflush(stdout);
    355    1.1       cgd 	}
    356   1.17     mikel 	return (0);
    357    1.1       cgd }
    358    1.1       cgd 
    359  1.133     kamil static void
    360  1.134  christos fmtprint(const char *fmt, const struct ioctlinfo *ii)
    361  1.133     kamil {
    362  1.133     kamil 	int c;
    363  1.133     kamil 
    364  1.134  christos 
    365  1.133     kamil 	while ((c = *fmt++) != '\0') {
    366  1.133     kamil 		switch (c) {
    367  1.133     kamil 		default:
    368  1.133     kamil 			putchar(c);
    369  1.133     kamil 			continue;
    370  1.133     kamil 		case '\\':
    371  1.133     kamil 			switch (c = *fmt) {
    372  1.133     kamil 				case '\0':
    373  1.133     kamil 				continue;
    374  1.133     kamil 			case 'n':
    375  1.133     kamil 				putchar('\n');
    376  1.133     kamil 				break;
    377  1.133     kamil 			case 't':
    378  1.133     kamil 				putchar('\t');
    379  1.133     kamil 				break;
    380  1.133     kamil 			}
    381  1.133     kamil 			break;
    382  1.133     kamil 		case '%':
    383  1.133     kamil 			switch (c = *fmt) {
    384  1.133     kamil 			case '\0':
    385  1.133     kamil 				continue;
    386  1.133     kamil 			case '%':
    387  1.133     kamil 			default:
    388  1.133     kamil 				putchar(c);
    389  1.133     kamil 				break;
    390  1.134  christos 			case 'E':
    391  1.134  christos 				printf("%s", ii->expr);
    392  1.134  christos 				break;
    393  1.133     kamil 			case 'e':
    394  1.134  christos 				ioctldecode(ii->value);
    395  1.133     kamil 				break;
    396  1.133     kamil 			case 'n':
    397  1.134  christos 				printf("%s", ii->name);
    398  1.133     kamil 				break;
    399  1.133     kamil 			case 'x':
    400  1.134  christos 				printf("%#lx", ii->value);
    401  1.133     kamil 				break;
    402  1.133     kamil 			case 'o':
    403  1.134  christos 				printf("%#lo", ii->value);
    404  1.133     kamil 				break;
    405  1.133     kamil 			case 'd': case 'i':
    406  1.134  christos 				printf("%ld", ii->value);
    407  1.133     kamil 				break;
    408  1.133     kamil 			}
    409  1.133     kamil 			break;
    410  1.133     kamil 		}
    411  1.133     kamil 		++fmt;
    412  1.133     kamil 	}
    413  1.133     kamil }
    414  1.133     kamil 
    415   1.81  christos static int
    416   1.81  christos fread_tail(void *buf, size_t num, size_t size)
    417    1.1       cgd {
    418    1.1       cgd 	int i;
    419    1.1       cgd 
    420    1.1       cgd 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
    421    1.1       cgd 		(void)sleep(1);
    422    1.1       cgd 		clearerr(stdin);
    423    1.1       cgd 	}
    424    1.1       cgd 	return (i);
    425    1.1       cgd }
    426    1.1       cgd 
    427   1.81  christos static int
    428   1.81  christos dumpheader(struct ktr_header *kth)
    429    1.1       cgd {
    430   1.81  christos 	char unknown[64];
    431   1.81  christos 	const char *type;
    432  1.127       uwe 	static struct timespec starttime, prevtime;
    433  1.126       uwe 	struct timespec temp;
    434   1.58       dsl 	int col;
    435    1.1       cgd 
    436  1.126       uwe 	if (__predict_false(kth->ktr_version != KTRFAC_VERSION(KTRFACv2)))
    437  1.126       uwe 		errx(EXIT_FAILURE, "Unsupported ktrace version %x",
    438  1.126       uwe 		     kth->ktr_version);
    439  1.126       uwe 
    440    1.1       cgd 	switch (kth->ktr_type) {
    441    1.1       cgd 	case KTR_SYSCALL:
    442    1.1       cgd 		type = "CALL";
    443    1.1       cgd 		break;
    444    1.1       cgd 	case KTR_SYSRET:
    445    1.1       cgd 		type = "RET ";
    446    1.1       cgd 		break;
    447    1.1       cgd 	case KTR_NAMEI:
    448    1.1       cgd 		type = "NAMI";
    449    1.1       cgd 		break;
    450    1.1       cgd 	case KTR_GENIO:
    451    1.1       cgd 		type = "GIO ";
    452    1.1       cgd 		break;
    453    1.1       cgd 	case KTR_PSIG:
    454    1.1       cgd 		type = "PSIG";
    455    1.1       cgd 		break;
    456    1.1       cgd 	case KTR_CSW:
    457   1.60  christos 		type = "CSW ";
    458    1.1       cgd 		break;
    459   1.11  christos 	case KTR_EMUL:
    460   1.11  christos 		type = "EMUL";
    461   1.11  christos 		break;
    462   1.32  jdolecek 	case KTR_USER:
    463   1.94       dsl 		type = "MISC";
    464   1.32  jdolecek 		break;
    465   1.58       dsl 	case KTR_EXEC_ENV:
    466   1.58       dsl 		type = "ENV";
    467   1.58       dsl 		break;
    468   1.58       dsl 	case KTR_EXEC_ARG:
    469   1.58       dsl 		type = "ARG";
    470   1.58       dsl 		break;
    471  1.112     alnsn 	case KTR_EXEC_FD:
    472  1.112     alnsn 		type = "FD";
    473  1.112     alnsn 		break;
    474   1.83  christos 	case KTR_SAUPCALL:
    475   1.83  christos 		type = "SAU";
    476   1.83  christos 		break;
    477   1.86      manu 	case KTR_MIB:
    478   1.86      manu 		type = "MIB";
    479   1.86      manu 		break;
    480    1.1       cgd 	default:
    481   1.81  christos 		(void)snprintf(unknown, sizeof(unknown), "UNKNOWN(%d)",
    482   1.81  christos 		    kth->ktr_type);
    483    1.1       cgd 		type = unknown;
    484    1.1       cgd 	}
    485    1.1       cgd 
    486  1.126       uwe 	col = printf("%6d %6d ", kth->ktr_pid, kth->ktr_lid);
    487   1.83  christos 	col += printf("%-8.*s ", MAXCOMLEN, kth->ktr_comm);
    488    1.1       cgd 	if (timestamp) {
    489  1.127       uwe 		if (timestamp & TIMESTAMP_ABSOLUTE) {
    490  1.127       uwe 			temp.tv_sec = kth->ktr_ts.tv_sec;
    491  1.127       uwe 			temp.tv_nsec = kth->ktr_ts.tv_nsec;
    492  1.128       uwe 			col += output_ts(&temp);
    493  1.127       uwe 		}
    494  1.127       uwe 
    495  1.127       uwe 		if (timestamp & TIMESTAMP_ELAPSED) {
    496  1.127       uwe 			if (starttime.tv_sec == 0) {
    497  1.127       uwe 				starttime.tv_sec = kth->ktr_ts.tv_sec;
    498  1.127       uwe 				starttime.tv_nsec = kth->ktr_ts.tv_nsec;
    499  1.127       uwe 				temp.tv_sec = temp.tv_nsec = 0;
    500  1.127       uwe 			} else
    501  1.127       uwe 				timespecsub(&kth->ktr_ts, &starttime, &temp);
    502  1.128       uwe 			col += output_ts(&temp);
    503  1.127       uwe 		}
    504  1.127       uwe 
    505  1.127       uwe 		if (timestamp & TIMESTAMP_RELATIVE) {
    506  1.126       uwe 			if (prevtime.tv_sec == 0)
    507  1.126       uwe 				temp.tv_sec = temp.tv_nsec = 0;
    508  1.126       uwe 			else
    509  1.126       uwe 				timespecsub(&kth->ktr_ts, &prevtime, &temp);
    510  1.126       uwe 			prevtime.tv_sec = kth->ktr_ts.tv_sec;
    511  1.126       uwe 			prevtime.tv_nsec = kth->ktr_ts.tv_nsec;
    512  1.128       uwe 			col += output_ts(&temp);
    513   1.83  christos 		}
    514    1.1       cgd 	}
    515   1.58       dsl 	col += printf("%-4s  ", type);
    516   1.58       dsl 	return col;
    517    1.1       cgd }
    518    1.1       cgd 
    519  1.128       uwe static int
    520  1.128       uwe output_ts(const struct timespec *ts)
    521  1.128       uwe {
    522  1.128       uwe 	int col;
    523  1.128       uwe 
    524  1.128       uwe 	if (__predict_true(ts->tv_sec >= 0))
    525  1.128       uwe 	    col = printf("%lld.%09ld ",
    526  1.128       uwe 			 (long long)ts->tv_sec, (long)ts->tv_nsec);
    527  1.128       uwe 	else {
    528  1.128       uwe 	    /*
    529  1.128       uwe 	     * The time represented by a timespec object ts is always
    530  1.128       uwe 	     *
    531  1.128       uwe 	     *   ts.tv_sec + ts.tv_nsec * 1e-9
    532  1.128       uwe 	     *
    533  1.128       uwe 	     * where ts.tv_sec may be negative but ts.tv_nsec is
    534  1.128       uwe 	     * always in [0, 1e9).  So, for example, -1/4 second is
    535  1.128       uwe 	     * represented by the struct timespec object
    536  1.128       uwe 	     *
    537  1.128       uwe 	     *   { .tv_sec = -1, .tv_nsec = 750000000 }
    538  1.128       uwe 	     */
    539  1.128       uwe 	    const struct timespec zero_ts = { 0, 0 };
    540  1.128       uwe 	    struct timespec abs_ts;
    541  1.128       uwe 	    timespecsub(&zero_ts, ts, &abs_ts);
    542  1.128       uwe 	    col = printf("-%lld.%09ld ",
    543  1.128       uwe 			 (long long)abs_ts.tv_sec, (long)abs_ts.tv_nsec);
    544  1.128       uwe 	}
    545  1.128       uwe 	return col;
    546  1.128       uwe }
    547  1.128       uwe 
    548   1.81  christos static void
    549   1.81  christos output_long(u_long it, int as_x)
    550   1.72       mrg {
    551   1.72       mrg 	if (cur_emul->flags & EMUL_FLAG_NETBSD32)
    552   1.72       mrg 		printf(as_x ? "%#x" : "%d", (u_int)it);
    553   1.72       mrg 	else
    554   1.72       mrg 		printf(as_x ? "%#lx" : "%ld", it);
    555   1.72       mrg }
    556   1.72       mrg 
    557  1.121  christos static const char *
    558  1.121  christos fcntlname(u_long cmd)
    559  1.121  christos {
    560  1.121  christos 	switch (cmd) {
    561  1.143  christos 	CASERETURN(F_DUPFD);
    562  1.143  christos 	CASERETURN(F_GETFD);
    563  1.143  christos 	CASERETURN(F_SETFD);
    564  1.143  christos 	CASERETURN(F_GETFL);
    565  1.143  christos 	CASERETURN(F_SETFL);
    566  1.143  christos 	CASERETURN(F_GETOWN);
    567  1.143  christos 	CASERETURN(F_SETOWN);
    568  1.143  christos 	CASERETURN(F_GETLK);
    569  1.143  christos 	CASERETURN(F_SETLK);
    570  1.143  christos 	CASERETURN(F_SETLKW);
    571  1.143  christos 	CASERETURN(F_CLOSEM);
    572  1.143  christos 	CASERETURN(F_MAXFD);
    573  1.143  christos 	CASERETURN(F_DUPFD_CLOEXEC);
    574  1.143  christos 	CASERETURN(F_GETNOSIGPIPE);
    575  1.143  christos 	CASERETURN(F_SETNOSIGPIPE);
    576  1.143  christos 	default:
    577  1.143  christos 		return NULL;
    578  1.143  christos 	}
    579  1.143  christos }
    580  1.143  christos 
    581  1.143  christos static const char *
    582  1.143  christos sockproto(register_t proto)
    583  1.143  christos {
    584  1.143  christos 	switch (proto) {
    585  1.143  christos 	CASERETURN(IPPROTO_IP);
    586  1.143  christos 	CASERETURN(IPPROTO_ICMP);
    587  1.143  christos 	CASERETURN(IPPROTO_IGMP);
    588  1.143  christos 	CASERETURN(IPPROTO_GGP);
    589  1.143  christos //	CASERETURN(IPPROTO_IPV4);
    590  1.143  christos 	CASERETURN(IPPROTO_IPIP);
    591  1.143  christos 	CASERETURN(IPPROTO_TCP);
    592  1.143  christos 	CASERETURN(IPPROTO_EGP);
    593  1.143  christos 	CASERETURN(IPPROTO_PUP);
    594  1.143  christos 	CASERETURN(IPPROTO_UDP);
    595  1.143  christos 	CASERETURN(IPPROTO_IDP);
    596  1.143  christos 	CASERETURN(IPPROTO_TP);
    597  1.143  christos 	CASERETURN(IPPROTO_DCCP);
    598  1.143  christos 	CASERETURN(IPPROTO_IPV6);
    599  1.143  christos 	CASERETURN(IPPROTO_ROUTING);
    600  1.143  christos 	CASERETURN(IPPROTO_FRAGMENT);
    601  1.143  christos 	CASERETURN(IPPROTO_RSVP);
    602  1.143  christos 	CASERETURN(IPPROTO_GRE);
    603  1.143  christos 	CASERETURN(IPPROTO_ESP);
    604  1.143  christos 	CASERETURN(IPPROTO_AH);
    605  1.143  christos 	CASERETURN(IPPROTO_MOBILE);
    606  1.143  christos //	CASERETURN(IPPROTO_IPV6_ICMP);
    607  1.143  christos 	CASERETURN(IPPROTO_ICMPV6);
    608  1.143  christos 	CASERETURN(IPPROTO_NONE);
    609  1.143  christos 	CASERETURN(IPPROTO_DSTOPTS);
    610  1.143  christos 	CASERETURN(IPPROTO_EON);
    611  1.143  christos 	CASERETURN(IPPROTO_ETHERIP);
    612  1.143  christos 	CASERETURN(IPPROTO_ENCAP);
    613  1.143  christos 	CASERETURN(IPPROTO_PIM);
    614  1.143  christos 	CASERETURN(IPPROTO_IPCOMP);
    615  1.143  christos 	CASERETURN(IPPROTO_VRRP);
    616  1.143  christos //	CASERETURN(IPPROTO_CARP);
    617  1.143  christos 	CASERETURN(IPPROTO_L2TP);
    618  1.143  christos 	CASERETURN(IPPROTO_SCTP);
    619  1.143  christos 	CASERETURN(IPPROTO_PFSYNC);
    620  1.143  christos 	CASERETURN(IPPROTO_RAW);
    621  1.143  christos 	CASERETURN(IPPROTO_MAX);
    622  1.143  christos 	CASERETURN(IPPROTO_DONE);
    623  1.143  christos 	CASERETURN(SOL_SOCKET);
    624  1.143  christos 	default:
    625  1.143  christos 		return NULL;
    626  1.143  christos 	}
    627  1.143  christos }
    628  1.143  christos 
    629  1.143  christos static const char *
    630  1.143  christos sockoptname(register_t optname)
    631  1.143  christos {
    632  1.143  christos 	switch (optname) {
    633  1.143  christos 	CASERETURN(SO_ACCEPTCONN);
    634  1.143  christos 	CASERETURN(SO_ACCEPTFILTER);
    635  1.143  christos 	CASERETURN(SO_BROADCAST);
    636  1.143  christos 	CASERETURN(SO_DEBUG);
    637  1.143  christos 	CASERETURN(SO_DONTROUTE);
    638  1.143  christos 	CASERETURN(SO_ERROR);
    639  1.143  christos 	CASERETURN(SO_KEEPALIVE);
    640  1.143  christos 	CASERETURN(SO_LINGER);
    641  1.143  christos 	CASERETURN(SO_NOHEADER);
    642  1.143  christos 	CASERETURN(SO_NOSIGPIPE);
    643  1.143  christos 	CASERETURN(SO_OOBINLINE);
    644  1.143  christos 	CASERETURN(SO_OVERFLOWED);
    645  1.143  christos 	CASERETURN(SO_RCVBUF);
    646  1.143  christos 	CASERETURN(SO_RCVLOWAT);
    647  1.143  christos 	CASERETURN(SO_RCVTIMEO);
    648  1.143  christos 	CASERETURN(SO_RERROR);
    649  1.143  christos 	CASERETURN(SO_REUSEADDR);
    650  1.143  christos 	CASERETURN(SO_REUSEPORT);
    651  1.143  christos 	CASERETURN(SO_SNDBUF);
    652  1.143  christos 	CASERETURN(SO_SNDLOWAT);
    653  1.143  christos 	CASERETURN(SO_SNDTIMEO);
    654  1.143  christos 	CASERETURN(SO_TIMESTAMP);
    655  1.143  christos 	CASERETURN(SO_TYPE);
    656  1.143  christos 	CASERETURN(SO_USELOOPBACK);
    657  1.143  christos 	default:
    658  1.143  christos 		return NULL;
    659  1.143  christos 	}
    660  1.143  christos }
    661  1.143  christos 
    662  1.143  christos static const char *
    663  1.143  christos tcpoptname(register_t optname)
    664  1.143  christos {
    665  1.143  christos 	switch (optname) {
    666  1.143  christos 	CASERETURN(TCP_NODELAY);
    667  1.143  christos 	CASERETURN(TCP_MAXSEG);
    668  1.143  christos 	CASERETURN(TCP_MD5SIG);
    669  1.143  christos 	CASERETURN(TCP_KEEPIDLE);
    670  1.143  christos 	CASERETURN(TCP_KEEPINTVL);
    671  1.143  christos 	CASERETURN(TCP_KEEPCNT);
    672  1.143  christos 	CASERETURN(TCP_KEEPINIT);
    673  1.143  christos 	CASERETURN(TCP_INFO);
    674  1.143  christos 	default:
    675  1.143  christos 		return NULL;
    676  1.143  christos 	}
    677  1.143  christos }
    678  1.143  christos 
    679  1.143  christos static const char *
    680  1.143  christos ipoptname(register_t optname)
    681  1.143  christos {
    682  1.143  christos 	switch (optname) {
    683  1.143  christos 	CASERETURN(IP_OPTIONS);
    684  1.143  christos 	CASERETURN(IP_HDRINCL);
    685  1.143  christos 	CASERETURN(IP_TOS);
    686  1.143  christos 	CASERETURN(IP_TTL);
    687  1.143  christos 	CASERETURN(IP_RECVOPTS);
    688  1.143  christos 	CASERETURN(IP_RECVRETOPTS);
    689  1.143  christos 	CASERETURN(IP_RECVDSTADDR);
    690  1.143  christos 	CASERETURN(IP_RETOPTS);
    691  1.143  christos 	CASERETURN(IP_MULTICAST_IF);
    692  1.143  christos 	CASERETURN(IP_MULTICAST_TTL);
    693  1.143  christos 	CASERETURN(IP_MULTICAST_LOOP);
    694  1.143  christos 	CASERETURN(IP_ADD_MEMBERSHIP);
    695  1.143  christos 	CASERETURN(IP_DROP_MEMBERSHIP);
    696  1.143  christos 	CASERETURN(IP_PORTALGO);
    697  1.143  christos 	CASERETURN(IP_PORTRANGE);
    698  1.143  christos 	CASERETURN(IP_RECVIF);
    699  1.143  christos 	CASERETURN(IP_ERRORMTU);
    700  1.143  christos 	CASERETURN(IP_IPSEC_POLICY);
    701  1.143  christos 	CASERETURN(IP_RECVTTL);
    702  1.143  christos 	CASERETURN(IP_MINTTL);
    703  1.143  christos 	CASERETURN(IP_PKTINFO);
    704  1.143  christos 	CASERETURN(IP_RECVPKTINFO);
    705  1.143  christos 	CASERETURN(IP_BINDANY);
    706  1.121  christos 	default:
    707  1.121  christos 		return NULL;
    708  1.121  christos 	}
    709  1.121  christos }
    710  1.121  christos 
    711   1.81  christos static void
    712   1.81  christos ioctldecode(u_long cmd)
    713   1.13  christos {
    714   1.13  christos 	char dirbuf[4], *dir = dirbuf;
    715   1.90     njoly 	int c;
    716   1.13  christos 
    717  1.142       kre 	if (~0xffffffffULL & cmd) {
    718  1.141  christos 		output_long(cmd, 1);
    719  1.141  christos 		return;
    720  1.141  christos 	}
    721  1.141  christos 
    722   1.15       cgd 	if (cmd & IOC_IN)
    723   1.15       cgd 		*dir++ = 'W';
    724   1.13  christos 	if (cmd & IOC_OUT)
    725   1.13  christos 		*dir++ = 'R';
    726   1.13  christos 	*dir = '\0';
    727   1.13  christos 
    728   1.90     njoly 	c = (cmd >> 8) & 0xff;
    729   1.90     njoly 	if (isprint(c))
    730  1.104  christos 		printf("_IO%s('%c',", dirbuf, c);
    731   1.90     njoly 	else
    732  1.104  christos 		printf("_IO%s(0x%02x,", dirbuf, c);
    733   1.72       mrg 	output_long(cmd & 0xff, decimal == 0);
    734   1.72       mrg 	if ((cmd & IOC_VOID) == 0) {
    735   1.72       mrg 		putchar(',');
    736   1.85  christos 		output_long(IOCPARM_LEN(cmd), decimal == 0);
    737   1.72       mrg 	}
    738   1.72       mrg 	putchar(')');
    739   1.13  christos }
    740    1.1       cgd 
    741   1.81  christos static void
    742  1.130  christos putprot(int pr)
    743  1.130  christos {
    744  1.130  christos 	const char *s = "";
    745  1.130  christos 
    746  1.130  christos 	if (pr == PROT_NONE) {
    747  1.130  christos 		fputs("PROT_NONE", stdout);
    748  1.130  christos 		return;
    749  1.130  christos 	}
    750  1.130  christos 
    751  1.130  christos 	if (pr & PROT_READ) {
    752  1.130  christos 		fputs("PROT_READ", stdout);
    753  1.130  christos 		s = "|";
    754  1.130  christos 		pr &= ~PROT_READ;
    755  1.130  christos 	}
    756  1.130  christos 
    757  1.130  christos 	if (pr & PROT_WRITE) {
    758  1.130  christos 		printf("%sPROT_WRITE", s);
    759  1.130  christos 		pr &= ~PROT_WRITE;
    760  1.130  christos 		s = "|";
    761  1.130  christos 	}
    762  1.130  christos 	if (pr & PROT_EXEC) {
    763  1.130  christos 		printf("%sPROT_EXEC", s);
    764  1.130  christos 		pr &= ~PROT_EXEC;
    765  1.130  christos 		s = "|";
    766  1.130  christos 	}
    767  1.130  christos 	if (pr) {
    768  1.130  christos 		printf("%s%#lx", s, (long)pr);
    769  1.130  christos 	}
    770  1.130  christos }
    771  1.130  christos 
    772  1.138   thorpej static const char *
    773  1.138   thorpej futex_op_name(u_long op)
    774  1.138   thorpej {
    775  1.138   thorpej 	switch (op & FUTEX_CMD_MASK) {
    776  1.143  christos 	CASERETURN(FUTEX_WAIT);
    777  1.143  christos 	CASERETURN(FUTEX_WAKE);
    778  1.143  christos 	CASERETURN(FUTEX_FD);
    779  1.143  christos 	CASERETURN(FUTEX_REQUEUE);
    780  1.143  christos 	CASERETURN(FUTEX_CMP_REQUEUE);
    781  1.143  christos 	CASERETURN(FUTEX_WAKE_OP);
    782  1.143  christos 	CASERETURN(FUTEX_LOCK_PI);
    783  1.143  christos 	CASERETURN(FUTEX_UNLOCK_PI);
    784  1.143  christos 	CASERETURN(FUTEX_TRYLOCK_PI);
    785  1.143  christos 	CASERETURN(FUTEX_WAIT_BITSET);
    786  1.143  christos 	CASERETURN(FUTEX_WAKE_BITSET);
    787  1.143  christos 	CASERETURN(FUTEX_WAIT_REQUEUE_PI);
    788  1.143  christos 	CASERETURN(FUTEX_CMP_REQUEUE_PI);
    789  1.138   thorpej 	default:
    790  1.138   thorpej 		return NULL;
    791  1.138   thorpej 	}
    792  1.143  christos #undef CASERETURN
    793  1.138   thorpej }
    794  1.138   thorpej 
    795  1.138   thorpej static void
    796  1.138   thorpej futexput(u_long op)
    797  1.138   thorpej {
    798  1.138   thorpej 	const char *opname = futex_op_name(op);
    799  1.138   thorpej 	const char *s = "";
    800  1.138   thorpej 
    801  1.138   thorpej 	if (opname == NULL) {
    802  1.139    martin 		printf("%#lx", op & (u_long)FUTEX_CMD_MASK);
    803  1.138   thorpej 	} else {
    804  1.138   thorpej 		fputs(opname, stdout);
    805  1.138   thorpej 	}
    806  1.138   thorpej 	op &= ~FUTEX_CMD_MASK;
    807  1.138   thorpej 
    808  1.138   thorpej 	if (op & FUTEX_PRIVATE_FLAG) {
    809  1.138   thorpej 		fputs("_PRIVATE", stdout);
    810  1.138   thorpej 		op &= ~FUTEX_PRIVATE_FLAG;
    811  1.138   thorpej 	}
    812  1.138   thorpej 
    813  1.138   thorpej 	if (op & FUTEX_CLOCK_REALTIME) {
    814  1.138   thorpej 		printf("%sFUTEX_CLOCK_REALTIME", s);
    815  1.138   thorpej 		op &= ~FUTEX_CLOCK_REALTIME;
    816  1.138   thorpej 		s = "|";
    817  1.138   thorpej 	}
    818  1.138   thorpej 
    819  1.138   thorpej 	if (op) {
    820  1.138   thorpej 		printf("%s%#lx", s, op);
    821  1.138   thorpej 	}
    822  1.138   thorpej }
    823  1.138   thorpej 
    824  1.130  christos static void
    825   1.81  christos ktrsyscall(struct ktr_syscall *ktr)
    826    1.1       cgd {
    827   1.71       mrg 	int argcount;
    828   1.67       dsl 	const struct emulation *emul = cur_emul;
    829   1.18     lukem 	register_t *ap;
    830   1.67       dsl 	char c;
    831   1.81  christos 	const char *cp;
    832   1.67       dsl 	const char *sys_name;
    833    1.1       cgd 
    834   1.72       mrg 	argcount = ktr->ktr_argsize / sizeof (*ap);
    835   1.72       mrg 
    836   1.67       dsl 	emul_changed = 0;
    837   1.67       dsl 
    838   1.72       mrg 	if (numeric ||
    839  1.110     joerg 	    ((ktr->ktr_code >= emul->nsysnames || ktr->ktr_code < 0))) {
    840   1.67       dsl 		sys_name = "?";
    841    1.1       cgd 		(void)printf("[%d]", ktr->ktr_code);
    842   1.67       dsl 	} else {
    843   1.67       dsl 		sys_name = emul->sysnames[ktr->ktr_code];
    844   1.67       dsl 		(void)printf("%s", sys_name);
    845   1.67       dsl 	}
    846   1.72       mrg #define NETBSD32_	"netbsd32_"
    847   1.72       mrg 	if (cur_emul->flags & EMUL_FLAG_NETBSD32) {
    848   1.72       mrg 		size_t len = strlen(NETBSD32_);
    849   1.72       mrg 		if (strncmp(sys_name, NETBSD32_, len) == 0)
    850   1.72       mrg 			sys_name += len;
    851   1.72       mrg 	}
    852   1.72       mrg #undef NETBSD32_
    853   1.76     enami 
    854    1.7       cgd 	ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
    855   1.67       dsl 	if (argcount) {
    856   1.67       dsl 		c = '(';
    857   1.67       dsl 		if (plain) {
    858   1.67       dsl 			;
    859   1.67       dsl 
    860  1.105       chs 		} else if (strcmp(sys_name, "exit_group") == 0 ||
    861  1.105       chs 			   (strcmp(emul->name, "linux") != 0 &&
    862  1.105       chs 			    strcmp(emul->name, "linux32") != 0 &&
    863  1.105       chs 			    strcmp(sys_name, "exit") == 0)) {
    864   1.67       dsl 			ectx_delete();
    865   1.67       dsl 
    866   1.72       mrg 		} else if (strcmp(sys_name, "ioctl") == 0 && argcount >= 2) {
    867   1.72       mrg 			(void)putchar('(');
    868   1.73  christos 			output_long((long)*ap, !(decimal || small(*ap)));
    869   1.67       dsl 			ap++;
    870   1.67       dsl 			argcount--;
    871   1.67       dsl 			if ((cp = ioctlname(*ap)) != NULL)
    872   1.67       dsl 				(void)printf(",%s", cp);
    873  1.104  christos 			else {
    874  1.104  christos 				(void)putchar(',');
    875   1.67       dsl 				ioctldecode(*ap);
    876  1.104  christos 			}
    877   1.67       dsl 			ap++;
    878   1.67       dsl 			argcount--;
    879   1.67       dsl 			c = ',';
    880   1.67       dsl 
    881  1.121  christos 		} else if (strcmp(sys_name, "fcntl") == 0 && argcount >= 2) {
    882  1.121  christos 			(void)putchar('(');
    883  1.121  christos 			output_long((long)*ap, !(decimal || small(*ap)));
    884  1.121  christos 			ap++;
    885  1.121  christos 			argcount--;
    886  1.121  christos 			if ((cp = fcntlname(*ap)) != NULL)
    887  1.121  christos 				(void)printf(",%s", cp);
    888  1.121  christos 			else {
    889  1.122    martin 				(void)printf(",%#lx", (unsigned long)*ap);
    890  1.121  christos 			}
    891  1.121  christos 			ap++;
    892  1.121  christos 			argcount--;
    893  1.121  christos 			c = ',';
    894  1.138   thorpej 
    895  1.143  christos 		} else if ((strcmp(sys_name, "setsockopt") == 0 ||
    896  1.143  christos 		    strcmp(sys_name, "getsockopt") == 0 ||
    897  1.143  christos 		    strcmp(sys_name, "getsockopt2") == 0) && argcount >= 3) {
    898  1.143  christos 			(void)putchar('(');
    899  1.143  christos 			output_long((long)*ap, !(decimal || small(*ap)));
    900  1.143  christos 			ap++;
    901  1.143  christos 			argcount--;
    902  1.143  christos 			register_t level = *ap;
    903  1.143  christos 			if ((cp = sockproto(level)) != NULL) {
    904  1.143  christos 				(void)printf(",%s", cp);
    905  1.143  christos 			} else {
    906  1.143  christos 				output_long((long)*ap,
    907  1.143  christos 				    !(decimal || small(*ap)));
    908  1.143  christos 			}
    909  1.143  christos 			ap++;
    910  1.143  christos 			argcount--;
    911  1.143  christos 			const char *(*f)(register_t);
    912  1.143  christos 			switch (level) {
    913  1.143  christos 			case SOL_SOCKET:
    914  1.143  christos 				f = sockoptname;
    915  1.143  christos 				break;
    916  1.143  christos 			case IPPROTO_IP:
    917  1.143  christos 				f = ipoptname;
    918  1.143  christos 				break;
    919  1.143  christos 			case IPPROTO_TCP:
    920  1.143  christos 				f = tcpoptname;
    921  1.143  christos 				break;
    922  1.143  christos 			default:
    923  1.143  christos 				f = NULL;
    924  1.143  christos 				break;
    925  1.143  christos 			}
    926  1.143  christos 
    927  1.143  christos 			if (f && (cp = (*f)(*ap)) != NULL)
    928  1.143  christos 				(void)printf(",%s", cp);
    929  1.143  christos 			else {
    930  1.143  christos 				(void)putchar(',');
    931  1.143  christos 				output_long((long)*ap,
    932  1.143  christos 				    !(decimal || small(*ap)));
    933  1.143  christos 			}
    934  1.143  christos 			ap++;
    935  1.143  christos 			argcount--;
    936  1.143  christos 			c = ',';
    937  1.144    rillig 
    938  1.138   thorpej 		} else if ((strcmp(sys_name, "futex") == 0 ||
    939  1.138   thorpej 			    strcmp(sys_name, "__futex") == 0) &&
    940  1.138   thorpej 			   argcount > 2) {
    941  1.144    rillig 			/*
    942  1.144    rillig 			 * Linux name is "futex".
    943  1.144    rillig 			 * Native name is "__futex".
    944  1.144    rillig 			 * Both have the same op argument.
    945  1.144    rillig 			 */
    946  1.138   thorpej 			(void)putchar('(');
    947  1.138   thorpej 			output_long((long)*ap, 1);
    948  1.138   thorpej 			(void)putchar(',');
    949  1.138   thorpej 			ap++;
    950  1.138   thorpej 			argcount--;
    951  1.138   thorpej 			futexput(*ap);
    952  1.138   thorpej 			ap++;
    953  1.138   thorpej 			argcount--;
    954  1.138   thorpej 			c = ',';
    955  1.138   thorpej 
    956   1.82  christos 		} else if ((strstr(sys_name, "sigaction") != NULL ||
    957   1.82  christos 		    strstr(sys_name, "sigvec") != NULL) && argcount >= 1) {
    958   1.82  christos 			(void)printf("(SIG%s", signame(ap[0], 1));
    959   1.82  christos 			ap += 1;
    960   1.82  christos 			argcount -= 1;
    961   1.82  christos 			c = ',';
    962   1.82  christos 
    963   1.82  christos 		} else if ((strcmp(sys_name, "kill") == 0 ||
    964   1.82  christos 		    strcmp(sys_name, "killpg") == 0) && argcount >= 2) {
    965   1.72       mrg 			putchar('(');
    966   1.73  christos 			output_long((long)ap[0], !(decimal || small(*ap)));
    967   1.72       mrg 			(void)printf(", SIG%s", signame(ap[1], 1));
    968   1.67       dsl 			ap += 2;
    969   1.67       dsl 			argcount -= 2;
    970   1.67       dsl 			c = ',';
    971  1.130  christos 		} else if (strcmp(sys_name, "mprotect") == 0 && argcount >= 3) {
    972  1.130  christos 			putchar('(');
    973  1.130  christos 			output_long((long)ap[0], !(decimal || small(ap[0])));
    974  1.130  christos 			c = ',';
    975  1.130  christos 			putchar(c);
    976  1.130  christos 			output_long((long)ap[1], !(decimal || small(ap[1])));
    977  1.130  christos 			putchar(c);
    978  1.130  christos 			putprot(ap[2]);
    979  1.130  christos 			ap += 3;
    980  1.130  christos 			argcount -= 3;
    981  1.130  christos 			c = ',';
    982  1.124  christos 		} else if (strcmp(sys_name, "mmap") == 0 && argcount >= 6) {
    983  1.124  christos 			char buf[1024];
    984  1.124  christos 			putchar('(');
    985  1.124  christos 			output_long((long)ap[0], !(decimal || small(ap[0])));
    986  1.124  christos 			c = ',';
    987  1.124  christos 			putchar(c);
    988  1.124  christos 			output_long((long)ap[1], !(decimal || small(ap[1])));
    989  1.124  christos 			putchar(c);
    990  1.130  christos 			putprot(ap[2]);
    991  1.124  christos 			snprintb(buf, sizeof(buf), MAP_FMT, ap[3]);
    992  1.124  christos 			printf(",%s", buf);
    993  1.124  christos 			ap += 4;
    994  1.124  christos 			argcount -= 4;
    995  1.124  christos 			c = ',';
    996   1.67       dsl 		} else if (strcmp(sys_name, "ptrace") == 0 && argcount >= 1) {
    997   1.72       mrg 			putchar('(');
    998   1.97     njoly 			if (strcmp(emul->name, "linux") == 0 ||
    999   1.97     njoly 			    strcmp(emul->name, "linux32") == 0) {
   1000   1.87       mrg 				if ((long)*ap >= 0 && *ap <
   1001  1.103     lukem 				    (register_t)(sizeof(linux_ptrace_ops) /
   1002  1.103     lukem 				    sizeof(linux_ptrace_ops[0])))
   1003   1.72       mrg 					(void)printf("%s",
   1004   1.37  christos 					    linux_ptrace_ops[*ap]);
   1005   1.72       mrg 				else
   1006   1.72       mrg 					output_long((long)*ap, 1);
   1007   1.67       dsl 			} else {
   1008  1.113  christos 				if ((long)*ap >= 0 && *ap < (register_t)
   1009  1.113  christos 				    __arraycount(ptrace_ops))
   1010   1.72       mrg 					(void)printf("%s", ptrace_ops[*ap]);
   1011   1.78      matt #ifdef PT_MACHDEP_STRINGS
   1012   1.78      matt 				else if (*ap >= PT_FIRSTMACH &&
   1013  1.113  christos 				    *ap - PT_FIRSTMACH < (register_t)
   1014  1.113  christos 				    __arraycount(ptrace_machdep_ops))
   1015   1.78      matt 					(void)printf("%s", ptrace_machdep_ops[*ap - PT_FIRSTMACH]);
   1016   1.78      matt #endif
   1017   1.75     enami 				else
   1018   1.72       mrg 					output_long((long)*ap, 1);
   1019   1.67       dsl 			}
   1020   1.67       dsl 			ap++;
   1021   1.67       dsl 			argcount--;
   1022   1.67       dsl 			c = ',';
   1023   1.37  christos 
   1024    1.1       cgd 		}
   1025   1.67       dsl 		while (argcount > 0) {
   1026   1.72       mrg 			putchar(c);
   1027   1.73  christos 			output_long((long)*ap, !(decimal || small(*ap)));
   1028   1.67       dsl 			ap++;
   1029   1.67       dsl 			argcount--;
   1030    1.1       cgd 			c = ',';
   1031    1.1       cgd 		}
   1032    1.1       cgd 		(void)putchar(')');
   1033    1.1       cgd 	}
   1034    1.1       cgd 	(void)putchar('\n');
   1035    1.1       cgd }
   1036    1.1       cgd 
   1037   1.81  christos static void
   1038   1.81  christos ktrsysret(struct ktr_sysret *ktr, int len)
   1039    1.1       cgd {
   1040   1.67       dsl 	const struct emulation *emul;
   1041   1.18     lukem 	int error = ktr->ktr_error;
   1042   1.18     lukem 	int code = ktr->ktr_code;
   1043    1.1       cgd 
   1044   1.67       dsl 	if (emul_changed)  {
   1045   1.67       dsl 		/* In order to get system call name right in execve return */
   1046   1.67       dsl 		emul = prev_emul;
   1047   1.67       dsl 		emul_changed = 0;
   1048   1.67       dsl 	} else
   1049   1.67       dsl 		emul = cur_emul;
   1050   1.35      manu 
   1051  1.110     joerg 	if (numeric || ((code >= emul->nsysnames || code < 0 || plain > 1)))
   1052    1.1       cgd 		(void)printf("[%d] ", code);
   1053    1.1       cgd 	else
   1054   1.67       dsl 		(void)printf("%s ", emul->sysnames[code]);
   1055    1.1       cgd 
   1056   1.22  christos 	switch (error) {
   1057   1.22  christos 	case 0:
   1058   1.48       dsl 		rprint(ktr->ktr_retval);
   1059  1.103     lukem 		if (len > (int)offsetof(struct ktr_sysret, ktr_retval_1) &&
   1060   1.48       dsl 		    ktr->ktr_retval_1 != 0) {
   1061   1.48       dsl 			(void)printf(", ");
   1062   1.48       dsl 			rprint(ktr->ktr_retval_1);
   1063    1.1       cgd 		}
   1064   1.22  christos 		break;
   1065   1.22  christos 
   1066   1.22  christos 	default:
   1067   1.22  christos 		eprint(error);
   1068   1.22  christos 		break;
   1069   1.22  christos 	}
   1070   1.22  christos 	(void)putchar('\n');
   1071   1.48       dsl }
   1072   1.48       dsl 
   1073   1.81  christos static void
   1074  1.112     alnsn ktrexecfd(struct ktr_execfd *ktr)
   1075  1.112     alnsn {
   1076  1.112     alnsn 	static const char *dnames[] = { DTYPE_NAMES };
   1077  1.112     alnsn 	if (ktr->ktr_dtype < __arraycount(dnames))
   1078  1.112     alnsn 		printf("%s %d\n", dnames[ktr->ktr_dtype], ktr->ktr_fd);
   1079  1.112     alnsn 	else
   1080  1.112     alnsn 		printf("UNKNOWN(%u) %d\n", ktr->ktr_dtype, ktr->ktr_fd);
   1081  1.112     alnsn }
   1082  1.112     alnsn 
   1083  1.112     alnsn static void
   1084   1.48       dsl rprint(register_t ret)
   1085   1.48       dsl {
   1086   1.76     enami 
   1087   1.48       dsl 	if (!plain) {
   1088  1.118     njoly 		output_long(ret, 0);
   1089  1.118     njoly 		if (!small(ret)) {
   1090  1.118     njoly 			putchar('/');
   1091  1.118     njoly 			output_long(ret, 1);
   1092  1.118     njoly 		}
   1093   1.48       dsl 	} else {
   1094  1.118     njoly 		output_long(ret, !(decimal || small(ret)));
   1095   1.48       dsl 	}
   1096   1.22  christos }
   1097   1.22  christos 
   1098   1.22  christos /*
   1099   1.22  christos  * We print the original emulation's error numerically, but we
   1100   1.22  christos  * translate it to netbsd to print it symbolically.
   1101   1.22  christos  */
   1102   1.81  christos static void
   1103   1.81  christos eprint(int e)
   1104   1.22  christos {
   1105   1.22  christos 	int i = e;
   1106   1.22  christos 
   1107   1.67       dsl 	if (cur_emul->errnomap) {
   1108   1.22  christos 
   1109   1.22  christos 		/* No remapping for ERESTART and EJUSTRETURN */
   1110   1.22  christos 		/* Kludge for linux that has negative error numbers */
   1111   1.67       dsl 		if (cur_emul->errnomap[2] > 0 && e < 0)
   1112   1.22  christos 			goto normal;
   1113   1.22  christos 
   1114   1.67       dsl 		for (i = 0; i < cur_emul->nerrnomap; i++)
   1115   1.67       dsl 			if (e == cur_emul->errnomap[i])
   1116   1.22  christos 				break;
   1117   1.22  christos 
   1118   1.67       dsl 		if (i == cur_emul->nerrnomap) {
   1119   1.22  christos 			printf("-1 unknown errno %d", e);
   1120   1.22  christos 			return;
   1121   1.22  christos 		}
   1122   1.22  christos 	}
   1123   1.22  christos 
   1124   1.22  christos normal:
   1125   1.22  christos 	switch (i) {
   1126   1.22  christos 	case ERESTART:
   1127    1.1       cgd 		(void)printf("RESTART");
   1128   1.22  christos 		break;
   1129   1.22  christos 
   1130   1.22  christos 	case EJUSTRETURN:
   1131    1.1       cgd 		(void)printf("JUSTRETURN");
   1132   1.22  christos 		break;
   1133   1.22  christos 
   1134   1.22  christos 	default:
   1135   1.22  christos 		(void)printf("-1 errno %d", e);
   1136   1.36  christos 		if (!plain)
   1137   1.22  christos 			(void)printf(" %s", strerror(i));
   1138    1.1       cgd 	}
   1139    1.1       cgd }
   1140    1.1       cgd 
   1141   1.81  christos static void
   1142   1.81  christos ktrnamei(char *cp, int len)
   1143    1.1       cgd {
   1144   1.17     mikel 
   1145    1.1       cgd 	(void)printf("\"%.*s\"\n", len, cp);
   1146    1.1       cgd }
   1147    1.1       cgd 
   1148   1.81  christos static void
   1149  1.123     alnsn ktremul(char *name, size_t len, size_t bufsize)
   1150   1.11  christos {
   1151   1.76     enami 
   1152   1.27  jdolecek 	if (len >= bufsize)
   1153   1.27  jdolecek 		len = bufsize - 1;
   1154   1.11  christos 
   1155   1.27  jdolecek 	name[len] = '\0';
   1156   1.27  jdolecek 	setemul(name, ktr_header.ktr_pid, 1);
   1157   1.35      manu 	emul_changed = 1;
   1158   1.11  christos 
   1159   1.11  christos 	(void)printf("\"%s\"\n", name);
   1160   1.11  christos }
   1161   1.11  christos 
   1162   1.52       dsl static void
   1163   1.81  christos hexdump_buf(const void *vdp, int datalen, int word_sz)
   1164   1.52       dsl {
   1165   1.65       dsl 	const char hex[] = "0123456789abcdef";
   1166   1.77       dsl 	char chars[16], prev[16];
   1167   1.65       dsl 	char bytes[16 * 3 + 4];
   1168   1.58       dsl 	const unsigned char *dp = vdp;
   1169   1.65       dsl 	const unsigned char *datalim = dp + datalen;
   1170   1.65       dsl 	const unsigned char *line_end;
   1171   1.80     lukem 	int off, l = 0, c;
   1172   1.65       dsl 	char *cp, *bp;
   1173   1.65       dsl 	int divmask = word_sz - 1;	/* block size in bytes */
   1174   1.65       dsl 	int gdelim = 3;			/* gap between blocks */
   1175   1.65       dsl 	int bsize = 2;			/* increment for each byte */
   1176   1.65       dsl 	int width;
   1177   1.77       dsl 	int dupl = 0;
   1178   1.65       dsl #if _BYTE_ORDER == _LITTLE_ENDIAN
   1179   1.65       dsl 	int bswap = word_sz - 1;
   1180   1.65       dsl #else
   1181   1.65       dsl #define	bswap 0
   1182   1.65       dsl #endif
   1183   1.65       dsl 
   1184   1.65       dsl 	switch (word_sz) {
   1185   1.65       dsl 	case 2:
   1186   1.65       dsl 		gdelim = 2;
   1187   1.64      manu 		break;
   1188   1.64      manu 	case 1:
   1189   1.65       dsl 		divmask = 7;
   1190   1.65       dsl 		bsize = 3;
   1191   1.65       dsl 		gdelim = 1;
   1192   1.65       dsl 		break;
   1193   1.64      manu 	default:
   1194   1.64      manu 		break;
   1195   1.64      manu 	}
   1196   1.65       dsl 	width = 16 * bsize + (16 / (divmask + 1)) * gdelim;
   1197   1.79       dsl 	if (word_sz != 1)
   1198   1.65       dsl 		width += 2;
   1199   1.65       dsl 
   1200   1.65       dsl 	for (off = 0; dp < datalim; off += l) {
   1201   1.65       dsl 		memset(bytes, ' ', sizeof bytes);
   1202   1.65       dsl 		line_end = dp + 16;
   1203   1.77       dsl 		if (line_end >= datalim) {
   1204   1.65       dsl 			line_end = datalim;
   1205   1.77       dsl 			dupl |= 1;	/* need to print */
   1206   1.77       dsl 		} else {
   1207   1.77       dsl 			if (dupl == 0 || memcmp(dp, prev, sizeof chars))
   1208   1.77       dsl 				dupl |= 1;
   1209   1.77       dsl 		}
   1210   1.77       dsl 
   1211   1.77       dsl 		if (!(dupl & 1)) {
   1212   1.77       dsl 			/* This is a duplicate of the line above, count 'em */
   1213   1.77       dsl 			dupl += 2;
   1214   1.77       dsl 			dp = line_end;
   1215   1.77       dsl 			continue;
   1216   1.77       dsl 		}
   1217   1.77       dsl 
   1218   1.77       dsl 		if (dupl > 3) {
   1219   1.77       dsl 			/* previous line as a duplicate */
   1220   1.77       dsl 			if (dupl == 5)
   1221   1.77       dsl 				/* Only one duplicate, print line */
   1222   1.77       dsl 				printf("\t%-5.3x%.*s%.*s\n",
   1223   1.77       dsl 					off - l, width, bytes, l, chars);
   1224   1.77       dsl 			else
   1225   1.77       dsl 				printf("\t%.*s\n",
   1226   1.77       dsl 					snprintf(NULL, 0, "%3x", off), "*****");
   1227   1.77       dsl 		}
   1228   1.52       dsl 
   1229   1.65       dsl 		for (l = 0, bp = bytes, cp = chars; dp < line_end; l++) {
   1230   1.52       dsl 			c = *dp++;
   1231   1.77       dsl 			prev[l] = c;
   1232   1.65       dsl 			if ((l & divmask) == 0)
   1233   1.65       dsl 				bp += gdelim;
   1234   1.65       dsl 			bp[(l ^ bswap) * bsize] = hex[c >> 4];
   1235   1.65       dsl 			bp[(l ^ bswap) * bsize + 1] = hex[c & 0xf];
   1236   1.52       dsl 			*cp++ = isgraph(c) ? c : '.';
   1237   1.76     enami 		}
   1238   1.64      manu 
   1239   1.67       dsl 		printf("\t%-5.3x%.*s%.*s\n", off, width, bytes, l, chars);
   1240   1.77       dsl 		dupl = 2;
   1241   1.52       dsl 	}
   1242   1.52       dsl }
   1243   1.52       dsl 
   1244   1.58       dsl static void
   1245   1.58       dsl visdump_buf(const void *vdp, int datalen, int col)
   1246    1.1       cgd {
   1247   1.58       dsl 	const unsigned char *dp = vdp;
   1248   1.18     lukem 	char *cp;
   1249   1.18     lukem 	int width;
   1250    1.1       cgd 	char visbuf[5];
   1251   1.20       mrg 	static int screenwidth = 0;
   1252    1.1       cgd 
   1253    1.1       cgd 	if (screenwidth == 0) {
   1254    1.1       cgd 		struct winsize ws;
   1255    1.1       cgd 
   1256   1.36  christos 		if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
   1257    1.1       cgd 		    ws.ws_col > 8)
   1258    1.1       cgd 			screenwidth = ws.ws_col;
   1259    1.1       cgd 		else
   1260    1.1       cgd 			screenwidth = 80;
   1261    1.1       cgd 	}
   1262   1.58       dsl 
   1263   1.58       dsl 	(void)printf("\"");
   1264   1.58       dsl 	col++;
   1265    1.4   mycroft 	for (; datalen > 0; datalen--, dp++) {
   1266   1.58       dsl 		(void)svis(visbuf, *dp, VIS_CSTYLE,
   1267   1.93       agc 		    datalen > 1 ? *(dp + 1) : 0, "\"\n");
   1268    1.1       cgd 		cp = visbuf;
   1269    1.1       cgd 		/*
   1270    1.1       cgd 		 * Keep track of printables and
   1271    1.1       cgd 		 * space chars (like fold(1)).
   1272    1.1       cgd 		 */
   1273    1.1       cgd 		if (col == 0) {
   1274    1.1       cgd 			(void)putchar('\t');
   1275    1.1       cgd 			col = 8;
   1276    1.1       cgd 		}
   1277   1.76     enami 		switch (*cp) {
   1278    1.1       cgd 		case '\n':
   1279    1.1       cgd 			col = 0;
   1280    1.1       cgd 			(void)putchar('\n');
   1281    1.1       cgd 			continue;
   1282    1.1       cgd 		case '\t':
   1283   1.58       dsl 			width = 8 - (col & 07);
   1284    1.1       cgd 			break;
   1285    1.1       cgd 		default:
   1286    1.1       cgd 			width = strlen(cp);
   1287    1.1       cgd 		}
   1288   1.58       dsl 		if (col + width > (screenwidth - 2)) {
   1289    1.1       cgd 			(void)printf("\\\n\t");
   1290    1.1       cgd 			col = 8;
   1291   1.74     enami 			if (*cp == '\t')
   1292   1.74     enami 				width = 8;
   1293    1.1       cgd 		}
   1294    1.1       cgd 		col += width;
   1295    1.1       cgd 		do {
   1296    1.1       cgd 			(void)putchar(*cp++);
   1297    1.1       cgd 		} while (*cp);
   1298    1.1       cgd 	}
   1299    1.1       cgd 	if (col == 0)
   1300    1.1       cgd 		(void)printf("       ");
   1301    1.1       cgd 	(void)printf("\"\n");
   1302   1.58       dsl }
   1303   1.58       dsl 
   1304   1.81  christos static void
   1305   1.81  christos ktrgenio(struct ktr_genio *ktr, int len)
   1306   1.58       dsl {
   1307   1.58       dsl 	int datalen = len - sizeof (struct ktr_genio);
   1308   1.58       dsl 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
   1309   1.58       dsl 
   1310   1.86      manu 	if (ktr->ktr_fd != -1)
   1311   1.86      manu 		printf("fd %d ", ktr->ktr_fd);
   1312   1.86      manu 	printf("%s %d bytes\n",
   1313   1.76     enami 	    ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
   1314   1.58       dsl 	if (maxdata == 0)
   1315   1.58       dsl 		return;
   1316   1.58       dsl 	if (maxdata > 0 && datalen > maxdata)
   1317   1.58       dsl 		datalen = maxdata;
   1318   1.65       dsl 	if (word_size) {
   1319   1.65       dsl 		hexdump_buf(dp, datalen, word_size);
   1320   1.58       dsl 		return;
   1321   1.58       dsl 	}
   1322   1.58       dsl 	(void)printf("       ");
   1323   1.58       dsl 	visdump_buf(dp, datalen, 7);
   1324    1.1       cgd }
   1325    1.1       cgd 
   1326   1.81  christos static void
   1327   1.81  christos ktrpsig(void *v, int len)
   1328    1.1       cgd {
   1329   1.21   mycroft 	int signo, first;
   1330   1.60  christos 	struct {
   1331   1.60  christos 		struct ktr_psig ps;
   1332   1.60  christos 		siginfo_t si;
   1333   1.60  christos 	} *psig = v;
   1334   1.60  christos 	siginfo_t *si = &psig->si;
   1335   1.60  christos 	const char *code;
   1336   1.60  christos 
   1337   1.60  christos 	(void)printf("SIG%s ", signame(psig->ps.signo, 0));
   1338   1.60  christos 	if (psig->ps.action == SIG_DFL)
   1339   1.60  christos 		(void)printf("SIG_DFL");
   1340   1.21   mycroft 	else {
   1341   1.60  christos 		(void)printf("caught handler=%p mask=(", psig->ps.action);
   1342   1.21   mycroft 		first = 1;
   1343   1.21   mycroft 		for (signo = 1; signo < NSIG; signo++) {
   1344   1.60  christos 			if (sigismember(&psig->ps.mask, signo)) {
   1345   1.21   mycroft 				if (first)
   1346   1.21   mycroft 					first = 0;
   1347   1.21   mycroft 				else
   1348   1.21   mycroft 					(void)printf(",");
   1349   1.21   mycroft 				(void)printf("%d", signo);
   1350   1.21   mycroft 			}
   1351   1.21   mycroft 		}
   1352   1.60  christos 		(void)printf(")");
   1353   1.60  christos 	}
   1354   1.60  christos 	switch (len) {
   1355   1.60  christos 	case sizeof(struct ktr_psig):
   1356   1.61  christos 		if (psig->ps.code)
   1357   1.61  christos 			printf(" code=0x%x", psig->ps.code);
   1358   1.61  christos 		printf(psig->ps.action == SIG_DFL ? "\n" : ")\n");
   1359   1.60  christos 		return;
   1360   1.60  christos 	case sizeof(*psig):
   1361   1.60  christos 		if (si->si_code == 0) {
   1362   1.61  christos 			printf(": code=SI_USER sent by pid=%d, uid=%d)\n",
   1363   1.76     enami 			    si->si_pid, si->si_uid);
   1364   1.60  christos 			return;
   1365   1.60  christos 		}
   1366   1.60  christos 
   1367   1.60  christos 		if (si->si_code < 0) {
   1368   1.60  christos 			switch (si->si_code) {
   1369   1.60  christos 			case SI_TIMER:
   1370  1.106  christos 			case SI_QUEUE:
   1371  1.106  christos 				printf(": code=%s sent by pid=%d, uid=%d with "
   1372  1.106  christos 				    "sigval %p)\n", si->si_code == SI_TIMER ?
   1373  1.106  christos 				    "SI_TIMER" : "SI_QUEUE", si->si_pid,
   1374  1.106  christos 				    si->si_uid, si->si_value.sival_ptr);
   1375   1.60  christos 				return;
   1376   1.60  christos 			case SI_ASYNCIO:
   1377   1.60  christos 			case SI_MESGQ:
   1378  1.107  christos 				printf(": code=%s with sigval %p)\n",
   1379  1.107  christos 				    si->si_code == SI_ASYNCIO ?
   1380  1.107  christos 				    "SI_ASYNCIO" : "SI_MESGQ",
   1381  1.107  christos 				    si->si_value.sival_ptr);
   1382  1.107  christos 				return;
   1383  1.100     njoly 			case SI_LWP:
   1384  1.108  christos 				printf(": code=SI_LWP sent by pid=%d, "
   1385  1.109  pgoyette 				    "uid=%d)\n", si->si_pid, si->si_uid);
   1386  1.107  christos 				return;
   1387   1.60  christos 			default:
   1388   1.60  christos 				code = NULL;
   1389   1.60  christos 				break;
   1390   1.60  christos 			}
   1391   1.60  christos 			if (code)
   1392   1.61  christos 				printf(": code=%s unimplemented)\n", code);
   1393   1.60  christos 			else
   1394   1.61  christos 				printf(": code=%d unimplemented)\n",
   1395   1.60  christos 				    si->si_code);
   1396   1.60  christos 			return;
   1397   1.60  christos 		}
   1398   1.60  christos 
   1399   1.99  christos 		if (si->si_code == SI_NOINFO) {
   1400   1.99  christos 			printf(": code=SI_NOINFO\n");
   1401   1.99  christos 			return;
   1402   1.99  christos 		}
   1403   1.99  christos 
   1404   1.60  christos 		code = siginfocodename(si->si_signo, si->si_code);
   1405   1.60  christos 		switch (si->si_signo) {
   1406   1.60  christos 		case SIGCHLD:
   1407   1.60  christos 			printf(": code=%s child pid=%d, uid=%d, "
   1408  1.146    rillig 			    "status=%u, utime=%lu, stime=%lu)\n",
   1409   1.60  christos 			    code, si->si_pid,
   1410   1.62      matt 			    si->si_uid, si->si_status,
   1411   1.62      matt 			    (unsigned long) si->si_utime,
   1412   1.76     enami 			    (unsigned long) si->si_stime);
   1413   1.60  christos 			return;
   1414   1.60  christos 		case SIGILL:
   1415   1.60  christos 		case SIGFPE:
   1416   1.60  christos 		case SIGSEGV:
   1417   1.60  christos 		case SIGBUS:
   1418   1.60  christos 		case SIGTRAP:
   1419   1.61  christos 			printf(": code=%s, addr=%p, trap=%d)\n",
   1420   1.60  christos 			    code, si->si_addr, si->si_trap);
   1421   1.60  christos 			return;
   1422   1.60  christos 		case SIGIO:
   1423   1.61  christos 			printf(": code=%s, fd=%d, band=%lx)\n",
   1424   1.60  christos 			    code, si->si_fd, si->si_band);
   1425   1.60  christos 			return;
   1426   1.60  christos 		default:
   1427   1.61  christos 			printf(": code=%s, errno=%d)\n",
   1428   1.60  christos 			    code, si->si_errno);
   1429   1.60  christos 			return;
   1430   1.60  christos 		}
   1431   1.60  christos 		/*NOTREACHED*/
   1432   1.60  christos 	default:
   1433  1.120  christos 		warnx("Unhandled size %d for ktrpsig", len);
   1434   1.60  christos 		break;
   1435   1.21   mycroft 	}
   1436    1.1       cgd }
   1437    1.1       cgd 
   1438   1.81  christos static void
   1439   1.81  christos ktrcsw(struct ktr_csw *cs)
   1440    1.1       cgd {
   1441   1.17     mikel 
   1442    1.1       cgd 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
   1443    1.4   mycroft 	    cs->user ? "user" : "kernel");
   1444   1.32  jdolecek }
   1445   1.32  jdolecek 
   1446   1.81  christos static void
   1447  1.114  christos ktruser_msghdr(const char *name, const void *buf, size_t len)
   1448  1.114  christos {
   1449  1.114  christos 	struct msghdr m;
   1450  1.114  christos 
   1451  1.114  christos 	if (len != sizeof(m))
   1452  1.114  christos 		warnx("%.*s: len %zu != %zu", KTR_USER_MAXIDLEN, name, len,
   1453  1.114  christos 		    sizeof(m));
   1454  1.114  christos 	memcpy(&m, buf, len);
   1455  1.114  christos 	printf("%.*s: [name=%p, namelen=%zu, iov=%p, iovlen=%zu, control=%p, "
   1456  1.114  christos 	    "controllen=%zu, flags=%x]\n", KTR_USER_MAXIDLEN, name,
   1457  1.114  christos 	    m.msg_name, (size_t)m.msg_namelen, m.msg_iov, (size_t)m.msg_iovlen,
   1458  1.114  christos 	    m.msg_control, (size_t)m.msg_controllen, m.msg_flags);
   1459  1.114  christos }
   1460  1.114  christos 
   1461  1.114  christos static void
   1462  1.114  christos ktruser_soname(const char *name, const void *buf, size_t len)
   1463  1.114  christos {
   1464  1.114  christos 	char fmt[512];
   1465  1.115  christos 	sockaddr_snprintf(fmt, sizeof(fmt), "%a", buf);
   1466  1.114  christos 	printf("%.*s: [%s]\n", KTR_USER_MAXIDLEN, name, fmt);
   1467  1.114  christos }
   1468  1.114  christos 
   1469  1.114  christos static void
   1470  1.137  christos ktruser_xattr_name(const char *name, const void *buf, size_t len)
   1471  1.137  christos {
   1472  1.140       chs 	printf("%.*s: [%.*s]\n", KTR_USER_MAXIDLEN, name, (int)len,
   1473  1.137  christos 	    (const char *)buf);
   1474  1.137  christos }
   1475  1.137  christos 
   1476  1.137  christos static void
   1477  1.137  christos ktruser_xattr_val(const char *name, const void *buf, size_t len)
   1478  1.137  christos {
   1479  1.137  christos 	const uint8_t *p = buf;
   1480  1.137  christos 	printf("%.*s: ", KTR_USER_MAXIDLEN, name);
   1481  1.137  christos 	for (size_t i = 0; i < len; i++)
   1482  1.137  christos 		printf("%.2x", *p++);
   1483  1.137  christos 	printf("\n");
   1484  1.137  christos }
   1485  1.137  christos 
   1486  1.137  christos static void
   1487  1.137  christos ktruser_xattr_list(const char *name, const void *buf, size_t len)
   1488  1.137  christos {
   1489  1.137  christos 	const uint8_t *p = buf, *ep = p + len;
   1490  1.137  christos 	printf("%.*s:", KTR_USER_MAXIDLEN, name);
   1491  1.137  christos 	while (p < ep) {
   1492  1.137  christos 		int l = *p++;
   1493  1.137  christos 		printf(" %.*s", l, p);
   1494  1.137  christos 		p += l;
   1495  1.137  christos 	}
   1496  1.137  christos 	printf("\n");
   1497  1.137  christos }
   1498  1.137  christos 
   1499  1.137  christos static void
   1500  1.114  christos ktruser_control(const char *name, const void *buf, size_t len)
   1501  1.114  christos {
   1502  1.114  christos 	struct cmsghdr m;
   1503  1.114  christos 
   1504  1.114  christos 	if (len < sizeof(m))
   1505  1.114  christos 		warnx("%.*s: len %zu < %zu", KTR_USER_MAXIDLEN, name, len,
   1506  1.114  christos 		    sizeof(m));
   1507  1.114  christos 	memcpy(&m, buf, sizeof(m));
   1508  1.114  christos 	printf("%.*s: [len=%zu, level=%d, type=%d]\n", KTR_USER_MAXIDLEN, name,
   1509  1.114  christos 	    (size_t)m.cmsg_len, m.cmsg_level, m.cmsg_type);
   1510  1.114  christos }
   1511  1.114  christos 
   1512  1.114  christos static void
   1513  1.132    nonaka ktruser_malloc(const char *name, const void *buf, size_t len)
   1514  1.132    nonaka {
   1515  1.132    nonaka 	struct ut { void *p; size_t s; void *r; } m;
   1516  1.132    nonaka 
   1517  1.132    nonaka 	if (len != sizeof(m))
   1518  1.132    nonaka 		warnx("%.*s: len %zu != %zu", KTR_USER_MAXIDLEN, name, len,
   1519  1.132    nonaka 		    sizeof(m));
   1520  1.132    nonaka 	memcpy(&m, buf, len < sizeof(m) ? len : sizeof(m));
   1521  1.132    nonaka 	if (m.p == NULL && m.s == 0 && m.r == NULL)
   1522  1.132    nonaka 		printf("%.*s: malloc_init()\n", KTR_USER_MAXIDLEN, name);
   1523  1.132    nonaka 	else if (m.p != NULL && m.s != 0)
   1524  1.132    nonaka 		printf("%.*s: %p = realloc(%p, %zu)\n", KTR_USER_MAXIDLEN, name,
   1525  1.132    nonaka 		    m.r, m.p, m.s);
   1526  1.132    nonaka 	else if (m.s == 0)
   1527  1.132    nonaka 		printf("%.*s: free(%p)\n", KTR_USER_MAXIDLEN, name, m.p);
   1528  1.132    nonaka 	else
   1529  1.132    nonaka 		printf("%.*s: %p = malloc(%zu)\n", KTR_USER_MAXIDLEN, name,
   1530  1.132    nonaka 		    m.r, m.s);
   1531  1.132    nonaka }
   1532  1.132    nonaka 
   1533  1.132    nonaka static void
   1534  1.114  christos ktruser_misc(const char *name, const void *buf, size_t len)
   1535  1.114  christos {
   1536  1.114  christos 	size_t i;
   1537  1.114  christos 	const char *dta = buf;
   1538  1.114  christos 
   1539  1.114  christos 	printf("%.*s: %zu, ", KTR_USER_MAXIDLEN, name, len);
   1540  1.114  christos 	for (i = 0; i < len; i++)
   1541  1.117  christos 		printf("%02x", (unsigned char)dta[i]);
   1542  1.114  christos 	printf("\n");
   1543  1.114  christos }
   1544  1.114  christos 
   1545  1.114  christos static struct {
   1546  1.114  christos 	const char *name;
   1547  1.114  christos 	void (*func)(const char *, const void *, size_t);
   1548  1.114  christos } nv[] = {
   1549  1.114  christos 	{ "msghdr", ktruser_msghdr },
   1550  1.114  christos 	{ "mbsoname", ktruser_soname },
   1551  1.114  christos 	{ "mbcontrol", ktruser_control },
   1552  1.132    nonaka 	{ "malloc", ktruser_malloc },
   1553  1.137  christos 	{ "xattr-name", ktruser_xattr_name },
   1554  1.137  christos 	{ "xattr-val", ktruser_xattr_val },
   1555  1.137  christos 	{ "xattr-list", ktruser_xattr_list },
   1556  1.114  christos 	{ NULL,	ktruser_misc },
   1557  1.114  christos };
   1558  1.114  christos 
   1559  1.114  christos static void
   1560   1.81  christos ktruser(struct ktr_user *usr, int len)
   1561   1.32  jdolecek {
   1562   1.40  jdolecek 	unsigned char *dta;
   1563   1.34  jdolecek 
   1564   1.92       dsl 	len -= sizeof(struct ktr_user);
   1565   1.92       dsl 	dta = (unsigned char *)(usr + 1);
   1566   1.92       dsl 	if (word_size) {
   1567  1.114  christos 		printf("%.*s:", KTR_USER_MAXIDLEN, usr->ktr_id);
   1568   1.92       dsl 		printf("\n");
   1569   1.92       dsl 		hexdump_buf(dta, len, word_size);
   1570   1.92       dsl 		return;
   1571   1.92       dsl 	}
   1572  1.114  christos 	for (size_t j = 0; j < __arraycount(nv); j++)
   1573  1.114  christos 		if (nv[j].name == NULL ||
   1574  1.114  christos 		    strncmp(nv[j].name, usr->ktr_id, KTR_USER_MAXIDLEN) == 0) {
   1575  1.114  christos 			(*nv[j].func)(usr->ktr_id, dta, len);
   1576  1.114  christos 			break;
   1577  1.114  christos 		}
   1578   1.37  christos }
   1579   1.37  christos 
   1580   1.81  christos static void
   1581   1.86      manu ktrmib(int *namep, int len)
   1582   1.86      manu {
   1583  1.103     lukem 	size_t i;
   1584   1.86      manu 
   1585   1.86      manu 	for (i = 0; i < (len / sizeof(*namep)); i++)
   1586   1.86      manu 		printf("%s%d", (i == 0) ? "" : ".", namep[i]);
   1587   1.86      manu 	printf("\n");
   1588   1.86      manu }
   1589   1.86      manu 
   1590   1.37  christos static const char *
   1591   1.37  christos signame(long sig, int xlat)
   1592   1.37  christos {
   1593   1.37  christos 	static char buf[64];
   1594   1.76     enami 
   1595   1.44  jdolecek 	if (sig == 0)
   1596   1.44  jdolecek 		return " 0";
   1597   1.44  jdolecek 	else if (sig < 0 || sig >= NSIG) {
   1598   1.37  christos 		(void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
   1599   1.37  christos 		return buf;
   1600   1.37  christos 	} else
   1601   1.67       dsl 		return sys_signame[(xlat && cur_emul->signalmap != NULL) ?
   1602   1.67       dsl 		    cur_emul->signalmap[sig] : sig];
   1603    1.1       cgd }
   1604    1.1       cgd 
   1605   1.81  christos static void
   1606   1.81  christos usage(void)
   1607    1.1       cgd {
   1608  1.133     kamil 	if (strcmp(getprogname(), "ioctlprint") == 0) {
   1609  1.136       wiz 		(void)fprintf(stderr, "Usage: %s [-l] [-e emulation] [-f format] <ioctl> ...\n",
   1610  1.104  christos 		    getprogname());
   1611  1.104  christos 	} else {
   1612  1.129       wiz 		(void)fprintf(stderr, "Usage: %s [-dElNnRT] [-e emulation] "
   1613  1.104  christos 		   "[-f file] [-m maxdata] [-p pid]\n             [-t trstr] "
   1614  1.104  christos 		   "[-x | -X size] [file]\n", getprogname());
   1615  1.104  christos 	}
   1616    1.1       cgd 	exit(1);
   1617   1.11  christos }
   1618  1.134  christos 
   1619  1.134  christos static const struct ioctlinfo *
   1620  1.134  christos find_ioctl_by_name(const char *name)
   1621  1.134  christos {
   1622  1.134  christos 	for (size_t i = 0; ioctlinfo[i].name != NULL; i++) {
   1623  1.134  christos 		if (strcmp(name, ioctlinfo[i].name) == 0)
   1624  1.134  christos 			return &ioctlinfo[i];
   1625  1.134  christos 	}
   1626  1.134  christos 	return NULL;
   1627  1.134  christos }
   1628  1.134  christos 
   1629  1.134  christos static const struct ioctlinfo *
   1630  1.134  christos find_ioctl_by_value(unsigned long value)
   1631  1.134  christos {
   1632  1.134  christos 	for (size_t i = 0; ioctlinfo[i].name != NULL; i++) {
   1633  1.134  christos 		if (value == ioctlinfo[i].value)
   1634  1.134  christos 			return &ioctlinfo[i];
   1635  1.134  christos 	}
   1636  1.134  christos 	return NULL;
   1637  1.134  christos }
   1638  1.134  christos 
   1639  1.134  christos static const struct ioctlinfo *
   1640  1.134  christos find_ioctl(const char *name)
   1641  1.134  christos {
   1642  1.134  christos 	if (isalpha((unsigned char)*name)) {
   1643  1.134  christos 		return find_ioctl_by_name(name);
   1644  1.134  christos 	}
   1645  1.134  christos 	int e;
   1646  1.134  christos 	unsigned long u = strtou(name, NULL, 0, 0, ULONG_MAX, &e);
   1647  1.134  christos 	if (e)
   1648  1.134  christos 		errc(1, e, "invalid argument: `%s'", name);
   1649  1.134  christos 	return find_ioctl_by_value(u);
   1650  1.134  christos }
   1651