From patchwork Fri May 25 04:26:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 920221 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40sY9j028Bz9s15 for ; Fri, 25 May 2018 14:26:49 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=popple.id.au Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 40sY9h5lVDzF1f5 for ; Fri, 25 May 2018 14:26:48 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=popple.id.au X-Original-To: pdbg@lists.ozlabs.org Delivered-To: pdbg@lists.ozlabs.org Received: from ozlabs.org (bilbo.ozlabs.org [203.11.71.1]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 40sY9d17GBzF1bQ for ; Fri, 25 May 2018 14:26:45 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=popple.id.au Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPSA id 40sY9c50s8z9s0q; Fri, 25 May 2018 14:26:44 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=popple.id.au From: Alistair Popple To: pdbg@lists.ozlabs.org Date: Fri, 25 May 2018 14:26:35 +1000 Message-Id: <20180525042635.26396-1-alistair@popple.id.au> X-Mailer: git-send-email 2.11.0 Subject: [Pdbg] [PATCH] reg.c: Refactor get/put register handling X-BeenThere: pdbg@lists.ozlabs.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "mailing list for https://github.com/open-power/pdbg development" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: pdbg-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Pdbg" We already parse the difference between get and put register so refactor to avoid parsing a second time. Signed-off-by: Alistair Popple --- src/main.c | 16 +++--- src/reg.c | 166 +++++++++++++++++++++++++++++++++---------------------------- src/reg.h | 12 +++-- 3 files changed, 106 insertions(+), 88 deletions(-) diff --git a/src/main.c b/src/main.c index 85770ef..5bc3222 100644 --- a/src/main.c +++ b/src/main.c @@ -88,14 +88,14 @@ struct action { }; static struct action actions[] = { - { "getgpr", "", "Read General Purpose Register (GPR)", &handle_gpr }, - { "putgpr", " ", "Write General Purpose Register (GPR)", &handle_gpr }, - { "getnia", "", "Get Next Instruction Address (NIA)", &handle_nia }, - { "putnia", "", "Write Next Instrution Address (NIA)", &handle_nia }, - { "getspr", "", "Get Special Purpose Register (SPR)", &handle_spr }, - { "putspr", " ", "Write Special Purpose Register (SPR)", &handle_spr }, - { "getmsr", "", "Get Machine State Register (MSR)", &handle_msr }, - { "putmsr", "", "Write Machine State Register (MSR)", &handle_msr }, + { "getgpr", "", "Read General Purpose Register (GPR)", &handle_getgpr }, + { "putgpr", " ", "Write General Purpose Register (GPR)", &handle_putgpr }, + { "getnia", "", "Get Next Instruction Address (NIA)", &handle_getnia }, + { "putnia", "", "Write Next Instrution Address (NIA)", &handle_putnia }, + { "getspr", "", "Get Special Purpose Register (SPR)", &handle_getspr }, + { "putspr", " ", "Write Special Purpose Register (SPR)", &handle_putspr }, + { "getmsr", "", "Get Machine State Register (MSR)", &handle_getmsr }, + { "putmsr", "", "Write Machine State Register (MSR)", &handle_putmsr }, { "getring", " ", "Read a ring. Length must be correct", &handle_getring }, { "start", "", "Start thread", &thread_start }, { "step", "", "Set a thread instructions", &thread_step }, diff --git a/src/reg.c b/src/reg.c index 002cfe9..932ae43 100644 --- a/src/reg.c +++ b/src/reg.c @@ -91,10 +91,10 @@ static int getprocreg(struct pdbg_target *target, uint32_t index, uint64_t *reg, return !rc; } -int handle_gpr(int optind, int argc, char *argv[]) +static int parse_gpr(int optind, int argc, char *argv[]) { char *endptr; - uint64_t gpr; + int gpr; if (optind + 1 >= argc) { printf("%s: command '%s' requires a GPR\n", argv[0], argv[optind]); @@ -102,10 +102,10 @@ int handle_gpr(int optind, int argc, char *argv[]) } errno = 0; - gpr = strtoull(argv[optind + 1], &endptr, 0); + gpr = strtoul(argv[optind + 1], &endptr, 0); if (errno || *endptr != '\0') { printf("%s: command '%s' couldn't parse GPR '%s'\n", - argv[0], argv[optind], argv[optind + 1]); + argv[0], argv[optind], argv[optind + 1]); return -1; } @@ -114,120 +114,134 @@ int handle_gpr(int optind, int argc, char *argv[]) return -1; } - if (strcmp(argv[optind], "putgpr") == 0) { - uint64_t data; + return gpr; +} - if (optind + 2 >= argc) { - printf("%s: command '%s' requires data\n", argv[0], argv[optind]); - return -1; - } +static int parse_data(int optind, int argc, char *argv[], uint64_t *data) +{ + char *endptr; - errno = 0; - data = 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; - } + if (optind + 2 >= argc) { + printf("%s: command '%s' requires data\n", argv[0], argv[optind]); + return -1; + } - return for_each_target("thread", putprocreg, &gpr, &data); + errno = 0; + *data = 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 0; +} + +int handle_getgpr(int optind, int argc, char *argv[]) +{ + uint64_t gpr; + + if ((gpr = parse_gpr(optind, argc, argv)) < 0) + return -1; + return for_each_target("thread", getprocreg, &gpr, NULL); } -int handle_nia(int optind, int argc, char *argv[]) +int handle_putgpr(int optind, int argc, char *argv[]) { - uint64_t reg = REG_NIA; - char *endptr; + uint64_t gpr, data; - if (strcmp(argv[optind], "putnia") == 0) { - uint64_t data; + if ((gpr = parse_gpr(optind, argc, argv)) < 0) + return -1; - if (optind + 2 >= argc) { - printf("%s: command '%s' requires data\n", argv[0], argv[optind]); - return -1; - } + if (parse_data(optind, argc, argv, &data)) + return -1; - errno = 0; - data = 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 for_each_target("thread", putprocreg, &gpr, &data); +} - return for_each_target("thread", putprocreg, ®, &data); - } +int handle_getnia(int optind, int argc, char *argv[]) +{ + uint64_t reg = REG_NIA; return for_each_target("thread", getprocreg, ®, NULL); } -int handle_spr(int optind, int argc, char *argv[]) +int handle_putnia(int optind, int argc, char *argv[]) +{ + uint64_t reg = REG_NIA, data; + + if (parse_data(optind, argc, argv, &data)) + return -1; + + return for_each_target("thread", putprocreg, ®, &data); +} + +static int parse_spr(int optind, int argc, char *argv[]) { char *endptr; - uint64_t spr; + int gpr; if (optind + 1 >= argc) { - printf("%s: command '%s' requires a GPR\n", argv[0], argv[optind]); + printf("%s: command '%s' requires a SPR\n", argv[0], argv[optind]); return -1; } errno = 0; - spr = strtoull(argv[optind + 1], &endptr, 0); + gpr = strtoul(argv[optind + 1], &endptr, 0); if (errno || *endptr != '\0') { - printf("%s: command '%s' couldn't parse GPR '%s'\n", - argv[0], argv[optind], argv[optind + 1]); + printf("%s: command '%s' couldn't parse SPR '%s'\n", + argv[0], argv[optind], argv[optind + 1]); return -1; } - spr += REG_R31; + if (gpr > 0x3ff) { + printf("A SPR must be between zero and 1023 inclusive\n"); + return -1; + } - if (strcmp(argv[optind], "putspr") == 0) { - uint64_t data; + return gpr; +} - if (optind + 2 >= argc) { - printf("%s: command '%s' requires data\n", argv[0], argv[optind]); - return -1; - } +int handle_getspr(int optind, int argc, char *argv[]) +{ + uint64_t spr; - errno = 0; - data = 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; - } + if ((spr = parse_spr(optind, argc, argv)) < 0) + return -1; - return for_each_target("thread", putprocreg, &spr, &data); - } + spr += REG_R31; return for_each_target("thread", getprocreg, &spr, NULL); } -int handle_msr(int optind, int argc, char *argv[]) +int handle_putspr(int optind, int argc, char *argv[]) { - uint64_t msr = REG_MSR; - char *endptr; + uint64_t spr, data; - if (strcmp(argv[optind], "putmsr") == 0) { - uint64_t data; + if ((spr = parse_spr(optind, argc, argv)) < 0) + return -1; - if (optind + 2 >= argc) { - printf("%s: command '%s' requires data\n", argv[0], argv[optind]); - return -1; - } + spr += REG_R31; + if (parse_data(optind, argc, argv, &data)) + return -1; - errno = 0; - data = 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 for_each_target("thread", putprocreg, &spr, &data); +} - return for_each_target("thread", putprocreg, &msr, &data); - } +int handle_getmsr(int optind, int argc, char *argv[]) +{ + uint64_t msr = REG_MSR; return for_each_target("thread", getprocreg, &msr, NULL); } + +int handle_putmsr(int optind, int argc, char *argv[]) +{ + uint64_t msr = REG_MSR, data; + + if (parse_data(optind, argc, argv, &data)) + return -1; + + return for_each_target("thread", putprocreg, &msr, &data); +} diff --git a/src/reg.h b/src/reg.h index ad41d9d..9ee2cd4 100644 --- a/src/reg.h +++ b/src/reg.h @@ -14,7 +14,11 @@ * limitations under the License. */ -int handle_gpr(int optind, int argc, char *argv[]); -int handle_nia(int optind, int argc, char *argv[]); -int handle_spr(int optind, int argc, char *argv[]); -int handle_msr(int optind, int argc, char *argv[]); +int handle_getgpr(int optind, int argc, char *argv[]); +int handle_putgpr(int optind, int argc, char *argv[]); +int handle_getnia(int optind, int argc, char *argv[]); +int handle_putnia(int optind, int argc, char *argv[]); +int handle_getspr(int optind, int argc, char *argv[]); +int handle_putspr(int optind, int argc, char *argv[]); +int handle_getmsr(int optind, int argc, char *argv[]); +int handle_putmsr(int optind, int argc, char *argv[]);