blob: 2c60925ea07361c2e4846aea9653314180979ee1 [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 Kim34fee3a2013-11-26 14:56:28 +090077/* Fetch type information table */
78const struct fetch_type uprobes_fetch_type_table[] = {
79 /* Special types */
80 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
81 sizeof(u32), 1, "__data_loc char[]"),
82 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
83 string_size, sizeof(u32), 0, "u32"),
84 /* Basic types */
85 ASSIGN_FETCH_TYPE(u8, u8, 0),
86 ASSIGN_FETCH_TYPE(u16, u16, 0),
87 ASSIGN_FETCH_TYPE(u32, u32, 0),
88 ASSIGN_FETCH_TYPE(u64, u64, 0),
89 ASSIGN_FETCH_TYPE(s8, u8, 1),
90 ASSIGN_FETCH_TYPE(s16, u16, 1),
91 ASSIGN_FETCH_TYPE(s32, u32, 1),
92 ASSIGN_FETCH_TYPE(s64, u64, 1),
93
94 ASSIGN_FETCH_TYPE_END
95};
96
Oleg Nesterov736288b2013-02-03 20:58:35 +010097static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
98{
99 rwlock_init(&filter->rwlock);
100 filter->nr_systemwide = 0;
101 INIT_LIST_HEAD(&filter->perf_events);
102}
103
104static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
105{
106 return !filter->nr_systemwide && list_empty(&filter->perf_events);
107}
108
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100109static inline bool is_ret_probe(struct trace_uprobe *tu)
110{
111 return tu->consumer.ret_handler != NULL;
112}
113
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530114/*
115 * Allocate new trace_uprobe and initialize it (including uprobes).
116 */
117static struct trace_uprobe *
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100118alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530119{
120 struct trace_uprobe *tu;
121
122 if (!event || !is_good_name(event))
123 return ERR_PTR(-EINVAL);
124
125 if (!group || !is_good_name(group))
126 return ERR_PTR(-EINVAL);
127
128 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
129 if (!tu)
130 return ERR_PTR(-ENOMEM);
131
Namhyung Kim14577c32013-07-03 15:42:53 +0900132 tu->tp.call.class = &tu->tp.class;
133 tu->tp.call.name = kstrdup(event, GFP_KERNEL);
134 if (!tu->tp.call.name)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530135 goto error;
136
Namhyung Kim14577c32013-07-03 15:42:53 +0900137 tu->tp.class.system = kstrdup(group, GFP_KERNEL);
138 if (!tu->tp.class.system)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530139 goto error;
140
141 INIT_LIST_HEAD(&tu->list);
Oleg Nesterova932b732013-01-31 19:47:23 +0100142 tu->consumer.handler = uprobe_dispatcher;
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100143 if (is_ret)
144 tu->consumer.ret_handler = uretprobe_dispatcher;
Oleg Nesterov736288b2013-02-03 20:58:35 +0100145 init_trace_uprobe_filter(&tu->filter);
Namhyung Kim14577c32013-07-03 15:42:53 +0900146 tu->tp.call.flags |= TRACE_EVENT_FL_USE_CALL_FILTER;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530147 return tu;
148
149error:
Namhyung Kim14577c32013-07-03 15:42:53 +0900150 kfree(tu->tp.call.name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530151 kfree(tu);
152
153 return ERR_PTR(-ENOMEM);
154}
155
156static void free_trace_uprobe(struct trace_uprobe *tu)
157{
158 int i;
159
Namhyung Kim14577c32013-07-03 15:42:53 +0900160 for (i = 0; i < tu->tp.nr_args; i++)
161 traceprobe_free_probe_arg(&tu->tp.args[i]);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530162
163 iput(tu->inode);
Namhyung Kim14577c32013-07-03 15:42:53 +0900164 kfree(tu->tp.call.class->system);
165 kfree(tu->tp.call.name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530166 kfree(tu->filename);
167 kfree(tu);
168}
169
170static struct trace_uprobe *find_probe_event(const char *event, const char *group)
171{
172 struct trace_uprobe *tu;
173
174 list_for_each_entry(tu, &uprobe_list, list)
Namhyung Kim14577c32013-07-03 15:42:53 +0900175 if (strcmp(tu->tp.call.name, event) == 0 &&
176 strcmp(tu->tp.call.class->system, group) == 0)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530177 return tu;
178
179 return NULL;
180}
181
182/* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400183static int unregister_trace_uprobe(struct trace_uprobe *tu)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530184{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400185 int ret;
186
187 ret = unregister_uprobe_event(tu);
188 if (ret)
189 return ret;
190
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530191 list_del(&tu->list);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530192 free_trace_uprobe(tu);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400193 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530194}
195
196/* Register a trace_uprobe and probe_event */
197static int register_trace_uprobe(struct trace_uprobe *tu)
198{
Namhyung Kim14577c32013-07-03 15:42:53 +0900199 struct trace_uprobe *old_tu;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530200 int ret;
201
202 mutex_lock(&uprobe_lock);
203
204 /* register as an event */
Namhyung Kim14577c32013-07-03 15:42:53 +0900205 old_tu = find_probe_event(tu->tp.call.name, tu->tp.call.class->system);
206 if (old_tu) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530207 /* delete old event */
Namhyung Kim14577c32013-07-03 15:42:53 +0900208 ret = unregister_trace_uprobe(old_tu);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400209 if (ret)
210 goto end;
211 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530212
213 ret = register_uprobe_event(tu);
214 if (ret) {
215 pr_warning("Failed to register probe event(%d)\n", ret);
216 goto end;
217 }
218
219 list_add_tail(&tu->list, &uprobe_list);
220
221end:
222 mutex_unlock(&uprobe_lock);
223
224 return ret;
225}
226
227/*
228 * Argument syntax:
Namhyung Kim306cfe22013-07-03 16:44:46 +0900229 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530230 *
231 * - Remove uprobe: -:[GRP/]EVENT
232 */
233static int create_trace_uprobe(int argc, char **argv)
234{
235 struct trace_uprobe *tu;
236 struct inode *inode;
237 char *arg, *event, *group, *filename;
238 char buf[MAX_EVENT_NAME_LEN];
239 struct path path;
240 unsigned long offset;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100241 bool is_delete, is_return;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530242 int i, ret;
243
244 inode = NULL;
245 ret = 0;
246 is_delete = false;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100247 is_return = false;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530248 event = NULL;
249 group = NULL;
250
251 /* argc must be >= 1 */
252 if (argv[0][0] == '-')
253 is_delete = true;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100254 else if (argv[0][0] == 'r')
255 is_return = true;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530256 else if (argv[0][0] != 'p') {
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100257 pr_info("Probe definition must be started with 'p', 'r' or '-'.\n");
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530258 return -EINVAL;
259 }
260
261 if (argv[0][1] == ':') {
262 event = &argv[0][2];
263 arg = strchr(event, '/');
264
265 if (arg) {
266 group = event;
267 event = arg + 1;
268 event[-1] = '\0';
269
270 if (strlen(group) == 0) {
271 pr_info("Group name is not specified\n");
272 return -EINVAL;
273 }
274 }
275 if (strlen(event) == 0) {
276 pr_info("Event name is not specified\n");
277 return -EINVAL;
278 }
279 }
280 if (!group)
281 group = UPROBE_EVENT_SYSTEM;
282
283 if (is_delete) {
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400284 int ret;
285
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530286 if (!event) {
287 pr_info("Delete command needs an event name.\n");
288 return -EINVAL;
289 }
290 mutex_lock(&uprobe_lock);
291 tu = find_probe_event(event, group);
292
293 if (!tu) {
294 mutex_unlock(&uprobe_lock);
295 pr_info("Event %s/%s doesn't exist.\n", group, event);
296 return -ENOENT;
297 }
298 /* delete an event */
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400299 ret = unregister_trace_uprobe(tu);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530300 mutex_unlock(&uprobe_lock);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400301 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530302 }
303
304 if (argc < 2) {
305 pr_info("Probe point is not specified.\n");
306 return -EINVAL;
307 }
308 if (isdigit(argv[1][0])) {
309 pr_info("probe point must be have a filename.\n");
310 return -EINVAL;
311 }
312 arg = strchr(argv[1], ':');
zhangwei(Jovi)fa440632013-06-13 14:21:51 +0800313 if (!arg) {
314 ret = -EINVAL;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530315 goto fail_address_parse;
zhangwei(Jovi)fa440632013-06-13 14:21:51 +0800316 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530317
318 *arg++ = '\0';
319 filename = argv[1];
320 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
321 if (ret)
322 goto fail_address_parse;
323
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530324 inode = igrab(path.dentry->d_inode);
Oleg Nesterov84d7ed72013-01-27 18:20:45 +0100325 path_put(&path);
326
Oleg Nesterov7e4e28c2013-01-28 17:08:47 +0100327 if (!inode || !S_ISREG(inode->i_mode)) {
Jovi Zhangd24d7db2012-07-18 18:16:44 +0800328 ret = -EINVAL;
329 goto fail_address_parse;
330 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530331
Oleg Nesterov84d7ed72013-01-27 18:20:45 +0100332 ret = kstrtoul(arg, 0, &offset);
333 if (ret)
334 goto fail_address_parse;
335
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530336 argc -= 2;
337 argv += 2;
338
339 /* setup a probe */
340 if (!event) {
Andy Shevchenkob2e902f2012-12-17 16:01:27 -0800341 char *tail;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530342 char *ptr;
343
Andy Shevchenkob2e902f2012-12-17 16:01:27 -0800344 tail = kstrdup(kbasename(filename), GFP_KERNEL);
345 if (!tail) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530346 ret = -ENOMEM;
347 goto fail_address_parse;
348 }
349
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530350 ptr = strpbrk(tail, ".-_");
351 if (ptr)
352 *ptr = '\0';
353
354 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
355 event = buf;
356 kfree(tail);
357 }
358
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100359 tu = alloc_trace_uprobe(group, event, argc, is_return);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530360 if (IS_ERR(tu)) {
361 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
362 ret = PTR_ERR(tu);
363 goto fail_address_parse;
364 }
365 tu->offset = offset;
366 tu->inode = inode;
367 tu->filename = kstrdup(filename, GFP_KERNEL);
368
369 if (!tu->filename) {
370 pr_info("Failed to allocate filename.\n");
371 ret = -ENOMEM;
372 goto error;
373 }
374
375 /* parse arguments */
376 ret = 0;
377 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Namhyung Kim14577c32013-07-03 15:42:53 +0900378 struct probe_arg *parg = &tu->tp.args[i];
379
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530380 /* Increment count for freeing args in error case */
Namhyung Kim14577c32013-07-03 15:42:53 +0900381 tu->tp.nr_args++;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530382
383 /* Parse argument name */
384 arg = strchr(argv[i], '=');
385 if (arg) {
386 *arg++ = '\0';
Namhyung Kim14577c32013-07-03 15:42:53 +0900387 parg->name = kstrdup(argv[i], GFP_KERNEL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530388 } else {
389 arg = argv[i];
390 /* If argument name is omitted, set "argN" */
391 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
Namhyung Kim14577c32013-07-03 15:42:53 +0900392 parg->name = kstrdup(buf, GFP_KERNEL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530393 }
394
Namhyung Kim14577c32013-07-03 15:42:53 +0900395 if (!parg->name) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530396 pr_info("Failed to allocate argument[%d] name.\n", i);
397 ret = -ENOMEM;
398 goto error;
399 }
400
Namhyung Kim14577c32013-07-03 15:42:53 +0900401 if (!is_good_name(parg->name)) {
402 pr_info("Invalid argument[%d] name: %s\n", i, parg->name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530403 ret = -EINVAL;
404 goto error;
405 }
406
Namhyung Kim14577c32013-07-03 15:42:53 +0900407 if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530408 pr_info("Argument[%d] name '%s' conflicts with "
409 "another field.\n", i, argv[i]);
410 ret = -EINVAL;
411 goto error;
412 }
413
414 /* Parse fetch argument */
Namhyung Kim14577c32013-07-03 15:42:53 +0900415 ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg,
416 false, false);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530417 if (ret) {
418 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
419 goto error;
420 }
421 }
422
423 ret = register_trace_uprobe(tu);
424 if (ret)
425 goto error;
426 return 0;
427
428error:
429 free_trace_uprobe(tu);
430 return ret;
431
432fail_address_parse:
433 if (inode)
434 iput(inode);
435
Jovi Zhangd24d7db2012-07-18 18:16:44 +0800436 pr_info("Failed to parse address or file.\n");
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530437
438 return ret;
439}
440
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400441static int cleanup_all_probes(void)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530442{
443 struct trace_uprobe *tu;
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400444 int ret = 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530445
446 mutex_lock(&uprobe_lock);
447 while (!list_empty(&uprobe_list)) {
448 tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400449 ret = unregister_trace_uprobe(tu);
450 if (ret)
451 break;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530452 }
453 mutex_unlock(&uprobe_lock);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400454 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530455}
456
457/* Probes listing interfaces */
458static void *probes_seq_start(struct seq_file *m, loff_t *pos)
459{
460 mutex_lock(&uprobe_lock);
461 return seq_list_start(&uprobe_list, *pos);
462}
463
464static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
465{
466 return seq_list_next(v, &uprobe_list, pos);
467}
468
469static void probes_seq_stop(struct seq_file *m, void *v)
470{
471 mutex_unlock(&uprobe_lock);
472}
473
474static int probes_seq_show(struct seq_file *m, void *v)
475{
476 struct trace_uprobe *tu = v;
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100477 char c = is_ret_probe(tu) ? 'r' : 'p';
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530478 int i;
479
Namhyung Kim14577c32013-07-03 15:42:53 +0900480 seq_printf(m, "%c:%s/%s", c, tu->tp.call.class->system, tu->tp.call.name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530481 seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset);
482
Namhyung Kim14577c32013-07-03 15:42:53 +0900483 for (i = 0; i < tu->tp.nr_args; i++)
484 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530485
486 seq_printf(m, "\n");
487 return 0;
488}
489
490static const struct seq_operations probes_seq_op = {
491 .start = probes_seq_start,
492 .next = probes_seq_next,
493 .stop = probes_seq_stop,
494 .show = probes_seq_show
495};
496
497static int probes_open(struct inode *inode, struct file *file)
498{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400499 int ret;
500
501 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
502 ret = cleanup_all_probes();
503 if (ret)
504 return ret;
505 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530506
507 return seq_open(file, &probes_seq_op);
508}
509
510static ssize_t probes_write(struct file *file, const char __user *buffer,
511 size_t count, loff_t *ppos)
512{
513 return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
514}
515
516static const struct file_operations uprobe_events_ops = {
517 .owner = THIS_MODULE,
518 .open = probes_open,
519 .read = seq_read,
520 .llseek = seq_lseek,
521 .release = seq_release,
522 .write = probes_write,
523};
524
525/* Probes profiling interfaces */
526static int probes_profile_seq_show(struct seq_file *m, void *v)
527{
528 struct trace_uprobe *tu = v;
529
Namhyung Kim14577c32013-07-03 15:42:53 +0900530 seq_printf(m, " %s %-44s %15lu\n", tu->filename, tu->tp.call.name, tu->nhit);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530531 return 0;
532}
533
534static const struct seq_operations profile_seq_op = {
535 .start = probes_seq_start,
536 .next = probes_seq_next,
537 .stop = probes_seq_stop,
538 .show = probes_profile_seq_show
539};
540
541static int profile_open(struct inode *inode, struct file *file)
542{
543 return seq_open(file, &profile_seq_op);
544}
545
546static const struct file_operations uprobe_profile_ops = {
547 .owner = THIS_MODULE,
548 .open = profile_open,
549 .read = seq_read,
550 .llseek = seq_lseek,
551 .release = seq_release,
552};
553
Oleg Nesterova51cc602013-03-30 18:02:12 +0100554static void uprobe_trace_print(struct trace_uprobe *tu,
555 unsigned long func, struct pt_regs *regs)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530556{
557 struct uprobe_trace_entry_head *entry;
558 struct ring_buffer_event *event;
559 struct ring_buffer *buffer;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100560 void *data;
Oleg Nesterov0e3853d2013-03-28 19:19:11 +0100561 int size, i;
Namhyung Kim14577c32013-07-03 15:42:53 +0900562 struct ftrace_event_call *call = &tu->tp.call;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530563
Oleg Nesterov393a7362013-03-30 18:46:22 +0100564 size = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530565 event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
Namhyung Kim14577c32013-07-03 15:42:53 +0900566 size + tu->tp.size, 0, 0);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530567 if (!event)
Oleg Nesterova51cc602013-03-30 18:02:12 +0100568 return;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530569
570 entry = ring_buffer_event_data(event);
Oleg Nesterov393a7362013-03-30 18:46:22 +0100571 if (is_ret_probe(tu)) {
572 entry->vaddr[0] = func;
573 entry->vaddr[1] = instruction_pointer(regs);
574 data = DATAOF_TRACE_ENTRY(entry, true);
575 } else {
576 entry->vaddr[0] = instruction_pointer(regs);
577 data = DATAOF_TRACE_ENTRY(entry, false);
578 }
579
Namhyung Kim14577c32013-07-03 15:42:53 +0900580 for (i = 0; i < tu->tp.nr_args; i++) {
581 call_fetch(&tu->tp.args[i].fetch, regs,
582 data + tu->tp.args[i].offset);
583 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530584
Tom Zanussif306cc82013-10-24 08:34:17 -0500585 if (!call_filter_check_discard(call, entry, buffer, event))
Oleg Nesterov0e3853d2013-03-28 19:19:11 +0100586 trace_buffer_unlock_commit(buffer, event, 0, 0);
Oleg Nesterova51cc602013-03-30 18:02:12 +0100587}
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100588
Oleg Nesterova51cc602013-03-30 18:02:12 +0100589/* uprobe handler */
590static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
591{
Oleg Nesterov393a7362013-03-30 18:46:22 +0100592 if (!is_ret_probe(tu))
593 uprobe_trace_print(tu, 0, regs);
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100594 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530595}
596
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100597static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
598 struct pt_regs *regs)
599{
600 uprobe_trace_print(tu, func, regs);
601}
602
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530603/* Event entry printers */
604static enum print_line_t
605print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
606{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100607 struct uprobe_trace_entry_head *entry;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530608 struct trace_seq *s = &iter->seq;
609 struct trace_uprobe *tu;
610 u8 *data;
611 int i;
612
Oleg Nesterov457d1772013-03-29 18:26:51 +0100613 entry = (struct uprobe_trace_entry_head *)iter->ent;
Namhyung Kim14577c32013-07-03 15:42:53 +0900614 tu = container_of(event, struct trace_uprobe, tp.call.event);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530615
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100616 if (is_ret_probe(tu)) {
Namhyung Kim14577c32013-07-03 15:42:53 +0900617 if (!trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", tu->tp.call.name,
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100618 entry->vaddr[1], entry->vaddr[0]))
619 goto partial;
620 data = DATAOF_TRACE_ENTRY(entry, true);
621 } else {
Namhyung Kim14577c32013-07-03 15:42:53 +0900622 if (!trace_seq_printf(s, "%s: (0x%lx)", tu->tp.call.name,
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100623 entry->vaddr[0]))
624 goto partial;
625 data = DATAOF_TRACE_ENTRY(entry, false);
626 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530627
Namhyung Kim14577c32013-07-03 15:42:53 +0900628 for (i = 0; i < tu->tp.nr_args; i++) {
629 struct probe_arg *parg = &tu->tp.args[i];
630
631 if (!parg->type->print(s, parg->name, data + parg->offset, entry))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530632 goto partial;
633 }
634
635 if (trace_seq_puts(s, "\n"))
636 return TRACE_TYPE_HANDLED;
637
638partial:
639 return TRACE_TYPE_PARTIAL_LINE;
640}
641
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100642typedef bool (*filter_func_t)(struct uprobe_consumer *self,
643 enum uprobe_filter_ctx ctx,
644 struct mm_struct *mm);
645
646static int
647probe_event_enable(struct trace_uprobe *tu, int flag, filter_func_t filter)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530648{
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530649 int ret = 0;
650
Namhyung Kim14577c32013-07-03 15:42:53 +0900651 if (trace_probe_is_enabled(&tu->tp))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530652 return -EINTR;
653
Oleg Nesterov736288b2013-02-03 20:58:35 +0100654 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
655
Namhyung Kim14577c32013-07-03 15:42:53 +0900656 tu->tp.flags |= flag;
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100657 tu->consumer.filter = filter;
Oleg Nesterova932b732013-01-31 19:47:23 +0100658 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
659 if (ret)
Namhyung Kim14577c32013-07-03 15:42:53 +0900660 tu->tp.flags &= ~flag;
Oleg Nesterov41618242013-01-27 18:36:24 +0100661
662 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530663}
664
665static void probe_event_disable(struct trace_uprobe *tu, int flag)
666{
Namhyung Kim14577c32013-07-03 15:42:53 +0900667 if (!trace_probe_is_enabled(&tu->tp))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530668 return;
669
Oleg Nesterov736288b2013-02-03 20:58:35 +0100670 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
671
Oleg Nesterova932b732013-01-31 19:47:23 +0100672 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
Namhyung Kim14577c32013-07-03 15:42:53 +0900673 tu->tp.flags &= ~flag;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530674}
675
676static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
677{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100678 int ret, i, size;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530679 struct uprobe_trace_entry_head field;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100680 struct trace_uprobe *tu = event_call->data;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530681
Oleg Nesterov4d1298e2013-03-30 19:23:15 +0100682 if (is_ret_probe(tu)) {
683 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
684 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
685 size = SIZEOF_TRACE_ENTRY(true);
686 } else {
687 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
688 size = SIZEOF_TRACE_ENTRY(false);
689 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530690 /* Set argument names as fields */
Namhyung Kim14577c32013-07-03 15:42:53 +0900691 for (i = 0; i < tu->tp.nr_args; i++) {
692 struct probe_arg *parg = &tu->tp.args[i];
693
694 ret = trace_define_field(event_call, parg->type->fmttype,
695 parg->name, size + parg->offset,
696 parg->type->size, parg->type->is_signed,
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530697 FILTER_OTHER);
698
699 if (ret)
700 return ret;
701 }
702 return 0;
703}
704
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530705#ifdef CONFIG_PERF_EVENTS
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100706static bool
707__uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
708{
709 struct perf_event *event;
710
711 if (filter->nr_systemwide)
712 return true;
713
714 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
715 if (event->hw.tp_target->mm == mm)
716 return true;
717 }
718
719 return false;
720}
721
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100722static inline bool
723uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
724{
725 return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm);
726}
727
Oleg Nesterov736288b2013-02-03 20:58:35 +0100728static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
729{
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100730 bool done;
731
Oleg Nesterov736288b2013-02-03 20:58:35 +0100732 write_lock(&tu->filter.rwlock);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100733 if (event->hw.tp_target) {
734 /*
735 * event->parent != NULL means copy_process(), we can avoid
736 * uprobe_apply(). current->mm must be probed and we can rely
737 * on dup_mmap() which preserves the already installed bp's.
738 *
739 * attr.enable_on_exec means that exec/mmap will install the
740 * breakpoints we need.
741 */
742 done = tu->filter.nr_systemwide ||
743 event->parent || event->attr.enable_on_exec ||
744 uprobe_filter_event(tu, event);
Oleg Nesterov736288b2013-02-03 20:58:35 +0100745 list_add(&event->hw.tp_list, &tu->filter.perf_events);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100746 } else {
747 done = tu->filter.nr_systemwide;
Oleg Nesterov736288b2013-02-03 20:58:35 +0100748 tu->filter.nr_systemwide++;
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100749 }
Oleg Nesterov736288b2013-02-03 20:58:35 +0100750 write_unlock(&tu->filter.rwlock);
751
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100752 if (!done)
753 uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100754
Oleg Nesterov736288b2013-02-03 20:58:35 +0100755 return 0;
756}
757
758static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
759{
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100760 bool done;
761
Oleg Nesterov736288b2013-02-03 20:58:35 +0100762 write_lock(&tu->filter.rwlock);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100763 if (event->hw.tp_target) {
Oleg Nesterov736288b2013-02-03 20:58:35 +0100764 list_del(&event->hw.tp_list);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100765 done = tu->filter.nr_systemwide ||
766 (event->hw.tp_target->flags & PF_EXITING) ||
767 uprobe_filter_event(tu, event);
768 } else {
Oleg Nesterov736288b2013-02-03 20:58:35 +0100769 tu->filter.nr_systemwide--;
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100770 done = tu->filter.nr_systemwide;
771 }
Oleg Nesterov736288b2013-02-03 20:58:35 +0100772 write_unlock(&tu->filter.rwlock);
773
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100774 if (!done)
775 uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100776
Oleg Nesterov736288b2013-02-03 20:58:35 +0100777 return 0;
778}
779
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100780static bool uprobe_perf_filter(struct uprobe_consumer *uc,
781 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
782{
783 struct trace_uprobe *tu;
784 int ret;
785
786 tu = container_of(uc, struct trace_uprobe, consumer);
787 read_lock(&tu->filter.rwlock);
788 ret = __uprobe_perf_filter(&tu->filter, mm);
789 read_unlock(&tu->filter.rwlock);
790
791 return ret;
792}
793
Oleg Nesterova51cc602013-03-30 18:02:12 +0100794static void uprobe_perf_print(struct trace_uprobe *tu,
795 unsigned long func, struct pt_regs *regs)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530796{
Namhyung Kim14577c32013-07-03 15:42:53 +0900797 struct ftrace_event_call *call = &tu->tp.call;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530798 struct uprobe_trace_entry_head *entry;
799 struct hlist_head *head;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100800 void *data;
801 int size, rctx, i;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530802
Oleg Nesterov393a7362013-03-30 18:46:22 +0100803 size = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
Namhyung Kim14577c32013-07-03 15:42:53 +0900804 size = ALIGN(size + tu->tp.size + sizeof(u32), sizeof(u64)) - sizeof(u32);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530805
806 preempt_disable();
Oleg Nesterov515619f2013-04-13 15:36:49 +0200807 head = this_cpu_ptr(call->perf_events);
808 if (hlist_empty(head))
809 goto out;
810
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530811 entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
812 if (!entry)
813 goto out;
814
Oleg Nesterov393a7362013-03-30 18:46:22 +0100815 if (is_ret_probe(tu)) {
816 entry->vaddr[0] = func;
Oleg Nesterov32520b22013-04-10 16:25:49 +0200817 entry->vaddr[1] = instruction_pointer(regs);
Oleg Nesterov393a7362013-03-30 18:46:22 +0100818 data = DATAOF_TRACE_ENTRY(entry, true);
819 } else {
Oleg Nesterov32520b22013-04-10 16:25:49 +0200820 entry->vaddr[0] = instruction_pointer(regs);
Oleg Nesterov393a7362013-03-30 18:46:22 +0100821 data = DATAOF_TRACE_ENTRY(entry, false);
822 }
823
Namhyung Kim14577c32013-07-03 15:42:53 +0900824 for (i = 0; i < tu->tp.nr_args; i++) {
825 struct probe_arg *parg = &tu->tp.args[i];
826
827 call_fetch(&parg->fetch, regs, data + parg->offset);
828 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530829
Oleg Nesterov32520b22013-04-10 16:25:49 +0200830 perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530831 out:
832 preempt_enable();
Oleg Nesterova51cc602013-03-30 18:02:12 +0100833}
834
835/* uprobe profile handler */
836static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
837{
838 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
839 return UPROBE_HANDLER_REMOVE;
840
Oleg Nesterov393a7362013-03-30 18:46:22 +0100841 if (!is_ret_probe(tu))
842 uprobe_perf_print(tu, 0, regs);
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100843 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530844}
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100845
846static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
847 struct pt_regs *regs)
848{
849 uprobe_perf_print(tu, func, regs);
850}
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530851#endif /* CONFIG_PERF_EVENTS */
852
853static
854int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data)
855{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100856 struct trace_uprobe *tu = event->data;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530857
858 switch (type) {
859 case TRACE_REG_REGISTER:
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100860 return probe_event_enable(tu, TP_FLAG_TRACE, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530861
862 case TRACE_REG_UNREGISTER:
863 probe_event_disable(tu, TP_FLAG_TRACE);
864 return 0;
865
866#ifdef CONFIG_PERF_EVENTS
867 case TRACE_REG_PERF_REGISTER:
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100868 return probe_event_enable(tu, TP_FLAG_PROFILE, uprobe_perf_filter);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530869
870 case TRACE_REG_PERF_UNREGISTER:
871 probe_event_disable(tu, TP_FLAG_PROFILE);
872 return 0;
Oleg Nesterov736288b2013-02-03 20:58:35 +0100873
874 case TRACE_REG_PERF_OPEN:
875 return uprobe_perf_open(tu, data);
876
877 case TRACE_REG_PERF_CLOSE:
878 return uprobe_perf_close(tu, data);
879
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530880#endif
881 default:
882 return 0;
883 }
884 return 0;
885}
886
887static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
888{
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530889 struct trace_uprobe *tu;
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100890 int ret = 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530891
Oleg Nesterova932b732013-01-31 19:47:23 +0100892 tu = container_of(con, struct trace_uprobe, consumer);
Oleg Nesterov1b47aef2013-01-31 19:55:27 +0100893 tu->nhit++;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530894
Namhyung Kim14577c32013-07-03 15:42:53 +0900895 if (tu->tp.flags & TP_FLAG_TRACE)
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100896 ret |= uprobe_trace_func(tu, regs);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530897
898#ifdef CONFIG_PERF_EVENTS
Namhyung Kim14577c32013-07-03 15:42:53 +0900899 if (tu->tp.flags & TP_FLAG_PROFILE)
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100900 ret |= uprobe_perf_func(tu, regs);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530901#endif
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100902 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530903}
904
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100905static int uretprobe_dispatcher(struct uprobe_consumer *con,
906 unsigned long func, struct pt_regs *regs)
907{
908 struct trace_uprobe *tu;
909
910 tu = container_of(con, struct trace_uprobe, consumer);
911
Namhyung Kim14577c32013-07-03 15:42:53 +0900912 if (tu->tp.flags & TP_FLAG_TRACE)
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100913 uretprobe_trace_func(tu, func, regs);
914
915#ifdef CONFIG_PERF_EVENTS
Namhyung Kim14577c32013-07-03 15:42:53 +0900916 if (tu->tp.flags & TP_FLAG_PROFILE)
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100917 uretprobe_perf_func(tu, func, regs);
918#endif
919 return 0;
920}
921
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530922static struct trace_event_functions uprobe_funcs = {
923 .trace = print_uprobe_event
924};
925
926static int register_uprobe_event(struct trace_uprobe *tu)
927{
Namhyung Kim14577c32013-07-03 15:42:53 +0900928 struct ftrace_event_call *call = &tu->tp.call;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530929 int ret;
930
931 /* Initialize ftrace_event_call */
932 INIT_LIST_HEAD(&call->class->fields);
933 call->event.funcs = &uprobe_funcs;
934 call->class->define_fields = uprobe_event_define_fields;
935
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900936 if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530937 return -ENOMEM;
938
939 ret = register_ftrace_event(&call->event);
940 if (!ret) {
941 kfree(call->print_fmt);
942 return -ENODEV;
943 }
944 call->flags = 0;
945 call->class->reg = trace_uprobe_register;
946 call->data = tu;
947 ret = trace_add_event_call(call);
948
949 if (ret) {
950 pr_info("Failed to register uprobe event: %s\n", call->name);
951 kfree(call->print_fmt);
952 unregister_ftrace_event(&call->event);
953 }
954
955 return ret;
956}
957
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400958static int unregister_uprobe_event(struct trace_uprobe *tu)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530959{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400960 int ret;
961
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530962 /* tu->event is unregistered in trace_remove_event_call() */
Namhyung Kim14577c32013-07-03 15:42:53 +0900963 ret = trace_remove_event_call(&tu->tp.call);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400964 if (ret)
965 return ret;
Namhyung Kim14577c32013-07-03 15:42:53 +0900966 kfree(tu->tp.call.print_fmt);
967 tu->tp.call.print_fmt = NULL;
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400968 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530969}
970
971/* Make a trace interface for controling probe points */
972static __init int init_uprobe_trace(void)
973{
974 struct dentry *d_tracer;
975
976 d_tracer = tracing_init_dentry();
977 if (!d_tracer)
978 return 0;
979
980 trace_create_file("uprobe_events", 0644, d_tracer,
981 NULL, &uprobe_events_ops);
982 /* Profile interface */
983 trace_create_file("uprobe_profile", 0444, d_tracer,
984 NULL, &uprobe_profile_ops);
985 return 0;
986}
987
988fs_initcall(init_uprobe_trace);