ddbping.c revision 1.1
11.1Suwe/* $NetBSD: ddbping.c,v 1.1 2020/06/01 03:37:40 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.1Suwe__KERNEL_RCSID(0, "$NetBSD: ddbping.c,v 1.1 2020/06/01 03:37:40 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.1Suwe/* XXX: db_command.h should provide something like these */ 391.1Suwe#define DB_CMD_TBL_END { DDB_ADD_CMD(NULL, NULL, 0, NULL, NULL, NULL) } 401.1Suwetypedef void db_cmdfn_t(db_expr_t, bool, db_expr_t, const char *); 411.1Suwe 421.1Suwe 431.1Suwestatic db_cmdfn_t db_ping; 441.1Suwestatic db_cmdfn_t db_show_ping; 451.1Suwe 461.1Suwe 471.1Suwestatic const struct db_command db_ping_base_tbl[] = { 481.1Suwe { DDB_ADD_CMD("ping", db_ping, 0, 491.1Suwe "Example command", 501.1Suwe NULL, NULL) }, 511.1Suwe DB_CMD_TBL_END 521.1Suwe}; 531.1Suwe 541.1Suwestatic const struct db_command db_ping_show_tbl[] = { 551.1Suwe { DDB_ADD_CMD("ping", db_show_ping, 0, 561.1Suwe "Example command stats", 571.1Suwe NULL, NULL) }, 581.1Suwe DB_CMD_TBL_END 591.1Suwe}; 601.1Suwe 611.1Suwe 621.1Suwestatic unsigned int ping_count; 631.1Suwestatic unsigned int ping_count_modif; 641.1Suwestatic unsigned int ping_count_addr; 651.1Suwestatic unsigned int ping_count_count; 661.1Suwe 671.1Suwe 681.1Suwestatic void 691.1Suwedb_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif) 701.1Suwe{ 711.1Suwe db_printf("pong"); 721.1Suwe ++ping_count; 731.1Suwe 741.1Suwe if (modif != NULL && *modif != '\0') { 751.1Suwe db_printf("/%s", modif); 761.1Suwe ++ping_count_modif; 771.1Suwe } 781.1Suwe 791.1Suwe if (have_addr) { 801.1Suwe db_printf(" 0x%zx", (size_t)addr); 811.1Suwe ++ping_count_addr; 821.1Suwe } 831.1Suwe 841.1Suwe if (count > 0) { 851.1Suwe db_printf(", 0t%zu", (size_t)count); 861.1Suwe ++ping_count_count; 871.1Suwe } 881.1Suwe 891.1Suwe db_printf("\n"); 901.1Suwe} 911.1Suwe 921.1Suwe 931.1Suwestatic void 941.1Suwedb_show_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif) 951.1Suwe{ 961.1Suwe db_printf("total\t\t%u\n", ping_count); 971.1Suwe db_printf("with modifiers\t%u\n", ping_count_modif); 981.1Suwe db_printf("with address\t%u\n", ping_count_addr); 991.1Suwe db_printf("with count\t%u\n", ping_count_count); 1001.1Suwe} 1011.1Suwe 1021.1Suwe 1031.1Suwe 1041.1SuweMODULE(MODULE_CLASS_MISC, ddbping, NULL); 1051.1Suwe 1061.1Suwestatic int 1071.1Suweddbping_modcmd(modcmd_t cmd, void *arg __unused) 1081.1Suwe{ 1091.1Suwe switch (cmd) { 1101.1Suwe case MODULE_CMD_INIT: 1111.1Suwe db_register_tbl(DDB_BASE_CMD, db_ping_base_tbl); 1121.1Suwe db_register_tbl(DDB_SHOW_CMD, db_ping_show_tbl); 1131.1Suwe break; 1141.1Suwe 1151.1Suwe case MODULE_CMD_FINI: 1161.1Suwe db_unregister_tbl(DDB_BASE_CMD, db_ping_base_tbl); 1171.1Suwe db_unregister_tbl(DDB_SHOW_CMD, db_ping_show_tbl); 1181.1Suwe break; 1191.1Suwe 1201.1Suwe case MODULE_CMD_STAT: /* FALLTHROUGH */ 1211.1Suwe default: 1221.1Suwe return ENOTTY; 1231.1Suwe } 1241.1Suwe 1251.1Suwe return 0; 1261.1Suwe} 127