blob: b8c90441bc874516a1c618d11d0c3507e759440c [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 Hiramatsu3da0f182014-04-17 17:18:28 +090045static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040046{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090047 return tk->rp.handler != NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040048}
49
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090050static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040051{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090052 return tk->symbol ? tk->symbol : "unknown";
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040053}
54
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090055static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +090056{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090057 return tk->rp.kp.offset;
Masami Hiramatsu61424312011-06-27 16:26:56 +090058}
59
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090060static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +090061{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090062 return !!(kprobe_gone(&tk->rp.kp));
Masami Hiramatsu61424312011-06-27 16:26:56 +090063}
64
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090065static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
Namhyung Kimc31ffb32013-07-03 13:50:51 +090066 struct module *mod)
Masami Hiramatsu61424312011-06-27 16:26:56 +090067{
68 int len = strlen(mod->name);
Namhyung Kimc31ffb32013-07-03 13:50:51 +090069 const char *name = trace_kprobe_symbol(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +090070 return strncmp(mod->name, name, len) == 0 && name[len] == ':';
71}
72
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090073static nokprobe_inline bool trace_kprobe_is_on_module(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +090074{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090075 return !!strchr(trace_kprobe_symbol(tk), ':');
Masami Hiramatsu61424312011-06-27 16:26:56 +090076}
77
Marcin Nowakowskif18f97a2016-12-09 15:19:37 +010078static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
79{
80 unsigned long nhit = 0;
81 int cpu;
82
83 for_each_possible_cpu(cpu)
84 nhit += *per_cpu_ptr(tk->nhit, cpu);
85
86 return nhit;
87}
88
Masami Hiramatsub4da3342018-01-13 02:54:04 +090089bool trace_kprobe_on_func_entry(struct trace_event_call *call)
Josef Bacik9802d862017-12-11 11:36:48 -050090{
91 struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
Masami Hiramatsub4da3342018-01-13 02:54:04 +090092
93 return kprobe_on_func_entry(tk->rp.kp.addr,
94 tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
95 tk->rp.kp.addr ? 0 : tk->rp.kp.offset);
Josef Bacik9802d862017-12-11 11:36:48 -050096}
97
Masami Hiramatsub4da3342018-01-13 02:54:04 +090098bool trace_kprobe_error_injectable(struct trace_event_call *call)
Josef Bacik9802d862017-12-11 11:36:48 -050099{
100 struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
101 unsigned long addr;
102
103 if (tk->symbol) {
104 addr = (unsigned long)
105 kallsyms_lookup_name(trace_kprobe_symbol(tk));
106 addr += tk->rp.kp.offset;
107 } else {
108 addr = (unsigned long)tk->rp.kp.addr;
109 }
110 return within_kprobe_error_injection_list(addr);
111}
112
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900113static int register_kprobe_event(struct trace_kprobe *tk);
114static int unregister_kprobe_event(struct trace_kprobe *tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400115
116static DEFINE_MUTEX(probe_lock);
117static LIST_HEAD(probe_list);
118
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400119static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
120static int kretprobe_dispatcher(struct kretprobe_instance *ri,
121 struct pt_regs *regs);
122
Namhyung Kim1301a442013-11-26 15:21:04 +0900123/* Memory fetching by symbol */
124struct symbol_cache {
125 char *symbol;
126 long offset;
127 unsigned long addr;
128};
129
130unsigned long update_symbol_cache(struct symbol_cache *sc)
131{
132 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
133
134 if (sc->addr)
135 sc->addr += sc->offset;
136
137 return sc->addr;
138}
139
140void free_symbol_cache(struct symbol_cache *sc)
141{
142 kfree(sc->symbol);
143 kfree(sc);
144}
145
146struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
147{
148 struct symbol_cache *sc;
149
150 if (!sym || strlen(sym) == 0)
151 return NULL;
152
153 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
154 if (!sc)
155 return NULL;
156
157 sc->symbol = kstrdup(sym, GFP_KERNEL);
158 if (!sc->symbol) {
159 kfree(sc);
160 return NULL;
161 }
162 sc->offset = offset;
163 update_symbol_cache(sc);
164
165 return sc;
166}
167
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900168/*
169 * Kprobes-specific fetch functions
170 */
171#define DEFINE_FETCH_stack(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900172static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900173 void *offset, void *dest) \
174{ \
175 *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
176 (unsigned int)((unsigned long)offset)); \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900177} \
178NOKPROBE_SYMBOL(FETCH_FUNC_NAME(stack, type));
179
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900180DEFINE_BASIC_FETCH_FUNCS(stack)
181/* No string on the stack entry */
182#define fetch_stack_string NULL
183#define fetch_stack_string_size NULL
184
Namhyung Kim5baaa592013-11-26 15:21:04 +0900185#define DEFINE_FETCH_memory(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900186static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
Namhyung Kim5baaa592013-11-26 15:21:04 +0900187 void *addr, void *dest) \
188{ \
189 type retval; \
190 if (probe_kernel_address(addr, retval)) \
191 *(type *)dest = 0; \
192 else \
193 *(type *)dest = retval; \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900194} \
195NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, type));
196
Namhyung Kim5baaa592013-11-26 15:21:04 +0900197DEFINE_BASIC_FETCH_FUNCS(memory)
198/*
199 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
200 * length and relative data location.
201 */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900202static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
203 void *addr, void *dest)
Namhyung Kim5baaa592013-11-26 15:21:04 +0900204{
Namhyung Kim5baaa592013-11-26 15:21:04 +0900205 int maxlen = get_rloc_len(*(u32 *)dest);
206 u8 *dst = get_rloc_data(dest);
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700207 long ret;
Namhyung Kim5baaa592013-11-26 15:21:04 +0900208
209 if (!maxlen)
210 return;
211
212 /*
213 * Try to get string again, since the string can be changed while
214 * probing.
215 */
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700216 ret = strncpy_from_unsafe(dst, addr, maxlen);
Namhyung Kim5baaa592013-11-26 15:21:04 +0900217
218 if (ret < 0) { /* Failed to fetch string */
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700219 dst[0] = '\0';
Namhyung Kim5baaa592013-11-26 15:21:04 +0900220 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
221 } else {
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700222 *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest));
Namhyung Kim5baaa592013-11-26 15:21:04 +0900223 }
224}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900225NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string));
Namhyung Kim5baaa592013-11-26 15:21:04 +0900226
227/* Return the length of string -- including null terminal byte */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900228static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
229 void *addr, void *dest)
Namhyung Kim5baaa592013-11-26 15:21:04 +0900230{
231 mm_segment_t old_fs;
232 int ret, len = 0;
233 u8 c;
234
235 old_fs = get_fs();
236 set_fs(KERNEL_DS);
237 pagefault_disable();
238
239 do {
240 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
241 len++;
242 } while (c && ret == 0 && len < MAX_STRING_SIZE);
243
244 pagefault_enable();
245 set_fs(old_fs);
246
247 if (ret < 0) /* Failed to check the length */
248 *(u32 *)dest = 0;
249 else
250 *(u32 *)dest = len;
251}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900252NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string_size));
Namhyung Kim5baaa592013-11-26 15:21:04 +0900253
Namhyung Kim1301a442013-11-26 15:21:04 +0900254#define DEFINE_FETCH_symbol(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900255void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, void *data, void *dest)\
Namhyung Kim1301a442013-11-26 15:21:04 +0900256{ \
257 struct symbol_cache *sc = data; \
258 if (sc->addr) \
259 fetch_memory_##type(regs, (void *)sc->addr, dest); \
260 else \
261 *(type *)dest = 0; \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900262} \
263NOKPROBE_SYMBOL(FETCH_FUNC_NAME(symbol, type));
264
Namhyung Kim1301a442013-11-26 15:21:04 +0900265DEFINE_BASIC_FETCH_FUNCS(symbol)
266DEFINE_FETCH_symbol(string)
267DEFINE_FETCH_symbol(string_size)
268
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900269/* kprobes don't support file_offset fetch methods */
270#define fetch_file_offset_u8 NULL
271#define fetch_file_offset_u16 NULL
272#define fetch_file_offset_u32 NULL
273#define fetch_file_offset_u64 NULL
274#define fetch_file_offset_string NULL
275#define fetch_file_offset_string_size NULL
276
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900277/* Fetch type information table */
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100278static const struct fetch_type kprobes_fetch_type_table[] = {
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900279 /* Special types */
280 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
281 sizeof(u32), 1, "__data_loc char[]"),
282 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
283 string_size, sizeof(u32), 0, "u32"),
284 /* Basic types */
285 ASSIGN_FETCH_TYPE(u8, u8, 0),
286 ASSIGN_FETCH_TYPE(u16, u16, 0),
287 ASSIGN_FETCH_TYPE(u32, u32, 0),
288 ASSIGN_FETCH_TYPE(u64, u64, 0),
289 ASSIGN_FETCH_TYPE(s8, u8, 1),
290 ASSIGN_FETCH_TYPE(s16, u16, 1),
291 ASSIGN_FETCH_TYPE(s32, u32, 1),
292 ASSIGN_FETCH_TYPE(s64, u64, 1),
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +0900293 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
294 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
295 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
296 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900297
298 ASSIGN_FETCH_TYPE_END
299};
300
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200301/*
302 * Allocate new trace_probe and initialize it (including kprobes).
303 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900304static struct trace_kprobe *alloc_trace_kprobe(const char *group,
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400305 const char *event,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200306 void *addr,
307 const char *symbol,
308 unsigned long offs,
Alban Crequy696ced42017-04-03 12:36:22 +0200309 int maxactive,
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530310 int nargs, bool is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400311{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900312 struct trace_kprobe *tk;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500313 int ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400314
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900315 tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
316 if (!tk)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500317 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400318
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800319 tk->nhit = alloc_percpu(unsigned long);
320 if (!tk->nhit)
321 goto error;
322
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400323 if (symbol) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900324 tk->symbol = kstrdup(symbol, GFP_KERNEL);
325 if (!tk->symbol)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400326 goto error;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900327 tk->rp.kp.symbol_name = tk->symbol;
328 tk->rp.kp.offset = offs;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200329 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900330 tk->rp.kp.addr = addr;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200331
332 if (is_return)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900333 tk->rp.handler = kretprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200334 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900335 tk->rp.kp.pre_handler = kprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200336
Alban Crequy696ced42017-04-03 12:36:22 +0200337 tk->rp.maxactive = maxactive;
338
Masami Hiramatsuda346342010-08-27 20:39:12 +0900339 if (!event || !is_good_name(event)) {
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500340 ret = -EINVAL;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400341 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500342 }
343
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900344 tk->tp.call.class = &tk->tp.class;
345 tk->tp.call.name = kstrdup(event, GFP_KERNEL);
346 if (!tk->tp.call.name)
Masami Hiramatsu42635652009-08-13 16:35:26 -0400347 goto error;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400348
Masami Hiramatsuda346342010-08-27 20:39:12 +0900349 if (!group || !is_good_name(group)) {
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500350 ret = -EINVAL;
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400351 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500352 }
353
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900354 tk->tp.class.system = kstrdup(group, GFP_KERNEL);
355 if (!tk->tp.class.system)
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400356 goto error;
357
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900358 INIT_LIST_HEAD(&tk->list);
359 INIT_LIST_HEAD(&tk->tp.files);
360 return tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400361error:
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900362 kfree(tk->tp.call.name);
363 kfree(tk->symbol);
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800364 free_percpu(tk->nhit);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900365 kfree(tk);
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500366 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400367}
368
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900369static void free_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400370{
371 int i;
372
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900373 for (i = 0; i < tk->tp.nr_args; i++)
374 traceprobe_free_probe_arg(&tk->tp.args[i]);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400375
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900376 kfree(tk->tp.call.class->system);
377 kfree(tk->tp.call.name);
378 kfree(tk->symbol);
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800379 free_percpu(tk->nhit);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900380 kfree(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400381}
382
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900383static struct trace_kprobe *find_trace_kprobe(const char *event,
384 const char *group)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400385{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900386 struct trace_kprobe *tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400387
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900388 list_for_each_entry(tk, &probe_list, list)
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400389 if (strcmp(trace_event_name(&tk->tp.call), event) == 0 &&
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900390 strcmp(tk->tp.call.class->system, group) == 0)
391 return tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400392 return NULL;
393}
394
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200395/*
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900396 * Enable trace_probe
397 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
398 */
399static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400400enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900401{
402 int ret = 0;
403
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900404 if (file) {
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200405 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900406
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200407 link = kmalloc(sizeof(*link), GFP_KERNEL);
408 if (!link) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900409 ret = -ENOMEM;
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200410 goto out;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900411 }
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900412
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200413 link->file = file;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900414 list_add_tail_rcu(&link->list, &tk->tp.files);
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200415
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900416 tk->tp.flags |= TP_FLAG_TRACE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900417 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900418 tk->tp.flags |= TP_FLAG_PROFILE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900419
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900420 if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) {
421 if (trace_kprobe_is_return(tk))
422 ret = enable_kretprobe(&tk->rp);
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900423 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900424 ret = enable_kprobe(&tk->rp.kp);
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900425 }
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200426 out:
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900427 return ret;
428}
429
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900430/*
431 * Disable trace_probe
432 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
433 */
434static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400435disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900436{
Masami Hiramatsua232e272013-07-09 18:35:26 +0900437 struct event_file_link *link = NULL;
438 int wait = 0;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900439 int ret = 0;
440
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900441 if (file) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900442 link = find_event_file_link(&tk->tp, file);
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200443 if (!link) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900444 ret = -EINVAL;
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200445 goto out;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900446 }
447
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200448 list_del_rcu(&link->list);
Masami Hiramatsua232e272013-07-09 18:35:26 +0900449 wait = 1;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900450 if (!list_empty(&tk->tp.files))
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200451 goto out;
452
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900453 tk->tp.flags &= ~TP_FLAG_TRACE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900454 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900455 tk->tp.flags &= ~TP_FLAG_PROFILE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900456
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900457 if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
458 if (trace_kprobe_is_return(tk))
459 disable_kretprobe(&tk->rp);
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900460 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900461 disable_kprobe(&tk->rp.kp);
Masami Hiramatsua232e272013-07-09 18:35:26 +0900462 wait = 1;
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900463 }
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200464 out:
Masami Hiramatsua232e272013-07-09 18:35:26 +0900465 if (wait) {
466 /*
467 * Synchronize with kprobe_trace_func/kretprobe_trace_func
468 * to ensure disabled (all running handlers are finished).
469 * This is not only for kfree(), but also the caller,
470 * trace_remove_event_call() supposes it for releasing
471 * event_call related objects, which will be accessed in
472 * the kprobe_trace_func/kretprobe_trace_func.
473 */
474 synchronize_sched();
475 kfree(link); /* Ignored if link == NULL */
476 }
477
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900478 return ret;
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900479}
480
Masami Hiramatsu61424312011-06-27 16:26:56 +0900481/* Internal register function - just handle k*probes and flags */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900482static int __register_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900483{
Masami Hiramatsu7f6878a2011-06-27 16:27:03 +0900484 int i, ret;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900485
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900486 if (trace_probe_is_registered(&tk->tp))
Masami Hiramatsu61424312011-06-27 16:26:56 +0900487 return -EINVAL;
488
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900489 for (i = 0; i < tk->tp.nr_args; i++)
490 traceprobe_update_arg(&tk->tp.args[i]);
Masami Hiramatsu7f6878a2011-06-27 16:27:03 +0900491
Masami Hiramatsu61424312011-06-27 16:26:56 +0900492 /* Set/clear disabled flag according to tp->flag */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900493 if (trace_probe_is_enabled(&tk->tp))
494 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900495 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900496 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900497
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900498 if (trace_kprobe_is_return(tk))
499 ret = register_kretprobe(&tk->rp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900500 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900501 ret = register_kprobe(&tk->rp.kp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900502
503 if (ret == 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900504 tk->tp.flags |= TP_FLAG_REGISTERED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900505 else {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700506 pr_warn("Could not insert probe at %s+%lu: %d\n",
507 trace_kprobe_symbol(tk), trace_kprobe_offset(tk), ret);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900508 if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700509 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
Masami Hiramatsu61424312011-06-27 16:26:56 +0900510 ret = 0;
511 } else if (ret == -EILSEQ) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700512 pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
513 tk->rp.kp.addr);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900514 ret = -EINVAL;
515 }
516 }
517
518 return ret;
519}
520
521/* Internal unregister function - just handle k*probes and flags */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900522static void __unregister_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900523{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900524 if (trace_probe_is_registered(&tk->tp)) {
525 if (trace_kprobe_is_return(tk))
526 unregister_kretprobe(&tk->rp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900527 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900528 unregister_kprobe(&tk->rp.kp);
529 tk->tp.flags &= ~TP_FLAG_REGISTERED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900530 /* Cleanup kprobe for reuse */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900531 if (tk->rp.kp.symbol_name)
532 tk->rp.kp.addr = NULL;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900533 }
534}
535
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400536/* Unregister a trace_probe and probe_event: call with locking probe_lock */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900537static int unregister_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400538{
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900539 /* Enabled event can not be unregistered */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900540 if (trace_probe_is_enabled(&tk->tp))
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900541 return -EBUSY;
542
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400543 /* Will fail if probe is being used by ftrace or perf */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900544 if (unregister_kprobe_event(tk))
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400545 return -EBUSY;
546
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900547 __unregister_trace_kprobe(tk);
548 list_del(&tk->list);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900549
550 return 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400551}
552
553/* Register a trace_probe and probe_event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900554static int register_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400555{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900556 struct trace_kprobe *old_tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400557 int ret;
558
559 mutex_lock(&probe_lock);
560
Masami Hiramatsu61424312011-06-27 16:26:56 +0900561 /* Delete old (same name) event if exist */
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400562 old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400563 tk->tp.call.class->system);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900564 if (old_tk) {
565 ret = unregister_trace_kprobe(old_tk);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900566 if (ret < 0)
567 goto end;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900568 free_trace_kprobe(old_tk);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400569 }
Masami Hiramatsu61424312011-06-27 16:26:56 +0900570
571 /* Register new event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900572 ret = register_kprobe_event(tk);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400573 if (ret) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700574 pr_warn("Failed to register probe event(%d)\n", ret);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400575 goto end;
576 }
577
Masami Hiramatsu61424312011-06-27 16:26:56 +0900578 /* Register k*probe */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900579 ret = __register_trace_kprobe(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900580 if (ret < 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900581 unregister_kprobe_event(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900582 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900583 list_add_tail(&tk->list, &probe_list);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900584
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400585end:
586 mutex_unlock(&probe_lock);
587 return ret;
588}
589
Masami Hiramatsu61424312011-06-27 16:26:56 +0900590/* Module notifier call back, checking event on the module */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900591static int trace_kprobe_module_callback(struct notifier_block *nb,
Masami Hiramatsu61424312011-06-27 16:26:56 +0900592 unsigned long val, void *data)
593{
594 struct module *mod = data;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900595 struct trace_kprobe *tk;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900596 int ret;
597
598 if (val != MODULE_STATE_COMING)
599 return NOTIFY_DONE;
600
601 /* Update probes on coming module */
602 mutex_lock(&probe_lock);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900603 list_for_each_entry(tk, &probe_list, list) {
604 if (trace_kprobe_within_module(tk, mod)) {
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900605 /* Don't need to check busy - this should have gone. */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900606 __unregister_trace_kprobe(tk);
607 ret = __register_trace_kprobe(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900608 if (ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700609 pr_warn("Failed to re-register probe %s on %s: %d\n",
610 trace_event_name(&tk->tp.call),
611 mod->name, ret);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900612 }
613 }
614 mutex_unlock(&probe_lock);
615
616 return NOTIFY_DONE;
617}
618
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900619static struct notifier_block trace_kprobe_module_nb = {
620 .notifier_call = trace_kprobe_module_callback,
Masami Hiramatsu61424312011-06-27 16:26:56 +0900621 .priority = 1 /* Invoked after kprobe module callback */
622};
623
Naveen N. Raofca18a42017-07-08 00:27:30 +0530624/* Convert certain expected symbols into '_' when generating event names */
625static inline void sanitize_event_name(char *name)
626{
627 while (*name++ != '\0')
628 if (*name == ':' || *name == '.')
629 *name = '_';
630}
631
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900632static int create_trace_kprobe(int argc, char **argv)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400633{
634 /*
635 * Argument syntax:
Alban Crequy696ced42017-04-03 12:36:22 +0200636 * - Add kprobe:
637 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
638 * - Add kretprobe:
639 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400640 * Fetch args:
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400641 * $retval : fetch return value
642 * $stack : fetch stack address
643 * $stackN : fetch Nth of stack (N:0-)
Omar Sandoval35abb672016-06-08 18:38:02 -0700644 * $comm : fetch current task comm
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400645 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
646 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
647 * %REG : fetch register REG
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400648 * Dereferencing memory fetch:
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400649 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400650 * Alias name of args:
651 * NAME=FETCHARG : set NAME as alias of FETCHARG.
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400652 * Type of args:
653 * FETCHARG:TYPE : use TYPE instead of unsigned long.
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400654 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900655 struct trace_kprobe *tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400656 int i, ret = 0;
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530657 bool is_return = false, is_delete = false;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400658 char *symbol = NULL, *event = NULL, *group = NULL;
Alban Crequy696ced42017-04-03 12:36:22 +0200659 int maxactive = 0;
Masami Hiramatsuda346342010-08-27 20:39:12 +0900660 char *arg;
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400661 unsigned long offset = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400662 void *addr = NULL;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200663 char buf[MAX_EVENT_NAME_LEN];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400664
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500665 /* argc must be >= 1 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400666 if (argv[0][0] == 'p')
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530667 is_return = false;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400668 else if (argv[0][0] == 'r')
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530669 is_return = true;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500670 else if (argv[0][0] == '-')
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530671 is_delete = true;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400672 else {
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500673 pr_info("Probe definition must be started with 'p', 'r' or"
674 " '-'.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400675 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400676 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400677
Alban Crequy696ced42017-04-03 12:36:22 +0200678 event = strchr(&argv[0][1], ':');
679 if (event) {
680 event[0] = '\0';
681 event++;
682 }
683 if (is_return && isdigit(argv[0][1])) {
684 ret = kstrtouint(&argv[0][1], 0, &maxactive);
685 if (ret) {
686 pr_info("Failed to parse maxactive.\n");
687 return ret;
688 }
689 /* kretprobes instances are iterated over via a list. The
690 * maximum should stay reasonable.
691 */
692 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
693 pr_info("Maxactive is too big (%d > %d).\n",
694 maxactive, KRETPROBE_MAXACTIVE_MAX);
695 return -E2BIG;
696 }
697 }
698
699 if (event) {
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400700 if (strchr(event, '/')) {
701 group = event;
702 event = strchr(group, '/') + 1;
703 event[-1] = '\0';
704 if (strlen(group) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800705 pr_info("Group name is not specified\n");
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400706 return -EINVAL;
707 }
708 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400709 if (strlen(event) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800710 pr_info("Event name is not specified\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400711 return -EINVAL;
712 }
713 }
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500714 if (!group)
715 group = KPROBE_EVENT_SYSTEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400716
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500717 if (is_delete) {
718 if (!event) {
719 pr_info("Delete command needs an event name.\n");
720 return -EINVAL;
721 }
Srikar Dronamraju9da79ab2010-06-30 14:15:48 +0530722 mutex_lock(&probe_lock);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900723 tk = find_trace_kprobe(event, group);
724 if (!tk) {
Srikar Dronamraju9da79ab2010-06-30 14:15:48 +0530725 mutex_unlock(&probe_lock);
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500726 pr_info("Event %s/%s doesn't exist.\n", group, event);
727 return -ENOENT;
728 }
729 /* delete an event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900730 ret = unregister_trace_kprobe(tk);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900731 if (ret == 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900732 free_trace_kprobe(tk);
Srikar Dronamraju9da79ab2010-06-30 14:15:48 +0530733 mutex_unlock(&probe_lock);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900734 return ret;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500735 }
736
737 if (argc < 2) {
738 pr_info("Probe point is not specified.\n");
739 return -EINVAL;
740 }
Sabrina Dubroca9e52b322017-06-22 11:24:42 +0200741
742 /* try to parse an address. if that fails, try to read the
743 * input as a symbol. */
744 if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400745 /* a symbol specified */
746 symbol = argv[1];
747 /* TODO: support .init module functions */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530748 ret = traceprobe_split_symbol_offset(symbol, &offset);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400749 if (ret) {
Sabrina Dubroca9e52b322017-06-22 11:24:42 +0200750 pr_info("Failed to parse either an address or a symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400751 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400752 }
Steven Rostedt (VMware)d0e02572017-02-27 11:52:04 -0500753 if (offset && is_return &&
Naveen N. Rao659b9572017-07-07 22:37:24 +0530754 !kprobe_on_func_entry(NULL, symbol, offset)) {
Steven Rostedt (VMware)d0e02572017-02-27 11:52:04 -0500755 pr_info("Given offset is not valid for return probe.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400756 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400757 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400758 }
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400759 argc -= 2; argv += 2;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400760
761 /* setup a probe */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400762 if (!event) {
763 /* Make a new event name */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400764 if (symbol)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500765 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400766 is_return ? 'r' : 'p', symbol, offset);
767 else
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500768 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400769 is_return ? 'r' : 'p', addr);
Naveen N. Raofca18a42017-07-08 00:27:30 +0530770 sanitize_event_name(buf);
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200771 event = buf;
772 }
Alban Crequy696ced42017-04-03 12:36:22 +0200773 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
774 argc, is_return);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900775 if (IS_ERR(tk)) {
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400776 pr_info("Failed to allocate trace_probe.(%d)\n",
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900777 (int)PTR_ERR(tk));
778 return PTR_ERR(tk);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400779 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400780
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400781 /* parse arguments */
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400782 ret = 0;
783 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900784 struct probe_arg *parg = &tk->tp.args[i];
785
Masami Hiramatsu61a52732010-08-27 20:38:46 +0900786 /* Increment count for freeing args in error case */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900787 tk->tp.nr_args++;
Masami Hiramatsu61a52732010-08-27 20:38:46 +0900788
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400789 /* Parse argument name */
790 arg = strchr(argv[i], '=');
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900791 if (arg) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400792 *arg++ = '\0';
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900793 parg->name = kstrdup(argv[i], GFP_KERNEL);
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900794 } else {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400795 arg = argv[i];
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900796 /* If argument name is omitted, set "argN" */
797 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900798 parg->name = kstrdup(buf, GFP_KERNEL);
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900799 }
Masami Hiramatsua703d942009-10-07 18:28:07 -0400800
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900801 if (!parg->name) {
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900802 pr_info("Failed to allocate argument[%d] name.\n", i);
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500803 ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400804 goto error;
805 }
Masami Hiramatsuda346342010-08-27 20:39:12 +0900806
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900807 if (!is_good_name(parg->name)) {
Masami Hiramatsuda346342010-08-27 20:39:12 +0900808 pr_info("Invalid argument[%d] name: %s\n",
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900809 i, parg->name);
Masami Hiramatsuda346342010-08-27 20:39:12 +0900810 ret = -EINVAL;
811 goto error;
812 }
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400813
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900814 if (traceprobe_conflict_field_name(parg->name,
815 tk->tp.args, i)) {
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900816 pr_info("Argument[%d] name '%s' conflicts with "
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400817 "another field.\n", i, argv[i]);
818 ret = -EINVAL;
819 goto error;
820 }
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500821
822 /* Parse fetch argument */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900823 ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100824 is_return, true,
825 kprobes_fetch_type_table);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400826 if (ret) {
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900827 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400828 goto error;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400829 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400830 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400831
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900832 ret = register_trace_kprobe(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400833 if (ret)
834 goto error;
835 return 0;
836
837error:
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900838 free_trace_kprobe(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400839 return ret;
840}
841
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900842static int release_all_trace_kprobes(void)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400843{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900844 struct trace_kprobe *tk;
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900845 int ret = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400846
847 mutex_lock(&probe_lock);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900848 /* Ensure no probe is in use. */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900849 list_for_each_entry(tk, &probe_list, list)
850 if (trace_probe_is_enabled(&tk->tp)) {
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900851 ret = -EBUSY;
852 goto end;
853 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400854 /* TODO: Use batch unregistration */
855 while (!list_empty(&probe_list)) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900856 tk = list_entry(probe_list.next, struct trace_kprobe, list);
857 ret = unregister_trace_kprobe(tk);
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400858 if (ret)
859 goto end;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900860 free_trace_kprobe(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400861 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900862
863end:
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400864 mutex_unlock(&probe_lock);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900865
866 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400867}
868
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400869/* Probes listing interfaces */
870static void *probes_seq_start(struct seq_file *m, loff_t *pos)
871{
872 mutex_lock(&probe_lock);
873 return seq_list_start(&probe_list, *pos);
874}
875
876static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
877{
878 return seq_list_next(v, &probe_list, pos);
879}
880
881static void probes_seq_stop(struct seq_file *m, void *v)
882{
883 mutex_unlock(&probe_lock);
884}
885
886static int probes_seq_show(struct seq_file *m, void *v)
887{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900888 struct trace_kprobe *tk = v;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400889 int i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400890
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100891 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400892 seq_printf(m, ":%s/%s", tk->tp.call.class->system,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400893 trace_event_name(&tk->tp.call));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400894
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900895 if (!tk->symbol)
896 seq_printf(m, " 0x%p", tk->rp.kp.addr);
897 else if (tk->rp.kp.offset)
898 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
899 tk->rp.kp.offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400900 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900901 seq_printf(m, " %s", trace_kprobe_symbol(tk));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400902
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900903 for (i = 0; i < tk->tp.nr_args; i++)
904 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100905 seq_putc(m, '\n');
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400906
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400907 return 0;
908}
909
910static const struct seq_operations probes_seq_op = {
911 .start = probes_seq_start,
912 .next = probes_seq_next,
913 .stop = probes_seq_stop,
914 .show = probes_seq_show
915};
916
917static int probes_open(struct inode *inode, struct file *file)
918{
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900919 int ret;
920
921 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900922 ret = release_all_trace_kprobes();
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900923 if (ret < 0)
924 return ret;
925 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400926
927 return seq_open(file, &probes_seq_op);
928}
929
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400930static ssize_t probes_write(struct file *file, const char __user *buffer,
931 size_t count, loff_t *ppos)
932{
Tom Zanussi7e465ba2017-09-22 14:58:20 -0500933 return trace_parse_run_command(file, buffer, count, ppos,
934 create_trace_kprobe);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400935}
936
937static const struct file_operations kprobe_events_ops = {
938 .owner = THIS_MODULE,
939 .open = probes_open,
940 .read = seq_read,
941 .llseek = seq_lseek,
942 .release = seq_release,
943 .write = probes_write,
944};
945
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400946/* Probes profiling interfaces */
947static int probes_profile_seq_show(struct seq_file *m, void *v)
948{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900949 struct trace_kprobe *tk = v;
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400950
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400951 seq_printf(m, " %-44s %15lu %15lu\n",
Marcin Nowakowskif18f97a2016-12-09 15:19:37 +0100952 trace_event_name(&tk->tp.call),
953 trace_kprobe_nhit(tk),
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900954 tk->rp.kp.nmissed);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400955
956 return 0;
957}
958
959static const struct seq_operations profile_seq_op = {
960 .start = probes_seq_start,
961 .next = probes_seq_next,
962 .stop = probes_seq_stop,
963 .show = probes_profile_seq_show
964};
965
966static int profile_open(struct inode *inode, struct file *file)
967{
968 return seq_open(file, &profile_seq_op);
969}
970
971static const struct file_operations kprobe_profile_ops = {
972 .owner = THIS_MODULE,
973 .open = profile_open,
974 .read = seq_read,
975 .llseek = seq_lseek,
976 .release = seq_release,
977};
978
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400979/* Kprobe handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900980static nokprobe_inline void
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900981__kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400982 struct trace_event_file *trace_file)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400983{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400984 struct kprobe_trace_entry_head *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400985 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200986 struct ring_buffer *buffer;
Masami Hiramatsue09c8612010-07-05 15:54:45 -0300987 int size, dsize, pc;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400988 unsigned long irq_flags;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400989 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400990
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400991 WARN_ON(call != trace_file->event_call);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900992
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -0400993 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500994 return;
Masami Hiramatsub8820082013-05-09 14:44:54 +0900995
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400996 local_save_flags(irq_flags);
997 pc = preempt_count();
998
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900999 dsize = __get_data_size(&tk->tp, regs);
1000 size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001001
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001002 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001003 call->event.type,
1004 size, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001005 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001006 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001007
1008 entry = ring_buffer_event_data(event);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001009 entry->ip = (unsigned long)tk->rp.kp.addr;
1010 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001011
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001012 event_trigger_unlock_commit_regs(trace_file, buffer, event,
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -05001013 entry, irq_flags, pc, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001014}
1015
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001016static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001017kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001018{
Oleg Nesterovb04d52e2013-06-20 19:38:14 +02001019 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001020
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001021 list_for_each_entry_rcu(link, &tk->tp.files, list)
1022 __kprobe_trace_func(tk, regs, link->file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001023}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001024NOKPROBE_SYMBOL(kprobe_trace_func);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001025
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001026/* Kretprobe handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001027static nokprobe_inline void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001028__kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001029 struct pt_regs *regs,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001030 struct trace_event_file *trace_file)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001031{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001032 struct kretprobe_trace_entry_head *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001033 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001034 struct ring_buffer *buffer;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001035 int size, pc, dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001036 unsigned long irq_flags;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001037 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001038
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001039 WARN_ON(call != trace_file->event_call);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001040
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -04001041 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -05001042 return;
Masami Hiramatsub8820082013-05-09 14:44:54 +09001043
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001044 local_save_flags(irq_flags);
1045 pc = preempt_count();
1046
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001047 dsize = __get_data_size(&tk->tp, regs);
1048 size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001049
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001050 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001051 call->event.type,
1052 size, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001053 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001054 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001055
1056 entry = ring_buffer_event_data(event);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001057 entry->func = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001058 entry->ret_ip = (unsigned long)ri->ret_addr;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001059 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001060
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001061 event_trigger_unlock_commit_regs(trace_file, buffer, event,
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -05001062 entry, irq_flags, pc, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001063}
1064
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001065static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001066kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001067 struct pt_regs *regs)
1068{
Oleg Nesterovb04d52e2013-06-20 19:38:14 +02001069 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001070
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001071 list_for_each_entry_rcu(link, &tk->tp.files, list)
1072 __kretprobe_trace_func(tk, ri, regs, link->file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001073}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001074NOKPROBE_SYMBOL(kretprobe_trace_func);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001075
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001076/* Event entry printers */
Masami Hiramatsub62fdd92013-05-13 20:58:39 +09001077static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001078print_kprobe_event(struct trace_iterator *iter, int flags,
1079 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001080{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001081 struct kprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001082 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001083 struct trace_probe *tp;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001084 u8 *data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001085 int i;
1086
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001087 field = (struct kprobe_trace_entry_head *)iter->ent;
Steven Rostedt80decc72010-04-23 10:00:22 -04001088 tp = container_of(event, struct trace_probe, call.event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001089
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001090 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001091
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001092 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001093 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001094
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001095 trace_seq_putc(s, ')');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001096
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001097 data = (u8 *)&field[1];
1098 for (i = 0; i < tp->nr_args; i++)
1099 if (!tp->args[i].type->print(s, tp->args[i].name,
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001100 data + tp->args[i].offset, field))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001101 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001102
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001103 trace_seq_putc(s, '\n');
1104 out:
1105 return trace_handle_return(s);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001106}
1107
Masami Hiramatsub62fdd92013-05-13 20:58:39 +09001108static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001109print_kretprobe_event(struct trace_iterator *iter, int flags,
1110 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001111{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001112 struct kretprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001113 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001114 struct trace_probe *tp;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001115 u8 *data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001116 int i;
1117
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001118 field = (struct kretprobe_trace_entry_head *)iter->ent;
Steven Rostedt80decc72010-04-23 10:00:22 -04001119 tp = container_of(event, struct trace_probe, call.event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001120
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001121 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001122
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001123 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001124 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001125
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001126 trace_seq_puts(s, " <- ");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001127
1128 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001129 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001130
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001131 trace_seq_putc(s, ')');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001132
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001133 data = (u8 *)&field[1];
1134 for (i = 0; i < tp->nr_args; i++)
1135 if (!tp->args[i].type->print(s, tp->args[i].name,
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001136 data + tp->args[i].offset, field))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001137 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001138
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001139 trace_seq_putc(s, '\n');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001140
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001141 out:
1142 return trace_handle_return(s);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001143}
1144
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001145
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001146static int kprobe_event_define_fields(struct trace_event_call *event_call)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001147{
1148 int ret, i;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001149 struct kprobe_trace_entry_head field;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001150 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001151
Masami Hiramatsua703d942009-10-07 18:28:07 -04001152 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001153 /* Set argument names as fields */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001154 for (i = 0; i < tk->tp.nr_args; i++) {
1155 struct probe_arg *parg = &tk->tp.args[i];
1156
1157 ret = trace_define_field(event_call, parg->type->fmttype,
1158 parg->name,
1159 sizeof(field) + parg->offset,
1160 parg->type->size,
1161 parg->type->is_signed,
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001162 FILTER_OTHER);
1163 if (ret)
1164 return ret;
1165 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001166 return 0;
1167}
1168
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001169static int kretprobe_event_define_fields(struct trace_event_call *event_call)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001170{
1171 int ret, i;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001172 struct kretprobe_trace_entry_head field;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001173 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001174
Masami Hiramatsua703d942009-10-07 18:28:07 -04001175 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1176 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001177 /* Set argument names as fields */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001178 for (i = 0; i < tk->tp.nr_args; i++) {
1179 struct probe_arg *parg = &tk->tp.args[i];
1180
1181 ret = trace_define_field(event_call, parg->type->fmttype,
1182 parg->name,
1183 sizeof(field) + parg->offset,
1184 parg->type->size,
1185 parg->type->is_signed,
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001186 FILTER_OTHER);
1187 if (ret)
1188 return ret;
1189 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001190 return 0;
1191}
1192
Li Zefan07b139c2009-12-21 14:27:35 +08001193#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001194
1195/* Kprobe profile handler */
Josef Bacik9802d862017-12-11 11:36:48 -05001196static int
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001197kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001198{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001199 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001200 struct kprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001201 struct hlist_head *head;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001202 int size, __size, dsize;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001203 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001204
Josef Bacik9802d862017-12-11 11:36:48 -05001205 if (bpf_prog_array_valid(call)) {
Masami Hiramatsu66665ad2018-01-13 02:54:33 +09001206 unsigned long orig_ip = instruction_pointer(regs);
Josef Bacik9802d862017-12-11 11:36:48 -05001207 int ret;
1208
1209 ret = trace_call_bpf(call, regs);
1210
1211 /*
1212 * We need to check and see if we modified the pc of the
1213 * pt_regs, and if so clear the kprobe and return 1 so that we
Masami Hiramatsu66665ad2018-01-13 02:54:33 +09001214 * don't do the single stepping.
1215 * The ftrace kprobe handler leaves it up to us to re-enable
1216 * preemption here before returning if we've modified the ip.
Josef Bacik9802d862017-12-11 11:36:48 -05001217 */
Masami Hiramatsu66665ad2018-01-13 02:54:33 +09001218 if (orig_ip != instruction_pointer(regs)) {
Josef Bacik9802d862017-12-11 11:36:48 -05001219 reset_current_kprobe();
Masami Hiramatsu66665ad2018-01-13 02:54:33 +09001220 preempt_enable_no_resched();
Josef Bacik9802d862017-12-11 11:36:48 -05001221 return 1;
1222 }
1223 if (!ret)
1224 return 0;
1225 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07001226
Oleg Nesterov288e9842013-06-20 19:38:06 +02001227 head = this_cpu_ptr(call->perf_events);
1228 if (hlist_empty(head))
Josef Bacik9802d862017-12-11 11:36:48 -05001229 return 0;
Oleg Nesterov288e9842013-06-20 19:38:06 +02001230
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001231 dsize = __get_data_size(&tk->tp, regs);
1232 __size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001233 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1234 size -= sizeof(u32);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001235
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001236 entry = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001237 if (!entry)
Josef Bacik9802d862017-12-11 11:36:48 -05001238 return 0;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001239
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001240 entry->ip = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001241 memset(&entry[1], 0, dsize);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001242 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001243 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02001244 head, NULL);
Josef Bacik9802d862017-12-11 11:36:48 -05001245 return 0;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001246}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001247NOKPROBE_SYMBOL(kprobe_perf_func);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001248
1249/* Kretprobe profile handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001250static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001251kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu2b106aa2013-05-09 14:44:41 +09001252 struct pt_regs *regs)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001253{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001254 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001255 struct kretprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001256 struct hlist_head *head;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001257 int size, __size, dsize;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001258 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001259
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001260 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
Alexei Starovoitov25415172015-03-25 12:49:20 -07001261 return;
1262
Oleg Nesterov288e9842013-06-20 19:38:06 +02001263 head = this_cpu_ptr(call->perf_events);
1264 if (hlist_empty(head))
1265 return;
1266
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001267 dsize = __get_data_size(&tk->tp, regs);
1268 __size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001269 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1270 size -= sizeof(u32);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001271
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001272 entry = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001273 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001274 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001275
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001276 entry->func = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001277 entry->ret_ip = (unsigned long)ri->ret_addr;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001278 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001279 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02001280 head, NULL);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001281}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001282NOKPROBE_SYMBOL(kretprobe_perf_func);
Li Zefan07b139c2009-12-21 14:27:35 +08001283#endif /* CONFIG_PERF_EVENTS */
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001284
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001285/*
1286 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1287 *
1288 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1289 * lockless, but we can't race with this __init function.
1290 */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001291static int kprobe_register(struct trace_event_call *event,
Masami Hiramatsufbc19632014-04-17 17:18:00 +09001292 enum trace_reg type, void *data)
Steven Rostedt22392912010-04-21 12:27:06 -04001293{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001294 struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001295 struct trace_event_file *file = data;
Masami Hiramatsu1538f882011-06-27 16:26:44 +09001296
Steven Rostedt22392912010-04-21 12:27:06 -04001297 switch (type) {
1298 case TRACE_REG_REGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001299 return enable_trace_kprobe(tk, file);
Steven Rostedt22392912010-04-21 12:27:06 -04001300 case TRACE_REG_UNREGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001301 return disable_trace_kprobe(tk, file);
Steven Rostedt22392912010-04-21 12:27:06 -04001302
1303#ifdef CONFIG_PERF_EVENTS
1304 case TRACE_REG_PERF_REGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001305 return enable_trace_kprobe(tk, NULL);
Steven Rostedt22392912010-04-21 12:27:06 -04001306 case TRACE_REG_PERF_UNREGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001307 return disable_trace_kprobe(tk, NULL);
Jiri Olsaceec0b62012-02-15 15:51:49 +01001308 case TRACE_REG_PERF_OPEN:
1309 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +01001310 case TRACE_REG_PERF_ADD:
1311 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +01001312 return 0;
Steven Rostedt22392912010-04-21 12:27:06 -04001313#endif
1314 }
1315 return 0;
1316}
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001317
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001318static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001319{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001320 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
Josef Bacik9802d862017-12-11 11:36:48 -05001321 int ret = 0;
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001322
Martin KaFai Laua7636d92016-02-03 12:28:28 -08001323 raw_cpu_inc(*tk->nhit);
Masami Hiramatsu48182bd2013-05-09 14:44:36 +09001324
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001325 if (tk->tp.flags & TP_FLAG_TRACE)
1326 kprobe_trace_func(tk, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001327#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsu66665ad2018-01-13 02:54:33 +09001328 if (tk->tp.flags & TP_FLAG_PROFILE)
Josef Bacik9802d862017-12-11 11:36:48 -05001329 ret = kprobe_perf_func(tk, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001330#endif
Josef Bacik9802d862017-12-11 11:36:48 -05001331 return ret;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001332}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001333NOKPROBE_SYMBOL(kprobe_dispatcher);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001334
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001335static int
1336kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001337{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001338 struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001339
Martin KaFai Laua7636d92016-02-03 12:28:28 -08001340 raw_cpu_inc(*tk->nhit);
Masami Hiramatsu48182bd2013-05-09 14:44:36 +09001341
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001342 if (tk->tp.flags & TP_FLAG_TRACE)
1343 kretprobe_trace_func(tk, ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001344#ifdef CONFIG_PERF_EVENTS
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001345 if (tk->tp.flags & TP_FLAG_PROFILE)
1346 kretprobe_perf_func(tk, ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001347#endif
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001348 return 0; /* We don't tweek kernel, so just return 0 */
1349}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001350NOKPROBE_SYMBOL(kretprobe_dispatcher);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001351
Steven Rostedta9a57762010-04-22 18:46:14 -04001352static struct trace_event_functions kretprobe_funcs = {
1353 .trace = print_kretprobe_event
1354};
1355
1356static struct trace_event_functions kprobe_funcs = {
1357 .trace = print_kprobe_event
1358};
1359
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001360static int register_kprobe_event(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001361{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001362 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001363 int ret;
1364
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001365 /* Initialize trace_event_call */
Li Zefanffb9f992010-05-24 16:24:52 +08001366 INIT_LIST_HEAD(&call->class->fields);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001367 if (trace_kprobe_is_return(tk)) {
Steven Rostedt80decc72010-04-23 10:00:22 -04001368 call->event.funcs = &kretprobe_funcs;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001369 call->class->define_fields = kretprobe_event_define_fields;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001370 } else {
Steven Rostedt80decc72010-04-23 10:00:22 -04001371 call->event.funcs = &kprobe_funcs;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001372 call->class->define_fields = kprobe_event_define_fields;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001373 }
Namhyung Kim5bf652a2013-07-03 16:09:02 +09001374 if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
Lai Jiangshana342a0282009-12-15 15:39:49 +08001375 return -ENOMEM;
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001376 ret = register_trace_event(&call->event);
Steven Rostedt32c0eda2010-04-23 10:38:03 -04001377 if (!ret) {
Lai Jiangshana342a0282009-12-15 15:39:49 +08001378 kfree(call->print_fmt);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001379 return -ENODEV;
Lai Jiangshana342a0282009-12-15 15:39:49 +08001380 }
Alexei Starovoitov72cbbc82015-03-25 12:49:19 -07001381 call->flags = TRACE_EVENT_FL_KPROBE;
Steven Rostedt22392912010-04-21 12:27:06 -04001382 call->class->reg = kprobe_register;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001383 call->data = tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001384 ret = trace_add_event_call(call);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001385 if (ret) {
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001386 pr_info("Failed to register kprobe event: %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001387 trace_event_name(call));
Lai Jiangshana342a0282009-12-15 15:39:49 +08001388 kfree(call->print_fmt);
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001389 unregister_trace_event(&call->event);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001390 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001391 return ret;
1392}
1393
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001394static int unregister_kprobe_event(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001395{
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -04001396 int ret;
1397
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001398 /* tp->event is unregistered in trace_remove_event_call() */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001399 ret = trace_remove_event_call(&tk->tp.call);
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -04001400 if (!ret)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001401 kfree(tk->tp.call.print_fmt);
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -04001402 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001403}
1404
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001405/* Make a tracefs interface for controlling probe points */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001406static __init int init_kprobe_trace(void)
1407{
1408 struct dentry *d_tracer;
1409 struct dentry *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001410
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001411 if (register_module_notifier(&trace_kprobe_module_nb))
Masami Hiramatsu61424312011-06-27 16:26:56 +09001412 return -EINVAL;
1413
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001414 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05001415 if (IS_ERR(d_tracer))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001416 return 0;
1417
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001418 entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001419 NULL, &kprobe_events_ops);
1420
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001421 /* Event list interface */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001422 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001423 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001424
1425 /* Profile interface */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001426 entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001427 NULL, &kprobe_profile_ops);
1428
1429 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001430 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001431 return 0;
1432}
1433fs_initcall(init_kprobe_trace);
1434
1435
1436#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedt265a5b72011-06-06 22:35:13 -04001437/*
1438 * The "__used" keeps gcc from removing the function symbol
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001439 * from the kallsyms table. 'noinline' makes sure that there
1440 * isn't an inlined version used by the test method below
Steven Rostedt265a5b72011-06-06 22:35:13 -04001441 */
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001442static __used __init noinline int
1443kprobe_trace_selftest_target(int a1, int a2, int a3, int a4, int a5, int a6)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001444{
1445 return a1 + a2 + a3 + a4 + a5 + a6;
1446}
1447
Arnd Bergmann26a346f2017-02-01 17:57:56 +01001448static __init struct trace_event_file *
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001449find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001450{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001451 struct trace_event_file *file;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001452
1453 list_for_each_entry(file, &tr->events, list)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001454 if (file->event_call == &tk->tp.call)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001455 return file;
1456
1457 return NULL;
1458}
1459
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001460/*
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001461 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001462 * stage, we can do this lockless.
1463 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001464static __init int kprobe_trace_self_tests_init(void)
1465{
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001466 int ret, warn = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001467 int (*target)(int, int, int, int, int, int);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001468 struct trace_kprobe *tk;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001469 struct trace_event_file *file;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001470
Yoshihiro YUNOMAE748ec3a2014-06-06 07:35:20 +09001471 if (tracing_is_disabled())
1472 return -ENODEV;
1473
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001474 target = kprobe_trace_selftest_target;
1475
1476 pr_info("Testing kprobe tracing: ");
1477
Tom Zanussi7e465ba2017-09-22 14:58:20 -05001478 ret = trace_run_command("p:testprobe kprobe_trace_selftest_target "
1479 "$stack $stack0 +0($stack)",
1480 create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001481 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001482 pr_warn("error on probing function entry.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001483 warn++;
1484 } else {
1485 /* Enable trace point */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001486 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1487 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001488 pr_warn("error on getting new probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001489 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001490 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001491 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001492 if (WARN_ON_ONCE(file == NULL)) {
1493 pr_warn("error on getting probe file.\n");
1494 warn++;
1495 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001496 enable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001497 }
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001498 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001499
Tom Zanussi7e465ba2017-09-22 14:58:20 -05001500 ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target "
1501 "$retval", create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001502 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001503 pr_warn("error on probing function return.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001504 warn++;
1505 } else {
1506 /* Enable trace point */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001507 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1508 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001509 pr_warn("error on getting 2nd new probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001510 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001511 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001512 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001513 if (WARN_ON_ONCE(file == NULL)) {
1514 pr_warn("error on getting probe file.\n");
1515 warn++;
1516 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001517 enable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001518 }
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001519 }
1520
1521 if (warn)
1522 goto end;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001523
1524 ret = target(1, 2, 3, 4, 5, 6);
1525
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001526 /*
1527 * Not expecting an error here, the check is only to prevent the
1528 * optimizer from removing the call to target() as otherwise there
1529 * are no side-effects and the call is never performed.
1530 */
1531 if (ret != 21)
1532 warn++;
1533
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001534 /* Disable trace points before removing it */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001535 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1536 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001537 pr_warn("error on getting test probe.\n");
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001538 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001539 } else {
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001540 if (trace_kprobe_nhit(tk) != 1) {
1541 pr_warn("incorrect number of testprobe hits\n");
1542 warn++;
1543 }
1544
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001545 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001546 if (WARN_ON_ONCE(file == NULL)) {
1547 pr_warn("error on getting probe file.\n");
1548 warn++;
1549 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001550 disable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001551 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001552
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001553 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1554 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001555 pr_warn("error on getting 2nd test probe.\n");
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001556 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001557 } else {
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001558 if (trace_kprobe_nhit(tk) != 1) {
1559 pr_warn("incorrect number of testprobe2 hits\n");
1560 warn++;
1561 }
1562
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001563 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001564 if (WARN_ON_ONCE(file == NULL)) {
1565 pr_warn("error on getting probe file.\n");
1566 warn++;
1567 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001568 disable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001569 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001570
Tom Zanussi7e465ba2017-09-22 14:58:20 -05001571 ret = trace_run_command("-:testprobe", create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001572 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001573 pr_warn("error on deleting a probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001574 warn++;
1575 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001576
Tom Zanussi7e465ba2017-09-22 14:58:20 -05001577 ret = trace_run_command("-:testprobe2", create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001578 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001579 pr_warn("error on deleting a probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001580 warn++;
1581 }
1582
1583end:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001584 release_all_trace_kprobes();
Thomas Gleixner30e7d8942017-05-17 10:19:49 +02001585 /*
1586 * Wait for the optimizer work to finish. Otherwise it might fiddle
1587 * with probes in already freed __init text.
1588 */
1589 wait_for_kprobe_optimizer();
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001590 if (warn)
1591 pr_cont("NG: Some tests are failed. Please check them.\n");
1592 else
1593 pr_cont("OK\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001594 return 0;
1595}
1596
1597late_initcall(kprobe_trace_self_tests_init);
1598
1599#endif