]> granicus.if.org Git - strace/blobdiff - io.c
Fix decoding and dumping of readv syscall in case of short read
[strace] / io.c
diff --git a/io.c b/io.c
index 4e00fe4e04617b5493ae1ee9ad8fc121f2616717..b98bc8a13c03bf5665b964662c0978dfef4687bf 100644 (file)
--- a/io.c
+++ b/io.c
@@ -2,6 +2,7 @@
  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
+ * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- *     $Id$
  */
 
 #include "defs.h"
-
 #include <fcntl.h>
 #include <sys/uio.h>
 
-int
-sys_read(tcp)
-struct tcb *tcp;
+SYS_FUNC(read)
 {
        if (entering(tcp)) {
-               tprintf("%ld, ", tcp->u_arg[0]);
+               printfd(tcp, tcp->u_arg[0]);
+               tprints(", ");
        } else {
                if (syserror(tcp))
-                       tprintf("%#lx", tcp->u_arg[1]);
+                       printaddr(tcp->u_arg[1]);
                else
                        printstr(tcp, tcp->u_arg[1], tcp->u_rval);
                tprintf(", %lu", tcp->u_arg[2]);
@@ -50,179 +47,241 @@ struct tcb *tcp;
        return 0;
 }
 
-int
-sys_write(tcp)
-struct tcb *tcp;
+SYS_FUNC(write)
 {
-       if (entering(tcp)) {
-               tprintf("%ld, ", tcp->u_arg[0]);
-               printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
-               tprintf(", %lu", tcp->u_arg[2]);
-       }
-       return 0;
+       printfd(tcp, tcp->u_arg[0]);
+       tprints(", ");
+       printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
+       tprintf(", %lu", tcp->u_arg[2]);
+
+       return RVAL_DECODED;
 }
 
-int
-sys_readv(tcp)
-struct tcb *tcp;
+/*
+ * data_size limits the cumulative size of printed data.
+ * Example: recvmsg returing a short read.
+ */
+void
+tprint_iov_upto(struct tcb *tcp, unsigned long len, unsigned long addr, int decode_iov, unsigned long data_size)
 {
-       struct iovec *iov;
-       int i, len;
+       unsigned long iov[2];
+       unsigned long size, cur, end, abbrev_end;
+       const unsigned long sizeof_iov = current_wordsize * 2;
 
-       if (entering(tcp)) {
-               tprintf("%ld, ", tcp->u_arg[0]);
+       if (!len) {
+               tprints("[]");
+               return;
+       }
+       size = len * sizeof_iov;
+       end = addr + size;
+       if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
+           !addr || size / sizeof_iov != len || end < addr) {
+               printaddr(addr);
+               return;
+       }
+       if (abbrev(tcp)) {
+               abbrev_end = addr + max_strlen * sizeof_iov;
+               if (abbrev_end < addr)
+                       abbrev_end = end;
        } else {
-               if (syserror(tcp)) {
-                       tprintf("%#lx, %lu",
-                                       tcp->u_arg[1], tcp->u_arg[2]);
-                       return 0;
-               }
-               len = tcp->u_arg[2];
-               if ((iov = (struct iovec *) malloc(len * sizeof *iov)) == NULL) {
-                       fprintf(stderr, "No memory");
-                       return 0;
-               }
-               if (umoven(tcp, tcp->u_arg[1],
-                               len * sizeof *iov, (char *) iov) < 0) {
-                       tprintf("%#lx", tcp->u_arg[1]);
-               } else {
-                       tprintf("[");
-                       for (i = 0; i < len; i++) {
-                               if (i)
-                                       tprintf(", ");
-                               tprintf("{");
-                               printstr(tcp, (long) iov[i].iov_base,
-                                       iov[i].iov_len);
-                               tprintf(", %u}", iov[i].iov_len);
-                       }
-                       tprintf("]");
+               abbrev_end = end;
+       }
+       tprints("[");
+       for (cur = addr; cur < end; cur += sizeof_iov) {
+               if (cur > addr)
+                       tprints(", ");
+               if (cur >= abbrev_end) {
+                       tprints("...");
+                       break;
                }
-               free((char *) iov);
-               tprintf(", %lu", tcp->u_arg[2]);
+               if (umove_ulong_array_or_printaddr(tcp, cur, iov,
+                                                  ARRAY_SIZE(iov)))
+                       break;
+               tprints("{");
+               if (decode_iov) {
+                       unsigned long len = iov[1];
+                       if (len > data_size)
+                               len = data_size;
+                       data_size -= len;
+                       printstr(tcp, iov[0], len);
+               } else
+                       printaddr(iov[0]);
+               tprintf(", %lu}", iov[1]);
        }
-       return 0;
+       tprints("]");
 }
 
-int
-sys_writev(tcp)
-struct tcb *tcp;
+void
+tprint_iov(struct tcb *tcp, unsigned long len, unsigned long addr, int decode_iov)
 {
-       struct iovec *iov;
-       int i, len;
+       tprint_iov_upto(tcp, len, addr, decode_iov, (unsigned long) -1L);
+}
 
+SYS_FUNC(readv)
+{
        if (entering(tcp)) {
-               tprintf("%ld, ", tcp->u_arg[0]);
-               len = tcp->u_arg[2];
-               iov = (struct iovec *) malloc(len * sizeof *iov);
-               if (iov == NULL) {
-                       fprintf(stderr, "No memory");
-                       return 0;
-               }
-               if (umoven(tcp, tcp->u_arg[1],
-                               len * sizeof *iov, (char *) iov) < 0) {
-                       tprintf("%#lx", tcp->u_arg[1]);
-               } else {
-                       tprintf("[");
-                       for (i = 0; i < len; i++) {
-                               if (i)
-                                       tprintf(", ");
-                               tprintf("{");
-                               printstr(tcp, (long) iov[i].iov_base,
-                                       iov[i].iov_len);
-                               tprintf(", %u}", iov[i].iov_len);
-                       }
-                       tprintf("]");
-               }
-               free((char *) iov);
+               printfd(tcp, tcp->u_arg[0]);
+               tprints(", ");
+       } else {
+               tprint_iov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], 1,
+                               tcp->u_rval);
                tprintf(", %lu", tcp->u_arg[2]);
        }
        return 0;
 }
 
