From 3235fa200513a7a0f3b503a28d3e63e45e8996c0 Mon Sep 17 00:00:00 2001 From: Jeremy Elson Date: Tue, 19 Mar 2019 12:30:37 -0700 Subject: [PATCH] Improve parsing of commands that require enable or disable arguments: * Accept prefixes of the words 'enable' and 'disable' * Prevent silent failures on parse errors * Print status after flag changes * Fix missing includes --- src/command.c | 63 ++++++++++++++++++++++++++++++++++++------- src/include/command.h | 10 +++++++ src/include/target.h | 4 +++ src/target/cortexm.c | 17 +++++++----- src/target/kinetis.c | 8 +++--- src/target/nxpke04.c | 8 +++--- 6 files changed, 88 insertions(+), 22 deletions(-) diff --git a/src/command.c b/src/command.c index e4152cb..0390309 100644 --- a/src/command.c +++ b/src/command.c @@ -247,14 +247,40 @@ bool cmd_morse(void) return true; } +bool parse_enable_or_disable(const char *s, bool *out) { + if (strlen(s) == 0) { + gdb_outf("'enable' or 'disable' argument must be provided\n"); + return false; + } else if (!strncmp(s, "enable", strlen(s))) { + *out = true; + return true; + } else if (!strncmp(s, "disable", strlen(s))) { + *out = false; + return true; + } else { + gdb_outf("Argument '%s' not recognized as 'enable' or 'disable'\n", s); + return false; + } +} + static bool cmd_connect_srst(target *t, int argc, const char **argv) { (void)t; - if (argc == 1) + bool print_status = false; + if (argc == 1) { + print_status = true; + } else if (argc == 2) { + if (parse_enable_or_disable(argv[1], &connect_assert_srst)) { + print_status = true; + } + } else { + gdb_outf("Unrecognized command format\n"); + } + + if (print_status) { gdb_outf("Assert SRST during connect: %s\n", connect_assert_srst ? "enabled" : "disabled"); - else - connect_assert_srst = !strcmp(argv[1], "enable"); + } return true; } @@ -280,11 +306,18 @@ static bool cmd_hard_srst(void) static bool cmd_target_power(target *t, int argc, const char **argv) { (void)t; - if (argc == 1) + if (argc == 1) { gdb_outf("Target Power: %s\n", platform_target_get_power() ? "enabled" : "disabled"); - else - platform_target_set_power(!strncmp(argv[1], "enable", strlen(argv[1]))); + } else if (argc == 2) { + bool want_enable = false; + if (parse_enable_or_disable(argv[1], &want_enable)) { + platform_target_set_power(want_enable); + gdb_outf("%s target power\n", want_enable ? "Enabling" : "Disabling"); + } + } else { + gdb_outf("Unrecognized command format\n"); + } return true; } #endif @@ -313,11 +346,21 @@ static bool cmd_traceswo(target *t, int argc, const char **argv) static bool cmd_debug_bmp(target *t, int argc, const char **argv) { (void)t; - if (argc > 1) { - debug_bmp = !strcmp(argv[1], "enable"); + bool print_status = false; + if (argc == 1) { + print_status = true; + } else if (argc == 2) { + if (parse_enable_or_disable(argv[1], &debug_bmp)) { + print_status = true; + } + } else { + gdb_outf("Unrecognized command format\n"); + } + + if (print_status) { + gdb_outf("Debug mode is %s\n", + debug_bmp ? "enabled" : "disabled"); } - gdb_outf("Debug mode is %s\n", - debug_bmp ? "enabled" : "disabled"); return true; } #endif diff --git a/src/include/command.h b/src/include/command.h index 539af5c..9de888a 100644 --- a/src/include/command.h +++ b/src/include/command.h @@ -21,9 +21,19 @@ #ifndef __COMMAND_H #define __COMMAND_H +#include + #include "target.h" int command_process(target *t, char *cmd); +/* + * Attempts to parse a string as either being "enable" or "disable". + * If the parse is successful, returns true and sets the out param to + * indicate what was parsed. If not successful, emits a warning to the + * gdb port, returns false and leaves out untouched. + */ +bool parse_enable_or_disable(const char *s, bool *out); + #endif diff --git a/src/include/target.h b/src/include/target.h index 3ec73d5..8704458 100644 --- a/src/include/target.h +++ b/src/include/target.h @@ -26,6 +26,10 @@ #define __TARGET_H #include +#include +#include +#include +#include typedef struct target_s target; typedef uint32_t target_addr; diff --git a/src/target/cortexm.c b/src/target/cortexm.c index 6ffdfb4..233eb17 100644 --- a/src/target/cortexm.c +++ b/src/target/cortexm.c @@ -32,6 +32,7 @@ #include "target_internal.h" #include "cortexm.h" #include "platform.h" +#include "command.h" #include @@ -874,7 +875,7 @@ static bool cortexm_vector_catch(target *t, int argc, char *argv[]) uint32_t tmp = 0; unsigned i; - if ((argc < 3) || ((argv[1][0] != 'e') && (argv[1][0] != 'd'))) { + if (argc < 3) { tc_printf(t, "usage: monitor vector_catch (enable|disable) " "(hard|int|bus|stat|chk|nocp|mm|reset)\n"); } else { @@ -884,12 +885,16 @@ static bool cortexm_vector_catch(target *t, int argc, char *argv[]) tmp |= 1 << i; } - if (argv[1][0] == 'e') - priv->demcr |= tmp; - else - priv->demcr &= ~tmp; + bool enable; + if (parse_enable_or_disable(argv[1], &enable)) { + if (enable) { + priv->demcr |= tmp; + } else { + priv->demcr &= ~tmp; + } - target_mem_write32(t, CORTEXM_DEMCR, priv->demcr); + target_mem_write32(t, CORTEXM_DEMCR, priv->demcr); + } } tc_printf(t, "Catching vectors: "); diff --git a/src/target/kinetis.c b/src/target/kinetis.c index f1f39c5..331e38f 100644 --- a/src/target/kinetis.c +++ b/src/target/kinetis.c @@ -33,6 +33,7 @@ * K64 Sub-Family Reference Manual, Rev. 2, */ +#include "command.h" #include "general.h" #include "target.h" #include "target_internal.h" @@ -81,11 +82,12 @@ const struct command_s kinetis_cmd_list[] = { static bool kinetis_cmd_unsafe(target *t, int argc, char *argv[]) { - if (argc == 1) + if (argc == 1) { tc_printf(t, "Allow programming security byte: %s\n", unsafe_enabled ? "enabled" : "disabled"); - else - unsafe_enabled = argv[1][0] == 'e'; + } else { + parse_enable_or_disable(argv[1], &unsafe_enabled); + } return true; } diff --git a/src/target/nxpke04.c b/src/target/nxpke04.c index 25ee1a4..92b7079 100644 --- a/src/target/nxpke04.c +++ b/src/target/nxpke04.c @@ -35,6 +35,7 @@ * KE04 Sub-Family Data Sheet */ +#include "command.h" #include "general.h" #include "target.h" #include "target_internal.h" @@ -170,11 +171,12 @@ static bool ke04_cmd_mass_erase(target *t, int argc, char *argv[]) static bool kinetis_cmd_unsafe(target *t, int argc, char *argv[]) { - if (argc == 1) + if (argc == 1) { tc_printf(t, "Allow programming security byte: %s\n", unsafe_enabled ? "enabled" : "disabled"); - else - unsafe_enabled = argv[1][0] == 'e'; + } else { + parse_enable_or_disable(argv[1], &unsafe_enabled); + } return true; }