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