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