11.3Suwe/* $NetBSD: ddbping.c,v 1.3 2023/02/01 10:22:20 uwe Exp $ */ 21.1Suwe/* 31.1Suwe * Copyright (c) 2020 Valery Ushakov 41.1Suwe * All rights reserved. 51.1Suwe * 61.1Suwe * Redistribution and use in source and binary forms, with or without 71.1Suwe * modification, are permitted provided that the following conditions 81.1Suwe * are met: 91.1Suwe * 1. Redistributions of source code must retain the above copyright 101.1Suwe * notice, this list of conditions and the following disclaimer. 111.1Suwe * 2. Redistributions in binary form must reproduce the above copyright 121.1Suwe * notice, this list of conditions and the following disclaimer in the 131.1Suwe * documentation and/or other materials provided with the distribution. 141.1Suwe * 151.1Suwe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 161.1Suwe * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 171.1Suwe * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 181.1Suwe * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 191.1Suwe * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 201.1Suwe * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 211.1Suwe * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 221.1Suwe * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 231.1Suwe * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 241.1Suwe * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 251.1Suwe */ 261.1Suwe 271.1Suwe/* 281.1Suwe * Example of a kernel module that registers DDB commands. 291.1Suwe */ 301.1Suwe#include <sys/cdefs.h> 311.3Suwe__KERNEL_RCSID(0, "$NetBSD: ddbping.c,v 1.3 2023/02/01 10:22:20 uwe Exp $"); 321.1Suwe 331.1Suwe#include <sys/param.h> 341.1Suwe#include <sys/module.h> 351.1Suwe 361.1Suwe#include <ddb/ddb.h> 371.1Suwe 381.3Suwe/* XXX: db_command.h should provide something like this */ 391.3Suwetypedef void db_cmdfn_t(db_expr_t, bool, db_expr_t, const char *); 401.3Suwe 411.3Suwe 421.3Suwestatic db_cmdfn_t db_ping; 431.3Suwestatic db_cmdfn_t db_show_ping; 441.3Suwe 451.3Suwe 461.3Suwestatic const struct db_command db_ping_base_tbl[] = { 471.3Suwe { DDB_ADD_CMD("ping", db_ping, 0, 481.3Suwe "Example command", 491.3Suwe NULL, NULL) }, 501.3Suwe { DDB_END_CMD }, 511.3Suwe}; 521.3Suwe 531.3Suwestatic const struct db_command db_ping_show_tbl[] = { 541.3Suwe { DDB_ADD_CMD("ping", db_show_ping, 0, 551.3Suwe "Example command stats", 561.3Suwe NULL, NULL) }, 571.3Suwe { DDB_END_CMD }, 581.3Suwe}; 591.3Suwe 601.1Suwe 611.1Suwestatic unsigned int ping_count; 621.1Suwestatic unsigned int ping_count_modif; 631.1Suwestatic unsigned int ping_count_addr; 641.1Suwestatic unsigned int ping_count_count; 651.1Suwe 661.3Suwe 671.1Suwestatic void 681.1Suwedb_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif) 691.1Suwe{ 701.1Suwe db_printf("pong"); 711.1Suwe ++ping_count; 721.1Suwe 731.1Suwe if (modif != NULL && *modif != '\0') { 741.1Suwe db_printf("/%s", modif); 751.1Suwe ++ping_count_modif; 761.1Suwe } 771.1Suwe 781.1Suwe if (have_addr) { 791.1Suwe db_printf(" 0x%zx", (size_t)addr); 801.1Suwe ++ping_count_addr; 811.1Suwe } 821.1Suwe 831.1Suwe if (count > 0) { 841.1Suwe db_printf(", 0t%zu", (size_t)count); 851.1Suwe ++ping_count_count; 861.1Suwe } 871.1Suwe 881.1Suwe db_printf("\n"); 891.1Suwe} 901.1Suwe 911.1Suwe 921.1Suwestatic void 931.1Suwedb_show_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif) 941.1Suwe{ 951.1Suwe db_printf("total\t\t%u\n", ping_count); 961.1Suwe db_printf("with modifiers\t%u\n", ping_count_modif); 971.1Suwe db_printf("with address\t%u\n", ping_count_addr); 981.1Suwe db_printf("with count\t%u\n", ping_count_count); 991.1Suwe} 1001.1Suwe 1011.1Suwe 1021.1SuweMODULE(MODULE_CLASS_MISC, ddbping, NULL); 1031.1Suwe 1041.1Suwestatic int 1051.1Suweddbping_modcmd(modcmd_t cmd, void *arg __unused) 1061.1Suwe{ 1071.1Suwe switch (cmd) { 1081.1Suwe case MODULE_CMD_INIT: 1091.1Suwe db_register_tbl(DDB_BASE_CMD, db_ping_base_tbl); 1101.1Suwe db_register_tbl(DDB_SHOW_CMD, db_ping_show_tbl); 1111.1Suwe break; 1121.1Suwe 1131.1Suwe case MODULE_CMD_FINI: 1141.1Suwe db_unregister_tbl(DDB_BASE_CMD, db_ping_base_tbl); 1151.1Suwe db_unregister_tbl(DDB_SHOW_CMD, db_ping_show_tbl); 1161.1Suwe break; 1171.1Suwe 1181.1Suwe case MODULE_CMD_STAT: /* FALLTHROUGH */ 1191.1Suwe default: 1201.1Suwe return ENOTTY; 1211.1Suwe } 1221.1Suwe 1231.1Suwe return 0; 1241.1Suwe} 125