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