xref: /freebsd/usr.bin/truss/syscalls.c (revision 2bae4eb30862565380ca4f43d5a24e30e7bdc7c8)
1bbeaf6c0SSean Eric Fagan /*
209d64da3SSean Eric Fagan  * Copryight 1997 Sean Eric Fagan
309d64da3SSean Eric Fagan  *
409d64da3SSean Eric Fagan  * Redistribution and use in source and binary forms, with or without
509d64da3SSean Eric Fagan  * modification, are permitted provided that the following conditions
609d64da3SSean Eric Fagan  * are met:
709d64da3SSean Eric Fagan  * 1. Redistributions of source code must retain the above copyright
809d64da3SSean Eric Fagan  *    notice, this list of conditions and the following disclaimer.
909d64da3SSean Eric Fagan  * 2. Redistributions in binary form must reproduce the above copyright
1009d64da3SSean Eric Fagan  *    notice, this list of conditions and the following disclaimer in the
1109d64da3SSean Eric Fagan  *    documentation and/or other materials provided with the distribution.
1209d64da3SSean Eric Fagan  * 3. All advertising materials mentioning features or use of this software
1309d64da3SSean Eric Fagan  *    must display the following acknowledgement:
1409d64da3SSean Eric Fagan  *	This product includes software developed by Sean Eric Fagan
1509d64da3SSean Eric Fagan  * 4. Neither the name of the author may be used to endorse or promote
1609d64da3SSean Eric Fagan  *    products derived from this software without specific prior written
1709d64da3SSean Eric Fagan  *    permission.
1809d64da3SSean Eric Fagan  *
1909d64da3SSean Eric Fagan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2009d64da3SSean Eric Fagan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2109d64da3SSean Eric Fagan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2209d64da3SSean Eric Fagan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2309d64da3SSean Eric Fagan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2409d64da3SSean Eric Fagan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2509d64da3SSean Eric Fagan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2609d64da3SSean Eric Fagan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2709d64da3SSean Eric Fagan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2809d64da3SSean Eric Fagan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2909d64da3SSean Eric Fagan  * SUCH DAMAGE.
3009d64da3SSean Eric Fagan  */
3109d64da3SSean Eric Fagan 
323cf51049SPhilippe Charnier #ifndef lint
333cf51049SPhilippe Charnier static const char rcsid[] =
34c3aac50fSPeter Wemm   "$FreeBSD$";
353cf51049SPhilippe Charnier #endif /* not lint */
363cf51049SPhilippe Charnier 
3709d64da3SSean Eric Fagan /*
38bbeaf6c0SSean Eric Fagan  * This file has routines used to print out system calls and their
39bbeaf6c0SSean Eric Fagan  * arguments.
40bbeaf6c0SSean Eric Fagan  */
41bbeaf6c0SSean Eric Fagan 
42894b8f7aSAlfred Perlstein #include <sys/mman.h>
439ddd1412SDag-Erling Smørgrav #include <sys/types.h>
449ddd1412SDag-Erling Smørgrav #include <sys/socket.h>
451d631f7eSMike Barcroft #include <sys/time.h>
469ddd1412SDag-Erling Smørgrav #include <sys/un.h>
479ddd1412SDag-Erling Smørgrav #include <netinet/in.h>
489ddd1412SDag-Erling Smørgrav #include <arpa/inet.h>
499ddd1412SDag-Erling Smørgrav 
50dec17687SBrian Feldman #include <ctype.h>
513cf51049SPhilippe Charnier #include <err.h>
52894b8f7aSAlfred Perlstein #include <fcntl.h>
53e45a5a0dSDavid Malone #include <poll.h>
549ddd1412SDag-Erling Smørgrav #include <signal.h>
55e45a5a0dSDavid Malone #include <stdint.h>
56bbeaf6c0SSean Eric Fagan #include <stdio.h>
57bbeaf6c0SSean Eric Fagan #include <stdlib.h>
58bbeaf6c0SSean Eric Fagan #include <string.h>
5937169f94SMatthew N. Dodd #include <time.h>
60bbeaf6c0SSean Eric Fagan #include <unistd.h>
619ddd1412SDag-Erling Smørgrav 
62ec0bed25SMatthew N. Dodd #include "truss.h"
631be5d704SMark Murray #include "extern.h"
64bbeaf6c0SSean Eric Fagan #include "syscall.h"
65bbeaf6c0SSean Eric Fagan 
66bbeaf6c0SSean Eric Fagan /*
67bbeaf6c0SSean Eric Fagan  * This should probably be in its own file.
68bbeaf6c0SSean Eric Fagan  */
69bbeaf6c0SSean Eric Fagan 
70bbeaf6c0SSean Eric Fagan struct syscall syscalls[] = {
71894b8f7aSAlfred Perlstein 	{ "fcntl", 1, 3,
72894b8f7aSAlfred Perlstein 	  { { Int, 0 } , { Fcntl, 1 }, { Hex, 2 }}},
73bbeaf6c0SSean Eric Fagan 	{ "readlink", 1, 3,
742bae4eb3SAlfred Perlstein 	  { { String, 0 } , { Readlinkres | OUT, 1 }, { Int, 2 }}},
75bbeaf6c0SSean Eric Fagan 	{ "lseek", 2, 3,
76fde3a7d1SAlfred Perlstein 	  { { Int, 0 }, {Quad, 2 }, { Whence, 4 }}},
77fde3a7d1SAlfred Perlstein 	{ "linux_lseek", 2, 3,
78fde3a7d1SAlfred Perlstein 	  { { Int, 0 }, {Int, 1 }, { Whence, 2 }}},
79bbeaf6c0SSean Eric Fagan 	{ "mmap", 2, 6,
80894b8f7aSAlfred Perlstein 	  { { Ptr, 0 }, {Int, 1}, {Mprot, 2}, {Mmapflags, 3}, {Int, 4}, {Quad, 6}}},
815214320fSAlfred Perlstein 	{ "mprotect", 1, 3,
825214320fSAlfred Perlstein 	  { { Ptr, 0 }, {Int, 1}, {Mprot, 2}}},
83bbeaf6c0SSean Eric Fagan 	{ "open", 1, 3,
8409bf1169SAlfred Perlstein 	  { { String | IN, 0} , { Hex, 1}, {Octal, 2}}},
85bbeaf6c0SSean Eric Fagan 	{ "linux_open", 1, 3,
8609bf1169SAlfred Perlstein 	  { { String, 0 }, { Hex, 1}, { Octal, 2 }}},
87726c2ca0SMatthew N. Dodd 	{ "close", 1, 1,
88726c2ca0SMatthew N. Dodd 	  { { Int, 0 } } },
89726c2ca0SMatthew N. Dodd 	{ "link", 0, 2,
90726c2ca0SMatthew N. Dodd 	  { { String, 0 }, { String, 1 }}},
91726c2ca0SMatthew N. Dodd 	{ "unlink", 0, 1,
92726c2ca0SMatthew N. Dodd 	  { { String, 0 }}},
93726c2ca0SMatthew N. Dodd 	{ "chdir", 0, 1,
94726c2ca0SMatthew N. Dodd 	  { { String, 0 }}},
95726c2ca0SMatthew N. Dodd 	{ "mknod", 0, 3,
96726c2ca0SMatthew N. Dodd 	  { { String, 0 }, { Octal, 1 }, { Int, 3 }}},
97726c2ca0SMatthew N. Dodd 	{ "chmod", 0, 2,
98726c2ca0SMatthew N. Dodd 	  { { String, 0 }, { Octal, 1 }}},
99726c2ca0SMatthew N. Dodd 	{ "chown", 0, 3,
100726c2ca0SMatthew N. Dodd 	  { { String, 0 }, { Int, 1 }, { Int, 2 }}},
101726c2ca0SMatthew N. Dodd 	{ "mount", 0, 4,
102726c2ca0SMatthew N. Dodd 	  { { String, 0 }, { String, 1 }, { Int, 2 }, { Ptr, 3 }}},
103726c2ca0SMatthew N. Dodd 	{ "umount", 0, 2,
104726c2ca0SMatthew N. Dodd 	  { { String, 0 }, { Int, 2 }}},
105bbeaf6c0SSean Eric Fagan 	{ "fstat", 1, 2,
106bbeaf6c0SSean Eric Fagan 	  { { Int, 0},  {Ptr | OUT , 1 }}},
107bbeaf6c0SSean Eric Fagan 	{ "stat", 1, 2,
108bbeaf6c0SSean Eric Fagan 	  { { String | IN, 0 }, { Ptr | OUT, 1 }}},
109a8f37218SSean Eric Fagan 	{ "lstat", 1, 2,
110a8f37218SSean Eric Fagan 	  { { String | IN, 0 }, { Ptr | OUT, 1 }}},
111bbeaf6c0SSean Eric Fagan 	{ "linux_newstat", 1, 2,
112bbeaf6c0SSean Eric Fagan 	  { { String | IN, 0 }, { Ptr | OUT, 1 }}},
113bbeaf6c0SSean Eric Fagan 	{ "linux_newfstat", 1, 2,
114bbeaf6c0SSean Eric Fagan 	  { { Int, 0 }, { Ptr | OUT, 1 }}},
115bbeaf6c0SSean Eric Fagan 	{ "write", 1, 3,
116bbeaf6c0SSean Eric Fagan 	  { { Int, 0 }, { Ptr | IN, 1 }, { Int, 2 }}},
117970649f9SSean Eric Fagan 	{ "ioctl", 1, 3,
118970649f9SSean Eric Fagan 	  { { Int, 0 }, { Ioctl, 1 }, { Hex, 2 }}},
119bbeaf6c0SSean Eric Fagan 	{ "break", 1, 1, { { Hex, 0 }}},
120bbeaf6c0SSean Eric Fagan 	{ "exit", 0, 1, { { Hex, 0 }}},
12110714102SDag-Erling Smørgrav 	{ "access", 1, 2, { { String | IN, 0 }, { Int, 1 }}},
122f0ebbc29SDag-Erling Smørgrav 	{ "sigaction", 1, 3,
123e45a5a0dSDavid Malone 	  { { Signal, 0 }, { Sigaction | IN, 1 }, { Sigaction | OUT, 2 }}},
1249ddd1412SDag-Erling Smørgrav 	{ "accept", 1, 3,
1259ddd1412SDag-Erling Smørgrav 	  { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
1269ddd1412SDag-Erling Smørgrav 	{ "bind", 1, 3,
1279ddd1412SDag-Erling Smørgrav 	  { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
1289ddd1412SDag-Erling Smørgrav 	{ "connect", 1, 3,
1299ddd1412SDag-Erling Smørgrav 	  { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
1309ddd1412SDag-Erling Smørgrav 	{ "getpeername", 1, 3,
1319ddd1412SDag-Erling Smørgrav 	  { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
1329ddd1412SDag-Erling Smørgrav 	{ "getsockname", 1, 3,
1339ddd1412SDag-Erling Smørgrav 	  { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
1349b2f78c5SDag-Erling Smørgrav 	{ "recvfrom", 1, 6,
1359b2f78c5SDag-Erling Smørgrav 	  { { Hex, 0 }, { Ptr | IN, 1 }, { Int, 3 }, { Hex, 3 }, { Sockaddr | OUT, 4 }, { Ptr | OUT, 5 } } },
1369b2f78c5SDag-Erling Smørgrav 	{ "sendto", 1, 6,
1379b2f78c5SDag-Erling Smørgrav 	  { { Hex, 0 }, { Ptr | IN, 1 }, { Int, 3 }, { Hex, 3 }, { Sockaddr | IN, 4 }, { Ptr | IN, 5 } } },
1389897b203SMatthew N. Dodd 	{ "execve", 1, 3,
1399897b203SMatthew N. Dodd 	  { { String | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } },
1409897b203SMatthew N. Dodd 	{ "linux_execve", 1, 3,
1419897b203SMatthew N. Dodd 	  { { String | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } },
142726c2ca0SMatthew N. Dodd 	{ "kldload", 0, 1, { { String | IN, 0 }}},
143726c2ca0SMatthew N. Dodd 	{ "kldunload", 0, 1, { { Int, 0 }}},
144726c2ca0SMatthew N. Dodd 	{ "kldfind", 0, 1, { { String | IN, 0 }}},
145726c2ca0SMatthew N. Dodd 	{ "kldnext", 0, 1, { { Int, 0 }}},
146726c2ca0SMatthew N. Dodd 	{ "kldstat", 0, 2, { { Int, 0 }, { Ptr, 1 }}},
147726c2ca0SMatthew N. Dodd 	{ "kldfirstmod", 0, 1, { { Int, 0 }}},
148e45a5a0dSDavid Malone 	{ "nanosleep", 0, 1, { { Timespec, 0 }}},
149e45a5a0dSDavid Malone 	{ "select", 1, 5, { { Int, 0 }, { Fd_set, 1 }, { Fd_set, 2 }, { Fd_set, 3 }, { Timeval, 4 }}},
150e45a5a0dSDavid Malone 	{ "poll", 1, 3, { { Pollfd, 0 }, { Int, 1 }, { Int, 2 }}},
151e45a5a0dSDavid Malone 	{ "gettimeofday", 1, 2, { { Timeval | OUT, 0 }, { Ptr, 1 }}},
152e45a5a0dSDavid Malone 	{ "clock_gettime", 1, 2, { { Int, 0 }, { Timeval | OUT, 1 }}},
153e45a5a0dSDavid Malone 	{ "recvfrom", 1, 6, { { Int, 0 }, { Ptr | OUT, 1 }, { Int, 2 }, { Int, 3 }, { Sockaddr | OUT, 4}, {Ptr | OUT, 5}}},
154e45a5a0dSDavid Malone 	{ "getitimer", 1, 2, { { Int, 0 }, { Itimerval | OUT, 2 }}},
155e45a5a0dSDavid Malone 	{ "setitimer", 1, 3, { { Int, 0 }, { Itimerval, 1} , { Itimerval | OUT, 2 }}},
1563cf51049SPhilippe Charnier 	{ 0, 0, 0, { { 0, 0 }}},
157bbeaf6c0SSean Eric Fagan };
158bbeaf6c0SSean Eric Fagan 
159bbeaf6c0SSean Eric Fagan /*
160bbeaf6c0SSean Eric Fagan  * If/when the list gets big, it might be desirable to do it
161bbeaf6c0SSean Eric Fagan  * as a hash table or binary search.
162bbeaf6c0SSean Eric Fagan  */
163bbeaf6c0SSean Eric Fagan 
164bbeaf6c0SSean Eric Fagan struct syscall *
165bbeaf6c0SSean Eric Fagan get_syscall(const char *name) {
166bbeaf6c0SSean Eric Fagan 	struct syscall *sc = syscalls;
167bbeaf6c0SSean Eric Fagan 
168bbeaf6c0SSean Eric Fagan 	while (sc->name) {
169bbeaf6c0SSean Eric Fagan 		if (!strcmp(name, sc->name))
170bbeaf6c0SSean Eric Fagan 			return sc;
171bbeaf6c0SSean Eric Fagan 		sc++;
172bbeaf6c0SSean Eric Fagan 	}
173bbeaf6c0SSean Eric Fagan 	return NULL;
174bbeaf6c0SSean Eric Fagan }
175bbeaf6c0SSean Eric Fagan 
176bbeaf6c0SSean Eric Fagan /*
1779ddd1412SDag-Erling Smørgrav  * get_struct
1789ddd1412SDag-Erling Smørgrav  *
1799ddd1412SDag-Erling Smørgrav  * Copy a fixed amount of bytes from the process.
1809ddd1412SDag-Erling Smørgrav  */
1819ddd1412SDag-Erling Smørgrav 
1821be5d704SMark Murray static int
1839ddd1412SDag-Erling Smørgrav get_struct(int procfd, void *offset, void *buf, int len) {
1849ddd1412SDag-Erling Smørgrav 
185b637237cSDavid Malone 	if (pread(procfd, buf, len, (uintptr_t)offset) != len)
186b637237cSDavid Malone 		return -1;
1879ddd1412SDag-Erling Smørgrav 	return 0;
1889ddd1412SDag-Erling Smørgrav }
1899ddd1412SDag-Erling Smørgrav 
1909ddd1412SDag-Erling Smørgrav /*
191bbeaf6c0SSean Eric Fagan  * get_string
192bbeaf6c0SSean Eric Fagan  * Copy a string from the process.  Note that it is
193bbeaf6c0SSean Eric Fagan  * expected to be a C string, but if max is set, it will
194bbeaf6c0SSean Eric Fagan  * only get that much.
195bbeaf6c0SSean Eric Fagan  */
196bbeaf6c0SSean Eric Fagan 
197bbeaf6c0SSean Eric Fagan char *
198bbeaf6c0SSean Eric Fagan get_string(int procfd, void *offset, int max) {
1993cf51049SPhilippe Charnier 	char *buf;
20005d5ca35SSean Eric Fagan 	int size, len, c, fd;
201bbeaf6c0SSean Eric Fagan 	FILE *p;
202bbeaf6c0SSean Eric Fagan 
20305d5ca35SSean Eric Fagan 	if ((fd = dup(procfd)) == -1)
20405d5ca35SSean Eric Fagan 		err(1, "dup");
20505d5ca35SSean Eric Fagan 	if ((p = fdopen(fd, "r")) == NULL)
2063cf51049SPhilippe Charnier 		err(1, "fdopen");
207bbeaf6c0SSean Eric Fagan 	buf = malloc( size = (max ? max : 64 ) );
208bbeaf6c0SSean Eric Fagan 	len = 0;
2098a2ecea9SJoerg Wunsch 	buf[0] = 0;
2104e92419dSMarcel Moolenaar 	if (fseeko(p, (uintptr_t)offset, SEEK_SET) == 0) {
211bbeaf6c0SSean Eric Fagan 		while ((c = fgetc(p)) != EOF) {
212bbeaf6c0SSean Eric Fagan 			buf[len++] = c;
213bbeaf6c0SSean Eric Fagan 			if (c == 0 || len == max) {
214bbeaf6c0SSean Eric Fagan 				buf[len] = 0;
215bbeaf6c0SSean Eric Fagan 				break;
216bbeaf6c0SSean Eric Fagan 			}
217bbeaf6c0SSean Eric Fagan 			if (len == size) {
21805d5ca35SSean Eric Fagan 				char *tmp;
219bbeaf6c0SSean Eric Fagan 				tmp = realloc(buf, size+64);
220bbeaf6c0SSean Eric Fagan 				if (tmp == NULL) {
221bbeaf6c0SSean Eric Fagan 					buf[len] = 0;
2224e92419dSMarcel Moolenaar 					break;
223bbeaf6c0SSean Eric Fagan 				}
224bbeaf6c0SSean Eric Fagan 				size += 64;
22505d5ca35SSean Eric Fagan 				buf = tmp;
226bbeaf6c0SSean Eric Fagan 			}
227bbeaf6c0SSean Eric Fagan 		}
2284e92419dSMarcel Moolenaar 	}
22905d5ca35SSean Eric Fagan 	fclose(p);
2304e92419dSMarcel Moolenaar 	return (buf);
231bbeaf6c0SSean Eric Fagan }
232bbeaf6c0SSean Eric Fagan 
233bbeaf6c0SSean Eric Fagan 
234bbeaf6c0SSean Eric Fagan /*
235bbeaf6c0SSean Eric Fagan  * Gag.  This is really unportable.  Multiplication is more portable.
236bbeaf6c0SSean Eric Fagan  * But slower, from the code I saw.
237bbeaf6c0SSean Eric Fagan  */
238bbeaf6c0SSean Eric Fagan 
239bbeaf6c0SSean Eric Fagan static long long
240bbeaf6c0SSean Eric Fagan make_quad(unsigned long p1, unsigned long p2) {
241bbeaf6c0SSean Eric Fagan   union {
242bbeaf6c0SSean Eric Fagan     long long ll;
243bbeaf6c0SSean Eric Fagan     unsigned long l[2];
244bbeaf6c0SSean Eric Fagan   } t;
245bbeaf6c0SSean Eric Fagan   t.l[0] = p1;
246bbeaf6c0SSean Eric Fagan   t.l[1] = p2;
247bbeaf6c0SSean Eric Fagan   return t.ll;
248bbeaf6c0SSean Eric Fagan }
249bbeaf6c0SSean Eric Fagan 
250894b8f7aSAlfred Perlstein /*
251894b8f7aSAlfred Perlstein  * Remove a trailing '|' in a string, useful for fixup after decoding
252894b8f7aSAlfred Perlstein  * a "flags" argument.
253894b8f7aSAlfred Perlstein  */
254894b8f7aSAlfred Perlstein 
255894b8f7aSAlfred Perlstein void
256894b8f7aSAlfred Perlstein remove_trailing_or(char *str)
257894b8f7aSAlfred Perlstein {
258894b8f7aSAlfred Perlstein 
259894b8f7aSAlfred Perlstein 	if (str != NULL && (str = rindex(str, '|')) != NULL && str[1] == '\0')
260894b8f7aSAlfred Perlstein 		*str = '\0';
261894b8f7aSAlfred Perlstein }
262bbeaf6c0SSean Eric Fagan 
263bbeaf6c0SSean Eric Fagan /*
264bbeaf6c0SSean Eric Fagan  * print_arg
265bbeaf6c0SSean Eric Fagan  * Converts a syscall argument into a string.  Said string is
266bbeaf6c0SSean Eric Fagan  * allocated via malloc(), so needs to be free()'d.  The file
267bbeaf6c0SSean Eric Fagan  * descriptor is for the process' memory (via /proc), and is used
268bbeaf6c0SSean Eric Fagan  * to get any data (where the argument is a pointer).  sc is
269bbeaf6c0SSean Eric Fagan  * a pointer to the syscall description (see above); args is
270bbeaf6c0SSean Eric Fagan  * an array of all of the system call arguments.
271bbeaf6c0SSean Eric Fagan  */
272bbeaf6c0SSean Eric Fagan 
273bbeaf6c0SSean Eric Fagan char *
2742bae4eb3SAlfred Perlstein print_arg(int fd, struct syscall_args *sc, unsigned long *args, long retval) {
2753cf51049SPhilippe Charnier   char *tmp = NULL;
276bbeaf6c0SSean Eric Fagan   switch (sc->type & ARG_MASK) {
277bbeaf6c0SSean Eric Fagan   case Hex:
2781bcb5f5aSMarcel Moolenaar     asprintf(&tmp, "0x%lx", args[sc->offset]);
279bbeaf6c0SSean Eric Fagan     break;
280bbeaf6c0SSean Eric Fagan   case Octal:
2811bcb5f5aSMarcel Moolenaar     asprintf(&tmp, "0%lo", args[sc->offset]);
282bbeaf6c0SSean Eric Fagan     break;
283bbeaf6c0SSean Eric Fagan   case Int:
2841bcb5f5aSMarcel Moolenaar     asprintf(&tmp, "%ld", args[sc->offset]);
285bbeaf6c0SSean Eric Fagan     break;
286bbeaf6c0SSean Eric Fagan   case String:
287bbeaf6c0SSean Eric Fagan     {
288bbeaf6c0SSean Eric Fagan       char *tmp2;
289bbeaf6c0SSean Eric Fagan       tmp2 = get_string(fd, (void*)args[sc->offset], 0);
2901bcb5f5aSMarcel Moolenaar       asprintf(&tmp, "\"%s\"", tmp2);
291bbeaf6c0SSean Eric Fagan       free(tmp2);
292bbeaf6c0SSean Eric Fagan     }
293bbeaf6c0SSean Eric Fagan   break;
2949897b203SMatthew N. Dodd   case StringArray:
2959897b203SMatthew N. Dodd     {
2969897b203SMatthew N. Dodd       int num, size, i;
2979897b203SMatthew N. Dodd       char *tmp2;
2989897b203SMatthew N. Dodd       char *string;
2999897b203SMatthew N. Dodd       char *strarray[100];	/* XXX This is ugly. */
3009897b203SMatthew N. Dodd 
3019897b203SMatthew N. Dodd       if (get_struct(fd, (void *)args[sc->offset], (void *)&strarray,
3029897b203SMatthew N. Dodd                      sizeof(strarray)) == -1) {
3039897b203SMatthew N. Dodd 	err(1, "get_struct %p", (void *)args[sc->offset]);
3049897b203SMatthew N. Dodd       }
3059897b203SMatthew N. Dodd       num = 0;
3069897b203SMatthew N. Dodd       size = 0;
3079897b203SMatthew N. Dodd 
3089897b203SMatthew N. Dodd       /* Find out how large of a buffer we'll need. */
3099897b203SMatthew N. Dodd       while (strarray[num] != NULL) {
3109897b203SMatthew N. Dodd 	string = get_string(fd, (void*)strarray[num], 0);
3119897b203SMatthew N. Dodd         size += strlen(string);
3129897b203SMatthew N. Dodd 	free(string);
3139897b203SMatthew N. Dodd 	num++;
3149897b203SMatthew N. Dodd       }
3159897b203SMatthew N. Dodd       size += 4 + (num * 4);
3169897b203SMatthew N. Dodd       tmp = (char *)malloc(size);
3179897b203SMatthew N. Dodd       tmp2 = tmp;
3189897b203SMatthew N. Dodd 
3199897b203SMatthew N. Dodd       tmp2 += sprintf(tmp2, " [");
3209897b203SMatthew N. Dodd       for (i = 0; i < num; i++) {
3219897b203SMatthew N. Dodd 	string = get_string(fd, (void*)strarray[i], 0);
3229897b203SMatthew N. Dodd         tmp2 += sprintf(tmp2, " \"%s\"%c", string, (i+1 == num) ? ' ' : ',');
3239897b203SMatthew N. Dodd 	free(string);
3249897b203SMatthew N. Dodd       }
3259897b203SMatthew N. Dodd       tmp2 += sprintf(tmp2, "]");
3269897b203SMatthew N. Dodd     }
3279897b203SMatthew N. Dodd   break;
328bbeaf6c0SSean Eric Fagan   case Quad:
329bbeaf6c0SSean Eric Fagan     {
330bbeaf6c0SSean Eric Fagan       unsigned long long t;
331bbeaf6c0SSean Eric Fagan       unsigned long l1, l2;
332bbeaf6c0SSean Eric Fagan       l1 = args[sc->offset];
333bbeaf6c0SSean Eric Fagan       l2 = args[sc->offset+1];
334bbeaf6c0SSean Eric Fagan       t = make_quad(l1, l2);
3351bcb5f5aSMarcel Moolenaar       asprintf(&tmp, "0x%qx", t);
336bbeaf6c0SSean Eric Fagan       break;
337bbeaf6c0SSean Eric Fagan     }
338bbeaf6c0SSean Eric Fagan   case Ptr:
3391bcb5f5aSMarcel Moolenaar     asprintf(&tmp, "0x%lx", args[sc->offset]);
340bbeaf6c0SSean Eric Fagan     break;
3412bae4eb3SAlfred Perlstein   case Readlinkres:
3422bae4eb3SAlfred Perlstein     {
3432bae4eb3SAlfred Perlstein       char *tmp2;
3442bae4eb3SAlfred Perlstein       if (retval == -1) {
3452bae4eb3SAlfred Perlstein 	tmp = strdup("");
3462bae4eb3SAlfred Perlstein 	break;
3472bae4eb3SAlfred Perlstein       }
3482bae4eb3SAlfred Perlstein       tmp2 = get_string(fd, (void*)args[sc->offset], retval);
3492bae4eb3SAlfred Perlstein       asprintf(&tmp, "\"%s\"", tmp2);
3502bae4eb3SAlfred Perlstein       free(tmp2);
3512bae4eb3SAlfred Perlstein     }
3522bae4eb3SAlfred Perlstein   break;
353970649f9SSean Eric Fagan   case Ioctl:
354970649f9SSean Eric Fagan     {
3551be5d704SMark Murray       const char *temp = ioctlname(args[sc->offset]);
356970649f9SSean Eric Fagan       if (temp)
357970649f9SSean Eric Fagan 	tmp = strdup(temp);
3581bcb5f5aSMarcel Moolenaar       else
3591bcb5f5aSMarcel Moolenaar 	asprintf(&tmp, "0x%lx", args[sc->offset]);
360970649f9SSean Eric Fagan     }
361f0ebbc29SDag-Erling Smørgrav     break;
362e45a5a0dSDavid Malone   case Timespec:
363e45a5a0dSDavid Malone     {
364e45a5a0dSDavid Malone       struct timespec ts;
365e45a5a0dSDavid Malone       if (get_struct(fd, (void *)args[sc->offset], &ts, sizeof(ts)) != -1)
366e45a5a0dSDavid Malone 	asprintf(&tmp, "{%jd %jd}", (intmax_t)ts.tv_sec, (intmax_t)ts.tv_nsec);
367e45a5a0dSDavid Malone       else
368e45a5a0dSDavid Malone 	asprintf(&tmp, "0x%lx", args[sc->offset]);
369e45a5a0dSDavid Malone     }
370e45a5a0dSDavid Malone     break;
371e45a5a0dSDavid Malone   case Timeval:
372e45a5a0dSDavid Malone     {
373e45a5a0dSDavid Malone       struct timeval tv;
374e45a5a0dSDavid Malone       if (get_struct(fd, (void *)args[sc->offset], &tv, sizeof(tv)) != -1)
375e45a5a0dSDavid Malone 	asprintf(&tmp, "{%jd %jd}", (intmax_t)tv.tv_sec, (intmax_t)tv.tv_usec);
376e45a5a0dSDavid Malone       else
377e45a5a0dSDavid Malone 	asprintf(&tmp, "0x%lx", args[sc->offset]);
378e45a5a0dSDavid Malone     }
379e45a5a0dSDavid Malone     break;
380e45a5a0dSDavid Malone   case Itimerval:
381e45a5a0dSDavid Malone     {
382e45a5a0dSDavid Malone       struct itimerval itv;
383e45a5a0dSDavid Malone       if (get_struct(fd, (void *)args[sc->offset], &itv, sizeof(itv)) != -1)
384e45a5a0dSDavid Malone 	asprintf(&tmp, "{%jd %jd, %jd %jd}",
385e45a5a0dSDavid Malone 	    (intmax_t)itv.it_interval.tv_sec,
386e45a5a0dSDavid Malone 	    (intmax_t)itv.it_interval.tv_usec,
387e45a5a0dSDavid Malone 	    (intmax_t)itv.it_value.tv_sec,
388e45a5a0dSDavid Malone 	    (intmax_t)itv.it_value.tv_usec);
389e45a5a0dSDavid Malone       else
390e45a5a0dSDavid Malone 	asprintf(&tmp, "0x%lx", args[sc->offset]);
391e45a5a0dSDavid Malone     }
392e45a5a0dSDavid Malone     break;
393e45a5a0dSDavid Malone   case Pollfd:
394e45a5a0dSDavid Malone     {
395e45a5a0dSDavid Malone       /*
396e45a5a0dSDavid Malone        * XXX: A Pollfd argument expects the /next/ syscall argument to be
397e45a5a0dSDavid Malone        * the number of fds in the array. This matches the poll syscall.
398e45a5a0dSDavid Malone        */
399e45a5a0dSDavid Malone       struct pollfd *pfd;
400e45a5a0dSDavid Malone       int numfds = args[sc->offset+1];
401e45a5a0dSDavid Malone       int bytes = sizeof(struct pollfd) * numfds;
402e45a5a0dSDavid Malone       int i, tmpsize, u, used;
403e45a5a0dSDavid Malone       const int per_fd = 100;
404e45a5a0dSDavid Malone 
405e45a5a0dSDavid Malone       if ((pfd = malloc(bytes)) == NULL)
406e45a5a0dSDavid Malone 	err(1, "Cannot malloc %d bytes for pollfd array", bytes);
407e45a5a0dSDavid Malone       if (get_struct(fd, (void *)args[sc->offset], pfd, bytes) != -1) {
408e45a5a0dSDavid Malone 
409e45a5a0dSDavid Malone 	used = 0;
410e45a5a0dSDavid Malone 	tmpsize = 1 + per_fd * numfds + 2;
411e45a5a0dSDavid Malone 	if ((tmp = malloc(tmpsize)) == NULL)
412e45a5a0dSDavid Malone 	  err(1, "Cannot alloc %d bytes for poll output", tmpsize);
413e45a5a0dSDavid Malone 
414e45a5a0dSDavid Malone 	tmp[used++] = '{';
415e45a5a0dSDavid Malone 	for (i = 0; i < numfds; i++) {
416e45a5a0dSDavid Malone #define POLLKNOWN_EVENTS \
417e45a5a0dSDavid Malone 	(POLLIN | POLLPRI | POLLOUT | POLLERR | POLLHUP | POLLNVAL | \
418e45a5a0dSDavid Malone 	 POLLRDNORM |POLLRDBAND | POLLWRBAND | POLLINIGNEOF)
419e45a5a0dSDavid Malone 
420e45a5a0dSDavid Malone 	  u += snprintf(tmp + used, per_fd,
421e45a5a0dSDavid Malone 	    "%s%d 0x%hx%s%s%s%s%s%s%s%s%s ",
422e45a5a0dSDavid Malone 	    i > 0 ? " " : "",
423e45a5a0dSDavid Malone 	    pfd[i].fd,
424e45a5a0dSDavid Malone 	    pfd[i].events & ~POLLKNOWN_EVENTS,
425e45a5a0dSDavid Malone 	    pfd[i].events & POLLIN ? "" : "|IN",
426e45a5a0dSDavid Malone 	    pfd[i].events & POLLPRI ? "" : "|PRI",
427e45a5a0dSDavid Malone 	    pfd[i].events & POLLOUT ? "" : "|OUT",
428e45a5a0dSDavid Malone 	    pfd[i].events & POLLERR ? "" : "|ERR",
429e45a5a0dSDavid Malone 	    pfd[i].events & POLLHUP ? "" : "|HUP",
430e45a5a0dSDavid Malone 	    pfd[i].events & POLLNVAL ? "" : "|NVAL",
431e45a5a0dSDavid Malone 	    pfd[i].events & POLLRDNORM ? "" : "|RDNORM",
432e45a5a0dSDavid Malone 	    pfd[i].events & POLLRDBAND ? "" : "|RDBAND",
433e45a5a0dSDavid Malone 	    pfd[i].events & POLLWRBAND ? "" : "|WRBAND");
434e45a5a0dSDavid Malone 	  if (u > 0)
435e45a5a0dSDavid Malone 	    used += u < per_fd ? u : per_fd;
436e45a5a0dSDavid Malone 	}
437e45a5a0dSDavid Malone 	tmp[used++] = '}';
438e45a5a0dSDavid Malone 	tmp[used++] = '\0';
439e45a5a0dSDavid Malone       } else
440e45a5a0dSDavid Malone 	asprintf(&tmp, "0x%lx", args[sc->offset]);
441e45a5a0dSDavid Malone       free(pfd);
442e45a5a0dSDavid Malone     }
443e45a5a0dSDavid Malone     break;
444e45a5a0dSDavid Malone   case Fd_set:
445e45a5a0dSDavid Malone     {
446e45a5a0dSDavid Malone       /*
447e45a5a0dSDavid Malone        * XXX: A Fd_set argument expects the /first/ syscall argument to be
448e45a5a0dSDavid Malone        * the number of fds in the array.  This matches the select syscall.
449e45a5a0dSDavid Malone        */
450e45a5a0dSDavid Malone       fd_set *fds;
451e45a5a0dSDavid Malone       int numfds = args[0];
452e45a5a0dSDavid Malone       int bytes = _howmany(numfds, _NFDBITS) * _NFDBITS;
453e45a5a0dSDavid Malone       int i, tmpsize, u, used;
454e45a5a0dSDavid Malone       const int per_fd = 20;
455e45a5a0dSDavid Malone 
456e45a5a0dSDavid Malone       if ((fds = malloc(bytes)) == NULL)
457e45a5a0dSDavid Malone 	err(1, "Cannot malloc %d bytes for fd_set array", bytes);
458e45a5a0dSDavid Malone       if (get_struct(fd, (void *)args[sc->offset], fds, bytes) != -1) {
459e45a5a0dSDavid Malone 	used = 0;
460e45a5a0dSDavid Malone 	tmpsize = 1 + numfds * per_fd + 2;
461e45a5a0dSDavid Malone 	if ((tmp = malloc(tmpsize)) == NULL)
462e45a5a0dSDavid Malone 	  err(1, "Cannot alloc %d bytes for fd_set output", tmpsize);
463e45a5a0dSDavid Malone 
464e45a5a0dSDavid Malone 	tmp[used++] = '{';
465e45a5a0dSDavid Malone 	for (i = 0; i < numfds; i++) {
466e45a5a0dSDavid Malone 	  if (FD_ISSET(i, fds)) {
467e45a5a0dSDavid Malone 	    u = snprintf(tmp + used, per_fd, "%d ", i);
468e45a5a0dSDavid Malone 	    if (u > 0)
469e45a5a0dSDavid Malone 	      used += u < per_fd ? u : per_fd;
470e45a5a0dSDavid Malone 	  }
471e45a5a0dSDavid Malone 	}
472e45a5a0dSDavid Malone 	if (tmp[used-1] == ' ')
473e45a5a0dSDavid Malone 		used--;
474e45a5a0dSDavid Malone 	tmp[used++] = '}';
475e45a5a0dSDavid Malone 	tmp[used++] = '\0';
476e45a5a0dSDavid Malone       } else
477e45a5a0dSDavid Malone 	asprintf(&tmp, "0x%lx", args[sc->offset]);
478e45a5a0dSDavid Malone       free(fds);
479e45a5a0dSDavid Malone     }
480e45a5a0dSDavid Malone     break;
481f0ebbc29SDag-Erling Smørgrav   case Signal:
482f0ebbc29SDag-Erling Smørgrav     {
483f0ebbc29SDag-Erling Smørgrav       long sig;
484f0ebbc29SDag-Erling Smørgrav 
485f0ebbc29SDag-Erling Smørgrav       sig = args[sc->offset];
486d75300bfSAlfred Perlstein       tmp = strsig(sig);
487d75300bfSAlfred Perlstein       if (tmp == NULL)
4881bcb5f5aSMarcel Moolenaar         asprintf(&tmp, "%ld", sig);
489f0ebbc29SDag-Erling Smørgrav     }
490f0ebbc29SDag-Erling Smørgrav     break;
491894b8f7aSAlfred Perlstein   case Fcntl:
492894b8f7aSAlfred Perlstein     {
493894b8f7aSAlfred Perlstein 	switch (args[sc->offset]) {
494894b8f7aSAlfred Perlstein #define S(a)	case a: tmp = strdup(#a); break;
495894b8f7aSAlfred Perlstein 	S(F_DUPFD);
496894b8f7aSAlfred Perlstein 	S(F_GETFD);
497894b8f7aSAlfred Perlstein 	S(F_SETFD);
498894b8f7aSAlfred Perlstein 	S(F_GETFL);
499894b8f7aSAlfred Perlstein 	S(F_SETFL);
500894b8f7aSAlfred Perlstein 	S(F_GETOWN);
501894b8f7aSAlfred Perlstein 	S(F_SETOWN);
502894b8f7aSAlfred Perlstein 	S(F_GETLK);
503894b8f7aSAlfred Perlstein 	S(F_SETLK);
504894b8f7aSAlfred Perlstein 	S(F_SETLKW);
505894b8f7aSAlfred Perlstein #undef S
506894b8f7aSAlfred Perlstein 	}
507894b8f7aSAlfred Perlstein 	if (tmp == NULL)
508894b8f7aSAlfred Perlstein     		asprintf(&tmp, "0x%lx", args[sc->offset]);
509894b8f7aSAlfred Perlstein     }
510894b8f7aSAlfred Perlstein     break;
511894b8f7aSAlfred Perlstein 
512894b8f7aSAlfred Perlstein   case Mprot:
513894b8f7aSAlfred Perlstein     {
514894b8f7aSAlfred Perlstein 
515894b8f7aSAlfred Perlstein #define S(a)	((args[sc->offset] & a) ? #a "|" : "")
516894b8f7aSAlfred Perlstein 	    asprintf(&tmp, "(0x%lx)%s%s%s%s", args[sc->offset],
517894b8f7aSAlfred Perlstein 		S(PROT_NONE), S(PROT_READ), S(PROT_WRITE), S(PROT_EXEC));
518894b8f7aSAlfred Perlstein #undef S
519894b8f7aSAlfred Perlstein 	    remove_trailing_or(tmp);
520894b8f7aSAlfred Perlstein 
521894b8f7aSAlfred Perlstein     }
522894b8f7aSAlfred Perlstein     break;
523894b8f7aSAlfred Perlstein 
524894b8f7aSAlfred Perlstein   case Mmapflags:
525894b8f7aSAlfred Perlstein     {
526894b8f7aSAlfred Perlstein #define S(a)	((args[sc->offset] & a) ? #a "|" : "")
527894b8f7aSAlfred Perlstein 	    asprintf(&tmp, "(0x%lx)%s%s%s%s%s%s%s%s", args[sc->offset],
528894b8f7aSAlfred Perlstein 		S(MAP_ANON), S(MAP_FIXED), S(MAP_HASSEMAPHORE),
529894b8f7aSAlfred Perlstein 		S(MAP_NOCORE), S(MAP_NOSYNC), S(MAP_PRIVATE),
530894b8f7aSAlfred Perlstein 		S(MAP_SHARED), S(MAP_STACK));
531894b8f7aSAlfred Perlstein #undef S
532894b8f7aSAlfred Perlstein 
533894b8f7aSAlfred Perlstein 	    remove_trailing_or(tmp);
534894b8f7aSAlfred Perlstein     }
535894b8f7aSAlfred Perlstein     break;
536894b8f7aSAlfred Perlstein 
537fde3a7d1SAlfred Perlstein   case Whence:
538fde3a7d1SAlfred Perlstein     {
539fde3a7d1SAlfred Perlstein 	switch (args[sc->offset]) {
540fde3a7d1SAlfred Perlstein #define S(a)	case a: tmp = strdup(#a); break;
541fde3a7d1SAlfred Perlstein 	S(SEEK_SET);
542fde3a7d1SAlfred Perlstein 	S(SEEK_CUR);
543fde3a7d1SAlfred Perlstein 	S(SEEK_END);
544fde3a7d1SAlfred Perlstein #undef S
545fde3a7d1SAlfred Perlstein 	default: asprintf(&tmp, "0x%lx", args[sc->offset]); break;
546fde3a7d1SAlfred Perlstein 	}
547fde3a7d1SAlfred Perlstein     }
548fde3a7d1SAlfred Perlstein     break;
5499ddd1412SDag-Erling Smørgrav   case Sockaddr:
5509ddd1412SDag-Erling Smørgrav     {
551dec17687SBrian Feldman       struct sockaddr_storage ss;
5529ddd1412SDag-Erling Smørgrav       char addr[64];
5531be5d704SMark Murray       struct sockaddr_in *lsin;
5541be5d704SMark Murray       struct sockaddr_in6 *lsin6;
555dec17687SBrian Feldman       struct sockaddr_un *sun;
556dec17687SBrian Feldman       struct sockaddr *sa;
557dec17687SBrian Feldman       char *p;
558dec17687SBrian Feldman       u_char *q;
559dec17687SBrian Feldman       int i;
5609ddd1412SDag-Erling Smørgrav 
561a7a08c7eSMarcel Moolenaar       if (args[sc->offset] == 0) {
562a7a08c7eSMarcel Moolenaar 	      asprintf(&tmp, "NULL");
563a7a08c7eSMarcel Moolenaar 	      break;
564a7a08c7eSMarcel Moolenaar       }
565a7a08c7eSMarcel Moolenaar 
566dec17687SBrian Feldman       /* yuck: get ss_len */
567dec17687SBrian Feldman       if (get_struct(fd, (void *)args[sc->offset], (void *)&ss,
568dec17687SBrian Feldman 	sizeof(ss.ss_len) + sizeof(ss.ss_family)) == -1)
569dec17687SBrian Feldman 	err(1, "get_struct %p", (void *)args[sc->offset]);
570dec17687SBrian Feldman       /* sockaddr_un never have the length filled in! */
571dec17687SBrian Feldman       if (ss.ss_family == AF_UNIX) {
572dec17687SBrian Feldman 	if (get_struct(fd, (void *)args[sc->offset], (void *)&ss,
573dec17687SBrian Feldman 	  sizeof(*sun))
574dec17687SBrian Feldman 	  == -1)
575dec17687SBrian Feldman 	  err(2, "get_struct %p", (void *)args[sc->offset]);
5769ddd1412SDag-Erling Smørgrav       } else {
577dec17687SBrian Feldman 	if (get_struct(fd, (void *)args[sc->offset], (void *)&ss, ss.ss_len)
578dec17687SBrian Feldman 	  == -1)
579dec17687SBrian Feldman 	  err(2, "get_struct %p", (void *)args[sc->offset]);
5809ddd1412SDag-Erling Smørgrav       }
581dec17687SBrian Feldman 
582dec17687SBrian Feldman       switch (ss.ss_family) {
583dec17687SBrian Feldman       case AF_INET:
5841be5d704SMark Murray 	lsin = (struct sockaddr_in *)&ss;
5851be5d704SMark Murray 	inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof addr);
5861be5d704SMark Murray 	asprintf(&tmp, "{ AF_INET %s:%d }", addr, htons(lsin->sin_port));
587dec17687SBrian Feldman 	break;
588dec17687SBrian Feldman       case AF_INET6:
5891be5d704SMark Murray 	lsin6 = (struct sockaddr_in6 *)&ss;
5901be5d704SMark Murray 	inet_ntop(AF_INET6, &lsin6->sin6_addr, addr, sizeof addr);
5911be5d704SMark Murray 	asprintf(&tmp, "{ AF_INET6 [%s]:%d }", addr, htons(lsin6->sin6_port));
592dec17687SBrian Feldman 	break;
593dec17687SBrian Feldman       case AF_UNIX:
594dec17687SBrian Feldman         sun = (struct sockaddr_un *)&ss;
595dec17687SBrian Feldman         asprintf(&tmp, "{ AF_UNIX \"%s\" }", sun->sun_path);
596dec17687SBrian Feldman 	break;
597dec17687SBrian Feldman       default:
598dec17687SBrian Feldman 	sa = (struct sockaddr *)&ss;
599dec17687SBrian Feldman         asprintf(&tmp, "{ sa_len = %d, sa_family = %d, sa_data = {%n%*s } }",
600dec17687SBrian Feldman 	  (int)sa->sa_len, (int)sa->sa_family, &i,
601dec17687SBrian Feldman 	  6 * (int)(sa->sa_len - ((char *)&sa->sa_data - (char *)sa)), "");
602dec17687SBrian Feldman 	if (tmp != NULL) {
603dec17687SBrian Feldman 	  p = tmp + i;
604dec17687SBrian Feldman           for (q = (u_char *)&sa->sa_data; q < (u_char *)sa + sa->sa_len; q++)
605dec17687SBrian Feldman             p += sprintf(p, " %#02x,", *q);
606dec17687SBrian Feldman 	}
607dec17687SBrian Feldman       }
6089ddd1412SDag-Erling Smørgrav     }
6099ddd1412SDag-Erling Smørgrav     break;
610e45a5a0dSDavid Malone   case Sigaction:
611e45a5a0dSDavid Malone     {
612e45a5a0dSDavid Malone       struct sigaction sa;
613e45a5a0dSDavid Malone       char *hand;
614e45a5a0dSDavid Malone       const char *h;
615e45a5a0dSDavid Malone #define SA_KNOWN_FLAGS \
616e45a5a0dSDavid Malone 	(SA_ONSTACK | SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER | \
61701b8b4dcSPoul-Henning Kamp 	 SA_NOCLDWAIT | SA_SIGINFO)
618e45a5a0dSDavid Malone 
619e45a5a0dSDavid Malone 
620e45a5a0dSDavid Malone       if (get_struct(fd, (void *)args[sc->offset], &sa, sizeof(sa)) != -1) {
621e45a5a0dSDavid Malone 
622e45a5a0dSDavid Malone 	asprintf(&hand, "%p", sa.sa_handler);
623e45a5a0dSDavid Malone 	if (sa.sa_handler == SIG_DFL)
624e45a5a0dSDavid Malone 	  h = "SIG_DFL";
625e45a5a0dSDavid Malone 	else if (sa.sa_handler == SIG_IGN)
626e45a5a0dSDavid Malone 	  h = "SIG_IGN";
627e45a5a0dSDavid Malone 	else
628e45a5a0dSDavid Malone 	  h = hand;
62901b8b4dcSPoul-Henning Kamp 	asprintf(&tmp, "{ %s 0x%x%s%s%s%s%s%s%s ss_t }",
630e45a5a0dSDavid Malone 	    h,
631e45a5a0dSDavid Malone 	    sa.sa_flags & ~SA_KNOWN_FLAGS,
632e45a5a0dSDavid Malone 	    sa.sa_flags & SA_ONSTACK ? "" : "|ONSTACK",
633e45a5a0dSDavid Malone 	    sa.sa_flags & SA_RESTART ? "" : "|RESTART",
634e45a5a0dSDavid Malone 	    sa.sa_flags & SA_RESETHAND ? "" : "|RESETHAND",
635e45a5a0dSDavid Malone 	    sa.sa_flags & SA_NOCLDSTOP ? "" : "|NOCLDSTOP",
636e45a5a0dSDavid Malone 	    sa.sa_flags & SA_NODEFER ? "" : "|NODEFER",
637e45a5a0dSDavid Malone 	    sa.sa_flags & SA_NOCLDWAIT ? "" : "|NOCLDWAIT",
63801b8b4dcSPoul-Henning Kamp 	    sa.sa_flags & SA_SIGINFO ? "" : "|SIGINFO");
639e45a5a0dSDavid Malone 	free(hand);
640e45a5a0dSDavid Malone       } else
641e45a5a0dSDavid Malone 	asprintf(&tmp, "0x%lx", args[sc->offset]);
642e45a5a0dSDavid Malone 
643e45a5a0dSDavid Malone     }
644e45a5a0dSDavid Malone     break;
645bbeaf6c0SSean Eric Fagan   }
646bbeaf6c0SSean Eric Fagan   return tmp;
647bbeaf6c0SSean Eric Fagan }
648bbeaf6c0SSean Eric Fagan 
649203098d8SMatthew N. Dodd #define timespecsubt(tvp, uvp, vvp)					\
650203098d8SMatthew N. Dodd 	do {								\
651203098d8SMatthew N. Dodd 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
652203098d8SMatthew N. Dodd 		(vvp)->tv_nsec = (tvp)->tv_nsec - (uvp)->tv_nsec;	\
653203098d8SMatthew N. Dodd 		if ((vvp)->tv_nsec < 0) {				\
654203098d8SMatthew N. Dodd 			(vvp)->tv_sec--;				\
655203098d8SMatthew N. Dodd 			(vvp)->tv_nsec += 1000000000;			\
656203098d8SMatthew N. Dodd 		}							\
657203098d8SMatthew N. Dodd 	} while (0)
658203098d8SMatthew N. Dodd 
659bbeaf6c0SSean Eric Fagan /*
660bbeaf6c0SSean Eric Fagan  * print_syscall
661bbeaf6c0SSean Eric Fagan  * Print (to outfile) the system call and its arguments.  Note that
662bbeaf6c0SSean Eric Fagan  * nargs is the number of arguments (not the number of words; this is
663bbeaf6c0SSean Eric Fagan  * potentially confusing, I know).
664bbeaf6c0SSean Eric Fagan  */
665bbeaf6c0SSean Eric Fagan 
666bbeaf6c0SSean Eric Fagan void
667ec0bed25SMatthew N. Dodd print_syscall(struct trussinfo *trussinfo, const char *name, int nargs, char **s_args) {
668bbeaf6c0SSean Eric Fagan   int i;
6696cb533feSSean Eric Fagan   int len = 0;
670203098d8SMatthew N. Dodd   struct timespec timediff;
6710d0bd00eSMatthew N. Dodd 
672c03bfcc8SMatthew N. Dodd   if (trussinfo->flags & FOLLOWFORKS)
673c03bfcc8SMatthew N. Dodd     len += fprintf(trussinfo->outfile, "%5d: ", trussinfo->pid);
674c03bfcc8SMatthew N. Dodd 
675967706f3SDavid Malone   if (name != NULL && (!strcmp(name, "execve") || !strcmp(name, "exit"))) {
676203098d8SMatthew N. Dodd     clock_gettime(CLOCK_REALTIME, &trussinfo->after);
6770d0bd00eSMatthew N. Dodd   }
6780d0bd00eSMatthew N. Dodd 
6790d0bd00eSMatthew N. Dodd   if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
680203098d8SMatthew N. Dodd     timespecsubt(&trussinfo->after, &trussinfo->start_time, &timediff);
68137169f94SMatthew N. Dodd     len += fprintf(trussinfo->outfile, "%ld.%09ld ",
682fb034d05SMatthew N. Dodd 		   (long)timediff.tv_sec, timediff.tv_nsec);
6830d0bd00eSMatthew N. Dodd   }
6840d0bd00eSMatthew N. Dodd 
6850d0bd00eSMatthew N. Dodd   if (trussinfo->flags & RELATIVETIMESTAMPS) {
686203098d8SMatthew N. Dodd     timespecsubt(&trussinfo->after, &trussinfo->before, &timediff);
68737169f94SMatthew N. Dodd     len += fprintf(trussinfo->outfile, "%ld.%09ld ",
688fb034d05SMatthew N. Dodd 		   (long)timediff.tv_sec, timediff.tv_nsec);
6890d0bd00eSMatthew N. Dodd   }
6900d0bd00eSMatthew N. Dodd 
691ec0bed25SMatthew N. Dodd   len += fprintf(trussinfo->outfile, "%s(", name);
692c03bfcc8SMatthew N. Dodd 
693bbeaf6c0SSean Eric Fagan   for (i = 0; i < nargs; i++) {
694bbeaf6c0SSean Eric Fagan     if (s_args[i])
695ec0bed25SMatthew N. Dodd       len += fprintf(trussinfo->outfile, "%s", s_args[i]);
696bbeaf6c0SSean Eric Fagan     else
697ec0bed25SMatthew N. Dodd       len += fprintf(trussinfo->outfile, "<missing argument>");
698ec0bed25SMatthew N. Dodd     len += fprintf(trussinfo->outfile, "%s", i < (nargs - 1) ? "," : "");
699bbeaf6c0SSean Eric Fagan   }
700ec0bed25SMatthew N. Dodd   len += fprintf(trussinfo->outfile, ")");
7016cb533feSSean Eric Fagan   for (i = 0; i < 6 - (len / 8); i++)
702ec0bed25SMatthew N. Dodd 	fprintf(trussinfo->outfile, "\t");
7036cb533feSSean Eric Fagan }
7046cb533feSSean Eric Fagan 
7056cb533feSSean Eric Fagan void
7061bcb5f5aSMarcel Moolenaar print_syscall_ret(struct trussinfo *trussinfo, const char *name, int nargs,
7071bcb5f5aSMarcel Moolenaar     char **s_args, int errorp, long retval)
7081bcb5f5aSMarcel Moolenaar {
709ec0bed25SMatthew N. Dodd   print_syscall(trussinfo, name, nargs, s_args);
7106cb533feSSean Eric Fagan   if (errorp) {
7119b83b9f7SDavid Malone     fprintf(trussinfo->outfile, " ERR#%ld '%s'\n", retval, strerror(retval));
7126cb533feSSean Eric Fagan   } else {
7131bcb5f5aSMarcel Moolenaar     fprintf(trussinfo->outfile, " = %ld (0x%lx)\n", retval, retval);
7146cb533feSSean Eric Fagan   }
715bbeaf6c0SSean Eric Fagan }
716