qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Laszlo Ersek <lersek@redhat.com>
To: qemu-devel@nongnu.org, pbonzini@redhat.com
Subject: [Qemu-devel] [PATCH 1/2] qemu_hexstr(): hexdump a small buffer to a string, for in-line printing
Date: Thu, 29 Aug 2013 15:37:49 +0200	[thread overview]
Message-ID: <1377783470-8981-1-git-send-email-lersek@redhat.com> (raw)
In-Reply-To: <521F4E76.2090507@redhat.com>

This function should primarily serve tracing needs.

Signed-off-by: Laszlo Ersek <lersek@redhat.com>
---
 include/qemu-common.h |   11 +++++++++++
 util/hexdump.c        |   20 ++++++++++++++++++++
 2 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/include/qemu-common.h b/include/qemu-common.h
index 6948bb9..18a373f 100644
--- a/include/qemu-common.h
+++ b/include/qemu-common.h
@@ -433,6 +433,17 @@ int mod_utf8_codepoint(const char *s, size_t n, char **end);
 
 void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
 
+/*
+ * Hexdump @size bytes from @buf to a dynamically allocated string.
+ * If @size is NULL, then @buf can be a NULL pointer, and an empty string will
+ * be formatted.
+ * If @str_len is non-NULL, then the length of the output string (excluding the
+ * terminating NUL) will be stored in *@str_len.
+ * The output is meant to be printed on a single line, hence it contains no
+ * newlines, and @size should be reasonable.
+ */
+char *qemu_hexstr(const void *buf, size_t size, size_t *str_len);
+
 /* vector definitions */
 #ifdef __ALTIVEC__
 #include <altivec.h>
diff --git a/util/hexdump.c b/util/hexdump.c
index 969b340..5c7e1a2 100644
--- a/util/hexdump.c
+++ b/util/hexdump.c
@@ -35,3 +35,23 @@ void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size)
         fprintf(fp, "\n");
     }
 }
+
+char *qemu_hexstr(const void *buf, size_t size, size_t *str_len)
+{
+    size_t str_size, i;
+    char *ret, *output;
+
+    str_size = size * 3 + (size == 0);
+    ret = g_malloc(str_size);
+    output = ret;
+
+    for (i = 0; i < size; ++i) {
+        output += sprintf(output, "%s%02x", (i == 0) ? "" : " ",
+                          ((uint8_t *)buf)[i]);
+    }
+    *output = '\0';
+    if (str_len != NULL) {
+        *str_len = str_size - 1;
+    }
+    return ret;
+}
-- 
1.7.1

  reply	other threads:[~2013-08-29 13:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-29 13:36 [Qemu-devel] [PATCH 0/2] some virtio-scsi tracing Laszlo Ersek
2013-08-29 13:37 ` Laszlo Ersek [this message]
2013-08-29 16:32   ` [Qemu-devel] [PATCH 1/2] qemu_hexstr(): hexdump a small buffer to a string, for in-line printing Markus Armbruster
2013-08-29 13:37 ` [Qemu-devel] [PATCH 2/2] add some virtio-scsi trace events Laszlo Ersek
2013-08-29 13:59   ` Paolo Bonzini
2013-08-29 14:18     ` Laszlo Ersek
2013-08-29 14:53       ` Paolo Bonzini
2013-08-29 15:35         ` Laszlo Ersek
2013-08-29 15:42           ` Paolo Bonzini
2013-09-04 14:21   ` Stefan Hajnoczi
2013-09-05  1:26     ` Laszlo Ersek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1377783470-8981-1-git-send-email-lersek@redhat.com \
    --to=lersek@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).