From f0f45a93a1744012bfb7bb5e3faecb6cc3615cc5 Mon Sep 17 00:00:00 2001 From: Tau Date: Thu, 8 Nov 2018 10:22:26 -0500 Subject: [PATCH] util/dump.c: Add hex pretty-print debug helper --- util/dump.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ util/dump.h | 15 +++++++++++++ util/meson.build | 2 ++ 3 files changed, 74 insertions(+) create mode 100644 util/dump.c create mode 100644 util/dump.h diff --git a/util/dump.c b/util/dump.c new file mode 100644 index 0000000..b937603 --- /dev/null +++ b/util/dump.c @@ -0,0 +1,57 @@ +#ifndef NDEBUG + +#include +#include + +#include "hook/iobuf.h" + +#include "util/dprintf.h" +#include "util/dump.h" + +void dump(const void *ptr, size_t nbytes) +{ + const uint8_t *bytes; + size_t i; + + assert(ptr != NULL); + + if (nbytes == 0) { + dprintf("\t--- Empty ---\n"); + } + + bytes = ptr; + + for (i = 0 ; i < nbytes ; i++) { + if (i % 16 == 0) { + dprintf("\t%08x:", i); + } + + dprintf(" %02x", bytes[i]); + + if (i % 16 == 15) { + dprintf("\n"); + } + } + + dprintf("\n"); +} + +void dump_iobuf(const struct iobuf *iobuf) +{ + assert(iobuf != NULL); + assert(iobuf->bytes != NULL || iobuf->nbytes == 0); + assert(iobuf->pos <= iobuf->nbytes); + + dump(iobuf->bytes, iobuf->pos); +} + +void dump_const_iobuf(const struct const_iobuf *iobuf) +{ + assert(iobuf != NULL); + assert(iobuf->bytes != NULL || iobuf->nbytes == 0); + assert(iobuf->pos <= iobuf->nbytes); + + dump(&iobuf->bytes[iobuf->pos], iobuf->nbytes - iobuf->pos); +} + +#endif diff --git a/util/dump.h b/util/dump.h new file mode 100644 index 0000000..bfc3c69 --- /dev/null +++ b/util/dump.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#include "hook/iobuf.h" + +#ifndef NDEBUG +void dump(const void *ptr, size_t nbytes); +void dump_iobuf(const struct iobuf *iobuf); +void dump_const_iobuf(const struct const_iobuf *iobuf); +#else +#define dump(ptr, nbytes) +#define dump_iobuf(iobuf) +#define dump_const_iobuf(iobuf) +#endif diff --git a/util/meson.build b/util/meson.build index c9f447f..0fa3470 100644 --- a/util/meson.build +++ b/util/meson.build @@ -11,6 +11,8 @@ util_lib = static_library( 'crc.h', 'dprintf.c', 'dprintf.h', + 'dump.c', + 'dump.h', 'setupapi.c', 'setupapi.h', ],