xref: /freebsd/usr.bin/truss/syscalls.c (revision 726c2ca08094ac41cdc750088e94b21dbf9140d3)
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 
429ddd1412SDag-Erling Smørgrav #include <sys/types.h>
439ddd1412SDag-Erling Smørgrav #include <sys/socket.h>
441d631f7eSMike Barcroft #include <sys/time.h>
459ddd1412SDag-Erling Smørgrav #include <sys/un.h>
469ddd1412SDag-Erling Smørgrav #include <netinet/in.h>
479ddd1412SDag-Erling Smørgrav #include <arpa/inet.h>
489ddd1412SDag-Erling Smørgrav 
49dec17687SBrian Feldman #include <ctype.h>
503cf51049SPhilippe Charnier #include <err.h>
519ddd1412SDag-Erling Smørgrav #include <signal.h>
52bbeaf6c0SSean Eric Fagan #include <stdio.h>
53bbeaf6c0SSean Eric Fagan #include <stdlib.h>
54bbeaf6c0SSean Eric Fagan #include <string.h>
5537169f94SMatthew N. Dodd #include <time.h>
56bbeaf6c0SSean Eric Fagan #include <unistd.h>
579ddd1412SDag-Erling Smørgrav 
58ec0bed25SMatthew N. Dodd #include "truss.h"
591be5d704SMark Murray #include "extern.h"
60bbeaf6c0SSean Eric Fagan #include "syscall.h"
61bbeaf6c0SSean Eric Fagan 
62bbeaf6c0SSean Eric Fagan /*
63bbeaf6c0SSean Eric Fagan  * This should probably be in its own file.
64bbeaf6c0SSean Eric Fagan  */
65bbeaf6c0SSean Eric Fagan 
66bbeaf6c0SSean Eric Fagan struct syscall syscalls[] = {
67bbeaf6c0SSean Eric Fagan 	{ "readlink", 1, 3,
68bbeaf6c0SSean Eric Fagan 	  { { String, 0 } , { String | OUT, 1 }, { Int, 2 }}},
69bbeaf6c0SSean Eric Fagan 	{ "lseek", 2, 3,
70bbeaf6c0SSean Eric Fagan 	  { { Int, 0 }, {Quad, 2 }, { Int, 4 }}},
71bbeaf6c0SSean Eric Fagan 	{ "mmap", 2, 6,
72bbeaf6c0SSean Eric Fagan 	  { { Hex, 0 }, {Int, 1}, {Hex, 2}, {Hex, 3}, {Int, 4}, {Quad, 6}}},
73bbeaf6c0SSean Eric Fagan 	{ "open", 1, 3,
7409bf1169SAlfred Perlstein 	  { { String | IN, 0} , { Hex, 1}, {Octal, 2}}},
75bbeaf6c0SSean Eric Fagan 	{ "linux_open", 1, 3,
7609bf1169SAlfred Perlstein 	  { { String, 0 }, { Hex, 1}, { Octal, 2 }}},
77726c2ca0SMatthew N. Dodd 	{ "close", 1, 1,
78726c2ca0SMatthew N. Dodd 	  { { Int, 0 } } },
79726c2ca0SMatthew N. Dodd 	{ "link", 0, 2,
80726c2ca0SMatthew N. Dodd 	  { { String, 0 }, { String, 1 }}},
81726c2ca0SMatthew N. Dodd 	{ "unlink", 0, 1,
82726c2ca0SMatthew N. Dodd 	  { { String, 0 }}},
83726c2ca0SMatthew N. Dodd 	{ "chdir", 0, 1,
84726c2ca0SMatthew N. Dodd 	  { { String, 0 }}},
85726c2ca0SMatthew N. Dodd 	{ "mknod", 0, 3,
86726c2ca0SMatthew N. Dodd 	  { { String, 0 }, { Octal, 1 }, { Int, 3 }}},
87726c2ca0SMatthew N. Dodd 	{ "chmod", 0, 2,
88726c2ca0SMatthew N. Dodd 	  { { String, 0 }, { Octal, 1 }}},
89726c2ca0SMatthew N. Dodd 	{ "chown", 0, 3,
90726c2ca0SMatthew N. Dodd 	  { { String, 0 }, { Int, 1 }, { Int, 2 }}},
91726c2ca0SMatthew N. Dodd 	{ "mount", 0, 4,
92726c2ca0SMatthew N. Dodd 	  { { String, 0 }, { String, 1 }, { Int, 2 }, { Ptr, 3 }}},
93726c2ca0SMatthew N. Dodd 	{ "umount", 0, 2,
94726c2ca0SMatthew N. Dodd 	  { { String, 0 }, { Int, 2 }}},
95bbeaf6c0SSean Eric Fagan 	{ "fstat", 1, 2,
96bbeaf6c0SSean Eric Fagan 	  { { Int, 0},  {Ptr | OUT , 1 }}},
97bbeaf6c0SSean Eric Fagan 	{ "stat", 1, 2,
98bbeaf6c0SSean Eric Fagan 	  { { String | IN, 0 }, { Ptr | OUT, 1 }}},
99a8f37218SSean Eric Fagan 	{ "lstat", 1, 2,
100a8f37218SSean Eric Fagan 	  { { String | IN, 0 }, { Ptr | OUT, 1 }}},
101bbeaf6c0SSean Eric Fagan 	{ "linux_newstat", 1, 2,
102bbeaf6c0SSean Eric Fagan 	  { { String | IN, 0 }, { Ptr | OUT, 1 }}},
103bbeaf6c0SSean Eric Fagan 	{ "linux_newfstat", 1, 2,
104bbeaf6c0SSean Eric Fagan 	  { { Int, 0 }, { Ptr | OUT, 1 }}},
105bbeaf6c0SSean Eric Fagan 	{ "write", 1, 3,
106bbeaf6c0SSean Eric Fagan 	  { { Int, 0}, { Ptr | IN, 1 }, { Int, 2 }}},
107970649f9SSean Eric Fagan 	{ "ioctl", 1, 3,
108970649f9SSean Eric Fagan 	  { { Int, 0}, { Ioctl, 1 }, { Hex, 2 }}},
109bbeaf6c0SSean Eric Fagan 	{ "break", 1, 1, { { Hex, 0 }}},
110bbeaf6c0SSean Eric Fagan 	{ "exit", 0, 1, { { Hex, 0 }}},
11110714102SDag-Erling Smørgrav 	{ "access", 1, 2, { { String | IN, 0 }, { Int, 1 }}},
112f0ebbc29SDag-Erling Smørgrav 	{ "sigaction", 1, 3,
113f0ebbc29SDag-Erling Smørgrav 	  { { Signal, 0 }, { Ptr | IN, 1 }, { Ptr | OUT, 2 }}},
1149ddd1412SDag-Erling Smørgrav 	{ "accept", 1, 3,
1159ddd1412SDag-Erling Smørgrav 	  { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
1169ddd1412SDag-Erling Smørgrav 	{ "bind", 1, 3,
1179ddd1412SDag-Erling Smørgrav 	  { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
1189ddd1412SDag-Erling Smørgrav 	{ "connect", 1, 3,
1199ddd1412SDag-Erling Smørgrav 	  { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
1209ddd1412SDag-Erling Smørgrav 	{ "getpeername", 1, 3,
1219ddd1412SDag-Erling Smørgrav 	  { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
1229ddd1412SDag-Erling Smørgrav 	{ "getsockname", 1, 3,
1239ddd1412SDag-Erling Smørgrav 	  { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
1249897b203SMatthew N. Dodd 	{ "execve", 1, 3,
1259897b203SMatthew N. Dodd 	  { { String | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } },
1269897b203SMatthew N. Dodd 	{ "linux_execve", 1, 3,
1279897b203SMatthew N. Dodd 	  { { String | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } },
128726c2ca0SMatthew N. Dodd 	{ "kldload", 0, 1, { { String | IN, 0 }}},
129726c2ca0SMatthew N. Dodd 	{ "kldunload", 0, 1, { { Int, 0 }}},
130726c2ca0SMatthew N. Dodd 	{ "kldfind", 0, 1, { { String | IN, 0 }}},
131726c2ca0SMatthew N. Dodd 	{ "kldnext", 0, 1, { { Int, 0 }}},
132726c2ca0SMatthew N. Dodd 	{ "kldstat", 0, 2, { { Int, 0 }, { Ptr, 1 }}},
133726c2ca0SMatthew N. Dodd 	{ "kldfirstmod", 0, 1, { { Int, 0 }}},
1343cf51049SPhilippe Charnier 	{ 0, 0, 0, { { 0, 0 }}},
135bbeaf6c0SSean Eric Fagan };
136bbeaf6c0SSean Eric Fagan 
137bbeaf6c0SSean Eric Fagan /*
138bbeaf6c0SSean Eric Fagan  * If/when the list gets big, it might be desirable to do it
139bbeaf6c0SSean Eric Fagan  * as a hash table or binary search.
140bbeaf6c0SSean Eric Fagan  */
141bbeaf6c0SSean Eric Fagan 
142bbeaf6c0SSean Eric Fagan struct syscall *
143bbeaf6c0SSean Eric Fagan get_syscall(const char *name) {
144bbeaf6c0SSean Eric Fagan 	struct syscall *sc = syscalls;
145bbeaf6c0SSean Eric Fagan 
146bbeaf6c0SSean Eric Fagan 	while (sc->name) {
147bbeaf6c0SSean Eric Fagan 		if (!strcmp(name, sc->name))
148bbeaf6c0SSean Eric Fagan 			return sc;
149bbeaf6c0SSean Eric Fagan 		sc++;
150bbeaf6c0SSean Eric Fagan 	}
151bbeaf6c0SSean Eric Fagan 	return NULL;
152bbeaf6c0SSean Eric Fagan }
153bbeaf6c0SSean Eric Fagan 
154bbeaf6c0SSean Eric Fagan /*
1559ddd1412SDag-Erling Smørgrav  * get_struct
1569ddd1412SDag-Erling Smørgrav  *
1579ddd1412SDag-Erling Smørgrav  * Copy a fixed amount of bytes from the process.
1589ddd1412SDag-Erling Smørgrav  */
1599ddd1412SDag-Erling Smørgrav 
1601be5d704SMark Murray static int
1619ddd1412SDag-Erling Smørgrav get_struct(int procfd, void *offset, void *buf, int len) {
1629ddd1412SDag-Erling Smørgrav 	char *pos;
1639ddd1412SDag-Erling Smørgrav 	FILE *p;
1649ddd1412SDag-Erling Smørgrav 	int c, fd;
1659ddd1412SDag-Erling Smørgrav 
1669ddd1412SDag-Erling Smørgrav 	if ((fd = dup(procfd)) == -1)
1679ddd1412SDag-Erling Smørgrav 		err(1, "dup");
1689ddd1412SDag-Erling Smørgrav 	if ((p = fdopen(fd, "r")) == NULL)
1699ddd1412SDag-Erling Smørgrav 		err(1, "fdopen");
1707c8225caSDavid Malone 	fseeko(p, (uintptr_t)offset, SEEK_SET);
1719ddd1412SDag-Erling Smørgrav 	for (pos = (char *)buf; len--; pos++) {
1729ddd1412SDag-Erling Smørgrav 		if ((c = fgetc(p)) == EOF)
1739ddd1412SDag-Erling Smørgrav 			return -1;
1749ddd1412SDag-Erling Smørgrav 		*pos = c;
1759ddd1412SDag-Erling Smørgrav 	}
1769ddd1412SDag-Erling Smørgrav 	fclose(p);
1779ddd1412SDag-Erling Smørgrav 	return 0;
1789ddd1412SDag-Erling Smørgrav }
1799ddd1412SDag-Erling Smørgrav 
1809ddd1412SDag-Erling Smørgrav /*
181bbeaf6c0SSean Eric Fagan  * get_string
182bbeaf6c0SSean Eric Fagan  * Copy a string from the process.  Note that it is
183bbeaf6c0SSean Eric Fagan  * expected to be a C string, but if max is set, it will
184bbeaf6c0SSean Eric Fagan  * only get that much.
185bbeaf6c0SSean Eric Fagan  */
186bbeaf6c0SSean Eric Fagan 
187bbeaf6c0SSean Eric Fagan char *
188bbeaf6c0SSean Eric Fagan get_string(int procfd, void *offset, int max) {
1893cf51049SPhilippe Charnier 	char *buf;
19005d5ca35SSean Eric Fagan 	int size, len, c, fd;
191bbeaf6c0SSean Eric Fagan 	FILE *p;
192bbeaf6c0SSean Eric Fagan 
19305d5ca35SSean Eric Fagan 	if ((fd = dup(procfd)) == -1)
19405d5ca35SSean Eric Fagan 		err(1, "dup");
19505d5ca35SSean Eric Fagan 	if ((p = fdopen(fd, "r")) == NULL)
1963cf51049SPhilippe Charnier 		err(1, "fdopen");
197bbeaf6c0SSean Eric Fagan 	buf = malloc( size = (max ? max : 64 ) );
198bbeaf6c0SSean Eric Fagan 	len = 0;
1998a2ecea9SJoerg Wunsch 	buf[0] = 0;
2007c8225caSDavid Malone 	fseeko(p, (uintptr_t)offset, SEEK_SET);
201bbeaf6c0SSean Eric Fagan 	while ((c = fgetc(p)) != EOF) {
202bbeaf6c0SSean Eric Fagan 		buf[len++] = c;
203bbeaf6c0SSean Eric Fagan 		if (c == 0 || len == max) {
204bbeaf6c0SSean Eric Fagan 			buf[len] = 0;
205bbeaf6c0SSean Eric Fagan 			break;
206bbeaf6c0SSean Eric Fagan 		}
207bbeaf6c0SSean Eric Fagan 		if (len == size) {
20805d5ca35SSean Eric Fagan 			char *tmp;
209bbeaf6c0SSean Eric Fagan 			tmp = realloc(buf, size+64);
210bbeaf6c0SSean Eric Fagan 			if (tmp == NULL) {
211bbeaf6c0SSean Eric Fagan 				buf[len] = 0;
21205d5ca35SSean Eric Fagan 				fclose(p);
213bbeaf6c0SSean Eric Fagan 				return buf;
214bbeaf6c0SSean Eric Fagan 			}
215bbeaf6c0SSean Eric Fagan 			size += 64;
21605d5ca35SSean Eric Fagan 			buf = tmp;
217bbeaf6c0SSean Eric Fagan 		}
218bbeaf6c0SSean Eric Fagan 	}
21905d5ca35SSean Eric Fagan 	fclose(p);
220bbeaf6c0SSean Eric Fagan 	return buf;
221bbeaf6c0SSean Eric Fagan }
222bbeaf6c0SSean Eric Fagan 
223bbeaf6c0SSean Eric Fagan 
224bbeaf6c0SSean Eric Fagan /*
225bbeaf6c0SSean Eric Fagan  * Gag.  This is really unportable.  Multiplication is more portable.
226bbeaf6c0SSean Eric Fagan  * But slower, from the code I saw.
227bbeaf6c0SSean Eric Fagan  */
228bbeaf6c0SSean Eric Fagan 
229bbeaf6c0SSean Eric Fagan static long long
230bbeaf6c0SSean Eric Fagan make_quad(unsigned long p1, unsigned long p2) {
231bbeaf6c0SSean Eric Fagan   union {
232bbeaf6c0SSean Eric Fagan     long long ll;
233bbeaf6c0SSean Eric Fagan     unsigned long l[2];
234bbeaf6c0SSean Eric Fagan   } t;
235bbeaf6c0SSean Eric Fagan   t.l[0] = p1;
236bbeaf6c0SSean Eric Fagan   t.l[1] = p2;
237bbeaf6c0SSean Eric Fagan   return t.ll;
238bbeaf6c0SSean Eric Fagan }
239bbeaf6c0SSean Eric Fagan 
240bbeaf6c0SSean Eric Fagan 
241bbeaf6c0SSean Eric Fagan /*
242bbeaf6c0SSean Eric Fagan  * print_arg
243bbeaf6c0SSean Eric Fagan  * Converts a syscall argument into a string.  Said string is
244bbeaf6c0SSean Eric Fagan  * allocated via malloc(), so needs to be free()'d.  The file
245bbeaf6c0SSean Eric Fagan  * descriptor is for the process' memory (via /proc), and is used
246bbeaf6c0SSean Eric Fagan  * to get any data (where the argument is a pointer).  sc is
247bbeaf6c0SSean Eric Fagan  * a pointer to the syscall description (see above); args is
248bbeaf6c0SSean Eric Fagan  * an array of all of the system call arguments.
249bbeaf6c0SSean Eric Fagan  */
250bbeaf6c0SSean Eric Fagan 
251bbeaf6c0SSean Eric Fagan char *
252bbeaf6c0SSean Eric Fagan print_arg(int fd, struct syscall_args *sc, unsigned long *args) {
2533cf51049SPhilippe Charnier   char *tmp = NULL;
254bbeaf6c0SSean Eric Fagan   switch (sc->type & ARG_MASK) {
255bbeaf6c0SSean Eric Fagan   case Hex:
256bbeaf6c0SSean Eric Fagan     tmp = malloc(12);
25722694ebaSBruce Evans     sprintf(tmp, "0x%lx", args[sc->offset]);
258bbeaf6c0SSean Eric Fagan     break;
259bbeaf6c0SSean Eric Fagan   case Octal:
260bbeaf6c0SSean Eric Fagan     tmp = malloc(13);
26122694ebaSBruce Evans     sprintf(tmp, "0%lo", args[sc->offset]);
262bbeaf6c0SSean Eric Fagan     break;
263bbeaf6c0SSean Eric Fagan   case Int:
264bbeaf6c0SSean Eric Fagan     tmp = malloc(12);
26522694ebaSBruce Evans     sprintf(tmp, "%ld", args[sc->offset]);
266bbeaf6c0SSean Eric Fagan     break;
267bbeaf6c0SSean Eric Fagan   case String:
268bbeaf6c0SSean Eric Fagan     {
269bbeaf6c0SSean Eric Fagan       char *tmp2;
270bbeaf6c0SSean Eric Fagan       tmp2 = get_string(fd, (void*)args[sc->offset], 0);
271bbeaf6c0SSean Eric Fagan       tmp = malloc(strlen(tmp2) + 3);
272bbeaf6c0SSean Eric Fagan       sprintf(tmp, "\"%s\"", tmp2);
273bbeaf6c0SSean Eric Fagan       free(tmp2);
274bbeaf6c0SSean Eric Fagan     }
275bbeaf6c0SSean Eric Fagan   break;
2769897b203SMatthew N. Dodd   case StringArray:
2779897b203SMatthew N. Dodd     {
2789897b203SMatthew N. Dodd       int num, size, i;
2799897b203SMatthew N. Dodd       char *tmp2;
2809897b203SMatthew N. Dodd       char *string;
2819897b203SMatthew N. Dodd       char *strarray[100];	/* XXX This is ugly. */
2829897b203SMatthew N. Dodd 
2839897b203SMatthew N. Dodd       if (get_struct(fd, (void *)args[sc->offset], (void *)&strarray,
2849897b203SMatthew N. Dodd                      sizeof(strarray)) == -1) {
2859897b203SMatthew N. Dodd 	err(1, "get_struct %p", (void *)args[sc->offset]);
2869897b203SMatthew N. Dodd       }
2879897b203SMatthew N. Dodd       num = 0;
2889897b203SMatthew N. Dodd       size = 0;
2899897b203SMatthew N. Dodd 
2909897b203SMatthew N. Dodd       /* Find out how large of a buffer we'll need. */
2919897b203SMatthew N. Dodd       while (strarray[num] != NULL) {
2929897b203SMatthew N. Dodd 	string = get_string(fd, (void*)strarray[num], 0);
2939897b203SMatthew N. Dodd         size += strlen(string);
2949897b203SMatthew N. Dodd 	free(string);
2959897b203SMatthew N. Dodd 	num++;
2969897b203SMatthew N. Dodd       }
2979897b203SMatthew N. Dodd       size += 4 + (num * 4);
2989897b203SMatthew N. Dodd       tmp = (char *)malloc(size);
2999897b203SMatthew N. Dodd       tmp2 = tmp;
3009897b203SMatthew N. Dodd 
3019897b203SMatthew N. Dodd       tmp2 += sprintf(tmp2, " [");
3029897b203SMatthew N. Dodd       for (i = 0; i < num; i++) {
3039897b203SMatthew N. Dodd 	string = get_string(fd, (void*)strarray[i], 0);
3049897b203SMatthew N. Dodd         tmp2 += sprintf(tmp2, " \"%s\"%c", string, (i+1 == num) ? ' ' : ',');
3059897b203SMatthew N. Dodd 	free(string);
3069897b203SMatthew N. Dodd       }
3079897b203SMatthew N. Dodd       tmp2 += sprintf(tmp2, "]");
3089897b203SMatthew N. Dodd     }
3099897b203SMatthew N. Dodd   break;
310bbeaf6c0SSean Eric Fagan   case Quad:
311bbeaf6c0SSean Eric Fagan     {
312bbeaf6c0SSean Eric Fagan       unsigned long long t;
313bbeaf6c0SSean Eric Fagan       unsigned long l1, l2;
314bbeaf6c0SSean Eric Fagan       l1 = args[sc->offset];
315bbeaf6c0SSean Eric Fagan       l2 = args[sc->offset+1];
316bbeaf6c0SSean Eric Fagan       t = make_quad(l1, l2);
317bbeaf6c0SSean Eric Fagan       tmp = malloc(24);
318bbeaf6c0SSean Eric Fagan       sprintf(tmp, "0x%qx", t);
319bbeaf6c0SSean Eric Fagan       break;
320bbeaf6c0SSean Eric Fagan     }
321bbeaf6c0SSean Eric Fagan   case Ptr:
322bbeaf6c0SSean Eric Fagan     tmp = malloc(12);
32322694ebaSBruce Evans     sprintf(tmp, "0x%lx", args[sc->offset]);
324bbeaf6c0SSean Eric Fagan     break;
325970649f9SSean Eric Fagan   case Ioctl:
326970649f9SSean Eric Fagan     {
3271be5d704SMark Murray       const char *temp = ioctlname(args[sc->offset]);
328970649f9SSean Eric Fagan       if (temp)
329970649f9SSean Eric Fagan 	tmp = strdup(temp);
330970649f9SSean Eric Fagan       else {
331970649f9SSean Eric Fagan 	tmp = malloc(12);
33222694ebaSBruce Evans 	sprintf(tmp, "0x%lx", args[sc->offset]);
333970649f9SSean Eric Fagan       }
334970649f9SSean Eric Fagan     }
335f0ebbc29SDag-Erling Smørgrav     break;
336f0ebbc29SDag-Erling Smørgrav   case Signal:
337f0ebbc29SDag-Erling Smørgrav     {
338f0ebbc29SDag-Erling Smørgrav       long sig;
339f0ebbc29SDag-Erling Smørgrav 
340f0ebbc29SDag-Erling Smørgrav       sig = args[sc->offset];
341f0ebbc29SDag-Erling Smørgrav       tmp = malloc(12);
342f0ebbc29SDag-Erling Smørgrav       if (sig > 0 && sig < NSIG) {
343f0ebbc29SDag-Erling Smørgrav 	int i;
344f0ebbc29SDag-Erling Smørgrav 	sprintf(tmp, "sig%s", sys_signame[sig]);
345f0ebbc29SDag-Erling Smørgrav 	for (i = 0; tmp[i] != '\0'; ++i)
346f0ebbc29SDag-Erling Smørgrav 	  tmp[i] = toupper(tmp[i]);
347f0ebbc29SDag-Erling Smørgrav       } else {
348f0ebbc29SDag-Erling Smørgrav         sprintf(tmp, "%ld", sig);
349f0ebbc29SDag-Erling Smørgrav       }
350f0ebbc29SDag-Erling Smørgrav     }
351f0ebbc29SDag-Erling Smørgrav     break;
3529ddd1412SDag-Erling Smørgrav   case Sockaddr:
3539ddd1412SDag-Erling Smørgrav     {
354dec17687SBrian Feldman       struct sockaddr_storage ss;
3559ddd1412SDag-Erling Smørgrav       char addr[64];
3561be5d704SMark Murray       struct sockaddr_in *lsin;
3571be5d704SMark Murray       struct sockaddr_in6 *lsin6;
358dec17687SBrian Feldman       struct sockaddr_un *sun;
359dec17687SBrian Feldman       struct sockaddr *sa;
360dec17687SBrian Feldman       char *p;
361dec17687SBrian Feldman       u_char *q;
362dec17687SBrian Feldman       int i;
3639ddd1412SDag-Erling Smørgrav 
364dec17687SBrian Feldman       /* yuck: get ss_len */
365dec17687SBrian Feldman       if (get_struct(fd, (void *)args[sc->offset], (void *)&ss,
366dec17687SBrian Feldman 	sizeof(ss.ss_len) + sizeof(ss.ss_family)) == -1)
367dec17687SBrian Feldman 	err(1, "get_struct %p", (void *)args[sc->offset]);
368dec17687SBrian Feldman       /* sockaddr_un never have the length filled in! */
369dec17687SBrian Feldman       if (ss.ss_family == AF_UNIX) {
370dec17687SBrian Feldman 	if (get_struct(fd, (void *)args[sc->offset], (void *)&ss,
371dec17687SBrian Feldman 	  sizeof(*sun))
372dec17687SBrian Feldman 	  == -1)
373dec17687SBrian Feldman 	  err(2, "get_struct %p", (void *)args[sc->offset]);
3749ddd1412SDag-Erling Smørgrav       } else {
375dec17687SBrian Feldman 	if (get_struct(fd, (void *)args[sc->offset], (void *)&ss, ss.ss_len)
376dec17687SBrian Feldman 	  == -1)
377dec17687SBrian Feldman 	  err(2, "get_struct %p", (void *)args[sc->offset]);
3789ddd1412SDag-Erling Smørgrav       }
379dec17687SBrian Feldman 
380dec17687SBrian Feldman       switch (ss.ss_family) {
381dec17687SBrian Feldman       case AF_INET:
3821be5d704SMark Murray 	lsin = (struct sockaddr_in *)&ss;
3831be5d704SMark Murray 	inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof addr);
3841be5d704SMark Murray 	asprintf(&tmp, "{ AF_INET %s:%d }", addr, htons(lsin->sin_port));
385dec17687SBrian Feldman 	break;
386dec17687SBrian Feldman       case AF_INET6:
3871be5d704SMark Murray 	lsin6 = (struct sockaddr_in6 *)&ss;
3881be5d704SMark Murray 	inet_ntop(AF_INET6, &lsin6->sin6_addr, addr, sizeof addr);
3891be5d704SMark Murray 	asprintf(&tmp, "{ AF_INET6 [%s]:%d }", addr, htons(lsin6->sin6_port));
390dec17687SBrian Feldman 	break;
391dec17687SBrian Feldman       case AF_UNIX:
392dec17687SBrian Feldman         sun = (struct sockaddr_un *)&ss;
393dec17687SBrian Feldman         asprintf(&tmp, "{ AF_UNIX \"%s\" }", sun->sun_path);
394dec17687SBrian Feldman 	break;
395dec17687SBrian Feldman       default:
396dec17687SBrian Feldman 	sa = (struct sockaddr *)&ss;
397dec17687SBrian Feldman         asprintf(&tmp, "{ sa_len = %d, sa_family = %d, sa_data = {%n%*s } }",
398dec17687SBrian Feldman 	  (int)sa->sa_len, (int)sa->sa_family, &i,
399dec17687SBrian Feldman 	  6 * (int)(sa->sa_len - ((char *)&sa->sa_data - (char *)sa)), "");
400dec17687SBrian Feldman 	if (tmp != NULL) {
401dec17687SBrian Feldman 	  p = tmp + i;
402dec17687SBrian Feldman           for (q = (u_char *)&sa->sa_data; q < (u_char *)sa + sa->sa_len; q++)
403dec17687SBrian Feldman             p += sprintf(p, " %#02x,", *q);
404dec17687SBrian Feldman 	}
405dec17687SBrian Feldman       }
4069ddd1412SDag-Erling Smørgrav     }
4079ddd1412SDag-Erling Smørgrav     break;
408bbeaf6c0SSean Eric Fagan   }
409bbeaf6c0SSean Eric Fagan   return tmp;
410bbeaf6c0SSean Eric Fagan }
411bbeaf6c0SSean Eric Fagan 
412203098d8SMatthew N. Dodd #define timespecsubt(tvp, uvp, vvp)					\
413203098d8SMatthew N. Dodd 	do {								\
414203098d8SMatthew N. Dodd 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
415203098d8SMatthew N. Dodd 		(vvp)->tv_nsec = (tvp)->tv_nsec - (uvp)->tv_nsec;	\
416203098d8SMatthew N. Dodd 		if ((vvp)->tv_nsec < 0) {				\
417203098d8SMatthew N. Dodd 			(vvp)->tv_sec--;				\
418203098d8SMatthew N. Dodd 			(vvp)->tv_nsec += 1000000000;			\
419203098d8SMatthew N. Dodd 		}							\
420203098d8SMatthew N. Dodd 	} while (0)
421203098d8SMatthew N. Dodd 
422bbeaf6c0SSean Eric Fagan /*
423bbeaf6c0SSean Eric Fagan  * print_syscall
424bbeaf6c0SSean Eric Fagan  * Print (to outfile) the system call and its arguments.  Note that
425bbeaf6c0SSean Eric Fagan  * nargs is the number of arguments (not the number of words; this is
426bbeaf6c0SSean Eric Fagan  * potentially confusing, I know).
427bbeaf6c0SSean Eric Fagan  */
428bbeaf6c0SSean Eric Fagan 
429bbeaf6c0SSean Eric Fagan void
430ec0bed25SMatthew N. Dodd print_syscall(struct trussinfo *trussinfo, const char *name, int nargs, char **s_args) {
431bbeaf6c0SSean Eric Fagan   int i;
4326cb533feSSean Eric Fagan   int len = 0;
433203098d8SMatthew N. Dodd   struct timespec timediff;
4340d0bd00eSMatthew N. Dodd 
435c03bfcc8SMatthew N. Dodd   if (trussinfo->flags & FOLLOWFORKS)
436c03bfcc8SMatthew N. Dodd     len += fprintf(trussinfo->outfile, "%5d: ", trussinfo->pid);
437c03bfcc8SMatthew N. Dodd 
438967706f3SDavid Malone   if (name != NULL && (!strcmp(name, "execve") || !strcmp(name, "exit"))) {
439203098d8SMatthew N. Dodd     clock_gettime(CLOCK_REALTIME, &trussinfo->after);
4400d0bd00eSMatthew N. Dodd   }
4410d0bd00eSMatthew N. Dodd 
4420d0bd00eSMatthew N. Dodd   if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
443203098d8SMatthew N. Dodd     timespecsubt(&trussinfo->after, &trussinfo->start_time, &timediff);
44437169f94SMatthew N. Dodd     len += fprintf(trussinfo->outfile, "%ld.%09ld ",
445fb034d05SMatthew N. Dodd 		   (long)timediff.tv_sec, timediff.tv_nsec);
4460d0bd00eSMatthew N. Dodd   }
4470d0bd00eSMatthew N. Dodd 
4480d0bd00eSMatthew N. Dodd   if (trussinfo->flags & RELATIVETIMESTAMPS) {
449203098d8SMatthew N. Dodd     timespecsubt(&trussinfo->after, &trussinfo->before, &timediff);
45037169f94SMatthew N. Dodd     len += fprintf(trussinfo->outfile, "%ld.%09ld ",
451fb034d05SMatthew N. Dodd 		   (long)timediff.tv_sec, timediff.tv_nsec);
4520d0bd00eSMatthew N. Dodd   }
4530d0bd00eSMatthew N. Dodd 
454ec0bed25SMatthew N. Dodd   len += fprintf(trussinfo->outfile, "%s(", name);
455c03bfcc8SMatthew N. Dodd 
456bbeaf6c0SSean Eric Fagan   for (i = 0; i < nargs; i++) {
457bbeaf6c0SSean Eric Fagan     if (s_args[i])
458ec0bed25SMatthew N. Dodd       len += fprintf(trussinfo->outfile, "%s", s_args[i]);
459bbeaf6c0SSean Eric Fagan     else
460ec0bed25SMatthew N. Dodd       len += fprintf(trussinfo->outfile, "<missing argument>");
461ec0bed25SMatthew N. Dodd     len += fprintf(trussinfo->outfile, "%s", i < (nargs - 1) ? "," : "");
462bbeaf6c0SSean Eric Fagan   }
463ec0bed25SMatthew N. Dodd   len += fprintf(trussinfo->outfile, ")");
4646cb533feSSean Eric Fagan   for (i = 0; i < 6 - (len / 8); i++)
465ec0bed25SMatthew N. Dodd 	fprintf(trussinfo->outfile, "\t");
4666cb533feSSean Eric Fagan }
4676cb533feSSean Eric Fagan 
4686cb533feSSean Eric Fagan void
469ec0bed25SMatthew N. Dodd print_syscall_ret(struct trussinfo *trussinfo, const char *name, int nargs, char **s_args, int errorp, int retval) {
470ec0bed25SMatthew N. Dodd   print_syscall(trussinfo, name, nargs, s_args);
4716cb533feSSean Eric Fagan   if (errorp) {
472ec0bed25SMatthew N. Dodd     fprintf(trussinfo->outfile, " ERR#%d '%s'\n", retval, strerror(retval));
4736cb533feSSean Eric Fagan   } else {
474ec0bed25SMatthew N. Dodd     fprintf(trussinfo->outfile, " = %d (0x%x)\n", retval, retval);
4756cb533feSSean Eric Fagan   }
476bbeaf6c0SSean Eric Fagan }
477