blob: 1fdea6d3f851b15162c80add785a9622fc83121b [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
Namhyung Kimb7e0bf32013-11-25 13:42:47 +090073struct uprobe_dispatch_data {
74 struct trace_uprobe *tu;
75 unsigned long bp_addr;
76};
77
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053078static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +010079static int uretprobe_dispatcher(struct uprobe_consumer *con,
80 unsigned long func, struct pt_regs *regs);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053081
Namhyung Kim3fd996a2013-11-26 15:21:04 +090082#ifdef CONFIG_STACK_GROWSUP
83static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
84{
85 return addr - (n * sizeof(long));
86}
87#else
88static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
89{
90 return addr + (n * sizeof(long));
91}
92#endif
93
94static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
95{
96 unsigned long ret;
97 unsigned long addr = user_stack_pointer(regs);
98
99 addr = adjust_stack_addr(addr, n);
100
101 if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
102 return 0;
103
104 return ret;
105}
106
107/*
108 * Uprobes-specific fetch functions
109 */
110#define DEFINE_FETCH_stack(type) \
111static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
112 void *offset, void *dest) \
113{ \
114 *(type *)dest = (type)get_user_stack_nth(regs, \
115 ((unsigned long)offset)); \
116}
117DEFINE_BASIC_FETCH_FUNCS(stack)
118/* No string on the stack entry */
119#define fetch_stack_string NULL
120#define fetch_stack_string_size NULL
121
Namhyung Kim5baaa592013-11-26 15:21:04 +0900122#define DEFINE_FETCH_memory(type) \
123static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\
124 void *addr, void *dest) \
125{ \
126 type retval; \
127 void __user *vaddr = (void __force __user *) addr; \
128 \
129 if (copy_from_user(&retval, vaddr, sizeof(type))) \
130 *(type *)dest = 0; \
131 else \
132 *(type *) dest = retval; \
133}
134DEFINE_BASIC_FETCH_FUNCS(memory)
135/*
136 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
137 * length and relative data location.
138 */
139static __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
140 void *addr, void *dest)
141{
142 long ret;
143 u32 rloc = *(u32 *)dest;
144 int maxlen = get_rloc_len(rloc);
145 u8 *dst = get_rloc_data(dest);
146 void __user *src = (void __force __user *) addr;
147
148 if (!maxlen)
149 return;
150
151 ret = strncpy_from_user(dst, src, maxlen);
152
153 if (ret < 0) { /* Failed to fetch string */
154 ((u8 *)get_rloc_data(dest))[0] = '\0';
155 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(rloc));
156 } else {
157 *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(rloc));
158 }
159}
160
161static __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
162 void *addr, void *dest)
163{
164 int len;
165 void __user *vaddr = (void __force __user *) addr;
166
167 len = strnlen_user(vaddr, MAX_STRING_SIZE);
168
169 if (len == 0 || len > MAX_STRING_SIZE) /* Failed to check length */
170 *(u32 *)dest = 0;
171 else
172 *(u32 *)dest = len;
173}
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900174
Namhyung Kim1301a442013-11-26 15:21:04 +0900175/* uprobes do not support symbol fetch methods */
176#define fetch_symbol_u8 NULL
177#define fetch_symbol_u16 NULL
178#define fetch_symbol_u32 NULL
179#define fetch_symbol_u64 NULL
180#define fetch_symbol_string NULL
181#define fetch_symbol_string_size NULL
182
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900183static unsigned long translate_user_vaddr(void *file_offset)
184{
185 unsigned long base_addr;
186 struct uprobe_dispatch_data *udd;
187
188 udd = (void *) current->utask->vaddr;
189
190 base_addr = udd->bp_addr - udd->tu->offset;
191 return base_addr + (unsigned long)file_offset;
192}
193
194#define DEFINE_FETCH_file_offset(type) \
195static __kprobes void FETCH_FUNC_NAME(file_offset, type)(struct pt_regs *regs,\
196 void *offset, void *dest) \
197{ \
198 void *vaddr = (void *)translate_user_vaddr(offset); \
199 \
200 FETCH_FUNC_NAME(memory, type)(regs, vaddr, dest); \
201}
202DEFINE_BASIC_FETCH_FUNCS(file_offset)
203DEFINE_FETCH_file_offset(string)
204DEFINE_FETCH_file_offset(string_size)
205
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900206/* Fetch type information table */
207const struct fetch_type uprobes_fetch_type_table[] = {
208 /* Special types */
209 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
210 sizeof(u32), 1, "__data_loc char[]"),
211 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
212 string_size, sizeof(u32), 0, "u32"),
213 /* Basic types */
214 ASSIGN_FETCH_TYPE(u8, u8, 0),
215 ASSIGN_FETCH_TYPE(u16, u16, 0),
216 ASSIGN_FETCH_TYPE(u32, u32, 0),
217 ASSIGN_FETCH_TYPE(u64, u64, 0),
218 ASSIGN_FETCH_TYPE(s8, u8, 1),
219 ASSIGN_FETCH_TYPE(s16, u16, 1),
220 ASSIGN_FETCH_TYPE(s32, u32, 1),
221 ASSIGN_FETCH_TYPE(s64, u64, 1),
222
223 ASSIGN_FETCH_TYPE_END
224};
225
Oleg Nesterov736288b2013-02-03 20:58:35 +0100226static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
227{
228 rwlock_init(&filter->rwlock);
229 filter->nr_systemwide = 0;
230 INIT_LIST_HEAD(&filter->perf_events);
231}
232
233static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
234{
235 return !filter->nr_systemwide && list_empty(&filter->perf_events);
236}
237
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100238static inline bool is_ret_probe(struct trace_uprobe *tu)
239{
240 return tu->consumer.ret_handler != NULL;
241}
242
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530243/*
244 * Allocate new trace_uprobe and initialize it (including uprobes).
245 */
246static struct trace_uprobe *
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100247alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530248{
249 struct trace_uprobe *tu;
250
251 if (!event || !is_good_name(event))
252 return ERR_PTR(-EINVAL);
253
254 if (!group || !is_good_name(group))
255 return ERR_PTR(-EINVAL);
256
257 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
258 if (!tu)
259 return ERR_PTR(-ENOMEM);
260
Namhyung Kim14577c32013-07-03 15:42:53 +0900261 tu->tp.call.class = &tu->tp.class;
262 tu->tp.call.name = kstrdup(event, GFP_KERNEL);
263 if (!tu->tp.call.name)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530264 goto error;
265
Namhyung Kim14577c32013-07-03 15:42:53 +0900266 tu->tp.class.system = kstrdup(group, GFP_KERNEL);
267 if (!tu->tp.class.system)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530268 goto error;
269
270 INIT_LIST_HEAD(&tu->list);
Oleg Nesterova932b732013-01-31 19:47:23 +0100271 tu->consumer.handler = uprobe_dispatcher;
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100272 if (is_ret)
273 tu->consumer.ret_handler = uretprobe_dispatcher;
Oleg Nesterov736288b2013-02-03 20:58:35 +0100274 init_trace_uprobe_filter(&tu->filter);
Namhyung Kim14577c32013-07-03 15:42:53 +0900275 tu->tp.call.flags |= TRACE_EVENT_FL_USE_CALL_FILTER;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530276 return tu;
277
278error:
Namhyung Kim14577c32013-07-03 15:42:53 +0900279 kfree(tu->tp.call.name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530280 kfree(tu);
281
282 return ERR_PTR(-ENOMEM);
283}
284
285static void free_trace_uprobe(struct trace_uprobe *tu)
286{
287 int i;
288
Namhyung Kim14577c32013-07-03 15:42:53 +0900289 for (i = 0; i < tu->tp.nr_args; i++)
290 traceprobe_free_probe_arg(&tu->tp.args[i]);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530291
292 iput(tu->inode);
Namhyung Kim14577c32013-07-03 15:42:53 +0900293 kfree(tu->tp.call.class->system);
294 kfree(tu->tp.call.name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530295 kfree(tu->filename);
296 kfree(tu);
297}
298
299static struct trace_uprobe *find_probe_event(const char *event, const char *group)
300{
301 struct trace_uprobe *tu;
302
303 list_for_each_entry(tu, &uprobe_list, list)
Namhyung Kim14577c32013-07-03 15:42:53 +0900304 if (strcmp(tu->tp.call.name, event) == 0 &&
305 strcmp(tu->tp.call.class->system, group) == 0)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530306 return tu;
307
308 return NULL;
309}
310
311/* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400312static int unregister_trace_uprobe(struct trace_uprobe *tu)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530313{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400314 int ret;
315
316 ret = unregister_uprobe_event(tu);
317 if (ret)
318 return ret;
319
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530320 list_del(&tu->list);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530321 free_trace_uprobe(tu);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400322 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530323}
324
325/* Register a trace_uprobe and probe_event */
326static int register_trace_uprobe(struct trace_uprobe *tu)
327{
Namhyung Kim14577c32013-07-03 15:42:53 +0900328 struct trace_uprobe *old_tu;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530329 int ret;
330
331 mutex_lock(&uprobe_lock);
332
333 /* register as an event */
Namhyung Kim14577c32013-07-03 15:42:53 +0900334 old_tu = find_probe_event(tu->tp.call.name, tu->tp.call.class->system);
335 if (old_tu) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530336 /* delete old event */
Namhyung Kim14577c32013-07-03 15:42:53 +0900337 ret = unregister_trace_uprobe(old_tu);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400338 if (ret)
339 goto end;
340 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530341
342 ret = register_uprobe_event(tu);
343 if (ret) {
344 pr_warning("Failed to register probe event(%d)\n", ret);
345 goto end;
346 }
347
348 list_add_tail(&tu->list, &uprobe_list);
349
350end:
351 mutex_unlock(&uprobe_lock);
352
353 return ret;
354}
355
356/*
357 * Argument syntax:
Namhyung Kim306cfe22013-07-03 16:44:46 +0900358 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530359 *
360 * - Remove uprobe: -:[GRP/]EVENT
361 */
362static int create_trace_uprobe(int argc, char **argv)
363{
364 struct trace_uprobe *tu;
365 struct inode *inode;
366 char *arg, *event, *group, *filename;
367 char buf[MAX_EVENT_NAME_LEN];
368 struct path path;
369 unsigned long offset;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100370 bool is_delete, is_return;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530371 int i, ret;
372
373 inode = NULL;
374 ret = 0;
375 is_delete = false;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100376 is_return = false;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530377 event = NULL;
378 group = NULL;
379
380 /* argc must be >= 1 */
381 if (argv[0][0] == '-')
382 is_delete = true;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100383 else if (argv[0][0] == 'r')
384 is_return = true;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530385 else if (argv[0][0] != 'p') {
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100386 pr_info("Probe definition must be started with 'p', 'r' or '-'.\n");
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530387 return -EINVAL;
388 }
389
390 if (argv[0][1] == ':') {
391 event = &argv[0][2];
392 arg = strchr(event, '/');
393
394 if (arg) {
395 group = event;
396 event = arg + 1;
397 event[-1] = '\0';
398
399 if (strlen(group) == 0) {
400 pr_info("Group name is not specified\n");
401 return -EINVAL;
402 }
403 }
404 if (strlen(event) == 0) {
405 pr_info("Event name is not specified\n");
406 return -EINVAL;
407 }
408 }
409 if (!group)
410 group = UPROBE_EVENT_SYSTEM;
411
412 if (is_delete) {
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400413 int ret;
414
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530415 if (!event) {
416 pr_info("Delete command needs an event name.\n");
417 return -EINVAL;
418 }
419 mutex_lock(&uprobe_lock);
420 tu = find_probe_event(event, group);
421
422 if (!tu) {
423 mutex_unlock(&uprobe_lock);
424 pr_info("Event %s/%s doesn't exist.\n", group, event);
425 return -ENOENT;
426 }
427 /* delete an event */
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400428 ret = unregister_trace_uprobe(tu);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530429 mutex_unlock(&uprobe_lock);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400430 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530431 }
432
433 if (argc < 2) {
434 pr_info("Probe point is not specified.\n");
435 return -EINVAL;
436 }
437 if (isdigit(argv[1][0])) {
438 pr_info("probe point must be have a filename.\n");
439 return -EINVAL;
440 }
441 arg = strchr(argv[1], ':');
zhangwei(Jovi)fa440632013-06-13 14:21:51 +0800442 if (!arg) {
443 ret = -EINVAL;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530444 goto fail_address_parse;
zhangwei(Jovi)fa440632013-06-13 14:21:51 +0800445 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530446
447 *arg++ = '\0';
448 filename = argv[1];
449 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
450 if (ret)
451 goto fail_address_parse;
452
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530453 inode = igrab(path.dentry->d_inode);
Oleg Nesterov84d7ed72013-01-27 18:20:45 +0100454 path_put(&path);
455
Oleg Nesterov7e4e28c2013-01-28 17:08:47 +0100456 if (!inode || !S_ISREG(inode->i_mode)) {
Jovi Zhangd24d7db2012-07-18 18:16:44 +0800457 ret = -EINVAL;
458 goto fail_address_parse;
459 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530460
Oleg Nesterov84d7ed72013-01-27 18:20:45 +0100461 ret = kstrtoul(arg, 0, &offset);
462 if (ret)
463 goto fail_address_parse;
464
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530465 argc -= 2;
466 argv += 2;
467
468 /* setup a probe */
469 if (!event) {
Andy Shevchenkob2e902f2012-12-17 16:01:27 -0800470 char *tail;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530471 char *ptr;
472
Andy Shevchenkob2e902f2012-12-17 16:01:27 -0800473 tail = kstrdup(kbasename(filename), GFP_KERNEL);
474 if (!tail) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530475 ret = -ENOMEM;
476 goto fail_address_parse;
477 }
478
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530479 ptr = strpbrk(tail, ".-_");
480 if (ptr)
481 *ptr = '\0';
482
483 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
484 event = buf;
485 kfree(tail);
486 }
487
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100488 tu = alloc_trace_uprobe(group, event, argc, is_return);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530489 if (IS_ERR(tu)) {
490 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
491 ret = PTR_ERR(tu);
492 goto fail_address_parse;
493 }
494 tu->offset = offset;
495 tu->inode = inode;
496 tu->filename = kstrdup(filename, GFP_KERNEL);
497
498 if (!tu->filename) {
499 pr_info("Failed to allocate filename.\n");
500 ret = -ENOMEM;
501 goto error;
502 }
503
504 /* parse arguments */
505 ret = 0;
506 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Namhyung Kim14577c32013-07-03 15:42:53 +0900507 struct probe_arg *parg = &tu->tp.args[i];
508
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530509 /* Increment count for freeing args in error case */
Namhyung Kim14577c32013-07-03 15:42:53 +0900510 tu->tp.nr_args++;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530511
512 /* Parse argument name */
513 arg = strchr(argv[i], '=');
514 if (arg) {
515 *arg++ = '\0';
Namhyung Kim14577c32013-07-03 15:42:53 +0900516 parg->name = kstrdup(argv[i], GFP_KERNEL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530517 } else {
518 arg = argv[i];
519 /* If argument name is omitted, set "argN" */
520 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
Namhyung Kim14577c32013-07-03 15:42:53 +0900521 parg->name = kstrdup(buf, GFP_KERNEL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530522 }
523
Namhyung Kim14577c32013-07-03 15:42:53 +0900524 if (!parg->name) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530525 pr_info("Failed to allocate argument[%d] name.\n", i);
526 ret = -ENOMEM;
527 goto error;
528 }
529
Namhyung Kim14577c32013-07-03 15:42:53 +0900530 if (!is_good_name(parg->name)) {
531 pr_info("Invalid argument[%d] name: %s\n", i, parg->name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530532 ret = -EINVAL;
533 goto error;
534 }
535
Namhyung Kim14577c32013-07-03 15:42:53 +0900536 if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530537 pr_info("Argument[%d] name '%s' conflicts with "
538 "another field.\n", i, argv[i]);
539 ret = -EINVAL;
540 goto error;
541 }
542
543 /* Parse fetch argument */
Namhyung Kim14577c32013-07-03 15:42:53 +0900544 ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg,
Namhyung Kima4734142013-11-27 11:36:47 +0900545 is_return, false);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530546 if (ret) {
547 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
548 goto error;
549 }
550 }
551
552 ret = register_trace_uprobe(tu);
553 if (ret)
554 goto error;
555 return 0;
556
557error:
558 free_trace_uprobe(tu);
559 return ret;
560
561fail_address_parse:
562 if (inode)
563 iput(inode);
564
Jovi Zhangd24d7db2012-07-18 18:16:44 +0800565 pr_info("Failed to parse address or file.\n");
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530566
567 return ret;
568}
569
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400570static int cleanup_all_probes(void)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530571{
572 struct trace_uprobe *tu;
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400573 int ret = 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530574
575 mutex_lock(&uprobe_lock);
576 while (!list_empty(&uprobe_list)) {
577 tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400578 ret = unregister_trace_uprobe(tu);
579 if (ret)
580 break;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530581 }
582 mutex_unlock(&uprobe_lock);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400583 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530584}
585
586/* Probes listing interfaces */
587static void *probes_seq_start(struct seq_file *m, loff_t *pos)
588{
589 mutex_lock(&uprobe_lock);
590 return seq_list_start(&uprobe_list, *pos);
591}
592
593static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
594{
595 return seq_list_next(v, &uprobe_list, pos);
596}
597
598static void probes_seq_stop(struct seq_file *m, void *v)
599{
600 mutex_unlock(&uprobe_lock);
601}
602
603static int probes_seq_show(struct seq_file *m, void *v)
604{
605 struct trace_uprobe *tu = v;
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100606 char c = is_ret_probe(tu) ? 'r' : 'p';
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530607 int i;
608
Namhyung Kim14577c32013-07-03 15:42:53 +0900609 seq_printf(m, "%c:%s/%s", c, tu->tp.call.class->system, tu->tp.call.name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530610 seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset);
611
Namhyung Kim14577c32013-07-03 15:42:53 +0900612 for (i = 0; i < tu->tp.nr_args; i++)
613 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530614
615 seq_printf(m, "\n");
616 return 0;
617}
618
619static const struct seq_operations probes_seq_op = {
620 .start = probes_seq_start,
621 .next = probes_seq_next,
622 .stop = probes_seq_stop,
623 .show = probes_seq_show
624};
625
626static int probes_open(struct inode *inode, struct file *file)
627{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400628 int ret;
629
630 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
631 ret = cleanup_all_probes();
632 if (ret)
633 return ret;
634 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530635
636 return seq_open(file, &probes_seq_op);
637}
638
639static ssize_t probes_write(struct file *file, const char __user *buffer,
640 size_t count, loff_t *ppos)
641{
642 return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
643}
644
645static const struct file_operations uprobe_events_ops = {
646 .owner = THIS_MODULE,
647 .open = probes_open,
648 .read = seq_read,
649 .llseek = seq_lseek,
650 .release = seq_release,
651 .write = probes_write,
652};
653
654/* Probes profiling interfaces */
655static int probes_profile_seq_show(struct seq_file *m, void *v)
656{
657 struct trace_uprobe *tu = v;
658
Namhyung Kim14577c32013-07-03 15:42:53 +0900659 seq_printf(m, " %s %-44s %15lu\n", tu->filename, tu->tp.call.name, tu->nhit);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530660 return 0;
661}
662
663static const struct seq_operations profile_seq_op = {
664 .start = probes_seq_start,
665 .next = probes_seq_next,
666 .stop = probes_seq_stop,
667 .show = probes_profile_seq_show
668};
669
670static int profile_open(struct inode *inode, struct file *file)
671{
672 return seq_open(file, &profile_seq_op);
673}
674
675static const struct file_operations uprobe_profile_ops = {
676 .owner = THIS_MODULE,
677 .open = profile_open,
678 .read = seq_read,
679 .llseek = seq_lseek,
680 .release = seq_release,
681};
682
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900683struct uprobe_cpu_buffer {
684 struct mutex mutex;
685 void *buf;
686};
687static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
688static int uprobe_buffer_refcnt;
689
690static int uprobe_buffer_init(void)
691{
692 int cpu, err_cpu;
693
694 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
695 if (uprobe_cpu_buffer == NULL)
696 return -ENOMEM;
697
698 for_each_possible_cpu(cpu) {
699 struct page *p = alloc_pages_node(cpu_to_node(cpu),
700 GFP_KERNEL, 0);
701 if (p == NULL) {
702 err_cpu = cpu;
703 goto err;
704 }
705 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
706 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
707 }
708
709 return 0;
710
711err:
712 for_each_possible_cpu(cpu) {
713 if (cpu == err_cpu)
714 break;
715 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
716 }
717
718 free_percpu(uprobe_cpu_buffer);
719 return -ENOMEM;
720}
721
722static int uprobe_buffer_enable(void)
723{
724 int ret = 0;
725
726 BUG_ON(!mutex_is_locked(&event_mutex));
727
728 if (uprobe_buffer_refcnt++ == 0) {
729 ret = uprobe_buffer_init();
730 if (ret < 0)
731 uprobe_buffer_refcnt--;
732 }
733
734 return ret;
735}
736
737static void uprobe_buffer_disable(void)
738{
739 BUG_ON(!mutex_is_locked(&event_mutex));
740
741 if (--uprobe_buffer_refcnt == 0) {
742 free_percpu(uprobe_cpu_buffer);
743 uprobe_cpu_buffer = NULL;
744 }
745}
746
747static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
748{
749 struct uprobe_cpu_buffer *ucb;
750 int cpu;
751
752 cpu = raw_smp_processor_id();
753 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
754
755 /*
756 * Use per-cpu buffers for fastest access, but we might migrate
757 * so the mutex makes sure we have sole access to it.
758 */
759 mutex_lock(&ucb->mutex);
760
761 return ucb;
762}
763
764static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
765{
766 mutex_unlock(&ucb->mutex);
767}
768
Oleg Nesterova51cc602013-03-30 18:02:12 +0100769static void uprobe_trace_print(struct trace_uprobe *tu,
770 unsigned long func, struct pt_regs *regs)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530771{
772 struct uprobe_trace_entry_head *entry;
773 struct ring_buffer_event *event;
774 struct ring_buffer *buffer;
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900775 struct uprobe_cpu_buffer *ucb;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100776 void *data;
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900777 int size, dsize, esize;
Namhyung Kim14577c32013-07-03 15:42:53 +0900778 struct ftrace_event_call *call = &tu->tp.call;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530779
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900780 dsize = __get_data_size(&tu->tp, regs);
781 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
782
783 if (WARN_ON_ONCE(!uprobe_cpu_buffer || tu->tp.size + dsize > PAGE_SIZE))
Oleg Nesterova51cc602013-03-30 18:02:12 +0100784 return;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530785
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900786 ucb = uprobe_buffer_get();
787 store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
788
789 size = esize + tu->tp.size + dsize;
790 event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
791 size, 0, 0);
792 if (!event)
793 goto out;
794
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530795 entry = ring_buffer_event_data(event);
Oleg Nesterov393a7362013-03-30 18:46:22 +0100796 if (is_ret_probe(tu)) {
797 entry->vaddr[0] = func;
798 entry->vaddr[1] = instruction_pointer(regs);
799 data = DATAOF_TRACE_ENTRY(entry, true);
800 } else {
801 entry->vaddr[0] = instruction_pointer(regs);
802 data = DATAOF_TRACE_ENTRY(entry, false);
803 }
804
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900805 memcpy(data, ucb->buf, tu->tp.size + dsize);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530806
Tom Zanussif306cc82013-10-24 08:34:17 -0500807 if (!call_filter_check_discard(call, entry, buffer, event))
Oleg Nesterov0e3853d2013-03-28 19:19:11 +0100808 trace_buffer_unlock_commit(buffer, event, 0, 0);
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900809
810out:
811 uprobe_buffer_put(ucb);
Oleg Nesterova51cc602013-03-30 18:02:12 +0100812}
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100813
Oleg Nesterova51cc602013-03-30 18:02:12 +0100814/* uprobe handler */
815static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
816{
Oleg Nesterov393a7362013-03-30 18:46:22 +0100817 if (!is_ret_probe(tu))
818 uprobe_trace_print(tu, 0, regs);
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100819 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530820}
821
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100822static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
823 struct pt_regs *regs)
824{
825 uprobe_trace_print(tu, func, regs);
826}
827
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530828/* Event entry printers */
829static enum print_line_t
830print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
831{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100832 struct uprobe_trace_entry_head *entry;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530833 struct trace_seq *s = &iter->seq;
834 struct trace_uprobe *tu;
835 u8 *data;
836 int i;
837
Oleg Nesterov457d1772013-03-29 18:26:51 +0100838 entry = (struct uprobe_trace_entry_head *)iter->ent;
Namhyung Kim14577c32013-07-03 15:42:53 +0900839 tu = container_of(event, struct trace_uprobe, tp.call.event);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530840
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100841 if (is_ret_probe(tu)) {
Namhyung Kim14577c32013-07-03 15:42:53 +0900842 if (!trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", tu->tp.call.name,
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100843 entry->vaddr[1], entry->vaddr[0]))
844 goto partial;
845 data = DATAOF_TRACE_ENTRY(entry, true);
846 } else {
Namhyung Kim14577c32013-07-03 15:42:53 +0900847 if (!trace_seq_printf(s, "%s: (0x%lx)", tu->tp.call.name,
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100848 entry->vaddr[0]))
849 goto partial;
850 data = DATAOF_TRACE_ENTRY(entry, false);
851 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530852
Namhyung Kim14577c32013-07-03 15:42:53 +0900853 for (i = 0; i < tu->tp.nr_args; i++) {
854 struct probe_arg *parg = &tu->tp.args[i];
855
856 if (!parg->type->print(s, parg->name, data + parg->offset, entry))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530857 goto partial;
858 }
859
860 if (trace_seq_puts(s, "\n"))
861 return TRACE_TYPE_HANDLED;
862
863partial:
864 return TRACE_TYPE_PARTIAL_LINE;
865}
866
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100867typedef bool (*filter_func_t)(struct uprobe_consumer *self,
868 enum uprobe_filter_ctx ctx,
869 struct mm_struct *mm);
870
871static int
872probe_event_enable(struct trace_uprobe *tu, int flag, filter_func_t filter)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530873{
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530874 int ret = 0;
875
Namhyung Kim14577c32013-07-03 15:42:53 +0900876 if (trace_probe_is_enabled(&tu->tp))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530877 return -EINTR;
878
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900879 ret = uprobe_buffer_enable();
880 if (ret < 0)
881 return ret;
882
Oleg Nesterov736288b2013-02-03 20:58:35 +0100883 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
884
Namhyung Kim14577c32013-07-03 15:42:53 +0900885 tu->tp.flags |= flag;
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100886 tu->consumer.filter = filter;
Oleg Nesterova932b732013-01-31 19:47:23 +0100887 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
888 if (ret)
Namhyung Kim14577c32013-07-03 15:42:53 +0900889 tu->tp.flags &= ~flag;
Oleg Nesterov41618242013-01-27 18:36:24 +0100890
891 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530892}
893
894static void probe_event_disable(struct trace_uprobe *tu, int flag)
895{
Namhyung Kim14577c32013-07-03 15:42:53 +0900896 if (!trace_probe_is_enabled(&tu->tp))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530897 return;
898
Oleg Nesterov736288b2013-02-03 20:58:35 +0100899 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
900
Oleg Nesterova932b732013-01-31 19:47:23 +0100901 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
Namhyung Kim14577c32013-07-03 15:42:53 +0900902 tu->tp.flags &= ~flag;
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900903
904 uprobe_buffer_disable();
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530905}
906
907static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
908{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100909 int ret, i, size;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530910 struct uprobe_trace_entry_head field;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100911 struct trace_uprobe *tu = event_call->data;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530912
Oleg Nesterov4d1298e2013-03-30 19:23:15 +0100913 if (is_ret_probe(tu)) {
914 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
915 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
916 size = SIZEOF_TRACE_ENTRY(true);
917 } else {
918 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
919 size = SIZEOF_TRACE_ENTRY(false);
920 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530921 /* Set argument names as fields */
Namhyung Kim14577c32013-07-03 15:42:53 +0900922 for (i = 0; i < tu->tp.nr_args; i++) {
923 struct probe_arg *parg = &tu->tp.args[i];
924
925 ret = trace_define_field(event_call, parg->type->fmttype,
926 parg->name, size + parg->offset,
927 parg->type->size, parg->type->is_signed,
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530928 FILTER_OTHER);
929
930 if (ret)
931 return ret;
932 }
933 return 0;
934}
935
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530936#ifdef CONFIG_PERF_EVENTS
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100937static bool
938__uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
939{
940 struct perf_event *event;
941
942 if (filter->nr_systemwide)
943 return true;
944
945 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
946 if (event->hw.tp_target->mm == mm)
947 return true;
948 }
949
950 return false;
951}
952
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100953static inline bool
954uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
955{
956 return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm);
957}
958
Oleg Nesterov736288b2013-02-03 20:58:35 +0100959static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
960{
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100961 bool done;
962
Oleg Nesterov736288b2013-02-03 20:58:35 +0100963 write_lock(&tu->filter.rwlock);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100964 if (event->hw.tp_target) {
965 /*
966 * event->parent != NULL means copy_process(), we can avoid
967 * uprobe_apply(). current->mm must be probed and we can rely
968 * on dup_mmap() which preserves the already installed bp's.
969 *
970 * attr.enable_on_exec means that exec/mmap will install the
971 * breakpoints we need.
972 */
973 done = tu->filter.nr_systemwide ||
974 event->parent || event->attr.enable_on_exec ||
975 uprobe_filter_event(tu, event);
Oleg Nesterov736288b2013-02-03 20:58:35 +0100976 list_add(&event->hw.tp_list, &tu->filter.perf_events);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100977 } else {
978 done = tu->filter.nr_systemwide;
Oleg Nesterov736288b2013-02-03 20:58:35 +0100979 tu->filter.nr_systemwide++;
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100980 }
Oleg Nesterov736288b2013-02-03 20:58:35 +0100981 write_unlock(&tu->filter.rwlock);
982
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100983 if (!done)
984 uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100985
Oleg Nesterov736288b2013-02-03 20:58:35 +0100986 return 0;
987}
988
989static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
990{
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100991 bool done;
992
Oleg Nesterov736288b2013-02-03 20:58:35 +0100993 write_lock(&tu->filter.rwlock);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100994 if (event->hw.tp_target) {
Oleg Nesterov736288b2013-02-03 20:58:35 +0100995 list_del(&event->hw.tp_list);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100996 done = tu->filter.nr_systemwide ||
997 (event->hw.tp_target->flags & PF_EXITING) ||
998 uprobe_filter_event(tu, event);
999 } else {
Oleg Nesterov736288b2013-02-03 20:58:35 +01001000 tu->filter.nr_systemwide--;
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001001 done = tu->filter.nr_systemwide;
1002 }
Oleg Nesterov736288b2013-02-03 20:58:35 +01001003 write_unlock(&tu->filter.rwlock);
1004
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001005 if (!done)
1006 uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
Oleg Nesterov31ba3342013-02-04 17:11:58 +01001007
Oleg Nesterov736288b2013-02-03 20:58:35 +01001008 return 0;
1009}
1010
Oleg Nesterov31ba3342013-02-04 17:11:58 +01001011static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1012 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1013{
1014 struct trace_uprobe *tu;
1015 int ret;
1016
1017 tu = container_of(uc, struct trace_uprobe, consumer);
1018 read_lock(&tu->filter.rwlock);
1019 ret = __uprobe_perf_filter(&tu->filter, mm);
1020 read_unlock(&tu->filter.rwlock);
1021
1022 return ret;
1023}
1024
Oleg Nesterova51cc602013-03-30 18:02:12 +01001025static void uprobe_perf_print(struct trace_uprobe *tu,
1026 unsigned long func, struct pt_regs *regs)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301027{
Namhyung Kim14577c32013-07-03 15:42:53 +09001028 struct ftrace_event_call *call = &tu->tp.call;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301029 struct uprobe_trace_entry_head *entry;
1030 struct hlist_head *head;
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001031 struct uprobe_cpu_buffer *ucb;
Oleg Nesterov457d1772013-03-29 18:26:51 +01001032 void *data;
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001033 int size, dsize, esize;
1034 int rctx;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301035
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001036 dsize = __get_data_size(&tu->tp, regs);
1037 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1038
1039 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1040 return;
1041
1042 size = esize + tu->tp.size + dsize;
1043 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1044 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1045 return;
1046
1047 ucb = uprobe_buffer_get();
1048 store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301049
1050 preempt_disable();
Oleg Nesterov515619f2013-04-13 15:36:49 +02001051 head = this_cpu_ptr(call->perf_events);
1052 if (hlist_empty(head))
1053 goto out;
1054
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301055 entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
1056 if (!entry)
1057 goto out;
1058
Oleg Nesterov393a7362013-03-30 18:46:22 +01001059 if (is_ret_probe(tu)) {
1060 entry->vaddr[0] = func;
Oleg Nesterov32520b22013-04-10 16:25:49 +02001061 entry->vaddr[1] = instruction_pointer(regs);
Oleg Nesterov393a7362013-03-30 18:46:22 +01001062 data = DATAOF_TRACE_ENTRY(entry, true);
1063 } else {
Oleg Nesterov32520b22013-04-10 16:25:49 +02001064 entry->vaddr[0] = instruction_pointer(regs);
Oleg Nesterov393a7362013-03-30 18:46:22 +01001065 data = DATAOF_TRACE_ENTRY(entry, false);
1066 }
1067
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001068 memcpy(data, ucb->buf, tu->tp.size + dsize);
Namhyung Kim14577c32013-07-03 15:42:53 +09001069
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001070 if (size - esize > tu->tp.size + dsize) {
1071 int len = tu->tp.size + dsize;
1072
1073 memset(data + len, 0, size - esize - len);
Namhyung Kim14577c32013-07-03 15:42:53 +09001074 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301075
Oleg Nesterov32520b22013-04-10 16:25:49 +02001076 perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301077 out:
1078 preempt_enable();
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001079 uprobe_buffer_put(ucb);
Oleg Nesterova51cc602013-03-30 18:02:12 +01001080}
1081
1082/* uprobe profile handler */
1083static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
1084{
1085 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1086 return UPROBE_HANDLER_REMOVE;
1087
Oleg Nesterov393a7362013-03-30 18:46:22 +01001088 if (!is_ret_probe(tu))
1089 uprobe_perf_print(tu, 0, regs);
Oleg Nesterovf42d24a2013-02-04 17:48:34 +01001090 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301091}
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001092
1093static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1094 struct pt_regs *regs)
1095{
1096 uprobe_perf_print(tu, func, regs);
1097}
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301098#endif /* CONFIG_PERF_EVENTS */
1099
1100static
1101int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data)
1102{
Oleg Nesterov457d1772013-03-29 18:26:51 +01001103 struct trace_uprobe *tu = event->data;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301104
1105 switch (type) {
1106 case TRACE_REG_REGISTER:
Oleg Nesterov31ba3342013-02-04 17:11:58 +01001107 return probe_event_enable(tu, TP_FLAG_TRACE, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301108
1109 case TRACE_REG_UNREGISTER:
1110 probe_event_disable(tu, TP_FLAG_TRACE);
1111 return 0;
1112
1113#ifdef CONFIG_PERF_EVENTS
1114 case TRACE_REG_PERF_REGISTER:
Oleg Nesterov31ba3342013-02-04 17:11:58 +01001115 return probe_event_enable(tu, TP_FLAG_PROFILE, uprobe_perf_filter);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301116
1117 case TRACE_REG_PERF_UNREGISTER:
1118 probe_event_disable(tu, TP_FLAG_PROFILE);
1119 return 0;
Oleg Nesterov736288b2013-02-03 20:58:35 +01001120
1121 case TRACE_REG_PERF_OPEN:
1122 return uprobe_perf_open(tu, data);
1123
1124 case TRACE_REG_PERF_CLOSE:
1125 return uprobe_perf_close(tu, data);
1126
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301127#endif
1128 default:
1129 return 0;
1130 }
1131 return 0;
1132}
1133
1134static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1135{
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301136 struct trace_uprobe *tu;
Namhyung Kimb7e0bf32013-11-25 13:42:47 +09001137 struct uprobe_dispatch_data udd;
Oleg Nesterovf42d24a2013-02-04 17:48:34 +01001138 int ret = 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301139
Oleg Nesterova932b732013-01-31 19:47:23 +01001140 tu = container_of(con, struct trace_uprobe, consumer);
Oleg Nesterov1b47aef2013-01-31 19:55:27 +01001141 tu->nhit++;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301142
Namhyung Kimb7e0bf32013-11-25 13:42:47 +09001143 udd.tu = tu;
1144 udd.bp_addr = instruction_pointer(regs);
1145
1146 current->utask->vaddr = (unsigned long) &udd;
1147
Namhyung Kim14577c32013-07-03 15:42:53 +09001148 if (tu->tp.flags & TP_FLAG_TRACE)
Oleg Nesterovf42d24a2013-02-04 17:48:34 +01001149 ret |= uprobe_trace_func(tu, regs);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301150
1151#ifdef CONFIG_PERF_EVENTS
Namhyung Kim14577c32013-07-03 15:42:53 +09001152 if (tu->tp.flags & TP_FLAG_PROFILE)
Oleg Nesterovf42d24a2013-02-04 17:48:34 +01001153 ret |= uprobe_perf_func(tu, regs);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301154#endif
Oleg Nesterovf42d24a2013-02-04 17:48:34 +01001155 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301156}
1157
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001158static int uretprobe_dispatcher(struct uprobe_consumer *con,
1159 unsigned long func, struct pt_regs *regs)
1160{
1161 struct trace_uprobe *tu;
Namhyung Kimb7e0bf32013-11-25 13:42:47 +09001162 struct uprobe_dispatch_data udd;
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001163
1164 tu = container_of(con, struct trace_uprobe, consumer);
1165
Namhyung Kimb7e0bf32013-11-25 13:42:47 +09001166 udd.tu = tu;
1167 udd.bp_addr = func;
1168
1169 current->utask->vaddr = (unsigned long) &udd;
1170
Namhyung Kim14577c32013-07-03 15:42:53 +09001171 if (tu->tp.flags & TP_FLAG_TRACE)
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001172 uretprobe_trace_func(tu, func, regs);
1173
1174#ifdef CONFIG_PERF_EVENTS
Namhyung Kim14577c32013-07-03 15:42:53 +09001175 if (tu->tp.flags & TP_FLAG_PROFILE)
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001176 uretprobe_perf_func(tu, func, regs);
1177#endif
1178 return 0;
1179}
1180
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301181static struct trace_event_functions uprobe_funcs = {
1182 .trace = print_uprobe_event
1183};
1184
1185static int register_uprobe_event(struct trace_uprobe *tu)
1186{
Namhyung Kim14577c32013-07-03 15:42:53 +09001187 struct ftrace_event_call *call = &tu->tp.call;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301188 int ret;
1189
1190 /* Initialize ftrace_event_call */
1191 INIT_LIST_HEAD(&call->class->fields);
1192 call->event.funcs = &uprobe_funcs;
1193 call->class->define_fields = uprobe_event_define_fields;
1194
Namhyung Kim5bf652a2013-07-03 16:09:02 +09001195 if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301196 return -ENOMEM;
1197
1198 ret = register_ftrace_event(&call->event);
1199 if (!ret) {
1200 kfree(call->print_fmt);
1201 return -ENODEV;
1202 }
1203 call->flags = 0;
1204 call->class->reg = trace_uprobe_register;
1205 call->data = tu;
1206 ret = trace_add_event_call(call);
1207
1208 if (ret) {
1209 pr_info("Failed to register uprobe event: %s\n", call->name);
1210 kfree(call->print_fmt);
1211 unregister_ftrace_event(&call->event);
1212 }
1213
1214 return ret;
1215}
1216
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001217static int unregister_uprobe_event(struct trace_uprobe *tu)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301218{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001219 int ret;
1220
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301221 /* tu->event is unregistered in trace_remove_event_call() */
Namhyung Kim14577c32013-07-03 15:42:53 +09001222 ret = trace_remove_event_call(&tu->tp.call);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001223 if (ret)
1224 return ret;
Namhyung Kim14577c32013-07-03 15:42:53 +09001225 kfree(tu->tp.call.print_fmt);
1226 tu->tp.call.print_fmt = NULL;
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001227 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301228}
1229
1230/* Make a trace interface for controling probe points */
1231static __init int init_uprobe_trace(void)
1232{
1233 struct dentry *d_tracer;
1234
1235 d_tracer = tracing_init_dentry();
1236 if (!d_tracer)
1237 return 0;
1238
1239 trace_create_file("uprobe_events", 0644, d_tracer,
1240 NULL, &uprobe_events_ops);
1241 /* Profile interface */
1242 trace_create_file("uprobe_profile", 0444, d_tracer,
1243 NULL, &uprobe_profile_ops);
1244 return 0;
1245}
1246
1247fs_initcall(init_uprobe_trace);