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