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