From patchwork Fri Mar 15 16:56:33 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 228103 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 457CF2C009A for ; Sat, 16 Mar 2013 04:12:52 +1100 (EST) Received: from localhost ([::1]:34039 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGYBW-0007qn-CM for incoming@patchwork.ozlabs.org; Fri, 15 Mar 2013 13:12:50 -0400 Received: from eggs.gnu.org ([208.118.235.92]:47040) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGYB8-0007iz-RZ for qemu-devel@nongnu.org; Fri, 15 Mar 2013 13:12:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UGXw2-0003gS-18 for qemu-devel@nongnu.org; Fri, 15 Mar 2013 12:57:15 -0400 Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.d.1.0.0.b.8.0.1.0.0.2.ip6.arpa ([2001:8b0:1d0::1]:32887 helo=mnementh.archaic.org.uk) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGXw1-0003bs-LF for qemu-devel@nongnu.org; Fri, 15 Mar 2013 12:56:49 -0400 Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.72) (envelope-from ) id 1UGXvr-0006Jj-M8; Fri, 15 Mar 2013 16:56:39 +0000 From: Peter Maydell To: Anthony Liguori , Blue Swirl Date: Fri, 15 Mar 2013 16:56:33 +0000 Message-Id: <1363366599-24238-12-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1363366599-24238-1-git-send-email-peter.maydell@linaro.org> References: <1363366599-24238-1-git-send-email-peter.maydell@linaro.org> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2001:8b0:1d0::1 Cc: qemu-devel@nongnu.org, Paul Brook Subject: [Qemu-devel] [PATCH 11/17] iov: Factor out hexdumper X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Peter Crosthwaite Factor out the hexdumper functionality from iov for all to use. Useful for creating verbose debug printfery that dumps packet data. Signed-off-by: Peter Crosthwaite Message-id: faaac219c55ea586d3f748befaf5a2788fd271b8.1361853677.git.peter.crosthwaite@xilinx.com Signed-off-by: Peter Maydell --- include/qemu-common.h | 6 ++++++ util/Makefile.objs | 1 + util/hexdump.c | 37 +++++++++++++++++++++++++++++++++++++ util/iov.c | 36 +++++++++++------------------------- 4 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 util/hexdump.c diff --git a/include/qemu-common.h b/include/qemu-common.h index 5e13708..7754ee2 100644 --- a/include/qemu-common.h +++ b/include/qemu-common.h @@ -442,4 +442,10 @@ int64_t pow2floor(int64_t value); int uleb128_encode_small(uint8_t *out, uint32_t n); int uleb128_decode_small(const uint8_t *in, uint32_t *n); +/* + * Hexdump a buffer to a file. An optional string prefix is added to every line + */ + +void hexdump(const char *buf, FILE *fp, const char *prefix, size_t size); + #endif diff --git a/util/Makefile.objs b/util/Makefile.objs index cad5ce8..557bda7 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -9,3 +9,4 @@ util-obj-y += error.o qemu-error.o util-obj-$(CONFIG_POSIX) += compatfd.o util-obj-y += iov.o aes.o qemu-config.o qemu-sockets.o uri.o notify.o util-obj-y += qemu-option.o qemu-progress.o +util-obj-y += hexdump.o diff --git a/util/hexdump.c b/util/hexdump.c new file mode 100644 index 0000000..0d0efc8 --- /dev/null +++ b/util/hexdump.c @@ -0,0 +1,37 @@ +/* + * Helper to hexdump a buffer + * + * Copyright (c) 2013 Red Hat, Inc. + * Copyright (c) 2013 Gerd Hoffmann + * Copyright (c) 2013 Peter Crosthwaite + * Copyright (c) 2013 Xilinx, Inc + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + * + * Contributions after 2012-01-13 are licensed under the terms of the + * GNU GPL, version 2 or (at your option) any later version. + */ + +#include "qemu-common.h" + +void hexdump(const char *buf, FILE *fp, const char *prefix, size_t size) +{ + unsigned int b; + + for (b = 0; b < size; b++) { + if ((b % 16) == 0) { + fprintf(fp, "%s: %04x:", prefix, b); + } + if ((b % 4) == 0) { + fprintf(fp, " "); + } + fprintf(fp, " %02x", (unsigned char)buf[b]); + if ((b % 16) == 15) { + fprintf(fp, "\n"); + } + } + if ((b % 16) != 0) { + fprintf(fp, "\n"); + } +} diff --git a/util/iov.c b/util/iov.c index fbe675d..9dae318 100644 --- a/util/iov.c +++ b/util/iov.c @@ -201,32 +201,18 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt, FILE *fp, const char *prefix, size_t limit) { - unsigned int i, v, b; - uint8_t *c; - - c = iov[0].iov_base; - for (i = 0, v = 0, b = 0; b < limit; i++, b++) { - if (i == iov[v].iov_len) { - i = 0; v++; - if (v == iov_cnt) { - break; - } - c = iov[v].iov_base; - } - if ((b % 16) == 0) { - fprintf(fp, "%s: %04x:", prefix, b); - } - if ((b % 4) == 0) { - fprintf(fp, " "); - } - fprintf(fp, " %02x", c[i]); - if ((b % 16) == 15) { - fprintf(fp, "\n"); - } - } - if ((b % 16) != 0) { - fprintf(fp, "\n"); + int v; + size_t size = 0; + char *buf; + + for (v = 0; v < iov_cnt; v++) { + size += iov[v].iov_len; } + size = size > limit ? limit : size; + buf = g_malloc(size); + iov_to_buf(iov, iov_cnt, 0, buf, size); + hexdump(buf, fp, prefix, size); + g_free(buf); } unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,