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