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