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 __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \
42 void *data, void *ent) \
44 return trace_seq_printf(s, " %s=" fmt, name, *(type *)data); \
46 const char PRINT_TYPE_FMT_NAME(type)[] = fmt;
48 DEFINE_BASIC_PRINT_TYPE_FUNC(u8
, "0x%x")
49 DEFINE_BASIC_PRINT_TYPE_FUNC(u16
, "0x%x")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(u32
, "0x%x")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(u64
, "0x%Lx")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(s8
, "%d")
53 DEFINE_BASIC_PRINT_TYPE_FUNC(s16
, "%d")
54 DEFINE_BASIC_PRINT_TYPE_FUNC(s32
, "%d")
55 DEFINE_BASIC_PRINT_TYPE_FUNC(s64
, "%Ld")
57 /* Print type function for string type */
58 __kprobes
int PRINT_TYPE_FUNC_NAME(string
)(struct trace_seq
*s
,
60 void *data
, void *ent
)
62 int len
= *(u32
*)data
>> 16;
65 return trace_seq_printf(s
, " %s=(fault)", name
);
67 return trace_seq_printf(s
, " %s=\"%s\"", name
,
68 (const char *)get_loc_data(data
, ent
));
71 const char PRINT_TYPE_FMT_NAME(string
)[] = "\\\"%s\\\"";
73 #define CHECK_FETCH_FUNCS(method, fn) \
74 (((FETCH_FUNC_NAME(method, u8) == fn) || \
75 (FETCH_FUNC_NAME(method, u16) == fn) || \
76 (FETCH_FUNC_NAME(method, u32) == fn) || \
77 (FETCH_FUNC_NAME(method, u64) == fn) || \
78 (FETCH_FUNC_NAME(method, string) == fn) || \
79 (FETCH_FUNC_NAME(method, string_size) == fn)) \
82 /* Data fetch function templates */
83 #define DEFINE_FETCH_reg(type) \
84 __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \
85 void *offset, void *dest) \
87 *(type *)dest = (type)regs_get_register(regs, \
88 (unsigned int)((unsigned long)offset)); \
90 DEFINE_BASIC_FETCH_FUNCS(reg
)
91 /* No string on the register */
92 #define fetch_reg_string NULL
93 #define fetch_reg_string_size NULL
95 #define DEFINE_FETCH_retval(type) \
96 __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs, \
97 void *dummy, void *dest) \
99 *(type *)dest = (type)regs_return_value(regs); \
101 DEFINE_BASIC_FETCH_FUNCS(retval
)
102 /* No string on the retval */
103 #define fetch_retval_string NULL
104 #define fetch_retval_string_size NULL
106 /* Dereference memory access function */
107 struct deref_fetch_param
{
108 struct fetch_param orig
;
111 fetch_func_t fetch_size
;
114 #define DEFINE_FETCH_deref(type) \
115 __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs, \
116 void *data, void *dest) \
118 struct deref_fetch_param *dprm = data; \
119 unsigned long addr; \
120 call_fetch(&dprm->orig, regs, &addr); \
122 addr += dprm->offset; \
123 dprm->fetch(regs, (void *)addr, dest); \
127 DEFINE_BASIC_FETCH_FUNCS(deref
)
128 DEFINE_FETCH_deref(string
)
130 __kprobes
void FETCH_FUNC_NAME(deref
, string_size
)(struct pt_regs
*regs
,
131 void *data
, void *dest
)
133 struct deref_fetch_param
*dprm
= data
;
136 call_fetch(&dprm
->orig
, regs
, &addr
);
137 if (addr
&& dprm
->fetch_size
) {
138 addr
+= dprm
->offset
;
139 dprm
->fetch_size(regs
, (void *)addr
, dest
);
141 *(string_size
*)dest
= 0;
144 static __kprobes
void update_deref_fetch_param(struct deref_fetch_param
*data
)
146 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
147 update_deref_fetch_param(data
->orig
.data
);
148 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
149 update_symbol_cache(data
->orig
.data
);
152 static __kprobes
void free_deref_fetch_param(struct deref_fetch_param
*data
)
154 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
155 free_deref_fetch_param(data
->orig
.data
);
156 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
157 free_symbol_cache(data
->orig
.data
);
161 /* Bitfield fetch function */
162 struct bitfield_fetch_param
{
163 struct fetch_param orig
;
164 unsigned char hi_shift
;
165 unsigned char low_shift
;
168 #define DEFINE_FETCH_bitfield(type) \
169 __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs, \
170 void *data, void *dest) \
172 struct bitfield_fetch_param *bprm = data; \
174 call_fetch(&bprm->orig, regs, &buf); \
176 buf <<= bprm->hi_shift; \
177 buf >>= bprm->low_shift; \
179 *(type *)dest = buf; \
182 DEFINE_BASIC_FETCH_FUNCS(bitfield
)
183 #define fetch_bitfield_string NULL
184 #define fetch_bitfield_string_size NULL
186 static __kprobes
void
187 update_bitfield_fetch_param(struct bitfield_fetch_param
*data
)
190 * Don't check the bitfield itself, because this must be the
191 * last fetch function.
193 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
194 update_deref_fetch_param(data
->orig
.data
);
195 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
196 update_symbol_cache(data
->orig
.data
);
199 static __kprobes
void
200 free_bitfield_fetch_param(struct bitfield_fetch_param
*data
)
203 * Don't check the bitfield itself, because this must be the
204 * last fetch function.
206 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
207 free_deref_fetch_param(data
->orig
.data
);
208 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
209 free_symbol_cache(data
->orig
.data
);
214 static const struct fetch_type
*find_fetch_type(const char *type
,
215 const struct fetch_type
*ftbl
)
220 type
= DEFAULT_FETCH_TYPE_STR
;
222 /* Special case: bitfield */
226 type
= strchr(type
, '/');
231 if (kstrtoul(type
, 0, &bs
))
236 return find_fetch_type("u8", ftbl
);
238 return find_fetch_type("u16", ftbl
);
240 return find_fetch_type("u32", ftbl
);
242 return find_fetch_type("u64", ftbl
);
248 for (i
= 0; ftbl
[i
].name
; i
++) {
249 if (strcmp(type
, ftbl
[i
].name
) == 0)
257 /* Special function : only accept unsigned long */
258 static __kprobes
void fetch_kernel_stack_address(struct pt_regs
*regs
,
259 void *dummy
, void *dest
)
261 *(unsigned long *)dest
= kernel_stack_pointer(regs
);
264 static __kprobes
void fetch_user_stack_address(struct pt_regs
*regs
,
265 void *dummy
, void *dest
)
267 *(unsigned long *)dest
= user_stack_pointer(regs
);
270 static fetch_func_t
get_fetch_size_function(const struct fetch_type
*type
,
271 fetch_func_t orig_fn
,
272 const struct fetch_type
*ftbl
)
276 if (type
!= &ftbl
[FETCH_TYPE_STRING
])
277 return NULL
; /* Only string type needs size function */
279 for (i
= 0; i
< FETCH_MTD_END
; i
++)
280 if (type
->fetch
[i
] == orig_fn
)
281 return ftbl
[FETCH_TYPE_STRSIZE
].fetch
[i
];
283 WARN_ON(1); /* This should not happen */
288 /* Split symbol and offset. */
289 int traceprobe_split_symbol_offset(char *symbol
, unsigned long *offset
)
297 tmp
= strchr(symbol
, '+');
299 /* skip sign because kstrtoul doesn't accept '+' */
300 ret
= kstrtoul(tmp
+ 1, 0, offset
);
311 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
313 static int parse_probe_vars(char *arg
, const struct fetch_type
*t
,
314 struct fetch_param
*f
, bool is_return
,
320 if (strcmp(arg
, "retval") == 0) {
322 f
->fn
= t
->fetch
[FETCH_MTD_retval
];
325 } else if (strncmp(arg
, "stack", 5) == 0) {
326 if (arg
[5] == '\0') {
327 if (strcmp(t
->name
, DEFAULT_FETCH_TYPE_STR
))
331 f
->fn
= fetch_kernel_stack_address
;
333 f
->fn
= fetch_user_stack_address
;
334 } else if (isdigit(arg
[5])) {
335 ret
= kstrtoul(arg
+ 5, 10, ¶m
);
336 if (ret
|| (is_kprobe
&& param
> PARAM_MAX_STACK
))
339 f
->fn
= t
->fetch
[FETCH_MTD_stack
];
340 f
->data
= (void *)param
;
350 /* Recursive argument parser */
351 static int parse_probe_arg(char *arg
, const struct fetch_type
*t
,
352 struct fetch_param
*f
, bool is_return
, bool is_kprobe
)
354 const struct fetch_type
*ftbl
;
360 ftbl
= is_kprobe
? kprobes_fetch_type_table
: uprobes_fetch_type_table
;
361 BUG_ON(ftbl
== NULL
);
365 ret
= parse_probe_vars(arg
+ 1, t
, f
, is_return
, is_kprobe
);
368 case '%': /* named register */
369 ret
= regs_query_register_offset(arg
+ 1);
371 f
->fn
= t
->fetch
[FETCH_MTD_reg
];
372 f
->data
= (void *)(unsigned long)ret
;
377 case '@': /* memory, file-offset or symbol */
378 if (isdigit(arg
[1])) {
379 ret
= kstrtoul(arg
+ 1, 0, ¶m
);
383 f
->fn
= t
->fetch
[FETCH_MTD_memory
];
384 f
->data
= (void *)param
;
385 } else if (arg
[1] == '+') {
386 /* kprobes don't support file offsets */
390 ret
= kstrtol(arg
+ 2, 0, &offset
);
394 f
->fn
= t
->fetch
[FETCH_MTD_file_offset
];
395 f
->data
= (void *)offset
;
397 /* uprobes don't support symbols */
401 ret
= traceprobe_split_symbol_offset(arg
+ 1, &offset
);
405 f
->data
= alloc_symbol_cache(arg
+ 1, offset
);
407 f
->fn
= t
->fetch
[FETCH_MTD_symbol
];
411 case '+': /* deref memory */
412 arg
++; /* Skip '+', because kstrtol() rejects it. */
414 tmp
= strchr(arg
, '(');
419 ret
= kstrtol(arg
, 0, &offset
);
425 tmp
= strrchr(arg
, ')');
428 struct deref_fetch_param
*dprm
;
429 const struct fetch_type
*t2
;
431 t2
= find_fetch_type(NULL
, ftbl
);
433 dprm
= kzalloc(sizeof(struct deref_fetch_param
), GFP_KERNEL
);
438 dprm
->offset
= offset
;
439 dprm
->fetch
= t
->fetch
[FETCH_MTD_memory
];
440 dprm
->fetch_size
= get_fetch_size_function(t
,
442 ret
= parse_probe_arg(arg
, t2
, &dprm
->orig
, is_return
,
447 f
->fn
= t
->fetch
[FETCH_MTD_deref
];
448 f
->data
= (void *)dprm
;
453 if (!ret
&& !f
->fn
) { /* Parsed, but do not find fetch method */
454 pr_info("%s type has no corresponding fetch method.\n", t
->name
);
461 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
463 /* Bitfield type needs to be parsed into a fetch function */
464 static int __parse_bitfield_probe_arg(const char *bf
,
465 const struct fetch_type
*t
,
466 struct fetch_param
*f
)
468 struct bitfield_fetch_param
*bprm
;
469 unsigned long bw
, bo
;
475 bprm
= kzalloc(sizeof(*bprm
), GFP_KERNEL
);
480 f
->fn
= t
->fetch
[FETCH_MTD_bitfield
];
481 f
->data
= (void *)bprm
;
482 bw
= simple_strtoul(bf
+ 1, &tail
, 0); /* Use simple one */
484 if (bw
== 0 || *tail
!= '@')
488 bo
= simple_strtoul(bf
, &tail
, 0);
490 if (tail
== bf
|| *tail
!= '/')
493 bprm
->hi_shift
= BYTES_TO_BITS(t
->size
) - (bw
+ bo
);
494 bprm
->low_shift
= bprm
->hi_shift
+ bo
;
496 return (BYTES_TO_BITS(t
->size
) < (bw
+ bo
)) ? -EINVAL
: 0;
499 /* String length checking wrapper */
500 int traceprobe_parse_probe_arg(char *arg
, ssize_t
*size
,
501 struct probe_arg
*parg
, bool is_return
, bool is_kprobe
)
503 const struct fetch_type
*ftbl
;
507 ftbl
= is_kprobe
? kprobes_fetch_type_table
: uprobes_fetch_type_table
;
508 BUG_ON(ftbl
== NULL
);
510 if (strlen(arg
) > MAX_ARGSTR_LEN
) {
511 pr_info("Argument is too long.: %s\n", arg
);
514 parg
->comm
= kstrdup(arg
, GFP_KERNEL
);
516 pr_info("Failed to allocate memory for command '%s'.\n", arg
);
519 t
= strchr(parg
->comm
, ':');
521 arg
[t
- parg
->comm
] = '\0';
524 parg
->type
= find_fetch_type(t
, ftbl
);
526 pr_info("Unsupported type: %s\n", t
);
529 parg
->offset
= *size
;
530 *size
+= parg
->type
->size
;
531 ret
= parse_probe_arg(arg
, parg
->type
, &parg
->fetch
, is_return
, is_kprobe
);
533 if (ret
>= 0 && t
!= NULL
)
534 ret
= __parse_bitfield_probe_arg(t
, parg
->type
, &parg
->fetch
);
537 parg
->fetch_size
.fn
= get_fetch_size_function(parg
->type
,
540 parg
->fetch_size
.data
= parg
->fetch
.data
;
546 /* Return 1 if name is reserved or already used by another argument */
547 int traceprobe_conflict_field_name(const char *name
,
548 struct probe_arg
*args
, int narg
)
552 for (i
= 0; i
< ARRAY_SIZE(reserved_field_names
); i
++)
553 if (strcmp(reserved_field_names
[i
], name
) == 0)
556 for (i
= 0; i
< narg
; i
++)
557 if (strcmp(args
[i
].name
, name
) == 0)
563 void traceprobe_update_arg(struct probe_arg
*arg
)
565 if (CHECK_FETCH_FUNCS(bitfield
, arg
->fetch
.fn
))
566 update_bitfield_fetch_param(arg
->fetch
.data
);
567 else if (CHECK_FETCH_FUNCS(deref
, arg
->fetch
.fn
))
568 update_deref_fetch_param(arg
->fetch
.data
);
569 else if (CHECK_FETCH_FUNCS(symbol
, arg
->fetch
.fn
))
570 update_symbol_cache(arg
->fetch
.data
);
573 void traceprobe_free_probe_arg(struct probe_arg
*arg
)
575 if (CHECK_FETCH_FUNCS(bitfield
, arg
->fetch
.fn
))
576 free_bitfield_fetch_param(arg
->fetch
.data
);
577 else if (CHECK_FETCH_FUNCS(deref
, arg
->fetch
.fn
))
578 free_deref_fetch_param(arg
->fetch
.data
);
579 else if (CHECK_FETCH_FUNCS(symbol
, arg
->fetch
.fn
))
580 free_symbol_cache(arg
->fetch
.data
);
586 int traceprobe_command(const char *buf
, int (*createfn
)(int, char **))
593 argv
= argv_split(GFP_KERNEL
, buf
, &argc
);
598 ret
= createfn(argc
, argv
);
605 #define WRITE_BUFSIZE 4096
607 ssize_t
traceprobe_probes_write(struct file
*file
, const char __user
*buffer
,
608 size_t count
, loff_t
*ppos
,
609 int (*createfn
)(int, char **))
616 kbuf
= kmalloc(WRITE_BUFSIZE
, GFP_KERNEL
);
620 while (done
< count
) {
623 if (size
>= WRITE_BUFSIZE
)
624 size
= WRITE_BUFSIZE
- 1;
626 if (copy_from_user(kbuf
, buffer
+ done
, size
)) {
631 tmp
= strchr(kbuf
, '\n');
635 size
= tmp
- kbuf
+ 1;
636 } else if (done
+ size
< count
) {
637 pr_warning("Line length is too long: "
638 "Should be less than %d.", WRITE_BUFSIZE
);
643 /* Remove comments */
644 tmp
= strchr(kbuf
, '#');
649 ret
= traceprobe_command(kbuf
, createfn
);
661 static int __set_print_fmt(struct trace_probe
*tp
, char *buf
, int len
,
667 const char *fmt
, *arg
;
671 arg
= "REC->" FIELD_STRING_IP
;
673 fmt
= "(%lx <- %lx)";
674 arg
= "REC->" FIELD_STRING_FUNC
", REC->" FIELD_STRING_RETIP
;
677 /* When len=0, we just calculate the needed length */
678 #define LEN_OR_ZERO (len ? len - pos : 0)
680 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"%s", fmt
);
682 for (i
= 0; i
< tp
->nr_args
; i
++) {
683 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, " %s=%s",
684 tp
->args
[i
].name
, tp
->args
[i
].type
->fmt
);
687 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\", %s", arg
);
689 for (i
= 0; i
< tp
->nr_args
; i
++) {
690 if (strcmp(tp
->args
[i
].type
->name
, "string") == 0)
691 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
695 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, ", REC->%s",
701 /* return the length of print_fmt */
705 int set_print_fmt(struct trace_probe
*tp
, bool is_return
)
710 /* First: called with 0 length to calculate the needed length */
711 len
= __set_print_fmt(tp
, NULL
, 0, is_return
);
712 print_fmt
= kmalloc(len
+ 1, GFP_KERNEL
);
716 /* Second: actually write the @print_fmt */
717 __set_print_fmt(tp
, print_fmt
, len
+ 1, is_return
);
718 tp
->call
.print_fmt
= print_fmt
;