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