kdump.c revision 171333
1171173Smlaier/*-
2171173Smlaier * Copyright (c) 1988, 1993
3171173Smlaier *	The Regents of the University of California.  All rights reserved.
4171173Smlaier *
5171173Smlaier * Redistribution and use in source and binary forms, with or without
6171173Smlaier * modification, are permitted provided that the following conditions
7171173Smlaier * are met:
8171173Smlaier * 1. Redistributions of source code must retain the above copyright
9171173Smlaier *    notice, this list of conditions and the following disclaimer.
10171173Smlaier * 2. Redistributions in binary form must reproduce the above copyright
11171173Smlaier *    notice, this list of conditions and the following disclaimer in the
12171173Smlaier *    documentation and/or other materials provided with the distribution.
13171173Smlaier * 3. All advertising materials mentioning features or use of this software
14171173Smlaier *    must display the following acknowledgement:
15171173Smlaier *	This product includes software developed by the University of
16171173Smlaier *	California, Berkeley and its contributors.
17171173Smlaier * 4. Neither the name of the University nor the names of its contributors
18171173Smlaier *    may be used to endorse or promote products derived from this software
19171173Smlaier *    without specific prior written permission.
20171173Smlaier *
21171173Smlaier * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22171173Smlaier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23171173Smlaier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24171173Smlaier * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25201390Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26201390Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27171173Smlaier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1988, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)kdump.c	8.1 (Berkeley) 6/6/93";
43#endif
44#endif /* not lint */
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: head/usr.bin/kdump/kdump.c 171333 2007-07-10 00:01:30Z jhb $");
47
48#define _KERNEL
49extern int errno;
50#include <sys/errno.h>
51#undef _KERNEL
52#include <sys/param.h>
53#include <sys/errno.h>
54#define _KERNEL
55#include <sys/time.h>
56#undef _KERNEL
57#include <sys/uio.h>
58#include <sys/ktrace.h>
59#include <sys/ioctl.h>
60#include <sys/socket.h>
61#include <dlfcn.h>
62#include <err.h>
63#include <locale.h>
64#include <stdio.h>
65#include <stdlib.h>
66#include <string.h>
67#include <unistd.h>
68#include <vis.h>
69#include "ktrace.h"
70#include "kdump_subr.h"
71
72int fread_tail(void *, int, int);
73void dumpheader(struct ktr_header *);
74void ktrsyscall(struct ktr_syscall *);
75void ktrsysret(struct ktr_sysret *);
76void ktrnamei(char *, int);
77void hexdump(char *, int, int);
78void visdump(char *, int, int);
79void ktrgenio(struct ktr_genio *, int);
80void ktrpsig(struct ktr_psig *);
81void ktrcsw(struct ktr_csw *);
82void ktruser(int, unsigned char *);
83void usage(void);
84const char *ioctlname(u_long);
85
86int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata;
87const char *tracefile = DEF_TRACEFILE;
88struct ktr_header ktr_header;
89
90#define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
91
92int
93main(int argc, char *argv[])
94{
95	int ch, ktrlen, size;
96	void *m;
97	int trpoints = ALL_POINTS;
98	int drop_logged;
99	pid_t pid = 0;
100
101	(void) setlocale(LC_CTYPE, "");
102
103	while ((ch = getopt(argc,argv,"f:dElm:np:HRsTt:")) != -1)
104		switch((char)ch) {
105		case 'f':
106			tracefile = optarg;
107			break;
108		case 'd':
109			decimal = 1;
110			break;
111		case 'l':
112			tail = 1;
113			break;
114		case 'm':
115			maxdata = atoi(optarg);
116			break;
117		case 'n':
118			fancy = 0;
119			break;
120		case 'p':
121			pid = atoi(optarg);
122			break;
123		case 's':
124			suppressdata = 1;
125			break;
126		case 'E':
127			timestamp = 3;	/* elapsed timestamp */
128			break;
129		case 'H':
130			threads = 1;
131			break;
132		case 'R':
133			timestamp = 2;	/* relative timestamp */
134			break;
135		case 'T':
136			timestamp = 1;
137			break;
138		case 't':
139			trpoints = getpoints(optarg);
140			if (trpoints < 0)
141				errx(1, "unknown trace point in %s", optarg);
142			break;
143		default:
144			usage();
145		}
146
147	if (argc > optind)
148		usage();
149
150	m = (void *)malloc(size = 1025);
151	if (m == NULL)
152		errx(1, "%s", strerror(ENOMEM));
153	if (!freopen(tracefile, "r", stdin))
154		err(1, "%s", tracefile);
155	drop_logged = 0;
156	while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
157		if (ktr_header.ktr_type & KTR_DROP) {
158			ktr_header.ktr_type &= ~KTR_DROP;
159			if (!drop_logged && threads) {
160				(void)printf("%6d %6d %-8.*s Events dropped.\n",
161				    ktr_header.ktr_pid, ktr_header.ktr_tid >
162				    0 ? ktr_header.ktr_tid : 0, MAXCOMLEN,
163				    ktr_header.ktr_comm);
164				drop_logged = 1;
165			} else if (!drop_logged) {
166				(void)printf("%6d %-8.*s Events dropped.\n",
167				    ktr_header.ktr_pid, MAXCOMLEN,
168				    ktr_header.ktr_comm);
169				drop_logged = 1;
170			}
171		}
172		if (trpoints & (1<<ktr_header.ktr_type))
173			if (pid == 0 || ktr_header.ktr_pid == pid)
174				dumpheader(&ktr_header);
175		if ((ktrlen = ktr_header.ktr_len) < 0)
176			errx(1, "bogus length 0x%x", ktrlen);
177		if (ktrlen > size) {
178			m = (void *)realloc(m, ktrlen+1);
179			if (m == NULL)
180				errx(1, "%s", strerror(ENOMEM));
181			size = ktrlen;
182		}
183		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
184			errx(1, "data too short");
185		if (pid && ktr_header.ktr_pid != pid)
186			continue;
187		if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
188			continue;
189		drop_logged = 0;
190		switch (ktr_header.ktr_type) {
191		case KTR_SYSCALL:
192			ktrsyscall((struct ktr_syscall *)m);
193			break;
194		case KTR_SYSRET:
195			ktrsysret((struct ktr_sysret *)m);
196			break;
197		case KTR_NAMEI:
198			ktrnamei(m, ktrlen);
199			break;
200		case KTR_GENIO:
201			ktrgenio((struct ktr_genio *)m, ktrlen);
202			break;
203		case KTR_PSIG:
204			ktrpsig((struct ktr_psig *)m);
205			break;
206		case KTR_CSW:
207			ktrcsw((struct ktr_csw *)m);
208			break;
209		case KTR_USER:
210			ktruser(ktrlen, m);
211			break;
212		default:
213			printf("\n");
214			break;
215		}
216		if (tail)
217			(void)fflush(stdout);
218	}
219	return 0;
220}
221
222int
223fread_tail(void *buf, int size, int num)
224{
225	int i;
226
227	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
228		(void)sleep(1);
229		clearerr(stdin);
230	}
231	return (i);
232}
233
234void
235dumpheader(struct ktr_header *kth)
236{
237	static char unknown[64];
238	static struct timeval prevtime, temp;
239	const char *type;
240
241	switch (kth->ktr_type) {
242	case KTR_SYSCALL:
243		type = "CALL";
244		break;
245	case KTR_SYSRET:
246		type = "RET ";
247		break;
248	case KTR_NAMEI:
249		type = "NAMI";
250		break;
251	case KTR_GENIO:
252		type = "GIO ";
253		break;
254	case KTR_PSIG:
255		type = "PSIG";
256		break;
257	case KTR_CSW:
258		type = "CSW ";
259		break;
260	case KTR_USER:
261		type = "USER";
262		break;
263	default:
264		(void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
265		type = unknown;
266	}
267
268	/*
269	 * The ktr_tid field was previously the ktr_buffer field, which held
270	 * the kernel pointer value for the buffer associated with data
271	 * following the record header.  It now holds a threadid, but only
272	 * for trace files after the change.  Older trace files still contain
273	 * kernel pointers.  Detect this and suppress the results by printing
274	 * negative tid's as 0.
275	 */
276	if (threads)
277		(void)printf("%6d %6d %-8.*s ", kth->ktr_pid, kth->ktr_tid >
278		    0 ? kth->ktr_tid : 0, MAXCOMLEN, kth->ktr_comm);
279	else
280		(void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN,
281		    kth->ktr_comm);
282	if (timestamp) {
283		if (timestamp == 3) {
284			if (prevtime.tv_sec == 0)
285				prevtime = kth->ktr_time;
286			timevalsub(&kth->ktr_time, &prevtime);
287		}
288		if (timestamp == 2) {
289			temp = kth->ktr_time;
290			timevalsub(&kth->ktr_time, &prevtime);
291			prevtime = temp;
292		}
293		(void)printf("%ld.%06ld ",
294		    kth->ktr_time.tv_sec, kth->ktr_time.tv_usec);
295	}
296	(void)printf("%s  ", type);
297}
298
299#include <sys/syscall.h>
300#define KTRACE
301#include <sys/kern/syscalls.c>
302#undef KTRACE
303int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
304
305void
306ktrsyscall(struct ktr_syscall *ktr)
307{
308	int narg = ktr->ktr_narg;
309	register_t *ip;
310
311	if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0)
312		(void)printf("[%d]", ktr->ktr_code);
313	else
314		(void)printf("%s", syscallnames[ktr->ktr_code]);
315	ip = &ktr->ktr_args[0];
316	if (narg) {
317		char c = '(';
318		if (fancy) {
319
320#define print_number(i,n,c) do {                      \
321	if (decimal)                                  \
322		(void)printf("%c%ld", c, (long)*i);   \
323	else                                          \
324		(void)printf("%c%#lx", c, (long)*i);  \
325	i++;                                          \
326	n--;                                          \
327	c = ',';                                      \
328	} while (0);
329
330			if (ktr->ktr_code == SYS_ioctl) {
331				const char *cp;
332				print_number(ip,narg,c);
333				if ((cp = ioctlname(*ip)) != NULL)
334					(void)printf(",%s", cp);
335				else {
336					if (decimal)
337						(void)printf(",%ld", (long)*ip);
338					else
339						(void)printf(",%#lx ", (long)*ip);
340				}
341				c = ',';
342				ip++;
343				narg--;
344			} else if (ktr->ktr_code == SYS_ptrace) {
345				(void)putchar('(');
346				ptraceopname ((int)*ip);
347				c = ',';
348				ip++;
349				narg--;
350			} else if (ktr->ktr_code == SYS_access ||
351				   ktr->ktr_code == SYS_eaccess) {
352				print_number(ip,narg,c);
353				(void)putchar(',');
354				accessmodename ((int)*ip);
355				ip++;
356				narg--;
357			} else if (ktr->ktr_code == SYS_open) {
358				int	flags;
359				int	mode;
360				print_number(ip,narg,c);
361				flags = *ip;
362				mode = *++ip;
363				(void)putchar(',');
364				flagsandmodename (flags, mode, decimal);
365				ip++;
366				narg-=2;
367			} else if (ktr->ktr_code == SYS_wait4) {
368				print_number(ip,narg,c);
369				print_number(ip,narg,c);
370				(void)putchar(',');
371				wait4optname ((int)*ip);
372				ip++;
373				narg--;
374			} else if (ktr->ktr_code == SYS_chmod ||
375				   ktr->ktr_code == SYS_fchmod ||
376				   ktr->ktr_code == SYS_lchmod) {
377				print_number(ip,narg,c);
378				(void)putchar(',');
379				modename ((int)*ip);
380				ip++;
381				narg--;
382			} else if (ktr->ktr_code == SYS_mknod) {
383				print_number(ip,narg,c);
384				(void)putchar(',');
385				modename ((int)*ip);
386				ip++;
387				narg--;
388			} else if (ktr->ktr_code == SYS_getfsstat) {
389				print_number(ip,narg,c);
390				print_number(ip,narg,c);
391				(void)putchar(',');
392				getfsstatflagsname ((int)*ip);
393				ip++;
394				narg--;
395			} else if (ktr->ktr_code == SYS_mount) {
396				print_number(ip,narg,c);
397				print_number(ip,narg,c);
398				(void)putchar(',');
399				mountflagsname ((int)*ip);
400				ip++;
401				narg--;
402			} else if (ktr->ktr_code == SYS_unmount) {
403				print_number(ip,narg,c);
404				(void)putchar(',');
405				mountflagsname ((int)*ip);
406				ip++;
407				narg--;
408			} else if (ktr->ktr_code == SYS_recvmsg ||
409				   ktr->ktr_code == SYS_sendmsg) {
410				print_number(ip,narg,c);
411				print_number(ip,narg,c);
412				(void)putchar(',');
413				sendrecvflagsname ((int)*ip);
414				ip++;
415				narg--;
416			} else if (ktr->ktr_code == SYS_recvfrom ||
417				   ktr->ktr_code == SYS_sendto) {
418				print_number(ip,narg,c);
419				print_number(ip,narg,c);
420				print_number(ip,narg,c);
421				(void)putchar(',');
422				sendrecvflagsname ((int)*ip);
423				ip++;
424				narg--;
425			} else if (ktr->ktr_code == SYS_chflags ||
426				   ktr->ktr_code == SYS_fchflags ||
427				   ktr->ktr_code == SYS_lchflags) {
428				print_number(ip,narg,c);
429				(void)putchar(',');
430				modename((int)*ip);
431				ip++;
432				narg--;
433			} else if (ktr->ktr_code == SYS_kill) {
434				print_number(ip,narg,c);
435				(void)putchar(',');
436				signame((int)*ip);
437				ip++;
438				narg--;
439			} else if (ktr->ktr_code == SYS_reboot) {
440				(void)putchar('(');
441				rebootoptname((int)*ip);
442				ip++;
443				narg--;
444			} else if (ktr->ktr_code == SYS_umask) {
445				(void)putchar('(');
446				modename((int)*ip);
447				ip++;
448				narg--;
449			} else if (ktr->ktr_code == SYS_msync) {
450				print_number(ip,narg,c);
451				print_number(ip,narg,c);
452				(void)putchar(',');
453				msyncflagsname((int)*ip);
454				ip++;
455				narg--;
456#ifdef SYS_freebsd6_mmap
457			} else if (ktr->ktr_code == SYS_freebsd6_mmap) {
458				print_number(ip,narg,c);
459				print_number(ip,narg,c);
460				(void)putchar(',');
461				mmapprotname ((int)*ip);
462				(void)putchar(',');
463				ip++;
464				narg--;
465				mmapflagsname ((int)*ip);
466				ip++;
467				narg--;
468#endif
469			} else if (ktr->ktr_code == SYS_mmap) {
470				print_number(ip,narg,c);
471				print_number(ip,narg,c);
472				(void)putchar(',');
473				mmapprotname ((int)*ip);
474				(void)putchar(',');
475				ip++;
476				narg--;
477				mmapflagsname ((int)*ip);
478				ip++;
479				narg--;
480			} else if (ktr->ktr_code == SYS_mprotect) {
481				print_number(ip,narg,c);
482				print_number(ip,narg,c);
483				(void)putchar(',');
484				mmapprotname ((int)*ip);
485				ip++;
486				narg--;
487			} else if (ktr->ktr_code == SYS_madvise) {
488				print_number(ip,narg,c);
489				print_number(ip,narg,c);
490				(void)putchar(',');
491				madvisebehavname((int)*ip);
492				ip++;
493				narg--;
494			} else if (ktr->ktr_code == SYS_setpriority) {
495				print_number(ip,narg,c);
496				print_number(ip,narg,c);
497				(void)putchar(',');
498				prioname((int)*ip);
499				ip++;
500				narg--;
501			} else if (ktr->ktr_code == SYS_fcntl) {
502				int cmd;
503				int arg;
504				print_number(ip,narg,c);
505				cmd = *ip;
506				arg = *++ip;
507				(void)putchar(',');
508				fcntlcmdname(cmd, arg, decimal);
509				ip++;
510				narg-=2;
511			} else if (ktr->ktr_code == SYS_socket) {
512				int sockdomain;
513				(void)putchar('(');
514				sockdomain=(int)*ip;
515				sockdomainname(sockdomain);
516				ip++;
517				narg--;
518				(void)putchar(',');
519				socktypename((int)*ip);
520				ip++;
521				narg--;
522				if (sockdomain == PF_INET ||
523				    sockdomain == PF_INET6) {
524					(void)putchar(',');
525					sockipprotoname((int)*ip);
526					ip++;
527					narg--;
528				}
529				c = ',';
530			} else if (ktr->ktr_code == SYS_setsockopt ||
531				   ktr->ktr_code == SYS_getsockopt) {
532				print_number(ip,narg,c);
533				(void)putchar(',');
534				sockoptlevelname((int)*ip, decimal);
535				ip++;
536				narg--;
537				(void)putchar(',');
538				sockoptname((int)*ip);
539				ip++;
540				narg--;
541#ifdef SYS_freebsd6_lseek
542			} else if (ktr->ktr_code == SYS_freebsd6_lseek) {
543				print_number(ip,narg,c);
544				/* Hidden 'pad' argument, not in lseek(2) */
545				print_number(ip,narg,c);
546				print_number(ip,narg,c);
547				(void)putchar(',');
548				whencename ((int)*ip);
549				ip++;
550				narg--;
551#endif
552			} else if (ktr->ktr_code == SYS_lseek) {
553				print_number(ip,narg,c);
554				/* Hidden 'pad' argument, not in lseek(2) */
555				print_number(ip,narg,c);
556				(void)putchar(',');
557				whencename ((int)*ip);
558				ip++;
559				narg--;
560
561			} else if (ktr->ktr_code == SYS_flock) {
562				print_number(ip,narg,c);
563				(void)putchar(',');
564				flockname((int)*ip);
565				ip++;
566				narg--;
567			} else if (ktr->ktr_code == SYS_mkfifo ||
568				   ktr->ktr_code == SYS_mkdir) {
569				print_number(ip,narg,c);
570				(void)putchar(',');
571				modename((int)*ip);
572				ip++;
573				narg--;
574			} else if (ktr->ktr_code == SYS_shutdown) {
575				print_number(ip,narg,c);
576				(void)putchar(',');
577				shutdownhowname((int)*ip);
578				ip++;
579				narg--;
580			} else if (ktr->ktr_code == SYS_socketpair) {
581				(void)putchar('(');
582				sockdomainname((int)*ip);
583				ip++;
584				narg--;
585				(void)putchar(',');
586				socktypename((int)*ip);
587				ip++;
588				narg--;
589				c = ',';
590			} else if (ktr->ktr_code == SYS_getrlimit ||
591				   ktr->ktr_code == SYS_setrlimit) {
592				(void)putchar('(');
593				rlimitname((int)*ip);
594				ip++;
595				narg--;
596				c = ',';
597			} else if (ktr->ktr_code == SYS_quotactl) {
598				print_number(ip,narg,c);
599				quotactlname((int)*ip);
600				ip++;
601				narg--;
602				c = ',';
603			} else if (ktr->ktr_code == SYS_nfssvc) {
604				(void)putchar('(');
605				nfssvcname((int)*ip);
606				ip++;
607				narg--;
608				c = ',';
609			} else if (ktr->ktr_code == SYS_rtprio) {
610				(void)putchar('(');
611				rtprioname((int)*ip);
612				ip++;
613				narg--;
614				c = ',';
615			} else if (ktr->ktr_code == SYS___semctl) {
616				print_number(ip,narg,c);
617				print_number(ip,narg,c);
618				semctlname((int)*ip);
619				ip++;
620				narg--;
621			} else if (ktr->ktr_code == SYS_semget) {
622				print_number(ip,narg,c);
623				print_number(ip,narg,c);
624				semgetname((int)*ip);
625				ip++;
626				narg--;
627			} else if (ktr->ktr_code == SYS_msgctl) {
628				print_number(ip,narg,c);
629				shmctlname((int)*ip);
630				ip++;
631				narg--;
632			} else if (ktr->ktr_code == SYS_shmat) {
633				print_number(ip,narg,c);
634				print_number(ip,narg,c);
635				shmatname((int)*ip);
636				ip++;
637				narg--;
638			} else if (ktr->ktr_code == SYS_shmctl) {
639				print_number(ip,narg,c);
640				shmctlname((int)*ip);
641				ip++;
642				narg--;
643			} else if (ktr->ktr_code == SYS_minherit) {
644				print_number(ip,narg,c);
645				print_number(ip,narg,c);
646				minheritname((int)*ip);
647				ip++;
648				narg--;
649			} else if (ktr->ktr_code == SYS_rfork) {
650				(void)putchar('(');
651				rforkname((int)*ip);
652				ip++;
653				narg--;
654				c = ',';
655			} else if (ktr->ktr_code == SYS_lio_listio) {
656				(void)putchar('(');
657				lio_listioname((int)*ip);
658				ip++;
659				narg--;
660				c = ',';
661			} else if (ktr->ktr_code == SYS_mlockall) {
662				(void)putchar('(');
663				mlockallname((int)*ip);
664				ip++;
665				narg--;
666			} else if (ktr->ktr_code == SYS_sched_setscheduler) {
667				print_number(ip,narg,c);
668				schedpolicyname((int)*ip);
669				ip++;
670				narg--;
671			} else if (ktr->ktr_code == SYS_sched_get_priority_max ||
672				   ktr->ktr_code == SYS_sched_get_priority_min) {
673				(void)putchar('(');
674				schedpolicyname((int)*ip);
675				ip++;
676				narg--;
677			} else if (ktr->ktr_code == SYS_sendfile) {
678				print_number(ip,narg,c);
679				print_number(ip,narg,c);
680				print_number(ip,narg,c);
681				print_number(ip,narg,c);
682				print_number(ip,narg,c);
683				print_number(ip,narg,c);
684				sendfileflagsname((int)*ip);
685				ip++;
686				narg--;
687			} else if (ktr->ktr_code == SYS_kldsym) {
688				print_number(ip,narg,c);
689				kldsymcmdname((int)*ip);
690				ip++;
691				narg--;
692			} else if (ktr->ktr_code == SYS_sigprocmask) {
693				(void)putchar('(');
694				sigprocmaskhowname((int)*ip);
695				ip++;
696				narg--;
697				c = ',';
698			} else if (ktr->ktr_code == SYS___acl_get_file ||
699				   ktr->ktr_code == SYS___acl_set_file ||
700				   ktr->ktr_code == SYS___acl_get_fd ||
701				   ktr->ktr_code == SYS___acl_set_fd ||
702				   ktr->ktr_code == SYS___acl_delete_file ||
703				   ktr->ktr_code == SYS___acl_delete_fd ||
704				   ktr->ktr_code == SYS___acl_aclcheck_file ||
705				   ktr->ktr_code == SYS___acl_aclcheck_fd ||
706				   ktr->ktr_code == SYS___acl_get_link ||
707				   ktr->ktr_code == SYS___acl_set_link ||
708				   ktr->ktr_code == SYS___acl_delete_link ||
709				   ktr->ktr_code == SYS___acl_aclcheck_link) {
710				print_number(ip,narg,c);
711				acltypename((int)*ip);
712				ip++;
713				narg--;
714			} else if (ktr->ktr_code == SYS_sigaction) {
715				(void)putchar('(');
716				signame((int)*ip);
717				ip++;
718				narg--;
719				c = ',';
720			} else if (ktr->ktr_code == SYS_extattrctl) {
721				print_number(ip,narg,c);
722				extattrctlname((int)*ip);
723				ip++;
724				narg--;
725			} else if (ktr->ktr_code == SYS_nmount) {
726				print_number(ip,narg,c);
727				print_number(ip,narg,c);
728				(void)putchar(',');
729				mountflagsname ((int)*ip);
730				ip++;
731				narg--;
732			} else if (ktr->ktr_code == SYS_kse_thr_interrupt) {
733				print_number(ip,narg,c);
734				(void)putchar(',');
735				ksethrcmdname ((int)*ip);
736				ip++;
737				narg--;
738			} else if (ktr->ktr_code == SYS_thr_create) {
739				print_number(ip,narg,c);
740				print_number(ip,narg,c);
741				(void)putchar(',');
742				thrcreateflagsname ((int)*ip);
743				ip++;
744				narg--;
745			} else if (ktr->ktr_code == SYS_thr_kill) {
746				print_number(ip,narg,c);
747				(void)putchar(',');
748				signame ((int)*ip);
749				ip++;
750				narg--;
751			} else if (ktr->ktr_code == SYS_kldunloadf) {
752				print_number(ip,narg,c);
753				(void)putchar(',');
754				kldunloadfflagsname ((int)*ip);
755				ip++;
756				narg--;
757			}
758		}
759		while (narg) {
760			print_number(ip,narg,c);
761		}
762		(void)putchar(')');
763	}
764	(void)putchar('\n');
765}
766
767void
768ktrsysret(struct ktr_sysret *ktr)
769{
770	register_t ret = ktr->ktr_retval;
771	int error = ktr->ktr_error;
772	int code = ktr->ktr_code;
773
774	if (code >= nsyscalls || code < 0)
775		(void)printf("[%d] ", code);
776	else
777		(void)printf("%s ", syscallnames[code]);
778
779	if (error == 0) {
780		if (fancy) {
781			(void)printf("%d", ret);
782			if (ret < 0 || ret > 9)
783				(void)printf("/%#lx", (long)ret);
784		} else {
785			if (decimal)
786				(void)printf("%ld", (long)ret);
787			else
788				(void)printf("%#lx", (long)ret);
789		}
790	} else if (error == ERESTART)
791		(void)printf("RESTART");
792	else if (error == EJUSTRETURN)
793		(void)printf("JUSTRETURN");
794	else {
795		(void)printf("-1 errno %d", ktr->ktr_error);
796		if (fancy)
797			(void)printf(" %s", strerror(ktr->ktr_error));
798	}
799	(void)putchar('\n');
800}
801
802void
803ktrnamei(char *cp, int len)
804{
805	(void)printf("\"%.*s\"\n", len, cp);
806}
807
808void
809hexdump(char *p, int len, int screenwidth)
810{
811	int n, i;
812	int width;
813
814	width = 0;
815	do {
816		width += 2;
817		i = 13;			/* base offset */
818		i += (width / 2) + 1;	/* spaces every second byte */
819		i += (width * 2);	/* width of bytes */
820		i += 3;			/* "  |" */
821		i += width;		/* each byte */
822		i += 1;			/* "|" */
823	} while (i < screenwidth);
824	width -= 2;
825
826	for (n = 0; n < len; n += width) {
827		for (i = n; i < n + width; i++) {
828			if ((i % width) == 0) {	/* beginning of line */
829				printf("       0x%04x", i);
830			}
831			if ((i % 2) == 0) {
832				printf(" ");
833			}
834			if (i < len)
835				printf("%02x", p[i] & 0xff);
836			else
837				printf("  ");
838		}
839		printf("  |");
840		for (i = n; i < n + width; i++) {
841			if (i >= len)
842				break;
843			if (p[i] >= ' ' && p[i] <= '~')
844				printf("%c", p[i]);
845			else
846				printf(".");
847		}
848		printf("|\n");
849	}
850	if ((i % width) != 0)
851		printf("\n");
852}
853
854void
855visdump(char *dp, int datalen, int screenwidth)
856{
857	int col = 0;
858	char *cp;
859	int width;
860	char visbuf[5];
861
862	(void)printf("       \"");
863	col = 8;
864	for (;datalen > 0; datalen--, dp++) {
865		(void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
866		cp = visbuf;
867		/*
868		 * Keep track of printables and
869		 * space chars (like fold(1)).
870		 */
871		if (col == 0) {
872			(void)putchar('\t');
873			col = 8;
874		}
875		switch(*cp) {
876		case '\n':
877			col = 0;
878			(void)putchar('\n');
879			continue;
880		case '\t':
881			width = 8 - (col&07);
882			break;
883		default:
884			width = strlen(cp);
885		}
886		if (col + width > (screenwidth-2)) {
887			(void)printf("\\\n\t");
888			col = 8;
889		}
890		col += width;
891		do {
892			(void)putchar(*cp++);
893		} while (*cp);
894	}
895	if (col == 0)
896		(void)printf("       ");
897	(void)printf("\"\n");
898}
899
900void
901ktrgenio(struct ktr_genio *ktr, int len)
902{
903	int datalen = len - sizeof (struct ktr_genio);
904	char *dp = (char *)ktr + sizeof (struct ktr_genio);
905	static int screenwidth = 0;
906	int i, binary;
907
908	if (screenwidth == 0) {
909		struct winsize ws;
910
911		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
912		    ws.ws_col > 8)
913			screenwidth = ws.ws_col;
914		else
915			screenwidth = 80;
916	}
917	printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
918		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
919		datalen == 1 ? "" : "s");
920	if (suppressdata)
921		return;
922	if (maxdata && datalen > maxdata)
923		datalen = maxdata;
924
925	for (i = 0, binary = 0; i < datalen && binary == 0; i++)  {
926		if (dp[i] >= 32 && dp[i] < 127)
927			continue;
928		if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
929			continue;
930		binary = 1;
931	}
932	if (binary)
933		hexdump(dp, datalen, screenwidth);
934	else
935		visdump(dp, datalen, screenwidth);
936}
937
938const char *signames[] = {
939	"NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT",	/*  1 - 6  */
940	"EMT", "FPE", "KILL", "BUS", "SEGV", "SYS",		/*  7 - 12 */
941	"PIPE", "ALRM",  "TERM", "URG", "STOP", "TSTP",		/* 13 - 18 */
942	"CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU",		/* 19 - 24 */
943	"XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1",	/* 25 - 30 */
944	"USR2", NULL,						/* 31 - 32 */
945};
946
947void
948ktrpsig(struct ktr_psig *psig)
949{
950	if (psig->signo > 0 && psig->signo < NSIG)
951		(void)printf("SIG%s ", signames[psig->signo]);
952	else
953		(void)printf("SIG %d ", psig->signo);
954	if (psig->action == SIG_DFL)
955		(void)printf("SIG_DFL\n");
956	else {
957		(void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
958		    (u_long)psig->action, psig->mask.__bits[0], psig->code);
959	}
960}
961
962void
963ktrcsw(struct ktr_csw *cs)
964{
965	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
966		cs->user ? "user" : "kernel");
967}
968
969#define	UTRACE_DLOPEN_START		1
970#define	UTRACE_DLOPEN_STOP		2
971#define	UTRACE_DLCLOSE_START		3
972#define	UTRACE_DLCLOSE_STOP		4
973#define	UTRACE_LOAD_OBJECT		5
974#define	UTRACE_UNLOAD_OBJECT		6
975#define	UTRACE_ADD_RUNDEP		7
976#define	UTRACE_PRELOAD_FINISHED		8
977#define	UTRACE_INIT_CALL		9
978#define	UTRACE_FINI_CALL		10
979
980struct utrace_rtld {
981	char sig[4];				/* 'RTLD' */
982	int event;
983	void *handle;
984	void *mapbase;
985	size_t mapsize;
986	int refcnt;
987	char name[MAXPATHLEN];
988};
989
990void
991ktruser_rtld(int len, unsigned char *p)
992{
993	struct utrace_rtld *ut = (struct utrace_rtld *)p;
994	void *parent;
995	int mode;
996
997	switch (ut->event) {
998	case UTRACE_DLOPEN_START:
999		mode = ut->refcnt;
1000		printf("dlopen(%s, ", ut->name);
1001		switch (mode & RTLD_MODEMASK) {
1002		case RTLD_NOW:
1003			printf("RTLD_NOW");
1004			break;
1005		case RTLD_LAZY:
1006			printf("RTLD_LAZY");
1007			break;
1008		default:
1009			printf("%#x", mode & RTLD_MODEMASK);
1010		}
1011		if (mode & RTLD_GLOBAL)
1012			printf(" | RTLD_GLOBAL");
1013		if (mode & RTLD_TRACE)
1014			printf(" | RTLD_TRACE");
1015		if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1016			printf(" | %#x", mode &
1017			    ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1018		printf(")\n");
1019		break;
1020	case UTRACE_DLOPEN_STOP:
1021		printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1022		    ut->refcnt);
1023		break;
1024	case UTRACE_DLCLOSE_START:
1025		printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1026		    ut->refcnt);
1027		break;
1028	case UTRACE_DLCLOSE_STOP:
1029		printf("dlclose(%p) finished\n", ut->handle);
1030		break;
1031	case UTRACE_LOAD_OBJECT:
1032		printf("RTLD: loaded   %p @ %p - %p (%s)\n", ut->handle,
1033		    ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1034		    ut->name);
1035		break;
1036	case UTRACE_UNLOAD_OBJECT:
1037		printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1038		    ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1039		    ut->name);
1040		break;
1041	case UTRACE_ADD_RUNDEP:
1042		parent = ut->mapbase;
1043		printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1044		    ut->handle, ut->name, ut->refcnt);
1045		break;
1046	case UTRACE_PRELOAD_FINISHED:
1047		printf("RTLD: LD_PRELOAD finished\n");
1048		break;
1049	case UTRACE_INIT_CALL:
1050		printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1051		    ut->name);
1052		break;
1053	case UTRACE_FINI_CALL:
1054		printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1055		    ut->name);
1056		break;
1057	default:
1058		p += 4;
1059		len -= 4;
1060		printf("RTLD: %d ", len);
1061		while (len--)
1062			if (decimal)
1063				printf(" %d", *p++);
1064			else
1065				printf(" %02x", *p++);
1066		printf("\n");
1067	}
1068}
1069
1070struct utrace_malloc {
1071	void *p;
1072	size_t s;
1073	void *r;
1074};
1075
1076void
1077ktruser_malloc(int len, unsigned char *p)
1078{
1079	struct utrace_malloc *ut = (struct utrace_malloc *)p;
1080
1081	if (ut->p == NULL) {
1082		if (ut->s == 0 && ut->r == NULL)
1083			printf("malloc_init()\n");
1084		else
1085			printf("%p = malloc(%zu)\n", ut->r, ut->s);
1086	} else {
1087		if (ut->s == 0)
1088			printf("free(%p)\n", ut->p);
1089		else
1090			printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1091	}
1092}
1093
1094void
1095ktruser(int len, unsigned char *p)
1096{
1097
1098	if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1099		ktruser_rtld(len, p);
1100		return;
1101	}
1102
1103	if (len == sizeof(struct utrace_malloc)) {
1104		ktruser_malloc(len, p);
1105		return;
1106	}
1107
1108	(void)printf("%d ", len);
1109	while (len--)
1110		if (decimal)
1111			(void)printf(" %d", *p++);
1112		else
1113			(void)printf(" %02x", *p++);
1114	(void)printf("\n");
1115}
1116
1117void
1118usage(void)
1119{
1120	(void)fprintf(stderr,
1121  "usage: kdump [-dEnlHRsT] [-f trfile] [-m maxdata] [-p pid] [-t [cnisuw]]\n");
1122	exit(1);
1123}
1124