Skip to content

Commit 6ee64cc

Browse files
mhiramatAlexei Starovoitov
authored andcommitted
fprobe: Add sample program for fprobe
Add a sample program for the fprobe. The sample_fprobe puts a fprobe on kernel_clone() by default. This dump stack and some called address info at the function entry and exit. The sample_fprobe.ko gets 2 parameters. - symbol: you can specify the comma separated symbols or wildcard symbol pattern (in this case you can not use comma) - stackdump: a bool value to enable or disable stack dump in the fprobe handler. Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org> Tested-by: Steven Rostedt (Google) <rostedt@goodmis.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/164735291987.1084943.4449670993752806840.stgit@devnote2
1 parent 5b0ab78 commit 6ee64cc

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

samples/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ config SAMPLE_HW_BREAKPOINT
7373
help
7474
This builds kernel hardware breakpoint example modules.
7575

76+
config SAMPLE_FPROBE
77+
tristate "Build fprobe examples -- loadable modules only"
78+
depends on FPROBE && m
79+
help
80+
This builds a fprobe example module. This module has an option 'symbol'.
81+
You can specify a probed symbol or symbols separated with ','.
82+
7683
config SAMPLE_KFIFO
7784
tristate "Build kfifo examples -- loadable modules only"
7885
depends on m

samples/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ subdir-$(CONFIG_SAMPLE_WATCHDOG) += watchdog
3333
subdir-$(CONFIG_SAMPLE_WATCH_QUEUE) += watch_queue
3434
obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) += kmemleak/
3535
obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG) += coresight/
36+
obj-$(CONFIG_SAMPLE_FPROBE) += fprobe/

samples/fprobe/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
3+
obj-$(CONFIG_SAMPLE_FPROBE) += fprobe_example.o

samples/fprobe/fprobe_example.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Here's a sample kernel module showing the use of fprobe to dump a
4+
* stack trace and selected registers when kernel_clone() is called.
5+
*
6+
* For more information on theory of operation of kprobes, see
7+
* Documentation/trace/kprobes.rst
8+
*
9+
* You will see the trace data in /var/log/messages and on the console
10+
* whenever kernel_clone() is invoked to create a new process.
11+
*/
12+
13+
#define pr_fmt(fmt) "%s: " fmt, __func__
14+
15+
#include <linux/kernel.h>
16+
#include <linux/module.h>
17+
#include <linux/fprobe.h>
18+
#include <linux/sched/debug.h>
19+
#include <linux/slab.h>
20+
21+
#define BACKTRACE_DEPTH 16
22+
#define MAX_SYMBOL_LEN 4096
23+
struct fprobe sample_probe;
24+
25+
static char symbol[MAX_SYMBOL_LEN] = "kernel_clone";
26+
module_param_string(symbol, symbol, sizeof(symbol), 0644);
27+
static char nosymbol[MAX_SYMBOL_LEN] = "";
28+
module_param_string(nosymbol, nosymbol, sizeof(nosymbol), 0644);
29+
static bool stackdump = true;
30+
module_param(stackdump, bool, 0644);
31+
32+
static void show_backtrace(void)
33+
{
34+
unsigned long stacks[BACKTRACE_DEPTH];
35+
unsigned int len;
36+
37+
len = stack_trace_save(stacks, BACKTRACE_DEPTH, 2);
38+
stack_trace_print(stacks, len, 24);
39+
}
40+
41+
static void sample_entry_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs)
42+
{
43+
pr_info("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
44+
if (stackdump)
45+
show_backtrace();
46+
}
47+
48+
static void sample_exit_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs)
49+
{
50+
unsigned long rip = instruction_pointer(regs);
51+
52+
pr_info("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
53+
(void *)ip, (void *)ip, (void *)rip, (void *)rip);
54+
if (stackdump)
55+
show_backtrace();
56+
}
57+
58+
static int __init fprobe_init(void)
59+
{
60+
char *p, *symbuf = NULL;
61+
const char **syms;
62+
int ret, count, i;
63+
64+
sample_probe.entry_handler = sample_entry_handler;
65+
sample_probe.exit_handler = sample_exit_handler;
66+
67+
if (strchr(symbol, '*')) {
68+
/* filter based fprobe */
69+
ret = register_fprobe(&sample_probe, symbol,
70+
nosymbol[0] == '\0' ? NULL : nosymbol);
71+
goto out;
72+
} else if (!strchr(symbol, ',')) {
73+
symbuf = symbol;
74+
ret = register_fprobe_syms(&sample_probe, (const char **)&symbuf, 1);
75+
goto out;
76+
}
77+
78+
/* Comma separated symbols */
79+
symbuf = kstrdup(symbol, GFP_KERNEL);
80+
if (!symbuf)
81+
return -ENOMEM;
82+
p = symbuf;
83+
count = 1;
84+
while ((p = strchr(++p, ',')) != NULL)
85+
count++;
86+
87+
pr_info("%d symbols found\n", count);
88+
89+
syms = kcalloc(count, sizeof(char *), GFP_KERNEL);
90+
if (!syms) {
91+
kfree(symbuf);
92+
return -ENOMEM;
93+
}
94+
95+
p = symbuf;
96+
for (i = 0; i < count; i++)
97+
syms[i] = strsep(&p, ",");
98+
99+
ret = register_fprobe_syms(&sample_probe, syms, count);
100+
kfree(syms);
101+
kfree(symbuf);
102+
out:
103+
if (ret < 0)
104+
pr_err("register_fprobe failed, returned %d\n", ret);
105+
else
106+
pr_info("Planted fprobe at %s\n", symbol);
107+
108+
return ret;
109+
}
110+
111+
static void __exit fprobe_exit(void)
112+
{
113+
unregister_fprobe(&sample_probe);
114+
115+
pr_info("fprobe at %s unregistered\n", symbol);
116+
}
117+
118+
module_init(fprobe_init)
119+
module_exit(fprobe_exit)
120+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)