Merge pull request #450 from jelson/prefix-fix-2

Improve parsing of commands that require enable or disable arguments
This commit is contained in:
UweBonnes 2019-03-20 11:23:11 +01:00 committed by GitHub
commit 88ec557683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 88 additions and 22 deletions

View File

@ -247,14 +247,40 @@ bool cmd_morse(void)
return true; 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) static bool cmd_connect_srst(target *t, int argc, const char **argv)
{ {
(void)t; (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", gdb_outf("Assert SRST during connect: %s\n",
connect_assert_srst ? "enabled" : "disabled"); connect_assert_srst ? "enabled" : "disabled");
else }
connect_assert_srst = !strcmp(argv[1], "enable");
return true; return true;
} }
@ -280,11 +306,18 @@ static bool cmd_hard_srst(void)
static bool cmd_target_power(target *t, int argc, const char **argv) static bool cmd_target_power(target *t, int argc, const char **argv)
{ {
(void)t; (void)t;
if (argc == 1) if (argc == 1) {
gdb_outf("Target Power: %s\n", gdb_outf("Target Power: %s\n",
platform_target_get_power() ? "enabled" : "disabled"); platform_target_get_power() ? "enabled" : "disabled");
else } else if (argc == 2) {
platform_target_set_power(!strncmp(argv[1], "enable", strlen(argv[1]))); 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; return true;
} }
#endif #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) static bool cmd_debug_bmp(target *t, int argc, const char **argv)
{ {
(void)t; (void)t;
if (argc > 1) { bool print_status = false;
debug_bmp = !strcmp(argv[1], "enable"); 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", gdb_outf("Debug mode is %s\n",
debug_bmp ? "enabled" : "disabled"); debug_bmp ? "enabled" : "disabled");
}
return true; return true;
} }
#endif #endif

View File

@ -21,9 +21,19 @@
#ifndef __COMMAND_H #ifndef __COMMAND_H
#define __COMMAND_H #define __COMMAND_H
#include <stdbool.h>
#include "target.h" #include "target.h"
int command_process(target *t, char *cmd); 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 #endif

View File

@ -26,6 +26,10 @@
#define __TARGET_H #define __TARGET_H
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
typedef struct target_s target; typedef struct target_s target;
typedef uint32_t target_addr; typedef uint32_t target_addr;

View File

@ -32,6 +32,7 @@
#include "target_internal.h" #include "target_internal.h"
#include "cortexm.h" #include "cortexm.h"
#include "platform.h" #include "platform.h"
#include "command.h"
#include <unistd.h> #include <unistd.h>
@ -874,7 +875,7 @@ static bool cortexm_vector_catch(target *t, int argc, char *argv[])
uint32_t tmp = 0; uint32_t tmp = 0;
unsigned i; 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) " tc_printf(t, "usage: monitor vector_catch (enable|disable) "
"(hard|int|bus|stat|chk|nocp|mm|reset)\n"); "(hard|int|bus|stat|chk|nocp|mm|reset)\n");
} else { } else {
@ -884,13 +885,17 @@ static bool cortexm_vector_catch(target *t, int argc, char *argv[])
tmp |= 1 << i; tmp |= 1 << i;
} }
if (argv[1][0] == 'e') bool enable;
if (parse_enable_or_disable(argv[1], &enable)) {
if (enable) {
priv->demcr |= tmp; priv->demcr |= tmp;
else } else {
priv->demcr &= ~tmp; priv->demcr &= ~tmp;
}
target_mem_write32(t, CORTEXM_DEMCR, priv->demcr); target_mem_write32(t, CORTEXM_DEMCR, priv->demcr);
} }
}
tc_printf(t, "Catching vectors: "); tc_printf(t, "Catching vectors: ");
for (i = 0; i < sizeof(vectors) / sizeof(char*); i++) { for (i = 0; i < sizeof(vectors) / sizeof(char*); i++) {

View File

@ -33,6 +33,7 @@
* K64 Sub-Family Reference Manual, Rev. 2, * K64 Sub-Family Reference Manual, Rev. 2,
*/ */
#include "command.h"
#include "general.h" #include "general.h"
#include "target.h" #include "target.h"
#include "target_internal.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[]) 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", tc_printf(t, "Allow programming security byte: %s\n",
unsafe_enabled ? "enabled" : "disabled"); unsafe_enabled ? "enabled" : "disabled");
else } else {
unsafe_enabled = argv[1][0] == 'e'; parse_enable_or_disable(argv[1], &unsafe_enabled);
}
return true; return true;
} }

View File

@ -35,6 +35,7 @@
* KE04 Sub-Family Data Sheet * KE04 Sub-Family Data Sheet
*/ */
#include "command.h"
#include "general.h" #include "general.h"
#include "target.h" #include "target.h"
#include "target_internal.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[]) 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", tc_printf(t, "Allow programming security byte: %s\n",
unsafe_enabled ? "enabled" : "disabled"); unsafe_enabled ? "enabled" : "disabled");
else } else {
unsafe_enabled = argv[1][0] == 'e'; parse_enable_or_disable(argv[1], &unsafe_enabled);
}
return true; return true;
} }