xref: /freebsd/usr.bin/truss/syscalls.c (revision f44fc79d0b1f77f3faba65f2e7a92fd5a3680b72)
1bbeaf6c0SSean Eric Fagan /*
20a6c71f8SWarner Losh  * Copyright 1997 Sean Eric Fagan
309d64da3SSean Eric Fagan  *
409d64da3SSean Eric Fagan  * Redistribution and use in source and binary forms, with or without
509d64da3SSean Eric Fagan  * modification, are permitted provided that the following conditions
609d64da3SSean Eric Fagan  * are met:
709d64da3SSean Eric Fagan  * 1. Redistributions of source code must retain the above copyright
809d64da3SSean Eric Fagan  *    notice, this list of conditions and the following disclaimer.
909d64da3SSean Eric Fagan  * 2. Redistributions in binary form must reproduce the above copyright
1009d64da3SSean Eric Fagan  *    notice, this list of conditions and the following disclaimer in the
1109d64da3SSean Eric Fagan  *    documentation and/or other materials provided with the distribution.
1209d64da3SSean Eric Fagan  * 3. All advertising materials mentioning features or use of this software
1309d64da3SSean Eric Fagan  *    must display the following acknowledgement:
1409d64da3SSean Eric Fagan  *	This product includes software developed by Sean Eric Fagan
1509d64da3SSean Eric Fagan  * 4. Neither the name of the author may be used to endorse or promote
1609d64da3SSean Eric Fagan  *    products derived from this software without specific prior written
1709d64da3SSean Eric Fagan  *    permission.
1809d64da3SSean Eric Fagan  *
1909d64da3SSean Eric Fagan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2009d64da3SSean Eric Fagan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2109d64da3SSean Eric Fagan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2209d64da3SSean Eric Fagan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2309d64da3SSean Eric Fagan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2409d64da3SSean Eric Fagan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2509d64da3SSean Eric Fagan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2609d64da3SSean Eric Fagan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2709d64da3SSean Eric Fagan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2809d64da3SSean Eric Fagan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2909d64da3SSean Eric Fagan  * SUCH DAMAGE.
3009d64da3SSean Eric Fagan  */
3109d64da3SSean Eric Fagan 
322b75c8adSJohn Baldwin #include <sys/cdefs.h>
332b75c8adSJohn Baldwin __FBSDID("$FreeBSD$");
343cf51049SPhilippe Charnier 
3509d64da3SSean Eric Fagan /*
36bbeaf6c0SSean Eric Fagan  * This file has routines used to print out system calls and their
37bbeaf6c0SSean Eric Fagan  * arguments.
38bbeaf6c0SSean Eric Fagan  */
39bbeaf6c0SSean Eric Fagan 
409ddd1412SDag-Erling Smørgrav #include <sys/types.h>
412b75c8adSJohn Baldwin #include <sys/event.h>
422b75c8adSJohn Baldwin #include <sys/ioccom.h>
4334763d1cSJohn Baldwin #include <sys/mman.h>
44a776866bSBryan Drewery #include <sys/mount.h>
4555648840SJohn Baldwin #include <sys/procctl.h>
465d2d083cSXin LI #include <sys/ptrace.h>
472b75c8adSJohn Baldwin #include <sys/resource.h>
489ddd1412SDag-Erling Smørgrav #include <sys/socket.h>
492b75c8adSJohn Baldwin #include <sys/stat.h>
502b75c8adSJohn Baldwin #include <sys/umtx.h>
519ddd1412SDag-Erling Smørgrav #include <sys/un.h>
5234763d1cSJohn Baldwin #include <sys/wait.h>
532b75c8adSJohn Baldwin #include <machine/sysarch.h>
549ddd1412SDag-Erling Smørgrav #include <netinet/in.h>
559ddd1412SDag-Erling Smørgrav #include <arpa/inet.h>
569ddd1412SDag-Erling Smørgrav 
57dec17687SBrian Feldman #include <ctype.h>
583cf51049SPhilippe Charnier #include <err.h>
59894b8f7aSAlfred Perlstein #include <fcntl.h>
60e45a5a0dSDavid Malone #include <poll.h>
619ddd1412SDag-Erling Smørgrav #include <signal.h>
6266917ca9SJohn Baldwin #include <stddef.h>
63e45a5a0dSDavid Malone #include <stdint.h>
64bbeaf6c0SSean Eric Fagan #include <stdio.h>
65bbeaf6c0SSean Eric Fagan #include <stdlib.h>
66bbeaf6c0SSean Eric Fagan #include <string.h>
6737169f94SMatthew N. Dodd #include <time.h>
68bbeaf6c0SSean Eric Fagan #include <unistd.h>
69081e5c48SPav Lucistnik #include <vis.h>
709ddd1412SDag-Erling Smørgrav 
71ec0bed25SMatthew N. Dodd #include "truss.h"
721be5d704SMark Murray #include "extern.h"
73bbeaf6c0SSean Eric Fagan #include "syscall.h"
74bbeaf6c0SSean Eric Fagan 
752c02627fSMarcel Moolenaar /* 64-bit alignment on 32-bit platforms. */
76df438f42SJohn Baldwin #if !defined(__LP64__) && defined(__powerpc__)
772c02627fSMarcel Moolenaar #define	QUAD_ALIGN	1
782c02627fSMarcel Moolenaar #else
792c02627fSMarcel Moolenaar #define	QUAD_ALIGN	0
802c02627fSMarcel Moolenaar #endif
812c02627fSMarcel Moolenaar 
822c02627fSMarcel Moolenaar /* Number of slots needed for a 64-bit argument. */
832c02627fSMarcel Moolenaar #ifdef __LP64__
842c02627fSMarcel Moolenaar #define	QUAD_SLOTS	1
852c02627fSMarcel Moolenaar #else
862c02627fSMarcel Moolenaar #define	QUAD_SLOTS	2
872c02627fSMarcel Moolenaar #endif
882c02627fSMarcel Moolenaar 
89bbeaf6c0SSean Eric Fagan /*
90081e5c48SPav Lucistnik  * This should probably be in its own file, sorted alphabetically.
91bbeaf6c0SSean Eric Fagan  */
926c61b0f3SBryan Drewery static struct syscall decoded_syscalls[] = {
93*f44fc79dSJohn Baldwin 	/* Native ABI */
94*f44fc79dSJohn Baldwin 	{ .name = "__getcwd", .ret_type = 1, .nargs = 2,
95*f44fc79dSJohn Baldwin 	  .args = { { Name | OUT, 0 }, { Int, 1 } } },
96*f44fc79dSJohn Baldwin 	{ .name = "_umtx_op", .ret_type = 1, .nargs = 5,
97*f44fc79dSJohn Baldwin 	  .args = { { Ptr, 0 }, { Umtxop, 1 }, { LongHex, 2 }, { Ptr, 3 },
98*f44fc79dSJohn Baldwin 		    { Ptr, 4 } } },
99*f44fc79dSJohn Baldwin 	{ .name = "accept", .ret_type = 1, .nargs = 3,
100*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
101*f44fc79dSJohn Baldwin 	{ .name = "access", .ret_type = 1, .nargs = 2,
102*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { Accessmode, 1 } } },
103*f44fc79dSJohn Baldwin 	{ .name = "bind", .ret_type = 1, .nargs = 3,
104*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
105*f44fc79dSJohn Baldwin 	{ .name = "bindat", .ret_type = 1, .nargs = 4,
106*f44fc79dSJohn Baldwin 	  .args = { { Atfd, 0 }, { Int, 1 }, { Sockaddr | IN, 2 },
1077d897327SJohn Baldwin 		    { Int, 3 } } },
108*f44fc79dSJohn Baldwin 	{ .name = "break", .ret_type = 1, .nargs = 1,
109*f44fc79dSJohn Baldwin 	  .args = { { Ptr, 0 } } },
110*f44fc79dSJohn Baldwin 	{ .name = "chdir", .ret_type = 1, .nargs = 1,
111*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 } } },
112*f44fc79dSJohn Baldwin 	{ .name = "chflags", .ret_type = 1, .nargs = 2,
113*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { Hex, 1 } } },
114*f44fc79dSJohn Baldwin 	{ .name = "chmod", .ret_type = 1, .nargs = 2,
115ee3b0f6eSDiomidis Spinellis 	  .args = { { Name, 0 }, { Octal, 1 } } },
116*f44fc79dSJohn Baldwin 	{ .name = "chown", .ret_type = 1, .nargs = 3,
117*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 }, { Int, 1 }, { Int, 2 } } },
118*f44fc79dSJohn Baldwin 	{ .name = "chroot", .ret_type = 1, .nargs = 1,
119*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 } } },
120*f44fc79dSJohn Baldwin 	{ .name = "clock_gettime", .ret_type = 1, .nargs = 2,
121*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Timespec | OUT, 1 } } },
122ee3b0f6eSDiomidis Spinellis 	{ .name = "close", .ret_type = 1, .nargs = 1,
123ee3b0f6eSDiomidis Spinellis 	  .args = { { Int, 0 } } },
124*f44fc79dSJohn Baldwin 	{ .name = "connect", .ret_type = 1, .nargs = 3,
125*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
126*f44fc79dSJohn Baldwin 	{ .name = "connectat", .ret_type = 1, .nargs = 4,
127*f44fc79dSJohn Baldwin 	  .args = { { Atfd, 0 }, { Int, 1 }, { Sockaddr | IN, 2 },
128*f44fc79dSJohn Baldwin 		    { Int, 3 } } },
129*f44fc79dSJohn Baldwin 	{ .name = "eaccess", .ret_type = 1, .nargs = 2,
130*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { Accessmode, 1 } } },
131*f44fc79dSJohn Baldwin 	{ .name = "execve", .ret_type = 1, .nargs = 3,
132*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { ExecArgs | IN, 1 },
133*f44fc79dSJohn Baldwin 		    { ExecEnv | IN, 2 } } },
134*f44fc79dSJohn Baldwin 	{ .name = "exit", .ret_type = 0, .nargs = 1,
135*f44fc79dSJohn Baldwin 	  .args = { { Hex, 0 } } },
136*f44fc79dSJohn Baldwin 	{ .name = "faccessat", .ret_type = 1, .nargs = 4,
137*f44fc79dSJohn Baldwin 	  .args = { { Atfd, 0 }, { Name | IN, 1 }, { Accessmode, 2 },
138*f44fc79dSJohn Baldwin 		    { Atflags, 3 } } },
139*f44fc79dSJohn Baldwin 	{ .name = "fchmod", .ret_type = 1, .nargs = 2,
140*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Octal, 1 } } },
141*f44fc79dSJohn Baldwin 	{ .name = "fchmodat", .ret_type = 1, .nargs = 4,
142*f44fc79dSJohn Baldwin 	  .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 }, { Atflags, 3 } } },
143*f44fc79dSJohn Baldwin 	{ .name = "fchown", .ret_type = 1, .nargs = 3,
144*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Int, 1 }, { Int, 2 } } },
145*f44fc79dSJohn Baldwin 	{ .name = "fchownat", .ret_type = 1, .nargs = 5,
146*f44fc79dSJohn Baldwin 	  .args = { { Atfd, 0 }, { Name, 1 }, { Int, 2 }, { Int, 3 },
147*f44fc79dSJohn Baldwin 		    { Atflags, 4 } } },
148*f44fc79dSJohn Baldwin 	{ .name = "fcntl", .ret_type = 1, .nargs = 3,
149*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Fcntl, 1 }, { Fcntlflag, 2 } } },
150*f44fc79dSJohn Baldwin 	{ .name = "fstat", .ret_type = 1, .nargs = 2,
151*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Stat | OUT, 1 } } },
152*f44fc79dSJohn Baldwin 	{ .name = "fstatat", .ret_type = 1, .nargs = 4,
153*f44fc79dSJohn Baldwin 	  .args = { { Atfd, 0 }, { Name | IN, 1 }, { Stat | OUT, 2 },
154*f44fc79dSJohn Baldwin 		    { Atflags, 3 } } },
155*f44fc79dSJohn Baldwin 	{ .name = "fstatfs", .ret_type = 1, .nargs = 2,
156*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { StatFs | OUT, 1 } } },
157*f44fc79dSJohn Baldwin 	{ .name = "ftruncate", .ret_type = 1, .nargs = 2,
158*f44fc79dSJohn Baldwin 	  .args = { { Int | IN, 0 }, { QuadHex | IN, 1 + QUAD_ALIGN } } },
159*f44fc79dSJohn Baldwin 	{ .name = "futimens", .ret_type = 1, .nargs = 2,
160*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Timespec2 | IN, 1 } } },
161*f44fc79dSJohn Baldwin 	{ .name = "futimes", .ret_type = 1, .nargs = 2,
162*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Timeval2 | IN, 1 } } },
163*f44fc79dSJohn Baldwin 	{ .name = "futimesat", .ret_type = 1, .nargs = 3,
164*f44fc79dSJohn Baldwin 	  .args = { { Atfd, 0 }, { Name | IN, 1 }, { Timeval2 | IN, 2 } } },
165*f44fc79dSJohn Baldwin 	{ .name = "getitimer", .ret_type = 1, .nargs = 2,
166*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Itimerval | OUT, 2 } } },
167*f44fc79dSJohn Baldwin 	{ .name = "getpeername", .ret_type = 1, .nargs = 3,
168*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
169*f44fc79dSJohn Baldwin 	{ .name = "getpgid", .ret_type = 1, .nargs = 1,
170*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 } } },
171*f44fc79dSJohn Baldwin 	{ .name = "getrlimit", .ret_type = 1, .nargs = 2,
172*f44fc79dSJohn Baldwin 	  .args = { { Resource, 0 }, { Rlimit | OUT, 1 } } },
173*f44fc79dSJohn Baldwin 	{ .name = "getrusage", .ret_type = 1, .nargs = 2,
174*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Rusage | OUT, 1 } } },
175*f44fc79dSJohn Baldwin 	{ .name = "getsid", .ret_type = 1, .nargs = 1,
176*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 } } },
177*f44fc79dSJohn Baldwin 	{ .name = "getsockname", .ret_type = 1, .nargs = 3,
178*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
179*f44fc79dSJohn Baldwin 	{ .name = "gettimeofday", .ret_type = 1, .nargs = 2,
180*f44fc79dSJohn Baldwin 	  .args = { { Timeval | OUT, 0 }, { Ptr, 1 } } },
181*f44fc79dSJohn Baldwin 	{ .name = "ioctl", .ret_type = 1, .nargs = 3,
182*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Ioctl, 1 }, { Hex, 2 } } },
183*f44fc79dSJohn Baldwin 	{ .name = "kevent", .ret_type = 1, .nargs = 6,
184*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Kevent, 1 }, { Int, 2 }, { Kevent | OUT, 3 },
185*f44fc79dSJohn Baldwin 		    { Int, 4 }, { Timespec, 5 } } },
186*f44fc79dSJohn Baldwin 	{ .name = "kill", .ret_type = 1, .nargs = 2,
187*f44fc79dSJohn Baldwin 	  .args = { { Int | IN, 0 }, { Signal | IN, 1 } } },
188*f44fc79dSJohn Baldwin 	{ .name = "kldfind", .ret_type = 1, .nargs = 1,
189*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 } } },
190*f44fc79dSJohn Baldwin 	{ .name = "kldfirstmod", .ret_type = 1, .nargs = 1,
191*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 } } },
192*f44fc79dSJohn Baldwin 	{ .name = "kldload", .ret_type = 1, .nargs = 1,
193*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 } } },
194*f44fc79dSJohn Baldwin 	{ .name = "kldnext", .ret_type = 1, .nargs = 1,
195*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 } } },
196*f44fc79dSJohn Baldwin 	{ .name = "kldstat", .ret_type = 1, .nargs = 2,
197*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Ptr, 1 } } },
198*f44fc79dSJohn Baldwin 	{ .name = "kldunload", .ret_type = 1, .nargs = 1,
199*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 } } },
200*f44fc79dSJohn Baldwin 	{ .name = "kse_release", .ret_type = 0, .nargs = 1,
201*f44fc79dSJohn Baldwin 	  .args = { { Timespec, 0 } } },
202*f44fc79dSJohn Baldwin 	{ .name = "lchflags", .ret_type = 1, .nargs = 2,
203*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { Hex, 1 } } },
204*f44fc79dSJohn Baldwin 	{ .name = "lchmod", .ret_type = 1, .nargs = 2,
205*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 }, { Octal, 1 } } },
206*f44fc79dSJohn Baldwin 	{ .name = "lchown", .ret_type = 1, .nargs = 3,
207*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 }, { Int, 1 }, { Int, 2 } } },
2082b75c8adSJohn Baldwin 	{ .name = "link", .ret_type = 1, .nargs = 2,
209ee3b0f6eSDiomidis Spinellis 	  .args = { { Name, 0 }, { Name, 1 } } },
2102b75c8adSJohn Baldwin 	{ .name = "linkat", .ret_type = 1, .nargs = 5,
2117d897327SJohn Baldwin 	  .args = { { Atfd, 0 }, { Name, 1 }, { Atfd, 2 }, { Name, 3 },
2127d897327SJohn Baldwin 		    { Atflags, 4 } } },
213*f44fc79dSJohn Baldwin 	{ .name = "lseek", .ret_type = 2, .nargs = 3,
214*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { QuadHex, 1 + QUAD_ALIGN },
215*f44fc79dSJohn Baldwin 		    { Whence, 1 + QUAD_SLOTS + QUAD_ALIGN } } },
216*f44fc79dSJohn Baldwin 	{ .name = "lstat", .ret_type = 1, .nargs = 2,
217*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { Stat | OUT, 1 } } },
218*f44fc79dSJohn Baldwin 	{ .name = "lutimes", .ret_type = 1, .nargs = 2,
219*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { Timeval2 | IN, 1 } } },
220*f44fc79dSJohn Baldwin 	{ .name = "mkdir", .ret_type = 1, .nargs = 2,
221*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 }, { Octal, 1 } } },
222*f44fc79dSJohn Baldwin 	{ .name = "mkdirat", .ret_type = 1, .nargs = 3,
223*f44fc79dSJohn Baldwin 	  .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 } } },
2242b75c8adSJohn Baldwin 	{ .name = "mkfifo", .ret_type = 1, .nargs = 2,
225e82ce59cSJohn Baldwin 	  .args = { { Name, 0 }, { Octal, 1 } } },
2262b75c8adSJohn Baldwin 	{ .name = "mkfifoat", .ret_type = 1, .nargs = 3,
2277d897327SJohn Baldwin 	  .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 } } },
2282b75c8adSJohn Baldwin 	{ .name = "mknod", .ret_type = 1, .nargs = 3,
229e82ce59cSJohn Baldwin 	  .args = { { Name, 0 }, { Octal, 1 }, { Int, 2 } } },
2302b75c8adSJohn Baldwin 	{ .name = "mknodat", .ret_type = 1, .nargs = 4,
2317d897327SJohn Baldwin 	  .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 }, { Int, 3 } } },
232*f44fc79dSJohn Baldwin 	{ .name = "mmap", .ret_type = 1, .nargs = 6,
233*f44fc79dSJohn Baldwin 	  .args = { { Ptr, 0 }, { Int, 1 }, { Mprot, 2 }, { Mmapflags, 3 },
234*f44fc79dSJohn Baldwin 		    { Int, 4 }, { QuadHex, 5 + QUAD_ALIGN } } },
235*f44fc79dSJohn Baldwin 	{ .name = "modfind", .ret_type = 1, .nargs = 1,
236*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 } } },
2372b75c8adSJohn Baldwin 	{ .name = "mount", .ret_type = 1, .nargs = 4,
238ee3b0f6eSDiomidis Spinellis 	  .args = { { Name, 0 }, { Name, 1 }, { Int, 2 }, { Ptr, 3 } } },
239*f44fc79dSJohn Baldwin 	{ .name = "mprotect", .ret_type = 1, .nargs = 3,
240*f44fc79dSJohn Baldwin 	  .args = { { Ptr, 0 }, { Int, 1 }, { Mprot, 2 } } },
241*f44fc79dSJohn Baldwin 	{ .name = "munmap", .ret_type = 1, .nargs = 2,
242*f44fc79dSJohn Baldwin 	  .args = { { Ptr, 0 }, { Int, 1 } } },
243*f44fc79dSJohn Baldwin 	{ .name = "nanosleep", .ret_type = 1, .nargs = 1,
244*f44fc79dSJohn Baldwin 	  .args = { { Timespec, 0 } } },
245*f44fc79dSJohn Baldwin 	{ .name = "open", .ret_type = 1, .nargs = 3,
246*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { Open, 1 }, { Octal, 2 } } },
247*f44fc79dSJohn Baldwin 	{ .name = "openat", .ret_type = 1, .nargs = 4,
248*f44fc79dSJohn Baldwin 	  .args = { { Atfd, 0 }, { Name | IN, 1 }, { Open, 2 },
249*f44fc79dSJohn Baldwin 		    { Octal, 3 } } },
250*f44fc79dSJohn Baldwin 	{ .name = "pathconf", .ret_type = 1, .nargs = 2,
251*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { Pathconf, 1 } } },
252*f44fc79dSJohn Baldwin 	{ .name = "pipe", .ret_type = 1, .nargs = 1,
253*f44fc79dSJohn Baldwin 	  .args = { { PipeFds | OUT, 0 } } },
254*f44fc79dSJohn Baldwin 	{ .name = "pipe2", .ret_type = 1, .nargs = 2,
255*f44fc79dSJohn Baldwin 	  .args = { { Ptr, 0 }, { Open, 1 } } },
256*f44fc79dSJohn Baldwin 	{ .name = "poll", .ret_type = 1, .nargs = 3,
257*f44fc79dSJohn Baldwin 	  .args = { { Pollfd, 0 }, { Int, 1 }, { Int, 2 } } },
258*f44fc79dSJohn Baldwin 	{ .name = "posix_openpt", .ret_type = 1, .nargs = 1,
259*f44fc79dSJohn Baldwin 	  .args = { { Open, 0 } } },
260*f44fc79dSJohn Baldwin 	{ .name = "procctl", .ret_type = 1, .nargs = 4,
261*f44fc79dSJohn Baldwin 	  .args = { { Idtype, 0 }, { Quad, 1 + QUAD_ALIGN },
262*f44fc79dSJohn Baldwin 		    { Procctl, 1 + QUAD_ALIGN + QUAD_SLOTS },
263*f44fc79dSJohn Baldwin 		    { Ptr, 2 + QUAD_ALIGN + QUAD_SLOTS } } },
264*f44fc79dSJohn Baldwin 	{ .name = "read", .ret_type = 1, .nargs = 3,
265*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { BinString | OUT, 1 }, { Int, 2 } } },
266*f44fc79dSJohn Baldwin 	{ .name = "readlink", .ret_type = 1, .nargs = 3,
267*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 }, { Readlinkres | OUT, 1 }, { Int, 2 } } },
268*f44fc79dSJohn Baldwin 	{ .name = "readlinkat", .ret_type = 1, .nargs = 4,
269*f44fc79dSJohn Baldwin 	  .args = { { Atfd, 0 }, { Name, 1 }, { Readlinkres | OUT, 2 },
2707d897327SJohn Baldwin 		    { Int, 3 } } },
271ee3b0f6eSDiomidis Spinellis 	{ .name = "recvfrom", .ret_type = 1, .nargs = 6,
2720a46af44SJohn Baldwin 	  .args = { { Int, 0 }, { BinString | OUT, 1 }, { Int, 2 }, { Hex, 3 },
2730a46af44SJohn Baldwin 		    { Sockaddr | OUT, 4 }, { Ptr | OUT, 5 } } },
274*f44fc79dSJohn Baldwin 	{ .name = "rename", .ret_type = 1, .nargs = 2,
275*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 }, { Name, 1 } } },
276*f44fc79dSJohn Baldwin 	{ .name = "renameat", .ret_type = 1, .nargs = 4,
277*f44fc79dSJohn Baldwin 	  .args = { { Atfd, 0 }, { Name, 1 }, { Atfd, 2 }, { Name, 3 } } },
278*f44fc79dSJohn Baldwin 	{ .name = "rfork", .ret_type = 1, .nargs = 1,
279*f44fc79dSJohn Baldwin 	  .args = { { Rforkflags, 0 } } },
280ee3b0f6eSDiomidis Spinellis 	{ .name = "select", .ret_type = 1, .nargs = 5,
2810a46af44SJohn Baldwin 	  .args = { { Int, 0 }, { Fd_set, 1 }, { Fd_set, 2 }, { Fd_set, 3 },
2820a46af44SJohn Baldwin 		    { Timeval, 4 } } },
283*f44fc79dSJohn Baldwin 	{ .name = "sendto", .ret_type = 1, .nargs = 6,
284*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { BinString | IN, 1 }, { Int, 2 }, { Hex, 3 },
285*f44fc79dSJohn Baldwin 		    { Sockaddr | IN, 4 }, { Ptr | IN, 5 } } },
286ee3b0f6eSDiomidis Spinellis 	{ .name = "setitimer", .ret_type = 1, .nargs = 3,
287ee3b0f6eSDiomidis Spinellis 	  .args = { { Int, 0 }, { Itimerval, 1 }, { Itimerval | OUT, 2 } } },
288*f44fc79dSJohn Baldwin 	{ .name = "setrlimit", .ret_type = 1, .nargs = 2,
289*f44fc79dSJohn Baldwin 	  .args = { { Resource, 0 }, { Rlimit | IN, 1 } } },
290*f44fc79dSJohn Baldwin 	{ .name = "shutdown", .ret_type = 1, .nargs = 2,
291*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Shutdown, 1 } } },
292*f44fc79dSJohn Baldwin 	{ .name = "sigaction", .ret_type = 1, .nargs = 3,
293*f44fc79dSJohn Baldwin 	  .args = { { Signal, 0 }, { Sigaction | IN, 1 },
294*f44fc79dSJohn Baldwin 		    { Sigaction | OUT, 2 } } },
2952b75c8adSJohn Baldwin 	{ .name = "sigpending", .ret_type = 1, .nargs = 1,
296b289a8d7SJohn Baldwin 	  .args = { { Sigset | OUT, 0 } } },
2972b75c8adSJohn Baldwin 	{ .name = "sigprocmask", .ret_type = 1, .nargs = 3,
298ee3b0f6eSDiomidis Spinellis 	  .args = { { Sigprocmask, 0 }, { Sigset, 1 }, { Sigset | OUT, 2 } } },
2992b75c8adSJohn Baldwin 	{ .name = "sigqueue", .ret_type = 1, .nargs = 3,
300b289a8d7SJohn Baldwin 	  .args = { { Int, 0 }, { Signal, 1 }, { LongHex, 2 } } },
3012b75c8adSJohn Baldwin 	{ .name = "sigreturn", .ret_type = 1, .nargs = 1,
302b289a8d7SJohn Baldwin 	  .args = { { Ptr, 0 } } },
3032b75c8adSJohn Baldwin 	{ .name = "sigsuspend", .ret_type = 1, .nargs = 1,
304b289a8d7SJohn Baldwin 	  .args = { { Sigset | IN, 0 } } },
305b289a8d7SJohn Baldwin 	{ .name = "sigtimedwait", .ret_type = 1, .nargs = 3,
306b289a8d7SJohn Baldwin 	  .args = { { Sigset | IN, 0 }, { Ptr, 1 }, { Timespec | IN, 2 } } },
307b289a8d7SJohn Baldwin 	{ .name = "sigwait", .ret_type = 1, .nargs = 2,
308b289a8d7SJohn Baldwin 	  .args = { { Sigset | IN, 0 }, { Ptr, 1 } } },
309b289a8d7SJohn Baldwin 	{ .name = "sigwaitinfo", .ret_type = 1, .nargs = 2,
310b289a8d7SJohn Baldwin 	  .args = { { Sigset | IN, 0 }, { Ptr, 1 } } },
311ee3b0f6eSDiomidis Spinellis 	{ .name = "socket", .ret_type = 1, .nargs = 3,
312ee3b0f6eSDiomidis Spinellis 	  .args = { { Sockdomain, 0 }, { Socktype, 1 }, { Int, 2 } } },
313*f44fc79dSJohn Baldwin 	{ .name = "stat", .ret_type = 1, .nargs = 2,
314*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { Stat | OUT, 1 } } },
315*f44fc79dSJohn Baldwin 	{ .name = "statfs", .ret_type = 1, .nargs = 2,
316*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { StatFs | OUT, 1 } } },
317ee3b0f6eSDiomidis Spinellis 	{ .name = "symlink", .ret_type = 1, .nargs = 2,
318ee3b0f6eSDiomidis Spinellis 	  .args = { { Name, 0 }, { Name, 1 } } },
3197d897327SJohn Baldwin 	{ .name = "symlinkat", .ret_type = 1, .nargs = 3,
3207d897327SJohn Baldwin 	  .args = { { Name, 0 }, { Atfd, 1 }, { Name, 2 } } },
321*f44fc79dSJohn Baldwin 	{ .name = "sysarch", .ret_type = 1, .nargs = 2,
322*f44fc79dSJohn Baldwin 	  .args = { { Sysarch, 0 }, { Ptr, 1 } } },
323*f44fc79dSJohn Baldwin 	{ .name = "thr_kill", .ret_type = 1, .nargs = 2,
324*f44fc79dSJohn Baldwin 	  .args = { { Long, 0 }, { Signal, 1 } } },
325*f44fc79dSJohn Baldwin 	{ .name = "thr_self", .ret_type = 1, .nargs = 1,
326*f44fc79dSJohn Baldwin 	  .args = { { Ptr, 0 } } },
327*f44fc79dSJohn Baldwin 	{ .name = "truncate", .ret_type = 1, .nargs = 2,
328*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { QuadHex | IN, 1 + QUAD_ALIGN } } },
329*f44fc79dSJohn Baldwin #if 0
330*f44fc79dSJohn Baldwin 	/* Does not exist */
331*f44fc79dSJohn Baldwin 	{ .name = "umount", .ret_type = 1, .nargs = 2,
332*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 }, { Int, 2 } } },
333*f44fc79dSJohn Baldwin #endif
334*f44fc79dSJohn Baldwin 	{ .name = "unlink", .ret_type = 1, .nargs = 1,
335*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 } } },
336*f44fc79dSJohn Baldwin 	{ .name = "unlinkat", .ret_type = 1, .nargs = 3,
337*f44fc79dSJohn Baldwin 	  .args = { { Atfd, 0 }, { Name, 1 }, { Atflags, 2 } } },
338*f44fc79dSJohn Baldwin 	{ .name = "unmount", .ret_type = 1, .nargs = 2,
339*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 }, { Int, 1 } } },
340*f44fc79dSJohn Baldwin 	{ .name = "utimensat", .ret_type = 1, .nargs = 4,
341*f44fc79dSJohn Baldwin 	  .args = { { Atfd, 0 }, { Name | IN, 1 }, { Timespec2 | IN, 2 },
342*f44fc79dSJohn Baldwin 		    { Atflags, 3 } } },
343*f44fc79dSJohn Baldwin 	{ .name = "utimes", .ret_type = 1, .nargs = 2,
344*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { Timeval2 | IN, 1 } } },
34534763d1cSJohn Baldwin 	{ .name = "wait4", .ret_type = 1, .nargs = 4,
34634763d1cSJohn Baldwin 	  .args = { { Int, 0 }, { ExitStatus | OUT, 1 }, { Waitoptions, 2 },
34734763d1cSJohn Baldwin 		    { Rusage | OUT, 3 } } },
34834763d1cSJohn Baldwin 	{ .name = "wait6", .ret_type = 1, .nargs = 6,
34972df19e7SJohn Baldwin 	  .args = { { Idtype, 0 }, { Quad, 1 + QUAD_ALIGN },
35072df19e7SJohn Baldwin 		    { ExitStatus | OUT, 1 + QUAD_ALIGN + QUAD_SLOTS },
35172df19e7SJohn Baldwin 		    { Waitoptions, 2 + QUAD_ALIGN + QUAD_SLOTS },
35272df19e7SJohn Baldwin 		    { Rusage | OUT, 3 + QUAD_ALIGN + QUAD_SLOTS },
35372df19e7SJohn Baldwin 		    { Ptr, 4 + QUAD_ALIGN + QUAD_SLOTS } } },
354*f44fc79dSJohn Baldwin 	{ .name = "write", .ret_type = 1, .nargs = 3,
355*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { BinString | IN, 1 }, { Int, 2 } } },
356*f44fc79dSJohn Baldwin 
357*f44fc79dSJohn Baldwin 	/* Linux ABI */
358*f44fc79dSJohn Baldwin 	{ .name = "linux_access", .ret_type = 1, .nargs = 2,
359*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 }, { Accessmode, 1 } } },
360*f44fc79dSJohn Baldwin 	{ .name = "linux_execve", .ret_type = 1, .nargs = 3,
361*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { ExecArgs | IN, 1 },
362*f44fc79dSJohn Baldwin 		    { ExecEnv | IN, 2 } } },
363*f44fc79dSJohn Baldwin 	{ .name = "linux_lseek", .ret_type = 2, .nargs = 3,
364*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Int, 1 }, { Whence, 2 } } },
365*f44fc79dSJohn Baldwin 	{ .name = "linux_mkdir", .ret_type = 1, .nargs = 2,
366*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { Int, 1 } } },
367*f44fc79dSJohn Baldwin 	{ .name = "linux_newfstat", .ret_type = 1, .nargs = 2,
368*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { Ptr | OUT, 1 } } },
369*f44fc79dSJohn Baldwin 	{ .name = "linux_newstat", .ret_type = 1, .nargs = 2,
370*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { Ptr | OUT, 1 } } },
371*f44fc79dSJohn Baldwin 	{ .name = "linux_open", .ret_type = 1, .nargs = 3,
372*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 }, { Hex, 1 }, { Octal, 2 } } },
373*f44fc79dSJohn Baldwin 	{ .name = "linux_readlink", .ret_type = 1, .nargs = 3,
374*f44fc79dSJohn Baldwin 	  .args = { { Name, 0 }, { Name | OUT, 1 }, { Int, 2 } } },
375*f44fc79dSJohn Baldwin 	{ .name = "linux_socketcall", .ret_type = 1, .nargs = 2,
376*f44fc79dSJohn Baldwin 	  .args = { { Int, 0 }, { LinuxSockArgs, 1 } } },
377*f44fc79dSJohn Baldwin 	{ .name = "linux_stat64", .ret_type = 1, .nargs = 3,
378*f44fc79dSJohn Baldwin 	  .args = { { Name | IN, 0 }, { Ptr | OUT, 1 }, { Ptr | IN, 1 } } },
379*f44fc79dSJohn Baldwin 
380ee3b0f6eSDiomidis Spinellis 	{ .name = 0 },
381bbeaf6c0SSean Eric Fagan };
3826c61b0f3SBryan Drewery static STAILQ_HEAD(, syscall) syscalls;
383bbeaf6c0SSean Eric Fagan 
384081e5c48SPav Lucistnik /* Xlat idea taken from strace */
385081e5c48SPav Lucistnik struct xlat {
386081e5c48SPav Lucistnik 	int val;
3875d2d083cSXin LI 	const char *str;
388081e5c48SPav Lucistnik };
389081e5c48SPav Lucistnik 
390081e5c48SPav Lucistnik #define	X(a)	{ a, #a },
391081e5c48SPav Lucistnik #define	XEND	{ 0, NULL }
392081e5c48SPav Lucistnik 
393081e5c48SPav Lucistnik static struct xlat kevent_filters[] = {
394081e5c48SPav Lucistnik 	X(EVFILT_READ) X(EVFILT_WRITE) X(EVFILT_AIO) X(EVFILT_VNODE)
395081e5c48SPav Lucistnik 	X(EVFILT_PROC) X(EVFILT_SIGNAL) X(EVFILT_TIMER)
396d98d7ba0SJohn Baldwin 	X(EVFILT_PROCDESC) X(EVFILT_FS) X(EVFILT_LIO) X(EVFILT_USER)
397d98d7ba0SJohn Baldwin 	X(EVFILT_SENDFILE) XEND
398081e5c48SPav Lucistnik };
399081e5c48SPav Lucistnik 
400081e5c48SPav Lucistnik static struct xlat kevent_flags[] = {
401081e5c48SPav Lucistnik 	X(EV_ADD) X(EV_DELETE) X(EV_ENABLE) X(EV_DISABLE) X(EV_ONESHOT)
402d98d7ba0SJohn Baldwin 	X(EV_CLEAR) X(EV_RECEIPT) X(EV_DISPATCH) X(EV_FORCEONESHOT)
403d98d7ba0SJohn Baldwin 	X(EV_DROP) X(EV_FLAG1) X(EV_ERROR) X(EV_EOF) XEND
404081e5c48SPav Lucistnik };
405081e5c48SPav Lucistnik 
406c915ff03SJohn Baldwin static struct xlat kevent_user_ffctrl[] = {
407c915ff03SJohn Baldwin 	X(NOTE_FFNOP) X(NOTE_FFAND) X(NOTE_FFOR) X(NOTE_FFCOPY)
408c915ff03SJohn Baldwin 	XEND
409c915ff03SJohn Baldwin };
410c915ff03SJohn Baldwin 
411c915ff03SJohn Baldwin static struct xlat kevent_rdwr_fflags[] = {
412c915ff03SJohn Baldwin 	X(NOTE_LOWAT) X(NOTE_FILE_POLL) XEND
413c915ff03SJohn Baldwin };
414c915ff03SJohn Baldwin 
415c915ff03SJohn Baldwin static struct xlat kevent_vnode_fflags[] = {
416c915ff03SJohn Baldwin 	X(NOTE_DELETE) X(NOTE_WRITE) X(NOTE_EXTEND) X(NOTE_ATTRIB)
417c915ff03SJohn Baldwin 	X(NOTE_LINK) X(NOTE_RENAME) X(NOTE_REVOKE) XEND
418c915ff03SJohn Baldwin };
419c915ff03SJohn Baldwin 
420c915ff03SJohn Baldwin static struct xlat kevent_proc_fflags[] = {
421c915ff03SJohn Baldwin 	X(NOTE_EXIT) X(NOTE_FORK) X(NOTE_EXEC) X(NOTE_TRACK) X(NOTE_TRACKERR)
422c915ff03SJohn Baldwin 	X(NOTE_CHILD) XEND
423c915ff03SJohn Baldwin };
424c915ff03SJohn Baldwin 
425c915ff03SJohn Baldwin static struct xlat kevent_timer_fflags[] = {
426c915ff03SJohn Baldwin 	X(NOTE_SECONDS) X(NOTE_MSECONDS) X(NOTE_USECONDS) X(NOTE_NSECONDS)
427c915ff03SJohn Baldwin 	XEND
428c915ff03SJohn Baldwin };
429c915ff03SJohn Baldwin 
430a02c83afSEd Schouten static struct xlat poll_flags[] = {
431081e5c48SPav Lucistnik 	X(POLLSTANDARD) X(POLLIN) X(POLLPRI) X(POLLOUT) X(POLLERR)
432081e5c48SPav Lucistnik 	X(POLLHUP) X(POLLNVAL) X(POLLRDNORM) X(POLLRDBAND)
433081e5c48SPav Lucistnik 	X(POLLWRBAND) X(POLLINIGNEOF) XEND
434081e5c48SPav Lucistnik };
435081e5c48SPav Lucistnik 
436081e5c48SPav Lucistnik static struct xlat mmap_flags[] = {
4375817298fSJohn Baldwin 	X(MAP_SHARED) X(MAP_PRIVATE) X(MAP_FIXED) X(MAP_RESERVED0020)
4385817298fSJohn Baldwin 	X(MAP_RESERVED0040) X(MAP_RESERVED0080) X(MAP_RESERVED0100)
439081e5c48SPav Lucistnik 	X(MAP_HASSEMAPHORE) X(MAP_STACK) X(MAP_NOSYNC) X(MAP_ANON)
440d98d7ba0SJohn Baldwin 	X(MAP_EXCL) X(MAP_NOCORE) X(MAP_PREFAULT_READ)
441edb572a3SJohn Baldwin #ifdef MAP_32BIT
442edb572a3SJohn Baldwin 	X(MAP_32BIT)
443edb572a3SJohn Baldwin #endif
444edb572a3SJohn Baldwin 	XEND
445081e5c48SPav Lucistnik };
446081e5c48SPav Lucistnik 
447081e5c48SPav Lucistnik static struct xlat mprot_flags[] = {
448081e5c48SPav Lucistnik 	X(PROT_NONE) X(PROT_READ) X(PROT_WRITE) X(PROT_EXEC) XEND
449081e5c48SPav Lucistnik };
450081e5c48SPav Lucistnik 
451081e5c48SPav Lucistnik static struct xlat whence_arg[] = {
452d98d7ba0SJohn Baldwin 	X(SEEK_SET) X(SEEK_CUR) X(SEEK_END) X(SEEK_DATA) X(SEEK_HOLE) XEND
453081e5c48SPav Lucistnik };
454081e5c48SPav Lucistnik 
455081e5c48SPav Lucistnik static struct xlat sigaction_flags[] = {
456081e5c48SPav Lucistnik 	X(SA_ONSTACK) X(SA_RESTART) X(SA_RESETHAND) X(SA_NOCLDSTOP)
457081e5c48SPav Lucistnik 	X(SA_NODEFER) X(SA_NOCLDWAIT) X(SA_SIGINFO) XEND
458081e5c48SPav Lucistnik };
459081e5c48SPav Lucistnik 
460081e5c48SPav Lucistnik static struct xlat fcntl_arg[] = {
461081e5c48SPav Lucistnik 	X(F_DUPFD) X(F_GETFD) X(F_SETFD) X(F_GETFL) X(F_SETFL)
462d98d7ba0SJohn Baldwin 	X(F_GETOWN) X(F_SETOWN) X(F_OGETLK) X(F_OSETLK) X(F_OSETLKW)
463d98d7ba0SJohn Baldwin 	X(F_DUP2FD) X(F_GETLK) X(F_SETLK) X(F_SETLKW) X(F_SETLK_REMOTE)
464d98d7ba0SJohn Baldwin 	X(F_READAHEAD) X(F_RDAHEAD) X(F_DUPFD_CLOEXEC) X(F_DUP2FD_CLOEXEC)
465d98d7ba0SJohn Baldwin 	XEND
466081e5c48SPav Lucistnik };
467081e5c48SPav Lucistnik 
468081e5c48SPav Lucistnik static struct xlat fcntlfd_arg[] = {
469081e5c48SPav Lucistnik 	X(FD_CLOEXEC) XEND
470081e5c48SPav Lucistnik };
471081e5c48SPav Lucistnik 
472081e5c48SPav Lucistnik static struct xlat fcntlfl_arg[] = {
473081e5c48SPav Lucistnik 	X(O_APPEND) X(O_ASYNC) X(O_FSYNC) X(O_NONBLOCK) X(O_NOFOLLOW)
474d98d7ba0SJohn Baldwin 	X(FRDAHEAD) X(O_DIRECT) XEND
475081e5c48SPav Lucistnik };
476081e5c48SPav Lucistnik 
477081e5c48SPav Lucistnik static struct xlat sockdomain_arg[] = {
478081e5c48SPav Lucistnik 	X(PF_UNSPEC) X(PF_LOCAL) X(PF_UNIX) X(PF_INET) X(PF_IMPLINK)
479081e5c48SPav Lucistnik 	X(PF_PUP) X(PF_CHAOS) X(PF_NETBIOS) X(PF_ISO) X(PF_OSI)
480081e5c48SPav Lucistnik 	X(PF_ECMA) X(PF_DATAKIT) X(PF_CCITT) X(PF_SNA) X(PF_DECnet)
481081e5c48SPav Lucistnik 	X(PF_DLI) X(PF_LAT) X(PF_HYLINK) X(PF_APPLETALK) X(PF_ROUTE)
482081e5c48SPav Lucistnik 	X(PF_LINK) X(PF_XTP) X(PF_COIP) X(PF_CNT) X(PF_SIP) X(PF_IPX)
483081e5c48SPav Lucistnik 	X(PF_RTIP) X(PF_PIP) X(PF_ISDN) X(PF_KEY) X(PF_INET6)
484081e5c48SPav Lucistnik 	X(PF_NATM) X(PF_ATM) X(PF_NETGRAPH) X(PF_SLOW) X(PF_SCLUSTER)
485d98d7ba0SJohn Baldwin 	X(PF_ARP) X(PF_BLUETOOTH) X(PF_IEEE80211) X(PF_INET_SDP)
486d98d7ba0SJohn Baldwin 	X(PF_INET6_SDP) XEND
487081e5c48SPav Lucistnik };
488081e5c48SPav Lucistnik 
489081e5c48SPav Lucistnik static struct xlat socktype_arg[] = {
490081e5c48SPav Lucistnik 	X(SOCK_STREAM) X(SOCK_DGRAM) X(SOCK_RAW) X(SOCK_RDM)
491081e5c48SPav Lucistnik 	X(SOCK_SEQPACKET) XEND
492081e5c48SPav Lucistnik };
493081e5c48SPav Lucistnik 
494081e5c48SPav Lucistnik static struct xlat open_flags[] = {
495081e5c48SPav Lucistnik 	X(O_RDONLY) X(O_WRONLY) X(O_RDWR) X(O_ACCMODE) X(O_NONBLOCK)
496081e5c48SPav Lucistnik 	X(O_APPEND) X(O_SHLOCK) X(O_EXLOCK) X(O_ASYNC) X(O_FSYNC)
497081e5c48SPav Lucistnik 	X(O_NOFOLLOW) X(O_CREAT) X(O_TRUNC) X(O_EXCL) X(O_NOCTTY)
498d98d7ba0SJohn Baldwin 	X(O_DIRECT) X(O_DIRECTORY) X(O_EXEC) X(O_TTY_INIT) X(O_CLOEXEC)
499d98d7ba0SJohn Baldwin 	X(O_VERIFY) XEND
500081e5c48SPav Lucistnik };
501081e5c48SPav Lucistnik 
502081e5c48SPav Lucistnik static struct xlat shutdown_arg[] = {
503081e5c48SPav Lucistnik 	X(SHUT_RD) X(SHUT_WR) X(SHUT_RDWR) XEND
504081e5c48SPav Lucistnik };
505081e5c48SPav Lucistnik 
506081e5c48SPav Lucistnik static struct xlat resource_arg[] = {
507081e5c48SPav Lucistnik 	X(RLIMIT_CPU) X(RLIMIT_FSIZE) X(RLIMIT_DATA) X(RLIMIT_STACK)
508081e5c48SPav Lucistnik 	X(RLIMIT_CORE) X(RLIMIT_RSS) X(RLIMIT_MEMLOCK) X(RLIMIT_NPROC)
509d98d7ba0SJohn Baldwin 	X(RLIMIT_NOFILE) X(RLIMIT_SBSIZE) X(RLIMIT_VMEM) X(RLIMIT_NPTS)
510d98d7ba0SJohn Baldwin 	X(RLIMIT_SWAP) X(RLIMIT_KQUEUES) XEND
511081e5c48SPav Lucistnik };
512081e5c48SPav Lucistnik 
513081e5c48SPav Lucistnik static struct xlat pathconf_arg[] = {
514081e5c48SPav Lucistnik 	X(_PC_LINK_MAX)  X(_PC_MAX_CANON)  X(_PC_MAX_INPUT)
515081e5c48SPav Lucistnik 	X(_PC_NAME_MAX) X(_PC_PATH_MAX) X(_PC_PIPE_BUF)
516081e5c48SPav Lucistnik 	X(_PC_CHOWN_RESTRICTED) X(_PC_NO_TRUNC) X(_PC_VDISABLE)
517081e5c48SPav Lucistnik 	X(_PC_ASYNC_IO) X(_PC_PRIO_IO) X(_PC_SYNC_IO)
518081e5c48SPav Lucistnik 	X(_PC_ALLOC_SIZE_MIN) X(_PC_FILESIZEBITS)
519081e5c48SPav Lucistnik 	X(_PC_REC_INCR_XFER_SIZE) X(_PC_REC_MAX_XFER_SIZE)
520081e5c48SPav Lucistnik 	X(_PC_REC_MIN_XFER_SIZE) X(_PC_REC_XFER_ALIGN)
521081e5c48SPav Lucistnik 	X(_PC_SYMLINK_MAX) X(_PC_ACL_EXTENDED) X(_PC_ACL_PATH_MAX)
522081e5c48SPav Lucistnik 	X(_PC_CAP_PRESENT) X(_PC_INF_PRESENT) X(_PC_MAC_PRESENT)
523d98d7ba0SJohn Baldwin 	X(_PC_ACL_NFS4) X(_PC_MIN_HOLE_SIZE) XEND
524081e5c48SPav Lucistnik };
525081e5c48SPav Lucistnik 
5269e1db66eSMark Johnston static struct xlat rfork_flags[] = {
527d98d7ba0SJohn Baldwin 	X(RFFDG) X(RFPROC) X(RFMEM) X(RFNOWAIT) X(RFCFDG) X(RFTHREAD)
528d98d7ba0SJohn Baldwin 	X(RFSIGSHARE) X(RFLINUXTHPN) X(RFTSIGZMB) X(RFPPWAIT) XEND
5299e1db66eSMark Johnston };
5309e1db66eSMark Johnston 
53134763d1cSJohn Baldwin static struct xlat wait_options[] = {
53234763d1cSJohn Baldwin 	X(WNOHANG) X(WUNTRACED) X(WCONTINUED) X(WNOWAIT) X(WEXITED)
53334763d1cSJohn Baldwin 	X(WTRAPPED) XEND
53434763d1cSJohn Baldwin };
53534763d1cSJohn Baldwin 
53634763d1cSJohn Baldwin static struct xlat idtype_arg[] = {
53734763d1cSJohn Baldwin 	X(P_PID) X(P_PPID) X(P_PGID) X(P_SID) X(P_CID) X(P_UID) X(P_GID)
53834763d1cSJohn Baldwin 	X(P_ALL) X(P_LWPID) X(P_TASKID) X(P_PROJID) X(P_POOLID) X(P_JAILID)
53934763d1cSJohn Baldwin 	X(P_CTID) X(P_CPUID) X(P_PSETID) XEND
54034763d1cSJohn Baldwin };
54134763d1cSJohn Baldwin 
54255648840SJohn Baldwin static struct xlat procctl_arg[] = {
543a061e3c5SJohn Baldwin 	X(PROC_SPROTECT) X(PROC_REAP_ACQUIRE) X(PROC_REAP_RELEASE)
544a061e3c5SJohn Baldwin 	X(PROC_REAP_STATUS) X(PROC_REAP_GETPIDS) X(PROC_REAP_KILL)
545a061e3c5SJohn Baldwin 	X(PROC_TRACE_CTL) X(PROC_TRACE_STATUS) XEND
54655648840SJohn Baldwin };
54755648840SJohn Baldwin 
548fdb5bf37SJohn Baldwin static struct xlat umtx_ops[] = {
549fdb5bf37SJohn Baldwin 	X(UMTX_OP_RESERVED0) X(UMTX_OP_RESERVED1) X(UMTX_OP_WAIT)
550fdb5bf37SJohn Baldwin 	X(UMTX_OP_WAKE) X(UMTX_OP_MUTEX_TRYLOCK) X(UMTX_OP_MUTEX_LOCK)
551fdb5bf37SJohn Baldwin 	X(UMTX_OP_MUTEX_UNLOCK) X(UMTX_OP_SET_CEILING) X(UMTX_OP_CV_WAIT)
552fdb5bf37SJohn Baldwin 	X(UMTX_OP_CV_SIGNAL) X(UMTX_OP_CV_BROADCAST) X(UMTX_OP_WAIT_UINT)
553fdb5bf37SJohn Baldwin 	X(UMTX_OP_RW_RDLOCK) X(UMTX_OP_RW_WRLOCK) X(UMTX_OP_RW_UNLOCK)
554fdb5bf37SJohn Baldwin 	X(UMTX_OP_WAIT_UINT_PRIVATE) X(UMTX_OP_WAKE_PRIVATE)
555fdb5bf37SJohn Baldwin 	X(UMTX_OP_MUTEX_WAIT) X(UMTX_OP_MUTEX_WAKE) X(UMTX_OP_SEM_WAIT)
556fdb5bf37SJohn Baldwin 	X(UMTX_OP_SEM_WAKE) X(UMTX_OP_NWAKE_PRIVATE) X(UMTX_OP_MUTEX_WAKE2)
55753e1ffbbSJohn Baldwin 	X(UMTX_OP_SEM2_WAIT) X(UMTX_OP_SEM2_WAKE)
558fdb5bf37SJohn Baldwin 	XEND
559fdb5bf37SJohn Baldwin };
560fdb5bf37SJohn Baldwin 
5617d897327SJohn Baldwin static struct xlat at_flags[] = {
5627d897327SJohn Baldwin 	X(AT_EACCESS) X(AT_SYMLINK_NOFOLLOW) X(AT_SYMLINK_FOLLOW)
5637d897327SJohn Baldwin 	X(AT_REMOVEDIR) XEND
5647d897327SJohn Baldwin };
5657d897327SJohn Baldwin 
5667d897327SJohn Baldwin static struct xlat access_modes[] = {
5677d897327SJohn Baldwin 	X(R_OK) X(W_OK) X(X_OK) XEND
5687d897327SJohn Baldwin };
5697d897327SJohn Baldwin 
570b289a8d7SJohn Baldwin static struct xlat sysarch_ops[] = {
571b289a8d7SJohn Baldwin #if defined(__i386__) || defined(__amd64__)
572b289a8d7SJohn Baldwin 	X(I386_GET_LDT) X(I386_SET_LDT) X(I386_GET_IOPERM) X(I386_SET_IOPERM)
573b289a8d7SJohn Baldwin 	X(I386_VM86) X(I386_GET_FSBASE) X(I386_SET_FSBASE) X(I386_GET_GSBASE)
574b289a8d7SJohn Baldwin 	X(I386_SET_GSBASE) X(I386_GET_XFPUSTATE) X(AMD64_GET_FSBASE)
575b289a8d7SJohn Baldwin 	X(AMD64_SET_FSBASE) X(AMD64_GET_GSBASE) X(AMD64_SET_GSBASE)
576b289a8d7SJohn Baldwin 	X(AMD64_GET_XFPUSTATE)
577b289a8d7SJohn Baldwin #endif
578b289a8d7SJohn Baldwin 	XEND
579b289a8d7SJohn Baldwin };
580fb7eabb0SJohn Baldwin 
581fb7eabb0SJohn Baldwin static struct xlat linux_socketcall_ops[] = {
582fb7eabb0SJohn Baldwin 	X(LINUX_SOCKET) X(LINUX_BIND) X(LINUX_CONNECT) X(LINUX_LISTEN)
583fb7eabb0SJohn Baldwin 	X(LINUX_ACCEPT) X(LINUX_GETSOCKNAME) X(LINUX_GETPEERNAME)
584fb7eabb0SJohn Baldwin 	X(LINUX_SOCKETPAIR) X(LINUX_SEND) X(LINUX_RECV) X(LINUX_SENDTO)
585fb7eabb0SJohn Baldwin 	X(LINUX_RECVFROM) X(LINUX_SHUTDOWN) X(LINUX_SETSOCKOPT)
586fb7eabb0SJohn Baldwin 	X(LINUX_GETSOCKOPT) X(LINUX_SENDMSG) X(LINUX_RECVMSG)
587fb7eabb0SJohn Baldwin 	XEND
588fb7eabb0SJohn Baldwin };
589fb7eabb0SJohn Baldwin 
590e462b127SJohn Baldwin static struct xlat sigprocmask_ops[] = {
591e462b127SJohn Baldwin 	X(SIG_BLOCK) X(SIG_UNBLOCK) X(SIG_SETMASK)
592e462b127SJohn Baldwin 	XEND
593e462b127SJohn Baldwin };
594e462b127SJohn Baldwin 
595081e5c48SPav Lucistnik #undef X
596081e5c48SPav Lucistnik #undef XEND
597081e5c48SPav Lucistnik 
598d8984f48SDag-Erling Smørgrav /*
599d8984f48SDag-Erling Smørgrav  * Searches an xlat array for a value, and returns it if found.  Otherwise
600d8984f48SDag-Erling Smørgrav  * return a string representation.
601d8984f48SDag-Erling Smørgrav  */
602d8984f48SDag-Erling Smørgrav static const char *
603d8984f48SDag-Erling Smørgrav lookup(struct xlat *xlat, int val, int base)
604081e5c48SPav Lucistnik {
605081e5c48SPav Lucistnik 	static char tmp[16];
606d8984f48SDag-Erling Smørgrav 
607081e5c48SPav Lucistnik 	for (; xlat->str != NULL; xlat++)
608081e5c48SPav Lucistnik 		if (xlat->val == val)
609d8984f48SDag-Erling Smørgrav 			return (xlat->str);
610081e5c48SPav Lucistnik 	switch (base) {
611081e5c48SPav Lucistnik 		case 8:
612081e5c48SPav Lucistnik 			sprintf(tmp, "0%o", val);
613081e5c48SPav Lucistnik 			break;
614081e5c48SPav Lucistnik 		case 16:
615081e5c48SPav Lucistnik 			sprintf(tmp, "0x%x", val);
616081e5c48SPav Lucistnik 			break;
617081e5c48SPav Lucistnik 		case 10:
618081e5c48SPav Lucistnik 			sprintf(tmp, "%u", val);
619081e5c48SPav Lucistnik 			break;
620081e5c48SPav Lucistnik 		default:
621081e5c48SPav Lucistnik 			errx(1,"Unknown lookup base");
622081e5c48SPav Lucistnik 			break;
623081e5c48SPav Lucistnik 	}
624d8984f48SDag-Erling Smørgrav 	return (tmp);
625081e5c48SPav Lucistnik }
626081e5c48SPav Lucistnik 
6275d2d083cSXin LI static const char *
6285d2d083cSXin LI xlookup(struct xlat *xlat, int val)
629081e5c48SPav Lucistnik {
630d8984f48SDag-Erling Smørgrav 
631d8984f48SDag-Erling Smørgrav 	return (lookup(xlat, val, 16));
632081e5c48SPav Lucistnik }
633081e5c48SPav Lucistnik 
6344e3da534SJohn Baldwin /*
6354e3da534SJohn Baldwin  * Searches an xlat array containing bitfield values.  Remaining bits
6364e3da534SJohn Baldwin  * set after removing the known ones are printed at the end:
6374e3da534SJohn Baldwin  * IN|0x400.
6384e3da534SJohn Baldwin  */
639d8984f48SDag-Erling Smørgrav static char *
640d8984f48SDag-Erling Smørgrav xlookup_bits(struct xlat *xlat, int val)
641081e5c48SPav Lucistnik {
64294355cfdSAndrey Zonov 	int len, rem;
643081e5c48SPav Lucistnik 	static char str[512];
644081e5c48SPav Lucistnik 
64594355cfdSAndrey Zonov 	len = 0;
64694355cfdSAndrey Zonov 	rem = val;
647d8984f48SDag-Erling Smørgrav 	for (; xlat->str != NULL; xlat++) {
648d8984f48SDag-Erling Smørgrav 		if ((xlat->val & rem) == xlat->val) {
6494e3da534SJohn Baldwin 			/*
6504e3da534SJohn Baldwin 			 * Don't print the "all-bits-zero" string unless all
6514e3da534SJohn Baldwin 			 * bits are really zero.
6524e3da534SJohn Baldwin 			 */
653081e5c48SPav Lucistnik 			if (xlat->val == 0 && val != 0)
654081e5c48SPav Lucistnik 				continue;
655081e5c48SPav Lucistnik 			len += sprintf(str + len, "%s|", xlat->str);
656081e5c48SPav Lucistnik 			rem &= ~(xlat->val);
657081e5c48SPav Lucistnik 		}
658081e5c48SPav Lucistnik 	}
6594e3da534SJohn Baldwin 
6604e3da534SJohn Baldwin 	/*
6614e3da534SJohn Baldwin 	 * If we have leftover bits or didn't match anything, print
6624e3da534SJohn Baldwin 	 * the remainder.
6634e3da534SJohn Baldwin 	 */
664081e5c48SPav Lucistnik 	if (rem || len == 0)
665081e5c48SPav Lucistnik 		len += sprintf(str + len, "0x%x", rem);
666081e5c48SPav Lucistnik 	if (len && str[len - 1] == '|')
667081e5c48SPav Lucistnik 		len--;
668081e5c48SPav Lucistnik 	str[len] = 0;
669d8984f48SDag-Erling Smørgrav 	return (str);
670081e5c48SPav Lucistnik }
671081e5c48SPav Lucistnik 
6726c61b0f3SBryan Drewery void
6736c61b0f3SBryan Drewery init_syscalls(void)
6746c61b0f3SBryan Drewery {
6756c61b0f3SBryan Drewery 	struct syscall *sc;
6766c61b0f3SBryan Drewery 
6776c61b0f3SBryan Drewery 	STAILQ_INIT(&syscalls);
6786c61b0f3SBryan Drewery 	for (sc = decoded_syscalls; sc->name != NULL; sc++)
6796c61b0f3SBryan Drewery 		STAILQ_INSERT_HEAD(&syscalls, sc, entries);
6806c61b0f3SBryan Drewery }
681bbeaf6c0SSean Eric Fagan /*
682bbeaf6c0SSean Eric Fagan  * If/when the list gets big, it might be desirable to do it
683bbeaf6c0SSean Eric Fagan  * as a hash table or binary search.
684bbeaf6c0SSean Eric Fagan  */
685bbeaf6c0SSean Eric Fagan struct syscall *
6866c61b0f3SBryan Drewery get_syscall(const char *name, int nargs)
687d8984f48SDag-Erling Smørgrav {
68894355cfdSAndrey Zonov 	struct syscall *sc;
6896c61b0f3SBryan Drewery 	int i;
690bbeaf6c0SSean Eric Fagan 
691d10f73b3SAlfred Perlstein 	if (name == NULL)
692d10f73b3SAlfred Perlstein 		return (NULL);
6936c61b0f3SBryan Drewery 	STAILQ_FOREACH(sc, &syscalls, entries)
69494355cfdSAndrey Zonov 		if (strcmp(name, sc->name) == 0)
695d8984f48SDag-Erling Smørgrav 			return (sc);
6966c61b0f3SBryan Drewery 
6976c61b0f3SBryan Drewery 	/* It is unknown.  Add it into the list. */
6986c61b0f3SBryan Drewery #if DEBUG
6996c61b0f3SBryan Drewery 	fprintf(stderr, "unknown syscall %s -- setting args to %d\n", name,
7006c61b0f3SBryan Drewery 	    nargs);
7016c61b0f3SBryan Drewery #endif
7026c61b0f3SBryan Drewery 
7036c61b0f3SBryan Drewery 	sc = calloc(1, sizeof(struct syscall));
7046c61b0f3SBryan Drewery 	sc->name = strdup(name);
7056c61b0f3SBryan Drewery 	sc->ret_type = 1;
7066c61b0f3SBryan Drewery 	sc->nargs = nargs;
7076c61b0f3SBryan Drewery 	for (i = 0; i < nargs; i++) {
7086c61b0f3SBryan Drewery 		sc->args[i].offset = i;
7096c61b0f3SBryan Drewery 		/* Treat all unknown arguments as LongHex. */
7106c61b0f3SBryan Drewery 		sc->args[i].type = LongHex;
711bbeaf6c0SSean Eric Fagan 	}
7126c61b0f3SBryan Drewery 	STAILQ_INSERT_HEAD(&syscalls, sc, entries);
7136c61b0f3SBryan Drewery 
7146c61b0f3SBryan Drewery 	return (sc);
715bbeaf6c0SSean Eric Fagan }
716bbeaf6c0SSean Eric Fagan 
717bbeaf6c0SSean Eric Fagan /*
7189ddd1412SDag-Erling Smørgrav  * Copy a fixed amount of bytes from the process.
7199ddd1412SDag-Erling Smørgrav  */
7201be5d704SMark Murray static int
721be305c9cSAndrey Zonov get_struct(pid_t pid, void *offset, void *buf, int len)
722d8984f48SDag-Erling Smørgrav {
7235d2d083cSXin LI 	struct ptrace_io_desc iorequest;
7249ddd1412SDag-Erling Smørgrav 
7255d2d083cSXin LI 	iorequest.piod_op = PIOD_READ_D;
7265d2d083cSXin LI 	iorequest.piod_offs = offset;
7275d2d083cSXin LI 	iorequest.piod_addr = buf;
7285d2d083cSXin LI 	iorequest.piod_len = len;
7295d2d083cSXin LI 	if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0)
730d8984f48SDag-Erling Smørgrav 		return (-1);
731d8984f48SDag-Erling Smørgrav 	return (0);
7329ddd1412SDag-Erling Smørgrav }
7339ddd1412SDag-Erling Smørgrav 
7345d2d083cSXin LI #define	MAXSIZE		4096
735abb3f965SJohn Baldwin 
7369ddd1412SDag-Erling Smørgrav /*
737bbeaf6c0SSean Eric Fagan  * Copy a string from the process.  Note that it is
738bbeaf6c0SSean Eric Fagan  * expected to be a C string, but if max is set, it will
739bbeaf6c0SSean Eric Fagan  * only get that much.
740bbeaf6c0SSean Eric Fagan  */
7415d2d083cSXin LI static char *
742abb3f965SJohn Baldwin get_string(pid_t pid, void *addr, int max)
743d8984f48SDag-Erling Smørgrav {
7445d2d083cSXin LI 	struct ptrace_io_desc iorequest;
745abb3f965SJohn Baldwin 	char *buf, *nbuf;
746abb3f965SJohn Baldwin 	size_t offset, size, totalsize;
747bbeaf6c0SSean Eric Fagan 
748abb3f965SJohn Baldwin 	offset = 0;
749abb3f965SJohn Baldwin 	if (max)
750abb3f965SJohn Baldwin 		size = max + 1;
751abb3f965SJohn Baldwin 	else {
752abb3f965SJohn Baldwin 		/* Read up to the end of the current page. */
753abb3f965SJohn Baldwin 		size = PAGE_SIZE - ((uintptr_t)addr % PAGE_SIZE);
754abb3f965SJohn Baldwin 		if (size > MAXSIZE)
755abb3f965SJohn Baldwin 			size = MAXSIZE;
756abb3f965SJohn Baldwin 	}
757abb3f965SJohn Baldwin 	totalsize = size;
7585d2d083cSXin LI 	buf = malloc(totalsize);
7595d2d083cSXin LI 	if (buf == NULL)
760d8984f48SDag-Erling Smørgrav 		return (NULL);
7615d2d083cSXin LI 	for (;;) {
7625d2d083cSXin LI 		iorequest.piod_op = PIOD_READ_D;
763abb3f965SJohn Baldwin 		iorequest.piod_offs = (char *)addr + offset;
764abb3f965SJohn Baldwin 		iorequest.piod_addr = buf + offset;
7655d2d083cSXin LI 		iorequest.piod_len = size;
7665d2d083cSXin LI 		if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0) {
7675d2d083cSXin LI 			free(buf);
768d8984f48SDag-Erling Smørgrav 			return (NULL);
769bbeaf6c0SSean Eric Fagan 		}
770abb3f965SJohn Baldwin 		if (memchr(buf + offset, '\0', size) != NULL)
771abb3f965SJohn Baldwin 			return (buf);
772abb3f965SJohn Baldwin 		offset += size;
773abb3f965SJohn Baldwin 		if (totalsize < MAXSIZE && max == 0) {
774abb3f965SJohn Baldwin 			size = MAXSIZE - totalsize;
775abb3f965SJohn Baldwin 			if (size > PAGE_SIZE)
776abb3f965SJohn Baldwin 				size = PAGE_SIZE;
777abb3f965SJohn Baldwin 			nbuf = realloc(buf, totalsize + size);
778abb3f965SJohn Baldwin 			if (nbuf == NULL) {
779abb3f965SJohn Baldwin 				buf[totalsize - 1] = '\0';
7804e92419dSMarcel Moolenaar 				return (buf);
781bbeaf6c0SSean Eric Fagan 			}
782abb3f965SJohn Baldwin 			buf = nbuf;
783abb3f965SJohn Baldwin 			totalsize += size;
784d8984f48SDag-Erling Smørgrav 		} else {
785cdfc719cSJaakko Heinonen 			buf[totalsize - 1] = '\0';
786d8984f48SDag-Erling Smørgrav 			return (buf);
7875d2d083cSXin LI 		}
7885d2d083cSXin LI 	}
7895d2d083cSXin LI }
790bbeaf6c0SSean Eric Fagan 
79134763d1cSJohn Baldwin static char *
79234763d1cSJohn Baldwin strsig2(int sig)
79334763d1cSJohn Baldwin {
794f083f689SJohn Baldwin 	static char tmp[sizeof(int) * 3 + 1];
795f083f689SJohn Baldwin 	char *ret;
79634763d1cSJohn Baldwin 
797f083f689SJohn Baldwin 	ret = strsig(sig);
798f083f689SJohn Baldwin 	if (ret == NULL) {
799f083f689SJohn Baldwin 		snprintf(tmp, sizeof(tmp), "%d", sig);
800f083f689SJohn Baldwin 		ret = tmp;
801f083f689SJohn Baldwin 	}
802f083f689SJohn Baldwin 	return (ret);
80334763d1cSJohn Baldwin }
804bbeaf6c0SSean Eric Fagan 
805c915ff03SJohn Baldwin static void
806c915ff03SJohn Baldwin print_kevent(FILE *fp, struct kevent *ke, int input)
807c915ff03SJohn Baldwin {
808c915ff03SJohn Baldwin 
809c915ff03SJohn Baldwin 	switch (ke->filter) {
810c915ff03SJohn Baldwin 	case EVFILT_READ:
811c915ff03SJohn Baldwin 	case EVFILT_WRITE:
812c915ff03SJohn Baldwin 	case EVFILT_VNODE:
813c915ff03SJohn Baldwin 	case EVFILT_PROC:
814c915ff03SJohn Baldwin 	case EVFILT_TIMER:
815c915ff03SJohn Baldwin 	case EVFILT_PROCDESC:
816c915ff03SJohn Baldwin 		fprintf(fp, "%ju", (uintmax_t)ke->ident);
817c915ff03SJohn Baldwin 		break;
818c915ff03SJohn Baldwin 	case EVFILT_SIGNAL:
819c915ff03SJohn Baldwin 		fputs(strsig2(ke->ident), fp);
820c915ff03SJohn Baldwin 		break;
821c915ff03SJohn Baldwin 	default:
822c915ff03SJohn Baldwin 		fprintf(fp, "%p", (void *)ke->ident);
823c915ff03SJohn Baldwin 	}
824c915ff03SJohn Baldwin 	fprintf(fp, ",%s,%s,", xlookup(kevent_filters, ke->filter),
825c915ff03SJohn Baldwin 	    xlookup_bits(kevent_flags, ke->flags));
826c915ff03SJohn Baldwin 	switch (ke->filter) {
827c915ff03SJohn Baldwin 	case EVFILT_READ:
828c915ff03SJohn Baldwin 	case EVFILT_WRITE:
829c915ff03SJohn Baldwin 		fputs(xlookup_bits(kevent_rdwr_fflags, ke->fflags), fp);
830c915ff03SJohn Baldwin 		break;
831c915ff03SJohn Baldwin 	case EVFILT_VNODE:
832c915ff03SJohn Baldwin 		fputs(xlookup_bits(kevent_vnode_fflags, ke->fflags), fp);
833c915ff03SJohn Baldwin 		break;
834c915ff03SJohn Baldwin 	case EVFILT_PROC:
835c915ff03SJohn Baldwin 	case EVFILT_PROCDESC:
836c915ff03SJohn Baldwin 		fputs(xlookup_bits(kevent_proc_fflags, ke->fflags), fp);
837c915ff03SJohn Baldwin 		break;
838c915ff03SJohn Baldwin 	case EVFILT_TIMER:
839c915ff03SJohn Baldwin 		fputs(xlookup_bits(kevent_timer_fflags, ke->fflags), fp);
840c915ff03SJohn Baldwin 		break;
841c915ff03SJohn Baldwin 	case EVFILT_USER: {
842c915ff03SJohn Baldwin 		int ctrl, data;
843c915ff03SJohn Baldwin 
844c915ff03SJohn Baldwin 		ctrl = ke->fflags & NOTE_FFCTRLMASK;
845c915ff03SJohn Baldwin 		data = ke->fflags & NOTE_FFLAGSMASK;
846c915ff03SJohn Baldwin 		if (input) {
847c915ff03SJohn Baldwin 			fputs(xlookup(kevent_user_ffctrl, ctrl), fp);
848c915ff03SJohn Baldwin 			if (ke->fflags & NOTE_TRIGGER)
849c915ff03SJohn Baldwin 				fputs("|NOTE_TRIGGER", fp);
850c915ff03SJohn Baldwin 			if (data != 0)
851c915ff03SJohn Baldwin 				fprintf(fp, "|%#x", data);
852c915ff03SJohn Baldwin 		} else {
853c915ff03SJohn Baldwin 			fprintf(fp, "%#x", data);
854c915ff03SJohn Baldwin 		}
855c915ff03SJohn Baldwin 		break;
856c915ff03SJohn Baldwin 	}
857c915ff03SJohn Baldwin 	default:
858c915ff03SJohn Baldwin 		fprintf(fp, "%#x", ke->fflags);
859c915ff03SJohn Baldwin 	}
860c915ff03SJohn Baldwin 	fprintf(fp, ",%p,%p", (void *)ke->data, (void *)ke->udata);
861c915ff03SJohn Baldwin }
862c915ff03SJohn Baldwin 
863bbeaf6c0SSean Eric Fagan /*
864bbeaf6c0SSean Eric Fagan  * Converts a syscall argument into a string.  Said string is
8654e3da534SJohn Baldwin  * allocated via malloc(), so needs to be free()'d.  sc is
866bbeaf6c0SSean Eric Fagan  * a pointer to the syscall description (see above); args is
867bbeaf6c0SSean Eric Fagan  * an array of all of the system call arguments.
868bbeaf6c0SSean Eric Fagan  */
869bbeaf6c0SSean Eric Fagan char *
8702b75c8adSJohn Baldwin print_arg(struct syscall_args *sc, unsigned long *args, long *retval,
87194355cfdSAndrey Zonov     struct trussinfo *trussinfo)
872d8984f48SDag-Erling Smørgrav {
873f083f689SJohn Baldwin 	FILE *fp;
87494355cfdSAndrey Zonov 	char *tmp;
875f083f689SJohn Baldwin 	size_t tmplen;
87694355cfdSAndrey Zonov 	pid_t pid;
877d8984f48SDag-Erling Smørgrav 
878f083f689SJohn Baldwin 	fp = open_memstream(&tmp, &tmplen);
8792b75c8adSJohn Baldwin 	pid = trussinfo->curthread->proc->pid;
880bbeaf6c0SSean Eric Fagan 	switch (sc->type & ARG_MASK) {
881bbeaf6c0SSean Eric Fagan 	case Hex:
882f083f689SJohn Baldwin 		fprintf(fp, "0x%x", (int)args[sc->offset]);
883bbeaf6c0SSean Eric Fagan 		break;
884bbeaf6c0SSean Eric Fagan 	case Octal:
885f083f689SJohn Baldwin 		fprintf(fp, "0%o", (int)args[sc->offset]);
886bbeaf6c0SSean Eric Fagan 		break;
887bbeaf6c0SSean Eric Fagan 	case Int:
888f083f689SJohn Baldwin 		fprintf(fp, "%d", (int)args[sc->offset]);
889bbeaf6c0SSean Eric Fagan 		break;
890fdb5bf37SJohn Baldwin 	case LongHex:
891f083f689SJohn Baldwin 		fprintf(fp, "0x%lx", args[sc->offset]);
892fdb5bf37SJohn Baldwin 		break;
893b289a8d7SJohn Baldwin 	case Long:
894f083f689SJohn Baldwin 		fprintf(fp, "%ld", args[sc->offset]);
895b289a8d7SJohn Baldwin 		break;
896d8984f48SDag-Erling Smørgrav 	case Name: {
897081e5c48SPav Lucistnik 		/* NULL-terminated string. */
898bbeaf6c0SSean Eric Fagan 		char *tmp2;
8994e3da534SJohn Baldwin 
9005d2d083cSXin LI 		tmp2 = get_string(pid, (void*)args[sc->offset], 0);
901f083f689SJohn Baldwin 		fprintf(fp, "\"%s\"", tmp2);
902bbeaf6c0SSean Eric Fagan 		free(tmp2);
903bbeaf6c0SSean Eric Fagan 		break;
904d8984f48SDag-Erling Smørgrav 	}
905d8984f48SDag-Erling Smørgrav 	case BinString: {
9064e3da534SJohn Baldwin 		/*
9074e3da534SJohn Baldwin 		 * Binary block of data that might have printable characters.
9084e3da534SJohn Baldwin 		 * XXX If type|OUT, assume that the length is the syscall's
9094e3da534SJohn Baldwin 		 * return value.  Otherwise, assume that the length of the block
9104e3da534SJohn Baldwin 		 * is in the next syscall argument.
9114e3da534SJohn Baldwin 		 */
912081e5c48SPav Lucistnik 		int max_string = trussinfo->strsize;
913081e5c48SPav Lucistnik 		char tmp2[max_string + 1], *tmp3;
914081e5c48SPav Lucistnik 		int len;
915081e5c48SPav Lucistnik 		int truncated = 0;
916081e5c48SPav Lucistnik 
917081e5c48SPav Lucistnik 		if (sc->type & OUT)
9182b75c8adSJohn Baldwin 			len = retval[0];
919081e5c48SPav Lucistnik 		else
920081e5c48SPav Lucistnik 			len = args[sc->offset + 1];
921081e5c48SPav Lucistnik 
9224e3da534SJohn Baldwin 		/*
9234e3da534SJohn Baldwin 		 * Don't print more than max_string characters, to avoid word
9244e3da534SJohn Baldwin 		 * wrap.  If we have to truncate put some ... after the string.
925081e5c48SPav Lucistnik 		 */
926081e5c48SPav Lucistnik 		if (len > max_string) {
927081e5c48SPav Lucistnik 			len = max_string;
928081e5c48SPav Lucistnik 			truncated = 1;
929081e5c48SPav Lucistnik 		}
93094355cfdSAndrey Zonov 		if (len && get_struct(pid, (void*)args[sc->offset], &tmp2, len)
93194355cfdSAndrey Zonov 		    != -1) {
932081e5c48SPav Lucistnik 			tmp3 = malloc(len * 4 + 1);
933081e5c48SPav Lucistnik 			while (len) {
93494355cfdSAndrey Zonov 				if (strvisx(tmp3, tmp2, len,
93594355cfdSAndrey Zonov 				    VIS_CSTYLE|VIS_TAB|VIS_NL) <= max_string)
936081e5c48SPav Lucistnik 					break;
937081e5c48SPav Lucistnik 				len--;
938081e5c48SPav Lucistnik 				truncated = 1;
939081e5c48SPav Lucistnik 			};
940f083f689SJohn Baldwin 			fprintf(fp, "\"%s\"%s", tmp3, truncated ?
94194355cfdSAndrey Zonov 			    "..." : "");
942081e5c48SPav Lucistnik 			free(tmp3);
943d8984f48SDag-Erling Smørgrav 		} else {
944f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
945081e5c48SPav Lucistnik 		}
946081e5c48SPav Lucistnik 		break;
947d8984f48SDag-Erling Smørgrav 	}
94868055893SJohn Baldwin 	case ExecArgs:
94968055893SJohn Baldwin 	case ExecEnv:
950d8984f48SDag-Erling Smørgrav 	case StringArray: {
951890843c1SJohn Baldwin 		uintptr_t addr;
952890843c1SJohn Baldwin 		union {
953890843c1SJohn Baldwin 			char *strarray[0];
954890843c1SJohn Baldwin 			char buf[PAGE_SIZE];
955890843c1SJohn Baldwin 		} u;
9569897b203SMatthew N. Dodd 		char *string;
957890843c1SJohn Baldwin 		size_t len;
9582b75c8adSJohn Baldwin 		u_int first, i;
9599897b203SMatthew N. Dodd 
960890843c1SJohn Baldwin 		/*
96168055893SJohn Baldwin 		 * Only parse argv[] and environment arrays from exec calls
96268055893SJohn Baldwin 		 * if requested.
96368055893SJohn Baldwin 		 */
96468055893SJohn Baldwin 		if (((sc->type & ARG_MASK) == ExecArgs &&
96568055893SJohn Baldwin 		    (trussinfo->flags & EXECVEARGS) == 0) ||
96668055893SJohn Baldwin 		    ((sc->type & ARG_MASK) == ExecEnv &&
96768055893SJohn Baldwin 		    (trussinfo->flags & EXECVEENVS) == 0)) {
96868055893SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
96968055893SJohn Baldwin 			break;
97068055893SJohn Baldwin 		}
97168055893SJohn Baldwin 
97268055893SJohn Baldwin 		/*
973890843c1SJohn Baldwin 		 * Read a page of pointers at a time.  Punt if the top-level
974890843c1SJohn Baldwin 		 * pointer is not aligned.  Note that the first read is of
975890843c1SJohn Baldwin 		 * a partial page.
976890843c1SJohn Baldwin 		 */
977890843c1SJohn Baldwin 		addr = args[sc->offset];
978890843c1SJohn Baldwin 		if (addr % sizeof(char *) != 0) {
979890843c1SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
980890843c1SJohn Baldwin 			break;
9819897b203SMatthew N. Dodd 		}
9829897b203SMatthew N. Dodd 
983890843c1SJohn Baldwin 		len = PAGE_SIZE - (addr & PAGE_MASK);
984890843c1SJohn Baldwin 		if (get_struct(pid, (void *)addr, u.buf, len) == -1) {
985890843c1SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
986890843c1SJohn Baldwin 			break;
9879897b203SMatthew N. Dodd 		}
988890843c1SJohn Baldwin 
989890843c1SJohn Baldwin 		fputc('[', fp);
990890843c1SJohn Baldwin 		first = 1;
991890843c1SJohn Baldwin 		i = 0;
992890843c1SJohn Baldwin 		while (u.strarray[i] != NULL) {
993890843c1SJohn Baldwin 			string = get_string(pid, u.strarray[i], 0);
994890843c1SJohn Baldwin 			fprintf(fp, "%s \"%s\"", first ? "" : ",", string);
995890843c1SJohn Baldwin 			free(string);
996890843c1SJohn Baldwin 			first = 0;
997890843c1SJohn Baldwin 
998890843c1SJohn Baldwin 			i++;
999890843c1SJohn Baldwin 			if (i == len / sizeof(char *)) {
1000890843c1SJohn Baldwin 				addr += len;
1001890843c1SJohn Baldwin 				len = PAGE_SIZE;
1002890843c1SJohn Baldwin 				if (get_struct(pid, (void *)addr, u.buf, len) ==
1003890843c1SJohn Baldwin 				    -1) {
1004890843c1SJohn Baldwin 					fprintf(fp, ", <inval>");
1005890843c1SJohn Baldwin 					break;
1006890843c1SJohn Baldwin 				}
1007890843c1SJohn Baldwin 				i = 0;
1008890843c1SJohn Baldwin 			}
1009890843c1SJohn Baldwin 		}
1010890843c1SJohn Baldwin 		fputs(" ]", fp);
10119897b203SMatthew N. Dodd 		break;
1012d8984f48SDag-Erling Smørgrav 	}
101310aeefc9SMarcel Moolenaar #ifdef __LP64__
101410aeefc9SMarcel Moolenaar 	case Quad:
101572df19e7SJohn Baldwin 		fprintf(fp, "%ld", args[sc->offset]);
101672df19e7SJohn Baldwin 		break;
101772df19e7SJohn Baldwin 	case QuadHex:
1018f083f689SJohn Baldwin 		fprintf(fp, "0x%lx", args[sc->offset]);
101910aeefc9SMarcel Moolenaar 		break;
102010aeefc9SMarcel Moolenaar #else
102172df19e7SJohn Baldwin 	case Quad:
102272df19e7SJohn Baldwin 	case QuadHex: {
102310aeefc9SMarcel Moolenaar 		unsigned long long ll;
10244e3da534SJohn Baldwin 
10252b75c8adSJohn Baldwin #if _BYTE_ORDER == _LITTLE_ENDIAN
10262b75c8adSJohn Baldwin 		ll = (unsigned long long)args[sc->offset + 1] << 32 |
10272b75c8adSJohn Baldwin 		    args[sc->offset];
10282b75c8adSJohn Baldwin #else
10292b75c8adSJohn Baldwin 		ll = (unsigned long long)args[sc->offset] << 32 |
10302b75c8adSJohn Baldwin 		    args[sc->offset + 1];
10312b75c8adSJohn Baldwin #endif
103272df19e7SJohn Baldwin 		if ((sc->type & ARG_MASK) == Quad)
103372df19e7SJohn Baldwin 			fprintf(fp, "%lld", ll);
103472df19e7SJohn Baldwin 		else
1035f083f689SJohn Baldwin 			fprintf(fp, "0x%llx", ll);
1036bbeaf6c0SSean Eric Fagan 		break;
1037bbeaf6c0SSean Eric Fagan 	}
103810aeefc9SMarcel Moolenaar #endif
1039bbeaf6c0SSean Eric Fagan 	case Ptr:
1040f083f689SJohn Baldwin 		fprintf(fp, "0x%lx", args[sc->offset]);
1041bbeaf6c0SSean Eric Fagan 		break;
1042d8984f48SDag-Erling Smørgrav 	case Readlinkres: {
10432bae4eb3SAlfred Perlstein 		char *tmp2;
10444e3da534SJohn Baldwin 
10452b75c8adSJohn Baldwin 		if (retval[0] == -1)
10462bae4eb3SAlfred Perlstein 			break;
10472b75c8adSJohn Baldwin 		tmp2 = get_string(pid, (void*)args[sc->offset], retval[0]);
1048f083f689SJohn Baldwin 		fprintf(fp, "\"%s\"", tmp2);
10492bae4eb3SAlfred Perlstein 		free(tmp2);
10502bae4eb3SAlfred Perlstein 		break;
1051d8984f48SDag-Erling Smørgrav 	}
1052d8984f48SDag-Erling Smørgrav 	case Ioctl: {
10534e3da534SJohn Baldwin 		const char *temp;
10544e3da534SJohn Baldwin 		unsigned long cmd;
10554e3da534SJohn Baldwin 
10564e3da534SJohn Baldwin 		cmd = args[sc->offset];
10574e3da534SJohn Baldwin 		temp = ioctlname(cmd);
105894355cfdSAndrey Zonov 		if (temp)
1059f083f689SJohn Baldwin 			fputs(temp, fp);
106094355cfdSAndrey Zonov 		else {
1061f083f689SJohn Baldwin 			fprintf(fp, "0x%lx { IO%s%s 0x%lx('%c'), %lu, %lu }",
10624e3da534SJohn Baldwin 			    cmd, cmd & IOC_OUT ? "R" : "",
10634e3da534SJohn Baldwin 			    cmd & IOC_IN ? "W" : "", IOCGROUP(cmd),
10644e3da534SJohn Baldwin 			    isprint(IOCGROUP(cmd)) ? (char)IOCGROUP(cmd) : '?',
10654e3da534SJohn Baldwin 			    cmd & 0xFF, IOCPARM_LEN(cmd));
1066081e5c48SPav Lucistnik 		}
1067081e5c48SPav Lucistnik 		break;
1068d8984f48SDag-Erling Smørgrav 	}
1069d8984f48SDag-Erling Smørgrav 	case Timespec: {
1070e45a5a0dSDavid Malone 		struct timespec ts;
10714e3da534SJohn Baldwin 
107294355cfdSAndrey Zonov 		if (get_struct(pid, (void *)args[sc->offset], &ts,
107394355cfdSAndrey Zonov 		    sizeof(ts)) != -1)
1074a1436773SJohn Baldwin 			fprintf(fp, "{ %jd.%09ld }", (intmax_t)ts.tv_sec,
107594355cfdSAndrey Zonov 			    ts.tv_nsec);
1076e45a5a0dSDavid Malone 		else
1077f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1078e45a5a0dSDavid Malone 		break;
1079d8984f48SDag-Erling Smørgrav 	}
10807d897327SJohn Baldwin 	case Timespec2: {
10817d897327SJohn Baldwin 		struct timespec ts[2];
10827d897327SJohn Baldwin 		const char *sep;
10837d897327SJohn Baldwin 		unsigned int i;
10847d897327SJohn Baldwin 
10857d897327SJohn Baldwin 		if (get_struct(pid, (void *)args[sc->offset], &ts, sizeof(ts))
10867d897327SJohn Baldwin 		    != -1) {
10871e2ec671SJohn Baldwin 			fputs("{ ", fp);
10887d897327SJohn Baldwin 			sep = "";
10897d897327SJohn Baldwin 			for (i = 0; i < nitems(ts); i++) {
10907d897327SJohn Baldwin 				fputs(sep, fp);
10917d897327SJohn Baldwin 				sep = ", ";
10927d897327SJohn Baldwin 				switch (ts[i].tv_nsec) {
10937d897327SJohn Baldwin 				case UTIME_NOW:
10947d897327SJohn Baldwin 					fprintf(fp, "UTIME_NOW");
10957d897327SJohn Baldwin 					break;
10967d897327SJohn Baldwin 				case UTIME_OMIT:
10977d897327SJohn Baldwin 					fprintf(fp, "UTIME_OMIT");
10987d897327SJohn Baldwin 					break;
10997d897327SJohn Baldwin 				default:
1100a1436773SJohn Baldwin 					fprintf(fp, "%jd.%09ld",
1101a1436773SJohn Baldwin 					    (intmax_t)ts[i].tv_sec,
1102a1436773SJohn Baldwin 					    ts[i].tv_nsec);
11037d897327SJohn Baldwin 					break;
11047d897327SJohn Baldwin 				}
11057d897327SJohn Baldwin 			}
11061e2ec671SJohn Baldwin 			fputs(" }", fp);
11077d897327SJohn Baldwin 		} else
1108f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
11097d897327SJohn Baldwin 		break;
11107d897327SJohn Baldwin 	}
1111d8984f48SDag-Erling Smørgrav 	case Timeval: {
1112e45a5a0dSDavid Malone 		struct timeval tv;
11134e3da534SJohn Baldwin 
111494355cfdSAndrey Zonov 		if (get_struct(pid, (void *)args[sc->offset], &tv, sizeof(tv))
111594355cfdSAndrey Zonov 		    != -1)
1116a1436773SJohn Baldwin 			fprintf(fp, "{ %jd.%06ld }", (intmax_t)tv.tv_sec,
111794355cfdSAndrey Zonov 			    tv.tv_usec);
1118081e5c48SPav Lucistnik 		else
1119f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1120081e5c48SPav Lucistnik 		break;
1121d8984f48SDag-Erling Smørgrav 	}
1122d8984f48SDag-Erling Smørgrav 	case Timeval2: {
1123081e5c48SPav Lucistnik 		struct timeval tv[2];
11244e3da534SJohn Baldwin 
112594355cfdSAndrey Zonov 		if (get_struct(pid, (void *)args[sc->offset], &tv, sizeof(tv))
112694355cfdSAndrey Zonov 		    != -1)
1127a1436773SJohn Baldwin 			fprintf(fp, "{ %jd.%06ld, %jd.%06ld }",
1128a1436773SJohn Baldwin 			    (intmax_t)tv[0].tv_sec, tv[0].tv_usec,
1129a1436773SJohn Baldwin 			    (intmax_t)tv[1].tv_sec, tv[1].tv_usec);
1130e45a5a0dSDavid Malone 		else
1131f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1132e45a5a0dSDavid Malone 		break;
1133d8984f48SDag-Erling Smørgrav 	}
1134d8984f48SDag-Erling Smørgrav 	case Itimerval: {
1135e45a5a0dSDavid Malone 		struct itimerval itv;
11364e3da534SJohn Baldwin 
113794355cfdSAndrey Zonov 		if (get_struct(pid, (void *)args[sc->offset], &itv,
113894355cfdSAndrey Zonov 		    sizeof(itv)) != -1)
1139a1436773SJohn Baldwin 			fprintf(fp, "{ %jd.%06ld, %jd.%06ld }",
1140a1436773SJohn Baldwin 			    (intmax_t)itv.it_interval.tv_sec,
1141081e5c48SPav Lucistnik 			    itv.it_interval.tv_usec,
1142a1436773SJohn Baldwin 			    (intmax_t)itv.it_value.tv_sec,
1143081e5c48SPav Lucistnik 			    itv.it_value.tv_usec);
1144e45a5a0dSDavid Malone 		else
1145f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1146e45a5a0dSDavid Malone 		break;
1147d8984f48SDag-Erling Smørgrav 	}
11481c99a22aSSteven Hartland 	case LinuxSockArgs:
11491c99a22aSSteven Hartland 	{
11501c99a22aSSteven Hartland 		struct linux_socketcall_args largs;
11514e3da534SJohn Baldwin 
11521c99a22aSSteven Hartland 		if (get_struct(pid, (void *)args[sc->offset], (void *)&largs,
1153fb7eabb0SJohn Baldwin 		    sizeof(largs)) != -1)
1154f083f689SJohn Baldwin 			fprintf(fp, "{ %s, 0x%lx }",
1155fb7eabb0SJohn Baldwin 			    lookup(linux_socketcall_ops, largs.what, 10),
11560a46af44SJohn Baldwin 			    (long unsigned int)largs.args);
1157fb7eabb0SJohn Baldwin 		else
1158f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
11591c99a22aSSteven Hartland 		break;
11601c99a22aSSteven Hartland 	}
1161d8984f48SDag-Erling Smørgrav 	case Pollfd: {
1162e45a5a0dSDavid Malone 		/*
116394355cfdSAndrey Zonov 		 * XXX: A Pollfd argument expects the /next/ syscall argument
116494355cfdSAndrey Zonov 		 * to be the number of fds in the array. This matches the poll
116594355cfdSAndrey Zonov 		 * syscall.
1166e45a5a0dSDavid Malone 		 */
1167e45a5a0dSDavid Malone 		struct pollfd *pfd;
1168e45a5a0dSDavid Malone 		int numfds = args[sc->offset + 1];
1169f083f689SJohn Baldwin 		size_t bytes = sizeof(struct pollfd) * numfds;
1170f083f689SJohn Baldwin 		int i;
1171e45a5a0dSDavid Malone 
1172e45a5a0dSDavid Malone 		if ((pfd = malloc(bytes)) == NULL)
1173f083f689SJohn Baldwin 			err(1, "Cannot malloc %zu bytes for pollfd array",
117494355cfdSAndrey Zonov 			    bytes);
117594355cfdSAndrey Zonov 		if (get_struct(pid, (void *)args[sc->offset], pfd, bytes)
117694355cfdSAndrey Zonov 		    != -1) {
1177f083f689SJohn Baldwin 			fputs("{", fp);
1178e45a5a0dSDavid Malone 			for (i = 0; i < numfds; i++) {
1179f083f689SJohn Baldwin 				fprintf(fp, " %d/%s", pfd[i].fd,
1180081e5c48SPav Lucistnik 				    xlookup_bits(poll_flags, pfd[i].events));
1181e45a5a0dSDavid Malone 			}
1182f083f689SJohn Baldwin 			fputs(" }", fp);
1183d8984f48SDag-Erling Smørgrav 		} else {
1184f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1185e45a5a0dSDavid Malone 		}
1186d8984f48SDag-Erling Smørgrav 		free(pfd);
1187e45a5a0dSDavid Malone 		break;
1188d8984f48SDag-Erling Smørgrav 	}
1189d8984f48SDag-Erling Smørgrav 	case Fd_set: {
1190e45a5a0dSDavid Malone 		/*
119194355cfdSAndrey Zonov 		 * XXX: A Fd_set argument expects the /first/ syscall argument
119294355cfdSAndrey Zonov 		 * to be the number of fds in the array.  This matches the
119394355cfdSAndrey Zonov 		 * select syscall.
1194e45a5a0dSDavid Malone 		 */
1195e45a5a0dSDavid Malone 		fd_set *fds;
1196e45a5a0dSDavid Malone 		int numfds = args[0];
1197f083f689SJohn Baldwin 		size_t bytes = _howmany(numfds, _NFDBITS) * _NFDBITS;
1198f083f689SJohn Baldwin 		int i;
1199e45a5a0dSDavid Malone 
1200e45a5a0dSDavid Malone 		if ((fds = malloc(bytes)) == NULL)
1201f083f689SJohn Baldwin 			err(1, "Cannot malloc %zu bytes for fd_set array",
120294355cfdSAndrey Zonov 			    bytes);
120394355cfdSAndrey Zonov 		if (get_struct(pid, (void *)args[sc->offset], fds, bytes)
120494355cfdSAndrey Zonov 		    != -1) {
1205f083f689SJohn Baldwin 			fputs("{", fp);
1206e45a5a0dSDavid Malone 			for (i = 0; i < numfds; i++) {
1207f083f689SJohn Baldwin 				if (FD_ISSET(i, fds))
1208f083f689SJohn Baldwin 					fprintf(fp, " %d", i);
1209e45a5a0dSDavid Malone 			}
1210f083f689SJohn Baldwin 			fputs(" }", fp);
121194355cfdSAndrey Zonov 		} else
1212f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1213d8984f48SDag-Erling Smørgrav 		free(fds);
1214e45a5a0dSDavid Malone 		break;
1215d8984f48SDag-Erling Smørgrav 	}
121634763d1cSJohn Baldwin 	case Signal:
1217f083f689SJohn Baldwin 		fputs(strsig2(args[sc->offset]), fp);
1218f0ebbc29SDag-Erling Smørgrav 		break;
1219d8984f48SDag-Erling Smørgrav 	case Sigset: {
1220081e5c48SPav Lucistnik 		long sig;
1221081e5c48SPav Lucistnik 		sigset_t ss;
1222f083f689SJohn Baldwin 		int i, first;
1223081e5c48SPav Lucistnik 
1224081e5c48SPav Lucistnik 		sig = args[sc->offset];
122594355cfdSAndrey Zonov 		if (get_struct(pid, (void *)args[sc->offset], (void *)&ss,
122694355cfdSAndrey Zonov 		    sizeof(ss)) == -1) {
1227f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1228081e5c48SPav Lucistnik 			break;
1229081e5c48SPav Lucistnik 		}
1230f083f689SJohn Baldwin 		fputs("{ ", fp);
1231f083f689SJohn Baldwin 		first = 1;
1232d8984f48SDag-Erling Smørgrav 		for (i = 1; i < sys_nsig; i++) {
123334763d1cSJohn Baldwin 			if (sigismember(&ss, i)) {
1234f083f689SJohn Baldwin 				fprintf(fp, "%s%s", !first ? "|" : "",
1235f083f689SJohn Baldwin 				    strsig(i));
1236f083f689SJohn Baldwin 				first = 0;
123734763d1cSJohn Baldwin 			}
1238081e5c48SPav Lucistnik 		}
1239f083f689SJohn Baldwin 		if (!first)
1240f083f689SJohn Baldwin 			fputc(' ', fp);
1241f083f689SJohn Baldwin 		fputc('}', fp);
1242081e5c48SPav Lucistnik 		break;
1243d8984f48SDag-Erling Smørgrav 	}
1244d8984f48SDag-Erling Smørgrav 	case Sigprocmask: {
1245f083f689SJohn Baldwin 		fputs(xlookup(sigprocmask_ops, args[sc->offset]), fp);
1246894b8f7aSAlfred Perlstein 		break;
1247d8984f48SDag-Erling Smørgrav 	}
1248d8984f48SDag-Erling Smørgrav 	case Fcntlflag: {
12494e3da534SJohn Baldwin 		/* XXX: Output depends on the value of the previous argument. */
1250081e5c48SPav Lucistnik 		switch (args[sc->offset - 1]) {
1251081e5c48SPav Lucistnik 		case F_SETFD:
1252f083f689SJohn Baldwin 			fputs(xlookup_bits(fcntlfd_arg, args[sc->offset]), fp);
1253081e5c48SPav Lucistnik 			break;
1254081e5c48SPav Lucistnik 		case F_SETFL:
1255f083f689SJohn Baldwin 			fputs(xlookup_bits(fcntlfl_arg, args[sc->offset]), fp);
1256081e5c48SPav Lucistnik 			break;
1257081e5c48SPav Lucistnik 		case F_GETFD:
1258081e5c48SPav Lucistnik 		case F_GETFL:
1259081e5c48SPav Lucistnik 		case F_GETOWN:
1260081e5c48SPav Lucistnik 			break;
1261081e5c48SPav Lucistnik 		default:
1262f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1263081e5c48SPav Lucistnik 			break;
1264081e5c48SPav Lucistnik 		}
1265081e5c48SPav Lucistnik 		break;
1266d8984f48SDag-Erling Smørgrav 	}
1267081e5c48SPav Lucistnik 	case Open:
1268f083f689SJohn Baldwin 		fputs(xlookup_bits(open_flags, args[sc->offset]), fp);
1269081e5c48SPav Lucistnik 		break;
1270081e5c48SPav Lucistnik 	case Fcntl:
1271f083f689SJohn Baldwin 		fputs(xlookup(fcntl_arg, args[sc->offset]), fp);
1272081e5c48SPav Lucistnik 		break;
1273894b8f7aSAlfred Perlstein 	case Mprot:
1274f083f689SJohn Baldwin 		fputs(xlookup_bits(mprot_flags, args[sc->offset]), fp);
1275894b8f7aSAlfred Perlstein 		break;
12765aa60b6fSJohn Baldwin 	case Mmapflags: {
12775aa60b6fSJohn Baldwin 		int align, flags;
12785aa60b6fSJohn Baldwin 
12795aa60b6fSJohn Baldwin 		/*
12805aa60b6fSJohn Baldwin 		 * MAP_ALIGNED can't be handled by xlookup_bits(), so
12815aa60b6fSJohn Baldwin 		 * generate that string manually and prepend it to the
12825aa60b6fSJohn Baldwin 		 * string from xlookup_bits().  Have to be careful to
12835aa60b6fSJohn Baldwin 		 * avoid outputting MAP_ALIGNED|0 if MAP_ALIGNED is
12845aa60b6fSJohn Baldwin 		 * the only flag.
12855aa60b6fSJohn Baldwin 		 */
12865aa60b6fSJohn Baldwin 		flags = args[sc->offset] & ~MAP_ALIGNMENT_MASK;
12875aa60b6fSJohn Baldwin 		align = args[sc->offset] & MAP_ALIGNMENT_MASK;
12885aa60b6fSJohn Baldwin 		if (align != 0) {
12895aa60b6fSJohn Baldwin 			if (align == MAP_ALIGNED_SUPER)
1290f083f689SJohn Baldwin 				fputs("MAP_ALIGNED_SUPER", fp);
12915aa60b6fSJohn Baldwin 			else
1292f083f689SJohn Baldwin 				fprintf(fp, "MAP_ALIGNED(%d)",
12935aa60b6fSJohn Baldwin 				    align >> MAP_ALIGNMENT_SHIFT);
1294f083f689SJohn Baldwin 			if (flags == 0)
1295894b8f7aSAlfred Perlstein 				break;
1296f083f689SJohn Baldwin 			fputc('|', fp);
12975aa60b6fSJohn Baldwin 		}
1298f083f689SJohn Baldwin 		fputs(xlookup_bits(mmap_flags, flags), fp);
12995aa60b6fSJohn Baldwin 		break;
13005aa60b6fSJohn Baldwin 	}
1301fde3a7d1SAlfred Perlstein 	case Whence:
1302f083f689SJohn Baldwin 		fputs(xlookup(whence_arg, args[sc->offset]), fp);
1303081e5c48SPav Lucistnik 		break;
1304081e5c48SPav Lucistnik 	case Sockdomain:
1305f083f689SJohn Baldwin 		fputs(xlookup(sockdomain_arg, args[sc->offset]), fp);
1306081e5c48SPav Lucistnik 		break;
1307c9c2e2dcSJohn Baldwin 	case Socktype: {
1308c9c2e2dcSJohn Baldwin 		int type, flags;
1309c9c2e2dcSJohn Baldwin 
1310c9c2e2dcSJohn Baldwin 		flags = args[sc->offset] & (SOCK_CLOEXEC | SOCK_NONBLOCK);
1311c9c2e2dcSJohn Baldwin 		type = args[sc->offset] & ~flags;
1312c9c2e2dcSJohn Baldwin 		fputs(xlookup(socktype_arg, type), fp);
1313c9c2e2dcSJohn Baldwin 		if (flags & SOCK_CLOEXEC)
1314c9c2e2dcSJohn Baldwin 			fprintf(fp, "|SOCK_CLOEXEC");
1315c9c2e2dcSJohn Baldwin 		if (flags & SOCK_NONBLOCK)
1316c9c2e2dcSJohn Baldwin 			fprintf(fp, "|SOCK_NONBLOCK");
1317081e5c48SPav Lucistnik 		break;
1318c9c2e2dcSJohn Baldwin 	}
1319081e5c48SPav Lucistnik 	case Shutdown:
1320f083f689SJohn Baldwin 		fputs(xlookup(shutdown_arg, args[sc->offset]), fp);
1321081e5c48SPav Lucistnik 		break;
1322081e5c48SPav Lucistnik 	case Resource:
1323f083f689SJohn Baldwin 		fputs(xlookup(resource_arg, args[sc->offset]), fp);
1324081e5c48SPav Lucistnik 		break;
1325081e5c48SPav Lucistnik 	case Pathconf:
1326f083f689SJohn Baldwin 		fputs(xlookup(pathconf_arg, args[sc->offset]), fp);
1327fde3a7d1SAlfred Perlstein 		break;
13289e1db66eSMark Johnston 	case Rforkflags:
1329f083f689SJohn Baldwin 		fputs(xlookup_bits(rfork_flags, args[sc->offset]), fp);
13309e1db66eSMark Johnston 		break;
1331d8984f48SDag-Erling Smørgrav 	case Sockaddr: {
13329ddd1412SDag-Erling Smørgrav 		char addr[64];
13331be5d704SMark Murray 		struct sockaddr_in *lsin;
13341be5d704SMark Murray 		struct sockaddr_in6 *lsin6;
1335dec17687SBrian Feldman 		struct sockaddr_un *sun;
1336dec17687SBrian Feldman 		struct sockaddr *sa;
133766917ca9SJohn Baldwin 		socklen_t len;
1338dec17687SBrian Feldman 		u_char *q;
13399ddd1412SDag-Erling Smørgrav 
1340a7a08c7eSMarcel Moolenaar 		if (args[sc->offset] == 0) {
1341f083f689SJohn Baldwin 			fputs("NULL", fp);
1342a7a08c7eSMarcel Moolenaar 			break;
1343a7a08c7eSMarcel Moolenaar 		}
1344a7a08c7eSMarcel Moolenaar 
13456a656761SAlfred Perlstein 		/*
134666917ca9SJohn Baldwin 		 * Extract the address length from the next argument.  If
134766917ca9SJohn Baldwin 		 * this is an output sockaddr (OUT is set), then the
134866917ca9SJohn Baldwin 		 * next argument is a pointer to a socklen_t.  Otherwise
134966917ca9SJohn Baldwin 		 * the next argument contains a socklen_t by value.
13506a656761SAlfred Perlstein 		 */
135166917ca9SJohn Baldwin 		if (sc->type & OUT) {
135266917ca9SJohn Baldwin 			if (get_struct(pid, (void *)args[sc->offset + 1],
135366917ca9SJohn Baldwin 			    &len, sizeof(len)) == -1) {
135466917ca9SJohn Baldwin 				fprintf(fp, "0x%lx", args[sc->offset]);
13556a656761SAlfred Perlstein 				break;
13566a656761SAlfred Perlstein 			}
135766917ca9SJohn Baldwin 		} else
135866917ca9SJohn Baldwin 			len = args[sc->offset + 1];
135966917ca9SJohn Baldwin 
136066917ca9SJohn Baldwin 		/* If the length is too small, just bail. */
136166917ca9SJohn Baldwin 		if (len < sizeof(*sa)) {
1362f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1363f083f689SJohn Baldwin 			break;
13649ddd1412SDag-Erling Smørgrav 		}
1365dec17687SBrian Feldman 
136666917ca9SJohn Baldwin 		sa = calloc(1, len);
136766917ca9SJohn Baldwin 		if (get_struct(pid, (void *)args[sc->offset], sa, len) == -1) {
136866917ca9SJohn Baldwin 			free(sa);
136966917ca9SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
137066917ca9SJohn Baldwin 			break;
137166917ca9SJohn Baldwin 		}
137266917ca9SJohn Baldwin 
137366917ca9SJohn Baldwin 		switch (sa->sa_family) {
1374dec17687SBrian Feldman 		case AF_INET:
137566917ca9SJohn Baldwin 			if (len < sizeof(*lsin))
137666917ca9SJohn Baldwin 				goto sockaddr_short;
137766917ca9SJohn Baldwin 			lsin = (struct sockaddr_in *)(void *)sa;
13784e3da534SJohn Baldwin 			inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof(addr));
1379f083f689SJohn Baldwin 			fprintf(fp, "{ AF_INET %s:%d }", addr,
138094355cfdSAndrey Zonov 			    htons(lsin->sin_port));
1381dec17687SBrian Feldman 			break;
1382dec17687SBrian Feldman 		case AF_INET6:
138366917ca9SJohn Baldwin 			if (len < sizeof(*lsin6))
138466917ca9SJohn Baldwin 				goto sockaddr_short;
138566917ca9SJohn Baldwin 			lsin6 = (struct sockaddr_in6 *)(void *)sa;
138694355cfdSAndrey Zonov 			inet_ntop(AF_INET6, &lsin6->sin6_addr, addr,
13874e3da534SJohn Baldwin 			    sizeof(addr));
1388f083f689SJohn Baldwin 			fprintf(fp, "{ AF_INET6 [%s]:%d }", addr,
138994355cfdSAndrey Zonov 			    htons(lsin6->sin6_port));
1390dec17687SBrian Feldman 			break;
1391dec17687SBrian Feldman 		case AF_UNIX:
139266917ca9SJohn Baldwin 			sun = (struct sockaddr_un *)sa;
139366917ca9SJohn Baldwin 			fprintf(fp, "{ AF_UNIX \"%.*s\" }",
139466917ca9SJohn Baldwin 			    (int)(len - offsetof(struct sockaddr_un, sun_path)),
139566917ca9SJohn Baldwin 			    sun->sun_path);
1396dec17687SBrian Feldman 			break;
1397dec17687SBrian Feldman 		default:
139866917ca9SJohn Baldwin 		sockaddr_short:
1399f083f689SJohn Baldwin 			fprintf(fp,
1400f083f689SJohn Baldwin 			    "{ sa_len = %d, sa_family = %d, sa_data = {",
1401f083f689SJohn Baldwin 			    (int)sa->sa_len, (int)sa->sa_family);
1402f083f689SJohn Baldwin 			for (q = (u_char *)sa->sa_data;
140366917ca9SJohn Baldwin 			     q < (u_char *)sa + len; q++)
1404f083f689SJohn Baldwin 				fprintf(fp, "%s 0x%02x",
1405f083f689SJohn Baldwin 				    q == (u_char *)sa->sa_data ? "" : ",",
1406f083f689SJohn Baldwin 				    *q);
1407f083f689SJohn Baldwin 			fputs(" } }", fp);
1408dec17687SBrian Feldman 		}
140966917ca9SJohn Baldwin 		free(sa);
14109ddd1412SDag-Erling Smørgrav 		break;
1411d8984f48SDag-Erling Smørgrav 	}
1412d8984f48SDag-Erling Smørgrav 	case Sigaction: {
1413e45a5a0dSDavid Malone 		struct sigaction sa;
1414e45a5a0dSDavid Malone 
141594355cfdSAndrey Zonov 		if (get_struct(pid, (void *)args[sc->offset], &sa, sizeof(sa))
141694355cfdSAndrey Zonov 		    != -1) {
1417f083f689SJohn Baldwin 			fputs("{ ", fp);
1418e45a5a0dSDavid Malone 			if (sa.sa_handler == SIG_DFL)
1419f083f689SJohn Baldwin 				fputs("SIG_DFL", fp);
1420e45a5a0dSDavid Malone 			else if (sa.sa_handler == SIG_IGN)
1421f083f689SJohn Baldwin 				fputs("SIG_IGN", fp);
1422e45a5a0dSDavid Malone 			else
1423f083f689SJohn Baldwin 				fprintf(fp, "%p", sa.sa_handler);
1424f083f689SJohn Baldwin 			fprintf(fp, " %s ss_t }",
1425081e5c48SPav Lucistnik 			    xlookup_bits(sigaction_flags, sa.sa_flags));
142694355cfdSAndrey Zonov 		} else
1427f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1428e45a5a0dSDavid Malone 		break;
1429d8984f48SDag-Erling Smørgrav 	}
1430d8984f48SDag-Erling Smørgrav 	case Kevent: {
1431081e5c48SPav Lucistnik 		/*
14324e3da534SJohn Baldwin 		 * XXX XXX: The size of the array is determined by either the
1433081e5c48SPav Lucistnik 		 * next syscall argument, or by the syscall return value,
1434081e5c48SPav Lucistnik 		 * depending on which argument number we are.  This matches the
1435081e5c48SPav Lucistnik 		 * kevent syscall, but luckily that's the only syscall that uses
1436081e5c48SPav Lucistnik 		 * them.
1437081e5c48SPav Lucistnik 		 */
1438081e5c48SPav Lucistnik 		struct kevent *ke;
1439081e5c48SPav Lucistnik 		int numevents = -1;
1440f083f689SJohn Baldwin 		size_t bytes;
1441f083f689SJohn Baldwin 		int i;
1442081e5c48SPav Lucistnik 
1443081e5c48SPav Lucistnik 		if (sc->offset == 1)
1444081e5c48SPav Lucistnik 			numevents = args[sc->offset+1];
14452b75c8adSJohn Baldwin 		else if (sc->offset == 3 && retval[0] != -1)
14462b75c8adSJohn Baldwin 			numevents = retval[0];
1447081e5c48SPav Lucistnik 
1448f083f689SJohn Baldwin 		if (numevents >= 0) {
1449081e5c48SPav Lucistnik 			bytes = sizeof(struct kevent) * numevents;
1450081e5c48SPav Lucistnik 			if ((ke = malloc(bytes)) == NULL)
1451f083f689SJohn Baldwin 				err(1,
1452f083f689SJohn Baldwin 				    "Cannot malloc %zu bytes for kevent array",
145394355cfdSAndrey Zonov 				    bytes);
1454f083f689SJohn Baldwin 		} else
1455f083f689SJohn Baldwin 			ke = NULL;
145694355cfdSAndrey Zonov 		if (numevents >= 0 && get_struct(pid, (void *)args[sc->offset],
145794355cfdSAndrey Zonov 		    ke, bytes) != -1) {
1458f083f689SJohn Baldwin 			fputc('{', fp);
1459c915ff03SJohn Baldwin 			for (i = 0; i < numevents; i++) {
1460c915ff03SJohn Baldwin 				fputc(' ', fp);
1461c915ff03SJohn Baldwin 				print_kevent(fp, &ke[i], sc->offset == 1);
1462c915ff03SJohn Baldwin 			}
1463f083f689SJohn Baldwin 			fputs(" }", fp);
1464d8984f48SDag-Erling Smørgrav 		} else {
1465f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1466081e5c48SPav Lucistnik 		}
1467d8984f48SDag-Erling Smørgrav 		free(ke);
1468081e5c48SPav Lucistnik 		break;
1469d8984f48SDag-Erling Smørgrav 	}
1470d8984f48SDag-Erling Smørgrav 	case Stat: {
1471081e5c48SPav Lucistnik 		struct stat st;
14724e3da534SJohn Baldwin 
147394355cfdSAndrey Zonov 		if (get_struct(pid, (void *)args[sc->offset], &st, sizeof(st))
147494355cfdSAndrey Zonov 		    != -1) {
1475081e5c48SPav Lucistnik 			char mode[12];
14764e3da534SJohn Baldwin 
1477081e5c48SPav Lucistnik 			strmode(st.st_mode, mode);
1478f083f689SJohn Baldwin 			fprintf(fp,
1479b38fbc2eSJohn Baldwin 			    "{ mode=%s,inode=%ju,size=%jd,blksize=%ld }", mode,
1480b38fbc2eSJohn Baldwin 			    (uintmax_t)st.st_ino, (intmax_t)st.st_size,
148194355cfdSAndrey Zonov 			    (long)st.st_blksize);
1482d8984f48SDag-Erling Smørgrav 		} else {
1483f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1484081e5c48SPav Lucistnik 		}
1485081e5c48SPav Lucistnik 		break;
1486d8984f48SDag-Erling Smørgrav 	}
1487a776866bSBryan Drewery 	case StatFs: {
1488a776866bSBryan Drewery 		unsigned int i;
1489a776866bSBryan Drewery 		struct statfs buf;
14900a71c082SBryan Drewery 
1491a776866bSBryan Drewery 		if (get_struct(pid, (void *)args[sc->offset], &buf,
1492a776866bSBryan Drewery 		    sizeof(buf)) != -1) {
1493a776866bSBryan Drewery 			char fsid[17];
1494a776866bSBryan Drewery 
1495a776866bSBryan Drewery 			bzero(fsid, sizeof(fsid));
1496a776866bSBryan Drewery 			if (buf.f_fsid.val[0] != 0 || buf.f_fsid.val[1] != 0) {
1497a776866bSBryan Drewery 			        for (i = 0; i < sizeof(buf.f_fsid); i++)
1498a776866bSBryan Drewery 					snprintf(&fsid[i*2],
1499a776866bSBryan Drewery 					    sizeof(fsid) - (i*2), "%02x",
1500a776866bSBryan Drewery 					    ((u_char *)&buf.f_fsid)[i]);
1501a776866bSBryan Drewery 			}
1502a776866bSBryan Drewery 			fprintf(fp,
1503a776866bSBryan Drewery 			    "{ fstypename=%s,mntonname=%s,mntfromname=%s,"
1504a776866bSBryan Drewery 			    "fsid=%s }", buf.f_fstypename, buf.f_mntonname,
1505a776866bSBryan Drewery 			    buf.f_mntfromname, fsid);
1506a776866bSBryan Drewery 		} else
1507a776866bSBryan Drewery 			fprintf(fp, "0x%lx", args[sc->offset]);
1508a776866bSBryan Drewery 		break;
1509a776866bSBryan Drewery 	}
1510a776866bSBryan Drewery 
1511d8984f48SDag-Erling Smørgrav 	case Rusage: {
1512081e5c48SPav Lucistnik 		struct rusage ru;
15134e3da534SJohn Baldwin 
151494355cfdSAndrey Zonov 		if (get_struct(pid, (void *)args[sc->offset], &ru, sizeof(ru))
151594355cfdSAndrey Zonov 		    != -1) {
1516f083f689SJohn Baldwin 			fprintf(fp,
1517a1436773SJohn Baldwin 			    "{ u=%jd.%06ld,s=%jd.%06ld,in=%ld,out=%ld }",
1518a1436773SJohn Baldwin 			    (intmax_t)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec,
1519a1436773SJohn Baldwin 			    (intmax_t)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec,
1520081e5c48SPav Lucistnik 			    ru.ru_inblock, ru.ru_oublock);
152194355cfdSAndrey Zonov 		} else
1522f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1523081e5c48SPav Lucistnik 		break;
1524d8984f48SDag-Erling Smørgrav 	}
1525d8984f48SDag-Erling Smørgrav 	case Rlimit: {
1526081e5c48SPav Lucistnik 		struct rlimit rl;
15274e3da534SJohn Baldwin 
152894355cfdSAndrey Zonov 		if (get_struct(pid, (void *)args[sc->offset], &rl, sizeof(rl))
152994355cfdSAndrey Zonov 		    != -1) {
1530f083f689SJohn Baldwin 			fprintf(fp, "{ cur=%ju,max=%ju }",
1531081e5c48SPav Lucistnik 			    rl.rlim_cur, rl.rlim_max);
153294355cfdSAndrey Zonov 		} else
1533f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
1534081e5c48SPav Lucistnik 		break;
1535d8984f48SDag-Erling Smørgrav 	}
153634763d1cSJohn Baldwin 	case ExitStatus: {
153734763d1cSJohn Baldwin 		int status;
1538f083f689SJohn Baldwin 
153934763d1cSJohn Baldwin 		if (get_struct(pid, (void *)args[sc->offset], &status,
154034763d1cSJohn Baldwin 		    sizeof(status)) != -1) {
1541f083f689SJohn Baldwin 			fputs("{ ", fp);
154234763d1cSJohn Baldwin 			if (WIFCONTINUED(status))
1543f083f689SJohn Baldwin 				fputs("CONTINUED", fp);
154434763d1cSJohn Baldwin 			else if (WIFEXITED(status))
1545f083f689SJohn Baldwin 				fprintf(fp, "EXITED,val=%d",
154634763d1cSJohn Baldwin 				    WEXITSTATUS(status));
154734763d1cSJohn Baldwin 			else if (WIFSIGNALED(status))
1548f083f689SJohn Baldwin 				fprintf(fp, "SIGNALED,sig=%s%s",
1549f083f689SJohn Baldwin 				    strsig2(WTERMSIG(status)),
155034763d1cSJohn Baldwin 				    WCOREDUMP(status) ? ",cored" : "");
155134763d1cSJohn Baldwin 			else
1552f083f689SJohn Baldwin 				fprintf(fp, "STOPPED,sig=%s",
1553f083f689SJohn Baldwin 				    strsig2(WTERMSIG(status)));
1554f083f689SJohn Baldwin 			fputs(" }", fp);
155534763d1cSJohn Baldwin 		} else
1556f083f689SJohn Baldwin 			fprintf(fp, "0x%lx", args[sc->offset]);
155734763d1cSJohn Baldwin 		break;
155834763d1cSJohn Baldwin 	}
155934763d1cSJohn Baldwin 	case Waitoptions:
1560f083f689SJohn Baldwin 		fputs(xlookup_bits(wait_options, args[sc->offset]), fp);
156134763d1cSJohn Baldwin 		break;
156234763d1cSJohn Baldwin 	case Idtype:
1563f083f689SJohn Baldwin 		fputs(xlookup(idtype_arg, args[sc->offset]), fp);
156434763d1cSJohn Baldwin 		break;
156555648840SJohn Baldwin 	case Procctl:
1566f083f689SJohn Baldwin 		fputs(xlookup(procctl_arg, args[sc->offset]), fp);
156755648840SJohn Baldwin 		break;
1568fdb5bf37SJohn Baldwin 	case Umtxop:
1569f083f689SJohn Baldwin 		fputs(xlookup(umtx_ops, args[sc->offset]), fp);
1570fdb5bf37SJohn Baldwin 		break;
15717d897327SJohn Baldwin 	case Atfd:
15727d897327SJohn Baldwin 		if ((int)args[sc->offset] == AT_FDCWD)
1573f083f689SJohn Baldwin 			fputs("AT_FDCWD", fp);
15747d897327SJohn Baldwin 		else
1575f083f689SJohn Baldwin 			fprintf(fp, "%d", (int)args[sc->offset]);
15767d897327SJohn Baldwin 		break;
15777d897327SJohn Baldwin 	case Atflags:
1578f083f689SJohn Baldwin 		fputs(xlookup_bits(at_flags, args[sc->offset]), fp);
15797d897327SJohn Baldwin 		break;
15807d897327SJohn Baldwin 	case Accessmode:
15817d897327SJohn Baldwin 		if (args[sc->offset] == F_OK)
1582f083f689SJohn Baldwin 			fputs("F_OK", fp);
15837d897327SJohn Baldwin 		else
1584f083f689SJohn Baldwin 			fputs(xlookup_bits(access_modes, args[sc->offset]), fp);
15857d897327SJohn Baldwin 		break;
1586b289a8d7SJohn Baldwin 	case Sysarch:
1587f083f689SJohn Baldwin 		fputs(xlookup(sysarch_ops, args[sc->offset]), fp);
1588b289a8d7SJohn Baldwin 		break;
15892b75c8adSJohn Baldwin 	case PipeFds:
15902b75c8adSJohn Baldwin 		/*
15912b75c8adSJohn Baldwin 		 * The pipe() system call in the kernel returns its
15922b75c8adSJohn Baldwin 		 * two file descriptors via return values.  However,
15932b75c8adSJohn Baldwin 		 * the interface exposed by libc is that pipe()
15942b75c8adSJohn Baldwin 		 * accepts a pointer to an array of descriptors.
15952b75c8adSJohn Baldwin 		 * Format the output to match the libc API by printing
15962b75c8adSJohn Baldwin 		 * the returned file descriptors as a fake argument.
15972b75c8adSJohn Baldwin 		 *
15982b75c8adSJohn Baldwin 		 * Overwrite the first retval to signal a successful
15992b75c8adSJohn Baldwin 		 * return as well.
16002b75c8adSJohn Baldwin 		 */
16012b75c8adSJohn Baldwin 		fprintf(fp, "{ %ld, %ld }", retval[0], retval[1]);
16022b75c8adSJohn Baldwin 		retval[0] = 0;
16032b75c8adSJohn Baldwin 		break;
1604081e5c48SPav Lucistnik 	default:
1605081e5c48SPav Lucistnik 		errx(1, "Invalid argument type %d\n", sc->type & ARG_MASK);
1606bbeaf6c0SSean Eric Fagan 	}
1607f083f689SJohn Baldwin 	fclose(fp);
1608d8984f48SDag-Erling Smørgrav 	return (tmp);
1609bbeaf6c0SSean Eric Fagan }
1610bbeaf6c0SSean Eric Fagan 
1611bbeaf6c0SSean Eric Fagan /*
1612bbeaf6c0SSean Eric Fagan  * Print (to outfile) the system call and its arguments.  Note that
1613bbeaf6c0SSean Eric Fagan  * nargs is the number of arguments (not the number of words; this is
1614bbeaf6c0SSean Eric Fagan  * potentially confusing, I know).
1615bbeaf6c0SSean Eric Fagan  */
1616bbeaf6c0SSean Eric Fagan void
161794355cfdSAndrey Zonov print_syscall(struct trussinfo *trussinfo, const char *name, int nargs,
161894355cfdSAndrey Zonov     char **s_args)
1619d8984f48SDag-Erling Smørgrav {
1620203098d8SMatthew N. Dodd 	struct timespec timediff;
162194355cfdSAndrey Zonov 	int i, len;
16220d0bd00eSMatthew N. Dodd 
162394355cfdSAndrey Zonov 	len = 0;
1624c03bfcc8SMatthew N. Dodd 	if (trussinfo->flags & FOLLOWFORKS)
16252b75c8adSJohn Baldwin 		len += fprintf(trussinfo->outfile, "%5d: ",
16262b75c8adSJohn Baldwin 		    trussinfo->curthread->proc->pid);
1627c03bfcc8SMatthew N. Dodd 
162894355cfdSAndrey Zonov 	if (name != NULL && (strcmp(name, "execve") == 0 ||
162994355cfdSAndrey Zonov 	    strcmp(name, "exit") == 0)) {
16305695afdeSAndrey Zonov 		clock_gettime(CLOCK_REALTIME, &trussinfo->curthread->after);
16310d0bd00eSMatthew N. Dodd 	}
16320d0bd00eSMatthew N. Dodd 
16330d0bd00eSMatthew N. Dodd 	if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
1634d9dcc463SXin LI 		timespecsubt(&trussinfo->curthread->after,
16355695afdeSAndrey Zonov 		    &trussinfo->start_time, &timediff);
1636a1436773SJohn Baldwin 		len += fprintf(trussinfo->outfile, "%jd.%09ld ",
1637a1436773SJohn Baldwin 		    (intmax_t)timediff.tv_sec, timediff.tv_nsec);
16380d0bd00eSMatthew N. Dodd 	}
16390d0bd00eSMatthew N. Dodd 
16400d0bd00eSMatthew N. Dodd 	if (trussinfo->flags & RELATIVETIMESTAMPS) {
1641d9dcc463SXin LI 		timespecsubt(&trussinfo->curthread->after,
16425695afdeSAndrey Zonov 		    &trussinfo->curthread->before, &timediff);
1643a1436773SJohn Baldwin 		len += fprintf(trussinfo->outfile, "%jd.%09ld ",
1644a1436773SJohn Baldwin 		    (intmax_t)timediff.tv_sec, timediff.tv_nsec);
16450d0bd00eSMatthew N. Dodd 	}
16460d0bd00eSMatthew N. Dodd 
1647ec0bed25SMatthew N. Dodd 	len += fprintf(trussinfo->outfile, "%s(", name);
1648c03bfcc8SMatthew N. Dodd 
1649bbeaf6c0SSean Eric Fagan 	for (i = 0; i < nargs; i++) {
1650bbeaf6c0SSean Eric Fagan 		if (s_args[i])
1651ec0bed25SMatthew N. Dodd 			len += fprintf(trussinfo->outfile, "%s", s_args[i]);
1652bbeaf6c0SSean Eric Fagan 		else
165394355cfdSAndrey Zonov 			len += fprintf(trussinfo->outfile,
165494355cfdSAndrey Zonov 			    "<missing argument>");
165594355cfdSAndrey Zonov 		len += fprintf(trussinfo->outfile, "%s", i < (nargs - 1) ?
165694355cfdSAndrey Zonov 		    "," : "");
1657bbeaf6c0SSean Eric Fagan 	}
1658ec0bed25SMatthew N. Dodd 	len += fprintf(trussinfo->outfile, ")");
16596cb533feSSean Eric Fagan 	for (i = 0; i < 6 - (len / 8); i++)
1660ec0bed25SMatthew N. Dodd 		fprintf(trussinfo->outfile, "\t");
16616cb533feSSean Eric Fagan }
16626cb533feSSean Eric Fagan 
16636cb533feSSean Eric Fagan void
16641bcb5f5aSMarcel Moolenaar print_syscall_ret(struct trussinfo *trussinfo, const char *name, int nargs,
16652b75c8adSJohn Baldwin     char **s_args, int errorp, long *retval, struct syscall *sc)
16661bcb5f5aSMarcel Moolenaar {
1667ee3b0f6eSDiomidis Spinellis 	struct timespec timediff;
1668ee3b0f6eSDiomidis Spinellis 
1669ee3b0f6eSDiomidis Spinellis 	if (trussinfo->flags & COUNTONLY) {
16705695afdeSAndrey Zonov 		clock_gettime(CLOCK_REALTIME, &trussinfo->curthread->after);
1671d9dcc463SXin LI 		timespecsubt(&trussinfo->curthread->after,
16725695afdeSAndrey Zonov 		    &trussinfo->curthread->before, &timediff);
1673d9dcc463SXin LI 		timespecadd(&sc->time, &timediff, &sc->time);
1674ee3b0f6eSDiomidis Spinellis 		sc->ncalls++;
1675ee3b0f6eSDiomidis Spinellis 		if (errorp)
1676ee3b0f6eSDiomidis Spinellis 			sc->nerror++;
1677ee3b0f6eSDiomidis Spinellis 		return;
1678ee3b0f6eSDiomidis Spinellis 	}
1679d8984f48SDag-Erling Smørgrav 
1680ec0bed25SMatthew N. Dodd 	print_syscall(trussinfo, name, nargs, s_args);
16810cf21b4fSBrian Somers 	fflush(trussinfo->outfile);
168294355cfdSAndrey Zonov 	if (errorp)
16832b75c8adSJohn Baldwin 		fprintf(trussinfo->outfile, " ERR#%ld '%s'\n", retval[0],
16842b75c8adSJohn Baldwin 		    strerror(retval[0]));
16852b75c8adSJohn Baldwin #ifndef __LP64__
16866c61b0f3SBryan Drewery 	else if (sc->ret_type == 2) {
16872b75c8adSJohn Baldwin 		off_t off;
16882b75c8adSJohn Baldwin 
16892b75c8adSJohn Baldwin #if _BYTE_ORDER == _LITTLE_ENDIAN
16902b75c8adSJohn Baldwin 		off = (off_t)retval[1] << 32 | retval[0];
16912b75c8adSJohn Baldwin #else
16922b75c8adSJohn Baldwin 		off = (off_t)retval[0] << 32 | retval[1];
16932b75c8adSJohn Baldwin #endif
16942b75c8adSJohn Baldwin 		fprintf(trussinfo->outfile, " = %jd (0x%jx)\n", (intmax_t)off,
16952b75c8adSJohn Baldwin 		    (intmax_t)off);
16966cb533feSSean Eric Fagan 	}
16972b75c8adSJohn Baldwin #endif
16982b75c8adSJohn Baldwin 	else
16992b75c8adSJohn Baldwin 		fprintf(trussinfo->outfile, " = %ld (0x%lx)\n", retval[0],
17002b75c8adSJohn Baldwin 		    retval[0]);
1701bbeaf6c0SSean Eric Fagan }
1702ee3b0f6eSDiomidis Spinellis 
1703ee3b0f6eSDiomidis Spinellis void
1704ee3b0f6eSDiomidis Spinellis print_summary(struct trussinfo *trussinfo)
1705ee3b0f6eSDiomidis Spinellis {
1706ee3b0f6eSDiomidis Spinellis 	struct timespec total = {0, 0};
170794355cfdSAndrey Zonov 	struct syscall *sc;
1708ee3b0f6eSDiomidis Spinellis 	int ncall, nerror;
1709ee3b0f6eSDiomidis Spinellis 
1710ee3b0f6eSDiomidis Spinellis 	fprintf(trussinfo->outfile, "%-20s%15s%8s%8s\n",
1711ee3b0f6eSDiomidis Spinellis 	    "syscall", "seconds", "calls", "errors");
1712ee3b0f6eSDiomidis Spinellis 	ncall = nerror = 0;
17136c61b0f3SBryan Drewery 	STAILQ_FOREACH(sc, &syscalls, entries)
1714ee3b0f6eSDiomidis Spinellis 		if (sc->ncalls) {
171555a8d2bbSJaakko Heinonen 			fprintf(trussinfo->outfile, "%-20s%5jd.%09ld%8d%8d\n",
171655a8d2bbSJaakko Heinonen 			    sc->name, (intmax_t)sc->time.tv_sec,
171755a8d2bbSJaakko Heinonen 			    sc->time.tv_nsec, sc->ncalls, sc->nerror);
1718d9dcc463SXin LI 			timespecadd(&total, &sc->time, &total);
1719ee3b0f6eSDiomidis Spinellis 			ncall += sc->ncalls;
1720ee3b0f6eSDiomidis Spinellis 			nerror += sc->nerror;
1721ee3b0f6eSDiomidis Spinellis 		}
1722ee3b0f6eSDiomidis Spinellis 	fprintf(trussinfo->outfile, "%20s%15s%8s%8s\n",
1723ee3b0f6eSDiomidis Spinellis 	    "", "-------------", "-------", "-------");
172455a8d2bbSJaakko Heinonen 	fprintf(trussinfo->outfile, "%-20s%5jd.%09ld%8d%8d\n",
172555a8d2bbSJaakko Heinonen 	    "", (intmax_t)total.tv_sec, total.tv_nsec, ncall, nerror);
1726ee3b0f6eSDiomidis Spinellis }
1727