Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 5 Jul 2023 23:02:11 GMT
From:      John Baldwin <jhb@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 884eaacd24bd - main - ddb: Rework macros to make it easier to add new command tables.
Message-ID:  <202307052302.365N2BKk060316@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=884eaacd24bdb85c1571d31145278847bad6e55b

commit 884eaacd24bdb85c1571d31145278847bad6e55b
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2023-07-05 23:00:47 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2023-07-05 23:02:01 +0000

    ddb: Rework macros to make it easier to add new command tables.
    
    - Add new DB_DEFINE_TABLE and DB_DECLARE_TABLE macros to define new
      command tables.  DB_DECLARE_TABLE is intended for use in headers
      similar to MALLOC_DECLARE and SYSCTL_DECL.
    
      DB_DEFINE_TABLE takes three arguments, the name of the parent table,
      the command name, and the name of the table itself, e.g.
      DB_DEFINE_TABLE(show, foo, show_foo) defines a new "show foo" table.
    
    - DB_TABLE_COMMAND, DB_TABLE_COMMAND_FLAGS, DB_TABLE_ALIAS, and
      DB_ALIAS_FLAGS allow new commands and aliases to be defined.  These
      are similar to the existing DB_COMMAND, etc. except that they take
      an initial argument giving the name of the parent table, e.g.:
    
      DB_TABLE_COMMAND(show_foo, bar, db_show_foo_bar)
    
      defines a new "show foo bar" command.
    
    This provides a cleaner interface than the ad-hoc use of internal
    macros like _DB_SET that was required previously (e.g. in cxgbe(4)).
    
    This retires DB_FUNC macro as well as the internal _DB_FUNC macro.
    
    Reviewed by:    melifaro, kib, markj
    Differential Revision:  https://reviews.freebsd.org/D40819
---
 share/man/man9/DB_COMMAND.9 | 39 ++++++++++++++++--
 share/man/man9/Makefile     |  8 +++-
 sys/ddb/ddb.h               | 96 ++++++++++++++++++++++++++++-----------------
 sys/dev/cxgbe/t4_main.c     |  7 ++--
 sys/net/route/route_ddb.c   |  4 +-
 5 files changed, 108 insertions(+), 46 deletions(-)

diff --git a/share/man/man9/DB_COMMAND.9 b/share/man/man9/DB_COMMAND.9
index 15c2adce3580..ebf15f8b73f3 100644
--- a/share/man/man9/DB_COMMAND.9
+++ b/share/man/man9/DB_COMMAND.9
@@ -25,7 +25,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd June 24, 2022
+.Dd July 5, 2023
 .Dt DB_COMMAND 9
 .Os
 .Sh NAME
@@ -34,11 +34,17 @@
 .Nm DB_SHOW_COMMAND ,
 .Nm DB_SHOW_COMMAND_FLAGS ,
 .Nm DB_SHOW_ALL_COMMAND ,
+.Nm DB_TABLE_COMMAND ,
+.Nm DB_TABLE_COMMAND_FLAGS ,
 .Nm DB_ALIAS ,
 .Nm DB_ALIAS_FLAGS ,
 .Nm DB_SHOW_ALIAS ,
 .Nm DB_SHOW_ALIAS_FLAGS ,
-.Nm DB_SHOW_ALL_ALIAS
+.Nm DB_SHOW_ALL_ALIAS ,
+.Nm DB_TABLE_ALIAS ,
+.Nm DB_TABLE_ALIAS_FLAGS
+.Nm DB_DECLARE_TABLE ,
+.Nm DB_DEFINE_TABLE ,
 .Nd Extends the ddb command set
 .Sh SYNOPSIS
 .In ddb/ddb.h
@@ -47,11 +53,17 @@
 .Fn DB_SHOW_COMMAND "command_name" "command_function"
 .Fn DB_SHOW_COMMAND_FLAGS "command_name" "command_function" "flags"
 .Fn DB_SHOW_ALL_COMMAND "command_name" "command_function"
+.Fn DB_TABLE_COMMAND "table" "command_name" "command_function"
+.Fn DB_TABLE_COMMAND_FLAGS "table" "command_name" "command_function" "flags"
 .Fn DB_ALIAS "alias_name" "command_function"
 .Fn DB_ALIAS_FLAGS "alias_name" "command_function" "flags"
 .Fn DB_SHOW_ALIAS "alias_name" "command_function"
 .Fn DB_SHOW_ALIAS_FLAGS "alias_name" "command_function" "flags"
 .Fn DB_SHOW_ALL_ALIAS "alias_name" "command_function"
+.Fn DB_TABLE_ALIAS "table" "alias_name" "command_function"
+.Fn DB_TABLE_ALIAS_FLAGS "table" "alias_name" "command_function" "flags"
+.Fn DB_DEFINE_TABLE "parent" "name" "table"
+.Fn DB_DECLARE_TABLE "table"
 .Sh DESCRIPTION
 The
 .Fn DB_COMMAND
