Commit 9a5db530 authored by Sumit Garg's avatar Sumit Garg Committed by Daniel Thompson

kdb: Simplify kdb_defcmd macro logic

Switch to use a linked list instead of dynamic array which makes
allocation of kdb macro and traversing the kdb macro commands list
simpler.
Suggested-by: default avatarDaniel Thompson <daniel.thompson@linaro.org>
Signed-off-by: default avatarSumit Garg <sumit.garg@linaro.org>
Reviewed-by: default avatarDouglas Anderson <dianders@chromium.org>
Link: https://lore.kernel.org/r/20210712134620.276667-4-sumit.garg@linaro.orgSigned-off-by: default avatarDaniel Thompson <daniel.thompson@linaro.org>
parent c25abcd6
...@@ -654,13 +654,16 @@ static void kdb_cmderror(int diag) ...@@ -654,13 +654,16 @@ static void kdb_cmderror(int diag)
* zero for success, a kdb diagnostic if error * zero for success, a kdb diagnostic if error
*/ */
struct kdb_macro { struct kdb_macro {
int count; kdbtab_t cmd; /* Macro command */
bool usable; struct list_head statements; /* Associated statement list */
kdbtab_t cmd;
char **command;
}; };
struct kdb_macro_statement {
char *statement; /* Statement text */
struct list_head list_node; /* Statement list node */
};
static struct kdb_macro *kdb_macro; static struct kdb_macro *kdb_macro;
static int kdb_macro_count;
static bool defcmd_in_progress; static bool defcmd_in_progress;
/* Forward references */ /* Forward references */
...@@ -668,34 +671,33 @@ static int kdb_exec_defcmd(int argc, const char **argv); ...@@ -668,34 +671,33 @@ static int kdb_exec_defcmd(int argc, const char **argv);
static int kdb_defcmd2(const char *cmdstr, const char *argv0) static int kdb_defcmd2(const char *cmdstr, const char *argv0)
{ {
struct kdb_macro *s = kdb_macro + kdb_macro_count - 1; struct kdb_macro_statement *kms;
char **save_command = s->command;
if (!kdb_macro)
return KDB_NOTIMP;
if (strcmp(argv0, "endefcmd") == 0) { if (strcmp(argv0, "endefcmd") == 0) {
defcmd_in_progress = false; defcmd_in_progress = false;
if (!s->count) if (!list_empty(&kdb_macro->statements))
s->usable = false; kdb_register(&kdb_macro->cmd);
if (s->usable)
kdb_register(&s->cmd);
return 0; return 0;
} }
if (!s->usable)
return KDB_NOTIMP; kms = kmalloc(sizeof(*kms), GFP_KDB);
s->command = kcalloc(s->count + 1, sizeof(*(s->command)), GFP_KDB); if (!kms) {
if (!s->command) { kdb_printf("Could not allocate new kdb macro command: %s\n",
kdb_printf("Could not allocate new kdb_defcmd table for %s\n",
cmdstr); cmdstr);
s->usable = false;
return KDB_NOTIMP; return KDB_NOTIMP;
} }
memcpy(s->command, save_command, s->count * sizeof(*(s->command)));
s->command[s->count++] = kdb_strdup(cmdstr, GFP_KDB); kms->statement = kdb_strdup(cmdstr, GFP_KDB);
kfree(save_command); list_add_tail(&kms->list_node, &kdb_macro->statements);
return 0; return 0;
} }
static int kdb_defcmd(int argc, const char **argv) static int kdb_defcmd(int argc, const char **argv)
{ {
struct kdb_macro *save_kdb_macro = kdb_macro, *s;
kdbtab_t *mp; kdbtab_t *mp;
if (defcmd_in_progress) { if (defcmd_in_progress) {
...@@ -704,13 +706,21 @@ static int kdb_defcmd(int argc, const char **argv) ...@@ -704,13 +706,21 @@ static int kdb_defcmd(int argc, const char **argv)
kdb_defcmd2("endefcmd", "endefcmd"); kdb_defcmd2("endefcmd", "endefcmd");
} }
if (argc == 0) { if (argc == 0) {
int i; kdbtab_t *kp;
for (s = kdb_macro; s < kdb_macro + kdb_macro_count; ++s) { struct kdb_macro *kmp;
kdb_printf("defcmd %s \"%s\" \"%s\"\n", s->cmd.cmd_name, struct kdb_macro_statement *kms;
s->cmd.cmd_usage, s->cmd.cmd_help);
for (i = 0; i < s->count; ++i) list_for_each_entry(kp, &kdb_cmds_head, list_node) {
kdb_printf("%s", s->command[i]); if (kp->cmd_func == kdb_exec_defcmd) {
kdb_printf("endefcmd\n"); kdb_printf("defcmd %s \"%s\" \"%s\"\n",
kp->cmd_name, kp->cmd_usage,
kp->cmd_help);
kmp = container_of(kp, struct kdb_macro, cmd);
list_for_each_entry(kms, &kmp->statements,
list_node)
kdb_printf("%s", kms->statement);
kdb_printf("endefcmd\n");
}
} }
return 0; return 0;
} }
...@@ -720,17 +730,11 @@ static int kdb_defcmd(int argc, const char **argv) ...@@ -720,17 +730,11 @@ static int kdb_defcmd(int argc, const char **argv)
kdb_printf("Command only available during kdb_init()\n"); kdb_printf("Command only available during kdb_init()\n");
return KDB_NOTIMP; return KDB_NOTIMP;
} }
kdb_macro = kmalloc_array(kdb_macro_count + 1, sizeof(*kdb_macro), kdb_macro = kzalloc(sizeof(*kdb_macro), GFP_KDB);
GFP_KDB);
if (!kdb_macro) if (!kdb_macro)
goto fail_defcmd; goto fail_defcmd;
memcpy(kdb_macro, save_kdb_macro,
kdb_macro_count * sizeof(*kdb_macro));
s = kdb_macro + kdb_macro_count;
memset(s, 0, sizeof(*s));
s->usable = true;
mp = &s->cmd; mp = &kdb_macro->cmd;
mp->cmd_func = kdb_exec_defcmd; mp->cmd_func = kdb_exec_defcmd;
mp->cmd_minlen = 0; mp->cmd_minlen = 0;
mp->cmd_flags = KDB_ENABLE_ALWAYS_SAFE; mp->cmd_flags = KDB_ENABLE_ALWAYS_SAFE;
...@@ -751,9 +755,9 @@ static int kdb_defcmd(int argc, const char **argv) ...@@ -751,9 +755,9 @@ static int kdb_defcmd(int argc, const char **argv)
strcpy(mp->cmd_help, argv[3]+1); strcpy(mp->cmd_help, argv[3]+1);
mp->cmd_help[strlen(mp->cmd_help)-1] = '\0'; mp->cmd_help[strlen(mp->cmd_help)-1] = '\0';
} }
++kdb_macro_count;
INIT_LIST_HEAD(&kdb_macro->statements);
defcmd_in_progress = true; defcmd_in_progress = true;
kfree(save_kdb_macro);
return 0; return 0;
fail_help: fail_help:
kfree(mp->cmd_usage); kfree(mp->cmd_usage);
...@@ -763,7 +767,6 @@ static int kdb_defcmd(int argc, const char **argv) ...@@ -763,7 +767,6 @@ static int kdb_defcmd(int argc, const char **argv)
kfree(kdb_macro); kfree(kdb_macro);
fail_defcmd: fail_defcmd:
kdb_printf("Could not allocate new kdb_macro entry for %s\n", argv[1]); kdb_printf("Could not allocate new kdb_macro entry for %s\n", argv[1]);
kdb_macro = save_kdb_macro;
return KDB_NOTIMP; return KDB_NOTIMP;
} }
...@@ -778,25 +781,31 @@ static int kdb_defcmd(int argc, const char **argv) ...@@ -778,25 +781,31 @@ static int kdb_defcmd(int argc, const char **argv)
*/ */
static int kdb_exec_defcmd(int argc, const char **argv) static int kdb_exec_defcmd(int argc, const char **argv)
{ {
int i, ret; int ret;
struct kdb_macro *s; kdbtab_t *kp;
struct kdb_macro *kmp;
struct kdb_macro_statement *kms;
if (argc != 0) if (argc != 0)
return KDB_ARGCOUNT; return KDB_ARGCOUNT;
for (s = kdb_macro, i = 0; i < kdb_macro_count; ++i, ++s) {
if (strcmp(s->cmd.cmd_name, argv[0]) == 0) list_for_each_entry(kp, &kdb_cmds_head, list_node) {
if (strcmp(kp->cmd_name, argv[0]) == 0)
break; break;
} }
if (i == kdb_macro_count) { if (list_entry_is_head(kp, &kdb_cmds_head, list_node)) {
kdb_printf("kdb_exec_defcmd: could not find commands for %s\n", kdb_printf("kdb_exec_defcmd: could not find commands for %s\n",
argv[0]); argv[0]);
return KDB_NOTIMP; return KDB_NOTIMP;
} }
for (i = 0; i < s->count; ++i) { kmp = container_of(kp, struct kdb_macro, cmd);
/* Recursive use of kdb_parse, do not use argv after list_for_each_entry(kms, &kmp->statements, list_node) {
* this point */ /*
* Recursive use of kdb_parse, do not use argv after this point.
*/
argv = NULL; argv = NULL;
kdb_printf("[%s]kdb> %s\n", s->cmd.cmd_name, s->command[i]); kdb_printf("[%s]kdb> %s\n", kmp->cmd.cmd_name, kms->statement);
ret = kdb_parse(s->command[i]); ret = kdb_parse(kms->statement);
if (ret) if (ret)
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment