Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/modules/examples/ddbping Example of a kernel module that...



details:   https://anonhg.NetBSD.org/src/rev/71305265a1fe
branches:  trunk
changeset: 933960:71305265a1fe
user:      uwe <uwe%NetBSD.org@localhost>
date:      Mon Jun 01 03:37:40 2020 +0000

description:
Example of a kernel module that registers DDB commands.

diffstat:

 sys/modules/examples/ddbping/Makefile  |    9 ++
 sys/modules/examples/ddbping/ddbping.c |  126 +++++++++++++++++++++++++++++++++
 2 files changed, 135 insertions(+), 0 deletions(-)

diffs (143 lines):

diff -r 20e7d947af5f -r 71305265a1fe sys/modules/examples/ddbping/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/ddbping/Makefile     Mon Jun 01 03:37:40 2020 +0000
@@ -0,0 +1,9 @@
+#      $NetBSD: Makefile,v 1.1 2020/06/01 03:37:40 uwe Exp $
+
+# set in nbmake-$machine wrapper:
+# NETBSDSRCDIR?=/usr/src
+
+KMOD=  ddbping
+SRCS=  ddbping.c
+
+.include <bsd.kmodule.mk>
diff -r 20e7d947af5f -r 71305265a1fe sys/modules/examples/ddbping/ddbping.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/ddbping/ddbping.c    Mon Jun 01 03:37:40 2020 +0000
@@ -0,0 +1,126 @@
+/*     $NetBSD: ddbping.c,v 1.1 2020/06/01 03:37:40 uwe Exp $ */
+/*
+ * Copyright (c) 2020 Valery Ushakov
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Example of a kernel module that registers DDB commands.
+ */
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: ddbping.c,v 1.1 2020/06/01 03:37:40 uwe Exp $");
+
+#include <sys/param.h>
+#include <sys/module.h>
+
+#include <ddb/ddb.h>
+
+/* XXX: db_command.h should provide something like these */
+#define DB_CMD_TBL_END { DDB_ADD_CMD(NULL, NULL, 0, NULL, NULL, NULL) }
+typedef void db_cmdfn_t(db_expr_t, bool, db_expr_t, const char *);
+
+
+static db_cmdfn_t db_ping;
+static db_cmdfn_t db_show_ping;
+
+
+static const struct db_command db_ping_base_tbl[] = {
+       { DDB_ADD_CMD("ping", db_ping, 0,
+                     "Example command",
+                     NULL, NULL) },
+       DB_CMD_TBL_END
+};
+
+static const struct db_command db_ping_show_tbl[] = {
+       { DDB_ADD_CMD("ping", db_show_ping, 0,
+                     "Example command stats",
+                     NULL, NULL) },
+       DB_CMD_TBL_END
+};
+
+
+static unsigned int ping_count;
+static unsigned int ping_count_modif;
+static unsigned int ping_count_addr;
+static unsigned int ping_count_count;
+
+
+static void
+db_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
+{
+       db_printf("pong");
+       ++ping_count;
+
+       if (modif != NULL && *modif != '\0') {
+               db_printf("/%s", modif);
+               ++ping_count_modif;
+       }
+
+       if (have_addr) {
+               db_printf(" 0x%zx", (size_t)addr);
+               ++ping_count_addr;
+       }
+
+       if (count > 0) {
+               db_printf(", 0t%zu", (size_t)count);
+               ++ping_count_count;
+       }
+
+       db_printf("\n");
+}
+
+
+static void
+db_show_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
+{
+       db_printf("total\t\t%u\n", ping_count);
+       db_printf("with modifiers\t%u\n", ping_count_modif);
+       db_printf("with address\t%u\n", ping_count_addr);
+       db_printf("with count\t%u\n", ping_count_count);
+}
+
+
+
+MODULE(MODULE_CLASS_MISC, ddbping, NULL);
+
+static int
+ddbping_modcmd(modcmd_t cmd, void *arg __unused)
+{
+       switch (cmd) {
+       case MODULE_CMD_INIT:
+               db_register_tbl(DDB_BASE_CMD, db_ping_base_tbl);
+               db_register_tbl(DDB_SHOW_CMD, db_ping_show_tbl);
+               break;
+
+       case MODULE_CMD_FINI:
+               db_unregister_tbl(DDB_BASE_CMD, db_ping_base_tbl);
+               db_unregister_tbl(DDB_SHOW_CMD, db_ping_show_tbl);
+               break;
+
+       case MODULE_CMD_STAT:   /* FALLTHROUGH */
+       default:
+               return ENOTTY;
+       }
+
+       return 0;
+}



Home | Main Index | Thread Index | Old Index