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 in basic type function template */
39 #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt) \
40 int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, const char *name, \
41 void *data, void *ent) \
43 trace_seq_printf(s, " %s=" fmt, name, *(type *)data); \
44 return !trace_seq_has_overflowed(s); \
46 const char PRINT_TYPE_FMT_NAME(type)[] = fmt; \
47 NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(type));
49 DEFINE_BASIC_PRINT_TYPE_FUNC(u8
, "0x%x")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(u16
, "0x%x")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(u32
, "0x%x")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(u64
, "0x%Lx")
53 DEFINE_BASIC_PRINT_TYPE_FUNC(s8
, "%d")
54 DEFINE_BASIC_PRINT_TYPE_FUNC(s16
, "%d")
55 DEFINE_BASIC_PRINT_TYPE_FUNC(s32
, "%d")
56 DEFINE_BASIC_PRINT_TYPE_FUNC(s64
, "%Ld")
58 /* Print type function for string type */
59 int PRINT_TYPE_FUNC_NAME(string
)(struct trace_seq
*s
, const char *name
,
60 void *data
, void *ent
)
62 int len
= *(u32
*)data
>> 16;
65 trace_seq_printf(s
, " %s=(fault)", name
);
67 trace_seq_printf(s
, " %s=\"%s\"", name
,
68 (const char *)get_loc_data(data
, ent
));
69 return !trace_seq_has_overflowed(s
);
71 NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(string
));
73 const char PRINT_TYPE_FMT_NAME(string
)[] = "\\\"%s\\\"";
75 #define CHECK_FETCH_FUNCS(method, fn) \
76 (((FETCH_FUNC_NAME(method, u8) == fn) || \
77 (FETCH_FUNC_NAME(method, u16) == fn) || \
78 (FETCH_FUNC_NAME(method, u32) == fn) || \
79 (FETCH_FUNC_NAME(method, u64) == fn) || \
80 (FETCH_FUNC_NAME(method, string) == fn) || \
81 (FETCH_FUNC_NAME(method, string_size) == fn)) \
84 /* Data fetch function templates */
85 #define DEFINE_FETCH_reg(type) \
86 void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, void *offset, void *dest) \
88 *(type *)dest = (type)regs_get_register(regs, \
89 (unsigned int)((unsigned long)offset)); \
91 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(reg, type));
92 DEFINE_BASIC_FETCH_FUNCS(reg
)
93 /* No string on the register */
94 #define fetch_reg_string NULL
95 #define fetch_reg_string_size NULL
97 #define DEFINE_FETCH_retval(type) \
98 void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs, \
99 void *dummy, void *dest) \
101 *(type *)dest = (type)regs_return_value(regs); \
103 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(retval, type));
104 DEFINE_BASIC_FETCH_FUNCS(retval
)
105 /* No string on the retval */
106 #define fetch_retval_string NULL
107 #define fetch_retval_string_size NULL
109 /* Dereference memory access function */
110 struct deref_fetch_param
{
111 struct fetch_param orig
;
114 fetch_func_t fetch_size
;
117 #define DEFINE_FETCH_deref(type) \
118 void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs, \
119 void *data, void *dest) \
121 struct deref_fetch_param *dprm = data; \
122 unsigned long addr; \
123 call_fetch(&dprm->orig, regs, &addr); \
125 addr += dprm->offset; \
126 dprm->fetch(regs, (void *)addr, dest); \
130 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref, type));
131 DEFINE_BASIC_FETCH_FUNCS(deref
)
132 DEFINE_FETCH_deref(string
)
134 void FETCH_FUNC_NAME(deref
, string_size
)(struct pt_regs
*regs
,
135 void *data
, void *dest
)
137 struct deref_fetch_param
*dprm
= data
;
140 call_fetch(&dprm
->orig
, regs
, &addr
);
141 if (addr
&& dprm
->fetch_size
) {
142 addr
+= dprm
->offset
;
143 dprm
->fetch_size(regs
, (void *)addr
, dest
);
145 *(string_size
*)dest
= 0;
147 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref
, string_size
));
149 static void update_deref_fetch_param(struct deref_fetch_param
*data
)
151 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
152 update_deref_fetch_param(data
->orig
.data
);
153 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
154 update_symbol_cache(data
->orig
.data
);
156 NOKPROBE_SYMBOL(update_deref_fetch_param
);
158 static void free_deref_fetch_param(struct deref_fetch_param
*data
)
160 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
161 free_deref_fetch_param(data
->orig
.data
);
162 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
163 free_symbol_cache(data
->orig
.data
);
166 NOKPROBE_SYMBOL(free_deref_fetch_param
);
168 /* Bitfield fetch function */
169 struct bitfield_fetch_param
{
170 struct fetch_param orig
;
171 unsigned char hi_shift
;
172 unsigned char low_shift
;
175 #define DEFINE_FETCH_bitfield(type) \
176 void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs, \
177 void *data, void *dest) \
179 struct bitfield_fetch_param *bprm = data; \
181 call_fetch(&bprm->orig, regs, &buf); \
183 buf <<= bprm->hi_shift; \
184 buf >>= bprm->low_shift; \
186 *(type *)dest = buf; \
188 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(bitfield, type));
189 DEFINE_BASIC_FETCH_FUNCS(bitfield
)
190 #define fetch_bitfield_string NULL
191 #define fetch_bitfield_string_size NULL
194 update_bitfield_fetch_param(struct bitfield_fetch_param
*data
)
197 * Don't check the bitfield itself, because this must be the
198 * last fetch function.
200 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
201 update_deref_fetch_param(data
->orig
.data
);
202 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
203 update_symbol_cache(data
->orig
.data
);
207 free_bitfield_fetch_param(struct bitfield_fetch_param
*data
)
210 * Don't check the bitfield itself, because this must be the
211 * last fetch function.
213 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
214 free_deref_fetch_param(data
->orig
.data
);
215 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
216 free_symbol_cache(data
->orig
.data
);
221 static const struct fetch_type
*find_fetch_type(const char *type
,
222 const struct fetch_type
*ftbl
)
227 type
= DEFAULT_FETCH_TYPE_STR
;
229 /* Special case: bitfield */
233 type
= strchr(type
, '/');
238 if (kstrtoul(type
, 0, &bs
))
243 return find_fetch_type("u8", ftbl
);
245 return find_fetch_type("u16", ftbl
);
247 return find_fetch_type("u32", ftbl
);
249 return find_fetch_type("u64", ftbl
);
255 for (i
= 0; ftbl
[i
].name
; i
++) {
256 if (strcmp(type
, ftbl
[i
].name
) == 0)
264 /* Special function : only accept unsigned long */
265 static void fetch_kernel_stack_address(struct pt_regs
*regs
, void *dummy
, void *dest
)
267 *(unsigned long *)dest
= kernel_stack_pointer(regs
);
269 NOKPROBE_SYMBOL(fetch_kernel_stack_address
);
271 static void fetch_user_stack_address(struct pt_regs
*regs
, void *dummy
, void *dest
)
273 *(unsigned long *)dest
= user_stack_pointer(regs
);
275 NOKPROBE_SYMBOL(fetch_user_stack_address
);
277 static fetch_func_t
get_fetch_size_function(const struct fetch_type
*type
,
278 fetch_func_t orig_fn
,
279 const struct fetch_type
*ftbl
)
283 if (type
!= &ftbl
[FETCH_TYPE_STRING
])
284 return NULL
; /* Only string type needs size function */
286 for (i
= 0; i
< FETCH_MTD_END
; i
++)
287 if (type
->fetch
[i
] == orig_fn
)
288 return ftbl
[FETCH_TYPE_STRSIZE
].fetch
[i
];
290 WARN_ON(1); /* This should not happen */
295 /* Split symbol and offset. */
296 int traceprobe_split_symbol_offset(char *symbol
, unsigned long *offset
)
304 tmp
= strchr(symbol
, '+');
306 /* skip sign because kstrtoul doesn't accept '+' */
307 ret
= kstrtoul(tmp
+ 1, 0, offset
);
318 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
320 static int parse_probe_vars(char *arg
, const struct fetch_type
*t
,
321 struct fetch_param
*f
, bool is_return
,
327 if (strcmp(arg
, "retval") == 0) {
329 f
->fn
= t
->fetch
[FETCH_MTD_retval
];
332 } else if (strncmp(arg
, "stack", 5) == 0) {
333 if (arg
[5] == '\0') {
334 if (strcmp(t
->name
, DEFAULT_FETCH_TYPE_STR
))
338 f
->fn
= fetch_kernel_stack_address
;
340 f
->fn
= fetch_user_stack_address
;
341 } else if (isdigit(arg
[5])) {
342 ret
= kstrtoul(arg
+ 5, 10, ¶m
);
343 if (ret
|| (is_kprobe
&& param
> PARAM_MAX_STACK
))
346 f
->fn
= t
->fetch
[FETCH_MTD_stack
];
347 f
->data
= (void *)param
;
357 /* Recursive argument parser */
358 static int parse_probe_arg(char *arg
, const struct fetch_type
*t
,
359 struct fetch_param
*f
, bool is_return
, bool is_kprobe
,
360 const struct fetch_type
*ftbl
)
369 ret
= parse_probe_vars(arg
+ 1, t
, f
, is_return
, is_kprobe
);
372 case '%': /* named register */
373 ret
= regs_query_register_offset(arg
+ 1);
375 f
->fn
= t
->fetch
[FETCH_MTD_reg
];
376 f
->data
= (void *)(unsigned long)ret
;
381 case '@': /* memory, file-offset or symbol */
382 if (isdigit(arg
[1])) {
383 ret
= kstrtoul(arg
+ 1, 0, ¶m
);
387 f
->fn
= t
->fetch
[FETCH_MTD_memory
];
388 f
->data
= (void *)param
;
389 } else if (arg
[1] == '+') {
390 /* kprobes don't support file offsets */
394 ret
= kstrtol(arg
+ 2, 0, &offset
);
398 f
->fn
= t
->fetch
[FETCH_MTD_file_offset
];
399 f
->data
= (void *)offset
;
401 /* uprobes don't support symbols */
405 ret
= traceprobe_split_symbol_offset(arg
+ 1, &offset
);
409 f
->data
= alloc_symbol_cache(arg
+ 1, offset
);
411 f
->fn
= t
->fetch
[FETCH_MTD_symbol
];
415 case '+': /* deref memory */
416 arg
++; /* Skip '+', because kstrtol() rejects it. */
418 tmp
= strchr(arg
, '(');
423 ret
= kstrtol(arg
, 0, &offset
);
429 tmp
= strrchr(arg
, ')');
432 struct deref_fetch_param
*dprm
;
433 const struct fetch_type
*t2
;
435 t2
= find_fetch_type(NULL
, ftbl
);
437 dprm
= kzalloc(sizeof(struct deref_fetch_param
), GFP_KERNEL
);
442 dprm
->offset
= offset
;
443 dprm
->fetch
= t
->fetch
[FETCH_MTD_memory
];
444 dprm
->fetch_size
= get_fetch_size_function(t
,
446 ret
= parse_probe_arg(arg
, t2
, &dprm
->orig
, is_return
,
451 f
->fn
= t
->fetch
[FETCH_MTD_deref
];
452 f
->data
= (void *)dprm
;
457 if (!ret
&& !f
->fn
) { /* Parsed, but do not find fetch method */
458 pr_info("%s type has no corresponding fetch method.\n", t
->name
);
465 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
467 /* Bitfield type needs to be parsed into a fetch function */
468 static int __parse_bitfield_probe_arg(const char *bf
,
469 const struct fetch_type
*t
,
470 struct fetch_param
*f
)
472 struct bitfield_fetch_param
*bprm
;
473 unsigned long bw
, bo
;
479 bprm
= kzalloc(sizeof(*bprm
), GFP_KERNEL
);
484 f
->fn
= t
->fetch
[FETCH_MTD_bitfield
];
485 f
->data
= (void *)bprm
;
486 bw
= simple_strtoul(bf
+ 1, &tail
, 0); /* Use simple one */
488 if (bw
== 0 || *tail
!= '@')
492 bo
= simple_strtoul(bf
, &tail
, 0);
494 if (tail
== bf
|| *tail
!= '/')
497 bprm
->hi_shift
= BYTES_TO_BITS(t
->size
) - (bw
+ bo
);
498 bprm
->low_shift
= bprm
->hi_shift
+ bo
;
500 return (BYTES_TO_BITS(t
->size
) < (bw
+ bo
)) ? -EINVAL
: 0;
503 /* String length checking wrapper */
504 int traceprobe_parse_probe_arg(char *arg
, ssize_t
*size
,
505 struct probe_arg
*parg
, bool is_return
, bool is_kprobe
,
506 const struct fetch_type
*ftbl
)
511 if (strlen(arg
) > MAX_ARGSTR_LEN
) {
512 pr_info("Argument is too long.: %s\n", arg
);
515 parg
->comm
= kstrdup(arg
, GFP_KERNEL
);
517 pr_info("Failed to allocate memory for command '%s'.\n", arg
);
520 t
= strchr(parg
->comm
, ':');
522 arg
[t
- parg
->comm
] = '\0';
525 parg
->type
= find_fetch_type(t
, ftbl
);
527 pr_info("Unsupported type: %s\n", t
);
530 parg
->offset
= *size
;
531 *size
+= parg
->type
->size
;
532 ret
= parse_probe_arg(arg
, parg
->type
, &parg
->fetch
, is_return
,
535 if (ret
>= 0 && t
!= NULL
)
536 ret
= __parse_bitfield_probe_arg(t
, parg
->type
, &parg
->fetch
);
539 parg
->fetch_size
.fn
= get_fetch_size_function(parg
->type
,
542 parg
->fetch_size
.data
= parg
->fetch
.data
;
548 /* Return 1 if name is reserved or already used by another argument */
549 int traceprobe_conflict_field_name(const char *name
,
550 struct probe_arg
*args
, int narg
)
554 for (i
= 0; i
< ARRAY_SIZE(reserved_field_names
); i
++)
555 if (strcmp(reserved_field_names
[i
], name
) == 0)
558 for (i
= 0; i
< narg
; i
++)
559 if (strcmp(args
[i
].name
, name
) == 0)
565 void traceprobe_update_arg(struct probe_arg
*arg
)
567 if (CHECK_FETCH_FUNCS(bitfield
, arg
->fetch
.fn
))
568 update_bitfield_fetch_param(arg
->fetch
.data
);
569 else if (CHECK_FETCH_FUNCS(deref
, arg
->fetch
.fn
))
570 update_deref_fetch_param(arg
->fetch
.data
);
571 else if (CHECK_FETCH_FUNCS(symbol
, arg
->fetch
.fn
))
572 update_symbol_cache(arg
->fetch
.data
);
575 void traceprobe_free_probe_arg(struct probe_arg
*arg
)
577 if (CHECK_FETCH_FUNCS(bitfield
, arg
->fetch
.fn
))
578 free_bitfield_fetch_param(arg
->fetch
.data
);
579 else if (CHECK_FETCH_FUNCS(deref
, arg
->fetch
.fn
))
580 free_deref_fetch_param(arg
->fetch
.data
);
581 else if (CHECK_FETCH_FUNCS(symbol
, arg
->fetch
.fn
))
582 free_symbol_cache(arg
->fetch
.data
);
588 int traceprobe_command(const char *buf
, int (*createfn
)(int, char **))
595 argv
= argv_split(GFP_KERNEL
, buf
, &argc
);
600 ret
= createfn(argc
, argv
);
607 #define WRITE_BUFSIZE 4096
609 ssize_t
traceprobe_probes_write(struct file
*file
, const char __user
*buffer
,
610 size_t count
, loff_t
*ppos
,
611 int (*createfn
)(int, char **))
618 kbuf
= kmalloc(WRITE_BUFSIZE
, GFP_KERNEL
);
622 while (done
< count
) {
625 if (size
>= WRITE_BUFSIZE
)
626 size
= WRITE_BUFSIZE
- 1;
628 if (copy_from_user(kbuf
, buffer
+ done
, size
)) {
633 tmp
= strchr(kbuf
, '\n');
637 size
= tmp
- kbuf
+ 1;
638 } else if (done
+ size
< count
) {
639 pr_warning("Line length is too long: "
640 "Should be less than %d.", WRITE_BUFSIZE
);
645 /* Remove comments */
646 tmp
= strchr(kbuf
, '#');
651 ret
= traceprobe_command(kbuf
, createfn
);
663 static int __set_print_fmt(struct trace_probe
*tp
, char *buf
, int len
,
669 const char *fmt
, *arg
;
673 arg
= "REC->" FIELD_STRING_IP
;
675 fmt
= "(%lx <- %lx)";
676 arg
= "REC->" FIELD_STRING_FUNC
", REC->" FIELD_STRING_RETIP
;
679 /* When len=0, we just calculate the needed length */
680 #define LEN_OR_ZERO (len ? len - pos : 0)
682 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"%s", fmt
);
684 for (i
= 0; i
< tp
->nr_args
; i
++) {
685 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, " %s=%s",
686 tp
->args
[i
].name
, tp
->args
[i
].type
->fmt
);
689 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\", %s", arg
);
691 for (i
= 0; i
< tp
->nr_args
; i
++) {
692 if (strcmp(tp
->args
[i
].type
->name
, "string") == 0)
693 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
697 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, ", REC->%s",
703 /* return the length of print_fmt */
707 int set_print_fmt(struct trace_probe
*tp
, bool is_return
)
712 /* First: called with 0 length to calculate the needed length */
713 len
= __set_print_fmt(tp
, NULL
, 0, is_return
);
714 print_fmt
= kmalloc(len
+ 1, GFP_KERNEL
);
718 /* Second: actually write the @print_fmt */
719 __set_print_fmt(tp
, print_fmt
, len
+ 1, is_return
);
720 tp
->call
.print_fmt
= print_fmt
;