1 // SPDX-License-Identifier: GPL-2.0
3 * Common code for probe-based Dynamic events.
5 * This code was copied from kernel/trace/trace_kprobe.c written by
6 * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
8 * Updates to make this generic:
9 * Copyright (C) IBM Corporation, 2010-2011
10 * Author: Srikar Dronamraju
12 #define pr_fmt(fmt) "trace_probe: " fmt
14 #include "trace_probe.h"
16 const char *reserved_field_names
[] = {
19 "common_preempt_count",
27 /* Printing in basic type function template */
28 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
29 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, const char *name, \
30 void *data, void *ent) \
32 trace_seq_printf(s, " %s=" fmt, name, *(type *)data); \
33 return !trace_seq_has_overflowed(s); \
35 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt; \
36 NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(tname));
38 DEFINE_BASIC_PRINT_TYPE_FUNC(u8
, u8
, "%u")
39 DEFINE_BASIC_PRINT_TYPE_FUNC(u16
, u16
, "%u")
40 DEFINE_BASIC_PRINT_TYPE_FUNC(u32
, u32
, "%u")
41 DEFINE_BASIC_PRINT_TYPE_FUNC(u64
, u64
, "%Lu")
42 DEFINE_BASIC_PRINT_TYPE_FUNC(s8
, s8
, "%d")
43 DEFINE_BASIC_PRINT_TYPE_FUNC(s16
, s16
, "%d")
44 DEFINE_BASIC_PRINT_TYPE_FUNC(s32
, s32
, "%d")
45 DEFINE_BASIC_PRINT_TYPE_FUNC(s64
, s64
, "%Ld")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(x8
, u8
, "0x%x")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(x16
, u16
, "0x%x")
48 DEFINE_BASIC_PRINT_TYPE_FUNC(x32
, u32
, "0x%x")
49 DEFINE_BASIC_PRINT_TYPE_FUNC(x64
, u64
, "0x%Lx")
51 /* Print type function for string type */
52 int PRINT_TYPE_FUNC_NAME(string
)(struct trace_seq
*s
, const char *name
,
53 void *data
, void *ent
)
55 int len
= *(u32
*)data
>> 16;
58 trace_seq_printf(s
, " %s=(fault)", name
);
60 trace_seq_printf(s
, " %s=\"%s\"", name
,
61 (const char *)get_loc_data(data
, ent
));
62 return !trace_seq_has_overflowed(s
);
64 NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(string
));
66 const char PRINT_TYPE_FMT_NAME(string
)[] = "\\\"%s\\\"";
68 #define CHECK_FETCH_FUNCS(method, fn) \
69 (((FETCH_FUNC_NAME(method, u8) == fn) || \
70 (FETCH_FUNC_NAME(method, u16) == fn) || \
71 (FETCH_FUNC_NAME(method, u32) == fn) || \
72 (FETCH_FUNC_NAME(method, u64) == fn) || \
73 (FETCH_FUNC_NAME(method, string) == fn) || \
74 (FETCH_FUNC_NAME(method, string_size) == fn)) \
77 /* Data fetch function templates */
78 #define DEFINE_FETCH_reg(type) \
79 void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, void *offset, void *dest) \
81 *(type *)dest = (type)regs_get_register(regs, \
82 (unsigned int)((unsigned long)offset)); \
84 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(reg, type));
85 DEFINE_BASIC_FETCH_FUNCS(reg
)
86 /* No string on the register */
87 #define fetch_reg_string NULL
88 #define fetch_reg_string_size NULL
90 #define DEFINE_FETCH_retval(type) \
91 void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs, \
92 void *dummy, void *dest) \
94 *(type *)dest = (type)regs_return_value(regs); \
96 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(retval, type));
97 DEFINE_BASIC_FETCH_FUNCS(retval
)
98 /* No string on the retval */
99 #define fetch_retval_string NULL
100 #define fetch_retval_string_size NULL
102 /* Dereference memory access function */
103 struct deref_fetch_param
{
104 struct fetch_param orig
;
107 fetch_func_t fetch_size
;
110 #define DEFINE_FETCH_deref(type) \
111 void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs, \
112 void *data, void *dest) \
114 struct deref_fetch_param *dprm = data; \
115 unsigned long addr; \
116 call_fetch(&dprm->orig, regs, &addr); \
118 addr += dprm->offset; \
119 dprm->fetch(regs, (void *)addr, dest); \
123 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref, type));
124 DEFINE_BASIC_FETCH_FUNCS(deref
)
125 DEFINE_FETCH_deref(string
)
127 void FETCH_FUNC_NAME(deref
, string_size
)(struct pt_regs
*regs
,
128 void *data
, void *dest
)
130 struct deref_fetch_param
*dprm
= data
;
133 call_fetch(&dprm
->orig
, regs
, &addr
);
134 if (addr
&& dprm
->fetch_size
) {
135 addr
+= dprm
->offset
;
136 dprm
->fetch_size(regs
, (void *)addr
, dest
);
138 *(string_size
*)dest
= 0;
140 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref
, string_size
));
142 static void update_deref_fetch_param(struct deref_fetch_param
*data
)
144 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
145 update_deref_fetch_param(data
->orig
.data
);
146 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
147 update_symbol_cache(data
->orig
.data
);
149 NOKPROBE_SYMBOL(update_deref_fetch_param
);
151 static void free_deref_fetch_param(struct deref_fetch_param
*data
)
153 if (CHECK_FETCH_FUNCS(deref
, data
->orig
.fn
))
154 free_deref_fetch_param(data
->orig
.data
);
155 else if (CHECK_FETCH_FUNCS(symbol
, data
->orig
.fn
))
156 free_symbol_cache(data
->orig
.data
);
159 NOKPROBE_SYMBOL(free_deref_fetch_param
);
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 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; \
181 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(bitfield, type));
182 DEFINE_BASIC_FETCH_FUNCS(bitfield
)
183 #define fetch_bitfield_string NULL
184 #define fetch_bitfield_string_size NULL
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
);
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 void FETCH_FUNC_NAME(comm
, string
)(struct pt_regs
*regs
,
215 void *data
, void *dest
)
217 int maxlen
= get_rloc_len(*(u32
*)dest
);
218 u8
*dst
= get_rloc_data(dest
);
224 ret
= strlcpy(dst
, current
->comm
, maxlen
);
225 *(u32
*)dest
= make_data_rloc(ret
, get_rloc_offs(*(u32
*)dest
));
227 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(comm
, string
));
229 void FETCH_FUNC_NAME(comm
, string_size
)(struct pt_regs
*regs
,
230 void *data
, void *dest
)
232 *(u32
*)dest
= strlen(current
->comm
) + 1;
234 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(comm
, string_size
));
236 static const struct fetch_type
*find_fetch_type(const char *type
,
237 const struct fetch_type
*ftbl
)
242 type
= DEFAULT_FETCH_TYPE_STR
;
244 /* Special case: bitfield */
248 type
= strchr(type
, '/');
253 if (kstrtoul(type
, 0, &bs
))
258 return find_fetch_type("u8", ftbl
);
260 return find_fetch_type("u16", ftbl
);
262 return find_fetch_type("u32", ftbl
);
264 return find_fetch_type("u64", ftbl
);
270 for (i
= 0; ftbl
[i
].name
; i
++) {
271 if (strcmp(type
, ftbl
[i
].name
) == 0)
279 /* Special function : only accept unsigned long */
280 static void fetch_kernel_stack_address(struct pt_regs
*regs
, void *dummy
, void *dest
)
282 *(unsigned long *)dest
= kernel_stack_pointer(regs
);
284 NOKPROBE_SYMBOL(fetch_kernel_stack_address
);
286 static void fetch_user_stack_address(struct pt_regs
*regs
, void *dummy
, void *dest
)
288 *(unsigned long *)dest
= user_stack_pointer(regs
);
290 NOKPROBE_SYMBOL(fetch_user_stack_address
);
292 static fetch_func_t
get_fetch_size_function(const struct fetch_type
*type
,
293 fetch_func_t orig_fn
,
294 const struct fetch_type
*ftbl
)
298 if (type
!= &ftbl
[FETCH_TYPE_STRING
])
299 return NULL
; /* Only string type needs size function */
301 for (i
= 0; i
< FETCH_MTD_END
; i
++)
302 if (type
->fetch
[i
] == orig_fn
)
303 return ftbl
[FETCH_TYPE_STRSIZE
].fetch
[i
];
305 WARN_ON(1); /* This should not happen */
310 /* Split symbol and offset. */
311 int traceprobe_split_symbol_offset(char *symbol
, long *offset
)
319 tmp
= strpbrk(symbol
, "+-");
321 ret
= kstrtol(tmp
, 0, offset
);
331 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
333 static int parse_probe_vars(char *arg
, const struct fetch_type
*t
,
334 struct fetch_param
*f
, bool is_return
,
340 if (strcmp(arg
, "retval") == 0) {
342 f
->fn
= t
->fetch
[FETCH_MTD_retval
];
345 } else if (strncmp(arg
, "stack", 5) == 0) {
346 if (arg
[5] == '\0') {
347 if (strcmp(t
->name
, DEFAULT_FETCH_TYPE_STR
))
351 f
->fn
= fetch_kernel_stack_address
;
353 f
->fn
= fetch_user_stack_address
;
354 } else if (isdigit(arg
[5])) {
355 ret
= kstrtoul(arg
+ 5, 10, ¶m
);
356 if (ret
|| (is_kprobe
&& param
> PARAM_MAX_STACK
))
359 f
->fn
= t
->fetch
[FETCH_MTD_stack
];
360 f
->data
= (void *)param
;
364 } else if (strcmp(arg
, "comm") == 0) {
365 if (strcmp(t
->name
, "string") != 0 &&
366 strcmp(t
->name
, "string_size") != 0)
368 f
->fn
= t
->fetch
[FETCH_MTD_comm
];
375 /* Recursive argument parser */
376 static int parse_probe_arg(char *arg
, const struct fetch_type
*t
,
377 struct fetch_param
*f
, bool is_return
, bool is_kprobe
,
378 const struct fetch_type
*ftbl
)
387 ret
= parse_probe_vars(arg
+ 1, t
, f
, is_return
, is_kprobe
);
390 case '%': /* named register */
391 ret
= regs_query_register_offset(arg
+ 1);
393 f
->fn
= t
->fetch
[FETCH_MTD_reg
];
394 f
->data
= (void *)(unsigned long)ret
;
399 case '@': /* memory, file-offset or symbol */
400 if (isdigit(arg
[1])) {
401 ret
= kstrtoul(arg
+ 1, 0, ¶m
);
405 f
->fn
= t
->fetch
[FETCH_MTD_memory
];
406 f
->data
= (void *)param
;
407 } else if (arg
[1] == '+') {
408 /* kprobes don't support file offsets */
412 ret
= kstrtol(arg
+ 2, 0, &offset
);
416 f
->fn
= t
->fetch
[FETCH_MTD_file_offset
];
417 f
->data
= (void *)offset
;
419 /* uprobes don't support symbols */
423 ret
= traceprobe_split_symbol_offset(arg
+ 1, &offset
);
427 f
->data
= alloc_symbol_cache(arg
+ 1, offset
);
429 f
->fn
= t
->fetch
[FETCH_MTD_symbol
];
433 case '+': /* deref memory */
434 arg
++; /* Skip '+', because kstrtol() rejects it. */
436 tmp
= strchr(arg
, '(');
441 ret
= kstrtol(arg
, 0, &offset
);
447 tmp
= strrchr(arg
, ')');
450 struct deref_fetch_param
*dprm
;
451 const struct fetch_type
*t2
;
453 t2
= find_fetch_type(NULL
, ftbl
);
455 dprm
= kzalloc(sizeof(struct deref_fetch_param
), GFP_KERNEL
);
460 dprm
->offset
= offset
;
461 dprm
->fetch
= t
->fetch
[FETCH_MTD_memory
];
462 dprm
->fetch_size
= get_fetch_size_function(t
,
464 ret
= parse_probe_arg(arg
, t2
, &dprm
->orig
, is_return
,
469 f
->fn
= t
->fetch
[FETCH_MTD_deref
];
470 f
->data
= (void *)dprm
;
475 if (!ret
&& !f
->fn
) { /* Parsed, but do not find fetch method */
476 pr_info("%s type has no corresponding fetch method.\n", t
->name
);
483 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
485 /* Bitfield type needs to be parsed into a fetch function */
486 static int __parse_bitfield_probe_arg(const char *bf
,
487 const struct fetch_type
*t
,
488 struct fetch_param
*f
)
490 struct bitfield_fetch_param
*bprm
;
491 unsigned long bw
, bo
;
497 bprm
= kzalloc(sizeof(*bprm
), GFP_KERNEL
);
502 f
->fn
= t
->fetch
[FETCH_MTD_bitfield
];
503 f
->data
= (void *)bprm
;
504 bw
= simple_strtoul(bf
+ 1, &tail
, 0); /* Use simple one */
506 if (bw
== 0 || *tail
!= '@')
510 bo
= simple_strtoul(bf
, &tail
, 0);
512 if (tail
== bf
|| *tail
!= '/')
515 bprm
->hi_shift
= BYTES_TO_BITS(t
->size
) - (bw
+ bo
);
516 bprm
->low_shift
= bprm
->hi_shift
+ bo
;
518 return (BYTES_TO_BITS(t
->size
) < (bw
+ bo
)) ? -EINVAL
: 0;
521 /* String length checking wrapper */
522 int traceprobe_parse_probe_arg(char *arg
, ssize_t
*size
,
523 struct probe_arg
*parg
, bool is_return
, bool is_kprobe
,
524 const struct fetch_type
*ftbl
)
529 if (strlen(arg
) > MAX_ARGSTR_LEN
) {
530 pr_info("Argument is too long.: %s\n", arg
);
533 parg
->comm
= kstrdup(arg
, GFP_KERNEL
);
535 pr_info("Failed to allocate memory for command '%s'.\n", arg
);
538 t
= strchr(parg
->comm
, ':');
540 arg
[t
- parg
->comm
] = '\0';
544 * The default type of $comm should be "string", and it can't be
547 if (!t
&& strcmp(arg
, "$comm") == 0)
549 parg
->type
= find_fetch_type(t
, ftbl
);
551 pr_info("Unsupported type: %s\n", t
);
554 parg
->offset
= *size
;
555 *size
+= parg
->type
->size
;
556 ret
= parse_probe_arg(arg
, parg
->type
, &parg
->fetch
, is_return
,
559 if (ret
>= 0 && t
!= NULL
)
560 ret
= __parse_bitfield_probe_arg(t
, parg
->type
, &parg
->fetch
);
563 parg
->fetch_size
.fn
= get_fetch_size_function(parg
->type
,
566 parg
->fetch_size
.data
= parg
->fetch
.data
;
572 /* Return 1 if name is reserved or already used by another argument */
573 int traceprobe_conflict_field_name(const char *name
,
574 struct probe_arg
*args
, int narg
)
578 for (i
= 0; i
< ARRAY_SIZE(reserved_field_names
); i
++)
579 if (strcmp(reserved_field_names
[i
], name
) == 0)
582 for (i
= 0; i
< narg
; i
++)
583 if (strcmp(args
[i
].name
, name
) == 0)
589 void traceprobe_update_arg(struct probe_arg
*arg
)
591 if (CHECK_FETCH_FUNCS(bitfield
, arg
->fetch
.fn
))
592 update_bitfield_fetch_param(arg
->fetch
.data
);
593 else if (CHECK_FETCH_FUNCS(deref
, arg
->fetch
.fn
))
594 update_deref_fetch_param(arg
->fetch
.data
);
595 else if (CHECK_FETCH_FUNCS(symbol
, arg
->fetch
.fn
))
596 update_symbol_cache(arg
->fetch
.data
);
599 void traceprobe_free_probe_arg(struct probe_arg
*arg
)
601 if (CHECK_FETCH_FUNCS(bitfield
, arg
->fetch
.fn
))
602 free_bitfield_fetch_param(arg
->fetch
.data
);
603 else if (CHECK_FETCH_FUNCS(deref
, arg
->fetch
.fn
))
604 free_deref_fetch_param(arg
->fetch
.data
);
605 else if (CHECK_FETCH_FUNCS(symbol
, arg
->fetch
.fn
))
606 free_symbol_cache(arg
->fetch
.data
);
612 static int __set_print_fmt(struct trace_probe
*tp
, char *buf
, int len
,
618 const char *fmt
, *arg
;
622 arg
= "REC->" FIELD_STRING_IP
;
624 fmt
= "(%lx <- %lx)";
625 arg
= "REC->" FIELD_STRING_FUNC
", REC->" FIELD_STRING_RETIP
;
628 /* When len=0, we just calculate the needed length */
629 #define LEN_OR_ZERO (len ? len - pos : 0)
631 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"%s", fmt
);
633 for (i
= 0; i
< tp
->nr_args
; i
++) {
634 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, " %s=%s",
635 tp
->args
[i
].name
, tp
->args
[i
].type
->fmt
);
638 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\", %s", arg
);
640 for (i
= 0; i
< tp
->nr_args
; i
++) {
641 if (strcmp(tp
->args
[i
].type
->name
, "string") == 0)
642 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
646 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, ", REC->%s",
652 /* return the length of print_fmt */
656 int set_print_fmt(struct trace_probe
*tp
, bool is_return
)
661 /* First: called with 0 length to calculate the needed length */
662 len
= __set_print_fmt(tp
, NULL
, 0, is_return
);
663 print_fmt
= kmalloc(len
+ 1, GFP_KERNEL
);
667 /* Second: actually write the @print_fmt */
668 __set_print_fmt(tp
, print_fmt
, len
+ 1, is_return
);
669 tp
->call
.print_fmt
= print_fmt
;