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