blob: 919e0ddd8fcc386de7e6e168f5d2a9911118c331 [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 */
19
20#include <linux/module.h>
21#include <linux/uaccess.h>
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040022
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053023#include "trace_probe.h"
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040024
Masami Hiramatsuf52487e2009-09-10 19:53:53 -040025#define KPROBE_EVENT_SYSTEM "kprobes"
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040026
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040027/**
Masami Hiramatsu77b44d12009-11-03 19:12:47 -050028 * Kprobe event core functions
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040029 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +090030struct trace_kprobe {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040031 struct list_head list;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +020032 struct kretprobe rp; /* Use rp.kp for kprobe use */
Martin KaFai Laua7636d92016-02-03 12:28:28 -080033 unsigned long __percpu *nhit;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040034 const char *symbol; /* symbol name */
Namhyung Kimc31ffb32013-07-03 13:50:51 +090035 struct trace_probe tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040036};
37
Namhyung Kimc31ffb32013-07-03 13:50:51 +090038#define SIZEOF_TRACE_KPROBE(n) \
39 (offsetof(struct trace_kprobe, tp.args) + \
Masami Hiramatsueca0d912009-09-10 19:53:38 -040040 (sizeof(struct probe_arg) * (n)))
Masami Hiramatsua82378d2009-08-13 16:35:18 -040041
Masami Hiramatsu93ccae72010-04-12 13:17:08 -040042
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090043static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040044{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090045 return tk->rp.handler != NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040046}
47
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090048static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040049{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090050 return tk->symbol ? tk->symbol : "unknown";
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040051}
52
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090053static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +090054{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090055 return tk->rp.kp.offset;
Masami Hiramatsu61424312011-06-27 16:26:56 +090056}
57
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090058static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +090059{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090060 return !!(kprobe_gone(&tk->rp.kp));
Masami Hiramatsu61424312011-06-27 16:26:56 +090061}
62
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090063static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
Namhyung Kimc31ffb32013-07-03 13:50:51 +090064 struct module *mod)
Masami Hiramatsu61424312011-06-27 16:26:56 +090065{
66 int len = strlen(mod->name);
Namhyung Kimc31ffb32013-07-03 13:50:51 +090067 const char *name = trace_kprobe_symbol(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +090068 return strncmp(mod->name, name, len) == 0 && name[len] == ':';
69}
70
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090071static nokprobe_inline bool trace_kprobe_is_on_module(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +090072{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090073 return !!strchr(trace_kprobe_symbol(tk), ':');
Masami Hiramatsu61424312011-06-27 16:26:56 +090074}
75
Namhyung Kimc31ffb32013-07-03 13:50:51 +090076static int register_kprobe_event(struct trace_kprobe *tk);
77static int unregister_kprobe_event(struct trace_kprobe *tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040078
79static DEFINE_MUTEX(probe_lock);
80static LIST_HEAD(probe_list);
81
Masami Hiramatsu50d78052009-09-14 16:49:20 -040082static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
83static int kretprobe_dispatcher(struct kretprobe_instance *ri,
84 struct pt_regs *regs);
85
Namhyung Kim1301a442013-11-26 15:21:04 +090086/* Memory fetching by symbol */
87struct symbol_cache {
88 char *symbol;
89 long offset;
90 unsigned long addr;
91};
92
93unsigned long update_symbol_cache(struct symbol_cache *sc)
94{
95 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
96
97 if (sc->addr)
98 sc->addr += sc->offset;
99
100 return sc->addr;
101}
102
103void free_symbol_cache(struct symbol_cache *sc)
104{
105 kfree(sc->symbol);
106 kfree(sc);
107}
108
109struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
110{
111 struct symbol_cache *sc;
112
113 if (!sym || strlen(sym) == 0)
114 return NULL;
115
116 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
117 if (!sc)
118 return NULL;
119
120 sc->symbol = kstrdup(sym, GFP_KERNEL);
121 if (!sc->symbol) {
122 kfree(sc);
123 return NULL;
124 }
125 sc->offset = offset;
126 update_symbol_cache(sc);
127
128 return sc;
129}
130
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900131/*
132 * Kprobes-specific fetch functions
133 */
134#define DEFINE_FETCH_stack(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900135static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900136 void *offset, void *dest) \
137{ \
138 *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
139 (unsigned int)((unsigned long)offset)); \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900140} \
141NOKPROBE_SYMBOL(FETCH_FUNC_NAME(stack, type));
142
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900143DEFINE_BASIC_FETCH_FUNCS(stack)
144/* No string on the stack entry */
145#define fetch_stack_string NULL
146#define fetch_stack_string_size NULL
147
Namhyung Kim5baaa592013-11-26 15:21:04 +0900148#define DEFINE_FETCH_memory(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900149static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
Namhyung Kim5baaa592013-11-26 15:21:04 +0900150 void *addr, void *dest) \
151{ \
152 type retval; \
153 if (probe_kernel_address(addr, retval)) \
154 *(type *)dest = 0; \
155 else \
156 *(type *)dest = retval; \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900157} \
158NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, type));
159
Namhyung Kim5baaa592013-11-26 15:21:04 +0900160DEFINE_BASIC_FETCH_FUNCS(memory)
161/*
162 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
163 * length and relative data location.
164 */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900165static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
166 void *addr, void *dest)
Namhyung Kim5baaa592013-11-26 15:21:04 +0900167{
Namhyung Kim5baaa592013-11-26 15:21:04 +0900168 int maxlen = get_rloc_len(*(u32 *)dest);
169 u8 *dst = get_rloc_data(dest);
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700170 long ret;
Namhyung Kim5baaa592013-11-26 15:21:04 +0900171
172 if (!maxlen)
173 return;
174
175 /*
176 * Try to get string again, since the string can be changed while
177 * probing.
178 */
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700179 ret = strncpy_from_unsafe(dst, addr, maxlen);
Namhyung Kim5baaa592013-11-26 15:21:04 +0900180
181 if (ret < 0) { /* Failed to fetch string */
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700182 dst[0] = '\0';
Namhyung Kim5baaa592013-11-26 15:21:04 +0900183 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
184 } else {
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700185 *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest));
Namhyung Kim5baaa592013-11-26 15:21:04 +0900186 }
187}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900188NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string));
Namhyung Kim5baaa592013-11-26 15:21:04 +0900189
190/* Return the length of string -- including null terminal byte */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900191static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
192 void *addr, void *dest)
Namhyung Kim5baaa592013-11-26 15:21:04 +0900193{
194 mm_segment_t old_fs;
195 int ret, len = 0;
196 u8 c;
197
198 old_fs = get_fs();
199 set_fs(KERNEL_DS);
200 pagefault_disable();
201
202 do {
203 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
204 len++;
205 } while (c && ret == 0 && len < MAX_STRING_SIZE);
206
207 pagefault_enable();
208 set_fs(old_fs);
209
210 if (ret < 0) /* Failed to check the length */
211 *(u32 *)dest = 0;
212 else
213 *(u32 *)dest = len;
214}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900215NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string_size));
Namhyung Kim5baaa592013-11-26 15:21:04 +0900216
Namhyung Kim1301a442013-11-26 15:21:04 +0900217#define DEFINE_FETCH_symbol(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900218void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, void *data, void *dest)\
Namhyung Kim1301a442013-11-26 15:21:04 +0900219{ \
220 struct symbol_cache *sc = data; \
221 if (sc->addr) \
222 fetch_memory_##type(regs, (void *)sc->addr, dest); \
223 else \
224 *(type *)dest = 0; \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900225} \
226NOKPROBE_SYMBOL(FETCH_FUNC_NAME(symbol, type));
227
Namhyung Kim1301a442013-11-26 15:21:04 +0900228DEFINE_BASIC_FETCH_FUNCS(symbol)
229DEFINE_FETCH_symbol(string)
230DEFINE_FETCH_symbol(string_size)
231
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900232/* kprobes don't support file_offset fetch methods */
233#define fetch_file_offset_u8 NULL
234#define fetch_file_offset_u16 NULL
235#define fetch_file_offset_u32 NULL
236#define fetch_file_offset_u64 NULL
237#define fetch_file_offset_string NULL
238#define fetch_file_offset_string_size NULL
239
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900240/* Fetch type information table */
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100241static const struct fetch_type kprobes_fetch_type_table[] = {
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900242 /* Special types */
243 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
244 sizeof(u32), 1, "__data_loc char[]"),
245 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
246 string_size, sizeof(u32), 0, "u32"),
247 /* Basic types */
248 ASSIGN_FETCH_TYPE(u8, u8, 0),
249 ASSIGN_FETCH_TYPE(u16, u16, 0),
250 ASSIGN_FETCH_TYPE(u32, u32, 0),
251 ASSIGN_FETCH_TYPE(u64, u64, 0),
252 ASSIGN_FETCH_TYPE(s8, u8, 1),
253 ASSIGN_FETCH_TYPE(s16, u16, 1),
254 ASSIGN_FETCH_TYPE(s32, u32, 1),
255 ASSIGN_FETCH_TYPE(s64, u64, 1),
256
257 ASSIGN_FETCH_TYPE_END
258};
259
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200260/*
261 * Allocate new trace_probe and initialize it (including kprobes).
262 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900263static struct trace_kprobe *alloc_trace_kprobe(const char *group,
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400264 const char *event,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200265 void *addr,
266 const char *symbol,
267 unsigned long offs,
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530268 int nargs, bool is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400269{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900270 struct trace_kprobe *tk;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500271 int ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400272
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900273 tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
274 if (!tk)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500275 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400276
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800277 tk->nhit = alloc_percpu(unsigned long);
278 if (!tk->nhit)
279 goto error;
280
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400281 if (symbol) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900282 tk->symbol = kstrdup(symbol, GFP_KERNEL);
283 if (!tk->symbol)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400284 goto error;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900285 tk->rp.kp.symbol_name = tk->symbol;
286 tk->rp.kp.offset = offs;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200287 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900288 tk->rp.kp.addr = addr;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200289
290 if (is_return)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900291 tk->rp.handler = kretprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200292 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900293 tk->rp.kp.pre_handler = kprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200294
Masami Hiramatsuda346342010-08-27 20:39:12 +0900295 if (!event || !is_good_name(event)) {
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500296 ret = -EINVAL;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400297 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500298 }
299
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900300 tk->tp.call.class = &tk->tp.class;
301 tk->tp.call.name = kstrdup(event, GFP_KERNEL);
302 if (!tk->tp.call.name)
Masami Hiramatsu42635652009-08-13 16:35:26 -0400303 goto error;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400304
Masami Hiramatsuda346342010-08-27 20:39:12 +0900305 if (!group || !is_good_name(group)) {
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500306 ret = -EINVAL;
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400307 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500308 }
309
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900310 tk->tp.class.system = kstrdup(group, GFP_KERNEL);
311 if (!tk->tp.class.system)
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400312 goto error;
313
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900314 INIT_LIST_HEAD(&tk->list);
315 INIT_LIST_HEAD(&tk->tp.files);
316 return tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400317error:
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900318 kfree(tk->tp.call.name);
319 kfree(tk->symbol);
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800320 free_percpu(tk->nhit);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900321 kfree(tk);
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500322 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400323}
324
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900325static void free_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400326{
327 int i;
328
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900329 for (i = 0; i < tk->tp.nr_args; i++)
330 traceprobe_free_probe_arg(&tk->tp.args[i]);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400331
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900332 kfree(tk->tp.call.class->system);
333 kfree(tk->tp.call.name);
334 kfree(tk->symbol);
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800335 free_percpu(tk->nhit);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900336 kfree(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400337}
338
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900339static struct trace_kprobe *find_trace_kprobe(const char *event,
340 const char *group)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400341{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900342 struct trace_kprobe *tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400343
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900344 list_for_each_entry(tk, &probe_list, list)
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400345 if (strcmp(trace_event_name(&tk->tp.call), event) == 0 &&
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900346 strcmp(tk->tp.call.class->system, group) == 0)
347 return tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400348 return NULL;
349}
350
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200351/*
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900352 * Enable trace_probe
353 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
354 */
355static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400356enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900357{
358 int ret = 0;
359
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900360 if (file) {
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200361 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900362
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200363 link = kmalloc(sizeof(*link), GFP_KERNEL);
364 if (!link) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900365 ret = -ENOMEM;
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200366 goto out;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900367 }
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900368
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200369 link->file = file;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900370 list_add_tail_rcu(&link->list, &tk->tp.files);
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200371
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900372 tk->tp.flags |= TP_FLAG_TRACE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900373 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900374 tk->tp.flags |= TP_FLAG_PROFILE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900375
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900376 if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) {
377 if (trace_kprobe_is_return(tk))
378 ret = enable_kretprobe(&tk->rp);
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900379 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900380 ret = enable_kprobe(&tk->rp.kp);
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900381 }
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200382 out:
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900383 return ret;
384}
385
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900386/*
387 * Disable trace_probe
388 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
389 */
390static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400391disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900392{
Masami Hiramatsua232e272013-07-09 18:35:26 +0900393 struct event_file_link *link = NULL;
394 int wait = 0;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900395 int ret = 0;
396
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900397 if (file) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900398 link = find_event_file_link(&tk->tp, file);
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200399 if (!link) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900400 ret = -EINVAL;
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200401 goto out;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900402 }
403
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200404 list_del_rcu(&link->list);
Masami Hiramatsua232e272013-07-09 18:35:26 +0900405 wait = 1;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900406 if (!list_empty(&tk->tp.files))
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200407 goto out;
408
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900409 tk->tp.flags &= ~TP_FLAG_TRACE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900410 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900411 tk->tp.flags &= ~TP_FLAG_PROFILE;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900412
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900413 if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
414 if (trace_kprobe_is_return(tk))
415 disable_kretprobe(&tk->rp);
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900416 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900417 disable_kprobe(&tk->rp.kp);
Masami Hiramatsua232e272013-07-09 18:35:26 +0900418 wait = 1;
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900419 }
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200420 out:
Masami Hiramatsua232e272013-07-09 18:35:26 +0900421 if (wait) {
422 /*
423 * Synchronize with kprobe_trace_func/kretprobe_trace_func
424 * to ensure disabled (all running handlers are finished).
425 * This is not only for kfree(), but also the caller,
426 * trace_remove_event_call() supposes it for releasing
427 * event_call related objects, which will be accessed in
428 * the kprobe_trace_func/kretprobe_trace_func.
429 */
430 synchronize_sched();
431 kfree(link); /* Ignored if link == NULL */
432 }
433
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900434 return ret;
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900435}
436
Masami Hiramatsu61424312011-06-27 16:26:56 +0900437/* Internal register function - just handle k*probes and flags */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900438static int __register_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900439{
Masami Hiramatsu7f6878a2011-06-27 16:27:03 +0900440 int i, ret;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900441
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900442 if (trace_probe_is_registered(&tk->tp))
Masami Hiramatsu61424312011-06-27 16:26:56 +0900443 return -EINVAL;
444
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900445 for (i = 0; i < tk->tp.nr_args; i++)
446 traceprobe_update_arg(&tk->tp.args[i]);
Masami Hiramatsu7f6878a2011-06-27 16:27:03 +0900447
Masami Hiramatsu61424312011-06-27 16:26:56 +0900448 /* Set/clear disabled flag according to tp->flag */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900449 if (trace_probe_is_enabled(&tk->tp))
450 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900451 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900452 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900453
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900454 if (trace_kprobe_is_return(tk))
455 ret = register_kretprobe(&tk->rp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900456 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900457 ret = register_kprobe(&tk->rp.kp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900458
459 if (ret == 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900460 tk->tp.flags |= TP_FLAG_REGISTERED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900461 else {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700462 pr_warn("Could not insert probe at %s+%lu: %d\n",
463 trace_kprobe_symbol(tk), trace_kprobe_offset(tk), ret);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900464 if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700465 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
Masami Hiramatsu61424312011-06-27 16:26:56 +0900466 ret = 0;
467 } else if (ret == -EILSEQ) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700468 pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
469 tk->rp.kp.addr);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900470 ret = -EINVAL;
471 }
472 }
473
474 return ret;
475}
476
477/* Internal unregister function - just handle k*probes and flags */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900478static void __unregister_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900479{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900480 if (trace_probe_is_registered(&tk->tp)) {
481 if (trace_kprobe_is_return(tk))
482 unregister_kretprobe(&tk->rp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900483 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900484 unregister_kprobe(&tk->rp.kp);
485 tk->tp.flags &= ~TP_FLAG_REGISTERED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900486 /* Cleanup kprobe for reuse */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900487 if (tk->rp.kp.symbol_name)
488 tk->rp.kp.addr = NULL;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900489 }
490}
491
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400492/* Unregister a trace_probe and probe_event: call with locking probe_lock */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900493static int unregister_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400494{
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900495 /* Enabled event can not be unregistered */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900496 if (trace_probe_is_enabled(&tk->tp))
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900497 return -EBUSY;
498
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400499 /* Will fail if probe is being used by ftrace or perf */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900500 if (unregister_kprobe_event(tk))
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400501 return -EBUSY;
502
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900503 __unregister_trace_kprobe(tk);
504 list_del(&tk->list);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900505
506 return 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400507}
508
509/* Register a trace_probe and probe_event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900510static int register_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400511{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900512 struct trace_kprobe *old_tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400513 int ret;
514
515 mutex_lock(&probe_lock);
516
Masami Hiramatsu61424312011-06-27 16:26:56 +0900517 /* Delete old (same name) event if exist */
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400518 old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400519 tk->tp.call.class->system);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900520 if (old_tk) {
521 ret = unregister_trace_kprobe(old_tk);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900522 if (ret < 0)
523 goto end;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900524 free_trace_kprobe(old_tk);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400525 }
Masami Hiramatsu61424312011-06-27 16:26:56 +0900526
527 /* Register new event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900528 ret = register_kprobe_event(tk);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400529 if (ret) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700530 pr_warn("Failed to register probe event(%d)\n", ret);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400531 goto end;
532 }
533
Masami Hiramatsu61424312011-06-27 16:26:56 +0900534 /* Register k*probe */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900535 ret = __register_trace_kprobe(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900536 if (ret < 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900537 unregister_kprobe_event(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900538 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900539 list_add_tail(&tk->list, &probe_list);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900540
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400541end:
542 mutex_unlock(&probe_lock);
543 return ret;
544}
545
Masami Hiramatsu61424312011-06-27 16:26:56 +0900546/* Module notifier call back, checking event on the module */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900547static int trace_kprobe_module_callback(struct notifier_block *nb,
Masami Hiramatsu61424312011-06-27 16:26:56 +0900548 unsigned long val, void *data)
549{
550 struct module *mod = data;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900551 struct trace_kprobe *tk;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900552 int ret;
553
554 if (val != MODULE_STATE_COMING)
555 return NOTIFY_DONE;
556
557 /* Update probes on coming module */
558 mutex_lock(&probe_lock);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900559 list_for_each_entry(tk, &probe_list, list) {
560 if (trace_kprobe_within_module(tk, mod)) {
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900561 /* Don't need to check busy - this should have gone. */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900562 __unregister_trace_kprobe(tk);
563 ret = __register_trace_kprobe(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900564 if (ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700565 pr_warn("Failed to re-register probe %s on %s: %d\n",
566 trace_event_name(&tk->tp.call),
567 mod->name, ret);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900568 }
569 }
570 mutex_unlock(&probe_lock);
571
572 return NOTIFY_DONE;
573}
574
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900575static struct notifier_block trace_kprobe_module_nb = {
576 .notifier_call = trace_kprobe_module_callback,
Masami Hiramatsu61424312011-06-27 16:26:56 +0900577 .priority = 1 /* Invoked after kprobe module callback */
578};
579
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900580static int create_trace_kprobe(int argc, char **argv)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400581{
582 /*
583 * Argument syntax:
Masami Hiramatsu61424312011-06-27 16:26:56 +0900584 * - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
585 * - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400586 * Fetch args:
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400587 * $retval : fetch return value
588 * $stack : fetch stack address
589 * $stackN : fetch Nth of stack (N:0-)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400590 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
591 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
592 * %REG : fetch register REG
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400593 * Dereferencing memory fetch:
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400594 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400595 * Alias name of args:
596 * NAME=FETCHARG : set NAME as alias of FETCHARG.
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400597 * Type of args:
598 * FETCHARG:TYPE : use TYPE instead of unsigned long.
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400599 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900600 struct trace_kprobe *tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400601 int i, ret = 0;
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530602 bool is_return = false, is_delete = false;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400603 char *symbol = NULL, *event = NULL, *group = NULL;
Masami Hiramatsuda346342010-08-27 20:39:12 +0900604 char *arg;
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400605 unsigned long offset = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400606 void *addr = NULL;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200607 char buf[MAX_EVENT_NAME_LEN];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400608
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500609 /* argc must be >= 1 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400610 if (argv[0][0] == 'p')
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530611 is_return = false;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400612 else if (argv[0][0] == 'r')
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530613 is_return = true;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500614 else if (argv[0][0] == '-')
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530615 is_delete = true;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400616 else {
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500617 pr_info("Probe definition must be started with 'p', 'r' or"
618 " '-'.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400619 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400620 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400621
622 if (argv[0][1] == ':') {
623 event = &argv[0][2];
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400624 if (strchr(event, '/')) {
625 group = event;
626 event = strchr(group, '/') + 1;
627 event[-1] = '\0';
628 if (strlen(group) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800629 pr_info("Group name is not specified\n");
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400630 return -EINVAL;
631 }
632 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400633 if (strlen(event) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800634 pr_info("Event name is not specified\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400635 return -EINVAL;
636 }
637 }
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500638 if (!group)
639 group = KPROBE_EVENT_SYSTEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400640
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500641 if (is_delete) {
642 if (!event) {
643 pr_info("Delete command needs an event name.\n");
644 return -EINVAL;
645 }
Srikar Dronamraju9da79ab2010-06-30 14:15:48 +0530646 mutex_lock(&probe_lock);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900647 tk = find_trace_kprobe(event, group);
648 if (!tk) {
Srikar Dronamraju9da79ab2010-06-30 14:15:48 +0530649 mutex_unlock(&probe_lock);
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500650 pr_info("Event %s/%s doesn't exist.\n", group, event);
651 return -ENOENT;
652 }
653 /* delete an event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900654 ret = unregister_trace_kprobe(tk);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900655 if (ret == 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900656 free_trace_kprobe(tk);
Srikar Dronamraju9da79ab2010-06-30 14:15:48 +0530657 mutex_unlock(&probe_lock);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900658 return ret;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500659 }
660
661 if (argc < 2) {
662 pr_info("Probe point is not specified.\n");
663 return -EINVAL;
664 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400665 if (isdigit(argv[1][0])) {
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400666 if (is_return) {
667 pr_info("Return probe point must be a symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400668 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400669 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400670 /* an address specified */
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200671 ret = kstrtoul(&argv[1][0], 0, (unsigned long *)&addr);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400672 if (ret) {
673 pr_info("Failed to parse address.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400674 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400675 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400676 } else {
677 /* a symbol specified */
678 symbol = argv[1];
679 /* TODO: support .init module functions */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530680 ret = traceprobe_split_symbol_offset(symbol, &offset);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400681 if (ret) {
682 pr_info("Failed to parse symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400683 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400684 }
685 if (offset && is_return) {
686 pr_info("Return probe must be used without offset.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400687 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400688 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400689 }
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400690 argc -= 2; argv += 2;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400691
692 /* setup a probe */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400693 if (!event) {
694 /* Make a new event name */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400695 if (symbol)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500696 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400697 is_return ? 'r' : 'p', symbol, offset);
698 else
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500699 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400700 is_return ? 'r' : 'p', addr);
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200701 event = buf;
702 }
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900703 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, argc,
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400704 is_return);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900705 if (IS_ERR(tk)) {
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400706 pr_info("Failed to allocate trace_probe.(%d)\n",
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900707 (int)PTR_ERR(tk));
708 return PTR_ERR(tk);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400709 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400710
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400711 /* parse arguments */
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400712 ret = 0;
713 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900714 struct probe_arg *parg = &tk->tp.args[i];
715
Masami Hiramatsu61a52732010-08-27 20:38:46 +0900716 /* Increment count for freeing args in error case */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900717 tk->tp.nr_args++;
Masami Hiramatsu61a52732010-08-27 20:38:46 +0900718
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400719 /* Parse argument name */
720 arg = strchr(argv[i], '=');
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900721 if (arg) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400722 *arg++ = '\0';
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900723 parg->name = kstrdup(argv[i], GFP_KERNEL);
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900724 } else {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400725 arg = argv[i];
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900726 /* If argument name is omitted, set "argN" */
727 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900728 parg->name = kstrdup(buf, GFP_KERNEL);
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900729 }
Masami Hiramatsua703d942009-10-07 18:28:07 -0400730
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900731 if (!parg->name) {
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900732 pr_info("Failed to allocate argument[%d] name.\n", i);
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500733 ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400734 goto error;
735 }
Masami Hiramatsuda346342010-08-27 20:39:12 +0900736
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900737 if (!is_good_name(parg->name)) {
Masami Hiramatsuda346342010-08-27 20:39:12 +0900738 pr_info("Invalid argument[%d] name: %s\n",
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900739 i, parg->name);
Masami Hiramatsuda346342010-08-27 20:39:12 +0900740 ret = -EINVAL;
741 goto error;
742 }
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400743
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900744 if (traceprobe_conflict_field_name(parg->name,
745 tk->tp.args, i)) {
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900746 pr_info("Argument[%d] name '%s' conflicts with "
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400747 "another field.\n", i, argv[i]);
748 ret = -EINVAL;
749 goto error;
750 }
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500751
752 /* Parse fetch argument */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900753 ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100754 is_return, true,
755 kprobes_fetch_type_table);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400756 if (ret) {
Masami Hiramatsuaba91592010-08-27 20:39:06 +0900757 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400758 goto error;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400759 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400760 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400761
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900762 ret = register_trace_kprobe(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400763 if (ret)
764 goto error;
765 return 0;
766
767error:
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900768 free_trace_kprobe(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400769 return ret;
770}
771
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900772static int release_all_trace_kprobes(void)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400773{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900774 struct trace_kprobe *tk;
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900775 int ret = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400776
777 mutex_lock(&probe_lock);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900778 /* Ensure no probe is in use. */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900779 list_for_each_entry(tk, &probe_list, list)
780 if (trace_probe_is_enabled(&tk->tp)) {
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900781 ret = -EBUSY;
782 goto end;
783 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400784 /* TODO: Use batch unregistration */
785 while (!list_empty(&probe_list)) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900786 tk = list_entry(probe_list.next, struct trace_kprobe, list);
787 ret = unregister_trace_kprobe(tk);
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400788 if (ret)
789 goto end;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900790 free_trace_kprobe(tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400791 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900792
793end:
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400794 mutex_unlock(&probe_lock);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900795
796 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400797}
798
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400799/* Probes listing interfaces */
800static void *probes_seq_start(struct seq_file *m, loff_t *pos)
801{
802 mutex_lock(&probe_lock);
803 return seq_list_start(&probe_list, *pos);
804}
805
806static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
807{
808 return seq_list_next(v, &probe_list, pos);
809}
810
811static void probes_seq_stop(struct seq_file *m, void *v)
812{
813 mutex_unlock(&probe_lock);
814}
815
816static int probes_seq_show(struct seq_file *m, void *v)
817{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900818 struct trace_kprobe *tk = v;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400819 int i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400820
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100821 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400822 seq_printf(m, ":%s/%s", tk->tp.call.class->system,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400823 trace_event_name(&tk->tp.call));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400824
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900825 if (!tk->symbol)
826 seq_printf(m, " 0x%p", tk->rp.kp.addr);
827 else if (tk->rp.kp.offset)
828 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
829 tk->rp.kp.offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400830 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900831 seq_printf(m, " %s", trace_kprobe_symbol(tk));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400832
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900833 for (i = 0; i < tk->tp.nr_args; i++)
834 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100835 seq_putc(m, '\n');
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400836
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400837 return 0;
838}
839
840static const struct seq_operations probes_seq_op = {
841 .start = probes_seq_start,
842 .next = probes_seq_next,
843 .stop = probes_seq_stop,
844 .show = probes_seq_show
845};
846
847static int probes_open(struct inode *inode, struct file *file)
848{
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900849 int ret;
850
851 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900852 ret = release_all_trace_kprobes();
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900853 if (ret < 0)
854 return ret;
855 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400856
857 return seq_open(file, &probes_seq_op);
858}
859
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400860static ssize_t probes_write(struct file *file, const char __user *buffer,
861 size_t count, loff_t *ppos)
862{
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530863 return traceprobe_probes_write(file, buffer, count, ppos,
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900864 create_trace_kprobe);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400865}
866
867static const struct file_operations kprobe_events_ops = {
868 .owner = THIS_MODULE,
869 .open = probes_open,
870 .read = seq_read,
871 .llseek = seq_lseek,
872 .release = seq_release,
873 .write = probes_write,
874};
875
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400876/* Probes profiling interfaces */
877static int probes_profile_seq_show(struct seq_file *m, void *v)
878{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900879 struct trace_kprobe *tk = v;
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800880 unsigned long nhit = 0;
881 int cpu;
882
883 for_each_possible_cpu(cpu)
884 nhit += *per_cpu_ptr(tk->nhit, cpu);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400885
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400886 seq_printf(m, " %-44s %15lu %15lu\n",
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800887 trace_event_name(&tk->tp.call), nhit,
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900888 tk->rp.kp.nmissed);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400889
890 return 0;
891}
892
893static const struct seq_operations profile_seq_op = {
894 .start = probes_seq_start,
895 .next = probes_seq_next,
896 .stop = probes_seq_stop,
897 .show = probes_profile_seq_show
898};
899
900static int profile_open(struct inode *inode, struct file *file)
901{
902 return seq_open(file, &profile_seq_op);
903}
904
905static const struct file_operations kprobe_profile_ops = {
906 .owner = THIS_MODULE,
907 .open = profile_open,
908 .read = seq_read,
909 .llseek = seq_lseek,
910 .release = seq_release,
911};
912
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400913/* Kprobe handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900914static nokprobe_inline void
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900915__kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400916 struct trace_event_file *trace_file)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400917{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400918 struct kprobe_trace_entry_head *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400919 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200920 struct ring_buffer *buffer;
Masami Hiramatsue09c8612010-07-05 15:54:45 -0300921 int size, dsize, pc;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400922 unsigned long irq_flags;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400923 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400924
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400925 WARN_ON(call != trace_file->event_call);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900926
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -0400927 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500928 return;
Masami Hiramatsub8820082013-05-09 14:44:54 +0900929
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400930 local_save_flags(irq_flags);
931 pc = preempt_count();
932
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900933 dsize = __get_data_size(&tk->tp, regs);
934 size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400935
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400936 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900937 call->event.type,
938 size, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400939 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +0800940 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400941
942 entry = ring_buffer_event_data(event);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900943 entry->ip = (unsigned long)tk->rp.kp.addr;
944 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400945
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400946 event_trigger_unlock_commit_regs(trace_file, buffer, event,
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500947 entry, irq_flags, pc, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400948}
949
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900950static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900951kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900952{
Oleg Nesterovb04d52e2013-06-20 19:38:14 +0200953 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900954
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900955 list_for_each_entry_rcu(link, &tk->tp.files, list)
956 __kprobe_trace_func(tk, regs, link->file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900957}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900958NOKPROBE_SYMBOL(kprobe_trace_func);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900959
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400960/* Kretprobe handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900961static nokprobe_inline void
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900962__kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900963 struct pt_regs *regs,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400964 struct trace_event_file *trace_file)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400965{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400966 struct kretprobe_trace_entry_head *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400967 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200968 struct ring_buffer *buffer;
Masami Hiramatsue09c8612010-07-05 15:54:45 -0300969 int size, pc, dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400970 unsigned long irq_flags;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400971 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400972
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400973 WARN_ON(call != trace_file->event_call);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900974
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -0400975 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500976 return;
Masami Hiramatsub8820082013-05-09 14:44:54 +0900977
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400978 local_save_flags(irq_flags);
979 pc = preempt_count();
980
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900981 dsize = __get_data_size(&tk->tp, regs);
982 size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400983
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400984 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900985 call->event.type,
986 size, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400987 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +0800988 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400989
990 entry = ring_buffer_event_data(event);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900991 entry->func = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400992 entry->ret_ip = (unsigned long)ri->ret_addr;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900993 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400994
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400995 event_trigger_unlock_commit_regs(trace_file, buffer, event,
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500996 entry, irq_flags, pc, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400997}
998
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900999static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001000kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001001 struct pt_regs *regs)
1002{
Oleg Nesterovb04d52e2013-06-20 19:38:14 +02001003 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001004
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001005 list_for_each_entry_rcu(link, &tk->tp.files, list)
1006 __kretprobe_trace_func(tk, ri, regs, link->file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001007}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001008NOKPROBE_SYMBOL(kretprobe_trace_func);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001009
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001010/* Event entry printers */
Masami Hiramatsub62fdd92013-05-13 20:58:39 +09001011static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001012print_kprobe_event(struct trace_iterator *iter, int flags,
1013 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001014{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001015 struct kprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001016 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001017 struct trace_probe *tp;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001018 u8 *data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001019 int i;
1020
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001021 field = (struct kprobe_trace_entry_head *)iter->ent;
Steven Rostedt80decc72010-04-23 10:00:22 -04001022 tp = container_of(event, struct trace_probe, call.event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001023
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001024 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001025
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001026 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001027 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001028
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001029 trace_seq_putc(s, ')');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001030
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001031 data = (u8 *)&field[1];
1032 for (i = 0; i < tp->nr_args; i++)
1033 if (!tp->args[i].type->print(s, tp->args[i].name,
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001034 data + tp->args[i].offset, field))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001035 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001036
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001037 trace_seq_putc(s, '\n');
1038 out:
1039 return trace_handle_return(s);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001040}
1041
Masami Hiramatsub62fdd92013-05-13 20:58:39 +09001042static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001043print_kretprobe_event(struct trace_iterator *iter, int flags,
1044 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001045{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001046 struct kretprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001047 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001048 struct trace_probe *tp;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001049 u8 *data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001050 int i;
1051
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001052 field = (struct kretprobe_trace_entry_head *)iter->ent;
Steven Rostedt80decc72010-04-23 10:00:22 -04001053 tp = container_of(event, struct trace_probe, call.event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001054
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001055 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001056
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001057 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001058 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001059
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001060 trace_seq_puts(s, " <- ");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001061
1062 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001063 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001064
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001065 trace_seq_putc(s, ')');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001066
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001067 data = (u8 *)&field[1];
1068 for (i = 0; i < tp->nr_args; i++)
1069 if (!tp->args[i].type->print(s, tp->args[i].name,
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001070 data + tp->args[i].offset, field))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001071 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001072
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001073 trace_seq_putc(s, '\n');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001074
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001075 out:
1076 return trace_handle_return(s);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001077}
1078
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001079
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001080static int kprobe_event_define_fields(struct trace_event_call *event_call)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001081{
1082 int ret, i;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001083 struct kprobe_trace_entry_head field;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001084 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001085
Masami Hiramatsua703d942009-10-07 18:28:07 -04001086 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001087 /* Set argument names as fields */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001088 for (i = 0; i < tk->tp.nr_args; i++) {
1089 struct probe_arg *parg = &tk->tp.args[i];
1090
1091 ret = trace_define_field(event_call, parg->type->fmttype,
1092 parg->name,
1093 sizeof(field) + parg->offset,
1094 parg->type->size,
1095 parg->type->is_signed,
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001096 FILTER_OTHER);
1097 if (ret)
1098 return ret;
1099 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001100 return 0;
1101}
1102
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001103static int kretprobe_event_define_fields(struct trace_event_call *event_call)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001104{
1105 int ret, i;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001106 struct kretprobe_trace_entry_head field;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001107 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001108
Masami Hiramatsua703d942009-10-07 18:28:07 -04001109 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1110 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001111 /* Set argument names as fields */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001112 for (i = 0; i < tk->tp.nr_args; i++) {
1113 struct probe_arg *parg = &tk->tp.args[i];
1114
1115 ret = trace_define_field(event_call, parg->type->fmttype,
1116 parg->name,
1117 sizeof(field) + parg->offset,
1118 parg->type->size,
1119 parg->type->is_signed,
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001120 FILTER_OTHER);
1121 if (ret)
1122 return ret;
1123 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001124 return 0;
1125}
1126
Li Zefan07b139c2009-12-21 14:27:35 +08001127#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001128
1129/* Kprobe profile handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001130static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001131kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001132{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001133 struct trace_event_call *call = &tk->tp.call;
Alexei Starovoitov25415172015-03-25 12:49:20 -07001134 struct bpf_prog *prog = call->prog;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001135 struct kprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001136 struct hlist_head *head;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001137 int size, __size, dsize;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001138 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001139
Alexei Starovoitov25415172015-03-25 12:49:20 -07001140 if (prog && !trace_call_bpf(prog, regs))
1141 return;
1142
Oleg Nesterov288e9842013-06-20 19:38:06 +02001143 head = this_cpu_ptr(call->perf_events);
1144 if (hlist_empty(head))
1145 return;
1146
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001147 dsize = __get_data_size(&tk->tp, regs);
1148 __size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001149 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1150 size -= sizeof(u32);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001151
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01001152 entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001153 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001154 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001155
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001156 entry->ip = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001157 memset(&entry[1], 0, dsize);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001158 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Oleg Nesterovcf6735a2013-06-20 19:38:11 +02001159 perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001160}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001161NOKPROBE_SYMBOL(kprobe_perf_func);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001162
1163/* Kretprobe profile handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001164static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001165kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu2b106aa2013-05-09 14:44:41 +09001166 struct pt_regs *regs)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001167{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001168 struct trace_event_call *call = &tk->tp.call;
Alexei Starovoitov25415172015-03-25 12:49:20 -07001169 struct bpf_prog *prog = call->prog;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001170 struct kretprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001171 struct hlist_head *head;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001172 int size, __size, dsize;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001173 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001174
Alexei Starovoitov25415172015-03-25 12:49:20 -07001175 if (prog && !trace_call_bpf(prog, regs))
1176 return;
1177
Oleg Nesterov288e9842013-06-20 19:38:06 +02001178 head = this_cpu_ptr(call->perf_events);
1179 if (hlist_empty(head))
1180 return;
1181
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001182 dsize = __get_data_size(&tk->tp, regs);
1183 __size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001184 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1185 size -= sizeof(u32);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001186
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01001187 entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001188 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001189 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001190
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001191 entry->func = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001192 entry->ret_ip = (unsigned long)ri->ret_addr;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001193 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
Oleg Nesterovcf6735a2013-06-20 19:38:11 +02001194 perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001195}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001196NOKPROBE_SYMBOL(kretprobe_perf_func);
Li Zefan07b139c2009-12-21 14:27:35 +08001197#endif /* CONFIG_PERF_EVENTS */
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001198
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001199/*
1200 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1201 *
1202 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1203 * lockless, but we can't race with this __init function.
1204 */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001205static int kprobe_register(struct trace_event_call *event,
Masami Hiramatsufbc19632014-04-17 17:18:00 +09001206 enum trace_reg type, void *data)
Steven Rostedt22392912010-04-21 12:27:06 -04001207{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001208 struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001209 struct trace_event_file *file = data;
Masami Hiramatsu1538f882011-06-27 16:26:44 +09001210
Steven Rostedt22392912010-04-21 12:27:06 -04001211 switch (type) {
1212 case TRACE_REG_REGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001213 return enable_trace_kprobe(tk, file);
Steven Rostedt22392912010-04-21 12:27:06 -04001214 case TRACE_REG_UNREGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001215 return disable_trace_kprobe(tk, file);
Steven Rostedt22392912010-04-21 12:27:06 -04001216
1217#ifdef CONFIG_PERF_EVENTS
1218 case TRACE_REG_PERF_REGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001219 return enable_trace_kprobe(tk, NULL);
Steven Rostedt22392912010-04-21 12:27:06 -04001220 case TRACE_REG_PERF_UNREGISTER:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001221 return disable_trace_kprobe(tk, NULL);
Jiri Olsaceec0b62012-02-15 15:51:49 +01001222 case TRACE_REG_PERF_OPEN:
1223 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +01001224 case TRACE_REG_PERF_ADD:
1225 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +01001226 return 0;
Steven Rostedt22392912010-04-21 12:27:06 -04001227#endif
1228 }
1229 return 0;
1230}
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001231
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001232static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001233{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001234 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001235
Martin KaFai Laua7636d92016-02-03 12:28:28 -08001236 raw_cpu_inc(*tk->nhit);
Masami Hiramatsu48182bd2013-05-09 14:44:36 +09001237
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001238 if (tk->tp.flags & TP_FLAG_TRACE)
1239 kprobe_trace_func(tk, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001240#ifdef CONFIG_PERF_EVENTS
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001241 if (tk->tp.flags & TP_FLAG_PROFILE)
1242 kprobe_perf_func(tk, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001243#endif
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001244 return 0; /* We don't tweek kernel, so just return 0 */
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001245}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001246NOKPROBE_SYMBOL(kprobe_dispatcher);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001247
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001248static int
1249kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001250{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001251 struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001252
Martin KaFai Laua7636d92016-02-03 12:28:28 -08001253 raw_cpu_inc(*tk->nhit);
Masami Hiramatsu48182bd2013-05-09 14:44:36 +09001254
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001255 if (tk->tp.flags & TP_FLAG_TRACE)
1256 kretprobe_trace_func(tk, ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001257#ifdef CONFIG_PERF_EVENTS
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001258 if (tk->tp.flags & TP_FLAG_PROFILE)
1259 kretprobe_perf_func(tk, ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001260#endif
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001261 return 0; /* We don't tweek kernel, so just return 0 */
1262}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001263NOKPROBE_SYMBOL(kretprobe_dispatcher);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001264
Steven Rostedta9a57762010-04-22 18:46:14 -04001265static struct trace_event_functions kretprobe_funcs = {
1266 .trace = print_kretprobe_event
1267};
1268
1269static struct trace_event_functions kprobe_funcs = {
1270 .trace = print_kprobe_event
1271};
1272
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001273static int register_kprobe_event(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001274{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001275 struct trace_event_call *call = &tk->tp.call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001276 int ret;
1277
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001278 /* Initialize trace_event_call */
Li Zefanffb9f992010-05-24 16:24:52 +08001279 INIT_LIST_HEAD(&call->class->fields);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001280 if (trace_kprobe_is_return(tk)) {
Steven Rostedt80decc72010-04-23 10:00:22 -04001281 call->event.funcs = &kretprobe_funcs;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001282 call->class->define_fields = kretprobe_event_define_fields;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001283 } else {
Steven Rostedt80decc72010-04-23 10:00:22 -04001284 call->event.funcs = &kprobe_funcs;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001285 call->class->define_fields = kprobe_event_define_fields;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001286 }
Namhyung Kim5bf652a2013-07-03 16:09:02 +09001287 if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
Lai Jiangshana342a0282009-12-15 15:39:49 +08001288 return -ENOMEM;
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001289 ret = register_trace_event(&call->event);
Steven Rostedt32c0eda2010-04-23 10:38:03 -04001290 if (!ret) {
Lai Jiangshana342a0282009-12-15 15:39:49 +08001291 kfree(call->print_fmt);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001292 return -ENODEV;
Lai Jiangshana342a0282009-12-15 15:39:49 +08001293 }
Alexei Starovoitov72cbbc82015-03-25 12:49:19 -07001294 call->flags = TRACE_EVENT_FL_KPROBE;
Steven Rostedt22392912010-04-21 12:27:06 -04001295 call->class->reg = kprobe_register;
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001296 call->data = tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001297 ret = trace_add_event_call(call);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001298 if (ret) {
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001299 pr_info("Failed to register kprobe event: %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001300 trace_event_name(call));
Lai Jiangshana342a0282009-12-15 15:39:49 +08001301 kfree(call->print_fmt);
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001302 unregister_trace_event(&call->event);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001303 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001304 return ret;
1305}
1306
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001307static int unregister_kprobe_event(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001308{
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -04001309 int ret;
1310
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001311 /* tp->event is unregistered in trace_remove_event_call() */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001312 ret = trace_remove_event_call(&tk->tp.call);
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -04001313 if (!ret)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001314 kfree(tk->tp.call.print_fmt);
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -04001315 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001316}
1317
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001318/* Make a tracefs interface for controlling probe points */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001319static __init int init_kprobe_trace(void)
1320{
1321 struct dentry *d_tracer;
1322 struct dentry *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001323
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001324 if (register_module_notifier(&trace_kprobe_module_nb))
Masami Hiramatsu61424312011-06-27 16:26:56 +09001325 return -EINVAL;
1326
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001327 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05001328 if (IS_ERR(d_tracer))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001329 return 0;
1330
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001331 entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001332 NULL, &kprobe_events_ops);
1333
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001334 /* Event list interface */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001335 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001336 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001337
1338 /* Profile interface */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001339 entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001340 NULL, &kprobe_profile_ops);
1341
1342 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001343 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001344 return 0;
1345}
1346fs_initcall(init_kprobe_trace);
1347
1348
1349#ifdef CONFIG_FTRACE_STARTUP_TEST
1350
Steven Rostedt265a5b72011-06-06 22:35:13 -04001351/*
1352 * The "__used" keeps gcc from removing the function symbol
1353 * from the kallsyms table.
1354 */
1355static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
1356 int a4, int a5, int a6)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001357{
1358 return a1 + a2 + a3 + a4 + a5 + a6;
1359}
1360
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001361static struct trace_event_file *
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001362find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001363{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001364 struct trace_event_file *file;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001365
1366 list_for_each_entry(file, &tr->events, list)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001367 if (file->event_call == &tk->tp.call)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001368 return file;
1369
1370 return NULL;
1371}
1372
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001373/*
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001374 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001375 * stage, we can do this lockless.
1376 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001377static __init int kprobe_trace_self_tests_init(void)
1378{
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001379 int ret, warn = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001380 int (*target)(int, int, int, int, int, int);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001381 struct trace_kprobe *tk;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001382 struct trace_event_file *file;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001383
Yoshihiro YUNOMAE748ec3a2014-06-06 07:35:20 +09001384 if (tracing_is_disabled())
1385 return -ENODEV;
1386
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001387 target = kprobe_trace_selftest_target;
1388
1389 pr_info("Testing kprobe tracing: ");
1390
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +05301391 ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
1392 "$stack $stack0 +0($stack)",
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001393 create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001394 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001395 pr_warn("error on probing function entry.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001396 warn++;
1397 } else {
1398 /* Enable trace point */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001399 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1400 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001401 pr_warn("error on getting new probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001402 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001403 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001404 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001405 if (WARN_ON_ONCE(file == NULL)) {
1406 pr_warn("error on getting probe file.\n");
1407 warn++;
1408 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001409 enable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001410 }
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001411 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001412
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +05301413 ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001414 "$retval", create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001415 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001416 pr_warn("error on probing function return.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001417 warn++;
1418 } else {
1419 /* Enable trace point */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001420 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1421 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001422 pr_warn("error on getting 2nd new probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001423 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001424 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001425 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001426 if (WARN_ON_ONCE(file == NULL)) {
1427 pr_warn("error on getting probe file.\n");
1428 warn++;
1429 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001430 enable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001431 }
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001432 }
1433
1434 if (warn)
1435 goto end;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001436
1437 ret = target(1, 2, 3, 4, 5, 6);
1438
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001439 /* Disable trace points before removing it */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001440 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1441 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001442 pr_warn("error on getting test probe.\n");
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001443 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001444 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001445 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001446 if (WARN_ON_ONCE(file == NULL)) {
1447 pr_warn("error on getting probe file.\n");
1448 warn++;
1449 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001450 disable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001451 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001452
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001453 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1454 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001455 pr_warn("error on getting 2nd test probe.\n");
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001456 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001457 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001458 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001459 if (WARN_ON_ONCE(file == NULL)) {
1460 pr_warn("error on getting probe file.\n");
1461 warn++;
1462 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001463 disable_trace_kprobe(tk, file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001464 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001465
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001466 ret = traceprobe_command("-:testprobe", create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001467 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001468 pr_warn("error on deleting a probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001469 warn++;
1470 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001471
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001472 ret = traceprobe_command("-:testprobe2", create_trace_kprobe);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001473 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001474 pr_warn("error on deleting a probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001475 warn++;
1476 }
1477
1478end:
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001479 release_all_trace_kprobes();
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001480 if (warn)
1481 pr_cont("NG: Some tests are failed. Please check them.\n");
1482 else
1483 pr_cont("OK\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001484 return 0;
1485}
1486
1487late_initcall(kprobe_trace_self_tests_init);
1488
1489#endif