Blame view

kernel/trace/trace_uprobe.c 23.3 KB
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  /*
   * uprobes-based tracing events
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   *
   * Copyright (C) IBM Corporation, 2010-2012
   * Author:	Srikar Dronamraju <srikar@linux.vnet.ibm.com>
   */
  
  #include <linux/module.h>
  #include <linux/uaccess.h>
  #include <linux/uprobes.h>
  #include <linux/namei.h>
b2e902f02   Andy Shevchenko   trace: use kbasen...
25
  #include <linux/string.h>
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
26
27
28
29
  
  #include "trace_probe.h"
  
  #define UPROBE_EVENT_SYSTEM	"uprobes"
457d1772f   Oleg Nesterov   uprobes/tracing: ...
30
31
32
33
34
35
36
37
38
39
40
  struct uprobe_trace_entry_head {
  	struct trace_entry	ent;
  	unsigned long		vaddr[];
  };
  
  #define SIZEOF_TRACE_ENTRY(is_return)			\
  	(sizeof(struct uprobe_trace_entry_head) +	\
  	 sizeof(unsigned long) * (is_return ? 2 : 1))
  
  #define DATAOF_TRACE_ENTRY(entry, is_return)		\
  	((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
41
42
43
44
45
  struct trace_uprobe_filter {
  	rwlock_t		rwlock;
  	int			nr_systemwide;
  	struct list_head	perf_events;
  };
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
46
47
48
  /*
   * uprobe event core functions
   */
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
49
50
51
52
  struct trace_uprobe {
  	struct list_head		list;
  	struct ftrace_event_class	class;
  	struct ftrace_event_call	call;
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
53
  	struct trace_uprobe_filter	filter;
a932b7381   Oleg Nesterov   uprobes/tracing: ...
54
  	struct uprobe_consumer		consumer;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  	struct inode			*inode;
  	char				*filename;
  	unsigned long			offset;
  	unsigned long			nhit;
  	unsigned int			flags;	/* For TP_FLAG_* */
  	ssize_t				size;	/* trace entry size */
  	unsigned int			nr_args;
  	struct probe_arg		args[];
  };
  
  #define SIZEOF_TRACE_UPROBE(n)			\
  	(offsetof(struct trace_uprobe, args) +	\
  	(sizeof(struct probe_arg) * (n)))
  
  static int register_uprobe_event(struct trace_uprobe *tu);
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
70
  static int unregister_uprobe_event(struct trace_uprobe *tu);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
71
72
73
74
75
  
  static DEFINE_MUTEX(uprobe_lock);
  static LIST_HEAD(uprobe_list);
  
  static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
c1ae5c75e   Oleg Nesterov   uprobes/tracing: ...
76
77
  static int uretprobe_dispatcher(struct uprobe_consumer *con,
  				unsigned long func, struct pt_regs *regs);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
78

736288ba5   Oleg Nesterov   uprobes/perf: Tea...
79
80
81
82
83
84
85
86
87
88
89
  static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
  {
  	rwlock_init(&filter->rwlock);
  	filter->nr_systemwide = 0;
  	INIT_LIST_HEAD(&filter->perf_events);
  }
  
  static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
  {
  	return !filter->nr_systemwide && list_empty(&filter->perf_events);
  }
c1ae5c75e   Oleg Nesterov   uprobes/tracing: ...
90
91
92
93
  static inline bool is_ret_probe(struct trace_uprobe *tu)
  {
  	return tu->consumer.ret_handler != NULL;
  }
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
94
95
96
97
  /*
   * Allocate new trace_uprobe and initialize it (including uprobes).
   */
  static struct trace_uprobe *
c1ae5c75e   Oleg Nesterov   uprobes/tracing: ...
98
  alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  {
  	struct trace_uprobe *tu;
  
  	if (!event || !is_good_name(event))
  		return ERR_PTR(-EINVAL);
  
  	if (!group || !is_good_name(group))
  		return ERR_PTR(-EINVAL);
  
  	tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
  	if (!tu)
  		return ERR_PTR(-ENOMEM);
  
  	tu->call.class = &tu->class;
  	tu->call.name = kstrdup(event, GFP_KERNEL);
  	if (!tu->call.name)
  		goto error;
  
  	tu->class.system = kstrdup(group, GFP_KERNEL);
  	if (!tu->class.system)
  		goto error;
  
  	INIT_LIST_HEAD(&tu->list);
a932b7381   Oleg Nesterov   uprobes/tracing: ...
122
  	tu->consumer.handler = uprobe_dispatcher;
c1ae5c75e   Oleg Nesterov   uprobes/tracing: ...
123
124
  	if (is_ret)
  		tu->consumer.ret_handler = uretprobe_dispatcher;
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
125
  	init_trace_uprobe_filter(&tu->filter);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  	return tu;
  
  error:
  	kfree(tu->call.name);
  	kfree(tu);
  
  	return ERR_PTR(-ENOMEM);
  }
  
  static void free_trace_uprobe(struct trace_uprobe *tu)
  {
  	int i;
  
  	for (i = 0; i < tu->nr_args; i++)
  		traceprobe_free_probe_arg(&tu->args[i]);
  
  	iput(tu->inode);
  	kfree(tu->call.class->system);
  	kfree(tu->call.name);
  	kfree(tu->filename);
  	kfree(tu);
  }
  
  static struct trace_uprobe *find_probe_event(const char *event, const char *group)
  {
  	struct trace_uprobe *tu;
  
  	list_for_each_entry(tu, &uprobe_list, list)
  		if (strcmp(tu->call.name, event) == 0 &&
  		    strcmp(tu->call.class->system, group) == 0)
  			return tu;
  
  	return NULL;
  }
  
  /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
162
  static int unregister_trace_uprobe(struct trace_uprobe *tu)
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
163
  {
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
164
165
166
167
168
  	int ret;
  
  	ret = unregister_uprobe_event(tu);
  	if (ret)
  		return ret;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
169
  	list_del(&tu->list);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
170
  	free_trace_uprobe(tu);
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
171
  	return 0;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
172
173
174
175
176
177
178
179
180
181
182
183
  }
  
  /* Register a trace_uprobe and probe_event */
  static int register_trace_uprobe(struct trace_uprobe *tu)
  {
  	struct trace_uprobe *old_tp;
  	int ret;
  
  	mutex_lock(&uprobe_lock);
  
  	/* register as an event */
  	old_tp = find_probe_event(tu->call.name, tu->call.class->system);
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
184
  	if (old_tp) {
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
185
  		/* delete old event */
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
186
187
188
189
  		ret = unregister_trace_uprobe(old_tp);
  		if (ret)
  			goto end;
  	}
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  
  	ret = register_uprobe_event(tu);
  	if (ret) {
  		pr_warning("Failed to register probe event(%d)
  ", ret);
  		goto end;
  	}
  
  	list_add_tail(&tu->list, &uprobe_list);
  
  end:
  	mutex_unlock(&uprobe_lock);
  
  	return ret;
  }
  
  /*
   * Argument syntax:
4ee5a52ed   Oleg Nesterov   uprobes/tracing: ...
208
   *  - Add uprobe: p|r[:[GRP/]EVENT] PATH:SYMBOL [FETCHARGS]
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
209
210
211
212
213
214
215
216
217
218
219
   *
   *  - Remove uprobe: -:[GRP/]EVENT
   */
  static int create_trace_uprobe(int argc, char **argv)
  {
  	struct trace_uprobe *tu;
  	struct inode *inode;
  	char *arg, *event, *group, *filename;
  	char buf[MAX_EVENT_NAME_LEN];
  	struct path path;
  	unsigned long offset;
4ee5a52ed   Oleg Nesterov   uprobes/tracing: ...
220
  	bool is_delete, is_return;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
221
222
223
224
225
  	int i, ret;
  
  	inode = NULL;
  	ret = 0;
  	is_delete = false;
4ee5a52ed   Oleg Nesterov   uprobes/tracing: ...
226
  	is_return = false;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
227
228
229
230
231
232
  	event = NULL;
  	group = NULL;
  
  	/* argc must be >= 1 */
  	if (argv[0][0] == '-')
  		is_delete = true;
4ee5a52ed   Oleg Nesterov   uprobes/tracing: ...
233
234
  	else if (argv[0][0] == 'r')
  		is_return = true;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
235
  	else if (argv[0][0] != 'p') {
4ee5a52ed   Oleg Nesterov   uprobes/tracing: ...
236
237
  		pr_info("Probe definition must be started with 'p', 'r' or '-'.
  ");
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  		return -EINVAL;
  	}
  
  	if (argv[0][1] == ':') {
  		event = &argv[0][2];
  		arg = strchr(event, '/');
  
  		if (arg) {
  			group = event;
  			event = arg + 1;
  			event[-1] = '\0';
  
  			if (strlen(group) == 0) {
  				pr_info("Group name is not specified
  ");
  				return -EINVAL;
  			}
  		}
  		if (strlen(event) == 0) {
  			pr_info("Event name is not specified
  ");
  			return -EINVAL;
  		}
  	}
  	if (!group)
  		group = UPROBE_EVENT_SYSTEM;
  
  	if (is_delete) {
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
266
  		int ret;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  		if (!event) {
  			pr_info("Delete command needs an event name.
  ");
  			return -EINVAL;
  		}
  		mutex_lock(&uprobe_lock);
  		tu = find_probe_event(event, group);
  
  		if (!tu) {
  			mutex_unlock(&uprobe_lock);
  			pr_info("Event %s/%s doesn't exist.
  ", group, event);
  			return -ENOENT;
  		}
  		/* delete an event */
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
282
  		ret = unregister_trace_uprobe(tu);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
283
  		mutex_unlock(&uprobe_lock);
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
284
  		return ret;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
285
286
287
288
289
290
291
292
293
294
295
296
297
  	}
  
  	if (argc < 2) {
  		pr_info("Probe point is not specified.
  ");
  		return -EINVAL;
  	}
  	if (isdigit(argv[1][0])) {
  		pr_info("probe point must be have a filename.
  ");
  		return -EINVAL;
  	}
  	arg = strchr(argv[1], ':');
3873153a6   zhangwei(Jovi)   uprobes: Fix retu...
298
299
  	if (!arg) {
  		ret = -EINVAL;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
300
  		goto fail_address_parse;
3873153a6   zhangwei(Jovi)   uprobes: Fix retu...
301
  	}
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
302
303
304
305
306
307
  
  	*arg++ = '\0';
  	filename = argv[1];
  	ret = kern_path(filename, LOOKUP_FOLLOW, &path);
  	if (ret)
  		goto fail_address_parse;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
308
  	inode = igrab(path.dentry->d_inode);
84d7ed799   Oleg Nesterov   uprobes/tracing: ...
309
  	path_put(&path);
7e4e28c53   Oleg Nesterov   uprobes/tracing: ...
310
  	if (!inode || !S_ISREG(inode->i_mode)) {
d24d7dbf3   Jovi Zhang   tracing: Verify t...
311
312
313
  		ret = -EINVAL;
  		goto fail_address_parse;
  	}
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
314

84d7ed799   Oleg Nesterov   uprobes/tracing: ...
315
316
317
  	ret = kstrtoul(arg, 0, &offset);
  	if (ret)
  		goto fail_address_parse;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
318
319
320
321
322
  	argc -= 2;
  	argv += 2;
  
  	/* setup a probe */
  	if (!event) {
b2e902f02   Andy Shevchenko   trace: use kbasen...
323
  		char *tail;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
324
  		char *ptr;
b2e902f02   Andy Shevchenko   trace: use kbasen...
325
326
  		tail = kstrdup(kbasename(filename), GFP_KERNEL);
  		if (!tail) {
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
327
328
329
  			ret = -ENOMEM;
  			goto fail_address_parse;
  		}
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
330
331
332
333
334
335
336
337
  		ptr = strpbrk(tail, ".-_");
  		if (ptr)
  			*ptr = '\0';
  
  		snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
  		event = buf;
  		kfree(tail);
  	}
4ee5a52ed   Oleg Nesterov   uprobes/tracing: ...
338
  	tu = alloc_trace_uprobe(group, event, argc, is_return);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
  	if (IS_ERR(tu)) {
  		pr_info("Failed to allocate trace_uprobe.(%d)
  ", (int)PTR_ERR(tu));
  		ret = PTR_ERR(tu);
  		goto fail_address_parse;
  	}
  	tu->offset = offset;
  	tu->inode = inode;
  	tu->filename = kstrdup(filename, GFP_KERNEL);
  
  	if (!tu->filename) {
  		pr_info("Failed to allocate filename.
  ");
  		ret = -ENOMEM;
  		goto error;
  	}
  
  	/* parse arguments */
  	ret = 0;
  	for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  		/* Increment count for freeing args in error case */
  		tu->nr_args++;
  
  		/* Parse argument name */
  		arg = strchr(argv[i], '=');
  		if (arg) {
  			*arg++ = '\0';
  			tu->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  		} else {
  			arg = argv[i];
  			/* If argument name is omitted, set "argN" */
  			snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  			tu->args[i].name = kstrdup(buf, GFP_KERNEL);
  		}
  
  		if (!tu->args[i].name) {
  			pr_info("Failed to allocate argument[%d] name.
  ", i);
  			ret = -ENOMEM;
  			goto error;
  		}
  
  		if (!is_good_name(tu->args[i].name)) {
  			pr_info("Invalid argument[%d] name: %s
  ", i, tu->args[i].name);
  			ret = -EINVAL;
  			goto error;
  		}
  
  		if (traceprobe_conflict_field_name(tu->args[i].name, tu->args, i)) {
  			pr_info("Argument[%d] name '%s' conflicts with "
  				"another field.
  ", i, argv[i]);
  			ret = -EINVAL;
  			goto error;
  		}
  
  		/* Parse fetch argument */
  		ret = traceprobe_parse_probe_arg(arg, &tu->size, &tu->args[i], false, false);
  		if (ret) {
  			pr_info("Parse error at argument[%d]. (%d)
  ", i, ret);
  			goto error;
  		}
  	}
  
  	ret = register_trace_uprobe(tu);
  	if (ret)
  		goto error;
  	return 0;
  
  error:
  	free_trace_uprobe(tu);
  	return ret;
  
  fail_address_parse:
  	if (inode)
  		iput(inode);
d24d7dbf3   Jovi Zhang   tracing: Verify t...
417
418
  	pr_info("Failed to parse address or file.
  ");
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
419
420
421
  
  	return ret;
  }
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
422
  static int cleanup_all_probes(void)
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
423
424
  {
  	struct trace_uprobe *tu;
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
425
  	int ret = 0;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
426
427
428
429
  
  	mutex_lock(&uprobe_lock);
  	while (!list_empty(&uprobe_list)) {
  		tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
430
431
432
  		ret = unregister_trace_uprobe(tu);
  		if (ret)
  			break;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
433
434
  	}
  	mutex_unlock(&uprobe_lock);
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
435
  	return ret;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
  }
  
  /* Probes listing interfaces */
  static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  {
  	mutex_lock(&uprobe_lock);
  	return seq_list_start(&uprobe_list, *pos);
  }
  
  static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  {
  	return seq_list_next(v, &uprobe_list, pos);
  }
  
  static void probes_seq_stop(struct seq_file *m, void *v)
  {
  	mutex_unlock(&uprobe_lock);
  }
  
  static int probes_seq_show(struct seq_file *m, void *v)
  {
  	struct trace_uprobe *tu = v;
3ede82dd3   Oleg Nesterov   uprobes/tracing: ...
458
  	char c = is_ret_probe(tu) ? 'r' : 'p';
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
459
  	int i;
3ede82dd3   Oleg Nesterov   uprobes/tracing: ...
460
  	seq_printf(m, "%c:%s/%s", c, tu->call.class->system, tu->call.name);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
  	seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset);
  
  	for (i = 0; i < tu->nr_args; i++)
  		seq_printf(m, " %s=%s", tu->args[i].name, tu->args[i].comm);
  
  	seq_printf(m, "
  ");
  	return 0;
  }
  
  static const struct seq_operations probes_seq_op = {
  	.start	= probes_seq_start,
  	.next	= probes_seq_next,
  	.stop	= probes_seq_stop,
  	.show	= probes_seq_show
  };
  
  static int probes_open(struct inode *inode, struct file *file)
  {
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
480
481
482
483
484
485
486
  	int ret;
  
  	if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  		ret = cleanup_all_probes();
  		if (ret)
  			return ret;
  	}
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
  
  	return seq_open(file, &probes_seq_op);
  }
  
  static ssize_t probes_write(struct file *file, const char __user *buffer,
  			    size_t count, loff_t *ppos)
  {
  	return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
  }
  
  static const struct file_operations uprobe_events_ops = {
  	.owner		= THIS_MODULE,
  	.open		= probes_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
  	.release	= seq_release,
  	.write		= probes_write,
  };
  
  /* Probes profiling interfaces */
  static int probes_profile_seq_show(struct seq_file *m, void *v)
  {
  	struct trace_uprobe *tu = v;
  
  	seq_printf(m, "  %s %-44s %15lu
  ", tu->filename, tu->call.name, tu->nhit);
  	return 0;
  }
  
  static const struct seq_operations profile_seq_op = {
  	.start	= probes_seq_start,
  	.next	= probes_seq_next,
  	.stop	= probes_seq_stop,
  	.show	= probes_profile_seq_show
  };
  
  static int profile_open(struct inode *inode, struct file *file)
  {
  	return seq_open(file, &profile_seq_op);
  }
  
  static const struct file_operations uprobe_profile_ops = {
  	.owner		= THIS_MODULE,
  	.open		= profile_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
  	.release	= seq_release,
  };
a51cc6041   Oleg Nesterov   uprobes/tracing: ...
535
536
  static void uprobe_trace_print(struct trace_uprobe *tu,
  				unsigned long func, struct pt_regs *regs)
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
537
538
539
540
  {
  	struct uprobe_trace_entry_head *entry;
  	struct ring_buffer_event *event;
  	struct ring_buffer *buffer;
457d1772f   Oleg Nesterov   uprobes/tracing: ...
541
  	void *data;
0e3853d20   Oleg Nesterov   uprobes/tracing: ...
542
  	int size, i;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
543
  	struct ftrace_event_call *call = &tu->call;
393a736c2   Oleg Nesterov   uprobes/tracing: ...
544
  	size = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
545
  	event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
393a736c2   Oleg Nesterov   uprobes/tracing: ...
546
  						  size + tu->size, 0, 0);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
547
  	if (!event)
a51cc6041   Oleg Nesterov   uprobes/tracing: ...
548
  		return;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
549
550
  
  	entry = ring_buffer_event_data(event);
393a736c2   Oleg Nesterov   uprobes/tracing: ...
551
552
553
554
555
556
557
558
  	if (is_ret_probe(tu)) {
  		entry->vaddr[0] = func;
  		entry->vaddr[1] = instruction_pointer(regs);
  		data = DATAOF_TRACE_ENTRY(entry, true);
  	} else {
  		entry->vaddr[0] = instruction_pointer(regs);
  		data = DATAOF_TRACE_ENTRY(entry, false);
  	}
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
559
560
561
562
  	for (i = 0; i < tu->nr_args; i++)
  		call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
  
  	if (!filter_current_check_discard(buffer, call, entry, event))
0e3853d20   Oleg Nesterov   uprobes/tracing: ...
563
  		trace_buffer_unlock_commit(buffer, event, 0, 0);
a51cc6041   Oleg Nesterov   uprobes/tracing: ...
564
  }
f42d24a1d   Oleg Nesterov   uprobes/perf: Tea...
565

a51cc6041   Oleg Nesterov   uprobes/tracing: ...
566
567
568
  /* uprobe handler */
  static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
  {
393a736c2   Oleg Nesterov   uprobes/tracing: ...
569
570
  	if (!is_ret_probe(tu))
  		uprobe_trace_print(tu, 0, regs);
f42d24a1d   Oleg Nesterov   uprobes/perf: Tea...
571
  	return 0;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
572
  }
c1ae5c75e   Oleg Nesterov   uprobes/tracing: ...
573
574
575
576
577
  static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
  				struct pt_regs *regs)
  {
  	uprobe_trace_print(tu, func, regs);
  }
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
578
579
580
581
  /* Event entry printers */
  static enum print_line_t
  print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
  {
457d1772f   Oleg Nesterov   uprobes/tracing: ...
582
  	struct uprobe_trace_entry_head *entry;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
583
584
585
586
  	struct trace_seq *s = &iter->seq;
  	struct trace_uprobe *tu;
  	u8 *data;
  	int i;
457d1772f   Oleg Nesterov   uprobes/tracing: ...
587
  	entry = (struct uprobe_trace_entry_head *)iter->ent;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
588
  	tu = container_of(event, struct trace_uprobe, call.event);
3ede82dd3   Oleg Nesterov   uprobes/tracing: ...
589
590
591
592
593
594
595
596
597
598
599
  	if (is_ret_probe(tu)) {
  		if (!trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", tu->call.name,
  					entry->vaddr[1], entry->vaddr[0]))
  			goto partial;
  		data = DATAOF_TRACE_ENTRY(entry, true);
  	} else {
  		if (!trace_seq_printf(s, "%s: (0x%lx)", tu->call.name,
  					entry->vaddr[0]))
  			goto partial;
  		data = DATAOF_TRACE_ENTRY(entry, false);
  	}
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
600

f3f096cfe   Srikar Dronamraju   tracing: Provide ...
601
602
  	for (i = 0; i < tu->nr_args; i++) {
  		if (!tu->args[i].type->print(s, tu->args[i].name,
457d1772f   Oleg Nesterov   uprobes/tracing: ...
603
  					     data + tu->args[i].offset, entry))
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
604
605
606
607
608
609
610
611
612
613
  			goto partial;
  	}
  
  	if (trace_seq_puts(s, "
  "))
  		return TRACE_TYPE_HANDLED;
  
  partial:
  	return TRACE_TYPE_PARTIAL_LINE;
  }
b64b00779   Oleg Nesterov   uprobes/tracing: ...
614
615
616
617
  static inline bool is_trace_uprobe_enabled(struct trace_uprobe *tu)
  {
  	return tu->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE);
  }
31ba33483   Oleg Nesterov   uprobes/perf: Tea...
618
619
620
621
622
623
  typedef bool (*filter_func_t)(struct uprobe_consumer *self,
  				enum uprobe_filter_ctx ctx,
  				struct mm_struct *mm);
  
  static int
  probe_event_enable(struct trace_uprobe *tu, int flag, filter_func_t filter)
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
624
  {
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
625
  	int ret = 0;
b64b00779   Oleg Nesterov   uprobes/tracing: ...
626
  	if (is_trace_uprobe_enabled(tu))
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
627
  		return -EINTR;
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
628
  	WARN_ON(!uprobe_filter_is_empty(&tu->filter));
4161824f1   Oleg Nesterov   uprobes/tracing: ...
629
  	tu->flags |= flag;
31ba33483   Oleg Nesterov   uprobes/perf: Tea...
630
  	tu->consumer.filter = filter;
a932b7381   Oleg Nesterov   uprobes/tracing: ...
631
632
  	ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
  	if (ret)
4161824f1   Oleg Nesterov   uprobes/tracing: ...
633
  		tu->flags &= ~flag;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
634

4161824f1   Oleg Nesterov   uprobes/tracing: ...
635
  	return ret;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
636
637
638
639
  }
  
  static void probe_event_disable(struct trace_uprobe *tu, int flag)
  {
b64b00779   Oleg Nesterov   uprobes/tracing: ...
640
  	if (!is_trace_uprobe_enabled(tu))
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
641
  		return;
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
642
  	WARN_ON(!uprobe_filter_is_empty(&tu->filter));
a932b7381   Oleg Nesterov   uprobes/tracing: ...
643
  	uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
644
  	tu->flags &= ~flag;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
645
646
647
648
  }
  
  static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
  {
457d1772f   Oleg Nesterov   uprobes/tracing: ...
649
  	int ret, i, size;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
650
  	struct uprobe_trace_entry_head field;
457d1772f   Oleg Nesterov   uprobes/tracing: ...
651
  	struct trace_uprobe *tu = event_call->data;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
652

4d1298e21   Oleg Nesterov   uprobes/tracing: ...
653
654
655
656
657
658
659
660
  	if (is_ret_probe(tu)) {
  		DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
  		DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
  		size = SIZEOF_TRACE_ENTRY(true);
  	} else {
  		DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
  		size = SIZEOF_TRACE_ENTRY(false);
  	}
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
661
662
663
664
  	/* Set argument names as fields */
  	for (i = 0; i < tu->nr_args; i++) {
  		ret = trace_define_field(event_call, tu->args[i].type->fmttype,
  					 tu->args[i].name,
457d1772f   Oleg Nesterov   uprobes/tracing: ...
665
  					 size + tu->args[i].offset,
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
  					 tu->args[i].type->size,
  					 tu->args[i].type->is_signed,
  					 FILTER_OTHER);
  
  		if (ret)
  			return ret;
  	}
  	return 0;
  }
  
  #define LEN_OR_ZERO		(len ? len - pos : 0)
  static int __set_print_fmt(struct trace_uprobe *tu, char *buf, int len)
  {
  	const char *fmt, *arg;
  	int i;
  	int pos = 0;
4d1298e21   Oleg Nesterov   uprobes/tracing: ...
682
683
684
685
686
687
688
  	if (is_ret_probe(tu)) {
  		fmt = "(%lx <- %lx)";
  		arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  	} else {
  		fmt = "(%lx)";
  		arg = "REC->" FIELD_STRING_IP;
  	}
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
  
  	/* When len=0, we just calculate the needed length */
  
  	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  
  	for (i = 0; i < tu->nr_args; i++) {
  		pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  				tu->args[i].name, tu->args[i].type->fmt);
  	}
  
  	pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  
  	for (i = 0; i < tu->nr_args; i++) {
  		pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  				tu->args[i].name);
  	}
  
  	return pos;	/* return the length of print_fmt */
  }
  #undef LEN_OR_ZERO
  
  static int set_print_fmt(struct trace_uprobe *tu)
  {
  	char *print_fmt;
  	int len;
  
  	/* First: called with 0 length to calculate the needed length */
  	len = __set_print_fmt(tu, NULL, 0);
  	print_fmt = kmalloc(len + 1, GFP_KERNEL);
  	if (!print_fmt)
  		return -ENOMEM;
  
  	/* Second: actually write the @print_fmt */
  	__set_print_fmt(tu, print_fmt, len + 1);
  	tu->call.print_fmt = print_fmt;
  
  	return 0;
  }
  
  #ifdef CONFIG_PERF_EVENTS
31ba33483   Oleg Nesterov   uprobes/perf: Tea...
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
  static bool
  __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
  {
  	struct perf_event *event;
  
  	if (filter->nr_systemwide)
  		return true;
  
  	list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
  		if (event->hw.tp_target->mm == mm)
  			return true;
  	}
  
  	return false;
  }
b2fe8ba67   Oleg Nesterov   uprobes/perf: Avo...
744
745
746
747
748
  static inline bool
  uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
  {
  	return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm);
  }
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
749
750
  static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
  {
b2fe8ba67   Oleg Nesterov   uprobes/perf: Avo...
751
  	bool done;
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
752
  	write_lock(&tu->filter.rwlock);
b2fe8ba67   Oleg Nesterov   uprobes/perf: Avo...
753
754
755
756
757
758
759
760
761
762
763
764
  	if (event->hw.tp_target) {
  		/*
  		 * event->parent != NULL means copy_process(), we can avoid
  		 * uprobe_apply(). current->mm must be probed and we can rely
  		 * on dup_mmap() which preserves the already installed bp's.
  		 *
  		 * attr.enable_on_exec means that exec/mmap will install the
  		 * breakpoints we need.
  		 */
  		done = tu->filter.nr_systemwide ||
  			event->parent || event->attr.enable_on_exec ||
  			uprobe_filter_event(tu, event);
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
765
  		list_add(&event->hw.tp_list, &tu->filter.perf_events);
b2fe8ba67   Oleg Nesterov   uprobes/perf: Avo...
766
767
  	} else {
  		done = tu->filter.nr_systemwide;
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
768
  		tu->filter.nr_systemwide++;
b2fe8ba67   Oleg Nesterov   uprobes/perf: Avo...
769
  	}
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
770
  	write_unlock(&tu->filter.rwlock);
b2fe8ba67   Oleg Nesterov   uprobes/perf: Avo...
771
772
  	if (!done)
  		uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
31ba33483   Oleg Nesterov   uprobes/perf: Tea...
773

736288ba5   Oleg Nesterov   uprobes/perf: Tea...
774
775
776
777
778
  	return 0;
  }
  
  static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
  {
b2fe8ba67   Oleg Nesterov   uprobes/perf: Avo...
779
  	bool done;
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
780
  	write_lock(&tu->filter.rwlock);
b2fe8ba67   Oleg Nesterov   uprobes/perf: Avo...
781
  	if (event->hw.tp_target) {
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
782
  		list_del(&event->hw.tp_list);
b2fe8ba67   Oleg Nesterov   uprobes/perf: Avo...
783
784
785
786
  		done = tu->filter.nr_systemwide ||
  			(event->hw.tp_target->flags & PF_EXITING) ||
  			uprobe_filter_event(tu, event);
  	} else {
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
787
  		tu->filter.nr_systemwide--;
b2fe8ba67   Oleg Nesterov   uprobes/perf: Avo...
788
789
  		done = tu->filter.nr_systemwide;
  	}
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
790
  	write_unlock(&tu->filter.rwlock);
b2fe8ba67   Oleg Nesterov   uprobes/perf: Avo...
791
792
  	if (!done)
  		uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
31ba33483   Oleg Nesterov   uprobes/perf: Tea...
793

736288ba5   Oleg Nesterov   uprobes/perf: Tea...
794
795
  	return 0;
  }
31ba33483   Oleg Nesterov   uprobes/perf: Tea...
796
797
798
799
800
801
802
803
804
805
806
807
808
  static bool uprobe_perf_filter(struct uprobe_consumer *uc,
  				enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  {
  	struct trace_uprobe *tu;
  	int ret;
  
  	tu = container_of(uc, struct trace_uprobe, consumer);
  	read_lock(&tu->filter.rwlock);
  	ret = __uprobe_perf_filter(&tu->filter, mm);
  	read_unlock(&tu->filter.rwlock);
  
  	return ret;
  }
a51cc6041   Oleg Nesterov   uprobes/tracing: ...
809
810
  static void uprobe_perf_print(struct trace_uprobe *tu,
  				unsigned long func, struct pt_regs *regs)
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
811
812
813
814
  {
  	struct ftrace_event_call *call = &tu->call;
  	struct uprobe_trace_entry_head *entry;
  	struct hlist_head *head;
457d1772f   Oleg Nesterov   uprobes/tracing: ...
815
816
  	void *data;
  	int size, rctx, i;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
817

393a736c2   Oleg Nesterov   uprobes/tracing: ...
818
  	size = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
457d1772f   Oleg Nesterov   uprobes/tracing: ...
819
  	size = ALIGN(size + tu->size + sizeof(u32), sizeof(u64)) - sizeof(u32);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
820
  	if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
a51cc6041   Oleg Nesterov   uprobes/tracing: ...
821
  		return;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
822
823
  
  	preempt_disable();
515619f20   Oleg Nesterov   uprobes/perf: Avo...
824
825
826
  	head = this_cpu_ptr(call->perf_events);
  	if (hlist_empty(head))
  		goto out;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
827
828
829
  	entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  	if (!entry)
  		goto out;
393a736c2   Oleg Nesterov   uprobes/tracing: ...
830
831
  	if (is_ret_probe(tu)) {
  		entry->vaddr[0] = func;
32520b2c6   Oleg Nesterov   uprobes/tracing: ...
832
  		entry->vaddr[1] = instruction_pointer(regs);
393a736c2   Oleg Nesterov   uprobes/tracing: ...
833
834
  		data = DATAOF_TRACE_ENTRY(entry, true);
  	} else {
32520b2c6   Oleg Nesterov   uprobes/tracing: ...
835
  		entry->vaddr[0] = instruction_pointer(regs);
393a736c2   Oleg Nesterov   uprobes/tracing: ...
836
837
  		data = DATAOF_TRACE_ENTRY(entry, false);
  	}
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
838
839
  	for (i = 0; i < tu->nr_args; i++)
  		call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
32520b2c6   Oleg Nesterov   uprobes/tracing: ...
840
  	perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
841
842
   out:
  	preempt_enable();
a51cc6041   Oleg Nesterov   uprobes/tracing: ...
843
844
845
846
847
848
849
  }
  
  /* uprobe profile handler */
  static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
  {
  	if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
  		return UPROBE_HANDLER_REMOVE;
393a736c2   Oleg Nesterov   uprobes/tracing: ...
850
851
  	if (!is_ret_probe(tu))
  		uprobe_perf_print(tu, 0, regs);
f42d24a1d   Oleg Nesterov   uprobes/perf: Tea...
852
  	return 0;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
853
  }
c1ae5c75e   Oleg Nesterov   uprobes/tracing: ...
854
855
856
857
858
859
  
  static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
  				struct pt_regs *regs)
  {
  	uprobe_perf_print(tu, func, regs);
  }
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
860
861
862
863
864
  #endif	/* CONFIG_PERF_EVENTS */
  
  static
  int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data)
  {
457d1772f   Oleg Nesterov   uprobes/tracing: ...
865
  	struct trace_uprobe *tu = event->data;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
866
867
868
  
  	switch (type) {
  	case TRACE_REG_REGISTER:
31ba33483   Oleg Nesterov   uprobes/perf: Tea...
869
  		return probe_event_enable(tu, TP_FLAG_TRACE, NULL);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
870
871
872
873
874
875
876
  
  	case TRACE_REG_UNREGISTER:
  		probe_event_disable(tu, TP_FLAG_TRACE);
  		return 0;
  
  #ifdef CONFIG_PERF_EVENTS
  	case TRACE_REG_PERF_REGISTER:
31ba33483   Oleg Nesterov   uprobes/perf: Tea...
877
  		return probe_event_enable(tu, TP_FLAG_PROFILE, uprobe_perf_filter);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
878
879
880
881
  
  	case TRACE_REG_PERF_UNREGISTER:
  		probe_event_disable(tu, TP_FLAG_PROFILE);
  		return 0;
736288ba5   Oleg Nesterov   uprobes/perf: Tea...
882
883
884
885
886
887
  
  	case TRACE_REG_PERF_OPEN:
  		return uprobe_perf_open(tu, data);
  
  	case TRACE_REG_PERF_CLOSE:
  		return uprobe_perf_close(tu, data);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
888
889
890
891
892
893
894
895
896
  #endif
  	default:
  		return 0;
  	}
  	return 0;
  }
  
  static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
  {
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
897
  	struct trace_uprobe *tu;
f42d24a1d   Oleg Nesterov   uprobes/perf: Tea...
898
  	int ret = 0;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
899

a932b7381   Oleg Nesterov   uprobes/tracing: ...
900
  	tu = container_of(con, struct trace_uprobe, consumer);
1b47aefd9   Oleg Nesterov   uprobes/perf: Alw...
901
  	tu->nhit++;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
902
903
  
  	if (tu->flags & TP_FLAG_TRACE)
f42d24a1d   Oleg Nesterov   uprobes/perf: Tea...
904
  		ret |= uprobe_trace_func(tu, regs);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
905
906
907
  
  #ifdef CONFIG_PERF_EVENTS
  	if (tu->flags & TP_FLAG_PROFILE)
f42d24a1d   Oleg Nesterov   uprobes/perf: Tea...
908
  		ret |= uprobe_perf_func(tu, regs);
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
909
  #endif
f42d24a1d   Oleg Nesterov   uprobes/perf: Tea...
910
  	return ret;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
911
  }
c1ae5c75e   Oleg Nesterov   uprobes/tracing: ...
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
  static int uretprobe_dispatcher(struct uprobe_consumer *con,
  				unsigned long func, struct pt_regs *regs)
  {
  	struct trace_uprobe *tu;
  
  	tu = container_of(con, struct trace_uprobe, consumer);
  
  	if (tu->flags & TP_FLAG_TRACE)
  		uretprobe_trace_func(tu, func, regs);
  
  #ifdef CONFIG_PERF_EVENTS
  	if (tu->flags & TP_FLAG_PROFILE)
  		uretprobe_perf_func(tu, func, regs);
  #endif
  	return 0;
  }
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
  static struct trace_event_functions uprobe_funcs = {
  	.trace		= print_uprobe_event
  };
  
  static int register_uprobe_event(struct trace_uprobe *tu)
  {
  	struct ftrace_event_call *call = &tu->call;
  	int ret;
  
  	/* Initialize ftrace_event_call */
  	INIT_LIST_HEAD(&call->class->fields);
  	call->event.funcs = &uprobe_funcs;
  	call->class->define_fields = uprobe_event_define_fields;
  
  	if (set_print_fmt(tu) < 0)
  		return -ENOMEM;
  
  	ret = register_ftrace_event(&call->event);
  	if (!ret) {
  		kfree(call->print_fmt);
  		return -ENODEV;
  	}
  	call->flags = 0;
  	call->class->reg = trace_uprobe_register;
  	call->data = tu;
  	ret = trace_add_event_call(call);
  
  	if (ret) {
  		pr_info("Failed to register uprobe event: %s
  ", call->name);
  		kfree(call->print_fmt);
  		unregister_ftrace_event(&call->event);
  	}
  
  	return ret;
  }
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
964
  static int unregister_uprobe_event(struct trace_uprobe *tu)
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
965
  {
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
966
  	int ret;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
967
  	/* tu->event is unregistered in trace_remove_event_call() */
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
968
969
970
  	ret = trace_remove_event_call(&tu->call);
  	if (ret)
  		return ret;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
971
972
  	kfree(tu->call.print_fmt);
  	tu->call.print_fmt = NULL;
396dd5005   Steven Rostedt (Red Hat)   tracing/uprobes: ...
973
  	return 0;
f3f096cfe   Srikar Dronamraju   tracing: Provide ...
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
  }
  
  /* Make a trace interface for controling probe points */
  static __init int init_uprobe_trace(void)
  {
  	struct dentry *d_tracer;
  
  	d_tracer = tracing_init_dentry();
  	if (!d_tracer)
  		return 0;
  
  	trace_create_file("uprobe_events", 0644, d_tracer,
  				    NULL, &uprobe_events_ops);
  	/* Profile interface */
  	trace_create_file("uprobe_profile", 0444, d_tracer,
  				    NULL, &uprobe_profile_ops);
  	return 0;
  }
  
  fs_initcall(init_uprobe_trace);