Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

kdb: Get rid of redundant kdb_register_flags()

Commit 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.

Suggested-by: Daniel Thompson <daniel.thompson@linaro.org>
Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
Acked-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Link: https://lore.kernel.org/r/20210712134620.276667-3-sumit.garg@linaro.org
Signed-off-by: Daniel Thompson <daniel.thompson@linaro.org>

authored by

Sumit Garg and committed by
Daniel Thompson
c25abcd6 b39cded8

+88 -151
+17 -10
include/linux/kdb.h
··· 13 13 * Copyright (C) 2009 Jason Wessel <jason.wessel@windriver.com> 14 14 */ 15 15 16 + #include <linux/list.h> 17 + 16 18 /* Shifted versions of the command enable bits are be used if the command 17 19 * has no arguments (see kdb_check_flags). This allows commands, such as 18 20 * go, to have different permissions depending upon whether it is called ··· 65 63 } kdb_cmdflags_t; 66 64 67 65 typedef int (*kdb_func_t)(int, const char **); 66 + 67 + /* The KDB shell command table */ 68 + typedef struct _kdbtab { 69 + char *cmd_name; /* Command name */ 70 + kdb_func_t cmd_func; /* Function to execute command */ 71 + char *cmd_usage; /* Usage String for this command */ 72 + char *cmd_help; /* Help message for this command */ 73 + short cmd_minlen; /* Minimum legal # cmd chars required */ 74 + kdb_cmdflags_t cmd_flags; /* Command behaviour flags */ 75 + struct list_head list_node; /* Command list */ 76 + } kdbtab_t; 68 77 69 78 #ifdef CONFIG_KGDB_KDB 70 79 #include <linux/init.h> ··· 206 193 #endif /* ! CONFIG_KALLSYMS */ 207 194 208 195 /* Dynamic kdb shell command registration */ 209 - extern int kdb_register(char *, kdb_func_t, char *, char *, short); 210 - extern int kdb_register_flags(char *, kdb_func_t, char *, char *, 211 - short, kdb_cmdflags_t); 212 - extern int kdb_unregister(char *); 196 + extern int kdb_register(kdbtab_t *cmd); 197 + extern void kdb_unregister(kdbtab_t *cmd); 213 198 #else /* ! CONFIG_KGDB_KDB */ 214 199 static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; } 215 200 static inline void kdb_init(int level) {} 216 - static inline int kdb_register(char *cmd, kdb_func_t func, char *usage, 217 - char *help, short minlen) { return 0; } 218 - static inline int kdb_register_flags(char *cmd, kdb_func_t func, char *usage, 219 - char *help, short minlen, 220 - kdb_cmdflags_t flags) { return 0; } 221 - static inline int kdb_unregister(char *cmd) { return 0; } 201 + static inline int kdb_register(kdbtab_t *cmd) { return 0; } 202 + static inline void kdb_unregister(kdbtab_t *cmd) {} 222 203 #endif /* CONFIG_KGDB_KDB */ 223 204 enum { 224 205 KDB_NOT_INITIALIZED,
+53 -114
kernel/debug/kdb/kdb_main.c
··· 33 33 #include <linux/kallsyms.h> 34 34 #include <linux/kgdb.h> 35 35 #include <linux/kdb.h> 36 - #include <linux/list.h> 37 36 #include <linux/notifier.h> 38 37 #include <linux/interrupt.h> 39 38 #include <linux/delay.h> ··· 656 657 struct kdb_macro { 657 658 int count; 658 659 bool usable; 659 - char *name; 660 - char *usage; 661 - char *help; 660 + kdbtab_t cmd; 662 661 char **command; 663 662 }; 664 663 static struct kdb_macro *kdb_macro; ··· 675 678 if (!s->count) 676 679 s->usable = false; 677 680 if (s->usable) 678 - /* macros are always safe because when executed each 679 - * internal command re-enters kdb_parse() and is 680 - * safety checked individually. 681 - */ 682 - kdb_register_flags(s->name, kdb_exec_defcmd, s->usage, 683 - s->help, 0, 684 - KDB_ENABLE_ALWAYS_SAFE); 681 + kdb_register(&s->cmd); 685 682 return 0; 686 683 } 687 684 if (!s->usable) ··· 696 705 static int kdb_defcmd(int argc, const char **argv) 697 706 { 698 707 struct kdb_macro *save_kdb_macro = kdb_macro, *s; 708 + kdbtab_t *mp; 709 + 699 710 if (defcmd_in_progress) { 700 711 kdb_printf("kdb: nested defcmd detected, assuming missing " 701 712 "endefcmd\n"); ··· 706 713 if (argc == 0) { 707 714 int i; 708 715 for (s = kdb_macro; s < kdb_macro + kdb_macro_count; ++s) { 709 - kdb_printf("defcmd %s \"%s\" \"%s\"\n", s->name, 710 - s->usage, s->help); 716 + kdb_printf("defcmd %s \"%s\" \"%s\"\n", s->cmd.cmd_name, 717 + s->cmd.cmd_usage, s->cmd.cmd_help); 711 718 for (i = 0; i < s->count; ++i) 712 719 kdb_printf("%s", s->command[i]); 713 720 kdb_printf("endefcmd\n"); ··· 729 736 s = kdb_macro + kdb_macro_count; 730 737 memset(s, 0, sizeof(*s)); 731 738 s->usable = true; 732 - s->name = kdb_strdup(argv[1], GFP_KDB); 733 - if (!s->name) 739 + 740 + mp = &s->cmd; 741 + mp->cmd_func = kdb_exec_defcmd; 742 + mp->cmd_minlen = 0; 743 + mp->cmd_flags = KDB_ENABLE_ALWAYS_SAFE; 744 + mp->cmd_name = kdb_strdup(argv[1], GFP_KDB); 745 + if (!mp->cmd_name) 734 746 goto fail_name; 735 - s->usage = kdb_strdup(argv[2], GFP_KDB); 736 - if (!s->usage) 747 + mp->cmd_usage = kdb_strdup(argv[2], GFP_KDB); 748 + if (!mp->cmd_usage) 737 749 goto fail_usage; 738 - s->help = kdb_strdup(argv[3], GFP_KDB); 739 - if (!s->help) 750 + mp->cmd_help = kdb_strdup(argv[3], GFP_KDB); 751 + if (!mp->cmd_help) 740 752 goto fail_help; 741 - if (s->usage[0] == '"') { 742 - strcpy(s->usage, argv[2]+1); 743 - s->usage[strlen(s->usage)-1] = '\0'; 753 + if (mp->cmd_usage[0] == '"') { 754 + strcpy(mp->cmd_usage, argv[2]+1); 755 + mp->cmd_usage[strlen(mp->cmd_usage)-1] = '\0'; 744 756 } 745 - if (s->help[0] == '"') { 746 - strcpy(s->help, argv[3]+1); 747 - s->help[strlen(s->help)-1] = '\0'; 757 + if (mp->cmd_help[0] == '"') { 758 + strcpy(mp->cmd_help, argv[3]+1); 759 + mp->cmd_help[strlen(mp->cmd_help)-1] = '\0'; 748 760 } 749 761 ++kdb_macro_count; 750 762 defcmd_in_progress = true; 751 763 kfree(save_kdb_macro); 752 764 return 0; 753 765 fail_help: 754 - kfree(s->usage); 766 + kfree(mp->cmd_usage); 755 767 fail_usage: 756 - kfree(s->name); 768 + kfree(mp->cmd_name); 757 769 fail_name: 758 770 kfree(kdb_macro); 759 771 fail_defcmd: ··· 783 785 if (argc != 0) 784 786 return KDB_ARGCOUNT; 785 787 for (s = kdb_macro, i = 0; i < kdb_macro_count; ++i, ++s) { 786 - if (strcmp(s->name, argv[0]) == 0) 788 + if (strcmp(s->cmd.cmd_name, argv[0]) == 0) 787 789 break; 788 790 } 789 791 if (i == kdb_macro_count) { ··· 795 797 /* Recursive use of kdb_parse, do not use argv after 796 798 * this point */ 797 799 argv = NULL; 798 - kdb_printf("[%s]kdb> %s\n", s->name, s->command[i]); 800 + kdb_printf("[%s]kdb> %s\n", s->cmd.cmd_name, s->command[i]); 799 801 ret = kdb_parse(s->command[i]); 800 802 if (ret) 801 803 return ret; ··· 2611 2613 return 0; 2612 2614 } 2613 2615 2614 - /* 2615 - * kdb_register_flags - This function is used to register a kernel 2616 - * debugger command. 2617 - * Inputs: 2618 - * cmd Command name 2619 - * func Function to execute the command 2620 - * usage A simple usage string showing arguments 2621 - * help A simple help string describing command 2622 - * repeat Does the command auto repeat on enter? 2623 - * Returns: 2624 - * zero for success, one if a duplicate command. 2616 + /** 2617 + * kdb_register() - This function is used to register a kernel debugger 2618 + * command. 2619 + * @cmd: pointer to kdb command 2620 + * 2621 + * Note that it's the job of the caller to keep the memory for the cmd 2622 + * allocated until unregister is called. 2625 2623 */ 2626 - int kdb_register_flags(char *cmd, 2627 - kdb_func_t func, 2628 - char *usage, 2629 - char *help, 2630 - short minlen, 2631 - kdb_cmdflags_t flags) 2624 + int kdb_register(kdbtab_t *cmd) 2632 2625 { 2633 2626 kdbtab_t *kp; 2634 2627 2635 2628 list_for_each_entry(kp, &kdb_cmds_head, list_node) { 2636 - if (strcmp(kp->cmd_name, cmd) == 0) { 2637 - kdb_printf("Duplicate kdb command registered: " 2638 - "%s, func %px help %s\n", cmd, func, help); 2629 + if (strcmp(kp->cmd_name, cmd->cmd_name) == 0) { 2630 + kdb_printf("Duplicate kdb cmd: %s, func %p help %s\n", 2631 + cmd->cmd_name, cmd->cmd_func, cmd->cmd_help); 2639 2632 return 1; 2640 2633 } 2641 2634 } 2642 2635 2643 - kp = kmalloc(sizeof(*kp), GFP_KDB); 2644 - if (!kp) { 2645 - kdb_printf("Could not allocate new kdb_command table\n"); 2646 - return 1; 2647 - } 2648 - 2649 - kp->cmd_name = cmd; 2650 - kp->cmd_func = func; 2651 - kp->cmd_usage = usage; 2652 - kp->cmd_help = help; 2653 - kp->cmd_minlen = minlen; 2654 - kp->cmd_flags = flags; 2655 - kp->is_dynamic = true; 2656 - 2657 - list_add_tail(&kp->list_node, &kdb_cmds_head); 2658 - 2636 + list_add_tail(&cmd->list_node, &kdb_cmds_head); 2659 2637 return 0; 2660 2638 } 2661 - EXPORT_SYMBOL_GPL(kdb_register_flags); 2639 + EXPORT_SYMBOL_GPL(kdb_register); 2662 2640 2663 - /* 2641 + /** 2664 2642 * kdb_register_table() - This function is used to register a kdb command 2665 2643 * table. 2666 2644 * @kp: pointer to kdb command table ··· 2650 2676 } 2651 2677 } 2652 2678 2653 - /* 2654 - * kdb_register - Compatibility register function for commands that do 2655 - * not need to specify a repeat state. Equivalent to 2656 - * kdb_register_flags with flags set to 0. 2657 - * Inputs: 2658 - * cmd Command name 2659 - * func Function to execute the command 2660 - * usage A simple usage string showing arguments 2661 - * help A simple help string describing command 2662 - * Returns: 2663 - * zero for success, one if a duplicate command. 2679 + /** 2680 + * kdb_unregister() - This function is used to unregister a kernel debugger 2681 + * command. It is generally called when a module which 2682 + * implements kdb command is unloaded. 2683 + * @cmd: pointer to kdb command 2664 2684 */ 2665 - int kdb_register(char *cmd, 2666 - kdb_func_t func, 2667 - char *usage, 2668 - char *help, 2669 - short minlen) 2685 + void kdb_unregister(kdbtab_t *cmd) 2670 2686 { 2671 - return kdb_register_flags(cmd, func, usage, help, minlen, 0); 2672 - } 2673 - EXPORT_SYMBOL_GPL(kdb_register); 2674 - 2675 - /* 2676 - * kdb_unregister - This function is used to unregister a kernel 2677 - * debugger command. It is generally called when a module which 2678 - * implements kdb commands is unloaded. 2679 - * Inputs: 2680 - * cmd Command name 2681 - * Returns: 2682 - * zero for success, one command not registered. 2683 - */ 2684 - int kdb_unregister(char *cmd) 2685 - { 2686 - kdbtab_t *kp; 2687 - 2688 - /* 2689 - * find the command. 2690 - */ 2691 - list_for_each_entry(kp, &kdb_cmds_head, list_node) { 2692 - if (strcmp(kp->cmd_name, cmd) == 0) { 2693 - list_del(&kp->list_node); 2694 - if (kp->is_dynamic) 2695 - kfree(kp); 2696 - return 0; 2697 - } 2698 - } 2699 - 2700 - /* Couldn't find it. */ 2701 - return 1; 2687 + list_del(&cmd->list_node); 2702 2688 } 2703 2689 EXPORT_SYMBOL_GPL(kdb_unregister); 2704 2690 ··· 2834 2900 .cmd_func = kdb_defcmd, 2835 2901 .cmd_usage = "name \"usage\" \"help\"", 2836 2902 .cmd_help = "Define a set of commands, down to endefcmd", 2903 + /* 2904 + * Macros are always safe because when executed each 2905 + * internal command re-enters kdb_parse() and is safety 2906 + * checked individually. 2907 + */ 2837 2908 .cmd_flags = KDB_ENABLE_ALWAYS_SAFE, 2838 2909 }, 2839 2910 { .cmd_name = "kill",
-13
kernel/debug/kdb/kdb_private.h
··· 164 164 #ifdef CONFIG_KGDB_KDB 165 165 extern kdb_bp_t kdb_breakpoints[/* KDB_MAXBPT */]; 166 166 167 - /* The KDB shell command table */ 168 - typedef struct _kdbtab { 169 - char *cmd_name; /* Command name */ 170 - kdb_func_t cmd_func; /* Function to execute command */ 171 - char *cmd_usage; /* Usage String for this command */ 172 - char *cmd_help; /* Help message for this command */ 173 - short cmd_minlen; /* Minimum legal # command 174 - * chars required */ 175 - kdb_cmdflags_t cmd_flags; /* Command behaviour flags */ 176 - struct list_head list_node; /* Command list */ 177 - bool is_dynamic; /* Command table allocation type */ 178 - } kdbtab_t; 179 - 180 167 extern void kdb_register_table(kdbtab_t *kp, size_t len); 181 168 extern int kdb_bt(int, const char **); /* KDB display back trace */ 182 169
+9 -3
kernel/trace/trace_kdb.c
··· 147 147 return 0; 148 148 } 149 149 150 + static kdbtab_t ftdump_cmd = { 151 + .cmd_name = "ftdump", 152 + .cmd_func = kdb_ftdump, 153 + .cmd_usage = "[skip_#entries] [cpu]", 154 + .cmd_help = "Dump ftrace log; -skip dumps last #entries", 155 + .cmd_flags = KDB_ENABLE_ALWAYS_SAFE, 156 + }; 157 + 150 158 static __init int kdb_ftrace_register(void) 151 159 { 152 - kdb_register_flags("ftdump", kdb_ftdump, "[skip_#entries] [cpu]", 153 - "Dump ftrace log; -skip dumps last #entries", 0, 154 - KDB_ENABLE_ALWAYS_SAFE); 160 + kdb_register(&ftdump_cmd); 155 161 return 0; 156 162 } 157 163
+9 -11
samples/kdb/kdb_hello.c
··· 28 28 return 0; 29 29 } 30 30 31 + static kdbtab_t hello_cmd = { 32 + .cmd_name = "hello", 33 + .cmd_func = kdb_hello_cmd, 34 + .cmd_usage = "[string]", 35 + .cmd_help = "Say Hello World or Hello [string]", 36 + }; 31 37 32 38 static int __init kdb_hello_cmd_init(void) 33 39 { 34 40 /* 35 41 * Registration of a dynamically added kdb command is done with 36 - * kdb_register() with the arguments being: 37 - * 1: The name of the shell command 38 - * 2: The function that processes the command 39 - * 3: Description of the usage of any arguments 40 - * 4: Descriptive text when you run help 41 - * 5: Number of characters to complete the command 42 - * 0 == type the whole command 43 - * 1 == match both "g" and "go" for example 42 + * kdb_register(). 44 43 */ 45 - kdb_register("hello", kdb_hello_cmd, "[string]", 46 - "Say Hello World or Hello [string]", 0); 44 + kdb_register(&hello_cmd); 47 45 return 0; 48 46 } 49 47 50 48 static void __exit kdb_hello_cmd_exit(void) 51 49 { 52 - kdb_unregister("hello"); 50 + kdb_unregister(&hello_cmd); 53 51 } 54 52 55 53 module_init(kdb_hello_cmd_init);