1*9e9fc3f7Suwe /* $NetBSD: ddbping.c,v 1.3 2023/02/01 10:22:20 uwe Exp $ */
257ff52faSuwe /*
357ff52faSuwe * Copyright (c) 2020 Valery Ushakov
457ff52faSuwe * All rights reserved.
557ff52faSuwe *
657ff52faSuwe * Redistribution and use in source and binary forms, with or without
757ff52faSuwe * modification, are permitted provided that the following conditions
857ff52faSuwe * are met:
957ff52faSuwe * 1. Redistributions of source code must retain the above copyright
1057ff52faSuwe * notice, this list of conditions and the following disclaimer.
1157ff52faSuwe * 2. Redistributions in binary form must reproduce the above copyright
1257ff52faSuwe * notice, this list of conditions and the following disclaimer in the
1357ff52faSuwe * documentation and/or other materials provided with the distribution.
1457ff52faSuwe *
1557ff52faSuwe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1657ff52faSuwe * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1757ff52faSuwe * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1857ff52faSuwe * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1957ff52faSuwe * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2057ff52faSuwe * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2157ff52faSuwe * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2257ff52faSuwe * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2357ff52faSuwe * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2457ff52faSuwe * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2557ff52faSuwe */
2657ff52faSuwe
2757ff52faSuwe /*
2857ff52faSuwe * Example of a kernel module that registers DDB commands.
2957ff52faSuwe */
3057ff52faSuwe #include <sys/cdefs.h>
31*9e9fc3f7Suwe __KERNEL_RCSID(0, "$NetBSD: ddbping.c,v 1.3 2023/02/01 10:22:20 uwe Exp $");
3257ff52faSuwe
3357ff52faSuwe #include <sys/param.h>
3457ff52faSuwe #include <sys/module.h>
3557ff52faSuwe
3657ff52faSuwe #include <ddb/ddb.h>
3757ff52faSuwe
38*9e9fc3f7Suwe /* XXX: db_command.h should provide something like this */
39*9e9fc3f7Suwe typedef void db_cmdfn_t(db_expr_t, bool, db_expr_t, const char *);
40*9e9fc3f7Suwe
41*9e9fc3f7Suwe
42*9e9fc3f7Suwe static db_cmdfn_t db_ping;
43*9e9fc3f7Suwe static db_cmdfn_t db_show_ping;
44*9e9fc3f7Suwe
45*9e9fc3f7Suwe
46*9e9fc3f7Suwe static const struct db_command db_ping_base_tbl[] = {
47*9e9fc3f7Suwe { DDB_ADD_CMD("ping", db_ping, 0,
48*9e9fc3f7Suwe "Example command",
49*9e9fc3f7Suwe NULL, NULL) },
50*9e9fc3f7Suwe { DDB_END_CMD },
51*9e9fc3f7Suwe };
52*9e9fc3f7Suwe
53*9e9fc3f7Suwe static const struct db_command db_ping_show_tbl[] = {
54*9e9fc3f7Suwe { DDB_ADD_CMD("ping", db_show_ping, 0,
55*9e9fc3f7Suwe "Example command stats",
56*9e9fc3f7Suwe NULL, NULL) },
57*9e9fc3f7Suwe { DDB_END_CMD },
58*9e9fc3f7Suwe };
59*9e9fc3f7Suwe
6057ff52faSuwe
6157ff52faSuwe static unsigned int ping_count;
6257ff52faSuwe static unsigned int ping_count_modif;
6357ff52faSuwe static unsigned int ping_count_addr;
6457ff52faSuwe static unsigned int ping_count_count;
6557ff52faSuwe
66*9e9fc3f7Suwe
6757ff52faSuwe static void
db_ping(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)6857ff52faSuwe db_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
6957ff52faSuwe {
7057ff52faSuwe db_printf("pong");
7157ff52faSuwe ++ping_count;
7257ff52faSuwe
7357ff52faSuwe if (modif != NULL && *modif != '\0') {
7457ff52faSuwe db_printf("/%s", modif);
7557ff52faSuwe ++ping_count_modif;
7657ff52faSuwe }
7757ff52faSuwe
7857ff52faSuwe if (have_addr) {
7957ff52faSuwe db_printf(" 0x%zx", (size_t)addr);
8057ff52faSuwe ++ping_count_addr;
8157ff52faSuwe }
8257ff52faSuwe
8357ff52faSuwe if (count > 0) {
8457ff52faSuwe db_printf(", 0t%zu", (size_t)count);
8557ff52faSuwe ++ping_count_count;
8657ff52faSuwe }
8757ff52faSuwe
8857ff52faSuwe db_printf("\n");
8957ff52faSuwe }
9057ff52faSuwe
9157ff52faSuwe
9257ff52faSuwe static void
db_show_ping(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)9357ff52faSuwe db_show_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
9457ff52faSuwe {
9557ff52faSuwe db_printf("total\t\t%u\n", ping_count);
9657ff52faSuwe db_printf("with modifiers\t%u\n", ping_count_modif);
9757ff52faSuwe db_printf("with address\t%u\n", ping_count_addr);
9857ff52faSuwe db_printf("with count\t%u\n", ping_count_count);
9957ff52faSuwe }
10057ff52faSuwe
10157ff52faSuwe
10257ff52faSuwe MODULE(MODULE_CLASS_MISC, ddbping, NULL);
10357ff52faSuwe
10457ff52faSuwe static int
ddbping_modcmd(modcmd_t cmd,void * arg __unused)10557ff52faSuwe ddbping_modcmd(modcmd_t cmd, void *arg __unused)
10657ff52faSuwe {
10757ff52faSuwe switch (cmd) {
10857ff52faSuwe case MODULE_CMD_INIT:
10957ff52faSuwe db_register_tbl(DDB_BASE_CMD, db_ping_base_tbl);
11057ff52faSuwe db_register_tbl(DDB_SHOW_CMD, db_ping_show_tbl);
11157ff52faSuwe break;
11257ff52faSuwe
11357ff52faSuwe case MODULE_CMD_FINI:
11457ff52faSuwe db_unregister_tbl(DDB_BASE_CMD, db_ping_base_tbl);
11557ff52faSuwe db_unregister_tbl(DDB_SHOW_CMD, db_ping_show_tbl);
11657ff52faSuwe break;
11757ff52faSuwe
11857ff52faSuwe case MODULE_CMD_STAT: /* FALLTHROUGH */
11957ff52faSuwe default:
12057ff52faSuwe return ENOTTY;
12157ff52faSuwe }
12257ff52faSuwe
12357ff52faSuwe return 0;
12457ff52faSuwe }
125