Merge tag 'block-5.5-2020-01-16' of git://git.kernel.dk/linux-block
[linux.git] / samples / ftrace / ftrace-direct.c
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("waking 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");