1 // SPDX-License-Identifier: GPL-2.0
3 * Test module for in-kernel kprobe event creation and generation.
5 * Copyright (C) 2019 Tom Zanussi <zanussi@kernel.org>
8 #include <linux/module.h>
9 #include <linux/trace_events.h>
12 * This module is a simple test of basic functionality for in-kernel
13 * kprobe/kretprobe event creation. The first test uses
14 * kprobe_event_gen_cmd_start(), kprobe_event_add_fields() and
15 * kprobe_event_gen_cmd_end() to create a kprobe event, which is then
16 * enabled in order to generate trace output. The second creates a
17 * kretprobe event using kretprobe_event_gen_cmd_start() and
18 * kretprobe_event_gen_cmd_end(), and is also then enabled.
20 * To test, select CONFIG_KPROBE_EVENT_GEN_TEST and build the module.
23 * # insmod kernel/trace/kprobe_event_gen_test.ko
24 * # cat /sys/kernel/debug/tracing/trace
26 * You should see many instances of the "gen_kprobe_test" and
27 * "gen_kretprobe_test" events in the trace buffer.
29 * To remove the events, remove the module:
31 * # rmmod kprobe_event_gen_test
35 static struct trace_event_file *gen_kprobe_test;
36 static struct trace_event_file *gen_kretprobe_test;
38 #define KPROBE_GEN_TEST_FUNC "do_sys_open"
41 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_32)
42 #define KPROBE_GEN_TEST_ARG0 "dfd=%ax"
43 #define KPROBE_GEN_TEST_ARG1 "filename=%dx"
44 #define KPROBE_GEN_TEST_ARG2 "flags=%cx"
45 #define KPROBE_GEN_TEST_ARG3 "mode=+4($stack)"
48 #elif defined(CONFIG_ARM64)
49 #define KPROBE_GEN_TEST_ARG0 "dfd=%x0"
50 #define KPROBE_GEN_TEST_ARG1 "filename=%x1"
51 #define KPROBE_GEN_TEST_ARG2 "flags=%x2"
52 #define KPROBE_GEN_TEST_ARG3 "mode=%x3"
55 #elif defined(CONFIG_ARM)
56 #define KPROBE_GEN_TEST_ARG0 "dfd=%r0"
57 #define KPROBE_GEN_TEST_ARG1 "filename=%r1"
58 #define KPROBE_GEN_TEST_ARG2 "flags=%r2"
59 #define KPROBE_GEN_TEST_ARG3 "mode=%r3"
62 #elif defined(CONFIG_RISCV)
63 #define KPROBE_GEN_TEST_ARG0 "dfd=%a0"
64 #define KPROBE_GEN_TEST_ARG1 "filename=%a1"
65 #define KPROBE_GEN_TEST_ARG2 "flags=%a2"
66 #define KPROBE_GEN_TEST_ARG3 "mode=%a3"
70 #define KPROBE_GEN_TEST_ARG0 NULL
71 #define KPROBE_GEN_TEST_ARG1 NULL
72 #define KPROBE_GEN_TEST_ARG2 NULL
73 #define KPROBE_GEN_TEST_ARG3 NULL
78 * Test to make sure we can create a kprobe event, then add more
81 static int __init test_gen_kprobe_cmd(void)
83 struct dynevent_cmd cmd;
87 /* Create a buffer to hold the generated command */
88 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
92 /* Before generating the command, initialize the cmd object */
93 kprobe_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
96 * Define the gen_kprobe_test event with the first 2 kprobe
99 ret = kprobe_event_gen_cmd_start(&cmd, "gen_kprobe_test",
100 KPROBE_GEN_TEST_FUNC,
101 KPROBE_GEN_TEST_ARG0, KPROBE_GEN_TEST_ARG1);
105 /* Use kprobe_event_add_fields to add the rest of the fields */
107 ret = kprobe_event_add_fields(&cmd, KPROBE_GEN_TEST_ARG2, KPROBE_GEN_TEST_ARG3);
112 * This actually creates the event.
114 ret = kprobe_event_gen_cmd_end(&cmd);
119 * Now get the gen_kprobe_test event file. We need to prevent
120 * the instance and event from disappearing from underneath
121 * us, which trace_get_event_file() does (though in this case
122 * we're using the top-level instance which never goes away).
124 gen_kprobe_test = trace_get_event_file(NULL, "kprobes",
126 if (IS_ERR(gen_kprobe_test)) {
127 ret = PTR_ERR(gen_kprobe_test);
131 /* Enable the event or you won't see anything */
132 ret = trace_array_set_clr_event(gen_kprobe_test->tr,
133 "kprobes", "gen_kprobe_test", true);
135 trace_put_event_file(gen_kprobe_test);
142 /* We got an error after creating the event, delete it */
143 ret = kprobe_event_delete("gen_kprobe_test");
148 * Test to make sure we can create a kretprobe event.
150 static int __init test_gen_kretprobe_cmd(void)
152 struct dynevent_cmd cmd;
156 /* Create a buffer to hold the generated command */
157 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
161 /* Before generating the command, initialize the cmd object */
162 kprobe_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
165 * Define the kretprobe event.
167 ret = kretprobe_event_gen_cmd_start(&cmd, "gen_kretprobe_test",
168 KPROBE_GEN_TEST_FUNC,
174 * This actually creates the event.
176 ret = kretprobe_event_gen_cmd_end(&cmd);
181 * Now get the gen_kretprobe_test event file. We need to
182 * prevent the instance and event from disappearing from
183 * underneath us, which trace_get_event_file() does (though in
184 * this case we're using the top-level instance which never
187 gen_kretprobe_test = trace_get_event_file(NULL, "kprobes",
188 "gen_kretprobe_test");
189 if (IS_ERR(gen_kretprobe_test)) {
190 ret = PTR_ERR(gen_kretprobe_test);
194 /* Enable the event or you won't see anything */
195 ret = trace_array_set_clr_event(gen_kretprobe_test->tr,
196 "kprobes", "gen_kretprobe_test", true);
198 trace_put_event_file(gen_kretprobe_test);
205 /* We got an error after creating the event, delete it */
206 ret = kprobe_event_delete("gen_kretprobe_test");
210 static int __init kprobe_event_gen_test_init(void)
214 ret = test_gen_kprobe_cmd();
218 ret = test_gen_kretprobe_cmd();
220 WARN_ON(trace_array_set_clr_event(gen_kretprobe_test->tr,
222 "gen_kretprobe_test", false));
223 trace_put_event_file(gen_kretprobe_test);
224 WARN_ON(kprobe_event_delete("gen_kretprobe_test"));
230 static void __exit kprobe_event_gen_test_exit(void)
232 /* Disable the event or you can't remove it */
233 WARN_ON(trace_array_set_clr_event(gen_kprobe_test->tr,
235 "gen_kprobe_test", false));
237 /* Now give the file and instance back */
238 trace_put_event_file(gen_kprobe_test);
240 /* Now unregister and free the event */
241 WARN_ON(kprobe_event_delete("gen_kprobe_test"));
243 /* Disable the event or you can't remove it */
244 WARN_ON(trace_array_set_clr_event(gen_kretprobe_test->tr,
246 "gen_kretprobe_test", false));
248 /* Now give the file and instance back */
249 trace_put_event_file(gen_kretprobe_test);
251 /* Now unregister and free the event */
252 WARN_ON(kprobe_event_delete("gen_kretprobe_test"));
255 module_init(kprobe_event_gen_test_init)
256 module_exit(kprobe_event_gen_test_exit)
258 MODULE_AUTHOR("Tom Zanussi");
259 MODULE_DESCRIPTION("kprobe event generation test");
260 MODULE_LICENSE("GPL v2");