Commit | Line | Data |
---|---|---|
413d37d1 | 1 | /* |
77b44d1b | 2 | * Kprobes-based tracing events |
413d37d1 MH |
3 | * |
4 | * Created by Masami Hiramatsu <mhiramat@redhat.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | ||
20 | #include <linux/module.h> | |
21 | #include <linux/uaccess.h> | |
413d37d1 | 22 | |
8ab83f56 | 23 | #include "trace_probe.h" |
1ff511e3 | 24 | |
8ab83f56 | 25 | #define KPROBE_EVENT_SYSTEM "kprobes" |
e09c8614 | 26 | |
413d37d1 | 27 | /** |
77b44d1b | 28 | * Kprobe event core functions |
413d37d1 | 29 | */ |
c31ffb3f | 30 | struct trace_kprobe { |
413d37d1 | 31 | struct list_head list; |
4a846b44 | 32 | struct kretprobe rp; /* Use rp.kp for kprobe use */ |
cd7e7bd5 | 33 | unsigned long nhit; |
413d37d1 | 34 | const char *symbol; /* symbol name */ |
c31ffb3f | 35 | struct trace_probe tp; |
413d37d1 MH |
36 | }; |
37 | ||
c31ffb3f NK |
38 | #define SIZEOF_TRACE_KPROBE(n) \ |
39 | (offsetof(struct trace_kprobe, tp.args) + \ | |
eca0d916 | 40 | (sizeof(struct probe_arg) * (n))) |
a82378d8 | 41 | |
93ccae7a | 42 | |
3da0f180 | 43 | static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk) |
413d37d1 | 44 | { |
c31ffb3f | 45 | return tk->rp.handler != NULL; |
413d37d1 MH |
46 | } |
47 | ||
3da0f180 | 48 | static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk) |
413d37d1 | 49 | { |
c31ffb3f | 50 | return tk->symbol ? tk->symbol : "unknown"; |
413d37d1 MH |
51 | } |
52 | ||
3da0f180 | 53 | static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk) |
61424318 | 54 | { |
c31ffb3f | 55 | return tk->rp.kp.offset; |
61424318 MH |
56 | } |
57 | ||
3da0f180 | 58 | static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk) |
61424318 | 59 | { |
c31ffb3f | 60 | return !!(kprobe_gone(&tk->rp.kp)); |
61424318 MH |
61 | } |
62 | ||
3da0f180 | 63 | static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk, |
c31ffb3f | 64 | struct module *mod) |
61424318 MH |
65 | { |
66 | int len = strlen(mod->name); | |
c31ffb3f | 67 | const char *name = trace_kprobe_symbol(tk); |
61424318 MH |
68 | return strncmp(mod->name, name, len) == 0 && name[len] == ':'; |
69 | } | |
70 | ||
3da0f180 | 71 | static nokprobe_inline bool trace_kprobe_is_on_module(struct trace_kprobe *tk) |
61424318 | 72 | { |
c31ffb3f | 73 | return !!strchr(trace_kprobe_symbol(tk), ':'); |
61424318 MH |
74 | } |
75 | ||
c31ffb3f NK |
76 | static int register_kprobe_event(struct trace_kprobe *tk); |
77 | static int unregister_kprobe_event(struct trace_kprobe *tk); | |
413d37d1 MH |
78 | |
79 | static DEFINE_MUTEX(probe_lock); | |
80 | static LIST_HEAD(probe_list); | |
81 | ||
50d78056 MH |
82 | static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs); |
83 | static int kretprobe_dispatcher(struct kretprobe_instance *ri, | |
84 | struct pt_regs *regs); | |
85 | ||
1301a44e NK |
86 | /* Memory fetching by symbol */ |
87 | struct symbol_cache { | |
88 | char *symbol; | |
89 | long offset; | |
90 | unsigned long addr; | |
91 | }; | |
92 | ||
93 | unsigned long update_symbol_cache(struct symbol_cache *sc) | |
94 | { | |
95 | sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol); | |
96 | ||
97 | if (sc->addr) | |
98 | sc->addr += sc->offset; | |
99 | ||
100 | return sc->addr; | |
101 | } | |
102 | ||
103 | void free_symbol_cache(struct symbol_cache *sc) | |
104 | { | |
105 | kfree(sc->symbol); | |
106 | kfree(sc); | |
107 | } | |
108 | ||
109 | struct symbol_cache *alloc_symbol_cache(const char *sym, long offset) | |
110 | { | |
111 | struct symbol_cache *sc; | |
112 | ||
113 | if (!sym || strlen(sym) == 0) | |
114 | return NULL; | |
115 | ||
116 | sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL); | |
117 | if (!sc) | |
118 | return NULL; | |
119 | ||
120 | sc->symbol = kstrdup(sym, GFP_KERNEL); | |
121 | if (!sc->symbol) { | |
122 | kfree(sc); | |
123 | return NULL; | |
124 | } | |
125 | sc->offset = offset; | |
126 | update_symbol_cache(sc); | |
127 | ||
128 | return sc; | |
129 | } | |
130 | ||
3fd996a2 NK |
131 | /* |
132 | * Kprobes-specific fetch functions | |
133 | */ | |
134 | #define DEFINE_FETCH_stack(type) \ | |
3da0f180 | 135 | static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \ |
3fd996a2 NK |
136 | void *offset, void *dest) \ |
137 | { \ | |
138 | *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \ | |
139 | (unsigned int)((unsigned long)offset)); \ | |
3da0f180 MH |
140 | } \ |
141 | NOKPROBE_SYMBOL(FETCH_FUNC_NAME(stack, type)); | |
142 | ||
3fd996a2 NK |
143 | DEFINE_BASIC_FETCH_FUNCS(stack) |
144 | /* No string on the stack entry */ | |
145 | #define fetch_stack_string NULL | |
146 | #define fetch_stack_string_size NULL | |
147 | ||
5baaa59e | 148 | #define DEFINE_FETCH_memory(type) \ |
3da0f180 | 149 | static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \ |
5baaa59e NK |
150 | void *addr, void *dest) \ |
151 | { \ | |
152 | type retval; \ | |
153 | if (probe_kernel_address(addr, retval)) \ | |
154 | *(type *)dest = 0; \ | |
155 | else \ | |
156 | *(type *)dest = retval; \ | |
3da0f180 MH |
157 | } \ |
158 | NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, type)); | |
159 | ||
5baaa59e NK |
160 | DEFINE_BASIC_FETCH_FUNCS(memory) |
161 | /* | |
162 | * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max | |
163 | * length and relative data location. | |
164 | */ | |
3da0f180 MH |
165 | static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs, |
166 | void *addr, void *dest) | |
5baaa59e | 167 | { |
5baaa59e NK |
168 | int maxlen = get_rloc_len(*(u32 *)dest); |
169 | u8 *dst = get_rloc_data(dest); | |
1a6877b9 | 170 | long ret; |
5baaa59e NK |
171 | |
172 | if (!maxlen) | |
173 | return; | |
174 | ||
175 | /* | |
176 | * Try to get string again, since the string can be changed while | |
177 | * probing. | |
178 | */ | |
1a6877b9 | 179 | ret = strncpy_from_unsafe(dst, addr, maxlen); |
5baaa59e NK |
180 | |
181 | if (ret < 0) { /* Failed to fetch string */ | |
1a6877b9 | 182 | dst[0] = '\0'; |
5baaa59e NK |
183 | *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest)); |
184 | } else { | |
1a6877b9 | 185 | *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest)); |
5baaa59e NK |
186 | } |
187 | } | |
3da0f180 | 188 | NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string)); |
5baaa59e NK |
189 | |
190 | /* Return the length of string -- including null terminal byte */ | |
3da0f180 MH |
191 | static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs, |
192 | void *addr, void *dest) | |
5baaa59e NK |
193 | { |
194 | mm_segment_t old_fs; | |
195 | int ret, len = 0; | |
196 | u8 c; | |
197 | ||
198 | old_fs = get_fs(); | |
199 | set_fs(KERNEL_DS); | |
200 | pagefault_disable(); | |
201 | ||
202 | do { | |
203 | ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1); | |
204 | len++; | |
205 | } while (c && ret == 0 && len < MAX_STRING_SIZE); | |
206 | ||
207 | pagefault_enable(); | |
208 | set_fs(old_fs); | |
209 | ||
210 | if (ret < 0) /* Failed to check the length */ | |
211 | *(u32 *)dest = 0; | |
212 | else | |
213 | *(u32 *)dest = len; | |
214 | } | |
3da0f180 | 215 | NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string_size)); |
5baaa59e | 216 | |
1301a44e | 217 | #define DEFINE_FETCH_symbol(type) \ |
3da0f180 | 218 | void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, void *data, void *dest)\ |
1301a44e NK |
219 | { \ |
220 | struct symbol_cache *sc = data; \ | |
221 | if (sc->addr) \ | |
222 | fetch_memory_##type(regs, (void *)sc->addr, dest); \ | |
223 | else \ | |
224 | *(type *)dest = 0; \ | |
3da0f180 MH |
225 | } \ |
226 | NOKPROBE_SYMBOL(FETCH_FUNC_NAME(symbol, type)); | |
227 | ||
1301a44e NK |
228 | DEFINE_BASIC_FETCH_FUNCS(symbol) |
229 | DEFINE_FETCH_symbol(string) | |
230 | DEFINE_FETCH_symbol(string_size) | |
231 | ||
b7e0bf34 NK |
232 | /* kprobes don't support file_offset fetch methods */ |
233 | #define fetch_file_offset_u8 NULL | |
234 | #define fetch_file_offset_u16 NULL | |
235 | #define fetch_file_offset_u32 NULL | |
236 | #define fetch_file_offset_u64 NULL | |
237 | #define fetch_file_offset_string NULL | |
238 | #define fetch_file_offset_string_size NULL | |
239 | ||
34fee3a1 | 240 | /* Fetch type information table */ |
d9a16d3a | 241 | static const struct fetch_type kprobes_fetch_type_table[] = { |
34fee3a1 NK |
242 | /* Special types */ |
243 | [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string, | |
244 | sizeof(u32), 1, "__data_loc char[]"), | |
245 | [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32, | |
246 | string_size, sizeof(u32), 0, "u32"), | |
247 | /* Basic types */ | |
248 | ASSIGN_FETCH_TYPE(u8, u8, 0), | |
249 | ASSIGN_FETCH_TYPE(u16, u16, 0), | |
250 | ASSIGN_FETCH_TYPE(u32, u32, 0), | |
251 | ASSIGN_FETCH_TYPE(u64, u64, 0), | |
252 | ASSIGN_FETCH_TYPE(s8, u8, 1), | |
253 | ASSIGN_FETCH_TYPE(s16, u16, 1), | |
254 | ASSIGN_FETCH_TYPE(s32, u32, 1), | |
255 | ASSIGN_FETCH_TYPE(s64, u64, 1), | |
256 | ||
257 | ASSIGN_FETCH_TYPE_END | |
258 | }; | |
259 | ||
4a846b44 MH |
260 | /* |
261 | * Allocate new trace_probe and initialize it (including kprobes). | |
262 | */ | |
c31ffb3f | 263 | static struct trace_kprobe *alloc_trace_kprobe(const char *group, |
f52487e9 | 264 | const char *event, |
4a846b44 MH |
265 | void *addr, |
266 | const char *symbol, | |
267 | unsigned long offs, | |
3a6b7666 | 268 | int nargs, bool is_return) |
413d37d1 | 269 | { |
c31ffb3f | 270 | struct trace_kprobe *tk; |
6f3cf440 | 271 | int ret = -ENOMEM; |
413d37d1 | 272 | |
c31ffb3f NK |
273 | tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL); |
274 | if (!tk) | |
6f3cf440 | 275 | return ERR_PTR(ret); |
413d37d1 MH |
276 | |
277 | if (symbol) { | |
c31ffb3f NK |
278 | tk->symbol = kstrdup(symbol, GFP_KERNEL); |
279 | if (!tk->symbol) | |
413d37d1 | 280 | goto error; |
c31ffb3f NK |
281 | tk->rp.kp.symbol_name = tk->symbol; |
282 | tk->rp.kp.offset = offs; | |
4a846b44 | 283 | } else |
c31ffb3f | 284 | tk->rp.kp.addr = addr; |
4a846b44 MH |
285 | |
286 | if (is_return) | |
c31ffb3f | 287 | tk->rp.handler = kretprobe_dispatcher; |
4a846b44 | 288 | else |
c31ffb3f | 289 | tk->rp.kp.pre_handler = kprobe_dispatcher; |
4a846b44 | 290 | |
da34634f | 291 | if (!event || !is_good_name(event)) { |
6f3cf440 | 292 | ret = -EINVAL; |
4263565d | 293 | goto error; |
6f3cf440 MH |
294 | } |
295 | ||
c31ffb3f NK |
296 | tk->tp.call.class = &tk->tp.class; |
297 | tk->tp.call.name = kstrdup(event, GFP_KERNEL); | |
298 | if (!tk->tp.call.name) | |
4263565d | 299 | goto error; |
413d37d1 | 300 | |
da34634f | 301 | if (!group || !is_good_name(group)) { |
6f3cf440 | 302 | ret = -EINVAL; |
f52487e9 | 303 | goto error; |
6f3cf440 MH |
304 | } |
305 | ||
c31ffb3f NK |
306 | tk->tp.class.system = kstrdup(group, GFP_KERNEL); |
307 | if (!tk->tp.class.system) | |
f52487e9 MH |
308 | goto error; |
309 | ||
c31ffb3f NK |
310 | INIT_LIST_HEAD(&tk->list); |
311 | INIT_LIST_HEAD(&tk->tp.files); | |
312 | return tk; | |
413d37d1 | 313 | error: |
c31ffb3f NK |
314 | kfree(tk->tp.call.name); |
315 | kfree(tk->symbol); | |
316 | kfree(tk); | |
6f3cf440 | 317 | return ERR_PTR(ret); |
413d37d1 MH |
318 | } |
319 | ||
c31ffb3f | 320 | static void free_trace_kprobe(struct trace_kprobe *tk) |
413d37d1 MH |
321 | { |
322 | int i; | |
323 | ||
c31ffb3f NK |
324 | for (i = 0; i < tk->tp.nr_args; i++) |
325 | traceprobe_free_probe_arg(&tk->tp.args[i]); | |
413d37d1 | 326 | |
c31ffb3f NK |
327 | kfree(tk->tp.call.class->system); |
328 | kfree(tk->tp.call.name); | |
329 | kfree(tk->symbol); | |
330 | kfree(tk); | |
413d37d1 MH |
331 | } |
332 | ||
c31ffb3f NK |
333 | static struct trace_kprobe *find_trace_kprobe(const char *event, |
334 | const char *group) | |
413d37d1 | 335 | { |
c31ffb3f | 336 | struct trace_kprobe *tk; |
413d37d1 | 337 | |
c31ffb3f | 338 | list_for_each_entry(tk, &probe_list, list) |
687fcc4a | 339 | if (strcmp(trace_event_name(&tk->tp.call), event) == 0 && |
c31ffb3f NK |
340 | strcmp(tk->tp.call.class->system, group) == 0) |
341 | return tk; | |
413d37d1 MH |
342 | return NULL; |
343 | } | |
344 | ||
41a7dd42 MH |
345 | /* |
346 | * Enable trace_probe | |
347 | * if the file is NULL, enable "perf" handler, or enable "trace" handler. | |
348 | */ | |
349 | static int | |
7f1d2f82 | 350 | enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file) |
1538f888 MH |
351 | { |
352 | int ret = 0; | |
353 | ||
41a7dd42 | 354 | if (file) { |
b04d52e3 ON |
355 | struct event_file_link *link; |
356 | ||
357 | link = kmalloc(sizeof(*link), GFP_KERNEL); | |
358 | if (!link) { | |
41a7dd42 | 359 | ret = -ENOMEM; |
3fe3d619 | 360 | goto out; |
41a7dd42 | 361 | } |
41a7dd42 | 362 | |
b04d52e3 | 363 | link->file = file; |
c31ffb3f | 364 | list_add_tail_rcu(&link->list, &tk->tp.files); |
41a7dd42 | 365 | |
c31ffb3f | 366 | tk->tp.flags |= TP_FLAG_TRACE; |
41a7dd42 | 367 | } else |
c31ffb3f | 368 | tk->tp.flags |= TP_FLAG_PROFILE; |
41a7dd42 | 369 | |
c31ffb3f NK |
370 | if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) { |
371 | if (trace_kprobe_is_return(tk)) | |
372 | ret = enable_kretprobe(&tk->rp); | |
1538f888 | 373 | else |
c31ffb3f | 374 | ret = enable_kprobe(&tk->rp.kp); |
1538f888 | 375 | } |
3fe3d619 | 376 | out: |
1538f888 MH |
377 | return ret; |
378 | } | |
379 | ||
41a7dd42 MH |
380 | /* |
381 | * Disable trace_probe | |
382 | * if the file is NULL, disable "perf" handler, or disable "trace" handler. | |
383 | */ | |
384 | static int | |
7f1d2f82 | 385 | disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file) |
1538f888 | 386 | { |
a232e270 MH |
387 | struct event_file_link *link = NULL; |
388 | int wait = 0; | |
41a7dd42 MH |
389 | int ret = 0; |
390 | ||
41a7dd42 | 391 | if (file) { |
c31ffb3f | 392 | link = find_event_file_link(&tk->tp, file); |
b04d52e3 | 393 | if (!link) { |
41a7dd42 | 394 | ret = -EINVAL; |
3fe3d619 | 395 | goto out; |
41a7dd42 MH |
396 | } |
397 | ||
b04d52e3 | 398 | list_del_rcu(&link->list); |
a232e270 | 399 | wait = 1; |
c31ffb3f | 400 | if (!list_empty(&tk->tp.files)) |
b04d52e3 | 401 | goto out; |
41a7dd42 | 402 | |
c31ffb3f | 403 | tk->tp.flags &= ~TP_FLAG_TRACE; |
41a7dd42 | 404 | } else |
c31ffb3f | 405 | tk->tp.flags &= ~TP_FLAG_PROFILE; |
41a7dd42 | 406 | |
c31ffb3f NK |
407 | if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) { |
408 | if (trace_kprobe_is_return(tk)) | |
409 | disable_kretprobe(&tk->rp); | |
1538f888 | 410 | else |
c31ffb3f | 411 | disable_kprobe(&tk->rp.kp); |
a232e270 | 412 | wait = 1; |
1538f888 | 413 | } |
3fe3d619 | 414 | out: |
a232e270 MH |
415 | if (wait) { |
416 | /* | |
417 | * Synchronize with kprobe_trace_func/kretprobe_trace_func | |
418 | * to ensure disabled (all running handlers are finished). | |
419 | * This is not only for kfree(), but also the caller, | |
420 | * trace_remove_event_call() supposes it for releasing | |
421 | * event_call related objects, which will be accessed in | |
422 | * the kprobe_trace_func/kretprobe_trace_func. | |
423 | */ | |
424 | synchronize_sched(); | |
425 | kfree(link); /* Ignored if link == NULL */ | |
426 | } | |
427 | ||
41a7dd42 | 428 | return ret; |
1538f888 MH |
429 | } |
430 | ||
61424318 | 431 | /* Internal register function - just handle k*probes and flags */ |
c31ffb3f | 432 | static int __register_trace_kprobe(struct trace_kprobe *tk) |
413d37d1 | 433 | { |
7f6878a3 | 434 | int i, ret; |
61424318 | 435 | |
c31ffb3f | 436 | if (trace_probe_is_registered(&tk->tp)) |
61424318 MH |
437 | return -EINVAL; |
438 | ||
c31ffb3f NK |
439 | for (i = 0; i < tk->tp.nr_args; i++) |
440 | traceprobe_update_arg(&tk->tp.args[i]); | |
7f6878a3 | 441 | |
61424318 | 442 | /* Set/clear disabled flag according to tp->flag */ |
c31ffb3f NK |
443 | if (trace_probe_is_enabled(&tk->tp)) |
444 | tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED; | |
61424318 | 445 | else |
c31ffb3f | 446 | tk->rp.kp.flags |= KPROBE_FLAG_DISABLED; |
61424318 | 447 | |
c31ffb3f NK |
448 | if (trace_kprobe_is_return(tk)) |
449 | ret = register_kretprobe(&tk->rp); | |
413d37d1 | 450 | else |
c31ffb3f | 451 | ret = register_kprobe(&tk->rp.kp); |
61424318 MH |
452 | |
453 | if (ret == 0) | |
c31ffb3f | 454 | tk->tp.flags |= TP_FLAG_REGISTERED; |
61424318 MH |
455 | else { |
456 | pr_warning("Could not insert probe at %s+%lu: %d\n", | |
c31ffb3f NK |
457 | trace_kprobe_symbol(tk), trace_kprobe_offset(tk), ret); |
458 | if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) { | |
61424318 MH |
459 | pr_warning("This probe might be able to register after" |
460 | "target module is loaded. Continue.\n"); | |
461 | ret = 0; | |
462 | } else if (ret == -EILSEQ) { | |
463 | pr_warning("Probing address(0x%p) is not an " | |
464 | "instruction boundary.\n", | |
c31ffb3f | 465 | tk->rp.kp.addr); |
61424318 MH |
466 | ret = -EINVAL; |
467 | } | |
468 | } | |
469 | ||
470 | return ret; | |
471 | } | |
472 | ||
473 | /* Internal unregister function - just handle k*probes and flags */ | |
c31ffb3f | 474 | static void __unregister_trace_kprobe(struct trace_kprobe *tk) |
61424318 | 475 | { |
c31ffb3f NK |
476 | if (trace_probe_is_registered(&tk->tp)) { |
477 | if (trace_kprobe_is_return(tk)) | |
478 | unregister_kretprobe(&tk->rp); | |
61424318 | 479 | else |
c31ffb3f NK |
480 | unregister_kprobe(&tk->rp.kp); |
481 | tk->tp.flags &= ~TP_FLAG_REGISTERED; | |
61424318 | 482 | /* Cleanup kprobe for reuse */ |
c31ffb3f NK |
483 | if (tk->rp.kp.symbol_name) |
484 | tk->rp.kp.addr = NULL; | |
61424318 MH |
485 | } |
486 | } | |
487 | ||
488 | /* Unregister a trace_probe and probe_event: call with locking probe_lock */ | |
c31ffb3f | 489 | static int unregister_trace_kprobe(struct trace_kprobe *tk) |
61424318 | 490 | { |
02ca1521 | 491 | /* Enabled event can not be unregistered */ |
c31ffb3f | 492 | if (trace_probe_is_enabled(&tk->tp)) |
02ca1521 MH |
493 | return -EBUSY; |
494 | ||
40c32592 | 495 | /* Will fail if probe is being used by ftrace or perf */ |
c31ffb3f | 496 | if (unregister_kprobe_event(tk)) |
40c32592 SRRH |
497 | return -EBUSY; |
498 | ||
c31ffb3f NK |
499 | __unregister_trace_kprobe(tk); |
500 | list_del(&tk->list); | |
02ca1521 MH |
501 | |
502 | return 0; | |
413d37d1 MH |
503 | } |
504 | ||
505 | /* Register a trace_probe and probe_event */ | |
c31ffb3f | 506 | static int register_trace_kprobe(struct trace_kprobe *tk) |
413d37d1 | 507 | { |
c31ffb3f | 508 | struct trace_kprobe *old_tk; |
413d37d1 MH |
509 | int ret; |
510 | ||
511 | mutex_lock(&probe_lock); | |
512 | ||
61424318 | 513 | /* Delete old (same name) event if exist */ |
687fcc4a | 514 | old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call), |
de7b2973 | 515 | tk->tp.call.class->system); |
c31ffb3f NK |
516 | if (old_tk) { |
517 | ret = unregister_trace_kprobe(old_tk); | |
02ca1521 MH |
518 | if (ret < 0) |
519 | goto end; | |
c31ffb3f | 520 | free_trace_kprobe(old_tk); |
2d5e067e | 521 | } |
61424318 MH |
522 | |
523 | /* Register new event */ | |
c31ffb3f | 524 | ret = register_kprobe_event(tk); |
2d5e067e | 525 | if (ret) { |
426d3107 | 526 | pr_warning("Failed to register probe event(%d)\n", ret); |
2d5e067e MH |
527 | goto end; |
528 | } | |
529 | ||
61424318 | 530 | /* Register k*probe */ |
c31ffb3f | 531 | ret = __register_trace_kprobe(tk); |
61424318 | 532 | if (ret < 0) |
c31ffb3f | 533 | unregister_kprobe_event(tk); |
61424318 | 534 | else |
c31ffb3f | 535 | list_add_tail(&tk->list, &probe_list); |
61424318 | 536 | |
413d37d1 MH |
537 | end: |
538 | mutex_unlock(&probe_lock); | |
539 | return ret; | |
540 | } | |
541 | ||
61424318 | 542 | /* Module notifier call back, checking event on the module */ |
c31ffb3f | 543 | static int trace_kprobe_module_callback(struct notifier_block *nb, |
61424318 MH |
544 | unsigned long val, void *data) |
545 | { | |
546 | struct module *mod = data; | |
c31ffb3f | 547 | struct trace_kprobe *tk; |
61424318 MH |
548 | int ret; |
549 | ||
550 | if (val != MODULE_STATE_COMING) | |
551 | return NOTIFY_DONE; | |
552 | ||
553 | /* Update probes on coming module */ | |
554 | mutex_lock(&probe_lock); | |
c31ffb3f NK |
555 | list_for_each_entry(tk, &probe_list, list) { |
556 | if (trace_kprobe_within_module(tk, mod)) { | |
02ca1521 | 557 | /* Don't need to check busy - this should have gone. */ |
c31ffb3f NK |
558 | __unregister_trace_kprobe(tk); |
559 | ret = __register_trace_kprobe(tk); | |
61424318 MH |
560 | if (ret) |
561 | pr_warning("Failed to re-register probe %s on" | |
562 | "%s: %d\n", | |
687fcc4a | 563 | trace_event_name(&tk->tp.call), |
de7b2973 | 564 | mod->name, ret); |
61424318 MH |
565 | } |
566 | } | |
567 | mutex_unlock(&probe_lock); | |
568 | ||
569 | return NOTIFY_DONE; | |
570 | } | |
571 | ||
c31ffb3f NK |
572 | static struct notifier_block trace_kprobe_module_nb = { |
573 | .notifier_call = trace_kprobe_module_callback, | |
61424318 MH |
574 | .priority = 1 /* Invoked after kprobe module callback */ |
575 | }; | |
576 | ||
c31ffb3f | 577 | static int create_trace_kprobe(int argc, char **argv) |
413d37d1 MH |
578 | { |
579 | /* | |
580 | * Argument syntax: | |
61424318 MH |
581 | * - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS] |
582 | * - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS] | |
413d37d1 | 583 | * Fetch args: |
2e06ff63 MH |
584 | * $retval : fetch return value |
585 | * $stack : fetch stack address | |
586 | * $stackN : fetch Nth of stack (N:0-) | |
413d37d1 MH |
587 | * @ADDR : fetch memory at ADDR (ADDR should be in kernel) |
588 | * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol) | |
589 | * %REG : fetch register REG | |
93ccae7a | 590 | * Dereferencing memory fetch: |
413d37d1 | 591 | * +|-offs(ARG) : fetch memory at ARG +|- offs address. |
eca0d916 MH |
592 | * Alias name of args: |
593 | * NAME=FETCHARG : set NAME as alias of FETCHARG. | |
93ccae7a MH |
594 | * Type of args: |
595 | * FETCHARG:TYPE : use TYPE instead of unsigned long. | |
413d37d1 | 596 | */ |
c31ffb3f | 597 | struct trace_kprobe *tk; |
413d37d1 | 598 | int i, ret = 0; |
3a6b7666 | 599 | bool is_return = false, is_delete = false; |
93ccae7a | 600 | char *symbol = NULL, *event = NULL, *group = NULL; |
da34634f | 601 | char *arg; |
2fba0c88 | 602 | unsigned long offset = 0; |
413d37d1 | 603 | void *addr = NULL; |
4a846b44 | 604 | char buf[MAX_EVENT_NAME_LEN]; |
413d37d1 | 605 | |
a7c312be | 606 | /* argc must be >= 1 */ |
413d37d1 | 607 | if (argv[0][0] == 'p') |
3a6b7666 | 608 | is_return = false; |
413d37d1 | 609 | else if (argv[0][0] == 'r') |
3a6b7666 | 610 | is_return = true; |
a7c312be | 611 | else if (argv[0][0] == '-') |
3a6b7666 | 612 | is_delete = true; |
e63cc239 | 613 | else { |
a7c312be MH |
614 | pr_info("Probe definition must be started with 'p', 'r' or" |
615 | " '-'.\n"); | |
413d37d1 | 616 | return -EINVAL; |
e63cc239 | 617 | } |
413d37d1 MH |
618 | |
619 | if (argv[0][1] == ':') { | |
620 | event = &argv[0][2]; | |
f52487e9 MH |
621 | if (strchr(event, '/')) { |
622 | group = event; | |
623 | event = strchr(group, '/') + 1; | |
624 | event[-1] = '\0'; | |
625 | if (strlen(group) == 0) { | |
a5efd925 | 626 | pr_info("Group name is not specified\n"); |
f52487e9 MH |
627 | return -EINVAL; |
628 | } | |
629 | } | |
413d37d1 | 630 | if (strlen(event) == 0) { |
a5efd925 | 631 | pr_info("Event name is not specified\n"); |
413d37d1 MH |
632 | return -EINVAL; |
633 | } | |
634 | } | |
a7c312be MH |
635 | if (!group) |
636 | group = KPROBE_EVENT_SYSTEM; | |
413d37d1 | 637 | |
a7c312be MH |
638 | if (is_delete) { |
639 | if (!event) { | |
640 | pr_info("Delete command needs an event name.\n"); | |
641 | return -EINVAL; | |
642 | } | |
9da79ab8 | 643 | mutex_lock(&probe_lock); |
c31ffb3f NK |
644 | tk = find_trace_kprobe(event, group); |
645 | if (!tk) { | |
9da79ab8 | 646 | mutex_unlock(&probe_lock); |
a7c312be MH |
647 | pr_info("Event %s/%s doesn't exist.\n", group, event); |
648 | return -ENOENT; | |
649 | } | |
650 | /* delete an event */ | |
c31ffb3f | 651 | ret = unregister_trace_kprobe(tk); |
02ca1521 | 652 | if (ret == 0) |
c31ffb3f | 653 | free_trace_kprobe(tk); |
9da79ab8 | 654 | mutex_unlock(&probe_lock); |
02ca1521 | 655 | return ret; |
a7c312be MH |
656 | } |
657 | ||
658 | if (argc < 2) { | |
659 | pr_info("Probe point is not specified.\n"); | |
660 | return -EINVAL; | |
661 | } | |
413d37d1 | 662 | if (isdigit(argv[1][0])) { |
e63cc239 MH |
663 | if (is_return) { |
664 | pr_info("Return probe point must be a symbol.\n"); | |
413d37d1 | 665 | return -EINVAL; |
e63cc239 | 666 | } |
413d37d1 | 667 | /* an address specified */ |
bcd83ea6 | 668 | ret = kstrtoul(&argv[1][0], 0, (unsigned long *)&addr); |
e63cc239 MH |
669 | if (ret) { |
670 | pr_info("Failed to parse address.\n"); | |
413d37d1 | 671 | return ret; |
e63cc239 | 672 | } |
413d37d1 MH |
673 | } else { |
674 | /* a symbol specified */ | |
675 | symbol = argv[1]; | |
676 | /* TODO: support .init module functions */ | |
8ab83f56 | 677 | ret = traceprobe_split_symbol_offset(symbol, &offset); |
e63cc239 MH |
678 | if (ret) { |
679 | pr_info("Failed to parse symbol.\n"); | |
413d37d1 | 680 | return ret; |
e63cc239 MH |
681 | } |
682 | if (offset && is_return) { | |
683 | pr_info("Return probe must be used without offset.\n"); | |
413d37d1 | 684 | return -EINVAL; |
e63cc239 | 685 | } |
413d37d1 | 686 | } |
a82378d8 | 687 | argc -= 2; argv += 2; |
413d37d1 MH |
688 | |
689 | /* setup a probe */ | |
4263565d MH |
690 | if (!event) { |
691 | /* Make a new event name */ | |
4263565d | 692 | if (symbol) |
6f3cf440 | 693 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld", |
4263565d MH |
694 | is_return ? 'r' : 'p', symbol, offset); |
695 | else | |
6f3cf440 | 696 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p", |
4263565d | 697 | is_return ? 'r' : 'p', addr); |
4a846b44 MH |
698 | event = buf; |
699 | } | |
c31ffb3f | 700 | tk = alloc_trace_kprobe(group, event, addr, symbol, offset, argc, |
f52487e9 | 701 | is_return); |
c31ffb3f | 702 | if (IS_ERR(tk)) { |
e63cc239 | 703 | pr_info("Failed to allocate trace_probe.(%d)\n", |
c31ffb3f NK |
704 | (int)PTR_ERR(tk)); |
705 | return PTR_ERR(tk); | |
e63cc239 | 706 | } |
413d37d1 | 707 | |
413d37d1 | 708 | /* parse arguments */ |
a82378d8 MH |
709 | ret = 0; |
710 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { | |
c31ffb3f NK |
711 | struct probe_arg *parg = &tk->tp.args[i]; |
712 | ||
61a52736 | 713 | /* Increment count for freeing args in error case */ |
c31ffb3f | 714 | tk->tp.nr_args++; |
61a52736 | 715 | |
eca0d916 MH |
716 | /* Parse argument name */ |
717 | arg = strchr(argv[i], '='); | |
aba91595 | 718 | if (arg) { |
eca0d916 | 719 | *arg++ = '\0'; |
c31ffb3f | 720 | parg->name = kstrdup(argv[i], GFP_KERNEL); |
aba91595 | 721 | } else { |
eca0d916 | 722 | arg = argv[i]; |
aba91595 MH |
723 | /* If argument name is omitted, set "argN" */ |
724 | snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1); | |
c31ffb3f | 725 | parg->name = kstrdup(buf, GFP_KERNEL); |
aba91595 | 726 | } |
a703d946 | 727 | |
c31ffb3f | 728 | if (!parg->name) { |
aba91595 | 729 | pr_info("Failed to allocate argument[%d] name.\n", i); |
ba8665d7 | 730 | ret = -ENOMEM; |
413d37d1 MH |
731 | goto error; |
732 | } | |
da34634f | 733 | |
c31ffb3f | 734 | if (!is_good_name(parg->name)) { |
da34634f | 735 | pr_info("Invalid argument[%d] name: %s\n", |
c31ffb3f | 736 | i, parg->name); |
da34634f MH |
737 | ret = -EINVAL; |
738 | goto error; | |
739 | } | |
93ccae7a | 740 | |
c31ffb3f NK |
741 | if (traceprobe_conflict_field_name(parg->name, |
742 | tk->tp.args, i)) { | |
aba91595 | 743 | pr_info("Argument[%d] name '%s' conflicts with " |
93ccae7a MH |
744 | "another field.\n", i, argv[i]); |
745 | ret = -EINVAL; | |
746 | goto error; | |
747 | } | |
ba8665d7 MH |
748 | |
749 | /* Parse fetch argument */ | |
c31ffb3f | 750 | ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg, |
d9a16d3a SR |
751 | is_return, true, |
752 | kprobes_fetch_type_table); | |
e63cc239 | 753 | if (ret) { |
aba91595 | 754 | pr_info("Parse error at argument[%d]. (%d)\n", i, ret); |
413d37d1 | 755 | goto error; |
e63cc239 | 756 | } |
413d37d1 | 757 | } |
413d37d1 | 758 | |
c31ffb3f | 759 | ret = register_trace_kprobe(tk); |
413d37d1 MH |
760 | if (ret) |
761 | goto error; | |
762 | return 0; | |
763 | ||
764 | error: | |
c31ffb3f | 765 | free_trace_kprobe(tk); |
413d37d1 MH |
766 | return ret; |
767 | } | |
768 | ||
c31ffb3f | 769 | static int release_all_trace_kprobes(void) |
413d37d1 | 770 | { |
c31ffb3f | 771 | struct trace_kprobe *tk; |
02ca1521 | 772 | int ret = 0; |
413d37d1 MH |
773 | |
774 | mutex_lock(&probe_lock); | |
02ca1521 | 775 | /* Ensure no probe is in use. */ |
c31ffb3f NK |
776 | list_for_each_entry(tk, &probe_list, list) |
777 | if (trace_probe_is_enabled(&tk->tp)) { | |
02ca1521 MH |
778 | ret = -EBUSY; |
779 | goto end; | |
780 | } | |
413d37d1 MH |
781 | /* TODO: Use batch unregistration */ |
782 | while (!list_empty(&probe_list)) { | |
c31ffb3f NK |
783 | tk = list_entry(probe_list.next, struct trace_kprobe, list); |
784 | ret = unregister_trace_kprobe(tk); | |
40c32592 SRRH |
785 | if (ret) |
786 | goto end; | |
c31ffb3f | 787 | free_trace_kprobe(tk); |
413d37d1 | 788 | } |
02ca1521 MH |
789 | |
790 | end: | |
413d37d1 | 791 | mutex_unlock(&probe_lock); |
02ca1521 MH |
792 | |
793 | return ret; | |
413d37d1 MH |
794 | } |
795 | ||
413d37d1 MH |
796 | /* Probes listing interfaces */ |
797 | static void *probes_seq_start(struct seq_file *m, loff_t *pos) | |
798 | { | |
799 | mutex_lock(&probe_lock); | |
800 | return seq_list_start(&probe_list, *pos); | |
801 | } | |
802 | ||
803 | static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos) | |
804 | { | |
805 | return seq_list_next(v, &probe_list, pos); | |
806 | } | |
807 | ||
808 | static void probes_seq_stop(struct seq_file *m, void *v) | |
809 | { | |
810 | mutex_unlock(&probe_lock); | |
811 | } | |
812 | ||
813 | static int probes_seq_show(struct seq_file *m, void *v) | |
814 | { | |
c31ffb3f | 815 | struct trace_kprobe *tk = v; |
93ccae7a | 816 | int i; |
413d37d1 | 817 | |
fa6f0cc7 | 818 | seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p'); |
de7b2973 | 819 | seq_printf(m, ":%s/%s", tk->tp.call.class->system, |
687fcc4a | 820 | trace_event_name(&tk->tp.call)); |
413d37d1 | 821 | |
c31ffb3f NK |
822 | if (!tk->symbol) |
823 | seq_printf(m, " 0x%p", tk->rp.kp.addr); | |
824 | else if (tk->rp.kp.offset) | |
825 | seq_printf(m, " %s+%u", trace_kprobe_symbol(tk), | |
826 | tk->rp.kp.offset); | |
413d37d1 | 827 | else |
c31ffb3f | 828 | seq_printf(m, " %s", trace_kprobe_symbol(tk)); |
413d37d1 | 829 | |
c31ffb3f NK |
830 | for (i = 0; i < tk->tp.nr_args; i++) |
831 | seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm); | |
fa6f0cc7 | 832 | seq_putc(m, '\n'); |
93ccae7a | 833 | |
413d37d1 MH |
834 | return 0; |
835 | } | |
836 | ||
837 | static const struct seq_operations probes_seq_op = { | |
838 | .start = probes_seq_start, | |
839 | .next = probes_seq_next, | |
840 | .stop = probes_seq_stop, | |
841 | .show = probes_seq_show | |
842 | }; | |
843 | ||
844 | static int probes_open(struct inode *inode, struct file *file) | |
845 | { | |
02ca1521 MH |
846 | int ret; |
847 | ||
848 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { | |
c31ffb3f | 849 | ret = release_all_trace_kprobes(); |
02ca1521 MH |
850 | if (ret < 0) |
851 | return ret; | |
852 | } | |
413d37d1 MH |
853 | |
854 | return seq_open(file, &probes_seq_op); | |
855 | } | |
856 | ||
413d37d1 MH |
857 | static ssize_t probes_write(struct file *file, const char __user *buffer, |
858 | size_t count, loff_t *ppos) | |
859 | { | |
8ab83f56 | 860 | return traceprobe_probes_write(file, buffer, count, ppos, |
c31ffb3f | 861 | create_trace_kprobe); |
413d37d1 MH |
862 | } |
863 | ||
864 | static const struct file_operations kprobe_events_ops = { | |
865 | .owner = THIS_MODULE, | |
866 | .open = probes_open, | |
867 | .read = seq_read, | |
868 | .llseek = seq_lseek, | |
869 | .release = seq_release, | |
870 | .write = probes_write, | |
871 | }; | |
872 | ||
cd7e7bd5 MH |
873 | /* Probes profiling interfaces */ |
874 | static int probes_profile_seq_show(struct seq_file *m, void *v) | |
875 | { | |
c31ffb3f | 876 | struct trace_kprobe *tk = v; |
cd7e7bd5 | 877 | |
de7b2973 | 878 | seq_printf(m, " %-44s %15lu %15lu\n", |
687fcc4a | 879 | trace_event_name(&tk->tp.call), tk->nhit, |
c31ffb3f | 880 | tk->rp.kp.nmissed); |
cd7e7bd5 MH |
881 | |
882 | return 0; | |
883 | } | |
884 | ||
885 | static const struct seq_operations profile_seq_op = { | |
886 | .start = probes_seq_start, | |
887 | .next = probes_seq_next, | |
888 | .stop = probes_seq_stop, | |
889 | .show = probes_profile_seq_show | |
890 | }; | |
891 | ||
892 | static int profile_open(struct inode *inode, struct file *file) | |
893 | { | |
894 | return seq_open(file, &profile_seq_op); | |
895 | } | |
896 | ||
897 | static const struct file_operations kprobe_profile_ops = { | |
898 | .owner = THIS_MODULE, | |
899 | .open = profile_open, | |
900 | .read = seq_read, | |
901 | .llseek = seq_lseek, | |
902 | .release = seq_release, | |
903 | }; | |
904 | ||
413d37d1 | 905 | /* Kprobe handler */ |
3da0f180 | 906 | static nokprobe_inline void |
c31ffb3f | 907 | __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs, |
7f1d2f82 | 908 | struct trace_event_file *trace_file) |
413d37d1 | 909 | { |
93ccae7a | 910 | struct kprobe_trace_entry_head *entry; |
413d37d1 | 911 | struct ring_buffer_event *event; |
8f8ffe24 | 912 | struct ring_buffer *buffer; |
e09c8614 | 913 | int size, dsize, pc; |
413d37d1 | 914 | unsigned long irq_flags; |
2425bcb9 | 915 | struct trace_event_call *call = &tk->tp.call; |
413d37d1 | 916 | |
7f1d2f82 | 917 | WARN_ON(call != trace_file->event_call); |
41a7dd42 | 918 | |
09a5059a | 919 | if (trace_trigger_soft_disabled(trace_file)) |
13a1e4ae | 920 | return; |
b8820084 | 921 | |
413d37d1 MH |
922 | local_save_flags(irq_flags); |
923 | pc = preempt_count(); | |
924 | ||
c31ffb3f NK |
925 | dsize = __get_data_size(&tk->tp, regs); |
926 | size = sizeof(*entry) + tk->tp.size + dsize; | |
413d37d1 | 927 | |
7f1d2f82 | 928 | event = trace_event_buffer_lock_reserve(&buffer, trace_file, |
41a7dd42 MH |
929 | call->event.type, |
930 | size, irq_flags, pc); | |
413d37d1 | 931 | if (!event) |
1e12a4a7 | 932 | return; |
413d37d1 MH |
933 | |
934 | entry = ring_buffer_event_data(event); | |
c31ffb3f NK |
935 | entry->ip = (unsigned long)tk->rp.kp.addr; |
936 | store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize); | |
413d37d1 | 937 | |
7f1d2f82 | 938 | event_trigger_unlock_commit_regs(trace_file, buffer, event, |
13a1e4ae | 939 | entry, irq_flags, pc, regs); |
413d37d1 MH |
940 | } |
941 | ||
3da0f180 | 942 | static void |
c31ffb3f | 943 | kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs) |
41a7dd42 | 944 | { |
b04d52e3 | 945 | struct event_file_link *link; |
41a7dd42 | 946 | |
c31ffb3f NK |
947 | list_for_each_entry_rcu(link, &tk->tp.files, list) |
948 | __kprobe_trace_func(tk, regs, link->file); | |
41a7dd42 | 949 | } |
3da0f180 | 950 | NOKPROBE_SYMBOL(kprobe_trace_func); |
41a7dd42 | 951 | |
413d37d1 | 952 | /* Kretprobe handler */ |
3da0f180 | 953 | static nokprobe_inline void |
c31ffb3f | 954 | __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, |
41a7dd42 | 955 | struct pt_regs *regs, |
7f1d2f82 | 956 | struct trace_event_file *trace_file) |
413d37d1 | 957 | { |
93ccae7a | 958 | struct kretprobe_trace_entry_head *entry; |
413d37d1 | 959 | struct ring_buffer_event *event; |
8f8ffe24 | 960 | struct ring_buffer *buffer; |
e09c8614 | 961 | int size, pc, dsize; |
413d37d1 | 962 | unsigned long irq_flags; |
2425bcb9 | 963 | struct trace_event_call *call = &tk->tp.call; |
413d37d1 | 964 | |
7f1d2f82 | 965 | WARN_ON(call != trace_file->event_call); |
41a7dd42 | 966 | |
09a5059a | 967 | if (trace_trigger_soft_disabled(trace_file)) |
13a1e4ae | 968 | return; |
b8820084 | 969 | |
413d37d1 MH |
970 | local_save_flags(irq_flags); |
971 | pc = preempt_count(); | |
972 | ||
c31ffb3f NK |
973 | dsize = __get_data_size(&tk->tp, regs); |
974 | size = sizeof(*entry) + tk->tp.size + dsize; | |
413d37d1 | 975 | |
7f1d2f82 | 976 | event = trace_event_buffer_lock_reserve(&buffer, trace_file, |
41a7dd42 MH |
977 | call->event.type, |
978 | size, irq_flags, pc); | |
413d37d1 | 979 | if (!event) |
1e12a4a7 | 980 | return; |
413d37d1 MH |
981 | |
982 | entry = ring_buffer_event_data(event); | |
c31ffb3f | 983 | entry->func = (unsigned long)tk->rp.kp.addr; |
413d37d1 | 984 | entry->ret_ip = (unsigned long)ri->ret_addr; |
c31ffb3f | 985 | store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize); |
413d37d1 | 986 | |
7f1d2f82 | 987 | event_trigger_unlock_commit_regs(trace_file, buffer, event, |
13a1e4ae | 988 | entry, irq_flags, pc, regs); |
413d37d1 MH |
989 | } |
990 | ||
3da0f180 | 991 | static void |
c31ffb3f | 992 | kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, |
41a7dd42 MH |
993 | struct pt_regs *regs) |
994 | { | |
b04d52e3 | 995 | struct event_file_link *link; |
41a7dd42 | 996 | |
c31ffb3f NK |
997 | list_for_each_entry_rcu(link, &tk->tp.files, list) |
998 | __kretprobe_trace_func(tk, ri, regs, link->file); | |
41a7dd42 | 999 | } |
3da0f180 | 1000 | NOKPROBE_SYMBOL(kretprobe_trace_func); |
41a7dd42 | 1001 | |
413d37d1 | 1002 | /* Event entry printers */ |
b62fdd97 | 1003 | static enum print_line_t |
a9a57763 SR |
1004 | print_kprobe_event(struct trace_iterator *iter, int flags, |
1005 | struct trace_event *event) | |
413d37d1 | 1006 | { |
93ccae7a | 1007 | struct kprobe_trace_entry_head *field; |
413d37d1 | 1008 | struct trace_seq *s = &iter->seq; |
eca0d916 | 1009 | struct trace_probe *tp; |
93ccae7a | 1010 | u8 *data; |
413d37d1 MH |
1011 | int i; |
1012 | ||
93ccae7a | 1013 | field = (struct kprobe_trace_entry_head *)iter->ent; |
80decc70 | 1014 | tp = container_of(event, struct trace_probe, call.event); |
413d37d1 | 1015 | |
687fcc4a | 1016 | trace_seq_printf(s, "%s: (", trace_event_name(&tp->call)); |
6e9f23d1 | 1017 | |
413d37d1 | 1018 | if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET)) |
85224da0 | 1019 | goto out; |
413d37d1 | 1020 | |
85224da0 | 1021 | trace_seq_putc(s, ')'); |
413d37d1 | 1022 | |
93ccae7a MH |
1023 | data = (u8 *)&field[1]; |
1024 | for (i = 0; i < tp->nr_args; i++) | |
1025 | if (!tp->args[i].type->print(s, tp->args[i].name, | |
e09c8614 | 1026 | data + tp->args[i].offset, field)) |
85224da0 | 1027 | goto out; |
413d37d1 | 1028 | |
85224da0 SRRH |
1029 | trace_seq_putc(s, '\n'); |
1030 | out: | |
1031 | return trace_handle_return(s); | |
413d37d1 MH |
1032 | } |
1033 | ||
b62fdd97 | 1034 | static enum print_line_t |
a9a57763 SR |
1035 | print_kretprobe_event(struct trace_iterator *iter, int flags, |
1036 | struct trace_event *event) | |
413d37d1 | 1037 | { |
93ccae7a | 1038 | struct kretprobe_trace_entry_head *field; |
413d37d1 | 1039 | struct trace_seq *s = &iter->seq; |
eca0d916 | 1040 | struct trace_probe *tp; |
93ccae7a | 1041 | u8 *data; |
413d37d1 MH |
1042 | int i; |
1043 | ||
93ccae7a | 1044 | field = (struct kretprobe_trace_entry_head *)iter->ent; |
80decc70 | 1045 | tp = container_of(event, struct trace_probe, call.event); |
413d37d1 | 1046 | |
687fcc4a | 1047 | trace_seq_printf(s, "%s: (", trace_event_name(&tp->call)); |
6e9f23d1 | 1048 | |
413d37d1 | 1049 | if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET)) |
85224da0 | 1050 | goto out; |
413d37d1 | 1051 | |
85224da0 | 1052 | trace_seq_puts(s, " <- "); |
413d37d1 MH |
1053 | |
1054 | if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET)) | |
85224da0 | 1055 | goto out; |
413d37d1 | 1056 | |
85224da0 | 1057 | trace_seq_putc(s, ')'); |
413d37d1 | 1058 | |
93ccae7a MH |
1059 | data = (u8 *)&field[1]; |
1060 | for (i = 0; i < tp->nr_args; i++) | |
1061 | if (!tp->args[i].type->print(s, tp->args[i].name, | |
e09c8614 | 1062 | data + tp->args[i].offset, field)) |
85224da0 | 1063 | goto out; |
413d37d1 | 1064 | |
85224da0 | 1065 | trace_seq_putc(s, '\n'); |
413d37d1 | 1066 | |
85224da0 SRRH |
1067 | out: |
1068 | return trace_handle_return(s); | |
413d37d1 MH |
1069 | } |
1070 | ||
413d37d1 | 1071 | |
2425bcb9 | 1072 | static int kprobe_event_define_fields(struct trace_event_call *event_call) |
413d37d1 MH |
1073 | { |
1074 | int ret, i; | |
93ccae7a | 1075 | struct kprobe_trace_entry_head field; |
c31ffb3f | 1076 | struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data; |
413d37d1 | 1077 | |
a703d946 | 1078 | DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0); |
eca0d916 | 1079 | /* Set argument names as fields */ |
c31ffb3f NK |
1080 | for (i = 0; i < tk->tp.nr_args; i++) { |
1081 | struct probe_arg *parg = &tk->tp.args[i]; | |
1082 | ||
1083 | ret = trace_define_field(event_call, parg->type->fmttype, | |
1084 | parg->name, | |
1085 | sizeof(field) + parg->offset, | |
1086 | parg->type->size, | |
1087 | parg->type->is_signed, | |
93ccae7a MH |
1088 | FILTER_OTHER); |
1089 | if (ret) | |
1090 | return ret; | |
1091 | } | |
413d37d1 MH |
1092 | return 0; |
1093 | } | |
1094 | ||
2425bcb9 | 1095 | static int kretprobe_event_define_fields(struct trace_event_call *event_call) |
413d37d1 MH |
1096 | { |
1097 | int ret, i; | |
93ccae7a | 1098 | struct kretprobe_trace_entry_head field; |
c31ffb3f | 1099 | struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data; |
413d37d1 | 1100 | |
a703d946 MH |
1101 | DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0); |
1102 | DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0); | |
eca0d916 | 1103 | /* Set argument names as fields */ |
c31ffb3f NK |
1104 | for (i = 0; i < tk->tp.nr_args; i++) { |
1105 | struct probe_arg *parg = &tk->tp.args[i]; | |
1106 | ||
1107 | ret = trace_define_field(event_call, parg->type->fmttype, | |
1108 | parg->name, | |
1109 | sizeof(field) + parg->offset, | |
1110 | parg->type->size, | |
1111 | parg->type->is_signed, | |
93ccae7a MH |
1112 | FILTER_OTHER); |
1113 | if (ret) | |
1114 | return ret; | |
1115 | } | |
413d37d1 MH |
1116 | return 0; |
1117 | } | |
1118 | ||
07b139c8 | 1119 | #ifdef CONFIG_PERF_EVENTS |
e08d1c65 MH |
1120 | |
1121 | /* Kprobe profile handler */ | |
3da0f180 | 1122 | static void |
c31ffb3f | 1123 | kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs) |
e08d1c65 | 1124 | { |
2425bcb9 | 1125 | struct trace_event_call *call = &tk->tp.call; |
2541517c | 1126 | struct bpf_prog *prog = call->prog; |
93ccae7a | 1127 | struct kprobe_trace_entry_head *entry; |
1c024eca | 1128 | struct hlist_head *head; |
e09c8614 | 1129 | int size, __size, dsize; |
4ed7c92d | 1130 | int rctx; |
e08d1c65 | 1131 | |
2541517c AS |
1132 | if (prog && !trace_call_bpf(prog, regs)) |
1133 | return; | |
1134 | ||
288e984e ON |
1135 | head = this_cpu_ptr(call->perf_events); |
1136 | if (hlist_empty(head)) | |
1137 | return; | |
1138 | ||
c31ffb3f NK |
1139 | dsize = __get_data_size(&tk->tp, regs); |
1140 | __size = sizeof(*entry) + tk->tp.size + dsize; | |
74ebb63e MH |
1141 | size = ALIGN(__size + sizeof(u32), sizeof(u64)); |
1142 | size -= sizeof(u32); | |
ce71b9df | 1143 | |
86038c5e | 1144 | entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx); |
430ad5a6 | 1145 | if (!entry) |
1e12a4a7 | 1146 | return; |
a1a138d0 | 1147 | |
c31ffb3f | 1148 | entry->ip = (unsigned long)tk->rp.kp.addr; |
e09c8614 | 1149 | memset(&entry[1], 0, dsize); |
c31ffb3f | 1150 | store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize); |
cf6735a4 | 1151 | perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL); |
e08d1c65 | 1152 | } |
3da0f180 | 1153 | NOKPROBE_SYMBOL(kprobe_perf_func); |
e08d1c65 MH |
1154 | |
1155 | /* Kretprobe profile handler */ | |
3da0f180 | 1156 | static void |
c31ffb3f | 1157 | kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, |
2b106aab | 1158 | struct pt_regs *regs) |
e08d1c65 | 1159 | { |
2425bcb9 | 1160 | struct trace_event_call *call = &tk->tp.call; |
2541517c | 1161 | struct bpf_prog *prog = call->prog; |
93ccae7a | 1162 | struct kretprobe_trace_entry_head *entry; |
1c024eca | 1163 | struct hlist_head *head; |
e09c8614 | 1164 | int size, __size, dsize; |
4ed7c92d | 1165 | int rctx; |
e08d1c65 | 1166 | |
2541517c AS |
1167 | if (prog && !trace_call_bpf(prog, regs)) |
1168 | return; | |
1169 | ||
288e984e ON |
1170 | head = this_cpu_ptr(call->perf_events); |
1171 | if (hlist_empty(head)) | |
1172 | return; | |
1173 | ||
c31ffb3f NK |
1174 | dsize = __get_data_size(&tk->tp, regs); |
1175 | __size = sizeof(*entry) + tk->tp.size + dsize; | |
74ebb63e MH |
1176 | size = ALIGN(__size + sizeof(u32), sizeof(u64)); |
1177 | size -= sizeof(u32); | |
444a2a3b | 1178 | |
86038c5e | 1179 | entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx); |
430ad5a6 | 1180 | if (!entry) |
1e12a4a7 | 1181 | return; |
e08d1c65 | 1182 | |
c31ffb3f | 1183 | entry->func = (unsigned long)tk->rp.kp.addr; |
a1a138d0 | 1184 | entry->ret_ip = (unsigned long)ri->ret_addr; |
c31ffb3f | 1185 | store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize); |
cf6735a4 | 1186 | perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL); |
e08d1c65 | 1187 | } |
3da0f180 | 1188 | NOKPROBE_SYMBOL(kretprobe_perf_func); |
07b139c8 | 1189 | #endif /* CONFIG_PERF_EVENTS */ |
50d78056 | 1190 | |
3fe3d619 ON |
1191 | /* |
1192 | * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex. | |
1193 | * | |
1194 | * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe | |
1195 | * lockless, but we can't race with this __init function. | |
1196 | */ | |
2425bcb9 | 1197 | static int kprobe_register(struct trace_event_call *event, |
fbc1963d | 1198 | enum trace_reg type, void *data) |
2239291a | 1199 | { |
c31ffb3f | 1200 | struct trace_kprobe *tk = (struct trace_kprobe *)event->data; |
7f1d2f82 | 1201 | struct trace_event_file *file = data; |
1538f888 | 1202 | |
2239291a SR |
1203 | switch (type) { |
1204 | case TRACE_REG_REGISTER: | |
c31ffb3f | 1205 | return enable_trace_kprobe(tk, file); |
2239291a | 1206 | case TRACE_REG_UNREGISTER: |
c31ffb3f | 1207 | return disable_trace_kprobe(tk, file); |
2239291a SR |
1208 | |
1209 | #ifdef CONFIG_PERF_EVENTS | |
1210 | case TRACE_REG_PERF_REGISTER: | |
c31ffb3f | 1211 | return enable_trace_kprobe(tk, NULL); |
2239291a | 1212 | case TRACE_REG_PERF_UNREGISTER: |
c31ffb3f | 1213 | return disable_trace_kprobe(tk, NULL); |
ceec0b6f JO |
1214 | case TRACE_REG_PERF_OPEN: |
1215 | case TRACE_REG_PERF_CLOSE: | |
489c75c3 JO |
1216 | case TRACE_REG_PERF_ADD: |
1217 | case TRACE_REG_PERF_DEL: | |
ceec0b6f | 1218 | return 0; |
2239291a SR |
1219 | #endif |
1220 | } | |
1221 | return 0; | |
1222 | } | |
50d78056 | 1223 | |
3da0f180 | 1224 | static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs) |
50d78056 | 1225 | { |
c31ffb3f | 1226 | struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp); |
e08d1c65 | 1227 | |
c31ffb3f | 1228 | tk->nhit++; |
48182bd2 | 1229 | |
c31ffb3f NK |
1230 | if (tk->tp.flags & TP_FLAG_TRACE) |
1231 | kprobe_trace_func(tk, regs); | |
07b139c8 | 1232 | #ifdef CONFIG_PERF_EVENTS |
c31ffb3f NK |
1233 | if (tk->tp.flags & TP_FLAG_PROFILE) |
1234 | kprobe_perf_func(tk, regs); | |
07b139c8 | 1235 | #endif |
50d78056 MH |
1236 | return 0; /* We don't tweek kernel, so just return 0 */ |
1237 | } | |
3da0f180 | 1238 | NOKPROBE_SYMBOL(kprobe_dispatcher); |
50d78056 | 1239 | |
3da0f180 MH |
1240 | static int |
1241 | kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs) | |
50d78056 | 1242 | { |
c31ffb3f | 1243 | struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp); |
50d78056 | 1244 | |
c31ffb3f | 1245 | tk->nhit++; |
48182bd2 | 1246 | |
c31ffb3f NK |
1247 | if (tk->tp.flags & TP_FLAG_TRACE) |
1248 | kretprobe_trace_func(tk, ri, regs); | |
07b139c8 | 1249 | #ifdef CONFIG_PERF_EVENTS |
c31ffb3f NK |
1250 | if (tk->tp.flags & TP_FLAG_PROFILE) |
1251 | kretprobe_perf_func(tk, ri, regs); | |
07b139c8 | 1252 | #endif |
50d78056 MH |
1253 | return 0; /* We don't tweek kernel, so just return 0 */ |
1254 | } | |
3da0f180 | 1255 | NOKPROBE_SYMBOL(kretprobe_dispatcher); |
e08d1c65 | 1256 | |
a9a57763 SR |
1257 | static struct trace_event_functions kretprobe_funcs = { |
1258 | .trace = print_kretprobe_event | |
1259 | }; | |
1260 | ||
1261 | static struct trace_event_functions kprobe_funcs = { | |
1262 | .trace = print_kprobe_event | |
1263 | }; | |
1264 | ||
c31ffb3f | 1265 | static int register_kprobe_event(struct trace_kprobe *tk) |
413d37d1 | 1266 | { |
2425bcb9 | 1267 | struct trace_event_call *call = &tk->tp.call; |
413d37d1 MH |
1268 | int ret; |
1269 | ||
2425bcb9 | 1270 | /* Initialize trace_event_call */ |
ffb9f995 | 1271 | INIT_LIST_HEAD(&call->class->fields); |
c31ffb3f | 1272 | if (trace_kprobe_is_return(tk)) { |
80decc70 | 1273 | call->event.funcs = &kretprobe_funcs; |
2e33af02 | 1274 | call->class->define_fields = kretprobe_event_define_fields; |
413d37d1 | 1275 | } else { |
80decc70 | 1276 | call->event.funcs = &kprobe_funcs; |
2e33af02 | 1277 | call->class->define_fields = kprobe_event_define_fields; |
413d37d1 | 1278 | } |
5bf652aa | 1279 | if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) |
a342a028 | 1280 | return -ENOMEM; |
9023c930 | 1281 | ret = register_trace_event(&call->event); |
32c0edae | 1282 | if (!ret) { |
a342a028 | 1283 | kfree(call->print_fmt); |
ff50d991 | 1284 | return -ENODEV; |
a342a028 | 1285 | } |
72cbbc89 | 1286 | call->flags = TRACE_EVENT_FL_KPROBE; |
2239291a | 1287 | call->class->reg = kprobe_register; |
c31ffb3f | 1288 | call->data = tk; |
413d37d1 | 1289 | ret = trace_add_event_call(call); |
ff50d991 | 1290 | if (ret) { |
de7b2973 | 1291 | pr_info("Failed to register kprobe event: %s\n", |
687fcc4a | 1292 | trace_event_name(call)); |
a342a028 | 1293 | kfree(call->print_fmt); |
9023c930 | 1294 | unregister_trace_event(&call->event); |
ff50d991 | 1295 | } |
413d37d1 MH |
1296 | return ret; |
1297 | } | |
1298 | ||
c31ffb3f | 1299 | static int unregister_kprobe_event(struct trace_kprobe *tk) |
413d37d1 | 1300 | { |
40c32592 SRRH |
1301 | int ret; |
1302 | ||
ff50d991 | 1303 | /* tp->event is unregistered in trace_remove_event_call() */ |
c31ffb3f | 1304 | ret = trace_remove_event_call(&tk->tp.call); |
40c32592 | 1305 | if (!ret) |
c31ffb3f | 1306 | kfree(tk->tp.call.print_fmt); |
40c32592 | 1307 | return ret; |
413d37d1 MH |
1308 | } |
1309 | ||
8434dc93 | 1310 | /* Make a tracefs interface for controlling probe points */ |
413d37d1 MH |
1311 | static __init int init_kprobe_trace(void) |
1312 | { | |
1313 | struct dentry *d_tracer; | |
1314 | struct dentry *entry; | |
413d37d1 | 1315 | |
c31ffb3f | 1316 | if (register_module_notifier(&trace_kprobe_module_nb)) |
61424318 MH |
1317 | return -EINVAL; |
1318 | ||
413d37d1 | 1319 | d_tracer = tracing_init_dentry(); |
14a5ae40 | 1320 | if (IS_ERR(d_tracer)) |
413d37d1 MH |
1321 | return 0; |
1322 | ||
8434dc93 | 1323 | entry = tracefs_create_file("kprobe_events", 0644, d_tracer, |
413d37d1 MH |
1324 | NULL, &kprobe_events_ops); |
1325 | ||
cd7e7bd5 | 1326 | /* Event list interface */ |
413d37d1 | 1327 | if (!entry) |
8434dc93 | 1328 | pr_warning("Could not create tracefs " |
413d37d1 | 1329 | "'kprobe_events' entry\n"); |
cd7e7bd5 MH |
1330 | |
1331 | /* Profile interface */ | |
8434dc93 | 1332 | entry = tracefs_create_file("kprobe_profile", 0444, d_tracer, |
cd7e7bd5 MH |
1333 | NULL, &kprobe_profile_ops); |
1334 | ||
1335 | if (!entry) | |
8434dc93 | 1336 | pr_warning("Could not create tracefs " |
cd7e7bd5 | 1337 | "'kprobe_profile' entry\n"); |
413d37d1 MH |
1338 | return 0; |
1339 | } | |
1340 | fs_initcall(init_kprobe_trace); | |
1341 | ||
1342 | ||
1343 | #ifdef CONFIG_FTRACE_STARTUP_TEST | |
1344 | ||
265a5b7e SR |
1345 | /* |
1346 | * The "__used" keeps gcc from removing the function symbol | |
1347 | * from the kallsyms table. | |
1348 | */ | |
1349 | static __used int kprobe_trace_selftest_target(int a1, int a2, int a3, | |
1350 | int a4, int a5, int a6) | |
413d37d1 MH |
1351 | { |
1352 | return a1 + a2 + a3 + a4 + a5 + a6; | |
1353 | } | |
1354 | ||
7f1d2f82 | 1355 | static struct trace_event_file * |
c31ffb3f | 1356 | find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr) |
41a7dd42 | 1357 | { |
7f1d2f82 | 1358 | struct trace_event_file *file; |
41a7dd42 MH |
1359 | |
1360 | list_for_each_entry(file, &tr->events, list) | |
c31ffb3f | 1361 | if (file->event_call == &tk->tp.call) |
41a7dd42 MH |
1362 | return file; |
1363 | ||
1364 | return NULL; | |
1365 | } | |
1366 | ||
3fe3d619 | 1367 | /* |
c31ffb3f | 1368 | * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this |
3fe3d619 ON |
1369 | * stage, we can do this lockless. |
1370 | */ | |
413d37d1 MH |
1371 | static __init int kprobe_trace_self_tests_init(void) |
1372 | { | |
231e36f4 | 1373 | int ret, warn = 0; |
413d37d1 | 1374 | int (*target)(int, int, int, int, int, int); |
c31ffb3f | 1375 | struct trace_kprobe *tk; |
7f1d2f82 | 1376 | struct trace_event_file *file; |
413d37d1 | 1377 | |
748ec3a2 YY |
1378 | if (tracing_is_disabled()) |
1379 | return -ENODEV; | |
1380 | ||
413d37d1 MH |
1381 | target = kprobe_trace_selftest_target; |
1382 | ||
1383 | pr_info("Testing kprobe tracing: "); | |
1384 | ||
8ab83f56 SD |
1385 | ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target " |
1386 | "$stack $stack0 +0($stack)", | |
c31ffb3f | 1387 | create_trace_kprobe); |
231e36f4 | 1388 | if (WARN_ON_ONCE(ret)) { |
41a7dd42 | 1389 | pr_warn("error on probing function entry.\n"); |
231e36f4 MH |
1390 | warn++; |
1391 | } else { | |
1392 | /* Enable trace point */ | |
c31ffb3f NK |
1393 | tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM); |
1394 | if (WARN_ON_ONCE(tk == NULL)) { | |
41a7dd42 | 1395 | pr_warn("error on getting new probe.\n"); |
231e36f4 | 1396 | warn++; |
41a7dd42 | 1397 | } else { |
c31ffb3f | 1398 | file = find_trace_probe_file(tk, top_trace_array()); |
41a7dd42 MH |
1399 | if (WARN_ON_ONCE(file == NULL)) { |
1400 | pr_warn("error on getting probe file.\n"); | |
1401 | warn++; | |
1402 | } else | |
c31ffb3f | 1403 | enable_trace_kprobe(tk, file); |
41a7dd42 | 1404 | } |
231e36f4 | 1405 | } |
413d37d1 | 1406 | |
8ab83f56 | 1407 | ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target " |
c31ffb3f | 1408 | "$retval", create_trace_kprobe); |
231e36f4 | 1409 | if (WARN_ON_ONCE(ret)) { |
41a7dd42 | 1410 | pr_warn("error on probing function return.\n"); |
231e36f4 MH |
1411 | warn++; |
1412 | } else { | |
1413 | /* Enable trace point */ | |
c31ffb3f NK |
1414 | tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM); |
1415 | if (WARN_ON_ONCE(tk == NULL)) { | |
41a7dd42 | 1416 | pr_warn("error on getting 2nd new probe.\n"); |
231e36f4 | 1417 | warn++; |
41a7dd42 | 1418 | } else { |
c31ffb3f | 1419 | file = find_trace_probe_file(tk, top_trace_array()); |
41a7dd42 MH |
1420 | if (WARN_ON_ONCE(file == NULL)) { |
1421 | pr_warn("error on getting probe file.\n"); | |
1422 | warn++; | |
1423 | } else | |
c31ffb3f | 1424 | enable_trace_kprobe(tk, file); |
41a7dd42 | 1425 | } |
231e36f4 MH |
1426 | } |
1427 | ||
1428 | if (warn) | |
1429 | goto end; | |
413d37d1 MH |
1430 | |
1431 | ret = target(1, 2, 3, 4, 5, 6); | |
1432 | ||
02ca1521 | 1433 | /* Disable trace points before removing it */ |
c31ffb3f NK |
1434 | tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM); |
1435 | if (WARN_ON_ONCE(tk == NULL)) { | |
41a7dd42 | 1436 | pr_warn("error on getting test probe.\n"); |
02ca1521 | 1437 | warn++; |
41a7dd42 | 1438 | } else { |
c31ffb3f | 1439 | file = find_trace_probe_file(tk, top_trace_array()); |
41a7dd42 MH |
1440 | if (WARN_ON_ONCE(file == NULL)) { |
1441 | pr_warn("error on getting probe file.\n"); | |
1442 | warn++; | |
1443 | } else | |
c31ffb3f | 1444 | disable_trace_kprobe(tk, file); |
41a7dd42 | 1445 | } |
02ca1521 | 1446 | |
c31ffb3f NK |
1447 | tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM); |
1448 | if (WARN_ON_ONCE(tk == NULL)) { | |
41a7dd42 | 1449 | pr_warn("error on getting 2nd test probe.\n"); |
02ca1521 | 1450 | warn++; |
41a7dd42 | 1451 | } else { |
c31ffb3f | 1452 | file = find_trace_probe_file(tk, top_trace_array()); |
41a7dd42 MH |
1453 | if (WARN_ON_ONCE(file == NULL)) { |
1454 | pr_warn("error on getting probe file.\n"); | |
1455 | warn++; | |
1456 | } else | |
c31ffb3f | 1457 | disable_trace_kprobe(tk, file); |
41a7dd42 | 1458 | } |
02ca1521 | 1459 | |
c31ffb3f | 1460 | ret = traceprobe_command("-:testprobe", create_trace_kprobe); |
231e36f4 | 1461 | if (WARN_ON_ONCE(ret)) { |
41a7dd42 | 1462 | pr_warn("error on deleting a probe.\n"); |
231e36f4 MH |
1463 | warn++; |
1464 | } | |
1465 | ||
c31ffb3f | 1466 | ret = traceprobe_command("-:testprobe2", create_trace_kprobe); |
231e36f4 | 1467 | if (WARN_ON_ONCE(ret)) { |
41a7dd42 | 1468 | pr_warn("error on deleting a probe.\n"); |
231e36f4 MH |
1469 | warn++; |
1470 | } | |
413d37d1 | 1471 | |
231e36f4 | 1472 | end: |
c31ffb3f | 1473 | release_all_trace_kprobes(); |
231e36f4 MH |
1474 | if (warn) |
1475 | pr_cont("NG: Some tests are failed. Please check them.\n"); | |
1476 | else | |
1477 | pr_cont("OK\n"); | |
413d37d1 MH |
1478 | return 0; |
1479 | } | |
1480 | ||
1481 | late_initcall(kprobe_trace_self_tests_init); | |
1482 | ||
1483 | #endif |