Commit | Line | Data |
---|---|---|
bcea3f96 | 1 | // SPDX-License-Identifier: GPL-2.0 |
f3f096cf SD |
2 | /* |
3 | * uprobes-based tracing events | |
4 | * | |
f3f096cf SD |
5 | * Copyright (C) IBM Corporation, 2010-2012 |
6 | * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com> | |
7 | */ | |
ea6eb5e7 | 8 | #define pr_fmt(fmt) "trace_uprobe: " fmt |
f3f096cf | 9 | |
17911ff3 | 10 | #include <linux/security.h> |
0597c49c | 11 | #include <linux/ctype.h> |
f3f096cf SD |
12 | #include <linux/module.h> |
13 | #include <linux/uaccess.h> | |
14 | #include <linux/uprobes.h> | |
15 | #include <linux/namei.h> | |
b2e902f0 | 16 | #include <linux/string.h> |
b2d09103 | 17 | #include <linux/rculist.h> |
f3f096cf | 18 | |
0597c49c | 19 | #include "trace_dynevent.h" |
f3f096cf | 20 | #include "trace_probe.h" |
53305928 | 21 | #include "trace_probe_tmpl.h" |
f3f096cf SD |
22 | |
23 | #define UPROBE_EVENT_SYSTEM "uprobes" | |
24 | ||
457d1772 ON |
25 | struct uprobe_trace_entry_head { |
26 | struct trace_entry ent; | |
27 | unsigned long vaddr[]; | |
28 | }; | |
29 | ||
30 | #define SIZEOF_TRACE_ENTRY(is_return) \ | |
31 | (sizeof(struct uprobe_trace_entry_head) + \ | |
32 | sizeof(unsigned long) * (is_return ? 2 : 1)) | |
33 | ||
34 | #define DATAOF_TRACE_ENTRY(entry, is_return) \ | |
35 | ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return)) | |
36 | ||
d262271d | 37 | static int trace_uprobe_create(const char *raw_command); |
0597c49c MH |
38 | static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev); |
39 | static int trace_uprobe_release(struct dyn_event *ev); | |
40 | static bool trace_uprobe_is_busy(struct dyn_event *ev); | |
41 | static bool trace_uprobe_match(const char *system, const char *event, | |
30199137 | 42 | int argc, const char **argv, struct dyn_event *ev); |
0597c49c MH |
43 | |
44 | static struct dyn_event_operations trace_uprobe_ops = { | |
45 | .create = trace_uprobe_create, | |
46 | .show = trace_uprobe_show, | |
47 | .is_busy = trace_uprobe_is_busy, | |
48 | .free = trace_uprobe_release, | |
49 | .match = trace_uprobe_match, | |
50 | }; | |
51 | ||
f3f096cf SD |
52 | /* |
53 | * uprobe event core functions | |
54 | */ | |
f3f096cf | 55 | struct trace_uprobe { |
0597c49c | 56 | struct dyn_event devent; |
a932b738 | 57 | struct uprobe_consumer consumer; |
0c92c7a3 | 58 | struct path path; |
f3f096cf SD |
59 | struct inode *inode; |
60 | char *filename; | |
61 | unsigned long offset; | |
1cc33161 | 62 | unsigned long ref_ctr_offset; |
f3f096cf | 63 | unsigned long nhit; |
14577c39 | 64 | struct trace_probe tp; |
f3f096cf SD |
65 | }; |
66 | ||
0597c49c MH |
67 | static bool is_trace_uprobe(struct dyn_event *ev) |
68 | { | |
69 | return ev->ops == &trace_uprobe_ops; | |
70 | } | |
71 | ||
72 | static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev) | |
73 | { | |
74 | return container_of(ev, struct trace_uprobe, devent); | |
75 | } | |
76 | ||
77 | /** | |
78 | * for_each_trace_uprobe - iterate over the trace_uprobe list | |
79 | * @pos: the struct trace_uprobe * for each entry | |
80 | * @dpos: the struct dyn_event * to use as a loop cursor | |
81 | */ | |
82 | #define for_each_trace_uprobe(pos, dpos) \ | |
83 | for_each_dyn_event(dpos) \ | |
84 | if (is_trace_uprobe(dpos) && (pos = to_trace_uprobe(dpos))) | |
85 | ||
f3f096cf | 86 | static int register_uprobe_event(struct trace_uprobe *tu); |
c6c2401d | 87 | static int unregister_uprobe_event(struct trace_uprobe *tu); |
f3f096cf | 88 | |
b7e0bf34 NK |
89 | struct uprobe_dispatch_data { |
90 | struct trace_uprobe *tu; | |
91 | unsigned long bp_addr; | |
92 | }; | |
93 | ||
f3f096cf | 94 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs); |
c1ae5c75 ON |
95 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
96 | unsigned long func, struct pt_regs *regs); | |
f3f096cf | 97 | |
3fd996a2 NK |
98 | #ifdef CONFIG_STACK_GROWSUP |
99 | static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) | |
100 | { | |
101 | return addr - (n * sizeof(long)); | |
102 | } | |
103 | #else | |
104 | static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) | |
105 | { | |
106 | return addr + (n * sizeof(long)); | |
107 | } | |
108 | #endif | |
109 | ||
110 | static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n) | |
111 | { | |
112 | unsigned long ret; | |
113 | unsigned long addr = user_stack_pointer(regs); | |
114 | ||
115 | addr = adjust_stack_addr(addr, n); | |
116 | ||
117 | if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret))) | |
118 | return 0; | |
119 | ||
120 | return ret; | |
121 | } | |
122 | ||
123 | /* | |
124 | * Uprobes-specific fetch functions | |
125 | */ | |
53305928 | 126 | static nokprobe_inline int |
9b960a38 | 127 | probe_mem_read(void *dest, void *src, size_t size) |
53305928 MH |
128 | { |
129 | void __user *vaddr = (void __force __user *)src; | |
130 | ||
f3f58935 | 131 | return copy_from_user(dest, vaddr, size) ? -EFAULT : 0; |
5baaa59e | 132 | } |
e65f7ae7 MH |
133 | |
134 | static nokprobe_inline int | |
135 | probe_mem_read_user(void *dest, void *src, size_t size) | |
136 | { | |
137 | return probe_mem_read(dest, src, size); | |
138 | } | |
139 | ||
5baaa59e NK |
140 | /* |
141 | * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max | |
142 | * length and relative data location. | |
143 | */ | |
9178412d MH |
144 | static nokprobe_inline int |
145 | fetch_store_string(unsigned long addr, void *dest, void *base) | |
5baaa59e NK |
146 | { |
147 | long ret; | |
9178412d MH |
148 | u32 loc = *(u32 *)dest; |
149 | int maxlen = get_loc_len(loc); | |
150 | u8 *dst = get_loc_data(dest, base); | |
5baaa59e NK |
151 | void __user *src = (void __force __user *) addr; |
152 | ||
9178412d MH |
153 | if (unlikely(!maxlen)) |
154 | return -ENOMEM; | |
5baaa59e | 155 | |
4dd537ac MH |
156 | if (addr == FETCH_TOKEN_COMM) |
157 | ret = strlcpy(dst, current->comm, maxlen); | |
158 | else | |
159 | ret = strncpy_from_user(dst, src, maxlen); | |
9178412d MH |
160 | if (ret >= 0) { |
161 | if (ret == maxlen) | |
162 | dst[ret - 1] = '\0'; | |
0722069a AZ |
163 | else |
164 | /* | |
165 | * Include the terminating null byte. In this case it | |
166 | * was copied by strncpy_from_user but not accounted | |
167 | * for in ret. | |
168 | */ | |
169 | ret++; | |
9178412d | 170 | *(u32 *)dest = make_data_loc(ret, (void *)dst - base); |
5baaa59e | 171 | } |
9178412d MH |
172 | |
173 | return ret; | |
5baaa59e NK |
174 | } |
175 | ||
88903c46 MH |
176 | static nokprobe_inline int |
177 | fetch_store_string_user(unsigned long addr, void *dest, void *base) | |
178 | { | |
179 | return fetch_store_string(addr, dest, base); | |
180 | } | |
181 | ||
53305928 | 182 | /* Return the length of string -- including null terminal byte */ |
9178412d MH |
183 | static nokprobe_inline int |
184 | fetch_store_strlen(unsigned long addr) | |
5baaa59e NK |
185 | { |
186 | int len; | |
187 | void __user *vaddr = (void __force __user *) addr; | |
188 | ||
4dd537ac MH |
189 | if (addr == FETCH_TOKEN_COMM) |
190 | len = strlen(current->comm) + 1; | |
191 | else | |
192 | len = strnlen_user(vaddr, MAX_STRING_SIZE); | |
5baaa59e | 193 | |
9178412d | 194 | return (len > MAX_STRING_SIZE) ? 0 : len; |
5baaa59e | 195 | } |
3fd996a2 | 196 | |
88903c46 MH |
197 | static nokprobe_inline int |
198 | fetch_store_strlen_user(unsigned long addr) | |
199 | { | |
200 | return fetch_store_strlen(addr); | |
201 | } | |
202 | ||
53305928 | 203 | static unsigned long translate_user_vaddr(unsigned long file_offset) |
b7e0bf34 NK |
204 | { |
205 | unsigned long base_addr; | |
206 | struct uprobe_dispatch_data *udd; | |
207 | ||
208 | udd = (void *) current->utask->vaddr; | |
209 | ||
210 | base_addr = udd->bp_addr - udd->tu->offset; | |
53305928 | 211 | return base_addr + file_offset; |
b7e0bf34 | 212 | } |
b7e0bf34 | 213 | |
53305928 MH |
214 | /* Note that we don't verify it, since the code does not come from user space */ |
215 | static int | |
8565a45d | 216 | process_fetch_insn(struct fetch_insn *code, void *rec, void *dest, |
9178412d | 217 | void *base) |
53305928 | 218 | { |
8565a45d | 219 | struct pt_regs *regs = rec; |
53305928 | 220 | unsigned long val; |
53305928 MH |
221 | |
222 | /* 1st stage: get value from context */ | |
223 | switch (code->op) { | |
224 | case FETCH_OP_REG: | |
225 | val = regs_get_register(regs, code->param); | |
226 | break; | |
227 | case FETCH_OP_STACK: | |
228 | val = get_user_stack_nth(regs, code->param); | |
229 | break; | |
230 | case FETCH_OP_STACKP: | |
231 | val = user_stack_pointer(regs); | |
232 | break; | |
233 | case FETCH_OP_RETVAL: | |
234 | val = regs_return_value(regs); | |
235 | break; | |
236 | case FETCH_OP_IMM: | |
237 | val = code->immediate; | |
238 | break; | |
4dd537ac MH |
239 | case FETCH_OP_COMM: |
240 | val = FETCH_TOKEN_COMM; | |
241 | break; | |
a42e3c4d MH |
242 | case FETCH_OP_DATA: |
243 | val = (unsigned long)code->data; | |
244 | break; | |
53305928 MH |
245 | case FETCH_OP_FOFFS: |
246 | val = translate_user_vaddr(code->immediate); | |
247 | break; | |
248 | default: | |
249 | return -EILSEQ; | |
250 | } | |
251 | code++; | |
252 | ||
9b960a38 | 253 | return process_fetch_insn_bottom(code, val, dest, base); |
53305928 MH |
254 | } |
255 | NOKPROBE_SYMBOL(process_fetch_insn) | |
256 | ||
736288ba ON |
257 | static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter) |
258 | { | |
259 | rwlock_init(&filter->rwlock); | |
260 | filter->nr_systemwide = 0; | |
261 | INIT_LIST_HEAD(&filter->perf_events); | |
262 | } | |
263 | ||
264 | static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter) | |
265 | { | |
266 | return !filter->nr_systemwide && list_empty(&filter->perf_events); | |
267 | } | |
268 | ||
c1ae5c75 ON |
269 | static inline bool is_ret_probe(struct trace_uprobe *tu) |
270 | { | |
271 | return tu->consumer.ret_handler != NULL; | |
272 | } | |
273 | ||
0597c49c MH |
274 | static bool trace_uprobe_is_busy(struct dyn_event *ev) |
275 | { | |
276 | struct trace_uprobe *tu = to_trace_uprobe(ev); | |
277 | ||
278 | return trace_probe_is_enabled(&tu->tp); | |
279 | } | |
280 | ||
ab10d69e MH |
281 | static bool trace_uprobe_match_command_head(struct trace_uprobe *tu, |
282 | int argc, const char **argv) | |
283 | { | |
284 | char buf[MAX_ARGSTR_LEN + 1]; | |
285 | int len; | |
286 | ||
287 | if (!argc) | |
288 | return true; | |
289 | ||
290 | len = strlen(tu->filename); | |
291 | if (strncmp(tu->filename, argv[0], len) || argv[0][len] != ':') | |
292 | return false; | |
293 | ||
294 | if (tu->ref_ctr_offset == 0) | |
295 | snprintf(buf, sizeof(buf), "0x%0*lx", | |
296 | (int)(sizeof(void *) * 2), tu->offset); | |
297 | else | |
298 | snprintf(buf, sizeof(buf), "0x%0*lx(0x%lx)", | |
299 | (int)(sizeof(void *) * 2), tu->offset, | |
300 | tu->ref_ctr_offset); | |
301 | if (strcmp(buf, &argv[0][len + 1])) | |
302 | return false; | |
303 | ||
304 | argc--; argv++; | |
305 | ||
306 | return trace_probe_match_command_args(&tu->tp, argc, argv); | |
307 | } | |
308 | ||
0597c49c | 309 | static bool trace_uprobe_match(const char *system, const char *event, |
30199137 | 310 | int argc, const char **argv, struct dyn_event *ev) |
0597c49c MH |
311 | { |
312 | struct trace_uprobe *tu = to_trace_uprobe(ev); | |
313 | ||
b55ce203 | 314 | return strcmp(trace_probe_name(&tu->tp), event) == 0 && |
ab10d69e MH |
315 | (!system || strcmp(trace_probe_group_name(&tu->tp), system) == 0) && |
316 | trace_uprobe_match_command_head(tu, argc, argv); | |
0597c49c MH |
317 | } |
318 | ||
60d53e2c MH |
319 | static nokprobe_inline struct trace_uprobe * |
320 | trace_uprobe_primary_from_call(struct trace_event_call *call) | |
321 | { | |
322 | struct trace_probe *tp; | |
323 | ||
324 | tp = trace_probe_primary_from_call(call); | |
325 | if (WARN_ON_ONCE(!tp)) | |
326 | return NULL; | |
327 | ||
328 | return container_of(tp, struct trace_uprobe, tp); | |
329 | } | |
330 | ||
f3f096cf SD |
331 | /* |
332 | * Allocate new trace_uprobe and initialize it (including uprobes). | |
333 | */ | |
334 | static struct trace_uprobe * | |
c1ae5c75 | 335 | alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret) |
f3f096cf SD |
336 | { |
337 | struct trace_uprobe *tu; | |
455b2899 | 338 | int ret; |
f3f096cf | 339 | |
845cbf3e | 340 | tu = kzalloc(struct_size(tu, tp.args, nargs), GFP_KERNEL); |
f3f096cf SD |
341 | if (!tu) |
342 | return ERR_PTR(-ENOMEM); | |
343 | ||
b61387cb | 344 | ret = trace_probe_init(&tu->tp, event, group, true); |
455b2899 | 345 | if (ret < 0) |
f3f096cf SD |
346 | goto error; |
347 | ||
0597c49c | 348 | dyn_event_init(&tu->devent, &trace_uprobe_ops); |
a932b738 | 349 | tu->consumer.handler = uprobe_dispatcher; |
c1ae5c75 ON |
350 | if (is_ret) |
351 | tu->consumer.ret_handler = uretprobe_dispatcher; | |
b61387cb | 352 | init_trace_uprobe_filter(tu->tp.event->filter); |
f3f096cf SD |
353 | return tu; |
354 | ||
355 | error: | |
f3f096cf SD |
356 | kfree(tu); |
357 | ||
455b2899 | 358 | return ERR_PTR(ret); |
f3f096cf SD |
359 | } |
360 | ||
361 | static void free_trace_uprobe(struct trace_uprobe *tu) | |
362 | { | |
0597c49c MH |
363 | if (!tu) |
364 | return; | |
365 | ||
0c92c7a3 | 366 | path_put(&tu->path); |
455b2899 | 367 | trace_probe_cleanup(&tu->tp); |
f3f096cf SD |
368 | kfree(tu->filename); |
369 | kfree(tu); | |
370 | } | |
371 | ||
372 | static struct trace_uprobe *find_probe_event(const char *event, const char *group) | |
373 | { | |
0597c49c | 374 | struct dyn_event *pos; |
f3f096cf SD |
375 | struct trace_uprobe *tu; |
376 | ||
0597c49c | 377 | for_each_trace_uprobe(tu, pos) |
b55ce203 MH |
378 | if (strcmp(trace_probe_name(&tu->tp), event) == 0 && |
379 | strcmp(trace_probe_group_name(&tu->tp), group) == 0) | |
f3f096cf SD |
380 | return tu; |
381 | ||
382 | return NULL; | |
383 | } | |
384 | ||
0597c49c | 385 | /* Unregister a trace_uprobe and probe_event */ |
c6c2401d | 386 | static int unregister_trace_uprobe(struct trace_uprobe *tu) |
f3f096cf | 387 | { |
c6c2401d SRRH |
388 | int ret; |
389 | ||
41af3cf5 MH |
390 | if (trace_probe_has_sibling(&tu->tp)) |
391 | goto unreg; | |
392 | ||
1d18538e SRV |
393 | /* If there's a reference to the dynamic event */ |
394 | if (trace_event_dyn_busy(trace_probe_event_call(&tu->tp))) | |
395 | return -EBUSY; | |
396 | ||
c6c2401d SRRH |
397 | ret = unregister_uprobe_event(tu); |
398 | if (ret) | |
399 | return ret; | |
400 | ||
41af3cf5 | 401 | unreg: |
0597c49c | 402 | dyn_event_remove(&tu->devent); |
41af3cf5 | 403 | trace_probe_unlink(&tu->tp); |
f3f096cf | 404 | free_trace_uprobe(tu); |
c6c2401d | 405 | return 0; |
f3f096cf SD |
406 | } |
407 | ||
fe60b0ce MH |
408 | static bool trace_uprobe_has_same_uprobe(struct trace_uprobe *orig, |
409 | struct trace_uprobe *comp) | |
410 | { | |
411 | struct trace_probe_event *tpe = orig->tp.event; | |
fe60b0ce MH |
412 | struct inode *comp_inode = d_real_inode(comp->path.dentry); |
413 | int i; | |
414 | ||
e161c6bf | 415 | list_for_each_entry(orig, &tpe->probes, tp.list) { |
fe60b0ce MH |
416 | if (comp_inode != d_real_inode(orig->path.dentry) || |
417 | comp->offset != orig->offset) | |
418 | continue; | |
419 | ||
420 | /* | |
421 | * trace_probe_compare_arg_type() ensured that nr_args and | |
422 | * each argument name and type are same. Let's compare comm. | |
423 | */ | |
424 | for (i = 0; i < orig->tp.nr_args; i++) { | |
425 | if (strcmp(orig->tp.args[i].comm, | |
426 | comp->tp.args[i].comm)) | |
f8d7ab2b | 427 | break; |
fe60b0ce MH |
428 | } |
429 | ||
f8d7ab2b SD |
430 | if (i == orig->tp.nr_args) |
431 | return true; | |
fe60b0ce MH |
432 | } |
433 | ||
434 | return false; | |
435 | } | |
436 | ||
41af3cf5 MH |
437 | static int append_trace_uprobe(struct trace_uprobe *tu, struct trace_uprobe *to) |
438 | { | |
439 | int ret; | |
440 | ||
fe60b0ce MH |
441 | ret = trace_probe_compare_arg_type(&tu->tp, &to->tp); |
442 | if (ret) { | |
443 | /* Note that argument starts index = 2 */ | |
444 | trace_probe_log_set_index(ret + 1); | |
445 | trace_probe_log_err(0, DIFF_ARG_TYPE); | |
446 | return -EEXIST; | |
447 | } | |
448 | if (trace_uprobe_has_same_uprobe(to, tu)) { | |
449 | trace_probe_log_set_index(0); | |
450 | trace_probe_log_err(0, SAME_PROBE); | |
451 | return -EEXIST; | |
452 | } | |
453 | ||
41af3cf5 MH |
454 | /* Append to existing event */ |
455 | ret = trace_probe_append(&tu->tp, &to->tp); | |
456 | if (!ret) | |
8b0e6c74 | 457 | dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp)); |
41af3cf5 MH |
458 | |
459 | return ret; | |
460 | } | |
461 | ||
ccea8727 RB |
462 | /* |
463 | * Uprobe with multiple reference counter is not allowed. i.e. | |
464 | * If inode and offset matches, reference counter offset *must* | |
465 | * match as well. Though, there is one exception: If user is | |
466 | * replacing old trace_uprobe with new one(same group/event), | |
467 | * then we allow same uprobe with new reference counter as far | |
468 | * as the new one does not conflict with any other existing | |
469 | * ones. | |
470 | */ | |
41af3cf5 | 471 | static int validate_ref_ctr_offset(struct trace_uprobe *new) |
ccea8727 | 472 | { |
0597c49c | 473 | struct dyn_event *pos; |
41af3cf5 | 474 | struct trace_uprobe *tmp; |
ccea8727 RB |
475 | struct inode *new_inode = d_real_inode(new->path.dentry); |
476 | ||
0597c49c | 477 | for_each_trace_uprobe(tmp, pos) { |
41af3cf5 | 478 | if (new_inode == d_real_inode(tmp->path.dentry) && |
ccea8727 RB |
479 | new->offset == tmp->offset && |
480 | new->ref_ctr_offset != tmp->ref_ctr_offset) { | |
481 | pr_warn("Reference counter offset mismatch."); | |
41af3cf5 | 482 | return -EINVAL; |
ccea8727 RB |
483 | } |
484 | } | |
41af3cf5 | 485 | return 0; |
ccea8727 RB |
486 | } |
487 | ||
f3f096cf SD |
488 | /* Register a trace_uprobe and probe_event */ |
489 | static int register_trace_uprobe(struct trace_uprobe *tu) | |
490 | { | |
14577c39 | 491 | struct trace_uprobe *old_tu; |
f3f096cf SD |
492 | int ret; |
493 | ||
0597c49c | 494 | mutex_lock(&event_mutex); |
f3f096cf | 495 | |
41af3cf5 MH |
496 | ret = validate_ref_ctr_offset(tu); |
497 | if (ret) | |
ccea8727 | 498 | goto end; |
ccea8727 | 499 | |
41af3cf5 MH |
500 | /* register as an event */ |
501 | old_tu = find_probe_event(trace_probe_name(&tu->tp), | |
502 | trace_probe_group_name(&tu->tp)); | |
14577c39 | 503 | if (old_tu) { |
41af3cf5 MH |
504 | if (is_ret_probe(tu) != is_ret_probe(old_tu)) { |
505 | trace_probe_log_set_index(0); | |
506 | trace_probe_log_err(0, DIFF_PROBE_TYPE); | |
507 | ret = -EEXIST; | |
508 | } else { | |
fe60b0ce | 509 | ret = append_trace_uprobe(tu, old_tu); |
41af3cf5 MH |
510 | } |
511 | goto end; | |
c6c2401d | 512 | } |
f3f096cf SD |
513 | |
514 | ret = register_uprobe_event(tu); | |
515 | if (ret) { | |
8e242060 MH |
516 | if (ret == -EEXIST) { |
517 | trace_probe_log_set_index(0); | |
518 | trace_probe_log_err(0, EVENT_EXIST); | |
519 | } else | |
520 | pr_warn("Failed to register probe event(%d)\n", ret); | |
f3f096cf SD |
521 | goto end; |
522 | } | |
523 | ||
8b0e6c74 | 524 | dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp)); |
f3f096cf SD |
525 | |
526 | end: | |
0597c49c | 527 | mutex_unlock(&event_mutex); |
f3f096cf SD |
528 | |
529 | return ret; | |
530 | } | |
531 | ||
532 | /* | |
533 | * Argument syntax: | |
3dd3aae3 | 534 | * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET[%return][(REF)] [FETCHARGS] |
f3f096cf | 535 | */ |
d262271d | 536 | static int __trace_uprobe_create(int argc, const char **argv) |
f3f096cf SD |
537 | { |
538 | struct trace_uprobe *tu; | |
0597c49c MH |
539 | const char *event = NULL, *group = UPROBE_EVENT_SYSTEM; |
540 | char *arg, *filename, *rctr, *rctr_end, *tmp; | |
f3f096cf | 541 | char buf[MAX_EVENT_NAME_LEN]; |
007517a0 | 542 | enum probe_print_type ptype; |
f3f096cf | 543 | struct path path; |
1cc33161 | 544 | unsigned long offset, ref_ctr_offset; |
0597c49c | 545 | bool is_return = false; |
f3f096cf SD |
546 | int i, ret; |
547 | ||
f3f096cf | 548 | ret = 0; |
1cc33161 | 549 | ref_ctr_offset = 0; |
f3f096cf | 550 | |
f01098c7 ET |
551 | switch (argv[0][0]) { |
552 | case 'r': | |
4ee5a52e | 553 | is_return = true; |
f01098c7 ET |
554 | break; |
555 | case 'p': | |
556 | break; | |
557 | default: | |
558 | return -ECANCELED; | |
559 | } | |
560 | ||
561 | if (argc < 2) | |
0597c49c | 562 | return -ECANCELED; |
f3f096cf | 563 | |
0597c49c | 564 | if (argv[0][1] == ':') |
f3f096cf | 565 | event = &argv[0][2]; |
f3f096cf | 566 | |
0597c49c MH |
567 | if (!strchr(argv[1], '/')) |
568 | return -ECANCELED; | |
f3f096cf | 569 | |
0597c49c MH |
570 | filename = kstrdup(argv[1], GFP_KERNEL); |
571 | if (!filename) | |
572 | return -ENOMEM; | |
f3f096cf | 573 | |
6496bb72 | 574 | /* Find the last occurrence, in case the path contains ':' too. */ |
0597c49c MH |
575 | arg = strrchr(filename, ':'); |
576 | if (!arg || !isdigit(arg[1])) { | |
577 | kfree(filename); | |
578 | return -ECANCELED; | |
579 | } | |
f3f096cf | 580 | |
ab105a4f MH |
581 | trace_probe_log_init("trace_uprobe", argc, argv); |
582 | trace_probe_log_set_index(1); /* filename is the 2nd argument */ | |
583 | ||
f3f096cf | 584 | *arg++ = '\0'; |
f3f096cf | 585 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); |
0597c49c | 586 | if (ret) { |
ab105a4f | 587 | trace_probe_log_err(0, FILE_NOT_FOUND); |
0597c49c | 588 | kfree(filename); |
ab105a4f | 589 | trace_probe_log_clear(); |
0c92c7a3 | 590 | return ret; |
0597c49c | 591 | } |
0c92c7a3 | 592 | if (!d_is_reg(path.dentry)) { |
ab105a4f | 593 | trace_probe_log_err(0, NO_REGULAR_FILE); |
d24d7dbf JZ |
594 | ret = -EINVAL; |
595 | goto fail_address_parse; | |
596 | } | |
f3f096cf | 597 | |
1cc33161 RB |
598 | /* Parse reference counter offset if specified. */ |
599 | rctr = strchr(arg, '('); | |
600 | if (rctr) { | |
601 | rctr_end = strchr(rctr, ')'); | |
ab105a4f MH |
602 | if (!rctr_end) { |
603 | ret = -EINVAL; | |
604 | rctr_end = rctr + strlen(rctr); | |
605 | trace_probe_log_err(rctr_end - filename, | |
606 | REFCNT_OPEN_BRACE); | |
607 | goto fail_address_parse; | |
608 | } else if (rctr_end[1] != '\0') { | |
1cc33161 | 609 | ret = -EINVAL; |
ab105a4f MH |
610 | trace_probe_log_err(rctr_end + 1 - filename, |
611 | BAD_REFCNT_SUFFIX); | |
1cc33161 RB |
612 | goto fail_address_parse; |
613 | } | |
614 | ||
615 | *rctr++ = '\0'; | |
616 | *rctr_end = '\0'; | |
617 | ret = kstrtoul(rctr, 0, &ref_ctr_offset); | |
618 | if (ret) { | |
ab105a4f | 619 | trace_probe_log_err(rctr - filename, BAD_REFCNT); |
1cc33161 RB |
620 | goto fail_address_parse; |
621 | } | |
622 | } | |
623 | ||
3dd3aae3 MH |
624 | /* Check if there is %return suffix */ |
625 | tmp = strchr(arg, '%'); | |
626 | if (tmp) { | |
627 | if (!strcmp(tmp, "%return")) { | |
628 | *tmp = '\0'; | |
629 | is_return = true; | |
630 | } else { | |
631 | trace_probe_log_err(tmp - filename, BAD_ADDR_SUFFIX); | |
632 | ret = -EINVAL; | |
633 | goto fail_address_parse; | |
634 | } | |
635 | } | |
636 | ||
1cc33161 | 637 | /* Parse uprobe offset. */ |
84d7ed79 | 638 | ret = kstrtoul(arg, 0, &offset); |
ab105a4f MH |
639 | if (ret) { |
640 | trace_probe_log_err(arg - filename, BAD_UPROBE_OFFS); | |
84d7ed79 | 641 | goto fail_address_parse; |
ab105a4f | 642 | } |
f3f096cf SD |
643 | |
644 | /* setup a probe */ | |
ab105a4f | 645 | trace_probe_log_set_index(0); |
0597c49c | 646 | if (event) { |
ab105a4f MH |
647 | ret = traceprobe_parse_event_name(&event, &group, buf, |
648 | event - argv[0]); | |
0597c49c MH |
649 | if (ret) |
650 | goto fail_address_parse; | |
651 | } else { | |
b2e902f0 | 652 | char *tail; |
f3f096cf SD |
653 | char *ptr; |
654 | ||
b2e902f0 AS |
655 | tail = kstrdup(kbasename(filename), GFP_KERNEL); |
656 | if (!tail) { | |
f3f096cf SD |
657 | ret = -ENOMEM; |
658 | goto fail_address_parse; | |
659 | } | |
660 | ||
f3f096cf SD |
661 | ptr = strpbrk(tail, ".-_"); |
662 | if (ptr) | |
663 | *ptr = '\0'; | |
664 | ||
665 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset); | |
666 | event = buf; | |
667 | kfree(tail); | |
668 | } | |
669 | ||
ab105a4f MH |
670 | argc -= 2; |
671 | argv += 2; | |
672 | ||
4ee5a52e | 673 | tu = alloc_trace_uprobe(group, event, argc, is_return); |
f3f096cf | 674 | if (IS_ERR(tu)) { |
f3f096cf | 675 | ret = PTR_ERR(tu); |
a039480e MH |
676 | /* This must return -ENOMEM otherwise there is a bug */ |
677 | WARN_ON_ONCE(ret != -ENOMEM); | |
f3f096cf SD |
678 | goto fail_address_parse; |
679 | } | |
680 | tu->offset = offset; | |
1cc33161 | 681 | tu->ref_ctr_offset = ref_ctr_offset; |
0c92c7a3 | 682 | tu->path = path; |
0597c49c | 683 | tu->filename = filename; |
f3f096cf SD |
684 | |
685 | /* parse arguments */ | |
f3f096cf | 686 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { |
ab105a4f | 687 | trace_probe_log_set_index(i + 2); |
fcd9db51 | 688 | ret = traceprobe_parse_probe_arg(&tu->tp, i, argv[i], |
a1303af5 | 689 | is_return ? TPARG_FL_RETURN : 0); |
d00bbea9 | 690 | if (ret) |
f3f096cf | 691 | goto error; |
f3f096cf SD |
692 | } |
693 | ||
007517a0 SRV |
694 | ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL; |
695 | ret = traceprobe_set_print_fmt(&tu->tp, ptype); | |
b4d4b96b MH |
696 | if (ret < 0) |
697 | goto error; | |
698 | ||
f3f096cf | 699 | ret = register_trace_uprobe(tu); |
ab105a4f MH |
700 | if (!ret) |
701 | goto out; | |
f3f096cf SD |
702 | |
703 | error: | |
704 | free_trace_uprobe(tu); | |
ab105a4f MH |
705 | out: |
706 | trace_probe_log_clear(); | |
f3f096cf SD |
707 | return ret; |
708 | ||
709 | fail_address_parse: | |
ab105a4f | 710 | trace_probe_log_clear(); |
0c92c7a3 | 711 | path_put(&path); |
0597c49c | 712 | kfree(filename); |
f3f096cf | 713 | |
f3f096cf SD |
714 | return ret; |
715 | } | |
716 | ||
d262271d MH |
717 | int trace_uprobe_create(const char *raw_command) |
718 | { | |
719 | return trace_probe_create(raw_command, __trace_uprobe_create); | |
720 | } | |
721 | ||
722 | static int create_or_delete_trace_uprobe(const char *raw_command) | |
f3f096cf | 723 | { |
0597c49c | 724 | int ret; |
f3f096cf | 725 | |
d262271d MH |
726 | if (raw_command[0] == '-') |
727 | return dyn_event_release(raw_command, &trace_uprobe_ops); | |
f3f096cf | 728 | |
d262271d | 729 | ret = trace_uprobe_create(raw_command); |
0597c49c | 730 | return ret == -ECANCELED ? -EINVAL : ret; |
f3f096cf SD |
731 | } |
732 | ||
0597c49c | 733 | static int trace_uprobe_release(struct dyn_event *ev) |
f3f096cf | 734 | { |
0597c49c | 735 | struct trace_uprobe *tu = to_trace_uprobe(ev); |
f3f096cf | 736 | |
0597c49c | 737 | return unregister_trace_uprobe(tu); |
f3f096cf SD |
738 | } |
739 | ||
0597c49c MH |
740 | /* Probes listing interfaces */ |
741 | static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev) | |
f3f096cf | 742 | { |
0597c49c | 743 | struct trace_uprobe *tu = to_trace_uprobe(ev); |
3ede82dd | 744 | char c = is_ret_probe(tu) ? 'r' : 'p'; |
f3f096cf SD |
745 | int i; |
746 | ||
b55ce203 MH |
747 | seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, trace_probe_group_name(&tu->tp), |
748 | trace_probe_name(&tu->tp), tu->filename, | |
a64b2c01 | 749 | (int)(sizeof(void *) * 2), tu->offset); |
f3f096cf | 750 | |
1cc33161 RB |
751 | if (tu->ref_ctr_offset) |
752 | seq_printf(m, "(0x%lx)", tu->ref_ctr_offset); | |
753 | ||
14577c39 NK |
754 | for (i = 0; i < tu->tp.nr_args; i++) |
755 | seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm); | |
f3f096cf | 756 | |
fa6f0cc7 | 757 | seq_putc(m, '\n'); |
f3f096cf SD |
758 | return 0; |
759 | } | |
760 | ||
0597c49c MH |
761 | static int probes_seq_show(struct seq_file *m, void *v) |
762 | { | |
763 | struct dyn_event *ev = v; | |
764 | ||
765 | if (!is_trace_uprobe(ev)) | |
766 | return 0; | |
767 | ||
768 | return trace_uprobe_show(m, ev); | |
769 | } | |
770 | ||
f3f096cf | 771 | static const struct seq_operations probes_seq_op = { |
0597c49c MH |
772 | .start = dyn_event_seq_start, |
773 | .next = dyn_event_seq_next, | |
774 | .stop = dyn_event_seq_stop, | |
775 | .show = probes_seq_show | |
f3f096cf SD |
776 | }; |
777 | ||
778 | static int probes_open(struct inode *inode, struct file *file) | |
779 | { | |
c6c2401d SRRH |
780 | int ret; |
781 | ||
17911ff3 SRV |
782 | ret = security_locked_down(LOCKDOWN_TRACEFS); |
783 | if (ret) | |
784 | return ret; | |
785 | ||
c6c2401d | 786 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { |
0597c49c | 787 | ret = dyn_events_release_all(&trace_uprobe_ops); |
c6c2401d SRRH |
788 | if (ret) |
789 | return ret; | |
790 | } | |
f3f096cf SD |
791 | |
792 | return seq_open(file, &probes_seq_op); | |
793 | } | |
794 | ||
795 | static ssize_t probes_write(struct file *file, const char __user *buffer, | |
796 | size_t count, loff_t *ppos) | |
797 | { | |
0597c49c MH |
798 | return trace_parse_run_command(file, buffer, count, ppos, |
799 | create_or_delete_trace_uprobe); | |
f3f096cf SD |
800 | } |
801 | ||
802 | static const struct file_operations uprobe_events_ops = { | |
803 | .owner = THIS_MODULE, | |
804 | .open = probes_open, | |
805 | .read = seq_read, | |
806 | .llseek = seq_lseek, | |
807 | .release = seq_release, | |
808 | .write = probes_write, | |
809 | }; | |
810 | ||
811 | /* Probes profiling interfaces */ | |
812 | static int probes_profile_seq_show(struct seq_file *m, void *v) | |
813 | { | |
0597c49c MH |
814 | struct dyn_event *ev = v; |
815 | struct trace_uprobe *tu; | |
816 | ||
817 | if (!is_trace_uprobe(ev)) | |
818 | return 0; | |
f3f096cf | 819 | |
0597c49c | 820 | tu = to_trace_uprobe(ev); |
de7b2973 | 821 | seq_printf(m, " %s %-44s %15lu\n", tu->filename, |
b55ce203 | 822 | trace_probe_name(&tu->tp), tu->nhit); |
f3f096cf SD |
823 | return 0; |
824 | } | |
825 | ||
826 | static const struct seq_operations profile_seq_op = { | |
0597c49c MH |
827 | .start = dyn_event_seq_start, |
828 | .next = dyn_event_seq_next, | |
829 | .stop = dyn_event_seq_stop, | |
f3f096cf SD |
830 | .show = probes_profile_seq_show |
831 | }; | |
832 | ||
833 | static int profile_open(struct inode *inode, struct file *file) | |
834 | { | |
17911ff3 SRV |
835 | int ret; |
836 | ||
837 | ret = security_locked_down(LOCKDOWN_TRACEFS); | |
838 | if (ret) | |
839 | return ret; | |
840 | ||
f3f096cf SD |
841 | return seq_open(file, &profile_seq_op); |
842 | } | |
843 | ||
844 | static const struct file_operations uprobe_profile_ops = { | |
845 | .owner = THIS_MODULE, | |
846 | .open = profile_open, | |
847 | .read = seq_read, | |
848 | .llseek = seq_lseek, | |
849 | .release = seq_release, | |
850 | }; | |
851 | ||
dcad1a20 NK |
852 | struct uprobe_cpu_buffer { |
853 | struct mutex mutex; | |
854 | void *buf; | |
855 | }; | |
856 | static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer; | |
857 | static int uprobe_buffer_refcnt; | |
858 | ||
859 | static int uprobe_buffer_init(void) | |
860 | { | |
861 | int cpu, err_cpu; | |
862 | ||
863 | uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer); | |
864 | if (uprobe_cpu_buffer == NULL) | |
865 | return -ENOMEM; | |
866 | ||
867 | for_each_possible_cpu(cpu) { | |
868 | struct page *p = alloc_pages_node(cpu_to_node(cpu), | |
869 | GFP_KERNEL, 0); | |
870 | if (p == NULL) { | |
871 | err_cpu = cpu; | |
872 | goto err; | |
873 | } | |
874 | per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p); | |
875 | mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex); | |
876 | } | |
877 | ||
878 | return 0; | |
879 | ||
880 | err: | |
881 | for_each_possible_cpu(cpu) { | |
882 | if (cpu == err_cpu) | |
883 | break; | |
884 | free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf); | |
885 | } | |
886 | ||
887 | free_percpu(uprobe_cpu_buffer); | |
888 | return -ENOMEM; | |
889 | } | |
890 | ||
891 | static int uprobe_buffer_enable(void) | |
892 | { | |
893 | int ret = 0; | |
894 | ||
895 | BUG_ON(!mutex_is_locked(&event_mutex)); | |
896 | ||
897 | if (uprobe_buffer_refcnt++ == 0) { | |
898 | ret = uprobe_buffer_init(); | |
899 | if (ret < 0) | |
900 | uprobe_buffer_refcnt--; | |
901 | } | |
902 | ||
903 | return ret; | |
904 | } | |
905 | ||
906 | static void uprobe_buffer_disable(void) | |
907 | { | |
6ea6215f J |
908 | int cpu; |
909 | ||
dcad1a20 NK |
910 | BUG_ON(!mutex_is_locked(&event_mutex)); |
911 | ||
912 | if (--uprobe_buffer_refcnt == 0) { | |
6ea6215f J |
913 | for_each_possible_cpu(cpu) |
914 | free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, | |
915 | cpu)->buf); | |
916 | ||
dcad1a20 NK |
917 | free_percpu(uprobe_cpu_buffer); |
918 | uprobe_cpu_buffer = NULL; | |
919 | } | |
920 | } | |
921 | ||
922 | static struct uprobe_cpu_buffer *uprobe_buffer_get(void) | |
923 | { | |
924 | struct uprobe_cpu_buffer *ucb; | |
925 | int cpu; | |
926 | ||
927 | cpu = raw_smp_processor_id(); | |
928 | ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu); | |
929 | ||
930 | /* | |
931 | * Use per-cpu buffers for fastest access, but we might migrate | |
932 | * so the mutex makes sure we have sole access to it. | |
933 | */ | |
934 | mutex_lock(&ucb->mutex); | |
935 | ||
936 | return ucb; | |
937 | } | |
938 | ||
939 | static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb) | |
940 | { | |
941 | mutex_unlock(&ucb->mutex); | |
942 | } | |
943 | ||
a43b9704 | 944 | static void __uprobe_trace_func(struct trace_uprobe *tu, |
dd9fa555 | 945 | unsigned long func, struct pt_regs *regs, |
70ed91c6 | 946 | struct uprobe_cpu_buffer *ucb, int dsize, |
7f1d2f82 | 947 | struct trace_event_file *trace_file) |
f3f096cf SD |
948 | { |
949 | struct uprobe_trace_entry_head *entry; | |
b7d5eb26 | 950 | struct trace_event_buffer fbuffer; |
457d1772 | 951 | void *data; |
dd9fa555 | 952 | int size, esize; |
e3dc9f89 | 953 | struct trace_event_call *call = trace_probe_event_call(&tu->tp); |
f3f096cf | 954 | |
7f1d2f82 | 955 | WARN_ON(call != trace_file->event_call); |
70ed91c6 | 956 | |
dd9fa555 | 957 | if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE)) |
dcad1a20 NK |
958 | return; |
959 | ||
09a5059a | 960 | if (trace_trigger_soft_disabled(trace_file)) |
ca3b1620 NK |
961 | return; |
962 | ||
dd9fa555 | 963 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
dcad1a20 | 964 | size = esize + tu->tp.size + dsize; |
b7d5eb26 SRV |
965 | entry = trace_event_buffer_reserve(&fbuffer, trace_file, size); |
966 | if (!entry) | |
dd9fa555 | 967 | return; |
f3f096cf | 968 | |
393a736c ON |
969 | if (is_ret_probe(tu)) { |
970 | entry->vaddr[0] = func; | |
971 | entry->vaddr[1] = instruction_pointer(regs); | |
972 | data = DATAOF_TRACE_ENTRY(entry, true); | |
973 | } else { | |
974 | entry->vaddr[0] = instruction_pointer(regs); | |
975 | data = DATAOF_TRACE_ENTRY(entry, false); | |
976 | } | |
977 | ||
dcad1a20 | 978 | memcpy(data, ucb->buf, tu->tp.size + dsize); |
f3f096cf | 979 | |
b7d5eb26 | 980 | trace_event_buffer_commit(&fbuffer); |
a51cc604 | 981 | } |
f42d24a1 | 982 | |
a51cc604 | 983 | /* uprobe handler */ |
dd9fa555 NK |
984 | static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs, |
985 | struct uprobe_cpu_buffer *ucb, int dsize) | |
a51cc604 | 986 | { |
70ed91c6 J |
987 | struct event_file_link *link; |
988 | ||
989 | if (is_ret_probe(tu)) | |
990 | return 0; | |
991 | ||
992 | rcu_read_lock(); | |
b5f935ee | 993 | trace_probe_for_each_link_rcu(link, &tu->tp) |
70ed91c6 J |
994 | __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file); |
995 | rcu_read_unlock(); | |
996 | ||
f42d24a1 | 997 | return 0; |
f3f096cf SD |
998 | } |
999 | ||
c1ae5c75 | 1000 | static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func, |
dd9fa555 NK |
1001 | struct pt_regs *regs, |
1002 | struct uprobe_cpu_buffer *ucb, int dsize) | |
c1ae5c75 | 1003 | { |
70ed91c6 J |
1004 | struct event_file_link *link; |
1005 | ||
1006 | rcu_read_lock(); | |
b5f935ee | 1007 | trace_probe_for_each_link_rcu(link, &tu->tp) |
70ed91c6 J |
1008 | __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file); |
1009 | rcu_read_unlock(); | |
c1ae5c75 ON |
1010 | } |
1011 | ||
f3f096cf SD |
1012 | /* Event entry printers */ |
1013 | static enum print_line_t | |
1014 | print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event) | |
1015 | { | |
457d1772 | 1016 | struct uprobe_trace_entry_head *entry; |
f3f096cf SD |
1017 | struct trace_seq *s = &iter->seq; |
1018 | struct trace_uprobe *tu; | |
1019 | u8 *data; | |
f3f096cf | 1020 | |
457d1772 | 1021 | entry = (struct uprobe_trace_entry_head *)iter->ent; |
60d53e2c MH |
1022 | tu = trace_uprobe_primary_from_call( |
1023 | container_of(event, struct trace_event_call, event)); | |
1024 | if (unlikely(!tu)) | |
1025 | goto out; | |
f3f096cf | 1026 | |
3ede82dd | 1027 | if (is_ret_probe(tu)) { |
8579a107 | 1028 | trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", |
b55ce203 | 1029 | trace_probe_name(&tu->tp), |
8579a107 | 1030 | entry->vaddr[1], entry->vaddr[0]); |
3ede82dd ON |
1031 | data = DATAOF_TRACE_ENTRY(entry, true); |
1032 | } else { | |
8579a107 | 1033 | trace_seq_printf(s, "%s: (0x%lx)", |
b55ce203 | 1034 | trace_probe_name(&tu->tp), |
8579a107 | 1035 | entry->vaddr[0]); |
3ede82dd ON |
1036 | data = DATAOF_TRACE_ENTRY(entry, false); |
1037 | } | |
f3f096cf | 1038 | |
56de7630 MH |
1039 | if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0) |
1040 | goto out; | |
f3f096cf | 1041 | |
8579a107 | 1042 | trace_seq_putc(s, '\n'); |
f3f096cf | 1043 | |
8579a107 SRRH |
1044 | out: |
1045 | return trace_handle_return(s); | |
f3f096cf SD |
1046 | } |
1047 | ||
31ba3348 ON |
1048 | typedef bool (*filter_func_t)(struct uprobe_consumer *self, |
1049 | enum uprobe_filter_ctx ctx, | |
1050 | struct mm_struct *mm); | |
1051 | ||
60d53e2c | 1052 | static int trace_uprobe_enable(struct trace_uprobe *tu, filter_func_t filter) |
f3f096cf | 1053 | { |
70ed91c6 J |
1054 | int ret; |
1055 | ||
60d53e2c MH |
1056 | tu->consumer.filter = filter; |
1057 | tu->inode = d_real_inode(tu->path.dentry); | |
1058 | ||
1059 | if (tu->ref_ctr_offset) | |
1060 | ret = uprobe_register_refctr(tu->inode, tu->offset, | |
1061 | tu->ref_ctr_offset, &tu->consumer); | |
1062 | else | |
1063 | ret = uprobe_register(tu->inode, tu->offset, &tu->consumer); | |
1064 | ||
1065 | if (ret) | |
1066 | tu->inode = NULL; | |
1067 | ||
1068 | return ret; | |
1069 | } | |
1070 | ||
1071 | static void __probe_event_disable(struct trace_probe *tp) | |
1072 | { | |
60d53e2c MH |
1073 | struct trace_uprobe *tu; |
1074 | ||
99c9a923 | 1075 | tu = container_of(tp, struct trace_uprobe, tp); |
b61387cb | 1076 | WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter)); |
99c9a923 | 1077 | |
e161c6bf | 1078 | list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { |
60d53e2c MH |
1079 | if (!tu->inode) |
1080 | continue; | |
1081 | ||
60d53e2c MH |
1082 | uprobe_unregister(tu->inode, tu->offset, &tu->consumer); |
1083 | tu->inode = NULL; | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | static int probe_event_enable(struct trace_event_call *call, | |
1088 | struct trace_event_file *file, filter_func_t filter) | |
1089 | { | |
e161c6bf | 1090 | struct trace_probe *tp; |
60d53e2c MH |
1091 | struct trace_uprobe *tu; |
1092 | bool enabled; | |
1093 | int ret; | |
1094 | ||
1095 | tp = trace_probe_primary_from_call(call); | |
1096 | if (WARN_ON_ONCE(!tp)) | |
1097 | return -ENODEV; | |
1098 | enabled = trace_probe_is_enabled(tp); | |
1099 | ||
1100 | /* This may also change "enabled" state */ | |
70ed91c6 | 1101 | if (file) { |
60d53e2c | 1102 | if (trace_probe_test_flag(tp, TP_FLAG_PROFILE)) |
48212542 ON |
1103 | return -EINTR; |
1104 | ||
60d53e2c | 1105 | ret = trace_probe_add_file(tp, file); |
b5f935ee MH |
1106 | if (ret < 0) |
1107 | return ret; | |
48212542 | 1108 | } else { |
60d53e2c | 1109 | if (trace_probe_test_flag(tp, TP_FLAG_TRACE)) |
48212542 ON |
1110 | return -EINTR; |
1111 | ||
60d53e2c | 1112 | trace_probe_set_flag(tp, TP_FLAG_PROFILE); |
48212542 | 1113 | } |
f3f096cf | 1114 | |
60d53e2c | 1115 | tu = container_of(tp, struct trace_uprobe, tp); |
b61387cb | 1116 | WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter)); |
736288ba | 1117 | |
70ed91c6 J |
1118 | if (enabled) |
1119 | return 0; | |
1120 | ||
fb6bab6a ON |
1121 | ret = uprobe_buffer_enable(); |
1122 | if (ret) | |
1123 | goto err_flags; | |
1124 | ||
e161c6bf | 1125 | list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { |
60d53e2c MH |
1126 | ret = trace_uprobe_enable(tu, filter); |
1127 | if (ret) { | |
1128 | __probe_event_disable(tp); | |
1129 | goto err_buffer; | |
1130 | } | |
1cc33161 RB |
1131 | } |
1132 | ||
fb6bab6a ON |
1133 | return 0; |
1134 | ||
1135 | err_buffer: | |
1136 | uprobe_buffer_disable(); | |
f3f096cf | 1137 | |
fb6bab6a | 1138 | err_flags: |
b5f935ee | 1139 | if (file) |
60d53e2c | 1140 | trace_probe_remove_file(tp, file); |
b5f935ee | 1141 | else |
60d53e2c | 1142 | trace_probe_clear_flag(tp, TP_FLAG_PROFILE); |
b5f935ee | 1143 | |
4161824f | 1144 | return ret; |
f3f096cf SD |
1145 | } |
1146 | ||
60d53e2c MH |
1147 | static void probe_event_disable(struct trace_event_call *call, |
1148 | struct trace_event_file *file) | |
f3f096cf | 1149 | { |
60d53e2c MH |
1150 | struct trace_probe *tp; |
1151 | ||
1152 | tp = trace_probe_primary_from_call(call); | |
1153 | if (WARN_ON_ONCE(!tp)) | |
1154 | return; | |
1155 | ||
1156 | if (!trace_probe_is_enabled(tp)) | |
f3f096cf SD |
1157 | return; |
1158 | ||
70ed91c6 | 1159 | if (file) { |
60d53e2c | 1160 | if (trace_probe_remove_file(tp, file) < 0) |
70ed91c6 J |
1161 | return; |
1162 | ||
60d53e2c | 1163 | if (trace_probe_is_enabled(tp)) |
70ed91c6 | 1164 | return; |
b5f935ee | 1165 | } else |
60d53e2c | 1166 | trace_probe_clear_flag(tp, TP_FLAG_PROFILE); |
dcad1a20 | 1167 | |
60d53e2c | 1168 | __probe_event_disable(tp); |
dcad1a20 | 1169 | uprobe_buffer_disable(); |
f3f096cf SD |
1170 | } |
1171 | ||
2425bcb9 | 1172 | static int uprobe_event_define_fields(struct trace_event_call *event_call) |
f3f096cf | 1173 | { |
eeb07b06 | 1174 | int ret, size; |
f3f096cf | 1175 | struct uprobe_trace_entry_head field; |
60d53e2c MH |
1176 | struct trace_uprobe *tu; |
1177 | ||
1178 | tu = trace_uprobe_primary_from_call(event_call); | |
1179 | if (unlikely(!tu)) | |
1180 | return -ENODEV; | |
f3f096cf | 1181 | |
4d1298e2 ON |
1182 | if (is_ret_probe(tu)) { |
1183 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0); | |
1184 | DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0); | |
1185 | size = SIZEOF_TRACE_ENTRY(true); | |
1186 | } else { | |
1187 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0); | |
1188 | size = SIZEOF_TRACE_ENTRY(false); | |
1189 | } | |
f3f096cf | 1190 | |
eeb07b06 | 1191 | return traceprobe_define_arg_fields(event_call, size, &tu->tp); |
f3f096cf SD |
1192 | } |
1193 | ||
f3f096cf | 1194 | #ifdef CONFIG_PERF_EVENTS |
31ba3348 ON |
1195 | static bool |
1196 | __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm) | |
1197 | { | |
1198 | struct perf_event *event; | |
1199 | ||
1200 | if (filter->nr_systemwide) | |
1201 | return true; | |
1202 | ||
1203 | list_for_each_entry(event, &filter->perf_events, hw.tp_list) { | |
50f16a8b | 1204 | if (event->hw.target->mm == mm) |
31ba3348 ON |
1205 | return true; |
1206 | } | |
1207 | ||
1208 | return false; | |
1209 | } | |
1210 | ||
b2fe8ba6 | 1211 | static inline bool |
99c9a923 MH |
1212 | trace_uprobe_filter_event(struct trace_uprobe_filter *filter, |
1213 | struct perf_event *event) | |
b2fe8ba6 | 1214 | { |
99c9a923 | 1215 | return __uprobe_perf_filter(filter, event->hw.target->mm); |
b2fe8ba6 ON |
1216 | } |
1217 | ||
99c9a923 MH |
1218 | static bool trace_uprobe_filter_remove(struct trace_uprobe_filter *filter, |
1219 | struct perf_event *event) | |
736288ba | 1220 | { |
b2fe8ba6 ON |
1221 | bool done; |
1222 | ||
99c9a923 | 1223 | write_lock(&filter->rwlock); |
50f16a8b | 1224 | if (event->hw.target) { |
ce5f36a5 | 1225 | list_del(&event->hw.tp_list); |
99c9a923 | 1226 | done = filter->nr_systemwide || |
50f16a8b | 1227 | (event->hw.target->flags & PF_EXITING) || |
99c9a923 | 1228 | trace_uprobe_filter_event(filter, event); |
b2fe8ba6 | 1229 | } else { |
99c9a923 MH |
1230 | filter->nr_systemwide--; |
1231 | done = filter->nr_systemwide; | |
b2fe8ba6 | 1232 | } |
99c9a923 | 1233 | write_unlock(&filter->rwlock); |
31ba3348 | 1234 | |
99c9a923 | 1235 | return done; |
736288ba ON |
1236 | } |
1237 | ||
99c9a923 MH |
1238 | /* This returns true if the filter always covers target mm */ |
1239 | static bool trace_uprobe_filter_add(struct trace_uprobe_filter *filter, | |
1240 | struct perf_event *event) | |
736288ba | 1241 | { |
b2fe8ba6 ON |
1242 | bool done; |
1243 | ||
99c9a923 | 1244 | write_lock(&filter->rwlock); |
50f16a8b | 1245 | if (event->hw.target) { |
ce5f36a5 ON |
1246 | /* |
1247 | * event->parent != NULL means copy_process(), we can avoid | |
1248 | * uprobe_apply(). current->mm must be probed and we can rely | |
1249 | * on dup_mmap() which preserves the already installed bp's. | |
1250 | * | |
1251 | * attr.enable_on_exec means that exec/mmap will install the | |
1252 | * breakpoints we need. | |
1253 | */ | |
99c9a923 | 1254 | done = filter->nr_systemwide || |
ce5f36a5 | 1255 | event->parent || event->attr.enable_on_exec || |
99c9a923 MH |
1256 | trace_uprobe_filter_event(filter, event); |
1257 | list_add(&event->hw.tp_list, &filter->perf_events); | |
b2fe8ba6 | 1258 | } else { |
99c9a923 MH |
1259 | done = filter->nr_systemwide; |
1260 | filter->nr_systemwide++; | |
b2fe8ba6 | 1261 | } |
99c9a923 | 1262 | write_unlock(&filter->rwlock); |
736288ba | 1263 | |
99c9a923 | 1264 | return done; |
736288ba ON |
1265 | } |
1266 | ||
99c9a923 MH |
1267 | static int uprobe_perf_close(struct trace_event_call *call, |
1268 | struct perf_event *event) | |
60d53e2c | 1269 | { |
e161c6bf | 1270 | struct trace_probe *tp; |
60d53e2c MH |
1271 | struct trace_uprobe *tu; |
1272 | int ret = 0; | |
1273 | ||
1274 | tp = trace_probe_primary_from_call(call); | |
1275 | if (WARN_ON_ONCE(!tp)) | |
1276 | return -ENODEV; | |
1277 | ||
99c9a923 | 1278 | tu = container_of(tp, struct trace_uprobe, tp); |
b61387cb | 1279 | if (trace_uprobe_filter_remove(tu->tp.event->filter, event)) |
99c9a923 MH |
1280 | return 0; |
1281 | ||
e161c6bf | 1282 | list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { |
99c9a923 | 1283 | ret = uprobe_apply(tu->inode, tu->offset, &tu->consumer, false); |
60d53e2c MH |
1284 | if (ret) |
1285 | break; | |
1286 | } | |
1287 | ||
1288 | return ret; | |
1289 | } | |
99c9a923 MH |
1290 | |
1291 | static int uprobe_perf_open(struct trace_event_call *call, | |
1292 | struct perf_event *event) | |
1293 | { | |
e161c6bf | 1294 | struct trace_probe *tp; |
99c9a923 MH |
1295 | struct trace_uprobe *tu; |
1296 | int err = 0; | |
1297 | ||
1298 | tp = trace_probe_primary_from_call(call); | |
1299 | if (WARN_ON_ONCE(!tp)) | |
1300 | return -ENODEV; | |
1301 | ||
1302 | tu = container_of(tp, struct trace_uprobe, tp); | |
b61387cb | 1303 | if (trace_uprobe_filter_add(tu->tp.event->filter, event)) |
99c9a923 MH |
1304 | return 0; |
1305 | ||
e161c6bf | 1306 | list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { |
99c9a923 MH |
1307 | err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true); |
1308 | if (err) { | |
1309 | uprobe_perf_close(call, event); | |
1310 | break; | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | return err; | |
1315 | } | |
1316 | ||
31ba3348 ON |
1317 | static bool uprobe_perf_filter(struct uprobe_consumer *uc, |
1318 | enum uprobe_filter_ctx ctx, struct mm_struct *mm) | |
1319 | { | |
99c9a923 | 1320 | struct trace_uprobe_filter *filter; |
31ba3348 ON |
1321 | struct trace_uprobe *tu; |
1322 | int ret; | |
1323 | ||
1324 | tu = container_of(uc, struct trace_uprobe, consumer); | |
b61387cb | 1325 | filter = tu->tp.event->filter; |
99c9a923 MH |
1326 | |
1327 | read_lock(&filter->rwlock); | |
1328 | ret = __uprobe_perf_filter(filter, mm); | |
1329 | read_unlock(&filter->rwlock); | |
31ba3348 ON |
1330 | |
1331 | return ret; | |
1332 | } | |
1333 | ||
a43b9704 | 1334 | static void __uprobe_perf_func(struct trace_uprobe *tu, |
dd9fa555 NK |
1335 | unsigned long func, struct pt_regs *regs, |
1336 | struct uprobe_cpu_buffer *ucb, int dsize) | |
f3f096cf | 1337 | { |
e3dc9f89 | 1338 | struct trace_event_call *call = trace_probe_event_call(&tu->tp); |
f3f096cf SD |
1339 | struct uprobe_trace_entry_head *entry; |
1340 | struct hlist_head *head; | |
457d1772 | 1341 | void *data; |
dd9fa555 | 1342 | int size, esize; |
dcad1a20 NK |
1343 | int rctx; |
1344 | ||
70ed0706 AS |
1345 | if (bpf_prog_array_valid(call)) { |
1346 | u32 ret; | |
1347 | ||
1348 | preempt_disable(); | |
1349 | ret = trace_call_bpf(call, regs); | |
1350 | preempt_enable(); | |
1351 | if (!ret) | |
1352 | return; | |
1353 | } | |
04a22fae | 1354 | |
dcad1a20 | 1355 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
f3f096cf | 1356 | |
dcad1a20 NK |
1357 | size = esize + tu->tp.size + dsize; |
1358 | size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32); | |
1359 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough")) | |
1360 | return; | |
1361 | ||
f3f096cf | 1362 | preempt_disable(); |
515619f2 ON |
1363 | head = this_cpu_ptr(call->perf_events); |
1364 | if (hlist_empty(head)) | |
1365 | goto out; | |
1366 | ||
1e1dcd93 | 1367 | entry = perf_trace_buf_alloc(size, NULL, &rctx); |
f3f096cf SD |
1368 | if (!entry) |
1369 | goto out; | |
1370 | ||
393a736c ON |
1371 | if (is_ret_probe(tu)) { |
1372 | entry->vaddr[0] = func; | |
32520b2c | 1373 | entry->vaddr[1] = instruction_pointer(regs); |
393a736c ON |
1374 | data = DATAOF_TRACE_ENTRY(entry, true); |
1375 | } else { | |
32520b2c | 1376 | entry->vaddr[0] = instruction_pointer(regs); |
393a736c ON |
1377 | data = DATAOF_TRACE_ENTRY(entry, false); |
1378 | } | |
1379 | ||
dcad1a20 NK |
1380 | memcpy(data, ucb->buf, tu->tp.size + dsize); |
1381 | ||
1382 | if (size - esize > tu->tp.size + dsize) { | |
1383 | int len = tu->tp.size + dsize; | |
14577c39 | 1384 | |
dcad1a20 | 1385 | memset(data + len, 0, size - esize - len); |
14577c39 | 1386 | } |
f3f096cf | 1387 | |
1e1dcd93 | 1388 | perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, |
8fd0fbbe | 1389 | head, NULL); |
f3f096cf SD |
1390 | out: |
1391 | preempt_enable(); | |
a51cc604 ON |
1392 | } |
1393 | ||
1394 | /* uprobe profile handler */ | |
dd9fa555 NK |
1395 | static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs, |
1396 | struct uprobe_cpu_buffer *ucb, int dsize) | |
a51cc604 ON |
1397 | { |
1398 | if (!uprobe_perf_filter(&tu->consumer, 0, current->mm)) | |
1399 | return UPROBE_HANDLER_REMOVE; | |
1400 | ||
393a736c | 1401 | if (!is_ret_probe(tu)) |
dd9fa555 | 1402 | __uprobe_perf_func(tu, 0, regs, ucb, dsize); |
f42d24a1 | 1403 | return 0; |
f3f096cf | 1404 | } |
c1ae5c75 ON |
1405 | |
1406 | static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func, | |
dd9fa555 NK |
1407 | struct pt_regs *regs, |
1408 | struct uprobe_cpu_buffer *ucb, int dsize) | |
c1ae5c75 | 1409 | { |
dd9fa555 | 1410 | __uprobe_perf_func(tu, func, regs, ucb, dsize); |
c1ae5c75 | 1411 | } |
41bdc4b4 YS |
1412 | |
1413 | int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type, | |
1414 | const char **filename, u64 *probe_offset, | |
1415 | bool perf_type_tracepoint) | |
1416 | { | |
1417 | const char *pevent = trace_event_name(event->tp_event); | |
1418 | const char *group = event->tp_event->class->system; | |
1419 | struct trace_uprobe *tu; | |
1420 | ||
1421 | if (perf_type_tracepoint) | |
1422 | tu = find_probe_event(pevent, group); | |
1423 | else | |
22d5bd68 | 1424 | tu = trace_uprobe_primary_from_call(event->tp_event); |
41bdc4b4 YS |
1425 | if (!tu) |
1426 | return -EINVAL; | |
1427 | ||
1428 | *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE | |
1429 | : BPF_FD_TYPE_UPROBE; | |
1430 | *filename = tu->filename; | |
1431 | *probe_offset = tu->offset; | |
1432 | return 0; | |
1433 | } | |
f3f096cf SD |
1434 | #endif /* CONFIG_PERF_EVENTS */ |
1435 | ||
70ed91c6 | 1436 | static int |
2425bcb9 | 1437 | trace_uprobe_register(struct trace_event_call *event, enum trace_reg type, |
70ed91c6 | 1438 | void *data) |
f3f096cf | 1439 | { |
7f1d2f82 | 1440 | struct trace_event_file *file = data; |
f3f096cf SD |
1441 | |
1442 | switch (type) { | |
1443 | case TRACE_REG_REGISTER: | |
60d53e2c | 1444 | return probe_event_enable(event, file, NULL); |
f3f096cf SD |
1445 | |
1446 | case TRACE_REG_UNREGISTER: | |
60d53e2c | 1447 | probe_event_disable(event, file); |
f3f096cf SD |
1448 | return 0; |
1449 | ||
1450 | #ifdef CONFIG_PERF_EVENTS | |
1451 | case TRACE_REG_PERF_REGISTER: | |
60d53e2c | 1452 | return probe_event_enable(event, NULL, uprobe_perf_filter); |
f3f096cf SD |
1453 | |
1454 | case TRACE_REG_PERF_UNREGISTER: | |
60d53e2c | 1455 | probe_event_disable(event, NULL); |
f3f096cf | 1456 | return 0; |
736288ba ON |
1457 | |
1458 | case TRACE_REG_PERF_OPEN: | |
99c9a923 | 1459 | return uprobe_perf_open(event, data); |
736288ba ON |
1460 | |
1461 | case TRACE_REG_PERF_CLOSE: | |
99c9a923 | 1462 | return uprobe_perf_close(event, data); |
736288ba | 1463 | |
f3f096cf SD |
1464 | #endif |
1465 | default: | |
1466 | return 0; | |
1467 | } | |
f3f096cf SD |
1468 | } |
1469 | ||
1470 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs) | |
1471 | { | |
f3f096cf | 1472 | struct trace_uprobe *tu; |
b7e0bf34 | 1473 | struct uprobe_dispatch_data udd; |
dd9fa555 NK |
1474 | struct uprobe_cpu_buffer *ucb; |
1475 | int dsize, esize; | |
f42d24a1 | 1476 | int ret = 0; |
f3f096cf | 1477 | |
dd9fa555 | 1478 | |
a932b738 | 1479 | tu = container_of(con, struct trace_uprobe, consumer); |
1b47aefd | 1480 | tu->nhit++; |
f3f096cf | 1481 | |
b7e0bf34 NK |
1482 | udd.tu = tu; |
1483 | udd.bp_addr = instruction_pointer(regs); | |
1484 | ||
1485 | current->utask->vaddr = (unsigned long) &udd; | |
1486 | ||
dd9fa555 NK |
1487 | if (WARN_ON_ONCE(!uprobe_cpu_buffer)) |
1488 | return 0; | |
1489 | ||
1490 | dsize = __get_data_size(&tu->tp, regs); | |
1491 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); | |
1492 | ||
1493 | ucb = uprobe_buffer_get(); | |
9178412d | 1494 | store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize); |
dd9fa555 | 1495 | |
747774d6 | 1496 | if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE)) |
dd9fa555 | 1497 | ret |= uprobe_trace_func(tu, regs, ucb, dsize); |
f3f096cf SD |
1498 | |
1499 | #ifdef CONFIG_PERF_EVENTS | |
747774d6 | 1500 | if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE)) |
dd9fa555 | 1501 | ret |= uprobe_perf_func(tu, regs, ucb, dsize); |
f3f096cf | 1502 | #endif |
dd9fa555 | 1503 | uprobe_buffer_put(ucb); |
f42d24a1 | 1504 | return ret; |
f3f096cf SD |
1505 | } |
1506 | ||
c1ae5c75 ON |
1507 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
1508 | unsigned long func, struct pt_regs *regs) | |
1509 | { | |
1510 | struct trace_uprobe *tu; | |
b7e0bf34 | 1511 | struct uprobe_dispatch_data udd; |
dd9fa555 NK |
1512 | struct uprobe_cpu_buffer *ucb; |
1513 | int dsize, esize; | |
c1ae5c75 ON |
1514 | |
1515 | tu = container_of(con, struct trace_uprobe, consumer); | |
1516 | ||
b7e0bf34 NK |
1517 | udd.tu = tu; |
1518 | udd.bp_addr = func; | |
1519 | ||
1520 | current->utask->vaddr = (unsigned long) &udd; | |
1521 | ||
dd9fa555 NK |
1522 | if (WARN_ON_ONCE(!uprobe_cpu_buffer)) |
1523 | return 0; | |
1524 | ||
1525 | dsize = __get_data_size(&tu->tp, regs); | |
1526 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); | |
1527 | ||
1528 | ucb = uprobe_buffer_get(); | |
9178412d | 1529 | store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize); |
dd9fa555 | 1530 | |
747774d6 | 1531 | if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE)) |
dd9fa555 | 1532 | uretprobe_trace_func(tu, func, regs, ucb, dsize); |
c1ae5c75 ON |
1533 | |
1534 | #ifdef CONFIG_PERF_EVENTS | |
747774d6 | 1535 | if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE)) |
dd9fa555 | 1536 | uretprobe_perf_func(tu, func, regs, ucb, dsize); |
c1ae5c75 | 1537 | #endif |
dd9fa555 | 1538 | uprobe_buffer_put(ucb); |
c1ae5c75 ON |
1539 | return 0; |
1540 | } | |
1541 | ||
f3f096cf SD |
1542 | static struct trace_event_functions uprobe_funcs = { |
1543 | .trace = print_uprobe_event | |
1544 | }; | |
1545 | ||
04ae87a5 PZ |
1546 | static struct trace_event_fields uprobe_fields_array[] = { |
1547 | { .type = TRACE_FUNCTION_TYPE, | |
1548 | .define_fields = uprobe_event_define_fields }, | |
1549 | {} | |
1550 | }; | |
1551 | ||
e3dc9f89 | 1552 | static inline void init_trace_event_call(struct trace_uprobe *tu) |
f3f096cf | 1553 | { |
e3dc9f89 | 1554 | struct trace_event_call *call = trace_probe_event_call(&tu->tp); |
f3f096cf | 1555 | call->event.funcs = &uprobe_funcs; |
04ae87a5 | 1556 | call->class->fields_array = uprobe_fields_array; |
f3f096cf | 1557 | |
9fd2e48b | 1558 | call->flags = TRACE_EVENT_FL_UPROBE | TRACE_EVENT_FL_CAP_ANY; |
33ea4b24 | 1559 | call->class->reg = trace_uprobe_register; |
33ea4b24 SL |
1560 | } |
1561 | ||
1562 | static int register_uprobe_event(struct trace_uprobe *tu) | |
1563 | { | |
e3dc9f89 | 1564 | init_trace_event_call(tu); |
f3f096cf | 1565 | |
46e5376d | 1566 | return trace_probe_register_event_call(&tu->tp); |
f3f096cf SD |
1567 | } |
1568 | ||
c6c2401d | 1569 | static int unregister_uprobe_event(struct trace_uprobe *tu) |
f3f096cf | 1570 | { |
46e5376d | 1571 | return trace_probe_unregister_event_call(&tu->tp); |
f3f096cf SD |
1572 | } |
1573 | ||
33ea4b24 SL |
1574 | #ifdef CONFIG_PERF_EVENTS |
1575 | struct trace_event_call * | |
a6ca88b2 SL |
1576 | create_local_trace_uprobe(char *name, unsigned long offs, |
1577 | unsigned long ref_ctr_offset, bool is_return) | |
33ea4b24 | 1578 | { |
007517a0 | 1579 | enum probe_print_type ptype; |
33ea4b24 | 1580 | struct trace_uprobe *tu; |
33ea4b24 SL |
1581 | struct path path; |
1582 | int ret; | |
1583 | ||
1584 | ret = kern_path(name, LOOKUP_FOLLOW, &path); | |
1585 | if (ret) | |
1586 | return ERR_PTR(ret); | |
1587 | ||
0c92c7a3 SL |
1588 | if (!d_is_reg(path.dentry)) { |
1589 | path_put(&path); | |
33ea4b24 SL |
1590 | return ERR_PTR(-EINVAL); |
1591 | } | |
1592 | ||
1593 | /* | |
0597c49c | 1594 | * local trace_kprobes are not added to dyn_event, so they are never |
33ea4b24 SL |
1595 | * searched in find_trace_kprobe(). Therefore, there is no concern of |
1596 | * duplicated name "DUMMY_EVENT" here. | |
1597 | */ | |
1598 | tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0, | |
1599 | is_return); | |
1600 | ||
1601 | if (IS_ERR(tu)) { | |
1602 | pr_info("Failed to allocate trace_uprobe.(%d)\n", | |
1603 | (int)PTR_ERR(tu)); | |
0c92c7a3 | 1604 | path_put(&path); |
33ea4b24 SL |
1605 | return ERR_CAST(tu); |
1606 | } | |
1607 | ||
1608 | tu->offset = offs; | |
0c92c7a3 | 1609 | tu->path = path; |
a6ca88b2 | 1610 | tu->ref_ctr_offset = ref_ctr_offset; |
33ea4b24 | 1611 | tu->filename = kstrdup(name, GFP_KERNEL); |
8c722424 XW |
1612 | if (!tu->filename) { |
1613 | ret = -ENOMEM; | |
1614 | goto error; | |
1615 | } | |
1616 | ||
e3dc9f89 | 1617 | init_trace_event_call(tu); |
33ea4b24 | 1618 | |
007517a0 SRV |
1619 | ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL; |
1620 | if (traceprobe_set_print_fmt(&tu->tp, ptype) < 0) { | |
33ea4b24 SL |
1621 | ret = -ENOMEM; |
1622 | goto error; | |
1623 | } | |
1624 | ||
e3dc9f89 | 1625 | return trace_probe_event_call(&tu->tp); |
33ea4b24 SL |
1626 | error: |
1627 | free_trace_uprobe(tu); | |
1628 | return ERR_PTR(ret); | |
1629 | } | |
1630 | ||
1631 | void destroy_local_trace_uprobe(struct trace_event_call *event_call) | |
1632 | { | |
1633 | struct trace_uprobe *tu; | |
1634 | ||
60d53e2c | 1635 | tu = trace_uprobe_primary_from_call(event_call); |
33ea4b24 | 1636 | |
33ea4b24 SL |
1637 | free_trace_uprobe(tu); |
1638 | } | |
1639 | #endif /* CONFIG_PERF_EVENTS */ | |
1640 | ||
39bcdd6a | 1641 | /* Make a trace interface for controlling probe points */ |
f3f096cf SD |
1642 | static __init int init_uprobe_trace(void) |
1643 | { | |
0597c49c MH |
1644 | int ret; |
1645 | ||
1646 | ret = dyn_event_register(&trace_uprobe_ops); | |
1647 | if (ret) | |
1648 | return ret; | |
f3f096cf | 1649 | |
22c36b18 WY |
1650 | ret = tracing_init_dentry(); |
1651 | if (ret) | |
f3f096cf SD |
1652 | return 0; |
1653 | ||
21ccc9cd | 1654 | trace_create_file("uprobe_events", TRACE_MODE_WRITE, NULL, |
f3f096cf SD |
1655 | NULL, &uprobe_events_ops); |
1656 | /* Profile interface */ | |
21ccc9cd | 1657 | trace_create_file("uprobe_profile", TRACE_MODE_READ, NULL, |
f3f096cf SD |
1658 | NULL, &uprobe_profile_ops); |
1659 | return 0; | |
1660 | } | |
1661 | ||
1662 | fs_initcall(init_uprobe_trace); |