[Pdbg] [PATCH v4 01/13] libpdbg: mem read/write should not require access type target

Amitay Isaacs amitay at ozlabs.org
Wed Jul 17 14:08:40 AEST 2019


Whether to use sbefifo or adu to access memory should be internal to
libpdbg.  Client shouldn't need to specify the access method for memory.

Signed-off-by: Amitay Isaacs <amitay at ozlabs.org>
---
 libpdbg/target.c |  36 +++++++------
 src/mem.c        | 128 ++++++++++++++---------------------------------
 2 files changed, 60 insertions(+), 104 deletions(-)

diff --git a/libpdbg/target.c b/libpdbg/target.c
index e193e7e..757c0db 100644
--- a/libpdbg/target.c
+++ b/libpdbg/target.c
@@ -220,21 +220,25 @@ int fsi_write(struct pdbg_target *fsi_dt, uint32_t addr, uint32_t data)
 int mem_read(struct pdbg_target *target, uint64_t addr, uint8_t *output, uint64_t size, uint8_t block_size, bool ci)
 {
 	int rc = -1;
+	struct pdbg_target *sbefifo_target, *adu_target;
 
-	assert(pdbg_target_is_class(target, "sbefifo") ||
-	       pdbg_target_is_class(target, "adu"));
-
-	if (pdbg_target_is_class(target, "sbefifo")) {
+	pdbg_for_each_class_target("sbefifo", sbefifo_target) {
 		struct sbefifo *sbefifo;
 
-		sbefifo = target_to_sbefifo(target);
+		if (pdbg_target_probe(sbefifo_target) != PDBG_TARGET_ENABLED)
+			continue;
+
+		sbefifo = target_to_sbefifo(sbefifo_target);
 		rc = sbefifo->mem_read(sbefifo, addr, output, size, ci);
 	}
 
-	if (pdbg_target_is_class(target, "adu")) {
+	if (!rc)
+		return rc;
+
+	pdbg_for_each_class_target("adu", adu_target) {
 		struct adu *adu;
 
-		adu = target_to_adu(target);
+		adu = target_to_adu(adu_target);
 		rc = adu->read(adu, addr, output, size, block_size, ci);
 	}
 
@@ -244,21 +248,25 @@ int mem_read(struct pdbg_target *target, uint64_t addr, uint8_t *output, uint64_
 int mem_write(struct pdbg_target *target, uint64_t addr, uint8_t *input, uint64_t size, uint8_t block_size, bool ci)
 {
 	int rc = -1;
+	struct pdbg_target *sbefifo_target, *adu_target;
 
-	assert(pdbg_target_is_class(target, "sbefifo") ||
-	       pdbg_target_is_class(target, "adu"));
-
-	if (pdbg_target_is_class(target, "sbefifo")) {
+	pdbg_for_each_class_target("sbefifo", sbefifo_target) {
 		struct sbefifo *sbefifo;
 
-		sbefifo = target_to_sbefifo(target);
+		if (pdbg_target_probe(sbefifo_target) != PDBG_TARGET_ENABLED)
+			continue;
+
+		sbefifo = target_to_sbefifo(sbefifo_target);
 		rc = sbefifo->mem_write(sbefifo, addr, input, size, ci);
 	}
 
-	if (pdbg_target_is_class(target, "adu")) {
+	if (!rc)
+		return rc;
+
+	pdbg_for_each_class_target("adu", adu_target) {
 		struct adu *adu;
 
-		adu = target_to_adu(target);
+		adu = target_to_adu(adu_target);
 		rc = adu->write(adu, addr, input, size, block_size, ci);
 	}
 
diff --git a/src/mem.c b/src/mem.c
index 0b8ad7e..8b62de2 100644
--- a/src/mem.c
+++ b/src/mem.c
@@ -83,9 +83,8 @@ static uint8_t *read_stdin(size_t *size)
 
 static int _getmem(uint64_t addr, uint64_t size, uint8_t block_size, bool ci, bool raw)
 {
-	struct pdbg_target *target;
 	uint8_t *buf;
-	int rc, count = 0;
+	int rc;
 
 	if (size == 0) {
 		PR_ERROR("Size must be > 0\n");
@@ -95,70 +94,45 @@ static int _getmem(uint64_t addr, uint64_t size, uint8_t block_size, bool ci, bo
 	buf = malloc(size);
 	assert(buf);
 
-	pdbg_for_each_class_target("sbefifo", target) {
-		if (pdbg_target_probe(target) != PDBG_TARGET_ENABLED)
-			continue;
-
-		pdbg_set_progress_tick(progress_tick);
-		progress_init();
-		rc = mem_read(target, addr, buf, size, block_size, ci);
-		progress_end();
-		if (rc) {
-			PR_ERROR("Unable to read memory using sbefifo\n");
-			continue;
-		}
+	pdbg_set_progress_tick(progress_tick);
+	progress_init();
+	rc = mem_read(NULL, addr, buf, size, block_size, ci);
+	progress_end();
 
-		count++;
-		goto done;
+	if (rc) {
+		fprintf(stderr, "Unable to read memory\n");
+		free(buf);
+		return 0;
 	}
 
-	pdbg_for_each_class_target("adu", target) {
-		if (pdbg_target_probe(target) != PDBG_TARGET_ENABLED)
-			continue;
-
-		pdbg_set_progress_tick(progress_tick);
-		progress_init();
-		rc = mem_read(target, addr, buf, size, block_size, ci);
-		progress_end();
-		if (rc) {
-			PR_ERROR("Unable to read memory using adu\n");
-			continue;
-		}
 
-		count++;
-		goto done;
-	}
-
-done:
-	if (count > 0) {
+	if (raw) {
+		if (write(STDOUT_FILENO, buf, size) < 0)
+			PR_ERROR("Unable to write stdout.\n");
+	} else {
 		uint64_t i;
 		bool printable = true;
 
-		if (raw) {
-			if (write(STDOUT_FILENO, buf, size) < 0)
-				PR_ERROR("Unable to write stdout.\n");
-		} else {
-			for (i=0; i<size; i++) {
-				if (buf[i] == 0x0a || buf[i] == 0x0d || buf[i] == 0)
-					continue;
-
-				if (!isprint(buf[i])) {
-					printable = false;
-					break;
-				}
-			}
+		for (i=0; i<size; i++) {
+			if (buf[i] == 0x0a || buf[i] == 0x0d || buf[i] == 0)
+				continue;
 
-			if (!printable) {
-				hexdump(addr, buf, size, 1);
-			} else {
-				if (write(STDOUT_FILENO, buf, size) < 0)
-					PR_ERROR("Unable to write stdout.\n");
+			if (!isprint(buf[i])) {
+				printable = false;
+				break;
 			}
 		}
+
+		if (!printable) {
+			hexdump(addr, buf, size, 1);
+		} else {
+			if (write(STDOUT_FILENO, buf, size) < 0)
+				PR_ERROR("Unable to write stdout.\n");
+		}
 	}
 
 	free(buf);
-	return count;
+	return 1;
 }
 
 static int getmem(uint64_t addr, uint64_t size, struct mem_flags flags)
@@ -182,52 +156,26 @@ static int _putmem(uint64_t addr, uint8_t block_size, bool ci)
 {
 	uint8_t *buf;
 	size_t buflen;
-	int rc, count = 0;
-	struct pdbg_target *target;
+	int rc;
 
 	buf = read_stdin(&buflen);
 	assert(buf);
 
-	pdbg_for_each_class_target("sbefifo", target) {
-		if (pdbg_target_probe(target) != PDBG_TARGET_ENABLED)
-			continue;
-
-		pdbg_set_progress_tick(progress_tick);
-		progress_init();
-		rc = mem_write(target, addr, buf, buflen, block_size, ci);
-		progress_end();
-		if (rc) {
-			printf("Unable to write memory using sbefifo\n");
-			continue;
-		}
-
-		count++;
-		goto done;
-	}
-
-	pdbg_for_each_class_target("adu", target) {
-		if (pdbg_target_probe(target) != PDBG_TARGET_ENABLED)
-			continue;
-
-		pdbg_set_progress_tick(progress_tick);
-		progress_init();
-		rc = mem_write(target, addr, buf, buflen, block_size, ci);
-		progress_end();
-		if (rc) {
-			printf("Unable to write memory using adu\n");
-			continue;
-		}
+	pdbg_set_progress_tick(progress_tick);
+	progress_init();
+	rc = mem_write(NULL, addr, buf, buflen, block_size, ci);
+	progress_end();
 
-		count++;
-		goto done;
+	if (rc) {
+		fprintf(stderr, "Unable to write memory\n");
+		free(buf);
+		return 0;
 	}
 
-done:
-	if (count > 0)
-		printf("Wrote %zu bytes starting at 0x%016" PRIx64 "\n", buflen, addr);
+	printf("Wrote %zu bytes starting at 0x%016" PRIx64 "\n", buflen, addr);
 
 	free(buf);
-	return count;
+	return 1;
 }
 
 static int putmem(uint64_t addr, struct mem_flags flags)
-- 
2.21.0



More information about the Pdbg mailing list