1 /* $NetBSD: ddbping.c,v 1.2 2021/02/23 07:13:53 mrg 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.2 2021/02/23 07:13:53 mrg Exp $"); 32 33 #include <sys/param.h> 34 #include <sys/module.h> 35 36 #include <ddb/ddb.h> 37 38 39 static unsigned int ping_count; 40 static unsigned int ping_count_modif; 41 static unsigned int ping_count_addr; 42 static unsigned int ping_count_count; 43 44 static void 45 db_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif) 46 { 47 db_printf("pong"); 48 ++ping_count; 49 50 if (modif != NULL && *modif != '\0') { 51 db_printf("/%s", modif); 52 ++ping_count_modif; 53 } 54 55 if (have_addr) { 56 db_printf(" 0x%zx", (size_t)addr); 57 ++ping_count_addr; 58 } 59 60 if (count > 0) { 61 db_printf(", 0t%zu", (size_t)count); 62 ++ping_count_count; 63 } 64 65 db_printf("\n"); 66 } 67 68 69 static void 70 db_show_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif) 71 { 72 db_printf("total\t\t%u\n", ping_count); 73 db_printf("with modifiers\t%u\n", ping_count_modif); 74 db_printf("with address\t%u\n", ping_count_addr); 75 db_printf("with count\t%u\n", ping_count_count); 76 } 77 78 static const struct db_command db_ping_base_tbl[] = { 79 { DDB_ADD_CMD("ping", db_ping, 0, 80 "Example command", 81 NULL, NULL) }, 82 { DDB_END_CMD }, 83 }; 84 85 static const struct db_command db_ping_show_tbl[] = { 86 { DDB_ADD_CMD("ping", db_show_ping, 0, 87 "Example command stats", 88 NULL, NULL) }, 89 { DDB_END_CMD }, 90 }; 91 92 93 MODULE(MODULE_CLASS_MISC, ddbping, NULL); 94 95 static int 96 ddbping_modcmd(modcmd_t cmd, void *arg __unused) 97 { 98 switch (cmd) { 99 case MODULE_CMD_INIT: 100 db_register_tbl(DDB_BASE_CMD, db_ping_base_tbl); 101 db_register_tbl(DDB_SHOW_CMD, db_ping_show_tbl); 102 break; 103 104 case MODULE_CMD_FINI: 105 db_unregister_tbl(DDB_BASE_CMD, db_ping_base_tbl); 106 db_unregister_tbl(DDB_SHOW_CMD, db_ping_show_tbl); 107 break; 108 109 case MODULE_CMD_STAT: /* FALLTHROUGH */ 110 default: 111 return ENOTTY; 112 } 113 114 return 0; 115 } 116