blob: bebd2f5d9ea3d2d63fea802fd26ee47d6ce2f201 [file] [log] [blame]
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301/*
2 * uprobes-based tracing events
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Copyright (C) IBM Corporation, 2010-2012
18 * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
19 */
20
21#include <linux/module.h>
22#include <linux/uaccess.h>
23#include <linux/uprobes.h>
24#include <linux/namei.h>
Andy Shevchenkob2e902f2012-12-17 16:01:27 -080025#include <linux/string.h>
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053026
27#include "trace_probe.h"
28
29#define UPROBE_EVENT_SYSTEM "uprobes"
30
Oleg Nesterov457d1772013-03-29 18:26:51 +010031struct uprobe_trace_entry_head {
32 struct trace_entry ent;
33 unsigned long vaddr[];
34};
35
36#define SIZEOF_TRACE_ENTRY(is_return) \
37 (sizeof(struct uprobe_trace_entry_head) + \
38 sizeof(unsigned long) * (is_return ? 2 : 1))
39
40#define DATAOF_TRACE_ENTRY(entry, is_return) \
41 ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
42
Oleg Nesterov736288b2013-02-03 20:58:35 +010043struct trace_uprobe_filter {
44 rwlock_t rwlock;
45 int nr_systemwide;
46 struct list_head perf_events;
47};
48
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053049/*
50 * uprobe event core functions
51 */
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053052struct trace_uprobe {
53 struct list_head list;
Oleg Nesterov736288b2013-02-03 20:58:35 +010054 struct trace_uprobe_filter filter;
Oleg Nesterova932b732013-01-31 19:47:23 +010055 struct uprobe_consumer consumer;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053056 struct inode *inode;
57 char *filename;
58 unsigned long offset;
59 unsigned long nhit;
Namhyung Kim14577c32013-07-03 15:42:53 +090060 struct trace_probe tp;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053061};
62
Namhyung Kim14577c32013-07-03 15:42:53 +090063#define SIZEOF_TRACE_UPROBE(n) \
64 (offsetof(struct trace_uprobe, tp.args) + \
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053065 (sizeof(struct probe_arg) * (n)))
66
67static int register_uprobe_event(struct trace_uprobe *tu);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -040068static int unregister_uprobe_event(struct trace_uprobe *tu);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053069
70static DEFINE_MUTEX(uprobe_lock);
71static LIST_HEAD(uprobe_list);
72
73static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +010074static int uretprobe_dispatcher(struct uprobe_consumer *con,
75 unsigned long func, struct pt_regs *regs);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053076
Namhyung Kim3fd996a2013-11-26 15:21:04 +090077#ifdef CONFIG_STACK_GROWSUP
78static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
79{
80 return addr - (n * sizeof(long));
81}
82#else
83static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
84{
85 return addr + (n * sizeof(long));
86}
87#endif
88
89static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
90{
91 unsigned long ret;
92 unsigned long addr = user_stack_pointer(regs);
93
94 addr = adjust_stack_addr(addr, n);
95
96 if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
97 return 0;
98
99 return ret;
100}
101
102/*
103 * Uprobes-specific fetch functions
104 */
105#define DEFINE_FETCH_stack(type) \
106static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
107 void *offset, void *dest) \
108{ \
109 *(type *)dest = (type)get_user_stack_nth(regs, \
110 ((unsigned long)offset)); \
111}
112DEFINE_BASIC_FETCH_FUNCS(stack)
113/* No string on the stack entry */
114#define fetch_stack_string NULL
115#define fetch_stack_string_size NULL
116
Namhyung Kim5baaa592013-11-26 15:21:04 +0900117#define DEFINE_FETCH_memory(type) \
118static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\
119 void *addr, void *dest) \
120{ \
121 type retval; \
122 void __user *vaddr = (void __force __user *) addr; \
123 \
124 if (copy_from_user(&retval, vaddr, sizeof(type))) \
125 *(type *)dest = 0; \
126 else \
127 *(type *) dest = retval; \
128}
129DEFINE_BASIC_FETCH_FUNCS(memory)
130/*
131 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
132 * length and relative data location.
133 */
134static __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
135 void *addr, void *dest)
136{
137 long ret;
138 u32 rloc = *(u32 *)dest;
139 int maxlen = get_rloc_len(rloc);
140 u8 *dst = get_rloc_data(dest);
141 void __user *src = (void __force __user *) addr;
142
143 if (!maxlen)
144 return;
145
146 ret = strncpy_from_user(dst, src, maxlen);
147
148 if (ret < 0) { /* Failed to fetch string */
149 ((u8 *)get_rloc_data(dest))[0] = '\0';
150 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(rloc));
151 } else {
152 *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(rloc));
153 }
154}
155
156static __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
157 void *addr, void *dest)
158{
159 int len;
160 void __user *vaddr = (void __force __user *) addr;
161
162 len = strnlen_user(vaddr, MAX_STRING_SIZE);
163
164 if (len == 0 || len > MAX_STRING_SIZE) /* Failed to check length */
165 *(u32 *)dest = 0;
166 else
167 *(u32 *)dest = len;
168}
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900169
Namhyung Kim1301a442013-11-26 15:21:04 +0900170/* uprobes do not support symbol fetch methods */
171#define fetch_symbol_u8 NULL
172#define fetch_symbol_u16 NULL
173#define fetch_symbol_u32 NULL
174#define fetch_symbol_u64 NULL
175#define fetch_symbol_string NULL
176#define fetch_symbol_string_size NULL
177
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900178/* Fetch type information table */
179const struct fetch_type uprobes_fetch_type_table[] = {
180 /* Special types */
181 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
182 sizeof(u32), 1, "__data_loc char[]"),
183 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
184 string_size, sizeof(u32), 0, "u32"),
185 /* Basic types */
186 ASSIGN_FETCH_TYPE(u8, u8, 0),
187 ASSIGN_FETCH_TYPE(u16, u16, 0),
188 ASSIGN_FETCH_TYPE(u32, u32, 0),
189 ASSIGN_FETCH_TYPE(u64, u64, 0),
190 ASSIGN_FETCH_TYPE(s8, u8, 1),
191 ASSIGN_FETCH_TYPE(s16, u16, 1),
192 ASSIGN_FETCH_TYPE(s32, u32, 1),
193 ASSIGN_FETCH_TYPE(s64, u64, 1),
194
195 ASSIGN_FETCH_TYPE_END
196};
197
Oleg Nesterov736288b2013-02-03 20:58:35 +0100198static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
199{
200 rwlock_init(&filter->rwlock);
201 filter->nr_systemwide = 0;
202 INIT_LIST_HEAD(&filter->perf_events);
203}
204
205static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
206{
207 return !filter->nr_systemwide && list_empty(&filter->perf_events);
208}
209
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100210static inline bool is_ret_probe(struct trace_uprobe *tu)
211{
212 return tu->consumer.ret_handler != NULL;
213}
214
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530215/*
216 * Allocate new trace_uprobe and initialize it (including uprobes).
217 */
218static struct trace_uprobe *
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100219alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530220{
221 struct trace_uprobe *tu;
222
223 if (!event || !is_good_name(event))
224 return ERR_PTR(-EINVAL);
225
226 if (!group || !is_good_name(group))
227 return ERR_PTR(-EINVAL);
228
229 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
230 if (!tu)
231 return ERR_PTR(-ENOMEM);
232
Namhyung Kim14577c32013-07-03 15:42:53 +0900233 tu->tp.call.class = &tu->tp.class;
234 tu->tp.call.name = kstrdup(event, GFP_KERNEL);
235 if (!tu->tp.call.name)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530236 goto error;
237
Namhyung Kim14577c32013-07-03 15:42:53 +0900238 tu->tp.class.system = kstrdup(group, GFP_KERNEL);
239 if (!tu->tp.class.system)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530240 goto error;
241
242 INIT_LIST_HEAD(&tu->list);
Oleg Nesterova932b732013-01-31 19:47:23 +0100243 tu->consumer.handler = uprobe_dispatcher;
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100244 if (is_ret)
245 tu->consumer.ret_handler = uretprobe_dispatcher;
Oleg Nesterov736288b2013-02-03 20:58:35 +0100246 init_trace_uprobe_filter(&tu->filter);
Namhyung Kim14577c32013-07-03 15:42:53 +0900247 tu->tp.call.flags |= TRACE_EVENT_FL_USE_CALL_FILTER;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530248 return tu;
249
250error:
Namhyung Kim14577c32013-07-03 15:42:53 +0900251 kfree(tu->tp.call.name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530252 kfree(tu);
253
254 return ERR_PTR(-ENOMEM);
255}
256
257static void free_trace_uprobe(struct trace_uprobe *tu)
258{
259 int i;
260
Namhyung Kim14577c32013-07-03 15:42:53 +0900261 for (i = 0; i < tu->tp.nr_args; i++)
262 traceprobe_free_probe_arg(&tu->tp.args[i]);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530263
264 iput(tu->inode);
Namhyung Kim14577c32013-07-03 15:42:53 +0900265 kfree(tu->tp.call.class->system);
266 kfree(tu->tp.call.name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530267 kfree(tu->filename);
268 kfree(tu);
269}
270
271static struct trace_uprobe *find_probe_event(const char *event, const char *group)
272{
273 struct trace_uprobe *tu;
274
275 list_for_each_entry(tu, &uprobe_list, list)
Namhyung Kim14577c32013-07-03 15:42:53 +0900276 if (strcmp(tu->tp.call.name, event) == 0 &&
277 strcmp(tu->tp.call.class->system, group) == 0)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530278 return tu;
279
280 return NULL;
281}
282
283/* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400284static int unregister_trace_uprobe(struct trace_uprobe *tu)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530285{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400286 int ret;
287
288 ret = unregister_uprobe_event(tu);
289 if (ret)
290 return ret;
291
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530292 list_del(&tu->list);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530293 free_trace_uprobe(tu);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400294 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530295}
296
297/* Register a trace_uprobe and probe_event */
298static int register_trace_uprobe(struct trace_uprobe *tu)
299{
Namhyung Kim14577c32013-07-03 15:42:53 +0900300 struct trace_uprobe *old_tu;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530301 int ret;
302
303 mutex_lock(&uprobe_lock);
304
305 /* register as an event */
Namhyung Kim14577c32013-07-03 15:42:53 +0900306 old_tu = find_probe_event(tu->tp.call.name, tu->tp.call.class->system);
307 if (old_tu) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530308 /* delete old event */
Namhyung Kim14577c32013-07-03 15:42:53 +0900309 ret = unregister_trace_uprobe(old_tu);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400310 if (ret)
311 goto end;
312 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530313
314 ret = register_uprobe_event(tu);
315 if (ret) {
316 pr_warning("Failed to register probe event(%d)\n", ret);
317 goto end;
318 }
319
320 list_add_tail(&tu->list, &uprobe_list);
321
322end:
323 mutex_unlock(&uprobe_lock);
324
325 return ret;
326}
327
328/*
329 * Argument syntax:
Namhyung Kim306cfe22013-07-03 16:44:46 +0900330 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530331 *
332 * - Remove uprobe: -:[GRP/]EVENT
333 */
334static int create_trace_uprobe(int argc, char **argv)
335{
336 struct trace_uprobe *tu;
337 struct inode *inode;
338 char *arg, *event, *group, *filename;
339 char buf[MAX_EVENT_NAME_LEN];
340 struct path path;
341 unsigned long offset;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100342 bool is_delete, is_return;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530343 int i, ret;
344
345 inode = NULL;
346 ret = 0;
347 is_delete = false;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100348 is_return = false;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530349 event = NULL;
350 group = NULL;
351
352 /* argc must be >= 1 */
353 if (argv[0][0] == '-')
354 is_delete = true;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100355 else if (argv[0][0] == 'r')
356 is_return = true;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530357 else if (argv[0][0] != 'p') {
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100358 pr_info("Probe definition must be started with 'p', 'r' or '-'.\n");
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530359 return -EINVAL;
360 }
361
362 if (argv[0][1] == ':') {
363 event = &argv[0][2];
364 arg = strchr(event, '/');
365
366 if (arg) {
367 group = event;
368 event = arg + 1;
369 event[-1] = '\0';
370
371 if (strlen(group) == 0) {
372 pr_info("Group name is not specified\n");
373 return -EINVAL;
374 }
375 }
376 if (strlen(event) == 0) {
377 pr_info("Event name is not specified\n");
378 return -EINVAL;
379 }
380 }
381 if (!group)
382 group = UPROBE_EVENT_SYSTEM;
383
384 if (is_delete) {
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400385 int ret;
386
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530387 if (!event) {
388 pr_info("Delete command needs an event name.\n");
389 return -EINVAL;
390 }
391 mutex_lock(&uprobe_lock);
392 tu = find_probe_event(event, group);
393
394 if (!tu) {
395 mutex_unlock(&uprobe_lock);
396 pr_info("Event %s/%s doesn't exist.\n", group, event);
397 return -ENOENT;
398 }
399 /* delete an event */
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400400 ret = unregister_trace_uprobe(tu);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530401 mutex_unlock(&uprobe_lock);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400402 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530403 }
404
405 if (argc < 2) {
406 pr_info("Probe point is not specified.\n");
407 return -EINVAL;
408 }
409 if (isdigit(argv[1][0])) {
410 pr_info("probe point must be have a filename.\n");
411 return -EINVAL;
412 }
413 arg = strchr(argv[1], ':');
zhangwei(Jovi)fa440632013-06-13 14:21:51 +0800414 if (!arg) {
415 ret = -EINVAL;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530416 goto fail_address_parse;
zhangwei(Jovi)fa440632013-06-13 14:21:51 +0800417 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530418
419 *arg++ = '\0';
420 filename = argv[1];
421 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
422 if (ret)
423 goto fail_address_parse;
424
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530425 inode = igrab(path.dentry->d_inode);
Oleg Nesterov84d7ed72013-01-27 18:20:45 +0100426 path_put(&path);
427
Oleg Nesterov7e4e28c2013-01-28 17:08:47 +0100428 if (!inode || !S_ISREG(inode->i_mode)) {
Jovi Zhangd24d7db2012-07-18 18:16:44 +0800429 ret = -EINVAL;
430 goto fail_address_parse;
431 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530432
Oleg Nesterov84d7ed72013-01-27 18:20:45 +0100433 ret = kstrtoul(arg, 0, &offset);
434 if (ret)
435 goto fail_address_parse;
436
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530437 argc -= 2;
438 argv += 2;
439
440 /* setup a probe */
441 if (!event) {
Andy Shevchenkob2e902f2012-12-17 16:01:27 -0800442 char *tail;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530443 char *ptr;
444
Andy Shevchenkob2e902f2012-12-17 16:01:27 -0800445 tail = kstrdup(kbasename(filename), GFP_KERNEL);
446 if (!tail) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530447 ret = -ENOMEM;
448 goto fail_address_parse;
449 }
450
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530451 ptr = strpbrk(tail, ".-_");
452 if (ptr)
453 *ptr = '\0';
454
455 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
456 event = buf;
457 kfree(tail);
458 }
459
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100460 tu = alloc_trace_uprobe(group, event, argc, is_return);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530461 if (IS_ERR(tu)) {
462 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
463 ret = PTR_ERR(tu);
464 goto fail_address_parse;
465 }
466 tu->offset = offset;
467 tu->inode = inode;
468 tu->filename = kstrdup(filename, GFP_KERNEL);
469
470 if (!tu->filename) {
471 pr_info("Failed to allocate filename.\n");
472 ret = -ENOMEM;
473 goto error;
474 }
475
476 /* parse arguments */
477 ret = 0;
478 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Namhyung Kim14577c32013-07-03 15:42:53 +0900479 struct probe_arg *parg = &tu->tp.args[i];
480
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530481 /* Increment count for freeing args in error case */
Namhyung Kim14577c32013-07-03 15:42:53 +0900482 tu->tp.nr_args++;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530483
484 /* Parse argument name */
485 arg = strchr(argv[i], '=');
486 if (arg) {
487 *arg++ = '\0';
Namhyung Kim14577c32013-07-03 15:42:53 +0900488 parg->name = kstrdup(argv[i], GFP_KERNEL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530489 } else {
490 arg = argv[i];
491 /* If argument name is omitted, set "argN" */
492 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
Namhyung Kim14577c32013-07-03 15:42:53 +0900493 parg->name = kstrdup(buf, GFP_KERNEL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530494 }
495
Namhyung Kim14577c32013-07-03 15:42:53 +0900496 if (!parg->name) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530497 pr_info("Failed to allocate argument[%d] name.\n", i);
498 ret = -ENOMEM;
499 goto error;
500 }
501
Namhyung Kim14577c32013-07-03 15:42:53 +0900502 if (!is_good_name(parg->name)) {
503 pr_info("Invalid argument[%d] name: %s\n", i, parg->name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530504 ret = -EINVAL;
505 goto error;
506 }
507
Namhyung Kim14577c32013-07-03 15:42:53 +0900508 if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530509 pr_info("Argument[%d] name '%s' conflicts with "
510 "another field.\n", i, argv[i]);
511 ret = -EINVAL;
512 goto error;
513 }
514
515 /* Parse fetch argument */
Namhyung Kim14577c32013-07-03 15:42:53 +0900516 ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg,
517 false, false);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530518 if (ret) {
519 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
520 goto error;
521 }
522 }
523
524 ret = register_trace_uprobe(tu);
525 if (ret)
526 goto error;
527 return 0;
528
529error:
530 free_trace_uprobe(tu);
531 return ret;
532
533fail_address_parse:
534 if (inode)
535 iput(inode);
536
Jovi Zhangd24d7db2012-07-18 18:16:44 +0800537 pr_info("Failed to parse address or file.\n");
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530538
539 return ret;
540}
541
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400542static int cleanup_all_probes(void)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530543{
544 struct trace_uprobe *tu;
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400545 int ret = 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530546
547 mutex_lock(&uprobe_lock);
548 while (!list_empty(&uprobe_list)) {
549 tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400550 ret = unregister_trace_uprobe(tu);
551 if (ret)
552 break;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530553 }
554 mutex_unlock(&uprobe_lock);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400555 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530556}
557
558/* Probes listing interfaces */
559static void *probes_seq_start(struct seq_file *m, loff_t *pos)
560{
561 mutex_lock(&uprobe_lock);
562 return seq_list_start(&uprobe_list, *pos);
563}
564
565static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
566{
567 return seq_list_next(v, &uprobe_list, pos);
568}
569
570static void probes_seq_stop(struct seq_file *m, void *v)
571{
572 mutex_unlock(&uprobe_lock);
573}
574
575static int probes_seq_show(struct seq_file *m, void *v)
576{
577 struct trace_uprobe *tu = v;
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100578 char c = is_ret_probe(tu) ? 'r' : 'p';
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530579 int i;
580
Namhyung Kim14577c32013-07-03 15:42:53 +0900581 seq_printf(m, "%c:%s/%s", c, tu->tp.call.class->system, tu->tp.call.name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530582 seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset);
583
Namhyung Kim14577c32013-07-03 15:42:53 +0900584 for (i = 0; i < tu->tp.nr_args; i++)
585 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530586
587 seq_printf(m, "\n");
588 return 0;
589}
590
591static const struct seq_operations probes_seq_op = {
592 .start = probes_seq_start,
593 .next = probes_seq_next,
594 .stop = probes_seq_stop,
595 .show = probes_seq_show
596};
597
598static int probes_open(struct inode *inode, struct file *file)
599{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400600 int ret;
601
602 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
603 ret = cleanup_all_probes();
604 if (ret)
605 return ret;
606 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530607
608 return seq_open(file, &probes_seq_op);
609}
610
611static ssize_t probes_write(struct file *file, const char __user *buffer,
612 size_t count, loff_t *ppos)
613{
614 return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
615}
616
617static const struct file_operations uprobe_events_ops = {
618 .owner = THIS_MODULE,
619 .open = probes_open,
620 .read = seq_read,
621 .llseek = seq_lseek,
622 .release = seq_release,
623 .write = probes_write,
624};
625
626/* Probes profiling interfaces */
627static int probes_profile_seq_show(struct seq_file *m, void *v)
628{
629 struct trace_uprobe *tu = v;
630
Namhyung Kim14577c32013-07-03 15:42:53 +0900631 seq_printf(m, " %s %-44s %15lu\n", tu->filename, tu->tp.call.name, tu->nhit);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530632 return 0;
633}
634
635static const struct seq_operations profile_seq_op = {
636 .start = probes_seq_start,
637 .next = probes_seq_next,
638 .stop = probes_seq_stop,
639 .show = probes_profile_seq_show
640};
641
642static int profile_open(struct inode *inode, struct file *file)
643{
644 return seq_open(file, &profile_seq_op);
645}
646
647static const struct file_operations uprobe_profile_ops = {
648 .owner = THIS_MODULE,
649 .open = profile_open,
650 .read = seq_read,
651 .llseek = seq_lseek,
652 .release = seq_release,
653};
654
Oleg Nesterova51cc602013-03-30 18:02:12 +0100655static void uprobe_trace_print(struct trace_uprobe *tu,
656 unsigned long func, struct pt_regs *regs)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530657{
658 struct uprobe_trace_entry_head *entry;
659 struct ring_buffer_event *event;
660 struct ring_buffer *buffer;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100661 void *data;
Oleg Nesterov0e3853d2013-03-28 19:19:11 +0100662 int size, i;
Namhyung Kim14577c32013-07-03 15:42:53 +0900663 struct ftrace_event_call *call = &tu->tp.call;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530664
Oleg Nesterov393a7362013-03-30 18:46:22 +0100665 size = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530666 event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
Namhyung Kim14577c32013-07-03 15:42:53 +0900667 size + tu->tp.size, 0, 0);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530668 if (!event)
Oleg Nesterova51cc602013-03-30 18:02:12 +0100669 return;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530670
671 entry = ring_buffer_event_data(event);
Oleg Nesterov393a7362013-03-30 18:46:22 +0100672 if (is_ret_probe(tu)) {
673 entry->vaddr[0] = func;
674 entry->vaddr[1] = instruction_pointer(regs);
675 data = DATAOF_TRACE_ENTRY(entry, true);
676 } else {
677 entry->vaddr[0] = instruction_pointer(regs);
678 data = DATAOF_TRACE_ENTRY(entry, false);
679 }
680
Namhyung Kim14577c32013-07-03 15:42:53 +0900681 for (i = 0; i < tu->tp.nr_args; i++) {
682 call_fetch(&tu->tp.args[i].fetch, regs,
683 data + tu->tp.args[i].offset);
684 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530685
Tom Zanussif306cc82013-10-24 08:34:17 -0500686 if (!call_filter_check_discard(call, entry, buffer, event))
Oleg Nesterov0e3853d2013-03-28 19:19:11 +0100687 trace_buffer_unlock_commit(buffer, event, 0, 0);
Oleg Nesterova51cc602013-03-30 18:02:12 +0100688}
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100689
Oleg Nesterova51cc602013-03-30 18:02:12 +0100690/* uprobe handler */
691static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
692{
Oleg Nesterov393a7362013-03-30 18:46:22 +0100693 if (!is_ret_probe(tu))
694 uprobe_trace_print(tu, 0, regs);
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100695 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530696}
697
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100698static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
699 struct pt_regs *regs)
700{
701 uprobe_trace_print(tu, func, regs);
702}
703
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530704/* Event entry printers */
705static enum print_line_t
706print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
707{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100708 struct uprobe_trace_entry_head *entry;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530709 struct trace_seq *s = &iter->seq;
710 struct trace_uprobe *tu;
711 u8 *data;
712 int i;
713
Oleg Nesterov457d1772013-03-29 18:26:51 +0100714 entry = (struct uprobe_trace_entry_head *)iter->ent;
Namhyung Kim14577c32013-07-03 15:42:53 +0900715 tu = container_of(event, struct trace_uprobe, tp.call.event);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530716
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100717 if (is_ret_probe(tu)) {
Namhyung Kim14577c32013-07-03 15:42:53 +0900718 if (!trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", tu->tp.call.name,
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100719 entry->vaddr[1], entry->vaddr[0]))
720 goto partial;
721 data = DATAOF_TRACE_ENTRY(entry, true);
722 } else {
Namhyung Kim14577c32013-07-03 15:42:53 +0900723 if (!trace_seq_printf(s, "%s: (0x%lx)", tu->tp.call.name,
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100724 entry->vaddr[0]))
725 goto partial;
726 data = DATAOF_TRACE_ENTRY(entry, false);
727 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530728
Namhyung Kim14577c32013-07-03 15:42:53 +0900729 for (i = 0; i < tu->tp.nr_args; i++) {
730 struct probe_arg *parg = &tu->tp.args[i];
731
732 if (!parg->type->print(s, parg->name, data + parg->offset, entry))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530733 goto partial;
734 }
735
736 if (trace_seq_puts(s, "\n"))
737 return TRACE_TYPE_HANDLED;
738
739partial:
740 return TRACE_TYPE_PARTIAL_LINE;
741}
742
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100743typedef bool (*filter_func_t)(struct uprobe_consumer *self,
744 enum uprobe_filter_ctx ctx,
745 struct mm_struct *mm);
746
747static int
748probe_event_enable(struct trace_uprobe *tu, int flag, filter_func_t filter)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530749{
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530750 int ret = 0;
751
Namhyung Kim14577c32013-07-03 15:42:53 +0900752 if (trace_probe_is_enabled(&tu->tp))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530753 return -EINTR;
754
Oleg Nesterov736288b2013-02-03 20:58:35 +0100755 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
756
Namhyung Kim14577c32013-07-03 15:42:53 +0900757 tu->tp.flags |= flag;
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100758 tu->consumer.filter = filter;
Oleg Nesterova932b732013-01-31 19:47:23 +0100759 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
760 if (ret)
Namhyung Kim14577c32013-07-03 15:42:53 +0900761 tu->tp.flags &= ~flag;
Oleg Nesterov41618242013-01-27 18:36:24 +0100762
763 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530764}
765
766static void probe_event_disable(struct trace_uprobe *tu, int flag)
767{
Namhyung Kim14577c32013-07-03 15:42:53 +0900768 if (!trace_probe_is_enabled(&tu->tp))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530769 return;
770
Oleg Nesterov736288b2013-02-03 20:58:35 +0100771 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
772
Oleg Nesterova932b732013-01-31 19:47:23 +0100773 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
Namhyung Kim14577c32013-07-03 15:42:53 +0900774 tu->tp.flags &= ~flag;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530775}
776
777static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
778{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100779 int ret, i, size;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530780 struct uprobe_trace_entry_head field;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100781 struct trace_uprobe *tu = event_call->data;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530782
Oleg Nesterov4d1298e2013-03-30 19:23:15 +0100783 if (is_ret_probe(tu)) {
784 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
785 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
786 size = SIZEOF_TRACE_ENTRY(true);
787 } else {
788 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
789 size = SIZEOF_TRACE_ENTRY(false);
790 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530791 /* Set argument names as fields */
Namhyung Kim14577c32013-07-03 15:42:53 +0900792 for (i = 0; i < tu->tp.nr_args; i++) {
793 struct probe_arg *parg = &tu->tp.args[i];
794
795 ret = trace_define_field(event_call, parg->type->fmttype,
796 parg->name, size + parg->offset,
797 parg->type->size, parg->type->is_signed,
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530798 FILTER_OTHER);
799
800 if (ret)
801 return ret;
802 }
803 return 0;
804}
805
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530806#ifdef CONFIG_PERF_EVENTS
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100807static bool
808__uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
809{
810 struct perf_event *event;
811
812 if (filter->nr_systemwide)
813 return true;
814
815 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
816 if (event->hw.tp_target->mm == mm)
817 return true;
818 }
819
820 return false;
821}
822
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100823static inline bool
824uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
825{
826 return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm);
827}
828
Oleg Nesterov736288b2013-02-03 20:58:35 +0100829static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
830{
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100831 bool done;
832
Oleg Nesterov736288b2013-02-03 20:58:35 +0100833 write_lock(&tu->filter.rwlock);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100834 if (event->hw.tp_target) {
835 /*
836 * event->parent != NULL means copy_process(), we can avoid
837 * uprobe_apply(). current->mm must be probed and we can rely
838 * on dup_mmap() which preserves the already installed bp's.
839 *
840 * attr.enable_on_exec means that exec/mmap will install the
841 * breakpoints we need.
842 */
843 done = tu->filter.nr_systemwide ||
844 event->parent || event->attr.enable_on_exec ||
845 uprobe_filter_event(tu, event);
Oleg Nesterov736288b2013-02-03 20:58:35 +0100846 list_add(&event->hw.tp_list, &tu->filter.perf_events);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100847 } else {
848 done = tu->filter.nr_systemwide;
Oleg Nesterov736288b2013-02-03 20:58:35 +0100849 tu->filter.nr_systemwide++;
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100850 }
Oleg Nesterov736288b2013-02-03 20:58:35 +0100851 write_unlock(&tu->filter.rwlock);
852
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100853 if (!done)
854 uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100855
Oleg Nesterov736288b2013-02-03 20:58:35 +0100856 return 0;
857}
858
859static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
860{
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100861 bool done;
862
Oleg Nesterov736288b2013-02-03 20:58:35 +0100863 write_lock(&tu->filter.rwlock);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100864 if (event->hw.tp_target) {
Oleg Nesterov736288b2013-02-03 20:58:35 +0100865 list_del(&event->hw.tp_list);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100866 done = tu->filter.nr_systemwide ||
867 (event->hw.tp_target->flags & PF_EXITING) ||
868 uprobe_filter_event(tu, event);
869 } else {
Oleg Nesterov736288b2013-02-03 20:58:35 +0100870 tu->filter.nr_systemwide--;
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100871 done = tu->filter.nr_systemwide;
872 }
Oleg Nesterov736288b2013-02-03 20:58:35 +0100873 write_unlock(&tu->filter.rwlock);
874
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100875 if (!done)
876 uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100877
Oleg Nesterov736288b2013-02-03 20:58:35 +0100878 return 0;
879}
880
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100881static bool uprobe_perf_filter(struct uprobe_consumer *uc,
882 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
883{
884 struct trace_uprobe *tu;
885 int ret;
886
887 tu = container_of(uc, struct trace_uprobe, consumer);
888 read_lock(&tu->filter.rwlock);
889 ret = __uprobe_perf_filter(&tu->filter, mm);
890 read_unlock(&tu->filter.rwlock);
891
892 return ret;
893}
894
Oleg Nesterova51cc602013-03-30 18:02:12 +0100895static void uprobe_perf_print(struct trace_uprobe *tu,
896 unsigned long func, struct pt_regs *regs)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530897{
Namhyung Kim14577c32013-07-03 15:42:53 +0900898 struct ftrace_event_call *call = &tu->tp.call;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530899 struct uprobe_trace_entry_head *entry;
900 struct hlist_head *head;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100901 void *data;
902 int size, rctx, i;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530903
Oleg Nesterov393a7362013-03-30 18:46:22 +0100904 size = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
Namhyung Kim14577c32013-07-03 15:42:53 +0900905 size = ALIGN(size + tu->tp.size + sizeof(u32), sizeof(u64)) - sizeof(u32);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530906
907 preempt_disable();
Oleg Nesterov515619f2013-04-13 15:36:49 +0200908 head = this_cpu_ptr(call->perf_events);
909 if (hlist_empty(head))
910 goto out;
911
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530912 entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
913 if (!entry)
914 goto out;
915
Oleg Nesterov393a7362013-03-30 18:46:22 +0100916 if (is_ret_probe(tu)) {
917 entry->vaddr[0] = func;
Oleg Nesterov32520b22013-04-10 16:25:49 +0200918 entry->vaddr[1] = instruction_pointer(regs);
Oleg Nesterov393a7362013-03-30 18:46:22 +0100919 data = DATAOF_TRACE_ENTRY(entry, true);
920 } else {
Oleg Nesterov32520b22013-04-10 16:25:49 +0200921 entry->vaddr[0] = instruction_pointer(regs);
Oleg Nesterov393a7362013-03-30 18:46:22 +0100922 data = DATAOF_TRACE_ENTRY(entry, false);
923 }
924
Namhyung Kim14577c32013-07-03 15:42:53 +0900925 for (i = 0; i < tu->tp.nr_args; i++) {
926 struct probe_arg *parg = &tu->tp.args[i];
927
928 call_fetch(&parg->fetch, regs, data + parg->offset);
929 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530930
Oleg Nesterov32520b22013-04-10 16:25:49 +0200931 perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530932 out:
933 preempt_enable();
Oleg Nesterova51cc602013-03-30 18:02:12 +0100934}
935
936/* uprobe profile handler */
937static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
938{
939 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
940 return UPROBE_HANDLER_REMOVE;
941
Oleg Nesterov393a7362013-03-30 18:46:22 +0100942 if (!is_ret_probe(tu))
943 uprobe_perf_print(tu, 0, regs);
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100944 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530945}
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100946
947static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
948 struct pt_regs *regs)
949{
950 uprobe_perf_print(tu, func, regs);
951}
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530952#endif /* CONFIG_PERF_EVENTS */
953
954static
955int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data)
956{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100957 struct trace_uprobe *tu = event->data;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530958
959 switch (type) {
960 case TRACE_REG_REGISTER:
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100961 return probe_event_enable(tu, TP_FLAG_TRACE, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530962
963 case TRACE_REG_UNREGISTER:
964 probe_event_disable(tu, TP_FLAG_TRACE);
965 return 0;
966
967#ifdef CONFIG_PERF_EVENTS
968 case TRACE_REG_PERF_REGISTER:
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100969 return probe_event_enable(tu, TP_FLAG_PROFILE, uprobe_perf_filter);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530970
971 case TRACE_REG_PERF_UNREGISTER:
972 probe_event_disable(tu, TP_FLAG_PROFILE);
973 return 0;
Oleg Nesterov736288b2013-02-03 20:58:35 +0100974
975 case TRACE_REG_PERF_OPEN:
976 return uprobe_perf_open(tu, data);
977
978 case TRACE_REG_PERF_CLOSE:
979 return uprobe_perf_close(tu, data);
980
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530981#endif
982 default:
983 return 0;
984 }
985 return 0;
986}
987
988static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
989{
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530990 struct trace_uprobe *tu;
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100991 int ret = 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530992
Oleg Nesterova932b732013-01-31 19:47:23 +0100993 tu = container_of(con, struct trace_uprobe, consumer);
Oleg Nesterov1b47aef2013-01-31 19:55:27 +0100994 tu->nhit++;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530995
Namhyung Kim14577c32013-07-03 15:42:53 +0900996 if (tu->tp.flags & TP_FLAG_TRACE)
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100997 ret |= uprobe_trace_func(tu, regs);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530998
999#ifdef CONFIG_PERF_EVENTS
Namhyung Kim14577c32013-07-03 15:42:53 +09001000 if (tu->tp.flags & TP_FLAG_PROFILE)
Oleg Nesterovf42d24a2013-02-04 17:48:34 +01001001 ret |= uprobe_perf_func(tu, regs);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301002#endif
Oleg Nesterovf42d24a2013-02-04 17:48:34 +01001003 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301004}
1005
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001006static int uretprobe_dispatcher(struct uprobe_consumer *con,
1007 unsigned long func, struct pt_regs *regs)
1008{
1009 struct trace_uprobe *tu;
1010
1011 tu = container_of(con, struct trace_uprobe, consumer);
1012
Namhyung Kim14577c32013-07-03 15:42:53 +09001013 if (tu->tp.flags & TP_FLAG_TRACE)
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001014 uretprobe_trace_func(tu, func, regs);
1015
1016#ifdef CONFIG_PERF_EVENTS
Namhyung Kim14577c32013-07-03 15:42:53 +09001017 if (tu->tp.flags & TP_FLAG_PROFILE)
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001018 uretprobe_perf_func(tu, func, regs);
1019#endif
1020 return 0;
1021}
1022
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301023static struct trace_event_functions uprobe_funcs = {
1024 .trace = print_uprobe_event
1025};
1026
1027static int register_uprobe_event(struct trace_uprobe *tu)
1028{
Namhyung Kim14577c32013-07-03 15:42:53 +09001029 struct ftrace_event_call *call = &tu->tp.call;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301030 int ret;
1031
1032 /* Initialize ftrace_event_call */
1033 INIT_LIST_HEAD(&call->class->fields);
1034 call->event.funcs = &uprobe_funcs;
1035 call->class->define_fields = uprobe_event_define_fields;
1036
Namhyung Kim5bf652a2013-07-03 16:09:02 +09001037 if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301038 return -ENOMEM;
1039
1040 ret = register_ftrace_event(&call->event);
1041 if (!ret) {
1042 kfree(call->print_fmt);
1043 return -ENODEV;
1044 }
1045 call->flags = 0;
1046 call->class->reg = trace_uprobe_register;
1047 call->data = tu;
1048 ret = trace_add_event_call(call);
1049
1050 if (ret) {
1051 pr_info("Failed to register uprobe event: %s\n", call->name);
1052 kfree(call->print_fmt);
1053 unregister_ftrace_event(&call->event);
1054 }
1055
1056 return ret;
1057}
1058
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001059static int unregister_uprobe_event(struct trace_uprobe *tu)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301060{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001061 int ret;
1062
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301063 /* tu->event is unregistered in trace_remove_event_call() */
Namhyung Kim14577c32013-07-03 15:42:53 +09001064 ret = trace_remove_event_call(&tu->tp.call);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001065 if (ret)
1066 return ret;
Namhyung Kim14577c32013-07-03 15:42:53 +09001067 kfree(tu->tp.call.print_fmt);
1068 tu->tp.call.print_fmt = NULL;
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001069 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301070}
1071
1072/* Make a trace interface for controling probe points */
1073static __init int init_uprobe_trace(void)
1074{
1075 struct dentry *d_tracer;
1076
1077 d_tracer = tracing_init_dentry();
1078 if (!d_tracer)
1079 return 0;
1080
1081 trace_create_file("uprobe_events", 0644, d_tracer,
1082 NULL, &uprobe_events_ops);
1083 /* Profile interface */
1084 trace_create_file("uprobe_profile", 0444, d_tracer,
1085 NULL, &uprobe_profile_ops);
1086 return 0;
1087}
1088
1089fs_initcall(init_uprobe_trace);