[Pdbg] [PATCH 1/7] mem: Read all the memory before call mem_write

Amitay Isaacs amitay at ozlabs.org
Wed Apr 10 18:08:48 AEST 2019


This fixes a bug in _putmem, where all writes are done to the same addr.

Doing a single write will be much faster for sbefifo backend.

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

diff --git a/src/mem.c b/src/mem.c
index 9c047ec..fe76c64 100644
--- a/src/mem.c
+++ b/src/mem.c
@@ -43,6 +43,37 @@ struct mem_flags {
 #define MEM_CI_FLAG ("--ci", ci, parse_flag_noarg, false)
 #define BLOCK_SIZE (parse_number8_pow2, NULL)
 
+static uint8_t *read_stdin(size_t *size)
+{
+	uint8_t *buf = NULL;
+	size_t allocated = 0;
+	size_t buflen = 0;
+	ssize_t n;
+
+	while (1) {
+		if (allocated == buflen) {
+			uint8_t *ptr;
+
+			ptr = realloc(buf, allocated + PUTMEM_BUF_SIZE);
+			if (!ptr) {
+				free(buf);
+				return NULL;
+			}
+			buf = ptr;
+			allocated += PUTMEM_BUF_SIZE;
+		}
+
+		n = read(STDIN_FILENO, buf + buflen, allocated - buflen);
+		if (n <= 0)
+			break;
+
+		buflen += n;
+	}
+
+	*size = buflen;
+	return buf;
+}
+
 static int _getmem(uint64_t addr, uint64_t size, uint8_t block_size, bool ci)
 {
 	struct pdbg_target *target;
@@ -121,7 +152,8 @@ OPTCMD_DEFINE_CMD_WITH_ARGS(getmemio, getmemio, (ADDRESS, DATA, BLOCK_SIZE));
 static int _putmem(uint64_t addr, uint8_t block_size, bool ci)
 {
 	uint8_t *buf;
-	int read_size, rc = 0;
+	size_t buflen;
+	int rc = 0;
 	struct pdbg_target *adu_target;
 
 	pdbg_for_each_class_target("adu", adu_target)
@@ -130,29 +162,22 @@ static int _putmem(uint64_t addr, uint8_t block_size, bool ci)
 	if (pdbg_target_probe(adu_target) != PDBG_TARGET_ENABLED)
 		return 0;
 
-	buf = malloc(PUTMEM_BUF_SIZE);
+	buf = read_stdin(&buflen);
 	assert(buf);
+
 	pdbg_set_progress_tick(progress_tick);
 	progress_init();
-	do {
-		read_size = read(STDIN_FILENO, buf, PUTMEM_BUF_SIZE);
-		if (read_size <= 0)
-			break;
-
-		rc = mem_write(adu_target, addr, buf, read_size, block_size, ci);
-		if (rc) {
-			rc = 0;
-			printf("Unable to write memory.\n");
-			break;
-		}
-
-		rc += read_size;
-	} while (read_size > 0);
+	rc = mem_write(adu_target, addr, buf, buflen, block_size, ci);
 	progress_end();
+	if (rc) {
+		printf("Unable to write memory\n");
+		free(buf);
+		return 0;
+	}
 
-	printf("Wrote %d bytes starting at 0x%016" PRIx64 "\n", rc, addr);
+	printf("Wrote %zu bytes starting at 0x%016" PRIx64 "\n", buflen, addr);
 	free(buf);
-	return rc;
+	return 1;
 }
 
 static int putmem(uint64_t addr, struct mem_flags flags)
-- 
2.20.1



More information about the Pdbg mailing list