blob: daa81571b22a4646bcc6400ccee0fe638dda2515 [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 Hiramatsu540adea2018-01-13 02:55:03 +090024#include <linux/error-injection.h>
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040025
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053026#include "trace_probe.h"
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040027
Masami Hiramatsuf52487e2009-09-10 19:53:53 -040028#define KPROBE_EVENT_SYSTEM "kprobes"
Alban Crequy696ced42017-04-03 12:36:22 +020029#define KRETPROBE_MAXACTIVE_MAX 4096
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040030
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040031/**
Masami Hiramatsu77b44d12009-11-03 19:12:47 -050032 * Kprobe event core functions
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040033 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +090034struct trace_kprobe {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040035 struct list_head list;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +020036 struct kretprobe rp; /* Use rp.kp for kprobe use */
Martin KaFai Laua7636d92016-02-03 12:28:28 -080037 unsigned long __percpu *nhit;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040038 const char *symbol; /* symbol name */
Namhyung Kimc31ffb32013-07-03 13:50:51 +090039 struct trace_probe tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040040};
41
Namhyung Kimc31ffb32013-07-03 13:50:51 +090042#define SIZEOF_TRACE_KPROBE(n) \
43 (offsetof(struct trace_kprobe, tp.args) + \
Masami Hiramatsueca0d912009-09-10 19:53:38 -040044 (sizeof(struct probe_arg) * (n)))
Masami Hiramatsua82378d2009-08-13 16:35:18 -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
Masami Hiramatsub4da3342018-01-13 02:54:04 +090090bool trace_kprobe_on_func_entry(struct trace_event_call *call)
Josef Bacik9802d862017-12-11 11:36:48 -050091{
92 struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
Masami Hiramatsub4da3342018-01-13 02:54:04 +090093
94 return kprobe_on_func_entry(tk->rp.kp.addr,
95 tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
96 tk->rp.kp.addr ? 0 : tk->rp.kp.offset);
Josef Bacik9802d862017-12-11 11:36:48 -050097}
98
Masami Hiramatsub4da3342018-01-13 02:54:04 +090099bool trace_kprobe_error_injectable(struct trace_event_call *call)
Josef Bacik9802d862017-12-11 11:36:48 -0500100{
101 struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
102 unsigned long addr;
103
104 if (tk->symbol) {
105 addr = (unsigned long)
106 kallsyms_lookup_name(trace_kprobe_symbol(tk));
107 addr += tk->rp.kp.offset;
108 } else {
109 addr = (unsigned long)tk->rp.kp.addr;
110 }
Masami Hiramatsu540adea2018-01-13 02:55:03 +0900111 return within_error_injection_list(addr);
Josef Bacik9802d862017-12-11 11:36:48 -0500112}
113
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900114static int register_kprobe_event(struct trace_kprobe *tk);
115static int unregister_kprobe_event(struct trace_kprobe *tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400116
117static DEFINE_MUTEX(probe_lock);
118static LIST_HEAD(probe_list);
119
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400120static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
121static int kretprobe_dispatcher(struct kretprobe_instance *ri,
122 struct pt_regs *regs);
123
Namhyung Kim1301a442013-11-26 15:21:04 +0900124/* Memory fetching by symbol */
125struct symbol_cache {
126 char *symbol;
127 long offset;
128 unsigned long addr;
129};
130
131unsigned long update_symbol_cache(struct symbol_cache *sc)
132{
133 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
134
135 if (sc->addr)
136 sc->addr += sc->offset;
137
138 return sc->addr;
139}
140
141void free_symbol_cache(struct symbol_cache *sc)
142{
143 kfree(sc->symbol);
144 kfree(sc);
145}
146
147struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
148{
149 struct symbol_cache *sc;
150
151 if (!sym || strlen(sym) == 0)
152 return NULL;
153
154 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
155 if (!sc)
156 return NULL;
157
158 sc->symbol = kstrdup(sym, GFP_KERNEL);
159 if (!sc->symbol) {
160 kfree(sc);
161 return NULL;
162 }
163 sc->offset = offset;
164 update_symbol_cache(sc);
165
166 return sc;
167}
168
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900169/*
170 * Kprobes-specific fetch functions
171 */
172#define DEFINE_FETCH_stack(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900173static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900174 void *offset, void *dest) \
175{ \
176 *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
177 (unsigned int)((unsigned long)offset)); \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900178} \
179NOKPROBE_SYMBOL(FETCH_FUNC_NAME(stack, type));
180
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900181DEFINE_BASIC_FETCH_FUNCS(stack)
182/* No string on the stack entry */
183#define fetch_stack_string NULL
184#define fetch_stack_string_size NULL
185
Namhyung Kim5baaa592013-11-26 15:21:04 +0900186#define DEFINE_FETCH_memory(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900187static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
Namhyung Kim5baaa592013-11-26 15:21:04 +0900188 void *addr, void *dest) \
189{ \
190 type retval; \
191 if (probe_kernel_address(addr, retval)) \
192 *(type *)dest = 0; \
193 else \
194 *(type *)dest = retval; \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900195} \
196NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, type));
197
Namhyung Kim5baaa592013-11-26 15:21:04 +0900198DEFINE_BASIC_FETCH_FUNCS(memory)
199/*
200 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
201 * length and relative data location.
202 */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900203static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
204 void *addr, void *dest)
Namhyung Kim5baaa592013-11-26 15:21:04 +0900205{
Namhyung Kim5baaa592013-11-26 15:21:04 +0900206 int maxlen = get_rloc_len(*(u32 *)dest);
207 u8 *dst = get_rloc_data(dest);
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700208 long ret;
Namhyung Kim5baaa592013-11-26 15:21:04 +0900209
210 if (!maxlen)
211 return;
212
213 /*
214 * Try to get string again, since the string can be changed while
215 * probing.
216 */
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700217 ret = strncpy_from_unsafe(dst, addr, maxlen);
Namhyung Kim5baaa592013-11-26 15:21:04 +0900218
219 if (ret < 0) { /* Failed to fetch string */
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700220 dst[0] = '\0';
Namhyung Kim5baaa592013-11-26 15:21:04 +0900221 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
222 } else {
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700223 *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest));
Namhyung Kim5baaa592013-11-26 15:21:04 +0900224 }
225}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900226NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string));
Namhyung Kim5baaa592013-11-26 15:21:04 +0900227
228/* Return the length of string -- including null terminal byte */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900229static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
230 void *addr, void *dest)
Namhyung Kim5baaa592013-11-26 15:21:04 +0900231{
232 mm_segment_t old_fs;
233 int ret, len = 0;
234 u8 c;
235
236 old_fs = get_fs();
237 set_fs(KERNEL_DS);
238 pagefault_disable();
239
240 do {
241 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
242 len++;
243 } while (c && ret == 0 && len < MAX_STRING_SIZE);
244
245 pagefault_enable();
246 set_fs(old_fs);
247
248 if (ret < 0) /* Failed to check the length */
249 *(u32 *)dest = 0;
250 else
251 *(u32 *)dest = len;
252}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900253NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string_size));
Namhyung Kim5baaa592013-11-26 15:21:04 +0900254
Namhyung Kim1301a442013-11-26 15:21:04 +0900255#define DEFINE_FETCH_symbol(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900256void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, void *data, void *dest)\
Namhyung Kim1301a442013-11-26 15:21:04 +0900257{ \
258 struct symbol_cache *sc = data; \
259 if (sc->addr) \
260 fetch_memory_##type(regs, (void *)sc->addr, dest); \
261 else \
262 *(type *)dest = 0; \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900263} \
264NOKPROBE_SYMBOL(FETCH_FUNC_NAME(symbol, type));
265
Namhyung Kim1301a442013-11-26 15:21:04 +0900266DEFINE_BASIC_FETCH_FUNCS(symbol)
267DEFINE_FETCH_symbol(string)
268DEFINE_FETCH_symbol(string_size)
269
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900270/* kprobes don't support file_offset fetch methods */
271#define fetch_file_offset_u8 NULL
272#define fetch_file_offset_u16 NULL
273#define fetch_file_offset_u32 NULL
274#define fetch_file_offset_u64 NULL
275#define fetch_file_offset_string NULL
276#define fetch_file_offset_string_size NULL
277
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900278/* Fetch type information table */
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100279static const struct fetch_type kprobes_fetch_type_table[] = {
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900280 /* Special types */
281 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
282 sizeof(u32), 1, "__data_loc char[]"),
283 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
284 string_size, sizeof(u32), 0, "u32"),
285 /* Basic types */
286 ASSIGN_FETCH_TYPE(u8, u8, 0),
287 ASSIGN_FETCH_TYPE(u16, u16, 0),
288 ASSIGN_FETCH_TYPE(u32, u32, 0),
289 ASSIGN_FETCH_TYPE(u64, u64, 0),
290 ASSIGN_FETCH_TYPE(s8, u8, 1),
291 ASSIGN_FETCH_TYPE(s16, u16, 1),
292 ASSIGN_FETCH_TYPE(s32, u32, 1),
293 ASSIGN_FETCH_TYPE(s64, u64, 1),
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +0900294 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
295 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
296 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
297 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900298
299 ASSIGN_FETCH_TYPE_END
300};
301
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200302/*
303 * Allocate new trace_probe and initialize it (including kprobes).
304 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900305static struct trace_kprobe *alloc_trace_kprobe(const char *group,
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400306 const char *event,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200307 void *addr,
308 const char *symbol,
309 unsigned long offs,
Alban Crequy696ced42017-04-03 12:36:22 +0200310 int maxactive,
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530311 int nargs, bool is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400312{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900313 struct trace_kprobe *tk;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500314 int ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400315
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900316 tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
317 if (!tk)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500318 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400319
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800320 tk->nhit = alloc_percpu(unsigned long);
321 if (!tk->nhit)
322 goto error;
323
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400324 if (symbol) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900325 tk->symbol = kstrdup(symbol, GFP_KERNEL);
326 if (!tk->symbol)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400327 goto error;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900328 tk->rp.kp.symbol_name = tk->symbol;
329 tk->rp.kp.offset = offs;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200330 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900331 tk->rp.kp.addr = addr;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200332
333 if (is_return)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900334 tk->rp.handler = kretprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200335 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900336 tk->rp.kp.pre_handler = kprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200337
Alban Crequy696ced42017-04-03 12:36:22 +0200338 tk->rp.maxactive = maxactive;
339
Masami Hiramatsuda346342010-08-27 20:39:12 +0900340 if (!event || !is_good_name(event)) {
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500341 ret = -EINVAL;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400342 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500343 }
344
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900345 tk->tp.call.class = &tk->tp.class;
346 tk->tp.call.name = kstrdup(event, GFP_KERNEL);
347 if (!tk->tp.call.name)
Masami Hiramatsu42635652009-08-13 16:35:26 -0400348 goto error;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400349
Masami Hiramatsuda346342010-08-27 20:39:12 +0900350 if (!group || !is_good_name(group)) {
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500351 ret = -EINVAL;
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400352 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500353 }
354
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900355 tk->tp.class.system = kstrdup(group, GFP_KERNEL);
356 if (!tk->tp.class.system)
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400357 goto error;
358
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900359 INIT_LIST_HEAD(&tk->list);
360 INIT_LIST_HEAD(&tk->tp.files);
361 return tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400362error:
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900363 kfree(tk->tp.call.name);
364 kfree(tk->symbol);
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800365 free_percpu(tk->nhit);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900366 kfree(tk);
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500367 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400368}
369
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900370static void free_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400371{
372 int i;
373
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900374 for (i = 0; i < tk->tp.nr_args; i++)
375 traceprobe_free_probe_arg(&tk->tp.args[i]);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400376
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900377 kfree(tk->tp.call.class->system);
378 kfree(tk->tp.call.name);
379 kfree(tk->symbol);
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800380 free_percpu(tk->nhit);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900381 kfree(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400382}
383
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900384static struct trace_kprobe *find_trace_kprobe(const char *event,
385 const char *group)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400386{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900387 struct trace_kprobe *tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400388
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900389 list_for_each_entry(tk, &probe_list, list)
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400390 if (strcmp(trace_event_name(&tk->tp.call), event) == 0 &&
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900391 strcmp(tk->tp.call.class->system, group) == 0)
392 return tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400393 return NULL;
394}
395
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200396/*
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900397 * Enable trace_probe
398 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
399 */
400static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400401enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900402{
403 int ret = 0;
404
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900405 if (file) {
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200406 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900407
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200408 link = kmalloc(sizeof(*link), GFP_KERNEL);
409 if (!link) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900410 ret = -ENOMEM;
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200411 goto out;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900412 }
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900413
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200414 link->file = file;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900415 list_add_tail_rcu(&link->list, &tk->tp.files);
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200416
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900417 tk->tp.flags |= TP_FLAG_TRACE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900418 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900419 tk->tp.flags |= TP_FLAG_PROFILE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900420
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900421 if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) {
422 if (trace_kprobe_is_return(tk))
423 ret = enable_kretprobe(&tk->rp);
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900424 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900425 ret = enable_kprobe(&tk->rp.kp);
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900426 }
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200427 out:
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900428 return ret;
429}
430
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900431/*
432 * Disable trace_probe
433 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
434 */
435static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400436disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900437{
Masami Hiramatsua232e272013-07-09 18:35:26 +0900438 struct event_file_link *link = NULL;
439 int wait = 0;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900440 int ret = 0;
441
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900442 if (file) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900443 link = find_event_file_link(&tk->tp, file);
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200444 if (!link) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900445 ret = -EINVAL;
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200446 goto out;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900447 }
448
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200449 list_del_rcu(&link->list);
Masami Hiramatsua232e272013-07-09 18:35:26 +0900450 wait = 1;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900451 if (!list_empty(&tk->tp.files))
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200452 goto out;
453
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900454 tk->tp.flags &= ~TP_FLAG_TRACE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900455 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900456 tk->tp.flags &= ~TP_FLAG_PROFILE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900457
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900458 if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
459 if (trace_kprobe_is_return(tk))
460 disable_kretprobe(&tk->rp);
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900461 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900462 disable_kprobe(&tk->rp.kp);
Masami Hiramatsua232e272013-07-09 18:35:26 +0900463 wait = 1;
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900464 }
Song Liue12f03d2017-12-06 14:45:15 -0800465
466 /*
467 * if tk is not added to any list, it must be a local trace_kprobe
468 * created with perf_event_open. We don't need to wait for these
469 * trace_kprobes
470 */
471 if (list_empty(&tk->list))
472 wait = 0;
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200473 out:
Masami Hiramatsua232e272013-07-09 18:35:26 +0900474 if (wait) {
475 /*
476 * Synchronize with kprobe_trace_func/kretprobe_trace_func
477 * to ensure disabled (all running handlers are finished).
478 * This is not only for kfree(), but also the caller,
479 * trace_remove_event_call() supposes it for releasing
480 * event_call related objects, which will be accessed in
481 * the kprobe_trace_func/kretprobe_trace_func.
482 */
483 synchronize_sched();
484 kfree(link); /* Ignored if link == NULL */
485 }
486
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900487 return ret;
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900488}
489
Masami Hiramatsu61424312011-06-27 16:26:56 +0900490/* Internal register function - just handle k*probes and flags */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900491static int __register_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900492{
Masami Hiramatsu7f6878a2011-06-27 16:27:03 +0900493 int i, ret;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900494
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900495 if (trace_probe_is_registered(&tk->tp))
Masami Hiramatsu61424312011-06-27 16:26:56 +0900496 return -EINVAL;
497
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900498 for (i = 0; i < tk->tp.nr_args; i++)
499 traceprobe_update_arg(&tk->tp.args[i]);
Masami Hiramatsu7f6878a2011-06-27 16:27:03 +0900500
Masami Hiramatsu61424312011-06-27 16:26:56 +0900501 /* Set/clear disabled flag according to tp->flag */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900502 if (trace_probe_is_enabled(&tk->tp))
503 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900504 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900505 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900506
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900507 if (trace_kprobe_is_return(tk))
508 ret = register_kretprobe(&tk->rp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900509 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900510 ret = register_kprobe(&tk->rp.kp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900511
512 if (ret == 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900513 tk->tp.flags |= TP_FLAG_REGISTERED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900514 else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900515 if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700516 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
Masami Hiramatsu61424312011-06-27 16:26:56 +0900517 ret = 0;
518 } else if (ret == -EILSEQ) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700519 pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
520 tk->rp.kp.addr);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900521 ret = -EINVAL;
522 }
523 }
524
525 return ret;
526}
527
528/* Internal unregister function - just handle k*probes and flags */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900529static void __unregister_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900530{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900531 if (trace_probe_is_registered(&tk->tp)) {
532 if (trace_kprobe_is_return(tk))
533 unregister_kretprobe(&tk->rp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900534 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900535 unregister_kprobe(&tk->rp.kp);
536 tk->tp.flags &= ~TP_FLAG_REGISTERED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900537 /* Cleanup kprobe for reuse */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900538 if (tk->rp.kp.symbol_name)
539 tk->rp.kp.addr = NULL;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900540 }
541}
542
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400543/* Unregister a trace_probe and probe_event: call with locking probe_lock */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900544static int unregister_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400545{
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900546 /* Enabled event can not be unregistered */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900547 if (trace_probe_is_enabled(&tk->tp))
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900548 return -EBUSY;
549
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400550 /* Will fail if probe is being used by ftrace or perf */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900551 if (unregister_kprobe_event(tk))
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400552 return -EBUSY;
553
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900554 __unregister_trace_kprobe(tk);
555 list_del(&tk->list);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900556
557 return 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400558}
559
560/* Register a trace_probe and probe_event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900561static int register_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400562{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900563 struct trace_kprobe *old_tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400564 int ret;
565
566 mutex_lock(&probe_lock);
567
Masami Hiramatsu61424312011-06-27 16:26:56 +0900568 /* Delete old (same name) event if exist */
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400569 old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400570 tk->tp.call.class->system);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900571 if (old_tk) {
572 ret = unregister_trace_kprobe(old_tk);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900573 if (ret < 0)
574 goto end;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900575 free_trace_kprobe(old_tk);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400576 }
Masami Hiramatsu61424312011-06-27 16:26:56 +0900577
578 /* Register new event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900579 ret = register_kprobe_event(tk);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400580 if (ret) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700581 pr_warn("Failed to register probe event(%d)\n", ret);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400582 goto end;
583 }
584
Masami Hiramatsu61424312011-06-27 16:26:56 +0900585 /* Register k*probe */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900586 ret = __register_trace_kprobe(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900587 if (ret < 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900588 unregister_kprobe_event(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900589 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900590 list_add_tail(&tk->list, &probe_list);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900591
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400592end:
593 mutex_unlock(&probe_lock);
594 return ret;
595}
596
Masami Hiramatsu61424312011-06-27 16:26:56 +0900597/* Module notifier call back, checking event on the module */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900598static int trace_kprobe_module_callback(struct notifier_block *nb,
Masami Hiramatsu61424312011-06-27 16:26:56 +0900599 unsigned long val, void *data)
600{
601 struct module *mod = data;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900602 struct trace_kprobe *tk;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900603 int ret;
604
605 if (val != MODULE_STATE_COMING)
606 return NOTIFY_DONE;
607
608 /* Update probes on coming module */
609 mutex_lock(&probe_lock);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900610 list_for_each_entry(tk, &probe_list, list) {
611 if (trace_kprobe_within_module(tk, mod)) {
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900612 /* Don't need to check busy - this should have gone. */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900613 __unregister_trace_kprobe(tk);
614 ret = __register_trace_kprobe(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900615 if (ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700616 pr_warn("Failed to re-register probe %s on %s: %d\n",
617 trace_event_name(&tk->tp.call),
618 mod->name, ret);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900619 }
620 }
621 mutex_unlock(&probe_lock);
622
623 return NOTIFY_DONE;
624}
625
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900626static struct notifier_block trace_kprobe_module_nb = {
627 .notifier_call = trace_kprobe_module_callback,
Masami Hiramatsu61424312011-06-27 16:26:56 +0900628 .priority = 1 /* Invoked after kprobe module callback */
629};
630
Naveen N. Raofca18a42017-07-08 00:27:30 +0530631/* Convert certain expected symbols into '_' when generating event names */
632static inline void sanitize_event_name(char *name)
633{
634 while (*name++ != '\0')
635 if (*name == ':' || *name == '.')
636 *name = '_';
637}
638
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900639static int create_trace_kprobe(int argc, char **argv)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400640{
641 /*
642 * Argument syntax:
Alban Crequy696ced42017-04-03 12:36:22 +0200643 * - Add kprobe:
644 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
645 * - Add kretprobe:
646 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400647 * Fetch args:
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400648 * $retval : fetch return value
649 * $stack : fetch stack address
650 * $stackN : fetch Nth of stack (N:0-)
Omar Sandoval35abb672016-06-08 18:38:02 -0700651 * $comm : fetch current task comm
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400652 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
653 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
654 * %REG : fetch register REG
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400655 * Dereferencing memory fetch:
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400656 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400657 * Alias name of args:
658 * NAME=FETCHARG : set NAME as alias of FETCHARG.
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400659 * Type of args:
660 * FETCHARG:TYPE : use TYPE instead of unsigned long.
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400661 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900662 struct trace_kprobe *tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400663 int i, ret = 0;
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530664 bool is_return = false, is_delete = false;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400665 char *symbol = NULL, *event = NULL, *group = NULL;
Alban Crequy696ced42017-04-03 12:36:22 +0200666 int maxactive = 0;
Masami Hiramatsuda346342010-08-27 20:39:12 +0900667 char *arg;
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900668 long offset = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400669 void *addr = NULL;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200670 char buf[MAX_EVENT_NAME_LEN];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400671
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500672 /* argc must be >= 1 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400673 if (argv[0][0] == 'p')
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530674 is_return = false;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400675 else if (argv[0][0] == 'r')
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530676 is_return = true;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500677 else if (argv[0][0] == '-')
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530678 is_delete = true;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400679 else {
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500680 pr_info("Probe definition must be started with 'p', 'r' or"
681 " '-'.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400682 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400683 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400684
Alban Crequy696ced42017-04-03 12:36:22 +0200685 event = strchr(&argv[0][1], ':');
686 if (event) {
687 event[0] = '\0';
688 event++;
689 }
690 if (is_return && isdigit(argv[0][1])) {
691 ret = kstrtouint(&argv[0][1], 0, &maxactive);
692 if (ret) {
693 pr_info("Failed to parse maxactive.\n");
694 return ret;
695 }
696 /* kretprobes instances are iterated over via a list. The
697 * maximum should stay reasonable.
698 */
699 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
700 pr_info("Maxactive is too big (%d > %d).\n",
701 maxactive, KRETPROBE_MAXACTIVE_MAX);
702 return -E2BIG;
703 }
704 }
705
706 if (event) {
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400707 if (strchr(event, '/')) {
708 group = event;
709 event = strchr(group, '/') + 1;
710 event[-1] = '\0';
711 if (strlen(group) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800712 pr_info("Group name is not specified\n");
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400713 return -EINVAL;
714 }
715 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400716 if (strlen(event) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800717 pr_info("Event name is not specified\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400718 return -EINVAL;
719 }
720 }
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500721 if (!group)
722 group = KPROBE_EVENT_SYSTEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400723
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500724 if (is_delete) {
725 if (!event) {
726 pr_info("Delete command needs an event name.\n");
727 return -EINVAL;
728 }
Srikar Dronamraju9da79ab2010-06-30 14:15:48 +0530729 mutex_lock(&probe_lock);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900730 tk = find_trace_kprobe(event, group);
731 if (!tk) {
Srikar Dronamraju9da79ab2010-06-30 14:15:48 +0530732 mutex_unlock(&probe_lock);
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500733 pr_info("Event %s/%s doesn't exist.\n", group, event);
734 return -ENOENT;
735 }
736 /* delete an event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900737 ret = unregister_trace_kprobe(tk);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900738 if (ret == 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900739 free_trace_kprobe(tk);
Srikar Dronamraju9da79ab2010-06-30 14:15:48 +0530740 mutex_unlock(&probe_lock);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900741 return ret;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500742 }
743
744 if (argc < 2) {
745 pr_info("Probe point is not specified.\n");
746 return -EINVAL;
747 }
Sabrina Dubroca9e52b322017-06-22 11:24:42 +0200748
749 /* try to parse an address. if that fails, try to read the
750 * input as a symbol. */
751 if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400752 /* a symbol specified */
753 symbol = argv[1];
754 /* TODO: support .init module functions */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530755 ret = traceprobe_split_symbol_offset(symbol, &offset);
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900756 if (ret || offset < 0 || offset > UINT_MAX) {
Sabrina Dubroca9e52b322017-06-22 11:24:42 +0200757 pr_info("Failed to parse either an address or a symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400758 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400759 }
Steven Rostedt (VMware)d0e02572017-02-27 11:52:04 -0500760 if (offset && is_return &&
Naveen N. Rao659b9572017-07-07 22:37:24 +0530761 !kprobe_on_func_entry(NULL, symbol, offset)) {
Steven Rostedt (VMware)d0e02572017-02-27 11:52:04 -0500762 pr_info("Given offset is not valid for return probe.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400763 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400764 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400765 }
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400766 argc -= 2; argv += 2;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400767
768 /* setup a probe */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400769 if (!event) {
770 /* Make a new event name */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400771 if (symbol)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500772 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400773 is_return ? 'r' : 'p', symbol, offset);
774 else
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500775 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400776 is_return ? 'r' : 'p', addr);
Naveen N. Raofca18a42017-07-08 00:27:30 +0530777 sanitize_event_name(buf);
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200778 event = buf;
779 }
Alban Crequy696ced42017-04-03 12:36:22 +0200780 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
781 argc, is_return);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900782 if (IS_ERR(tk)) {
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400783 pr_info("Failed to allocate trace_probe.(%d)\n",
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900784 (int)PTR_ERR(tk));
785 return PTR_ERR(tk);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400786 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400787
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400788 /* parse arguments */
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400789 ret = 0;
790 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900791 struct probe_arg *parg = &tk->tp.args[i];
792
Masami Hiramatsu61a52732010-08-27 20:38:46 +0900793 /* Increment count for freeing args in error case */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900794 tk->tp.nr_args++;
Masami Hiramatsu61a52732010-08-27 20:38:46 +0900795
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400796 /* Parse argument name */
797 arg = strchr(argv[i], '=');
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900798 if (arg) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400799 *arg++ = '\0';
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900800 parg->name = kstrdup(argv[i], GFP_KERNEL);
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900801 } else {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400802 arg = argv[i];
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900803 /* If argument name is omitted, set "argN" */
804 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900805 parg->name = kstrdup(buf, GFP_KERNEL);
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900806 }
Masami Hiramatsua703d942009-10-07 18:28:07 -0400807
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900808 if (!parg->name) {
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900809 pr_info("Failed to allocate argument[%d] name.\n", i);
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500810 ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400811 goto error;
812 }
Masami Hiramatsuda346342010-08-27 20:39:12 +0900813
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900814 if (!is_good_name(parg->name)) {
Masami Hiramatsuda346342010-08-27 20:39:12 +0900815 pr_info("Invalid argument[%d] name: %s\n",
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900816 i, parg->name);
Masami Hiramatsuda346342010-08-27 20:39:12 +0900817 ret = -EINVAL;
818 goto error;
819 }
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400820
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900821 if (traceprobe_conflict_field_name(parg->name,
822 tk->tp.args, i)) {
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900823 pr_info("Argument[%d] name '%s' conflicts with "
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400824 "another field.\n", i, argv[i]);
825 ret = -EINVAL;
826 goto error;
827 }
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500828
829 /* Parse fetch argument */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900830 ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100831 is_return, true,
832 kprobes_fetch_type_table);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400833 if (ret) {
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900834 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400835 goto error;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400836 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400837 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400838
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900839 ret = register_trace_kprobe(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400840 if (ret)
841 goto error;
842 return 0;
843
844error:
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900845 free_trace_kprobe(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400846 return ret;
847}
848
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900849static int release_all_trace_kprobes(void)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400850{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900851 struct trace_kprobe *tk;
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900852 int ret = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400853
854 mutex_lock(&probe_lock);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900855 /* Ensure no probe is in use. */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900856 list_for_each_entry(tk, &probe_list, list)
857 if (trace_probe_is_enabled(&tk->tp)) {
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900858 ret = -EBUSY;
859 goto end;
860 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400861 /* TODO: Use batch unregistration */
862 while (!list_empty(&probe_list)) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900863 tk = list_entry(probe_list.next, struct trace_kprobe, list);
864 ret = unregister_trace_kprobe(tk);
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400865 if (ret)
866 goto end;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900867 free_trace_kprobe(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400868 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900869
870end:
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400871 mutex_unlock(&probe_lock);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900872
873 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400874}
875
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400876/* Probes listing interfaces */
877static void *probes_seq_start(struct seq_file *m, loff_t *pos)
878{
879 mutex_lock(&probe_lock);
880 return seq_list_start(&probe_list, *pos);
881}
882
883static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
884{
885 return seq_list_next(v, &probe_list, pos);
886}
887
888static void probes_seq_stop(struct seq_file *m, void *v)
889{
890 mutex_unlock(&probe_lock);
891}
892
893static int probes_seq_show(struct seq_file *m, void *v)
894{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900895 struct trace_kprobe *tk = v;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400896 int i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400897
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100898 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400899 seq_printf(m, ":%s/%s", tk->tp.call.class->system,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400900 trace_event_name(&tk->tp.call));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400901
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900902 if (!tk->symbol)
903 seq_printf(m, " 0x%p", tk->rp.kp.addr);
904 else if (tk->rp.kp.offset)
905 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
906 tk->rp.kp.offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400907 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900908 seq_printf(m, " %s", trace_kprobe_symbol(tk));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400909
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900910 for (i = 0; i < tk->tp.nr_args; i++)
911 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100912 seq_putc(m, '\n');
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400913
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400914 return 0;
915}
916
917static const struct seq_operations probes_seq_op = {
918 .start = probes_seq_start,
919 .next = probes_seq_next,
920 .stop = probes_seq_stop,
921 .show = probes_seq_show
922};
923
924static int probes_open(struct inode *inode, struct file *file)
925{
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900926 int ret;
927
928 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900929 ret = release_all_trace_kprobes();
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900930 if (ret < 0)
931 return ret;
932 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400933
934 return seq_open(file, &probes_seq_op);
935}
936
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400937static ssize_t probes_write(struct file *file, const char __user *buffer,
938 size_t count, loff_t *ppos)
939{
Tom Zanussi7e465ba2017-09-22 14:58:20 -0500940 return trace_parse_run_command(file, buffer, count, ppos,
941 create_trace_kprobe);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400942}
943
944static const struct file_operations kprobe_events_ops = {
945 .owner = THIS_MODULE,
946 .open = probes_open,
947 .read = seq_read,
948 .llseek = seq_lseek,
949 .release = seq_release,
950 .write = probes_write,
951};
952
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400953/* Probes profiling interfaces */
954static int probes_profile_seq_show(struct seq_file *m, void *v)
955{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900956 struct trace_kprobe *tk = v;
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400957
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400958 seq_printf(m, " %-44s %15lu %15lu\n",
Marcin Nowakowskif18f97a2016-12-09 15:19:37 +0100959 trace_event_name(&tk->tp.call),
960 trace_kprobe_nhit(tk),
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900961 tk->rp.kp.nmissed);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400962
963 return 0;
964}
965
966static const struct seq_operations profile_seq_op = {
967 .start = probes_seq_start,
968 .next = probes_seq_next,
969 .stop = probes_seq_stop,
970 .show = probes_profile_seq_show
971};
972
973static int profile_open(struct inode *inode, struct file *file)
974{
975 return seq_open(file, &profile_seq_op);
976}
977
978static const struct file_operations kprobe_profile_ops = {
979 .owner = THIS_MODULE,
980 .open = profile_open,
981 .read = seq_read,
982 .llseek = seq_lseek,
983 .release = seq_release,
984};
985
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400986/* Kprobe handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900987static nokprobe_inline void
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900988__kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400989 struct trace_event_file *trace_file)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400990{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400991 struct kprobe_trace_entry_head *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400992 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200993 struct ring_buffer *buffer;
Masami Hiramatsue09c8612010-07-05 15:54:45 -0300994 int size, dsize, pc;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400995 unsigned long irq_flags;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400996 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400997
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400998 WARN_ON(call != trace_file->event_call);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900999
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -04001000 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -05001001 return;
Masami Hiramatsub8820082013-05-09 14:44:54 +09001002
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001003 local_save_flags(irq_flags);
1004 pc = preempt_count();
1005
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001006 dsize = __get_data_size(&tk->tp, regs);
1007 size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001008
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001009 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001010 call->event.type,
1011 size, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001012 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001013 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001014
1015 entry = ring_buffer_event_data(event);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001016 entry->ip = (unsigned long)tk->rp.kp.addr;
1017 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001018
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001019 event_trigger_unlock_commit_regs(trace_file, buffer, event,
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -05001020 entry, irq_flags, pc, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001021}
1022
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001023static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001024kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001025{
Oleg Nesterovb04d52e2013-06-20 19:38:14 +02001026 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001027
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001028 list_for_each_entry_rcu(link, &tk->tp.files, list)
1029 __kprobe_trace_func(tk, regs, link->file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001030}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001031NOKPROBE_SYMBOL(kprobe_trace_func);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001032
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001033/* Kretprobe handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001034static nokprobe_inline void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001035__kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001036 struct pt_regs *regs,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001037 struct trace_event_file *trace_file)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001038{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001039 struct kretprobe_trace_entry_head *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001040 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001041 struct ring_buffer *buffer;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001042 int size, pc, dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001043 unsigned long irq_flags;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001044 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001045
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001046 WARN_ON(call != trace_file->event_call);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001047
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -04001048 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -05001049 return;
Masami Hiramatsub8820082013-05-09 14:44:54 +09001050
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001051 local_save_flags(irq_flags);
1052 pc = preempt_count();
1053
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001054 dsize = __get_data_size(&tk->tp, regs);
1055 size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001056
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001057 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001058 call->event.type,
1059 size, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001060 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001061 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001062
1063 entry = ring_buffer_event_data(event);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001064 entry->func = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001065 entry->ret_ip = (unsigned long)ri->ret_addr;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001066 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001067
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001068 event_trigger_unlock_commit_regs(trace_file, buffer, event,
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -05001069 entry, irq_flags, pc, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001070}
1071
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001072static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001073kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001074 struct pt_regs *regs)
1075{
Oleg Nesterovb04d52e2013-06-20 19:38:14 +02001076 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001077
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001078 list_for_each_entry_rcu(link, &tk->tp.files, list)
1079 __kretprobe_trace_func(tk, ri, regs, link->file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001080}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001081NOKPROBE_SYMBOL(kretprobe_trace_func);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001082
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001083/* Event entry printers */
Masami Hiramatsub62fdd92013-05-13 20:58:39 +09001084static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001085print_kprobe_event(struct trace_iterator *iter, int flags,
1086 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001087{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001088 struct kprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001089 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001090 struct trace_probe *tp;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001091 u8 *data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001092 int i;
1093
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001094 field = (struct kprobe_trace_entry_head *)iter->ent;
Steven Rostedt80decc72010-04-23 10:00:22 -04001095 tp = container_of(event, struct trace_probe, call.event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001096
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001097 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001098
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001099 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001100 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001101
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001102 trace_seq_putc(s, ')');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001103
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001104 data = (u8 *)&field[1];
1105 for (i = 0; i < tp->nr_args; i++)
1106 if (!tp->args[i].type->print(s, tp->args[i].name,
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001107 data + tp->args[i].offset, field))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001108 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001109
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001110 trace_seq_putc(s, '\n');
1111 out:
1112 return trace_handle_return(s);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001113}
1114
Masami Hiramatsub62fdd92013-05-13 20:58:39 +09001115static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001116print_kretprobe_event(struct trace_iterator *iter, int flags,
1117 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001118{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001119 struct kretprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001120 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001121 struct trace_probe *tp;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001122 u8 *data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001123 int i;
1124
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001125 field = (struct kretprobe_trace_entry_head *)iter->ent;
Steven Rostedt80decc72010-04-23 10:00:22 -04001126 tp = container_of(event, struct trace_probe, call.event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001127
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001128 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001129
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001130 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001131 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001132
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001133 trace_seq_puts(s, " <- ");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001134
1135 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001136 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001137
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001138 trace_seq_putc(s, ')');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001139
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001140 data = (u8 *)&field[1];
1141 for (i = 0; i < tp->nr_args; i++)
1142 if (!tp->args[i].type->print(s, tp->args[i].name,
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001143 data + tp->args[i].offset, field))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001144 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001145
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001146 trace_seq_putc(s, '\n');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001147
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001148 out:
1149 return trace_handle_return(s);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001150}
1151
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001152
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001153static int kprobe_event_define_fields(struct trace_event_call *event_call)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001154{
1155 int ret, i;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001156 struct kprobe_trace_entry_head field;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001157 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001158
Masami Hiramatsua703d942009-10-07 18:28:07 -04001159 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001160 /* Set argument names as fields */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001161 for (i = 0; i < tk->tp.nr_args; i++) {
1162 struct probe_arg *parg = &tk->tp.args[i];
1163
1164 ret = trace_define_field(event_call, parg->type->fmttype,
1165 parg->name,
1166 sizeof(field) + parg->offset,
1167 parg->type->size,
1168 parg->type->is_signed,
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001169 FILTER_OTHER);
1170 if (ret)
1171 return ret;
1172 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001173 return 0;
1174}
1175
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001176static int kretprobe_event_define_fields(struct trace_event_call *event_call)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001177{
1178 int ret, i;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001179 struct kretprobe_trace_entry_head field;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001180 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001181
Masami Hiramatsua703d942009-10-07 18:28:07 -04001182 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1183 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001184 /* Set argument names as fields */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001185 for (i = 0; i < tk->tp.nr_args; i++) {
1186 struct probe_arg *parg = &tk->tp.args[i];
1187
1188 ret = trace_define_field(event_call, parg->type->fmttype,
1189 parg->name,
1190 sizeof(field) + parg->offset,
1191 parg->type->size,
1192 parg->type->is_signed,
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001193 FILTER_OTHER);
1194 if (ret)
1195 return ret;
1196 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001197 return 0;
1198}
1199
Li Zefan07b139c2009-12-21 14:27:35 +08001200#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001201
1202/* Kprobe profile handler */
Josef Bacik9802d862017-12-11 11:36:48 -05001203static int
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001204kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001205{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001206 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001207 struct kprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001208 struct hlist_head *head;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001209 int size, __size, dsize;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001210 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001211
Josef Bacik9802d862017-12-11 11:36:48 -05001212 if (bpf_prog_array_valid(call)) {
Masami Hiramatsu66665ad2018-01-13 02:54:33 +09001213 unsigned long orig_ip = instruction_pointer(regs);
Josef Bacik9802d862017-12-11 11:36:48 -05001214 int ret;
1215
1216 ret = trace_call_bpf(call, regs);
1217
1218 /*
1219 * We need to check and see if we modified the pc of the
1220 * pt_regs, and if so clear the kprobe and return 1 so that we
Masami Hiramatsu66665ad2018-01-13 02:54:33 +09001221 * don't do the single stepping.
1222 * The ftrace kprobe handler leaves it up to us to re-enable
1223 * preemption here before returning if we've modified the ip.
Josef Bacik9802d862017-12-11 11:36:48 -05001224 */
Masami Hiramatsu66665ad2018-01-13 02:54:33 +09001225 if (orig_ip != instruction_pointer(regs)) {
Josef Bacik9802d862017-12-11 11:36:48 -05001226 reset_current_kprobe();
Masami Hiramatsu66665ad2018-01-13 02:54:33 +09001227 preempt_enable_no_resched();
Josef Bacik9802d862017-12-11 11:36:48 -05001228 return 1;
1229 }
1230 if (!ret)
1231 return 0;
1232 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07001233
Oleg Nesterov288e9842013-06-20 19:38:06 +02001234 head = this_cpu_ptr(call->perf_events);
1235 if (hlist_empty(head))
Josef Bacik9802d862017-12-11 11:36:48 -05001236 return 0;
Oleg Nesterov288e9842013-06-20 19:38:06 +02001237
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001238 dsize = __get_data_size(&tk->tp, regs);
1239 __size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001240 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1241 size -= sizeof(u32);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001242
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001243 entry = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001244 if (!entry)
Josef Bacik9802d862017-12-11 11:36:48 -05001245 return 0;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001246
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001247 entry->ip = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001248 memset(&entry[1], 0, dsize);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001249 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001250 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02001251 head, NULL);
Josef Bacik9802d862017-12-11 11:36:48 -05001252 return 0;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001253}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001254NOKPROBE_SYMBOL(kprobe_perf_func);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001255
1256/* Kretprobe profile handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001257static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001258kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu2b106aa2013-05-09 14:44:41 +09001259 struct pt_regs *regs)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001260{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001261 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001262 struct kretprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001263 struct hlist_head *head;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001264 int size, __size, dsize;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001265 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001266
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001267 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
Alexei Starovoitov25415172015-03-25 12:49:20 -07001268 return;
1269
Oleg Nesterov288e9842013-06-20 19:38:06 +02001270 head = this_cpu_ptr(call->perf_events);
1271 if (hlist_empty(head))
1272 return;
1273
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001274 dsize = __get_data_size(&tk->tp, regs);
1275 __size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001276 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1277 size -= sizeof(u32);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001278
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001279 entry = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001280 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001281 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001282
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001283 entry->func = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001284 entry->ret_ip = (unsigned long)ri->ret_addr;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001285 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001286 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02001287 head, NULL);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001288}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001289NOKPROBE_SYMBOL(kretprobe_perf_func);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07001290
1291int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1292 const char **symbol, u64 *probe_offset,
1293 u64 *probe_addr, bool perf_type_tracepoint)
1294{
1295 const char *pevent = trace_event_name(event->tp_event);
1296 const char *group = event->tp_event->class->system;
1297 struct trace_kprobe *tk;
1298
1299 if (perf_type_tracepoint)
1300 tk = find_trace_kprobe(pevent, group);
1301 else
1302 tk = event->tp_event->data;
1303 if (!tk)
1304 return -EINVAL;
1305
1306 *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1307 : BPF_FD_TYPE_KPROBE;
1308 if (tk->symbol) {
1309 *symbol = tk->symbol;
1310 *probe_offset = tk->rp.kp.offset;
1311 *probe_addr = 0;
1312 } else {
1313 *symbol = NULL;
1314 *probe_offset = 0;
1315 *probe_addr = (unsigned long)tk->rp.kp.addr;
1316 }
1317 return 0;
1318}
Li Zefan07b139c2009-12-21 14:27:35 +08001319#endif /* CONFIG_PERF_EVENTS */
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001320
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001321/*
1322 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1323 *
1324 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1325 * lockless, but we can't race with this __init function.
1326 */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001327static int kprobe_register(struct trace_event_call *event,
Masami Hiramatsufbc19632014-04-17 17:18:00 +09001328 enum trace_reg type, void *data)
Steven Rostedt22392912010-04-21 12:27:06 -04001329{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001330 struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001331 struct trace_event_file *file = data;
Masami Hiramatsu1538f882011-06-27 16:26:44 +09001332
Steven Rostedt22392912010-04-21 12:27:06 -04001333 switch (type) {
1334 case TRACE_REG_REGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001335 return enable_trace_kprobe(tk, file);
Steven Rostedt22392912010-04-21 12:27:06 -04001336 case TRACE_REG_UNREGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001337 return disable_trace_kprobe(tk, file);
Steven Rostedt22392912010-04-21 12:27:06 -04001338
1339#ifdef CONFIG_PERF_EVENTS
1340 case TRACE_REG_PERF_REGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001341 return enable_trace_kprobe(tk, NULL);
Steven Rostedt22392912010-04-21 12:27:06 -04001342 case TRACE_REG_PERF_UNREGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001343 return disable_trace_kprobe(tk, NULL);
Jiri Olsaceec0b62012-02-15 15:51:49 +01001344 case TRACE_REG_PERF_OPEN:
1345 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +01001346 case TRACE_REG_PERF_ADD:
1347 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +01001348 return 0;
Steven Rostedt22392912010-04-21 12:27:06 -04001349#endif
1350 }
1351 return 0;
1352}
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001353
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001354static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001355{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001356 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
Josef Bacik9802d862017-12-11 11:36:48 -05001357 int ret = 0;
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001358
Martin KaFai Laua7636d92016-02-03 12:28:28 -08001359 raw_cpu_inc(*tk->nhit);
Masami Hiramatsu48182bd2013-05-09 14:44:36 +09001360
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001361 if (tk->tp.flags & TP_FLAG_TRACE)
1362 kprobe_trace_func(tk, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001363#ifdef CONFIG_PERF_EVENTS
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001364 if (tk->tp.flags & TP_FLAG_PROFILE)
Josef Bacik9802d862017-12-11 11:36:48 -05001365 ret = kprobe_perf_func(tk, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001366#endif
Josef Bacik9802d862017-12-11 11:36:48 -05001367 return ret;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001368}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001369NOKPROBE_SYMBOL(kprobe_dispatcher);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001370
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001371static int
1372kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001373{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001374 struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001375
Martin KaFai Laua7636d92016-02-03 12:28:28 -08001376 raw_cpu_inc(*tk->nhit);
Masami Hiramatsu48182bd2013-05-09 14:44:36 +09001377
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001378 if (tk->tp.flags & TP_FLAG_TRACE)
1379 kretprobe_trace_func(tk, ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001380#ifdef CONFIG_PERF_EVENTS
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001381 if (tk->tp.flags & TP_FLAG_PROFILE)
1382 kretprobe_perf_func(tk, ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001383#endif
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001384 return 0; /* We don't tweek kernel, so just return 0 */
1385}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001386NOKPROBE_SYMBOL(kretprobe_dispatcher);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001387
Steven Rostedta9a57762010-04-22 18:46:14 -04001388static struct trace_event_functions kretprobe_funcs = {
1389 .trace = print_kretprobe_event
1390};
1391
1392static struct trace_event_functions kprobe_funcs = {
1393 .trace = print_kprobe_event
1394};
1395
Song Liue12f03d2017-12-06 14:45:15 -08001396static inline void init_trace_event_call(struct trace_kprobe *tk,
1397 struct trace_event_call *call)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001398{
Li Zefanffb9f992010-05-24 16:24:52 +08001399 INIT_LIST_HEAD(&call->class->fields);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001400 if (trace_kprobe_is_return(tk)) {
Steven Rostedt80decc72010-04-23 10:00:22 -04001401 call->event.funcs = &kretprobe_funcs;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001402 call->class->define_fields = kretprobe_event_define_fields;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001403 } else {
Steven Rostedt80decc72010-04-23 10:00:22 -04001404 call->event.funcs = &kprobe_funcs;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001405 call->class->define_fields = kprobe_event_define_fields;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001406 }
Song Liue12f03d2017-12-06 14:45:15 -08001407
1408 call->flags = TRACE_EVENT_FL_KPROBE;
1409 call->class->reg = kprobe_register;
1410 call->data = tk;
1411}
1412
1413static int register_kprobe_event(struct trace_kprobe *tk)
1414{
1415 struct trace_event_call *call = &tk->tp.call;
1416 int ret = 0;
1417
1418 init_trace_event_call(tk, call);
1419
Namhyung Kim5bf652a2013-07-03 16:09:02 +09001420 if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
Lai Jiangshana342a0282009-12-15 15:39:49 +08001421 return -ENOMEM;
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001422 ret = register_trace_event(&call->event);
Steven Rostedt32c0eda2010-04-23 10:38:03 -04001423 if (!ret) {
Lai Jiangshana342a0282009-12-15 15:39:49 +08001424 kfree(call->print_fmt);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001425 return -ENODEV;
Lai Jiangshana342a0282009-12-15 15:39:49 +08001426 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001427 ret = trace_add_event_call(call);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001428 if (ret) {
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001429 pr_info("Failed to register kprobe event: %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001430 trace_event_name(call));
Lai Jiangshana342a0282009-12-15 15:39:49 +08001431 kfree(call->print_fmt);
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001432 unregister_trace_event(&call->event);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001433 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001434 return ret;
1435}
1436
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001437static int unregister_kprobe_event(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001438{
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -04001439 int ret;
1440
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001441 /* tp->event is unregistered in trace_remove_event_call() */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001442 ret = trace_remove_event_call(&tk->tp.call);
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -04001443 if (!ret)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001444 kfree(tk->tp.call.print_fmt);
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -04001445 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001446}
1447
Song Liue12f03d2017-12-06 14:45:15 -08001448#ifdef CONFIG_PERF_EVENTS
1449/* create a trace_kprobe, but don't add it to global lists */
1450struct trace_event_call *
1451create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1452 bool is_return)
1453{
1454 struct trace_kprobe *tk;
1455 int ret;
1456 char *event;
1457
1458 /*
1459 * local trace_kprobes are not added to probe_list, so they are never
1460 * searched in find_trace_kprobe(). Therefore, there is no concern of
1461 * duplicated name here.
1462 */
1463 event = func ? func : "DUMMY_EVENT";
1464
1465 tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1466 offs, 0 /* maxactive */, 0 /* nargs */,
1467 is_return);
1468
1469 if (IS_ERR(tk)) {
1470 pr_info("Failed to allocate trace_probe.(%d)\n",
1471 (int)PTR_ERR(tk));
1472 return ERR_CAST(tk);
1473 }
1474
1475 init_trace_event_call(tk, &tk->tp.call);
1476
1477 if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
1478 ret = -ENOMEM;
1479 goto error;
1480 }
1481
1482 ret = __register_trace_kprobe(tk);
1483 if (ret < 0)
1484 goto error;
1485
1486 return &tk->tp.call;
1487error:
1488 free_trace_kprobe(tk);
1489 return ERR_PTR(ret);
1490}
1491
1492void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1493{
1494 struct trace_kprobe *tk;
1495
1496 tk = container_of(event_call, struct trace_kprobe, tp.call);
1497
1498 if (trace_probe_is_enabled(&tk->tp)) {
1499 WARN_ON(1);
1500 return;
1501 }
1502
1503 __unregister_trace_kprobe(tk);
1504 free_trace_kprobe(tk);
1505}
1506#endif /* CONFIG_PERF_EVENTS */
1507
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001508/* Make a tracefs interface for controlling probe points */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001509static __init int init_kprobe_trace(void)
1510{
1511 struct dentry *d_tracer;
1512 struct dentry *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001513
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001514 if (register_module_notifier(&trace_kprobe_module_nb))
Masami Hiramatsu61424312011-06-27 16:26:56 +09001515 return -EINVAL;
1516
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001517 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05001518 if (IS_ERR(d_tracer))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001519 return 0;
1520
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001521 entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001522 NULL, &kprobe_events_ops);
1523
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001524 /* Event list interface */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001525 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001526 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001527
1528 /* Profile interface */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001529 entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001530 NULL, &kprobe_profile_ops);
1531
1532 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001533 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001534 return 0;
1535}
1536fs_initcall(init_kprobe_trace);
1537
1538
1539#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedt265a5b72011-06-06 22:35:13 -04001540/*
1541 * The "__used" keeps gcc from removing the function symbol
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001542 * from the kallsyms table. 'noinline' makes sure that there
1543 * isn't an inlined version used by the test method below
Steven Rostedt265a5b72011-06-06 22:35:13 -04001544 */
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001545static __used __init noinline int
1546kprobe_trace_selftest_target(int a1, int a2, int a3, int a4, int a5, int a6)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001547{
1548 return a1 + a2 + a3 + a4 + a5 + a6;
1549}
1550
Arnd Bergmann26a346f2017-02-01 17:57:56 +01001551static __init struct trace_event_file *
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001552find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001553{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001554 struct trace_event_file *file;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001555
1556 list_for_each_entry(file, &tr->events, list)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001557 if (file->event_call == &tk->tp.call)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001558 return file;
1559
1560 return NULL;
1561}
1562
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001563/*
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001564 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001565 * stage, we can do this lockless.
1566 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001567static __init int kprobe_trace_self_tests_init(void)
1568{
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001569 int ret, warn = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001570 int (*target)(int, int, int, int, int, int);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001571 struct trace_kprobe *tk;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001572 struct trace_event_file *file;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001573
Yoshihiro YUNOMAE748ec3a2014-06-06 07:35:20 +09001574 if (tracing_is_disabled())
1575 return -ENODEV;
1576
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001577 target = kprobe_trace_selftest_target;
1578
1579 pr_info("Testing kprobe tracing: ");
1580
Tom Zanussi7e465ba2017-09-22 14:58:20 -05001581 ret = trace_run_command("p:testprobe kprobe_trace_selftest_target "
1582 "$stack $stack0 +0($stack)",
1583 create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001584 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001585 pr_warn("error on probing function entry.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001586 warn++;
1587 } else {
1588 /* Enable trace point */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001589 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1590 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001591 pr_warn("error on getting new probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001592 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001593 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001594 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001595 if (WARN_ON_ONCE(file == NULL)) {
1596 pr_warn("error on getting probe file.\n");
1597 warn++;
1598 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001599 enable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001600 }
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001601 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001602
Tom Zanussi7e465ba2017-09-22 14:58:20 -05001603 ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target "
1604 "$retval", create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001605 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001606 pr_warn("error on probing function return.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001607 warn++;
1608 } else {
1609 /* Enable trace point */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001610 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1611 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001612 pr_warn("error on getting 2nd new probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001613 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001614 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001615 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001616 if (WARN_ON_ONCE(file == NULL)) {
1617 pr_warn("error on getting probe file.\n");
1618 warn++;
1619 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001620 enable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001621 }
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001622 }
1623
1624 if (warn)
1625 goto end;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001626
1627 ret = target(1, 2, 3, 4, 5, 6);
1628
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001629 /*
1630 * Not expecting an error here, the check is only to prevent the
1631 * optimizer from removing the call to target() as otherwise there
1632 * are no side-effects and the call is never performed.
1633 */
1634 if (ret != 21)
1635 warn++;
1636
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001637 /* Disable trace points before removing it */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001638 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1639 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001640 pr_warn("error on getting test probe.\n");
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001641 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001642 } else {
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001643 if (trace_kprobe_nhit(tk) != 1) {
1644 pr_warn("incorrect number of testprobe hits\n");
1645 warn++;
1646 }
1647
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001648 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001649 if (WARN_ON_ONCE(file == NULL)) {
1650 pr_warn("error on getting probe file.\n");
1651 warn++;
1652 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001653 disable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001654 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001655
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001656 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1657 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001658 pr_warn("error on getting 2nd test probe.\n");
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001659 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001660 } else {
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01001661 if (trace_kprobe_nhit(tk) != 1) {
1662 pr_warn("incorrect number of testprobe2 hits\n");
1663 warn++;
1664 }
1665
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001666 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001667 if (WARN_ON_ONCE(file == NULL)) {
1668 pr_warn("error on getting probe file.\n");
1669 warn++;
1670 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001671 disable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001672 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001673
Tom Zanussi7e465ba2017-09-22 14:58:20 -05001674 ret = trace_run_command("-:testprobe", create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001675 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001676 pr_warn("error on deleting a probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001677 warn++;
1678 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001679
Tom Zanussi7e465ba2017-09-22 14:58:20 -05001680 ret = trace_run_command("-:testprobe2", create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001681 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001682 pr_warn("error on deleting a probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001683 warn++;
1684 }
1685
1686end:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001687 release_all_trace_kprobes();
Thomas Gleixner30e7d8942017-05-17 10:19:49 +02001688 /*
1689 * Wait for the optimizer work to finish. Otherwise it might fiddle
1690 * with probes in already freed __init text.
1691 */
1692 wait_for_kprobe_optimizer();
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001693 if (warn)
1694 pr_cont("NG: Some tests are failed. Please check them.\n");
1695 else
1696 pr_cont("OK\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001697 return 0;
1698}
1699
1700late_initcall(kprobe_trace_self_tests_init);
1701
1702#endif