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 $
35 * This file has routines used to print out system calls and their
39 #include <sys/types.h>
40 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #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}}},
70 { "close", 1, 1, { { Int
, 0 } } },
72 { { Int
, 0}, {Ptr
| OUT
, 1 }}},
74 { { String
| IN
, 0 }, { Ptr
| OUT
, 1 }}},
76 { { String
| IN
, 0 }, { Ptr
| OUT
, 1 }}},
78 { { Int
, 0}, { Ptr
| IN
, 1 }, { Int
, 2 }}},
80 { { Int
, 0}, { Ioctl
, 1 }, { Hex
, 2 }}},
81 { "break", 1, 1, { { Hex
, 0 }}},
82 { "exit", 0, 1, { { Hex
, 0 }}},
83 { "access", 1, 2, { { String
| IN
, 0 }, { Int
, 1 }}},
85 { { Signal
, 0 }, { Ptr
| IN
, 1 }, { Ptr
| OUT
, 2 }}},
87 { { Hex
, 0 }, { Sockaddr
| OUT
, 1 }, { Ptr
| OUT
, 2 } } },
89 { { Hex
, 0 }, { Sockaddr
| IN
, 1 }, { Int
, 2 } } },
91 { { Hex
, 0 }, { Sockaddr
| IN
, 1 }, { Int
, 2 } } },
92 { "getpeername", 1, 3,
93 { { Hex
, 0 }, { Sockaddr
| OUT
, 1 }, { Ptr
| OUT
, 2 } } },
94 { "getsockname", 1, 3,
95 { { Hex
, 0 }, { Sockaddr
| OUT
, 1 }, { Ptr
| OUT
, 2 } } },
96 { 0, 0, 0, { { 0, 0 }}},
100 * If/when the list gets big, it might be desirable to do it
101 * as a hash table or binary search.
105 get_syscall(const char *name
) {
106 struct syscall
*sc
= syscalls
;
109 if (!strcmp(name
, sc
->name
))
119 * Copy a fixed amount of bytes from the process.
123 get_struct(int procfd
, void *offset
, void *buf
, int len
) {
128 if ((fd
= dup(procfd
)) == -1)
130 if ((p
= fdopen(fd
, "r")) == NULL
)
132 fseeko(p
, (uintptr_t)offset
, SEEK_SET
);
133 for (pos
= (char *)buf
; len
--; pos
++) {
134 if ((c
= fgetc(p
)) == EOF
)
144 * Copy a string from the process. Note that it is
145 * expected to be a C string, but if max is set, it will
146 * only get that much.
150 get_string(int procfd
, void *offset
, int max
) {
152 int size
, len
, c
, fd
;
155 if ((fd
= dup(procfd
)) == -1)
157 if ((p
= fdopen(fd
, "r")) == NULL
)
159 buf
= malloc( size
= (max
? max
: 64 ) );
162 fseeko(p
, (uintptr_t)offset
, SEEK_SET
);
163 while ((c
= fgetc(p
)) != EOF
) {
165 if (c
== 0 || len
== max
) {
171 tmp
= realloc(buf
, size
+64);
187 * Gag. This is really unportable. Multiplication is more portable.
188 * But slower, from the code I saw.
192 make_quad(unsigned long p1
, unsigned long p2
) {
205 * Converts a syscall argument into a string. Said string is
206 * allocated via malloc(), so needs to be free()'d. The file
207 * descriptor is for the process' memory (via /proc), and is used
208 * to get any data (where the argument is a pointer). sc is
209 * a pointer to the syscall description (see above); args is
210 * an array of all of the system call arguments.
214 print_arg(int fd
, struct syscall_args
*sc
, unsigned long *args
) {
216 switch (sc
->type
& ARG_MASK
) {
219 sprintf(tmp
, "0x%lx", args
[sc
->offset
]);
223 sprintf(tmp
, "0%lo", args
[sc
->offset
]);
227 sprintf(tmp
, "%ld", args
[sc
->offset
]);
232 tmp2
= get_string(fd
, (void*)args
[sc
->offset
], 0);
233 tmp
= malloc(strlen(tmp2
) + 3);
234 sprintf(tmp
, "\"%s\"", tmp2
);
240 unsigned long long t
;
241 unsigned long l1
, l2
;
242 l1
= args
[sc
->offset
];
243 l2
= args
[sc
->offset
+1];
244 t
= make_quad(l1
, l2
);
246 sprintf(tmp
, "0x%qx", t
);
251 sprintf(tmp
, "0x%lx", args
[sc
->offset
]);
255 const char *temp
= ioctlname(args
[sc
->offset
]);
260 sprintf(tmp
, "0x%lx", args
[sc
->offset
]);
268 sig
= args
[sc
->offset
];
270 if (sig
> 0 && sig
< NSIG
) {
272 sprintf(tmp
, "sig%s", sys_signame
[sig
]);
273 for (i
= 0; tmp
[i
] != '\0'; ++i
)
274 tmp
[i
] = toupper(tmp
[i
]);
276 sprintf(tmp
, "%ld", sig
);
282 struct sockaddr_storage ss
;
284 struct sockaddr_in
*lsin
;
285 struct sockaddr_in6
*lsin6
;
286 struct sockaddr_un
*sun
;
292 /* yuck: get ss_len */
293 if (get_struct(fd
, (void *)args
[sc
->offset
], &ss
,
294 sizeof(ss
.ss_len
) + sizeof(ss
.ss_family
)) == -1)
295 err(1, "get_struct %p", (void *)args
[sc
->offset
]);
296 /* sockaddr_un never have the length filled in! */
297 if (ss
.ss_family
== AF_UNIX
) {
298 if (get_struct(fd
, (void *)args
[sc
->offset
], &ss
,
301 err(2, "get_struct %p", (void *)args
[sc
->offset
]);
303 if (get_struct(fd
, (void *)args
[sc
->offset
], &ss
,
304 ss
.ss_len
< sizeof(ss
) ? ss
.ss_len
: sizeof(ss
))
306 err(2, "get_struct %p", (void *)args
[sc
->offset
]);
309 switch (ss
.ss_family
) {
311 lsin
= (struct sockaddr_in
*)&ss
;
312 inet_ntop(AF_INET
, &lsin
->sin_addr
, addr
, sizeof addr
);
313 asprintf(&tmp
, "{ AF_INET %s:%d }", addr
, htons(lsin
->sin_port
));
316 lsin6
= (struct sockaddr_in6
*)&ss
;
317 inet_ntop(AF_INET6
, &lsin6
->sin6_addr
, addr
, sizeof addr
);
318 asprintf(&tmp
, "{ AF_INET6 [%s]:%d }", addr
, htons(lsin6
->sin6_port
));
321 sun
= (struct sockaddr_un
*)&ss
;
322 asprintf(&tmp
, "{ AF_UNIX \"%s\" }", sun
->sun_path
);
325 sa
= (struct sockaddr
*)&ss
;
326 asprintf(&tmp
, "{ sa_len = %d, sa_family = %d, sa_data = {%n%*s } }",
327 (int)sa
->sa_len
, (int)sa
->sa_family
, &i
,
328 6 * (int)(sa
->sa_len
- ((char *)&sa
->sa_data
- (char *)sa
)), "");
331 for (q
= (u_char
*)&sa
->sa_data
; q
< (u_char
*)sa
+ sa
->sa_len
; q
++)
332 p
+= sprintf(p
, " %#02x,", *q
);
343 * Print (to trussinfo->outfile) the system call and its arguments. Note that
344 * nargs is the number of arguments (not the number of words; this is
345 * potentially confusing, I know).
349 print_syscall(struct trussinfo
*trussinfo
, const char *name
, int nargs
, char **s_args
) {
352 len
+= fprintf(trussinfo
->outfile
, "%s(", name
);
353 for (i
= 0; i
< nargs
; i
++) {
355 len
+= fprintf(trussinfo
->outfile
, "%s", s_args
[i
]);
357 len
+= fprintf(trussinfo
->outfile
, "<missing argument>");
358 len
+= fprintf(trussinfo
->outfile
, "%s", i
< (nargs
- 1) ? "," : "");
360 len
+= fprintf(trussinfo
->outfile
, ")");
361 for (i
= 0; i
< 6 - (len
/ 8); i
++)
362 fprintf(trussinfo
->outfile
, "\t");
366 print_syscall_ret(struct trussinfo
*trussinfo
, const char *name
, int nargs
, char **s_args
, int errorp
, int retval
) {
367 print_syscall(trussinfo
, name
, nargs
, s_args
);
369 fprintf(trussinfo
->outfile
, " ERR#%d '%s'\n", retval
, strerror(retval
));
371 fprintf(trussinfo
->outfile
, " = %d (0x%x)\n", retval
, retval
);