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