-#ifdef SVR4
+SYS_FUNC(writev)
+{
+       printfd(tcp, tcp->u_arg[0]);
+       tprints(", ");
+       tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
+       tprintf(", %lu", tcp->u_arg[2]);
+
+       return RVAL_DECODED;
+}
+
+/* The SH4 ABI does allow long longs in odd-numbered registers, but
+   does not allow them to be split between registers and memory - and
+   there are only four argument registers for normal functions.  As a
+   result pread takes an extra padding argument before the offset.  This
+   was changed late in the 2.4 series (around 2.4.20).  */
+#if defined(SH)
+#define PREAD_OFFSET_ARG 4
+#else
+#define PREAD_OFFSET_ARG 3
+#endif
 
-int
-sys_pread(tcp)
-struct tcb *tcp;
+SYS_FUNC(pread)
 {
        if (entering(tcp)) {
-               tprintf("%ld, ", tcp->u_arg[0]);
+               printfd(tcp, tcp->u_arg[0]);
+               tprints(", ");
        } else {
                if (syserror(tcp))
-                       tprintf("%#lx", tcp->u_arg[1]);
+                       printaddr(tcp->u_arg[1]);
                else
                        printstr(tcp, tcp->u_arg[1], tcp->u_rval);
-               tprintf(", %lu, %llu", tcp->u_arg[2],
-                               (((unsigned long long) tcp->u_arg[4]) << 32
-                                | tcp->u_arg[3]));
+               tprintf(", %lu, ", tcp->u_arg[2]);
+               printllval(tcp, "%llu", PREAD_OFFSET_ARG);
        }
        return 0;
 }
 
-int
-sys_pwrite(tcp)
-struct tcb *tcp;
+SYS_FUNC(pwrite)
 {
-       if (entering(tcp)) {
-               tprintf("%ld, ", tcp->u_arg[0]);
-               printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
-               tprintf(", %lu, %llu", tcp->u_arg[2],
-                               (((unsigned long long) tcp->u_arg[4]) << 32
-                                | tcp->u_arg[3]));
-       }
-       return 0;
+       printfd(tcp, tcp->u_arg[0]);
+       tprints(", ");
+       printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
+       tprintf(", %lu, ", tcp->u_arg[2]);
+       printllval(tcp, "%llu", PREAD_OFFSET_ARG);
+
+       return RVAL_DECODED;
 }
-#endif /* SVR4 */
 
