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