Skip to content

Commit b06457c

Browse files
committed
ftrace: Add sample module that uses register_ftrace_direct()
Add a sample module that shows a simple use case for regsiter_ftrace_direct(), and how to use it. Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent 013bf0d commit b06457c

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

‎samples/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ config SAMPLE_TRACE_PRINTK
1919
This builds a module that calls trace_printk() and can be used to
2020
test various trace_printk() calls from a module.
2121

22+
config SAMPLE_FTRACE_DIRECT
23+
tristate "Build register_ftrace_direct() example"
24+
depends on DYNAMIC_FTRACE_WITH_DIRECT_CALLS && m
25+
depends on X86_64 # has x86_64 inlined asm
26+
help
27+
This builds an ftrace direct function example
28+
that hooks to wake_up_process and prints the parameters.
29+
2230
config SAMPLE_KOBJECT
2331
tristate "Build kobject examples"
2432
help

‎samples/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ obj-$(CONFIG_SAMPLE_RPMSG_CLIENT) += rpmsg/
1717
subdir-$(CONFIG_SAMPLE_SECCOMP) += seccomp
1818
obj-$(CONFIG_SAMPLE_TRACE_EVENTS) += trace_events/
1919
obj-$(CONFIG_SAMPLE_TRACE_PRINTK) += trace_printk/
20+
obj-$(CONFIG_SAMPLE_FTRACE_DIRECT) += ftrace/
2021
obj-$(CONFIG_VIDEO_PCI_SKELETON) += v4l/
2122
obj-y += vfio-mdev/
2223
subdir-$(CONFIG_SAMPLE_VFS) += vfs

‎samples/ftrace/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_FTRACE_DIRECT) += ftrace-direct.o

‎samples/ftrace/ftrace-direct.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include <linux/module.h>
3+
4+
#include <linux/sched.h> /* for wake_up_process() */
5+
#include <linux/ftrace.h>
6+
7+
void my_direct_func(struct task_struct *p)
8+
{
9+
trace_printk("wakeing up %s-%d\n", p->comm, p->pid);
10+
}
11+
12+
extern void my_tramp(void *);
13+
14+
asm (
15+
" .pushsection .text, \"ax\", @progbits\n"
16+
" my_tramp:"
17+
" pushq %rbp\n"
18+
" movq %rsp, %rbp\n"
19+
" pushq %rdi\n"
20+
" call my_direct_func\n"
21+
" popq %rdi\n"
22+
" leave\n"
23+
" ret\n"
24+
" .popsection\n"
25+
);
26+
27+
28+
static int __init ftrace_direct_init(void)
29+
{
30+
return register_ftrace_direct((unsigned long)wake_up_process,
31+
(unsigned long)my_tramp);
32+
}
33+
34+
static void __exit ftrace_direct_exit(void)
35+
{
36+
unregister_ftrace_direct((unsigned long)wake_up_process,
37+
(unsigned long)my_tramp);
38+
}
39+
40+
module_init(ftrace_direct_init);
41+
module_exit(ftrace_direct_exit);
42+
43+
MODULE_AUTHOR("Steven Rostedt");
44+
MODULE_DESCRIPTION("Example use case of using register_ftrace_direct()");
45+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)