-#ifdef LINUX
-int
-sys_pread(tcp)
-struct tcb *tcp;
+static void
+print_llu_from_low_high_val(struct tcb *tcp, int arg)
+{
+#if SIZEOF_LONG == SIZEOF_LONG_LONG
+# if SUPPORTED_PERSONALITIES > 1
+#  ifdef X86_64
+       if (current_personality != 1)
+#  else
+       if (current_wordsize == sizeof(long))
+#  endif
+# endif
+               tprintf("%lu", (unsigned long) tcp->u_arg[arg]);
+# if SUPPORTED_PERSONALITIES > 1
+       else
+               tprintf("%lu",
+                       ((unsigned long) tcp->u_arg[arg + 1] << current_wordsize * 8)
+                       | (unsigned long) tcp->u_arg[arg]);
+# endif
+#else
+# ifdef X32
+       if (current_personality == 0)
+               tprintf("%llu", (unsigned long long) tcp->ext_arg[arg]);
+       else
+# endif
+       tprintf("%llu",
+               ((unsigned long long) (unsigned long) tcp->u_arg[arg + 1] << sizeof(long) * 8)
+               | (unsigned long long) (unsigned long) tcp->u_arg[arg]);
+#endif
+}
+
+SYS_FUNC(preadv)
 {
        if (entering(tcp)) {
-               tprintf("%ld, ", tcp->u_arg[0]);
+               printfd(tcp, tcp->u_arg[0]);
+               tprints(", ");
        } else {
-               if (syserror(tcp))
-                       tprintf("%#lx", tcp->u_arg[1]);
-               else
-                       printstr(tcp, tcp->u_arg[1], tcp->u_rval);
-               tprintf(", %lu, %lu", tcp->u_arg[2], tcp->u_arg[3]);
+               tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
+               tprintf(", %lu, ", tcp->u_arg[2]);
+               print_llu_from_low_high_val(tcp, 3);
        }
        return 0;
 }
 
-int
-sys_pwrite(tcp)
-struct tcb *tcp;
+SYS_FUNC(pwritev)
 {
-       if (entering(tcp)) {
-               tprintf("%ld, ", tcp->u_arg[0]);
-               printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
-               tprintf(", %lu, %lu", tcp->u_arg[2], tcp->u_arg[3]);
-       }
-       return 0;
+       printfd(tcp, tcp->u_arg[0]);
+       tprints(", ");
+       tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
+       tprintf(", %lu, ", tcp->u_arg[2]);
+       print_llu_from_low_high_val(tcp, 3);
+
+       return RVAL_DECODED;
 }
 
-#endif /* LINUX */
+#include "xlat/splice_flags.h"
 
-int
-sys_ioctl(tcp)
-struct tcb *tcp;
+SYS_FUNC(tee)
 {
-       char *symbol;
+       /* int fd_in */
+       printfd(tcp, tcp->u_arg[0]);
+       tprints(", ");
+       /* int fd_out */
+       printfd(tcp, tcp->u_arg[1]);
+       tprints(", ");
+       /* size_t len */
+       tprintf("%lu, ", tcp->u_arg[2]);
+       /* unsigned int flags */
+       printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
 
-       if (entering(tcp)) {
-               tprintf("%ld, ", tcp->u_arg[0]);
-               symbol = ioctl_lookup(tcp->u_arg[1]);
-               if (symbol)
-                       tprintf("%s", symbol);
-               else
-                       tprintf("%#lx", tcp->u_arg[1]);
-               ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
-       }
-       else {
-               if (ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]) == 0)
-                       tprintf(", %#lx", tcp->u_arg[2]);
-       }
-       return 0;
+       return RVAL_DECODED;
+}
+
+SYS_FUNC(splice)
+{
+       /* int fd_in */
+       printfd(tcp, tcp->u_arg[0]);
+       tprints(", ");
+       /* loff_t *off_in */
+       printnum_int64(tcp, tcp->u_arg[1], "%" PRIu64);
+       tprints(", ");
+       /* int fd_out */
+       printfd(tcp, tcp->u_arg[2]);
+       tprints(", ");
+       /* loff_t *off_out */
+       printnum_int64(tcp, tcp->u_arg[3], "%" PRIu64);
+       tprints(", ");
+       /* size_t len */
+       tprintf("%lu, ", tcp->u_arg[4]);
+       /* unsigned int flags */
+       printflags(splice_flags, tcp->u_arg[5], "SPLICE_F_???");
+
+       return RVAL_DECODED;
+}
+
+SYS_FUNC(vmsplice)
+{
+       /* int fd */
+       printfd(tcp, tcp->u_arg[0]);
+       tprints(", ");
+       /* const struct iovec *iov, unsigned long nr_segs */
+       tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
+       tprintf(", %lu, ", tcp->u_arg[2]);
+       /* unsigned int flags */
+       printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
+
+       return RVAL_DECODED;
 }