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 static unsigned long nhit;
25 
26 static char symbol[MAX_SYMBOL_LEN] = "kernel_clone";
27 module_param_string(symbol, symbol, sizeof(symbol), 0644);
28 static char nosymbol[MAX_SYMBOL_LEN] = "";
29 module_param_string(nosymbol, nosymbol, sizeof(nosymbol), 0644);
30 static bool stackdump = true;
31 module_param(stackdump, bool, 0644);
32 static bool use_trace = false;
33 module_param(use_trace, bool, 0644);
34 
35 static void show_backtrace(void)
36 {
37 	unsigned long stacks[BACKTRACE_DEPTH];
38 	unsigned int len;
39 
40 	len = stack_trace_save(stacks, BACKTRACE_DEPTH, 2);
41 	stack_trace_print(stacks, len, 24);
42 }
43 
44 static void sample_entry_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs)
45 {
46 	if (use_trace)
47 		/*
48 		 * This is just an example, no kernel code should call
49 		 * trace_printk() except when actively debugging.
50 		 */
51 		trace_printk("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
52 	else
53 		pr_info("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
54 	nhit++;
55 	if (stackdump)
56 		show_backtrace();
57 }
58 
59 static void sample_exit_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs)
60 {
61 	unsigned long rip = instruction_pointer(regs);
62 
63 	if (use_trace)
64 		/*
65 		 * This is just an example, no kernel code should call
66 		 * trace_printk() except when actively debugging.
67 		 */
68 		trace_printk("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
69 			(void *)ip, (void *)ip, (void *)rip, (void *)rip);
70 	else
71 		pr_info("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
72 			(void *)ip, (void *)ip, (void *)rip, (void *)rip);
73 	nhit++;
74 	if (stackdump)
75 		show_backtrace();
76 }
77 
78 static int __init fprobe_init(void)
79 {
80 	char *p, *symbuf = NULL;
81 	const char **syms;
82 	int ret, count, i;
83 
84 	sample_probe.entry_handler = sample_entry_handler;
85 	sample_probe.exit_handler = sample_exit_handler;
86 
87 	if (strchr(symbol, '*')) {
88 		/* filter based fprobe */
89 		ret = register_fprobe(&sample_probe, symbol,
90 				      nosymbol[0] == '\0' ? NULL : nosymbol);
91 		goto out;
92 	} else if (!strchr(symbol, ',')) {
93 		symbuf = symbol;
94 		ret = register_fprobe_syms(&sample_probe, (const char **)&symbuf, 1);
95 		goto out;
96 	}
97 
98 	/* Comma separated symbols */
99 	symbuf = kstrdup(symbol, GFP_KERNEL);
100 	if (!symbuf)
101 		return -ENOMEM;
102 	p = symbuf;
103 	count = 1;
104 	while ((p = strchr(++p, ',')) != NULL)
105 		count++;
106 
107 	pr_info("%d symbols found\n", count);
108 
109 	syms = kcalloc(count, sizeof(char *), GFP_KERNEL);
110 	if (!syms) {
111 		kfree(symbuf);
112 		return -ENOMEM;
113 	}
114 
115 	p = symbuf;
116 	for (i = 0; i < count; i++)
117 		syms[i] = strsep(&p, ",");
118 
119 	ret = register_fprobe_syms(&sample_probe, syms, count);
120 	kfree(syms);
121 	kfree(symbuf);
122 out:
123 	if (ret < 0)
124 		pr_err("register_fprobe failed, returned %d\n", ret);
125 	else
126 		pr_info("Planted fprobe at %s\n", symbol);
127 
128 	return ret;
129 }
130 
131 static void __exit fprobe_exit(void)
132 {
133 	unregister_fprobe(&sample_probe);
134 
135 	pr_info("fprobe at %s unregistered. %ld times hit, %ld times missed\n",
136 		symbol, nhit, sample_probe.nmissed);
137 }
138 
139 module_init(fprobe_init)
140 module_exit(fprobe_exit)
141 MODULE_LICENSE("GPL");
142