From: Dmitry V. Levin Date: Sat, 7 May 2016 22:54:04 +0000 (+0000) Subject: io.c: use print_array function X-Git-Tag: v4.12~221 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4ce3054340a41953e0f8b75d677dc1eb119f61a3;p=strace io.c: use print_array function * 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. --- diff --git a/io.c b/io.c index ce787cd2..a52904cf 100644 --- 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 diff --git a/tests/preadv.c b/tests/preadv.c index 0fabed25..f3d02e53 100644 --- a/tests/preadv.c +++ b/tests/preadv.c @@ -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"); diff --git a/tests/pwritev.c b/tests/pwritev.c index beac680f..0b7b8752 100644 --- a/tests/pwritev.c +++ b/tests/pwritev.c @@ -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());