1 /* $NetBSD: ddbping.c,v 1.3 2023/02/01 10:22:20 uwe Exp $ */ 2 /* 3 * Copyright (c) 2020 Valery Ushakov 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 /* 28 * Example of a kernel module that registers DDB commands. 29 */ 30 #include <sys/cdefs.h> 31 __KERNEL_RCSID(0, "$NetBSD: ddbping.c,v 1.3 2023/02/01 10:22:20 uwe Exp $"); 32 33 #include <sys/param.h> 34 #include <sys/module.h> 35 36 #include <ddb/ddb.h> 37 38 /* XXX: db_command.h should provide something like this */ 39 typedef void db_cmdfn_t(db_expr_t, bool, db_expr_t, const char *); 40 41 42 static db_cmdfn_t db_ping; 43 static db_cmdfn_t db_show_ping; 44 45 46 static const struct db_command db_ping_base_tbl[] = { 47 { DDB_ADD_CMD("ping", db_ping, 0, 48 "Example command", 49 NULL, NULL) }, 50 { DDB_END_CMD }, 51 }; 52 53 static const struct db_command db_ping_show_tbl[] = { 54 { DDB_ADD_CMD("ping", db_show_ping, 0, 55 "Example command stats", 56 NULL, NULL) }, 57 { DDB_END_CMD }, 58 }; 59 60 61 static unsigned int ping_count; 62 static unsigned int ping_count_modif; 63 static unsigned int ping_count_addr; 64 static unsigned int ping_count_count; 65 66 67 static void 68 db_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif) 69 { 70 db_printf("pong"); 71 ++ping_count; 72 73 if (modif != NULL && *modif != '\0') { 74 db_printf("/%s", modif); 75 ++ping_count_modif; 76 } 77 78 if (have_addr) { 79 db_printf(" 0x%zx", (size_t)addr); 80 ++ping_count_addr; 81 } 82 83 if (count > 0) { 84 db_printf(", 0t%zu", (size_t)count); 85 ++ping_count_count; 86 } 87 88 db_printf("\n"); 89 } 90 91 92 static void 93 db_show_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif) 94 { 95 db_printf("total\t\t%u\n", ping_count); 96 db_printf("with modifiers\t%u\n", ping_count_modif); 97 db_printf("with address\t%u\n", ping_count_addr); 98 db_printf("with count\t%u\n", ping_count_count); 99 } 100 101 102 MODULE(MODULE_CLASS_MISC, ddbping, NULL); 103 104 static int 105 ddbping_modcmd(modcmd_t cmd, void *arg __unused) 106 { 107 switch (cmd) { 108 case MODULE_CMD_INIT: 109 db_register_tbl(DDB_BASE_CMD, db_ping_base_tbl); 110 db_register_tbl(DDB_SHOW_CMD, db_ping_show_tbl); 111 break; 112 113 case MODULE_CMD_FINI: 114 db_unregister_tbl(DDB_BASE_CMD, db_ping_base_tbl); 115 db_unregister_tbl(DDB_SHOW_CMD, db_ping_show_tbl); 116 break; 117 118 case MODULE_CMD_STAT: /* FALLTHROUGH */ 119 default: 120 return ENOTTY; 121 } 122 123 return 0; 124 } 125