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[] =
38 * This file has routines used to print out system calls and their
43 #include <sys/types.h>
44 #include <sys/ptrace.h>
45 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <sys/ioccom.h>
51 #include <machine/atomic.h>
54 #include <sys/event.h>
56 #include <sys/resource.h>
75 /* 64-bit alignment on 32-bit platforms. */
82 /* Number of slots needed for a 64-bit argument. */
90 * This should probably be in its own file, sorted alphabetically.
93 struct syscall syscalls[] = {
95 { { Int, 0 } , { Fcntl, 1 }, { Fcntlflag | OUT, 2 }}},
97 { { Name, 0 } , { Readlinkres | OUT, 1 }, { Int, 2 }}},
99 { {Int, 0}, {Quad, 1 + QUAD_ALIGN},
100 {Whence, 1 + QUAD_SLOTS + QUAD_ALIGN}}},
101 { "linux_lseek", 2, 3,
102 { { Int, 0 }, {Int, 1 }, { Whence, 2 }}},
104 { {Ptr, 0}, {Int, 1}, {Mprot, 2}, {Mmapflags, 3}, {Int, 4},
105 {Quad, 5 + QUAD_ALIGN}}},
107 { { Ptr, 0 }, {Int, 1}, {Mprot, 2}}},
109 { { Name | IN, 0} , { Open, 1}, {Octal, 2}}},
111 { { Name, 0} , {Octal, 1}}},
112 { "linux_open", 1, 3,
113 { { Name, 0 }, { Hex, 1}, { Octal, 2 }}},
117 { { Name, 0 }, { Name, 1 }}},
125 { { Name, 0 }, { Octal, 1 }, { Int, 3 }}},
127 { { Name, 0 }, { Octal, 1 }}},
129 { { Name, 0 }, { Int, 1 }, { Int, 2 }}},
131 { { Name, 0 }, { Name, 1 }, { Int, 2 }, { Ptr, 3 }}},
133 { { Name, 0 }, { Int, 2 }}},
135 { { Int, 0}, { Stat | OUT , 1 }}},
137 { { Name | IN, 0 }, { Stat | OUT, 1 }}},
139 { { Name | IN, 0 }, { Stat | OUT, 1 }}},
140 { "linux_newstat", 1, 2,
141 { { Name | IN, 0 }, { Ptr | OUT, 1 }}},
142 { "linux_newfstat", 1, 2,
143 { { Int, 0 }, { Ptr | OUT, 1 }}},
145 { { Int, 0 }, { BinString | IN, 1 }, { Int, 2 }}},
147 { { Int, 0 }, { Ioctl, 1 }, { Hex, 2 }}},
148 { "break", 1, 1, { { Hex, 0 }}},
149 { "exit", 0, 1, { { Hex, 0 }}},
150 { "access", 1, 2, { { Name | IN, 0 }, { Int, 1 }}},
152 { { Signal, 0 }, { Sigaction | IN, 1 }, { Sigaction | OUT, 2 }}},
154 { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
156 { { Int, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
158 { { Int, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } },
159 { "getpeername", 1, 3,
160 { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
161 { "getsockname", 1, 3,
162 { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } },
164 { { Int, 0 }, { BinString | OUT, 1 }, { Int, 2 }, { Hex, 3 }, { Sockaddr | OUT, 4 }, { Ptr | OUT, 5 } } },
166 { { Int, 0 }, { BinString | IN, 1 }, { Int, 2 }, { Hex, 3 }, { Sockaddr | IN, 4 }, { Ptr | IN, 5 } } },
168 { { Name | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } },
169 { "linux_execve", 1, 3,
170 { { Name | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } },
171 { "kldload", 0, 1, { { Name | IN, 0 }}},
172 { "kldunload", 0, 1, { { Int, 0 }}},
173 { "kldfind", 0, 1, { { Name | IN, 0 }}},
174 { "kldnext", 0, 1, { { Int, 0 }}},
175 { "kldstat", 0, 2, { { Int, 0 }, { Ptr, 1 }}},
176 { "kldfirstmod", 0, 1, { { Int, 0 }}},
177 { "nanosleep", 0, 1, { { Timespec, 0 }}},
178 { "select", 1, 5, { { Int, 0 }, { Fd_set, 1 }, { Fd_set, 2 }, { Fd_set, 3 }, { Timeval, 4 }}},
179 { "poll", 1, 3, { { Pollfd, 0 }, { Int, 1 }, { Int, 2 }}},
180 { "gettimeofday", 1, 2, { { Timeval | OUT, 0 }, { Ptr, 1 }}},
181 { "clock_gettime", 1, 2, { { Int, 0 }, { Timespec | OUT, 1 }}},
182 { "getitimer", 1, 2, { { Int, 0 }, { Itimerval | OUT, 2 }}},
183 { "setitimer", 1, 3, { { Int, 0 }, { Itimerval, 1} , { Itimerval | OUT, 2 }}},
184 { "kse_release", 0, 1, { { Timespec, 0 }}},
185 { "kevent", 0, 6, { { Int, 0 }, { Kevent, 1 }, { Int, 2 }, { Kevent | OUT, 3 }, { Int, 4 }, { Timespec, 5 }}},
186 { "_umtx_lock", 0, 1, { { Umtx, 0 }}},
187 { "_umtx_unlock", 0, 1, { { Umtx, 0 }}},
188 { "sigprocmask", 0, 3, { { Sigprocmask, 0 }, { Sigset, 1 }, { Sigset | OUT, 2 }}},
189 { "unmount", 1, 2, { { Name, 0 }, { Int, 1 }}},
190 { "socket", 1, 3, { { Sockdomain, 0}, { Socktype, 1}, {Int, 2 }}},
191 { "getrusage", 1, 2, { { Int, 0 }, { Rusage | OUT, 1 }}},
192 { "__getcwd", 1, 2, { { Name | OUT, 0}, { Int, 1 }}},
193 { "shutdown", 1, 2, { { Int, 0}, { Shutdown, 1}}},
194 { "getrlimit", 1, 2, { { Resource, 0}, {Rlimit | OUT, 1}}},
195 { "setrlimit", 1, 2, { { Resource, 0}, {Rlimit | IN, 1}}},
197 { { Name | IN, 0 }, { Timeval2 | IN, 1 }}},
199 { { Name | IN, 0 }, { Timeval2 | IN, 1 }}},
201 { { Int, 0 }, { Timeval | IN, 1 }}},
203 { { Name | IN, 0 }, { Hex, 1 }}},
205 { { Name | IN, 0 }, { Hex, 1 }}},
207 { { Name | IN, 0 }, { Pathconf, 1 }}},
209 { { Name | IN, 0 }, { Int | IN, 1 }, { Quad | IN, 2 }}},
211 { { Int | IN, 0 }, { Int | IN, 1 }, { Quad | IN, 2 }}},
213 { { Int | IN, 0 }, { Signal | IN, 1}}},
215 { { Ptr, 0 }, { Int, 1 }}},
217 { { Int, 0}, { BinString | OUT, 1}, { Int, 2}}},
219 { { Name , 0} , { Name, 1}}},
221 { { Name , 0} , { Name, 1}}},
222 { 0, 0, 0, { { 0, 0 }}},
225 /* Xlat idea taken from strace */
231 #define X(a) { a, #a },
232 #define XEND { 0, NULL }
234 static struct xlat kevent_filters[] = {
235 X(EVFILT_READ) X(EVFILT_WRITE) X(EVFILT_AIO) X(EVFILT_VNODE)
236 X(EVFILT_PROC) X(EVFILT_SIGNAL) X(EVFILT_TIMER)
237 X(EVFILT_NETDEV) X(EVFILT_FS) X(EVFILT_READ) XEND
240 static struct xlat kevent_flags[] = {
241 X(EV_ADD) X(EV_DELETE) X(EV_ENABLE) X(EV_DISABLE) X(EV_ONESHOT)
242 X(EV_CLEAR) X(EV_FLAG1) X(EV_ERROR) X(EV_EOF) XEND
245 struct xlat poll_flags[] = {
246 X(POLLSTANDARD) X(POLLIN) X(POLLPRI) X(POLLOUT) X(POLLERR)
247 X(POLLHUP) X(POLLNVAL) X(POLLRDNORM) X(POLLRDBAND)
248 X(POLLWRBAND) X(POLLINIGNEOF) XEND
251 static struct xlat mmap_flags[] = {
252 X(MAP_SHARED) X(MAP_PRIVATE) X(MAP_FIXED) X(MAP_RENAME)
253 X(MAP_NORESERVE) X(MAP_RESERVED0080) X(MAP_RESERVED0100)
254 X(MAP_HASSEMAPHORE) X(MAP_STACK) X(MAP_NOSYNC) X(MAP_ANON)
258 static struct xlat mprot_flags[] = {
259 X(PROT_NONE) X(PROT_READ) X(PROT_WRITE) X(PROT_EXEC) XEND
262 static struct xlat whence_arg[] = {
263 X(SEEK_SET) X(SEEK_CUR) X(SEEK_END) XEND
266 static struct xlat sigaction_flags[] = {
267 X(SA_ONSTACK) X(SA_RESTART) X(SA_RESETHAND) X(SA_NOCLDSTOP)
268 X(SA_NODEFER) X(SA_NOCLDWAIT) X(SA_SIGINFO) XEND
271 static struct xlat fcntl_arg[] = {
272 X(F_DUPFD) X(F_GETFD) X(F_SETFD) X(F_GETFL) X(F_SETFL)
273 X(F_GETOWN) X(F_SETOWN) X(F_GETLK) X(F_SETLK) X(F_SETLKW) XEND
276 static struct xlat fcntlfd_arg[] = {
280 static struct xlat fcntlfl_arg[] = {
281 X(O_APPEND) X(O_ASYNC) X(O_FSYNC) X(O_NONBLOCK) X(O_NOFOLLOW)
285 static struct xlat sockdomain_arg[] = {
286 X(PF_UNSPEC) X(PF_LOCAL) X(PF_UNIX) X(PF_INET) X(PF_IMPLINK)
287 X(PF_PUP) X(PF_CHAOS) X(PF_NETBIOS) X(PF_ISO) X(PF_OSI)
288 X(PF_ECMA) X(PF_DATAKIT) X(PF_CCITT) X(PF_SNA) X(PF_DECnet)
289 X(PF_DLI) X(PF_LAT) X(PF_HYLINK) X(PF_APPLETALK) X(PF_ROUTE)
290 X(PF_LINK) X(PF_XTP) X(PF_COIP) X(PF_CNT) X(PF_SIP) X(PF_IPX)
291 X(PF_RTIP) X(PF_PIP) X(PF_ISDN) X(PF_KEY) X(PF_INET6)
292 X(PF_NATM) X(PF_ATM) X(PF_NETGRAPH) X(PF_SLOW) X(PF_SCLUSTER)
293 X(PF_ARP) X(PF_BLUETOOTH) XEND
296 static struct xlat socktype_arg[] = {
297 X(SOCK_STREAM) X(SOCK_DGRAM) X(SOCK_RAW) X(SOCK_RDM)
298 X(SOCK_SEQPACKET) XEND
301 static struct xlat open_flags[] = {
302 X(O_RDONLY) X(O_WRONLY) X(O_RDWR) X(O_ACCMODE) X(O_NONBLOCK)
303 X(O_APPEND) X(O_SHLOCK) X(O_EXLOCK) X(O_ASYNC) X(O_FSYNC)
304 X(O_NOFOLLOW) X(O_CREAT) X(O_TRUNC) X(O_EXCL) X(O_NOCTTY)
308 static struct xlat shutdown_arg[] = {
309 X(SHUT_RD) X(SHUT_WR) X(SHUT_RDWR) XEND
312 static struct xlat resource_arg[] = {
313 X(RLIMIT_CPU) X(RLIMIT_FSIZE) X(RLIMIT_DATA) X(RLIMIT_STACK)
314 X(RLIMIT_CORE) X(RLIMIT_RSS) X(RLIMIT_MEMLOCK) X(RLIMIT_NPROC)
315 X(RLIMIT_NOFILE) X(RLIMIT_SBSIZE) X(RLIMIT_VMEM) XEND
318 static struct xlat pathconf_arg[] = {
319 X(_PC_LINK_MAX) X(_PC_MAX_CANON) X(_PC_MAX_INPUT)
320 X(_PC_NAME_MAX) X(_PC_PATH_MAX) X(_PC_PIPE_BUF)
321 X(_PC_CHOWN_RESTRICTED) X(_PC_NO_TRUNC) X(_PC_VDISABLE)
322 X(_PC_ASYNC_IO) X(_PC_PRIO_IO) X(_PC_SYNC_IO)
323 X(_PC_ALLOC_SIZE_MIN) X(_PC_FILESIZEBITS)
324 X(_PC_REC_INCR_XFER_SIZE) X(_PC_REC_MAX_XFER_SIZE)
325 X(_PC_REC_MIN_XFER_SIZE) X(_PC_REC_XFER_ALIGN)
326 X(_PC_SYMLINK_MAX) X(_PC_ACL_EXTENDED) X(_PC_ACL_PATH_MAX)
327 X(_PC_CAP_PRESENT) X(_PC_INF_PRESENT) X(_PC_MAC_PRESENT)
334 /* Searches an xlat array for a value, and returns it if found. Otherwise
335 return a string representation. */
337 *lookup(struct xlat *xlat, int val, int base)
340 for (; xlat->str != NULL; xlat++)
341 if (xlat->val == val)
345 sprintf(tmp, "0%o", val);
348 sprintf(tmp, "0x%x", val);
351 sprintf(tmp, "%u", val);
354 errx(1,"Unknown lookup base");
361 xlookup(struct xlat *xlat, int val)
363 return lookup(xlat, val, 16);
366 /* Searches an xlat array containing bitfield values. Remaining bits
367 set after removing the known ones are printed at the end:
370 *xlookup_bits(struct xlat *xlat, int val)
372 static char str[512];
376 for (; xlat->str != NULL; xlat++)
378 if ((xlat->val & rem) == xlat->val)
380 /* don't print the "all-bits-zero" string unless all
381 bits are really zero */
382 if (xlat->val == 0 && val != 0)
384 len += sprintf(str + len, "%s|", xlat->str);
388 /* if we have leftover bits or didn't match anything */
390 len += sprintf(str + len, "0x%x", rem);
391 if (len && str[len - 1] == '|')
398 * If/when the list gets big, it might be desirable to do it
399 * as a hash table or binary search.
403 get_syscall(const char *name) {
404 struct syscall *sc = syscalls;
409 if (!strcmp(name, sc->name))
419 * Copy a fixed amount of bytes from the process.
423 get_struct(int pid, void *offset, void *buf, int len) {
424 struct ptrace_io_desc iorequest;
426 iorequest.piod_op = PIOD_READ_D;
427 iorequest.piod_offs = offset;
428 iorequest.piod_addr = buf;
429 iorequest.piod_len = len;
430 if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0)
436 #define BLOCKSIZE 1024
439 * Copy a string from the process. Note that it is
440 * expected to be a C string, but if max is set, it will
441 * only get that much.
445 get_string(pid_t pid, void *offset, int max) {
447 struct ptrace_io_desc iorequest;
452 totalsize = size = max ? (max + 1) : BLOCKSIZE;
453 buf = malloc(totalsize);
457 diff = totalsize - size;
458 iorequest.piod_op = PIOD_READ_D;
459 iorequest.piod_offs = (char *)offset + diff;
460 iorequest.piod_addr = buf + diff;
461 iorequest.piod_len = size;
462 if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0) {
466 for (i = 0 ; i < size; i++) {
467 if (buf[diff + i] == '\0')
470 if (totalsize < MAXSIZE - BLOCKSIZE && max == 0) {
471 totalsize += BLOCKSIZE;
472 buf = realloc(buf, totalsize);
476 buf[totalsize] = '\0';
485 * Converts a syscall argument into a string. Said string is
486 * allocated via malloc(), so needs to be free()'d. The file
487 * descriptor is for the process' memory (via /proc), and is used
488 * to get any data (where the argument is a pointer). sc is
489 * a pointer to the syscall description (see above); args is
490 * an array of all of the system call arguments.
494 print_arg(struct syscall_args *sc, unsigned long *args, long retval, struct trussinfo *trussinfo) {
496 int pid = trussinfo->pid;
497 switch (sc->type & ARG_MASK) {
499 asprintf(&tmp, "0x%x", (int)args[sc->offset]);
502 asprintf(&tmp, "0%o", (int)args[sc->offset]);
505 asprintf(&tmp, "%d", (int)args[sc->offset]);
509 /* NULL-terminated string. */
511 tmp2 = get_string(pid, (void*)args[sc->offset], 0);
512 asprintf(&tmp, "\"%s\"", tmp2);
518 /* Binary block of data that might have printable characters.
519 XXX If type|OUT, assume that the length is the syscall's
520 return value. Otherwise, assume that the length of the block
521 is in the next syscall argument. */
522 int max_string = trussinfo->strsize;
523 char tmp2[max_string+1], *tmp3;
530 len = args[sc->offset + 1];
532 /* Don't print more than max_string characters, to avoid word
533 wrap. If we have to truncate put some ... after the string.
535 if (len > max_string) {
539 if (len && get_struct(pid, (void*)args[sc->offset], &tmp2, len) != -1) {
540 tmp3 = malloc(len * 4 + 1);
542 if (strvisx(tmp3, tmp2, len, VIS_CSTYLE|VIS_TAB|VIS_NL) <= max_string)
547 asprintf(&tmp, "\"%s\"%s", tmp3, truncated?"...":"");
550 asprintf(&tmp, "0x%lx", args[sc->offset]);
558 char *strarray[100]; /* XXX This is ugly. */
560 if (get_struct(pid, (void *)args[sc->offset], (void *)&strarray,
561 sizeof(strarray)) == -1) {
562 err(1, "get_struct %p", (void *)args[sc->offset]);
567 /* Find out how large of a buffer we'll need. */
568 while (strarray[num] != NULL) {
569 string = get_string(pid, (void*)strarray[num], 0);
570 size += strlen(string);
574 size += 4 + (num * 4);
575 tmp = (char *)malloc(size);
578 tmp2 += sprintf(tmp2, " [");
579 for (i = 0; i < num; i++) {
580 string = get_string(pid, (void*)strarray[i], 0);
581 tmp2 += sprintf(tmp2, " \"%s\"%c", string, (i+1 == num) ? ' ' : ',');
584 tmp2 += sprintf(tmp2, "]");
589 asprintf(&tmp, "0x%lx", args[sc->offset]);
594 unsigned long long ll;
595 ll = *(unsigned long long *)(args + sc->offset);
596 asprintf(&tmp, "0x%llx", ll);
601 asprintf(&tmp, "0x%lx", args[sc->offset]);
610 tmp2 = get_string(pid, (void*)args[sc->offset], retval);
611 asprintf(&tmp, "\"%s\"", tmp2);
617 const char *temp = ioctlname(args[sc->offset]);
622 unsigned long arg = args[sc->offset];
623 asprintf(&tmp, "0x%lx { IO%s%s 0x%lx('%c'), %lu, %lu}", arg,
624 arg&IOC_OUT?"R":"", arg&IOC_IN?"W":"",
625 IOCGROUP(arg), isprint(IOCGROUP(arg))?(char)IOCGROUP(arg):'?',
626 arg & 0xFF, IOCPARM_LEN(arg));
633 if (get_struct(pid, (void *)args[sc->offset], &umtx, sizeof(umtx)) != -1)
634 asprintf(&tmp, "{0x%lx}", (long)umtx.u_owner);
636 asprintf(&tmp, "0x%lx", args[sc->offset]);
642 if (get_struct(pid, (void *)args[sc->offset], &ts, sizeof(ts)) != -1)
643 asprintf(&tmp, "{%ld.%09ld}", (long)ts.tv_sec, ts.tv_nsec);
645 asprintf(&tmp, "0x%lx", args[sc->offset]);
651 if (get_struct(pid, (void *)args[sc->offset], &tv, sizeof(tv)) != -1)
652 asprintf(&tmp, "{%ld.%06ld}", (long)tv.tv_sec, tv.tv_usec);
654 asprintf(&tmp, "0x%lx", args[sc->offset]);
659 struct timeval tv[2];
660 if (get_struct(pid, (void *)args[sc->offset], &tv, sizeof(tv)) != -1)
661 asprintf(&tmp, "{%ld.%06ld, %ld.%06ld}",
662 (long)tv[0].tv_sec, tv[0].tv_usec,
663 (long)tv[1].tv_sec, tv[1].tv_usec);
665 asprintf(&tmp, "0x%lx", args[sc->offset]);
670 struct itimerval itv;
671 if (get_struct(pid, (void *)args[sc->offset], &itv, sizeof(itv)) != -1)
672 asprintf(&tmp, "{%ld.%06ld, %ld.%06ld}",
673 (long)itv.it_interval.tv_sec,
674 itv.it_interval.tv_usec,
675 (long)itv.it_value.tv_sec,
676 itv.it_value.tv_usec);
678 asprintf(&tmp, "0x%lx", args[sc->offset]);
684 * XXX: A Pollfd argument expects the /next/ syscall argument to be
685 * the number of fds in the array. This matches the poll syscall.
688 int numfds = args[sc->offset+1];
689 int bytes = sizeof(struct pollfd) * numfds;
690 int i, tmpsize, u, used;
691 const int per_fd = 100;
693 if ((pfd = malloc(bytes)) == NULL)
694 err(1, "Cannot malloc %d bytes for pollfd array", bytes);
695 if (get_struct(pid, (void *)args[sc->offset], pfd, bytes) != -1) {
698 tmpsize = 1 + per_fd * numfds + 2;
699 if ((tmp = malloc(tmpsize)) == NULL)
700 err(1, "Cannot alloc %d bytes for poll output", tmpsize);
703 for (i = 0; i < numfds; i++) {
705 u = snprintf(tmp + used, per_fd,
709 xlookup_bits(poll_flags, pfd[i].events) );
711 used += u < per_fd ? u : per_fd;
716 asprintf(&tmp, "0x%lx", args[sc->offset]);
723 * XXX: A Fd_set argument expects the /first/ syscall argument to be
724 * the number of fds in the array. This matches the select syscall.
727 int numfds = args[0];
728 int bytes = _howmany(numfds, _NFDBITS) * _NFDBITS;
729 int i, tmpsize, u, used;
730 const int per_fd = 20;
732 if ((fds = malloc(bytes)) == NULL)
733 err(1, "Cannot malloc %d bytes for fd_set array", bytes);
734 if (get_struct(pid, (void *)args[sc->offset], fds, bytes) != -1) {
736 tmpsize = 1 + numfds * per_fd + 2;
737 if ((tmp = malloc(tmpsize)) == NULL)
738 err(1, "Cannot alloc %d bytes for fd_set output", tmpsize);
741 for (i = 0; i < numfds; i++) {
742 if (FD_ISSET(i, fds)) {
743 u = snprintf(tmp + used, per_fd, "%d ", i);
745 used += u < per_fd ? u : per_fd;
748 if (tmp[used-1] == ' ')
753 asprintf(&tmp, "0x%lx", args[sc->offset]);
761 sig = args[sc->offset];
764 asprintf(&tmp, "%ld", sig);
773 sig = args[sc->offset];
774 if (get_struct(pid, (void *)args[sc->offset], (void *)&ss,
777 asprintf(&tmp, "0x%lx", args[sc->offset]);
780 tmp = malloc(sys_nsig * 8); /* 7 bytes avg per signal name */
782 for (i = 1; i < sys_nsig; i++)
784 if (sigismember(&ss, i))
786 used += sprintf(tmp + used, "%s|", strsig(i));
797 switch (args[sc->offset]) {
798 #define S(a) case a: tmp = strdup(#a); break;
805 asprintf(&tmp, "0x%lx", args[sc->offset]);
811 /* XXX output depends on the value of the previous argument */
812 switch (args[sc->offset-1]) {
814 tmp = strdup(xlookup_bits(fcntlfd_arg, args[sc->offset]));
817 tmp = strdup(xlookup_bits(fcntlfl_arg, args[sc->offset]));
825 asprintf(&tmp, "0x%lx", args[sc->offset]);
831 tmp = strdup(xlookup_bits(open_flags, args[sc->offset]));
834 tmp = strdup(xlookup(fcntl_arg, args[sc->offset]));
837 tmp = strdup(xlookup_bits(mprot_flags, args[sc->offset]));
840 tmp = strdup(xlookup_bits(mmap_flags, args[sc->offset]));
843 tmp = strdup(xlookup(whence_arg, args[sc->offset]));
846 tmp = strdup(xlookup(sockdomain_arg, args[sc->offset]));
849 tmp = strdup(xlookup(socktype_arg, args[sc->offset]));
852 tmp = strdup(xlookup(shutdown_arg, args[sc->offset]));
855 tmp = strdup(xlookup(resource_arg, args[sc->offset]));
858 tmp = strdup(xlookup(pathconf_arg, args[sc->offset]));
862 struct sockaddr_storage ss;
864 struct sockaddr_in *lsin;
865 struct sockaddr_in6 *lsin6;
866 struct sockaddr_un *sun;
872 if (args[sc->offset] == 0) {
873 asprintf(&tmp, "NULL");
877 /* yuck: get ss_len */
878 if (get_struct(pid, (void *)args[sc->offset], (void *)&ss,
879 sizeof(ss.ss_len) + sizeof(ss.ss_family)) == -1)
880 err(1, "get_struct %p", (void *)args[sc->offset]);
882 * If ss_len is 0, then try to guess from the sockaddr type.
883 * AF_UNIX may be initialized incorrectly, so always frob
884 * it by using the "right" size.
886 if (ss.ss_len == 0 || ss.ss_family == AF_UNIX) {
887 switch (ss.ss_family) {
889 ss.ss_len = sizeof(*lsin);
892 ss.ss_len = sizeof(*sun);
899 if (get_struct(pid, (void *)args[sc->offset], (void *)&ss, ss.ss_len)
901 err(2, "get_struct %p", (void *)args[sc->offset]);
904 switch (ss.ss_family) {
906 lsin = (struct sockaddr_in *)&ss;
907 inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof addr);
908 asprintf(&tmp, "{ AF_INET %s:%d }", addr, htons(lsin->sin_port));
911 lsin6 = (struct sockaddr_in6 *)&ss;
912 inet_ntop(AF_INET6, &lsin6->sin6_addr, addr, sizeof addr);
913 asprintf(&tmp, "{ AF_INET6 [%s]:%d }", addr, htons(lsin6->sin6_port));
916 sun = (struct sockaddr_un *)&ss;
917 asprintf(&tmp, "{ AF_UNIX \"%s\" }", sun->sun_path);
920 sa = (struct sockaddr *)&ss;
921 asprintf(&tmp, "{ sa_len = %d, sa_family = %d, sa_data = {%n%*s } }",
922 (int)sa->sa_len, (int)sa->sa_family, &i,
923 6 * (int)(sa->sa_len - ((char *)&sa->sa_data - (char *)sa)), "");
926 for (q = (u_char *)&sa->sa_data; q < (u_char *)sa + sa->sa_len; q++)
927 p += sprintf(p, " %#02x,", *q);
938 if (get_struct(pid, (void *)args[sc->offset], &sa, sizeof(sa)) != -1) {
940 asprintf(&hand, "%p", sa.sa_handler);
941 if (sa.sa_handler == SIG_DFL)
943 else if (sa.sa_handler == SIG_IGN)
948 asprintf(&tmp, "{ %s %s ss_t }",
950 xlookup_bits(sigaction_flags, sa.sa_flags));
953 asprintf(&tmp, "0x%lx", args[sc->offset]);
960 * XXX XXX: the size of the array is determined by either the
961 * next syscall argument, or by the syscall returnvalue,
962 * depending on which argument number we are. This matches the
963 * kevent syscall, but luckily that's the only syscall that uses
969 int i, tmpsize, u, used;
970 const int per_ke = 100;
973 numevents = args[sc->offset+1];
974 else if (sc->offset == 3 && retval != -1)
978 bytes = sizeof(struct kevent) * numevents;
979 if ((ke = malloc(bytes)) == NULL)
980 err(1, "Cannot malloc %d bytes for kevent array", bytes);
981 if (numevents >= 0 && get_struct(pid, (void *)args[sc->offset], ke, bytes) != -1) {
983 tmpsize = 1 + per_ke * numevents + 2;
984 if ((tmp = malloc(tmpsize)) == NULL)
985 err(1, "Cannot alloc %d bytes for kevent output", tmpsize);
988 for (i = 0; i < numevents; i++) {
989 u = snprintf(tmp + used, per_ke,
990 "%s%p,%s,%s,%d,%p,%p",
993 xlookup(kevent_filters, ke[i].filter),
994 xlookup_bits(kevent_flags, ke[i].flags),
997 (void *)ke[i].udata);
999 used += u < per_ke ? u : per_ke;
1004 asprintf(&tmp, "0x%lx", args[sc->offset]);
1011 if (get_struct(pid, (void *)args[sc->offset], &st, sizeof(st)) != -1) {
1013 strmode(st.st_mode, mode);
1014 asprintf(&tmp, "{mode=%s,inode=%jd,size=%jd,blksize=%ld}",
1016 (intmax_t)st.st_ino,(intmax_t)st.st_size,(long)st.st_blksize);
1018 asprintf(&tmp, "0x%lx", args[sc->offset]);
1024 if (get_struct(pid, (void *)args[sc->offset], &ru, sizeof(ru)) != -1)
1025 asprintf(&tmp, "{u=%ld.%06ld,s=%ld.%06ld,in=%ld,out=%ld}",
1026 (long)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec,
1027 (long)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec,
1028 ru.ru_inblock, ru.ru_oublock);
1030 asprintf(&tmp, "0x%lx", args[sc->offset]);
1036 if (get_struct(pid, (void *)args[sc->offset], &rl, sizeof(rl)) != -1)
1037 asprintf(&tmp, "{cur=%ju,max=%ju}",
1038 rl.rlim_cur, rl.rlim_max);
1040 asprintf(&tmp, "0x%lx", args[sc->offset]);
1044 errx(1, "Invalid argument type %d\n", sc->type & ARG_MASK);
1052 * Print (to outfile) the system call and its arguments. Note that
1053 * nargs is the number of arguments (not the number of words; this is
1054 * potentially confusing, I know).
1058 print_syscall(struct trussinfo *trussinfo, const char *name, int nargs, char **s_args) {
1061 struct timespec timediff;
1063 if (trussinfo->flags & FOLLOWFORKS)
1064 len += fprintf(trussinfo->outfile, "%5d: ", trussinfo->pid);
1066 if (name != NULL && (!strcmp(name, "execve") || !strcmp(name, "exit"))) {
1067 clock_gettime(CLOCK_REALTIME, &trussinfo->after);
1070 if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
1071 timespecsubt(&trussinfo->after, &trussinfo->start_time, &timediff);
1072 len += fprintf(trussinfo->outfile, "%ld.%09ld ",
1073 (long)timediff.tv_sec, timediff.tv_nsec);
1076 if (trussinfo->flags & RELATIVETIMESTAMPS) {
1077 timespecsubt(&trussinfo->after, &trussinfo->before, &timediff);
1078 len += fprintf(trussinfo->outfile, "%ld.%09ld ",
1079 (long)timediff.tv_sec, timediff.tv_nsec);
1082 len += fprintf(trussinfo->outfile, "%s(", name);
1084 for (i = 0; i < nargs; i++) {
1086 len += fprintf(trussinfo->outfile, "%s", s_args[i]);
1088 len += fprintf(trussinfo->outfile, "<missing argument>");
1089 len += fprintf(trussinfo->outfile, "%s", i < (nargs - 1) ? "," : "");
1091 len += fprintf(trussinfo->outfile, ")");
1092 for (i = 0; i < 6 - (len / 8); i++)
1093 fprintf(trussinfo->outfile, "\t");
1097 print_syscall_ret(struct trussinfo *trussinfo, const char *name, int nargs,
1098 char **s_args, int errorp, long retval)
1100 print_syscall(trussinfo, name, nargs, s_args);
1101 fflush(trussinfo->outfile);
1103 fprintf(trussinfo->outfile, " ERR#%ld '%s'\n", retval, strerror(retval));
1105 fprintf(trussinfo->outfile, " = %ld (0x%lx)\n", retval, retval);