Home | History | Annotate | Line # | Download | only in kdump
kdump.c revision 1.44
      1  1.44  jdolecek /*	$NetBSD: kdump.c,v 1.44 2002/11/28 16:18:49 jdolecek 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.44  jdolecek __RCSID("$NetBSD: kdump.c,v 1.44 2002/11/28 16:18:49 jdolecek 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.28  jdolecek #include "setemul.h"
     70  1.28  jdolecek 
     71  1.28  jdolecek #include <sys/syscall.h>
     72   1.1       cgd 
     73  1.43    atatat int timestamp, decimal, plain, tail, maxdata, numeric;
     74  1.39    atatat pid_t do_pid = -1;
     75  1.41    atatat const char *tracefile = NULL;
     76   1.1       cgd struct ktr_header ktr_header;
     77  1.35      manu int emul_changed = 0;
     78   1.1       cgd 
     79   1.1       cgd #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
     80   1.1       cgd 
     81  1.17     mikel static const char *ptrace_ops[] = {
     82  1.11  christos 	"PT_TRACE_ME",	"PT_READ_I",	"PT_READ_D",	"PT_READ_U",
     83  1.11  christos 	"PT_WRITE_I",	"PT_WRITE_D",	"PT_WRITE_U",	"PT_CONTINUE",
     84  1.11  christos 	"PT_KILL",	"PT_ATTACH",	"PT_DETACH",
     85  1.11  christos };
     86  1.11  christos 
     87  1.31  jdolecek static const char *linux_ptrace_ops[] = {
     88  1.31  jdolecek 	"PTRACE_TRACEME",
     89  1.31  jdolecek 	"PTRACE_PEEKTEXT", "PTRACE_PEEKDATA", "PTRACE_PEEKUSER",
     90  1.31  jdolecek 	"PTRACE_POKETEXT", "PTRACE_POKEDATA", "PTRACE_POKEUSER",
     91  1.31  jdolecek 	"PTRACE_CONT", "PTRACE_KILL", "PTRACE_SINGLESTEP",
     92  1.31  jdolecek 	NULL, NULL,
     93  1.31  jdolecek 	"PTRACE_GETREGS", "PTRACE_SETREGS", "PTRACE_GETFPREGS",
     94  1.31  jdolecek 	"PTRACE_SETFPREGS", "PTRACE_ATTACH", "PTRACE_DETACH",
     95  1.31  jdolecek 	"PTRACE_SYSCALL",
     96  1.31  jdolecek };
     97  1.31  jdolecek 
     98  1.17     mikel int	main __P((int, char **));
     99  1.17     mikel int	fread_tail __P((char *, int, int));
    100  1.17     mikel void	dumpheader __P((struct ktr_header *));
    101  1.17     mikel void	ioctldecode __P((u_long));
    102  1.17     mikel void	ktrsyscall __P((struct ktr_syscall *));
    103  1.17     mikel void	ktrsysret __P((struct ktr_sysret *));
    104  1.17     mikel void	ktrnamei __P((char *, int));
    105  1.27  jdolecek void	ktremul __P((char *, int, int));
    106  1.17     mikel void	ktrgenio __P((struct ktr_genio *, int));
    107  1.17     mikel void	ktrpsig __P((struct ktr_psig *));
    108  1.17     mikel void	ktrcsw __P((struct ktr_csw *));
    109  1.34  jdolecek void	ktruser __P((struct ktr_user *, int));
    110  1.17     mikel void	usage __P((void));
    111  1.22  christos void	eprint __P((int));
    112  1.17     mikel char	*ioctlname __P((long));
    113  1.37  christos static const char *signame __P((long, int));
    114  1.17     mikel 
    115   1.4   mycroft int
    116   1.1       cgd main(argc, argv)
    117   1.1       cgd 	int argc;
    118   1.1       cgd 	char *argv[];
    119   1.1       cgd {
    120   1.1       cgd 	int ch, ktrlen, size;
    121  1.18     lukem 	void *m;
    122   1.1       cgd 	int trpoints = ALL_POINTS;
    123  1.27  jdolecek 	const char *emul_name = "netbsd";
    124  1.11  christos 
    125  1.43    atatat 	while ((ch = getopt(argc, argv, "e:f:dlm:Nnp:RTt:")) != -1)
    126   1.4   mycroft 		switch (ch) {
    127  1.11  christos 		case 'e':
    128  1.27  jdolecek 			emul_name = strdup(optarg); /* it's safer to copy it */
    129  1.11  christos 			break;
    130   1.1       cgd 		case 'f':
    131   1.1       cgd 			tracefile = optarg;
    132   1.1       cgd 			break;
    133   1.1       cgd 		case 'd':
    134   1.1       cgd 			decimal = 1;
    135   1.1       cgd 			break;
    136   1.1       cgd 		case 'l':
    137   1.1       cgd 			tail = 1;
    138   1.1       cgd 			break;
    139  1.39    atatat 		case 'p':
    140  1.39    atatat 			do_pid = atoi(optarg);
    141  1.39    atatat 			break;
    142   1.1       cgd 		case 'm':
    143   1.1       cgd 			maxdata = atoi(optarg);
    144   1.1       cgd 			break;
    145  1.43    atatat 		case 'N':
    146  1.43    atatat 			numeric++;
    147  1.43    atatat 			break;
    148   1.1       cgd 		case 'n':
    149  1.36  christos 			plain++;
    150   1.1       cgd 			break;
    151   1.1       cgd 		case 'R':
    152   1.1       cgd 			timestamp = 2;	/* relative timestamp */
    153   1.1       cgd 			break;
    154   1.1       cgd 		case 'T':
    155   1.1       cgd 			timestamp = 1;
    156   1.1       cgd 			break;
    157   1.1       cgd 		case 't':
    158   1.1       cgd 			trpoints = getpoints(optarg);
    159   1.4   mycroft 			if (trpoints < 0)
    160   1.4   mycroft 				errx(1, "unknown trace point in %s", optarg);
    161   1.1       cgd 			break;
    162   1.1       cgd 		default:
    163   1.1       cgd 			usage();
    164   1.1       cgd 		}
    165   1.1       cgd 	argv += optind;
    166   1.1       cgd 	argc -= optind;
    167   1.1       cgd 
    168  1.41    atatat 	if (tracefile == NULL) {
    169  1.41    atatat 		if (argc == 1) {
    170  1.41    atatat 			tracefile = argv[0];
    171  1.41    atatat 			argv++;
    172  1.41    atatat 			argc--;
    173  1.41    atatat 		}
    174  1.41    atatat 		else
    175  1.41    atatat 			tracefile = DEF_TRACEFILE;
    176  1.41    atatat 	}
    177  1.41    atatat 
    178  1.41    atatat 	if (argc > 0)
    179   1.1       cgd 		usage();
    180   1.1       cgd 
    181  1.27  jdolecek 	setemul(emul_name, 0, 0);
    182  1.42      manu 	mach_lookup_emul();
    183  1.27  jdolecek 
    184  1.27  jdolecek 	m = malloc(size = 1024);
    185   1.4   mycroft 	if (m == NULL)
    186  1.27  jdolecek 		errx(1, "malloc: %s", strerror(ENOMEM));
    187   1.4   mycroft 	if (!freopen(tracefile, "r", stdin))
    188   1.4   mycroft 		err(1, "%s", tracefile);
    189  1.17     mikel 	while (fread_tail((char *)&ktr_header, sizeof(struct ktr_header), 1)) {
    190   1.1       cgd 		if (trpoints & (1<<ktr_header.ktr_type))
    191  1.39    atatat 			if (do_pid == -1 || ktr_header.ktr_pid == do_pid)
    192  1.39    atatat 				dumpheader(&ktr_header);
    193   1.4   mycroft 		if ((ktrlen = ktr_header.ktr_len) < 0)
    194   1.4   mycroft 			errx(1, "bogus length 0x%x", ktrlen);
    195   1.1       cgd 		if (ktrlen > size) {
    196  1.27  jdolecek 			while(ktrlen > size) size *= 2;
    197  1.27  jdolecek 			m = (void *)realloc(m, size);
    198   1.4   mycroft 			if (m == NULL)
    199  1.27  jdolecek 				errx(1, "realloc: %s", strerror(ENOMEM));
    200   1.1       cgd 		}
    201   1.4   mycroft 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
    202   1.4   mycroft 			errx(1, "data too short");
    203   1.1       cgd 		if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
    204   1.1       cgd 			continue;
    205  1.27  jdolecek 
    206  1.27  jdolecek 		/* update context to match currently processed record */
    207  1.39    atatat 		if (do_pid != -1 && ktr_header.ktr_pid != do_pid)
    208  1.39    atatat 			continue;
    209  1.27  jdolecek 		ectx_sanify(ktr_header.ktr_pid);
    210  1.27  jdolecek 
    211   1.1       cgd 		switch (ktr_header.ktr_type) {
    212   1.1       cgd 		case KTR_SYSCALL:
    213   1.1       cgd 			ktrsyscall((struct ktr_syscall *)m);
    214   1.1       cgd 			break;
    215   1.1       cgd 		case KTR_SYSRET:
    216   1.1       cgd 			ktrsysret((struct ktr_sysret *)m);
    217   1.1       cgd 			break;
    218   1.1       cgd 		case KTR_NAMEI:
    219   1.1       cgd 			ktrnamei(m, ktrlen);
    220   1.1       cgd 			break;
    221   1.1       cgd 		case KTR_GENIO:
    222   1.1       cgd 			ktrgenio((struct ktr_genio *)m, ktrlen);
    223   1.1       cgd 			break;
    224   1.1       cgd 		case KTR_PSIG:
    225   1.1       cgd 			ktrpsig((struct ktr_psig *)m);
    226   1.1       cgd 			break;
    227   1.1       cgd 		case KTR_CSW:
    228   1.1       cgd 			ktrcsw((struct ktr_csw *)m);
    229   1.1       cgd 			break;
    230  1.11  christos 		case KTR_EMUL:
    231  1.27  jdolecek 			ktremul(m, ktrlen, size);
    232  1.11  christos 			break;
    233  1.32  jdolecek 		case KTR_USER:
    234  1.34  jdolecek 			ktruser((struct ktr_user *)m, ktrlen);
    235  1.32  jdolecek 			break;
    236   1.1       cgd 		}
    237   1.1       cgd 		if (tail)
    238   1.1       cgd 			(void)fflush(stdout);
    239   1.1       cgd 	}
    240  1.17     mikel 	return (0);
    241   1.1       cgd }
    242   1.1       cgd 
    243  1.17     mikel int
    244   1.1       cgd fread_tail(buf, size, num)
    245   1.1       cgd 	char *buf;
    246   1.1       cgd 	int num, size;
    247   1.1       cgd {
    248   1.1       cgd 	int i;
    249   1.1       cgd 
    250   1.1       cgd 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
    251   1.1       cgd 		(void)sleep(1);
    252   1.1       cgd 		clearerr(stdin);
    253   1.1       cgd 	}
    254   1.1       cgd 	return (i);
    255   1.1       cgd }
    256   1.1       cgd 
    257  1.17     mikel void
    258   1.1       cgd dumpheader(kth)
    259   1.1       cgd 	struct ktr_header *kth;
    260   1.1       cgd {
    261   1.6   mycroft 	char unknown[64], *type;
    262   1.6   mycroft 	static struct timeval prevtime;
    263   1.6   mycroft 	struct timeval temp;
    264   1.1       cgd 
    265   1.1       cgd 	switch (kth->ktr_type) {
    266   1.1       cgd 	case KTR_SYSCALL:
    267   1.1       cgd 		type = "CALL";
    268   1.1       cgd 		break;
    269   1.1       cgd 	case KTR_SYSRET:
    270   1.1       cgd 		type = "RET ";
    271   1.1       cgd 		break;
    272   1.1       cgd 	case KTR_NAMEI:
    273   1.1       cgd 		type = "NAMI";
    274   1.1       cgd 		break;
    275   1.1       cgd 	case KTR_GENIO:
    276   1.1       cgd 		type = "GIO ";
    277   1.1       cgd 		break;
    278   1.1       cgd 	case KTR_PSIG:
    279   1.1       cgd 		type = "PSIG";
    280   1.1       cgd 		break;
    281   1.1       cgd 	case KTR_CSW:
    282   1.1       cgd 		type = "CSW";
    283   1.1       cgd 		break;
    284  1.11  christos 	case KTR_EMUL:
    285  1.11  christos 		type = "EMUL";
    286  1.11  christos 		break;
    287  1.32  jdolecek 	case KTR_USER:
    288  1.32  jdolecek 		type = "USER";
    289  1.32  jdolecek 		break;
    290   1.1       cgd 	default:
    291   1.1       cgd 		(void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
    292   1.1       cgd 		type = unknown;
    293   1.1       cgd 	}
    294   1.1       cgd 
    295  1.10       cgd 	(void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
    296   1.1       cgd 	if (timestamp) {
    297   1.1       cgd 		if (timestamp == 2) {
    298   1.5   mycroft 			timersub(&kth->ktr_time, &prevtime, &temp);
    299   1.5   mycroft 			prevtime = kth->ktr_time;
    300   1.5   mycroft 		} else
    301   1.1       cgd 			temp = kth->ktr_time;
    302  1.26    kleink 		(void)printf("%ld.%06ld ",
    303  1.26    kleink 		    (long int)temp.tv_sec, (long int)temp.tv_usec);
    304   1.1       cgd 	}
    305   1.1       cgd 	(void)printf("%s  ", type);
    306   1.1       cgd }
    307   1.1       cgd 
    308  1.13  christos void
    309  1.13  christos ioctldecode(cmd)
    310  1.13  christos 	u_long cmd;
    311  1.13  christos {
    312  1.13  christos 	char dirbuf[4], *dir = dirbuf;
    313  1.13  christos 
    314  1.15       cgd 	if (cmd & IOC_IN)
    315  1.15       cgd 		*dir++ = 'W';
    316  1.13  christos 	if (cmd & IOC_OUT)
    317  1.13  christos 		*dir++ = 'R';
    318  1.13  christos 	*dir = '\0';
    319  1.13  christos 
    320  1.13  christos 	printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx",
    321  1.30        is 	    dirbuf, (int) ((cmd >> 8) & 0xff), cmd & 0xff);
    322  1.13  christos 	if ((cmd & IOC_VOID) == 0)
    323  1.13  christos 		printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff);
    324  1.13  christos 	else
    325  1.13  christos 		printf(")");
    326  1.13  christos }
    327   1.1       cgd 
    328  1.17     mikel void
    329   1.1       cgd ktrsyscall(ktr)
    330  1.18     lukem 	struct ktr_syscall *ktr;
    331   1.1       cgd {
    332  1.18     lukem 	int argsize = ktr->ktr_argsize;
    333  1.42      manu 	const struct emulation *revelant = current;
    334  1.18     lukem 	register_t *ap;
    335   1.1       cgd 
    336  1.43    atatat 	if (((ktr->ktr_code >= revelant->nsysnames || ktr->ktr_code < 0)
    337  1.43    atatat 	    && (mach_traps_dispatch(&ktr->ktr_code, &revelant) == 0)) ||
    338  1.43    atatat 	    numeric)
    339   1.1       cgd 		(void)printf("[%d]", ktr->ktr_code);
    340   1.1       cgd 	else
    341  1.42      manu 		(void)printf("%s", revelant->sysnames[ktr->ktr_code]);
    342   1.7       cgd 	ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
    343   1.7       cgd 	if (argsize) {
    344   1.1       cgd 		char c = '(';
    345  1.36  christos 		if (!plain) {
    346  1.37  christos 			char *cp;
    347  1.37  christos 
    348  1.37  christos 			switch (ktr->ktr_code) {
    349  1.37  christos 			case SYS_ioctl:
    350   1.1       cgd 				if (decimal)
    351   1.7       cgd 					(void)printf("(%ld", (long)*ap);
    352   1.1       cgd 				else
    353   1.7       cgd 					(void)printf("(%#lx", (long)*ap);
    354   1.7       cgd 				ap++;
    355   1.7       cgd 				argsize -= sizeof(register_t);
    356   1.7       cgd 				if ((cp = ioctlname(*ap)) != NULL)
    357   1.1       cgd 					(void)printf(",%s", cp);
    358  1.13  christos 				else
    359  1.13  christos 					ioctldecode(*ap);
    360   1.1       cgd 				c = ',';
    361   1.7       cgd 				ap++;
    362   1.7       cgd 				argsize -= sizeof(register_t);
    363  1.37  christos 				break;
    364  1.37  christos 
    365  1.37  christos 			case SYS_ptrace:
    366  1.42      manu 				if (strcmp(revelant->name, "linux") == 0) {
    367  1.37  christos 				  if (*ap >= 0 && *ap <=
    368  1.37  christos 				      sizeof(linux_ptrace_ops) /
    369  1.37  christos 				      sizeof(linux_ptrace_ops[0]))
    370  1.37  christos 					(void)printf("(%s",
    371  1.37  christos 					    linux_ptrace_ops[*ap]);
    372  1.31  jdolecek 				  else
    373  1.31  jdolecek 					(void)printf("(%ld", (long)*ap);
    374  1.31  jdolecek 				} else {
    375  1.31  jdolecek 				  if (*ap >= 0 && *ap <=
    376  1.12       jtc 				    sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
    377   1.7       cgd 					(void)printf("(%s", ptrace_ops[*ap]);
    378  1.31  jdolecek 				  else
    379   1.7       cgd 					(void)printf("(%ld", (long)*ap);
    380  1.31  jdolecek 				}
    381   1.1       cgd 				c = ',';
    382   1.7       cgd 				ap++;
    383   1.7       cgd 				argsize -= sizeof(register_t);
    384  1.37  christos 				break;
    385  1.37  christos 
    386  1.37  christos 			case SYS_kill:
    387  1.37  christos 				if (decimal)
    388  1.37  christos 					(void)printf("(%ld, SIG%s",
    389  1.37  christos 					    (long)ap[0], signame(ap[1], 1));
    390  1.37  christos 				else
    391  1.37  christos 					(void)printf("(%#lx, SIG%s",
    392  1.37  christos 					    (long)ap[0], signame(ap[1], 1));
    393  1.37  christos 				ap += 2;
    394  1.37  christos 				argsize -= 2 * sizeof(register_t);
    395  1.37  christos 				break;
    396  1.37  christos 
    397  1.37  christos 			default:
    398  1.37  christos 				/* No special handling */
    399  1.37  christos 				break;
    400   1.1       cgd 			}
    401   1.1       cgd 		}
    402   1.7       cgd 		while (argsize) {
    403   1.1       cgd 			if (decimal)
    404   1.7       cgd 				(void)printf("%c%ld", c, (long)*ap);
    405   1.1       cgd 			else
    406   1.7       cgd 				(void)printf("%c%#lx", c, (long)*ap);
    407   1.1       cgd 			c = ',';
    408   1.7       cgd 			ap++;
    409   1.7       cgd 			argsize -= sizeof(register_t);
    410   1.1       cgd 		}
    411   1.1       cgd 		(void)putchar(')');
    412   1.1       cgd 	}
    413   1.1       cgd 	(void)putchar('\n');
    414   1.1       cgd }
    415   1.1       cgd 
    416  1.17     mikel void
    417   1.1       cgd ktrsysret(ktr)
    418   1.1       cgd 	struct ktr_sysret *ktr;
    419   1.1       cgd {
    420  1.35      manu 	const struct emulation *revelant;
    421  1.24   thorpej 	register_t ret = ktr->ktr_retval;
    422  1.18     lukem 	int error = ktr->ktr_error;
    423  1.18     lukem 	int code = ktr->ktr_code;
    424   1.1       cgd 
    425  1.35      manu 	if (emul_changed)
    426  1.35      manu 		revelant = previous;
    427  1.35      manu 	else
    428  1.35      manu 		revelant = current;
    429  1.35      manu 	emul_changed = 0;
    430  1.35      manu 
    431  1.42      manu 	if ((code >= revelant->nsysnames || code < 0 || plain > 1)
    432  1.42      manu 	    && (mach_traps_dispatch(&code, &revelant) == 0))
    433   1.1       cgd 		(void)printf("[%d] ", code);
    434   1.1       cgd 	else
    435  1.35      manu 		(void)printf("%s ", revelant->sysnames[code]);
    436   1.1       cgd 
    437  1.22  christos 	switch (error) {
    438  1.22  christos 	case 0:
    439  1.36  christos 		if (!plain) {
    440  1.24   thorpej 			(void)printf("%ld", (long)ret);
    441   1.1       cgd 			if (ret < 0 || ret > 9)
    442  1.24   thorpej 				(void)printf("/%#lx", (long)ret);
    443   1.1       cgd 		} else {
    444   1.1       cgd 			if (decimal)
    445  1.24   thorpej 				(void)printf("%ld", (long)ret);
    446   1.1       cgd 			else
    447  1.24   thorpej 				(void)printf("%#lx", (long)ret);
    448   1.1       cgd 		}
    449  1.22  christos 		break;
    450  1.22  christos 
    451  1.22  christos 	default:
    452  1.22  christos 		eprint(error);
    453  1.22  christos 		break;
    454  1.22  christos 	}
    455  1.22  christos 	(void)putchar('\n');
    456  1.22  christos }
    457  1.22  christos 
    458  1.22  christos /*
    459  1.22  christos  * We print the original emulation's error numerically, but we
    460  1.22  christos  * translate it to netbsd to print it symbolically.
    461  1.22  christos  */
    462  1.22  christos void
    463  1.22  christos eprint(e)
    464  1.22  christos 	int e;
    465  1.22  christos {
    466  1.22  christos 	int i = e;
    467  1.22  christos 
    468  1.33    itojun 	if (current->errnomap) {
    469  1.22  christos 
    470  1.22  christos 		/* No remapping for ERESTART and EJUSTRETURN */
    471  1.22  christos 		/* Kludge for linux that has negative error numbers */
    472  1.33    itojun 		if (current->errnomap[2] > 0 && e < 0)
    473  1.22  christos 			goto normal;
    474  1.22  christos 
    475  1.33    itojun 		for (i = 0; i < current->nerrnomap; i++)
    476  1.33    itojun 			if (e == current->errnomap[i])
    477  1.22  christos 				break;
    478  1.22  christos 
    479  1.33    itojun 		if (i == current->nerrnomap) {
    480  1.22  christos 			printf("-1 unknown errno %d", e);
    481  1.22  christos 			return;
    482  1.22  christos 		}
    483  1.22  christos 	}
    484  1.22  christos 
    485  1.22  christos normal:
    486  1.22  christos 	switch (i) {
    487  1.22  christos 	case ERESTART:
    488   1.1       cgd 		(void)printf("RESTART");
    489  1.22  christos 		break;
    490  1.22  christos 
    491  1.22  christos 	case EJUSTRETURN:
    492   1.1       cgd 		(void)printf("JUSTRETURN");
    493  1.22  christos 		break;
    494  1.22  christos 
    495  1.22  christos 	default:
    496  1.22  christos 		(void)printf("-1 errno %d", e);
    497  1.36  christos 		if (!plain)
    498  1.22  christos 			(void)printf(" %s", strerror(i));
    499   1.1       cgd 	}
    500   1.1       cgd }
    501   1.1       cgd 
    502  1.17     mikel void
    503  1.16        pk ktrnamei(cp, len)
    504   1.1       cgd 	char *cp;
    505  1.17     mikel 	int len;
    506   1.1       cgd {
    507  1.17     mikel 
    508   1.1       cgd 	(void)printf("\"%.*s\"\n", len, cp);
    509   1.1       cgd }
    510   1.1       cgd 
    511  1.17     mikel void
    512  1.27  jdolecek ktremul(name, len, bufsize)
    513  1.27  jdolecek 	char *name;
    514  1.27  jdolecek 	int len, bufsize;
    515  1.11  christos {
    516  1.27  jdolecek 	if (len >= bufsize)
    517  1.27  jdolecek 		len = bufsize - 1;
    518  1.11  christos 
    519  1.27  jdolecek 	name[len] = '\0';
    520  1.27  jdolecek 	setemul(name, ktr_header.ktr_pid, 1);
    521  1.35      manu 	emul_changed = 1;
    522  1.11  christos 
    523  1.11  christos 	(void)printf("\"%s\"\n", name);
    524  1.11  christos }
    525  1.11  christos 
    526  1.17     mikel void
    527   1.1       cgd ktrgenio(ktr, len)
    528   1.1       cgd 	struct ktr_genio *ktr;
    529  1.17     mikel 	int len;
    530   1.1       cgd {
    531  1.18     lukem 	int datalen = len - sizeof (struct ktr_genio);
    532  1.18     lukem 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
    533  1.18     lukem 	char *cp;
    534  1.18     lukem 	int col = 0;
    535  1.18     lukem 	int width;
    536   1.1       cgd 	char visbuf[5];
    537  1.20       mrg 	static int screenwidth = 0;
    538   1.1       cgd 
    539   1.1       cgd 	if (screenwidth == 0) {
    540   1.1       cgd 		struct winsize ws;
    541   1.1       cgd 
    542  1.36  christos 		if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
    543   1.1       cgd 		    ws.ws_col > 8)
    544   1.1       cgd 			screenwidth = ws.ws_col;
    545   1.1       cgd 		else
    546   1.1       cgd 			screenwidth = 80;
    547   1.1       cgd 	}
    548   1.1       cgd 	printf("fd %d %s %d bytes\n", ktr->ktr_fd,
    549   1.1       cgd 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
    550   1.1       cgd 	if (maxdata && datalen > maxdata)
    551   1.1       cgd 		datalen = maxdata;
    552   1.1       cgd 	(void)printf("       \"");
    553   1.1       cgd 	col = 8;
    554   1.4   mycroft 	for (; datalen > 0; datalen--, dp++) {
    555  1.29      tron 		(void) vis(visbuf, *dp, VIS_CSTYLE, datalen>1?*(dp+1):0);
    556   1.1       cgd 		cp = visbuf;
    557   1.1       cgd 		/*
    558   1.1       cgd 		 * Keep track of printables and
    559   1.1       cgd 		 * space chars (like fold(1)).
    560   1.1       cgd 		 */
    561   1.1       cgd 		if (col == 0) {
    562   1.1       cgd 			(void)putchar('\t');
    563   1.1       cgd 			col = 8;
    564   1.1       cgd 		}
    565   1.1       cgd 		switch(*cp) {
    566   1.1       cgd 		case '\n':
    567   1.1       cgd 			col = 0;
    568   1.1       cgd 			(void)putchar('\n');
    569   1.1       cgd 			continue;
    570   1.1       cgd 		case '\t':
    571   1.1       cgd 			width = 8 - (col&07);
    572   1.1       cgd 			break;
    573   1.1       cgd 		default:
    574   1.1       cgd 			width = strlen(cp);
    575   1.1       cgd 		}
    576   1.1       cgd 		if (col + width > (screenwidth-2)) {
    577   1.1       cgd 			(void)printf("\\\n\t");
    578   1.1       cgd 			col = 8;
    579   1.1       cgd 		}
    580   1.1       cgd 		col += width;
    581   1.1       cgd 		do {
    582   1.1       cgd 			(void)putchar(*cp++);
    583   1.1       cgd 		} while (*cp);
    584   1.1       cgd 	}
    585   1.1       cgd 	if (col == 0)
    586   1.1       cgd 		(void)printf("       ");
    587   1.1       cgd 	(void)printf("\"\n");
    588   1.1       cgd }
    589   1.1       cgd 
    590  1.17     mikel void
    591   1.1       cgd ktrpsig(psig)
    592   1.1       cgd 	struct ktr_psig *psig;
    593   1.1       cgd {
    594  1.21   mycroft 	int signo, first;
    595  1.17     mikel 
    596  1.37  christos 	(void)printf("SIG%s ", signame(psig->signo, 0));
    597   1.1       cgd 	if (psig->action == SIG_DFL)
    598   1.1       cgd 		(void)printf("SIG_DFL\n");
    599  1.21   mycroft 	else {
    600  1.21   mycroft 		(void)printf("caught handler=0x%lx mask=(",
    601  1.21   mycroft 		    (u_long)psig->action);
    602  1.21   mycroft 		first = 1;
    603  1.21   mycroft 		for (signo = 1; signo < NSIG; signo++) {
    604  1.21   mycroft 			if (sigismember(&psig->mask, signo)) {
    605  1.21   mycroft 				if (first)
    606  1.21   mycroft 					first = 0;
    607  1.21   mycroft 				else
    608  1.21   mycroft 					(void)printf(",");
    609  1.21   mycroft 				(void)printf("%d", signo);
    610  1.21   mycroft 			}
    611  1.21   mycroft 		}
    612  1.21   mycroft 		(void)printf(") code=0x%x\n", psig->code);
    613  1.21   mycroft 	}
    614   1.1       cgd }
    615   1.1       cgd 
    616  1.17     mikel void
    617   1.1       cgd ktrcsw(cs)
    618   1.1       cgd 	struct ktr_csw *cs;
    619   1.1       cgd {
    620  1.17     mikel 
    621   1.1       cgd 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
    622   1.4   mycroft 	    cs->user ? "user" : "kernel");
    623  1.32  jdolecek }
    624  1.32  jdolecek 
    625  1.32  jdolecek void
    626  1.34  jdolecek ktruser(usr, len)
    627  1.34  jdolecek 	struct ktr_user *usr;
    628  1.32  jdolecek 	int len;
    629  1.32  jdolecek {
    630  1.32  jdolecek 	int i;
    631  1.40  jdolecek 	unsigned char *dta;
    632  1.34  jdolecek 
    633  1.34  jdolecek 	printf("\"%.*s: %d, ", KTR_USER_MAXIDLEN, usr->ktr_id, len);
    634  1.40  jdolecek 	dta = (unsigned char *)usr;
    635  1.34  jdolecek 	for(i=sizeof(struct ktr_user); i < len; i++)
    636  1.40  jdolecek 		printf("%02x", (unsigned int) dta[i]);
    637  1.32  jdolecek 	printf("\"\n");
    638  1.37  christos }
    639  1.37  christos 
    640  1.37  christos static const char *
    641  1.37  christos signame(long sig, int xlat)
    642  1.37  christos {
    643  1.37  christos 	static char buf[64];
    644  1.44  jdolecek 	if (sig == 0)
    645  1.44  jdolecek 		return " 0";
    646  1.44  jdolecek 	else if (sig < 0 || sig >= NSIG) {
    647  1.37  christos 		(void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
    648  1.37  christos 		return buf;
    649  1.37  christos 	} else
    650  1.38  christos 		return sys_signame[(xlat && current->signalmap != NULL) ?
    651  1.38  christos 		    current->signalmap[sig] : sig];
    652   1.1       cgd }
    653   1.1       cgd 
    654  1.17     mikel void
    655   1.1       cgd usage()
    656   1.1       cgd {
    657   1.4   mycroft 
    658   1.1       cgd 	(void)fprintf(stderr,
    659  1.41    atatat "usage: kdump [-dnlRT] [-e emulation] [-f trfile] [-m maxdata] [-p pid]\n"
    660  1.41    atatat "             [-t [cnis]] [trfile]\n");
    661   1.1       cgd 	exit(1);
    662  1.11  christos }
    663