blob: 49b400368bfdcfa674e3722cd0e6fbe4dcb8eeee [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;
54 struct ftrace_event_class class;
55 struct ftrace_event_call call;
Oleg Nesterov736288b2013-02-03 20:58:35 +010056 struct trace_uprobe_filter filter;
Oleg Nesterova932b732013-01-31 19:47:23 +010057 struct uprobe_consumer consumer;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053058 struct inode *inode;
59 char *filename;
60 unsigned long offset;
61 unsigned long nhit;
62 unsigned int flags; /* For TP_FLAG_* */
63 ssize_t size; /* trace entry size */
64 unsigned int nr_args;
65 struct probe_arg args[];
66};
67
68#define SIZEOF_TRACE_UPROBE(n) \
69 (offsetof(struct trace_uprobe, args) + \
70 (sizeof(struct probe_arg) * (n)))
71
72static int register_uprobe_event(struct trace_uprobe *tu);
73static void unregister_uprobe_event(struct trace_uprobe *tu);
74
75static DEFINE_MUTEX(uprobe_lock);
76static LIST_HEAD(uprobe_list);
77
78static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
79
Oleg Nesterov736288b2013-02-03 20:58:35 +010080static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
81{
82 rwlock_init(&filter->rwlock);
83 filter->nr_systemwide = 0;
84 INIT_LIST_HEAD(&filter->perf_events);
85}
86
87static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
88{
89 return !filter->nr_systemwide && list_empty(&filter->perf_events);
90}
91
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053092/*
93 * Allocate new trace_uprobe and initialize it (including uprobes).
94 */
95static struct trace_uprobe *
96alloc_trace_uprobe(const char *group, const char *event, int nargs)
97{
98 struct trace_uprobe *tu;
99
100 if (!event || !is_good_name(event))
101 return ERR_PTR(-EINVAL);
102
103 if (!group || !is_good_name(group))
104 return ERR_PTR(-EINVAL);
105
106 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
107 if (!tu)
108 return ERR_PTR(-ENOMEM);
109
110 tu->call.class = &tu->class;
111 tu->call.name = kstrdup(event, GFP_KERNEL);
112 if (!tu->call.name)
113 goto error;
114
115 tu->class.system = kstrdup(group, GFP_KERNEL);
116 if (!tu->class.system)
117 goto error;
118
119 INIT_LIST_HEAD(&tu->list);
Oleg Nesterova932b732013-01-31 19:47:23 +0100120 tu->consumer.handler = uprobe_dispatcher;
Oleg Nesterov736288b2013-02-03 20:58:35 +0100121 init_trace_uprobe_filter(&tu->filter);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530122 return tu;
123
124error:
125 kfree(tu->call.name);
126 kfree(tu);
127
128 return ERR_PTR(-ENOMEM);
129}
130
131static void free_trace_uprobe(struct trace_uprobe *tu)
132{
133 int i;
134
135 for (i = 0; i < tu->nr_args; i++)
136 traceprobe_free_probe_arg(&tu->args[i]);
137
138 iput(tu->inode);
139 kfree(tu->call.class->system);
140 kfree(tu->call.name);
141 kfree(tu->filename);
142 kfree(tu);
143}
144
145static struct trace_uprobe *find_probe_event(const char *event, const char *group)
146{
147 struct trace_uprobe *tu;
148
149 list_for_each_entry(tu, &uprobe_list, list)
150 if (strcmp(tu->call.name, event) == 0 &&
151 strcmp(tu->call.class->system, group) == 0)
152 return tu;
153
154 return NULL;
155}
156
157/* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
158static void unregister_trace_uprobe(struct trace_uprobe *tu)
159{
160 list_del(&tu->list);
161 unregister_uprobe_event(tu);
162 free_trace_uprobe(tu);
163}
164
165/* Register a trace_uprobe and probe_event */
166static int register_trace_uprobe(struct trace_uprobe *tu)
167{
168 struct trace_uprobe *old_tp;
169 int ret;
170
171 mutex_lock(&uprobe_lock);
172
173 /* register as an event */
174 old_tp = find_probe_event(tu->call.name, tu->call.class->system);
175 if (old_tp)
176 /* delete old event */
177 unregister_trace_uprobe(old_tp);
178
179 ret = register_uprobe_event(tu);
180 if (ret) {
181 pr_warning("Failed to register probe event(%d)\n", ret);
182 goto end;
183 }
184
185 list_add_tail(&tu->list, &uprobe_list);
186
187end:
188 mutex_unlock(&uprobe_lock);
189
190 return ret;
191}
192
193/*
194 * Argument syntax:
195 * - Add uprobe: p[:[GRP/]EVENT] PATH:SYMBOL[+offs] [FETCHARGS]
196 *
197 * - Remove uprobe: -:[GRP/]EVENT
198 */
199static int create_trace_uprobe(int argc, char **argv)
200{
201 struct trace_uprobe *tu;
202 struct inode *inode;
203 char *arg, *event, *group, *filename;
204 char buf[MAX_EVENT_NAME_LEN];
205 struct path path;
206 unsigned long offset;
207 bool is_delete;
208 int i, ret;
209
210 inode = NULL;
211 ret = 0;
212 is_delete = false;
213 event = NULL;
214 group = NULL;
215
216 /* argc must be >= 1 */
217 if (argv[0][0] == '-')
218 is_delete = true;
219 else if (argv[0][0] != 'p') {
Jovi Zhang0d13ac92012-07-18 17:51:26 +0800220 pr_info("Probe definition must be started with 'p' or '-'.\n");
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530221 return -EINVAL;
222 }
223
224 if (argv[0][1] == ':') {
225 event = &argv[0][2];
226 arg = strchr(event, '/');
227
228 if (arg) {
229 group = event;
230 event = arg + 1;
231 event[-1] = '\0';
232
233 if (strlen(group) == 0) {
234 pr_info("Group name is not specified\n");
235 return -EINVAL;
236 }
237 }
238 if (strlen(event) == 0) {
239 pr_info("Event name is not specified\n");
240 return -EINVAL;
241 }
242 }
243 if (!group)
244 group = UPROBE_EVENT_SYSTEM;
245
246 if (is_delete) {
247 if (!event) {
248 pr_info("Delete command needs an event name.\n");
249 return -EINVAL;
250 }
251 mutex_lock(&uprobe_lock);
252 tu = find_probe_event(event, group);
253
254 if (!tu) {
255 mutex_unlock(&uprobe_lock);
256 pr_info("Event %s/%s doesn't exist.\n", group, event);
257 return -ENOENT;
258 }
259 /* delete an event */
260 unregister_trace_uprobe(tu);
261 mutex_unlock(&uprobe_lock);
262 return 0;
263 }
264
265 if (argc < 2) {
266 pr_info("Probe point is not specified.\n");
267 return -EINVAL;
268 }
269 if (isdigit(argv[1][0])) {
270 pr_info("probe point must be have a filename.\n");
271 return -EINVAL;
272 }
273 arg = strchr(argv[1], ':');
274 if (!arg)
275 goto fail_address_parse;
276
277 *arg++ = '\0';
278 filename = argv[1];
279 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
280 if (ret)
281 goto fail_address_parse;
282
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530283 inode = igrab(path.dentry->d_inode);
Oleg Nesterov84d7ed72013-01-27 18:20:45 +0100284 path_put(&path);
285
Oleg Nesterov7e4e28c2013-01-28 17:08:47 +0100286 if (!inode || !S_ISREG(inode->i_mode)) {
Jovi Zhangd24d7db2012-07-18 18:16:44 +0800287 ret = -EINVAL;
288 goto fail_address_parse;
289 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530290
Oleg Nesterov84d7ed72013-01-27 18:20:45 +0100291 ret = kstrtoul(arg, 0, &offset);
292 if (ret)
293 goto fail_address_parse;
294
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530295 argc -= 2;
296 argv += 2;
297
298 /* setup a probe */
299 if (!event) {
Andy Shevchenkob2e902f2012-12-17 16:01:27 -0800300 char *tail;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530301 char *ptr;
302
Andy Shevchenkob2e902f2012-12-17 16:01:27 -0800303 tail = kstrdup(kbasename(filename), GFP_KERNEL);
304 if (!tail) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530305 ret = -ENOMEM;
306 goto fail_address_parse;
307 }
308
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530309 ptr = strpbrk(tail, ".-_");
310 if (ptr)
311 *ptr = '\0';
312
313 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
314 event = buf;
315 kfree(tail);
316 }
317
318 tu = alloc_trace_uprobe(group, event, argc);
319 if (IS_ERR(tu)) {
320 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
321 ret = PTR_ERR(tu);
322 goto fail_address_parse;
323 }
324 tu->offset = offset;
325 tu->inode = inode;
326 tu->filename = kstrdup(filename, GFP_KERNEL);
327
328 if (!tu->filename) {
329 pr_info("Failed to allocate filename.\n");
330 ret = -ENOMEM;
331 goto error;
332 }
333
334 /* parse arguments */
335 ret = 0;
336 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
337 /* Increment count for freeing args in error case */
338 tu->nr_args++;
339
340 /* Parse argument name */
341 arg = strchr(argv[i], '=');
342 if (arg) {
343 *arg++ = '\0';
344 tu->args[i].name = kstrdup(argv[i], GFP_KERNEL);
345 } else {
346 arg = argv[i];
347 /* If argument name is omitted, set "argN" */
348 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
349 tu->args[i].name = kstrdup(buf, GFP_KERNEL);
350 }
351
352 if (!tu->args[i].name) {
353 pr_info("Failed to allocate argument[%d] name.\n", i);
354 ret = -ENOMEM;
355 goto error;
356 }
357
358 if (!is_good_name(tu->args[i].name)) {
359 pr_info("Invalid argument[%d] name: %s\n", i, tu->args[i].name);
360 ret = -EINVAL;
361 goto error;
362 }
363
364 if (traceprobe_conflict_field_name(tu->args[i].name, tu->args, i)) {
365 pr_info("Argument[%d] name '%s' conflicts with "
366 "another field.\n", i, argv[i]);
367 ret = -EINVAL;
368 goto error;
369 }
370
371 /* Parse fetch argument */
372 ret = traceprobe_parse_probe_arg(arg, &tu->size, &tu->args[i], false, false);
373 if (ret) {
374 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
375 goto error;
376 }
377 }
378
379 ret = register_trace_uprobe(tu);
380 if (ret)
381 goto error;
382 return 0;
383
384error:
385 free_trace_uprobe(tu);
386 return ret;
387
388fail_address_parse:
389 if (inode)
390 iput(inode);
391
Jovi Zhangd24d7db2012-07-18 18:16:44 +0800392 pr_info("Failed to parse address or file.\n");
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530393
394 return ret;
395}
396
397static void cleanup_all_probes(void)
398{
399 struct trace_uprobe *tu;
400
401 mutex_lock(&uprobe_lock);
402 while (!list_empty(&uprobe_list)) {
403 tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
404 unregister_trace_uprobe(tu);
405 }
406 mutex_unlock(&uprobe_lock);
407}
408
409/* Probes listing interfaces */
410static void *probes_seq_start(struct seq_file *m, loff_t *pos)
411{
412 mutex_lock(&uprobe_lock);
413 return seq_list_start(&uprobe_list, *pos);
414}
415
416static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
417{
418 return seq_list_next(v, &uprobe_list, pos);
419}
420
421static void probes_seq_stop(struct seq_file *m, void *v)
422{
423 mutex_unlock(&uprobe_lock);
424}
425
426static int probes_seq_show(struct seq_file *m, void *v)
427{
428 struct trace_uprobe *tu = v;
429 int i;
430
431 seq_printf(m, "p:%s/%s", tu->call.class->system, tu->call.name);
432 seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset);
433
434 for (i = 0; i < tu->nr_args; i++)
435 seq_printf(m, " %s=%s", tu->args[i].name, tu->args[i].comm);
436
437 seq_printf(m, "\n");
438 return 0;
439}
440
441static const struct seq_operations probes_seq_op = {
442 .start = probes_seq_start,
443 .next = probes_seq_next,
444 .stop = probes_seq_stop,
445 .show = probes_seq_show
446};
447
448static int probes_open(struct inode *inode, struct file *file)
449{
450 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
451 cleanup_all_probes();
452
453 return seq_open(file, &probes_seq_op);
454}
455
456static ssize_t probes_write(struct file *file, const char __user *buffer,
457 size_t count, loff_t *ppos)
458{
459 return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
460}
461
462static const struct file_operations uprobe_events_ops = {
463 .owner = THIS_MODULE,
464 .open = probes_open,
465 .read = seq_read,
466 .llseek = seq_lseek,
467 .release = seq_release,
468 .write = probes_write,
469};
470
471/* Probes profiling interfaces */
472static int probes_profile_seq_show(struct seq_file *m, void *v)
473{
474 struct trace_uprobe *tu = v;
475
476 seq_printf(m, " %s %-44s %15lu\n", tu->filename, tu->call.name, tu->nhit);
477 return 0;
478}
479
480static const struct seq_operations profile_seq_op = {
481 .start = probes_seq_start,
482 .next = probes_seq_next,
483 .stop = probes_seq_stop,
484 .show = probes_profile_seq_show
485};
486
487static int profile_open(struct inode *inode, struct file *file)
488{
489 return seq_open(file, &profile_seq_op);
490}
491
492static const struct file_operations uprobe_profile_ops = {
493 .owner = THIS_MODULE,
494 .open = profile_open,
495 .read = seq_read,
496 .llseek = seq_lseek,
497 .release = seq_release,
498};
499
500/* uprobe handler */
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100501static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530502{
503 struct uprobe_trace_entry_head *entry;
504 struct ring_buffer_event *event;
505 struct ring_buffer *buffer;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100506 void *data;
Oleg Nesterov0e3853d2013-03-28 19:19:11 +0100507 int size, i;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530508 struct ftrace_event_call *call = &tu->call;
509
Oleg Nesterov457d1772013-03-29 18:26:51 +0100510 size = SIZEOF_TRACE_ENTRY(false) + tu->size;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530511 event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
Oleg Nesterov0e3853d2013-03-28 19:19:11 +0100512 size, 0, 0);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530513 if (!event)
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100514 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530515
516 entry = ring_buffer_event_data(event);
Oleg Nesterov457d1772013-03-29 18:26:51 +0100517 entry->vaddr[0] = instruction_pointer(regs);
518 data = DATAOF_TRACE_ENTRY(entry, false);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530519 for (i = 0; i < tu->nr_args; i++)
520 call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
521
522 if (!filter_current_check_discard(buffer, call, entry, event))
Oleg Nesterov0e3853d2013-03-28 19:19:11 +0100523 trace_buffer_unlock_commit(buffer, event, 0, 0);
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100524
525 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530526}
527
528/* Event entry printers */
529static enum print_line_t
530print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
531{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100532 struct uprobe_trace_entry_head *entry;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530533 struct trace_seq *s = &iter->seq;
534 struct trace_uprobe *tu;
535 u8 *data;
536 int i;
537
Oleg Nesterov457d1772013-03-29 18:26:51 +0100538 entry = (struct uprobe_trace_entry_head *)iter->ent;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530539 tu = container_of(event, struct trace_uprobe, call.event);
540
Oleg Nesterov457d1772013-03-29 18:26:51 +0100541 if (!trace_seq_printf(s, "%s: (0x%lx)", tu->call.name, entry->vaddr[0]))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530542 goto partial;
543
Oleg Nesterov457d1772013-03-29 18:26:51 +0100544 data = DATAOF_TRACE_ENTRY(entry, false);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530545 for (i = 0; i < tu->nr_args; i++) {
546 if (!tu->args[i].type->print(s, tu->args[i].name,
Oleg Nesterov457d1772013-03-29 18:26:51 +0100547 data + tu->args[i].offset, entry))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530548 goto partial;
549 }
550
551 if (trace_seq_puts(s, "\n"))
552 return TRACE_TYPE_HANDLED;
553
554partial:
555 return TRACE_TYPE_PARTIAL_LINE;
556}
557
Oleg Nesterovb64b0072013-01-31 19:15:30 +0100558static inline bool is_trace_uprobe_enabled(struct trace_uprobe *tu)
559{
560 return tu->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE);
561}
562
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100563typedef bool (*filter_func_t)(struct uprobe_consumer *self,
564 enum uprobe_filter_ctx ctx,
565 struct mm_struct *mm);
566
567static int
568probe_event_enable(struct trace_uprobe *tu, int flag, filter_func_t filter)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530569{
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530570 int ret = 0;
571
Oleg Nesterovb64b0072013-01-31 19:15:30 +0100572 if (is_trace_uprobe_enabled(tu))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530573 return -EINTR;
574
Oleg Nesterov736288b2013-02-03 20:58:35 +0100575 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
576
Oleg Nesterov41618242013-01-27 18:36:24 +0100577 tu->flags |= flag;
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100578 tu->consumer.filter = filter;
Oleg Nesterova932b732013-01-31 19:47:23 +0100579 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
580 if (ret)
Oleg Nesterov41618242013-01-27 18:36:24 +0100581 tu->flags &= ~flag;
Oleg Nesterov41618242013-01-27 18:36:24 +0100582
583 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530584}
585
586static void probe_event_disable(struct trace_uprobe *tu, int flag)
587{
Oleg Nesterovb64b0072013-01-31 19:15:30 +0100588 if (!is_trace_uprobe_enabled(tu))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530589 return;
590
Oleg Nesterov736288b2013-02-03 20:58:35 +0100591 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
592
Oleg Nesterova932b732013-01-31 19:47:23 +0100593 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530594 tu->flags &= ~flag;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530595}
596
597static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
598{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100599 int ret, i, size;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530600 struct uprobe_trace_entry_head field;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100601 struct trace_uprobe *tu = event_call->data;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530602
Oleg Nesterov457d1772013-03-29 18:26:51 +0100603 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
604 size = SIZEOF_TRACE_ENTRY(false);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530605 /* Set argument names as fields */
606 for (i = 0; i < tu->nr_args; i++) {
607 ret = trace_define_field(event_call, tu->args[i].type->fmttype,
608 tu->args[i].name,
Oleg Nesterov457d1772013-03-29 18:26:51 +0100609 size + tu->args[i].offset,
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530610 tu->args[i].type->size,
611 tu->args[i].type->is_signed,
612 FILTER_OTHER);
613
614 if (ret)
615 return ret;
616 }
617 return 0;
618}
619
620#define LEN_OR_ZERO (len ? len - pos : 0)
621static int __set_print_fmt(struct trace_uprobe *tu, char *buf, int len)
622{
623 const char *fmt, *arg;
624 int i;
625 int pos = 0;
626
627 fmt = "(%lx)";
628 arg = "REC->" FIELD_STRING_IP;
629
630 /* When len=0, we just calculate the needed length */
631
632 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
633
634 for (i = 0; i < tu->nr_args; i++) {
635 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
636 tu->args[i].name, tu->args[i].type->fmt);
637 }
638
639 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
640
641 for (i = 0; i < tu->nr_args; i++) {
642 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
643 tu->args[i].name);
644 }
645
646 return pos; /* return the length of print_fmt */
647}
648#undef LEN_OR_ZERO
649
650static int set_print_fmt(struct trace_uprobe *tu)
651{
652 char *print_fmt;
653 int len;
654
655 /* First: called with 0 length to calculate the needed length */
656 len = __set_print_fmt(tu, NULL, 0);
657 print_fmt = kmalloc(len + 1, GFP_KERNEL);
658 if (!print_fmt)
659 return -ENOMEM;
660
661 /* Second: actually write the @print_fmt */
662 __set_print_fmt(tu, print_fmt, len + 1);
663 tu->call.print_fmt = print_fmt;
664
665 return 0;
666}
667
668#ifdef CONFIG_PERF_EVENTS
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100669static bool
670__uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
671{
672 struct perf_event *event;
673
674 if (filter->nr_systemwide)
675 return true;
676
677 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
678 if (event->hw.tp_target->mm == mm)
679 return true;
680 }
681
682 return false;
683}
684
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100685static inline bool
686uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
687{
688 return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm);
689}
690
Oleg Nesterov736288b2013-02-03 20:58:35 +0100691static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
692{
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100693 bool done;
694
Oleg Nesterov736288b2013-02-03 20:58:35 +0100695 write_lock(&tu->filter.rwlock);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100696 if (event->hw.tp_target) {
697 /*
698 * event->parent != NULL means copy_process(), we can avoid
699 * uprobe_apply(). current->mm must be probed and we can rely
700 * on dup_mmap() which preserves the already installed bp's.
701 *
702 * attr.enable_on_exec means that exec/mmap will install the
703 * breakpoints we need.
704 */
705 done = tu->filter.nr_systemwide ||
706 event->parent || event->attr.enable_on_exec ||
707 uprobe_filter_event(tu, event);
Oleg Nesterov736288b2013-02-03 20:58:35 +0100708 list_add(&event->hw.tp_list, &tu->filter.perf_events);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100709 } else {
710 done = tu->filter.nr_systemwide;
Oleg Nesterov736288b2013-02-03 20:58:35 +0100711 tu->filter.nr_systemwide++;
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100712 }
Oleg Nesterov736288b2013-02-03 20:58:35 +0100713 write_unlock(&tu->filter.rwlock);
714
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100715 if (!done)
716 uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100717
Oleg Nesterov736288b2013-02-03 20:58:35 +0100718 return 0;
719}
720
721static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
722{
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100723 bool done;
724
Oleg Nesterov736288b2013-02-03 20:58:35 +0100725 write_lock(&tu->filter.rwlock);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100726 if (event->hw.tp_target) {
Oleg Nesterov736288b2013-02-03 20:58:35 +0100727 list_del(&event->hw.tp_list);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100728 done = tu->filter.nr_systemwide ||
729 (event->hw.tp_target->flags & PF_EXITING) ||
730 uprobe_filter_event(tu, event);
731 } else {
Oleg Nesterov736288b2013-02-03 20:58:35 +0100732 tu->filter.nr_systemwide--;
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100733 done = tu->filter.nr_systemwide;
734 }
Oleg Nesterov736288b2013-02-03 20:58:35 +0100735 write_unlock(&tu->filter.rwlock);
736
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +0100737 if (!done)
738 uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100739
Oleg Nesterov736288b2013-02-03 20:58:35 +0100740 return 0;
741}
742
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100743static bool uprobe_perf_filter(struct uprobe_consumer *uc,
744 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
745{
746 struct trace_uprobe *tu;
747 int ret;
748
749 tu = container_of(uc, struct trace_uprobe, consumer);
750 read_lock(&tu->filter.rwlock);
751 ret = __uprobe_perf_filter(&tu->filter, mm);
752 read_unlock(&tu->filter.rwlock);
753
754 return ret;
755}
756
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530757/* uprobe profile handler */
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100758static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530759{
760 struct ftrace_event_call *call = &tu->call;
761 struct uprobe_trace_entry_head *entry;
762 struct hlist_head *head;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100763 unsigned long ip;
764 void *data;
765 int size, rctx, i;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530766
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100767 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
768 return UPROBE_HANDLER_REMOVE;
769
Oleg Nesterov457d1772013-03-29 18:26:51 +0100770 size = SIZEOF_TRACE_ENTRY(false);
771 size = ALIGN(size + tu->size + sizeof(u32), sizeof(u64)) - sizeof(u32);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530772 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100773 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530774
775 preempt_disable();
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530776 entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
777 if (!entry)
778 goto out;
779
Oleg Nesterov457d1772013-03-29 18:26:51 +0100780 ip = instruction_pointer(regs);
781 entry->vaddr[0] = ip;
782 data = DATAOF_TRACE_ENTRY(entry, false);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530783 for (i = 0; i < tu->nr_args; i++)
784 call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
785
786 head = this_cpu_ptr(call->perf_events);
Oleg Nesterov457d1772013-03-29 18:26:51 +0100787 perf_trace_buf_submit(entry, size, rctx, ip, 1, regs, head, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530788 out:
789 preempt_enable();
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100790 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530791}
792#endif /* CONFIG_PERF_EVENTS */
793
794static
795int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data)
796{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100797 struct trace_uprobe *tu = event->data;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530798
799 switch (type) {
800 case TRACE_REG_REGISTER:
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100801 return probe_event_enable(tu, TP_FLAG_TRACE, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530802
803 case TRACE_REG_UNREGISTER:
804 probe_event_disable(tu, TP_FLAG_TRACE);
805 return 0;
806
807#ifdef CONFIG_PERF_EVENTS
808 case TRACE_REG_PERF_REGISTER:
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100809 return probe_event_enable(tu, TP_FLAG_PROFILE, uprobe_perf_filter);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530810
811 case TRACE_REG_PERF_UNREGISTER:
812 probe_event_disable(tu, TP_FLAG_PROFILE);
813 return 0;
Oleg Nesterov736288b2013-02-03 20:58:35 +0100814
815 case TRACE_REG_PERF_OPEN:
816 return uprobe_perf_open(tu, data);
817
818 case TRACE_REG_PERF_CLOSE:
819 return uprobe_perf_close(tu, data);
820
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530821#endif
822 default:
823 return 0;
824 }
825 return 0;
826}
827
828static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
829{
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530830 struct trace_uprobe *tu;
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100831 int ret = 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530832
Oleg Nesterova932b732013-01-31 19:47:23 +0100833 tu = container_of(con, struct trace_uprobe, consumer);
Oleg Nesterov1b47aef2013-01-31 19:55:27 +0100834 tu->nhit++;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530835
836 if (tu->flags & TP_FLAG_TRACE)
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100837 ret |= uprobe_trace_func(tu, regs);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530838
839#ifdef CONFIG_PERF_EVENTS
840 if (tu->flags & TP_FLAG_PROFILE)
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100841 ret |= uprobe_perf_func(tu, regs);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530842#endif
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100843 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530844}
845
846static struct trace_event_functions uprobe_funcs = {
847 .trace = print_uprobe_event
848};
849
850static int register_uprobe_event(struct trace_uprobe *tu)
851{
852 struct ftrace_event_call *call = &tu->call;
853 int ret;
854
855 /* Initialize ftrace_event_call */
856 INIT_LIST_HEAD(&call->class->fields);
857 call->event.funcs = &uprobe_funcs;
858 call->class->define_fields = uprobe_event_define_fields;
859
860 if (set_print_fmt(tu) < 0)
861 return -ENOMEM;
862
863 ret = register_ftrace_event(&call->event);
864 if (!ret) {
865 kfree(call->print_fmt);
866 return -ENODEV;
867 }
868 call->flags = 0;
869 call->class->reg = trace_uprobe_register;
870 call->data = tu;
871 ret = trace_add_event_call(call);
872
873 if (ret) {
874 pr_info("Failed to register uprobe event: %s\n", call->name);
875 kfree(call->print_fmt);
876 unregister_ftrace_event(&call->event);
877 }
878
879 return ret;
880}
881
882static void unregister_uprobe_event(struct trace_uprobe *tu)
883{
884 /* tu->event is unregistered in trace_remove_event_call() */
885 trace_remove_event_call(&tu->call);
886 kfree(tu->call.print_fmt);
887 tu->call.print_fmt = NULL;
888}
889
890/* Make a trace interface for controling probe points */
891static __init int init_uprobe_trace(void)
892{
893 struct dentry *d_tracer;
894
895 d_tracer = tracing_init_dentry();
896 if (!d_tracer)
897 return 0;
898
899 trace_create_file("uprobe_events", 0644, d_tracer,
900 NULL, &uprobe_events_ops);
901 /* Profile interface */
902 trace_create_file("uprobe_profile", 0444, d_tracer,
903 NULL, &uprobe_profile_ops);
904 return 0;
905}
906
907fs_initcall(init_uprobe_trace);