2 * Common code for probe-based Dynamic events.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * This code was copied from kernel/trace/trace_kprobe.c written by
18 * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
20 * Updates to make this generic:
21 * Copyright (C) IBM Corporation, 2010-2011
22 * Author: Srikar Dronamraju
25 #include "trace_probe.h"
27 const char *reserved_field_names
[] = {
30 "common_preempt_count",
38 /* Printing function type */
39 #define PRINT_TYPE_FUNC_NAME(type) print_type_##type
40 #define PRINT_TYPE_FMT_NAME(type) print_type_format_##type
42 /* Printing in basic type function template */
43 #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt, cast) \
44 static __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \
46 void *data, void *ent)\
48 return trace_seq_printf(s, " %s=" fmt, name, (cast)*(type *)data);\
50 static const char PRINT_TYPE_FMT_NAME(type)[] = fmt;
52 DEFINE_BASIC_PRINT_TYPE_FUNC(u8
, "%x", unsigned int)
53 DEFINE_BASIC_PRINT_TYPE_FUNC(u16
, "%x", unsigned int)
54 DEFINE_BASIC_PRINT_TYPE_FUNC(u32
, "%lx", unsigned long)
55 DEFINE_BASIC_PRINT_TYPE_FUNC(u64
, "%llx", unsigned long long)
56 DEFINE_BASIC_PRINT_TYPE_FUNC(s8
, "%d", int)
57 DEFINE_BASIC_PRINT_TYPE_FUNC(s16
, "%d", int)
58 DEFINE_BASIC_PRINT_TYPE_FUNC(s32
, "%ld", long)
59 DEFINE_BASIC_PRINT_TYPE_FUNC(s64
, "%lld", long long)
61 static inline void *get_rloc_data(u32
*dl
)
63 return (u8
*)dl
+ get_rloc_offs(*dl
);
66 /* For data_loc conversion */
67 static inline void *get_loc_data(u32
*dl
, void *ent
)
69 return (u8
*)ent
+ get_rloc_offs(*dl
);
72 /* For defining macros, define string/string_size types */
74 typedef u32 string_size
;
76 /* Print type function for string type */
77 static __kprobes
int PRINT_TYPE_FUNC_NAME(string
)(struct trace_seq
*s
,
79 void *data
, void *ent
)
81 int len
= *(u32
*)data
>> 16;
84 return trace_seq_printf(s
, " %s=(fault)", name
);
86 return trace_seq_printf(s
, " %s=\"%s\"", name
,
87 (const char *)get_loc_data(data
, ent
));
90 static const char PRINT_TYPE_FMT_NAME(string
)[] = "\\\"%s\\\"";
92 #define FETCH_FUNC_NAME(method, type) fetch_##method##_##type
94 * Define macro for basic types - we don't need to define s* types, because
95 * we have to care only about bitwidth at recording time.
97 #define DEFINE_BASIC_FETCH_FUNCS(method) \
98 DEFINE_FETCH_##method(u8) \
99 DEFINE_FETCH_##method(u16) \
100 DEFINE_FETCH_##method(u32) \
101 DEFINE_FETCH_##method(u64)
103 #define CHECK_FETCH_FUNCS(method, fn) \
104 (((FETCH_FUNC_NAME(method, u8) == fn) || \
105 (FETCH_FUNC_NAME(method, u16) == fn) || \
106 (FETCH_FUNC_NAME(method, u32) == fn) || \
107 (FETCH_FUNC_NAME(method, u64) == fn) || \
108 (FETCH_FUNC_NAME(method, string) == fn) || \
109 (FETCH_FUNC_NAME(method, string_size) == fn)) \
112 /* Data fetch function templates */
113 #define DEFINE_FETCH_reg(type) \
114 static __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \
115 void *offset, void *dest) \
117 *(type *)dest = (type)regs_get_register(regs, \
118 (unsigned int)((unsigned long)offset)); \
120 DEFINE_BASIC_FETCH_FUNCS(reg
)
121 /* No string on the register */
122 #define fetch_reg_string NULL
123 #define fetch_reg_string_size NULL
125 #define DEFINE_FETCH_stack(type) \
126 static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
127 void *offset, void *dest) \
129 *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
130 (unsigned int)((unsigned long)offset)); \
132 DEFINE_BASIC_FETCH_FUNCS(stack
)
133 /* No string on the stack entry */
134 #define fetch_stack_string NULL
135 #define fetch_stack_string_size NULL
137 #define DEFINE_FETCH_retval(type) \
138 static __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,\
139 void *dummy, void *dest) \
141 *(type *)dest = (type)regs_return_value(regs); \
143 DEFINE_BASIC_FETCH_FUNCS(retval
)
144 /* No string on the retval */
145 #define fetch_retval_string NULL
146 #define fetch_retval_string_size NULL
148 #define DEFINE_FETCH_memory(type) \
149 static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\
150 void *addr, void *dest) \
153 if (probe_kernel_address(addr, retval)) \
156 *(type *)dest = retval; \
158 DEFINE_BASIC_FETCH_FUNCS(memory
)
160 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
161 * length and relative data location.
163 static __kprobes
void FETCH_FUNC_NAME(memory
, string
)(struct pt_regs
*regs
,
164 void *addr
, void *dest
)
167 int maxlen
= get_rloc_len(*(u32
*)dest
);
168 u8
*dst
= get_rloc_data(dest
);
170 mm_segment_t old_fs
= get_fs();
176 * Try to get string again, since the string can be changed while
183 ret
= __copy_from_user_inatomic(dst
++, src
++, 1);
184 while (dst
[-1] && ret
== 0 && src
- (u8
*)addr
< maxlen
);
190 if (ret
< 0) { /* Failed to fetch string */
191 ((u8
*)get_rloc_data(dest
))[0] = '\0';
192 *(u32
*)dest
= make_data_rloc(0, get_rloc_offs(*(u32
*)dest
));
194 *(u32
*)dest
= make_data_rloc(src
- (u8
*)addr
,
195 get_rloc_offs(*(u32
*)dest
));
199 /* Return the length of string -- including null terminal byte */
200 static __kprobes
void FETCH_FUNC_NAME(memory
, string_size
)(struct pt_regs
*regs
,
201 void *addr
, void *dest
)
212 ret
= __copy_from_user_inatomic(&c
, (u8
*)addr
+ len
, 1);
214 } while (c
&& ret
== 0 && len
< MAX_STRING_SIZE
);
219 if (ret
< 0) /* Failed to check the length */
225 /* Memory fetching by symbol */
226 struct symbol_cache
{
232 static unsigned long update_symbol_cache(struct symbol_cache
*sc
)
234 sc
->addr
= (unsigned long)kallsyms_lookup_name(sc
->symbol
);
237 sc
->addr
+= sc
->offset
;
242 static void free_symbol_cache(struct symbol_cache
*sc
)
248 static struct symbol_cache
*alloc_symbol_cache(const char *sym
, long offset
)
250 struct symbol_cache
*sc
;
252 if (!sym
|| strlen(sym
) == 0)
255 sc
= kzalloc(sizeof(struct symbol_cache
), GFP_KERNEL
);
259 sc
->symbol
= kstrdup(sym
, GFP_KERNEL
);
265 update_symbol_cache(sc
);
270 #define DEFINE_FETCH_symbol(type) \
271 static __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,\
272 void *data, void *dest) \
274 struct symbol_cache *sc = data; \
276 fetch_memory_##type(regs, (void *)sc->addr, dest); \
280 DEFINE_BASIC_FETCH_FUNCS(symbol
)
281 DEFINE_FETCH_symbol(string
)
282 DEFINE_FETCH_symbol(string_size
)
284 /* Dereference memory access function */
285 struct deref_fetch_param
{
286 struct fetch_param orig
;
290 #define DEFINE_FETCH_deref(type) \
291 static __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,\
292 void *data, void *dest) \
294 struct deref_fetch_param *dprm = data; \
295 unsigned long addr; \
296 call_fetch(&dprm->orig, regs, &addr); \
298 addr += dprm->offset; \
299 fetch_memory_##type(regs, (void *)addr, dest); \
303 DEFINE_BASIC_FETCH_FUNCS(deref
)
304 DEFINE_FETCH_deref(string
)
305 DEFINE_FETCH_deref(string_size
)
307 static __kprobes
void update_deref_fetch_param(struct deref_fetch_param
*data
)
309 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
310 update_deref_fetch_param(data
->orig
.data
);
311 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
312 update_symbol_cache(data
->orig
.data
);
315 static __kprobes
void free_deref_fetch_param(struct deref_fetch_param
*data
)
317 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
318 free_deref_fetch_param(data
->orig
.data
);
319 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
320 free_symbol_cache(data
->orig
.data
);
324 /* Bitfield fetch function */
325 struct bitfield_fetch_param
{
326 struct fetch_param orig
;
327 unsigned char hi_shift
;
328 unsigned char low_shift
;
331 #define DEFINE_FETCH_bitfield(type) \
332 static __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs,\
333 void *data, void *dest) \
335 struct bitfield_fetch_param *bprm = data; \
337 call_fetch(&bprm->orig, regs, &buf); \
339 buf <<= bprm->hi_shift; \
340 buf >>= bprm->low_shift; \
342 *(type *)dest = buf; \
345 DEFINE_BASIC_FETCH_FUNCS(bitfield
)
346 #define fetch_bitfield_string NULL
347 #define fetch_bitfield_string_size NULL
349 static __kprobes
void
350 update_bitfield_fetch_param(struct bitfield_fetch_param
*data
)
353 * Don't check the bitfield itself, because this must be the
354 * last fetch function.
356 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
357 update_deref_fetch_param(data
->orig
.data
);
358 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
359 update_symbol_cache(data
->orig
.data
);
362 static __kprobes
void
363 free_bitfield_fetch_param(struct bitfield_fetch_param
*data
)
366 * Don't check the bitfield itself, because this must be the
367 * last fetch function.
369 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
370 free_deref_fetch_param(data
->orig
.data
);
371 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
372 free_symbol_cache(data
->orig
.data
);
377 /* Default (unsigned long) fetch type */
378 #define __DEFAULT_FETCH_TYPE(t) u##t
379 #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
380 #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
381 #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
383 #define ASSIGN_FETCH_FUNC(method, type) \
384 [FETCH_MTD_##method] = FETCH_FUNC_NAME(method, type)
386 #define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \
390 .print = PRINT_TYPE_FUNC_NAME(ptype), \
391 .fmt = PRINT_TYPE_FMT_NAME(ptype), \
392 .fmttype = _fmttype, \
394 ASSIGN_FETCH_FUNC(reg, ftype), \
395 ASSIGN_FETCH_FUNC(stack, ftype), \
396 ASSIGN_FETCH_FUNC(retval, ftype), \
397 ASSIGN_FETCH_FUNC(memory, ftype), \
398 ASSIGN_FETCH_FUNC(symbol, ftype), \
399 ASSIGN_FETCH_FUNC(deref, ftype), \
400 ASSIGN_FETCH_FUNC(bitfield, ftype), \
404 #define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \
405 __ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, #ptype)
407 #define FETCH_TYPE_STRING 0
408 #define FETCH_TYPE_STRSIZE 1
410 /* Fetch type information table */
411 static const struct fetch_type fetch_type_table
[] = {
413 [FETCH_TYPE_STRING
] = __ASSIGN_FETCH_TYPE("string", string
, string
,
414 sizeof(u32
), 1, "__data_loc char[]"),
415 [FETCH_TYPE_STRSIZE
] = __ASSIGN_FETCH_TYPE("string_size", u32
,
416 string_size
, sizeof(u32
), 0, "u32"),
418 ASSIGN_FETCH_TYPE(u8
, u8
, 0),
419 ASSIGN_FETCH_TYPE(u16
, u16
, 0),
420 ASSIGN_FETCH_TYPE(u32
, u32
, 0),
421 ASSIGN_FETCH_TYPE(u64
, u64
, 0),
422 ASSIGN_FETCH_TYPE(s8
, u8
, 1),
423 ASSIGN_FETCH_TYPE(s16
, u16
, 1),
424 ASSIGN_FETCH_TYPE(s32
, u32
, 1),
425 ASSIGN_FETCH_TYPE(s64
, u64
, 1),
428 static const struct fetch_type
*find_fetch_type(const char *type
)
433 type
= DEFAULT_FETCH_TYPE_STR
;
435 /* Special case: bitfield */
439 type
= strchr(type
, '/');
444 if (strict_strtoul(type
, 0, &bs
))
449 return find_fetch_type("u8");
451 return find_fetch_type("u16");
453 return find_fetch_type("u32");
455 return find_fetch_type("u64");
461 for (i
= 0; i
< ARRAY_SIZE(fetch_type_table
); i
++)
462 if (strcmp(type
, fetch_type_table
[i
].name
) == 0)
463 return &fetch_type_table
[i
];
469 /* Special function : only accept unsigned long */
470 static __kprobes
void fetch_stack_address(struct pt_regs
*regs
,
471 void *dummy
, void *dest
)
473 *(unsigned long *)dest
= kernel_stack_pointer(regs
);
476 static fetch_func_t
get_fetch_size_function(const struct fetch_type
*type
,
477 fetch_func_t orig_fn
)
481 if (type
!= &fetch_type_table
[FETCH_TYPE_STRING
])
482 return NULL
; /* Only string type needs size function */
484 for (i
= 0; i
< FETCH_MTD_END
; i
++)
485 if (type
->fetch
[i
] == orig_fn
)
486 return fetch_type_table
[FETCH_TYPE_STRSIZE
].fetch
[i
];
488 WARN_ON(1); /* This should not happen */
493 /* Split symbol and offset. */
494 int traceprobe_split_symbol_offset(char *symbol
, unsigned long *offset
)
502 tmp
= strchr(symbol
, '+');
504 /* skip sign because strict_strtol doesn't accept '+' */
505 ret
= strict_strtoul(tmp
+ 1, 0, offset
);
516 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
518 static int parse_probe_vars(char *arg
, const struct fetch_type
*t
,
519 struct fetch_param
*f
, bool is_return
)
524 if (strcmp(arg
, "retval") == 0) {
526 f
->fn
= t
->fetch
[FETCH_MTD_retval
];
529 } else if (strncmp(arg
, "stack", 5) == 0) {
530 if (arg
[5] == '\0') {
531 if (strcmp(t
->name
, DEFAULT_FETCH_TYPE_STR
) == 0)
532 f
->fn
= fetch_stack_address
;
535 } else if (isdigit(arg
[5])) {
536 ret
= strict_strtoul(arg
+ 5, 10, ¶m
);
537 if (ret
|| param
> PARAM_MAX_STACK
)
540 f
->fn
= t
->fetch
[FETCH_MTD_stack
];
541 f
->data
= (void *)param
;
551 /* Recursive argument parser */
552 static int parse_probe_arg(char *arg
, const struct fetch_type
*t
,
553 struct fetch_param
*f
, bool is_return
, bool is_kprobe
)
562 /* Until uprobe_events supports only reg arguments */
563 if (!is_kprobe
&& arg
[0] != '%')
568 ret
= parse_probe_vars(arg
+ 1, t
, f
, is_return
);
571 case '%': /* named register */
572 ret
= regs_query_register_offset(arg
+ 1);
574 f
->fn
= t
->fetch
[FETCH_MTD_reg
];
575 f
->data
= (void *)(unsigned long)ret
;
580 case '@': /* memory or symbol */
581 if (isdigit(arg
[1])) {
582 ret
= strict_strtoul(arg
+ 1, 0, ¶m
);
586 f
->fn
= t
->fetch
[FETCH_MTD_memory
];
587 f
->data
= (void *)param
;
589 ret
= traceprobe_split_symbol_offset(arg
+ 1, &offset
);
593 f
->data
= alloc_symbol_cache(arg
+ 1, offset
);
595 f
->fn
= t
->fetch
[FETCH_MTD_symbol
];
599 case '+': /* deref memory */
600 arg
++; /* Skip '+', because strict_strtol() rejects it. */
602 tmp
= strchr(arg
, '(');
607 ret
= strict_strtol(arg
, 0, &offset
);
613 tmp
= strrchr(arg
, ')');
616 struct deref_fetch_param
*dprm
;
617 const struct fetch_type
*t2
;
619 t2
= find_fetch_type(NULL
);
621 dprm
= kzalloc(sizeof(struct deref_fetch_param
), GFP_KERNEL
);
626 dprm
->offset
= offset
;
627 ret
= parse_probe_arg(arg
, t2
, &dprm
->orig
, is_return
,
632 f
->fn
= t
->fetch
[FETCH_MTD_deref
];
633 f
->data
= (void *)dprm
;
638 if (!ret
&& !f
->fn
) { /* Parsed, but do not find fetch method */
639 pr_info("%s type has no corresponding fetch method.\n", t
->name
);
646 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
648 /* Bitfield type needs to be parsed into a fetch function */
649 static int __parse_bitfield_probe_arg(const char *bf
,
650 const struct fetch_type
*t
,
651 struct fetch_param
*f
)
653 struct bitfield_fetch_param
*bprm
;
654 unsigned long bw
, bo
;
660 bprm
= kzalloc(sizeof(*bprm
), GFP_KERNEL
);
665 f
->fn
= t
->fetch
[FETCH_MTD_bitfield
];
666 f
->data
= (void *)bprm
;
667 bw
= simple_strtoul(bf
+ 1, &tail
, 0); /* Use simple one */
669 if (bw
== 0 || *tail
!= '@')
673 bo
= simple_strtoul(bf
, &tail
, 0);
675 if (tail
== bf
|| *tail
!= '/')
678 bprm
->hi_shift
= BYTES_TO_BITS(t
->size
) - (bw
+ bo
);
679 bprm
->low_shift
= bprm
->hi_shift
+ bo
;
681 return (BYTES_TO_BITS(t
->size
) < (bw
+ bo
)) ? -EINVAL
: 0;
684 /* String length checking wrapper */
685 int traceprobe_parse_probe_arg(char *arg
, ssize_t
*size
,
686 struct probe_arg
*parg
, bool is_return
, bool is_kprobe
)
691 if (strlen(arg
) > MAX_ARGSTR_LEN
) {
692 pr_info("Argument is too long.: %s\n", arg
);
695 parg
->comm
= kstrdup(arg
, GFP_KERNEL
);
697 pr_info("Failed to allocate memory for command '%s'.\n", arg
);
700 t
= strchr(parg
->comm
, ':');
702 arg
[t
- parg
->comm
] = '\0';
705 parg
->type
= find_fetch_type(t
);
707 pr_info("Unsupported type: %s\n", t
);
710 parg
->offset
= *size
;
711 *size
+= parg
->type
->size
;
712 ret
= parse_probe_arg(arg
, parg
->type
, &parg
->fetch
, is_return
, is_kprobe
);
714 if (ret
>= 0 && t
!= NULL
)
715 ret
= __parse_bitfield_probe_arg(t
, parg
->type
, &parg
->fetch
);
718 parg
->fetch_size
.fn
= get_fetch_size_function(parg
->type
,
720 parg
->fetch_size
.data
= parg
->fetch
.data
;
726 /* Return 1 if name is reserved or already used by another argument */
727 int traceprobe_conflict_field_name(const char *name
,
728 struct probe_arg
*args
, int narg
)
732 for (i
= 0; i
< ARRAY_SIZE(reserved_field_names
); i
++)
733 if (strcmp(reserved_field_names
[i
], name
) == 0)
736 for (i
= 0; i
< narg
; i
++)
737 if (strcmp(args
[i
].name
, name
) == 0)
743 void traceprobe_update_arg(struct probe_arg
*arg
)
745 if (CHECK_FETCH_FUNCS(bitfield
, arg
->fetch
.fn
))
746 update_bitfield_fetch_param(arg
->fetch
.data
);
747 else if (CHECK_FETCH_FUNCS(deref
, arg
->fetch
.fn
))
748 update_deref_fetch_param(arg
->fetch
.data
);
749 else if (CHECK_FETCH_FUNCS(symbol
, arg
->fetch
.fn
))
750 update_symbol_cache(arg
->fetch
.data
);
753 void traceprobe_free_probe_arg(struct probe_arg
*arg
)
755 if (CHECK_FETCH_FUNCS(bitfield
, arg
->fetch
.fn
))
756 free_bitfield_fetch_param(arg
->fetch
.data
);
757 else if (CHECK_FETCH_FUNCS(deref
, arg
->fetch
.fn
))
758 free_deref_fetch_param(arg
->fetch
.data
);
759 else if (CHECK_FETCH_FUNCS(symbol
, arg
->fetch
.fn
))
760 free_symbol_cache(arg
->fetch
.data
);
766 int traceprobe_command(const char *buf
, int (*createfn
)(int, char **))
773 argv
= argv_split(GFP_KERNEL
, buf
, &argc
);
778 ret
= createfn(argc
, argv
);
785 #define WRITE_BUFSIZE 4096
787 ssize_t
traceprobe_probes_write(struct file
*file
, const char __user
*buffer
,
788 size_t count
, loff_t
*ppos
,
789 int (*createfn
)(int, char **))
796 kbuf
= kmalloc(WRITE_BUFSIZE
, GFP_KERNEL
);
800 while (done
< count
) {
803 if (size
>= WRITE_BUFSIZE
)
804 size
= WRITE_BUFSIZE
- 1;
806 if (copy_from_user(kbuf
, buffer
+ done
, size
)) {
811 tmp
= strchr(kbuf
, '\n');
815 size
= tmp
- kbuf
+ 1;
816 } else if (done
+ size
< count
) {
817 pr_warning("Line length is too long: "
818 "Should be less than %d.", WRITE_BUFSIZE
);
823 /* Remove comments */
824 tmp
= strchr(kbuf
, '#');
829 ret
= traceprobe_command(kbuf
, createfn
);