hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/samples/kprobes/kprobe_example.c
....@@ -1,13 +1,14 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * NOTE: This example is works on x86 and powerpc.
34 * Here's a sample kernel module showing the use of kprobes to dump a
4
- * stack trace and selected registers when _do_fork() is called.
5
+ * stack trace and selected registers when kernel_clone() is called.
56 *
67 * For more information on theory of operation of kprobes, see
7
- * Documentation/kprobes.txt
8
+ * Documentation/trace/kprobes.rst
89 *
910 * You will see the trace data in /var/log/messages and on the console
10
- * whenever _do_fork() is invoked to create a new process.
11
+ * whenever kernel_clone() is invoked to create a new process.
1112 */
1213
1314 #include <linux/kernel.h>
....@@ -15,7 +16,7 @@
1516 #include <linux/kprobes.h>
1617
1718 #define MAX_SYMBOL_LEN 64
18
-static char symbol[MAX_SYMBOL_LEN] = "_do_fork";
19
+static char symbol[MAX_SYMBOL_LEN] = "kernel_clone";
1920 module_param_string(symbol, symbol, sizeof(symbol), 0644);
2021
2122 /* For each probe you need to allocate a kprobe structure */
....@@ -24,7 +25,7 @@
2425 };
2526
2627 /* kprobe pre_handler: called just before the probed instruction is executed */
27
-static int handler_pre(struct kprobe *p, struct pt_regs *regs)
28
+static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs)
2829 {
2930 #ifdef CONFIG_X86
3031 pr_info("<%s> pre_handler: p->addr = 0x%p, ip = %lx, flags = 0x%lx\n",
....@@ -53,7 +54,7 @@
5354 }
5455
5556 /* kprobe post_handler: called after the probed instruction is executed */
56
-static void handler_post(struct kprobe *p, struct pt_regs *regs,
57
+static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs,
5758 unsigned long flags)
5859 {
5960 #ifdef CONFIG_X86
....@@ -89,6 +90,8 @@
8990 /* Return 0 because we don't handle the fault. */
9091 return 0;
9192 }
93
+/* NOKPROBE_SYMBOL() is also available */
94
+NOKPROBE_SYMBOL(handler_fault);
9295
9396 static int __init kprobe_init(void)
9497 {