xref: /dragonfly/usr.bin/truss/syscalls.c (revision 984263bc)
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: src/usr.bin/truss/syscalls.c,v 1.10.2.6 2003/04/14 18:24:38 mdodd Exp $";
35 #endif /* not lint */
36 
37 /*
38  * This file has routines used to print out system calls and their
39  * arguments.
40  */
41 
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/un.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 
48 #include <ctype.h>
49 #include <err.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 #include "extern.h"
57 #include "syscall.h"
58 
59 /*
60  * This should probably be in its own file.
61  */
62 
63 struct syscall syscalls[] = {
64 	{ "readlink", 1, 3,
65 	  { { String, 0 } , { String | OUT, 1 }, { Int, 2 }}},
66 	{ "lseek", 2, 3,
67 	  { { Int, 0 }, {Quad, 2 }, { Int, 4 }}},
68 	{ "mmap", 2, 6,
69 	  { { Hex, 0 }, {Int, 1}, {Hex, 2}, {Hex, 3}, {Int, 4}, {Quad, 6}}},
70 	{ "open", 1, 3,
71 	  { { String | IN, 0} , { Hex, 1}, {Octal, 2}}},
72 	{ "linux_open", 1, 3,
73 	  { { String, 0 }, { Hex, 1}, { Octal, 2 }}},
74 	{ "close", 1, 1, { { Int, 0 } } },
75 	{ "fstat", 1, 2,
76 	  { { Int, 0},  {Ptr | OUT , 1 }}},
77 	{ "stat", 1, 2,
78 	  { { String | IN, 0 }, { Ptr | OUT, 1 }}},
79 	{ "lstat", 1, 2,
80 	  { { String | IN, 0 }, { Ptr | OUT, 1 }}},
81 	{ "linux_newstat", 1, 2,
82 	  { { String | IN, 0 }, { Ptr | OUT, 1 }}},
83 	{ "linux_newfstat", 1, 2,
84 	  { { Int, 0 }, { Ptr | OUT, 1 }}},
85 	{ "write", 1, 3,
86 	  { { Int, 0}, { Ptr | IN, 1 }, { Int, 2 }}},
87 	{ "ioctl", 1, 3,
88 	  { { Int, 0}, { Ioctl, 1 }, { Hex, 2 }}},
89 	{ "break", 1, 1, { { Hex, 0 }}},
90 	{ "exit", 0, 1, { { Hex, 0 }}},
91 	{ "access", 1, 2, { { String | IN, 0 }, { Int, 1 }}},
92 	{ "sigaction", 1, 3,
93 	  { { Signal, 0 }, { Ptr | IN, 1 }, { Ptr | OUT, 2 }}},
94 	{ "accept", 1, 3,
95 	  { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
96 	{ "bind", 1, 3,
97 	  { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
98 	{ "connect", 1, 3,
99 	  { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
100 	{ "getpeername", 1, 3,
101 	  { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
102 	{ "getsockname", 1, 3,
103 	  { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
104 	{ 0, 0, 0, { { 0, 0 }}},
105 };
106 
107 /*
108  * If/when the list gets big, it might be desirable to do it
109  * as a hash table or binary search.
110  */
111 
112 struct syscall *
113 get_syscall(const char *name) {
114 	struct syscall *sc = syscalls;
115 
116 	while (sc->name) {
117 		if (!strcmp(name, sc->name))
118 			return sc;
119 		sc++;
120 	}
121 	return NULL;
122 }
123 
124 /*
125  * get_struct
126  *
127  * Copy a fixed amount of bytes from the process.
128  */
129 
130 static int
131 get_struct(int procfd, void *offset, void *buf, int len) {
132 	char *pos;
133 	FILE *p;
134 	int c, fd;
135 
136 	if ((fd = dup(procfd)) == -1)
137 		err(1, "dup");
138 	if ((p = fdopen(fd, "r")) == NULL)
139 		err(1, "fdopen");
140 	fseeko(p, (uintptr_t)offset, SEEK_SET);
141 	for (pos = (char *)buf; len--; pos++) {
142 		if ((c = fgetc(p)) == EOF)
143 			return -1;
144 		*pos = c;
145 	}
146 	fclose(p);
147 	return 0;
148 }
149 
150 /*
151  * get_string
152  * Copy a string from the process.  Note that it is
153  * expected to be a C string, but if max is set, it will
154  * only get that much.
155  */
156 
157 char *
158 get_string(int procfd, void *offset, int max) {
159 	char *buf;
160 	int size, len, c, fd;
161 	FILE *p;
162 
163 	if ((fd = dup(procfd)) == -1)
164 		err(1, "dup");
165 	if ((p = fdopen(fd, "r")) == NULL)
166 		err(1, "fdopen");
167 	buf = malloc( size = (max ? max : 64 ) );
168 	len = 0;
169 	buf[0] = 0;
170 	fseeko(p, (uintptr_t)offset, SEEK_SET);
171 	while ((c = fgetc(p)) != EOF) {
172 		buf[len++] = c;
173 		if (c == 0 || len == max) {
174 			buf[len] = 0;
175 			break;
176 		}
177 		if (len == size) {
178 			char *tmp;
179 			tmp = realloc(buf, size+64);
180 			if (tmp == NULL) {
181 				buf[len] = 0;
182 				fclose(p);
183 				return buf;
184 			}
185 			size += 64;
186 			buf = tmp;
187 		}
188 	}
189 	fclose(p);
190 	return buf;
191 }
192 
193 
194 /*
195  * Gag.  This is really unportable.  Multiplication is more portable.
196  * But slower, from the code I saw.
197  */
198 
199 static long long
200 make_quad(unsigned long p1, unsigned long p2) {
201   union {
202     long long ll;
203     unsigned long l[2];
204   } t;
205   t.l[0] = p1;
206   t.l[1] = p2;
207   return t.ll;
208 }
209 
210 
211 /*
212  * print_arg
213  * Converts a syscall argument into a string.  Said string is
214  * allocated via malloc(), so needs to be free()'d.  The file
215  * descriptor is for the process' memory (via /proc), and is used
216  * to get any data (where the argument is a pointer).  sc is
217  * a pointer to the syscall description (see above); args is
218  * an array of all of the system call arguments.
219  */
220 
221 char *
222 print_arg(int fd, struct syscall_args *sc, unsigned long *args) {
223   char *tmp = NULL;
224   switch (sc->type & ARG_MASK) {
225   case Hex:
226     tmp = malloc(12);
227     sprintf(tmp, "0x%lx", args[sc->offset]);
228     break;
229   case Octal:
230     tmp = malloc(13);
231     sprintf(tmp, "0%lo", args[sc->offset]);
232     break;
233   case Int:
234     tmp = malloc(12);
235     sprintf(tmp, "%ld", args[sc->offset]);
236     break;
237   case String:
238     {
239       char *tmp2;
240       tmp2 = get_string(fd, (void*)args[sc->offset], 0);
241       tmp = malloc(strlen(tmp2) + 3);
242       sprintf(tmp, "\"%s\"", tmp2);
243       free(tmp2);
244     }
245   break;
246   case Quad:
247     {
248       unsigned long long t;
249       unsigned long l1, l2;
250       l1 = args[sc->offset];
251       l2 = args[sc->offset+1];
252       t = make_quad(l1, l2);
253       tmp = malloc(24);
254       sprintf(tmp, "0x%qx", t);
255       break;
256     }
257   case Ptr:
258     tmp = malloc(12);
259     sprintf(tmp, "0x%lx", args[sc->offset]);
260     break;
261   case Ioctl:
262     {
263       const char *temp = ioctlname(args[sc->offset]);
264       if (temp)
265 	tmp = strdup(temp);
266       else {
267 	tmp = malloc(12);
268 	sprintf(tmp, "0x%lx", args[sc->offset]);
269       }
270     }
271     break;
272   case Signal:
273     {
274       long sig;
275 
276       sig = args[sc->offset];
277       tmp = malloc(12);
278       if (sig > 0 && sig < NSIG) {
279 	int i;
280 	sprintf(tmp, "sig%s", sys_signame[sig]);
281 	for (i = 0; tmp[i] != '\0'; ++i)
282 	  tmp[i] = toupper(tmp[i]);
283       } else {
284         sprintf(tmp, "%ld", sig);
285       }
286     }
287     break;
288   case Sockaddr:
289     {
290       struct sockaddr_storage ss;
291       char addr[64];
292       struct sockaddr_in *lsin;
293       struct sockaddr_in6 *lsin6;
294       struct sockaddr_un *sun;
295       struct sockaddr *sa;
296       char *p;
297       u_char *q;
298       int i;
299 
300       /* yuck: get ss_len */
301       if (get_struct(fd, (void *)args[sc->offset], (void *)&ss,
302 	sizeof(ss.ss_len) + sizeof(ss.ss_family)) == -1)
303 	err(1, "get_struct %p", (void *)args[sc->offset]);
304       /* sockaddr_un never have the length filled in! */
305       if (ss.ss_family == AF_UNIX) {
306 	if (get_struct(fd, (void *)args[sc->offset], (void *)&ss,
307 	  sizeof(*sun))
308 	  == -1)
309 	  err(2, "get_struct %p", (void *)args[sc->offset]);
310       } else {
311 	if (get_struct(fd, (void *)args[sc->offset], (void *)&ss,
312 	    ss.ss_len < sizeof(ss) ? ss.ss_len : sizeof(ss))
313 	  == -1)
314 	  err(2, "get_struct %p", (void *)args[sc->offset]);
315       }
316 
317       switch (ss.ss_family) {
318       case AF_INET:
319 	lsin = (struct sockaddr_in *)&ss;
320 	inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof addr);
321 	asprintf(&tmp, "{ AF_INET %s:%d }", addr, htons(lsin->sin_port));
322 	break;
323       case AF_INET6:
324 	lsin6 = (struct sockaddr_in6 *)&ss;
325 	inet_ntop(AF_INET6, &lsin6->sin6_addr, addr, sizeof addr);
326 	asprintf(&tmp, "{ AF_INET6 [%s]:%d }", addr, htons(lsin6->sin6_port));
327 	break;
328       case AF_UNIX:
329         sun = (struct sockaddr_un *)&ss;
330         asprintf(&tmp, "{ AF_UNIX \"%s\" }", sun->sun_path);
331 	break;
332       default:
333 	sa = (struct sockaddr *)&ss;
334         asprintf(&tmp, "{ sa_len = %d, sa_family = %d, sa_data = {%n%*s } }",
335 	  (int)sa->sa_len, (int)sa->sa_family, &i,
336 	  6 * (int)(sa->sa_len - ((char *)&sa->sa_data - (char *)sa)), "");
337 	if (tmp != NULL) {
338 	  p = tmp + i;
339           for (q = (u_char *)&sa->sa_data; q < (u_char *)sa + sa->sa_len; q++)
340             p += sprintf(p, " %#02x,", *q);
341 	}
342       }
343     }
344     break;
345   }
346   return tmp;
347 }
348 
349 /*
350  * print_syscall
351  * Print (to outfile) the system call and its arguments.  Note that
352  * nargs is the number of arguments (not the number of words; this is
353  * potentially confusing, I know).
354  */
355 
356 void
357 print_syscall(FILE *outfile, const char *name, int nargs, char **s_args) {
358   int i;
359   int len = 0;
360   len += fprintf(outfile, "%s(", name);
361   for (i = 0; i < nargs; i++) {
362     if (s_args[i])
363       len += fprintf(outfile, "%s", s_args[i]);
364     else
365       len += fprintf(outfile, "<missing argument>");
366     len += fprintf(outfile, "%s", i < (nargs - 1) ? "," : "");
367   }
368   len += fprintf(outfile, ")");
369   for (i = 0; i < 6 - (len / 8); i++)
370 	fprintf(outfile, "\t");
371 }
372 
373 void
374 print_syscall_ret(FILE *outfile, const char *name, int nargs, char **s_args, int errorp, int retval) {
375   print_syscall(outfile, name, nargs, s_args);
376   if (errorp) {
377     fprintf(outfile, " ERR#%d '%s'\n", retval, strerror(retval));
378   } else {
379     fprintf(outfile, " = %d (0x%x)\n", retval, retval);
380   }
381 }
382