2 * Copryight 1997 Sean Eric Fagan
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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
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 $";
38 * This file has routines used to print out system calls and their
42 #include <sys/types.h>
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
60 * This should probably be in its own file.
63 struct syscall syscalls[] = {
65 { { String, 0 } , { String | OUT, 1 }, { Int, 2 }}},
67 { { Int, 0 }, {Quad, 2 }, { Int, 4 }}},
69 { { Hex, 0 }, {Int, 1}, {Hex, 2}, {Hex, 3}, {Int, 4}, {Quad, 6}}},
71 { { String | IN, 0} , { Hex, 1}, {Octal, 2}}},
73 { { String, 0 }, { Hex, 1}, { Octal, 2 }}},
74 { "close", 1, 1, { { Int, 0 } } },
76 { { Int, 0}, {Ptr | OUT , 1 }}},
78 { { String | IN, 0 }, { Ptr | OUT, 1 }}},
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 }}},
86 { { Int, 0}, { Ptr | IN, 1 }, { Int, 2 }}},
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 }}},
93 { { Signal, 0 }, { Ptr | IN, 1 }, { Ptr | OUT, 2 }}},
95 { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
97 { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
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 }}},
108 * If/when the list gets big, it might be desirable to do it
109 * as a hash table or binary search.
113 get_syscall(const char *name) {
114 struct syscall *sc = syscalls;
117 if (!strcmp(name, sc->name))
127 * Copy a fixed amount of bytes from the process.
131 get_struct(int procfd, void *offset, void *buf, int len) {
136 if ((fd = dup(procfd)) == -1)
138 if ((p = fdopen(fd, "r")) == NULL)
140 fseeko(p, (uintptr_t)offset, SEEK_SET);
141 for (pos = (char *)buf; len--; pos++) {
142 if ((c = fgetc(p)) == EOF)
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.
158 get_string(int procfd, void *offset, int max) {
160 int size, len, c, fd;
163 if ((fd = dup(procfd)) == -1)
165 if ((p = fdopen(fd, "r")) == NULL)
167 buf = malloc( size = (max ? max : 64 ) );
170 fseeko(p, (uintptr_t)offset, SEEK_SET);
171 while ((c = fgetc(p)) != EOF) {
173 if (c == 0 || len == max) {
179 tmp = realloc(buf, size+64);
195 * Gag. This is really unportable. Multiplication is more portable.
196 * But slower, from the code I saw.
200 make_quad(unsigned long p1, unsigned long p2) {
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.
222 print_arg(int fd, struct syscall_args *sc, unsigned long *args) {
224 switch (sc->type & ARG_MASK) {
227 sprintf(tmp, "0x%lx", args[sc->offset]);
231 sprintf(tmp, "0%lo", args[sc->offset]);
235 sprintf(tmp, "%ld", args[sc->offset]);
240 tmp2 = get_string(fd, (void*)args[sc->offset], 0);
241 tmp = malloc(strlen(tmp2) + 3);
242 sprintf(tmp, "\"%s\"", tmp2);
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);
254 sprintf(tmp, "0x%qx", t);
259 sprintf(tmp, "0x%lx", args[sc->offset]);
263 const char *temp = ioctlname(args[sc->offset]);
268 sprintf(tmp, "0x%lx", args[sc->offset]);
276 sig = args[sc->offset];
278 if (sig > 0 && sig < NSIG) {
280 sprintf(tmp, "sig%s", sys_signame[sig]);
281 for (i = 0; tmp[i] != '\0'; ++i)
282 tmp[i] = toupper(tmp[i]);
284 sprintf(tmp, "%ld", sig);
290 struct sockaddr_storage ss;
292 struct sockaddr_in *lsin;
293 struct sockaddr_in6 *lsin6;
294 struct sockaddr_un *sun;
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,
309 err(2, "get_struct %p", (void *)args[sc->offset]);
311 if (get_struct(fd, (void *)args[sc->offset], (void *)&ss,
312 ss.ss_len < sizeof(ss) ? ss.ss_len : sizeof(ss))
314 err(2, "get_struct %p", (void *)args[sc->offset]);
317 switch (ss.ss_family) {
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));
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));
329 sun = (struct sockaddr_un *)&ss;
330 asprintf(&tmp, "{ AF_UNIX \"%s\" }", sun->sun_path);
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)), "");
339 for (q = (u_char *)&sa->sa_data; q < (u_char *)sa + sa->sa_len; q++)
340 p += sprintf(p, " %#02x,", *q);
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).
357 print_syscall(FILE *outfile, const char *name, int nargs, char **s_args) {
360 len += fprintf(outfile, "%s(", name);
361 for (i = 0; i < nargs; i++) {
363 len += fprintf(outfile, "%s", s_args[i]);
365 len += fprintf(outfile, "<missing argument>");
366 len += fprintf(outfile, "%s", i < (nargs - 1) ? "," : "");
368 len += fprintf(outfile, ")");
369 for (i = 0; i < 6 - (len / 8); i++)
370 fprintf(outfile, "\t");
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);
377 fprintf(outfile, " ERR#%d '%s'\n", retval, strerror(retval));
379 fprintf(outfile, " = %d (0x%x)\n", retval, retval);