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
31 * $FreeBSD: src/usr.bin/truss/syscalls.c,v 1.10.2.6 2003/04/14 18:24:38 mdodd Exp $
32 * $DragonFly: src/usr.bin/truss/syscalls.c,v 1.2 2003/06/17 04:29:33 dillon Exp $
36 * This file has routines used to print out system calls and their
40 #include <sys/types.h>
41 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
59 * This should probably be in its own file.
62 struct syscall syscalls[] = {
64 { { String, 0 } , { String | OUT, 1 }, { Int, 2 }}},
66 { { Int, 0 }, {Quad, 2 }, { Int, 4 }}},
68 { { Hex, 0 }, {Int, 1}, {Hex, 2}, {Hex, 3}, {Int, 4}, {Quad, 6}}},
70 { { String | IN, 0} , { Hex, 1}, {Octal, 2}}},
72 { { String, 0 }, { Hex, 1}, { Octal, 2 }}},
73 { "close", 1, 1, { { Int, 0 } } },
75 { { Int, 0}, {Ptr | OUT , 1 }}},
77 { { String | IN, 0 }, { Ptr | OUT, 1 }}},
79 { { String | IN, 0 }, { Ptr | OUT, 1 }}},
80 { "linux_newstat", 1, 2,
81 { { String | IN, 0 }, { Ptr | OUT, 1 }}},
82 { "linux_newfstat", 1, 2,
83 { { Int, 0 }, { Ptr | OUT, 1 }}},
85 { { Int, 0}, { Ptr | IN, 1 }, { Int, 2 }}},
87 { { Int, 0}, { Ioctl, 1 }, { Hex, 2 }}},
88 { "break", 1, 1, { { Hex, 0 }}},
89 { "exit", 0, 1, { { Hex, 0 }}},
90 { "access", 1, 2, { { String | IN, 0 }, { Int, 1 }}},
92 { { Signal, 0 }, { Ptr | IN, 1 }, { Ptr | OUT, 2 }}},
94 { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
96 { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
98 { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
99 { "getpeername", 1, 3,
100 { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
101 { "getsockname", 1, 3,
102 { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
103 { 0, 0, 0, { { 0, 0 }}},
107 * If/when the list gets big, it might be desirable to do it
108 * as a hash table or binary search.
112 get_syscall(const char *name) {
113 struct syscall *sc = syscalls;
116 if (!strcmp(name, sc->name))
126 * Copy a fixed amount of bytes from the process.
130 get_struct(int procfd, void *offset, void *buf, int len) {
135 if ((fd = dup(procfd)) == -1)
137 if ((p = fdopen(fd, "r")) == NULL)
139 fseeko(p, (uintptr_t)offset, SEEK_SET);
140 for (pos = (char *)buf; len--; pos++) {
141 if ((c = fgetc(p)) == EOF)
151 * Copy a string from the process. Note that it is
152 * expected to be a C string, but if max is set, it will
153 * only get that much.
157 get_string(int procfd, void *offset, int max) {
159 int size, len, c, fd;
162 if ((fd = dup(procfd)) == -1)
164 if ((p = fdopen(fd, "r")) == NULL)
166 buf = malloc( size = (max ? max : 64 ) );
169 fseeko(p, (uintptr_t)offset, SEEK_SET);
170 while ((c = fgetc(p)) != EOF) {
172 if (c == 0 || len == max) {
178 tmp = realloc(buf, size+64);
194 * Gag. This is really unportable. Multiplication is more portable.
195 * But slower, from the code I saw.
199 make_quad(unsigned long p1, unsigned long p2) {
212 * Converts a syscall argument into a string. Said string is
213 * allocated via malloc(), so needs to be free()'d. The file
214 * descriptor is for the process' memory (via /proc), and is used
215 * to get any data (where the argument is a pointer). sc is
216 * a pointer to the syscall description (see above); args is
217 * an array of all of the system call arguments.
221 print_arg(int fd, struct syscall_args *sc, unsigned long *args) {
223 switch (sc->type & ARG_MASK) {
226 sprintf(tmp, "0x%lx", args[sc->offset]);
230 sprintf(tmp, "0%lo", args[sc->offset]);
234 sprintf(tmp, "%ld", args[sc->offset]);
239 tmp2 = get_string(fd, (void*)args[sc->offset], 0);
240 tmp = malloc(strlen(tmp2) + 3);
241 sprintf(tmp, "\"%s\"", tmp2);
247 unsigned long long t;
248 unsigned long l1, l2;
249 l1 = args[sc->offset];
250 l2 = args[sc->offset+1];
251 t = make_quad(l1, l2);
253 sprintf(tmp, "0x%qx", t);
258 sprintf(tmp, "0x%lx", args[sc->offset]);
262 const char *temp = ioctlname(args[sc->offset]);
267 sprintf(tmp, "0x%lx", args[sc->offset]);
275 sig = args[sc->offset];
277 if (sig > 0 && sig < NSIG) {
279 sprintf(tmp, "sig%s", sys_signame[sig]);
280 for (i = 0; tmp[i] != '\0'; ++i)
281 tmp[i] = toupper(tmp[i]);
283 sprintf(tmp, "%ld", sig);
289 struct sockaddr_storage ss;
291 struct sockaddr_in *lsin;
292 struct sockaddr_in6 *lsin6;
293 struct sockaddr_un *sun;
299 /* yuck: get ss_len */
300 if (get_struct(fd, (void *)args[sc->offset], (void *)&ss,
301 sizeof(ss.ss_len) + sizeof(ss.ss_family)) == -1)
302 err(1, "get_struct %p", (void *)args[sc->offset]);
303 /* sockaddr_un never have the length filled in! */
304 if (ss.ss_family == AF_UNIX) {
305 if (get_struct(fd, (void *)args[sc->offset], (void *)&ss,
308 err(2, "get_struct %p", (void *)args[sc->offset]);
310 if (get_struct(fd, (void *)args[sc->offset], (void *)&ss,
311 ss.ss_len < sizeof(ss) ? ss.ss_len : sizeof(ss))
313 err(2, "get_struct %p", (void *)args[sc->offset]);
316 switch (ss.ss_family) {
318 lsin = (struct sockaddr_in *)&ss;
319 inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof addr);
320 asprintf(&tmp, "{ AF_INET %s:%d }", addr, htons(lsin->sin_port));
323 lsin6 = (struct sockaddr_in6 *)&ss;
324 inet_ntop(AF_INET6, &lsin6->sin6_addr, addr, sizeof addr);
325 asprintf(&tmp, "{ AF_INET6 [%s]:%d }", addr, htons(lsin6->sin6_port));
328 sun = (struct sockaddr_un *)&ss;
329 asprintf(&tmp, "{ AF_UNIX \"%s\" }", sun->sun_path);
332 sa = (struct sockaddr *)&ss;
333 asprintf(&tmp, "{ sa_len = %d, sa_family = %d, sa_data = {%n%*s } }",
334 (int)sa->sa_len, (int)sa->sa_family, &i,
335 6 * (int)(sa->sa_len - ((char *)&sa->sa_data - (char *)sa)), "");
338 for (q = (u_char *)&sa->sa_data; q < (u_char *)sa + sa->sa_len; q++)
339 p += sprintf(p, " %#02x,", *q);
350 * Print (to trussinfo->outfile) the system call and its arguments. Note that
351 * nargs is the number of arguments (not the number of words; this is
352 * potentially confusing, I know).
356 print_syscall(struct trussinfo *trussinfo, const char *name, int nargs, char **s_args) {
359 len += fprintf(trussinfo->outfile, "%s(", name);
360 for (i = 0; i < nargs; i++) {
362 len += fprintf(trussinfo->outfile, "%s", s_args[i]);
364 len += fprintf(trussinfo->outfile, "<missing argument>");
365 len += fprintf(trussinfo->outfile, "%s", i < (nargs - 1) ? "," : "");
367 len += fprintf(trussinfo->outfile, ")");
368 for (i = 0; i < 6 - (len / 8); i++)
369 fprintf(trussinfo->outfile, "\t");
373 print_syscall_ret(struct trussinfo *trussinfo, const char *name, int nargs, char **s_args, int errorp, int retval) {
374 print_syscall(trussinfo, name, nargs, s_args);
376 fprintf(trussinfo->outfile, " ERR#%d '%s'\n", retval, strerror(retval));
378 fprintf(trussinfo->outfile, " = %d (0x%x)\n", retval, retval);