blob: f01d49b576c065080b213be30a537ea3eadd5b34 [file] [log] [blame]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001/*
Masami Hiramatsu77b44d12009-11-03 19:12:47 -05002 * Kprobes-based tracing events
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04003 *
4 * Created by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Masami Hiramatsu72576342017-02-07 20:21:28 +090019#define pr_fmt(fmt) "trace_kprobe: " fmt
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040020
21#include <linux/module.h>
22#include <linux/uaccess.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010023#include <linux/rculist.h>
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040024
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053025#include "trace_probe.h"
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040026
Masami Hiramatsuf52487e2009-09-10 19:53:53 -040027#define KPROBE_EVENT_SYSTEM "kprobes"
Alban Crequy696ced42017-04-03 12:36:22 +020028#define KRETPROBE_MAXACTIVE_MAX 4096
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040029
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040030/**
Masami Hiramatsu77b44d12009-11-03 19:12:47 -050031 * Kprobe event core functions
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040032 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +090033struct trace_kprobe {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040034 struct list_head list;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +020035 struct kretprobe rp; /* Use rp.kp for kprobe use */
Martin KaFai Laua7636d92016-02-03 12:28:28 -080036 unsigned long __percpu *nhit;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040037 const char *symbol; /* symbol name */
Namhyung Kimc31ffb32013-07-03 13:50:51 +090038 struct trace_probe tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040039};
40
Namhyung Kimc31ffb32013-07-03 13:50:51 +090041#define SIZEOF_TRACE_KPROBE(n) \
42 (offsetof(struct trace_kprobe, tp.args) + \
Masami Hiramatsueca0d912009-09-10 19:53:38 -040043 (sizeof(struct probe_arg) * (n)))
Masami Hiramatsua82378d2009-08-13 16:35:18 -040044
Masami Hiramatsu93ccae72010-04-12 13:17:08 -040045
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090046static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040047{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090048 return tk->rp.handler != NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040049}
50
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090051static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040052{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090053 return tk->symbol ? tk->symbol : "unknown";
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040054}
55
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090056static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +090057{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090058 return tk->rp.kp.offset;
Masami Hiramatsu61424312011-06-27 16:26:56 +090059}
60
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090061static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +090062{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090063 return !!(kprobe_gone(&tk->rp.kp));
Masami Hiramatsu61424312011-06-27 16:26:56 +090064}
65
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090066static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
Namhyung Kimc31ffb32013-07-03 13:50:51 +090067 struct module *mod)
Masami Hiramatsu61424312011-06-27 16:26:56 +090068{
69 int len = strlen(mod->name);
Namhyung Kimc31ffb32013-07-03 13:50:51 +090070 const char *name = trace_kprobe_symbol(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +090071 return strncmp(mod->name, name, len) == 0 && name[len] == ':';
72}
73
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090074static nokprobe_inline bool trace_kprobe_is_on_module(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +090075{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090076 return !!strchr(trace_kprobe_symbol(tk), ':');
Masami Hiramatsu61424312011-06-27 16:26:56 +090077}
78
Marcin Nowakowskif18f97a2016-12-09 15:19:37 +010079static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
80{
81 unsigned long nhit = 0;
82 int cpu;
83
84 for_each_possible_cpu(cpu)
85 nhit += *per_cpu_ptr(tk->nhit, cpu);
86
87 return nhit;
88}
89
Namhyung Kimc31ffb32013-07-03 13:50:51 +090090static int register_kprobe_event(struct trace_kprobe *tk);
91static int unregister_kprobe_event(struct trace_kprobe *tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040092
93static DEFINE_MUTEX(probe_lock);
94static LIST_HEAD(probe_list);
95
Masami Hiramatsu50d78052009-09-14 16:49:20 -040096static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
97static int kretprobe_dispatcher(struct kretprobe_instance *ri,
98 struct pt_regs *regs);
99
Namhyung Kim1301a442013-11-26 15:21:04 +0900100/* Memory fetching by symbol */
101struct symbol_cache {
102 char *symbol;
103 long offset;
104 unsigned long addr;
105};
106
107unsigned long update_symbol_cache(struct symbol_cache *sc)
108{
109 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
110
111 if (sc->addr)
112 sc->addr += sc->offset;
113
114 return sc->addr;
115}
116
117void free_symbol_cache(struct symbol_cache *sc)
118{
119 kfree(sc->symbol);
120 kfree(sc);
121}
122
123struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
124{
125 struct symbol_cache *sc;
126
127 if (!sym || strlen(sym) == 0)
128 return NULL;
129
130 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
131 if (!sc)
132 return NULL;
133
134 sc->symbol = kstrdup(sym, GFP_KERNEL);
135 if (!sc->symbol) {
136 kfree(sc);
137 return NULL;
138 }
139 sc->offset = offset;
140 update_symbol_cache(sc);
141
142 return sc;
143}
144
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900145/*
146 * Kprobes-specific fetch functions
147 */
148#define DEFINE_FETCH_stack(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900149static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900150 void *offset, void *dest) \
151{ \
152 *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
153 (unsigned int)((unsigned long)offset)); \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900154} \
155NOKPROBE_SYMBOL(FETCH_FUNC_NAME(stack, type));
156
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900157DEFINE_BASIC_FETCH_FUNCS(stack)
158/* No string on the stack entry */
159#define fetch_stack_string NULL
160#define fetch_stack_string_size NULL
161
Namhyung Kim5baaa592013-11-26 15:21:04 +0900162#define DEFINE_FETCH_memory(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900163static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
Namhyung Kim5baaa592013-11-26 15:21:04 +0900164 void *addr, void *dest) \
165{ \
166 type retval; \
167 if (probe_kernel_address(addr, retval)) \
168 *(type *)dest = 0; \
169 else \
170 *(type *)dest = retval; \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900171} \
172NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, type));
173
Namhyung Kim5baaa592013-11-26 15:21:04 +0900174DEFINE_BASIC_FETCH_FUNCS(memory)
175/*
176 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
177 * length and relative data location.
178 */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900179static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
180 void *addr, void *dest)
Namhyung Kim5baaa592013-11-26 15:21:04 +0900181{
Namhyung Kim5baaa592013-11-26 15:21:04 +0900182 int maxlen = get_rloc_len(*(u32 *)dest);
183 u8 *dst = get_rloc_data(dest);
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700184 long ret;
Namhyung Kim5baaa592013-11-26 15:21:04 +0900185
186 if (!maxlen)
187 return;
188
189 /*
190 * Try to get string again, since the string can be changed while
191 * probing.
192 */
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700193 ret = strncpy_from_unsafe(dst, addr, maxlen);
Namhyung Kim5baaa592013-11-26 15:21:04 +0900194
195 if (ret < 0) { /* Failed to fetch string */
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700196 dst[0] = '\0';
Namhyung Kim5baaa592013-11-26 15:21:04 +0900197 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
198 } else {
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700199 *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest));
Namhyung Kim5baaa592013-11-26 15:21:04 +0900200 }
201}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900202NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string));
Namhyung Kim5baaa592013-11-26 15:21:04 +0900203
204/* Return the length of string -- including null terminal byte */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900205static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
206 void *addr, void *dest)
Namhyung Kim5baaa592013-11-26 15:21:04 +0900207{
208 mm_segment_t old_fs;
209 int ret, len = 0;
210 u8 c;
211
212 old_fs = get_fs();
213 set_fs(KERNEL_DS);
214 pagefault_disable();
215
216 do {
217 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
218 len++;
219 } while (c && ret == 0 && len < MAX_STRING_SIZE);
220
221 pagefault_enable();
222 set_fs(old_fs);
223
224 if (ret < 0) /* Failed to check the length */
225 *(u32 *)dest = 0;
226 else
227 *(u32 *)dest = len;
228}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900229NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string_size));
Namhyung Kim5baaa592013-11-26 15:21:04 +0900230
Namhyung Kim1301a442013-11-26 15:21:04 +0900231#define DEFINE_FETCH_symbol(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900232void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, void *data, void *dest)\
Namhyung Kim1301a442013-11-26 15:21:04 +0900233{ \
234 struct symbol_cache *sc = data; \
235 if (sc->addr) \
236 fetch_memory_##type(regs, (void *)sc->addr, dest); \
237 else \
238 *(type *)dest = 0; \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900239} \
240NOKPROBE_SYMBOL(FETCH_FUNC_NAME(symbol, type));
241
Namhyung Kim1301a442013-11-26 15:21:04 +0900242DEFINE_BASIC_FETCH_FUNCS(symbol)
243DEFINE_FETCH_symbol(string)
244DEFINE_FETCH_symbol(string_size)
245
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900246/* kprobes don't support file_offset fetch methods */
247#define fetch_file_offset_u8 NULL
248#define fetch_file_offset_u16 NULL
249#define fetch_file_offset_u32 NULL
250#define fetch_file_offset_u64 NULL
251#define fetch_file_offset_string NULL
252#define fetch_file_offset_string_size NULL
253
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900254/* Fetch type information table */
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100255static const struct fetch_type kprobes_fetch_type_table[] = {
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900256 /* Special types */
257 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
258 sizeof(u32), 1, "__data_loc char[]"),
259 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
260 string_size, sizeof(u32), 0, "u32"),
261 /* Basic types */
262 ASSIGN_FETCH_TYPE(u8, u8, 0),
263 ASSIGN_FETCH_TYPE(u16, u16, 0),
264 ASSIGN_FETCH_TYPE(u32, u32, 0),
265 ASSIGN_FETCH_TYPE(u64, u64, 0),
266 ASSIGN_FETCH_TYPE(s8, u8, 1),
267 ASSIGN_FETCH_TYPE(s16, u16, 1),
268 ASSIGN_FETCH_TYPE(s32, u32, 1),
269 ASSIGN_FETCH_TYPE(s64, u64, 1),
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +0900270 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
271 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
272 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
273 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900274
275 ASSIGN_FETCH_TYPE_END
276};
277
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200278/*
279 * Allocate new trace_probe and initialize it (including kprobes).
280 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900281static struct trace_kprobe *alloc_trace_kprobe(const char *group,
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400282 const char *event,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200283 void *addr,
284 const char *symbol,
285 unsigned long offs,
Alban Crequy696ced42017-04-03 12:36:22 +0200286 int maxactive,
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530287 int nargs, bool is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400288{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900289 struct trace_kprobe *tk;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500290 int ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400291
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900292 tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
293 if (!tk)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500294 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400295
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800296 tk->nhit = alloc_percpu(unsigned long);
297 if (!tk->nhit)
298 goto error;
299
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400300 if (symbol) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900301 tk->symbol = kstrdup(symbol, GFP_KERNEL);
302 if (!tk->symbol)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400303 goto error;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900304 tk->rp.kp.symbol_name = tk->symbol;
305 tk->rp.kp.offset = offs;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200306 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900307 tk->rp.kp.addr = addr;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200308
309 if (is_return)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900310 tk->rp.handler = kretprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200311 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900312 tk->rp.kp.pre_handler = kprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200313
Alban Crequy696ced42017-04-03 12:36:22 +0200314 tk->rp.maxactive = maxactive;
315
Masami Hiramatsuda346342010-08-27 20:39:12 +0900316 if (!event || !is_good_name(event)) {
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500317 ret = -EINVAL;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400318 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500319 }
320
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900321 tk->tp.call.class = &tk->tp.class;
322 tk->tp.call.name = kstrdup(event, GFP_KERNEL);
323 if (!tk->tp.call.name)
Masami Hiramatsu42635652009-08-13 16:35:26 -0400324 goto error;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400325
Masami Hiramatsuda346342010-08-27 20:39:12 +0900326 if (!group || !is_good_name(group)) {
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500327 ret = -EINVAL;
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400328 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500329 }
330
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900331 tk->tp.class.system = kstrdup(group, GFP_KERNEL);
332 if (!tk->tp.class.system)
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400333 goto error;
334
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900335 INIT_LIST_HEAD(&tk->list);
336 INIT_LIST_HEAD(&tk->tp.files);
337 return tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400338error:
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900339 kfree(tk->tp.call.name);
340 kfree(tk->symbol);
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800341 free_percpu(tk->nhit);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900342 kfree(tk);
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500343 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400344}
345
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900346static void free_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400347{
348 int i;
349
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900350 for (i = 0; i < tk->tp.nr_args; i++)
351 traceprobe_free_probe_arg(&tk->tp.args[i]);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400352
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900353 kfree(tk->tp.call.class->system);
354 kfree(tk->tp.call.name);
355 kfree(tk->symbol);
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800356 free_percpu(tk->nhit);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900357 kfree(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400358}
359
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900360static struct trace_kprobe *find_trace_kprobe(const char *event,
361 const char *group)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400362{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900363 struct trace_kprobe *tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400364
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900365 list_for_each_entry(tk, &probe_list, list)
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400366 if (strcmp(trace_event_name(&tk->tp.call), event) == 0 &&
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900367 strcmp(tk->tp.call.class->system, group) == 0)
368 return tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400369 return NULL;
370}
371
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200372/*
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900373 * Enable trace_probe
374 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
375 */
376static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400377enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900378{
379 int ret = 0;
380
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900381 if (file) {
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200382 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900383
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200384 link = kmalloc(sizeof(*link), GFP_KERNEL);
385 if (!link) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900386 ret = -ENOMEM;
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200387 goto out;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900388 }
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900389
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200390 link->file = file;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900391 list_add_tail_rcu(&link->list, &tk->tp.files);
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200392
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900393 tk->tp.flags |= TP_FLAG_TRACE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900394 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900395 tk->tp.flags |= TP_FLAG_PROFILE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900396
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900397 if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) {
398 if (trace_kprobe_is_return(tk))
399 ret = enable_kretprobe(&tk->rp);
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900400 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900401 ret = enable_kprobe(&tk->rp.kp);
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900402 }
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200403 out:
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900404 return ret;
405}
406
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900407/*
408 * Disable trace_probe
409 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
410 */
411static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400412disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900413{
Masami Hiramatsua232e272013-07-09 18:35:26 +0900414 struct event_file_link *link = NULL;
415 int wait = 0;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900416 int ret = 0;
417
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900418 if (file) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900419 link = find_event_file_link(&tk->tp, file);
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200420 if (!link) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900421 ret = -EINVAL;
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200422 goto out;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900423 }
424
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200425 list_del_rcu(&link->list);
Masami Hiramatsua232e272013-07-09 18:35:26 +0900426 wait = 1;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900427 if (!list_empty(&tk->tp.files))
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200428 goto out;
429
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900430 tk->tp.flags &= ~TP_FLAG_TRACE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900431 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900432 tk->tp.flags &= ~TP_FLAG_PROFILE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900433
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900434 if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
435 if (trace_kprobe_is_return(tk))
436 disable_kretprobe(&tk->rp);
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900437 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900438 disable_kprobe(&tk->rp.kp);
Masami Hiramatsua232e272013-07-09 18:35:26 +0900439 wait = 1;
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900440 }
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200441 out:
Masami Hiramatsua232e272013-07-09 18:35:26 +0900442 if (wait) {
443 /*
444 * Synchronize with kprobe_trace_func/kretprobe_trace_func
445 * to ensure disabled (all running handlers are finished).
446 * This is not only for kfree(), but also the caller,
447 * trace_remove_event_call() supposes it for releasing
448 * event_call related objects, which will be accessed in
449 * the kprobe_trace_func/kretprobe_trace_func.
450 */
451 synchronize_sched();
452 kfree(link); /* Ignored if link == NULL */
453 }
454
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900455 return ret;
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900456}
457
Masami Hiramatsu61424312011-06-27 16:26:56 +0900458/* Internal register function - just handle k*probes and flags */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900459static int __register_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900460{
Masami Hiramatsu7f6878a2011-06-27 16:27:03 +0900461 int i, ret;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900462
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900463 if (trace_probe_is_registered(&tk->tp))
Masami Hiramatsu61424312011-06-27 16:26:56 +0900464 return -EINVAL;
465
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900466 for (i = 0; i < tk->tp.nr_args; i++)
467 traceprobe_update_arg(&tk->tp.args[i]);
Masami Hiramatsu7f6878a2011-06-27 16:27:03 +0900468
Masami Hiramatsu61424312011-06-27 16:26:56 +0900469 /* Set/clear disabled flag according to tp->flag */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900470 if (trace_probe_is_enabled(&tk->tp))
471 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900472 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900473 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900474
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900475 if (trace_kprobe_is_return(tk))
476 ret = register_kretprobe(&tk->rp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900477 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900478 ret = register_kprobe(&tk->rp.kp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900479
480 if (ret == 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900481 tk->tp.flags |= TP_FLAG_REGISTERED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900482 else {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700483 pr_warn("Could not insert probe at %s+%lu: %d\n",
484 trace_kprobe_symbol(tk), trace_kprobe_offset(tk), ret);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900485 if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700486 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
Masami Hiramatsu61424312011-06-27 16:26:56 +0900487 ret = 0;
488 } else if (ret == -EILSEQ) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700489 pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
490 tk->rp.kp.addr);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900491 ret = -EINVAL;
492 }
493 }
494
495 return ret;
496}
497
498/* Internal unregister function - just handle k*probes and flags */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900499static void __unregister_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900500{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900501 if (trace_probe_is_registered(&tk->tp)) {
502 if (trace_kprobe_is_return(tk))
503 unregister_kretprobe(&tk->rp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900504 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900505 unregister_kprobe(&tk->rp.kp);
506 tk->tp.flags &= ~TP_FLAG_REGISTERED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900507 /* Cleanup kprobe for reuse */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900508 if (tk->rp.kp.symbol_name)
509 tk->rp.kp.addr = NULL;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900510 }
511}
512
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400513/* Unregister a trace_probe and probe_event: call with locking probe_lock */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900514static int unregister_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400515{
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900516 /* Enabled event can not be unregistered */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900517 if (trace_probe_is_enabled(&tk->tp))
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900518 return -EBUSY;
519
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400520 /* Will fail if probe is being used by ftrace or perf */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900521 if (unregister_kprobe_event(tk))
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400522 return -EBUSY;
523
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900524 __unregister_trace_kprobe(tk);
525 list_del(&tk->list);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900526
527 return 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400528}
529
530/* Register a trace_probe and probe_event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900531static int register_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400532{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900533 struct trace_kprobe *old_tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400534 int ret;
535
536 mutex_lock(&probe_lock);
537
Masami Hiramatsu61424312011-06-27 16:26:56 +0900538 /* Delete old (same name) event if exist */
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400539 old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400540 tk->tp.call.class->system);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900541 if (old_tk) {
542 ret = unregister_trace_kprobe(old_tk);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900543 if (ret < 0)
544 goto end;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900545 free_trace_kprobe(old_tk);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400546 }
Masami Hiramatsu61424312011-06-27 16:26:56 +0900547
548 /* Register new event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900549 ret = register_kprobe_event(tk);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400550 if (ret) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700551 pr_warn("Failed to register probe event(%d)\n", ret);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400552 goto end;
553 }
554
Masami Hiramatsu61424312011-06-27 16:26:56 +0900555 /* Register k*probe */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900556 ret = __register_trace_kprobe(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900557 if (ret < 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900558 unregister_kprobe_event(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900559 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900560 list_add_tail(&tk->list, &probe_list);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900561
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400562end:
563 mutex_unlock(&probe_lock);
564 return ret;
565}
566
Masami Hiramatsu61424312011-06-27 16:26:56 +0900567/* Module notifier call back, checking event on the module */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900568static int trace_kprobe_module_callback(struct notifier_block *nb,
Masami Hiramatsu61424312011-06-27 16:26:56 +0900569 unsigned long val, void *data)
570{
571 struct module *mod = data;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900572 struct trace_kprobe *tk;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900573 int ret;
574
575 if (val != MODULE_STATE_COMING)
576 return NOTIFY_DONE;
577
578 /* Update probes on coming module */
579 mutex_lock(&probe_lock);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900580 list_for_each_entry(tk, &probe_list, list) {
581 if (trace_kprobe_within_module(tk, mod)) {
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900582 /* Don't need to check busy - this should have gone. */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900583 __unregister_trace_kprobe(tk);
584 ret = __register_trace_kprobe(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900585 if (ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700586 pr_warn("Failed to re-register probe %s on %s: %d\n",
587 trace_event_name(&tk->tp.call),
588 mod->name, ret);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900589 }
590 }
591 mutex_unlock(&probe_lock);
592
593 return NOTIFY_DONE;
594}
595
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900596static struct notifier_block trace_kprobe_module_nb = {
597 .notifier_call = trace_kprobe_module_callback,
Masami Hiramatsu61424312011-06-27 16:26:56 +0900598 .priority = 1 /* Invoked after kprobe module callback */
599};
600
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900601static int create_trace_kprobe(int argc, char **argv)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400602{
603 /*
604 * Argument syntax:
Alban Crequy696ced42017-04-03 12:36:22 +0200605 * - Add kprobe:
606 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
607 * - Add kretprobe:
608 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400609 * Fetch args:
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400610 * $retval : fetch return value
611 * $stack : fetch stack address
612 * $stackN : fetch Nth of stack (N:0-)
Omar Sandoval35abb672016-06-08 18:38:02 -0700613 * $comm : fetch current task comm
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400614 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
615 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
616 * %REG : fetch register REG
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400617 * Dereferencing memory fetch:
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400618 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400619 * Alias name of args:
620 * NAME=FETCHARG : set NAME as alias of FETCHARG.
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400621 * Type of args:
622 * FETCHARG:TYPE : use TYPE instead of unsigned long.
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400623 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900624 struct trace_kprobe *tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400625 int i, ret = 0;
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530626 bool is_return = false, is_delete = false;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400627 char *symbol = NULL, *event = NULL, *group = NULL;
Alban Crequy696ced42017-04-03 12:36:22 +0200628 int maxactive = 0;
Masami Hiramatsuda346342010-08-27 20:39:12 +0900629 char *arg;
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400630 unsigned long offset = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400631 void *addr = NULL;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200632 char buf[MAX_EVENT_NAME_LEN];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400633
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500634 /* argc must be >= 1 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400635 if (argv[0][0] == 'p')
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530636 is_return = false;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400637 else if (argv[0][0] == 'r')
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530638 is_return = true;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500639 else if (argv[0][0] == '-')
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530640 is_delete = true;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400641 else {
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500642 pr_info("Probe definition must be started with 'p', 'r' or"
643 " '-'.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400644 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400645 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400646
Alban Crequy696ced42017-04-03 12:36:22 +0200647 event = strchr(&argv[0][1], ':');
648 if (event) {
649 event[0] = '\0';
650 event++;
651 }
652 if (is_return && isdigit(argv[0][1])) {
653 ret = kstrtouint(&argv[0][1], 0, &maxactive);
654 if (ret) {
655 pr_info("Failed to parse maxactive.\n");
656 return ret;
657 }
658 /* kretprobes instances are iterated over via a list. The
659 * maximum should stay reasonable.
660 */
661 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
662 pr_info("Maxactive is too big (%d > %d).\n",
663 maxactive, KRETPROBE_MAXACTIVE_MAX);
664 return -E2BIG;
665 }
666 }
667
668 if (event) {
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400669 if (strchr(event, '/')) {
670 group = event;
671 event = strchr(group, '/') + 1;
672 event[-1] = '\0';
673 if (strlen(group) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800674 pr_info("Group name is not specified\n");
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400675 return -EINVAL;
676 }
677 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400678 if (strlen(event) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800679 pr_info("Event name is not specified\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400680 return -EINVAL;
681 }
682 }
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500683 if (!group)
684 group = KPROBE_EVENT_SYSTEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400685
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500686 if (is_delete) {
687 if (!event) {
688 pr_info("Delete command needs an event name.\n");
689 return -EINVAL;
690 }
Srikar Dronamraju9da79ab2010-06-30 14:15:48 +0530691 mutex_lock(&probe_lock);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900692 tk = find_trace_kprobe(event, group);
693 if (!tk) {
Srikar Dronamraju9da79ab2010-06-30 14:15:48 +0530694 mutex_unlock(&probe_lock);
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500695 pr_info("Event %s/%s doesn't exist.\n", group, event);
696 return -ENOENT;
697 }
698 /* delete an event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900699 ret = unregister_trace_kprobe(tk);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900700 if (ret == 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900701 free_trace_kprobe(tk);
Srikar Dronamraju9da79ab2010-06-30 14:15:48 +0530702 mutex_unlock(&probe_lock);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900703 return ret;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500704 }
705
706 if (argc < 2) {
707 pr_info("Probe point is not specified.\n");
708 return -EINVAL;
709 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400710 if (isdigit(argv[1][0])) {
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400711 if (is_return) {
712 pr_info("Return probe point must be a symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400713 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400714 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400715 /* an address specified */
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200716 ret = kstrtoul(&argv[1][0], 0, (unsigned long *)&addr);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400717 if (ret) {
718 pr_info("Failed to parse address.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400719 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400720 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400721 } else {
722 /* a symbol specified */
723 symbol = argv[1];
724 /* TODO: support .init module functions */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530725 ret = traceprobe_split_symbol_offset(symbol, &offset);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400726 if (ret) {
727 pr_info("Failed to parse symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400728 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400729 }
730 if (offset && is_return) {
731 pr_info("Return probe must be used without offset.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400732 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400733 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400734 }
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400735 argc -= 2; argv += 2;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400736
737 /* setup a probe */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400738 if (!event) {
739 /* Make a new event name */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400740 if (symbol)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500741 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400742 is_return ? 'r' : 'p', symbol, offset);
743 else
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500744 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400745 is_return ? 'r' : 'p', addr);
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200746 event = buf;
747 }
Alban Crequy696ced42017-04-03 12:36:22 +0200748 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
749 argc, is_return);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900750 if (IS_ERR(tk)) {
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400751 pr_info("Failed to allocate trace_probe.(%d)\n",
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900752 (int)PTR_ERR(tk));
753 return PTR_ERR(tk);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400754 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400755
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400756 /* parse arguments */
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400757 ret = 0;
758 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900759 struct probe_arg *parg = &tk->tp.args[i];
760
Masami Hiramatsu61a52732010-08-27 20:38:46 +0900761 /* Increment count for freeing args in error case */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900762 tk->tp.nr_args++;
Masami Hiramatsu61a52732010-08-27 20:38:46 +0900763
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400764 /* Parse argument name */
765 arg = strchr(argv[i], '=');
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900766 if (arg) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400767 *arg++ = '\0';
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900768 parg->name = kstrdup(argv[i], GFP_KERNEL);
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900769 } else {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400770 arg = argv[i];
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900771 /* If argument name is omitted, set "argN" */
772 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900773 parg->name = kstrdup(buf, GFP_KERNEL);
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900774 }
Masami Hiramatsua703d942009-10-07 18:28:07 -0400775
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900776 if (!parg->name) {
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900777 pr_info("Failed to allocate argument[%d] name.\n", i);
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500778 ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400779 goto error;
780 }
Masami Hiramatsuda346342010-08-27 20:39:12 +0900781
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900782 if (!is_good_name(parg->name)) {
Masami Hiramatsuda346342010-08-27 20:39:12 +0900783 pr_info("Invalid argument[%d] name: %s\n",
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900784 i, parg->name);
Masami Hiramatsuda346342010-08-27 20:39:12 +0900785 ret = -EINVAL;
786 goto error;
787 }
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400788
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900789 if (traceprobe_conflict_field_name(parg->name,
790 tk->tp.args, i)) {
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900791 pr_info("Argument[%d] name '%s' conflicts with "
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400792 "another field.\n", i, argv[i]);
793 ret = -EINVAL;
794 goto error;
795 }
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500796
797 /* Parse fetch argument */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900798 ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100799 is_return, true,
800 kprobes_fetch_type_table);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400801 if (ret) {
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900802 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400803 goto error;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400804 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400805 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400806
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900807 ret = register_trace_kprobe(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400808 if (ret)
809 goto error;
810 return 0;
811
812error:
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900813 free_trace_kprobe(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400814 return ret;
815}
816
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900817static int release_all_trace_kprobes(void)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400818{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900819 struct trace_kprobe *tk;
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900820 int ret = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400821
822 mutex_lock(&probe_lock);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900823 /* Ensure no probe is in use. */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900824 list_for_each_entry(tk, &probe_list, list)
825 if (trace_probe_is_enabled(&tk->tp)) {
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900826 ret = -EBUSY;
827 goto end;
828 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400829 /* TODO: Use batch unregistration */
830 while (!list_empty(&probe_list)) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900831 tk = list_entry(probe_list.next, struct trace_kprobe, list);
832 ret = unregister_trace_kprobe(tk);
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400833 if (ret)
834 goto end;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900835 free_trace_kprobe(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400836 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900837
838end:
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400839 mutex_unlock(&probe_lock);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900840
841 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400842}
843
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400844/* Probes listing interfaces */
845static void *probes_seq_start(struct seq_file *m, loff_t *pos)
846{
847 mutex_lock(&probe_lock);
848 return seq_list_start(&probe_list, *pos);
849}
850
851static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
852{
853 return seq_list_next(v, &probe_list, pos);
854}
855
856static void probes_seq_stop(struct seq_file *m, void *v)
857{
858 mutex_unlock(&probe_lock);
859}
860
861static int probes_seq_show(struct seq_file *m, void *v)
862{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900863 struct trace_kprobe *tk = v;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400864 int i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400865
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100866 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400867 seq_printf(m, ":%s/%s", tk->tp.call.class->system,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400868 trace_event_name(&tk->tp.call));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400869
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900870 if (!tk->symbol)
871 seq_printf(m, " 0x%p", tk->rp.kp.addr);
872 else if (tk->rp.kp.offset)
873 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
874 tk->rp.kp.offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400875 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900876 seq_printf(m, " %s", trace_kprobe_symbol(tk));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400877
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900878 for (i = 0; i < tk->tp.nr_args; i++)
879 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100880 seq_putc(m, '\n');
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400881
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400882 return 0;
883}
884
885static const struct seq_operations probes_seq_op = {
886 .start = probes_seq_start,
887 .next = probes_seq_next,
888 .stop = probes_seq_stop,
889 .show = probes_seq_show
890};
891
892static int probes_open(struct inode *inode, struct file *file)
893{
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900894 int ret;
895
896 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900897 ret = release_all_trace_kprobes();
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900898 if (ret < 0)
899 return ret;
900 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400901
902 return seq_open(file, &probes_seq_op);
903}
904
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400905static ssize_t probes_write(struct file *file, const char __user *buffer,
906 size_t count, loff_t *ppos)
907{
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530908 return traceprobe_probes_write(file, buffer, count, ppos,
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900909 create_trace_kprobe);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400910}
911
912static const struct file_operations kprobe_events_ops = {
913 .owner = THIS_MODULE,
914 .open = probes_open,
915 .read = seq_read,
916 .llseek = seq_lseek,
917 .release = seq_release,
918 .write = probes_write,
919};
920
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400921/* Probes profiling interfaces */
922static int probes_profile_seq_show(struct seq_file *m, void *v)
923{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900924 struct trace_kprobe *tk = v;
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400925
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400926 seq_printf(m, " %-44s %15lu %15lu\n",
Marcin Nowakowskif18f97a2016-12-09 15:19:37 +0100927 trace_event_name(&tk->tp.call),
928 trace_kprobe_nhit(tk),
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900929 tk->rp.kp.nmissed);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400930
931 return 0;
932}
933
934static const struct seq_operations profile_seq_op = {
935 .start = probes_seq_start,
936 .next = probes_seq_next,
937 .stop = probes_seq_stop,
938 .show = probes_profile_seq_show
939};
940
941static int profile_open(struct inode *inode, struct file *file)
942{
943 return seq_open(file, &profile_seq_op);
944}
945
946static const struct file_operations kprobe_profile_ops = {
947 .owner = THIS_MODULE,
948 .open = profile_open,
949 .read = seq_read,
950 .llseek = seq_lseek,
951 .release = seq_release,
952};
953
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400954/* Kprobe handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900955static nokprobe_inline void
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900956__kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400957 struct trace_event_file *trace_file)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400958{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400959 struct kprobe_trace_entry_head *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400960 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200961 struct ring_buffer *buffer;
Masami Hiramatsue09c8612010-07-05 15:54:45 -0300962 int size, dsize, pc;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400963 unsigned long irq_flags;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400964 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400965
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400966 WARN_ON(call != trace_file->event_call);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900967
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -0400968 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500969 return;
Masami Hiramatsub8820082013-05-09 14:44:54 +0900970
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400971 local_save_flags(irq_flags);
972 pc = preempt_count();
973
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900974 dsize = __get_data_size(&tk->tp, regs);
975 size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400976
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400977 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900978 call->event.type,
979 size, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400980 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +0800981 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400982
983 entry = ring_buffer_event_data(event);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900984 entry->ip = (unsigned long)tk->rp.kp.addr;
985 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400986
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400987 event_trigger_unlock_commit_regs(trace_file, buffer, event,
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500988 entry, irq_flags, pc, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400989}
990
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900991static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900992kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900993{
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200994 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900995
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900996 list_for_each_entry_rcu(link, &tk->tp.files, list)
997 __kprobe_trace_func(tk, regs, link->file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900998}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900999NOKPROBE_SYMBOL(kprobe_trace_func);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001000
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001001/* Kretprobe handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001002static nokprobe_inline void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001003__kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001004 struct pt_regs *regs,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001005 struct trace_event_file *trace_file)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001006{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001007 struct kretprobe_trace_entry_head *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001008 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001009 struct ring_buffer *buffer;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001010 int size, pc, dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001011 unsigned long irq_flags;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001012 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001013
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001014 WARN_ON(call != trace_file->event_call);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001015
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -04001016 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -05001017 return;
Masami Hiramatsub8820082013-05-09 14:44:54 +09001018
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001019 local_save_flags(irq_flags);
1020 pc = preempt_count();
1021
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001022 dsize = __get_data_size(&tk->tp, regs);
1023 size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001024
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001025 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001026 call->event.type,
1027 size, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001028 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001029 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001030
1031 entry = ring_buffer_event_data(event);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001032 entry->func = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001033 entry->ret_ip = (unsigned long)ri->ret_addr;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001034 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001035
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001036 event_trigger_unlock_commit_regs(trace_file, buffer, event,
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -05001037 entry, irq_flags, pc, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001038}
1039
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001040static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001041kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001042 struct pt_regs *regs)
1043{
Oleg Nesterovb04d52e2013-06-20 19:38:14 +02001044 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001045
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001046 list_for_each_entry_rcu(link, &tk->tp.files, list)
1047 __kretprobe_trace_func(tk, ri, regs, link->file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001048}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001049NOKPROBE_SYMBOL(kretprobe_trace_func);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001050
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001051/* Event entry printers */
Masami Hiramatsub62fdd92013-05-13 20:58:39 +09001052static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001053print_kprobe_event(struct trace_iterator *iter, int flags,
1054 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001055{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001056 struct kprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001057 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001058 struct trace_probe *tp;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001059 u8 *data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001060 int i;
1061
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001062 field = (struct kprobe_trace_entry_head *)iter->ent;
Steven Rostedt80decc72010-04-23 10:00:22 -04001063 tp = container_of(event, struct trace_probe, call.event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001064
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001065 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001066
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001067 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001068 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001069
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001070 trace_seq_putc(s, ')');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001071
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001072 data = (u8 *)&field[1];
1073 for (i = 0; i < tp->nr_args; i++)
1074 if (!tp->args[i].type->print(s, tp->args[i].name,
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001075 data + tp->args[i].offset, field))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001076 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001077
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001078 trace_seq_putc(s, '\n');
1079 out:
1080 return trace_handle_return(s);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001081}
1082
Masami Hiramatsub62fdd92013-05-13 20:58:39 +09001083static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001084print_kretprobe_event(struct trace_iterator *iter, int flags,
1085 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001086{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001087 struct kretprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001088 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001089 struct trace_probe *tp;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001090 u8 *data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001091 int i;
1092
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001093 field = (struct kretprobe_trace_entry_head *)iter->ent;
Steven Rostedt80decc72010-04-23 10:00:22 -04001094 tp = container_of(event, struct trace_probe, call.event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001095
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001096 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001097
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001098 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001099 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001100
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001101 trace_seq_puts(s, " <- ");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001102
1103 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001104 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001105
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001106 trace_seq_putc(s, ')');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001107
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001108 data = (u8 *)&field[1];
1109 for (i = 0; i < tp->nr_args; i++)
1110 if (!tp->args[i].type->print(s, tp->args[i].name,
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001111 data + tp->args[i].offset, field))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001112 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001113
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001114 trace_seq_putc(s, '\n');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001115
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001116 out:
1117 return trace_handle_return(s);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001118}
1119
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001120
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001121static int kprobe_event_define_fields(struct trace_event_call *event_call)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001122{
1123 int ret, i;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001124 struct kprobe_trace_entry_head field;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001125 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001126
Masami Hiramatsua703d942009-10-07 18:28:07 -04001127 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001128 /* Set argument names as fields */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001129 for (i = 0; i < tk->tp.nr_args; i++) {
1130 struct probe_arg *parg = &tk->tp.args[i];
1131
1132 ret = trace_define_field(event_call, parg->type->fmttype,
1133 parg->name,
1134 sizeof(field) + parg->offset,
1135 parg->type->size,
1136 parg->type->is_signed,
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001137 FILTER_OTHER);
1138 if (ret)
1139 return ret;
1140 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001141 return 0;
1142}
1143
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001144static int kretprobe_event_define_fields(struct trace_event_call *event_call)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001145{
1146 int ret, i;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001147 struct kretprobe_trace_entry_head field;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001148 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001149
Masami Hiramatsua703d942009-10-07 18:28:07 -04001150 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1151 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001152 /* Set argument names as fields */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001153 for (i = 0; i < tk->tp.nr_args; i++) {
1154 struct probe_arg *parg = &tk->tp.args[i];
1155
1156 ret = trace_define_field(event_call, parg->type->fmttype,
1157 parg->name,
1158 sizeof(field) + parg->offset,
1159 parg->type->size,
1160 parg->type->is_signed,
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001161 FILTER_OTHER);
1162 if (ret)
1163 return ret;
1164 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001165 return 0;
1166}
1167
Li Zefan07b139c2009-12-21 14:27:35 +08001168#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001169
1170/* Kprobe profile handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001171static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001172kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001173{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001174 struct trace_event_call *call = &tk->tp.call;
Alexei Starovoitov25415172015-03-25 12:49:20 -07001175 struct bpf_prog *prog = call->prog;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001176 struct kprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001177 struct hlist_head *head;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001178 int size, __size, dsize;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001179 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001180
Alexei Starovoitov25415172015-03-25 12:49:20 -07001181 if (prog && !trace_call_bpf(prog, regs))
1182 return;
1183
Oleg Nesterov288e9842013-06-20 19:38:06 +02001184 head = this_cpu_ptr(call->perf_events);
1185 if (hlist_empty(head))
1186 return;
1187
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001188 dsize = __get_data_size(&tk->tp, regs);
1189 __size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001190 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1191 size -= sizeof(u32);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001192
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001193 entry = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001194 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001195 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001196
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001197 entry->ip = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001198 memset(&entry[1], 0, dsize);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001199 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001200 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1201 head, NULL);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001202}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001203NOKPROBE_SYMBOL(kprobe_perf_func);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001204
1205/* Kretprobe profile handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001206static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001207kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu2b106aa2013-05-09 14:44:41 +09001208 struct pt_regs *regs)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001209{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001210 struct trace_event_call *call = &tk->tp.call;
Alexei Starovoitov25415172015-03-25 12:49:20 -07001211 struct bpf_prog *prog = call->prog;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001212 struct kretprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001213 struct hlist_head *head;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001214 int size, __size, dsize;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001215 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001216
Alexei Starovoitov25415172015-03-25 12:49:20 -07001217 if (prog && !trace_call_bpf(prog, regs))
1218 return;
1219
Oleg Nesterov288e9842013-06-20 19:38:06 +02001220 head = this_cpu_ptr(call->perf_events);
1221 if (hlist_empty(head))
1222 return;
1223
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001224 dsize = __get_data_size(&tk->tp, regs);
1225 __size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001226 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1227 size -= sizeof(u32);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001228
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001229 entry = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001230 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001231 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001232
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001233 entry->func = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001234 entry->ret_ip = (unsigned long)ri->ret_addr;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001235 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001236 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1237 head, NULL);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001238}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001239NOKPROBE_SYMBOL(kretprobe_perf_func);
Li Zefan07b139c2009-12-21 14:27:35 +08001240#endif /* CONFIG_PERF_EVENTS */
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001241
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001242/*
1243 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1244 *
1245 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1246 * lockless, but we can't race with this __init function.
1247 */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001248static int kprobe_register(struct trace_event_call *event,
Masami Hiramatsufbc19632014-04-17 17:18:00 +09001249 enum trace_reg type, void *data)
Steven Rostedt22392912010-04-21 12:27:06 -04001250{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001251 struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001252 struct trace_event_file *file = data;
Masami Hiramatsu1538f882011-06-27 16:26:44 +09001253
Steven Rostedt22392912010-04-21 12:27:06 -04001254 switch (type) {
1255 case TRACE_REG_REGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001256 return enable_trace_kprobe(tk, file);
Steven Rostedt22392912010-04-21 12:27:06 -04001257 case TRACE_REG_UNREGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001258 return disable_trace_kprobe(tk, file);
Steven Rostedt22392912010-04-21 12:27:06 -04001259
1260#ifdef CONFIG_PERF_EVENTS
1261 case TRACE_REG_PERF_REGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001262 return enable_trace_kprobe(tk, NULL);
Steven Rostedt22392912010-04-21 12:27:06 -04001263 case TRACE_REG_PERF_UNREGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001264 return disable_trace_kprobe(tk, NULL);
Jiri Olsaceec0b62012-02-15 15:51:49 +01001265 case TRACE_REG_PERF_OPEN:
1266 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +01001267 case TRACE_REG_PERF_ADD:
1268 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +01001269 return 0;
Steven Rostedt22392912010-04-21 12:27:06 -04001270#endif
1271 }
1272 return 0;
1273}
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001274
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001275static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001276{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001277 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001278
Martin KaFai Laua7636d92016-02-03 12:28:28 -08001279 raw_cpu_inc(*tk->nhit);
Masami Hiramatsu48182bd2013-05-09 14:44:36 +09001280
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001281 if (tk->tp.flags & TP_FLAG_TRACE)
1282 kprobe_trace_func(tk, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001283#ifdef CONFIG_PERF_EVENTS
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001284 if (tk->tp.flags & TP_FLAG_PROFILE)
1285 kprobe_perf_func(tk, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001286#endif
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001287 return 0; /* We don't tweek kernel, so just return 0 */
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001288}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001289NOKPROBE_SYMBOL(kprobe_dispatcher);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001290
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001291static int
1292kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001293{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001294 struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001295
Martin KaFai Laua7636d92016-02-03 12:28:28 -08001296 raw_cpu_inc(*tk->nhit);
Masami Hiramatsu48182bd2013-05-09 14:44:36 +09001297
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001298 if (tk->tp.flags & TP_FLAG_TRACE)
1299 kretprobe_trace_func(tk, ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001300#ifdef CONFIG_PERF_EVENTS
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001301 if (tk->tp.flags & TP_FLAG_PROFILE)
1302 kretprobe_perf_func(tk, ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001303#endif
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001304 return 0; /* We don't tweek kernel, so just return 0 */
1305}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001306NOKPROBE_SYMBOL(kretprobe_dispatcher);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001307
Steven Rostedta9a57762010-04-22 18:46:14 -04001308static struct trace_event_functions kretprobe_funcs = {
1309 .trace = print_kretprobe_event
1310};
1311
1312static struct trace_event_functions kprobe_funcs = {
1313 .trace = print_kprobe_event
1314};
1315
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001316static int register_kprobe_event(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001317{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001318 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001319 int ret;
1320
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001321 /* Initialize trace_event_call */
Li Zefanffb9f992010-05-24 16:24:52 +08001322 INIT_LIST_HEAD(&call->class->fields);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001323 if (trace_kprobe_is_return(tk)) {
Steven Rostedt80decc72010-04-23 10:00:22 -04001324 call->event.funcs = &kretprobe_funcs;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001325 call->class->define_fields = kretprobe_event_define_fields;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001326 } else {
Steven Rostedt80decc72010-04-23 10:00:22 -04001327 call->event.funcs = &kprobe_funcs;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001328 call->class->define_fields = kprobe_event_define_fields;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001329 }
Namhyung Kim5bf652a2013-07-03 16:09:02 +09001330 if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
Lai Jiangshana342a0282009-12-15 15:39:49 +08001331 return -ENOMEM;
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001332 ret = register_trace_event(&call->event);
Steven Rostedt32c0eda2010-04-23 10:38:03 -04001333 if (!ret) {
Lai Jiangshana342a0282009-12-15 15:39:49 +08001334 kfree(call->print_fmt);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001335 return -ENODEV;
Lai Jiangshana342a0282009-12-15 15:39:49 +08001336 }
Alexei Starovoitov72cbbc82015-03-25 12:49:19 -07001337 call->flags = TRACE_EVENT_FL_KPROBE;
Steven Rostedt22392912010-04-21 12:27:06 -04001338 call->class->reg = kprobe_register;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001339 call->data = tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001340 ret = trace_add_event_call(call);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001341 if (ret) {
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001342 pr_info("Failed to register kprobe event: %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001343 trace_event_name(call));
Lai Jiangshana342a0282009-12-15 15:39:49 +08001344 kfree(call->print_fmt);
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001345 unregister_trace_event(&call->event);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001346 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001347 return ret;
1348}
1349
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001350static int unregister_kprobe_event(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001351{
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -04001352 int ret;
1353
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001354 /* tp->event is unregistered in trace_remove_event_call() */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001355 ret = trace_remove_event_call(&tk->tp.call);
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -04001356 if (!ret)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001357 kfree(tk->tp.call.print_fmt);
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -04001358 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001359}
1360
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001361/* Make a tracefs interface for controlling probe points */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001362static __init int init_kprobe_trace(void)
1363{
1364 struct dentry *d_tracer;
1365 struct dentry *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001366
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001367 if (register_module_notifier(&trace_kprobe_module_nb))
Masami Hiramatsu61424312011-06-27 16:26:56 +09001368 return -EINVAL;
1369
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001370 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05001371 if (IS_ERR(d_tracer))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001372 return 0;
1373
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001374 entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001375 NULL, &kprobe_events_ops);
1376
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001377 /* Event list interface */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001378 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001379 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001380
1381 /* Profile interface */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001382 entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001383 NULL, &kprobe_profile_ops);
1384
1385 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001386 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001387 return 0;
1388}
1389fs_initcall(init_kprobe_trace);
1390
1391
1392#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedt265a5b72011-06-06 22:35:13 -04001393/*
1394 * The "__used" keeps gcc from removing the function symbol
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001395 * from the kallsyms table. 'noinline' makes sure that there
1396 * isn't an inlined version used by the test method below
Steven Rostedt265a5b72011-06-06 22:35:13 -04001397 */
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001398static __used __init noinline int
1399kprobe_trace_selftest_target(int a1, int a2, int a3, int a4, int a5, int a6)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001400{
1401 return a1 + a2 + a3 + a4 + a5 + a6;
1402}
1403
Arnd Bergmann26a346f2017-02-01 17:57:56 +01001404static __init struct trace_event_file *
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001405find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001406{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001407 struct trace_event_file *file;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001408
1409 list_for_each_entry(file, &tr->events, list)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001410 if (file->event_call == &tk->tp.call)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001411 return file;
1412
1413 return NULL;
1414}
1415
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001416/*
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001417 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001418 * stage, we can do this lockless.
1419 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001420static __init int kprobe_trace_self_tests_init(void)
1421{
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001422 int ret, warn = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001423 int (*target)(int, int, int, int, int, int);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001424 struct trace_kprobe *tk;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001425 struct trace_event_file *file;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001426
Yoshihiro YUNOMAE748ec3a2014-06-06 07:35:20 +09001427 if (tracing_is_disabled())
1428 return -ENODEV;
1429
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001430 target = kprobe_trace_selftest_target;
1431
1432 pr_info("Testing kprobe tracing: ");
1433
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +05301434 ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
1435 "$stack $stack0 +0($stack)",
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001436 create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001437 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001438 pr_warn("error on probing function entry.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001439 warn++;
1440 } else {
1441 /* Enable trace point */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001442 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1443 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001444 pr_warn("error on getting new probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001445 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001446 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001447 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001448 if (WARN_ON_ONCE(file == NULL)) {
1449 pr_warn("error on getting probe file.\n");
1450 warn++;
1451 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001452 enable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001453 }
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001454 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001455
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +05301456 ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001457 "$retval", create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001458 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001459 pr_warn("error on probing function return.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001460 warn++;
1461 } else {
1462 /* Enable trace point */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001463 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1464 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001465 pr_warn("error on getting 2nd new probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001466 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001467 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001468 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001469 if (WARN_ON_ONCE(file == NULL)) {
1470 pr_warn("error on getting probe file.\n");
1471 warn++;
1472 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001473 enable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001474 }
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001475 }
1476
1477 if (warn)
1478 goto end;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001479
1480 ret = target(1, 2, 3, 4, 5, 6);
1481
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001482 /*
1483 * Not expecting an error here, the check is only to prevent the
1484 * optimizer from removing the call to target() as otherwise there
1485 * are no side-effects and the call is never performed.
1486 */
1487 if (ret != 21)
1488 warn++;
1489
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001490 /* Disable trace points before removing it */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001491 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1492 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001493 pr_warn("error on getting test probe.\n");
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001494 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001495 } else {
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001496 if (trace_kprobe_nhit(tk) != 1) {
1497 pr_warn("incorrect number of testprobe hits\n");
1498 warn++;
1499 }
1500
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001501 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001502 if (WARN_ON_ONCE(file == NULL)) {
1503 pr_warn("error on getting probe file.\n");
1504 warn++;
1505 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001506 disable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001507 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001508
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001509 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1510 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001511 pr_warn("error on getting 2nd test probe.\n");
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001512 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001513 } else {
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001514 if (trace_kprobe_nhit(tk) != 1) {
1515 pr_warn("incorrect number of testprobe2 hits\n");
1516 warn++;
1517 }
1518
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001519 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001520 if (WARN_ON_ONCE(file == NULL)) {
1521 pr_warn("error on getting probe file.\n");
1522 warn++;
1523 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001524 disable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001525 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001526
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001527 ret = traceprobe_command("-:testprobe", create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001528 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001529 pr_warn("error on deleting a probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001530 warn++;
1531 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001532
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001533 ret = traceprobe_command("-:testprobe2", create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001534 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001535 pr_warn("error on deleting a probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001536 warn++;
1537 }
1538
1539end:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001540 release_all_trace_kprobes();
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001541 if (warn)
1542 pr_cont("NG: Some tests are failed. Please check them.\n");
1543 else
1544 pr_cont("OK\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001545 return 0;
1546}
1547
1548late_initcall(kprobe_trace_self_tests_init);
1549
1550#endif