Home | History | Annotate | Line # | Download | only in kdump
kdump.c revision 1.23
      1  1.23  christos /*	$NetBSD: kdump.c,v 1.23 1998/10/24 19:04:51 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.17     mikel #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.17     mikel __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
     39  1.17     mikel 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43  1.12       jtc #if 0
     44  1.12       jtc static char sccsid[] = "@(#)kdump.c	8.4 (Berkeley) 4/28/95";
     45  1.17     mikel #else
     46  1.23  christos __RCSID("$NetBSD: kdump.c,v 1.23 1998/10/24 19:04:51 christos Exp $");
     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.4   mycroft #include <err.h>
     61   1.4   mycroft #include <signal.h>
     62   1.1       cgd #include <stdio.h>
     63   1.1       cgd #include <stdlib.h>
     64   1.1       cgd #include <string.h>
     65   1.4   mycroft #include <unistd.h>
     66   1.4   mycroft #include <vis.h>
     67   1.4   mycroft 
     68   1.1       cgd #include "ktrace.h"
     69   1.1       cgd 
     70   1.1       cgd int timestamp, decimal, fancy = 1, tail, maxdata;
     71   1.1       cgd char *tracefile = DEF_TRACEFILE;
     72   1.1       cgd struct ktr_header ktr_header;
     73   1.1       cgd 
     74   1.1       cgd #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
     75   1.1       cgd 
     76  1.11  christos #include <sys/syscall.h>
     77  1.11  christos 
     78  1.19     lukem #include "../../sys/compat/freebsd/freebsd_syscall.h"
     79  1.11  christos #include "../../sys/compat/hpux/hpux_syscall.h"
     80  1.11  christos #include "../../sys/compat/ibcs2/ibcs2_syscall.h"
     81  1.11  christos #include "../../sys/compat/linux/linux_syscall.h"
     82  1.11  christos #include "../../sys/compat/osf1/osf1_syscall.h"
     83  1.11  christos #include "../../sys/compat/sunos/sunos_syscall.h"
     84  1.11  christos #include "../../sys/compat/svr4/svr4_syscall.h"
     85  1.11  christos #include "../../sys/compat/ultrix/ultrix_syscall.h"
     86  1.11  christos 
     87  1.11  christos #define KTRACE
     88  1.11  christos #include "../../sys/kern/syscalls.c"
     89  1.11  christos 
     90  1.19     lukem #include "../../sys/compat/freebsd/freebsd_syscalls.c"
     91  1.11  christos #include "../../sys/compat/hpux/hpux_syscalls.c"
     92  1.11  christos #include "../../sys/compat/ibcs2/ibcs2_syscalls.c"
     93  1.11  christos #include "../../sys/compat/linux/linux_syscalls.c"
     94  1.11  christos #include "../../sys/compat/osf1/osf1_syscalls.c"
     95  1.11  christos #include "../../sys/compat/sunos/sunos_syscalls.c"
     96  1.11  christos #include "../../sys/compat/svr4/svr4_syscalls.c"
     97  1.11  christos #include "../../sys/compat/ultrix/ultrix_syscalls.c"
     98  1.22  christos 
     99  1.23  christos #include "../../sys/compat/hpux/hpux_errno.c"
    100  1.23  christos #include "../../sys/compat/svr4/svr4_errno.c"
    101  1.23  christos #include "../../sys/compat/ibcs2/ibcs2_errno.c"
    102  1.23  christos #include "../../sys/compat/linux/common/linux_errno.c"
    103  1.11  christos #undef KTRACE
    104  1.11  christos 
    105  1.11  christos struct emulation {
    106  1.11  christos 	char *name;		/* Emulation name */
    107  1.11  christos 	char **sysnames;	/* Array of system call names */
    108  1.11  christos 	int  nsysnames;		/* Number of */
    109  1.22  christos 	int  *errno;		/* Array of error number mapping */
    110  1.22  christos 	int  nerrno;		/* number of elements in array */
    111  1.11  christos };
    112  1.11  christos 
    113  1.22  christos #define NELEM(a) (sizeof(a) / sizeof(a[0]))
    114  1.22  christos 
    115  1.11  christos static struct emulation emulations[] = {
    116  1.22  christos 	{   "netbsd",	       syscallnames,         SYS_MAXSYSCALL,
    117  1.22  christos 	        NULL,		        0 },
    118  1.22  christos 	{  "freebsd",  freebsd_syscallnames, FREEBSD_SYS_MAXSYSCALL,
    119  1.22  christos 	        NULL,		        0 },
    120  1.22  christos 	{     "hpux",	  hpux_syscallnames,    HPUX_SYS_MAXSYSCALL,
    121  1.23  christos 	  native_to_hpux_errno,  NELEM(native_to_hpux_errno)  },
    122  1.22  christos 	{    "ibcs2",    ibcs2_syscallnames,   IBCS2_SYS_MAXSYSCALL,
    123  1.23  christos 	 native_to_ibcs2_errno,  NELEM(native_to_ibcs2_errno) },
    124  1.22  christos 	{    "linux",    linux_syscallnames,   LINUX_SYS_MAXSYSCALL,
    125  1.23  christos 	 native_to_linux_errno,  NELEM(native_to_linux_errno) },
    126  1.22  christos 	{     "osf1",     osf1_syscallnames,    OSF1_SYS_MAXSYSCALL,
    127  1.22  christos 	        NULL,		        0 },
    128  1.22  christos 	{    "sunos",    sunos_syscallnames,   SUNOS_SYS_MAXSYSCALL,
    129  1.22  christos 	        NULL,		        0 },
    130  1.22  christos 	{     "svr4",     svr4_syscallnames,    SVR4_SYS_MAXSYSCALL,
    131  1.23  christos 	  native_to_svr4_errno,  NELEM(native_to_svr4_errno)  },
    132  1.22  christos 	{   "ultrix",   ultrix_syscallnames,  ULTRIX_SYS_MAXSYSCALL,
    133  1.22  christos 	        NULL,			0 },
    134  1.22  christos 	{       NULL,		       NULL,		          0,
    135  1.22  christos 	        NULL,			0 }
    136  1.11  christos };
    137  1.11  christos 
    138  1.11  christos struct emulation *current;
    139  1.11  christos 
    140  1.17     mikel static const char *ptrace_ops[] = {
    141  1.11  christos 	"PT_TRACE_ME",	"PT_READ_I",	"PT_READ_D",	"PT_READ_U",
    142  1.11  christos 	"PT_WRITE_I",	"PT_WRITE_D",	"PT_WRITE_U",	"PT_CONTINUE",
    143  1.11  christos 	"PT_KILL",	"PT_ATTACH",	"PT_DETACH",
    144  1.11  christos };
    145  1.11  christos 
    146  1.17     mikel int	main __P((int, char **));
    147  1.17     mikel int	fread_tail __P((char *, int, int));
    148  1.17     mikel void	dumpheader __P((struct ktr_header *));
    149  1.17     mikel void	ioctldecode __P((u_long));
    150  1.17     mikel void	ktrsyscall __P((struct ktr_syscall *));
    151  1.17     mikel void	ktrsysret __P((struct ktr_sysret *));
    152  1.17     mikel void	ktrnamei __P((char *, int));
    153  1.17     mikel void	ktremul __P((char *, int));
    154  1.17     mikel void	ktrgenio __P((struct ktr_genio *, int));
    155  1.17     mikel void	ktrpsig __P((struct ktr_psig *));
    156  1.17     mikel void	ktrcsw __P((struct ktr_csw *));
    157  1.17     mikel void	usage __P((void));
    158  1.17     mikel void	setemul __P((char *));
    159  1.22  christos void	eprint __P((int));
    160  1.17     mikel char	*ioctlname __P((long));
    161  1.17     mikel 
    162   1.4   mycroft int
    163   1.1       cgd main(argc, argv)
    164   1.1       cgd 	int argc;
    165   1.1       cgd 	char *argv[];
    166   1.1       cgd {
    167   1.1       cgd 	int ch, ktrlen, size;
    168  1.18     lukem 	void *m;
    169   1.1       cgd 	int trpoints = ALL_POINTS;
    170   1.1       cgd 
    171  1.11  christos 	current = &emulations[0];	/* NetBSD */
    172  1.11  christos 
    173  1.11  christos 	while ((ch = getopt(argc, argv, "e:f:dlm:nRTt:")) != -1)
    174   1.4   mycroft 		switch (ch) {
    175  1.11  christos 		case 'e':
    176  1.11  christos 			setemul(optarg);
    177  1.11  christos 			break;
    178   1.1       cgd 		case 'f':
    179   1.1       cgd 			tracefile = optarg;
    180   1.1       cgd 			break;
    181   1.1       cgd 		case 'd':
    182   1.1       cgd 			decimal = 1;
    183   1.1       cgd 			break;
    184   1.1       cgd 		case 'l':
    185   1.1       cgd 			tail = 1;
    186   1.1       cgd 			break;
    187   1.1       cgd 		case 'm':
    188   1.1       cgd 			maxdata = atoi(optarg);
    189   1.1       cgd 			break;
    190   1.1       cgd 		case 'n':
    191   1.1       cgd 			fancy = 0;
    192   1.1       cgd 			break;
    193   1.1       cgd 		case 'R':
    194   1.1       cgd 			timestamp = 2;	/* relative timestamp */
    195   1.1       cgd 			break;
    196   1.1       cgd 		case 'T':
    197   1.1       cgd 			timestamp = 1;
    198   1.1       cgd 			break;
    199   1.1       cgd 		case 't':
    200   1.1       cgd 			trpoints = getpoints(optarg);
    201   1.4   mycroft 			if (trpoints < 0)
    202   1.4   mycroft 				errx(1, "unknown trace point in %s", optarg);
    203   1.1       cgd 			break;
    204   1.1       cgd 		default:
    205   1.1       cgd 			usage();
    206   1.1       cgd 		}
    207   1.1       cgd 	argv += optind;
    208   1.1       cgd 	argc -= optind;
    209   1.1       cgd 
    210   1.1       cgd 	if (argc > 1)
    211   1.1       cgd 		usage();
    212   1.1       cgd 
    213  1.17     mikel 	m = malloc(size = 1025);
    214   1.4   mycroft 	if (m == NULL)
    215   1.4   mycroft 		errx(1, "%s", strerror(ENOMEM));
    216   1.4   mycroft 	if (!freopen(tracefile, "r", stdin))
    217   1.4   mycroft 		err(1, "%s", tracefile);
    218  1.17     mikel 	while (fread_tail((char *)&ktr_header, sizeof(struct ktr_header), 1)) {
    219   1.1       cgd 		if (trpoints & (1<<ktr_header.ktr_type))
    220   1.1       cgd 			dumpheader(&ktr_header);
    221   1.4   mycroft 		if ((ktrlen = ktr_header.ktr_len) < 0)
    222   1.4   mycroft 			errx(1, "bogus length 0x%x", ktrlen);
    223   1.1       cgd 		if (ktrlen > size) {
    224   1.1       cgd 			m = (void *)realloc(m, ktrlen+1);
    225   1.4   mycroft 			if (m == NULL)
    226   1.4   mycroft 				errx(1, "%s", strerror(ENOMEM));
    227   1.1       cgd 			size = ktrlen;
    228   1.1       cgd 		}
    229   1.4   mycroft 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
    230   1.4   mycroft 			errx(1, "data too short");
    231   1.1       cgd 		if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
    232   1.1       cgd 			continue;
    233   1.1       cgd 		switch (ktr_header.ktr_type) {
    234   1.1       cgd 		case KTR_SYSCALL:
    235   1.1       cgd 			ktrsyscall((struct ktr_syscall *)m);
    236   1.1       cgd 			break;
    237   1.1       cgd 		case KTR_SYSRET:
    238   1.1       cgd 			ktrsysret((struct ktr_sysret *)m);
    239   1.1       cgd 			break;
    240   1.1       cgd 		case KTR_NAMEI:
    241   1.1       cgd 			ktrnamei(m, ktrlen);
    242   1.1       cgd 			break;
    243   1.1       cgd 		case KTR_GENIO:
    244   1.1       cgd 			ktrgenio((struct ktr_genio *)m, ktrlen);
    245   1.1       cgd 			break;
    246   1.1       cgd 		case KTR_PSIG:
    247   1.1       cgd 			ktrpsig((struct ktr_psig *)m);
    248   1.1       cgd 			break;
    249   1.1       cgd 		case KTR_CSW:
    250   1.1       cgd 			ktrcsw((struct ktr_csw *)m);
    251   1.1       cgd 			break;
    252  1.11  christos 		case KTR_EMUL:
    253  1.11  christos 			ktremul(m, ktrlen);
    254  1.11  christos 			break;
    255   1.1       cgd 		}
    256   1.1       cgd 		if (tail)
    257   1.1       cgd 			(void)fflush(stdout);
    258   1.1       cgd 	}
    259  1.17     mikel 	return (0);
    260   1.1       cgd }
    261   1.1       cgd 
    262  1.17     mikel int
    263   1.1       cgd fread_tail(buf, size, num)
    264   1.1       cgd 	char *buf;
    265   1.1       cgd 	int num, size;
    266   1.1       cgd {
    267   1.1       cgd 	int i;
    268   1.1       cgd 
    269   1.1       cgd 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
    270   1.1       cgd 		(void)sleep(1);
    271   1.1       cgd 		clearerr(stdin);
    272   1.1       cgd 	}
    273   1.1       cgd 	return (i);
    274   1.1       cgd }
    275   1.1       cgd 
    276  1.17     mikel void
    277   1.1       cgd dumpheader(kth)
    278   1.1       cgd 	struct ktr_header *kth;
    279   1.1       cgd {
    280   1.6   mycroft 	char unknown[64], *type;
    281   1.6   mycroft 	static struct timeval prevtime;
    282   1.6   mycroft 	struct timeval temp;
    283   1.1       cgd 
    284   1.1       cgd 	switch (kth->ktr_type) {
    285   1.1       cgd 	case KTR_SYSCALL:
    286   1.1       cgd 		type = "CALL";
    287   1.1       cgd 		break;
    288   1.1       cgd 	case KTR_SYSRET:
    289   1.1       cgd 		type = "RET ";
    290   1.1       cgd 		break;
    291   1.1       cgd 	case KTR_NAMEI:
    292   1.1       cgd 		type = "NAMI";
    293   1.1       cgd 		break;
    294   1.1       cgd 	case KTR_GENIO:
    295   1.1       cgd 		type = "GIO ";
    296   1.1       cgd 		break;
    297   1.1       cgd 	case KTR_PSIG:
    298   1.1       cgd 		type = "PSIG";
    299   1.1       cgd 		break;
    300   1.1       cgd 	case KTR_CSW:
    301   1.1       cgd 		type = "CSW";
    302   1.1       cgd 		break;
    303  1.11  christos 	case KTR_EMUL:
    304  1.11  christos 		type = "EMUL";
    305  1.11  christos 		break;
    306   1.1       cgd 	default:
    307   1.1       cgd 		(void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
    308   1.1       cgd 		type = unknown;
    309   1.1       cgd 	}
    310   1.1       cgd 
    311  1.10       cgd 	(void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
    312   1.1       cgd 	if (timestamp) {
    313   1.1       cgd 		if (timestamp == 2) {
    314   1.5   mycroft 			timersub(&kth->ktr_time, &prevtime, &temp);
    315   1.5   mycroft 			prevtime = kth->ktr_time;
    316   1.5   mycroft 		} else
    317   1.1       cgd 			temp = kth->ktr_time;
    318   1.5   mycroft 		(void)printf("%ld.%06ld ", temp.tv_sec, temp.tv_usec);
    319   1.1       cgd 	}
    320   1.1       cgd 	(void)printf("%s  ", type);
    321   1.1       cgd }
    322   1.1       cgd 
    323  1.13  christos void
    324  1.13  christos ioctldecode(cmd)
    325  1.13  christos 	u_long cmd;
    326  1.13  christos {
    327  1.13  christos 	char dirbuf[4], *dir = dirbuf;
    328  1.13  christos 
    329  1.15       cgd 	if (cmd & IOC_IN)
    330  1.15       cgd 		*dir++ = 'W';
    331  1.13  christos 	if (cmd & IOC_OUT)
    332  1.13  christos 		*dir++ = 'R';
    333  1.13  christos 	*dir = '\0';
    334  1.13  christos 
    335  1.13  christos 	printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx",
    336  1.13  christos 	    dirbuf, (cmd >> 8) & 0xff, cmd & 0xff);
    337  1.13  christos 	if ((cmd & IOC_VOID) == 0)
    338  1.13  christos 		printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff);
    339  1.13  christos 	else
    340  1.13  christos 		printf(")");
    341  1.13  christos }
    342   1.1       cgd 
    343  1.17     mikel void
    344   1.1       cgd ktrsyscall(ktr)
    345  1.18     lukem 	struct ktr_syscall *ktr;
    346   1.1       cgd {
    347  1.18     lukem 	int argsize = ktr->ktr_argsize;
    348  1.18     lukem 	register_t *ap;
    349   1.1       cgd 
    350  1.11  christos 	if (ktr->ktr_code >= current->nsysnames || ktr->ktr_code < 0)
    351   1.1       cgd 		(void)printf("[%d]", ktr->ktr_code);
    352   1.1       cgd 	else
    353  1.11  christos 		(void)printf("%s", current->sysnames[ktr->ktr_code]);
    354   1.7       cgd 	ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
    355   1.7       cgd 	if (argsize) {
    356   1.1       cgd 		char c = '(';
    357   1.1       cgd 		if (fancy) {
    358   1.1       cgd 			if (ktr->ktr_code == SYS_ioctl) {
    359   1.1       cgd 				char *cp;
    360   1.1       cgd 				if (decimal)
    361   1.7       cgd 					(void)printf("(%ld", (long)*ap);
    362   1.1       cgd 				else
    363   1.7       cgd 					(void)printf("(%#lx", (long)*ap);
    364   1.7       cgd 				ap++;
    365   1.7       cgd 				argsize -= sizeof(register_t);
    366   1.7       cgd 				if ((cp = ioctlname(*ap)) != NULL)
    367   1.1       cgd 					(void)printf(",%s", cp);
    368  1.13  christos 				else
    369  1.13  christos 					ioctldecode(*ap);
    370   1.1       cgd 				c = ',';
    371   1.7       cgd 				ap++;
    372   1.7       cgd 				argsize -= sizeof(register_t);
    373   1.1       cgd 			} else if (ktr->ktr_code == SYS_ptrace) {
    374  1.12       jtc 				if (*ap >= 0 && *ap <=
    375  1.12       jtc 				    sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
    376   1.7       cgd 					(void)printf("(%s", ptrace_ops[*ap]);
    377   1.1       cgd 				else
    378   1.7       cgd 					(void)printf("(%ld", (long)*ap);
    379   1.1       cgd 				c = ',';
    380   1.7       cgd 				ap++;
    381   1.7       cgd 				argsize -= sizeof(register_t);
    382   1.1       cgd 			}
    383   1.1       cgd 		}
    384   1.7       cgd 		while (argsize) {
    385   1.1       cgd 			if (decimal)
    386   1.7       cgd 				(void)printf("%c%ld", c, (long)*ap);
    387   1.1       cgd 			else
    388   1.7       cgd 				(void)printf("%c%#lx", c, (long)*ap);
    389   1.1       cgd 			c = ',';
    390   1.7       cgd 			ap++;
    391   1.7       cgd 			argsize -= sizeof(register_t);
    392   1.1       cgd 		}
    393   1.1       cgd 		(void)putchar(')');
    394   1.1       cgd 	}
    395   1.1       cgd 	(void)putchar('\n');
    396   1.1       cgd }
    397   1.1       cgd 
    398  1.17     mikel void
    399   1.1       cgd ktrsysret(ktr)
    400   1.1       cgd 	struct ktr_sysret *ktr;
    401   1.1       cgd {
    402  1.18     lukem 	int ret = ktr->ktr_retval;
    403  1.18     lukem 	int error = ktr->ktr_error;
    404  1.18     lukem 	int code = ktr->ktr_code;
    405   1.1       cgd 
    406  1.11  christos 	if (code >= current->nsysnames || code < 0)
    407   1.1       cgd 		(void)printf("[%d] ", code);
    408   1.1       cgd 	else
    409  1.11  christos 		(void)printf("%s ", current->sysnames[code]);
    410   1.1       cgd 
    411  1.22  christos 	switch (error) {
    412  1.22  christos 	case 0:
    413   1.1       cgd 		if (fancy) {
    414   1.1       cgd 			(void)printf("%d", ret);
    415   1.1       cgd 			if (ret < 0 || ret > 9)
    416   1.1       cgd 				(void)printf("/%#x", ret);
    417   1.1       cgd 		} else {
    418   1.1       cgd 			if (decimal)
    419   1.1       cgd 				(void)printf("%d", ret);
    420   1.1       cgd 			else
    421   1.1       cgd 				(void)printf("%#x", ret);
    422   1.1       cgd 		}
    423  1.22  christos 		break;
    424  1.22  christos 
    425  1.22  christos 	default:
    426  1.22  christos 		eprint(error);
    427  1.22  christos 		break;
    428  1.22  christos 	}
    429  1.22  christos 	(void)putchar('\n');
    430  1.22  christos 
    431  1.22  christos }
    432  1.22  christos 
    433  1.22  christos /*
    434  1.22  christos  * We print the original emulation's error numerically, but we
    435  1.22  christos  * translate it to netbsd to print it symbolically.
    436  1.22  christos  */
    437  1.22  christos void
    438  1.22  christos eprint(e)
    439  1.22  christos 	int e;
    440  1.22  christos {
    441  1.22  christos 	int i = e;
    442  1.22  christos 
    443  1.22  christos 	if (current->errno) {
    444  1.22  christos 
    445  1.22  christos 		/* No remapping for ERESTART and EJUSTRETURN */
    446  1.22  christos 		/* Kludge for linux that has negative error numbers */
    447  1.22  christos 		if (current->errno[2] > 0 && e < 0)
    448  1.22  christos 			goto normal;
    449  1.22  christos 
    450  1.22  christos 		for (i = 0; i < current->nerrno; i++)
    451  1.22  christos 			if (e == current->errno[i])
    452  1.22  christos 				break;
    453  1.22  christos 
    454  1.22  christos 		if (i == current->nerrno) {
    455  1.22  christos 			printf("-1 unknown errno %d", e);
    456  1.22  christos 			return;
    457  1.22  christos 		}
    458  1.22  christos 	}
    459  1.22  christos 
    460  1.22  christos normal:
    461  1.22  christos 	switch (i) {
    462  1.22  christos 	case ERESTART:
    463   1.1       cgd 		(void)printf("RESTART");
    464  1.22  christos 		break;
    465  1.22  christos 
    466  1.22  christos 	case EJUSTRETURN:
    467   1.1       cgd 		(void)printf("JUSTRETURN");
    468  1.22  christos 		break;
    469  1.22  christos 
    470  1.22  christos 	default:
    471  1.22  christos 		(void)printf("-1 errno %d", e);
    472   1.1       cgd 		if (fancy)
    473  1.22  christos 			(void)printf(" %s", strerror(i));
    474   1.1       cgd 	}
    475   1.1       cgd }
    476   1.1       cgd 
    477  1.17     mikel void
    478  1.16        pk ktrnamei(cp, len)
    479   1.1       cgd 	char *cp;
    480  1.17     mikel 	int len;
    481   1.1       cgd {
    482  1.17     mikel 
    483   1.1       cgd 	(void)printf("\"%.*s\"\n", len, cp);
    484   1.1       cgd }
    485   1.1       cgd 
    486  1.17     mikel void
    487  1.16        pk ktremul(cp, len)
    488  1.11  christos 	char *cp;
    489  1.17     mikel 	int len;
    490  1.11  christos {
    491  1.11  christos 	char name[1024];
    492  1.11  christos 
    493  1.11  christos 	if (len >= sizeof(name))
    494  1.11  christos 		errx(1, "Emulation name too long");
    495  1.11  christos 
    496  1.11  christos 	strncpy(name, cp, len);
    497  1.11  christos 	name[len] = '\0';
    498  1.11  christos 	(void)printf("\"%s\"\n", name);
    499  1.11  christos 
    500  1.11  christos 	setemul(name);
    501  1.11  christos }
    502  1.11  christos 
    503  1.17     mikel void
    504   1.1       cgd ktrgenio(ktr, len)
    505   1.1       cgd 	struct ktr_genio *ktr;
    506  1.17     mikel 	int len;
    507   1.1       cgd {
    508  1.18     lukem 	int datalen = len - sizeof (struct ktr_genio);
    509  1.18     lukem 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
    510  1.18     lukem 	char *cp;
    511  1.18     lukem 	int col = 0;
    512  1.18     lukem 	int width;
    513   1.1       cgd 	char visbuf[5];
    514  1.20       mrg 	static int screenwidth = 0;
    515   1.1       cgd 
    516   1.1       cgd 	if (screenwidth == 0) {
    517   1.1       cgd 		struct winsize ws;
    518   1.1       cgd 
    519   1.1       cgd 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
    520   1.1       cgd 		    ws.ws_col > 8)
    521   1.1       cgd 			screenwidth = ws.ws_col;
    522   1.1       cgd 		else
    523   1.1       cgd 			screenwidth = 80;
    524   1.1       cgd 	}
    525   1.1       cgd 	printf("fd %d %s %d bytes\n", ktr->ktr_fd,
    526   1.1       cgd 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
    527   1.1       cgd 	if (maxdata && datalen > maxdata)
    528   1.1       cgd 		datalen = maxdata;
    529   1.1       cgd 	(void)printf("       \"");
    530   1.1       cgd 	col = 8;
    531   1.4   mycroft 	for (; datalen > 0; datalen--, dp++) {
    532   1.1       cgd 		(void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
    533   1.1       cgd 		cp = visbuf;
    534   1.1       cgd 		/*
    535   1.1       cgd 		 * Keep track of printables and
    536   1.1       cgd 		 * space chars (like fold(1)).
    537   1.1       cgd 		 */
    538   1.1       cgd 		if (col == 0) {
    539   1.1       cgd 			(void)putchar('\t');
    540   1.1       cgd 			col = 8;
    541   1.1       cgd 		}
    542   1.1       cgd 		switch(*cp) {
    543   1.1       cgd 		case '\n':
    544   1.1       cgd 			col = 0;
    545   1.1       cgd 			(void)putchar('\n');
    546   1.1       cgd 			continue;
    547   1.1       cgd 		case '\t':
    548   1.1       cgd 			width = 8 - (col&07);
    549   1.1       cgd 			break;
    550   1.1       cgd 		default:
    551   1.1       cgd 			width = strlen(cp);
    552   1.1       cgd 		}
    553   1.1       cgd 		if (col + width > (screenwidth-2)) {
    554   1.1       cgd 			(void)printf("\\\n\t");
    555   1.1       cgd 			col = 8;
    556   1.1       cgd 		}
    557   1.1       cgd 		col += width;
    558   1.1       cgd 		do {
    559   1.1       cgd 			(void)putchar(*cp++);
    560   1.1       cgd 		} while (*cp);
    561   1.1       cgd 	}
    562   1.1       cgd 	if (col == 0)
    563   1.1       cgd 		(void)printf("       ");
    564   1.1       cgd 	(void)printf("\"\n");
    565   1.1       cgd }
    566   1.1       cgd 
    567  1.17     mikel void
    568   1.1       cgd ktrpsig(psig)
    569   1.1       cgd 	struct ktr_psig *psig;
    570   1.1       cgd {
    571  1.21   mycroft 	int signo, first;
    572  1.17     mikel 
    573   1.4   mycroft 	(void)printf("SIG%s ", sys_signame[psig->signo]);
    574   1.1       cgd 	if (psig->action == SIG_DFL)
    575   1.1       cgd 		(void)printf("SIG_DFL\n");
    576  1.21   mycroft 	else {
    577  1.21   mycroft 		(void)printf("caught handler=0x%lx mask=(",
    578  1.21   mycroft 		    (u_long)psig->action);
    579  1.21   mycroft 		first = 1;
    580  1.21   mycroft 		for (signo = 1; signo < NSIG; signo++) {
    581  1.21   mycroft 			if (sigismember(&psig->mask, signo)) {
    582  1.21   mycroft 				if (first)
    583  1.21   mycroft 					first = 0;
    584  1.21   mycroft 				else
    585  1.21   mycroft 					(void)printf(",");
    586  1.21   mycroft 				(void)printf("%d", signo);
    587  1.21   mycroft 			}
    588  1.21   mycroft 		}
    589  1.21   mycroft 		(void)printf(") code=0x%x\n", psig->code);
    590  1.21   mycroft 	}
    591   1.1       cgd }
    592   1.1       cgd 
    593  1.17     mikel void
    594   1.1       cgd ktrcsw(cs)
    595   1.1       cgd 	struct ktr_csw *cs;
    596   1.1       cgd {
    597  1.17     mikel 
    598   1.1       cgd 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
    599   1.4   mycroft 	    cs->user ? "user" : "kernel");
    600   1.1       cgd }
    601   1.1       cgd 
    602  1.17     mikel void
    603   1.1       cgd usage()
    604   1.1       cgd {
    605   1.4   mycroft 
    606   1.1       cgd 	(void)fprintf(stderr,
    607  1.11  christos "usage: kdump [-dnlRT] [-e emulation] [-f trfile] [-m maxdata] [-t [cnis]]\n");
    608   1.1       cgd 	exit(1);
    609  1.11  christos }
    610  1.11  christos 
    611  1.17     mikel void
    612  1.11  christos setemul(name)
    613  1.11  christos 	char *name;
    614  1.11  christos {
    615  1.11  christos 	int i;
    616  1.17     mikel 
    617  1.11  christos 	for (i = 0; emulations[i].name != NULL; i++)
    618  1.11  christos 		if (strcmp(emulations[i].name, name) == 0) {
    619  1.11  christos 			current = &emulations[i];
    620  1.11  christos 			return;
    621  1.11  christos 		}
    622  1.11  christos 	warnx("Emulation `%s' unknown", name);
    623   1.1       cgd }
    624