[Pdbg] [PATCH 15/29] main: Use new command handling for memory commands

Cyril Bur cyrilbur at gmail.com
Fri Feb 9 15:38:43 AEDT 2018


Signed-off-by: Cyril Bur <cyrilbur at gmail.com>
---
 src/main.c | 35 ++++---------------------------
 src/mem.c  | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/mem.h  |  2 +-
 3 files changed, 73 insertions(+), 33 deletions(-)

diff --git a/src/main.c b/src/main.c
index 2dd4f24..c7ce171 100644
--- a/src/main.c
+++ b/src/main.c
@@ -87,7 +87,9 @@ static struct {
 	{ "getcfam", "<address>", "Read system cfam", &handle_cfams },
 	{ "putcfam", "<address> <value> [<mask>]", "Write system cfam", &handle_cfams },
 	{ "getscom", "<address>", "Read system scom", &handle_scoms },
-	{ "putscom", "<address> <value> [<mask>]", "Write system scom", &handle_scoms }
+	{ "putscom", "<address> <value> [<mask>]", "Write system scom", &handle_scoms },
+	{ "getmem",  "<address> <count>", "Read system memory", &handle_mem },
+	{ "putmem",  "<address>", "Write to system memory", &handle_mem },
 };
 
 static void print_usage(char *pname)
@@ -148,13 +150,7 @@ enum command parse_cmd(char *optarg)
 {
 	cmd_max_arg_count = 0;
 
-	if (strcmp(optarg, "getmem") == 0) {
-		cmd = GETMEM;
-		cmd_min_arg_count = 2;
-	} else if (strcmp(optarg, "putmem") == 0) {
-		cmd = PUTMEM;
-		cmd_min_arg_count = 1;
-	} else if (strcmp(optarg, "getgpr") == 0) {
+	if (strcmp(optarg, "getgpr") == 0) {
 		cmd = GETGPR;
 		cmd_min_arg_count = 1;
 	} else if (strcmp(optarg, "putgpr") == 0) {
@@ -587,7 +583,6 @@ int main(int argc, char *argv[])
 	struct pdbg_target *target;
 	bool found = true;
 	int i, rc = 0;
-	uint8_t *buf;
 
 	if (parse_options(argc, argv))
 		return 1;
@@ -607,28 +602,6 @@ int main(int argc, char *argv[])
 		return -1;
 
 	switch(cmd) {
-	case GETMEM:
-                buf = malloc(cmd_args[1]);
-                assert(buf);
-		pdbg_for_each_class_target("adu", target) {
-			if (!adu_getmem(target, cmd_args[0], buf, cmd_args[1])) {
-				if (write(STDOUT_FILENO, buf, cmd_args[1]) < 0)
-					PR_ERROR("Unable to write stdout.\n");
-				else
-					rc++;
-			} else
-				PR_ERROR("Unable to read memory.\n");
-
-			/* We only ever care about getting memory from a single processor */
-			break;
-		}
-
-		free(buf);
-		break;
-	case PUTMEM:
-                rc = putmem(cmd_args[0]);
-                printf("Wrote %d bytes starting at 0x%016" PRIx64 "\n", rc, cmd_args[0]);
-		break;
 	case GETGPR:
 		rc = for_each_target("thread", getprocreg, &cmd_args[0], NULL);
 		break;
diff --git a/src/mem.c b/src/mem.c
index b036f12..64fafe7 100644
--- a/src/mem.c
+++ b/src/mem.c
@@ -13,16 +13,42 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include <errno.h>
 #include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
 #include <operations.h>
 #include <target.h>
 
+#include "main.h"
+
 #define PUTMEM_BUF_SIZE 1024
-int putmem(uint64_t addr)
+static int getmem(uint64_t addr, uint64_t size)
+{
+	struct pdbg_target *target;
+	uint8_t *buf;
+	int rc = 0;
+	buf = malloc(size);
+	assert(buf);
+	pdbg_for_each_class_target("adu", target) {
+		if (!adu_getmem(target, addr, buf, size)) {
+			if (write(STDOUT_FILENO, buf, size) < 0)
+				PR_ERROR("Unable to write stdout.\n");
+			else
+				rc++;
+		} else
+			PR_ERROR("Unable to read memory.\n");
+			/* We only ever care about getting memory from a single processor */
+		break;
+	}
+	free(buf);
+	return rc;
+
+}
+static int putmem(uint64_t addr)
 {
 	uint8_t *buf;
 	int read_size, rc = 0;
@@ -43,6 +69,47 @@ int putmem(uint64_t addr)
 		rc += read_size;
 	} while (read_size > 0);
 
+	printf("Wrote %d bytes starting at 0x%016" PRIx64 "\n", rc, addr);
 	free(buf);
 	return rc;
 }
+
+int handle_mem(int optind, int argc, char *argv[])
+{
+	uint64_t addr;
+	char *endptr;
+
+	if (optind + 1 >= argc) {
+		printf("%s: command '%s' requires an address\n", argv[0], argv[optind]);
+		return -1;
+	}
+
+	errno = 0;
+	addr = strtoull(argv[optind + 1], &endptr, 0);
+	if (errno || *endptr != '\0') {
+		printf("%s: command '%s' couldn't parse address '%s'\n",
+				argv[0], argv[optind], argv[optind + 1]);
+		return -1;
+	}
+
+	if (strcmp(argv[optind], "getmem") == 0) {
+		uint64_t size;
+
+		if (optind + 2 >= argc) {
+			printf("%s: command '%s' requires data\n", argv[0], argv[optind]);
+			return -1;
+		}
+
+		errno = 0;
+		size = strtoull(argv[optind + 2], &endptr, 0);
+		if (errno || *endptr != '\0') {
+			printf("%s: command '%s' couldn't parse data '%s'\n",
+				argv[0], argv[optind], argv[optind + 1]);
+			return -1;
+		}
+
+		return getmem(addr, size);
+	}
+
+	return putmem(addr);
+}
diff --git a/src/mem.h b/src/mem.h
index 27312a2..6148de5 100644
--- a/src/mem.h
+++ b/src/mem.h
@@ -15,4 +15,4 @@
  */
 #include <inttypes.h>
 
-int putmem(uint64_t addr);
+int handle_mem(int optind, int argc, char *argv[]);
-- 
2.16.1



More information about the Pdbg mailing list