1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
/* $NetBSD: ddbping.c,v 1.3 2023/02/01 10:22:20 uwe Exp $ */
/*
* Copyright (c) 2020 Valery Ushakov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Example of a kernel module that registers DDB commands.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ddbping.c,v 1.3 2023/02/01 10:22:20 uwe Exp $");
#include <sys/param.h>
#include <sys/module.h>
#include <ddb/ddb.h>
/* XXX: db_command.h should provide something like this */
typedef void db_cmdfn_t(db_expr_t, bool, db_expr_t, const char *);
static db_cmdfn_t db_ping;
static db_cmdfn_t db_show_ping;
static const struct db_command db_ping_base_tbl[] = {
{ DDB_ADD_CMD("ping", db_ping, 0,
"Example command",
NULL, NULL) },
{ DDB_END_CMD },
};
static const struct db_command db_ping_show_tbl[] = {
{ DDB_ADD_CMD("ping", db_show_ping, 0,
"Example command stats",
NULL, NULL) },
{ DDB_END_CMD },
};
static unsigned int ping_count;
static unsigned int ping_count_modif;
static unsigned int ping_count_addr;
static unsigned int ping_count_count;
static void
db_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
{
db_printf("pong");
++ping_count;
if (modif != NULL && *modif != '\0') {
db_printf("/%s", modif);
++ping_count_modif;
}
if (have_addr) {
db_printf(" 0x%zx", (size_t)addr);
++ping_count_addr;
}
if (count > 0) {
db_printf(", 0t%zu", (size_t)count);
++ping_count_count;
}
db_printf("\n");
}
static void
db_show_ping(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
{
db_printf("total\t\t%u\n", ping_count);
db_printf("with modifiers\t%u\n", ping_count_modif);
db_printf("with address\t%u\n", ping_count_addr);
db_printf("with count\t%u\n", ping_count_count);
}
MODULE(MODULE_CLASS_MISC, ddbping, NULL);
static int
ddbping_modcmd(modcmd_t cmd, void *arg __unused)
{
switch (cmd) {
case MODULE_CMD_INIT:
db_register_tbl(DDB_BASE_CMD, db_ping_base_tbl);
db_register_tbl(DDB_SHOW_CMD, db_ping_show_tbl);
break;
case MODULE_CMD_FINI:
db_unregister_tbl(DDB_BASE_CMD, db_ping_base_tbl);
db_unregister_tbl(DDB_SHOW_CMD, db_ping_show_tbl);
break;
case MODULE_CMD_STAT: /* FALLTHROUGH */
default:
return ENOTTY;
}
return 0;
}
|