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>
37 #ifdef HAVE_LINUX_PERF_EVENT_H
38 # include <linux/perf_event.h>
41 static const struct xlat fcntlcmds[] = {
145 #ifdef F_DUPFD_CLOEXEC
146 XLAT(F_DUPFD_CLOEXEC),
151 static const struct xlat fdflags[] = {
160 static const struct xlat flockcmds[] = {
170 static const struct xlat lockfcmds[] = {
184 static const struct xlat notifyflags[] = {
210 static const struct xlat perf_event_open_flags[] = {
211 #ifdef PERF_FLAG_FD_NO_GROUP
212 XLAT(PERF_FLAG_FD_NO_GROUP),
214 #ifdef PERF_FLAG_FD_OUTPUT
215 XLAT(PERF_FLAG_FD_OUTPUT),
217 #ifdef PERF_FLAG_PID_CGROUP
218 XLAT(PERF_FLAG_PID_CGROUP),
223 #if defined(F_SETLK64) && F_SETLK64 + 0 != F_SETLK
224 # define HAVE_F_SETLK64 1
226 # define HAVE_F_SETLK64 0
229 #if defined(F_SETLKW64) && F_SETLKW64 + 0 != F_SETLKW
230 # define HAVE_F_SETLKW64 1
232 # define HAVE_F_SETLKW64 0
235 #if defined(F_GETLK64) && F_GETLK64+0 != F_GETLK
236 # define HAVE_F_GETLK64 1
238 # define HAVE_F_GETLK64 0
241 #if defined(X32) || defined(F_FREESP64) || \
242 HAVE_F_SETLK64 || HAVE_F_SETLKW64 || HAVE_F_GETLK64
244 #ifndef HAVE_STRUCT_FLOCK64
246 short int l_type, l_whence;
247 int64_t l_start, l_len;
254 printflock64(struct tcb *tcp, long addr, int getlk)
258 if (umove(tcp, addr, &fl) < 0) {
263 printxval(lockfcmds, fl.l_type, "F_???");
264 tprints(", whence=");
265 printxval(whence_codes, fl.l_whence, "SEEK_???");
266 tprintf(", start=%lld, len=%lld", (long long) fl.l_start, (long long) fl.l_len);
268 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
276 printflock(struct tcb *tcp, long addr, int getlk)
280 #if SUPPORTED_PERSONALITIES > 1
282 if (current_personality == 0) {
283 printflock64(tcp, addr, getlk);
287 if (current_wordsize != sizeof(fl.l_start)) {
288 if (current_wordsize == 4) {
289 /* 32-bit x86 app on x86_64 and similar cases */
293 int32_t l_start; /* off_t */
294 int32_t l_len; /* off_t */
295 int32_t l_pid; /* pid_t */
297 if (umove(tcp, addr, &fl32) < 0) {
301 fl.l_type = fl32.l_type;
302 fl.l_whence = fl32.l_whence;
303 fl.l_start = fl32.l_start;
304 fl.l_len = fl32.l_len;
305 fl.l_pid = fl32.l_pid;
307 /* let people know we have a problem here */
308 tprintf("<decode error: unsupported wordsize %d>",
315 if (umove(tcp, addr, &fl) < 0) {
321 printxval(lockfcmds, fl.l_type, "F_???");
322 tprints(", whence=");
323 printxval(whence_codes, fl.l_whence, "SEEK_???");
325 tprintf(", start=%lld, len=%lld", fl.l_start, fl.l_len);
327 tprintf(", start=%ld, len=%ld", fl.l_start, fl.l_len);
330 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
336 sys_fcntl(struct tcb *tcp)
339 printfd(tcp, tcp->u_arg[0]);
341 printxval(fcntlcmds, tcp->u_arg[1], "F_???");
342 switch (tcp->u_arg[1]) {
345 printflags(fdflags, tcp->u_arg[2], "FD_???");
347 case F_SETOWN: case F_DUPFD:
348 #ifdef F_DUPFD_CLOEXEC
349 case F_DUPFD_CLOEXEC:
351 tprintf(", %ld", tcp->u_arg[2]);
355 tprint_open_modes(tcp->u_arg[2]);
357 case F_SETLK: case F_SETLKW:
362 printflock(tcp, tcp->u_arg[2], 0);
364 #if defined(F_FREESP64) || HAVE_F_SETLK64 || HAVE_F_SETLKW64
368 /* Linux glibc defines SETLK64 as SETLK,
369 even though the kernel has different values - as does Solaris. */
377 printflock64(tcp, tcp->u_arg[2], 0);
379 #endif /* defined(F_FREESP64) || HAVE_F_SETLK64 || HAVE_F_SETLKW64 */
383 printflags(notifyflags, tcp->u_arg[2], "DN_???");
389 printxval(lockfcmds, tcp->u_arg[2], "F_???");
395 switch (tcp->u_arg[1]) {
397 #ifdef F_DUPFD_CLOEXEC
398 case F_DUPFD_CLOEXEC:
400 case F_SETFD: case F_SETFL:
401 case F_SETLK: case F_SETLKW:
402 case F_SETOWN: case F_GETOWN:
411 if (syserror(tcp) || tcp->u_rval == 0)
413 tcp->auxstr = sprintflags("flags ", fdflags, tcp->u_rval);
414 return RVAL_HEX|RVAL_STR;
418 tcp->auxstr = sprint_open_modes(tcp->u_rval);
419 return RVAL_HEX|RVAL_STR;
422 printflock(tcp, tcp->u_arg[2], 1);
427 printflock64(tcp, tcp->u_arg[2], 1);
434 tcp->auxstr = xlookup(lockfcmds, tcp->u_rval);
435 return RVAL_HEX|RVAL_STR;
438 tprintf(", %#lx", tcp->u_arg[2]);
448 sys_flock(struct tcb *tcp)
451 printfd(tcp, tcp->u_arg[0]);
453 printflags(flockcmds, tcp->u_arg[1], "LOCK_???");
460 sys_close(struct tcb *tcp)
463 printfd(tcp, tcp->u_arg[0]);
469 do_dup2(struct tcb *tcp, int flags_arg)
472 printfd(tcp, tcp->u_arg[0]);
474 printfd(tcp, tcp->u_arg[1]);
475 if (flags_arg >= 0) {
477 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
484 sys_dup2(struct tcb *tcp)
486 return do_dup2(tcp, -1);
490 sys_dup3(struct tcb *tcp)
492 return do_dup2(tcp, 2);
497 sys_getdtablesize(struct tcb *tcp)
504 decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
512 /* Kernel truncates arg[0] to int, we do the same. */
513 nfds = (int) args[0];
515 /* Kernel rejects negative nfds, so we don't parse it either. */
520 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
521 if (nfds > 1024*1024)
525 * We had bugs a-la "while (j < args[0])" and "umoven(args[0])" below.
526 * Instead of args[0], use nfds for fd count, fdsize for array lengths.
528 fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
531 tprintf("%d", (int) args[0]);
534 fds = malloc(fdsize);
538 for (i = 0; i < 3; i++) {
544 if (!verbose(tcp) || !fds) {
545 tprintf(", %#lx", arg);
548 if (umoven(tcp, arg, fdsize, (char *) fds) < 0) {
553 for (j = 0, sep = "";; j++) {
554 j = next_set_bit(fds, j, nfds);
565 printtv_bitness(tcp, args[4], bitness, 0);
568 static char outstr[1024];
570 #define end_outstr (outstr + sizeof(outstr))
576 ready_fds = tcp->u_rval;
577 if (ready_fds == 0) {
578 tcp->auxstr = "Timeout";
582 fds = malloc(fdsize);
588 for (i = 0; i < 3 && ready_fds > 0; i++) {
592 if (!arg || umoven(tcp, arg, fdsize, (char *) fds) < 0)
595 j = next_set_bit(fds, j, nfds);
598 /* +2 chars needed at the end: ']',NUL */
599 if (outptr < end_outstr - (sizeof(", except [") + sizeof(int)*3 + 2)) {
601 outptr += sprintf(outptr, "%s%s [%u",
603 i == 0 ? "in" : i == 1 ? "out" : "except",
610 outptr += sprintf(outptr, " %u", j);
613 if (--ready_fds == 0)
616 if (outptr != outstr)
620 /* This contains no useful information on SunOS. */
622 if (outptr < end_outstr - (10 + TIMEVAL_TEXT_BUFSIZE)) {
623 outptr += sprintf(outptr, "%sleft ", sep);
624 outptr = sprinttv(outptr, tcp, args[4], bitness, /*special:*/ 0);
628 tcp->auxstr = outstr;
636 sys_oldselect(struct tcb *tcp)
640 if (umoven(tcp, tcp->u_arg[0], sizeof args, (char *) args) < 0) {
644 return decode_select(tcp, args, BITNESS_CURRENT);
649 sys_osf_select(struct tcb *tcp)
651 long *args = tcp->u_arg;
652 return decode_select(tcp, args, BITNESS_32);
656 static const struct xlat epollctls[] = {
669 static const struct xlat epollevents[] = {
712 /* Not aliased to printargs_ld: we want it to have a distinct address */
714 sys_epoll_create(struct tcb *tcp)
716 return printargs_ld(tcp);
719 static const struct xlat epollflags[] = {
723 #ifdef EPOLL_NONBLOCK
724 XLAT(EPOLL_NONBLOCK),
730 sys_epoll_create1(struct tcb *tcp)
733 printflags(epollflags, tcp->u_arg[0], "EPOLL_???");
737 #ifdef HAVE_SYS_EPOLL_H
739 print_epoll_event(struct epoll_event *ev)
742 printflags(epollevents, ev->events, "EPOLL???");
743 /* We cannot know what format the program uses, so print u32 and u64
744 which will cover every value. */
745 tprintf(", {u32=%" PRIu32 ", u64=%" PRIu64 "}}",
746 ev->data.u32, ev->data.u64);
751 sys_epoll_ctl(struct tcb *tcp)
754 printfd(tcp, tcp->u_arg[0]);
756 printxval(epollctls, tcp->u_arg[1], "EPOLL_CTL_???");
758 printfd(tcp, tcp->u_arg[2]);
760 if (tcp->u_arg[3] == 0)
763 #ifdef HAVE_SYS_EPOLL_H
764 struct epoll_event ev;
765 if (umove(tcp, tcp->u_arg[3], &ev) == 0)
766 print_epoll_event(&ev);
776 epoll_wait_common(struct tcb *tcp)
779 printfd(tcp, tcp->u_arg[0]);
783 tprintf("%lx", tcp->u_arg[1]);
784 else if (tcp->u_rval == 0)
787 #ifdef HAVE_SYS_EPOLL_H
788 struct epoll_event ev, *start, *cur, *end;
792 start = (struct epoll_event *) tcp->u_arg[1];
793 end = start + tcp->u_rval;
794 for (cur = start; cur < end; ++cur) {
797 if (umove(tcp, (long) cur, &ev) == 0)
798 print_epoll_event(&ev);
807 tprintf(" %#lx", (long) start);
812 tprintf(", %d, %d", (int) tcp->u_arg[2], (int) tcp->u_arg[3]);
817 sys_epoll_wait(struct tcb *tcp)
819 epoll_wait_common(tcp);
824 sys_epoll_pwait(struct tcb *tcp)
826 epoll_wait_common(tcp);
829 /* NB: kernel requires arg[5] == NSIG / 8 */
830 print_sigset_addr_len(tcp, tcp->u_arg[4], tcp->u_arg[5]);
831 tprintf(", %lu", tcp->u_arg[5]);
837 sys_select(struct tcb *tcp)
839 return decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
843 sys_pselect6(struct tcb *tcp)
845 int rc = decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
852 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
853 if (current_wordsize == 4) {
858 r = umove(tcp, tcp->u_arg[5], &data32);
859 data.ptr = data32.ptr;
860 data.len = data32.len;
863 r = umove(tcp, tcp->u_arg[5], &data);
865 tprintf(", %#lx", tcp->u_arg[5]);
868 /* NB: kernel requires data.len == NSIG / 8 */
869 print_sigset_addr_len(tcp, data.ptr, data.len);
870 tprintf(", %lu}", data.len);
877 do_eventfd(struct tcb *tcp, int flags_arg)
880 tprintf("%lu", tcp->u_arg[0]);
881 if (flags_arg >= 0) {
883 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
890 sys_eventfd(struct tcb *tcp)
892 return do_eventfd(tcp, -1);
896 sys_eventfd2(struct tcb *tcp)
898 return do_eventfd(tcp, 1);
902 sys_perf_event_open(struct tcb *tcp)
905 tprintf("%#lx, %d, %d, %d, ",
909 (int) tcp->u_arg[3]);
910 printflags(perf_event_open_flags, tcp->u_arg[4],