@@ -78,10 +90,18 @@ command and
 command, respectively.
 .Pp
 The
+.Fn DB_TABLE_COMMAND
+macro is also similar to
+.Fn DB_COMMAND
+but adds the new command as a sub-command of the ddb command
+.Fa table .
+.Pp
+The
 .Fn DB_ALIAS ,
 .Fn DB_SHOW_ALIAS ,
+.Fn DB_SHOW_ALL_ALIAS ,
 and
-.Fn DB_SHOW_ALL_ALIAS
+.Fn DB_TABLE_ALIAS
 macros register the existing
 .Fa command_function
 under the alternative command name
@@ -117,6 +137,19 @@ For example, the
 .Sy examine
 command will display words in decimal form if it is passed the modifier "d".
 .El
+.Pp
+The
+.Fn DB_DEFINE_TABLE
+macro adds a new command
+.Fa name
+as a sub-command of the existing command table
+.Fa parent .
+The new command defines a table named
+.Fa table
+which contains sub-commands.
+New commands and aliases can be added to this table by passing
+.Fa table
+as the first argument to one of the DB_TABLE_ macros.
 .Sh EXAMPLES
 In your module, the command is declared as:
 .Bd -literal
diff --git a/share/man/man9/Makefile b/share/man/man9/Makefile
index 2a320cf0fcba..45f9ec9b0515 100644
--- a/share/man/man9/Makefile
+++ b/share/man/man9/Makefile
@@ -983,7 +983,13 @@ MLINKS+=DB_COMMAND.9 DB_ALIAS.9 \
 	DB_COMMAND.9 DB_SHOW_ALL_ALIAS.9 \
 	DB_COMMAND.9 DB_SHOW_ALL_COMMAND.9 \
 	DB_COMMAND.9 DB_SHOW_COMMAND.9 \
-	DB_COMMAND.9 DB_SHOW_COMMAND_FLAGS.9
+	DB_COMMAND.9 DB_SHOW_COMMAND_FLAGS.9 \
+	DB_COMMAND.9 DB_DECLARE_TABLE.9 \
+	DB_COMMAND.9 DB_DEFINE_TABLE.9 \
+	DB_COMMAND.9 DB_TABLE_COMMAND.9 \
+	DB_COMMAND.9 DB_TABLE_COMMAND_FLAGS.9 \
+	DB_COMMAND.9 DB_TABLE_ALIAS.9 \
+	DB_COMMAND.9 DB_TABLE_ALIAS_FLAGS.9
 MLINKS+=DECLARE_MODULE.9 DECLARE_MODULE_TIED.9
 MLINKS+=dev_clone.9 drain_dev_clone_events.9
 MLINKS+=dev_refthread.9 devvn_refthread.9 \
diff --git a/sys/ddb/ddb.h b/sys/ddb/ddb.h
index 2fa4bfeb495c..591371e8a5c7 100644
--- a/sys/ddb/ddb.h
+++ b/sys/ddb/ddb.h
@@ -85,21 +85,32 @@ int	DB_CALL(db_expr_t, db_expr_t *, int, db_expr_t[]);
  */
 extern vm_offset_t ksymtab, kstrtab, ksymtab_size, ksymtab_relbase;
 
+/* Command tables contain a list of commands. */
+struct db_command;
+LIST_HEAD(db_command_table, db_command);
+
+#define	_DB_TABLE_NAME(table)	db_##table##_table
+
+#define	DB_DEFINE_TABLE(parent, name, table)				\
+	struct db_command_table _DB_TABLE_NAME(table) =			\
+	    LIST_HEAD_INITIALIZER(_DB_TABLE_NAME(table));		\
+	_DB_SET(parent, name, NULL, 0, &_DB_TABLE_NAME(table))
+
+#define	DB_DECLARE_TABLE(table)						\
+	extern struct db_command_table _DB_TABLE_NAME(table)
+
 /*
- * There are three "command tables":
- * - One for simple commands; a list of these is displayed
+ * Builtin command tables:
+ * - cmd: Top-level command table; a list of these is displayed
  *   by typing 'help' at the debugger prompt.
- * - One for sub-commands of 'show'; to see this type 'show'
- *   without any arguments.
- * - The last one for sub-commands of 'show all'; type 'show all'
- *   without any argument to get a list.
+ * - show: Sub-commands of 'show'
+ * - show_all: Sub-commands of 'show all'
+ * - show_active: Sub-commands of 'show active'
  */
