xref: /freebsd/usr.bin/truss/syscalls.c (revision 657729a89dd578d8cfc70d6616f5c65a48a8b33a)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright 1997 Sean Eric Fagan
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Sean Eric Fagan
17  * 4. Neither the name of the author may be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * This file has routines used to print out system calls and their
39  * arguments.
40  */
41 
42 #include <sys/aio.h>
43 #include <sys/capsicum.h>
44 #include <sys/types.h>
45 #define	_WANT_FREEBSD11_KEVENT
46 #include <sys/event.h>
47 #include <sys/ioccom.h>
48 #include <sys/mman.h>
49 #include <sys/mount.h>
50 #include <sys/poll.h>
51 #include <sys/procfs.h>
52 #include <sys/ptrace.h>
53 #include <sys/resource.h>
54 #include <sys/sched.h>
55 #include <sys/socket.h>
56 #define _WANT_FREEBSD11_STAT
57 #include <sys/stat.h>
58 #include <sys/sysctl.h>
59 #include <sys/time.h>
60 #include <sys/un.h>
61 #include <sys/wait.h>
62 #include <netinet/in.h>
63 #include <netinet/sctp.h>
64 #include <arpa/inet.h>
65 
66 #include <assert.h>
67 #include <ctype.h>
68 #include <err.h>
69 #define _WANT_KERNEL_ERRNO
70 #include <errno.h>
71 #include <fcntl.h>
72 #include <signal.h>
73 #include <stdbool.h>
74 #include <stddef.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <sysdecode.h>
79 #include <unistd.h>
80 #include <vis.h>
81 
82 #include "truss.h"
83 #include "extern.h"
84 #include "syscall.h"
85 
86 /*
87  * This should probably be in its own file, sorted alphabetically.
88  *
89  * Note: We only scan this table on the initial syscall number to calling
90  * convention lookup, i.e. once each time a new syscall is encountered. This
91  * is unlikely to be a performance issue, but if it is we could sort this array
92  * and use a binary search instead.
93  */
94 static const struct syscall_decode decoded_syscalls[] = {
95 	/* Native ABI */
96 	{ .name = "__acl_aclcheck_fd", .ret_type = 1, .nargs = 3,
97 	  .args = { { Int, 0 }, { Acltype, 1 }, { Ptr, 2 } } },
98 	{ .name = "__acl_aclcheck_file", .ret_type = 1, .nargs = 3,
99 	  .args = { { Name, 0 }, { Acltype, 1 }, { Ptr, 2 } } },
100 	{ .name = "__acl_aclcheck_link", .ret_type = 1, .nargs = 3,
101 	  .args = { { Name, 0 }, { Acltype, 1 }, { Ptr, 2 } } },
102 	{ .name = "__acl_delete_fd", .ret_type = 1, .nargs = 2,
103 	  .args = { { Int, 0 }, { Acltype, 1 } } },
104 	{ .name = "__acl_delete_file", .ret_type = 1, .nargs = 2,
105 	  .args = { { Name, 0 }, { Acltype, 1 } } },
106 	{ .name = "__acl_delete_link", .ret_type = 1, .nargs = 2,
107 	  .args = { { Name, 0 }, { Acltype, 1 } } },
108 	{ .name = "__acl_get_fd", .ret_type = 1, .nargs = 3,
109 	  .args = { { Int, 0 }, { Acltype, 1 }, { Ptr, 2 } } },
110 	{ .name = "__acl_get_file", .ret_type = 1, .nargs = 3,
111 	  .args = { { Name, 0 }, { Acltype, 1 }, { Ptr, 2 } } },
112 	{ .name = "__acl_get_link", .ret_type = 1, .nargs = 3,
113 	  .args = { { Name, 0 }, { Acltype, 1 }, { Ptr, 2 } } },
114 	{ .name = "__acl_set_fd", .ret_type = 1, .nargs = 3,
115 	  .args = { { Int, 0 }, { Acltype, 1 }, { Ptr, 2 } } },
116 	{ .name = "__acl_set_file", .ret_type = 1, .nargs = 3,
117 	  .args = { { Name, 0 }, { Acltype, 1 }, { Ptr, 2 } } },
118 	{ .name = "__acl_set_link", .ret_type = 1, .nargs = 3,
119 	  .args = { { Name, 0 }, { Acltype, 1 }, { Ptr, 2 } } },
120 	{ .name = "__cap_rights_get", .ret_type = 1, .nargs = 3,
121 	  .args = { { Int, 0 }, { Int, 1 }, { CapRights | OUT, 2 } } },
122 	{ .name = "__getcwd", .ret_type = 1, .nargs = 2,
123 	  .args = { { Name | OUT, 0 }, { Int, 1 } } },
124 	{ .name = "__realpathat", .ret_type = 1, .nargs = 5,
125 	  .args = { { Atfd, 0 }, { Name | IN, 1 }, { Name | OUT, 2 },
126 		    { Sizet, 3 }, { Int, 4} } },
127 	{ .name = "_umtx_op", .ret_type = 1, .nargs = 5,
128 	  .args = { { Ptr, 0 }, { Umtxop, 1 }, { LongHex, 2 }, { Ptr, 3 },
129 		    { Ptr, 4 } } },
130 	{ .name = "accept", .ret_type = 1, .nargs = 3,
131 	  .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
132 	{ .name = "access", .ret_type = 1, .nargs = 2,
133 	  .args = { { Name | IN, 0 }, { Accessmode, 1 } } },
134 	{ .name = "aio_cancel", .ret_type = 1, .nargs = 2,
135 	  .args = { { Int, 0 }, { Aiocb, 1 } } },
136 	{ .name = "aio_error", .ret_type = 1, .nargs = 1,
137 	  .args = { { Aiocb, 0 } } },
138 	{ .name = "aio_fsync", .ret_type = 1, .nargs = 2,
139 	  .args = { { AiofsyncOp, 0 }, { Aiocb, 1 } } },
140 	{ .name = "aio_mlock", .ret_type = 1, .nargs = 1,
141 	  .args = { { Aiocb, 0 } } },
142 	{ .name = "aio_read", .ret_type = 1, .nargs = 1,
143 	  .args = { { Aiocb, 0 } } },
144 	{ .name = "aio_return", .ret_type = 1, .nargs = 1,
145 	  .args = { { Aiocb, 0 } } },
146 	{ .name = "aio_suspend", .ret_type = 1, .nargs = 3,
147 	  .args = { { AiocbArray, 0 }, { Int, 1 }, { Timespec, 2 } } },
148 	{ .name = "aio_waitcomplete", .ret_type = 1, .nargs = 2,
149 	  .args = { { AiocbPointer | OUT, 0 }, { Timespec, 1 } } },
150 	{ .name = "aio_write", .ret_type = 1, .nargs = 1,
151 	  .args = { { Aiocb, 0 } } },
152 	{ .name = "bind", .ret_type = 1, .nargs = 3,
153 	  .args = { { Int, 0 }, { Sockaddr | IN, 1 }, { Socklent, 2 } } },
154 	{ .name = "bindat", .ret_type = 1, .nargs = 4,
155 	  .args = { { Atfd, 0 }, { Int, 1 }, { Sockaddr | IN, 2 },
156 		    { Int, 3 } } },
157 	{ .name = "break", .ret_type = 1, .nargs = 1,
158 	  .args = { { Ptr, 0 } } },
159 	{ .name = "cap_fcntls_get", .ret_type = 1, .nargs = 2,
160 	  .args = { { Int, 0 }, { CapFcntlRights | OUT, 1 } } },
161 	{ .name = "cap_fcntls_limit", .ret_type = 1, .nargs = 2,
162 	  .args = { { Int, 0 }, { CapFcntlRights, 1 } } },
163 	{ .name = "cap_getmode", .ret_type = 1, .nargs = 1,
164 	  .args = { { PUInt | OUT, 0 } } },
165 	{ .name = "cap_rights_limit", .ret_type = 1, .nargs = 2,
166 	  .args = { { Int, 0 }, { CapRights, 1 } } },
167 	{ .name = "chdir", .ret_type = 1, .nargs = 1,
168 	  .args = { { Name, 0 } } },
169 	{ .name = "chflags", .ret_type = 1, .nargs = 2,
170 	  .args = { { Name | IN, 0 }, { FileFlags, 1 } } },
171 	{ .name = "chflagsat", .ret_type = 1, .nargs = 4,
172 	  .args = { { Atfd, 0 }, { Name | IN, 1 }, { FileFlags, 2 },
173 		    { Atflags, 3 } } },
174 	{ .name = "chmod", .ret_type = 1, .nargs = 2,
175 	  .args = { { Name, 0 }, { Octal, 1 } } },
176 	{ .name = "chown", .ret_type = 1, .nargs = 3,
177 	  .args = { { Name, 0 }, { Int, 1 }, { Int, 2 } } },
178 	{ .name = "chroot", .ret_type = 1, .nargs = 1,
179 	  .args = { { Name, 0 } } },
180 	{ .name = "clock_gettime", .ret_type = 1, .nargs = 2,
181 	  .args = { { Int, 0 }, { Timespec | OUT, 1 } } },
182 	{ .name = "close", .ret_type = 1, .nargs = 1,
183 	  .args = { { Int, 0 } } },
184 	{ .name = "closefrom", .ret_type = 1, .nargs = 1,
185 	  .args = { { Int, 0 } } },
186 	{ .name = "close_range", .ret_type = 1, .nargs = 3,
187 	  .args = { { Int, 0 }, { Int, 1 }, { Closerangeflags, 2 } } },
188 	{ .name = "compat11.fstat", .ret_type = 1, .nargs = 2,
189 	  .args = { { Int, 0 }, { Stat11 | OUT, 1 } } },
190 	{ .name = "compat11.fstatat", .ret_type = 1, .nargs = 4,
191 	  .args = { { Atfd, 0 }, { Name | IN, 1 }, { Stat11 | OUT, 2 },
192 		    { Atflags, 3 } } },
193 	{ .name = "compat11.kevent", .ret_type = 1, .nargs = 6,
194 	  .args = { { Int, 0 }, { Kevent11, 1 }, { Int, 2 },
195 		    { Kevent11 | OUT, 3 }, { Int, 4 }, { Timespec, 5 } } },
196 	{ .name = "compat11.lstat", .ret_type = 1, .nargs = 2,
197 	  .args = { { Name | IN, 0 }, { Stat11 | OUT, 1 } } },
198 	{ .name = "compat11.mknod", .ret_type = 1, .nargs = 3,
199 	  .args = { { Name, 0 }, { Octal, 1 }, { Int, 2 } } },
200 	{ .name = "compat11.mknodat", .ret_type = 1, .nargs = 4,
201 	  .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 }, { Int, 3 } } },
202 	{ .name = "compat11.stat", .ret_type = 1, .nargs = 2,
203 	  .args = { { Name | IN, 0 }, { Stat11 | OUT, 1 } } },
204 	{ .name = "connect", .ret_type = 1, .nargs = 3,
205 	  .args = { { Int, 0 }, { Sockaddr | IN, 1 }, { Socklent, 2 } } },
206 	{ .name = "connectat", .ret_type = 1, .nargs = 4,
207 	  .args = { { Atfd, 0 }, { Int, 1 }, { Sockaddr | IN, 2 },
208 		    { Int, 3 } } },
209 	{ .name = "dup", .ret_type = 1, .nargs = 1,
210 	  .args = { { Int, 0 } } },
211 	{ .name = "dup2", .ret_type = 1, .nargs = 2,
212 	  .args = { { Int, 0 }, { Int, 1 } } },
213 	{ .name = "eaccess", .ret_type = 1, .nargs = 2,
214 	  .args = { { Name | IN, 0 }, { Accessmode, 1 } } },
215 	{ .name = "execve", .ret_type = 1, .nargs = 3,
216 	  .args = { { Name | IN, 0 }, { ExecArgs | IN, 1 },
217 		    { ExecEnv | IN, 2 } } },
218 	{ .name = "exit", .ret_type = 0, .nargs = 1,
219 	  .args = { { Hex, 0 } } },
220 	{ .name = "extattr_delete_fd", .ret_type = 1, .nargs = 3,
221 	  .args = { { Int, 0 }, { Extattrnamespace, 1 }, { Name, 2 } } },
222 	{ .name = "extattr_delete_file", .ret_type = 1, .nargs = 3,
223 	  .args = { { Name, 0 }, { Extattrnamespace, 1 }, { Name, 2 } } },
224 	{ .name = "extattr_delete_link", .ret_type = 1, .nargs = 3,
225 	  .args = { { Name, 0 }, { Extattrnamespace, 1 }, { Name, 2 } } },
226 	{ .name = "extattr_get_fd", .ret_type = 1, .nargs = 5,
227 	  .args = { { Int, 0 }, { Extattrnamespace, 1 }, { Name, 2 },
228 		    { BinString | OUT, 3 }, { Sizet, 4 } } },
229 	{ .name = "extattr_get_file", .ret_type = 1, .nargs = 5,
230 	  .args = { { Name, 0 }, { Extattrnamespace, 1 }, { Name, 2 },
231 		    { BinString | OUT, 3 }, { Sizet, 4 } } },
232 	{ .name = "extattr_get_link", .ret_type = 1, .nargs = 5,
233 	  .args = { { Name, 0 }, { Extattrnamespace, 1 }, { Name, 2 },
234 		    { BinString | OUT, 3 }, { Sizet, 4 } } },
235 	{ .name = "extattr_list_fd", .ret_type = 1, .nargs = 4,
236 	  .args = { { Int, 0 }, { Extattrnamespace, 1 }, { BinString | OUT, 2 },
237 		    { Sizet, 3 } } },
238 	{ .name = "extattr_list_file", .ret_type = 1, .nargs = 4,
239 	  .args = { { Name, 0 }, { Extattrnamespace, 1 }, { BinString | OUT, 2 },
240 		    { Sizet, 3 } } },
241 	{ .name = "extattr_list_link", .ret_type = 1, .nargs = 4,
242 	  .args = { { Name, 0 }, { Extattrnamespace, 1 }, { BinString | OUT, 2 },
243 		    { Sizet, 3 } } },
244 	{ .name = "extattr_set_fd", .ret_type = 1, .nargs = 5,
245 	  .args = { { Int, 0 }, { Extattrnamespace, 1 }, { Name, 2 },
246 		    { BinString | IN, 3 }, { Sizet, 4 } } },
247 	{ .name = "extattr_set_file", .ret_type = 1, .nargs = 5,
248 	  .args = { { Name, 0 }, { Extattrnamespace, 1 }, { Name, 2 },
249 		    { BinString | IN, 3 }, { Sizet, 4 } } },
250 	{ .name = "extattr_set_link", .ret_type = 1, .nargs = 5,
251 	  .args = { { Name, 0 }, { Extattrnamespace, 1 }, { Name, 2 },
252 		    { BinString | IN, 3 }, { Sizet, 4 } } },
253 	{ .name = "extattrctl", .ret_type = 1, .nargs = 5,
254 	  .args = { { Name, 0 }, { Hex, 1 }, { Name, 2 },
255 		    { Extattrnamespace, 3 }, { Name, 4 } } },
256 	{ .name = "faccessat", .ret_type = 1, .nargs = 4,
257 	  .args = { { Atfd, 0 }, { Name | IN, 1 }, { Accessmode, 2 },
258 		    { Atflags, 3 } } },
259 	{ .name = "fchflags", .ret_type = 1, .nargs = 2,
260 	  .args = { { Int, 0 }, { FileFlags, 1 } } },
261 	{ .name = "fchmod", .ret_type = 1, .nargs = 2,
262 	  .args = { { Int, 0 }, { Octal, 1 } } },
263 	{ .name = "fchmodat", .ret_type = 1, .nargs = 4,
264 	  .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 }, { Atflags, 3 } } },
265 	{ .name = "fchown", .ret_type = 1, .nargs = 3,
266 	  .args = { { Int, 0 }, { Int, 1 }, { Int, 2 } } },
267 	{ .name = "fchownat", .ret_type = 1, .nargs = 5,
268 	  .args = { { Atfd, 0 }, { Name, 1 }, { Int, 2 }, { Int, 3 },
269 		    { Atflags, 4 } } },
270 	{ .name = "fcntl", .ret_type = 1, .nargs = 3,
271 	  .args = { { Int, 0 }, { Fcntl, 1 }, { Fcntlflag, 2 } } },
272 	{ .name = "fdatasync", .ret_type = 1, .nargs = 1,
273 	  .args = { { Int, 0 } } },
274 	{ .name = "flock", .ret_type = 1, .nargs = 2,
275 	  .args = { { Int, 0 }, { Flockop, 1 } } },
276 	{ .name = "fstat", .ret_type = 1, .nargs = 2,
277 	  .args = { { Int, 0 }, { Stat | OUT, 1 } } },
278 	{ .name = "fstatat", .ret_type = 1, .nargs = 4,
279 	  .args = { { Atfd, 0 }, { Name | IN, 1 }, { Stat | OUT, 2 },
280 		    { Atflags, 3 } } },
281 	{ .name = "fstatfs", .ret_type = 1, .nargs = 2,
282 	  .args = { { Int, 0 }, { StatFs | OUT, 1 } } },
283 	{ .name = "fsync", .ret_type = 1, .nargs = 1,
284 	  .args = { { Int, 0 } } },
285 	{ .name = "ftruncate", .ret_type = 1, .nargs = 2,
286 	  .args = { { Int | IN, 0 }, { QuadHex | IN, 1 } } },
287 	{ .name = "futimens", .ret_type = 1, .nargs = 2,
288 	  .args = { { Int, 0 }, { Timespec2 | IN, 1 } } },
289 	{ .name = "futimes", .ret_type = 1, .nargs = 2,
290 	  .args = { { Int, 0 }, { Timeval2 | IN, 1 } } },
291 	{ .name = "futimesat", .ret_type = 1, .nargs = 3,
292 	  .args = { { Atfd, 0 }, { Name | IN, 1 }, { Timeval2 | IN, 2 } } },
293 	{ .name = "getdirentries", .ret_type = 1, .nargs = 4,
294 	  .args = { { Int, 0 }, { BinString | OUT, 1 }, { Int, 2 },
295 		    { PQuadHex | OUT, 3 } } },
296 	{ .name = "getfsstat", .ret_type = 1, .nargs = 3,
297 	  .args = { { Ptr, 0 }, { Long, 1 }, { Getfsstatmode, 2 } } },
298 	{ .name = "getitimer", .ret_type = 1, .nargs = 2,
299 	  .args = { { Itimerwhich, 0 }, { Itimerval | OUT, 2 } } },
300 	{ .name = "getpeername", .ret_type = 1, .nargs = 3,
301 	  .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
302 	{ .name = "getpgid", .ret_type = 1, .nargs = 1,
303 	  .args = { { Int, 0 } } },
304 	{ .name = "getpriority", .ret_type = 1, .nargs = 2,
305 	  .args = { { Priowhich, 0 }, { Int, 1 } } },
306 	{ .name = "getrandom", .ret_type = 1, .nargs = 3,
307 	  .args = { { BinString | OUT, 0 }, { Sizet, 1 }, { UInt, 2 } } },
308 	{ .name = "getrlimit", .ret_type = 1, .nargs = 2,
309 	  .args = { { Resource, 0 }, { Rlimit | OUT, 1 } } },
310 	{ .name = "getrusage", .ret_type = 1, .nargs = 2,
311 	  .args = { { RusageWho, 0 }, { Rusage | OUT, 1 } } },
312 	{ .name = "getsid", .ret_type = 1, .nargs = 1,
313 	  .args = { { Int, 0 } } },
314 	{ .name = "getsockname", .ret_type = 1, .nargs = 3,
315 	  .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
316 	{ .name = "getsockopt", .ret_type = 1, .nargs = 5,
317 	  .args = { { Int, 0 }, { Sockoptlevel, 1 }, { Sockoptname, 2 },
318 		    { Ptr | OUT, 3 }, { Ptr | OUT, 4 } } },
319 	{ .name = "gettimeofday", .ret_type = 1, .nargs = 2,
320 	  .args = { { Timeval | OUT, 0 }, { Ptr, 1 } } },
321 	{ .name = "ioctl", .ret_type = 1, .nargs = 3,
322 	  .args = { { Int, 0 }, { Ioctl, 1 }, { Ptr, 2 } } },
323 	{ .name = "kevent", .ret_type = 1, .nargs = 6,
324 	  .args = { { Int, 0 }, { Kevent, 1 }, { Int, 2 }, { Kevent | OUT, 3 },
325 		    { Int, 4 }, { Timespec, 5 } } },
326 	{ .name = "kill", .ret_type = 1, .nargs = 2,
327 	  .args = { { Int | IN, 0 }, { Signal | IN, 1 } } },
328 	{ .name = "kldfind", .ret_type = 1, .nargs = 1,
329 	  .args = { { Name | IN, 0 } } },
330 	{ .name = "kldfirstmod", .ret_type = 1, .nargs = 1,
331 	  .args = { { Int, 0 } } },
332 	{ .name = "kldload", .ret_type = 1, .nargs = 1,
333 	  .args = { { Name | IN, 0 } } },
334 	{ .name = "kldnext", .ret_type = 1, .nargs = 1,
335 	  .args = { { Int, 0 } } },
336 	{ .name = "kldstat", .ret_type = 1, .nargs = 2,
337 	  .args = { { Int, 0 }, { Ptr, 1 } } },
338 	{ .name = "kldsym", .ret_type = 1, .nargs = 3,
339 	  .args = { { Int, 0 }, { Kldsymcmd, 1 }, { Ptr, 2 } } },
340 	{ .name = "kldunload", .ret_type = 1, .nargs = 1,
341 	  .args = { { Int, 0 } } },
342 	{ .name = "kldunloadf", .ret_type = 1, .nargs = 2,
343 	  .args = { { Int, 0 }, { Kldunloadflags, 1 } } },
344 	{ .name = "kse_release", .ret_type = 0, .nargs = 1,
345 	  .args = { { Timespec, 0 } } },
346 	{ .name = "lchflags", .ret_type = 1, .nargs = 2,
347 	  .args = { { Name | IN, 0 }, { FileFlags, 1 } } },
348 	{ .name = "lchmod", .ret_type = 1, .nargs = 2,
349 	  .args = { { Name, 0 }, { Octal, 1 } } },
350 	{ .name = "lchown", .ret_type = 1, .nargs = 3,
351 	  .args = { { Name, 0 }, { Int, 1 }, { Int, 2 } } },
352 	{ .name = "link", .ret_type = 1, .nargs = 2,
353 	  .args = { { Name, 0 }, { Name, 1 } } },
354 	{ .name = "linkat", .ret_type = 1, .nargs = 5,
355 	  .args = { { Atfd, 0 }, { Name, 1 }, { Atfd, 2 }, { Name, 3 },
356 		    { Atflags, 4 } } },
357 	{ .name = "lio_listio", .ret_type = 1, .nargs = 4,
358 	  .args = { { LioMode, 0 }, { AiocbArray, 1 }, { Int, 2 },
359 		    { Sigevent, 3 } } },
360 	{ .name = "listen", .ret_type = 1, .nargs = 2,
361 	  .args = { { Int, 0 }, { Int, 1 } } },
362  	{ .name = "lseek", .ret_type = 2, .nargs = 3,
363 	  .args = { { Int, 0 }, { QuadHex, 1 }, { Whence, 2 } } },
364 	{ .name = "lstat", .ret_type = 1, .nargs = 2,
365 	  .args = { { Name | IN, 0 }, { Stat | OUT, 1 } } },
366 	{ .name = "lutimes", .ret_type = 1, .nargs = 2,
367 	  .args = { { Name | IN, 0 }, { Timeval2 | IN, 1 } } },
368 	{ .name = "madvise", .ret_type = 1, .nargs = 3,
369 	  .args = { { Ptr, 0 }, { Sizet, 1 }, { Madvice, 2 } } },
370 	{ .name = "minherit", .ret_type = 1, .nargs = 3,
371 	  .args = { { Ptr, 0 }, { Sizet, 1 }, { Minherit, 2 } } },
372 	{ .name = "mkdir", .ret_type = 1, .nargs = 2,
373 	  .args = { { Name, 0 }, { Octal, 1 } } },
374 	{ .name = "mkdirat", .ret_type = 1, .nargs = 3,
375 	  .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 } } },
376 	{ .name = "mkfifo", .ret_type = 1, .nargs = 2,
377 	  .args = { { Name, 0 }, { Octal, 1 } } },
378 	{ .name = "mkfifoat", .ret_type = 1, .nargs = 3,
379 	  .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 } } },
380 	{ .name = "mknod", .ret_type = 1, .nargs = 3,
381 	  .args = { { Name, 0 }, { Octal, 1 }, { Quad, 2 } } },
382 	{ .name = "mknodat", .ret_type = 1, .nargs = 4,
383 	  .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 }, { Quad, 3 } } },
384 	{ .name = "mlock", .ret_type = 1, .nargs = 2,
385 	  .args = { { Ptr, 0 }, { Sizet, 1 } } },
386 	{ .name = "mlockall", .ret_type = 1, .nargs = 1,
387 	  .args = { { Mlockall, 0 } } },
388 	{ .name = "mmap", .ret_type = 1, .nargs = 6,
389 	  .args = { { Ptr, 0 }, { Sizet, 1 }, { Mprot, 2 }, { Mmapflags, 3 },
390 		    { Int, 4 }, { QuadHex, 5 } } },
391 	{ .name = "modfind", .ret_type = 1, .nargs = 1,
392 	  .args = { { Name | IN, 0 } } },
393 	{ .name = "mount", .ret_type = 1, .nargs = 4,
394 	  .args = { { Name, 0 }, { Name, 1 }, { Mountflags, 2 }, { Ptr, 3 } } },
395 	{ .name = "mprotect", .ret_type = 1, .nargs = 3,
396 	  .args = { { Ptr, 0 }, { Sizet, 1 }, { Mprot, 2 } } },
397 	{ .name = "msync", .ret_type = 1, .nargs = 3,
398 	  .args = { { Ptr, 0 }, { Sizet, 1 }, { Msync, 2 } } },
399 	{ .name = "munlock", .ret_type = 1, .nargs = 2,
400 	  .args = { { Ptr, 0 }, { Sizet, 1 } } },
401 	{ .name = "munmap", .ret_type = 1, .nargs = 2,
402 	  .args = { { Ptr, 0 }, { Sizet, 1 } } },
403 	{ .name = "nanosleep", .ret_type = 1, .nargs = 1,
404 	  .args = { { Timespec, 0 } } },
405 	{ .name = "nmount", .ret_type = 1, .nargs = 3,
406 	  .args = { { Ptr, 0 }, { UInt, 1 }, { Mountflags, 2 } } },
407 	{ .name = "open", .ret_type = 1, .nargs = 3,
408 	  .args = { { Name | IN, 0 }, { Open, 1 }, { Octal, 2 } } },
409 	{ .name = "openat", .ret_type = 1, .nargs = 4,
410 	  .args = { { Atfd, 0 }, { Name | IN, 1 }, { Open, 2 },
411 		    { Octal, 3 } } },
412 	{ .name = "pathconf", .ret_type = 1, .nargs = 2,
413 	  .args = { { Name | IN, 0 }, { Pathconf, 1 } } },
414 	{ .name = "pipe", .ret_type = 1, .nargs = 1,
415 	  .args = { { PipeFds | OUT, 0 } } },
416 	{ .name = "pipe2", .ret_type = 1, .nargs = 2,
417 	  .args = { { Ptr, 0 }, { Pipe2, 1 } } },
418 	{ .name = "poll", .ret_type = 1, .nargs = 3,
419 	  .args = { { Pollfd, 0 }, { Int, 1 }, { Int, 2 } } },
420 	{ .name = "posix_fadvise", .ret_type = 1, .nargs = 4,
421 	  .args = { { Int, 0 }, { QuadHex, 1 }, { QuadHex, 2 },
422 		    { Fadvice, 3 } } },
423 	{ .name = "posix_openpt", .ret_type = 1, .nargs = 1,
424 	  .args = { { Open, 0 } } },
425 	{ .name = "ppoll", .ret_type = 1, .nargs = 4,
426 	  .args = { { Pollfd, 0 }, { Int, 1 }, { Timespec | IN, 2 },
427  		    { Sigset | IN, 3 } } },
428 	{ .name = "pread", .ret_type = 1, .nargs = 4,
429 	  .args = { { Int, 0 }, { BinString | OUT, 1 }, { Sizet, 2 },
430 		    { QuadHex, 3 } } },
431 	{ .name = "procctl", .ret_type = 1, .nargs = 4,
432 	  .args = { { Idtype, 0 }, { Quad, 1 }, { Procctl, 2 }, { Ptr, 3 } } },
433 	{ .name = "ptrace", .ret_type = 1, .nargs = 4,
434 	  .args = { { Ptraceop, 0 }, { Int, 1 }, { Ptr, 2 }, { Int, 3 } } },
435 	{ .name = "pwrite", .ret_type = 1, .nargs = 4,
436 	  .args = { { Int, 0 }, { BinString | IN, 1 }, { Sizet, 2 },
437 		    { QuadHex, 3 } } },
438 	{ .name = "quotactl", .ret_type = 1, .nargs = 4,
439 	  .args = { { Name, 0 }, { Quotactlcmd, 1 }, { Int, 2 }, { Ptr, 3 } } },
440 	{ .name = "read", .ret_type = 1, .nargs = 3,
441 	  .args = { { Int, 0 }, { BinString | OUT, 1 }, { Sizet, 2 } } },
442 	{ .name = "readlink", .ret_type = 1, .nargs = 3,
443 	  .args = { { Name, 0 }, { Readlinkres | OUT, 1 }, { Sizet, 2 } } },
444 	{ .name = "readlinkat", .ret_type = 1, .nargs = 4,
445 	  .args = { { Atfd, 0 }, { Name, 1 }, { Readlinkres | OUT, 2 },
446 		    { Sizet, 3 } } },
447 	{ .name = "readv", .ret_type = 1, .nargs = 3,
448 	  .args = { { Int, 0 }, { Iovec | OUT, 1 }, { Int, 2 } } },
449 	{ .name = "reboot", .ret_type = 1, .nargs = 1,
450 	  .args = { { Reboothowto, 0 } } },
451 	{ .name = "recvfrom", .ret_type = 1, .nargs = 6,
452 	  .args = { { Int, 0 }, { BinString | OUT, 1 }, { Sizet, 2 },
453 	            { Msgflags, 3 }, { Sockaddr | OUT, 4 },
454 	            { Ptr | OUT, 5 } } },
455 	{ .name = "recvmsg", .ret_type = 1, .nargs = 3,
456 	  .args = { { Int, 0 }, { Msghdr | OUT, 1 }, { Msgflags, 2 } } },
457 	{ .name = "rename", .ret_type = 1, .nargs = 2,
458 	  .args = { { Name, 0 }, { Name, 1 } } },
459 	{ .name = "renameat", .ret_type = 1, .nargs = 4,
460 	  .args = { { Atfd, 0 }, { Name, 1 }, { Atfd, 2 }, { Name, 3 } } },
461 	{ .name = "rfork", .ret_type = 1, .nargs = 1,
462 	  .args = { { Rforkflags, 0 } } },
463 	{ .name = "rmdir", .ret_type = 1, .nargs = 1,
464 	  .args = { { Name, 0 } } },
465 	{ .name = "rtprio", .ret_type = 1, .nargs = 3,
466 	  .args = { { Rtpriofunc, 0 }, { Int, 1 }, { Ptr, 2 } } },
467 	{ .name = "rtprio_thread", .ret_type = 1, .nargs = 3,
468 	  .args = { { Rtpriofunc, 0 }, { Int, 1 }, { Ptr, 2 } } },
469 	{ .name = "sched_get_priority_max", .ret_type = 1, .nargs = 1,
470 	  .args = { { Schedpolicy, 0 } } },
471 	{ .name = "sched_get_priority_min", .ret_type = 1, .nargs = 1,
472 	  .args = { { Schedpolicy, 0 } } },
473 	{ .name = "sched_getparam", .ret_type = 1, .nargs = 2,
474 	  .args = { { Int, 0 }, { Schedparam | OUT, 1 } } },
475 	{ .name = "sched_getscheduler", .ret_type = 1, .nargs = 1,
476 	  .args = { { Int, 0 } } },
477 	{ .name = "sched_rr_get_interval", .ret_type = 1, .nargs = 2,
478 	  .args = { { Int, 0 }, { Timespec | OUT, 1 } } },
479 	{ .name = "sched_setparam", .ret_type = 1, .nargs = 2,
480 	  .args = { { Int, 0 }, { Schedparam, 1 } } },
481 	{ .name = "sched_setscheduler", .ret_type = 1, .nargs = 3,
482 	  .args = { { Int, 0 }, { Schedpolicy, 1 }, { Schedparam, 2 } } },
483 	{ .name = "sctp_generic_recvmsg", .ret_type = 1, .nargs = 7,
484 	  .args = { { Int, 0 }, { Iovec | OUT, 1 }, { Int, 2 },
485 	            { Sockaddr | OUT, 3 }, { Ptr | OUT, 4 },
486 	            { Sctpsndrcvinfo | OUT, 5 }, { Ptr | OUT, 6 } } },
487 	{ .name = "sctp_generic_sendmsg", .ret_type = 1, .nargs = 7,
488 	  .args = { { Int, 0 }, { BinString | IN, 1 }, { Int, 2 },
489 	            { Sockaddr | IN, 3 }, { Socklent, 4 },
490 	            { Sctpsndrcvinfo | IN, 5 }, { Msgflags, 6 } } },
491 	{ .name = "sctp_generic_sendmsg_iov", .ret_type = 1, .nargs = 7,
492 	  .args = { { Int, 0 }, { Iovec | IN, 1 }, { Int, 2 },
493 	            { Sockaddr | IN, 3 }, { Socklent, 4 },
494 	            { Sctpsndrcvinfo | IN, 5 }, { Msgflags, 6 } } },
495 	{ .name = "sendfile", .ret_type = 1, .nargs = 7,
496 	  .args = { { Int, 0 }, { Int, 1 }, { QuadHex, 2 }, { Sizet, 3 },
497 		    { Sendfilehdtr, 4 }, { QuadHex | OUT, 5 },
498 		    { Sendfileflags, 6 } } },
499 	{ .name = "select", .ret_type = 1, .nargs = 5,
500 	  .args = { { Int, 0 }, { Fd_set, 1 }, { Fd_set, 2 }, { Fd_set, 3 },
501 		    { Timeval, 4 } } },
502 	{ .name = "sendmsg", .ret_type = 1, .nargs = 3,
503 	  .args = { { Int, 0 }, { Msghdr | IN, 1 }, { Msgflags, 2 } } },
504 	{ .name = "sendto", .ret_type = 1, .nargs = 6,
505 	  .args = { { Int, 0 }, { BinString | IN, 1 }, { Sizet, 2 },
506 	            { Msgflags, 3 }, { Sockaddr | IN, 4 },
507 	            { Socklent | IN, 5 } } },
508 	{ .name = "setitimer", .ret_type = 1, .nargs = 3,
509 	  .args = { { Itimerwhich, 0 }, { Itimerval, 1 },
510 		    { Itimerval | OUT, 2 } } },
511 	{ .name = "setpriority", .ret_type = 1, .nargs = 3,
512 	  .args = { { Priowhich, 0 }, { Int, 1 }, { Int, 2 } } },
513 	{ .name = "setrlimit", .ret_type = 1, .nargs = 2,
514 	  .args = { { Resource, 0 }, { Rlimit | IN, 1 } } },
515 	{ .name = "setsockopt", .ret_type = 1, .nargs = 5,
516 	  .args = { { Int, 0 }, { Sockoptlevel, 1 }, { Sockoptname, 2 },
517 		    { Ptr | IN, 3 }, { Socklent, 4 } } },
518 	{ .name = "shm_open", .ret_type = 1, .nargs = 3,
519 	  .args = { { ShmName | IN, 0 }, { Open, 1 }, { Octal, 2 } } },
520 	{ .name = "shm_open2", .ret_type = 1, .nargs = 5,
521 	  .args = { { ShmName | IN, 0 }, { Open, 1 }, { Octal, 2 },
522 		    { ShmFlags, 3 }, { Name | IN, 4 } } },
523 	{ .name = "shm_rename", .ret_type = 1, .nargs = 3,
524 	  .args = { { Name | IN, 0 }, { Name | IN, 1 }, { Hex, 2 } } },
525 	{ .name = "shm_unlink", .ret_type = 1, .nargs = 1,
526 	  .args = { { Name | IN, 0 } } },
527 	{ .name = "shutdown", .ret_type = 1, .nargs = 2,
528 	  .args = { { Int, 0 }, { Shutdown, 1 } } },
529 	{ .name = "sigaction", .ret_type = 1, .nargs = 3,
530 	  .args = { { Signal, 0 }, { Sigaction | IN, 1 },
531 		    { Sigaction | OUT, 2 } } },
532 	{ .name = "sigpending", .ret_type = 1, .nargs = 1,
533 	  .args = { { Sigset | OUT, 0 } } },
534 	{ .name = "sigprocmask", .ret_type = 1, .nargs = 3,
535 	  .args = { { Sigprocmask, 0 }, { Sigset, 1 }, { Sigset | OUT, 2 } } },
536 	{ .name = "sigqueue", .ret_type = 1, .nargs = 3,
537 	  .args = { { Int, 0 }, { Signal, 1 }, { LongHex, 2 } } },
538 	{ .name = "sigreturn", .ret_type = 1, .nargs = 1,
539 	  .args = { { Ptr, 0 } } },
540 	{ .name = "sigsuspend", .ret_type = 1, .nargs = 1,
541 	  .args = { { Sigset | IN, 0 } } },
542 	{ .name = "sigtimedwait", .ret_type = 1, .nargs = 3,
543 	  .args = { { Sigset | IN, 0 }, { Siginfo | OUT, 1 },
544 		    { Timespec | IN, 2 } } },
545 	{ .name = "sigwait", .ret_type = 1, .nargs = 2,
546 	  .args = { { Sigset | IN, 0 }, { PSig | OUT, 1 } } },
547 	{ .name = "sigwaitinfo", .ret_type = 1, .nargs = 2,
548 	  .args = { { Sigset | IN, 0 }, { Siginfo | OUT, 1 } } },
549 	{ .name = "socket", .ret_type = 1, .nargs = 3,
550 	  .args = { { Sockdomain, 0 }, { Socktype, 1 }, { Sockprotocol, 2 } } },
551 	{ .name = "stat", .ret_type = 1, .nargs = 2,
552 	  .args = { { Name | IN, 0 }, { Stat | OUT, 1 } } },
553 	{ .name = "statfs", .ret_type = 1, .nargs = 2,
554 	  .args = { { Name | IN, 0 }, { StatFs | OUT, 1 } } },
555 	{ .name = "symlink", .ret_type = 1, .nargs = 2,
556 	  .args = { { Name, 0 }, { Name, 1 } } },
557 	{ .name = "symlinkat", .ret_type = 1, .nargs = 3,
558 	  .args = { { Name, 0 }, { Atfd, 1 }, { Name, 2 } } },
559 	{ .name = "sysarch", .ret_type = 1, .nargs = 2,
560 	  .args = { { Sysarch, 0 }, { Ptr, 1 } } },
561 	{ .name = "__sysctl", .ret_type = 1, .nargs = 6,
562 	  .args = { { Sysctl, 0 }, { Sizet, 1 }, { Ptr, 2 }, { Ptr, 3 },
563 	            { Ptr, 4 }, { Sizet, 5 } } },
564 	{ .name = "__sysctlbyname", .ret_type = 1, .nargs = 6,
565 	  .args = { { Name, 0 }, { Sizet, 1 }, { Ptr, 2 }, { Ptr, 3 },
566 	            { Ptr, 4}, { Sizet, 5 } } },
567 	{ .name = "thr_kill", .ret_type = 1, .nargs = 2,
568 	  .args = { { Long, 0 }, { Signal, 1 } } },
569 	{ .name = "thr_self", .ret_type = 1, .nargs = 1,
570 	  .args = { { Ptr, 0 } } },
571 	{ .name = "thr_set_name", .ret_type = 1, .nargs = 2,
572 	  .args = { { Long, 0 }, { Name, 1 } } },
573 	{ .name = "truncate", .ret_type = 1, .nargs = 2,
574 	  .args = { { Name | IN, 0 }, { QuadHex | IN, 1 } } },
575 #if 0
576 	/* Does not exist */
577 	{ .name = "umount", .ret_type = 1, .nargs = 2,
578 	  .args = { { Name, 0 }, { Int, 2 } } },
579 #endif
580 	{ .name = "unlink", .ret_type = 1, .nargs = 1,
581 	  .args = { { Name, 0 } } },
582 	{ .name = "unlinkat", .ret_type = 1, .nargs = 3,
583 	  .args = { { Atfd, 0 }, { Name, 1 }, { Atflags, 2 } } },
584 	{ .name = "unmount", .ret_type = 1, .nargs = 2,
585 	  .args = { { Name, 0 }, { Mountflags, 1 } } },
586 	{ .name = "utimensat", .ret_type = 1, .nargs = 4,
587 	  .args = { { Atfd, 0 }, { Name | IN, 1 }, { Timespec2 | IN, 2 },
588 		    { Atflags, 3 } } },
589 	{ .name = "utimes", .ret_type = 1, .nargs = 2,
590 	  .args = { { Name | IN, 0 }, { Timeval2 | IN, 1 } } },
591 	{ .name = "utrace", .ret_type = 1, .nargs = 1,
592 	  .args = { { Utrace, 0 } } },
593 	{ .name = "wait4", .ret_type = 1, .nargs = 4,
594 	  .args = { { Int, 0 }, { ExitStatus | OUT, 1 }, { Waitoptions, 2 },
595 		    { Rusage | OUT, 3 } } },
596 	{ .name = "wait6", .ret_type = 1, .nargs = 6,
597 	  .args = { { Idtype, 0 }, { Quad, 1 }, { ExitStatus | OUT, 2 },
598 		    { Waitoptions, 3 }, { Rusage | OUT, 4 },
599 		    { Siginfo | OUT, 5 } } },
600 	{ .name = "write", .ret_type = 1, .nargs = 3,
601 	  .args = { { Int, 0 }, { BinString | IN, 1 }, { Sizet, 2 } } },
602 	{ .name = "writev", .ret_type = 1, .nargs = 3,
603 	  .args = { { Int, 0 }, { Iovec | IN, 1 }, { Int, 2 } } },
604 
605 	/* Linux ABI */
606 	{ .name = "linux_access", .ret_type = 1, .nargs = 2,
607 	  .args = { { Name, 0 }, { Accessmode, 1 } } },
608 	{ .name = "linux_execve", .ret_type = 1, .nargs = 3,
609 	  .args = { { Name | IN, 0 }, { ExecArgs | IN, 1 },
610 		    { ExecEnv | IN, 2 } } },
611 	{ .name = "linux_getitimer", .ret_type = 1, .nargs = 2,
612 	  .args = { { Itimerwhich, 0 }, { Itimerval | OUT, 2 } } },
613 	{ .name = "linux_lseek", .ret_type = 2, .nargs = 3,
614 	  .args = { { Int, 0 }, { Int, 1 }, { Whence, 2 } } },
615 	{ .name = "linux_mkdir", .ret_type = 1, .nargs = 2,
616 	  .args = { { Name | IN, 0 }, { Int, 1 } } },
617 	{ .name = "linux_newfstat", .ret_type = 1, .nargs = 2,
618 	  .args = { { Int, 0 }, { Ptr | OUT, 1 } } },
619 	{ .name = "linux_newstat", .ret_type = 1, .nargs = 2,
620 	  .args = { { Name | IN, 0 }, { Ptr | OUT, 1 } } },
621 	{ .name = "linux_open", .ret_type = 1, .nargs = 3,
622 	  .args = { { Name, 0 }, { Hex, 1 }, { Octal, 2 } } },
623 	{ .name = "linux_readlink", .ret_type = 1, .nargs = 3,
624 	  .args = { { Name, 0 }, { Name | OUT, 1 }, { Sizet, 2 } } },
625 	{ .name = "linux_setitimer", .ret_type = 1, .nargs = 3,
626 	  .args = { { Itimerwhich, 0 }, { Itimerval, 1 },
627 		    { Itimerval | OUT, 2 } } },
628 	{ .name = "linux_socketcall", .ret_type = 1, .nargs = 2,
629 	  .args = { { Int, 0 }, { LinuxSockArgs, 1 } } },
630 	{ .name = "linux_stat64", .ret_type = 1, .nargs = 2,
631 	  .args = { { Name | IN, 0 }, { Ptr | OUT, 1 } } },
632 };
633 static STAILQ_HEAD(, syscall) seen_syscalls;
634 
635 /* Xlat idea taken from strace */
636 struct xlat {
637 	int val;
638 	const char *str;
639 };
640 
641 #define	X(a)	{ a, #a },
642 #define	XEND	{ 0, NULL }
643 
644 static struct xlat poll_flags[] = {
645 	X(POLLSTANDARD) X(POLLIN) X(POLLPRI) X(POLLOUT) X(POLLERR)
646 	X(POLLHUP) X(POLLNVAL) X(POLLRDNORM) X(POLLRDBAND)
647 	X(POLLWRBAND) X(POLLINIGNEOF) X(POLLRDHUP) XEND
648 };
649 
650 static struct xlat sigaction_flags[] = {
651 	X(SA_ONSTACK) X(SA_RESTART) X(SA_RESETHAND) X(SA_NOCLDSTOP)
652 	X(SA_NODEFER) X(SA_NOCLDWAIT) X(SA_SIGINFO) XEND
653 };
654 
655 static struct xlat linux_socketcall_ops[] = {
656 	X(LINUX_SOCKET) X(LINUX_BIND) X(LINUX_CONNECT) X(LINUX_LISTEN)
657 	X(LINUX_ACCEPT) X(LINUX_GETSOCKNAME) X(LINUX_GETPEERNAME)
658 	X(LINUX_SOCKETPAIR) X(LINUX_SEND) X(LINUX_RECV) X(LINUX_SENDTO)
659 	X(LINUX_RECVFROM) X(LINUX_SHUTDOWN) X(LINUX_SETSOCKOPT)
660 	X(LINUX_GETSOCKOPT) X(LINUX_SENDMSG) X(LINUX_RECVMSG)
661 	XEND
662 };
663 
664 static struct xlat lio_modes[] = {
665 	X(LIO_WAIT) X(LIO_NOWAIT)
666 	XEND
667 };
668 
669 static struct xlat lio_opcodes[] = {
670 	X(LIO_WRITE) X(LIO_READ) X(LIO_READV) X(LIO_WRITEV) X(LIO_NOP)
671 	XEND
672 };
673 
674 static struct xlat aio_fsync_ops[] = {
675 	X(O_SYNC)
676 	XEND
677 };
678 
679 #undef X
680 #undef XEND
681 
682 /*
683  * Searches an xlat array for a value, and returns it if found.  Otherwise
684  * return a string representation.
685  */
686 static const char *
687 lookup(struct xlat *xlat, int val, int base)
688 {
689 	static char tmp[16];
690 
691 	for (; xlat->str != NULL; xlat++)
692 		if (xlat->val == val)
693 			return (xlat->str);
694 	switch (base) {
695 	case 8:
696 		sprintf(tmp, "0%o", val);
697 		break;
698 	case 16:
699 		sprintf(tmp, "0x%x", val);
700 		break;
701 	case 10:
702 		sprintf(tmp, "%u", val);
703 		break;
704 	default:
705 		errx(1, "Unknown lookup base");
706 	}
707 	return (tmp);
708 }
709 
710 static const char *
711 xlookup(struct xlat *xlat, int val)
712 {
713 
714 	return (lookup(xlat, val, 16));
715 }
716 
717 /*
718  * Searches an xlat array containing bitfield values.  Remaining bits
719  * set after removing the known ones are printed at the end:
720  * IN|0x400.
721  */
722 static char *
723 xlookup_bits(struct xlat *xlat, int val)
724 {
725 	int len, rem;
726 	static char str[512];
727 
728 	len = 0;
729 	rem = val;
730 	for (; xlat->str != NULL; xlat++) {
731 		if ((xlat->val & rem) == xlat->val) {
732 			/*
733 			 * Don't print the "all-bits-zero" string unless all
734 			 * bits are really zero.
735 			 */
736 			if (xlat->val == 0 && val != 0)
737 				continue;
738 			len += sprintf(str + len, "%s|", xlat->str);
739 			rem &= ~(xlat->val);
740 		}
741 	}
742 
743 	/*
744 	 * If we have leftover bits or didn't match anything, print
745 	 * the remainder.
746 	 */
747 	if (rem || len == 0)
748 		len += sprintf(str + len, "0x%x", rem);
749 	if (len && str[len - 1] == '|')
750 		len--;
751 	str[len] = 0;
752 	return (str);
753 }
754 
755 static void
756 print_integer_arg(const char *(*decoder)(int), FILE *fp, int value)
757 {
758 	const char *str;
759 
760 	str = decoder(value);
761 	if (str != NULL)
762 		fputs(str, fp);
763 	else
764 		fprintf(fp, "%d", value);
765 }
766 
767 static bool
768 print_mask_arg_part(bool (*decoder)(FILE *, int, int *), FILE *fp, int value,
769     int *rem)
770 {
771 
772 	return (decoder(fp, value, rem));
773 }
774 
775 static void
776 print_mask_arg(bool (*decoder)(FILE *, int, int *), FILE *fp, int value)
777 {
778 	int rem;
779 
780 	if (!print_mask_arg_part(decoder, fp, value, &rem))
781 		fprintf(fp, "0x%x", rem);
782 	else if (rem != 0)
783 		fprintf(fp, "|0x%x", rem);
784 }
785 
786 static void
787 print_mask_arg32(bool (*decoder)(FILE *, uint32_t, uint32_t *), FILE *fp,
788     uint32_t value)
789 {
790 	uint32_t rem;
791 
792 	if (!decoder(fp, value, &rem))
793 		fprintf(fp, "0x%x", rem);
794 	else if (rem != 0)
795 		fprintf(fp, "|0x%x", rem);
796 }
797 
798 /*
799  * Add argument padding to subsequent system calls after Quad
800  * syscall arguments as needed.  This used to be done by hand in the
801  * decoded_syscalls table which was ugly and error prone.  It is
802  * simpler to do the fixup of offsets at initialization time than when
803  * decoding arguments.
804  */
805 static void
806 quad_fixup(struct syscall_decode *sc)
807 {
808 	int offset, prev;
809 	u_int i;
810 
811 	offset = 0;
812 	prev = -1;
813 	for (i = 0; i < sc->nargs; i++) {
814 		/* This arg type is a dummy that doesn't use offset. */
815 		if ((sc->args[i].type & ARG_MASK) == PipeFds)
816 			continue;
817 
818 		assert(prev < sc->args[i].offset);
819 		prev = sc->args[i].offset;
820 		sc->args[i].offset += offset;
821 		switch (sc->args[i].type & ARG_MASK) {
822 		case Quad:
823 		case QuadHex:
824 #if defined(__powerpc__) || defined(__arm__) || defined(__aarch64__)
825 			/*
826 			 * 64-bit arguments on 32-bit powerpc and arm must be
827 			 * 64-bit aligned.  If the current offset is
828 			 * not aligned, the calling convention inserts
829 			 * a 32-bit pad argument that should be skipped.
830 			 */
831 			if (sc->args[i].offset % 2 == 1) {
832 				sc->args[i].offset++;
833 				offset++;
834 			}
835 #endif
836 			offset++;
837 		default:
838 			break;
839 		}
840 	}
841 }
842 
843 static struct syscall *
844 find_syscall(struct procabi *abi, u_int number)
845 {
846 	struct extra_syscall *es;
847 
848 	if (number < nitems(abi->syscalls))
849 		return (abi->syscalls[number]);
850 	STAILQ_FOREACH(es, &abi->extra_syscalls, entries) {
851 		if (es->number == number)
852 			return (es->sc);
853 	}
854 	return (NULL);
855 }
856 
857 static void
858 add_syscall(struct procabi *abi, u_int number, struct syscall *sc)
859 {
860 	struct extra_syscall *es;
861 
862 	/*
863 	 * quad_fixup() is currently needed for all 32-bit ABIs.
864 	 * TODO: This should probably be a function pointer inside struct
865 	 *  procabi instead.
866 	 */
867 	if (abi->pointer_size == 4)
868 		quad_fixup(&sc->decode);
869 
870 	if (number < nitems(abi->syscalls)) {
871 		assert(abi->syscalls[number] == NULL);
872 		abi->syscalls[number] = sc;
873 	} else {
874 		es = malloc(sizeof(*es));
875 		es->sc = sc;
876 		es->number = number;
877 		STAILQ_INSERT_TAIL(&abi->extra_syscalls, es, entries);
878 	}
879 
880 	STAILQ_INSERT_HEAD(&seen_syscalls, sc, entries);
881 }
882 
883 /*
884  * If/when the list gets big, it might be desirable to do it
885  * as a hash table or binary search.
886  */
887 struct syscall *
888 get_syscall(struct threadinfo *t, u_int number, u_int nargs)
889 {
890 	struct syscall *sc;
891 	struct procabi *procabi;
892 	const char *sysdecode_name;
893 	const char *lookup_name;
894 	const char *name;
895 	u_int i;
896 
897 	procabi = t->proc->abi;
898 	sc = find_syscall(procabi, number);
899 	if (sc != NULL)
900 		return (sc);
901 
902 	/* Memory is not explicitly deallocated, it's released on exit(). */
903 	sysdecode_name = sysdecode_syscallname(procabi->abi, number);
904 	if (sysdecode_name == NULL)
905 		asprintf(__DECONST(char **, &name), "#%d", number);
906 	else
907 		name = sysdecode_name;
908 
909 	sc = calloc(1, sizeof(*sc));
910 	sc->name = name;
911 
912 	/* Also decode compat syscalls arguments by stripping the prefix. */
913 	lookup_name = name;
914 	if (procabi->compat_prefix != NULL && strncmp(procabi->compat_prefix,
915 	    name, strlen(procabi->compat_prefix)) == 0)
916 		lookup_name += strlen(procabi->compat_prefix);
917 
918 	for (i = 0; i < nitems(decoded_syscalls); i++) {
919 		if (strcmp(lookup_name, decoded_syscalls[i].name) == 0) {
920 			sc->decode = decoded_syscalls[i];
921 			add_syscall(t->proc->abi, number, sc);
922 			return (sc);
923 		}
924 	}
925 
926 	/* It is unknown.  Add it into the list. */
927 #if DEBUG
928 	fprintf(stderr, "unknown syscall %s -- setting args to %d\n", name,
929 	    nargs);
930 #endif
931 	sc->unknown = sysdecode_name == NULL;
932 	sc->decode.ret_type = 1; /* Assume 1 return value. */
933 	sc->decode.nargs = nargs;
934 	for (i = 0; i < nargs; i++) {
935 		sc->decode.args[i].offset = i;
936 		/* Treat all unknown arguments as LongHex. */
937 		sc->decode.args[i].type = LongHex;
938 	}
939 	add_syscall(t->proc->abi, number, sc);
940 	return (sc);
941 }
942 
943 /*
944  * Copy a fixed amount of bytes from the process.
945  */
946 static int
947 get_struct(pid_t pid, psaddr_t offset, void *buf, size_t len)
948 {
949 	struct ptrace_io_desc iorequest;
950 
951 	iorequest.piod_op = PIOD_READ_D;
952 	iorequest.piod_offs = (void *)(uintptr_t)offset;
953 	iorequest.piod_addr = buf;
954 	iorequest.piod_len = len;
955 	if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0)
956 		return (-1);
957 	return (0);
958 }
959 
960 #define	MAXSIZE		4096
961 
962 /*
963  * Copy a string from the process.  Note that it is
964  * expected to be a C string, but if max is set, it will
965  * only get that much.
966  */
967 static char *
968 get_string(pid_t pid, psaddr_t addr, int max)
969 {
970 	struct ptrace_io_desc iorequest;
971 	char *buf, *nbuf;
972 	size_t offset, size, totalsize;
973 
974 	offset = 0;
975 	if (max)
976 		size = max + 1;
977 	else {
978 		/* Read up to the end of the current page. */
979 		size = PAGE_SIZE - (addr % PAGE_SIZE);
980 		if (size > MAXSIZE)
981 			size = MAXSIZE;
982 	}
983 	totalsize = size;
984 	buf = malloc(totalsize);
985 	if (buf == NULL)
986 		return (NULL);
987 	for (;;) {
988 		iorequest.piod_op = PIOD_READ_D;
989 		iorequest.piod_offs = (void *)((uintptr_t)addr + offset);
990 		iorequest.piod_addr = buf + offset;
991 		iorequest.piod_len = size;
992 		if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0) {
993 			free(buf);
994 			return (NULL);
995 		}
996 		if (memchr(buf + offset, '\0', size) != NULL)
997 			return (buf);
998 		offset += size;
999 		if (totalsize < MAXSIZE && max == 0) {
1000 			size = MAXSIZE - totalsize;
1001 			if (size > PAGE_SIZE)
1002 				size = PAGE_SIZE;
1003 			nbuf = realloc(buf, totalsize + size);
1004 			if (nbuf == NULL) {
1005 				buf[totalsize - 1] = '\0';
1006 				return (buf);
1007 			}
1008 			buf = nbuf;
1009 			totalsize += size;
1010 		} else {
1011 			buf[totalsize - 1] = '\0';
1012 			return (buf);
1013 		}
1014 	}
1015 }
1016 
1017 static const char *
1018 strsig2(int sig)
1019 {
1020 	static char tmp[32];
1021 	const char *signame;
1022 
1023 	signame = sysdecode_signal(sig);
1024 	if (signame == NULL) {
1025 		snprintf(tmp, sizeof(tmp), "%d", sig);
1026 		signame = tmp;
1027 	}
1028 	return (signame);
1029 }
1030 
1031 static void
1032 print_kevent(FILE *fp, struct kevent *ke)
1033 {
1034 
1035 	switch (ke->filter) {
1036 	case EVFILT_READ:
1037 	case EVFILT_WRITE:
1038 	case EVFILT_VNODE:
1039 	case EVFILT_PROC:
1040 	case EVFILT_TIMER:
1041 	case EVFILT_PROCDESC:
1042 	case EVFILT_EMPTY:
1043 		fprintf(fp, "%ju", (uintmax_t)ke->ident);
1044 		break;
1045 	case EVFILT_SIGNAL:
1046 		fputs(strsig2(ke->ident), fp);
1047 		break;
1048 	default:
1049 		fprintf(fp, "%p", (void *)ke->ident);
1050 	}
1051 	fprintf(fp, ",");
1052 	print_integer_arg(sysdecode_kevent_filter, fp, ke->filter);
1053 	fprintf(fp, ",");
1054 	print_mask_arg(sysdecode_kevent_flags, fp, ke->flags);
1055 	fprintf(fp, ",");
1056 	sysdecode_kevent_fflags(fp, ke->filter, ke->fflags, 16);
1057 	fprintf(fp, ",%#jx,%p", (uintmax_t)ke->data, ke->udata);
1058 }
1059 
1060 static void
1061 print_utrace(FILE *fp, void *utrace_addr, size_t len)
1062 {
1063 	unsigned char *utrace_buffer;
1064 
1065 	fprintf(fp, "{ ");
1066 	if (sysdecode_utrace(fp, utrace_addr, len)) {
1067 		fprintf(fp, " }");
1068 		return;
1069 	}
1070 
1071 	utrace_buffer = utrace_addr;
1072 	fprintf(fp, "%zu:", len);
1073 	while (len--)
1074 		fprintf(fp, " %02x", *utrace_buffer++);
1075 	fprintf(fp, " }");
1076 }
1077 
1078 static void
1079 print_pointer(FILE *fp, uintptr_t arg)
1080 {
1081 
1082 	fprintf(fp, "%p", (void *)arg);
1083 }
1084 
1085 static void
1086 print_sockaddr(FILE *fp, struct trussinfo *trussinfo, uintptr_t arg,
1087     socklen_t len)
1088 {
1089 	char addr[64];
1090 	struct sockaddr_in *lsin;
1091 	struct sockaddr_in6 *lsin6;
1092 	struct sockaddr_un *sun;
1093 	struct sockaddr *sa;
1094 	u_char *q;
1095 	pid_t pid = trussinfo->curthread->proc->pid;
1096 
1097 	if (arg == 0) {
1098 		fputs("NULL", fp);
1099 		return;
1100 	}
1101 	/* If the length is too small, just bail. */
1102 	if (len < sizeof(*sa)) {
1103 		print_pointer(fp, arg);
1104 		return;
1105 	}
1106 
1107 	sa = calloc(1, len);
1108 	if (get_struct(pid, arg, sa, len) == -1) {
1109 		free(sa);
1110 		print_pointer(fp, arg);
1111 		return;
1112 	}
1113 
1114 	switch (sa->sa_family) {
1115 	case AF_INET:
1116 		if (len < sizeof(*lsin))
1117 			goto sockaddr_short;
1118 		lsin = (struct sockaddr_in *)(void *)sa;
1119 		inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof(addr));
1120 		fprintf(fp, "{ AF_INET %s:%d }", addr,
1121 		    htons(lsin->sin_port));
1122 		break;
1123 	case AF_INET6:
1124 		if (len < sizeof(*lsin6))
1125 			goto sockaddr_short;
1126 		lsin6 = (struct sockaddr_in6 *)(void *)sa;
1127 		inet_ntop(AF_INET6, &lsin6->sin6_addr, addr,
1128 		    sizeof(addr));
1129 		fprintf(fp, "{ AF_INET6 [%s]:%d }", addr,
1130 		    htons(lsin6->sin6_port));
1131 		break;
1132 	case AF_UNIX:
1133 		sun = (struct sockaddr_un *)sa;
1134 		fprintf(fp, "{ AF_UNIX \"%.*s\" }",
1135 		    (int)(len - offsetof(struct sockaddr_un, sun_path)),
1136 		    sun->sun_path);
1137 		break;
1138 	default:
1139 	sockaddr_short:
1140 		fprintf(fp,
1141 		    "{ sa_len = %d, sa_family = %d, sa_data = {",
1142 		    (int)sa->sa_len, (int)sa->sa_family);
1143 		for (q = (u_char *)sa->sa_data;
1144 		     q < (u_char *)sa + len; q++)
1145 			fprintf(fp, "%s 0x%02x",
1146 			    q == (u_char *)sa->sa_data ? "" : ",",
1147 			    *q);
1148 		fputs(" } }", fp);
1149 	}
1150 	free(sa);
1151 }
1152 
1153 #define IOV_LIMIT 16
1154 
1155 static void
1156 print_iovec(FILE *fp, struct trussinfo *trussinfo, uintptr_t arg, int iovcnt)
1157 {
1158 	struct iovec iov[IOV_LIMIT];
1159 	size_t max_string = trussinfo->strsize;
1160 	char tmp2[max_string + 1], *tmp3;
1161 	size_t len;
1162 	pid_t pid = trussinfo->curthread->proc->pid;
1163 	int i;
1164 	bool buf_truncated, iov_truncated;
1165 
1166 	if (iovcnt <= 0) {
1167 		print_pointer(fp, arg);
1168 		return;
1169 	}
1170 	if (iovcnt > IOV_LIMIT) {
1171 		iovcnt = IOV_LIMIT;
1172 		iov_truncated = true;
1173 	} else {
1174 		iov_truncated = false;
1175 	}
1176 	if (get_struct(pid, arg, &iov, iovcnt * sizeof(struct iovec)) == -1) {
1177 		print_pointer(fp, arg);
1178 		return;
1179 	}
1180 
1181 	fputs("[", fp);
1182 	for (i = 0; i < iovcnt; i++) {
1183 		len = iov[i].iov_len;
1184 		if (len > max_string) {
1185 			len = max_string;
1186 			buf_truncated = true;
1187 		} else {
1188 			buf_truncated = false;
1189 		}
1190 		fprintf(fp, "%s{", (i > 0) ? "," : "");
1191 		if (len && get_struct(pid, (uintptr_t)iov[i].iov_base, &tmp2, len) != -1) {
1192 			tmp3 = malloc(len * 4 + 1);
1193 			while (len) {
1194 				if (strvisx(tmp3, tmp2, len,
1195 				    VIS_CSTYLE|VIS_TAB|VIS_NL) <=
1196 				    (int)max_string)
1197 					break;
1198 				len--;
1199 				buf_truncated = true;
1200 			}
1201 			fprintf(fp, "\"%s\"%s", tmp3,
1202 			    buf_truncated ? "..." : "");
1203 			free(tmp3);
1204 		} else {
1205 			print_pointer(fp, (uintptr_t)iov[i].iov_base);
1206 		}
1207 		fprintf(fp, ",%zu}", iov[i].iov_len);
1208 	}
1209 	fprintf(fp, "%s%s", iov_truncated ? ",..." : "", "]");
1210 }
1211 
1212 static void
1213 print_sigval(FILE *fp, union sigval *sv)
1214 {
1215 	fprintf(fp, "{ %d, %p }", sv->sival_int, sv->sival_ptr);
1216 }
1217 
1218 static void
1219 print_sigevent(FILE *fp, struct sigevent *se)
1220 {
1221 	fputs("{ sigev_notify=", fp);
1222 	switch (se->sigev_notify) {
1223 	case SIGEV_NONE:
1224 		fputs("SIGEV_NONE", fp);
1225 		break;
1226 	case SIGEV_SIGNAL:
1227 		fprintf(fp, "SIGEV_SIGNAL, sigev_signo=%s, sigev_value=",
1228 				strsig2(se->sigev_signo));
1229 		print_sigval(fp, &se->sigev_value);
1230 		break;
1231 	case SIGEV_THREAD:
1232 		fputs("SIGEV_THREAD, sigev_value=", fp);
1233 		print_sigval(fp, &se->sigev_value);
1234 		break;
1235 	case SIGEV_KEVENT:
1236 		fprintf(fp, "SIGEV_KEVENT, sigev_notify_kqueue=%d, sigev_notify_kevent_flags=",
1237 				se->sigev_notify_kqueue);
1238 		print_mask_arg(sysdecode_kevent_flags, fp, se->sigev_notify_kevent_flags);
1239 		break;
1240 	case SIGEV_THREAD_ID:
1241 		fprintf(fp, "SIGEV_THREAD_ID, sigev_notify_thread_id=%d, sigev_signo=%s, sigev_value=",
1242 				se->sigev_notify_thread_id, strsig2(se->sigev_signo));
1243 		print_sigval(fp, &se->sigev_value);
1244 		break;
1245 	default:
1246 		fprintf(fp, "%d", se->sigev_notify);
1247 		break;
1248 	}
1249 	fputs(" }", fp);
1250 }
1251 
1252 static void
1253 print_aiocb(FILE *fp, struct aiocb *cb)
1254 {
1255 	fprintf(fp, "{ %d,%jd,%p,%zu,%s,",
1256 			cb->aio_fildes,
1257 			cb->aio_offset,
1258 			cb->aio_buf,
1259 			cb->aio_nbytes,
1260 			xlookup(lio_opcodes, cb->aio_lio_opcode));
1261 	print_sigevent(fp, &cb->aio_sigevent);
1262 	fputs(" }", fp);
1263 }
1264 
1265 static void
1266 print_gen_cmsg(FILE *fp, struct cmsghdr *cmsghdr)
1267 {
1268 	u_char *q;
1269 
1270 	fputs("{", fp);
1271 	for (q = CMSG_DATA(cmsghdr);
1272 	     q < (u_char *)cmsghdr + cmsghdr->cmsg_len; q++) {
1273 		fprintf(fp, "%s0x%02x", q == CMSG_DATA(cmsghdr) ? "" : ",", *q);
1274 	}
1275 	fputs("}", fp);
1276 }
1277 
1278 static void
1279 print_sctp_initmsg(FILE *fp, struct sctp_initmsg *init)
1280 {
1281 	fprintf(fp, "{out=%u,", init->sinit_num_ostreams);
1282 	fprintf(fp, "in=%u,", init->sinit_max_instreams);
1283 	fprintf(fp, "max_rtx=%u,", init->sinit_max_attempts);
1284 	fprintf(fp, "max_rto=%u}", init->sinit_max_init_timeo);
1285 }
1286 
1287 static void
1288 print_sctp_sndrcvinfo(FILE *fp, bool receive, struct sctp_sndrcvinfo *info)
1289 {
1290 	fprintf(fp, "{sid=%u,", info->sinfo_stream);
1291 	if (receive) {
1292 		fprintf(fp, "ssn=%u,", info->sinfo_ssn);
1293 	}
1294 	fputs("flgs=", fp);
1295 	sysdecode_sctp_sinfo_flags(fp, info->sinfo_flags);
1296 	fprintf(fp, ",ppid=%u,", ntohl(info->sinfo_ppid));
1297 	if (!receive) {
1298 		fprintf(fp, "ctx=%u,", info->sinfo_context);
1299 		fprintf(fp, "ttl=%u,", info->sinfo_timetolive);
1300 	}
1301 	if (receive) {
1302 		fprintf(fp, "tsn=%u,", info->sinfo_tsn);
1303 		fprintf(fp, "cumtsn=%u,", info->sinfo_cumtsn);
1304 	}
1305 	fprintf(fp, "id=%u}", info->sinfo_assoc_id);
1306 }
1307 
1308 static void
1309 print_sctp_sndinfo(FILE *fp, struct sctp_sndinfo *info)
1310 {
1311 	fprintf(fp, "{sid=%u,", info->snd_sid);
1312 	fputs("flgs=", fp);
1313 	print_mask_arg(sysdecode_sctp_snd_flags, fp, info->snd_flags);
1314 	fprintf(fp, ",ppid=%u,", ntohl(info->snd_ppid));
1315 	fprintf(fp, "ctx=%u,", info->snd_context);
1316 	fprintf(fp, "id=%u}", info->snd_assoc_id);
1317 }
1318 
1319 static void
1320 print_sctp_rcvinfo(FILE *fp, struct sctp_rcvinfo *info)
1321 {
1322 	fprintf(fp, "{sid=%u,", info->rcv_sid);
1323 	fprintf(fp, "ssn=%u,", info->rcv_ssn);
1324 	fputs("flgs=", fp);
1325 	print_mask_arg(sysdecode_sctp_rcv_flags, fp, info->rcv_flags);
1326 	fprintf(fp, ",ppid=%u,", ntohl(info->rcv_ppid));
1327 	fprintf(fp, "tsn=%u,", info->rcv_tsn);
1328 	fprintf(fp, "cumtsn=%u,", info->rcv_cumtsn);
1329 	fprintf(fp, "ctx=%u,", info->rcv_context);
1330 	fprintf(fp, "id=%u}", info->rcv_assoc_id);
1331 }
1332 
1333 static void
1334 print_sctp_nxtinfo(FILE *fp, struct sctp_nxtinfo *info)
1335 {
1336 	fprintf(fp, "{sid=%u,", info->nxt_sid);
1337 	fputs("flgs=", fp);
1338 	print_mask_arg(sysdecode_sctp_nxt_flags, fp, info->nxt_flags);
1339 	fprintf(fp, ",ppid=%u,", ntohl(info->nxt_ppid));
1340 	fprintf(fp, "len=%u,", info->nxt_length);
1341 	fprintf(fp, "id=%u}", info->nxt_assoc_id);
1342 }
1343 
1344 static void
1345 print_sctp_prinfo(FILE *fp, struct sctp_prinfo *info)
1346 {
1347 	fputs("{pol=", fp);
1348 	print_integer_arg(sysdecode_sctp_pr_policy, fp, info->pr_policy);
1349 	fprintf(fp, ",val=%u}", info->pr_value);
1350 }
1351 
1352 static void
1353 print_sctp_authinfo(FILE *fp, struct sctp_authinfo *info)
1354 {
1355 	fprintf(fp, "{num=%u}", info->auth_keynumber);
1356 }
1357 
1358 static void
1359 print_sctp_ipv4_addr(FILE *fp, struct in_addr *addr)
1360 {
1361 	char buf[INET_ADDRSTRLEN];
1362 	const char *s;
1363 
1364 	s = inet_ntop(AF_INET, addr, buf, INET_ADDRSTRLEN);
1365 	if (s != NULL)
1366 		fprintf(fp, "{addr=%s}", s);
1367 	else
1368 		fputs("{addr=???}", fp);
1369 }
1370 
1371 static void
1372 print_sctp_ipv6_addr(FILE *fp, struct in6_addr *addr)
1373 {
1374 	char buf[INET6_ADDRSTRLEN];
1375 	const char *s;
1376 
1377 	s = inet_ntop(AF_INET6, addr, buf, INET6_ADDRSTRLEN);
1378 	if (s != NULL)
1379 		fprintf(fp, "{addr=%s}", s);
1380 	else
1381 		fputs("{addr=???}", fp);
1382 }
1383 
1384 static void
1385 print_sctp_cmsg(FILE *fp, bool receive, struct cmsghdr *cmsghdr)
1386 {
1387 	void *data;
1388 	socklen_t len;
1389 
1390 	len = cmsghdr->cmsg_len;
1391 	data = CMSG_DATA(cmsghdr);
1392 	switch (cmsghdr->cmsg_type) {
1393 	case SCTP_INIT:
1394 		if (len == CMSG_LEN(sizeof(struct sctp_initmsg)))
1395 			print_sctp_initmsg(fp, (struct sctp_initmsg *)data);
1396 		else
1397 			print_gen_cmsg(fp, cmsghdr);
1398 		break;
1399 	case SCTP_SNDRCV:
1400 		if (len == CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
1401 			print_sctp_sndrcvinfo(fp, receive,
1402 			    (struct sctp_sndrcvinfo *)data);
1403 		else
1404 			print_gen_cmsg(fp, cmsghdr);
1405 		break;
1406 #if 0
1407 	case SCTP_EXTRCV:
1408 		if (len == CMSG_LEN(sizeof(struct sctp_extrcvinfo)))
1409 			print_sctp_extrcvinfo(fp,
1410 			    (struct sctp_extrcvinfo *)data);
1411 		else
1412 			print_gen_cmsg(fp, cmsghdr);
1413 		break;
1414 #endif
1415 	case SCTP_SNDINFO:
1416 		if (len == CMSG_LEN(sizeof(struct sctp_sndinfo)))
1417 			print_sctp_sndinfo(fp, (struct sctp_sndinfo *)data);
1418 		else
1419 			print_gen_cmsg(fp, cmsghdr);
1420 		break;
1421 	case SCTP_RCVINFO:
1422 		if (len == CMSG_LEN(sizeof(struct sctp_rcvinfo)))
1423 			print_sctp_rcvinfo(fp, (struct sctp_rcvinfo *)data);
1424 		else
1425 			print_gen_cmsg(fp, cmsghdr);
1426 		break;
1427 	case SCTP_NXTINFO:
1428 		if (len == CMSG_LEN(sizeof(struct sctp_nxtinfo)))
1429 			print_sctp_nxtinfo(fp, (struct sctp_nxtinfo *)data);
1430 		else
1431 			print_gen_cmsg(fp, cmsghdr);
1432 		break;
1433 	case SCTP_PRINFO:
1434 		if (len == CMSG_LEN(sizeof(struct sctp_prinfo)))
1435 			print_sctp_prinfo(fp, (struct sctp_prinfo *)data);
1436 		else
1437 			print_gen_cmsg(fp, cmsghdr);
1438 		break;
1439 	case SCTP_AUTHINFO:
1440 		if (len == CMSG_LEN(sizeof(struct sctp_authinfo)))
1441 			print_sctp_authinfo(fp, (struct sctp_authinfo *)data);
1442 		else
1443 			print_gen_cmsg(fp, cmsghdr);
1444 		break;
1445 	case SCTP_DSTADDRV4:
1446 		if (len == CMSG_LEN(sizeof(struct in_addr)))
1447 			print_sctp_ipv4_addr(fp, (struct in_addr *)data);
1448 		else
1449 			print_gen_cmsg(fp, cmsghdr);
1450 		break;
1451 	case SCTP_DSTADDRV6:
1452 		if (len == CMSG_LEN(sizeof(struct in6_addr)))
1453 			print_sctp_ipv6_addr(fp, (struct in6_addr *)data);
1454 		else
1455 			print_gen_cmsg(fp, cmsghdr);
1456 		break;
1457 	default:
1458 		print_gen_cmsg(fp, cmsghdr);
1459 	}
1460 }
1461 
1462 static void
1463 print_cmsgs(FILE *fp, pid_t pid, bool receive, struct msghdr *msghdr)
1464 {
1465 	struct cmsghdr *cmsghdr;
1466 	char *cmsgbuf;
1467 	const char *temp;
1468 	socklen_t len;
1469 	int level, type;
1470 	bool first;
1471 
1472 	len = msghdr->msg_controllen;
1473 	if (len == 0) {
1474 		fputs("{}", fp);
1475 		return;
1476 	}
1477 	cmsgbuf = calloc(1, len);
1478 	if (get_struct(pid, (uintptr_t)msghdr->msg_control, cmsgbuf, len) == -1) {
1479 		print_pointer(fp, (uintptr_t)msghdr->msg_control);
1480 		free(cmsgbuf);
1481 		return;
1482 	}
1483 	msghdr->msg_control = cmsgbuf;
1484 	first = true;
1485 	fputs("{", fp);
1486 	for (cmsghdr = CMSG_FIRSTHDR(msghdr);
1487 	   cmsghdr != NULL;
1488 	   cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr)) {
1489 		if (cmsghdr->cmsg_len < sizeof(*cmsghdr)) {
1490 			fprintf(fp, "{<invalid cmsg, len=%u>}",
1491 			    cmsghdr->cmsg_len);
1492 			if (cmsghdr->cmsg_len == 0) {
1493 				/* Avoid looping forever. */
1494 				break;
1495 			}
1496 			continue;
1497 		}
1498 
1499 		level = cmsghdr->cmsg_level;
1500 		type = cmsghdr->cmsg_type;
1501 		len = cmsghdr->cmsg_len;
1502 		fprintf(fp, "%s{level=", first ? "" : ",");
1503 		print_integer_arg(sysdecode_sockopt_level, fp, level);
1504 		fputs(",type=", fp);
1505 		temp = sysdecode_cmsg_type(level, type);
1506 		if (temp) {
1507 			fputs(temp, fp);
1508 		} else {
1509 			fprintf(fp, "%d", type);
1510 		}
1511 		fputs(",data=", fp);
1512 		switch (level) {
1513 		case IPPROTO_SCTP:
1514 			print_sctp_cmsg(fp, receive, cmsghdr);
1515 			break;
1516 		default:
1517 			print_gen_cmsg(fp, cmsghdr);
1518 			break;
1519 		}
1520 		fputs("}", fp);
1521 		first = false;
1522 	}
1523 	fputs("}", fp);
1524 	free(cmsgbuf);
1525 }
1526 
1527 static void
1528 print_sysctl_oid(FILE *fp, int *oid, size_t len)
1529 {
1530 	size_t i;
1531 	bool first;
1532 
1533 	first = true;
1534 	fprintf(fp, "{ ");
1535 	for (i = 0; i < len; i++) {
1536 		fprintf(fp, "%s%d", first ? "" : ".", oid[i]);
1537 		first = false;
1538 	}
1539 	fprintf(fp, " }");
1540 }
1541 
1542 static void
1543 print_sysctl(FILE *fp, int *oid, size_t len)
1544 {
1545 	char name[BUFSIZ];
1546 	int qoid[CTL_MAXNAME + 2];
1547 	size_t i;
1548 
1549 	qoid[0] = CTL_SYSCTL;
1550 	qoid[1] = CTL_SYSCTL_NAME;
1551 	memcpy(qoid + 2, oid, len * sizeof(int));
1552 	i = sizeof(name);
1553 	if (sysctl(qoid, len + 2, name, &i, 0, 0) == -1)
1554 		print_sysctl_oid(fp, oid, len);
1555 	else
1556 		fprintf(fp, "%s", name);
1557 }
1558 
1559 /*
1560  * Convert a 32-bit user-space pointer to psaddr_t by zero-extending.
1561  */
1562 static psaddr_t
1563 user_ptr32_to_psaddr(int32_t user_pointer)
1564 {
1565 	return ((psaddr_t)(uintptr_t)user_pointer);
1566 }
1567 
1568 /*
1569  * Converts a syscall argument into a string.  Said string is
1570  * allocated via malloc(), so needs to be free()'d.  sc is
1571  * a pointer to the syscall description (see above); args is
1572  * an array of all of the system call arguments.
1573  */
1574 char *
1575 print_arg(struct syscall_arg *sc, syscallarg_t *args, syscallarg_t *retval,
1576     struct trussinfo *trussinfo)
1577 {
1578 	FILE *fp;
1579 	char *tmp;
1580 	size_t tmplen;
1581 	pid_t pid;
1582 
1583 	fp = open_memstream(&tmp, &tmplen);
1584 	pid = trussinfo->curthread->proc->pid;
1585 	switch (sc->type & ARG_MASK) {
1586 	case Hex:
1587 		fprintf(fp, "0x%x", (int)args[sc->offset]);
1588 		break;
1589 	case Octal:
1590 		fprintf(fp, "0%o", (int)args[sc->offset]);
1591 		break;
1592 	case Int:
1593 		fprintf(fp, "%d", (int)args[sc->offset]);
1594 		break;
1595 	case UInt:
1596 		fprintf(fp, "%u", (unsigned int)args[sc->offset]);
1597 		break;
1598 	case PUInt: {
1599 		unsigned int val;
1600 
1601 		if (get_struct(pid, args[sc->offset], &val,
1602 		    sizeof(val)) == 0)
1603 			fprintf(fp, "{ %u }", val);
1604 		else
1605 			print_pointer(fp, args[sc->offset]);
1606 		break;
1607 	}
1608 	case LongHex:
1609 		fprintf(fp, "0x%lx", (long)args[sc->offset]);
1610 		break;
1611 	case Long:
1612 		fprintf(fp, "%ld", (long)args[sc->offset]);
1613 		break;
1614 	case Sizet:
1615 		fprintf(fp, "%zu", (size_t)args[sc->offset]);
1616 		break;
1617 	case ShmName:
1618 		/* Handle special SHM_ANON value. */
1619 		if ((char *)(uintptr_t)args[sc->offset] == SHM_ANON) {
1620 			fprintf(fp, "SHM_ANON");
1621 			break;
1622 		}
1623 		/* FALLTHROUGH */
1624 	case Name: {
1625 		/* NULL-terminated string. */
1626 		char *tmp2;
1627 
1628 		tmp2 = get_string(pid, args[sc->offset], 0);
1629 		fprintf(fp, "\"%s\"", tmp2);
1630 		free(tmp2);
1631 		break;
1632 	}
1633 	case BinString: {
1634 		/*
1635 		 * Binary block of data that might have printable characters.
1636 		 * XXX If type|OUT, assume that the length is the syscall's
1637 		 * return value.  Otherwise, assume that the length of the block
1638 		 * is in the next syscall argument.
1639 		 */
1640 		int max_string = trussinfo->strsize;
1641 		char tmp2[max_string + 1], *tmp3;
1642 		int len;
1643 		int truncated = 0;
1644 
1645 		if (sc->type & OUT)
1646 			len = retval[0];
1647 		else
1648 			len = args[sc->offset + 1];
1649 
1650 		/*
1651 		 * Don't print more than max_string characters, to avoid word
1652 		 * wrap.  If we have to truncate put some ... after the string.
1653 		 */
1654 		if (len > max_string) {
1655 			len = max_string;
1656 			truncated = 1;
1657 		}
1658 		if (len && get_struct(pid, args[sc->offset], &tmp2, len)
1659 		    != -1) {
1660 			tmp3 = malloc(len * 4 + 1);
1661 			while (len) {
1662 				if (strvisx(tmp3, tmp2, len,
1663 				    VIS_CSTYLE|VIS_TAB|VIS_NL) <= max_string)
1664 					break;
1665 				len--;
1666 				truncated = 1;
1667 			}
1668 			fprintf(fp, "\"%s\"%s", tmp3, truncated ?
1669 			    "..." : "");
1670 			free(tmp3);
1671 		} else {
1672 			print_pointer(fp, args[sc->offset]);
1673 		}
1674 		break;
1675 	}
1676 	case ExecArgs:
1677 	case ExecEnv:
1678 	case StringArray: {
1679 		psaddr_t addr;
1680 		union {
1681 			int32_t strarray32[PAGE_SIZE / sizeof(int32_t)];
1682 			int64_t strarray64[PAGE_SIZE / sizeof(int64_t)];
1683 			char buf[PAGE_SIZE];
1684 		} u;
1685 		char *string;
1686 		size_t len;
1687 		u_int first, i;
1688 		size_t pointer_size =
1689 		    trussinfo->curthread->proc->abi->pointer_size;
1690 
1691 		/*
1692 		 * Only parse argv[] and environment arrays from exec calls
1693 		 * if requested.
1694 		 */
1695 		if (((sc->type & ARG_MASK) == ExecArgs &&
1696 		    (trussinfo->flags & EXECVEARGS) == 0) ||
1697 		    ((sc->type & ARG_MASK) == ExecEnv &&
1698 		    (trussinfo->flags & EXECVEENVS) == 0)) {
1699 			print_pointer(fp, args[sc->offset]);
1700 			break;
1701 		}
1702 
1703 		/*
1704 		 * Read a page of pointers at a time.  Punt if the top-level
1705 		 * pointer is not aligned.  Note that the first read is of
1706 		 * a partial page.
1707 		 */
1708 		addr = args[sc->offset];
1709 		if (!__is_aligned(addr, pointer_size)) {
1710 			print_pointer(fp, args[sc->offset]);
1711 			break;
1712 		}
1713 
1714 		len = PAGE_SIZE - (addr & PAGE_MASK);
1715 		if (get_struct(pid, addr, u.buf, len) == -1) {
1716 			print_pointer(fp, args[sc->offset]);
1717 			break;
1718 		}
1719 		assert(len > 0);
1720 
1721 		fputc('[', fp);
1722 		first = 1;
1723 		i = 0;
1724 		for (;;) {
1725 			psaddr_t straddr;
1726 			if (pointer_size == 4) {
1727 				straddr = user_ptr32_to_psaddr(u.strarray32[i]);
1728 			} else if (pointer_size == 8) {
1729 				straddr = (psaddr_t)u.strarray64[i];
1730 			} else {
1731 				errx(1, "Unsupported pointer size: %zu",
1732 				    pointer_size);
1733 			}
1734 
1735 			/* Stop once we read the first NULL pointer. */
1736 			if (straddr == 0)
1737 				break;
1738 			string = get_string(pid, straddr, 0);
1739 			fprintf(fp, "%s \"%s\"", first ? "" : ",", string);
1740 			free(string);
1741 			first = 0;
1742 
1743 			i++;
1744 			if (i == len / pointer_size) {
1745 				addr += len;
1746 				len = PAGE_SIZE;
1747 				if (get_struct(pid, addr, u.buf, len) == -1) {
1748 					fprintf(fp, ", <inval>");
1749 					break;
1750 				}
1751 				i = 0;
1752 			}
1753 		}
1754 		fputs(" ]", fp);
1755 		break;
1756 	}
1757 	case Quad:
1758 	case QuadHex: {
1759 		uint64_t value;
1760 		size_t pointer_size =
1761 		    trussinfo->curthread->proc->abi->pointer_size;
1762 
1763 		if (pointer_size == 4) {
1764 #if _BYTE_ORDER == _LITTLE_ENDIAN
1765 			value = (uint64_t)args[sc->offset + 1] << 32 |
1766 			    args[sc->offset];
1767 #else
1768 			value = (uint64_t)args[sc->offset] << 32 |
1769 			    args[sc->offset + 1];
1770 #endif
1771 		} else {
1772 			value = (uint64_t)args[sc->offset];
1773 		}
1774 		if ((sc->type & ARG_MASK) == Quad)
1775 			fprintf(fp, "%jd", (intmax_t)value);
1776 		else
1777 			fprintf(fp, "0x%jx", (intmax_t)value);
1778 		break;
1779 	}
1780 	case PQuadHex: {
1781 		uint64_t val;
1782 
1783 		if (get_struct(pid, args[sc->offset], &val,
1784 		    sizeof(val)) == 0)
1785 			fprintf(fp, "{ 0x%jx }", (uintmax_t)val);
1786 		else
1787 			print_pointer(fp, args[sc->offset]);
1788 		break;
1789 	}
1790 	case Ptr:
1791 		print_pointer(fp, args[sc->offset]);
1792 		break;
1793 	case Readlinkres: {
1794 		char *tmp2;
1795 
1796 		if (retval[0] == -1)
1797 			break;
1798 		tmp2 = get_string(pid, args[sc->offset], retval[0]);
1799 		fprintf(fp, "\"%s\"", tmp2);
1800 		free(tmp2);
1801 		break;
1802 	}
1803 	case Ioctl: {
1804 		const char *temp;
1805 		unsigned long cmd;
1806 
1807 		cmd = args[sc->offset];
1808 		temp = sysdecode_ioctlname(cmd);
1809 		if (temp)
1810 			fputs(temp, fp);
1811 		else {
1812 			fprintf(fp, "0x%lx { IO%s%s 0x%lx('%c'), %lu, %lu }",
1813 			    cmd, cmd & IOC_OUT ? "R" : "",
1814 			    cmd & IOC_IN ? "W" : "", IOCGROUP(cmd),
1815 			    isprint(IOCGROUP(cmd)) ? (char)IOCGROUP(cmd) : '?',
1816 			    cmd & 0xFF, IOCPARM_LEN(cmd));
1817 		}
1818 		break;
1819 	}
1820 	case Timespec: {
1821 		struct timespec ts;
1822 
1823 		if (get_struct(pid, args[sc->offset], &ts, sizeof(ts)) != -1)
1824 			fprintf(fp, "{ %jd.%09ld }", (intmax_t)ts.tv_sec,
1825 			    ts.tv_nsec);
1826 		else
1827 			print_pointer(fp, args[sc->offset]);
1828 		break;
1829 	}
1830 	case Timespec2: {
1831 		struct timespec ts[2];
1832 		const char *sep;
1833 		unsigned int i;
1834 
1835 		if (get_struct(pid, args[sc->offset], &ts, sizeof(ts)) != -1) {
1836 			fputs("{ ", fp);
1837 			sep = "";
1838 			for (i = 0; i < nitems(ts); i++) {
1839 				fputs(sep, fp);
1840 				sep = ", ";
1841 				switch (ts[i].tv_nsec) {
1842 				case UTIME_NOW:
1843 					fprintf(fp, "UTIME_NOW");
1844 					break;
1845 				case UTIME_OMIT:
1846 					fprintf(fp, "UTIME_OMIT");
1847 					break;
1848 				default:
1849 					fprintf(fp, "%jd.%09ld",
1850 					    (intmax_t)ts[i].tv_sec,
1851 					    ts[i].tv_nsec);
1852 					break;
1853 				}
1854 			}
1855 			fputs(" }", fp);
1856 		} else
1857 			print_pointer(fp, args[sc->offset]);
1858 		break;
1859 	}
1860 	case Timeval: {
1861 		struct timeval tv;
1862 
1863 		if (get_struct(pid, args[sc->offset], &tv, sizeof(tv)) != -1)
1864 			fprintf(fp, "{ %jd.%06ld }", (intmax_t)tv.tv_sec,
1865 			    tv.tv_usec);
1866 		else
1867 			print_pointer(fp, args[sc->offset]);
1868 		break;
1869 	}
1870 	case Timeval2: {
1871 		struct timeval tv[2];
1872 
1873 		if (get_struct(pid, args[sc->offset], &tv, sizeof(tv)) != -1)
1874 			fprintf(fp, "{ %jd.%06ld, %jd.%06ld }",
1875 			    (intmax_t)tv[0].tv_sec, tv[0].tv_usec,
1876 			    (intmax_t)tv[1].tv_sec, tv[1].tv_usec);
1877 		else
1878 			print_pointer(fp, args[sc->offset]);
1879 		break;
1880 	}
1881 	case Itimerval: {
1882 		struct itimerval itv;
1883 
1884 		if (get_struct(pid, args[sc->offset], &itv, sizeof(itv)) != -1)
1885 			fprintf(fp, "{ %jd.%06ld, %jd.%06ld }",
1886 			    (intmax_t)itv.it_interval.tv_sec,
1887 			    itv.it_interval.tv_usec,
1888 			    (intmax_t)itv.it_value.tv_sec,
1889 			    itv.it_value.tv_usec);
1890 		else
1891 			print_pointer(fp, args[sc->offset]);
1892 		break;
1893 	}
1894 	case LinuxSockArgs:
1895 	{
1896 		struct linux_socketcall_args largs;
1897 
1898 		if (get_struct(pid, args[sc->offset], (void *)&largs,
1899 		    sizeof(largs)) != -1)
1900 			fprintf(fp, "{ %s, 0x%lx }",
1901 			    lookup(linux_socketcall_ops, largs.what, 10),
1902 			    (long unsigned int)largs.args);
1903 		else
1904 			print_pointer(fp, args[sc->offset]);
1905 		break;
1906 	}
1907 	case Pollfd: {
1908 		/*
1909 		 * XXX: A Pollfd argument expects the /next/ syscall argument
1910 		 * to be the number of fds in the array. This matches the poll
1911 		 * syscall.
1912 		 */
1913 		struct pollfd *pfd;
1914 		int numfds = args[sc->offset + 1];
1915 		size_t bytes = sizeof(struct pollfd) * numfds;
1916 		int i;
1917 
1918 		if ((pfd = malloc(bytes)) == NULL)
1919 			err(1, "Cannot malloc %zu bytes for pollfd array",
1920 			    bytes);
1921 		if (get_struct(pid, args[sc->offset], pfd, bytes) != -1) {
1922 			fputs("{", fp);
1923 			for (i = 0; i < numfds; i++) {
1924 				fprintf(fp, " %d/%s", pfd[i].fd,
1925 				    xlookup_bits(poll_flags, pfd[i].events));
1926 			}
1927 			fputs(" }", fp);
1928 		} else {
1929 			print_pointer(fp, args[sc->offset]);
1930 		}
1931 		free(pfd);
1932 		break;
1933 	}
1934 	case Fd_set: {
1935 		/*
1936 		 * XXX: A Fd_set argument expects the /first/ syscall argument
1937 		 * to be the number of fds in the array.  This matches the
1938 		 * select syscall.
1939 		 */
1940 		fd_set *fds;
1941 		int numfds = args[0];
1942 		size_t bytes = _howmany(numfds, _NFDBITS) * _NFDBITS;
1943 		int i;
1944 
1945 		if ((fds = malloc(bytes)) == NULL)
1946 			err(1, "Cannot malloc %zu bytes for fd_set array",
1947 			    bytes);
1948 		if (get_struct(pid, args[sc->offset], fds, bytes) != -1) {
1949 			fputs("{", fp);
1950 			for (i = 0; i < numfds; i++) {
1951 				if (FD_ISSET(i, fds))
1952 					fprintf(fp, " %d", i);
1953 			}
1954 			fputs(" }", fp);
1955 		} else
1956 			print_pointer(fp, args[sc->offset]);
1957 		free(fds);
1958 		break;
1959 	}
1960 	case Signal:
1961 		fputs(strsig2(args[sc->offset]), fp);
1962 		break;
1963 	case Sigset: {
1964 		sigset_t ss;
1965 		int i, first;
1966 
1967 		if (get_struct(pid, args[sc->offset], (void *)&ss,
1968 		    sizeof(ss)) == -1) {
1969 			print_pointer(fp, args[sc->offset]);
1970 			break;
1971 		}
1972 		fputs("{ ", fp);
1973 		first = 1;
1974 		for (i = 1; i < sys_nsig; i++) {
1975 			if (sigismember(&ss, i)) {
1976 				fprintf(fp, "%s%s", !first ? "|" : "",
1977 				    strsig2(i));
1978 				first = 0;
1979 			}
1980 		}
1981 		if (!first)
1982 			fputc(' ', fp);
1983 		fputc('}', fp);
1984 		break;
1985 	}
1986 	case Sigprocmask:
1987 		print_integer_arg(sysdecode_sigprocmask_how, fp,
1988 		    args[sc->offset]);
1989 		break;
1990 	case Fcntlflag:
1991 		/* XXX: Output depends on the value of the previous argument. */
1992 		if (sysdecode_fcntl_arg_p(args[sc->offset - 1]))
1993 			sysdecode_fcntl_arg(fp, args[sc->offset - 1],
1994 			    args[sc->offset], 16);
1995 		break;
1996 	case Open:
1997 		print_mask_arg(sysdecode_open_flags, fp, args[sc->offset]);
1998 		break;
1999 	case Fcntl:
2000 		print_integer_arg(sysdecode_fcntl_cmd, fp, args[sc->offset]);
2001 		break;
2002 	case Closerangeflags:
2003 		print_mask_arg(sysdecode_close_range_flags, fp, args[sc->offset]);
2004 		break;
2005 	case Mprot:
2006 		print_mask_arg(sysdecode_mmap_prot, fp, args[sc->offset]);
2007 		break;
2008 	case Mmapflags:
2009 		print_mask_arg(sysdecode_mmap_flags, fp, args[sc->offset]);
2010 		break;
2011 	case Whence:
2012 		print_integer_arg(sysdecode_whence, fp, args[sc->offset]);
2013 		break;
2014 	case ShmFlags:
2015 		print_mask_arg(sysdecode_shmflags, fp, args[sc->offset]);
2016 		break;
2017 	case Sockdomain:
2018 		print_integer_arg(sysdecode_socketdomain, fp, args[sc->offset]);
2019 		break;
2020 	case Socktype:
2021 		print_mask_arg(sysdecode_socket_type, fp, args[sc->offset]);
2022 		break;
2023 	case Shutdown:
2024 		print_integer_arg(sysdecode_shutdown_how, fp, args[sc->offset]);
2025 		break;
2026 	case Resource:
2027 		print_integer_arg(sysdecode_rlimit, fp, args[sc->offset]);
2028 		break;
2029 	case RusageWho:
2030 		print_integer_arg(sysdecode_getrusage_who, fp, args[sc->offset]);
2031 		break;
2032 	case Pathconf:
2033 		print_integer_arg(sysdecode_pathconf_name, fp, args[sc->offset]);
2034 		break;
2035 	case Rforkflags:
2036 		print_mask_arg(sysdecode_rfork_flags, fp, args[sc->offset]);
2037 		break;
2038 	case Sockaddr: {
2039 		socklen_t len;
2040 
2041 		if (args[sc->offset] == 0) {
2042 			fputs("NULL", fp);
2043 			break;
2044 		}
2045 
2046 		/*
2047 		 * Extract the address length from the next argument.  If
2048 		 * this is an output sockaddr (OUT is set), then the
2049 		 * next argument is a pointer to a socklen_t.  Otherwise
2050 		 * the next argument contains a socklen_t by value.
2051 		 */
2052 		if (sc->type & OUT) {
2053 			if (get_struct(pid, args[sc->offset + 1], &len,
2054 			    sizeof(len)) == -1) {
2055 				print_pointer(fp, args[sc->offset]);
2056 				break;
2057 			}
2058 		} else
2059 			len = args[sc->offset + 1];
2060 
2061 		print_sockaddr(fp, trussinfo, args[sc->offset], len);
2062 		break;
2063 	}
2064 	case Sigaction: {
2065 		struct sigaction sa;
2066 
2067 		if (get_struct(pid, args[sc->offset], &sa, sizeof(sa)) != -1) {
2068 			fputs("{ ", fp);
2069 			if (sa.sa_handler == SIG_DFL)
2070 				fputs("SIG_DFL", fp);
2071 			else if (sa.sa_handler == SIG_IGN)
2072 				fputs("SIG_IGN", fp);
2073 			else
2074 				fprintf(fp, "%p", sa.sa_handler);
2075 			fprintf(fp, " %s ss_t }",
2076 			    xlookup_bits(sigaction_flags, sa.sa_flags));
2077 		} else
2078 			print_pointer(fp, args[sc->offset]);
2079 		break;
2080 	}
2081 	case Sigevent: {
2082 		struct sigevent se;
2083 
2084 		if (get_struct(pid, args[sc->offset], &se, sizeof(se)) != -1)
2085 			print_sigevent(fp, &se);
2086 		else
2087 			print_pointer(fp, args[sc->offset]);
2088 		break;
2089 	}
2090 	case Kevent: {
2091 		/*
2092 		 * XXX XXX: The size of the array is determined by either the
2093 		 * next syscall argument, or by the syscall return value,
2094 		 * depending on which argument number we are.  This matches the
2095 		 * kevent syscall, but luckily that's the only syscall that uses
2096 		 * them.
2097 		 */
2098 		struct kevent *ke;
2099 		int numevents = -1;
2100 		size_t bytes;
2101 		int i;
2102 
2103 		if (sc->offset == 1)
2104 			numevents = args[sc->offset+1];
2105 		else if (sc->offset == 3 && retval[0] != -1)
2106 			numevents = retval[0];
2107 
2108 		if (numevents >= 0) {
2109 			bytes = sizeof(struct kevent) * numevents;
2110 			if ((ke = malloc(bytes)) == NULL)
2111 				err(1,
2112 				    "Cannot malloc %zu bytes for kevent array",
2113 				    bytes);
2114 		} else
2115 			ke = NULL;
2116 		if (numevents >= 0 && get_struct(pid, args[sc->offset],
2117 		    ke, bytes) != -1) {
2118 			fputc('{', fp);
2119 			for (i = 0; i < numevents; i++) {
2120 				fputc(' ', fp);
2121 				print_kevent(fp, &ke[i]);
2122 			}
2123 			fputs(" }", fp);
2124 		} else {
2125 			print_pointer(fp, args[sc->offset]);
2126 		}
2127 		free(ke);
2128 		break;
2129 	}
2130 	case Kevent11: {
2131 		struct freebsd11_kevent *ke11;
2132 		struct kevent ke;
2133 		int numevents = -1;
2134 		size_t bytes;
2135 		int i;
2136 
2137 		if (sc->offset == 1)
2138 			numevents = args[sc->offset+1];
2139 		else if (sc->offset == 3 && retval[0] != -1)
2140 			numevents = retval[0];
2141 
2142 		if (numevents >= 0) {
2143 			bytes = sizeof(struct freebsd11_kevent) * numevents;
2144 			if ((ke11 = malloc(bytes)) == NULL)
2145 				err(1,
2146 				    "Cannot malloc %zu bytes for kevent array",
2147 				    bytes);
2148 		} else
2149 			ke11 = NULL;
2150 		memset(&ke, 0, sizeof(ke));
2151 		if (numevents >= 0 && get_struct(pid, args[sc->offset],
2152 		    ke11, bytes) != -1) {
2153 			fputc('{', fp);
2154 			for (i = 0; i < numevents; i++) {
2155 				fputc(' ', fp);
2156 				ke.ident = ke11[i].ident;
2157 				ke.filter = ke11[i].filter;
2158 				ke.flags = ke11[i].flags;
2159 				ke.fflags = ke11[i].fflags;
2160 				ke.data = ke11[i].data;
2161 				ke.udata = ke11[i].udata;
2162 				print_kevent(fp, &ke);
2163 			}
2164 			fputs(" }", fp);
2165 		} else {
2166 			print_pointer(fp, args[sc->offset]);
2167 		}
2168 		free(ke11);
2169 		break;
2170 	}
2171 	case Stat: {
2172 		struct stat st;
2173 
2174 		if (get_struct(pid, args[sc->offset], &st, sizeof(st))
2175 		    != -1) {
2176 			char mode[12];
2177 
2178 			strmode(st.st_mode, mode);
2179 			fprintf(fp,
2180 			    "{ mode=%s,inode=%ju,size=%jd,blksize=%ld }", mode,
2181 			    (uintmax_t)st.st_ino, (intmax_t)st.st_size,
2182 			    (long)st.st_blksize);
2183 		} else {
2184 			print_pointer(fp, args[sc->offset]);
2185 		}
2186 		break;
2187 	}
2188 	case Stat11: {
2189 		struct freebsd11_stat st;
2190 
2191 		if (get_struct(pid, args[sc->offset], &st, sizeof(st))
2192 		    != -1) {
2193 			char mode[12];
2194 
2195 			strmode(st.st_mode, mode);
2196 			fprintf(fp,
2197 			    "{ mode=%s,inode=%ju,size=%jd,blksize=%ld }", mode,
2198 			    (uintmax_t)st.st_ino, (intmax_t)st.st_size,
2199 			    (long)st.st_blksize);
2200 		} else {
2201 			print_pointer(fp, args[sc->offset]);
2202 		}
2203 		break;
2204 	}
2205 	case StatFs: {
2206 		unsigned int i;
2207 		struct statfs buf;
2208 
2209 		if (get_struct(pid, args[sc->offset], &buf,
2210 		    sizeof(buf)) != -1) {
2211 			char fsid[17];
2212 
2213 			bzero(fsid, sizeof(fsid));
2214 			if (buf.f_fsid.val[0] != 0 || buf.f_fsid.val[1] != 0) {
2215 			        for (i = 0; i < sizeof(buf.f_fsid); i++)
2216 					snprintf(&fsid[i*2],
2217 					    sizeof(fsid) - (i*2), "%02x",
2218 					    ((u_char *)&buf.f_fsid)[i]);
2219 			}
2220 			fprintf(fp,
2221 			    "{ fstypename=%s,mntonname=%s,mntfromname=%s,"
2222 			    "fsid=%s }", buf.f_fstypename, buf.f_mntonname,
2223 			    buf.f_mntfromname, fsid);
2224 		} else
2225 			print_pointer(fp, args[sc->offset]);
2226 		break;
2227 	}
2228 
2229 	case Rusage: {
2230 		struct rusage ru;
2231 
2232 		if (get_struct(pid, args[sc->offset], &ru, sizeof(ru))
2233 		    != -1) {
2234 			fprintf(fp,
2235 			    "{ u=%jd.%06ld,s=%jd.%06ld,in=%ld,out=%ld }",
2236 			    (intmax_t)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec,
2237 			    (intmax_t)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec,
2238 			    ru.ru_inblock, ru.ru_oublock);
2239 		} else
2240 			print_pointer(fp, args[sc->offset]);
2241 		break;
2242 	}
2243 	case Rlimit: {
2244 		struct rlimit rl;
2245 
2246 		if (get_struct(pid, args[sc->offset], &rl, sizeof(rl))
2247 		    != -1) {
2248 			fprintf(fp, "{ cur=%ju,max=%ju }",
2249 			    rl.rlim_cur, rl.rlim_max);
2250 		} else
2251 			print_pointer(fp, args[sc->offset]);
2252 		break;
2253 	}
2254 	case ExitStatus: {
2255 		int status;
2256 
2257 		if (get_struct(pid, args[sc->offset], &status,
2258 		    sizeof(status)) != -1) {
2259 			fputs("{ ", fp);
2260 			if (WIFCONTINUED(status))
2261 				fputs("CONTINUED", fp);
2262 			else if (WIFEXITED(status))
2263 				fprintf(fp, "EXITED,val=%d",
2264 				    WEXITSTATUS(status));
2265 			else if (WIFSIGNALED(status))
2266 				fprintf(fp, "SIGNALED,sig=%s%s",
2267 				    strsig2(WTERMSIG(status)),
2268 				    WCOREDUMP(status) ? ",cored" : "");
2269 			else
2270 				fprintf(fp, "STOPPED,sig=%s",
2271 				    strsig2(WTERMSIG(status)));
2272 			fputs(" }", fp);
2273 		} else
2274 			print_pointer(fp, args[sc->offset]);
2275 		break;
2276 	}
2277 	case Waitoptions:
2278 		print_mask_arg(sysdecode_wait6_options, fp, args[sc->offset]);
2279 		break;
2280 	case Idtype:
2281 		print_integer_arg(sysdecode_idtype, fp, args[sc->offset]);
2282 		break;
2283 	case Procctl:
2284 		print_integer_arg(sysdecode_procctl_cmd, fp, args[sc->offset]);
2285 		break;
2286 	case Umtxop: {
2287 		int rem;
2288 
2289 		if (print_mask_arg_part(sysdecode_umtx_op_flags, fp,
2290 		    args[sc->offset], &rem))
2291 			fprintf(fp, "|");
2292 		print_integer_arg(sysdecode_umtx_op, fp, rem);
2293 		break;
2294 	}
2295 	case Atfd:
2296 		print_integer_arg(sysdecode_atfd, fp, args[sc->offset]);
2297 		break;
2298 	case Atflags:
2299 		print_mask_arg(sysdecode_atflags, fp, args[sc->offset]);
2300 		break;
2301 	case Accessmode:
2302 		print_mask_arg(sysdecode_access_mode, fp, args[sc->offset]);
2303 		break;
2304 	case Sysarch:
2305 		print_integer_arg(sysdecode_sysarch_number, fp,
2306 		    args[sc->offset]);
2307 		break;
2308 	case Sysctl: {
2309 		char name[BUFSIZ];
2310 		int oid[CTL_MAXNAME + 2];
2311 		size_t len;
2312 
2313 		memset(name, 0, sizeof(name));
2314 		len = args[sc->offset + 1];
2315 		if (get_struct(pid, args[sc->offset], oid,
2316 		    len * sizeof(oid[0])) != -1) {
2317 		    	fprintf(fp, "\"");
2318 			if (oid[0] == CTL_SYSCTL) {
2319 				fprintf(fp, "sysctl.");
2320 				switch (oid[1]) {
2321 				case CTL_SYSCTL_DEBUG:
2322 					fprintf(fp, "debug");
2323 					break;
2324 				case CTL_SYSCTL_NAME:
2325 					fprintf(fp, "name ");
2326 					print_sysctl_oid(fp, oid + 2, len - 2);
2327 					break;
2328 				case CTL_SYSCTL_NEXT:
2329 					fprintf(fp, "next");
2330 					break;
2331 				case CTL_SYSCTL_NAME2OID:
2332 					fprintf(fp, "name2oid %s",
2333 					    get_string(pid,
2334 					        args[sc->offset + 4],
2335 						args[sc->offset + 5]));
2336 					break;
2337 				case CTL_SYSCTL_OIDFMT:
2338 					fprintf(fp, "oidfmt ");
2339 					print_sysctl(fp, oid + 2, len - 2);
2340 					break;
2341 				case CTL_SYSCTL_OIDDESCR:
2342 					fprintf(fp, "oiddescr ");
2343 					print_sysctl(fp, oid + 2, len - 2);
2344 					break;
2345 				case CTL_SYSCTL_OIDLABEL:
2346 					fprintf(fp, "oidlabel ");
2347 					print_sysctl(fp, oid + 2, len - 2);
2348 					break;
2349 				case CTL_SYSCTL_NEXTNOSKIP:
2350 					fprintf(fp, "nextnoskip");
2351 					break;
2352 				default:
2353 					print_sysctl(fp, oid + 1, len - 1);
2354 				}
2355 			} else {
2356 				print_sysctl(fp, oid, len);
2357 			}
2358 		    	fprintf(fp, "\"");
2359 		}
2360 		break;
2361 	}
2362 	case PipeFds:
2363 		/*
2364 		 * The pipe() system call in the kernel returns its
2365 		 * two file descriptors via return values.  However,
2366 		 * the interface exposed by libc is that pipe()
2367 		 * accepts a pointer to an array of descriptors.
2368 		 * Format the output to match the libc API by printing
2369 		 * the returned file descriptors as a fake argument.
2370 		 *
2371 		 * Overwrite the first retval to signal a successful
2372 		 * return as well.
2373 		 */
2374 		fprintf(fp, "{ %d, %d }", (int)retval[0], (int)retval[1]);
2375 		retval[0] = 0;
2376 		break;
2377 	case Utrace: {
2378 		size_t len;
2379 		void *utrace_addr;
2380 
2381 		len = args[sc->offset + 1];
2382 		utrace_addr = calloc(1, len);
2383 		if (get_struct(pid, args[sc->offset],
2384 		    (void *)utrace_addr, len) != -1)
2385 			print_utrace(fp, utrace_addr, len);
2386 		else
2387 			print_pointer(fp, args[sc->offset]);
2388 		free(utrace_addr);
2389 		break;
2390 	}
2391 	case IntArray: {
2392 		int descriptors[16];
2393 		unsigned long i, ndescriptors;
2394 		bool truncated;
2395 
2396 		ndescriptors = args[sc->offset + 1];
2397 		truncated = false;
2398 		if (ndescriptors > nitems(descriptors)) {
2399 			ndescriptors = nitems(descriptors);
2400 			truncated = true;
2401 		}
2402 		if (get_struct(pid, args[sc->offset],
2403 		    descriptors, ndescriptors * sizeof(descriptors[0])) != -1) {
2404 			fprintf(fp, "{");
2405 			for (i = 0; i < ndescriptors; i++)
2406 				fprintf(fp, i == 0 ? " %d" : ", %d",
2407 				    descriptors[i]);
2408 			fprintf(fp, truncated ? ", ... }" : " }");
2409 		} else
2410 			print_pointer(fp, args[sc->offset]);
2411 		break;
2412 	}
2413 	case Pipe2:
2414 		print_mask_arg(sysdecode_pipe2_flags, fp, args[sc->offset]);
2415 		break;
2416 	case CapFcntlRights: {
2417 		uint32_t rights;
2418 
2419 		if (sc->type & OUT) {
2420 			if (get_struct(pid, args[sc->offset], &rights,
2421 			    sizeof(rights)) == -1) {
2422 				print_pointer(fp, args[sc->offset]);
2423 				break;
2424 			}
2425 		} else
2426 			rights = args[sc->offset];
2427 		print_mask_arg32(sysdecode_cap_fcntlrights, fp, rights);
2428 		break;
2429 	}
2430 	case Fadvice:
2431 		print_integer_arg(sysdecode_fadvice, fp, args[sc->offset]);
2432 		break;
2433 	case FileFlags: {
2434 		fflags_t rem;
2435 
2436 		if (!sysdecode_fileflags(fp, args[sc->offset], &rem))
2437 			fprintf(fp, "0x%x", rem);
2438 		else if (rem != 0)
2439 			fprintf(fp, "|0x%x", rem);
2440 		break;
2441 	}
2442 	case Flockop:
2443 		print_mask_arg(sysdecode_flock_operation, fp, args[sc->offset]);
2444 		break;
2445 	case Getfsstatmode:
2446 		print_integer_arg(sysdecode_getfsstat_mode, fp,
2447 		    args[sc->offset]);
2448 		break;
2449 	case Itimerwhich:
2450 		print_integer_arg(sysdecode_itimer, fp, args[sc->offset]);
2451 		break;
2452 	case Kldsymcmd:
2453 		print_integer_arg(sysdecode_kldsym_cmd, fp, args[sc->offset]);
2454 		break;
2455 	case Kldunloadflags:
2456 		print_integer_arg(sysdecode_kldunload_flags, fp,
2457 		    args[sc->offset]);
2458 		break;
2459 	case AiofsyncOp:
2460 		fputs(xlookup(aio_fsync_ops, args[sc->offset]), fp);
2461 		break;
2462 	case LioMode:
2463 		fputs(xlookup(lio_modes, args[sc->offset]), fp);
2464 		break;
2465 	case Madvice:
2466 		print_integer_arg(sysdecode_madvice, fp, args[sc->offset]);
2467 		break;
2468 	case Socklent:
2469 		fprintf(fp, "%u", (socklen_t)args[sc->offset]);
2470 		break;
2471 	case Sockprotocol: {
2472 		const char *temp;
2473 		int domain, protocol;
2474 
2475 		domain = args[sc->offset - 2];
2476 		protocol = args[sc->offset];
2477 		if (protocol == 0) {
2478 			fputs("0", fp);
2479 		} else {
2480 			temp = sysdecode_socket_protocol(domain, protocol);
2481 			if (temp) {
2482 				fputs(temp, fp);
2483 			} else {
2484 				fprintf(fp, "%d", protocol);
2485 			}
2486 		}
2487 		break;
2488 	}
2489 	case Sockoptlevel:
2490 		print_integer_arg(sysdecode_sockopt_level, fp,
2491 		    args[sc->offset]);
2492 		break;
2493 	case Sockoptname: {
2494 		const char *temp;
2495 		int level, name;
2496 
2497 		level = args[sc->offset - 1];
2498 		name = args[sc->offset];
2499 		temp = sysdecode_sockopt_name(level, name);
2500 		if (temp) {
2501 			fputs(temp, fp);
2502 		} else {
2503 			fprintf(fp, "%d", name);
2504 		}
2505 		break;
2506 	}
2507 	case Msgflags:
2508 		print_mask_arg(sysdecode_msg_flags, fp, args[sc->offset]);
2509 		break;
2510 	case CapRights: {
2511 		cap_rights_t rights;
2512 
2513 		if (get_struct(pid, args[sc->offset], &rights,
2514 		    sizeof(rights)) != -1) {
2515 			fputs("{ ", fp);
2516 			sysdecode_cap_rights(fp, &rights);
2517 			fputs(" }", fp);
2518 		} else
2519 			print_pointer(fp, args[sc->offset]);
2520 		break;
2521 	}
2522 	case Acltype:
2523 		print_integer_arg(sysdecode_acltype, fp, args[sc->offset]);
2524 		break;
2525 	case Extattrnamespace:
2526 		print_integer_arg(sysdecode_extattrnamespace, fp,
2527 		    args[sc->offset]);
2528 		break;
2529 	case Minherit:
2530 		print_integer_arg(sysdecode_minherit_inherit, fp,
2531 		    args[sc->offset]);
2532 		break;
2533 	case Mlockall:
2534 		print_mask_arg(sysdecode_mlockall_flags, fp, args[sc->offset]);
2535 		break;
2536 	case Mountflags:
2537 		print_mask_arg(sysdecode_mount_flags, fp, args[sc->offset]);
2538 		break;
2539 	case Msync:
2540 		print_mask_arg(sysdecode_msync_flags, fp, args[sc->offset]);
2541 		break;
2542 	case Priowhich:
2543 		print_integer_arg(sysdecode_prio_which, fp, args[sc->offset]);
2544 		break;
2545 	case Ptraceop:
2546 		print_integer_arg(sysdecode_ptrace_request, fp,
2547 		    args[sc->offset]);
2548 		break;
2549 	case Sendfileflags:
2550 		print_mask_arg(sysdecode_sendfile_flags, fp, args[sc->offset]);
2551 		break;
2552 	case Sendfilehdtr: {
2553 		struct sf_hdtr hdtr;
2554 
2555 		if (get_struct(pid, args[sc->offset], &hdtr, sizeof(hdtr)) !=
2556 		    -1) {
2557 			fprintf(fp, "{");
2558 			print_iovec(fp, trussinfo, (uintptr_t)hdtr.headers,
2559 			    hdtr.hdr_cnt);
2560 			print_iovec(fp, trussinfo, (uintptr_t)hdtr.trailers,
2561 			    hdtr.trl_cnt);
2562 			fprintf(fp, "}");
2563 		} else
2564 			print_pointer(fp, args[sc->offset]);
2565 		break;
2566 	}
2567 	case Quotactlcmd:
2568 		if (!sysdecode_quotactl_cmd(fp, args[sc->offset]))
2569 			fprintf(fp, "%#x", (int)args[sc->offset]);
2570 		break;
2571 	case Reboothowto:
2572 		print_mask_arg(sysdecode_reboot_howto, fp, args[sc->offset]);
2573 		break;
2574 	case Rtpriofunc:
2575 		print_integer_arg(sysdecode_rtprio_function, fp,
2576 		    args[sc->offset]);
2577 		break;
2578 	case Schedpolicy:
2579 		print_integer_arg(sysdecode_scheduler_policy, fp,
2580 		    args[sc->offset]);
2581 		break;
2582 	case Schedparam: {
2583 		struct sched_param sp;
2584 
2585 		if (get_struct(pid, args[sc->offset], &sp, sizeof(sp)) != -1)
2586 			fprintf(fp, "{ %d }", sp.sched_priority);
2587 		else
2588 			print_pointer(fp, args[sc->offset]);
2589 		break;
2590 	}
2591 	case PSig: {
2592 		int sig;
2593 
2594 		if (get_struct(pid, args[sc->offset], &sig, sizeof(sig)) == 0)
2595 			fprintf(fp, "{ %s }", strsig2(sig));
2596 		else
2597 			print_pointer(fp, args[sc->offset]);
2598 		break;
2599 	}
2600 	case Siginfo: {
2601 		siginfo_t si;
2602 
2603 		if (get_struct(pid, args[sc->offset], &si, sizeof(si)) != -1) {
2604 			fprintf(fp, "{ signo=%s", strsig2(si.si_signo));
2605 			decode_siginfo(fp, &si);
2606 			fprintf(fp, " }");
2607 		} else
2608 			print_pointer(fp, args[sc->offset]);
2609 		break;
2610 	}
2611 	case Iovec:
2612 		/*
2613 		 * Print argument as an array of struct iovec, where the next
2614 		 * syscall argument is the number of elements of the array.
2615 		 */
2616 
2617 		print_iovec(fp, trussinfo, args[sc->offset],
2618 		    (int)args[sc->offset + 1]);
2619 		break;
2620 	case Aiocb: {
2621 		struct aiocb cb;
2622 
2623 		if (get_struct(pid, args[sc->offset], &cb, sizeof(cb)) != -1)
2624 			print_aiocb(fp, &cb);
2625 		else
2626 			print_pointer(fp, args[sc->offset]);
2627 		break;
2628 	}
2629 	case AiocbArray: {
2630 		/*
2631 		 * Print argment as an array of pointers to struct aiocb, where
2632 		 * the next syscall argument is the number of elements.
2633 		 */
2634 		uintptr_t cbs[16];
2635 		unsigned int nent;
2636 		bool truncated;
2637 
2638 		nent = args[sc->offset + 1];
2639 		truncated = false;
2640 		if (nent > nitems(cbs)) {
2641 			nent = nitems(cbs);
2642 			truncated = true;
2643 		}
2644 
2645 		if (get_struct(pid, args[sc->offset], cbs, sizeof(uintptr_t) * nent) != -1) {
2646 			unsigned int i;
2647 			fputs("[", fp);
2648 			for (i = 0; i < nent; ++i) {
2649 				struct aiocb cb;
2650 				if (i > 0)
2651 					fputc(',', fp);
2652 				if (get_struct(pid, cbs[i], &cb, sizeof(cb)) != -1)
2653 					print_aiocb(fp, &cb);
2654 				else
2655 					print_pointer(fp, cbs[i]);
2656 			}
2657 			if (truncated)
2658 				fputs(",...", fp);
2659 			fputs("]", fp);
2660 		} else
2661 			print_pointer(fp, args[sc->offset]);
2662 		break;
2663 	}
2664 	case AiocbPointer: {
2665 		/*
2666 		 * aio_waitcomplete(2) assigns a pointer to a pointer to struct
2667 		 * aiocb, so we need to handle the extra layer of indirection.
2668 		 */
2669 		uintptr_t cbp;
2670 		struct aiocb cb;
2671 
2672 		if (get_struct(pid, args[sc->offset], &cbp, sizeof(cbp)) != -1) {
2673 			if (get_struct(pid, cbp, &cb, sizeof(cb)) != -1)
2674 				print_aiocb(fp, &cb);
2675 			else
2676 				print_pointer(fp, cbp);
2677 		} else
2678 			print_pointer(fp, args[sc->offset]);
2679 		break;
2680 	}
2681 	case Sctpsndrcvinfo: {
2682 		struct sctp_sndrcvinfo info;
2683 
2684 		if (get_struct(pid, args[sc->offset],
2685 		    &info, sizeof(struct sctp_sndrcvinfo)) == -1) {
2686 			print_pointer(fp, args[sc->offset]);
2687 			break;
2688 		}
2689 		print_sctp_sndrcvinfo(fp, sc->type & OUT, &info);
2690 		break;
2691 	}
2692 	case Msghdr: {
2693 		struct msghdr msghdr;
2694 
2695 		if (get_struct(pid, args[sc->offset],
2696 		    &msghdr, sizeof(struct msghdr)) == -1) {
2697 			print_pointer(fp, args[sc->offset]);
2698 			break;
2699 		}
2700 		fputs("{", fp);
2701 		print_sockaddr(fp, trussinfo, (uintptr_t)msghdr.msg_name, msghdr.msg_namelen);
2702 		fprintf(fp, ",%d,", msghdr.msg_namelen);
2703 		print_iovec(fp, trussinfo, (uintptr_t)msghdr.msg_iov, msghdr.msg_iovlen);
2704 		fprintf(fp, ",%d,", msghdr.msg_iovlen);
2705 		print_cmsgs(fp, pid, sc->type & OUT, &msghdr);
2706 		fprintf(fp, ",%u,", msghdr.msg_controllen);
2707 		print_mask_arg(sysdecode_msg_flags, fp, msghdr.msg_flags);
2708 		fputs("}", fp);
2709 		break;
2710 	}
2711 
2712 	default:
2713 		errx(1, "Invalid argument type %d\n", sc->type & ARG_MASK);
2714 	}
2715 	fclose(fp);
2716 	return (tmp);
2717 }
2718 
2719 /*
2720  * Print (to outfile) the system call and its arguments.
2721  */
2722 void
2723 print_syscall(struct trussinfo *trussinfo)
2724 {
2725 	struct threadinfo *t;
2726 	const char *name;
2727 	char **s_args;
2728 	int i, len, nargs;
2729 
2730 	t = trussinfo->curthread;
2731 
2732 	name = t->cs.sc->name;
2733 	nargs = t->cs.nargs;
2734 	s_args = t->cs.s_args;
2735 
2736 	len = print_line_prefix(trussinfo);
2737 	len += fprintf(trussinfo->outfile, "%s(", name);
2738 
2739 	for (i = 0; i < nargs; i++) {
2740 		if (s_args[i] != NULL)
2741 			len += fprintf(trussinfo->outfile, "%s", s_args[i]);
2742 		else
2743 			len += fprintf(trussinfo->outfile,
2744 			    "<missing argument>");
2745 		len += fprintf(trussinfo->outfile, "%s", i < (nargs - 1) ?
2746 		    "," : "");
2747 	}
2748 	len += fprintf(trussinfo->outfile, ")");
2749 	for (i = 0; i < 6 - (len / 8); i++)
2750 		fprintf(trussinfo->outfile, "\t");
2751 }
2752 
2753 void
2754 print_syscall_ret(struct trussinfo *trussinfo, int error, syscallarg_t *retval)
2755 {
2756 	struct timespec timediff;
2757 	struct threadinfo *t;
2758 	struct syscall *sc;
2759 
2760 	t = trussinfo->curthread;
2761 	sc = t->cs.sc;
2762 	if (trussinfo->flags & COUNTONLY) {
2763 		timespecsub(&t->after, &t->before, &timediff);
2764 		timespecadd(&sc->time, &timediff, &sc->time);
2765 		sc->ncalls++;
2766 		if (error != 0)
2767 			sc->nerror++;
2768 		return;
2769 	}
2770 
2771 	print_syscall(trussinfo);
2772 	fflush(trussinfo->outfile);
2773 
2774 	if (retval == NULL) {
2775 		/*
2776 		 * This system call resulted in the current thread's exit,
2777 		 * so there is no return value or error to display.
2778 		 */
2779 		fprintf(trussinfo->outfile, "\n");
2780 		return;
2781 	}
2782 
2783 	if (error == ERESTART)
2784 		fprintf(trussinfo->outfile, " ERESTART\n");
2785 	else if (error == EJUSTRETURN)
2786 		fprintf(trussinfo->outfile, " EJUSTRETURN\n");
2787 	else if (error != 0) {
2788 		fprintf(trussinfo->outfile, " ERR#%d '%s'\n",
2789 		    sysdecode_freebsd_to_abi_errno(t->proc->abi->abi, error),
2790 		    strerror(error));
2791 	} else if (sc->decode.ret_type == 2 &&
2792 	    t->proc->abi->pointer_size == 4) {
2793 		off_t off;
2794 #if _BYTE_ORDER == _LITTLE_ENDIAN
2795 		off = (off_t)retval[1] << 32 | retval[0];
2796 #else
2797 		off = (off_t)retval[0] << 32 | retval[1];
2798 #endif
2799 		fprintf(trussinfo->outfile, " = %jd (0x%jx)\n", (intmax_t)off,
2800 		    (intmax_t)off);
2801 	} else {
2802 		fprintf(trussinfo->outfile, " = %jd (0x%jx)\n",
2803 		    (intmax_t)retval[0], (intmax_t)retval[0]);
2804 	}
2805 }
2806 
2807 void
2808 print_summary(struct trussinfo *trussinfo)
2809 {
2810 	struct timespec total = {0, 0};
2811 	struct syscall *sc;
2812 	int ncall, nerror;
2813 
2814 	fprintf(trussinfo->outfile, "%-20s%15s%8s%8s\n",
2815 	    "syscall", "seconds", "calls", "errors");
2816 	ncall = nerror = 0;
2817 	STAILQ_FOREACH(sc, &seen_syscalls, entries) {
2818 		if (sc->ncalls) {
2819 			fprintf(trussinfo->outfile, "%-20s%5jd.%09ld%8d%8d\n",
2820 			    sc->name, (intmax_t)sc->time.tv_sec,
2821 			    sc->time.tv_nsec, sc->ncalls, sc->nerror);
2822 			timespecadd(&total, &sc->time, &total);
2823 			ncall += sc->ncalls;
2824 			nerror += sc->nerror;
2825 		}
2826 	}
2827 	fprintf(trussinfo->outfile, "%20s%15s%8s%8s\n",
2828 	    "", "-------------", "-------", "-------");
2829 	fprintf(trussinfo->outfile, "%-20s%5jd.%09ld%8d%8d\n",
2830 	    "", (intmax_t)total.tv_sec, total.tv_nsec, ncall, nerror);
2831 }
2832