]> granicus.if.org Git - strace/commitdiff
io.c: use print_array function
authorDmitry V. Levin <ldv@altlinux.org>
Sat, 7 May 2016 22:54:04 +0000 (22:54 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Sat, 7 May 2016 23:32:29 +0000 (23:32 +0000)
* io.c (print_iovec_config): New structure.
(print_iovec): New function.
(tprint_iov_upto): Use print_array.
* tests/preadv.c (main): Update.
* tests/pwritev.c (print_iovec, main): Likewise.

io.c
tests/preadv.c
tests/pwritev.c

diff --git a/io.c b/io.c
index ce787cd25fd49a13dc363dd6218169d059080b52..a52904cfc8074881eadbfa9f3d10a48ce9ef9199 100644 (file)
--- a/io.c
+++ b/io.c
@@ -57,65 +57,57 @@ SYS_FUNC(write)
        return RVAL_DECODED;
 }
 
+struct print_iovec_config {
+       int decode_iov;
+       unsigned long data_size;
+};
+
+static bool
+print_iovec(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
+{
+       const unsigned long *iov;
+       unsigned long iov_buf[2];
+       struct print_iovec_config *c = data;
+
+        if (elem_size < sizeof(iov_buf)) {
+               iov_buf[0] = ((unsigned int *) elem_buf)[0];
+               iov_buf[1] = ((unsigned int *) elem_buf)[1];
+               iov = iov_buf;
+       } else {
+               iov = elem_buf;
+       }
+
+       tprints("{");
+
+       if (c->decode_iov) {
+               unsigned long len = iov[1];
+               if (len > c->data_size)
+                       len = c->data_size;
+               c->data_size -= len;
+               printstr(tcp, iov[0], len);
+       } else {
+               printaddr(iov[0]);
+       }
+
+       tprintf(", %lu}", iov[1]);
+
+       return true;
+}
+
 /*
  * 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)
+tprint_iov_upto(struct tcb *tcp, unsigned long len, unsigned long addr,
+               int decode_iov, unsigned long data_size)
 {
        unsigned long iov[2];
-       unsigned long size, cur, end, abbrev_end;
-       const unsigned long sizeof_iov = current_wordsize * 2;
+       struct print_iovec_config config =
+               { .decode_iov = decode_iov, .data_size = data_size };
 
-       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 {
-               abbrev_end = end;
-       }
-       if (addr >= abbrev_end) {
-               tprints("[...]");
-               return;
-       }
-       for (cur = addr; cur < end; cur += sizeof_iov) {
-               if (cur > addr) {
-                       tprints(", ");
-                       if (cur >= abbrev_end) {
-                               tprints("...");
-                               break;
-                       }
-               }
-               if (umove_ulong_array_or_printaddr(tcp, cur, iov,
-                                                  ARRAY_SIZE(iov)))
-                       break;
-               if (cur <= addr)
-                       tprints("[");
-               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]);
-       }
-       if (cur > addr)
-               tprints("]");
+       print_array(tcp, addr, len, iov, current_wordsize * 2,
+                   umoven_or_printaddr, print_iovec, &config);
 }
 
 void
index 0fabed254e5497bffe5110f059f0839feb3b6f56..f3d02e53d463de54e0150fa73282d4f9a38abd9a 100644 (file)
@@ -88,7 +88,7 @@ main(void)
                perror_msg_and_fail("preadv");
        printf("preadv(0, NULL, 1, -2) = -1 EINVAL (%m)\n");
 
-       if (preadv(0, NULL, 0, -3) != -1)
+       if (preadv(0, iov, 0, -3) != -1)
                perror_msg_and_fail("preadv");
        printf("preadv(0, [], 0, -3) = -1 EINVAL (%m)\n");
 
index beac680fde56e2621156ff2673867b4781564ae4..0b7b875290fa3cccd076bab9decbff0cc8d5df07 100644 (file)
@@ -64,14 +64,14 @@ print_iovec(const struct iovec *iov, unsigned int cnt, unsigned int size)
        for (i = 0; i < cnt; ++i) {
                if (i)
                        fputs(", ", stdout);
-               if (i == LIM) {
-                       fputs("...", stdout);
-                       break;
-               }
                if (i == size) {
                        printf("%p", &iov[i]);
                        break;
                }
+               if (i == LIM) {
+                       fputs("...", stdout);
+                       break;
+               }
                print_iov(&iov[i]);
        }
        putchar(']');
@@ -133,7 +133,7 @@ main(void)
        printf("pwritev(0, NULL, 1, -3) = %ld %s (%m)\n",
               rc, errno2name());
 
-       rc = pwritev(0, NULL, 0, -4);
+       rc = pwritev(0, iov, 0, -4);
        printf("pwritev(0, [], 0, -4) = %ld %s (%m)\n",
               rc, errno2name());