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