xref: /freebsd/usr.bin/truss/syscalls.c (revision 830940567b49bb0c08dfaed40418999e76616909)
1 /*
2  * Copryight 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/mman.h>
43 #include <sys/types.h>
44 #include <sys/ptrace.h>
45 #include <sys/socket.h>
46 #include <sys/time.h>
47 #include <sys/un.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <sys/ioccom.h>
51 #include <machine/atomic.h>
52 #include <errno.h>
53 #include <sys/umtx.h>
54 #include <sys/event.h>
55 #include <sys/stat.h>
56 #include <sys/resource.h>
57 
58 #include <ctype.h>
59 #include <err.h>
60 #include <fcntl.h>
61 #include <poll.h>
62 #include <signal.h>
63 #include <stdint.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <time.h>
68 #include <unistd.h>
69 #include <vis.h>
70 
71 #include "truss.h"
72 #include "extern.h"
73 #include "syscall.h"
74 
75 /* 64-bit alignment on 32-bit platforms. */
76 #ifdef __powerpc__
77 #define	QUAD_ALIGN	1
78 #else
79 #define	QUAD_ALIGN	0
80 #endif
81 
82 /* Number of slots needed for a 64-bit argument. */
83 #ifdef __LP64__
84 #define	QUAD_SLOTS	1
85 #else
86 #define	QUAD_SLOTS	2
87 #endif
88 
89 /*
90  * This should probably be in its own file, sorted alphabetically.
91  */
92 struct syscall syscalls[] = {
93 	{ .name = "fcntl", .ret_type = 1, .nargs = 3,
94 	  .args = { { Int, 0 } , { Fcntl, 1 }, { Fcntlflag | OUT, 2 } } },
95 	{ .name = "readlink", .ret_type = 1, .nargs = 3,
96 	  .args = { { Name, 0 } , { Readlinkres | OUT, 1 }, { Int, 2 } } },
97 	{ .name = "lseek", .ret_type = 2, .nargs = 3,
98 	  .args = { { Int, 0 }, { Quad, 1 + QUAD_ALIGN }, { Whence, 1 + QUAD_SLOTS + QUAD_ALIGN } } },
99 	{ .name = "linux_lseek", .ret_type = 2, .nargs = 3,
100 	  .args = { { Int, 0 }, { Int, 1 }, { Whence, 2 } } },
101 	{ .name = "mmap", .ret_type = 2, .nargs = 6,
102 	  .args = { { Ptr, 0 }, { Int, 1 }, { Mprot, 2 }, { Mmapflags, 3 }, { Int, 4 }, { Quad, 5 + QUAD_ALIGN } } },
103 	{ .name = "mprotect", .ret_type = 1, .nargs = 3,
104 	  .args = { { Ptr, 0 }, { Int, 1 }, { Mprot, 2 } } },
105 	{ .name = "open", .ret_type = 1, .nargs = 3,
106 	  .args = { { Name | IN, 0 } , { Open, 1 }, { Octal, 2 } } },
107 	{ .name = "mkdir", .ret_type = 1, .nargs = 2,
108 	  .args = { { Name, 0 } , { Octal, 1 } } },
109 	{ .name = "linux_open", .ret_type = 1, .nargs = 3,
110 	  .args = { { Name, 0 }, { Hex, 1 }, { Octal, 2 } } },
111 	{ .name = "close", .ret_type = 1, .nargs = 1,
112 	  .args = { { Int, 0 } } },
113 	{ .name = "link", .ret_type = 0, .nargs = 2,
114 	  .args = { { Name, 0 }, { Name, 1 } } },
115 	{ .name = "unlink", .ret_type = 0, .nargs = 1,
116 	  .args = { { Name, 0 } } },
117 	{ .name = "chdir", .ret_type = 0, .nargs = 1,
118 	  .args = { { Name, 0 } } },
119 	{ .name = "chroot", .ret_type = 0, .nargs = 1,
120 	  .args = { { Name, 0 } } },
121 	{ .name = "mknod", .ret_type = 0, .nargs = 3,
122 	  .args = { { Name, 0 }, { Octal, 1 }, { Int, 3 } } },
123 	{ .name = "chmod", .ret_type = 0, .nargs = 2,
124 	  .args = { { Name, 0 }, { Octal, 1 } } },
125 	{ .name = "chown", .ret_type = 0, .nargs = 3,
126 	  .args = { { Name, 0 }, { Int, 1 }, { Int, 2 } } },
127 	{ .name = "mount", .ret_type = 0, .nargs = 4,
128 	  .args = { { Name, 0 }, { Name, 1 }, { Int, 2 }, { Ptr, 3 } } },
129 	{ .name = "umount", .ret_type = 0, .nargs = 2,
130 	  .args = { { Name, 0 }, { Int, 2 } } },
131 	{ .name = "fstat", .ret_type = 1, .nargs = 2,
132 	  .args = { { Int, 0 }, { Stat | OUT , 1 } } },
133 	{ .name = "stat", .ret_type = 1, .nargs = 2,
134 	  .args = { { Name | IN, 0 }, { Stat | OUT, 1 } } },
135 	{ .name = "lstat", .ret_type = 1, .nargs = 2,
136 	  .args = { { Name | IN, 0 }, { Stat | OUT, 1 } } },
137 	{ .name = "linux_newstat", .ret_type = 1, .nargs = 2,
138 	  .args = { { Name | IN, 0 }, { Ptr | OUT, 1 } } },
139 	{ .name = "linux_newfstat", .ret_type = 1, .nargs = 2,
140 	  .args = { { Int, 0 }, { Ptr | OUT, 1 } } },
141 	{ .name = "write", .ret_type = 1, .nargs = 3,
142 	  .args = { { Int, 0 }, { BinString | IN, 1 }, { Int, 2 } } },
143 	{ .name = "ioctl", .ret_type = 1, .nargs = 3,
144 	  .args = { { Int, 0 }, { Ioctl, 1 }, { Hex, 2 } } },
145 	{ .name = "break", .ret_type = 1, .nargs = 1,
146 	  .args = { { Ptr, 0 } } },
147 	{ .name = "exit", .ret_type = 0, .nargs = 1,
148 	  .args = { { Hex, 0 } } },
149 	{ .name = "access", .ret_type = 1, .nargs = 2,
150 	  .args = { { Name | IN, 0 }, { Int, 1 } } },
151 	{ .name = "sigaction", .ret_type = 1, .nargs = 3,
152 	  .args = { { Signal, 0 }, { Sigaction | IN, 1 }, { Sigaction | OUT, 2 } } },
153 	{ .name = "accept", .ret_type = 1, .nargs = 3,
154 	  .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
155 	{ .name = "bind", .ret_type = 1, .nargs = 3,
156 	  .args = { { Int, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
157 	{ .name = "connect", .ret_type = 1, .nargs = 3,
158 	  .args = { { Int, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
159 	{ .name = "getpeername", .ret_type = 1, .nargs = 3,
160 	  .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
161 	{ .name = "getsockname", .ret_type = 1, .nargs = 3,
162 	  .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
163 	{ .name = "recvfrom", .ret_type = 1, .nargs = 6,
164 	  .args = { { Int, 0 }, { BinString | OUT, 1 }, { Int, 2 }, { Hex, 3 }, { Sockaddr | OUT, 4 }, { Ptr | OUT, 5 } } },
165 	{ .name = "sendto", .ret_type = 1, .nargs = 6,
166 	  .args = { { Int, 0 }, { BinString | IN, 1 }, { Int, 2 }, { Hex, 3 }, { Sockaddr | IN, 4 }, { Ptr | IN, 5 } } },
167 	{ .name = "execve", .ret_type = 1, .nargs = 3,
168 	  .args = { { Name | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } },
169 	{ .name = "linux_execve", .ret_type = 1, .nargs = 3,
170 	  .args = { { Name | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } },
171 	{ .name = "kldload", .ret_type = 0, .nargs = 1,
172 	  .args = { { Name | IN, 0 } } },
173 	{ .name = "kldunload", .ret_type = 0, .nargs = 1,
174 	  .args = { { Int, 0 } } },
175 	{ .name = "kldfind", .ret_type = 0, .nargs = 1,
176 	  .args = { { Name | IN, 0 } } },
177 	{ .name = "kldnext", .ret_type = 0, .nargs = 1,
178 	  .args = { { Int, 0 } } },
179 	{ .name = "kldstat", .ret_type = 0, .nargs = 2,
180 	  .args = { { Int, 0 }, { Ptr, 1 } } },
181 	{ .name = "kldfirstmod", .ret_type = 0, .nargs = 1,
182 	  .args = { { Int, 0 } } },
183 	{ .name = "nanosleep", .ret_type = 0, .nargs = 1,
184 	  .args = { { Timespec, 0 } } },
185 	{ .name = "select", .ret_type = 1, .nargs = 5,
186 	  .args = { { Int, 0 }, { Fd_set, 1 }, { Fd_set, 2 }, { Fd_set, 3 }, { Timeval, 4 } } },
187 	{ .name = "poll", .ret_type = 1, .nargs = 3,
188 	  .args = { { Pollfd, 0 }, { Int, 1 }, { Int, 2 } } },
189 	{ .name = "gettimeofday", .ret_type = 1, .nargs = 2,
190 	  .args = { { Timeval | OUT, 0 }, { Ptr, 1 } } },
191 	{ .name = "clock_gettime", .ret_type = 1, .nargs = 2,
192 	  .args = { { Int, 0 }, { Timespec | OUT, 1 } } },
193 	{ .name = "getitimer", .ret_type = 1, .nargs = 2,
194 	  .args = { { Int, 0 }, { Itimerval | OUT, 2 } } },
195 	{ .name = "setitimer", .ret_type = 1, .nargs = 3,
196 	  .args = { { Int, 0 }, { Itimerval, 1 } , { Itimerval | OUT, 2 } } },
197 	{ .name = "kse_release", .ret_type = 0, .nargs = 1,
198 	  .args = { { Timespec, 0 } } },
199 	{ .name = "kevent", .ret_type = 0, .nargs = 6,
200 	  .args = { { Int, 0 }, { Kevent, 1 }, { Int, 2 }, { Kevent | OUT, 3 }, { Int, 4 }, { Timespec, 5 } } },
201 	{ .name = "_umtx_lock", .ret_type = 0, .nargs = 1,
202 	  .args = { { Umtx, 0 } } },
203 	{ .name = "_umtx_unlock", .ret_type = 0, .nargs = 1,
204 	  .args = { { Umtx, 0 } } },
205 	{ .name = "sigprocmask", .ret_type = 0, .nargs = 3,
206 	  .args = { { Sigprocmask, 0 }, { Sigset, 1 }, { Sigset | OUT, 2 } } },
207 	{ .name = "unmount", .ret_type = 1, .nargs = 2,
208 	  .args = { { Name, 0 }, { Int, 1 } } },
209 	{ .name = "socket", .ret_type = 1, .nargs = 3,
210 	  .args = { { Sockdomain, 0 }, { Socktype, 1 }, { Int, 2 } } },
211 	{ .name = "getrusage", .ret_type = 1, .nargs = 2,
212 	  .args = { { Int, 0 }, { Rusage | OUT, 1 } } },
213 	{ .name = "__getcwd", .ret_type = 1, .nargs = 2,
214 	  .args = { { Name | OUT, 0 }, { Int, 1 } } },
215 	{ .name = "shutdown", .ret_type = 1, .nargs = 2,
216 	  .args = { { Int, 0 }, { Shutdown, 1 } } },
217 	{ .name = "getrlimit", .ret_type = 1, .nargs = 2,
218 	  .args = { { Resource, 0 }, { Rlimit | OUT, 1 } } },
219 	{ .name = "setrlimit", .ret_type = 1, .nargs = 2,
220 	  .args = { { Resource, 0 }, { Rlimit | IN, 1 } } },
221 	{ .name = "utimes", .ret_type = 1, .nargs = 2,
222 	  .args = { { Name | IN, 0 }, { Timeval2 | IN, 1 } } },
223 	{ .name = "lutimes", .ret_type = 1, .nargs = 2,
224 	  .args = { { Name | IN, 0 }, { Timeval2 | IN, 1 } } },
225 	{ .name = "futimes", .ret_type = 1, .nargs = 2,
226 	  .args = { { Int, 0 }, { Timeval | IN, 1 } } },
227 	{ .name = "chflags", .ret_type = 1, .nargs = 2,
228 	  .args = { { Name | IN, 0 }, { Hex, 1 } } },
229 	{ .name = "lchflags", .ret_type = 1, .nargs = 2,
230 	  .args = { { Name | IN, 0 }, { Hex, 1 } } },
231 	{ .name = "pathconf", .ret_type = 1, .nargs = 2,
232 	  .args = { { Name | IN, 0 }, { Pathconf, 1 } } },
233 	{ .name = "truncate", .ret_type = 1, .nargs = 3,
234 	  .args = { { Name | IN, 0 }, { Int | IN, 1 }, { Quad | IN, 2 } } },
235 	{ .name = "ftruncate", .ret_type = 1, .nargs = 3,
236 	  .args = { { Int | IN, 0 }, { Int | IN, 1 }, { Quad | IN, 2 } } },
237 	{ .name = "kill", .ret_type = 1, .nargs = 2,
238 	  .args = { { Int | IN, 0 }, { Signal | IN, 1 } } },
239 	{ .name = "munmap", .ret_type = 1, .nargs = 2,
240 	  .args = { { Ptr, 0 }, { Int, 1 } } },
241 	{ .name = "read", .ret_type = 1, .nargs = 3,
242 	  .args = { { Int, 0 }, { BinString | OUT, 1 }, { Int, 2 } } },
243 	{ .name = "rename", .ret_type = 1, .nargs = 2,
244 	  .args = { { Name , 0 } , { Name, 1 } } },
245 	{ .name = "symlink", .ret_type = 1, .nargs = 2,
246 	  .args = { { Name , 0 } , { Name, 1 } } },
247 	{ .name = 0 },
248 };
249 
250 /* Xlat idea taken from strace */
251 struct xlat {
252 	int val;
253 	const char *str;
254 };
255 
256 #define X(a) { a, #a },
257 #define XEND { 0, NULL }
258 
259 static struct xlat kevent_filters[] = {
260 	X(EVFILT_READ) X(EVFILT_WRITE) X(EVFILT_AIO) X(EVFILT_VNODE)
261 	X(EVFILT_PROC) X(EVFILT_SIGNAL) X(EVFILT_TIMER)
262 	X(EVFILT_NETDEV) X(EVFILT_FS) X(EVFILT_READ) XEND
263 };
264 
265 static struct xlat kevent_flags[] = {
266 	X(EV_ADD) X(EV_DELETE) X(EV_ENABLE) X(EV_DISABLE) X(EV_ONESHOT)
267 	X(EV_CLEAR) X(EV_FLAG1) X(EV_ERROR) X(EV_EOF) XEND
268 };
269 
270 struct xlat poll_flags[] = {
271 	X(POLLSTANDARD) X(POLLIN) X(POLLPRI) X(POLLOUT) X(POLLERR)
272 	X(POLLHUP) X(POLLNVAL) X(POLLRDNORM) X(POLLRDBAND)
273 	X(POLLWRBAND) X(POLLINIGNEOF) XEND
274 };
275 
276 static struct xlat mmap_flags[] = {
277 	X(MAP_SHARED) X(MAP_PRIVATE) X(MAP_FIXED) X(MAP_RENAME)
278 	X(MAP_NORESERVE) X(MAP_RESERVED0080) X(MAP_RESERVED0100)
279 	X(MAP_HASSEMAPHORE) X(MAP_STACK) X(MAP_NOSYNC) X(MAP_ANON)
280 	X(MAP_NOCORE) XEND
281 };
282 
283 static struct xlat mprot_flags[] = {
284 	X(PROT_NONE) X(PROT_READ) X(PROT_WRITE) X(PROT_EXEC) XEND
285 };
286 
287 static struct xlat whence_arg[] = {
288 	X(SEEK_SET) X(SEEK_CUR) X(SEEK_END) XEND
289 };
290 
291 static struct xlat sigaction_flags[] = {
292 	X(SA_ONSTACK) X(SA_RESTART) X(SA_RESETHAND) X(SA_NOCLDSTOP)
293 	X(SA_NODEFER) X(SA_NOCLDWAIT) X(SA_SIGINFO) XEND
294 };
295 
296 static struct xlat fcntl_arg[] = {
297 	X(F_DUPFD) X(F_GETFD) X(F_SETFD) X(F_GETFL) X(F_SETFL)
298 	X(F_GETOWN) X(F_SETOWN) X(F_GETLK) X(F_SETLK) X(F_SETLKW) XEND
299 };
300 
301 static struct xlat fcntlfd_arg[] = {
302 	X(FD_CLOEXEC) XEND
303 };
304 
305 static struct xlat fcntlfl_arg[] = {
306 	X(O_APPEND) X(O_ASYNC) X(O_FSYNC) X(O_NONBLOCK) X(O_NOFOLLOW)
307 	X(O_DIRECT) XEND
308 };
309 
310 static struct xlat sockdomain_arg[] = {
311 	X(PF_UNSPEC) X(PF_LOCAL) X(PF_UNIX) X(PF_INET) X(PF_IMPLINK)
312 	X(PF_PUP) X(PF_CHAOS) X(PF_NETBIOS) X(PF_ISO) X(PF_OSI)
313 	X(PF_ECMA) X(PF_DATAKIT) X(PF_CCITT) X(PF_SNA) X(PF_DECnet)
314 	X(PF_DLI) X(PF_LAT) X(PF_HYLINK) X(PF_APPLETALK) X(PF_ROUTE)
315 	X(PF_LINK) X(PF_XTP) X(PF_COIP) X(PF_CNT) X(PF_SIP) X(PF_IPX)
316 	X(PF_RTIP) X(PF_PIP) X(PF_ISDN) X(PF_KEY) X(PF_INET6)
317 	X(PF_NATM) X(PF_ATM) X(PF_NETGRAPH) X(PF_SLOW) X(PF_SCLUSTER)
318 	X(PF_ARP) X(PF_BLUETOOTH) XEND
319 };
320 
321 static struct xlat socktype_arg[] = {
322 	X(SOCK_STREAM) X(SOCK_DGRAM) X(SOCK_RAW) X(SOCK_RDM)
323 	X(SOCK_SEQPACKET) XEND
324 };
325 
326 static struct xlat open_flags[] = {
327 	X(O_RDONLY) X(O_WRONLY) X(O_RDWR) X(O_ACCMODE) X(O_NONBLOCK)
328 	X(O_APPEND) X(O_SHLOCK) X(O_EXLOCK) X(O_ASYNC) X(O_FSYNC)
329 	X(O_NOFOLLOW) X(O_CREAT) X(O_TRUNC) X(O_EXCL) X(O_NOCTTY)
330 	X(O_DIRECT) XEND
331 };
332 
333 static struct xlat shutdown_arg[] = {
334 	X(SHUT_RD) X(SHUT_WR) X(SHUT_RDWR) XEND
335 };
336 
337 static struct xlat resource_arg[] = {
338 	X(RLIMIT_CPU) X(RLIMIT_FSIZE) X(RLIMIT_DATA) X(RLIMIT_STACK)
339 	X(RLIMIT_CORE) X(RLIMIT_RSS) X(RLIMIT_MEMLOCK) X(RLIMIT_NPROC)
340 	X(RLIMIT_NOFILE) X(RLIMIT_SBSIZE) X(RLIMIT_VMEM) XEND
341 };
342 
343 static struct xlat pathconf_arg[] = {
344 	X(_PC_LINK_MAX)  X(_PC_MAX_CANON)  X(_PC_MAX_INPUT)
345 	X(_PC_NAME_MAX) X(_PC_PATH_MAX) X(_PC_PIPE_BUF)
346 	X(_PC_CHOWN_RESTRICTED) X(_PC_NO_TRUNC) X(_PC_VDISABLE)
347 	X(_PC_ASYNC_IO) X(_PC_PRIO_IO) X(_PC_SYNC_IO)
348 	X(_PC_ALLOC_SIZE_MIN) X(_PC_FILESIZEBITS)
349 	X(_PC_REC_INCR_XFER_SIZE) X(_PC_REC_MAX_XFER_SIZE)
350 	X(_PC_REC_MIN_XFER_SIZE) X(_PC_REC_XFER_ALIGN)
351 	X(_PC_SYMLINK_MAX) X(_PC_ACL_EXTENDED) X(_PC_ACL_PATH_MAX)
352 	X(_PC_CAP_PRESENT) X(_PC_INF_PRESENT) X(_PC_MAC_PRESENT)
353 	XEND
354 };
355 
356 #undef X
357 #undef XEND
358 
359 /*
360  * Searches an xlat array for a value, and returns it if found.  Otherwise
361  * return a string representation.
362  */
363 static const char *
364 lookup(struct xlat *xlat, int val, int base)
365 {
366 	static char tmp[16];
367 
368 	for (; xlat->str != NULL; xlat++)
369 		if (xlat->val == val)
370 			return (xlat->str);
371 	switch (base) {
372 		case 8:
373 			sprintf(tmp, "0%o", val);
374 			break;
375 		case 16:
376 			sprintf(tmp, "0x%x", val);
377 			break;
378 		case 10:
379 			sprintf(tmp, "%u", val);
380 			break;
381 		default:
382 			errx(1,"Unknown lookup base");
383 			break;
384 	}
385 	return (tmp);
386 }
387 
388 static const char *
389 xlookup(struct xlat *xlat, int val)
390 {
391 
392 	return (lookup(xlat, val, 16));
393 }
394 
395 /* Searches an xlat array containing bitfield values.  Remaining bits
396    set after removing the known ones are printed at the end:
397    IN|0x400 */
398 static char *
399 xlookup_bits(struct xlat *xlat, int val)
400 {
401 	static char str[512];
402 	int len = 0;
403 	int rem = val;
404 
405 	for (; xlat->str != NULL; xlat++) {
406 		if ((xlat->val & rem) == xlat->val) {
407 			/* don't print the "all-bits-zero" string unless all
408 			   bits are really zero */
409 			if (xlat->val == 0 && val != 0)
410 				continue;
411 			len += sprintf(str + len, "%s|", xlat->str);
412 			rem &= ~(xlat->val);
413 		}
414 	}
415 	/* if we have leftover bits or didn't match anything */
416 	if (rem || len == 0)
417 		len += sprintf(str + len, "0x%x", rem);
418 	if (len && str[len - 1] == '|')
419 		len--;
420 	str[len] = 0;
421 	return (str);
422 }
423 
424 /*
425  * If/when the list gets big, it might be desirable to do it
426  * as a hash table or binary search.
427  */
428 
429 struct syscall *
430 get_syscall(const char *name)
431 {
432 	struct syscall *sc = syscalls;
433 
434 	if (name == NULL)
435 		return (NULL);
436 	while (sc->name) {
437 		if (!strcmp(name, sc->name))
438 			return (sc);
439 		sc++;
440 	}
441 	return (NULL);
442 }
443 
444 /*
445  * get_struct
446  *
447  * Copy a fixed amount of bytes from the process.
448  */
449 
450 static int
451 get_struct(int pid, void *offset, void *buf, int len)
452 {
453 	struct ptrace_io_desc iorequest;
454 
455 	iorequest.piod_op = PIOD_READ_D;
456 	iorequest.piod_offs = offset;
457 	iorequest.piod_addr = buf;
458 	iorequest.piod_len = len;
459 	if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0)
460 		return (-1);
461 	return (0);
462 }
463 
464 #define MAXSIZE 4096
465 #define BLOCKSIZE 1024
466 /*
467  * get_string
468  * Copy a string from the process.  Note that it is
469  * expected to be a C string, but if max is set, it will
470  * only get that much.
471  */
472 
473 static char *
474 get_string(pid_t pid, void *offset, int max)
475 {
476 	char *buf;
477 	struct ptrace_io_desc iorequest;
478 	int totalsize, size;
479 	int diff = 0;
480 	int i;
481 
482 	totalsize = size = max ? (max + 1) : BLOCKSIZE;
483 	buf = malloc(totalsize);
484 	if (buf == NULL)
485 		return (NULL);
486 	for (;;) {
487 		diff = totalsize - size;
488 		iorequest.piod_op = PIOD_READ_D;
489 		iorequest.piod_offs = (char *)offset + diff;
490 		iorequest.piod_addr = buf + diff;
491 		iorequest.piod_len = size;
492 		if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0) {
493 			free(buf);
494 			return (NULL);
495 		}
496 		for (i = 0 ; i < size; i++) {
497 			if (buf[diff + i] == '\0')
498 				return (buf);
499 		}
500 		if (totalsize < MAXSIZE - BLOCKSIZE && max == 0) {
501 			totalsize += BLOCKSIZE;
502 			buf = realloc(buf, totalsize);
503 			size = BLOCKSIZE;
504 		} else {
505 			buf[totalsize] = '\0';
506 			return (buf);
507 		}
508 	}
509 }
510 
511 
512 /*
513  * print_arg
514  * Converts a syscall argument into a string.  Said string is
515  * allocated via malloc(), so needs to be free()'d.  The file
516  * descriptor is for the process' memory (via /proc), and is used
517  * to get any data (where the argument is a pointer).  sc is
518  * a pointer to the syscall description (see above); args is
519  * an array of all of the system call arguments.
520  */
521 
522 char *
523 print_arg(struct syscall_args *sc, unsigned long *args, long retval, struct trussinfo *trussinfo)
524 {
525 	char *tmp = NULL;
526 	int pid = trussinfo->pid;
527 
528 	switch (sc->type & ARG_MASK) {
529 	case Hex:
530 		asprintf(&tmp, "0x%x", (int)args[sc->offset]);
531 		break;
532 	case Octal:
533 		asprintf(&tmp, "0%o", (int)args[sc->offset]);
534 		break;
535 	case Int:
536 		asprintf(&tmp, "%d", (int)args[sc->offset]);
537 		break;
538 	case Name: {
539 		/* NULL-terminated string. */
540 		char *tmp2;
541 		tmp2 = get_string(pid, (void*)args[sc->offset], 0);
542 		asprintf(&tmp, "\"%s\"", tmp2);
543 		free(tmp2);
544 		break;
545 	}
546 	case BinString: {
547 		/* Binary block of data that might have printable characters.
548 		   XXX If type|OUT, assume that the length is the syscall's
549 		   return value.  Otherwise, assume that the length of the block
550 		   is in the next syscall argument. */
551 		int max_string = trussinfo->strsize;
552 		char tmp2[max_string+1], *tmp3;
553 		int len;
554 		int truncated = 0;
555 
556 		if (sc->type & OUT)
557 			len = retval;
558 		else
559 			len = args[sc->offset + 1];
560 
561 		/* Don't print more than max_string characters, to avoid word
562 		   wrap.  If we have to truncate put some ... after the string.
563 		*/
564 		if (len > max_string) {
565 			len = max_string;
566 			truncated = 1;
567 		}
568 		if (len && get_struct(pid, (void*)args[sc->offset], &tmp2, len) != -1) {
569 			tmp3 = malloc(len * 4 + 1);
570 			while (len) {
571 				if (strvisx(tmp3, tmp2, len, VIS_CSTYLE|VIS_TAB|VIS_NL) <= max_string)
572 					break;
573 				len--;
574 				truncated = 1;
575 			};
576 			asprintf(&tmp, "\"%s\"%s", tmp3, truncated?"...":"");
577 			free(tmp3);
578 		} else {
579 			asprintf(&tmp, "0x%lx", args[sc->offset]);
580 		}
581 		break;
582 	}
583 	case StringArray: {
584 		int num, size, i;
585 		char *tmp2;
586 		char *string;
587 		char *strarray[100];	/* XXX This is ugly. */
588 
589 		if (get_struct(pid, (void *)args[sc->offset], (void *)&strarray,
590 			sizeof(strarray)) == -1) {
591 			err(1, "get_struct %p", (void *)args[sc->offset]);
592 		}
593 		num = 0;
594 		size = 0;
595 
596 		/* Find out how large of a buffer we'll need. */
597 		while (strarray[num] != NULL) {
598 			string = get_string(pid, (void*)strarray[num], 0);
599 			size += strlen(string);
600 			free(string);
601 			num++;
602 		}
603 		size += 4 + (num * 4);
604 		tmp = (char *)malloc(size);
605 		tmp2 = tmp;
606 
607 		tmp2 += sprintf(tmp2, " [");
608 		for (i = 0; i < num; i++) {
609 			string = get_string(pid, (void*)strarray[i], 0);
610 			tmp2 += sprintf(tmp2, " \"%s\"%c", string, (i+1 == num) ? ' ' : ',');
611 			free(string);
612 		}
613 		tmp2 += sprintf(tmp2, "]");
614 		break;
615 	}
616 #ifdef __LP64__
617 	case Quad:
618 		asprintf(&tmp, "0x%lx", args[sc->offset]);
619 		break;
620 #else
621 	case Quad: {
622 		unsigned long long ll;
623 		ll = *(unsigned long long *)(args + sc->offset);
624 		asprintf(&tmp, "0x%llx", ll);
625 		break;
626 	}
627 #endif
628 	case Ptr:
629 		asprintf(&tmp, "0x%lx", args[sc->offset]);
630 		break;
631 	case Readlinkres: {
632 		char *tmp2;
633 		if (retval == -1) {
634 			tmp = strdup("");
635 			break;
636 		}
637 		tmp2 = get_string(pid, (void*)args[sc->offset], retval);
638 		asprintf(&tmp, "\"%s\"", tmp2);
639 		free(tmp2);
640 		break;
641 	}
642 	case Ioctl: {
643 		const char *temp = ioctlname(args[sc->offset]);
644 		if (temp) {
645 			tmp = strdup(temp);
646 		} else {
647 			unsigned long arg = args[sc->offset];
648 			asprintf(&tmp, "0x%lx { IO%s%s 0x%lx('%c'), %lu, %lu }", arg,
649 			    arg&IOC_OUT?"R":"", arg&IOC_IN?"W":"",
650 			    IOCGROUP(arg), isprint(IOCGROUP(arg))?(char)IOCGROUP(arg):'?',
651 			    arg & 0xFF, IOCPARM_LEN(arg));
652 		}
653 		break;
654 	}
655 	case Umtx: {
656 		struct umtx umtx;
657 		if (get_struct(pid, (void *)args[sc->offset], &umtx, sizeof(umtx)) != -1)
658 			asprintf(&tmp, "{ 0x%lx }", (long)umtx.u_owner);
659 		else
660 			asprintf(&tmp, "0x%lx", args[sc->offset]);
661 		break;
662 	}
663 	case Timespec: {
664 		struct timespec ts;
665 		if (get_struct(pid, (void *)args[sc->offset], &ts, sizeof(ts)) != -1)
666 			asprintf(&tmp, "{%ld.%09ld }", (long)ts.tv_sec, ts.tv_nsec);
667 		else
668 			asprintf(&tmp, "0x%lx", args[sc->offset]);
669 		break;
670 	}
671 	case Timeval: {
672 		struct timeval tv;
673 		if (get_struct(pid, (void *)args[sc->offset], &tv, sizeof(tv)) != -1)
674 			asprintf(&tmp, "{%ld.%06ld }", (long)tv.tv_sec, tv.tv_usec);
675 		else
676 			asprintf(&tmp, "0x%lx", args[sc->offset]);
677 		break;
678 	}
679 	case Timeval2: {
680 		struct timeval tv[2];
681 		if (get_struct(pid, (void *)args[sc->offset], &tv, sizeof(tv)) != -1)
682 			asprintf(&tmp, "{%ld.%06ld, %ld.%06ld }",
683 			    (long)tv[0].tv_sec, tv[0].tv_usec,
684 			    (long)tv[1].tv_sec, tv[1].tv_usec);
685 		else
686 			asprintf(&tmp, "0x%lx", args[sc->offset]);
687 		break;
688 	}
689 	case Itimerval: {
690 		struct itimerval itv;
691 		if (get_struct(pid, (void *)args[sc->offset], &itv, sizeof(itv)) != -1)
692 			asprintf(&tmp, "{%ld.%06ld, %ld.%06ld }",
693 			    (long)itv.it_interval.tv_sec,
694 			    itv.it_interval.tv_usec,
695 			    (long)itv.it_value.tv_sec,
696 			    itv.it_value.tv_usec);
697 		else
698 			asprintf(&tmp, "0x%lx", args[sc->offset]);
699 		break;
700 	}
701 	case Pollfd: {
702 		/*
703 		 * XXX: A Pollfd argument expects the /next/ syscall argument to be
704 		 * the number of fds in the array. This matches the poll syscall.
705 		 */
706 		struct pollfd *pfd;
707 		int numfds = args[sc->offset+1];
708 		int bytes = sizeof(struct pollfd) * numfds;
709 		int i, tmpsize, u, used;
710 		const int per_fd = 100;
711 
712 		if ((pfd = malloc(bytes)) == NULL)
713 			err(1, "Cannot malloc %d bytes for pollfd array", bytes);
714 		if (get_struct(pid, (void *)args[sc->offset], pfd, bytes) != -1) {
715 
716 			used = 0;
717 			tmpsize = 1 + per_fd * numfds + 2;
718 			if ((tmp = malloc(tmpsize)) == NULL)
719 				err(1, "Cannot alloc %d bytes for poll output", tmpsize);
720 
721 			tmp[used++] = '{';
722 			for (i = 0; i < numfds; i++) {
723 
724 				u = snprintf(tmp + used, per_fd,
725 				    "%s%d/%s",
726 				    i > 0 ? " " : "",
727 				    pfd[i].fd,
728 				    xlookup_bits(poll_flags, pfd[i].events) );
729 				if (u > 0)
730 					used += u < per_fd ? u : per_fd;
731 			}
732 			tmp[used++] = '}';
733 			tmp[used++] = '\0';
734 		} else {
735 			asprintf(&tmp, "0x%lx", args[sc->offset]);
736 		}
737 		free(pfd);
738 		break;
739 	}
740 	case Fd_set: {
741 		/*
742 		 * XXX: A Fd_set argument expects the /first/ syscall argument to be
743 		 * the number of fds in the array.  This matches the select syscall.
744 		 */
745 		fd_set *fds;
746 		int numfds = args[0];
747 		int bytes = _howmany(numfds, _NFDBITS) * _NFDBITS;
748 		int i, tmpsize, u, used;
749 		const int per_fd = 20;
750 
751 		if ((fds = malloc(bytes)) == NULL)
752 			err(1, "Cannot malloc %d bytes for fd_set array", bytes);
753 		if (get_struct(pid, (void *)args[sc->offset], fds, bytes) != -1) {
754 			used = 0;
755 			tmpsize = 1 + numfds * per_fd + 2;
756 			if ((tmp = malloc(tmpsize)) == NULL)
757 				err(1, "Cannot alloc %d bytes for fd_set output", tmpsize);
758 
759 			tmp[used++] = '{';
760 			for (i = 0; i < numfds; i++) {
761 				if (FD_ISSET(i, fds)) {
762 					u = snprintf(tmp + used, per_fd, "%d ", i);
763 					if (u > 0)
764 						used += u < per_fd ? u : per_fd;
765 				}
766 			}
767 			if (tmp[used-1] == ' ')
768 				used--;
769 			tmp[used++] = '}';
770 			tmp[used++] = '\0';
771 		} else {
772 			asprintf(&tmp, "0x%lx", args[sc->offset]);
773 		}
774 		free(fds);
775 		break;
776 	}
777 	case Signal: {
778 		long sig;
779 
780 		sig = args[sc->offset];
781 		tmp = strsig(sig);
782 		if (tmp == NULL)
783 			asprintf(&tmp, "%ld", sig);
784 		break;
785 	}
786 	case Sigset: {
787 		long sig;
788 		sigset_t ss;
789 		int i, used;
790 
791 		sig = args[sc->offset];
792 		if (get_struct(pid, (void *)args[sc->offset], (void *)&ss, sizeof(ss)) == -1) {
793 			asprintf(&tmp, "0x%lx", args[sc->offset]);
794 			break;
795 		}
796 		tmp = malloc(sys_nsig * 8); /* 7 bytes avg per signal name */
797 		used = 0;
798 		for (i = 1; i < sys_nsig; i++) {
799 			if (sigismember(&ss, i)) {
800 				used += sprintf(tmp + used, "%s|", strsig(i));
801 			}
802 		}
803 		if (used)
804 			tmp[used-1] = 0;
805 		else
806 			strcpy(tmp, "0x0");
807 		break;
808 	}
809 	case Sigprocmask: {
810 		switch (args[sc->offset]) {
811 #define S(a)	case a: tmp = strdup(#a); break;
812 			S(SIG_BLOCK);
813 			S(SIG_UNBLOCK);
814 			S(SIG_SETMASK);
815 #undef S
816 		}
817 		if (tmp == NULL)
818 			asprintf(&tmp, "0x%lx", args[sc->offset]);
819 		break;
820 	}
821 	case Fcntlflag: {
822 		/* XXX output depends on the value of the previous argument */
823 		switch (args[sc->offset-1]) {
824 		case F_SETFD:
825 			tmp = strdup(xlookup_bits(fcntlfd_arg, args[sc->offset]));
826 			break;
827 		case F_SETFL:
828 			tmp = strdup(xlookup_bits(fcntlfl_arg, args[sc->offset]));
829 			break;
830 		case F_GETFD:
831 		case F_GETFL:
832 		case F_GETOWN:
833 			tmp = strdup("");
834 			break;
835 		default:
836 			asprintf(&tmp, "0x%lx", args[sc->offset]);
837 			break;
838 		}
839 		break;
840 	}
841 	case Open:
842 		tmp = strdup(xlookup_bits(open_flags, args[sc->offset]));
843 		break;
844 	case Fcntl:
845 		tmp = strdup(xlookup(fcntl_arg, args[sc->offset]));
846 		break;
847 	case Mprot:
848 		tmp = strdup(xlookup_bits(mprot_flags, args[sc->offset]));
849 		break;
850 	case Mmapflags:
851 		tmp = strdup(xlookup_bits(mmap_flags, args[sc->offset]));
852 		break;
853 	case Whence:
854 		tmp = strdup(xlookup(whence_arg, args[sc->offset]));
855 		break;
856 	case Sockdomain:
857 		tmp = strdup(xlookup(sockdomain_arg, args[sc->offset]));
858 		break;
859 	case Socktype:
860 		tmp = strdup(xlookup(socktype_arg, args[sc->offset]));
861 		break;
862 	case Shutdown:
863 		tmp = strdup(xlookup(shutdown_arg, args[sc->offset]));
864 		break;
865 	case Resource:
866 		tmp = strdup(xlookup(resource_arg, args[sc->offset]));
867 		break;
868 	case Pathconf:
869 		tmp = strdup(xlookup(pathconf_arg, args[sc->offset]));
870 		break;
871 	case Sockaddr: {
872 		struct sockaddr_storage ss;
873 		char addr[64];
874 		struct sockaddr_in *lsin;
875 		struct sockaddr_in6 *lsin6;
876 		struct sockaddr_un *sun;
877 		struct sockaddr *sa;
878 		char *p;
879 		u_char *q;
880 		int i;
881 
882 		if (args[sc->offset] == 0) {
883 			asprintf(&tmp, "NULL");
884 			break;
885 		}
886 
887 		/* yuck: get ss_len */
888 		if (get_struct(pid, (void *)args[sc->offset], (void *)&ss,
889 			sizeof(ss.ss_len) + sizeof(ss.ss_family)) == -1)
890 			err(1, "get_struct %p", (void *)args[sc->offset]);
891 		/*
892 		 * If ss_len is 0, then try to guess from the sockaddr type.
893 		 * AF_UNIX may be initialized incorrectly, so always frob
894 		 * it by using the "right" size.
895 		 */
896 		if (ss.ss_len == 0 || ss.ss_family == AF_UNIX) {
897 			switch (ss.ss_family) {
898 			case AF_INET:
899 				ss.ss_len = sizeof(*lsin);
900 				break;
901 			case AF_UNIX:
902 				ss.ss_len = sizeof(*sun);
903 				break;
904 			default:
905 				/* hurrrr */
906 				break;
907 			}
908 		}
909 		if (get_struct(pid, (void *)args[sc->offset], (void *)&ss, ss.ss_len)
910 		    == -1) {
911 			err(2, "get_struct %p", (void *)args[sc->offset]);
912 		}
913 
914 		switch (ss.ss_family) {
915 		case AF_INET:
916 			lsin = (struct sockaddr_in *)&ss;
917 			inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof addr);
918 			asprintf(&tmp, "{ AF_INET %s:%d }", addr, htons(lsin->sin_port));
919 			break;
920 		case AF_INET6:
921 			lsin6 = (struct sockaddr_in6 *)&ss;
922 			inet_ntop(AF_INET6, &lsin6->sin6_addr, addr, sizeof addr);
923 			asprintf(&tmp, "{ AF_INET6 [%s]:%d }", addr, htons(lsin6->sin6_port));
924 			break;
925 		case AF_UNIX:
926 			sun = (struct sockaddr_un *)&ss;
927 			asprintf(&tmp, "{ AF_UNIX \"%s\" }", sun->sun_path);
928 			break;
929 		default:
930 			sa = (struct sockaddr *)&ss;
931 			asprintf(&tmp, "{ sa_len = %d, sa_family = %d, sa_data = {%n%*s } }",
932 			    (int)sa->sa_len, (int)sa->sa_family, &i,
933 			    6 * (int)(sa->sa_len - ((char *)&sa->sa_data - (char *)sa)), "");
934 			if (tmp != NULL) {
935 				p = tmp + i;
936 				for (q = (u_char *)&sa->sa_data; q < (u_char *)sa + sa->sa_len; q++)
937 					p += sprintf(p, " %#02x,", *q);
938 			}
939 		}
940 		break;
941 	}
942 	case Sigaction: {
943 		struct sigaction sa;
944 		char *hand;
945 		const char *h;
946 
947 		if (get_struct(pid, (void *)args[sc->offset], &sa, sizeof(sa)) != -1) {
948 
949 			asprintf(&hand, "%p", sa.sa_handler);
950 			if (sa.sa_handler == SIG_DFL)
951 				h = "SIG_DFL";
952 			else if (sa.sa_handler == SIG_IGN)
953 				h = "SIG_IGN";
954 			else
955 				h = hand;
956 
957 			asprintf(&tmp, "{ %s %s ss_t }",
958 			    h,
959 			    xlookup_bits(sigaction_flags, sa.sa_flags));
960 			free(hand);
961 		} else {
962 			asprintf(&tmp, "0x%lx", args[sc->offset]);
963 		}
964 		break;
965 	}
966 	case Kevent: {
967 		/*
968 		 * XXX XXX: the size of the array is determined by either the
969 		 * next syscall argument, or by the syscall returnvalue,
970 		 * depending on which argument number we are.  This matches the
971 		 * kevent syscall, but luckily that's the only syscall that uses
972 		 * them.
973 		 */
974 		struct kevent *ke;
975 		int numevents = -1;
976 		int bytes = 0;
977 		int i, tmpsize, u, used;
978 		const int per_ke = 100;
979 
980 		if (sc->offset == 1)
981 			numevents = args[sc->offset+1];
982 		else if (sc->offset == 3 && retval != -1)
983 			numevents = retval;
984 
985 		if (numevents >= 0)
986 			bytes = sizeof(struct kevent) * numevents;
987 		if ((ke = malloc(bytes)) == NULL)
988 			err(1, "Cannot malloc %d bytes for kevent array", bytes);
989 		if (numevents >= 0 && get_struct(pid, (void *)args[sc->offset], ke, bytes) != -1) {
990 			used = 0;
991 			tmpsize = 1 + per_ke * numevents + 2;
992 			if ((tmp = malloc(tmpsize)) == NULL)
993 				err(1, "Cannot alloc %d bytes for kevent output", tmpsize);
994 
995 			tmp[used++] = '{';
996 			for (i = 0; i < numevents; i++) {
997 				u = snprintf(tmp + used, per_ke,
998 				    "%s%p,%s,%s,%d,%p,%p",
999 				    i > 0 ? " " : "",
1000 				    (void *)ke[i].ident,
1001 				    xlookup(kevent_filters, ke[i].filter),
1002 				    xlookup_bits(kevent_flags, ke[i].flags),
1003 				    ke[i].fflags,
1004 				    (void *)ke[i].data,
1005 				    (void *)ke[i].udata);
1006 				if (u > 0)
1007 					used += u < per_ke ? u : per_ke;
1008 			}
1009 			tmp[used++] = '}';
1010 			tmp[used++] = '\0';
1011 		} else {
1012 			asprintf(&tmp, "0x%lx", args[sc->offset]);
1013 		}
1014 		free(ke);
1015 		break;
1016 	}
1017 	case Stat: {
1018 		struct stat st;
1019 		if (get_struct(pid, (void *)args[sc->offset], &st, sizeof(st)) != -1) {
1020 			char mode[12];
1021 			strmode(st.st_mode, mode);
1022 			asprintf(&tmp, "{ mode=%s,inode=%jd,size=%jd,blksize=%ld }",
1023 			    mode,
1024 			    (intmax_t)st.st_ino,(intmax_t)st.st_size,(long)st.st_blksize);
1025 		} else {
1026 			asprintf(&tmp, "0x%lx", args[sc->offset]);
1027 		}
1028 		break;
1029 	}
1030 	case Rusage: {
1031 		struct rusage ru;
1032 		if (get_struct(pid, (void *)args[sc->offset], &ru, sizeof(ru)) != -1) {
1033 			asprintf(&tmp, "{ u=%ld.%06ld,s=%ld.%06ld,in=%ld,out=%ld }",
1034 			    (long)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec,
1035 			    (long)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec,
1036 			    ru.ru_inblock, ru.ru_oublock);
1037 		} else {
1038 			asprintf(&tmp, "0x%lx", args[sc->offset]);
1039 		}
1040 		break;
1041 	}
1042 	case Rlimit: {
1043 		struct rlimit rl;
1044 		if (get_struct(pid, (void *)args[sc->offset], &rl, sizeof(rl)) != -1) {
1045 			asprintf(&tmp, "{ cur=%ju,max=%ju }",
1046 			    rl.rlim_cur, rl.rlim_max);
1047 		} else {
1048 			asprintf(&tmp, "0x%lx", args[sc->offset]);
1049 		}
1050 		break;
1051 	}
1052 	default:
1053 		errx(1, "Invalid argument type %d\n", sc->type & ARG_MASK);
1054 	}
1055 	return (tmp);
1056 }
1057 
1058 /*
1059  * print_syscall
1060  * Print (to outfile) the system call and its arguments.  Note that
1061  * nargs is the number of arguments (not the number of words; this is
1062  * potentially confusing, I know).
1063  */
1064 
1065 void
1066 print_syscall(struct trussinfo *trussinfo, const char *name, int nargs, char **s_args)
1067 {
1068 	int i;
1069 	int len = 0;
1070 	struct timespec timediff;
1071 
1072 	if (trussinfo->flags & FOLLOWFORKS)
1073 		len += fprintf(trussinfo->outfile, "%5d: ", trussinfo->pid);
1074 
1075 	if (name != NULL && (!strcmp(name, "execve") || !strcmp(name, "exit"))) {
1076 		clock_gettime(CLOCK_REALTIME, &trussinfo->after);
1077 	}
1078 
1079 	if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
1080 		timespecsubt(&trussinfo->after, &trussinfo->start_time, &timediff);
1081 		len += fprintf(trussinfo->outfile, "%ld.%09ld ",
1082 		    (long)timediff.tv_sec, timediff.tv_nsec);
1083 	}
1084 
1085 	if (trussinfo->flags & RELATIVETIMESTAMPS) {
1086 		timespecsubt(&trussinfo->after, &trussinfo->before, &timediff);
1087 		len += fprintf(trussinfo->outfile, "%ld.%09ld ",
1088 		    (long)timediff.tv_sec, timediff.tv_nsec);
1089 	}
1090 
1091 	len += fprintf(trussinfo->outfile, "%s(", name);
1092 
1093 	for (i = 0; i < nargs; i++) {
1094 		if (s_args[i])
1095 			len += fprintf(trussinfo->outfile, "%s", s_args[i]);
1096 		else
1097 			len += fprintf(trussinfo->outfile, "<missing argument>");
1098 		len += fprintf(trussinfo->outfile, "%s", i < (nargs - 1) ? "," : "");
1099 	}
1100 	len += fprintf(trussinfo->outfile, ")");
1101 	for (i = 0; i < 6 - (len / 8); i++)
1102 		fprintf(trussinfo->outfile, "\t");
1103 }
1104 
1105 void
1106 print_syscall_ret(struct trussinfo *trussinfo, const char *name, int nargs,
1107     char **s_args, int errorp, long retval, struct syscall *sc)
1108 {
1109 	struct timespec timediff;
1110 
1111 	if (trussinfo->flags & COUNTONLY) {
1112 		if (!sc)
1113 			return;
1114 		clock_gettime(CLOCK_REALTIME, &trussinfo->after);
1115 		timespecsubt(&trussinfo->after, &trussinfo->before, &timediff);
1116 		timespecadd(&sc->time, &timediff, &sc->time);
1117 		sc->ncalls++;
1118 		if (errorp)
1119 			sc->nerror++;
1120 		return;
1121 	}
1122 
1123 	print_syscall(trussinfo, name, nargs, s_args);
1124 	fflush(trussinfo->outfile);
1125 	if (errorp) {
1126 		fprintf(trussinfo->outfile, " ERR#%ld '%s'\n", retval, strerror(retval));
1127 	} else {
1128 		fprintf(trussinfo->outfile, " = %ld (0x%lx)\n", retval, retval);
1129 	}
1130 }
1131 
1132 void
1133 print_summary(struct trussinfo *trussinfo)
1134 {
1135 	struct syscall *sc;
1136 	struct timespec total = {0, 0};
1137 	int ncall, nerror;
1138 
1139 	fprintf(trussinfo->outfile, "%-20s%15s%8s%8s\n",
1140 		"syscall", "seconds", "calls", "errors");
1141 	ncall = nerror = 0;
1142 	for (sc = syscalls; sc->name != NULL; sc++)
1143 		if (sc->ncalls) {
1144 			fprintf(trussinfo->outfile, "%-20s%5d.%09ld%8d%8d\n",
1145 			    sc->name, sc->time.tv_sec, sc->time.tv_nsec,
1146 			    sc->ncalls, sc->nerror);
1147 			timespecadd(&total, &sc->time, &total);
1148 			ncall += sc->ncalls;
1149 			nerror += sc->nerror;
1150 		}
1151 	fprintf(trussinfo->outfile, "%20s%15s%8s%8s\n",
1152 		"", "-------------", "-------", "-------");
1153 	fprintf(trussinfo->outfile, "%-20s%5d.%09ld%8d%8d\n",
1154 		"", total.tv_sec, total.tv_nsec, ncall, nerror);
1155 }
1156