blob: 0d450b40988edbb1184dd270537a9162ddf58336 [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 */
Masami Hiramatsu72576342017-02-07 20:21:28 +090020#define pr_fmt(fmt) "trace_kprobe: " fmt
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053021
22#include <linux/module.h>
23#include <linux/uaccess.h>
24#include <linux/uprobes.h>
25#include <linux/namei.h>
Andy Shevchenkob2e902f2012-12-17 16:01:27 -080026#include <linux/string.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010027#include <linux/rculist.h>
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053028
29#include "trace_probe.h"
30
31#define UPROBE_EVENT_SYSTEM "uprobes"
32
Oleg Nesterov457d1772013-03-29 18:26:51 +010033struct uprobe_trace_entry_head {
34 struct trace_entry ent;
35 unsigned long vaddr[];
36};
37
38#define SIZEOF_TRACE_ENTRY(is_return) \
39 (sizeof(struct uprobe_trace_entry_head) + \
40 sizeof(unsigned long) * (is_return ? 2 : 1))
41
42#define DATAOF_TRACE_ENTRY(entry, is_return) \
43 ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
44
Oleg Nesterov736288b2013-02-03 20:58:35 +010045struct trace_uprobe_filter {
46 rwlock_t rwlock;
47 int nr_systemwide;
48 struct list_head perf_events;
49};
50
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053051/*
52 * uprobe event core functions
53 */
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053054struct trace_uprobe {
55 struct list_head list;
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;
Namhyung Kim14577c32013-07-03 15:42:53 +090062 struct trace_probe tp;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053063};
64
Namhyung Kim14577c32013-07-03 15:42:53 +090065#define SIZEOF_TRACE_UPROBE(n) \
66 (offsetof(struct trace_uprobe, tp.args) + \
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053067 (sizeof(struct probe_arg) * (n)))
68
69static int register_uprobe_event(struct trace_uprobe *tu);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -040070static int unregister_uprobe_event(struct trace_uprobe *tu);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053071
72static DEFINE_MUTEX(uprobe_lock);
73static LIST_HEAD(uprobe_list);
74
Namhyung Kimb7e0bf32013-11-25 13:42:47 +090075struct uprobe_dispatch_data {
76 struct trace_uprobe *tu;
77 unsigned long bp_addr;
78};
79
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053080static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +010081static int uretprobe_dispatcher(struct uprobe_consumer *con,
82 unsigned long func, struct pt_regs *regs);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053083
Namhyung Kim3fd996a2013-11-26 15:21:04 +090084#ifdef CONFIG_STACK_GROWSUP
85static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
86{
87 return addr - (n * sizeof(long));
88}
89#else
90static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
91{
92 return addr + (n * sizeof(long));
93}
94#endif
95
96static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
97{
98 unsigned long ret;
99 unsigned long addr = user_stack_pointer(regs);
100
101 addr = adjust_stack_addr(addr, n);
102
103 if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
104 return 0;
105
106 return ret;
107}
108
109/*
110 * Uprobes-specific fetch functions
111 */
112#define DEFINE_FETCH_stack(type) \
Masami Hiramatsufbc19632014-04-17 17:18:00 +0900113static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
114 void *offset, void *dest) \
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900115{ \
116 *(type *)dest = (type)get_user_stack_nth(regs, \
117 ((unsigned long)offset)); \
118}
119DEFINE_BASIC_FETCH_FUNCS(stack)
120/* No string on the stack entry */
121#define fetch_stack_string NULL
122#define fetch_stack_string_size NULL
123
Namhyung Kim5baaa592013-11-26 15:21:04 +0900124#define DEFINE_FETCH_memory(type) \
Masami Hiramatsufbc19632014-04-17 17:18:00 +0900125static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
126 void *addr, void *dest) \
Namhyung Kim5baaa592013-11-26 15:21:04 +0900127{ \
128 type retval; \
129 void __user *vaddr = (void __force __user *) addr; \
130 \
131 if (copy_from_user(&retval, vaddr, sizeof(type))) \
132 *(type *)dest = 0; \
133 else \
134 *(type *) dest = retval; \
135}
136DEFINE_BASIC_FETCH_FUNCS(memory)
137/*
138 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
139 * length and relative data location.
140 */
Masami Hiramatsufbc19632014-04-17 17:18:00 +0900141static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
142 void *addr, void *dest)
Namhyung Kim5baaa592013-11-26 15:21:04 +0900143{
144 long ret;
145 u32 rloc = *(u32 *)dest;
146 int maxlen = get_rloc_len(rloc);
147 u8 *dst = get_rloc_data(dest);
148 void __user *src = (void __force __user *) addr;
149
150 if (!maxlen)
151 return;
152
153 ret = strncpy_from_user(dst, src, maxlen);
Masami Hiramatsu50268a32018-04-10 21:20:08 +0900154 if (ret == maxlen)
155 dst[--ret] = '\0';
Namhyung Kim5baaa592013-11-26 15:21:04 +0900156
157 if (ret < 0) { /* Failed to fetch string */
158 ((u8 *)get_rloc_data(dest))[0] = '\0';
159 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(rloc));
160 } else {
161 *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(rloc));
162 }
163}
164
Masami Hiramatsufbc19632014-04-17 17:18:00 +0900165static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
166 void *addr, void *dest)
Namhyung Kim5baaa592013-11-26 15:21:04 +0900167{
168 int len;
169 void __user *vaddr = (void __force __user *) addr;
170
171 len = strnlen_user(vaddr, MAX_STRING_SIZE);
172
173 if (len == 0 || len > MAX_STRING_SIZE) /* Failed to check length */
174 *(u32 *)dest = 0;
175 else
176 *(u32 *)dest = len;
177}
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900178
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900179static unsigned long translate_user_vaddr(void *file_offset)
180{
181 unsigned long base_addr;
182 struct uprobe_dispatch_data *udd;
183
184 udd = (void *) current->utask->vaddr;
185
186 base_addr = udd->bp_addr - udd->tu->offset;
187 return base_addr + (unsigned long)file_offset;
188}
189
190#define DEFINE_FETCH_file_offset(type) \
Masami Hiramatsufbc19632014-04-17 17:18:00 +0900191static void FETCH_FUNC_NAME(file_offset, type)(struct pt_regs *regs, \
192 void *offset, void *dest)\
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900193{ \
194 void *vaddr = (void *)translate_user_vaddr(offset); \
195 \
196 FETCH_FUNC_NAME(memory, type)(regs, vaddr, dest); \
197}
198DEFINE_BASIC_FETCH_FUNCS(file_offset)
199DEFINE_FETCH_file_offset(string)
200DEFINE_FETCH_file_offset(string_size)
201
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900202/* Fetch type information table */
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100203static const struct fetch_type uprobes_fetch_type_table[] = {
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900204 /* Special types */
205 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
206 sizeof(u32), 1, "__data_loc char[]"),
207 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
208 string_size, sizeof(u32), 0, "u32"),
209 /* Basic types */
210 ASSIGN_FETCH_TYPE(u8, u8, 0),
211 ASSIGN_FETCH_TYPE(u16, u16, 0),
212 ASSIGN_FETCH_TYPE(u32, u32, 0),
213 ASSIGN_FETCH_TYPE(u64, u64, 0),
214 ASSIGN_FETCH_TYPE(s8, u8, 1),
215 ASSIGN_FETCH_TYPE(s16, u16, 1),
216 ASSIGN_FETCH_TYPE(s32, u32, 1),
217 ASSIGN_FETCH_TYPE(s64, u64, 1),
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +0900218 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
219 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
220 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
221 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900222
223 ASSIGN_FETCH_TYPE_END
224};
225
Oleg Nesterov736288b2013-02-03 20:58:35 +0100226static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
227{
228 rwlock_init(&filter->rwlock);
229 filter->nr_systemwide = 0;
230 INIT_LIST_HEAD(&filter->perf_events);
231}
232
233static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
234{
235 return !filter->nr_systemwide && list_empty(&filter->perf_events);
236}
237
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100238static inline bool is_ret_probe(struct trace_uprobe *tu)
239{
240 return tu->consumer.ret_handler != NULL;
241}
242
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530243/*
244 * Allocate new trace_uprobe and initialize it (including uprobes).
245 */
246static struct trace_uprobe *
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100247alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530248{
249 struct trace_uprobe *tu;
250
251 if (!event || !is_good_name(event))
252 return ERR_PTR(-EINVAL);
253
254 if (!group || !is_good_name(group))
255 return ERR_PTR(-EINVAL);
256
257 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
258 if (!tu)
259 return ERR_PTR(-ENOMEM);
260
Namhyung Kim14577c32013-07-03 15:42:53 +0900261 tu->tp.call.class = &tu->tp.class;
262 tu->tp.call.name = kstrdup(event, GFP_KERNEL);
263 if (!tu->tp.call.name)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530264 goto error;
265
Namhyung Kim14577c32013-07-03 15:42:53 +0900266 tu->tp.class.system = kstrdup(group, GFP_KERNEL);
267 if (!tu->tp.class.system)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530268 goto error;
269
270 INIT_LIST_HEAD(&tu->list);
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900271 INIT_LIST_HEAD(&tu->tp.files);
Oleg Nesterova932b732013-01-31 19:47:23 +0100272 tu->consumer.handler = uprobe_dispatcher;
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100273 if (is_ret)
274 tu->consumer.ret_handler = uretprobe_dispatcher;
Oleg Nesterov736288b2013-02-03 20:58:35 +0100275 init_trace_uprobe_filter(&tu->filter);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530276 return tu;
277
278error:
Namhyung Kim14577c32013-07-03 15:42:53 +0900279 kfree(tu->tp.call.name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530280 kfree(tu);
281
282 return ERR_PTR(-ENOMEM);
283}
284
285static void free_trace_uprobe(struct trace_uprobe *tu)
286{
287 int i;
288
Namhyung Kim14577c32013-07-03 15:42:53 +0900289 for (i = 0; i < tu->tp.nr_args; i++)
290 traceprobe_free_probe_arg(&tu->tp.args[i]);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530291
292 iput(tu->inode);
Namhyung Kim14577c32013-07-03 15:42:53 +0900293 kfree(tu->tp.call.class->system);
294 kfree(tu->tp.call.name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530295 kfree(tu->filename);
296 kfree(tu);
297}
298
299static struct trace_uprobe *find_probe_event(const char *event, const char *group)
300{
301 struct trace_uprobe *tu;
302
303 list_for_each_entry(tu, &uprobe_list, list)
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400304 if (strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
Namhyung Kim14577c32013-07-03 15:42:53 +0900305 strcmp(tu->tp.call.class->system, group) == 0)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530306 return tu;
307
308 return NULL;
309}
310
311/* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400312static int unregister_trace_uprobe(struct trace_uprobe *tu)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530313{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400314 int ret;
315
316 ret = unregister_uprobe_event(tu);
317 if (ret)
318 return ret;
319
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530320 list_del(&tu->list);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530321 free_trace_uprobe(tu);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400322 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530323}
324
325/* Register a trace_uprobe and probe_event */
326static int register_trace_uprobe(struct trace_uprobe *tu)
327{
Namhyung Kim14577c32013-07-03 15:42:53 +0900328 struct trace_uprobe *old_tu;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530329 int ret;
330
331 mutex_lock(&uprobe_lock);
332
333 /* register as an event */
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400334 old_tu = find_probe_event(trace_event_name(&tu->tp.call),
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400335 tu->tp.call.class->system);
Namhyung Kim14577c32013-07-03 15:42:53 +0900336 if (old_tu) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530337 /* delete old event */
Namhyung Kim14577c32013-07-03 15:42:53 +0900338 ret = unregister_trace_uprobe(old_tu);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400339 if (ret)
340 goto end;
341 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530342
343 ret = register_uprobe_event(tu);
344 if (ret) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700345 pr_warn("Failed to register probe event(%d)\n", ret);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530346 goto end;
347 }
348
349 list_add_tail(&tu->list, &uprobe_list);
350
351end:
352 mutex_unlock(&uprobe_lock);
353
354 return ret;
355}
356
357/*
358 * Argument syntax:
Namhyung Kim306cfe22013-07-03 16:44:46 +0900359 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530360 *
361 * - Remove uprobe: -:[GRP/]EVENT
362 */
363static int create_trace_uprobe(int argc, char **argv)
364{
365 struct trace_uprobe *tu;
366 struct inode *inode;
367 char *arg, *event, *group, *filename;
368 char buf[MAX_EVENT_NAME_LEN];
369 struct path path;
370 unsigned long offset;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100371 bool is_delete, is_return;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530372 int i, ret;
373
374 inode = NULL;
375 ret = 0;
376 is_delete = false;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100377 is_return = false;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530378 event = NULL;
379 group = NULL;
380
381 /* argc must be >= 1 */
382 if (argv[0][0] == '-')
383 is_delete = true;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100384 else if (argv[0][0] == 'r')
385 is_return = true;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530386 else if (argv[0][0] != 'p') {
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100387 pr_info("Probe definition must be started with 'p', 'r' or '-'.\n");
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530388 return -EINVAL;
389 }
390
391 if (argv[0][1] == ':') {
392 event = &argv[0][2];
393 arg = strchr(event, '/');
394
395 if (arg) {
396 group = event;
397 event = arg + 1;
398 event[-1] = '\0';
399
400 if (strlen(group) == 0) {
401 pr_info("Group name is not specified\n");
402 return -EINVAL;
403 }
404 }
405 if (strlen(event) == 0) {
406 pr_info("Event name is not specified\n");
407 return -EINVAL;
408 }
409 }
410 if (!group)
411 group = UPROBE_EVENT_SYSTEM;
412
413 if (is_delete) {
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400414 int ret;
415
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530416 if (!event) {
417 pr_info("Delete command needs an event name.\n");
418 return -EINVAL;
419 }
420 mutex_lock(&uprobe_lock);
421 tu = find_probe_event(event, group);
422
423 if (!tu) {
424 mutex_unlock(&uprobe_lock);
425 pr_info("Event %s/%s doesn't exist.\n", group, event);
426 return -ENOENT;
427 }
428 /* delete an event */
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400429 ret = unregister_trace_uprobe(tu);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530430 mutex_unlock(&uprobe_lock);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400431 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530432 }
433
434 if (argc < 2) {
435 pr_info("Probe point is not specified.\n");
436 return -EINVAL;
437 }
Kenny Yu6496bb72017-01-13 08:58:34 -0800438 /* Find the last occurrence, in case the path contains ':' too. */
439 arg = strrchr(argv[1], ':');
zhangwei(Jovi)fa440632013-06-13 14:21:51 +0800440 if (!arg) {
441 ret = -EINVAL;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530442 goto fail_address_parse;
zhangwei(Jovi)fa440632013-06-13 14:21:51 +0800443 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530444
445 *arg++ = '\0';
446 filename = argv[1];
447 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
448 if (ret)
449 goto fail_address_parse;
450
David Howells7682c912015-03-17 22:26:16 +0000451 inode = igrab(d_inode(path.dentry));
Oleg Nesterov84d7ed72013-01-27 18:20:45 +0100452 path_put(&path);
453
Oleg Nesterov7e4e28c2013-01-28 17:08:47 +0100454 if (!inode || !S_ISREG(inode->i_mode)) {
Jovi Zhangd24d7db2012-07-18 18:16:44 +0800455 ret = -EINVAL;
456 goto fail_address_parse;
457 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530458
Oleg Nesterov84d7ed72013-01-27 18:20:45 +0100459 ret = kstrtoul(arg, 0, &offset);
460 if (ret)
461 goto fail_address_parse;
462
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530463 argc -= 2;
464 argv += 2;
465
466 /* setup a probe */
467 if (!event) {
Andy Shevchenkob2e902f2012-12-17 16:01:27 -0800468 char *tail;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530469 char *ptr;
470
Andy Shevchenkob2e902f2012-12-17 16:01:27 -0800471 tail = kstrdup(kbasename(filename), GFP_KERNEL);
472 if (!tail) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530473 ret = -ENOMEM;
474 goto fail_address_parse;
475 }
476
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530477 ptr = strpbrk(tail, ".-_");
478 if (ptr)
479 *ptr = '\0';
480
481 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
482 event = buf;
483 kfree(tail);
484 }
485
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100486 tu = alloc_trace_uprobe(group, event, argc, is_return);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530487 if (IS_ERR(tu)) {
488 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
489 ret = PTR_ERR(tu);
490 goto fail_address_parse;
491 }
492 tu->offset = offset;
493 tu->inode = inode;
494 tu->filename = kstrdup(filename, GFP_KERNEL);
495
496 if (!tu->filename) {
497 pr_info("Failed to allocate filename.\n");
498 ret = -ENOMEM;
499 goto error;
500 }
501
502 /* parse arguments */
503 ret = 0;
504 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Namhyung Kim14577c32013-07-03 15:42:53 +0900505 struct probe_arg *parg = &tu->tp.args[i];
506
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530507 /* Increment count for freeing args in error case */
Namhyung Kim14577c32013-07-03 15:42:53 +0900508 tu->tp.nr_args++;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530509
510 /* Parse argument name */
511 arg = strchr(argv[i], '=');
512 if (arg) {
513 *arg++ = '\0';
Namhyung Kim14577c32013-07-03 15:42:53 +0900514 parg->name = kstrdup(argv[i], GFP_KERNEL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530515 } else {
516 arg = argv[i];
517 /* If argument name is omitted, set "argN" */
518 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
Namhyung Kim14577c32013-07-03 15:42:53 +0900519 parg->name = kstrdup(buf, GFP_KERNEL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530520 }
521
Namhyung Kim14577c32013-07-03 15:42:53 +0900522 if (!parg->name) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530523 pr_info("Failed to allocate argument[%d] name.\n", i);
524 ret = -ENOMEM;
525 goto error;
526 }
527
Namhyung Kim14577c32013-07-03 15:42:53 +0900528 if (!is_good_name(parg->name)) {
529 pr_info("Invalid argument[%d] name: %s\n", i, parg->name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530530 ret = -EINVAL;
531 goto error;
532 }
533
Namhyung Kim14577c32013-07-03 15:42:53 +0900534 if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530535 pr_info("Argument[%d] name '%s' conflicts with "
536 "another field.\n", i, argv[i]);
537 ret = -EINVAL;
538 goto error;
539 }
540
541 /* Parse fetch argument */
Namhyung Kim14577c32013-07-03 15:42:53 +0900542 ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg,
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100543 is_return, false,
544 uprobes_fetch_type_table);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530545 if (ret) {
546 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
547 goto error;
548 }
549 }
550
551 ret = register_trace_uprobe(tu);
552 if (ret)
553 goto error;
554 return 0;
555
556error:
557 free_trace_uprobe(tu);
558 return ret;
559
560fail_address_parse:
Markus Elfring16a8ef22014-11-16 14:46:28 +0100561 iput(inode);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530562
Jovi Zhangd24d7db2012-07-18 18:16:44 +0800563 pr_info("Failed to parse address or file.\n");
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530564
565 return ret;
566}
567
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400568static int cleanup_all_probes(void)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530569{
570 struct trace_uprobe *tu;
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400571 int ret = 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530572
573 mutex_lock(&uprobe_lock);
574 while (!list_empty(&uprobe_list)) {
575 tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400576 ret = unregister_trace_uprobe(tu);
577 if (ret)
578 break;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530579 }
580 mutex_unlock(&uprobe_lock);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400581 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530582}
583
584/* Probes listing interfaces */
585static void *probes_seq_start(struct seq_file *m, loff_t *pos)
586{
587 mutex_lock(&uprobe_lock);
588 return seq_list_start(&uprobe_list, *pos);
589}
590
591static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
592{
593 return seq_list_next(v, &uprobe_list, pos);
594}
595
596static void probes_seq_stop(struct seq_file *m, void *v)
597{
598 mutex_unlock(&uprobe_lock);
599}
600
601static int probes_seq_show(struct seq_file *m, void *v)
602{
603 struct trace_uprobe *tu = v;
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100604 char c = is_ret_probe(tu) ? 'r' : 'p';
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530605 int i;
606
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400607 seq_printf(m, "%c:%s/%s", c, tu->tp.call.class->system,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400608 trace_event_name(&tu->tp.call));
Wang Nana2fb3382015-08-26 10:57:46 +0000609 seq_printf(m, " %s:", tu->filename);
610
611 /* Don't print "0x (null)" when offset is 0 */
612 if (tu->offset) {
Ravi Bangoria0e4d8192018-01-06 11:12:46 +0530613 seq_printf(m, "0x%px", (void *)tu->offset);
Wang Nana2fb3382015-08-26 10:57:46 +0000614 } else {
615 switch (sizeof(void *)) {
616 case 4:
617 seq_printf(m, "0x00000000");
618 break;
619 case 8:
620 default:
621 seq_printf(m, "0x0000000000000000");
622 break;
623 }
624 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530625
Namhyung Kim14577c32013-07-03 15:42:53 +0900626 for (i = 0; i < tu->tp.nr_args; i++)
627 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530628
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100629 seq_putc(m, '\n');
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530630 return 0;
631}
632
633static const struct seq_operations probes_seq_op = {
634 .start = probes_seq_start,
635 .next = probes_seq_next,
636 .stop = probes_seq_stop,
637 .show = probes_seq_show
638};
639
640static int probes_open(struct inode *inode, struct file *file)
641{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400642 int ret;
643
644 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
645 ret = cleanup_all_probes();
646 if (ret)
647 return ret;
648 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530649
650 return seq_open(file, &probes_seq_op);
651}
652
653static ssize_t probes_write(struct file *file, const char __user *buffer,
654 size_t count, loff_t *ppos)
655{
Tom Zanussi7e465ba2017-09-22 14:58:20 -0500656 return trace_parse_run_command(file, buffer, count, ppos, create_trace_uprobe);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530657}
658
659static const struct file_operations uprobe_events_ops = {
660 .owner = THIS_MODULE,
661 .open = probes_open,
662 .read = seq_read,
663 .llseek = seq_lseek,
664 .release = seq_release,
665 .write = probes_write,
666};
667
668/* Probes profiling interfaces */
669static int probes_profile_seq_show(struct seq_file *m, void *v)
670{
671 struct trace_uprobe *tu = v;
672
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400673 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400674 trace_event_name(&tu->tp.call), tu->nhit);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530675 return 0;
676}
677
678static const struct seq_operations profile_seq_op = {
679 .start = probes_seq_start,
680 .next = probes_seq_next,
681 .stop = probes_seq_stop,
682 .show = probes_profile_seq_show
683};
684
685static int profile_open(struct inode *inode, struct file *file)
686{
687 return seq_open(file, &profile_seq_op);
688}
689
690static const struct file_operations uprobe_profile_ops = {
691 .owner = THIS_MODULE,
692 .open = profile_open,
693 .read = seq_read,
694 .llseek = seq_lseek,
695 .release = seq_release,
696};
697
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900698struct uprobe_cpu_buffer {
699 struct mutex mutex;
700 void *buf;
701};
702static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
703static int uprobe_buffer_refcnt;
704
705static int uprobe_buffer_init(void)
706{
707 int cpu, err_cpu;
708
709 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
710 if (uprobe_cpu_buffer == NULL)
711 return -ENOMEM;
712
713 for_each_possible_cpu(cpu) {
714 struct page *p = alloc_pages_node(cpu_to_node(cpu),
715 GFP_KERNEL, 0);
716 if (p == NULL) {
717 err_cpu = cpu;
718 goto err;
719 }
720 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
721 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
722 }
723
724 return 0;
725
726err:
727 for_each_possible_cpu(cpu) {
728 if (cpu == err_cpu)
729 break;
730 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
731 }
732
733 free_percpu(uprobe_cpu_buffer);
734 return -ENOMEM;
735}
736
737static int uprobe_buffer_enable(void)
738{
739 int ret = 0;
740
741 BUG_ON(!mutex_is_locked(&event_mutex));
742
743 if (uprobe_buffer_refcnt++ == 0) {
744 ret = uprobe_buffer_init();
745 if (ret < 0)
746 uprobe_buffer_refcnt--;
747 }
748
749 return ret;
750}
751
752static void uprobe_buffer_disable(void)
753{
zhangwei(Jovi)6ea62152014-04-17 16:05:19 +0800754 int cpu;
755
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900756 BUG_ON(!mutex_is_locked(&event_mutex));
757
758 if (--uprobe_buffer_refcnt == 0) {
zhangwei(Jovi)6ea62152014-04-17 16:05:19 +0800759 for_each_possible_cpu(cpu)
760 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
761 cpu)->buf);
762
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900763 free_percpu(uprobe_cpu_buffer);
764 uprobe_cpu_buffer = NULL;
765 }
766}
767
768static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
769{
770 struct uprobe_cpu_buffer *ucb;
771 int cpu;
772
773 cpu = raw_smp_processor_id();
774 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
775
776 /*
777 * Use per-cpu buffers for fastest access, but we might migrate
778 * so the mutex makes sure we have sole access to it.
779 */
780 mutex_lock(&ucb->mutex);
781
782 return ucb;
783}
784
785static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
786{
787 mutex_unlock(&ucb->mutex);
788}
789
Namhyung Kima43b9702014-01-17 17:08:36 +0900790static void __uprobe_trace_func(struct trace_uprobe *tu,
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900791 unsigned long func, struct pt_regs *regs,
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900792 struct uprobe_cpu_buffer *ucb, int dsize,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400793 struct trace_event_file *trace_file)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530794{
795 struct uprobe_trace_entry_head *entry;
796 struct ring_buffer_event *event;
797 struct ring_buffer *buffer;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100798 void *data;
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900799 int size, esize;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400800 struct trace_event_call *call = &tu->tp.call;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530801
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400802 WARN_ON(call != trace_file->event_call);
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900803
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900804 if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
Oleg Nesterova51cc602013-03-30 18:02:12 +0100805 return;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530806
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -0400807 if (trace_trigger_soft_disabled(trace_file))
Namhyung Kimca3b1622014-01-17 17:08:39 +0900808 return;
809
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900810 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900811 size = esize + tu->tp.size + dsize;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400812 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900813 call->event.type, size, 0, 0);
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900814 if (!event)
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900815 return;
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900816
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530817 entry = ring_buffer_event_data(event);
Oleg Nesterov393a7362013-03-30 18:46:22 +0100818 if (is_ret_probe(tu)) {
819 entry->vaddr[0] = func;
820 entry->vaddr[1] = instruction_pointer(regs);
821 data = DATAOF_TRACE_ENTRY(entry, true);
822 } else {
823 entry->vaddr[0] = instruction_pointer(regs);
824 data = DATAOF_TRACE_ENTRY(entry, false);
825 }
826
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900827 memcpy(data, ucb->buf, tu->tp.size + dsize);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530828
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400829 event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
Oleg Nesterova51cc602013-03-30 18:02:12 +0100830}
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100831
Oleg Nesterova51cc602013-03-30 18:02:12 +0100832/* uprobe handler */
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900833static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
834 struct uprobe_cpu_buffer *ucb, int dsize)
Oleg Nesterova51cc602013-03-30 18:02:12 +0100835{
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900836 struct event_file_link *link;
837
838 if (is_ret_probe(tu))
839 return 0;
840
841 rcu_read_lock();
842 list_for_each_entry_rcu(link, &tu->tp.files, list)
843 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
844 rcu_read_unlock();
845
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100846 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530847}
848
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100849static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900850 struct pt_regs *regs,
851 struct uprobe_cpu_buffer *ucb, int dsize)
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100852{
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900853 struct event_file_link *link;
854
855 rcu_read_lock();
856 list_for_each_entry_rcu(link, &tu->tp.files, list)
857 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
858 rcu_read_unlock();
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100859}
860
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530861/* Event entry printers */
862static enum print_line_t
863print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
864{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100865 struct uprobe_trace_entry_head *entry;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530866 struct trace_seq *s = &iter->seq;
867 struct trace_uprobe *tu;
868 u8 *data;
869 int i;
870
Oleg Nesterov457d1772013-03-29 18:26:51 +0100871 entry = (struct uprobe_trace_entry_head *)iter->ent;
Namhyung Kim14577c32013-07-03 15:42:53 +0900872 tu = container_of(event, struct trace_uprobe, tp.call.event);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530873
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100874 if (is_ret_probe(tu)) {
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500875 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400876 trace_event_name(&tu->tp.call),
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500877 entry->vaddr[1], entry->vaddr[0]);
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100878 data = DATAOF_TRACE_ENTRY(entry, true);
879 } else {
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500880 trace_seq_printf(s, "%s: (0x%lx)",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400881 trace_event_name(&tu->tp.call),
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500882 entry->vaddr[0]);
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100883 data = DATAOF_TRACE_ENTRY(entry, false);
884 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530885
Namhyung Kim14577c32013-07-03 15:42:53 +0900886 for (i = 0; i < tu->tp.nr_args; i++) {
887 struct probe_arg *parg = &tu->tp.args[i];
888
889 if (!parg->type->print(s, parg->name, data + parg->offset, entry))
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500890 goto out;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530891 }
892
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500893 trace_seq_putc(s, '\n');
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530894
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500895 out:
896 return trace_handle_return(s);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530897}
898
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100899typedef bool (*filter_func_t)(struct uprobe_consumer *self,
900 enum uprobe_filter_ctx ctx,
901 struct mm_struct *mm);
902
903static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400904probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900905 filter_func_t filter)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530906{
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900907 bool enabled = trace_probe_is_enabled(&tu->tp);
908 struct event_file_link *link = NULL;
909 int ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530910
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900911 if (file) {
Oleg Nesterov48212542014-06-27 19:01:36 +0200912 if (tu->tp.flags & TP_FLAG_PROFILE)
913 return -EINTR;
914
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900915 link = kmalloc(sizeof(*link), GFP_KERNEL);
916 if (!link)
917 return -ENOMEM;
918
919 link->file = file;
920 list_add_tail_rcu(&link->list, &tu->tp.files);
921
922 tu->tp.flags |= TP_FLAG_TRACE;
Oleg Nesterov48212542014-06-27 19:01:36 +0200923 } else {
924 if (tu->tp.flags & TP_FLAG_TRACE)
925 return -EINTR;
926
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900927 tu->tp.flags |= TP_FLAG_PROFILE;
Oleg Nesterov48212542014-06-27 19:01:36 +0200928 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530929
Oleg Nesterov736288b2013-02-03 20:58:35 +0100930 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
931
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900932 if (enabled)
933 return 0;
934
Oleg Nesterovfb6bab62014-06-27 19:01:46 +0200935 ret = uprobe_buffer_enable();
936 if (ret)
937 goto err_flags;
938
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100939 tu->consumer.filter = filter;
Oleg Nesterova932b732013-01-31 19:47:23 +0100940 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
Oleg Nesterovfb6bab62014-06-27 19:01:46 +0200941 if (ret)
942 goto err_buffer;
Oleg Nesterov41618242013-01-27 18:36:24 +0100943
Oleg Nesterovfb6bab62014-06-27 19:01:46 +0200944 return 0;
945
946 err_buffer:
947 uprobe_buffer_disable();
948
949 err_flags:
950 if (file) {
951 list_del(&link->list);
952 kfree(link);
953 tu->tp.flags &= ~TP_FLAG_TRACE;
954 } else {
955 tu->tp.flags &= ~TP_FLAG_PROFILE;
956 }
Oleg Nesterov41618242013-01-27 18:36:24 +0100957 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530958}
959
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900960static void
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400961probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530962{
Namhyung Kim14577c32013-07-03 15:42:53 +0900963 if (!trace_probe_is_enabled(&tu->tp))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530964 return;
965
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900966 if (file) {
967 struct event_file_link *link;
968
969 link = find_event_file_link(&tu->tp, file);
970 if (!link)
971 return;
972
973 list_del_rcu(&link->list);
974 /* synchronize with u{,ret}probe_trace_func */
975 synchronize_sched();
976 kfree(link);
977
978 if (!list_empty(&tu->tp.files))
979 return;
980 }
981
Oleg Nesterov736288b2013-02-03 20:58:35 +0100982 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
983
Oleg Nesterova932b732013-01-31 19:47:23 +0100984 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900985 tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900986
987 uprobe_buffer_disable();
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530988}
989
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400990static int uprobe_event_define_fields(struct trace_event_call *event_call)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530991{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100992 int ret, i, size;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530993 struct uprobe_trace_entry_head field;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100994 struct trace_uprobe *tu = event_call->data;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530995
Oleg Nesterov4d1298e2013-03-30 19:23:15 +0100996 if (is_ret_probe(tu)) {
997 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
998 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
999 size = SIZEOF_TRACE_ENTRY(true);
1000 } else {
1001 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
1002 size = SIZEOF_TRACE_ENTRY(false);
1003 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301004 /* Set argument names as fields */
Namhyung Kim14577c32013-07-03 15:42:53 +09001005 for (i = 0; i < tu->tp.nr_args; i++) {
1006 struct probe_arg *parg = &tu->tp.args[i];
1007
1008 ret = trace_define_field(event_call, parg->type->fmttype,
1009 parg->name, size + parg->offset,
1010 parg->type->size, parg->type->is_signed,
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301011 FILTER_OTHER);
1012
1013 if (ret)
1014 return ret;
1015 }
1016 return 0;
1017}
1018
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301019#ifdef CONFIG_PERF_EVENTS
Oleg Nesterov31ba3342013-02-04 17:11:58 +01001020static bool
1021__uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
1022{
1023 struct perf_event *event;
1024
1025 if (filter->nr_systemwide)
1026 return true;
1027
1028 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
Peter Zijlstra50f16a82015-03-05 22:10:19 +01001029 if (event->hw.target->mm == mm)
Oleg Nesterov31ba3342013-02-04 17:11:58 +01001030 return true;
1031 }
1032
1033 return false;
1034}
1035
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001036static inline bool
1037uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1038{
Peter Zijlstra50f16a82015-03-05 22:10:19 +01001039 return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001040}
1041
Oleg Nesterovce5f36a2014-04-24 13:26:01 +02001042static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1043{
1044 bool done;
1045
1046 write_lock(&tu->filter.rwlock);
Peter Zijlstra50f16a82015-03-05 22:10:19 +01001047 if (event->hw.target) {
Oleg Nesterovce5f36a2014-04-24 13:26:01 +02001048 list_del(&event->hw.tp_list);
1049 done = tu->filter.nr_systemwide ||
Peter Zijlstra50f16a82015-03-05 22:10:19 +01001050 (event->hw.target->flags & PF_EXITING) ||
Oleg Nesterovce5f36a2014-04-24 13:26:01 +02001051 uprobe_filter_event(tu, event);
1052 } else {
1053 tu->filter.nr_systemwide--;
1054 done = tu->filter.nr_systemwide;
1055 }
1056 write_unlock(&tu->filter.rwlock);
1057
1058 if (!done)
Oleg Nesterov927d6872014-04-24 13:33:31 +02001059 return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
Oleg Nesterovce5f36a2014-04-24 13:26:01 +02001060
1061 return 0;
1062}
1063
Oleg Nesterov736288b2013-02-03 20:58:35 +01001064static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1065{
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001066 bool done;
Oleg Nesterov927d6872014-04-24 13:33:31 +02001067 int err;
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001068
Oleg Nesterov736288b2013-02-03 20:58:35 +01001069 write_lock(&tu->filter.rwlock);
Peter Zijlstra50f16a82015-03-05 22:10:19 +01001070 if (event->hw.target) {
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001071 /*
1072 * event->parent != NULL means copy_process(), we can avoid
1073 * uprobe_apply(). current->mm must be probed and we can rely
1074 * on dup_mmap() which preserves the already installed bp's.
1075 *
1076 * attr.enable_on_exec means that exec/mmap will install the
1077 * breakpoints we need.
1078 */
1079 done = tu->filter.nr_systemwide ||
1080 event->parent || event->attr.enable_on_exec ||
1081 uprobe_filter_event(tu, event);
Oleg Nesterov736288b2013-02-03 20:58:35 +01001082 list_add(&event->hw.tp_list, &tu->filter.perf_events);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001083 } else {
1084 done = tu->filter.nr_systemwide;
Oleg Nesterov736288b2013-02-03 20:58:35 +01001085 tu->filter.nr_systemwide++;
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001086 }
Oleg Nesterov736288b2013-02-03 20:58:35 +01001087 write_unlock(&tu->filter.rwlock);
1088
Oleg Nesterov927d6872014-04-24 13:33:31 +02001089 err = 0;
1090 if (!done) {
1091 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1092 if (err)
1093 uprobe_perf_close(tu, event);
1094 }
1095 return err;
Oleg Nesterov736288b2013-02-03 20:58:35 +01001096}
1097
Oleg Nesterov31ba3342013-02-04 17:11:58 +01001098static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1099 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1100{
1101 struct trace_uprobe *tu;
1102 int ret;
1103
1104 tu = container_of(uc, struct trace_uprobe, consumer);
1105 read_lock(&tu->filter.rwlock);
1106 ret = __uprobe_perf_filter(&tu->filter, mm);
1107 read_unlock(&tu->filter.rwlock);
1108
1109 return ret;
1110}
1111
Namhyung Kima43b9702014-01-17 17:08:36 +09001112static void __uprobe_perf_func(struct trace_uprobe *tu,
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001113 unsigned long func, struct pt_regs *regs,
1114 struct uprobe_cpu_buffer *ucb, int dsize)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301115{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001116 struct trace_event_call *call = &tu->tp.call;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301117 struct uprobe_trace_entry_head *entry;
1118 struct hlist_head *head;
Oleg Nesterov457d1772013-03-29 18:26:51 +01001119 void *data;
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001120 int size, esize;
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001121 int rctx;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301122
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001123 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
Wang Nan04a22fa2015-07-01 02:13:50 +00001124 return;
1125
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001126 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1127
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001128 size = esize + tu->tp.size + dsize;
1129 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1130 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1131 return;
1132
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301133 preempt_disable();
Oleg Nesterov515619f2013-04-13 15:36:49 +02001134 head = this_cpu_ptr(call->perf_events);
1135 if (hlist_empty(head))
1136 goto out;
1137
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001138 entry = perf_trace_buf_alloc(size, NULL, &rctx);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301139 if (!entry)
1140 goto out;
1141
Oleg Nesterov393a7362013-03-30 18:46:22 +01001142 if (is_ret_probe(tu)) {
1143 entry->vaddr[0] = func;
Oleg Nesterov32520b22013-04-10 16:25:49 +02001144 entry->vaddr[1] = instruction_pointer(regs);
Oleg Nesterov393a7362013-03-30 18:46:22 +01001145 data = DATAOF_TRACE_ENTRY(entry, true);
1146 } else {
Oleg Nesterov32520b22013-04-10 16:25:49 +02001147 entry->vaddr[0] = instruction_pointer(regs);
Oleg Nesterov393a7362013-03-30 18:46:22 +01001148 data = DATAOF_TRACE_ENTRY(entry, false);
1149 }
1150
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001151 memcpy(data, ucb->buf, tu->tp.size + dsize);
Namhyung Kim14577c32013-07-03 15:42:53 +09001152
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001153 if (size - esize > tu->tp.size + dsize) {
1154 int len = tu->tp.size + dsize;
1155
1156 memset(data + len, 0, size - esize - len);
Namhyung Kim14577c32013-07-03 15:42:53 +09001157 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301158
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001159 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02001160 head, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301161 out:
1162 preempt_enable();
Oleg Nesterova51cc602013-03-30 18:02:12 +01001163}
1164
1165/* uprobe profile handler */
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001166static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1167 struct uprobe_cpu_buffer *ucb, int dsize)
Oleg Nesterova51cc602013-03-30 18:02:12 +01001168{
1169 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1170 return UPROBE_HANDLER_REMOVE;
1171
Oleg Nesterov393a7362013-03-30 18:46:22 +01001172 if (!is_ret_probe(tu))
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001173 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
Oleg Nesterovf42d24a2013-02-04 17:48:34 +01001174 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301175}
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001176
1177static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001178 struct pt_regs *regs,
1179 struct uprobe_cpu_buffer *ucb, int dsize)
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001180{
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001181 __uprobe_perf_func(tu, func, regs, ucb, dsize);
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001182}
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301183#endif /* CONFIG_PERF_EVENTS */
1184
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001185static int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001186trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001187 void *data)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301188{
Oleg Nesterov457d1772013-03-29 18:26:51 +01001189 struct trace_uprobe *tu = event->data;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001190 struct trace_event_file *file = data;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301191
1192 switch (type) {
1193 case TRACE_REG_REGISTER:
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001194 return probe_event_enable(tu, file, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301195
1196 case TRACE_REG_UNREGISTER:
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001197 probe_event_disable(tu, file);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301198 return 0;
1199
1200#ifdef CONFIG_PERF_EVENTS
1201 case TRACE_REG_PERF_REGISTER:
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001202 return probe_event_enable(tu, NULL, uprobe_perf_filter);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301203
1204 case TRACE_REG_PERF_UNREGISTER:
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001205 probe_event_disable(tu, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301206 return 0;
Oleg Nesterov736288b2013-02-03 20:58:35 +01001207
1208 case TRACE_REG_PERF_OPEN:
1209 return uprobe_perf_open(tu, data);
1210
1211 case TRACE_REG_PERF_CLOSE:
1212 return uprobe_perf_close(tu, data);
1213
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301214#endif
1215 default:
1216 return 0;
1217 }
1218 return 0;
1219}
1220
1221static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1222{
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301223 struct trace_uprobe *tu;
Namhyung Kimb7e0bf32013-11-25 13:42:47 +09001224 struct uprobe_dispatch_data udd;
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001225 struct uprobe_cpu_buffer *ucb;
1226 int dsize, esize;
Oleg Nesterovf42d24a2013-02-04 17:48:34 +01001227 int ret = 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301228
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001229
Oleg Nesterova932b732013-01-31 19:47:23 +01001230 tu = container_of(con, struct trace_uprobe, consumer);
Oleg Nesterov1b47aef2013-01-31 19:55:27 +01001231 tu->nhit++;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301232
Namhyung Kimb7e0bf32013-11-25 13:42:47 +09001233 udd.tu = tu;
1234 udd.bp_addr = instruction_pointer(regs);
1235
1236 current->utask->vaddr = (unsigned long) &udd;
1237
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001238 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1239 return 0;
1240
1241 dsize = __get_data_size(&tu->tp, regs);
1242 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1243
1244 ucb = uprobe_buffer_get();
1245 store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
1246
Namhyung Kim14577c32013-07-03 15:42:53 +09001247 if (tu->tp.flags & TP_FLAG_TRACE)
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001248 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301249
1250#ifdef CONFIG_PERF_EVENTS
Namhyung Kim14577c32013-07-03 15:42:53 +09001251 if (tu->tp.flags & TP_FLAG_PROFILE)
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001252 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301253#endif
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001254 uprobe_buffer_put(ucb);
Oleg Nesterovf42d24a2013-02-04 17:48:34 +01001255 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301256}
1257
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001258static int uretprobe_dispatcher(struct uprobe_consumer *con,
1259 unsigned long func, struct pt_regs *regs)
1260{
1261 struct trace_uprobe *tu;
Namhyung Kimb7e0bf32013-11-25 13:42:47 +09001262 struct uprobe_dispatch_data udd;
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001263 struct uprobe_cpu_buffer *ucb;
1264 int dsize, esize;
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001265
1266 tu = container_of(con, struct trace_uprobe, consumer);
1267
Namhyung Kimb7e0bf32013-11-25 13:42:47 +09001268 udd.tu = tu;
1269 udd.bp_addr = func;
1270
1271 current->utask->vaddr = (unsigned long) &udd;
1272
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001273 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1274 return 0;
1275
1276 dsize = __get_data_size(&tu->tp, regs);
1277 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1278
1279 ucb = uprobe_buffer_get();
1280 store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
1281
Namhyung Kim14577c32013-07-03 15:42:53 +09001282 if (tu->tp.flags & TP_FLAG_TRACE)
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001283 uretprobe_trace_func(tu, func, regs, ucb, dsize);
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001284
1285#ifdef CONFIG_PERF_EVENTS
Namhyung Kim14577c32013-07-03 15:42:53 +09001286 if (tu->tp.flags & TP_FLAG_PROFILE)
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001287 uretprobe_perf_func(tu, func, regs, ucb, dsize);
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001288#endif
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001289 uprobe_buffer_put(ucb);
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001290 return 0;
1291}
1292
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301293static struct trace_event_functions uprobe_funcs = {
1294 .trace = print_uprobe_event
1295};
1296
Song Liu33ea4b22017-12-06 14:45:16 -08001297static inline void init_trace_event_call(struct trace_uprobe *tu,
1298 struct trace_event_call *call)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301299{
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301300 INIT_LIST_HEAD(&call->class->fields);
1301 call->event.funcs = &uprobe_funcs;
1302 call->class->define_fields = uprobe_event_define_fields;
1303
Song Liu33ea4b22017-12-06 14:45:16 -08001304 call->flags = TRACE_EVENT_FL_UPROBE;
1305 call->class->reg = trace_uprobe_register;
1306 call->data = tu;
1307}
1308
1309static int register_uprobe_event(struct trace_uprobe *tu)
1310{
1311 struct trace_event_call *call = &tu->tp.call;
1312 int ret = 0;
1313
1314 init_trace_event_call(tu, call);
1315
Namhyung Kim5bf652a2013-07-03 16:09:02 +09001316 if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301317 return -ENOMEM;
1318
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001319 ret = register_trace_event(&call->event);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301320 if (!ret) {
1321 kfree(call->print_fmt);
1322 return -ENODEV;
1323 }
Oleg Nesterovede392a2014-07-15 20:48:24 +02001324
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301325 ret = trace_add_event_call(call);
1326
1327 if (ret) {
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001328 pr_info("Failed to register uprobe event: %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001329 trace_event_name(call));
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301330 kfree(call->print_fmt);
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001331 unregister_trace_event(&call->event);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301332 }
1333
1334 return ret;
1335}
1336
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001337static int unregister_uprobe_event(struct trace_uprobe *tu)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301338{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001339 int ret;
1340
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301341 /* tu->event is unregistered in trace_remove_event_call() */
Namhyung Kim14577c32013-07-03 15:42:53 +09001342 ret = trace_remove_event_call(&tu->tp.call);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001343 if (ret)
1344 return ret;
Namhyung Kim14577c32013-07-03 15:42:53 +09001345 kfree(tu->tp.call.print_fmt);
1346 tu->tp.call.print_fmt = NULL;
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001347 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301348}
1349
Song Liu33ea4b22017-12-06 14:45:16 -08001350#ifdef CONFIG_PERF_EVENTS
1351struct trace_event_call *
1352create_local_trace_uprobe(char *name, unsigned long offs, bool is_return)
1353{
1354 struct trace_uprobe *tu;
1355 struct inode *inode;
1356 struct path path;
1357 int ret;
1358
1359 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1360 if (ret)
1361 return ERR_PTR(ret);
1362
1363 inode = igrab(d_inode(path.dentry));
1364 path_put(&path);
1365
1366 if (!inode || !S_ISREG(inode->i_mode)) {
1367 iput(inode);
1368 return ERR_PTR(-EINVAL);
1369 }
1370
1371 /*
1372 * local trace_kprobes are not added to probe_list, so they are never
1373 * searched in find_trace_kprobe(). Therefore, there is no concern of
1374 * duplicated name "DUMMY_EVENT" here.
1375 */
1376 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1377 is_return);
1378
1379 if (IS_ERR(tu)) {
1380 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1381 (int)PTR_ERR(tu));
1382 return ERR_CAST(tu);
1383 }
1384
1385 tu->offset = offs;
1386 tu->inode = inode;
1387 tu->filename = kstrdup(name, GFP_KERNEL);
1388 init_trace_event_call(tu, &tu->tp.call);
1389
1390 if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1391 ret = -ENOMEM;
1392 goto error;
1393 }
1394
1395 return &tu->tp.call;
1396error:
1397 free_trace_uprobe(tu);
1398 return ERR_PTR(ret);
1399}
1400
1401void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1402{
1403 struct trace_uprobe *tu;
1404
1405 tu = container_of(event_call, struct trace_uprobe, tp.call);
1406
1407 kfree(tu->tp.call.print_fmt);
1408 tu->tp.call.print_fmt = NULL;
1409
1410 free_trace_uprobe(tu);
1411}
1412#endif /* CONFIG_PERF_EVENTS */
1413
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301414/* Make a trace interface for controling probe points */
1415static __init int init_uprobe_trace(void)
1416{
1417 struct dentry *d_tracer;
1418
1419 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05001420 if (IS_ERR(d_tracer))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301421 return 0;
1422
1423 trace_create_file("uprobe_events", 0644, d_tracer,
1424 NULL, &uprobe_events_ops);
1425 /* Profile interface */
1426 trace_create_file("uprobe_profile", 0444, d_tracer,
1427 NULL, &uprobe_profile_ops);
1428 return 0;
1429}
1430
1431fs_initcall(init_uprobe_trace);