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