]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/trace/trace_kprobe.c
perf, trace: Optimize tracepoints by using per-tracepoint-per-cpu hlist to track...
[net-next-2.6.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>
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>
d7a4b414 31#include <linux/perf_event.h>
93ccae7a
MH
32#include <linux/stringify.h>
33#include <asm/bitsperlong.h>
413d37d1
MH
34
35#include "trace.h"
36#include "trace_output.h"
37
a82378d8 38#define MAX_TRACE_ARGS 128
413d37d1 39#define MAX_ARGSTR_LEN 63
4263565d 40#define MAX_EVENT_NAME_LEN 64
f52487e9 41#define KPROBE_EVENT_SYSTEM "kprobes"
413d37d1 42
a703d946 43/* Reserved field names */
e93f4d85 44#define FIELD_STRING_IP "__probe_ip"
e93f4d85
MH
45#define FIELD_STRING_RETIP "__probe_ret_ip"
46#define FIELD_STRING_FUNC "__probe_func"
a703d946
MH
47
48const char *reserved_field_names[] = {
49 "common_type",
50 "common_flags",
51 "common_preempt_count",
52 "common_pid",
53 "common_tgid",
54 "common_lock_depth",
55 FIELD_STRING_IP,
a703d946
MH
56 FIELD_STRING_RETIP,
57 FIELD_STRING_FUNC,
58};
59
93ccae7a
MH
60/* Printing function type */
61typedef int (*print_type_func_t)(struct trace_seq *, const char *, void *);
62#define PRINT_TYPE_FUNC_NAME(type) print_type_##type
63#define PRINT_TYPE_FMT_NAME(type) print_type_format_##type
64
65/* Printing in basic type function template */
66#define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt, cast) \
67static __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \
68 const char *name, void *data)\
69{ \
70 return trace_seq_printf(s, " %s=" fmt, name, (cast)*(type *)data);\
71} \
72static const char PRINT_TYPE_FMT_NAME(type)[] = fmt;
73
74DEFINE_BASIC_PRINT_TYPE_FUNC(u8, "%x", unsigned int)
75DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "%x", unsigned int)
76DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "%lx", unsigned long)
77DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "%llx", unsigned long long)
78DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d", int)
79DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d", int)
80DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%ld", long)
81DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%lld", long long)
82
83/* Data fetch function type */
84typedef void (*fetch_func_t)(struct pt_regs *, void *, void *);
85
86struct fetch_param {
87 fetch_func_t fn;
413d37d1
MH
88 void *data;
89};
90
93ccae7a
MH
91static __kprobes void call_fetch(struct fetch_param *fprm,
92 struct pt_regs *regs, void *dest)
413d37d1 93{
93ccae7a 94 return fprm->fn(regs, fprm->data, dest);
413d37d1
MH
95}
96
93ccae7a
MH
97#define FETCH_FUNC_NAME(kind, type) fetch_##kind##_##type
98/*
99 * Define macro for basic types - we don't need to define s* types, because
100 * we have to care only about bitwidth at recording time.
101 */
102#define DEFINE_BASIC_FETCH_FUNCS(kind) \
103DEFINE_FETCH_##kind(u8) \
104DEFINE_FETCH_##kind(u16) \
105DEFINE_FETCH_##kind(u32) \
106DEFINE_FETCH_##kind(u64)
107
108#define CHECK_BASIC_FETCH_FUNCS(kind, fn) \
109 ((FETCH_FUNC_NAME(kind, u8) == fn) || \
110 (FETCH_FUNC_NAME(kind, u16) == fn) || \
111 (FETCH_FUNC_NAME(kind, u32) == fn) || \
112 (FETCH_FUNC_NAME(kind, u64) == fn))
113
114/* Data fetch function templates */
115#define DEFINE_FETCH_reg(type) \
116static __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \
117 void *offset, void *dest) \
118{ \
119 *(type *)dest = (type)regs_get_register(regs, \
120 (unsigned int)((unsigned long)offset)); \
413d37d1 121}
93ccae7a
MH
122DEFINE_BASIC_FETCH_FUNCS(reg)
123
124#define DEFINE_FETCH_stack(type) \
125static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
126 void *offset, void *dest) \
127{ \
128 *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
129 (unsigned int)((unsigned long)offset)); \
413d37d1 130}
93ccae7a 131DEFINE_BASIC_FETCH_FUNCS(stack)
413d37d1 132
93ccae7a
MH
133#define DEFINE_FETCH_retval(type) \
134static __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,\
135 void *dummy, void *dest) \
136{ \
137 *(type *)dest = (type)regs_return_value(regs); \
413d37d1 138}
93ccae7a
MH
139DEFINE_BASIC_FETCH_FUNCS(retval)
140
141#define DEFINE_FETCH_memory(type) \
142static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\
143 void *addr, void *dest) \
144{ \
145 type retval; \
146 if (probe_kernel_address(addr, retval)) \
147 *(type *)dest = 0; \
148 else \
149 *(type *)dest = retval; \
413d37d1 150}
93ccae7a 151DEFINE_BASIC_FETCH_FUNCS(memory)
413d37d1
MH
152
153/* Memory fetching by symbol */
154struct symbol_cache {
155 char *symbol;
156 long offset;
157 unsigned long addr;
158};
159
160static unsigned long update_symbol_cache(struct symbol_cache *sc)
161{
162 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
163 if (sc->addr)
164 sc->addr += sc->offset;
165 return sc->addr;
166}
167
168static void free_symbol_cache(struct symbol_cache *sc)
169{
170 kfree(sc->symbol);
171 kfree(sc);
172}
173
174static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
175{
176 struct symbol_cache *sc;
177
178 if (!sym || strlen(sym) == 0)
179 return NULL;
180 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
181 if (!sc)
182 return NULL;
183
184 sc->symbol = kstrdup(sym, GFP_KERNEL);
185 if (!sc->symbol) {
186 kfree(sc);
187 return NULL;
188 }
189 sc->offset = offset;
190
191 update_symbol_cache(sc);
192 return sc;
193}
194
93ccae7a
MH
195#define DEFINE_FETCH_symbol(type) \
196static __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,\
197 void *data, void *dest) \
198{ \
199 struct symbol_cache *sc = data; \
200 if (sc->addr) \
201 fetch_memory_##type(regs, (void *)sc->addr, dest); \
202 else \
203 *(type *)dest = 0; \
413d37d1 204}
93ccae7a 205DEFINE_BASIC_FETCH_FUNCS(symbol)
413d37d1 206
93ccae7a
MH
207/* Dereference memory access function */
208struct deref_fetch_param {
209 struct fetch_param orig;
413d37d1
MH
210 long offset;
211};
212
93ccae7a
MH
213#define DEFINE_FETCH_deref(type) \
214static __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,\
215 void *data, void *dest) \
216{ \
217 struct deref_fetch_param *dprm = data; \
218 unsigned long addr; \
219 call_fetch(&dprm->orig, regs, &addr); \
220 if (addr) { \
221 addr += dprm->offset; \
222 fetch_memory_##type(regs, (void *)addr, dest); \
223 } else \
224 *(type *)dest = 0; \
413d37d1 225}
93ccae7a 226DEFINE_BASIC_FETCH_FUNCS(deref)
413d37d1 227
93ccae7a 228static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
413d37d1 229{
93ccae7a
MH
230 if (CHECK_BASIC_FETCH_FUNCS(deref, data->orig.fn))
231 free_deref_fetch_param(data->orig.data);
232 else if (CHECK_BASIC_FETCH_FUNCS(symbol, data->orig.fn))
413d37d1
MH
233 free_symbol_cache(data->orig.data);
234 kfree(data);
235}
236
93ccae7a
MH
237/* Default (unsigned long) fetch type */
238#define __DEFAULT_FETCH_TYPE(t) u##t
239#define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
240#define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
241#define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
242
243#define ASSIGN_FETCH_FUNC(kind, type) \
244 .kind = FETCH_FUNC_NAME(kind, type)
245
246#define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \
247 {.name = #ptype, \
248 .size = sizeof(ftype), \
249 .is_signed = sign, \
250 .print = PRINT_TYPE_FUNC_NAME(ptype), \
251 .fmt = PRINT_TYPE_FMT_NAME(ptype), \
252ASSIGN_FETCH_FUNC(reg, ftype), \
253ASSIGN_FETCH_FUNC(stack, ftype), \
254ASSIGN_FETCH_FUNC(retval, ftype), \
255ASSIGN_FETCH_FUNC(memory, ftype), \
256ASSIGN_FETCH_FUNC(symbol, ftype), \
257ASSIGN_FETCH_FUNC(deref, ftype), \
258 }
259
260/* Fetch type information table */
261static const struct fetch_type {
262 const char *name; /* Name of type */
263 size_t size; /* Byte size of type */
264 int is_signed; /* Signed flag */
265 print_type_func_t print; /* Print functions */
266 const char *fmt; /* Fromat string */
267 /* Fetch functions */
268 fetch_func_t reg;
269 fetch_func_t stack;
270 fetch_func_t retval;
271 fetch_func_t memory;
272 fetch_func_t symbol;
273 fetch_func_t deref;
274} fetch_type_table[] = {
275 ASSIGN_FETCH_TYPE(u8, u8, 0),
276 ASSIGN_FETCH_TYPE(u16, u16, 0),
277 ASSIGN_FETCH_TYPE(u32, u32, 0),
278 ASSIGN_FETCH_TYPE(u64, u64, 0),
279 ASSIGN_FETCH_TYPE(s8, u8, 1),
280 ASSIGN_FETCH_TYPE(s16, u16, 1),
281 ASSIGN_FETCH_TYPE(s32, u32, 1),
282 ASSIGN_FETCH_TYPE(s64, u64, 1),
283};
284
285static const struct fetch_type *find_fetch_type(const char *type)
286{
287 int i;
288
289 if (!type)
290 type = DEFAULT_FETCH_TYPE_STR;
291
292 for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++)
293 if (strcmp(type, fetch_type_table[i].name) == 0)
294 return &fetch_type_table[i];
295 return NULL;
296}
297
298/* Special function : only accept unsigned long */
299static __kprobes void fetch_stack_address(struct pt_regs *regs,
300 void *dummy, void *dest)
301{
302 *(unsigned long *)dest = kernel_stack_pointer(regs);
303}
304
413d37d1 305/**
77b44d1b 306 * Kprobe event core functions
413d37d1
MH
307 */
308
eca0d916 309struct probe_arg {
93ccae7a
MH
310 struct fetch_param fetch;
311 unsigned int offset; /* Offset from argument entry */
312 const char *name; /* Name of this argument */
313 const char *comm; /* Command of this argument */
314 const struct fetch_type *type; /* Type of this argument */
eca0d916
MH
315};
316
50d78056
MH
317/* Flags for trace_probe */
318#define TP_FLAG_TRACE 1
319#define TP_FLAG_PROFILE 2
320
413d37d1
MH
321struct trace_probe {
322 struct list_head list;
4a846b44 323 struct kretprobe rp; /* Use rp.kp for kprobe use */
cd7e7bd5 324 unsigned long nhit;
50d78056 325 unsigned int flags; /* For TP_FLAG_* */
413d37d1 326 const char *symbol; /* symbol name */
413d37d1 327 struct ftrace_event_call call;
ff50d991 328 struct trace_event event;
93ccae7a 329 ssize_t size; /* trace entry size */
a82378d8 330 unsigned int nr_args;
eca0d916 331 struct probe_arg args[];
413d37d1
MH
332};
333
a82378d8
MH
334#define SIZEOF_TRACE_PROBE(n) \
335 (offsetof(struct trace_probe, args) + \
eca0d916 336 (sizeof(struct probe_arg) * (n)))
a82378d8 337
93ccae7a 338
413d37d1
MH
339static __kprobes int probe_is_return(struct trace_probe *tp)
340{
4a846b44 341 return tp->rp.handler != NULL;
413d37d1
MH
342}
343
344static __kprobes const char *probe_symbol(struct trace_probe *tp)
345{
346 return tp->symbol ? tp->symbol : "unknown";
347}
348
413d37d1
MH
349static int register_probe_event(struct trace_probe *tp);
350static void unregister_probe_event(struct trace_probe *tp);
351
352static DEFINE_MUTEX(probe_lock);
353static LIST_HEAD(probe_list);
354
50d78056
MH
355static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
356static int kretprobe_dispatcher(struct kretprobe_instance *ri,
357 struct pt_regs *regs);
358
6f3cf440
MH
359/* Check the name is good for event/group */
360static int check_event_name(const char *name)
361{
362 if (!isalpha(*name) && *name != '_')
363 return 0;
364 while (*++name != '\0') {
365 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
366 return 0;
367 }
368 return 1;
369}
370
4a846b44
MH
371/*
372 * Allocate new trace_probe and initialize it (including kprobes).
373 */
f52487e9
MH
374static struct trace_probe *alloc_trace_probe(const char *group,
375 const char *event,
4a846b44
MH
376 void *addr,
377 const char *symbol,
378 unsigned long offs,
379 int nargs, int is_return)
413d37d1
MH
380{
381 struct trace_probe *tp;
6f3cf440 382 int ret = -ENOMEM;
413d37d1 383
a82378d8 384 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
413d37d1 385 if (!tp)
6f3cf440 386 return ERR_PTR(ret);
413d37d1
MH
387
388 if (symbol) {
389 tp->symbol = kstrdup(symbol, GFP_KERNEL);
390 if (!tp->symbol)
391 goto error;
4a846b44
MH
392 tp->rp.kp.symbol_name = tp->symbol;
393 tp->rp.kp.offset = offs;
394 } else
395 tp->rp.kp.addr = addr;
396
397 if (is_return)
50d78056 398 tp->rp.handler = kretprobe_dispatcher;
4a846b44 399 else
50d78056 400 tp->rp.kp.pre_handler = kprobe_dispatcher;
4a846b44 401
6f3cf440
MH
402 if (!event || !check_event_name(event)) {
403 ret = -EINVAL;
4263565d 404 goto error;
6f3cf440
MH
405 }
406
4263565d
MH
407 tp->call.name = kstrdup(event, GFP_KERNEL);
408 if (!tp->call.name)
409 goto error;
413d37d1 410
6f3cf440
MH
411 if (!group || !check_event_name(group)) {
412 ret = -EINVAL;
f52487e9 413 goto error;
6f3cf440
MH
414 }
415
f52487e9
MH
416 tp->call.system = kstrdup(group, GFP_KERNEL);
417 if (!tp->call.system)
418 goto error;
419
413d37d1
MH
420 INIT_LIST_HEAD(&tp->list);
421 return tp;
422error:
f52487e9 423 kfree(tp->call.name);
413d37d1
MH
424 kfree(tp->symbol);
425 kfree(tp);
6f3cf440 426 return ERR_PTR(ret);
413d37d1
MH
427}
428
eca0d916
MH
429static void free_probe_arg(struct probe_arg *arg)
430{
93ccae7a
MH
431 if (CHECK_BASIC_FETCH_FUNCS(deref, arg->fetch.fn))
432 free_deref_fetch_param(arg->fetch.data);
433 else if (CHECK_BASIC_FETCH_FUNCS(symbol, arg->fetch.fn))
eca0d916 434 free_symbol_cache(arg->fetch.data);
eca0d916 435 kfree(arg->name);
93ccae7a 436 kfree(arg->comm);
eca0d916
MH
437}
438
413d37d1
MH
439static void free_trace_probe(struct trace_probe *tp)
440{
441 int i;
442
443 for (i = 0; i < tp->nr_args; i++)
eca0d916 444 free_probe_arg(&tp->args[i]);
413d37d1 445
f52487e9 446 kfree(tp->call.system);
413d37d1
MH
447 kfree(tp->call.name);
448 kfree(tp->symbol);
449 kfree(tp);
450}
451
dd004c47
MH
452static struct trace_probe *find_probe_event(const char *event,
453 const char *group)
413d37d1
MH
454{
455 struct trace_probe *tp;
456
457 list_for_each_entry(tp, &probe_list, list)
dd004c47
MH
458 if (strcmp(tp->call.name, event) == 0 &&
459 strcmp(tp->call.system, group) == 0)
413d37d1
MH
460 return tp;
461 return NULL;
462}
463
2d5e067e
MH
464/* Unregister a trace_probe and probe_event: call with locking probe_lock */
465static void unregister_trace_probe(struct trace_probe *tp)
413d37d1
MH
466{
467 if (probe_is_return(tp))
468 unregister_kretprobe(&tp->rp);
469 else
4a846b44 470 unregister_kprobe(&tp->rp.kp);
413d37d1 471 list_del(&tp->list);
2d5e067e 472 unregister_probe_event(tp);
413d37d1
MH
473}
474
475/* Register a trace_probe and probe_event */
476static int register_trace_probe(struct trace_probe *tp)
477{
478 struct trace_probe *old_tp;
479 int ret;
480
481 mutex_lock(&probe_lock);
482
2d5e067e 483 /* register as an event */
dd004c47 484 old_tp = find_probe_event(tp->call.name, tp->call.system);
2d5e067e
MH
485 if (old_tp) {
486 /* delete old event */
487 unregister_trace_probe(old_tp);
488 free_trace_probe(old_tp);
489 }
490 ret = register_probe_event(tp);
491 if (ret) {
492 pr_warning("Faild to register probe event(%d)\n", ret);
493 goto end;
494 }
495
5a0d9050 496 tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
413d37d1
MH
497 if (probe_is_return(tp))
498 ret = register_kretprobe(&tp->rp);
499 else
4a846b44 500 ret = register_kprobe(&tp->rp.kp);
413d37d1
MH
501
502 if (ret) {
503 pr_warning("Could not insert probe(%d)\n", ret);
504 if (ret == -EILSEQ) {
505 pr_warning("Probing address(0x%p) is not an "
506 "instruction boundary.\n",
4a846b44 507 tp->rp.kp.addr);
413d37d1
MH
508 ret = -EINVAL;
509 }
2d5e067e
MH
510 unregister_probe_event(tp);
511 } else
512 list_add_tail(&tp->list, &probe_list);
413d37d1
MH
513end:
514 mutex_unlock(&probe_lock);
515 return ret;
516}
517
518/* Split symbol and offset. */
2fba0c88 519static int split_symbol_offset(char *symbol, unsigned long *offset)
413d37d1
MH
520{
521 char *tmp;
522 int ret;
523
524 if (!offset)
525 return -EINVAL;
526
527 tmp = strchr(symbol, '+');
413d37d1
MH
528 if (tmp) {
529 /* skip sign because strict_strtol doesn't accept '+' */
2fba0c88 530 ret = strict_strtoul(tmp + 1, 0, offset);
413d37d1
MH
531 if (ret)
532 return ret;
413d37d1
MH
533 *tmp = '\0';
534 } else
535 *offset = 0;
536 return 0;
537}
538
539#define PARAM_MAX_ARGS 16
540#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
541
93ccae7a
MH
542static int parse_probe_vars(char *arg, const struct fetch_type *t,
543 struct fetch_param *f, int is_return)
413d37d1
MH
544{
545 int ret = 0;
546 unsigned long param;
413d37d1 547
2e06ff63 548 if (strcmp(arg, "retval") == 0) {
93ccae7a
MH
549 if (is_return)
550 f->fn = t->retval;
551 else
413d37d1 552 ret = -EINVAL;
2e06ff63
MH
553 } else if (strncmp(arg, "stack", 5) == 0) {
554 if (arg[5] == '\0') {
93ccae7a
MH
555 if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
556 f->fn = fetch_stack_address;
557 else
558 ret = -EINVAL;
2e06ff63
MH
559 } else if (isdigit(arg[5])) {
560 ret = strict_strtoul(arg + 5, 10, &param);
413d37d1
MH
561 if (ret || param > PARAM_MAX_STACK)
562 ret = -EINVAL;
563 else {
93ccae7a
MH
564 f->fn = t->stack;
565 f->data = (void *)param;
413d37d1 566 }
2e06ff63
MH
567 } else
568 ret = -EINVAL;
2e06ff63 569 } else
405b2651 570 ret = -EINVAL;
405b2651
MH
571 return ret;
572}
573
ba8665d7 574/* Recursive argument parser */
93ccae7a
MH
575static int __parse_probe_arg(char *arg, const struct fetch_type *t,
576 struct fetch_param *f, int is_return)
405b2651
MH
577{
578 int ret = 0;
579 unsigned long param;
580 long offset;
581 char *tmp;
582
583 switch (arg[0]) {
584 case '$':
93ccae7a 585 ret = parse_probe_vars(arg + 1, t, f, is_return);
405b2651
MH
586 break;
587 case '%': /* named register */
588 ret = regs_query_register_offset(arg + 1);
589 if (ret >= 0) {
93ccae7a
MH
590 f->fn = t->reg;
591 f->data = (void *)(unsigned long)ret;
405b2651
MH
592 ret = 0;
593 }
594 break;
413d37d1
MH
595 case '@': /* memory or symbol */
596 if (isdigit(arg[1])) {
597 ret = strict_strtoul(arg + 1, 0, &param);
598 if (ret)
599 break;
93ccae7a
MH
600 f->fn = t->memory;
601 f->data = (void *)param;
413d37d1
MH
602 } else {
603 ret = split_symbol_offset(arg + 1, &offset);
604 if (ret)
605 break;
93ccae7a
MH
606 f->data = alloc_symbol_cache(arg + 1, offset);
607 if (f->data)
608 f->fn = t->symbol;
413d37d1
MH
609 }
610 break;
93ccae7a 611 case '+': /* deref memory */
413d37d1
MH
612 case '-':
613 tmp = strchr(arg, '(');
93ccae7a 614 if (!tmp)
413d37d1 615 break;
413d37d1
MH
616 *tmp = '\0';
617 ret = strict_strtol(arg + 1, 0, &offset);
618 if (ret)
619 break;
620 if (arg[0] == '-')
621 offset = -offset;
622 arg = tmp + 1;
623 tmp = strrchr(arg, ')');
624 if (tmp) {
93ccae7a
MH
625 struct deref_fetch_param *dprm;
626 const struct fetch_type *t2 = find_fetch_type(NULL);
413d37d1 627 *tmp = '\0';
93ccae7a
MH
628 dprm = kzalloc(sizeof(struct deref_fetch_param),
629 GFP_KERNEL);
630 if (!dprm)
413d37d1 631 return -ENOMEM;
93ccae7a
MH
632 dprm->offset = offset;
633 ret = __parse_probe_arg(arg, t2, &dprm->orig,
634 is_return);
413d37d1 635 if (ret)
93ccae7a 636 kfree(dprm);
413d37d1 637 else {
93ccae7a
MH
638 f->fn = t->deref;
639 f->data = (void *)dprm;
413d37d1 640 }
93ccae7a 641 }
413d37d1 642 break;
413d37d1 643 }
93ccae7a
MH
644 if (!ret && !f->fn)
645 ret = -EINVAL;
413d37d1
MH
646 return ret;
647}
648
ba8665d7 649/* String length checking wrapper */
93ccae7a
MH
650static int parse_probe_arg(char *arg, struct trace_probe *tp,
651 struct probe_arg *parg, int is_return)
ba8665d7 652{
93ccae7a
MH
653 const char *t;
654
ba8665d7
MH
655 if (strlen(arg) > MAX_ARGSTR_LEN) {
656 pr_info("Argument is too long.: %s\n", arg);
657 return -ENOSPC;
658 }
93ccae7a
MH
659 parg->comm = kstrdup(arg, GFP_KERNEL);
660 if (!parg->comm) {
661 pr_info("Failed to allocate memory for command '%s'.\n", arg);
662 return -ENOMEM;
663 }
664 t = strchr(parg->comm, ':');
665 if (t) {
666 arg[t - parg->comm] = '\0';
667 t++;
668 }
669 parg->type = find_fetch_type(t);
670 if (!parg->type) {
671 pr_info("Unsupported type: %s\n", t);
672 return -EINVAL;
673 }
674 parg->offset = tp->size;
675 tp->size += parg->type->size;
676 return __parse_probe_arg(arg, parg->type, &parg->fetch, is_return);
ba8665d7
MH
677}
678
a703d946
MH
679/* Return 1 if name is reserved or already used by another argument */
680static int conflict_field_name(const char *name,
681 struct probe_arg *args, int narg)
682{
683 int i;
684 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
685 if (strcmp(reserved_field_names[i], name) == 0)
686 return 1;
687 for (i = 0; i < narg; i++)
688 if (strcmp(args[i].name, name) == 0)
689 return 1;
690 return 0;
691}
692
413d37d1
MH
693static int create_trace_probe(int argc, char **argv)
694{
695 /*
696 * Argument syntax:
f52487e9
MH
697 * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
698 * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
413d37d1 699 * Fetch args:
2e06ff63
MH
700 * $retval : fetch return value
701 * $stack : fetch stack address
702 * $stackN : fetch Nth of stack (N:0-)
413d37d1
MH
703 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
704 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
705 * %REG : fetch register REG
93ccae7a 706 * Dereferencing memory fetch:
413d37d1 707 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
eca0d916
MH
708 * Alias name of args:
709 * NAME=FETCHARG : set NAME as alias of FETCHARG.
93ccae7a
MH
710 * Type of args:
711 * FETCHARG:TYPE : use TYPE instead of unsigned long.
413d37d1
MH
712 */
713 struct trace_probe *tp;
413d37d1 714 int i, ret = 0;
a7c312be 715 int is_return = 0, is_delete = 0;
93ccae7a
MH
716 char *symbol = NULL, *event = NULL, *group = NULL;
717 char *arg, *tmp;
2fba0c88 718 unsigned long offset = 0;
413d37d1 719 void *addr = NULL;
4a846b44 720 char buf[MAX_EVENT_NAME_LEN];
413d37d1 721
a7c312be 722 /* argc must be >= 1 */
413d37d1
MH
723 if (argv[0][0] == 'p')
724 is_return = 0;
725 else if (argv[0][0] == 'r')
726 is_return = 1;
a7c312be
MH
727 else if (argv[0][0] == '-')
728 is_delete = 1;
e63cc239 729 else {
a7c312be
MH
730 pr_info("Probe definition must be started with 'p', 'r' or"
731 " '-'.\n");
413d37d1 732 return -EINVAL;
e63cc239 733 }
413d37d1
MH
734
735 if (argv[0][1] == ':') {
736 event = &argv[0][2];
f52487e9
MH
737 if (strchr(event, '/')) {
738 group = event;
739 event = strchr(group, '/') + 1;
740 event[-1] = '\0';
741 if (strlen(group) == 0) {
a5efd925 742 pr_info("Group name is not specified\n");
f52487e9
MH
743 return -EINVAL;
744 }
745 }
413d37d1 746 if (strlen(event) == 0) {
a5efd925 747 pr_info("Event name is not specified\n");
413d37d1
MH
748 return -EINVAL;
749 }
750 }
a7c312be
MH
751 if (!group)
752 group = KPROBE_EVENT_SYSTEM;
413d37d1 753
a7c312be
MH
754 if (is_delete) {
755 if (!event) {
756 pr_info("Delete command needs an event name.\n");
757 return -EINVAL;
758 }
759 tp = find_probe_event(event, group);
760 if (!tp) {
761 pr_info("Event %s/%s doesn't exist.\n", group, event);
762 return -ENOENT;
763 }
764 /* delete an event */
765 unregister_trace_probe(tp);
766 free_trace_probe(tp);
767 return 0;
768 }
769
770 if (argc < 2) {
771 pr_info("Probe point is not specified.\n");
772 return -EINVAL;
773 }
413d37d1 774 if (isdigit(argv[1][0])) {
e63cc239
MH
775 if (is_return) {
776 pr_info("Return probe point must be a symbol.\n");
413d37d1 777 return -EINVAL;
e63cc239 778 }
413d37d1 779 /* an address specified */
a9bb18f3 780 ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
e63cc239
MH
781 if (ret) {
782 pr_info("Failed to parse address.\n");
413d37d1 783 return ret;
e63cc239 784 }
413d37d1
MH
785 } else {
786 /* a symbol specified */
787 symbol = argv[1];
788 /* TODO: support .init module functions */
789 ret = split_symbol_offset(symbol, &offset);
e63cc239
MH
790 if (ret) {
791 pr_info("Failed to parse symbol.\n");
413d37d1 792 return ret;
e63cc239
MH
793 }
794 if (offset && is_return) {
795 pr_info("Return probe must be used without offset.\n");
413d37d1 796 return -EINVAL;
e63cc239 797 }
413d37d1 798 }
a82378d8 799 argc -= 2; argv += 2;
413d37d1
MH
800
801 /* setup a probe */
4263565d
MH
802 if (!event) {
803 /* Make a new event name */
4263565d 804 if (symbol)
6f3cf440 805 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
4263565d
MH
806 is_return ? 'r' : 'p', symbol, offset);
807 else
6f3cf440 808 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
4263565d 809 is_return ? 'r' : 'p', addr);
4a846b44
MH
810 event = buf;
811 }
f52487e9
MH
812 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
813 is_return);
e63cc239
MH
814 if (IS_ERR(tp)) {
815 pr_info("Failed to allocate trace_probe.(%d)\n",
816 (int)PTR_ERR(tp));
413d37d1 817 return PTR_ERR(tp);
e63cc239 818 }
413d37d1 819
413d37d1 820 /* parse arguments */
a82378d8
MH
821 ret = 0;
822 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
eca0d916
MH
823 /* Parse argument name */
824 arg = strchr(argv[i], '=');
825 if (arg)
826 *arg++ = '\0';
827 else
828 arg = argv[i];
a703d946 829
eca0d916 830 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
ba8665d7
MH
831 if (!tp->args[i].name) {
832 pr_info("Failed to allocate argument%d name '%s'.\n",
833 i, argv[i]);
834 ret = -ENOMEM;
413d37d1
MH
835 goto error;
836 }
93ccae7a
MH
837 tmp = strchr(tp->args[i].name, ':');
838 if (tmp)
839 *tmp = '_'; /* convert : to _ */
840
841 if (conflict_field_name(tp->args[i].name, tp->args, i)) {
842 pr_info("Argument%d name '%s' conflicts with "
843 "another field.\n", i, argv[i]);
844 ret = -EINVAL;
845 goto error;
846 }
ba8665d7
MH
847
848 /* Parse fetch argument */
93ccae7a 849 ret = parse_probe_arg(arg, tp, &tp->args[i], is_return);
e63cc239
MH
850 if (ret) {
851 pr_info("Parse error at argument%d. (%d)\n", i, ret);
abab9d37 852 kfree(tp->args[i].name);
413d37d1 853 goto error;
e63cc239 854 }
abab9d37
LJ
855
856 tp->nr_args++;
413d37d1 857 }
413d37d1
MH
858
859 ret = register_trace_probe(tp);
860 if (ret)
861 goto error;
862 return 0;
863
864error:
865 free_trace_probe(tp);
866 return ret;
867}
868
869static void cleanup_all_probes(void)
870{
871 struct trace_probe *tp;
872
873 mutex_lock(&probe_lock);
874 /* TODO: Use batch unregistration */
875 while (!list_empty(&probe_list)) {
876 tp = list_entry(probe_list.next, struct trace_probe, list);
877 unregister_trace_probe(tp);
878 free_trace_probe(tp);
879 }
880 mutex_unlock(&probe_lock);
881}
882
883
884/* Probes listing interfaces */
885static void *probes_seq_start(struct seq_file *m, loff_t *pos)
886{
887 mutex_lock(&probe_lock);
888 return seq_list_start(&probe_list, *pos);
889}
890
891static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
892{
893 return seq_list_next(v, &probe_list, pos);
894}
895
896static void probes_seq_stop(struct seq_file *m, void *v)
897{
898 mutex_unlock(&probe_lock);
899}
900
901static int probes_seq_show(struct seq_file *m, void *v)
902{
903 struct trace_probe *tp = v;
93ccae7a 904 int i;
413d37d1
MH
905
906 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
3d9b2e1d 907 seq_printf(m, ":%s/%s", tp->call.system, tp->call.name);
413d37d1 908
52a11f35
LJ
909 if (!tp->symbol)
910 seq_printf(m, " 0x%p", tp->rp.kp.addr);
911 else if (tp->rp.kp.offset)
4a846b44 912 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
413d37d1 913 else
52a11f35 914 seq_printf(m, " %s", probe_symbol(tp));
413d37d1 915
93ccae7a
MH
916 for (i = 0; i < tp->nr_args; i++)
917 seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
413d37d1 918 seq_printf(m, "\n");
93ccae7a 919
413d37d1
MH
920 return 0;
921}
922
923static const struct seq_operations probes_seq_op = {
924 .start = probes_seq_start,
925 .next = probes_seq_next,
926 .stop = probes_seq_stop,
927 .show = probes_seq_show
928};
929
930static int probes_open(struct inode *inode, struct file *file)
931{
932 if ((file->f_mode & FMODE_WRITE) &&
933 (file->f_flags & O_TRUNC))
934 cleanup_all_probes();
935
936 return seq_open(file, &probes_seq_op);
937}
938
939static int command_trace_probe(const char *buf)
940{
941 char **argv;
942 int argc = 0, ret = 0;
943
944 argv = argv_split(GFP_KERNEL, buf, &argc);
945 if (!argv)
946 return -ENOMEM;
947
948 if (argc)
949 ret = create_trace_probe(argc, argv);
950
951 argv_free(argv);
952 return ret;
953}
954
955#define WRITE_BUFSIZE 128
956
957static ssize_t probes_write(struct file *file, const char __user *buffer,
958 size_t count, loff_t *ppos)
959{
960 char *kbuf, *tmp;
961 int ret;
962 size_t done;
963 size_t size;
964
965 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
966 if (!kbuf)
967 return -ENOMEM;
968
969 ret = done = 0;
970 while (done < count) {
971 size = count - done;
972 if (size >= WRITE_BUFSIZE)
973 size = WRITE_BUFSIZE - 1;
974 if (copy_from_user(kbuf, buffer + done, size)) {
975 ret = -EFAULT;
976 goto out;
977 }
978 kbuf[size] = '\0';
979 tmp = strchr(kbuf, '\n');
980 if (tmp) {
981 *tmp = '\0';
982 size = tmp - kbuf + 1;
983 } else if (done + size < count) {
984 pr_warning("Line length is too long: "
985 "Should be less than %d.", WRITE_BUFSIZE);
986 ret = -EINVAL;
987 goto out;
988 }
989 done += size;
990 /* Remove comments */
991 tmp = strchr(kbuf, '#');
992 if (tmp)
993 *tmp = '\0';
994
995 ret = command_trace_probe(kbuf);
996 if (ret)
997 goto out;
998 }
999 ret = done;
1000out:
1001 kfree(kbuf);
1002 return ret;
1003}
1004
1005static const struct file_operations kprobe_events_ops = {
1006 .owner = THIS_MODULE,
1007 .open = probes_open,
1008 .read = seq_read,
1009 .llseek = seq_lseek,
1010 .release = seq_release,
1011 .write = probes_write,
1012};
1013
cd7e7bd5
MH
1014/* Probes profiling interfaces */
1015static int probes_profile_seq_show(struct seq_file *m, void *v)
1016{
1017 struct trace_probe *tp = v;
1018
1019 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
4a846b44 1020 tp->rp.kp.nmissed);
cd7e7bd5
MH
1021
1022 return 0;
1023}
1024
1025static const struct seq_operations profile_seq_op = {
1026 .start = probes_seq_start,
1027 .next = probes_seq_next,
1028 .stop = probes_seq_stop,
1029 .show = probes_profile_seq_show
1030};
1031
1032static int profile_open(struct inode *inode, struct file *file)
1033{
1034 return seq_open(file, &profile_seq_op);
1035}
1036
1037static const struct file_operations kprobe_profile_ops = {
1038 .owner = THIS_MODULE,
1039 .open = profile_open,
1040 .read = seq_read,
1041 .llseek = seq_lseek,
1042 .release = seq_release,
1043};
1044
413d37d1 1045/* Kprobe handler */
1e12a4a7 1046static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
413d37d1 1047{
4a846b44 1048 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
93ccae7a 1049 struct kprobe_trace_entry_head *entry;
413d37d1 1050 struct ring_buffer_event *event;
8f8ffe24 1051 struct ring_buffer *buffer;
93ccae7a 1052 u8 *data;
413d37d1
MH
1053 int size, i, pc;
1054 unsigned long irq_flags;
4263565d 1055 struct ftrace_event_call *call = &tp->call;
413d37d1 1056
cd7e7bd5
MH
1057 tp->nhit++;
1058
413d37d1
MH
1059 local_save_flags(irq_flags);
1060 pc = preempt_count();
1061
93ccae7a 1062 size = sizeof(*entry) + tp->size;
413d37d1 1063
8f8ffe24 1064 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
413d37d1
MH
1065 irq_flags, pc);
1066 if (!event)
1e12a4a7 1067 return;
413d37d1
MH
1068
1069 entry = ring_buffer_event_data(event);
413d37d1 1070 entry->ip = (unsigned long)kp->addr;
93ccae7a 1071 data = (u8 *)&entry[1];
413d37d1 1072 for (i = 0; i < tp->nr_args; i++)
93ccae7a 1073 call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
413d37d1 1074
8f8ffe24
FW
1075 if (!filter_current_check_discard(buffer, call, entry, event))
1076 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
413d37d1
MH
1077}
1078
1079/* Kretprobe handler */
1e12a4a7 1080static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
413d37d1
MH
1081 struct pt_regs *regs)
1082{
1083 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
93ccae7a 1084 struct kretprobe_trace_entry_head *entry;
413d37d1 1085 struct ring_buffer_event *event;
8f8ffe24 1086 struct ring_buffer *buffer;
93ccae7a 1087 u8 *data;
413d37d1
MH
1088 int size, i, pc;
1089 unsigned long irq_flags;
4263565d 1090 struct ftrace_event_call *call = &tp->call;
413d37d1
MH
1091
1092 local_save_flags(irq_flags);
1093 pc = preempt_count();
1094
93ccae7a 1095 size = sizeof(*entry) + tp->size;
413d37d1 1096
8f8ffe24 1097 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
413d37d1
MH
1098 irq_flags, pc);
1099 if (!event)
1e12a4a7 1100 return;
413d37d1
MH
1101
1102 entry = ring_buffer_event_data(event);
4a846b44 1103 entry->func = (unsigned long)tp->rp.kp.addr;
413d37d1 1104 entry->ret_ip = (unsigned long)ri->ret_addr;
93ccae7a 1105 data = (u8 *)&entry[1];
413d37d1 1106 for (i = 0; i < tp->nr_args; i++)
93ccae7a 1107 call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
413d37d1 1108
8f8ffe24
FW
1109 if (!filter_current_check_discard(buffer, call, entry, event))
1110 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
413d37d1
MH
1111}
1112
1113/* Event entry printers */
1114enum print_line_t
1115print_kprobe_event(struct trace_iterator *iter, int flags)
1116{
93ccae7a 1117 struct kprobe_trace_entry_head *field;
413d37d1 1118 struct trace_seq *s = &iter->seq;
eca0d916
MH
1119 struct trace_event *event;
1120 struct trace_probe *tp;
93ccae7a 1121 u8 *data;
413d37d1
MH
1122 int i;
1123
93ccae7a 1124 field = (struct kprobe_trace_entry_head *)iter->ent;
eca0d916
MH
1125 event = ftrace_find_event(field->ent.type);
1126 tp = container_of(event, struct trace_probe, event);
413d37d1 1127
6e9f23d1
MH
1128 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1129 goto partial;
1130
413d37d1
MH
1131 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1132 goto partial;
1133
6e9f23d1 1134 if (!trace_seq_puts(s, ")"))
413d37d1
MH
1135 goto partial;
1136
93ccae7a
MH
1137 data = (u8 *)&field[1];
1138 for (i = 0; i < tp->nr_args; i++)
1139 if (!tp->args[i].type->print(s, tp->args[i].name,
1140 data + tp->args[i].offset))
413d37d1
MH
1141 goto partial;
1142
1143 if (!trace_seq_puts(s, "\n"))
1144 goto partial;
1145
1146 return TRACE_TYPE_HANDLED;
1147partial:
1148 return TRACE_TYPE_PARTIAL_LINE;
1149}
1150
1151enum print_line_t
1152print_kretprobe_event(struct trace_iterator *iter, int flags)
1153{
93ccae7a 1154 struct kretprobe_trace_entry_head *field;
413d37d1 1155 struct trace_seq *s = &iter->seq;
eca0d916
MH
1156 struct trace_event *event;
1157 struct trace_probe *tp;
93ccae7a 1158 u8 *data;
413d37d1
MH
1159 int i;
1160
93ccae7a 1161 field = (struct kretprobe_trace_entry_head *)iter->ent;
eca0d916
MH
1162 event = ftrace_find_event(field->ent.type);
1163 tp = container_of(event, struct trace_probe, event);
413d37d1 1164
6e9f23d1
MH
1165 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1166 goto partial;
1167
413d37d1
MH
1168 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1169 goto partial;
1170
1171 if (!trace_seq_puts(s, " <- "))
1172 goto partial;
1173
1174 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1175 goto partial;
1176
6e9f23d1 1177 if (!trace_seq_puts(s, ")"))
413d37d1
MH
1178 goto partial;
1179
93ccae7a
MH
1180 data = (u8 *)&field[1];
1181 for (i = 0; i < tp->nr_args; i++)
1182 if (!tp->args[i].type->print(s, tp->args[i].name,
1183 data + tp->args[i].offset))
413d37d1
MH
1184 goto partial;
1185
1186 if (!trace_seq_puts(s, "\n"))
1187 goto partial;
1188
1189 return TRACE_TYPE_HANDLED;
1190partial:
1191 return TRACE_TYPE_PARTIAL_LINE;
1192}
1193
413d37d1
MH
1194static int probe_event_enable(struct ftrace_event_call *call)
1195{
1196 struct trace_probe *tp = (struct trace_probe *)call->data;
1197
50d78056
MH
1198 tp->flags |= TP_FLAG_TRACE;
1199 if (probe_is_return(tp))
413d37d1 1200 return enable_kretprobe(&tp->rp);
50d78056 1201 else
4a846b44 1202 return enable_kprobe(&tp->rp.kp);
413d37d1
MH
1203}
1204
1205static void probe_event_disable(struct ftrace_event_call *call)
1206{
1207 struct trace_probe *tp = (struct trace_probe *)call->data;
1208
50d78056
MH
1209 tp->flags &= ~TP_FLAG_TRACE;
1210 if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
1211 if (probe_is_return(tp))
1212 disable_kretprobe(&tp->rp);
1213 else
1214 disable_kprobe(&tp->rp.kp);
1215 }
413d37d1
MH
1216}
1217
1218static int probe_event_raw_init(struct ftrace_event_call *event_call)
1219{
1220 INIT_LIST_HEAD(&event_call->fields);
8f8ffe24 1221
413d37d1
MH
1222 return 0;
1223}
1224
1225#undef DEFINE_FIELD
1226#define DEFINE_FIELD(type, item, name, is_signed) \
1227 do { \
1228 ret = trace_define_field(event_call, #type, name, \
1229 offsetof(typeof(field), item), \
1230 sizeof(field.item), is_signed, \
1231 FILTER_OTHER); \
1232 if (ret) \
1233 return ret; \
1234 } while (0)
1235
1236static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1237{
1238 int ret, i;
93ccae7a 1239 struct kprobe_trace_entry_head field;
413d37d1
MH
1240 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1241
a703d946 1242 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
eca0d916 1243 /* Set argument names as fields */
93ccae7a
MH
1244 for (i = 0; i < tp->nr_args; i++) {
1245 ret = trace_define_field(event_call, tp->args[i].type->name,
1246 tp->args[i].name,
1247 sizeof(field) + tp->args[i].offset,
1248 tp->args[i].type->size,
1249 tp->args[i].type->is_signed,
1250 FILTER_OTHER);
1251 if (ret)
1252 return ret;
1253 }
413d37d1
MH
1254 return 0;
1255}
1256
1257static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1258{
1259 int ret, i;
93ccae7a 1260 struct kretprobe_trace_entry_head field;
413d37d1
MH
1261 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1262
a703d946
MH
1263 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1264 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
eca0d916 1265 /* Set argument names as fields */
93ccae7a
MH
1266 for (i = 0; i < tp->nr_args; i++) {
1267 ret = trace_define_field(event_call, tp->args[i].type->name,
1268 tp->args[i].name,
1269 sizeof(field) + tp->args[i].offset,
1270 tp->args[i].type->size,
1271 tp->args[i].type->is_signed,
1272 FILTER_OTHER);
1273 if (ret)
1274 return ret;
1275 }
413d37d1
MH
1276 return 0;
1277}
1278
a342a028
LJ
1279static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
1280{
1281 int i;
1282 int pos = 0;
1283
1284 const char *fmt, *arg;
1285
1286 if (!probe_is_return(tp)) {
1287 fmt = "(%lx)";
1288 arg = "REC->" FIELD_STRING_IP;
1289 } else {
1290 fmt = "(%lx <- %lx)";
1291 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1292 }
1293
1294 /* When len=0, we just calculate the needed length */
1295#define LEN_OR_ZERO (len ? len - pos : 0)
1296
1297 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
1298
1299 for (i = 0; i < tp->nr_args; i++) {
93ccae7a
MH
1300 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
1301 tp->args[i].name, tp->args[i].type->fmt);
a342a028
LJ
1302 }
1303
1304 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
1305
1306 for (i = 0; i < tp->nr_args; i++) {
1307 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
1308 tp->args[i].name);
1309 }
1310
1311#undef LEN_OR_ZERO
1312
1313 /* return the length of print_fmt */
1314 return pos;
1315}
1316
1317static int set_print_fmt(struct trace_probe *tp)
1318{
1319 int len;
1320 char *print_fmt;
1321
1322 /* First: called with 0 length to calculate the needed length */
1323 len = __set_print_fmt(tp, NULL, 0);
1324 print_fmt = kmalloc(len + 1, GFP_KERNEL);
1325 if (!print_fmt)
1326 return -ENOMEM;
1327
1328 /* Second: actually write the @print_fmt */
1329 __set_print_fmt(tp, print_fmt, len + 1);
1330 tp->call.print_fmt = print_fmt;
1331
1332 return 0;
1333}
1334
07b139c8 1335#ifdef CONFIG_PERF_EVENTS
e08d1c65
MH
1336
1337/* Kprobe profile handler */
97d5a220 1338static __kprobes void kprobe_perf_func(struct kprobe *kp,
e08d1c65
MH
1339 struct pt_regs *regs)
1340{
1341 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1342 struct ftrace_event_call *call = &tp->call;
93ccae7a 1343 struct kprobe_trace_entry_head *entry;
1c024eca 1344 struct hlist_head *head;
93ccae7a 1345 u8 *data;
430ad5a6 1346 int size, __size, i;
4ed7c92d 1347 int rctx;
e08d1c65 1348
93ccae7a 1349 __size = sizeof(*entry) + tp->size;
74ebb63e
MH
1350 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1351 size -= sizeof(u32);
97d5a220 1352 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
a1a138d0 1353 "profile buffer not large enough"))
1e12a4a7 1354 return;
ce71b9df 1355
b7e2ecef 1356 entry = perf_trace_buf_prepare(size, call->id, regs, &rctx);
430ad5a6 1357 if (!entry)
1e12a4a7 1358 return;
a1a138d0 1359
a1a138d0 1360 entry->ip = (unsigned long)kp->addr;
93ccae7a 1361 data = (u8 *)&entry[1];
a1a138d0 1362 for (i = 0; i < tp->nr_args; i++)
93ccae7a 1363 call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
444a2a3b 1364
1c024eca
PZ
1365 head = per_cpu_ptr(call->perf_events, smp_processor_id());
1366 perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
e08d1c65
MH
1367}
1368
1369/* Kretprobe profile handler */
97d5a220 1370static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
e08d1c65
MH
1371 struct pt_regs *regs)
1372{
1373 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1374 struct ftrace_event_call *call = &tp->call;
93ccae7a 1375 struct kretprobe_trace_entry_head *entry;
1c024eca 1376 struct hlist_head *head;
93ccae7a 1377 u8 *data;
430ad5a6 1378 int size, __size, i;
4ed7c92d 1379 int rctx;
e08d1c65 1380
93ccae7a 1381 __size = sizeof(*entry) + tp->size;
74ebb63e
MH
1382 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1383 size -= sizeof(u32);
97d5a220 1384 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
a1a138d0 1385 "profile buffer not large enough"))
1e12a4a7 1386 return;
444a2a3b 1387
b7e2ecef 1388 entry = perf_trace_buf_prepare(size, call->id, regs, &rctx);
430ad5a6 1389 if (!entry)
1e12a4a7 1390 return;
e08d1c65 1391
a1a138d0
MH
1392 entry->func = (unsigned long)tp->rp.kp.addr;
1393 entry->ret_ip = (unsigned long)ri->ret_addr;
93ccae7a 1394 data = (u8 *)&entry[1];
a1a138d0 1395 for (i = 0; i < tp->nr_args; i++)
93ccae7a 1396 call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
444a2a3b 1397
1c024eca
PZ
1398 head = per_cpu_ptr(call->perf_events, smp_processor_id());
1399 perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
e08d1c65
MH
1400}
1401
97d5a220 1402static int probe_perf_enable(struct ftrace_event_call *call)
e08d1c65
MH
1403{
1404 struct trace_probe *tp = (struct trace_probe *)call->data;
1405
50d78056 1406 tp->flags |= TP_FLAG_PROFILE;
d7a4b414 1407
50d78056 1408 if (probe_is_return(tp))
e08d1c65 1409 return enable_kretprobe(&tp->rp);
50d78056 1410 else
e08d1c65 1411 return enable_kprobe(&tp->rp.kp);
e08d1c65
MH
1412}
1413
97d5a220 1414static void probe_perf_disable(struct ftrace_event_call *call)
e08d1c65 1415{
50d78056
MH
1416 struct trace_probe *tp = (struct trace_probe *)call->data;
1417
d7a4b414 1418 tp->flags &= ~TP_FLAG_PROFILE;
50d78056 1419
d7a4b414 1420 if (!(tp->flags & TP_FLAG_TRACE)) {
50d78056
MH
1421 if (probe_is_return(tp))
1422 disable_kretprobe(&tp->rp);
1423 else
1424 disable_kprobe(&tp->rp.kp);
1425 }
e08d1c65 1426}
07b139c8 1427#endif /* CONFIG_PERF_EVENTS */
50d78056
MH
1428
1429
1430static __kprobes
1431int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1432{
1433 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
e08d1c65 1434
50d78056
MH
1435 if (tp->flags & TP_FLAG_TRACE)
1436 kprobe_trace_func(kp, regs);
07b139c8 1437#ifdef CONFIG_PERF_EVENTS
50d78056 1438 if (tp->flags & TP_FLAG_PROFILE)
97d5a220 1439 kprobe_perf_func(kp, regs);
07b139c8 1440#endif
50d78056
MH
1441 return 0; /* We don't tweek kernel, so just return 0 */
1442}
1443
1444static __kprobes
1445int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1446{
1447 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1448
1449 if (tp->flags & TP_FLAG_TRACE)
1450 kretprobe_trace_func(ri, regs);
07b139c8 1451#ifdef CONFIG_PERF_EVENTS
50d78056 1452 if (tp->flags & TP_FLAG_PROFILE)
97d5a220 1453 kretprobe_perf_func(ri, regs);
07b139c8 1454#endif
50d78056
MH
1455 return 0; /* We don't tweek kernel, so just return 0 */
1456}
e08d1c65 1457
413d37d1
MH
1458static int register_probe_event(struct trace_probe *tp)
1459{
1460 struct ftrace_event_call *call = &tp->call;
1461 int ret;
1462
1463 /* Initialize ftrace_event_call */
413d37d1 1464 if (probe_is_return(tp)) {
ff50d991 1465 tp->event.trace = print_kretprobe_event;
413d37d1 1466 call->raw_init = probe_event_raw_init;
413d37d1
MH
1467 call->define_fields = kretprobe_event_define_fields;
1468 } else {
ff50d991 1469 tp->event.trace = print_kprobe_event;
413d37d1 1470 call->raw_init = probe_event_raw_init;
413d37d1
MH
1471 call->define_fields = kprobe_event_define_fields;
1472 }
a342a028
LJ
1473 if (set_print_fmt(tp) < 0)
1474 return -ENOMEM;
ff50d991
MH
1475 call->event = &tp->event;
1476 call->id = register_ftrace_event(&tp->event);
a342a028
LJ
1477 if (!call->id) {
1478 kfree(call->print_fmt);
ff50d991 1479 return -ENODEV;
a342a028 1480 }
5a0d9050 1481 call->enabled = 0;
413d37d1
MH
1482 call->regfunc = probe_event_enable;
1483 call->unregfunc = probe_event_disable;
e08d1c65 1484
07b139c8 1485#ifdef CONFIG_PERF_EVENTS
97d5a220
FW
1486 call->perf_event_enable = probe_perf_enable;
1487 call->perf_event_disable = probe_perf_disable;
e08d1c65 1488#endif
413d37d1
MH
1489 call->data = tp;
1490 ret = trace_add_event_call(call);
ff50d991 1491 if (ret) {
413d37d1 1492 pr_info("Failed to register kprobe event: %s\n", call->name);
a342a028 1493 kfree(call->print_fmt);
ff50d991
MH
1494 unregister_ftrace_event(&tp->event);
1495 }
413d37d1
MH
1496 return ret;
1497}
1498
1499static void unregister_probe_event(struct trace_probe *tp)
1500{
ff50d991 1501 /* tp->event is unregistered in trace_remove_event_call() */
413d37d1 1502 trace_remove_event_call(&tp->call);
a342a028 1503 kfree(tp->call.print_fmt);
413d37d1
MH
1504}
1505
1506/* Make a debugfs interface for controling probe points */
1507static __init int init_kprobe_trace(void)
1508{
1509 struct dentry *d_tracer;
1510 struct dentry *entry;
413d37d1
MH
1511
1512 d_tracer = tracing_init_dentry();
1513 if (!d_tracer)
1514 return 0;
1515
1516 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1517 NULL, &kprobe_events_ops);
1518
cd7e7bd5 1519 /* Event list interface */
413d37d1
MH
1520 if (!entry)
1521 pr_warning("Could not create debugfs "
1522 "'kprobe_events' entry\n");
cd7e7bd5
MH
1523
1524 /* Profile interface */
1525 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1526 NULL, &kprobe_profile_ops);
1527
1528 if (!entry)
1529 pr_warning("Could not create debugfs "
1530 "'kprobe_profile' entry\n");
413d37d1
MH
1531 return 0;
1532}
1533fs_initcall(init_kprobe_trace);
1534
1535
1536#ifdef CONFIG_FTRACE_STARTUP_TEST
1537
1538static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1539 int a4, int a5, int a6)
1540{
1541 return a1 + a2 + a3 + a4 + a5 + a6;
1542}
1543
1544static __init int kprobe_trace_self_tests_init(void)
1545{
231e36f4 1546 int ret, warn = 0;
413d37d1 1547 int (*target)(int, int, int, int, int, int);
231e36f4 1548 struct trace_probe *tp;
413d37d1
MH
1549
1550 target = kprobe_trace_selftest_target;
1551
1552 pr_info("Testing kprobe tracing: ");
1553
1554 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
231e36f4
MH
1555 "$stack $stack0 +0($stack)");
1556 if (WARN_ON_ONCE(ret)) {
1557 pr_warning("error on probing function entry.\n");
1558 warn++;
1559 } else {
1560 /* Enable trace point */
1561 tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
1562 if (WARN_ON_ONCE(tp == NULL)) {
1563 pr_warning("error on getting new probe.\n");
1564 warn++;
1565 } else
1566 probe_event_enable(&tp->call);
1567 }
413d37d1
MH
1568
1569 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
f397af06 1570 "$retval");
231e36f4
MH
1571 if (WARN_ON_ONCE(ret)) {
1572 pr_warning("error on probing function return.\n");
1573 warn++;
1574 } else {
1575 /* Enable trace point */
1576 tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
1577 if (WARN_ON_ONCE(tp == NULL)) {
1578 pr_warning("error on getting new probe.\n");
1579 warn++;
1580 } else
1581 probe_event_enable(&tp->call);
1582 }
1583
1584 if (warn)
1585 goto end;
413d37d1
MH
1586
1587 ret = target(1, 2, 3, 4, 5, 6);
1588
231e36f4
MH
1589 ret = command_trace_probe("-:testprobe");
1590 if (WARN_ON_ONCE(ret)) {
1591 pr_warning("error on deleting a probe.\n");
1592 warn++;
1593 }
1594
1595 ret = command_trace_probe("-:testprobe2");
1596 if (WARN_ON_ONCE(ret)) {
1597 pr_warning("error on deleting a probe.\n");
1598 warn++;
1599 }
413d37d1 1600
231e36f4
MH
1601end:
1602 cleanup_all_probes();
1603 if (warn)
1604 pr_cont("NG: Some tests are failed. Please check them.\n");
1605 else
1606 pr_cont("OK\n");
413d37d1
MH
1607 return 0;
1608}
1609
1610late_initcall(kprobe_trace_self_tests_init);
1611
1612#endif