]> granicus.if.org Git - strace/blobdiff - desc.c
tests: add a test for rt_sigaction output
[strace] / desc.c
diff --git a/desc.c b/desc.c
index be4d2a91918487b55e60954a2aaaddec1bfe16ac..3ccf422af01cdb4c46084a1a231ca19707143820 100644 (file)
--- a/desc.c
+++ b/desc.c
 #ifdef HAVE_SYS_EPOLL_H
 # include <sys/epoll.h>
 #endif
-#ifdef HAVE_LIBAIO_H
-# include <libaio.h>
-#endif
-
-#if HAVE_LONG_LONG_OFF_T
-/*
- * Hacks for systems that have a long long off_t
- */
-# define flock64       flock           /* Horrid hack */
-# define printflock printflock64       /* Horrider hack */
+#ifdef HAVE_LINUX_PERF_EVENT_H
+# include  <linux/perf_event.h>
 #endif
 
 static const struct xlat fcntlcmds[] = {
@@ -215,14 +207,70 @@ static const struct xlat notifyflags[] = {
 };
 #endif
 
-static const struct xlat whence[] = {
-       { SEEK_SET,     "SEEK_SET"      },
-       { SEEK_CUR,     "SEEK_CUR"      },
-       { SEEK_END,     "SEEK_END"      },
-       { 0,            NULL            },
+static const struct xlat perf_event_open_flags[] = {
+#ifdef PERF_FLAG_FD_NO_GROUP
+       { PERF_FLAG_FD_NO_GROUP,        "PERF_FLAG_FD_NO_GROUP" },
+#endif
+#ifdef PERF_FLAG_FD_OUTPUT
+       { PERF_FLAG_FD_OUTPUT,          "PERF_FLAG_FD_OUTPUT"   },
+#endif
+#ifdef PERF_FLAG_PID_CGROUP
+       { PERF_FLAG_PID_CGROUP,         "PERF_FLAG_PID_CGROUP"  },
+#endif
+       { 0,                            NULL                    },
 };
 
-#ifndef HAVE_LONG_LONG_OFF_T
+#if defined(F_SETLK64) && F_SETLK64 + 0 != F_SETLK
+# define HAVE_F_SETLK64 1
+#else
+# define HAVE_F_SETLK64 0
+#endif
+
+#if defined(F_SETLKW64) && F_SETLKW64 + 0 != F_SETLKW
+# define HAVE_F_SETLKW64 1
+#else
+# define HAVE_F_SETLKW64 0
+#endif
+
+#if defined(F_GETLK64) && F_GETLK64+0 != F_GETLK
+# define HAVE_F_GETLK64 1
+#else
+# define HAVE_F_GETLK64 0
+#endif
+
+#if defined(X32) || defined(F_FREESP64) || \
+    HAVE_F_SETLK64 || HAVE_F_SETLKW64 || HAVE_F_GETLK64
+
+#ifndef HAVE_STRUCT_FLOCK64
+struct flock64 {
+       short int l_type, l_whence;
+       int64_t l_start, l_len;
+       int l_pid;
+};
+#endif
+
+/* fcntl/lockf */
+static void
+printflock64(struct tcb *tcp, long addr, int getlk)
+{
+       struct flock64 fl;
+
+       if (umove(tcp, addr, &fl) < 0) {
+               tprints("{...}");
+               return;
+       }
+       tprints("{type=");
+       printxval(lockfcmds, fl.l_type, "F_???");
+       tprints(", whence=");
+       printxval(whence_codes, fl.l_whence, "SEEK_???");
+       tprintf(", start=%lld, len=%lld", (long long) fl.l_start, (long long) fl.l_len);
+       if (getlk)
+               tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
+       else
+               tprints("}");
+}
+#endif
+
 /* fcntl/lockf */
 static void
 printflock(struct tcb *tcp, long addr, int getlk)
@@ -230,8 +278,14 @@ printflock(struct tcb *tcp, long addr, int getlk)
        struct flock fl;
 
 #if SUPPORTED_PERSONALITIES > 1
-       if (personality_wordsize[current_personality] != sizeof(fl.l_start)) {
-               if (personality_wordsize[current_personality] == 4) {
+# ifdef X32
+       if (current_personality == 0) {
+               printflock64(tcp, addr, getlk);
+               return;
+       }
+# endif
+       if (current_wordsize != sizeof(fl.l_start)) {
+               if (current_wordsize == 4) {
                        /* 32-bit x86 app on x86_64 and similar cases */
                        struct {
                                short int l_type;
@@ -251,8 +305,8 @@ printflock(struct tcb *tcp, long addr, int getlk)
                        fl.l_pid = fl32.l_pid;
                } else {
                        /* let people know we have a problem here */
-                       tprintf("{ <decode error: unsupported wordsize %d> }",
-                               personality_wordsize[current_personality]);
+                       tprintf("<decode error: unsupported wordsize %d>",
+                               current_wordsize);
                        return;
                }
        } else
@@ -266,37 +320,17 @@ printflock(struct tcb *tcp, long addr, int getlk)
        tprints("{type=");
        printxval(lockfcmds, fl.l_type, "F_???");
        tprints(", whence=");
-       printxval(whence, fl.l_whence, "SEEK_???");
+       printxval(whence_codes, fl.l_whence, "SEEK_???");
+#ifdef X32
+       tprintf(", start=%lld, len=%lld", fl.l_start, fl.l_len);
+#else
        tprintf(", start=%ld, len=%ld", fl.l_start, fl.l_len);
-       if (getlk)
-               tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
-       else
-               tprints("}");
-}
 #endif
-
-#if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
-/* fcntl/lockf */
-static void
-printflock64(struct tcb *tcp, long addr, int getlk)
-{
-       struct flock64 fl;
-
-       if (umove(tcp, addr, &fl) < 0) {
-               tprints("{...}");
-               return;
-       }
-       tprints("{type=");
-       printxval(lockfcmds, fl.l_type, "F_???");
-       tprints(", whence=");
-       printxval(whence, fl.l_whence, "SEEK_???");
-       tprintf(", start=%lld, len=%lld", (long long) fl.l_start, (long long) fl.l_len);
        if (getlk)
                tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
        else
                tprints("}");
 }
-#endif
 
 int
 sys_fcntl(struct tcb *tcp)
@@ -327,22 +361,22 @@ sys_fcntl(struct tcb *tcp)
                        tprints(", ");
                        printflock(tcp, tcp->u_arg[2], 0);
                        break;
-#if _LFS64_LARGEFILE
+#if defined(F_FREESP64) || HAVE_F_SETLK64 || HAVE_F_SETLKW64
 #ifdef F_FREESP64
                case F_FREESP64:
 #endif
                /* Linux glibc defines SETLK64 as SETLK,
                   even though the kernel has different values - as does Solaris. */
-#if defined(F_SETLK64) && F_SETLK64 + 0 != F_SETLK
+#if HAVE_F_SETLK64
                case F_SETLK64:
 #endif
-#if defined(F_SETLKW64) && F_SETLKW64 + 0 != F_SETLKW
+#if HAVE_F_SETLKW64
                case F_SETLKW64:
 #endif
                        tprints(", ");
                        printflock64(tcp, tcp->u_arg[2], 0);
                        break;
-#endif
+#endif /* defined(F_FREESP64) || HAVE_F_SETLK64 || HAVE_F_SETLKW64 */
 #ifdef F_NOTIFY
                case F_NOTIFY:
                        tprints(", ");
@@ -387,10 +421,8 @@ sys_fcntl(struct tcb *tcp)
                        tprints(", ");
                        printflock(tcp, tcp->u_arg[2], 1);
                        break;
-#if _LFS64_LARGEFILE
-#if defined(F_GETLK64) && F_GETLK64+0 != F_GETLK
+#if HAVE_F_GETLK64
                case F_GETLK64:
-#endif
                        tprints(", ");
                        printflock64(tcp, tcp->u_arg[2], 1);
                        break;
@@ -472,32 +504,44 @@ static int
 decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
 {
        int i, j;
-       unsigned nfds, fdsize;
+       int nfds, fdsize;
        fd_set *fds;
        const char *sep;
        long arg;
 
-       fdsize = args[0];
+       /* Kernel truncates arg[0] to int, we do the same. */
+       nfds = (int) args[0];
+
+       /* Kernel rejects negative nfds, so we don't parse it either. */
+       if (nfds < 0) {
+               nfds = 0;
+               fds = NULL;
+       }
        /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
-       if (args[0] > 1024*1024)
-               fdsize = 1024*1024;
-       if (args[0] < 0)
-               fdsize = 0;
-       fdsize = (((fdsize + 7) / 8) + sizeof(long)-1) & -sizeof(long);
+       if (nfds > 1024*1024)
+               nfds = 1024*1024;
+
+       /*
+        * We had bugs a-la "while (j < args[0])" and "umoven(args[0])" below.
+        * Instead of args[0], use nfds for fd count, fdsize for array lengths.
+        */
+       fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
 
        if (entering(tcp)) {
-               fds = malloc(fdsize);
-               if (!fds)
-                       die_out_of_memory();
-               nfds = args[0];
-               tprintf("%d", nfds);
+               tprintf("%d", (int) args[0]);
+
+               if (fdsize > 0) {
+                       fds = malloc(fdsize);
+                       if (!fds)
+                               die_out_of_memory();
+               }
                for (i = 0; i < 3; i++) {
                        arg = args[i+1];
                        if (arg == 0) {
                                tprints(", NULL");
                                continue;
                        }
-                       if (!verbose(tcp)) {
+                       if (!verbose(tcp) || !fds) {
                                tprintf(", %#lx", arg);
                                continue;
                        }
@@ -506,12 +550,13 @@ decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
                                continue;
                        }
                        tprints(", [");
-                       for (j = 0, sep = ""; j < nfds; j++) {
-                               if (FD_ISSET(j, fds)) {
-                                       tprints(sep);
-                                       printfd(tcp, j);
-                                       sep = " ";
-                               }
+                       for (j = 0, sep = "";; j++) {
+                               j = next_set_bit(fds, j, nfds);
+                               if (j < 0)
+                                       break;
+                               tprints(sep);
+                               printfd(tcp, j);
+                               sep = " ";
                        }
                        tprints("]");
                }
@@ -523,13 +568,13 @@ decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
                static char outstr[1024];
                char *outptr;
 #define end_outstr (outstr + sizeof(outstr))
-               const char *sep;
+               int ready_fds;
 
                if (syserror(tcp))
                        return 0;
 
-               nfds = tcp->u_rval;
-               if (nfds == 0) {
+               ready_fds = tcp->u_rval;
+               if (ready_fds == 0) {
                        tcp->auxstr = "Timeout";
                        return RVAL_STR;
                }
@@ -540,36 +585,36 @@ decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
 
                outptr = outstr;
                sep = "";
-               for (i = 0; i < 3; i++) {
+               for (i = 0; i < 3 && ready_fds > 0; i++) {
                        int first = 1;
 
                        arg = args[i+1];
                        if (!arg || umoven(tcp, arg, fdsize, (char *) fds) < 0)
                                continue;
-                       for (j = 0; j < args[0]; j++) {
-                               if (FD_ISSET(j, fds)) {
-                                       /* +2 chars needed at the end: ']',NUL */
-                                       if (outptr < end_outstr - (sizeof(", except [") + sizeof(int)*3 + 2)) {
-                                               if (first) {
-                                                       outptr += sprintf(outptr, "%s%s [%u",
-                                                               sep,
-                                                               i == 0 ? "in" : i == 1 ? "out" : "except",
-                                                               j
-                                                       );
-                                                       first = 0;
-                                                       sep = ", ";
-                                               }
-                                               else {
-                                                       outptr += sprintf(outptr, " %u", j);
-                                               }
+                       for (j = 0;; j++) {
+                               j = next_set_bit(fds, j, nfds);
+                               if (j < 0)
+                                       break;
+                               /* +2 chars needed at the end: ']',NUL */
+                               if (outptr < end_outstr - (sizeof(", except [") + sizeof(int)*3 + 2)) {
+                                       if (first) {
+                                               outptr += sprintf(outptr, "%s%s [%u",
+                                                       sep,
+                                                       i == 0 ? "in" : i == 1 ? "out" : "except",
+                                                       j
+                                               );
+                                               first = 0;
+                                               sep = ", ";
+                                       }
+                                       else {
+                                               outptr += sprintf(outptr, " %u", j);
                                        }
-                                       nfds--;
                                }
+                               if (--ready_fds == 0)
+                                       break;
                        }
                        if (outptr != outstr)
                                *outptr++ = ']';
-                       if (nfds == 0)
-                               break;
                }
                free(fds);
                /* This contains no useful information on SunOS.  */
@@ -781,236 +826,9 @@ sys_epoll_pwait(struct tcb *tcp)
        epoll_wait_common(tcp);
        if (exiting(tcp)) {
                tprints(", ");
-               print_sigset(tcp, tcp->u_arg[4], 0);
-       }
-       return 0;
-}
-
-int
-sys_io_setup(struct tcb *tcp)
-{
-       if (entering(tcp))
-               tprintf("%ld, ", tcp->u_arg[0]);
-       else {
-               if (syserror(tcp))
-                       tprintf("0x%0lx", tcp->u_arg[1]);
-               else {
-                       unsigned long user_id;
-                       if (umove(tcp, tcp->u_arg[1], &user_id) == 0)
-                               tprintf("{%lu}", user_id);
-                       else
-                               tprints("{...}");
-               }
-       }
-       return 0;
-}
-
-int
-sys_io_destroy(struct tcb *tcp)
-{
-       if (entering(tcp))
-               tprintf("%lu", tcp->u_arg[0]);
-       return 0;
-}
-
-#ifdef HAVE_LIBAIO_H
-
-enum iocb_sub {
-       SUB_NONE, SUB_COMMON, SUB_POLL, SUB_VECTOR
-};
-
-static const char *
-iocb_cmd_lookup(unsigned cmd, enum iocb_sub *sub)
-{
-       static char buf[sizeof("%u /* SUB_??? */") + sizeof(int)*3];
-       static const struct {
-               const char *name;
-               enum iocb_sub sub;
-       } cmds[] = {
-               { "pread", SUB_COMMON },
-               { "pwrite", SUB_COMMON },
-               { "fsync", SUB_NONE },
-               { "fdsync", SUB_NONE },
-               { "op4", SUB_NONE },
-               { "poll", SUB_POLL },
-               { "noop", SUB_NONE },
-               { "preadv", SUB_VECTOR },
-               { "pwritev", SUB_VECTOR },
-       };
-
-       if (cmd < ARRAY_SIZE(cmds)) {
-               *sub = cmds[cmd].sub;
-               return cmds[cmd].name;
-       }
-       *sub = SUB_NONE;
-       sprintf(buf, "%u /* SUB_??? */", cmd);
-       return buf;
-}
-
-/* Not defined in libaio.h */
-#ifndef IOCB_RESFD
-# define IOCB_RESFD (1 << 0)
-#endif
-
-static void
-print_common_flags(struct iocb *iocb)
-{
-       if (iocb->u.c.flags & IOCB_RESFD)
-               tprintf("resfd=%d, ", iocb->u.c.resfd);
-       if (iocb->u.c.flags & ~IOCB_RESFD)
-               tprintf("flags=%x, ", iocb->u.c.flags);
-}
-
-#endif /* HAVE_LIBAIO_H */
-
-int
-sys_io_submit(struct tcb *tcp)
-{
-       long nr;
-       if (entering(tcp)) {
-               tprintf("%lu, %ld, ", tcp->u_arg[0], tcp->u_arg[1]);
-               nr = tcp->u_arg[1];
-               /* and if nr is negative? */
-               if (nr == 0)
-                       tprints("{}");
-               else {
-#ifdef HAVE_LIBAIO_H
-                       long i;
-                       struct iocb *iocbp, **iocbs = (void *)tcp->u_arg[2];
-
-                       for (i = 0; i < nr; i++, iocbs++) {
-                               enum iocb_sub sub;
-                               struct iocb iocb;
-                               if (i == 0)
-                                       tprints("{");
-                               else
-                                       tprints(", ");
-
-                               if (umove(tcp, (unsigned long)iocbs, &iocbp) ||
-                                   umove(tcp, (unsigned long)iocbp, &iocb)) {
-                                       tprints("{...}");
-                                       continue;
-                               }
-                               tprints("{");
-                               if (iocb.data)
-                                       tprintf("data:%p, ", iocb.data);
-                               if (iocb.key)
-                                       tprintf("key:%u, ", iocb.key);
-                               tprintf("%s, ", iocb_cmd_lookup(iocb.aio_lio_opcode, &sub));
-                               if (iocb.aio_reqprio)
-                                       tprintf("reqprio:%d, ", iocb.aio_reqprio);
-                               tprintf("filedes:%d", iocb.aio_fildes);
-                               switch (sub) {
-                               case SUB_COMMON:
-                                       if (iocb.aio_lio_opcode == IO_CMD_PWRITE) {
-                                               tprints(", str:");
-                                               printstr(tcp, (unsigned long)iocb.u.c.buf,
-                                                        iocb.u.c.nbytes);
-                                       } else {
-                                               tprintf(", buf:%p", iocb.u.c.buf);
-                                       }
-                                       tprintf(", nbytes:%lu, offset:%llx",
-                                               iocb.u.c.nbytes,
-                                               iocb.u.c.offset);
-                                       print_common_flags(&iocb);
-                                       break;
-                               case SUB_VECTOR:
-                                       tprintf(", %llx, ", iocb.u.v.offset);
-                                       print_common_flags(&iocb);
-                                       tprint_iov(tcp, iocb.u.v.nr,
-                                                  (unsigned long)iocb.u.v.vec,
-                                                  iocb.aio_lio_opcode == IO_CMD_PWRITEV);
-                                       break;
-                               case SUB_POLL:
-                                       tprintf(", %x", iocb.u.poll.events);
-                                       break;
-                               case SUB_NONE:
-                                       break;
-                               }
-                               tprints("}");
-                       }
-                       if (i)
-                               tprints("}");
-#else
-#warning "libaio-devel is not available => no io_submit decoding"
-                       tprintf("%#lx", tcp->u_arg[2]);
-#endif
-               }
-       }
-       return 0;
-}
-
-int
-sys_io_cancel(struct tcb *tcp)
-{
-       if (entering(tcp)) {
-#ifdef HAVE_LIBAIO_H
-               struct iocb iocb;
-#endif
-               tprintf("%lu, ", tcp->u_arg[0]);
-#ifdef HAVE_LIBAIO_H
-               if (umove(tcp, tcp->u_arg[1], &iocb) == 0) {
-                       tprintf("{%p, %u, %hu, %hu, %d}, ",
-                               iocb.data, iocb.key,
-                               iocb.aio_lio_opcode,
-                               iocb.aio_reqprio, iocb.aio_fildes);
-               } else
-#endif
-                       tprints("{...}, ");
-       } else {
-               if (tcp->u_rval < 0)
-                       tprints("{...}");
-               else {
-#ifdef HAVE_LIBAIO_H
-                       struct io_event event;
-                       if (umove(tcp, tcp->u_arg[2], &event) == 0)
-                               tprintf("{%p, %p, %ld, %ld}",
-                                       event.data, event.obj,
-                                       event.res, event.res2);
-                       else
-#endif
-                               tprints("{...}");
-               }
-       }
-       return 0;
-}
-
-int
-sys_io_getevents(struct tcb *tcp)
-{
-       if (entering(tcp)) {
-               tprintf("%ld, %ld, %ld, ", tcp->u_arg[0], tcp->u_arg[1],
-                       tcp->u_arg[2]);
-       } else {
-               if (tcp->u_rval == 0) {
-                       tprints("{}");
-               } else {
-#ifdef HAVE_LIBAIO_H
-                       struct io_event *events = (void *)tcp->u_arg[3];
-                       long i, nr = tcp->u_rval;
-
-                       for (i = 0; i < nr; i++, events++) {
-                               struct io_event event;
-
-                               if (i == 0)
-                                       tprints("{");
-                               else
-                                       tprints(", ");
-
-                               if (umove(tcp, (unsigned long)events, &event) != 0) {
-                                       tprints("{...}");
-                                       continue;
-                               }
-                               tprintf("{%p, %p, %ld, %ld}", event.data,
-                                       event.obj, event.res, event.res2);
-                       }
-                       tprints("}, ");
-#else
-                       tprints("{...}");
-#endif
-               }
-
-               print_timespec(tcp, tcp->u_arg[4]);
+               /* NB: kernel requires arg[5] == NSIG / 8 */
+               print_sigset_addr_len(tcp, tcp->u_arg[4], tcp->u_arg[5]);
+               tprintf(", %lu", tcp->u_arg[5]);
        }
        return 0;
 }
@@ -1026,18 +844,29 @@ sys_pselect6(struct tcb *tcp)
 {
        int rc = decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
        if (entering(tcp)) {
+               long r;
                struct {
-                       void *ss;
+                       unsigned long ptr;
                        unsigned long len;
                } data;
-               if (umove(tcp, tcp->u_arg[5], &data) < 0)
+#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
+               if (current_wordsize == 4) {
+                       struct {
+                               uint32_t ptr;
+                               uint32_t len;
+                       } data32;
+                       r = umove(tcp, tcp->u_arg[5], &data32);
+                       data.ptr = data32.ptr;
+                       data.len = data32.len;
+               } else
+#endif
+                       r = umove(tcp, tcp->u_arg[5], &data);
+               if (r < 0)
                        tprintf(", %#lx", tcp->u_arg[5]);
                else {
                        tprints(", {");
-                       if (data.len < sizeof(long))
-                               tprintf("%#lx", (long)data.ss);
-                       else
-                               print_sigset(tcp, (long)data.ss, 0);
+                       /* NB: kernel requires data.len == NSIG / 8 */
+                       print_sigset_addr_len(tcp, data.ptr, data.len);
                        tprintf(", %lu}", data.len);
                }
        }
@@ -1068,3 +897,18 @@ sys_eventfd2(struct tcb *tcp)
 {
        return do_eventfd(tcp, 1);
 }
+
+int
+sys_perf_event_open(struct tcb *tcp)
+{
+       if (entering(tcp)) {
+               tprintf("%#lx, %d, %d, %d, ",
+                       tcp->u_arg[0],
+                       (int) tcp->u_arg[1],
+                       (int) tcp->u_arg[2],
+                       (int) tcp->u_arg[3]);
+               printflags(perf_event_open_flags, tcp->u_arg[4],
+                          "PERF_FLAG_???");
+       }
+       return 0;
+}