kdb code refactoring
Subsystems (2)
v5 cover kdb code refactoring
Some more kdb code refactoring related to:
- Removal of redundant kdb_register_flags().
- Simplification of kdb defcmd macro logic.
Tested with kgdbtest on arm64, doesn't show any regressions.
Changes in v5:
- Incorporated minor comments from Doug.
- Added Doug's review tag.
Changes in v4:
- Split rename of "defcmd_set" to "kdb_macro" as a separate patch.
- Incorporated misc. comments from Doug.
- Added a patch that removes redundant prefix "cmd_" from name of members of struct kdbtab_t.
Changes in v3:
- Split patch into 2 for ease of review.
- Get rid of kdb_register_flags() completely via switching all user to register pre-allocated kdb commands.
Changes in v2:
- Define new structs: kdb_macro_t and kdb_macro_cmd_t instead of modifying existing kdb command struct and struct kdb_subcmd.
- Reword commit message.
Sumit Garg (4):
kdb: Rename struct defcmd_set to struct kdb_macro
kdb: Get rid of redundant kdb_register_flags()
kdb: Simplify kdb_defcmd macro logic
kdb: Rename members of struct kdbtab_t
include/linux/kdb.h | 27 +- kernel/debug/kdb/kdb_bp.c | 72 ++-- kernel/debug/kdb/kdb_main.c | 626 +++++++++++++++------------------ kernel/debug/kdb/kdb_private.h | 13 - kernel/trace/trace_kdb.c | 12 +- samples/kdb/kdb_hello.c | 20 +- 6 files changed, 357 insertions(+), 413 deletions(-)
Signature · 1 line
2.25.1
PATCH v5 1/4 kdb: Rename struct defcmd_set to struct kdb_macro
Rename struct defcmd_set to struct kdb_macro as that sounds more appropriate given its purpose.
--- kernel/debug/kdb/kdb_main.c | 40 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-)
kernel/debug/kdb/kdb_main.c +20 -20
@@ -654,7 +654,7 @@ static void kdb_cmderror(int diag)* Returns: * zero for success, a kdb diagnostic if error */ -struct defcmd_set { +struct kdb_macro {int count;bool usable;char *name;
@@ -662,8 +662,8 @@ struct defcmd_set {char *help;char **command; }; -static struct defcmd_set *defcmd_set; -static int defcmd_set_count; +static struct kdb_macro *kdb_macro; +static int kdb_macro_count;static bool defcmd_in_progress;/* Forward references */
@@ -671,7 +671,7 @@ static int kdb_exec_defcmd(int argc, const char **argv);static int kdb_defcmd2 (const char *cmdstr,const char *argv0) { -struct defcmd_set *s = defcmd_set + defcmd_set_count -1 ; +struct kdb_macro *s = kdb_macro + kdb_macro_count -1 ;char **save_command = s->command;if (strcmp (argv0,"endefcmd" ) ==0 ) { defcmd_in_progress = false;
@@ -704,7 +704,7 @@ static int kdb_defcmd2(const char *cmdstr, const char *argv0)static int kdb_defcmd (int argc,const char **argv) { -struct defcmd_set *save_defcmd_set = defcmd_set, *s; +struct kdb_macro *save_kdb_macro = kdb_macro, *s;if (defcmd_in_progress) {kdb_printf ("kdb: nested defcmd detected, assuming missing " "endefcmd\n" );
@@ -712,7 +712,7 @@ static int kdb_defcmd(int argc, const char **argv) }if (argc ==0 ) {int i; -for (s = defcmd_set; s < defcmd_set + defcmd_set_count; ++s) { +for (s = kdb_macro; s < kdb_macro + kdb_macro_count; ++s) {kdb_printf ("defcmd %s \"%s\" \"%s\"\n" , s->name, s->usage, s->help);for (i =0 ; i < s->count; ++i)
@@ -727,13 +727,13 @@ static int kdb_defcmd(int argc, const char **argv)kdb_printf ("Command only available during kdb_init()\n" );return KDB_NOTIMP ; } - defcmd_set =kmalloc_array (defcmd_set_count +1 ,sizeof (*defcmd_set), -GFP_KDB ); -if (!defcmd_set) + kdb_macro =kmalloc_array (kdb_macro_count +1 ,sizeof (*kdb_macro), +GFP_KDB ); +if (!kdb_macro) goto fail_defcmd; -memcpy (defcmd_set, save_defcmd_set, - defcmd_set_count *sizeof (*defcmd_set)); - s = defcmd_set + defcmd_set_count; +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; s->name =kdb_strdup (argv[1 ],GFP_KDB );
@@ -753,19 +753,19 @@ static int kdb_defcmd(int argc, const char **argv)strcpy (s->help, argv[3 ]+1 ); s->help[strlen (s->help)-1 ] ='\0' ; } - ++defcmd_set_count; + ++kdb_macro_count; defcmd_in_progress = true; -kfree (save_defcmd_set); +kfree (save_kdb_macro);return 0 ; fail_help:kfree (s->usage); fail_usage:kfree (s->name); fail_name: -kfree (defcmd_set); +kfree (kdb_macro); fail_defcmd: -kdb_printf ("Could not allocate new defcmd_set entry for %s\n" , argv[1 ]); - defcmd_set = save_defcmd_set; +kdb_printf ("Could not allocate new kdb_macro entry for %s\n" , argv[1 ]); + kdb_macro = save_kdb_macro;return KDB_NOTIMP ; }
@@ -781,14 +781,14 @@ static int kdb_defcmd(int argc, const char **argv)static int kdb_exec_defcmd (int argc,const char **argv) {int i, ret; -struct defcmd_set *s; +struct kdb_macro *s;if (argc !=0 )return KDB_ARGCOUNT ; -for (s = defcmd_set, i =0 ; i < defcmd_set_count; ++i, ++s) { +for (s = kdb_macro, i =0 ; i < kdb_macro_count; ++i, ++s) {if (strcmp (s->name, argv[0 ]) ==0 )break ; } -if (i == defcmd_set_count) { +if (i == kdb_macro_count) {kdb_printf ("kdb_exec_defcmd: could not find commands for %s\n" , argv[0 ]);return KDB_NOTIMP ;
Signature · 1 line
2.25.1
PATCH v5 2/4 kdb: Get rid of redundant kdb_register_flags()
In reply to Sumit GargCommit e4f291b3f7bb ("kdb: Simplify kdb commands registration") allowed registration of pre-allocated kdb commands with pointer to struct kdbtab_t. Lets switch other users as well to register pre- allocated kdb commands via:
- Changing prototype for kdb_register() to pass a pointer to struct kdbtab_t instead.
- Embed kdbtab_t structure in kdb_macro_t rather than individual params.
With these changes kdb_register_flags() becomes redundant and hence removed. Also, since we have switched all users to register pre-allocated commands, "is_dynamic" flag in struct kdbtab_t becomes redundant and hence removed as well.
--- include/linux/kdb.h | 27 ++++-- kernel/debug/kdb/kdb_main.c | 167 +++++++++++---------------------- kernel/debug/kdb/kdb_private.h | 13 --- kernel/trace/trace_kdb.c | 12 ++- samples/kdb/kdb_hello.c | 20 ++-- 5 files changed, 88 insertions(+), 151 deletions(-)
include/linux/kdb.h +17 -10
@@ -13,6 +13,8 @@* Copyright (C) 2009 Jason Wessel <[email protected]> */ +#include <linux/list.h> +/* Shifted versions of the command enable bits are be used if the command * has no arguments (see kdb_check_flags). This allows commands, such as * go, to have different permissions depending upon whether it is called
@@ -64,6 +66,17 @@ typedef enum {typedef int (*kdb_func_t )(int ,const char **); +/* The KDB shell command table */ +typedef struct _kdbtab { +char *cmd_name;/* Command name */ +kdb_func_t cmd_func;/* Function to execute command */ +char *cmd_usage;/* Usage String for this command */ +char *cmd_help;/* Help message for this command */ +short cmd_minlen;/* Minimum legal # cmd chars required */ +kdb_cmdflags_t cmd_flags;/* Command behaviour flags */ +struct list_head list_node;/* Command list */ +}kdbtab_t ; +#ifdef CONFIG_KGDB_KDB #include <linux/init.h> #include <linux/sched.h>
@@ -193,19 +206,13 @@ static inline const char *kdb_walk_kallsyms(loff_t *pos)#endif /* ! CONFIG_KALLSYMS */ /* Dynamic kdb shell command registration */ -extern int kdb_register (char *,kdb_func_t ,char *,char *,short ); -extern int kdb_register_flags (char *,kdb_func_t ,char *,char *, -short ,kdb_cmdflags_t ); -extern int kdb_unregister (char *); +extern int kdb_register (kdbtab_t *cmd); +extern void kdb_unregister (kdbtab_t *cmd);#else /* ! CONFIG_KGDB_KDB */ static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }static inline void kdb_init (int level) {} -static inline int kdb_register (char *cmd,kdb_func_t func,char *usage, -char *help,short minlen) {return 0 ; } -static inline int kdb_register_flags (char *cmd,kdb_func_t func,char *usage, -char *help,short minlen, -kdb_cmdflags_t flags) {return 0 ; } -static inline int kdb_unregister (char *cmd) {return 0 ; } +static inline int kdb_register (kdbtab_t *cmd) {return 0 ; } +static inline void kdb_unregister (kdbtab_t *cmd) {}#endif /* CONFIG_KGDB_KDB */ enum {KDB_NOT_INITIALIZED ,
kernel/debug/kdb/kdb_main.c +53 -114
258 lines not on this page · raw message
kernel/debug/kdb/kdb_private.h +0 -13
@@ -164,19 +164,6 @@ typedef struct _kdb_bp {#ifdef CONFIG_KGDB_KDB extern kdb_bp_t kdb_breakpoints[/* KDB_MAXBPT */ ]; -/* The KDB shell command table */ -typedef struct _kdbtab { -char *cmd_name;/* Command name */ -kdb_func_t cmd_func;/* Function to execute command */ -char *cmd_usage;/* Usage String for this command */ -char *cmd_help;/* Help message for this command */ -short cmd_minlen;/* Minimum legal # command -* chars required */ -kdb_cmdflags_t cmd_flags;/* Command behaviour flags */ -struct list_head list_node;/* Command list */ -bool is_dynamic;/* Command table allocation type */ -}kdbtab_t ; -extern void kdb_register_table (kdbtab_t *kp,size_t len);extern int kdb_bt (int ,const char **);/* KDB display back trace */
kernel/trace/trace_kdb.c +9 -3
@@ -147,11 +147,17 @@ static int kdb_ftdump(int argc, const char **argv)return 0 ; } +static kdbtab_t ftdump_cmd = { + .cmd_name ="ftdump" , + .cmd_func = kdb_ftdump, + .cmd_usage ="[skip_#entries] [cpu]" , + .cmd_help ="Dump ftrace log; -skip dumps last #entries" , + .cmd_flags =KDB_ENABLE_ALWAYS_SAFE , +}; +static __init int kdb_ftrace_register (void ) { -kdb_register_flags ("ftdump" , kdb_ftdump,"[skip_#entries] [cpu]" , -"Dump ftrace log; -skip dumps last #entries" ,0 , -KDB_ENABLE_ALWAYS_SAFE ); +kdb_register (&ftdump_cmd);return 0 ; }
samples/kdb/kdb_hello.c +9 -11
@@ -28,28 +28,26 @@ static int kdb_hello_cmd(int argc, const char **argv)return 0 ; } +static kdbtab_t hello_cmd = { + .cmd_name ="hello" , + .cmd_func = kdb_hello_cmd, + .cmd_usage ="[string]" , + .cmd_help ="Say Hello World or Hello [string]" , +};static int __init kdb_hello_cmd_init (void ) {/* * Registration of a dynamically added kdb command is done with -* kdb_register() with the arguments being: -* 1: The name of the shell command -* 2: The function that processes the command -* 3: Description of the usage of any arguments -* 4: Descriptive text when you run help -* 5: Number of characters to complete the command -* 0 == type the whole command -* 1 == match both "g" and "go" for example +* kdb_register(). */ -kdb_register ("hello" , kdb_hello_cmd,"[string]" , -"Say Hello World or Hello [string]" ,0 ); +kdb_register (&hello_cmd);return 0 ; } static void __exit kdb_hello_cmd_exit(void) { -kdb_unregister ("hello" ); +kdb_unregister (&hello_cmd); }module_init (kdb_hello_cmd_init);
Signature · 1 line
2.25.1
PATCH v5 3/4 kdb: Simplify kdb_defcmd macro logic
In reply to Sumit GargSwitch to use a linked list instead of dynamic array which makes allocation of kdb macro and traversing the kdb macro commands list simpler.
--- kernel/debug/kdb/kdb_main.c | 107 +++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 49 deletions(-)
kernel/debug/kdb/kdb_main.c +58 -49
@@ -654,13 +654,16 @@ static void kdb_cmderror(int diag)* zero for success, a kdb diagnostic if error */ struct kdb_macro { -int count; -bool usable; -kdbtab_t cmd; -char **command; +kdbtab_t cmd;/* Macro command */ +struct list_head statements;/* Associated statement list */ }; + +struct kdb_macro_statement { +char *statement;/* Statement text */ +struct list_head list_node;/* Statement list node */ +}; +static struct kdb_macro *kdb_macro; -static int kdb_macro_count;static bool defcmd_in_progress;/* Forward references */
@@ -668,34 +671,33 @@ static int kdb_exec_defcmd(int argc, const char **argv);static int kdb_defcmd2 (const char *cmdstr,const char *argv0) { -struct kdb_macro *s = kdb_macro + kdb_macro_count -1 ; -char **save_command = s->command; +struct kdb_macro_statement *kms; + +if (!kdb_macro) +return KDB_NOTIMP ; +if (strcmp (argv0,"endefcmd" ) ==0 ) { defcmd_in_progress = false; -if (!s->count) - s->usable = false; -if (s->usable) -kdb_register (&s->cmd); +if (!list_empty (&kdb_macro->statements)) +kdb_register (&kdb_macro->cmd);return 0 ; } -if (!s->usable) -return KDB_NOTIMP ; - s->command =kcalloc (s->count +1 ,sizeof (*(s->command)),GFP_KDB ); -if (!s->command) { -kdb_printf ("Could not allocate new kdb_defcmd table for %s\n" , + + kms =kmalloc (sizeof (*kms),GFP_KDB ); +if (!kms) { +kdb_printf ("Could not allocate new kdb macro command: %s\n" , cmdstr); - s->usable = false;return KDB_NOTIMP ; } -memcpy (s->command, save_command, s->count *sizeof (*(s->command))); - s->command[s->count++] =kdb_strdup (cmdstr,GFP_KDB ); -kfree (save_command); + + kms->statement =kdb_strdup (cmdstr,GFP_KDB ); +list_add_tail (&kms->list_node, &kdb_macro->statements); +return 0 ; }static int kdb_defcmd (int argc,const char **argv) { -struct kdb_macro *save_kdb_macro = kdb_macro, *s;kdbtab_t *mp;if (defcmd_in_progress) {
@@ -704,13 +706,21 @@ static int kdb_defcmd(int argc, const char **argv)kdb_defcmd2 ("endefcmd" ,"endefcmd" ); }if (argc ==0 ) { -int i; -for (s = kdb_macro; s < kdb_macro + kdb_macro_count; ++s) { -kdb_printf ("defcmd %s \"%s\" \"%s\"\n" , s->cmd.cmd_name, - s->cmd.cmd_usage, s->cmd.cmd_help); -for (i =0 ; i < s->count; ++i) -kdb_printf ("%s" , s->command[i]); -kdb_printf ("endefcmd\n" ); +kdbtab_t *kp; +struct kdb_macro *kmp; +struct kdb_macro_statement *kms; + +list_for_each_entry (kp, &kdb_cmds_head, list_node) { +if (kp->cmd_func == kdb_exec_defcmd) { +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 ; }
@@ -720,17 +730,11 @@ static int kdb_defcmd(int argc, const char **argv)kdb_printf ("Command only available during kdb_init()\n" );return KDB_NOTIMP ; } - kdb_macro =kmalloc_array (kdb_macro_count +1 ,sizeof (*kdb_macro), -GFP_KDB ); + kdb_macro =kzalloc (sizeof (*kdb_macro),GFP_KDB );if (!kdb_macro) 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_minlen =0 ; mp->cmd_flags =KDB_ENABLE_ALWAYS_SAFE ;
@@ -751,9 +755,9 @@ static int kdb_defcmd(int argc, const char **argv)strcpy (mp->cmd_help, argv[3 ]+1 ); mp->cmd_help[strlen (mp->cmd_help)-1 ] ='\0' ; } - ++kdb_macro_count; + +INIT_LIST_HEAD (&kdb_macro->statements); defcmd_in_progress = true; -kfree (save_kdb_macro);return 0 ; fail_help:kfree (mp->cmd_usage);
@@ -763,7 +767,6 @@ static int kdb_defcmd(int argc, const char **argv)kfree (kdb_macro); fail_defcmd:kdb_printf ("Could not allocate new kdb_macro entry for %s\n" , argv[1 ]); - kdb_macro = save_kdb_macro;return KDB_NOTIMP ; }
@@ -778,25 +781,31 @@ static int kdb_defcmd(int argc, const char **argv)*/ static int kdb_exec_defcmd (int argc,const char **argv) { -int i, ret; -struct kdb_macro *s; +int ret; +kdbtab_t *kp; +struct kdb_macro *kmp; +struct kdb_macro_statement *kms; +if (argc !=0 )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 ; } -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" , argv[0 ]);return KDB_NOTIMP ; } -for (i =0 ; i < s->count; ++i) { -/* Recursive use of kdb_parse, do not use argv after -* this point */ + kmp = container_of(kp, struct kdb_macro, cmd); +list_for_each_entry (kms, &kmp->statements, list_node) { +/* +* Recursive use of kdb_parse, do not use argv after this point. +*/ argv =NULL ; -kdb_printf ("[%s]kdb> %s\n" , s->cmd.cmd_name, s->command[i]); - ret =kdb_parse (s->command[i]); +kdb_printf ("[%s]kdb> %s\n" , kmp->cmd.cmd_name, kms->statement); + ret =kdb_parse (kms->statement);if (ret)return ret; }
Signature · 1 line
2.25.1
PATCH v5 4/4 kdb: Rename members of struct kdbtab_t
In reply to Sumit GargRemove redundant prefix "cmd_" from name of members in struct kdbtab_t for better readibility.
--- include/linux/kdb.h | 12 +- kernel/debug/kdb/kdb_bp.c | 72 +++---- kernel/debug/kdb/kdb_main.c | 404 ++++++++++++++++++------------------ kernel/trace/trace_kdb.c | 10 +- samples/kdb/kdb_hello.c | 8 +- 5 files changed, 252 insertions(+), 254 deletions(-)
include/linux/kdb.h +6 -6
@@ -68,12 +68,12 @@ typedef int (*kdb_func_t)(int, const char **);/* The KDB shell command table */ typedef struct _kdbtab { -char *cmd_name;/* Command name */ -kdb_func_t cmd_func;/* Function to execute command */ -char *cmd_usage;/* Usage String for this command */ -char *cmd_help;/* Help message for this command */ -short cmd_minlen;/* Minimum legal # cmd chars required */ -kdb_cmdflags_t cmd_flags;/* Command behaviour flags */ +char *name;/* Command name */ +kdb_func_t func;/* Function to execute command */ +char *usage;/* Usage String for this command */ +char *help;/* Help message for this command */ +short minlen;/* Minimum legal # cmd chars required */ +kdb_cmdflags_t flags;/* Command behaviour flags */ struct list_head list_node;/* Command list */ }kdbtab_t ;
kernel/debug/kdb/kdb_bp.c +36 -36
87 lines not on this page · raw message
kernel/debug/kdb/kdb_main.c +201 -203
536 lines not on this page · raw message
kernel/trace/trace_kdb.c +5 -5
@@ -148,11 +148,11 @@ static int kdb_ftdump(int argc, const char **argv) }static kdbtab_t ftdump_cmd = { - .cmd_name ="ftdump" , - .cmd_func = kdb_ftdump, - .cmd_usage ="[skip_#entries] [cpu]" , - .cmd_help ="Dump ftrace log; -skip dumps last #entries" , - .cmd_flags =KDB_ENABLE_ALWAYS_SAFE , + .name ="ftdump" , + .func = kdb_ftdump, + .usage ="[skip_#entries] [cpu]" , + .help ="Dump ftrace log; -skip dumps last #entries" , + .flags =KDB_ENABLE_ALWAYS_SAFE , };static __init int kdb_ftrace_register (void )
samples/kdb/kdb_hello.c +4 -4
@@ -29,10 +29,10 @@ static int kdb_hello_cmd(int argc, const char **argv) }static kdbtab_t hello_cmd = { - .cmd_name ="hello" , - .cmd_func = kdb_hello_cmd, - .cmd_usage ="[string]" , - .cmd_help ="Say Hello World or Hello [string]" , + .name ="hello" , + .func = kdb_hello_cmd, + .usage ="[string]" , + .help ="Say Hello World or Hello [string]" , };static int __init kdb_hello_cmd_init (void )
Signature · 1 line
2.25.1
Quoted 26 lines · Sumit Garg
Some more kdb code refactoring related to:
- Removal of redundantkdb_register_flags().
- Simplification of kdb defcmd macro logic.Tested with kgdbtest on arm64, doesn't show any regressions.
Changes in v5:
- Incorporated minor comments from Doug.
- Added Doug's review tag.Changes in v4:
- Split rename of "defcmd_set" to "kdb_macro" as a separate patch.
- Incorporated misc. comments from Doug.
- Added a patch that removes redundant prefix "cmd_" from name of members of structkdbtab_t.Changes in v3:
- Split patch into 2 for ease of review.
- Get rid ofkdb_register_flags()completely via switching all user to register pre-allocated kdb commands.Changes in v2:
- Define new structs:kdb_macro_tandkdb_macro_cmd_tinstead of modifying existing kdb command struct and structkdb_subcmd.
- Reword commit message.
Quoted 5 lines
Sumit Garg (4):
kdb: Rename structdefcmd_setto structkdb_macro
kdb: Get rid of redundantkdb_register_flags()
kdb: Simplifykdb_defcmdmacro logic
kdb: Rename members of structkdbtab_t
Series applied, thanks!
Quoted 11 lines
include/linux/kdb.h | 27 +- kernel/debug/kdb/kdb_bp.c | 72 ++-- kernel/debug/kdb/kdb_main.c | 626 +++++++++++++++------------------ kernel/debug/kdb/kdb_private.h | 13 - kernel/trace/trace_kdb.c | 12 +- samples/kdb/kdb_hello.c | 20 +- 6 files changed, 357 insertions(+), 413 deletions(-)Signature · 1 line
2.25.1