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