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