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>
58 * This should probably be in its own file.
61 struct syscall syscalls
[] = {
63 { { String
, 0 } , { String
| OUT
, 1 }, { Int
, 2 }}},
65 { { Int
, 0 }, {Quad
, 2 }, { Int
, 4 }}},
67 { { Hex
, 0 }, {Int
, 1}, {Hex
, 2}, {Hex
, 3}, {Int
, 4}, {Quad
, 6}}},
69 { { String
| IN
, 0} , { Hex
, 1}, {Octal
, 2}}},
71 { { String
, 0 }, { Hex
, 1}, { Octal
, 2 }}},
72 { "close", 1, 1, { { Int
, 0 } } },
74 { { Int
, 0}, {Ptr
| OUT
, 1 }}},
76 { { String
| IN
, 0 }, { Ptr
| OUT
, 1 }}},
78 { { String
| IN
, 0 }, { Ptr
| OUT
, 1 }}},
79 { "linux_newstat", 1, 2,
80 { { String
| IN
, 0 }, { Ptr
| OUT
, 1 }}},
81 { "linux_newfstat", 1, 2,
82 { { Int
, 0 }, { Ptr
| OUT
, 1 }}},
84 { { Int
, 0}, { Ptr
| IN
, 1 }, { Int
, 2 }}},
86 { { Int
, 0}, { Ioctl
, 1 }, { Hex
, 2 }}},
87 { "break", 1, 1, { { Hex
, 0 }}},
88 { "exit", 0, 1, { { Hex
, 0 }}},
89 { "access", 1, 2, { { String
| IN
, 0 }, { Int
, 1 }}},
91 { { Signal
, 0 }, { Ptr
| IN
, 1 }, { Ptr
| OUT
, 2 }}},
93 { { Hex
, 0 }, { Sockaddr
| OUT
, 1 }, { Ptr
| OUT
, 2 } } },
95 { { Hex
, 0 }, { Sockaddr
| IN
, 1 }, { Int
, 2 } } },
97 { { Hex
, 0 }, { Sockaddr
| IN
, 1 }, { Int
, 2 } } },
98 { "getpeername", 1, 3,
99 { { Hex
, 0 }, { Sockaddr
| OUT
, 1 }, { Ptr
| OUT
, 2 } } },
100 { "getsockname", 1, 3,
101 { { Hex
, 0 }, { Sockaddr
| OUT
, 1 }, { Ptr
| OUT
, 2 } } },
102 { 0, 0, 0, { { 0, 0 }}},
106 * If/when the list gets big, it might be desirable to do it
107 * as a hash table or binary search.
111 get_syscall(const char *name
) {
112 struct syscall
*sc
= syscalls
;
115 if (!strcmp(name
, sc
->name
))
125 * Copy a fixed amount of bytes from the process.
129 get_struct(int procfd
, void *offset
, void *buf
, int len
) {
134 if ((fd
= dup(procfd
)) == -1)
136 if ((p
= fdopen(fd
, "r")) == NULL
)
138 fseeko(p
, (uintptr_t)offset
, SEEK_SET
);
139 for (pos
= (char *)buf
; len
--; pos
++) {
140 if ((c
= fgetc(p
)) == EOF
)
150 * Copy a string from the process. Note that it is
151 * expected to be a C string, but if max is set, it will
152 * only get that much.
156 get_string(int procfd
, void *offset
, int max
) {
158 int size
, len
, c
, fd
;
161 if ((fd
= dup(procfd
)) == -1)
163 if ((p
= fdopen(fd
, "r")) == NULL
)
165 buf
= malloc( size
= (max
? max
: 64 ) );
168 fseeko(p
, (uintptr_t)offset
, SEEK_SET
);
169 while ((c
= fgetc(p
)) != EOF
) {
171 if (c
== 0 || len
== max
) {
177 tmp
= realloc(buf
, size
+64);
193 * Gag. This is really unportable. Multiplication is more portable.
194 * But slower, from the code I saw.
198 make_quad(unsigned long p1
, unsigned long p2
) {
211 * Converts a syscall argument into a string. Said string is
212 * allocated via malloc(), so needs to be free()'d. The file
213 * descriptor is for the process' memory (via /proc), and is used
214 * to get any data (where the argument is a pointer). sc is
215 * a pointer to the syscall description (see above); args is
216 * an array of all of the system call arguments.
220 print_arg(int fd
, struct syscall_args
*sc
, unsigned long *args
) {
222 switch (sc
->type
& ARG_MASK
) {
225 sprintf(tmp
, "0x%lx", args
[sc
->offset
]);
229 sprintf(tmp
, "0%lo", args
[sc
->offset
]);
233 sprintf(tmp
, "%ld", args
[sc
->offset
]);
238 tmp2
= get_string(fd
, (void*)args
[sc
->offset
], 0);
239 tmp
= malloc(strlen(tmp2
) + 3);
240 sprintf(tmp
, "\"%s\"", tmp2
);
246 unsigned long long t
;
247 unsigned long l1
, l2
;
248 l1
= args
[sc
->offset
];
249 l2
= args
[sc
->offset
+1];
250 t
= make_quad(l1
, l2
);
252 sprintf(tmp
, "0x%qx", t
);
257 sprintf(tmp
, "0x%lx", args
[sc
->offset
]);
261 const char *temp
= ioctlname(args
[sc
->offset
]);
266 sprintf(tmp
, "0x%lx", args
[sc
->offset
]);
274 sig
= args
[sc
->offset
];
276 if (sig
> 0 && sig
< NSIG
) {
278 sprintf(tmp
, "sig%s", sys_signame
[sig
]);
279 for (i
= 0; tmp
[i
] != '\0'; ++i
)
280 tmp
[i
] = toupper(tmp
[i
]);
282 sprintf(tmp
, "%ld", sig
);
288 struct sockaddr_storage ss
;
290 struct sockaddr_in
*lsin
;
291 struct sockaddr_in6
*lsin6
;
292 struct sockaddr_un
*sun
;
298 /* yuck: get ss_len */
299 if (get_struct(fd
, (void *)args
[sc
->offset
], (void *)&ss
,
300 sizeof(ss
.ss_len
) + sizeof(ss
.ss_family
)) == -1)
301 err(1, "get_struct %p", (void *)args
[sc
->offset
]);
302 /* sockaddr_un never have the length filled in! */
303 if (ss
.ss_family
== AF_UNIX
) {
304 if (get_struct(fd
, (void *)args
[sc
->offset
], (void *)&ss
,
307 err(2, "get_struct %p", (void *)args
[sc
->offset
]);
309 if (get_struct(fd
, (void *)args
[sc
->offset
], (void *)&ss
,
310 ss
.ss_len
< sizeof(ss
) ? ss
.ss_len
: sizeof(ss
))
312 err(2, "get_struct %p", (void *)args
[sc
->offset
]);
315 switch (ss
.ss_family
) {
317 lsin
= (struct sockaddr_in
*)&ss
;
318 inet_ntop(AF_INET
, &lsin
->sin_addr
, addr
, sizeof addr
);
319 asprintf(&tmp
, "{ AF_INET %s:%d }", addr
, htons(lsin
->sin_port
));
322 lsin6
= (struct sockaddr_in6
*)&ss
;
323 inet_ntop(AF_INET6
, &lsin6
->sin6_addr
, addr
, sizeof addr
);
324 asprintf(&tmp
, "{ AF_INET6 [%s]:%d }", addr
, htons(lsin6
->sin6_port
));
327 sun
= (struct sockaddr_un
*)&ss
;
328 asprintf(&tmp
, "{ AF_UNIX \"%s\" }", sun
->sun_path
);
331 sa
= (struct sockaddr
*)&ss
;
332 asprintf(&tmp
, "{ sa_len = %d, sa_family = %d, sa_data = {%n%*s } }",
333 (int)sa
->sa_len
, (int)sa
->sa_family
, &i
,
334 6 * (int)(sa
->sa_len
- ((char *)&sa
->sa_data
- (char *)sa
)), "");
337 for (q
= (u_char
*)&sa
->sa_data
; q
< (u_char
*)sa
+ sa
->sa_len
; q
++)
338 p
+= sprintf(p
, " %#02x,", *q
);
349 * Print (to outfile) the system call and its arguments. Note that
350 * nargs is the number of arguments (not the number of words; this is
351 * potentially confusing, I know).
355 print_syscall(FILE *outfile
, const char *name
, int nargs
, char **s_args
) {
358 len
+= fprintf(outfile
, "%s(", name
);
359 for (i
= 0; i
< nargs
; i
++) {
361 len
+= fprintf(outfile
, "%s", s_args
[i
]);
363 len
+= fprintf(outfile
, "<missing argument>");
364 len
+= fprintf(outfile
, "%s", i
< (nargs
- 1) ? "," : "");
366 len
+= fprintf(outfile
, ")");
367 for (i
= 0; i
< 6 - (len
/ 8); i
++)
368 fprintf(outfile
, "\t");
372 print_syscall_ret(FILE *outfile
, const char *name
, int nargs
, char **s_args
, int errorp
, int retval
) {
373 print_syscall(outfile
, name
, nargs
, s_args
);
375 fprintf(outfile
, " ERR#%d '%s'\n", retval
, strerror(retval
));
377 fprintf(outfile
, " = %d (0x%x)\n", retval
, retval
);