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