[Pdbg] [PATCH v4 4/5] main: Use hexdump to dump memory from gemem

Amitay Isaacs amitay at ozlabs.org
Wed Apr 10 17:04:42 AEST 2019


If getmem is being run on a terminal and if the output contains
non-printable characters, then use hexdump() to dump the data.

Signed-off-by: Amitay Isaacs <amitay at ozlabs.org>
---
 src/mem.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/src/mem.c b/src/mem.c
index 8045069..b264d98 100644
--- a/src/mem.c
+++ b/src/mem.c
@@ -21,6 +21,7 @@
 #include <unistd.h>
 #include <assert.h>
 #include <stdbool.h>
+#include <ctype.h>
 
 #include <libpdbg.h>
 
@@ -28,6 +29,7 @@
 #include "progress.h"
 #include "optcmd.h"
 #include "parsers.h"
+#include "util.h"
 
 #define PR_ERROR(x, args...) \
 	pdbg_log(PDBG_ERROR, x, ##args)
@@ -75,8 +77,22 @@ static int _getmem(uint64_t addr, uint64_t size, uint8_t block_size, bool ci)
 	}
 
 	if (count > 0) {
-		if (write(STDOUT_FILENO, buf, size) < 0)
-			PR_ERROR("Unable to write stdout.\n");
+		uint64_t i;
+		bool printable = true;
+
+		for (i=0; i<size; i++) {
+			if (!isprint(buf[i])) {
+				printable = false;
+				break;
+			}
+		}
+
+		if (isatty(STDOUT_FILENO) && !printable) {
+			hexdump(addr, buf, size, 1);
+		} else {
+			if (write(STDOUT_FILENO, buf, size) < 0)
+				PR_ERROR("Unable to write stdout.\n");
+		}
 	}
 
 	free(buf);
-- 
2.20.1



More information about the Pdbg mailing list