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