blob: ea0db8eee570542738c66cf04be847c126b6dcb7 [file] [log] [blame]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001/*
2 * kprobe based kernel tracer
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>
22#include <linux/kprobes.h>
23#include <linux/seq_file.h>
24#include <linux/slab.h>
25#include <linux/smp.h>
26#include <linux/debugfs.h>
27#include <linux/types.h>
28#include <linux/string.h>
29#include <linux/ctype.h>
30#include <linux/ptrace.h>
Masami Hiramatsue08d1c62009-09-10 19:53:30 -040031#include <linux/perf_counter.h>
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040032
33#include "trace.h"
34#include "trace_output.h"
35
Masami Hiramatsua82378d2009-08-13 16:35:18 -040036#define MAX_TRACE_ARGS 128
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040037#define MAX_ARGSTR_LEN 63
Masami Hiramatsu42635652009-08-13 16:35:26 -040038#define MAX_EVENT_NAME_LEN 64
Masami Hiramatsuf52487e2009-09-10 19:53:53 -040039#define KPROBE_EVENT_SYSTEM "kprobes"
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040040
41/* currently, trace_kprobe only supports X86. */
42
43struct fetch_func {
44 unsigned long (*func)(struct pt_regs *, void *);
45 void *data;
46};
47
48static __kprobes unsigned long call_fetch(struct fetch_func *f,
49 struct pt_regs *regs)
50{
51 return f->func(regs, f->data);
52}
53
54/* fetch handlers */
55static __kprobes unsigned long fetch_register(struct pt_regs *regs,
56 void *offset)
57{
58 return regs_get_register(regs, (unsigned int)((unsigned long)offset));
59}
60
61static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
62 void *num)
63{
64 return regs_get_kernel_stack_nth(regs,
65 (unsigned int)((unsigned long)num));
66}
67
68static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
69{
70 unsigned long retval;
71
72 if (probe_kernel_address(addr, retval))
73 return 0;
74 return retval;
75}
76
77static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
78{
79 return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
80}
81
82static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
83 void *dummy)
84{
85 return regs_return_value(regs);
86}
87
88static __kprobes unsigned long fetch_ip(struct pt_regs *regs, void *dummy)
89{
90 return instruction_pointer(regs);
91}
92
93static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
94 void *dummy)
95{
96 return kernel_stack_pointer(regs);
97}
98
99/* Memory fetching by symbol */
100struct symbol_cache {
101 char *symbol;
102 long offset;
103 unsigned long addr;
104};
105
106static unsigned long update_symbol_cache(struct symbol_cache *sc)
107{
108 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
109 if (sc->addr)
110 sc->addr += sc->offset;
111 return sc->addr;
112}
113
114static void free_symbol_cache(struct symbol_cache *sc)
115{
116 kfree(sc->symbol);
117 kfree(sc);
118}
119
120static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
121{
122 struct symbol_cache *sc;
123
124 if (!sym || strlen(sym) == 0)
125 return NULL;
126 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
127 if (!sc)
128 return NULL;
129
130 sc->symbol = kstrdup(sym, GFP_KERNEL);
131 if (!sc->symbol) {
132 kfree(sc);
133 return NULL;
134 }
135 sc->offset = offset;
136
137 update_symbol_cache(sc);
138 return sc;
139}
140
141static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
142{
143 struct symbol_cache *sc = data;
144
145 if (sc->addr)
146 return fetch_memory(regs, (void *)sc->addr);
147 else
148 return 0;
149}
150
151/* Special indirect memory access interface */
152struct indirect_fetch_data {
153 struct fetch_func orig;
154 long offset;
155};
156
157static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
158{
159 struct indirect_fetch_data *ind = data;
160 unsigned long addr;
161
162 addr = call_fetch(&ind->orig, regs);
163 if (addr) {
164 addr += ind->offset;
165 return fetch_memory(regs, (void *)addr);
166 } else
167 return 0;
168}
169
170static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
171{
172 if (data->orig.func == fetch_indirect)
173 free_indirect_fetch_data(data->orig.data);
174 else if (data->orig.func == fetch_symbol)
175 free_symbol_cache(data->orig.data);
176 kfree(data);
177}
178
179/**
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400180 * Kprobe tracer core functions
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400181 */
182
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400183struct probe_arg {
184 struct fetch_func fetch;
185 const char *name;
186};
187
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400188struct trace_probe {
189 struct list_head list;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200190 struct kretprobe rp; /* Use rp.kp for kprobe use */
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400191 unsigned long nhit;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400192 const char *symbol; /* symbol name */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400193 struct ftrace_event_call call;
Masami Hiramatsuff50d992009-08-13 16:35:34 -0400194 struct trace_event event;
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400195 unsigned int nr_args;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400196 struct probe_arg args[];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400197};
198
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400199#define SIZEOF_TRACE_PROBE(n) \
200 (offsetof(struct trace_probe, args) + \
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400201 (sizeof(struct probe_arg) * (n)))
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400202
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400203static int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs);
204static int kretprobe_trace_func(struct kretprobe_instance *ri,
205 struct pt_regs *regs);
206
207static __kprobes int probe_is_return(struct trace_probe *tp)
208{
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200209 return tp->rp.handler != NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400210}
211
212static __kprobes const char *probe_symbol(struct trace_probe *tp)
213{
214 return tp->symbol ? tp->symbol : "unknown";
215}
216
Masami Hiramatsu30a7e072009-08-21 15:43:51 -0400217static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400218{
219 int ret = -EINVAL;
220
221 if (ff->func == fetch_argument)
222 ret = snprintf(buf, n, "a%lu", (unsigned long)ff->data);
223 else if (ff->func == fetch_register) {
224 const char *name;
225 name = regs_query_register_name((unsigned int)((long)ff->data));
226 ret = snprintf(buf, n, "%%%s", name);
227 } else if (ff->func == fetch_stack)
228 ret = snprintf(buf, n, "s%lu", (unsigned long)ff->data);
229 else if (ff->func == fetch_memory)
230 ret = snprintf(buf, n, "@0x%p", ff->data);
231 else if (ff->func == fetch_symbol) {
232 struct symbol_cache *sc = ff->data;
233 ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
234 } else if (ff->func == fetch_retvalue)
235 ret = snprintf(buf, n, "rv");
236 else if (ff->func == fetch_ip)
237 ret = snprintf(buf, n, "ra");
238 else if (ff->func == fetch_stack_address)
239 ret = snprintf(buf, n, "sa");
240 else if (ff->func == fetch_indirect) {
241 struct indirect_fetch_data *id = ff->data;
242 size_t l = 0;
243 ret = snprintf(buf, n, "%+ld(", id->offset);
244 if (ret >= n)
245 goto end;
246 l += ret;
Masami Hiramatsu30a7e072009-08-21 15:43:51 -0400247 ret = probe_arg_string(buf + l, n - l, &id->orig);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400248 if (ret < 0)
249 goto end;
250 l += ret;
251 ret = snprintf(buf + l, n - l, ")");
252 ret += l;
253 }
254end:
255 if (ret >= n)
256 return -ENOSPC;
257 return ret;
258}
259
260static int register_probe_event(struct trace_probe *tp);
261static void unregister_probe_event(struct trace_probe *tp);
262
263static DEFINE_MUTEX(probe_lock);
264static LIST_HEAD(probe_list);
265
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200266/*
267 * Allocate new trace_probe and initialize it (including kprobes).
268 */
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400269static struct trace_probe *alloc_trace_probe(const char *group,
270 const char *event,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200271 void *addr,
272 const char *symbol,
273 unsigned long offs,
274 int nargs, int is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400275{
276 struct trace_probe *tp;
277
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400278 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400279 if (!tp)
280 return ERR_PTR(-ENOMEM);
281
282 if (symbol) {
283 tp->symbol = kstrdup(symbol, GFP_KERNEL);
284 if (!tp->symbol)
285 goto error;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200286 tp->rp.kp.symbol_name = tp->symbol;
287 tp->rp.kp.offset = offs;
288 } else
289 tp->rp.kp.addr = addr;
290
Masami Hiramatsue08d1c62009-09-10 19:53:30 -0400291 /* Set handler here for checking whether this probe is return or not. */
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200292 if (is_return)
293 tp->rp.handler = kretprobe_trace_func;
294 else
295 tp->rp.kp.pre_handler = kprobe_trace_func;
296
Masami Hiramatsu42635652009-08-13 16:35:26 -0400297 if (!event)
298 goto error;
299 tp->call.name = kstrdup(event, GFP_KERNEL);
300 if (!tp->call.name)
301 goto error;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400302
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400303 if (!group)
304 goto error;
305 tp->call.system = kstrdup(group, GFP_KERNEL);
306 if (!tp->call.system)
307 goto error;
308
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400309 INIT_LIST_HEAD(&tp->list);
310 return tp;
311error:
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400312 kfree(tp->call.name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400313 kfree(tp->symbol);
314 kfree(tp);
315 return ERR_PTR(-ENOMEM);
316}
317
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400318static void free_probe_arg(struct probe_arg *arg)
319{
320 if (arg->fetch.func == fetch_symbol)
321 free_symbol_cache(arg->fetch.data);
322 else if (arg->fetch.func == fetch_indirect)
323 free_indirect_fetch_data(arg->fetch.data);
324 kfree(arg->name);
325}
326
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400327static void free_trace_probe(struct trace_probe *tp)
328{
329 int i;
330
331 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400332 free_probe_arg(&tp->args[i]);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400333
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400334 kfree(tp->call.system);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400335 kfree(tp->call.name);
336 kfree(tp->symbol);
337 kfree(tp);
338}
339
340static struct trace_probe *find_probe_event(const char *event)
341{
342 struct trace_probe *tp;
343
344 list_for_each_entry(tp, &probe_list, list)
Masami Hiramatsu42635652009-08-13 16:35:26 -0400345 if (!strcmp(tp->call.name, event))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400346 return tp;
347 return NULL;
348}
349
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400350/* Unregister a trace_probe and probe_event: call with locking probe_lock */
351static void unregister_trace_probe(struct trace_probe *tp)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400352{
353 if (probe_is_return(tp))
354 unregister_kretprobe(&tp->rp);
355 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200356 unregister_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400357 list_del(&tp->list);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400358 unregister_probe_event(tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400359}
360
361/* Register a trace_probe and probe_event */
362static int register_trace_probe(struct trace_probe *tp)
363{
364 struct trace_probe *old_tp;
365 int ret;
366
367 mutex_lock(&probe_lock);
368
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400369 /* register as an event */
370 old_tp = find_probe_event(tp->call.name);
371 if (old_tp) {
372 /* delete old event */
373 unregister_trace_probe(old_tp);
374 free_trace_probe(old_tp);
375 }
376 ret = register_probe_event(tp);
377 if (ret) {
378 pr_warning("Faild to register probe event(%d)\n", ret);
379 goto end;
380 }
381
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400382 if (probe_is_return(tp))
383 ret = register_kretprobe(&tp->rp);
384 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200385 ret = register_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400386
387 if (ret) {
388 pr_warning("Could not insert probe(%d)\n", ret);
389 if (ret == -EILSEQ) {
390 pr_warning("Probing address(0x%p) is not an "
391 "instruction boundary.\n",
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200392 tp->rp.kp.addr);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400393 ret = -EINVAL;
394 }
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400395 unregister_probe_event(tp);
396 } else
397 list_add_tail(&tp->list, &probe_list);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400398end:
399 mutex_unlock(&probe_lock);
400 return ret;
401}
402
403/* Split symbol and offset. */
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400404static int split_symbol_offset(char *symbol, unsigned long *offset)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400405{
406 char *tmp;
407 int ret;
408
409 if (!offset)
410 return -EINVAL;
411
412 tmp = strchr(symbol, '+');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400413 if (tmp) {
414 /* skip sign because strict_strtol doesn't accept '+' */
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400415 ret = strict_strtoul(tmp + 1, 0, offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400416 if (ret)
417 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400418 *tmp = '\0';
419 } else
420 *offset = 0;
421 return 0;
422}
423
424#define PARAM_MAX_ARGS 16
425#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
426
Masami Hiramatsu30a7e072009-08-21 15:43:51 -0400427static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400428{
429 int ret = 0;
430 unsigned long param;
431 long offset;
432 char *tmp;
433
434 switch (arg[0]) {
435 case 'a': /* argument */
436 ret = strict_strtoul(arg + 1, 10, &param);
437 if (ret || param > PARAM_MAX_ARGS)
438 ret = -EINVAL;
439 else {
440 ff->func = fetch_argument;
441 ff->data = (void *)param;
442 }
443 break;
444 case 'r': /* retval or retaddr */
445 if (is_return && arg[1] == 'v') {
446 ff->func = fetch_retvalue;
447 ff->data = NULL;
448 } else if (is_return && arg[1] == 'a') {
449 ff->func = fetch_ip;
450 ff->data = NULL;
451 } else
452 ret = -EINVAL;
453 break;
454 case '%': /* named register */
455 ret = regs_query_register_offset(arg + 1);
456 if (ret >= 0) {
457 ff->func = fetch_register;
458 ff->data = (void *)(unsigned long)ret;
459 ret = 0;
460 }
461 break;
462 case 's': /* stack */
463 if (arg[1] == 'a') {
464 ff->func = fetch_stack_address;
465 ff->data = NULL;
466 } else {
467 ret = strict_strtoul(arg + 1, 10, &param);
468 if (ret || param > PARAM_MAX_STACK)
469 ret = -EINVAL;
470 else {
471 ff->func = fetch_stack;
472 ff->data = (void *)param;
473 }
474 }
475 break;
476 case '@': /* memory or symbol */
477 if (isdigit(arg[1])) {
478 ret = strict_strtoul(arg + 1, 0, &param);
479 if (ret)
480 break;
481 ff->func = fetch_memory;
482 ff->data = (void *)param;
483 } else {
484 ret = split_symbol_offset(arg + 1, &offset);
485 if (ret)
486 break;
487 ff->data = alloc_symbol_cache(arg + 1,
488 offset);
489 if (ff->data)
490 ff->func = fetch_symbol;
491 else
492 ret = -EINVAL;
493 }
494 break;
495 case '+': /* indirect memory */
496 case '-':
497 tmp = strchr(arg, '(');
498 if (!tmp) {
499 ret = -EINVAL;
500 break;
501 }
502 *tmp = '\0';
503 ret = strict_strtol(arg + 1, 0, &offset);
504 if (ret)
505 break;
506 if (arg[0] == '-')
507 offset = -offset;
508 arg = tmp + 1;
509 tmp = strrchr(arg, ')');
510 if (tmp) {
511 struct indirect_fetch_data *id;
512 *tmp = '\0';
513 id = kzalloc(sizeof(struct indirect_fetch_data),
514 GFP_KERNEL);
515 if (!id)
516 return -ENOMEM;
517 id->offset = offset;
Masami Hiramatsu30a7e072009-08-21 15:43:51 -0400518 ret = parse_probe_arg(arg, &id->orig, is_return);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400519 if (ret)
520 kfree(id);
521 else {
522 ff->func = fetch_indirect;
523 ff->data = (void *)id;
524 }
525 } else
526 ret = -EINVAL;
527 break;
528 default:
529 /* TODO: support custom handler */
530 ret = -EINVAL;
531 }
532 return ret;
533}
534
535static int create_trace_probe(int argc, char **argv)
536{
537 /*
538 * Argument syntax:
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400539 * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
540 * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400541 * Fetch args:
542 * aN : fetch Nth of function argument. (N:0-)
543 * rv : fetch return value
544 * ra : fetch return address
545 * sa : fetch stack address
546 * sN : fetch Nth of stack (N:0-)
547 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
548 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
549 * %REG : fetch register REG
550 * Indirect memory fetch:
551 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400552 * Alias name of args:
553 * NAME=FETCHARG : set NAME as alias of FETCHARG.
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400554 */
555 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400556 int i, ret = 0;
557 int is_return = 0;
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400558 char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400559 unsigned long offset = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400560 void *addr = NULL;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200561 char buf[MAX_EVENT_NAME_LEN];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400562
563 if (argc < 2)
564 return -EINVAL;
565
566 if (argv[0][0] == 'p')
567 is_return = 0;
568 else if (argv[0][0] == 'r')
569 is_return = 1;
570 else
571 return -EINVAL;
572
573 if (argv[0][1] == ':') {
574 event = &argv[0][2];
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400575 if (strchr(event, '/')) {
576 group = event;
577 event = strchr(group, '/') + 1;
578 event[-1] = '\0';
579 if (strlen(group) == 0) {
580 pr_info("Group name is not specifiled\n");
581 return -EINVAL;
582 }
583 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400584 if (strlen(event) == 0) {
585 pr_info("Event name is not specifiled\n");
586 return -EINVAL;
587 }
588 }
589
590 if (isdigit(argv[1][0])) {
591 if (is_return)
592 return -EINVAL;
593 /* an address specified */
594 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
595 if (ret)
596 return ret;
597 } else {
598 /* a symbol specified */
599 symbol = argv[1];
600 /* TODO: support .init module functions */
601 ret = split_symbol_offset(symbol, &offset);
602 if (ret)
603 return ret;
604 if (offset && is_return)
605 return -EINVAL;
606 }
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400607 argc -= 2; argv += 2;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400608
609 /* setup a probe */
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400610 if (!group)
611 group = KPROBE_EVENT_SYSTEM;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400612 if (!event) {
613 /* Make a new event name */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400614 if (symbol)
615 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
616 is_return ? 'r' : 'p', symbol, offset);
617 else
618 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
619 is_return ? 'r' : 'p', addr);
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200620 event = buf;
621 }
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400622 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
623 is_return);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400624 if (IS_ERR(tp))
625 return PTR_ERR(tp);
626
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400627 /* parse arguments */
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400628 ret = 0;
629 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400630 /* Parse argument name */
631 arg = strchr(argv[i], '=');
632 if (arg)
633 *arg++ = '\0';
634 else
635 arg = argv[i];
636 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
637
638 /* Parse fetch argument */
639 if (strlen(arg) > MAX_ARGSTR_LEN) {
640 pr_info("Argument%d(%s) is too long.\n", i, arg);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400641 ret = -ENOSPC;
642 goto error;
643 }
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400644 ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400645 if (ret)
646 goto error;
647 }
648 tp->nr_args = i;
649
650 ret = register_trace_probe(tp);
651 if (ret)
652 goto error;
653 return 0;
654
655error:
656 free_trace_probe(tp);
657 return ret;
658}
659
660static void cleanup_all_probes(void)
661{
662 struct trace_probe *tp;
663
664 mutex_lock(&probe_lock);
665 /* TODO: Use batch unregistration */
666 while (!list_empty(&probe_list)) {
667 tp = list_entry(probe_list.next, struct trace_probe, list);
668 unregister_trace_probe(tp);
669 free_trace_probe(tp);
670 }
671 mutex_unlock(&probe_lock);
672}
673
674
675/* Probes listing interfaces */
676static void *probes_seq_start(struct seq_file *m, loff_t *pos)
677{
678 mutex_lock(&probe_lock);
679 return seq_list_start(&probe_list, *pos);
680}
681
682static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
683{
684 return seq_list_next(v, &probe_list, pos);
685}
686
687static void probes_seq_stop(struct seq_file *m, void *v)
688{
689 mutex_unlock(&probe_lock);
690}
691
692static int probes_seq_show(struct seq_file *m, void *v)
693{
694 struct trace_probe *tp = v;
695 int i, ret;
696 char buf[MAX_ARGSTR_LEN + 1];
697
698 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
Masami Hiramatsu42635652009-08-13 16:35:26 -0400699 seq_printf(m, ":%s", tp->call.name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400700
701 if (tp->symbol)
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200702 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400703 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200704 seq_printf(m, " 0x%p", tp->rp.kp.addr);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400705
706 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400707 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400708 if (ret < 0) {
709 pr_warning("Argument%d decoding error(%d).\n", i, ret);
710 return ret;
711 }
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400712 seq_printf(m, " %s=%s", tp->args[i].name, buf);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400713 }
714 seq_printf(m, "\n");
715 return 0;
716}
717
718static const struct seq_operations probes_seq_op = {
719 .start = probes_seq_start,
720 .next = probes_seq_next,
721 .stop = probes_seq_stop,
722 .show = probes_seq_show
723};
724
725static int probes_open(struct inode *inode, struct file *file)
726{
727 if ((file->f_mode & FMODE_WRITE) &&
728 (file->f_flags & O_TRUNC))
729 cleanup_all_probes();
730
731 return seq_open(file, &probes_seq_op);
732}
733
734static int command_trace_probe(const char *buf)
735{
736 char **argv;
737 int argc = 0, ret = 0;
738
739 argv = argv_split(GFP_KERNEL, buf, &argc);
740 if (!argv)
741 return -ENOMEM;
742
743 if (argc)
744 ret = create_trace_probe(argc, argv);
745
746 argv_free(argv);
747 return ret;
748}
749
750#define WRITE_BUFSIZE 128
751
752static ssize_t probes_write(struct file *file, const char __user *buffer,
753 size_t count, loff_t *ppos)
754{
755 char *kbuf, *tmp;
756 int ret;
757 size_t done;
758 size_t size;
759
760 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
761 if (!kbuf)
762 return -ENOMEM;
763
764 ret = done = 0;
765 while (done < count) {
766 size = count - done;
767 if (size >= WRITE_BUFSIZE)
768 size = WRITE_BUFSIZE - 1;
769 if (copy_from_user(kbuf, buffer + done, size)) {
770 ret = -EFAULT;
771 goto out;
772 }
773 kbuf[size] = '\0';
774 tmp = strchr(kbuf, '\n');
775 if (tmp) {
776 *tmp = '\0';
777 size = tmp - kbuf + 1;
778 } else if (done + size < count) {
779 pr_warning("Line length is too long: "
780 "Should be less than %d.", WRITE_BUFSIZE);
781 ret = -EINVAL;
782 goto out;
783 }
784 done += size;
785 /* Remove comments */
786 tmp = strchr(kbuf, '#');
787 if (tmp)
788 *tmp = '\0';
789
790 ret = command_trace_probe(kbuf);
791 if (ret)
792 goto out;
793 }
794 ret = done;
795out:
796 kfree(kbuf);
797 return ret;
798}
799
800static const struct file_operations kprobe_events_ops = {
801 .owner = THIS_MODULE,
802 .open = probes_open,
803 .read = seq_read,
804 .llseek = seq_lseek,
805 .release = seq_release,
806 .write = probes_write,
807};
808
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400809/* Probes profiling interfaces */
810static int probes_profile_seq_show(struct seq_file *m, void *v)
811{
812 struct trace_probe *tp = v;
813
814 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200815 tp->rp.kp.nmissed);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400816
817 return 0;
818}
819
820static const struct seq_operations profile_seq_op = {
821 .start = probes_seq_start,
822 .next = probes_seq_next,
823 .stop = probes_seq_stop,
824 .show = probes_profile_seq_show
825};
826
827static int profile_open(struct inode *inode, struct file *file)
828{
829 return seq_open(file, &profile_seq_op);
830}
831
832static const struct file_operations kprobe_profile_ops = {
833 .owner = THIS_MODULE,
834 .open = profile_open,
835 .read = seq_read,
836 .llseek = seq_lseek,
837 .release = seq_release,
838};
839
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400840/* Kprobe handler */
841static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
842{
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200843 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400844 struct kprobe_trace_entry *entry;
845 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200846 struct ring_buffer *buffer;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400847 int size, i, pc;
848 unsigned long irq_flags;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400849 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400850
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400851 tp->nhit++;
852
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400853 local_save_flags(irq_flags);
854 pc = preempt_count();
855
856 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
857
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200858 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400859 irq_flags, pc);
860 if (!event)
861 return 0;
862
863 entry = ring_buffer_event_data(event);
864 entry->nargs = tp->nr_args;
865 entry->ip = (unsigned long)kp->addr;
866 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400867 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400868
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200869 if (!filter_current_check_discard(buffer, call, entry, event))
870 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400871 return 0;
872}
873
874/* Kretprobe handler */
875static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
876 struct pt_regs *regs)
877{
878 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
879 struct kretprobe_trace_entry *entry;
880 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200881 struct ring_buffer *buffer;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400882 int size, i, pc;
883 unsigned long irq_flags;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400884 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400885
886 local_save_flags(irq_flags);
887 pc = preempt_count();
888
889 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
890
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200891 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400892 irq_flags, pc);
893 if (!event)
894 return 0;
895
896 entry = ring_buffer_event_data(event);
897 entry->nargs = tp->nr_args;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200898 entry->func = (unsigned long)tp->rp.kp.addr;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400899 entry->ret_ip = (unsigned long)ri->ret_addr;
900 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400901 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400902
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200903 if (!filter_current_check_discard(buffer, call, entry, event))
904 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400905
906 return 0;
907}
908
909/* Event entry printers */
910enum print_line_t
911print_kprobe_event(struct trace_iterator *iter, int flags)
912{
913 struct kprobe_trace_entry *field;
914 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400915 struct trace_event *event;
916 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400917 int i;
918
Masami Hiramatsuff50d992009-08-13 16:35:34 -0400919 field = (struct kprobe_trace_entry *)iter->ent;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400920 event = ftrace_find_event(field->ent.type);
921 tp = container_of(event, struct trace_probe, event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400922
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -0400923 if (!trace_seq_printf(s, "%s: (", tp->call.name))
924 goto partial;
925
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400926 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
927 goto partial;
928
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -0400929 if (!trace_seq_puts(s, ")"))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400930 goto partial;
931
932 for (i = 0; i < field->nargs; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400933 if (!trace_seq_printf(s, " %s=%lx",
934 tp->args[i].name, field->args[i]))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400935 goto partial;
936
937 if (!trace_seq_puts(s, "\n"))
938 goto partial;
939
940 return TRACE_TYPE_HANDLED;
941partial:
942 return TRACE_TYPE_PARTIAL_LINE;
943}
944
945enum print_line_t
946print_kretprobe_event(struct trace_iterator *iter, int flags)
947{
948 struct kretprobe_trace_entry *field;
949 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400950 struct trace_event *event;
951 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400952 int i;
953
Masami Hiramatsuff50d992009-08-13 16:35:34 -0400954 field = (struct kretprobe_trace_entry *)iter->ent;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400955 event = ftrace_find_event(field->ent.type);
956 tp = container_of(event, struct trace_probe, event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400957
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -0400958 if (!trace_seq_printf(s, "%s: (", tp->call.name))
959 goto partial;
960
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400961 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
962 goto partial;
963
964 if (!trace_seq_puts(s, " <- "))
965 goto partial;
966
967 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
968 goto partial;
969
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -0400970 if (!trace_seq_puts(s, ")"))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400971 goto partial;
972
973 for (i = 0; i < field->nargs; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400974 if (!trace_seq_printf(s, " %s=%lx",
975 tp->args[i].name, field->args[i]))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400976 goto partial;
977
978 if (!trace_seq_puts(s, "\n"))
979 goto partial;
980
981 return TRACE_TYPE_HANDLED;
982partial:
983 return TRACE_TYPE_PARTIAL_LINE;
984}
985
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400986static int probe_event_enable(struct ftrace_event_call *call)
987{
988 struct trace_probe *tp = (struct trace_probe *)call->data;
989
Masami Hiramatsue08d1c62009-09-10 19:53:30 -0400990 if (probe_is_return(tp)) {
991 tp->rp.handler = kretprobe_trace_func;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400992 return enable_kretprobe(&tp->rp);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -0400993 } else {
994 tp->rp.kp.pre_handler = kprobe_trace_func;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200995 return enable_kprobe(&tp->rp.kp);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -0400996 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400997}
998
999static void probe_event_disable(struct ftrace_event_call *call)
1000{
1001 struct trace_probe *tp = (struct trace_probe *)call->data;
1002
1003 if (probe_is_return(tp))
1004 disable_kretprobe(&tp->rp);
1005 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +02001006 disable_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001007}
1008
1009static int probe_event_raw_init(struct ftrace_event_call *event_call)
1010{
1011 INIT_LIST_HEAD(&event_call->fields);
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001012
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001013 return 0;
1014}
1015
1016#undef DEFINE_FIELD
1017#define DEFINE_FIELD(type, item, name, is_signed) \
1018 do { \
1019 ret = trace_define_field(event_call, #type, name, \
1020 offsetof(typeof(field), item), \
1021 sizeof(field.item), is_signed, \
1022 FILTER_OTHER); \
1023 if (ret) \
1024 return ret; \
1025 } while (0)
1026
1027static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1028{
1029 int ret, i;
1030 struct kprobe_trace_entry field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001031 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1032
1033 ret = trace_define_common_fields(event_call);
1034 if (!ret)
1035 return ret;
1036
1037 DEFINE_FIELD(unsigned long, ip, "ip", 0);
1038 DEFINE_FIELD(int, nargs, "nargs", 1);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001039 /* Set argument names as fields */
1040 for (i = 0; i < tp->nr_args; i++)
1041 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001042 return 0;
1043}
1044
1045static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1046{
1047 int ret, i;
1048 struct kretprobe_trace_entry field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001049 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1050
1051 ret = trace_define_common_fields(event_call);
1052 if (!ret)
1053 return ret;
1054
1055 DEFINE_FIELD(unsigned long, func, "func", 0);
1056 DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
1057 DEFINE_FIELD(int, nargs, "nargs", 1);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001058 /* Set argument names as fields */
1059 for (i = 0; i < tp->nr_args; i++)
1060 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001061 return 0;
1062}
1063
1064static int __probe_event_show_format(struct trace_seq *s,
1065 struct trace_probe *tp, const char *fmt,
1066 const char *arg)
1067{
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001068 int i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001069
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001070 /* Show format */
1071 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1072 return 0;
1073
1074 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001075 if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001076 return 0;
1077
1078 if (!trace_seq_printf(s, "\", %s", arg))
1079 return 0;
1080
1081 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001082 if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001083 return 0;
1084
1085 return trace_seq_puts(s, "\n");
1086}
1087
1088#undef SHOW_FIELD
1089#define SHOW_FIELD(type, item, name) \
1090 do { \
1091 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
Masami Hiramatsu38a47492009-08-21 15:43:43 -04001092 "offset:%u;\tsize:%u;\n", name, \
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001093 (unsigned int)offsetof(typeof(field), item),\
1094 (unsigned int)sizeof(type)); \
1095 if (!ret) \
1096 return 0; \
1097 } while (0)
1098
1099static int kprobe_event_show_format(struct ftrace_event_call *call,
1100 struct trace_seq *s)
1101{
1102 struct kprobe_trace_entry field __attribute__((unused));
1103 int ret, i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001104 struct trace_probe *tp = (struct trace_probe *)call->data;
1105
1106 SHOW_FIELD(unsigned long, ip, "ip");
1107 SHOW_FIELD(int, nargs, "nargs");
1108
1109 /* Show fields */
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001110 for (i = 0; i < tp->nr_args; i++)
1111 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001112 trace_seq_puts(s, "\n");
1113
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001114 return __probe_event_show_format(s, tp, "(%lx)", "REC->ip");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001115}
1116
1117static int kretprobe_event_show_format(struct ftrace_event_call *call,
1118 struct trace_seq *s)
1119{
1120 struct kretprobe_trace_entry field __attribute__((unused));
1121 int ret, i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001122 struct trace_probe *tp = (struct trace_probe *)call->data;
1123
1124 SHOW_FIELD(unsigned long, func, "func");
1125 SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
1126 SHOW_FIELD(int, nargs, "nargs");
1127
1128 /* Show fields */
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001129 for (i = 0; i < tp->nr_args; i++)
1130 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001131 trace_seq_puts(s, "\n");
1132
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001133 return __probe_event_show_format(s, tp, "(%lx <- %lx)",
1134 "REC->func, REC->ret_ip");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001135}
1136
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001137#ifdef CONFIG_EVENT_PROFILE
1138
1139/* Kprobe profile handler */
1140static __kprobes int kprobe_profile_func(struct kprobe *kp,
1141 struct pt_regs *regs)
1142{
1143 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1144 struct ftrace_event_call *call = &tp->call;
1145 struct kprobe_trace_entry *entry;
1146 int size, i, pc;
1147 unsigned long irq_flags;
1148
1149 local_save_flags(irq_flags);
1150 pc = preempt_count();
1151
1152 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1153
1154 do {
1155 char raw_data[size];
1156 struct trace_entry *ent;
1157
1158 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1159 entry = (struct kprobe_trace_entry *)raw_data;
1160 ent = &entry->ent;
1161
1162 tracing_generic_entry_update(ent, irq_flags, pc);
1163 ent->type = call->id;
1164 entry->nargs = tp->nr_args;
1165 entry->ip = (unsigned long)kp->addr;
1166 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001167 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001168 perf_tpcounter_event(call->id, entry->ip, 1, entry, size);
1169 } while (0);
1170 return 0;
1171}
1172
1173/* Kretprobe profile handler */
1174static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
1175 struct pt_regs *regs)
1176{
1177 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1178 struct ftrace_event_call *call = &tp->call;
1179 struct kretprobe_trace_entry *entry;
1180 int size, i, pc;
1181 unsigned long irq_flags;
1182
1183 local_save_flags(irq_flags);
1184 pc = preempt_count();
1185
1186 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1187
1188 do {
1189 char raw_data[size];
1190 struct trace_entry *ent;
1191
1192 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1193 entry = (struct kretprobe_trace_entry *)raw_data;
1194 ent = &entry->ent;
1195
1196 tracing_generic_entry_update(ent, irq_flags, pc);
1197 ent->type = call->id;
1198 entry->nargs = tp->nr_args;
1199 entry->func = (unsigned long)tp->rp.kp.addr;
1200 entry->ret_ip = (unsigned long)ri->ret_addr;
1201 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001202 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001203 perf_tpcounter_event(call->id, entry->ret_ip, 1, entry, size);
1204 } while (0);
1205 return 0;
1206}
1207
1208static int probe_profile_enable(struct ftrace_event_call *call)
1209{
1210 struct trace_probe *tp = (struct trace_probe *)call->data;
1211
1212 if (atomic_inc_return(&call->profile_count))
1213 return 0;
1214
1215 if (probe_is_return(tp)) {
1216 tp->rp.handler = kretprobe_profile_func;
1217 return enable_kretprobe(&tp->rp);
1218 } else {
1219 tp->rp.kp.pre_handler = kprobe_profile_func;
1220 return enable_kprobe(&tp->rp.kp);
1221 }
1222}
1223
1224static void probe_profile_disable(struct ftrace_event_call *call)
1225{
1226 if (atomic_add_negative(-1, &call->profile_count))
1227 probe_event_disable(call);
1228}
1229
1230#endif /* CONFIG_EVENT_PROFILE */
1231
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001232static int register_probe_event(struct trace_probe *tp)
1233{
1234 struct ftrace_event_call *call = &tp->call;
1235 int ret;
1236
1237 /* Initialize ftrace_event_call */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001238 if (probe_is_return(tp)) {
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001239 tp->event.trace = print_kretprobe_event;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001240 call->raw_init = probe_event_raw_init;
1241 call->show_format = kretprobe_event_show_format;
1242 call->define_fields = kretprobe_event_define_fields;
1243 } else {
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001244 tp->event.trace = print_kprobe_event;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001245 call->raw_init = probe_event_raw_init;
1246 call->show_format = kprobe_event_show_format;
1247 call->define_fields = kprobe_event_define_fields;
1248 }
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001249 call->event = &tp->event;
1250 call->id = register_ftrace_event(&tp->event);
1251 if (!call->id)
1252 return -ENODEV;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001253 call->enabled = 1;
1254 call->regfunc = probe_event_enable;
1255 call->unregfunc = probe_event_disable;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001256
1257#ifdef CONFIG_EVENT_PROFILE
1258 atomic_set(&call->profile_count, -1);
1259 call->profile_enable = probe_profile_enable;
1260 call->profile_disable = probe_profile_disable;
1261#endif
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001262 call->data = tp;
1263 ret = trace_add_event_call(call);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001264 if (ret) {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001265 pr_info("Failed to register kprobe event: %s\n", call->name);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001266 unregister_ftrace_event(&tp->event);
1267 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001268 return ret;
1269}
1270
1271static void unregister_probe_event(struct trace_probe *tp)
1272{
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001273 /* tp->event is unregistered in trace_remove_event_call() */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001274 trace_remove_event_call(&tp->call);
1275}
1276
1277/* Make a debugfs interface for controling probe points */
1278static __init int init_kprobe_trace(void)
1279{
1280 struct dentry *d_tracer;
1281 struct dentry *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001282
1283 d_tracer = tracing_init_dentry();
1284 if (!d_tracer)
1285 return 0;
1286
1287 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1288 NULL, &kprobe_events_ops);
1289
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001290 /* Event list interface */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001291 if (!entry)
1292 pr_warning("Could not create debugfs "
1293 "'kprobe_events' entry\n");
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001294
1295 /* Profile interface */
1296 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1297 NULL, &kprobe_profile_ops);
1298
1299 if (!entry)
1300 pr_warning("Could not create debugfs "
1301 "'kprobe_profile' entry\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001302 return 0;
1303}
1304fs_initcall(init_kprobe_trace);
1305
1306
1307#ifdef CONFIG_FTRACE_STARTUP_TEST
1308
1309static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1310 int a4, int a5, int a6)
1311{
1312 return a1 + a2 + a3 + a4 + a5 + a6;
1313}
1314
1315static __init int kprobe_trace_self_tests_init(void)
1316{
1317 int ret;
1318 int (*target)(int, int, int, int, int, int);
1319
1320 target = kprobe_trace_selftest_target;
1321
1322 pr_info("Testing kprobe tracing: ");
1323
1324 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1325 "a1 a2 a3 a4 a5 a6");
1326 if (WARN_ON_ONCE(ret))
1327 pr_warning("error enabling function entry\n");
1328
1329 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1330 "ra rv");
1331 if (WARN_ON_ONCE(ret))
1332 pr_warning("error enabling function return\n");
1333
1334 ret = target(1, 2, 3, 4, 5, 6);
1335
1336 cleanup_all_probes();
1337
1338 pr_cont("OK\n");
1339 return 0;
1340}
1341
1342late_initcall(kprobe_trace_self_tests_init);
1343
1344#endif