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