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(tname, type, fmt) \
40 int PRINT_TYPE_FUNC_NAME(tname)(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(tname)[] = fmt; \
47 NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(tname));
49 DEFINE_BASIC_PRINT_TYPE_FUNC(u8
, u8
, "%u")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(u16
, u16
, "%u")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(u32
, u32
, "%u")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(u64
, u64
, "%Lu")
53 DEFINE_BASIC_PRINT_TYPE_FUNC(s8
, s8
, "%d")
54 DEFINE_BASIC_PRINT_TYPE_FUNC(s16
, s16
, "%d")
55 DEFINE_BASIC_PRINT_TYPE_FUNC(s32
, s32
, "%d")
56 DEFINE_BASIC_PRINT_TYPE_FUNC(s64
, s64
, "%Ld")
57 DEFINE_BASIC_PRINT_TYPE_FUNC(x8
, u8
, "0x%x")
58 DEFINE_BASIC_PRINT_TYPE_FUNC(x16
, u16
, "0x%x")
59 DEFINE_BASIC_PRINT_TYPE_FUNC(x32
, u32
, "0x%x")
60 DEFINE_BASIC_PRINT_TYPE_FUNC(x64
, u64
, "0x%Lx")
62 /* Print type function for string type */
63 int PRINT_TYPE_FUNC_NAME(string
)(struct trace_seq
*s
, const char *name
,
64 void *data
, void *ent
)
66 int len
= *(u32
*)data
>> 16;
69 trace_seq_printf(s
, " %s=(fault)", name
);
71 trace_seq_printf(s
, " %s=\"%s\"", name
,
72 (const char *)get_loc_data(data
, ent
));
73 return !trace_seq_has_overflowed(s
);
75 NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(string
));
77 const char PRINT_TYPE_FMT_NAME(string
)[] = "\\\"%s\\\"";
79 #define CHECK_FETCH_FUNCS(method, fn) \
80 (((FETCH_FUNC_NAME(method, u8) == fn) || \
81 (FETCH_FUNC_NAME(method, u16) == fn) || \
82 (FETCH_FUNC_NAME(method, u32) == fn) || \
83 (FETCH_FUNC_NAME(method, u64) == fn) || \
84 (FETCH_FUNC_NAME(method, string) == fn) || \
85 (FETCH_FUNC_NAME(method, string_size) == fn)) \
88 /* Data fetch function templates */
89 #define DEFINE_FETCH_reg(type) \
90 void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, void *offset, void *dest) \
92 *(type *)dest = (type)regs_get_register(regs, \
93 (unsigned int)((unsigned long)offset)); \
95 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(reg, type));
96 DEFINE_BASIC_FETCH_FUNCS(reg
)
97 /* No string on the register */
98 #define fetch_reg_string NULL
99 #define fetch_reg_string_size NULL
101 #define DEFINE_FETCH_retval(type) \
102 void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs, \
103 void *dummy, void *dest) \
105 *(type *)dest = (type)regs_return_value(regs); \
107 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(retval, type));
108 DEFINE_BASIC_FETCH_FUNCS(retval
)
109 /* No string on the retval */
110 #define fetch_retval_string NULL
111 #define fetch_retval_string_size NULL
113 /* Dereference memory access function */
114 struct deref_fetch_param
{
115 struct fetch_param orig
;
118 fetch_func_t fetch_size
;
121 #define DEFINE_FETCH_deref(type) \
122 void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs, \
123 void *data, void *dest) \
125 struct deref_fetch_param *dprm = data; \
126 unsigned long addr; \
127 call_fetch(&dprm->orig, regs, &addr); \
129 addr += dprm->offset; \
130 dprm->fetch(regs, (void *)addr, dest); \
134 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref, type));
135 DEFINE_BASIC_FETCH_FUNCS(deref
)
136 DEFINE_FETCH_deref(string
)
138 void FETCH_FUNC_NAME(deref
, string_size
)(struct pt_regs
*regs
,
139 void *data
, void *dest
)
141 struct deref_fetch_param
*dprm
= data
;
144 call_fetch(&dprm
->orig
, regs
, &addr
);
145 if (addr
&& dprm
->fetch_size
) {
146 addr
+= dprm
->offset
;
147 dprm
->fetch_size(regs
, (void *)addr
, dest
);
149 *(string_size
*)dest
= 0;
151 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref
, string_size
));
153 static void update_deref_fetch_param(struct deref_fetch_param
*data
)
155 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
156 update_deref_fetch_param(data
->orig
.data
);
157 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
158 update_symbol_cache(data
->orig
.data
);
160 NOKPROBE_SYMBOL(update_deref_fetch_param
);
162 static void free_deref_fetch_param(struct deref_fetch_param
*data
)
164 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
165 free_deref_fetch_param(data
->orig
.data
);
166 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
167 free_symbol_cache(data
->orig
.data
);
170 NOKPROBE_SYMBOL(free_deref_fetch_param
);
172 /* Bitfield fetch function */
173 struct bitfield_fetch_param
{
174 struct fetch_param orig
;
175 unsigned char hi_shift
;
176 unsigned char low_shift
;
179 #define DEFINE_FETCH_bitfield(type) \
180 void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs, \
181 void *data, void *dest) \
183 struct bitfield_fetch_param *bprm = data; \
185 call_fetch(&bprm->orig, regs, &buf); \
187 buf <<= bprm->hi_shift; \
188 buf >>= bprm->low_shift; \
190 *(type *)dest = buf; \
192 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(bitfield, type));
193 DEFINE_BASIC_FETCH_FUNCS(bitfield
)
194 #define fetch_bitfield_string NULL
195 #define fetch_bitfield_string_size NULL
198 update_bitfield_fetch_param(struct bitfield_fetch_param
*data
)
201 * Don't check the bitfield itself, because this must be the
202 * last fetch function.
204 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
205 update_deref_fetch_param(data
->orig
.data
);
206 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
207 update_symbol_cache(data
->orig
.data
);
211 free_bitfield_fetch_param(struct bitfield_fetch_param
*data
)
214 * Don't check the bitfield itself, because this must be the
215 * last fetch function.
217 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
218 free_deref_fetch_param(data
->orig
.data
);
219 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
220 free_symbol_cache(data
->orig
.data
);
225 void FETCH_FUNC_NAME(comm
, string
)(struct pt_regs
*regs
,
226 void *data
, void *dest
)
228 int maxlen
= get_rloc_len(*(u32
*)dest
);
229 u8
*dst
= get_rloc_data(dest
);
235 ret
= strlcpy(dst
, current
->comm
, maxlen
);
236 *(u32
*)dest
= make_data_rloc(ret
, get_rloc_offs(*(u32
*)dest
));
238 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(comm
, string
));
240 void FETCH_FUNC_NAME(comm
, string_size
)(struct pt_regs
*regs
,
241 void *data
, void *dest
)
243 *(u32
*)dest
= strlen(current
->comm
) + 1;
245 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(comm
, string_size
));
247 static const struct fetch_type
*find_fetch_type(const char *type
,
248 const struct fetch_type
*ftbl
)
253 type
= DEFAULT_FETCH_TYPE_STR
;
255 /* Special case: bitfield */
259 type
= strchr(type
, '/');
264 if (kstrtoul(type
, 0, &bs
))
269 return find_fetch_type("u8", ftbl
);
271 return find_fetch_type("u16", ftbl
);
273 return find_fetch_type("u32", ftbl
);
275 return find_fetch_type("u64", ftbl
);
281 for (i
= 0; ftbl
[i
].name
; i
++) {
282 if (strcmp(type
, ftbl
[i
].name
) == 0)
290 /* Special function : only accept unsigned long */
291 static void fetch_kernel_stack_address(struct pt_regs
*regs
, void *dummy
, void *dest
)
293 *(unsigned long *)dest
= kernel_stack_pointer(regs
);
295 NOKPROBE_SYMBOL(fetch_kernel_stack_address
);
297 static void fetch_user_stack_address(struct pt_regs
*regs
, void *dummy
, void *dest
)
299 *(unsigned long *)dest
= user_stack_pointer(regs
);
301 NOKPROBE_SYMBOL(fetch_user_stack_address
);
303 static fetch_func_t
get_fetch_size_function(const struct fetch_type
*type
,
304 fetch_func_t orig_fn
,
305 const struct fetch_type
*ftbl
)
309 if (type
!= &ftbl
[FETCH_TYPE_STRING
])
310 return NULL
; /* Only string type needs size function */
312 for (i
= 0; i
< FETCH_MTD_END
; i
++)
313 if (type
->fetch
[i
] == orig_fn
)
314 return ftbl
[FETCH_TYPE_STRSIZE
].fetch
[i
];
316 WARN_ON(1); /* This should not happen */
321 /* Split symbol and offset. */
322 int traceprobe_split_symbol_offset(char *symbol
, unsigned long *offset
)
330 tmp
= strchr(symbol
, '+');
332 /* skip sign because kstrtoul doesn't accept '+' */
333 ret
= kstrtoul(tmp
+ 1, 0, offset
);
344 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
346 static int parse_probe_vars(char *arg
, const struct fetch_type
*t
,
347 struct fetch_param
*f
, bool is_return
,
353 if (strcmp(arg
, "retval") == 0) {
355 f
->fn
= t
->fetch
[FETCH_MTD_retval
];
358 } else if (strncmp(arg
, "stack", 5) == 0) {
359 if (arg
[5] == '\0') {
360 if (strcmp(t
->name
, DEFAULT_FETCH_TYPE_STR
))
364 f
->fn
= fetch_kernel_stack_address
;
366 f
->fn
= fetch_user_stack_address
;
367 } else if (isdigit(arg
[5])) {
368 ret
= kstrtoul(arg
+ 5, 10, ¶m
);
369 if (ret
|| (is_kprobe
&& param
> PARAM_MAX_STACK
))
372 f
->fn
= t
->fetch
[FETCH_MTD_stack
];
373 f
->data
= (void *)param
;
377 } else if (strcmp(arg
, "comm") == 0) {
378 if (strcmp(t
->name
, "string") != 0 &&
379 strcmp(t
->name
, "string_size") != 0)
381 f
->fn
= t
->fetch
[FETCH_MTD_comm
];
388 /* Recursive argument parser */
389 static int parse_probe_arg(char *arg
, const struct fetch_type
*t
,
390 struct fetch_param
*f
, bool is_return
, bool is_kprobe
,
391 const struct fetch_type
*ftbl
)
400 ret
= parse_probe_vars(arg
+ 1, t
, f
, is_return
, is_kprobe
);
403 case '%': /* named register */
404 ret
= regs_query_register_offset(arg
+ 1);
406 f
->fn
= t
->fetch
[FETCH_MTD_reg
];
407 f
->data
= (void *)(unsigned long)ret
;
412 case '@': /* memory, file-offset or symbol */
413 if (isdigit(arg
[1])) {
414 ret
= kstrtoul(arg
+ 1, 0, ¶m
);
418 f
->fn
= t
->fetch
[FETCH_MTD_memory
];
419 f
->data
= (void *)param
;
420 } else if (arg
[1] == '+') {
421 /* kprobes don't support file offsets */
425 ret
= kstrtol(arg
+ 2, 0, &offset
);
429 f
->fn
= t
->fetch
[FETCH_MTD_file_offset
];
430 f
->data
= (void *)offset
;
432 /* uprobes don't support symbols */
436 ret
= traceprobe_split_symbol_offset(arg
+ 1, &offset
);
440 f
->data
= alloc_symbol_cache(arg
+ 1, offset
);
442 f
->fn
= t
->fetch
[FETCH_MTD_symbol
];
446 case '+': /* deref memory */
447 arg
++; /* Skip '+', because kstrtol() rejects it. */
449 tmp
= strchr(arg
, '(');
454 ret
= kstrtol(arg
, 0, &offset
);
460 tmp
= strrchr(arg
, ')');
463 struct deref_fetch_param
*dprm
;
464 const struct fetch_type
*t2
;
466 t2
= find_fetch_type(NULL
, ftbl
);
468 dprm
= kzalloc(sizeof(struct deref_fetch_param
), GFP_KERNEL
);
473 dprm
->offset
= offset
;
474 dprm
->fetch
= t
->fetch
[FETCH_MTD_memory
];
475 dprm
->fetch_size
= get_fetch_size_function(t
,
477 ret
= parse_probe_arg(arg
, t2
, &dprm
->orig
, is_return
,
482 f
->fn
= t
->fetch
[FETCH_MTD_deref
];
483 f
->data
= (void *)dprm
;
488 if (!ret
&& !f
->fn
) { /* Parsed, but do not find fetch method */
489 pr_info("%s type has no corresponding fetch method.\n", t
->name
);
496 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
498 /* Bitfield type needs to be parsed into a fetch function */
499 static int __parse_bitfield_probe_arg(const char *bf
,
500 const struct fetch_type
*t
,
501 struct fetch_param
*f
)
503 struct bitfield_fetch_param
*bprm
;
504 unsigned long bw
, bo
;
510 bprm
= kzalloc(sizeof(*bprm
), GFP_KERNEL
);
515 f
->fn
= t
->fetch
[FETCH_MTD_bitfield
];
516 f
->data
= (void *)bprm
;
517 bw
= simple_strtoul(bf
+ 1, &tail
, 0); /* Use simple one */
519 if (bw
== 0 || *tail
!= '@')
523 bo
= simple_strtoul(bf
, &tail
, 0);
525 if (tail
== bf
|| *tail
!= '/')
528 bprm
->hi_shift
= BYTES_TO_BITS(t
->size
) - (bw
+ bo
);
529 bprm
->low_shift
= bprm
->hi_shift
+ bo
;
531 return (BYTES_TO_BITS(t
->size
) < (bw
+ bo
)) ? -EINVAL
: 0;
534 /* String length checking wrapper */
535 int traceprobe_parse_probe_arg(char *arg
, ssize_t
*size
,
536 struct probe_arg
*parg
, bool is_return
, bool is_kprobe
,
537 const struct fetch_type
*ftbl
)
542 if (strlen(arg
) > MAX_ARGSTR_LEN
) {
543 pr_info("Argument is too long.: %s\n", arg
);
546 parg
->comm
= kstrdup(arg
, GFP_KERNEL
);
548 pr_info("Failed to allocate memory for command '%s'.\n", arg
);
551 t
= strchr(parg
->comm
, ':');
553 arg
[t
- parg
->comm
] = '\0';
557 * The default type of $comm should be "string", and it can't be
560 if (!t
&& strcmp(arg
, "$comm") == 0)
562 parg
->type
= find_fetch_type(t
, ftbl
);
564 pr_info("Unsupported type: %s\n", t
);
567 parg
->offset
= *size
;
568 *size
+= parg
->type
->size
;
569 ret
= parse_probe_arg(arg
, parg
->type
, &parg
->fetch
, is_return
,
572 if (ret
>= 0 && t
!= NULL
)
573 ret
= __parse_bitfield_probe_arg(t
, parg
->type
, &parg
->fetch
);
576 parg
->fetch_size
.fn
= get_fetch_size_function(parg
->type
,
579 parg
->fetch_size
.data
= parg
->fetch
.data
;
585 /* Return 1 if name is reserved or already used by another argument */
586 int traceprobe_conflict_field_name(const char *name
,
587 struct probe_arg
*args
, int narg
)
591 for (i
= 0; i
< ARRAY_SIZE(reserved_field_names
); i
++)
592 if (strcmp(reserved_field_names
[i
], name
) == 0)
595 for (i
= 0; i
< narg
; i
++)
596 if (strcmp(args
[i
].name
, name
) == 0)
602 void traceprobe_update_arg(struct probe_arg
*arg
)
604 if (CHECK_FETCH_FUNCS(bitfield
, arg
->fetch
.fn
))
605 update_bitfield_fetch_param(arg
->fetch
.data
);
606 else if (CHECK_FETCH_FUNCS(deref
, arg
->fetch
.fn
))
607 update_deref_fetch_param(arg
->fetch
.data
);
608 else if (CHECK_FETCH_FUNCS(symbol
, arg
->fetch
.fn
))
609 update_symbol_cache(arg
->fetch
.data
);
612 void traceprobe_free_probe_arg(struct probe_arg
*arg
)
614 if (CHECK_FETCH_FUNCS(bitfield
, arg
->fetch
.fn
))
615 free_bitfield_fetch_param(arg
->fetch
.data
);
616 else if (CHECK_FETCH_FUNCS(deref
, arg
->fetch
.fn
))
617 free_deref_fetch_param(arg
->fetch
.data
);
618 else if (CHECK_FETCH_FUNCS(symbol
, arg
->fetch
.fn
))
619 free_symbol_cache(arg
->fetch
.data
);
625 int traceprobe_command(const char *buf
, int (*createfn
)(int, char **))
632 argv
= argv_split(GFP_KERNEL
, buf
, &argc
);
637 ret
= createfn(argc
, argv
);
644 #define WRITE_BUFSIZE 4096
646 ssize_t
traceprobe_probes_write(struct file
*file
, const char __user
*buffer
,
647 size_t count
, loff_t
*ppos
,
648 int (*createfn
)(int, char **))
655 kbuf
= kmalloc(WRITE_BUFSIZE
, GFP_KERNEL
);
659 while (done
< count
) {
662 if (size
>= WRITE_BUFSIZE
)
663 size
= WRITE_BUFSIZE
- 1;
665 if (copy_from_user(kbuf
, buffer
+ done
, size
)) {
670 tmp
= strchr(kbuf
, '\n');
674 size
= tmp
- kbuf
+ 1;
675 } else if (done
+ size
< count
) {
676 pr_warn("Line length is too long: Should be less than %d\n",
682 /* Remove comments */
683 tmp
= strchr(kbuf
, '#');
688 ret
= traceprobe_command(kbuf
, createfn
);
700 static int __set_print_fmt(struct trace_probe
*tp
, char *buf
, int len
,
706 const char *fmt
, *arg
;
710 arg
= "REC->" FIELD_STRING_IP
;
712 fmt
= "(%lx <- %lx)";
713 arg
= "REC->" FIELD_STRING_FUNC
", REC->" FIELD_STRING_RETIP
;
716 /* When len=0, we just calculate the needed length */
717 #define LEN_OR_ZERO (len ? len - pos : 0)
719 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"%s", fmt
);
721 for (i
= 0; i
< tp
->nr_args
; i
++) {
722 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, " %s=%s",
723 tp
->args
[i
].name
, tp
->args
[i
].type
->fmt
);
726 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\", %s", arg
);
728 for (i
= 0; i
< tp
->nr_args
; i
++) {
729 if (strcmp(tp
->args
[i
].type
->name
, "string") == 0)
730 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
734 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, ", REC->%s",
740 /* return the length of print_fmt */
744 int set_print_fmt(struct trace_probe
*tp
, bool is_return
)
749 /* First: called with 0 length to calculate the needed length */
750 len
= __set_print_fmt(tp
, NULL
, 0, is_return
);
751 print_fmt
= kmalloc(len
+ 1, GFP_KERNEL
);
755 /* Second: actually write the @print_fmt */
756 __set_print_fmt(tp
, print_fmt
, len
+ 1, is_return
);
757 tp
->call
.print_fmt
= print_fmt
;