2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifdef HAVE_SYS_EPOLL_H
35 # include <sys/epoll.h>
40 #ifdef HAVE_LINUX_PERF_EVENT_H
41 # include <linux/perf_event.h>
44 static const struct xlat fcntlcmds[] = {
45 { F_DUPFD, "F_DUPFD" },
46 { F_GETFD, "F_GETFD" },
47 { F_SETFD, "F_SETFD" },
48 { F_GETFL, "F_GETFL" },
49 { F_SETFL, "F_SETFL" },
50 { F_GETLK, "F_GETLK" },
51 { F_SETLK, "F_SETLK" },
52 { F_SETLKW, "F_SETLKW" },
53 { F_GETOWN, "F_GETOWN" },
54 { F_SETOWN, "F_SETOWN" },
56 { F_RSETLK, "F_RSETLK" },
59 { F_RSETLKW, "F_RSETLKW" },
62 { F_RGETLK, "F_RGETLK" },
68 { F_SETSIG, "F_SETSIG" },
71 { F_GETSIG, "F_GETSIG" },
74 { F_CHKFL, "F_CHKFL" },
77 { F_DUP2FD, "F_DUP2FD" },
80 { F_ALLOCSP, "F_ALLOCSP" },
83 { F_ISSTREAM, "F_ISSTREAM" },
89 { F_NPRIV, "F_NPRIV" },
92 { F_QUOTACL, "F_QUOTACL" },
95 { F_BLOCKS, "F_BLOCKS" },
98 { F_BLKSIZE, "F_BLKSIZE" },
101 { F_GETOWN, "F_GETOWN" },
104 { F_SETOWN, "F_SETOWN" },
107 { F_REVOKE, "F_REVOKE" },
110 { F_SETLK, "F_SETLK" },
113 { F_SETLKW, "F_SETLKW" },
116 { F_FREESP, "F_FREESP" },
119 { F_GETLK, "F_GETLK" },
122 { F_SETLK64, "F_SETLK64" },
125 { F_SETLKW64, "F_SETLKW64" },
128 { F_FREESP64, "F_FREESP64" },
131 { F_GETLK64, "F_GETLK64" },
134 { F_SHARE, "F_SHARE" },
137 { F_UNSHARE, "F_UNSHARE" },
140 { F_SETLEASE, "F_SETLEASE" },
143 { F_GETLEASE, "F_GETLEASE" },
146 { F_NOTIFY, "F_NOTIFY" },
148 #ifdef F_DUPFD_CLOEXEC
149 { F_DUPFD_CLOEXEC,"F_DUPFD_CLOEXEC"},
154 static const struct xlat fdflags[] = {
156 { FD_CLOEXEC, "FD_CLOEXEC" },
163 static const struct xlat flockcmds[] = {
164 { LOCK_SH, "LOCK_SH" },
165 { LOCK_EX, "LOCK_EX" },
166 { LOCK_NB, "LOCK_NB" },
167 { LOCK_UN, "LOCK_UN" },
173 static const struct xlat lockfcmds[] = {
174 { F_RDLCK, "F_RDLCK" },
175 { F_WRLCK, "F_WRLCK" },
176 { F_UNLCK, "F_UNLCK" },
178 { F_EXLCK, "F_EXLCK" },
181 { F_SHLCK, "F_SHLCK" },
187 static const struct xlat notifyflags[] = {
189 { DN_ACCESS, "DN_ACCESS" },
192 { DN_MODIFY, "DN_MODIFY" },
195 { DN_CREATE, "DN_CREATE" },
198 { DN_DELETE, "DN_DELETE" },
201 { DN_RENAME, "DN_RENAME" },
204 { DN_ATTRIB, "DN_ATTRIB" },
207 { DN_MULTISHOT, "DN_MULTISHOT" },
213 static const struct xlat perf_event_open_flags[] = {
214 #ifdef PERF_FLAG_FD_NO_GROUP
215 { PERF_FLAG_FD_NO_GROUP, "PERF_FLAG_FD_NO_GROUP" },
217 #ifdef PERF_FLAG_FD_OUTPUT
218 { PERF_FLAG_FD_OUTPUT, "PERF_FLAG_FD_OUTPUT" },
220 #ifdef PERF_FLAG_PID_CGROUP
221 { PERF_FLAG_PID_CGROUP, "PERF_FLAG_PID_CGROUP" },
229 printflock64(struct tcb *tcp, long addr, int getlk)
233 if (umove(tcp, addr, &fl) < 0) {
238 printxval(lockfcmds, fl.l_type, "F_???");
239 tprints(", whence=");
240 printxval(whence_codes, fl.l_whence, "SEEK_???");
241 tprintf(", start=%lld, len=%lld", (long long) fl.l_start, (long long) fl.l_len);
243 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
251 printflock(struct tcb *tcp, long addr, int getlk)
255 #if SUPPORTED_PERSONALITIES > 1
257 if (current_personality == 0) {
258 printflock64(tcp, addr, getlk);
262 if (current_wordsize != sizeof(fl.l_start)) {
263 if (current_wordsize == 4) {
264 /* 32-bit x86 app on x86_64 and similar cases */
268 int32_t l_start; /* off_t */
269 int32_t l_len; /* off_t */
270 int32_t l_pid; /* pid_t */
272 if (umove(tcp, addr, &fl32) < 0) {
276 fl.l_type = fl32.l_type;
277 fl.l_whence = fl32.l_whence;
278 fl.l_start = fl32.l_start;
279 fl.l_len = fl32.l_len;
280 fl.l_pid = fl32.l_pid;
282 /* let people know we have a problem here */
283 tprintf("<decode error: unsupported wordsize %d>",
290 if (umove(tcp, addr, &fl) < 0) {
296 printxval(lockfcmds, fl.l_type, "F_???");
297 tprints(", whence=");
298 printxval(whence_codes, fl.l_whence, "SEEK_???");
300 tprintf(", start=%lld, len=%lld", fl.l_start, fl.l_len);
302 tprintf(", start=%ld, len=%ld", fl.l_start, fl.l_len);
305 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
311 sys_fcntl(struct tcb *tcp)
314 printfd(tcp, tcp->u_arg[0]);
316 printxval(fcntlcmds, tcp->u_arg[1], "F_???");
317 switch (tcp->u_arg[1]) {
320 printflags(fdflags, tcp->u_arg[2], "FD_???");
322 case F_SETOWN: case F_DUPFD:
323 #ifdef F_DUPFD_CLOEXEC
324 case F_DUPFD_CLOEXEC:
326 tprintf(", %ld", tcp->u_arg[2]);
330 tprint_open_modes(tcp->u_arg[2]);
332 case F_SETLK: case F_SETLKW:
337 printflock(tcp, tcp->u_arg[2], 0);
343 /* Linux glibc defines SETLK64 as SETLK,
344 even though the kernel has different values - as does Solaris. */
345 #if defined(F_SETLK64) && F_SETLK64 + 0 != F_SETLK
348 #if defined(F_SETLKW64) && F_SETLKW64 + 0 != F_SETLKW
352 printflock64(tcp, tcp->u_arg[2], 0);
358 printflags(notifyflags, tcp->u_arg[2], "DN_???");
364 printxval(lockfcmds, tcp->u_arg[2], "F_???");
370 switch (tcp->u_arg[1]) {
372 #ifdef F_DUPFD_CLOEXEC
373 case F_DUPFD_CLOEXEC:
375 case F_SETFD: case F_SETFL:
376 case F_SETLK: case F_SETLKW:
377 case F_SETOWN: case F_GETOWN:
386 if (syserror(tcp) || tcp->u_rval == 0)
388 tcp->auxstr = sprintflags("flags ", fdflags, tcp->u_rval);
389 return RVAL_HEX|RVAL_STR;
393 tcp->auxstr = sprint_open_modes(tcp->u_rval);
394 return RVAL_HEX|RVAL_STR;
397 printflock(tcp, tcp->u_arg[2], 1);
400 #if defined(F_GETLK64) && F_GETLK64+0 != F_GETLK
404 printflock64(tcp, tcp->u_arg[2], 1);
411 tcp->auxstr = xlookup(lockfcmds, tcp->u_rval);
412 return RVAL_HEX|RVAL_STR;
415 tprintf(", %#lx", tcp->u_arg[2]);
425 sys_flock(struct tcb *tcp)
428 printfd(tcp, tcp->u_arg[0]);
430 printflags(flockcmds, tcp->u_arg[1], "LOCK_???");
437 sys_close(struct tcb *tcp)
440 printfd(tcp, tcp->u_arg[0]);
446 do_dup2(struct tcb *tcp, int flags_arg)
449 printfd(tcp, tcp->u_arg[0]);
451 printfd(tcp, tcp->u_arg[1]);
452 if (flags_arg >= 0) {
454 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
461 sys_dup2(struct tcb *tcp)
463 return do_dup2(tcp, -1);
467 sys_dup3(struct tcb *tcp)
469 return do_dup2(tcp, 2);
474 sys_getdtablesize(struct tcb *tcp)
481 decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
489 /* Kernel truncates arg[0] to int, we do the same. */
490 nfds = (int) args[0];
492 /* Kernel rejects negative nfds, so we don't parse it either. */
497 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
498 if (nfds > 1024*1024)
502 * We had bugs a-la "while (j < args[0])" and "umoven(args[0])" below.
503 * Instead of args[0], use nfds for fd count, fdsize for array lengths.
505 fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
508 tprintf("%d", (int) args[0]);
511 fds = malloc(fdsize);
515 for (i = 0; i < 3; i++) {
521 if (!verbose(tcp) || !fds) {
522 tprintf(", %#lx", arg);
525 if (umoven(tcp, arg, fdsize, (char *) fds) < 0) {
530 for (j = 0, sep = "";; j++) {
531 j = next_set_bit(fds, j, nfds);
542 printtv_bitness(tcp, args[4], bitness, 0);
545 static char outstr[1024];
547 #define end_outstr (outstr + sizeof(outstr))
553 ready_fds = tcp->u_rval;
554 if (ready_fds == 0) {
555 tcp->auxstr = "Timeout";
559 fds = malloc(fdsize);
565 for (i = 0; i < 3 && ready_fds > 0; i++) {
569 if (!arg || umoven(tcp, arg, fdsize, (char *) fds) < 0)
572 j = next_set_bit(fds, j, nfds);
575 /* +2 chars needed at the end: ']',NUL */
576 if (outptr < end_outstr - (sizeof(", except [") + sizeof(int)*3 + 2)) {
578 outptr += sprintf(outptr, "%s%s [%u",
580 i == 0 ? "in" : i == 1 ? "out" : "except",
587 outptr += sprintf(outptr, " %u", j);
590 if (--ready_fds == 0)
593 if (outptr != outstr)
597 /* This contains no useful information on SunOS. */
599 if (outptr < end_outstr - (10 + TIMEVAL_TEXT_BUFSIZE)) {
600 outptr += sprintf(outptr, "%sleft ", sep);
601 outptr = sprinttv(outptr, tcp, args[4], bitness, /*special:*/ 0);
605 tcp->auxstr = outstr;
613 sys_oldselect(struct tcb *tcp)
617 if (umoven(tcp, tcp->u_arg[0], sizeof args, (char *) args) < 0) {
621 return decode_select(tcp, args, BITNESS_CURRENT);
626 sys_osf_select(struct tcb *tcp)
628 long *args = tcp->u_arg;
629 return decode_select(tcp, args, BITNESS_32);
633 static const struct xlat epollctls[] = {
635 { EPOLL_CTL_ADD, "EPOLL_CTL_ADD" },
638 { EPOLL_CTL_MOD, "EPOLL_CTL_MOD" },
641 { EPOLL_CTL_DEL, "EPOLL_CTL_DEL" },
646 static const struct xlat epollevents[] = {
648 { EPOLLIN, "EPOLLIN" },
651 { EPOLLPRI, "EPOLLPRI" },
654 { EPOLLOUT, "EPOLLOUT" },
657 { EPOLLRDNORM, "EPOLLRDNORM" },
660 { EPOLLRDBAND, "EPOLLRDBAND" },
663 { EPOLLWRNORM, "EPOLLWRNORM" },
666 { EPOLLWRBAND, "EPOLLWRBAND" },
669 { EPOLLMSG, "EPOLLMSG" },
672 { EPOLLERR, "EPOLLERR" },
675 { EPOLLHUP, "EPOLLHUP" },
678 { EPOLLRDHUP, "EPOLLRDHUP" },
681 { EPOLLONESHOT, "EPOLLONESHOT" },
684 { EPOLLET, "EPOLLET" },
689 /* Not aliased to printargs_ld: we want it to have a distinct address */
691 sys_epoll_create(struct tcb *tcp)
693 return printargs_ld(tcp);
696 static const struct xlat epollflags[] = {
698 { EPOLL_CLOEXEC, "EPOLL_CLOEXEC" },
700 #ifdef EPOLL_NONBLOCK
701 { EPOLL_NONBLOCK, "EPOLL_NONBLOCK" },
707 sys_epoll_create1(struct tcb *tcp)
710 printflags(epollflags, tcp->u_arg[0], "EPOLL_???");
714 #ifdef HAVE_SYS_EPOLL_H
716 print_epoll_event(struct epoll_event *ev)
719 printflags(epollevents, ev->events, "EPOLL???");
720 /* We cannot know what format the program uses, so print u32 and u64
721 which will cover every value. */
722 tprintf(", {u32=%" PRIu32 ", u64=%" PRIu64 "}}",
723 ev->data.u32, ev->data.u64);
728 sys_epoll_ctl(struct tcb *tcp)
731 printfd(tcp, tcp->u_arg[0]);
733 printxval(epollctls, tcp->u_arg[1], "EPOLL_CTL_???");
735 printfd(tcp, tcp->u_arg[2]);
737 if (tcp->u_arg[3] == 0)
740 #ifdef HAVE_SYS_EPOLL_H
741 struct epoll_event ev;
742 if (umove(tcp, tcp->u_arg[3], &ev) == 0)
743 print_epoll_event(&ev);
753 epoll_wait_common(struct tcb *tcp)
756 printfd(tcp, tcp->u_arg[0]);
760 tprintf("%lx", tcp->u_arg[1]);
761 else if (tcp->u_rval == 0)
764 #ifdef HAVE_SYS_EPOLL_H
765 struct epoll_event ev, *start, *cur, *end;
769 start = (struct epoll_event *) tcp->u_arg[1];
770 end = start + tcp->u_rval;
771 for (cur = start; cur < end; ++cur) {
774 if (umove(tcp, (long) cur, &ev) == 0)
775 print_epoll_event(&ev);
784 tprintf(" %#lx", (long) start);
789 tprintf(", %d, %d", (int) tcp->u_arg[2], (int) tcp->u_arg[3]);
794 sys_epoll_wait(struct tcb *tcp)
796 epoll_wait_common(tcp);
801 sys_epoll_pwait(struct tcb *tcp)
803 epoll_wait_common(tcp);
806 /* NB: kernel requires arg[5] == NSIG / 8 */
807 print_sigset_addr_len(tcp, tcp->u_arg[4], tcp->u_arg[5]);
808 tprintf(", %lu", tcp->u_arg[5]);
814 sys_io_setup(struct tcb *tcp)
817 tprintf("%ld, ", tcp->u_arg[0]);
820 tprintf("0x%0lx", tcp->u_arg[1]);
822 unsigned long user_id;
823 if (umove(tcp, tcp->u_arg[1], &user_id) == 0)
824 tprintf("{%lu}", user_id);
833 sys_io_destroy(struct tcb *tcp)
836 tprintf("%lu", tcp->u_arg[0]);
843 SUB_NONE, SUB_COMMON, SUB_POLL, SUB_VECTOR
847 tprint_lio_opcode(unsigned cmd)
849 static const struct {
853 { "pread", SUB_COMMON },
854 { "pwrite", SUB_COMMON },
855 { "fsync", SUB_NONE },
856 { "fdsync", SUB_NONE },
858 { "poll", SUB_POLL },
859 { "noop", SUB_NONE },
860 { "preadv", SUB_VECTOR },
861 { "pwritev", SUB_VECTOR },
864 if (cmd < ARRAY_SIZE(cmds)) {
865 tprints(cmds[cmd].name);
866 return cmds[cmd].sub;
868 tprintf("%u /* SUB_??? */", cmd);
872 /* Not defined in libaio.h */
874 # define IOCB_RESFD (1 << 0)
878 print_common_flags(struct iocb *iocb)
880 #if HAVE_STRUCT_IOCB_U_C_FLAGS
881 if (iocb->u.c.flags & IOCB_RESFD)
882 tprintf(", resfd=%d", iocb->u.c.resfd);
883 if (iocb->u.c.flags & ~IOCB_RESFD)
884 tprintf(", flags=%x", iocb->u.c.flags);
886 # warning "libaio.h is too old => limited io_submit decoding"
890 #endif /* HAVE_LIBAIO_H */
893 sys_io_submit(struct tcb *tcp)
897 long nr = tcp->u_arg[1];
898 /* if nr <= 0, we end up printing just "{}" */
899 tprintf("%lu, %ld, {", tcp->u_arg[0], tcp->u_arg[1]);
902 struct iocb **iocbs = (void *)tcp->u_arg[2];
904 for (i = 0; i < nr; i++, iocbs++) {
911 if (umove(tcp, (unsigned long)iocbs, &iocbp)) {
912 tprintf("%#lx", (unsigned long)iocbs);
913 /* No point in trying to read iocbs+1 etc */
914 /* (nr can be ridiculously large): */
917 if (umove(tcp, (unsigned long)iocbp, &iocb)) {
918 tprintf("{%#lx}", (unsigned long)iocbp);
923 tprintf("data:%p, ", iocb.data);
925 tprintf("key:%u, ", iocb.key);
926 sub = tprint_lio_opcode(iocb.aio_lio_opcode);
927 if (iocb.aio_reqprio)
928 tprintf(", reqprio:%d", iocb.aio_reqprio);
929 tprintf(", filedes:%d", iocb.aio_fildes);
932 #if HAVE_DECL_IO_CMD_PWRITE
933 if (iocb.aio_lio_opcode == IO_CMD_PWRITE) {
935 printstr(tcp, (unsigned long)iocb.u.c.buf,
939 tprintf(", buf:%p", iocb.u.c.buf);
940 tprintf(", nbytes:%lu, offset:%lld",
943 print_common_flags(&iocb);
946 tprintf(", %lld", iocb.u.v.offset);
947 print_common_flags(&iocb);
949 tprint_iov(tcp, iocb.u.v.nr,
950 (unsigned long)iocb.u.v.vec,
951 #if HAVE_DECL_IO_CMD_PWRITEV
952 iocb.aio_lio_opcode == IO_CMD_PWRITEV
959 tprintf(", %x", iocb.u.poll.events);
969 #warning "libaio.h is not available => no io_submit decoding"
970 tprintf("%lu, %ld, %#lx", tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
977 sys_io_cancel(struct tcb *tcp)
983 tprintf("%lu, ", tcp->u_arg[0]);
985 if (umove(tcp, tcp->u_arg[1], &iocb) == 0) {
986 tprintf("{%p, %u, %u, %u, %d}, ",
988 (unsigned)iocb.aio_lio_opcode,
989 (unsigned)iocb.aio_reqprio, iocb.aio_fildes);
998 struct io_event event;
999 if (umove(tcp, tcp->u_arg[2], &event) == 0)
1000 tprintf("{%p, %p, %ld, %ld}",
1001 event.data, event.obj,
1002 event.res, event.res2);
1012 sys_io_getevents(struct tcb *tcp)
1014 if (entering(tcp)) {
1015 tprintf("%ld, %ld, %ld, ", tcp->u_arg[0], tcp->u_arg[1],
1018 if (tcp->u_rval == 0) {
1021 #ifdef HAVE_LIBAIO_H
1022 struct io_event *events = (void *)tcp->u_arg[3];
1023 long i, nr = tcp->u_rval;
1025 for (i = 0; i < nr; i++, events++) {
1026 struct io_event event;
1033 if (umove(tcp, (unsigned long)events, &event) != 0) {
1037 tprintf("{%p, %p, %ld, %ld}", event.data,
1038 event.obj, event.res, event.res2);
1046 print_timespec(tcp, tcp->u_arg[4]);
1052 sys_select(struct tcb *tcp)
1054 return decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
1058 sys_pselect6(struct tcb *tcp)
1060 int rc = decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
1061 if (entering(tcp)) {
1067 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1068 if (current_wordsize == 4) {
1073 r = umove(tcp, tcp->u_arg[5], &data32);
1074 data.ptr = data32.ptr;
1075 data.len = data32.len;
1078 r = umove(tcp, tcp->u_arg[5], &data);
1080 tprintf(", %#lx", tcp->u_arg[5]);
1083 /* NB: kernel requires data.len == NSIG / 8 */
1084 print_sigset_addr_len(tcp, data.ptr, data.len);
1085 tprintf(", %lu}", data.len);
1092 do_eventfd(struct tcb *tcp, int flags_arg)
1094 if (entering(tcp)) {
1095 tprintf("%lu", tcp->u_arg[0]);
1096 if (flags_arg >= 0) {
1098 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
1105 sys_eventfd(struct tcb *tcp)
1107 return do_eventfd(tcp, -1);
1111 sys_eventfd2(struct tcb *tcp)
1113 return do_eventfd(tcp, 1);
1117 sys_perf_event_open(struct tcb *tcp)
1119 if (entering(tcp)) {
1120 tprintf("%#lx, %d, %d, %d, ",
1122 (int) tcp->u_arg[1],
1123 (int) tcp->u_arg[2],
1124 (int) tcp->u_arg[3]);
1125 printflags(perf_event_open_flags, tcp->u_arg[4],