-struct db_command;
-LIST_HEAD(db_command_table, db_command);
-extern struct db_command_table db_cmd_table;
-extern struct db_command_table db_show_table;
-extern struct db_command_table db_show_all_table;
-extern struct db_command_table db_show_active_table;
+DB_DECLARE_TABLE(cmd);
+DB_DECLARE_TABLE(show);
+DB_DECLARE_TABLE(show_all);
+DB_DECLARE_TABLE(show_active);
 
 /*
  * Type signature for a function implementing a ddb command.
@@ -133,26 +144,36 @@ struct db_command {
  * in modules in which case they will be available only when
  * the module is loaded.
  */
-#define	_DB_SET(_suffix, _name, _func, list, _flag, _more)	\
-static struct db_command __CONCAT(_name,_suffix) = {		\
+#define	_DB_SET(_table, _name, _func, _flag, _more)		\
+static struct db_command db_##_table##_##_name##_cmd = {	\
 	.name	= __STRING(_name),				\
 	.fcn	= _func,					\
 	.flag	= _flag,					\
 	.more	= _more						\
 };								\
-static void __CONCAT(__CONCAT(_name,_suffix),_add)(void *arg __unused) \
-    { db_command_register(&list, &__CONCAT(_name,_suffix)); }	\
-SYSINIT(__CONCAT(_name,_suffix), SI_SUB_KLD, SI_ORDER_ANY,	\
-    __CONCAT(__CONCAT(_name,_suffix),_add), NULL);		\
-static void __CONCAT(__CONCAT(_name,_suffix),_del)(void *arg __unused) \
-    { db_command_unregister(&list, &__CONCAT(_name,_suffix)); }	\
-SYSUNINIT(__CONCAT(_name,_suffix), SI_SUB_KLD, SI_ORDER_ANY,	\
-    __CONCAT(__CONCAT(_name,_suffix),_del), NULL);
+								\
+static void							\
+db##_table##_##_name##_add(void *arg __unused)			\
+{								\
+	db_command_register(&_DB_TABLE_NAME(_table),		\
+	    &db_##_table##_##_name##_cmd);			\
+}								\
+SYSINIT(db_##_table##_##_name, SI_SUB_KLD, SI_ORDER_ANY,	\
+    db##_table##_##_name##_add, NULL);				\
+								\
+static void							\
+db##_table##_##_name##_del(void *arg __unused)			\
+{								\
+	db_command_unregister(&_DB_TABLE_NAME(_table),		\
+	    &db_##_table##_##_name##_cmd);			\
+}								\
+SYSUNINIT(db_##_table##_##_name, SI_SUB_KLD, SI_ORDER_ANY,	\
+    db##_table##_##_name##_del, NULL)
 
 /*
  * Like _DB_SET but also create the function declaration which
  * must be followed immediately by the body; e.g.
- *   _DB_FUNC(_cmd, panic, db_panic, db_cmd_table, 0, NULL)
+ *   DB_TABLE_COMMAND_FLAGS(_cmd, panic, db_panic, 0)
  *   {
  *	...panic implementation...
  *   }
@@ -160,38 +181,41 @@ SYSUNINIT(__CONCAT(_name,_suffix), SI_SUB_KLD, SI_ORDER_ANY,	\
  * This macro is mostly used to define commands placed in one of
  * the ddb command tables; see DB_COMMAND, etc. below.
  */
-#define	_DB_FUNC(_suffix, _name, _func, list, _flag, _more)	\
+#define	DB_TABLE_COMMAND_FLAGS(_table, _name, _func, _flag)	\
 static db_cmdfcn_t _func;					\
-_DB_SET(_suffix, _name, _func, list, _flag, _more);		\
+_DB_SET(_table, _name, _func, _flag, NULL);			\
 static void							\
 _func(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
 
-/* common idom provided for backwards compatibility */
-#define	DB_FUNC(_name, _func, list, _flag, _more)		\
-	_DB_FUNC(_cmd, _name, _func, list, _flag, _more)
+#define	DB_TABLE_COMMAND(_table, _name, _func)			\
+	DB_TABLE_COMMAND_FLAGS(_table, _name, _func, 0)
+
+/* Wrappers around _DB_SET used for aliases. */
+#define	DB_TABLE_ALIAS_FLAGS(_table, _name, _func, _flag)	\
+	_DB_SET(_table, _name, _func, _flag, NULL)
+#define	DB_TABLE_ALIAS(_table, _name, _func)			\
+	DB_TABLE_ALIAS_FLAGS(_table, _name, _func, 0)
 
 #define	DB_COMMAND_FLAGS(cmd_name, func_name, flags) \
-	_DB_FUNC(_cmd, cmd_name, func_name, db_cmd_table, flags, NULL)
+	DB_TABLE_COMMAND_FLAGS(cmd, cmd_name, func_name, flags)
 #define	DB_COMMAND(cmd_name, func_name) \
 	DB_COMMAND_FLAGS(cmd_name, func_name, 0)
 #define	DB_ALIAS_FLAGS(alias_name, func_name, flags) \
-	_DB_SET(_cmd, alias_name, func_name, db_cmd_table, flags, NULL)
+	DB_TABLE_ALIAS_FLAGS(cmd, alias_name, func_name, flags)
 #define	DB_ALIAS(alias_name, func_name) \
 	DB_ALIAS_FLAGS(alias_name, func_name, 0)
 #define	DB_SHOW_COMMAND_FLAGS(cmd_name, func_name, flags) \
-	_DB_FUNC(_show, cmd_name, func_name, db_show_table, flags, NULL)
+	DB_TABLE_COMMAND_FLAGS(show, cmd_name, func_name, flags)
 #define	DB_SHOW_COMMAND(cmd_name, func_name) \
 	DB_SHOW_COMMAND_FLAGS(cmd_name, func_name, 0)
 #define	DB_SHOW_ALIAS_FLAGS(alias_name, func_name, flags) \
-	_DB_SET(_show, alias_name, func_name, db_show_table, flags, NULL)
+	DB_TABLE_ALIAS_FLAGS(show, alias_name, func_name, flags)
 #define	DB_SHOW_ALIAS(alias_name, func_name) \
 	DB_SHOW_ALIAS_FLAGS(alias_name, func_name, 0)
 #define	DB_SHOW_ALL_COMMAND(cmd_name, func_name)			\
-	_DB_FUNC(_show_all, cmd_name, func_name, db_show_all_table,	\
-	    DB_CMD_MEMSAFE, NULL)
+	DB_TABLE_COMMAND_FLAGS(show_all, cmd_name, func_name, DB_CMD_MEMSAFE)
 #define	DB_SHOW_ALL_ALIAS(alias_name, func_name)			\
-	_DB_SET(_show_all, alias_name, func_name, db_show_all_table,	\
-	    DB_CMD_MEMSAFE, NULL)
+	DB_TABLE_ALIAS_FLAGS(show_all, alias_name, func_name, DB_CMD_MEMSAFE)
 
 extern db_expr_t db_maxoff;
 extern int db_indent;
diff --git a/sys/dev/cxgbe/t4_main.c b/sys/dev/cxgbe/t4_main.c
index 2fa49ddee10b..7ddd3b416995 100644
--- a/sys/dev/cxgbe/t4_main.c
+++ b/sys/dev/cxgbe/t4_main.c
@@ -12921,10 +12921,9 @@ t4_dump_devlog(struct adapter *sc)
 	} while (i != first && !db_pager_quit);
 }
 
-static struct db_command_table db_t4_table = LIST_HEAD_INITIALIZER(db_t4_table);
-_DB_SET(_show, t4, NULL, db_show_table, 0, &db_t4_table);
+static DB_DEFINE_TABLE(show, t4, show_t4);
 
-DB_FUNC(devlog, db_show_devlog, db_t4_table, CS_OWN, NULL)
+DB_TABLE_COMMAND_FLAGS(show_t4, devlog, db_show_devlog, CS_OWN)
 {
 	device_t dev;
 	int t;
@@ -12950,7 +12949,7 @@ DB_FUNC(devlog, db_show_devlog, db_t4_table, CS_OWN, NULL)
 	t4_dump_devlog(device_get_softc(dev));
 }
 
-DB_FUNC(tcb, db_show_t4tcb, db_t4_table, CS_OWN, NULL)
+DB_TABLE_COMMAND_FLAGS(show_t4, tcb, db_show_t4tcb, CS_OWN)
 {
 	device_t dev;
 	int radix, tid, t;
diff --git a/sys/net/route/route_ddb.c b/sys/net/route/route_ddb.c
index 92e7a6bef574..a8e70a1d65c3 100644
--- a/sys/net/route/route_ddb.c
+++ b/sys/net/route/route_ddb.c
@@ -166,7 +166,7 @@ rt_dumpentry_ddb(struct radix_node *rn, void *arg __unused)
 	return (0);
 }
 
-DB_SHOW_COMMAND(routetable, db_show_routetable_cmd)
+DB_SHOW_COMMAND(routetable, db_show_routetable)
 {
 	struct rib_head *rnh;
 	int error, i, lim;
@@ -204,7 +204,7 @@ DB_SHOW_COMMAND(routetable, db_show_routetable_cmd)
 	}
 }
 
-DB_SHOW_COMMAND_FLAGS(route, db_show_route_cmd, CS_OWN)
+DB_SHOW_COMMAND_FLAGS(route, db_show_route, CS_OWN)
 {
 	char abuf[INET6_ADDRSTRLEN], *buf, *end;
 	struct rib_head *rh;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202307052302.365N2BKk060316>