[Pdbg] [PATCH v4 1/5] main: Add hexdump() function to dump bytes
Amitay Isaacs
amitay at ozlabs.org
Wed Apr 10 17:04:39 AEST 2019
Signed-off-by: Amitay Isaacs <amitay at ozlabs.org>
---
src/util.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
src/util.h | 16 ++++++++++++++++
2 files changed, 61 insertions(+)
diff --git a/src/util.c b/src/util.c
index 3a4520d..24c3bee 100644
--- a/src/util.c
+++ b/src/util.c
@@ -19,6 +19,9 @@
#include <stdbool.h>
#include <limits.h>
#include <assert.h>
+#include <inttypes.h>
+
+#include "util.h"
/* Parse argument of the form 0-5,7,9-11,15,17 */
bool parse_list(const char *arg, int max, int *list, int *count)
@@ -93,3 +96,45 @@ bool parse_list(const char *arg, int max, int *list, int *count)
return true;
}
+void hexdump(uint64_t addr, uint8_t *buf, uint64_t size, uint8_t group_size)
+{
+ uint64_t start_addr, offset, i;
+ int j, k;
+
+ start_addr = addr & (~(uint64_t)0xf);
+ offset = addr - start_addr;
+
+ if (group_size == 0)
+ group_size = 1;
+
+ assert(group_size == 1 || group_size == 2 || group_size == 4 || group_size == 8);
+
+ for (i = 0; i < size + 15; i += 16) {
+ bool do_prefix = true;
+
+ if (start_addr + i >= addr + size)
+ break;
+
+ for (j = 0; j < 16; j += group_size) {
+ for (k = j; k < j + group_size; k++) {
+ uint64_t cur_addr = start_addr + i + k;
+
+ if (cur_addr >= addr + size) {
+ printf("\n");
+ return;
+ }
+
+ if (do_prefix) {
+ printf("0x%016" PRIx64 ": ", start_addr + i);
+ do_prefix = false;
+ }
+ if (i+k >= offset && i+k <= offset + size)
+ printf("%02x", buf[i+k - offset]);
+ else
+ printf(" ");
+ }
+ printf(" ");
+ }
+ printf("\n");
+ }
+}
diff --git a/src/util.h b/src/util.h
index 131e3f9..f63fc81 100644
--- a/src/util.h
+++ b/src/util.h
@@ -16,6 +16,9 @@
#ifndef __UTIL_H
#define __UTIL_H
+#include <stdbool.h>
+#include <stdint.h>
+
/**
* @brief Parse a range or a list of numbers from a string into an array
*
@@ -31,4 +34,17 @@
*/
bool parse_list(const char *arg, int max, int *list, int *count);
+/**
+ * @brief Dump bytes in hex similar to hexdump format
+ *
+ * Prints 16 bytes per line in the specified groups. The addresses are
+ * printed aligned to 16 bytes.
+ *
+ * @param[in] addr Address
+ * @param[in] buf Data to print
+ * @param[in] size Number of bytes to print
+ * @param[in] group_size How to group bytes (valid values 1, 2, 4, 8)
+ */
+void hexdump(uint64_t addr, uint8_t *buf, uint64_t size, uint8_t group_size);
+
#endif
--
2.20.1
More information about the Pdbg
mailing list