xref: /freebsd/usr.bin/truss/syscalls.c (revision e72055b7feba695a760d45f01f0f8268b1cb4a74)
1 /*
2  * Copyright 1997 Sean Eric Fagan
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. All advertising materials mentioning features or use of this software
13  *    must display the following acknowledgement:
14  *	This product includes software developed by Sean Eric Fagan
15  * 4. Neither the name of the author may be used to endorse or promote
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char rcsid[] =
34   "$FreeBSD$";
35 #endif /* not lint */
36 
37 /*
38  * This file has routines used to print out system calls and their
39  * arguments.
40  */
41 
42 #include <sys/types.h>
43 #include <sys/mman.h>
44 #include <sys/procctl.h>
45 #include <sys/ptrace.h>
46 #include <sys/socket.h>
47 #include <sys/time.h>
48 #include <sys/un.h>
49 #include <sys/wait.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <sys/ioccom.h>
53 #include <machine/atomic.h>
54 #include <errno.h>
55 #include <sys/umtx.h>
56 #include <sys/event.h>
57 #include <sys/stat.h>
58 #include <sys/resource.h>
59 
60 #include <ctype.h>
61 #include <err.h>
62 #include <fcntl.h>
63 #include <poll.h>
64 #include <signal.h>
65 #include <stdint.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <time.h>
70 #include <unistd.h>
71 #include <vis.h>
72 
73 #include "truss.h"
74 #include "extern.h"
75 #include "syscall.h"
76 
77 /* 64-bit alignment on 32-bit platforms. */
78 #ifdef __powerpc__
79 #define	QUAD_ALIGN	1
80 #else
81 #define	QUAD_ALIGN	0
82 #endif
83 
84 /* Number of slots needed for a 64-bit argument. */
85 #ifdef __LP64__
86 #define	QUAD_SLOTS	1
87 #else
88 #define	QUAD_SLOTS	2
89 #endif
90 
91 /*
92  * This should probably be in its own file, sorted alphabetically.
93  */
94 static struct syscall syscalls[] = {
95 	{ .name = "fcntl", .ret_type = 1, .nargs = 3,
96 	  .args = { { Int, 0 } , { Fcntl, 1 }, { Fcntlflag | OUT, 2 } } },
97 	{ .name = "fork", .ret_type = 1, .nargs = 0 },
98 	{ .name = "vfork", .ret_type = 1, .nargs = 0 },
99 	{ .name = "rfork", .ret_type = 1, .nargs = 1,
100 	  .args = { { Rforkflags, 0 } } },
101 	{ .name = "getegid", .ret_type = 1, .nargs = 0 },
102 	{ .name = "geteuid", .ret_type = 1, .nargs = 0 },
103 	{ .name = "linux_readlink", .ret_type = 1, .nargs = 3,
104 	  .args = { { Name, 0 } , { Name | OUT, 1 }, { Int, 2 }}},
105 	{ .name = "linux_socketcall", .ret_type = 1, .nargs = 2,
106 	  .args = { { Int, 0 } , { LinuxSockArgs, 1 }}},
107 	{ .name = "getgid", .ret_type = 1, .nargs = 0 },
108 	{ .name = "getpid", .ret_type = 1, .nargs = 0 },
109 	{ .name = "getpgid", .ret_type = 1, .nargs = 1,
110 	  .args = { { Int, 0 } } },
111 	{ .name = "getpgrp", .ret_type = 1, .nargs = 0 },
112 	{ .name = "getppid", .ret_type = 1, .nargs = 0 },
113 	{ .name = "getsid", .ret_type = 1, .nargs = 1,
114 	  .args = { { Int, 0 } } },
115 	{ .name = "getuid", .ret_type = 1, .nargs = 0 },
116 	{ .name = "readlink", .ret_type = 1, .nargs = 3,
117 	  .args = { { Name, 0 } , { Readlinkres | OUT, 1 }, { Int, 2 } } },
118 	{ .name = "lseek", .ret_type = 2, .nargs = 3,
119 	  .args = { { Int, 0 }, { Quad, 1 + QUAD_ALIGN }, { Whence, 1 + QUAD_SLOTS + QUAD_ALIGN } } },
120 	{ .name = "linux_lseek", .ret_type = 2, .nargs = 3,
121 	  .args = { { Int, 0 }, { Int, 1 }, { Whence, 2 } } },
122 	{ .name = "mmap", .ret_type = 2, .nargs = 6,
123 	  .args = { { Ptr, 0 }, { Int, 1 }, { Mprot, 2 }, { Mmapflags, 3 }, { Int, 4 }, { Quad, 5 + QUAD_ALIGN } } },
124 	{ .name = "linux_mkdir", .ret_type = 1, .nargs = 2,
125 	  .args = { { Name | IN, 0} , {Int, 1}}},
126 	{ .name = "mprotect", .ret_type = 1, .nargs = 3,
127 	  .args = { { Ptr, 0 }, { Int, 1 }, { Mprot, 2 } } },
128 	{ .name = "open", .ret_type = 1, .nargs = 3,
129 	  .args = { { Name | IN, 0 } , { Open, 1 }, { Octal, 2 } } },
130 	{ .name = "mkdir", .ret_type = 1, .nargs = 2,
131 	  .args = { { Name, 0 } , { Octal, 1 } } },
132 	{ .name = "linux_open", .ret_type = 1, .nargs = 3,
133 	  .args = { { Name, 0 }, { Hex, 1 }, { Octal, 2 } } },
134 	{ .name = "close", .ret_type = 1, .nargs = 1,
135 	  .args = { { Int, 0 } } },
136 	{ .name = "link", .ret_type = 0, .nargs = 2,
137 	  .args = { { Name, 0 }, { Name, 1 } } },
138 	{ .name = "unlink", .ret_type = 0, .nargs = 1,
139 	  .args = { { Name, 0 } } },
140 	{ .name = "chdir", .ret_type = 0, .nargs = 1,
141 	  .args = { { Name, 0 } } },
142 	{ .name = "chroot", .ret_type = 0, .nargs = 1,
143 	  .args = { { Name, 0 } } },
144 	{ .name = "mknod", .ret_type = 0, .nargs = 3,
145 	  .args = { { Name, 0 }, { Octal, 1 }, { Int, 3 } } },
146 	{ .name = "chmod", .ret_type = 0, .nargs = 2,
147 	  .args = { { Name, 0 }, { Octal, 1 } } },
148 	{ .name = "chown", .ret_type = 0, .nargs = 3,
149 	  .args = { { Name, 0 }, { Int, 1 }, { Int, 2 } } },
150 	{ .name = "linux_stat64", .ret_type = 1, .nargs = 3,
151 	  .args = { { Name | IN, 0 }, { Ptr | OUT, 1 }, { Ptr | IN, 1 }}},
152 	{ .name = "mount", .ret_type = 0, .nargs = 4,
153 	  .args = { { Name, 0 }, { Name, 1 }, { Int, 2 }, { Ptr, 3 } } },
154 	{ .name = "umount", .ret_type = 0, .nargs = 2,
155 	  .args = { { Name, 0 }, { Int, 2 } } },
156 	{ .name = "fstat", .ret_type = 1, .nargs = 2,
157 	  .args = { { Int, 0 }, { Stat | OUT , 1 } } },
158 	{ .name = "stat", .ret_type = 1, .nargs = 2,
159 	  .args = { { Name | IN, 0 }, { Stat | OUT, 1 } } },
160 	{ .name = "lstat", .ret_type = 1, .nargs = 2,
161 	  .args = { { Name | IN, 0 }, { Stat | OUT, 1 } } },
162 	{ .name = "linux_newstat", .ret_type = 1, .nargs = 2,
163 	  .args = { { Name | IN, 0 }, { Ptr | OUT, 1 } } },
164 	{ .name = "linux_access", .ret_type = 1, .nargs = 2,
165 	  .args = { { Name, 0 }, { Int, 1 }}},
166 	{ .name = "linux_newfstat", .ret_type = 1, .nargs = 2,
167 	  .args = { { Int, 0 }, { Ptr | OUT, 1 } } },
168 	{ .name = "write", .ret_type = 1, .nargs = 3,
169 	  .args = { { Int, 0 }, { BinString | IN, 1 }, { Int, 2 } } },
170 	{ .name = "ioctl", .ret_type = 1, .nargs = 3,
171 	  .args = { { Int, 0 }, { Ioctl, 1 }, { Hex, 2 } } },
172 	{ .name = "break", .ret_type = 1, .nargs = 1,
173 	  .args = { { Ptr, 0 } } },
174 	{ .name = "exit", .ret_type = 0, .nargs = 1,
175 	  .args = { { Hex, 0 } } },
176 	{ .name = "access", .ret_type = 1, .nargs = 2,
177 	  .args = { { Name | IN, 0 }, { Int, 1 } } },
178 	{ .name = "sigaction", .ret_type = 1, .nargs = 3,
179 	  .args = { { Signal, 0 }, { Sigaction | IN, 1 }, { Sigaction | OUT, 2 } } },
180 	{ .name = "accept", .ret_type = 1, .nargs = 3,
181 	  .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
182 	{ .name = "bind", .ret_type = 1, .nargs = 3,
183 	  .args = { { Int, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
184 	{ .name = "connect", .ret_type = 1, .nargs = 3,
185 	  .args = { { Int, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
186 	{ .name = "getpeername", .ret_type = 1, .nargs = 3,
187 	  .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
188 	{ .name = "getsockname", .ret_type = 1, .nargs = 3,
189 	  .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
190 	{ .name = "recvfrom", .ret_type = 1, .nargs = 6,
191 	  .args = { { Int, 0 }, { BinString | OUT, 1 }, { Int, 2 }, { Hex, 3 }, { Sockaddr | OUT, 4 }, { Ptr | OUT, 5 } } },
192 	{ .name = "sendto", .ret_type = 1, .nargs = 6,
193 	  .args = { { Int, 0 }, { BinString | IN, 1 }, { Int, 2 }, { Hex, 3 }, { Sockaddr | IN, 4 }, { Ptr | IN, 5 } } },
194 	{ .name = "execve", .ret_type = 1, .nargs = 3,
195 	  .args = { { Name | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } },
196 	{ .name = "linux_execve", .ret_type = 1, .nargs = 3,
197 	  .args = { { Name | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } },
198 	{ .name = "kldload", .ret_type = 0, .nargs = 1,
199 	  .args = { { Name | IN, 0 } } },
200 	{ .name = "kldunload", .ret_type = 0, .nargs = 1,
201 	  .args = { { Int, 0 } } },
202 	{ .name = "kldfind", .ret_type = 0, .nargs = 1,
203 	  .args = { { Name | IN, 0 } } },
204 	{ .name = "kldnext", .ret_type = 0, .nargs = 1,
205 	  .args = { { Int, 0 } } },
206 	{ .name = "kldstat", .ret_type = 0, .nargs = 2,
207 	  .args = { { Int, 0 }, { Ptr, 1 } } },
208 	{ .name = "kldfirstmod", .ret_type = 0, .nargs = 1,
209 	  .args = { { Int, 0 } } },
210 	{ .name = "nanosleep", .ret_type = 0, .nargs = 1,
211 	  .args = { { Timespec, 0 } } },
212 	{ .name = "select", .ret_type = 1, .nargs = 5,
213 	  .args = { { Int, 0 }, { Fd_set, 1 }, { Fd_set, 2 }, { Fd_set, 3 }, { Timeval, 4 } } },
214 	{ .name = "poll", .ret_type = 1, .nargs = 3,
215 	  .args = { { Pollfd, 0 }, { Int, 1 }, { Int, 2 } } },
216 	{ .name = "gettimeofday", .ret_type = 1, .nargs = 2,
217 	  .args = { { Timeval | OUT, 0 }, { Ptr, 1 } } },
218 	{ .name = "clock_gettime", .ret_type = 1, .nargs = 2,
219 	  .args = { { Int, 0 }, { Timespec | OUT, 1 } } },
220 	{ .name = "getitimer", .ret_type = 1, .nargs = 2,
221 	  .args = { { Int, 0 }, { Itimerval | OUT, 2 } } },
222 	{ .name = "setitimer", .ret_type = 1, .nargs = 3,
223 	  .args = { { Int, 0 }, { Itimerval, 1 } , { Itimerval | OUT, 2 } } },
224 	{ .name = "kse_release", .ret_type = 0, .nargs = 1,
225 	  .args = { { Timespec, 0 } } },
226 	{ .name = "kevent", .ret_type = 0, .nargs = 6,
227 	  .args = { { Int, 0 }, { Kevent, 1 }, { Int, 2 }, { Kevent | OUT, 3 }, { Int, 4 }, { Timespec, 5 } } },
228 	{ .name = "sigprocmask", .ret_type = 0, .nargs = 3,
229 	  .args = { { Sigprocmask, 0 }, { Sigset, 1 }, { Sigset | OUT, 2 } } },
230 	{ .name = "unmount", .ret_type = 1, .nargs = 2,
231 	  .args = { { Name, 0 }, { Int, 1 } } },
232 	{ .name = "socket", .ret_type = 1, .nargs = 3,
233 	  .args = { { Sockdomain, 0 }, { Socktype, 1 }, { Int, 2 } } },
234 	{ .name = "getrusage", .ret_type = 1, .nargs = 2,
235 	  .args = { { Int, 0 }, { Rusage | OUT, 1 } } },
236 	{ .name = "__getcwd", .ret_type = 1, .nargs = 2,
237 	  .args = { { Name | OUT, 0 }, { Int, 1 } } },
238 	{ .name = "shutdown", .ret_type = 1, .nargs = 2,
239 	  .args = { { Int, 0 }, { Shutdown, 1 } } },
240 	{ .name = "getrlimit", .ret_type = 1, .nargs = 2,
241 	  .args = { { Resource, 0 }, { Rlimit | OUT, 1 } } },
242 	{ .name = "setrlimit", .ret_type = 1, .nargs = 2,
243 	  .args = { { Resource, 0 }, { Rlimit | IN, 1 } } },
244 	{ .name = "utimes", .ret_type = 1, .nargs = 2,
245 	  .args = { { Name | IN, 0 }, { Timeval2 | IN, 1 } } },
246 	{ .name = "lutimes", .ret_type = 1, .nargs = 2,
247 	  .args = { { Name | IN, 0 }, { Timeval2 | IN, 1 } } },
248 	{ .name = "futimes", .ret_type = 1, .nargs = 2,
249 	  .args = { { Int, 0 }, { Timeval | IN, 1 } } },
250 	{ .name = "chflags", .ret_type = 1, .nargs = 2,
251 	  .args = { { Name | IN, 0 }, { Hex, 1 } } },
252 	{ .name = "lchflags", .ret_type = 1, .nargs = 2,
253 	  .args = { { Name | IN, 0 }, { Hex, 1 } } },
254 	{ .name = "pathconf", .ret_type = 1, .nargs = 2,
255 	  .args = { { Name | IN, 0 }, { Pathconf, 1 } } },
256 	{ .name = "pipe", .ret_type = 1, .nargs = 1,
257 	  .args = { { Ptr, 0 } } },
258 	{ .name = "truncate", .ret_type = 1, .nargs = 3,
259 	  .args = { { Name | IN, 0 }, { Int | IN, 1 }, { Quad | IN, 2 } } },
260 	{ .name = "ftruncate", .ret_type = 1, .nargs = 3,
261 	  .args = { { Int | IN, 0 }, { Int | IN, 1 }, { Quad | IN, 2 } } },
262 	{ .name = "kill", .ret_type = 1, .nargs = 2,
263 	  .args = { { Int | IN, 0 }, { Signal | IN, 1 } } },
264 	{ .name = "munmap", .ret_type = 1, .nargs = 2,
265 	  .args = { { Ptr, 0 }, { Int, 1 } } },
266 	{ .name = "read", .ret_type = 1, .nargs = 3,
267 	  .args = { { Int, 0 }, { BinString | OUT, 1 }, { Int, 2 } } },
268 	{ .name = "rename", .ret_type = 1, .nargs = 2,
269 	  .args = { { Name , 0 } , { Name, 1 } } },
270 	{ .name = "symlink", .ret_type = 1, .nargs = 2,
271 	  .args = { { Name , 0 } , { Name, 1 } } },
272 	{ .name = "posix_openpt", .ret_type = 1, .nargs = 1,
273 	  .args = { { Open, 0 } } },
274 	{ .name = "wait4", .ret_type = 1, .nargs = 4,
275 	  .args = { { Int, 0 }, { ExitStatus | OUT, 1 }, { Waitoptions, 2 },
276 		    { Rusage | OUT, 3 } } },
277 	{ .name = "wait6", .ret_type = 1, .nargs = 6,
278 	  .args = { { Idtype, 0 }, { Int, 1 }, { ExitStatus | OUT, 2 },
279 		    { Waitoptions, 3 }, { Rusage | OUT, 4 }, { Ptr, 5 } } },
280 	{ .name = "procctl", .ret_type = 1, .nargs = 4,
281 	  .args = { { Idtype, 0 }, { Int, 1 }, { Procctl, 2 }, { Ptr, 3 } } },
282 	{ .name = 0 },
283 };
284 
285 /* Xlat idea taken from strace */
286 struct xlat {
287 	int val;
288 	const char *str;
289 };
290 
291 #define	X(a)	{ a, #a },
292 #define	XEND	{ 0, NULL }
293 
294 static struct xlat kevent_filters[] = {
295 	X(EVFILT_READ) X(EVFILT_WRITE) X(EVFILT_AIO) X(EVFILT_VNODE)
296 	X(EVFILT_PROC) X(EVFILT_SIGNAL) X(EVFILT_TIMER)
297 	X(EVFILT_FS) X(EVFILT_READ) XEND
298 };
299 
300 static struct xlat kevent_flags[] = {
301 	X(EV_ADD) X(EV_DELETE) X(EV_ENABLE) X(EV_DISABLE) X(EV_ONESHOT)
302 	X(EV_CLEAR) X(EV_FLAG1) X(EV_ERROR) X(EV_EOF) XEND
303 };
304 
305 static struct xlat poll_flags[] = {
306 	X(POLLSTANDARD) X(POLLIN) X(POLLPRI) X(POLLOUT) X(POLLERR)
307 	X(POLLHUP) X(POLLNVAL) X(POLLRDNORM) X(POLLRDBAND)
308 	X(POLLWRBAND) X(POLLINIGNEOF) XEND
309 };
310 
311 static struct xlat mmap_flags[] = {
312 	X(MAP_SHARED) X(MAP_PRIVATE) X(MAP_FIXED) X(MAP_RENAME)
313 	X(MAP_NORESERVE) X(MAP_RESERVED0080) X(MAP_RESERVED0100)
314 	X(MAP_HASSEMAPHORE) X(MAP_STACK) X(MAP_NOSYNC) X(MAP_ANON)
315 	X(MAP_NOCORE) X(MAP_PREFAULT_READ)
316 #ifdef MAP_32BIT
317 	X(MAP_32BIT)
318 #endif
319 	XEND
320 };
321 
322 static struct xlat mprot_flags[] = {
323 	X(PROT_NONE) X(PROT_READ) X(PROT_WRITE) X(PROT_EXEC) XEND
324 };
325 
326 static struct xlat whence_arg[] = {
327 	X(SEEK_SET) X(SEEK_CUR) X(SEEK_END) XEND
328 };
329 
330 static struct xlat sigaction_flags[] = {
331 	X(SA_ONSTACK) X(SA_RESTART) X(SA_RESETHAND) X(SA_NOCLDSTOP)
332 	X(SA_NODEFER) X(SA_NOCLDWAIT) X(SA_SIGINFO) XEND
333 };
334 
335 static struct xlat fcntl_arg[] = {
336 	X(F_DUPFD) X(F_GETFD) X(F_SETFD) X(F_GETFL) X(F_SETFL)
337 	X(F_GETOWN) X(F_SETOWN) X(F_GETLK) X(F_SETLK) X(F_SETLKW) XEND
338 };
339 
340 static struct xlat fcntlfd_arg[] = {
341 	X(FD_CLOEXEC) XEND
342 };
343 
344 static struct xlat fcntlfl_arg[] = {
345 	X(O_APPEND) X(O_ASYNC) X(O_FSYNC) X(O_NONBLOCK) X(O_NOFOLLOW)
346 	X(O_DIRECT) XEND
347 };
348 
349 static struct xlat sockdomain_arg[] = {
350 	X(PF_UNSPEC) X(PF_LOCAL) X(PF_UNIX) X(PF_INET) X(PF_IMPLINK)
351 	X(PF_PUP) X(PF_CHAOS) X(PF_NETBIOS) X(PF_ISO) X(PF_OSI)
352 	X(PF_ECMA) X(PF_DATAKIT) X(PF_CCITT) X(PF_SNA) X(PF_DECnet)
353 	X(PF_DLI) X(PF_LAT) X(PF_HYLINK) X(PF_APPLETALK) X(PF_ROUTE)
354 	X(PF_LINK) X(PF_XTP) X(PF_COIP) X(PF_CNT) X(PF_SIP) X(PF_IPX)
355 	X(PF_RTIP) X(PF_PIP) X(PF_ISDN) X(PF_KEY) X(PF_INET6)
356 	X(PF_NATM) X(PF_ATM) X(PF_NETGRAPH) X(PF_SLOW) X(PF_SCLUSTER)
357 	X(PF_ARP) X(PF_BLUETOOTH) XEND
358 };
359 
360 static struct xlat socktype_arg[] = {
361 	X(SOCK_STREAM) X(SOCK_DGRAM) X(SOCK_RAW) X(SOCK_RDM)
362 	X(SOCK_SEQPACKET) XEND
363 };
364 
365 static struct xlat open_flags[] = {
366 	X(O_RDONLY) X(O_WRONLY) X(O_RDWR) X(O_ACCMODE) X(O_NONBLOCK)
367 	X(O_APPEND) X(O_SHLOCK) X(O_EXLOCK) X(O_ASYNC) X(O_FSYNC)
368 	X(O_NOFOLLOW) X(O_CREAT) X(O_TRUNC) X(O_EXCL) X(O_NOCTTY)
369 	X(O_DIRECT) X(O_DIRECTORY) X(O_EXEC) X(O_TTY_INIT) X(O_CLOEXEC) XEND
370 };
371 
372 static struct xlat shutdown_arg[] = {
373 	X(SHUT_RD) X(SHUT_WR) X(SHUT_RDWR) XEND
374 };
375 
376 static struct xlat resource_arg[] = {
377 	X(RLIMIT_CPU) X(RLIMIT_FSIZE) X(RLIMIT_DATA) X(RLIMIT_STACK)
378 	X(RLIMIT_CORE) X(RLIMIT_RSS) X(RLIMIT_MEMLOCK) X(RLIMIT_NPROC)
379 	X(RLIMIT_NOFILE) X(RLIMIT_SBSIZE) X(RLIMIT_VMEM) XEND
380 };
381 
382 static struct xlat pathconf_arg[] = {
383 	X(_PC_LINK_MAX)  X(_PC_MAX_CANON)  X(_PC_MAX_INPUT)
384 	X(_PC_NAME_MAX) X(_PC_PATH_MAX) X(_PC_PIPE_BUF)
385 	X(_PC_CHOWN_RESTRICTED) X(_PC_NO_TRUNC) X(_PC_VDISABLE)
386 	X(_PC_ASYNC_IO) X(_PC_PRIO_IO) X(_PC_SYNC_IO)
387 	X(_PC_ALLOC_SIZE_MIN) X(_PC_FILESIZEBITS)
388 	X(_PC_REC_INCR_XFER_SIZE) X(_PC_REC_MAX_XFER_SIZE)
389 	X(_PC_REC_MIN_XFER_SIZE) X(_PC_REC_XFER_ALIGN)
390 	X(_PC_SYMLINK_MAX) X(_PC_ACL_EXTENDED) X(_PC_ACL_PATH_MAX)
391 	X(_PC_CAP_PRESENT) X(_PC_INF_PRESENT) X(_PC_MAC_PRESENT)
392 	XEND
393 };
394 
395 static struct xlat rfork_flags[] = {
396 	X(RFPROC) X(RFNOWAIT) X(RFFDG) X(RFCFDG) X(RFTHREAD) X(RFMEM)
397 	X(RFSIGSHARE) X(RFTSIGZMB) X(RFLINUXTHPN) XEND
398 };
399 
400 static struct xlat wait_options[] = {
401 	X(WNOHANG) X(WUNTRACED) X(WCONTINUED) X(WNOWAIT) X(WEXITED)
402 	X(WTRAPPED) XEND
403 };
404 
405 static struct xlat idtype_arg[] = {
406 	X(P_PID) X(P_PPID) X(P_PGID) X(P_SID) X(P_CID) X(P_UID) X(P_GID)
407 	X(P_ALL) X(P_LWPID) X(P_TASKID) X(P_PROJID) X(P_POOLID) X(P_JAILID)
408 	X(P_CTID) X(P_CPUID) X(P_PSETID) XEND
409 };
410 
411 static struct xlat procctl_arg[] = {
412 	X(PROC_SPROTECT) XEND
413 };
414 
415 #undef X
416 #undef XEND
417 
418 /*
419  * Searches an xlat array for a value, and returns it if found.  Otherwise
420  * return a string representation.
421  */
422 static const char *
423 lookup(struct xlat *xlat, int val, int base)
424 {
425 	static char tmp[16];
426 
427 	for (; xlat->str != NULL; xlat++)
428 		if (xlat->val == val)
429 			return (xlat->str);
430 	switch (base) {
431 		case 8:
432 			sprintf(tmp, "0%o", val);
433 			break;
434 		case 16:
435 			sprintf(tmp, "0x%x", val);
436 			break;
437 		case 10:
438 			sprintf(tmp, "%u", val);
439 			break;
440 		default:
441 			errx(1,"Unknown lookup base");
442 			break;
443 	}
444 	return (tmp);
445 }
446 
447 static const char *
448 xlookup(struct xlat *xlat, int val)
449 {
450 
451 	return (lookup(xlat, val, 16));
452 }
453 
454 /* Searches an xlat array containing bitfield values.  Remaining bits
455    set after removing the known ones are printed at the end:
456    IN|0x400 */
457 static char *
458 xlookup_bits(struct xlat *xlat, int val)
459 {
460 	int len, rem;
461 	static char str[512];
462 
463 	len = 0;
464 	rem = val;
465 	for (; xlat->str != NULL; xlat++) {
466 		if ((xlat->val & rem) == xlat->val) {
467 			/* don't print the "all-bits-zero" string unless all
468 			   bits are really zero */
469 			if (xlat->val == 0 && val != 0)
470 				continue;
471 			len += sprintf(str + len, "%s|", xlat->str);
472 			rem &= ~(xlat->val);
473 		}
474 	}
475 	/* if we have leftover bits or didn't match anything */
476 	if (rem || len == 0)
477 		len += sprintf(str + len, "0x%x", rem);
478 	if (len && str[len - 1] == '|')
479 		len--;
480 	str[len] = 0;
481 	return (str);
482 }
483 
484 /*
485  * If/when the list gets big, it might be desirable to do it
486  * as a hash table or binary search.
487  */
488 
489 struct syscall *
490 get_syscall(const char *name)
491 {
492 	struct syscall *sc;
493 
494 	sc = syscalls;
495 	if (name == NULL)
496 		return (NULL);
497 	while (sc->name) {
498 		if (strcmp(name, sc->name) == 0)
499 			return (sc);
500 		sc++;
501 	}
502 	return (NULL);
503 }
504 
505 /*
506  * get_struct
507  *
508  * Copy a fixed amount of bytes from the process.
509  */
510 
511 static int
512 get_struct(pid_t pid, void *offset, void *buf, int len)
513 {
514 	struct ptrace_io_desc iorequest;
515 
516 	iorequest.piod_op = PIOD_READ_D;
517 	iorequest.piod_offs = offset;
518 	iorequest.piod_addr = buf;
519 	iorequest.piod_len = len;
520 	if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0)
521 		return (-1);
522 	return (0);
523 }
524 
525 #define	MAXSIZE		4096
526 #define	BLOCKSIZE	1024
527 /*
528  * get_string
529  * Copy a string from the process.  Note that it is
530  * expected to be a C string, but if max is set, it will
531  * only get that much.
532  */
533 
534 static char *
535 get_string(pid_t pid, void *offset, int max)
536 {
537 	struct ptrace_io_desc iorequest;
538 	char *buf;
539 	int diff, i, size, totalsize;
540 
541 	diff = 0;
542 	totalsize = size = max ? (max + 1) : BLOCKSIZE;
543 	buf = malloc(totalsize);
544 	if (buf == NULL)
545 		return (NULL);
546 	for (;;) {
547 		diff = totalsize - size;
548 		iorequest.piod_op = PIOD_READ_D;
549 		iorequest.piod_offs = (char *)offset + diff;
550 		iorequest.piod_addr = buf + diff;
551 		iorequest.piod_len = size;
552 		if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0) {
553 			free(buf);
554 			return (NULL);
555 		}
556 		for (i = 0 ; i < size; i++) {
557 			if (buf[diff + i] == '\0')
558 				return (buf);
559 		}
560 		if (totalsize < MAXSIZE - BLOCKSIZE && max == 0) {
561 			totalsize += BLOCKSIZE;
562 			buf = realloc(buf, totalsize);
563 			size = BLOCKSIZE;
564 		} else {
565 			buf[totalsize - 1] = '\0';
566 			return (buf);
567 		}
568 	}
569 }
570 
571 static char *
572 strsig2(int sig)
573 {
574 	char *tmp;
575 
576 	tmp = strsig(sig);
577 	if (tmp == NULL)
578 		asprintf(&tmp, "%d", sig);
579 	return (tmp);
580 }
581 
582 /*
583  * print_arg
584  * Converts a syscall argument into a string.  Said string is
585  * allocated via malloc(), so needs to be free()'d.  The file
586  * descriptor is for the process' memory (via /proc), and is used
587  * to get any data (where the argument is a pointer).  sc is
588  * a pointer to the syscall description (see above); args is
589  * an array of all of the system call arguments.
590  */
591 
592 char *
593 print_arg(struct syscall_args *sc, unsigned long *args, long retval,
594     struct trussinfo *trussinfo)
595 {
596 	char *tmp;
597 	pid_t pid;
598 
599 	tmp = NULL;
600 	pid = trussinfo->pid;
601 	switch (sc->type & ARG_MASK) {
602 	case Hex:
603 		asprintf(&tmp, "0x%x", (int)args[sc->offset]);
604 		break;
605 	case Octal:
606 		asprintf(&tmp, "0%o", (int)args[sc->offset]);
607 		break;
608 	case Int:
609 		asprintf(&tmp, "%d", (int)args[sc->offset]);
610 		break;
611 	case Name: {
612 		/* NULL-terminated string. */
613 		char *tmp2;
614 		tmp2 = get_string(pid, (void*)args[sc->offset], 0);
615 		asprintf(&tmp, "\"%s\"", tmp2);
616 		free(tmp2);
617 		break;
618 	}
619 	case BinString: {
620 		/* Binary block of data that might have printable characters.
621 		   XXX If type|OUT, assume that the length is the syscall's
622 		   return value.  Otherwise, assume that the length of the block
623 		   is in the next syscall argument. */
624 		int max_string = trussinfo->strsize;
625 		char tmp2[max_string+1], *tmp3;
626 		int len;
627 		int truncated = 0;
628 
629 		if (sc->type & OUT)
630 			len = retval;
631 		else
632 			len = args[sc->offset + 1];
633 
634 		/* Don't print more than max_string characters, to avoid word
635 		   wrap.  If we have to truncate put some ... after the string.
636 		*/
637 		if (len > max_string) {
638 			len = max_string;
639 			truncated = 1;
640 		}
641 		if (len && get_struct(pid, (void*)args[sc->offset], &tmp2, len)
642 		    != -1) {
643 			tmp3 = malloc(len * 4 + 1);
644 			while (len) {
645 				if (strvisx(tmp3, tmp2, len,
646 				    VIS_CSTYLE|VIS_TAB|VIS_NL) <= max_string)
647 					break;
648 				len--;
649 				truncated = 1;
650 			};
651 			asprintf(&tmp, "\"%s\"%s", tmp3, truncated ?
652 			    "..." : "");
653 			free(tmp3);
654 		} else {
655 			asprintf(&tmp, "0x%lx", args[sc->offset]);
656 		}
657 		break;
658 	}
659 	case StringArray: {
660 		int num, size, i;
661 		char *tmp2;
662 		char *string;
663 		char *strarray[100];	/* XXX This is ugly. */
664 
665 		if (get_struct(pid, (void *)args[sc->offset],
666 		    (void *)&strarray, sizeof(strarray)) == -1)
667 			err(1, "get_struct %p", (void *)args[sc->offset]);
668 		num = 0;
669 		size = 0;
670 
671 		/* Find out how large of a buffer we'll need. */
672 		while (strarray[num] != NULL) {
673 			string = get_string(pid, (void*)strarray[num], 0);
674 			size += strlen(string);
675 			free(string);
676 			num++;
677 		}
678 		size += 4 + (num * 4);
679 		tmp = (char *)malloc(size);
680 		tmp2 = tmp;
681 
682 		tmp2 += sprintf(tmp2, " [");
683 		for (i = 0; i < num; i++) {
684 			string = get_string(pid, (void*)strarray[i], 0);
685 			tmp2 += sprintf(tmp2, " \"%s\"%c", string,
686 			    (i + 1 == num) ? ' ' : ',');
687 			free(string);
688 		}
689 		tmp2 += sprintf(tmp2, "]");
690 		break;
691 	}
692 #ifdef __LP64__
693 	case Quad:
694 		asprintf(&tmp, "0x%lx", args[sc->offset]);
695 		break;
696 #else
697 	case Quad: {
698 		unsigned long long ll;
699 		ll = *(unsigned long long *)(args + sc->offset);
700 		asprintf(&tmp, "0x%llx", ll);
701 		break;
702 	}
703 #endif
704 	case Ptr:
705 		asprintf(&tmp, "0x%lx", args[sc->offset]);
706 		break;
707 	case Readlinkres: {
708 		char *tmp2;
709 		if (retval == -1) {
710 			tmp = strdup("");
711 			break;
712 		}
713 		tmp2 = get_string(pid, (void*)args[sc->offset], retval);
714 		asprintf(&tmp, "\"%s\"", tmp2);
715 		free(tmp2);
716 		break;
717 	}
718 	case Ioctl: {
719 		const char *temp = ioctlname(args[sc->offset]);
720 		if (temp)
721 			tmp = strdup(temp);
722 		else {
723 			unsigned long arg = args[sc->offset];
724 			asprintf(&tmp, "0x%lx { IO%s%s 0x%lx('%c'), %lu, %lu }",
725 			    arg, arg & IOC_OUT ? "R" : "",
726 			    arg & IOC_IN ? "W" : "", IOCGROUP(arg),
727 			    isprint(IOCGROUP(arg)) ? (char)IOCGROUP(arg) : '?',
728 			    arg & 0xFF, IOCPARM_LEN(arg));
729 		}
730 		break;
731 	}
732 	case Timespec: {
733 		struct timespec ts;
734 		if (get_struct(pid, (void *)args[sc->offset], &ts,
735 		    sizeof(ts)) != -1)
736 			asprintf(&tmp, "{%ld.%09ld }", (long)ts.tv_sec,
737 			    ts.tv_nsec);
738 		else
739 			asprintf(&tmp, "0x%lx", args[sc->offset]);
740 		break;
741 	}
742 	case Timeval: {
743 		struct timeval tv;
744 		if (get_struct(pid, (void *)args[sc->offset], &tv, sizeof(tv))
745 		    != -1)
746 			asprintf(&tmp, "{%ld.%06ld }", (long)tv.tv_sec,
747 			    tv.tv_usec);
748 		else
749 			asprintf(&tmp, "0x%lx", args[sc->offset]);
750 		break;
751 	}
752 	case Timeval2: {
753 		struct timeval tv[2];
754 		if (get_struct(pid, (void *)args[sc->offset], &tv, sizeof(tv))
755 		    != -1)
756 			asprintf(&tmp, "{%ld.%06ld, %ld.%06ld }",
757 			    (long)tv[0].tv_sec, tv[0].tv_usec,
758 			    (long)tv[1].tv_sec, tv[1].tv_usec);
759 		else
760 			asprintf(&tmp, "0x%lx", args[sc->offset]);
761 		break;
762 	}
763 	case Itimerval: {
764 		struct itimerval itv;
765 		if (get_struct(pid, (void *)args[sc->offset], &itv,
766 		    sizeof(itv)) != -1)
767 			asprintf(&tmp, "{%ld.%06ld, %ld.%06ld }",
768 			    (long)itv.it_interval.tv_sec,
769 			    itv.it_interval.tv_usec,
770 			    (long)itv.it_value.tv_sec,
771 			    itv.it_value.tv_usec);
772 		else
773 			asprintf(&tmp, "0x%lx", args[sc->offset]);
774 		break;
775 	}
776 	case LinuxSockArgs:
777 	{
778 		struct linux_socketcall_args largs;
779 		if (get_struct(pid, (void *)args[sc->offset], (void *)&largs,
780 		    sizeof(largs)) == -1) {
781 			err(1, "get_struct %p", (void *)args[sc->offset]);
782 		}
783 		const char *what;
784 		char buf[30];
785 
786 		switch (largs.what) {
787 		case LINUX_SOCKET:
788 			what = "LINUX_SOCKET";
789 			break;
790 		case LINUX_BIND:
791 			what = "LINUX_BIND";
792 			break;
793 		case LINUX_CONNECT:
794 			what = "LINUX_CONNECT";
795 			break;
796 		case LINUX_LISTEN:
797 			what = "LINUX_LISTEN";
798 			break;
799 		case LINUX_ACCEPT:
800 			what = "LINUX_ACCEPT";
801 			break;
802 		case LINUX_GETSOCKNAME:
803 			what = "LINUX_GETSOCKNAME";
804 			break;
805 		case LINUX_GETPEERNAME:
806 			what = "LINUX_GETPEERNAME";
807 			break;
808 		case LINUX_SOCKETPAIR:
809 			what = "LINUX_SOCKETPAIR";
810 			break;
811 		case LINUX_SEND:
812 			what = "LINUX_SEND";
813 			break;
814 		case LINUX_RECV:
815 			what = "LINUX_RECV";
816 			break;
817 		case LINUX_SENDTO:
818 			what = "LINUX_SENDTO";
819 			break;
820 		case LINUX_RECVFROM:
821 			what = "LINUX_RECVFROM";
822 			break;
823 		case LINUX_SHUTDOWN:
824 			what = "LINUX_SHUTDOWN";
825 			break;
826 		case LINUX_SETSOCKOPT:
827 			what = "LINUX_SETSOCKOPT";
828 			break;
829 		case LINUX_GETSOCKOPT:
830 			what = "LINUX_GETSOCKOPT";
831 			break;
832 		case LINUX_SENDMSG:
833 			what = "LINUX_SENDMSG";
834 			break;
835 		case LINUX_RECVMSG:
836 			what = "LINUX_RECVMSG";
837 			break;
838 		default:
839 			sprintf(buf, "%d", largs.what);
840 			what = buf;
841 			break;
842 		}
843 		asprintf(&tmp, "(0x%lx)%s, 0x%lx", args[sc->offset], what, (long unsigned int)largs.args);
844 		break;
845 	}
846 	case Pollfd: {
847 		/*
848 		 * XXX: A Pollfd argument expects the /next/ syscall argument
849 		 * to be the number of fds in the array. This matches the poll
850 		 * syscall.
851 		 */
852 		struct pollfd *pfd;
853 		int numfds = args[sc->offset+1];
854 		int bytes = sizeof(struct pollfd) * numfds;
855 		int i, tmpsize, u, used;
856 		const int per_fd = 100;
857 
858 		if ((pfd = malloc(bytes)) == NULL)
859 			err(1, "Cannot malloc %d bytes for pollfd array",
860 			    bytes);
861 		if (get_struct(pid, (void *)args[sc->offset], pfd, bytes)
862 		    != -1) {
863 			used = 0;
864 			tmpsize = 1 + per_fd * numfds + 2;
865 			if ((tmp = malloc(tmpsize)) == NULL)
866 				err(1, "Cannot alloc %d bytes for poll output",
867 				    tmpsize);
868 
869 			tmp[used++] = '{';
870 			for (i = 0; i < numfds; i++) {
871 
872 				u = snprintf(tmp + used, per_fd, "%s%d/%s",
873 				    i > 0 ? " " : "", pfd[i].fd,
874 				    xlookup_bits(poll_flags, pfd[i].events));
875 				if (u > 0)
876 					used += u < per_fd ? u : per_fd;
877 			}
878 			tmp[used++] = '}';
879 			tmp[used++] = '\0';
880 		} else {
881 			asprintf(&tmp, "0x%lx", args[sc->offset]);
882 		}
883 		free(pfd);
884 		break;
885 	}
886 	case Fd_set: {
887 		/*
888 		 * XXX: A Fd_set argument expects the /first/ syscall argument
889 		 * to be the number of fds in the array.  This matches the
890 		 * select syscall.
891 		 */
892 		fd_set *fds;
893 		int numfds = args[0];
894 		int bytes = _howmany(numfds, _NFDBITS) * _NFDBITS;
895 		int i, tmpsize, u, used;
896 		const int per_fd = 20;
897 
898 		if ((fds = malloc(bytes)) == NULL)
899 			err(1, "Cannot malloc %d bytes for fd_set array",
900 			    bytes);
901 		if (get_struct(pid, (void *)args[sc->offset], fds, bytes)
902 		    != -1) {
903 			used = 0;
904 			tmpsize = 1 + numfds * per_fd + 2;
905 			if ((tmp = malloc(tmpsize)) == NULL)
906 				err(1, "Cannot alloc %d bytes for fd_set "
907 				    "output", tmpsize);
908 
909 			tmp[used++] = '{';
910 			for (i = 0; i < numfds; i++) {
911 				if (FD_ISSET(i, fds)) {
912 					u = snprintf(tmp + used, per_fd, "%d ",
913 					    i);
914 					if (u > 0)
915 						used += u < per_fd ? u : per_fd;
916 				}
917 			}
918 			if (tmp[used-1] == ' ')
919 				used--;
920 			tmp[used++] = '}';
921 			tmp[used++] = '\0';
922 		} else
923 			asprintf(&tmp, "0x%lx", args[sc->offset]);
924 		free(fds);
925 		break;
926 	}
927 	case Signal:
928 		tmp = strsig2(args[sc->offset]);
929 		break;
930 	case Sigset: {
931 		long sig;
932 		sigset_t ss;
933 		int i, used;
934 		char *signame;
935 
936 		sig = args[sc->offset];
937 		if (get_struct(pid, (void *)args[sc->offset], (void *)&ss,
938 		    sizeof(ss)) == -1) {
939 			asprintf(&tmp, "0x%lx", args[sc->offset]);
940 			break;
941 		}
942 		tmp = malloc(sys_nsig * 8); /* 7 bytes avg per signal name */
943 		used = 0;
944 		for (i = 1; i < sys_nsig; i++) {
945 			if (sigismember(&ss, i)) {
946 				signame = strsig(i);
947 				used += sprintf(tmp + used, "%s|", signame);
948 				free(signame);
949 			}
950 		}
951 		if (used)
952 			tmp[used-1] = 0;
953 		else
954 			strcpy(tmp, "0x0");
955 		break;
956 	}
957 	case Sigprocmask: {
958 		switch (args[sc->offset]) {
959 #define	S(a)	case a: tmp = strdup(#a); break;
960 			S(SIG_BLOCK);
961 			S(SIG_UNBLOCK);
962 			S(SIG_SETMASK);
963 #undef S
964 		}
965 		if (tmp == NULL)
966 			asprintf(&tmp, "0x%lx", args[sc->offset]);
967 		break;
968 	}
969 	case Fcntlflag: {
970 		/* XXX output depends on the value of the previous argument */
971 		switch (args[sc->offset-1]) {
972 		case F_SETFD:
973 			tmp = strdup(xlookup_bits(fcntlfd_arg,
974 			    args[sc->offset]));
975 			break;
976 		case F_SETFL:
977 			tmp = strdup(xlookup_bits(fcntlfl_arg,
978 			    args[sc->offset]));
979 			break;
980 		case F_GETFD:
981 		case F_GETFL:
982 		case F_GETOWN:
983 			tmp = strdup("");
984 			break;
985 		default:
986 			asprintf(&tmp, "0x%lx", args[sc->offset]);
987 			break;
988 		}
989 		break;
990 	}
991 	case Open:
992 		tmp = strdup(xlookup_bits(open_flags, args[sc->offset]));
993 		break;
994 	case Fcntl:
995 		tmp = strdup(xlookup(fcntl_arg, args[sc->offset]));
996 		break;
997 	case Mprot:
998 		tmp = strdup(xlookup_bits(mprot_flags, args[sc->offset]));
999 		break;
1000 	case Mmapflags: {
1001 		char *base, *alignstr;
1002 		int align, flags;
1003 
1004 		/*
1005 		 * MAP_ALIGNED can't be handled by xlookup_bits(), so
1006 		 * generate that string manually and prepend it to the
1007 		 * string from xlookup_bits().  Have to be careful to
1008 		 * avoid outputting MAP_ALIGNED|0 if MAP_ALIGNED is
1009 		 * the only flag.
1010 		 */
1011 		flags = args[sc->offset] & ~MAP_ALIGNMENT_MASK;
1012 		align = args[sc->offset] & MAP_ALIGNMENT_MASK;
1013 		if (align != 0) {
1014 			if (align == MAP_ALIGNED_SUPER)
1015 				alignstr = strdup("MAP_ALIGNED_SUPER");
1016 			else
1017 				asprintf(&alignstr, "MAP_ALIGNED(%d)",
1018 				    align >> MAP_ALIGNMENT_SHIFT);
1019 			if (flags == 0) {
1020 				tmp = alignstr;
1021 				break;
1022 			}
1023 		} else
1024 			alignstr = NULL;
1025 		base = strdup(xlookup_bits(mmap_flags, flags));
1026 		if (alignstr == NULL) {
1027 			tmp = base;
1028 			break;
1029 		}
1030 		asprintf(&tmp, "%s|%s", alignstr, base);
1031 		free(alignstr);
1032 		free(base);
1033 		break;
1034 	}
1035 	case Whence:
1036 		tmp = strdup(xlookup(whence_arg, args[sc->offset]));
1037 		break;
1038 	case Sockdomain:
1039 		tmp = strdup(xlookup(sockdomain_arg, args[sc->offset]));
1040 		break;
1041 	case Socktype:
1042 		tmp = strdup(xlookup(socktype_arg, args[sc->offset]));
1043 		break;
1044 	case Shutdown:
1045 		tmp = strdup(xlookup(shutdown_arg, args[sc->offset]));
1046 		break;
1047 	case Resource:
1048 		tmp = strdup(xlookup(resource_arg, args[sc->offset]));
1049 		break;
1050 	case Pathconf:
1051 		tmp = strdup(xlookup(pathconf_arg, args[sc->offset]));
1052 		break;
1053 	case Rforkflags:
1054 		tmp = strdup(xlookup_bits(rfork_flags, args[sc->offset]));
1055 		break;
1056 	case Sockaddr: {
1057 		struct sockaddr_storage ss;
1058 		char addr[64];
1059 		struct sockaddr_in *lsin;
1060 		struct sockaddr_in6 *lsin6;
1061 		struct sockaddr_un *sun;
1062 		struct sockaddr *sa;
1063 		char *p;
1064 		u_char *q;
1065 		int i;
1066 
1067 		if (args[sc->offset] == 0) {
1068 			asprintf(&tmp, "NULL");
1069 			break;
1070 		}
1071 
1072 		/* yuck: get ss_len */
1073 		if (get_struct(pid, (void *)args[sc->offset], (void *)&ss,
1074 		    sizeof(ss.ss_len) + sizeof(ss.ss_family)) == -1)
1075 			err(1, "get_struct %p", (void *)args[sc->offset]);
1076 		/*
1077 		 * If ss_len is 0, then try to guess from the sockaddr type.
1078 		 * AF_UNIX may be initialized incorrectly, so always frob
1079 		 * it by using the "right" size.
1080 		 */
1081 		if (ss.ss_len == 0 || ss.ss_family == AF_UNIX) {
1082 			switch (ss.ss_family) {
1083 			case AF_INET:
1084 				ss.ss_len = sizeof(*lsin);
1085 				break;
1086 			case AF_UNIX:
1087 				ss.ss_len = sizeof(*sun);
1088 				break;
1089 			default:
1090 				/* hurrrr */
1091 				break;
1092 			}
1093 		}
1094 		if (get_struct(pid, (void *)args[sc->offset], (void *)&ss,
1095 		    ss.ss_len) == -1) {
1096 			err(2, "get_struct %p", (void *)args[sc->offset]);
1097 		}
1098 
1099 		switch (ss.ss_family) {
1100 		case AF_INET:
1101 			lsin = (struct sockaddr_in *)&ss;
1102 			inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof addr);
1103 			asprintf(&tmp, "{ AF_INET %s:%d }", addr,
1104 			    htons(lsin->sin_port));
1105 			break;
1106 		case AF_INET6:
1107 			lsin6 = (struct sockaddr_in6 *)&ss;
1108 			inet_ntop(AF_INET6, &lsin6->sin6_addr, addr,
1109 			    sizeof addr);
1110 			asprintf(&tmp, "{ AF_INET6 [%s]:%d }", addr,
1111 			    htons(lsin6->sin6_port));
1112 			break;
1113 		case AF_UNIX:
1114 			sun = (struct sockaddr_un *)&ss;
1115 			asprintf(&tmp, "{ AF_UNIX \"%s\" }", sun->sun_path);
1116 			break;
1117 		default:
1118 			sa = (struct sockaddr *)&ss;
1119 			asprintf(&tmp, "{ sa_len = %d, sa_family = %d, sa_data "
1120 			    "= {%n%*s } }", (int)sa->sa_len, (int)sa->sa_family,
1121 			    &i, 6 * (int)(sa->sa_len - ((char *)&sa->sa_data -
1122 			    (char *)sa)), "");
1123 			if (tmp != NULL) {
1124 				p = tmp + i;
1125 				for (q = (u_char *)&sa->sa_data;
1126 				    q < (u_char *)sa + sa->sa_len; q++)
1127 					p += sprintf(p, " %#02x,", *q);
1128 			}
1129 		}
1130 		break;
1131 	}
1132 	case Sigaction: {
1133 		struct sigaction sa;
1134 		char *hand;
1135 		const char *h;
1136 
1137 		if (get_struct(pid, (void *)args[sc->offset], &sa, sizeof(sa))
1138 		    != -1) {
1139 			asprintf(&hand, "%p", sa.sa_handler);
1140 			if (sa.sa_handler == SIG_DFL)
1141 				h = "SIG_DFL";
1142 			else if (sa.sa_handler == SIG_IGN)
1143 				h = "SIG_IGN";
1144 			else
1145 				h = hand;
1146 
1147 			asprintf(&tmp, "{ %s %s ss_t }", h,
1148 			    xlookup_bits(sigaction_flags, sa.sa_flags));
1149 			free(hand);
1150 		} else
1151 			asprintf(&tmp, "0x%lx", args[sc->offset]);
1152 		break;
1153 	}
1154 	case Kevent: {
1155 		/*
1156 		 * XXX XXX: the size of the array is determined by either the
1157 		 * next syscall argument, or by the syscall returnvalue,
1158 		 * depending on which argument number we are.  This matches the
1159 		 * kevent syscall, but luckily that's the only syscall that uses
1160 		 * them.
1161 		 */
1162 		struct kevent *ke;
1163 		int numevents = -1;
1164 		int bytes = 0;
1165 		int i, tmpsize, u, used;
1166 		const int per_ke = 100;
1167 
1168 		if (sc->offset == 1)
1169 			numevents = args[sc->offset+1];
1170 		else if (sc->offset == 3 && retval != -1)
1171 			numevents = retval;
1172 
1173 		if (numevents >= 0)
1174 			bytes = sizeof(struct kevent) * numevents;
1175 		if ((ke = malloc(bytes)) == NULL)
1176 			err(1, "Cannot malloc %d bytes for kevent array",
1177 			    bytes);
1178 		if (numevents >= 0 && get_struct(pid, (void *)args[sc->offset],
1179 		    ke, bytes) != -1) {
1180 			used = 0;
1181 			tmpsize = 1 + per_ke * numevents + 2;
1182 			if ((tmp = malloc(tmpsize)) == NULL)
1183 				err(1, "Cannot alloc %d bytes for kevent "
1184 				    "output", tmpsize);
1185 
1186 			tmp[used++] = '{';
1187 			for (i = 0; i < numevents; i++) {
1188 				u = snprintf(tmp + used, per_ke,
1189 				    "%s%p,%s,%s,%d,%p,%p",
1190 				    i > 0 ? " " : "",
1191 				    (void *)ke[i].ident,
1192 				    xlookup(kevent_filters, ke[i].filter),
1193 				    xlookup_bits(kevent_flags, ke[i].flags),
1194 				    ke[i].fflags,
1195 				    (void *)ke[i].data,
1196 				    (void *)ke[i].udata);
1197 				if (u > 0)
1198 					used += u < per_ke ? u : per_ke;
1199 			}
1200 			tmp[used++] = '}';
1201 			tmp[used++] = '\0';
1202 		} else {
1203 			asprintf(&tmp, "0x%lx", args[sc->offset]);
1204 		}
1205 		free(ke);
1206 		break;
1207 	}
1208 	case Stat: {
1209 		struct stat st;
1210 		if (get_struct(pid, (void *)args[sc->offset], &st, sizeof(st))
1211 		    != -1) {
1212 			char mode[12];
1213 			strmode(st.st_mode, mode);
1214 			asprintf(&tmp,
1215 			    "{ mode=%s,inode=%jd,size=%jd,blksize=%ld }", mode,
1216 			    (intmax_t)st.st_ino, (intmax_t)st.st_size,
1217 			    (long)st.st_blksize);
1218 		} else {
1219 			asprintf(&tmp, "0x%lx", args[sc->offset]);
1220 		}
1221 		break;
1222 	}
1223 	case Rusage: {
1224 		struct rusage ru;
1225 		if (get_struct(pid, (void *)args[sc->offset], &ru, sizeof(ru))
1226 		    != -1) {
1227 			asprintf(&tmp,
1228 			    "{ u=%ld.%06ld,s=%ld.%06ld,in=%ld,out=%ld }",
1229 			    (long)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec,
1230 			    (long)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec,
1231 			    ru.ru_inblock, ru.ru_oublock);
1232 		} else
1233 			asprintf(&tmp, "0x%lx", args[sc->offset]);
1234 		break;
1235 	}
1236 	case Rlimit: {
1237 		struct rlimit rl;
1238 		if (get_struct(pid, (void *)args[sc->offset], &rl, sizeof(rl))
1239 		    != -1) {
1240 			asprintf(&tmp, "{ cur=%ju,max=%ju }",
1241 			    rl.rlim_cur, rl.rlim_max);
1242 		} else
1243 			asprintf(&tmp, "0x%lx", args[sc->offset]);
1244 		break;
1245 	}
1246 	case ExitStatus: {
1247 		char *signame;
1248 		int status;
1249 		signame = NULL;
1250 		if (get_struct(pid, (void *)args[sc->offset], &status,
1251 		    sizeof(status)) != -1) {
1252 			if (WIFCONTINUED(status))
1253 				tmp = strdup("{ CONTINUED }");
1254 			else if (WIFEXITED(status))
1255 				asprintf(&tmp, "{ EXITED,val=%d }",
1256 				    WEXITSTATUS(status));
1257 			else if (WIFSIGNALED(status))
1258 				asprintf(&tmp, "{ SIGNALED,sig=%s%s }",
1259 				    signame = strsig2(WTERMSIG(status)),
1260 				    WCOREDUMP(status) ? ",cored" : "");
1261 			else
1262 				asprintf(&tmp, "{ STOPPED,sig=%s }",
1263 				    signame = strsig2(WTERMSIG(status)));
1264 		} else
1265 			asprintf(&tmp, "0x%lx", args[sc->offset]);
1266 		free(signame);
1267 		break;
1268 	}
1269 	case Waitoptions:
1270 		tmp = strdup(xlookup_bits(wait_options, args[sc->offset]));
1271 		break;
1272 	case Idtype:
1273 		tmp = strdup(xlookup(idtype_arg, args[sc->offset]));
1274 		break;
1275 	case Procctl:
1276 		tmp = strdup(xlookup(procctl_arg, args[sc->offset]));
1277 		break;
1278 	default:
1279 		errx(1, "Invalid argument type %d\n", sc->type & ARG_MASK);
1280 	}
1281 	return (tmp);
1282 }
1283 
1284 /*
1285  * print_syscall
1286  * Print (to outfile) the system call and its arguments.  Note that
1287  * nargs is the number of arguments (not the number of words; this is
1288  * potentially confusing, I know).
1289  */
1290 
1291 void
1292 print_syscall(struct trussinfo *trussinfo, const char *name, int nargs,
1293     char **s_args)
1294 {
1295 	struct timespec timediff;
1296 	int i, len;
1297 
1298 	len = 0;
1299 	if (trussinfo->flags & FOLLOWFORKS)
1300 		len += fprintf(trussinfo->outfile, "%5d: ", trussinfo->pid);
1301 
1302 	if (name != NULL && (strcmp(name, "execve") == 0 ||
1303 	    strcmp(name, "exit") == 0)) {
1304 		clock_gettime(CLOCK_REALTIME, &trussinfo->curthread->after);
1305 	}
1306 
1307 	if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
1308 		timespecsubt(&trussinfo->curthread->after,
1309 		    &trussinfo->start_time, &timediff);
1310 		len += fprintf(trussinfo->outfile, "%ld.%09ld ",
1311 		    (long)timediff.tv_sec, timediff.tv_nsec);
1312 	}
1313 
1314 	if (trussinfo->flags & RELATIVETIMESTAMPS) {
1315 		timespecsubt(&trussinfo->curthread->after,
1316 		    &trussinfo->curthread->before, &timediff);
1317 		len += fprintf(trussinfo->outfile, "%ld.%09ld ",
1318 		    (long)timediff.tv_sec, timediff.tv_nsec);
1319 	}
1320 
1321 	len += fprintf(trussinfo->outfile, "%s(", name);
1322 
1323 	for (i = 0; i < nargs; i++) {
1324 		if (s_args[i])
1325 			len += fprintf(trussinfo->outfile, "%s", s_args[i]);
1326 		else
1327 			len += fprintf(trussinfo->outfile,
1328 			    "<missing argument>");
1329 		len += fprintf(trussinfo->outfile, "%s", i < (nargs - 1) ?
1330 		    "," : "");
1331 	}
1332 	len += fprintf(trussinfo->outfile, ")");
1333 	for (i = 0; i < 6 - (len / 8); i++)
1334 		fprintf(trussinfo->outfile, "\t");
1335 }
1336 
1337 void
1338 print_syscall_ret(struct trussinfo *trussinfo, const char *name, int nargs,
1339     char **s_args, int errorp, long retval, struct syscall *sc)
1340 {
1341 	struct timespec timediff;
1342 
1343 	if (trussinfo->flags & COUNTONLY) {
1344 		if (!sc)
1345 			return;
1346 		clock_gettime(CLOCK_REALTIME, &trussinfo->curthread->after);
1347 		timespecsubt(&trussinfo->curthread->after,
1348 		    &trussinfo->curthread->before, &timediff);
1349 		timespecadd(&sc->time, &timediff, &sc->time);
1350 		sc->ncalls++;
1351 		if (errorp)
1352 			sc->nerror++;
1353 		return;
1354 	}
1355 
1356 	print_syscall(trussinfo, name, nargs, s_args);
1357 	fflush(trussinfo->outfile);
1358 	if (errorp)
1359 		fprintf(trussinfo->outfile, " ERR#%ld '%s'\n", retval,
1360 		    strerror(retval));
1361 	else {
1362 		/*
1363 		 * Because pipe(2) has a special assembly glue to provide the
1364 		 * libc API, we have to adjust retval.
1365 		 */
1366 		if (name != NULL && strcmp(name, "pipe") == 0)
1367 			retval = 0;
1368 		fprintf(trussinfo->outfile, " = %ld (0x%lx)\n", retval, retval);
1369 	}
1370 }
1371 
1372 void
1373 print_summary(struct trussinfo *trussinfo)
1374 {
1375 	struct timespec total = {0, 0};
1376 	struct syscall *sc;
1377 	int ncall, nerror;
1378 
1379 	fprintf(trussinfo->outfile, "%-20s%15s%8s%8s\n",
1380 	    "syscall", "seconds", "calls", "errors");
1381 	ncall = nerror = 0;
1382 	for (sc = syscalls; sc->name != NULL; sc++)
1383 		if (sc->ncalls) {
1384 			fprintf(trussinfo->outfile, "%-20s%5jd.%09ld%8d%8d\n",
1385 			    sc->name, (intmax_t)sc->time.tv_sec,
1386 			    sc->time.tv_nsec, sc->ncalls, sc->nerror);
1387 			timespecadd(&total, &sc->time, &total);
1388 			ncall += sc->ncalls;
1389 			nerror += sc->nerror;
1390 		}
1391 	fprintf(trussinfo->outfile, "%20s%15s%8s%8s\n",
1392 	    "", "-------------", "-------", "-------");
1393 	fprintf(trussinfo->outfile, "%-20s%5jd.%09ld%8d%8d\n",
1394 	    "", (intmax_t)total.tv_sec, total.tv_nsec, ncall, nerror);
1395 }
1396