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