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 static 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, void *data, void *ent)\
31 trace_seq_printf(s, fmt, *(type *)data); \
32 return !trace_seq_has_overflowed(s); \
34 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
36 DEFINE_BASIC_PRINT_TYPE_FUNC(u8
, u8
, "%u")
37 DEFINE_BASIC_PRINT_TYPE_FUNC(u16
, u16
, "%u")
38 DEFINE_BASIC_PRINT_TYPE_FUNC(u32
, u32
, "%u")
39 DEFINE_BASIC_PRINT_TYPE_FUNC(u64
, u64
, "%Lu")
40 DEFINE_BASIC_PRINT_TYPE_FUNC(s8
, s8
, "%d")
41 DEFINE_BASIC_PRINT_TYPE_FUNC(s16
, s16
, "%d")
42 DEFINE_BASIC_PRINT_TYPE_FUNC(s32
, s32
, "%d")
43 DEFINE_BASIC_PRINT_TYPE_FUNC(s64
, s64
, "%Ld")
44 DEFINE_BASIC_PRINT_TYPE_FUNC(x8
, u8
, "0x%x")
45 DEFINE_BASIC_PRINT_TYPE_FUNC(x16
, u16
, "0x%x")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(x32
, u32
, "0x%x")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(x64
, u64
, "0x%Lx")
49 int PRINT_TYPE_FUNC_NAME(symbol
)(struct trace_seq
*s
, void *data
, void *ent
)
51 trace_seq_printf(s
, "%pS", (void *)*(unsigned long *)data
);
52 return !trace_seq_has_overflowed(s
);
54 const char PRINT_TYPE_FMT_NAME(symbol
)[] = "%pS";
56 /* Print type function for string type */
57 int PRINT_TYPE_FUNC_NAME(string
)(struct trace_seq
*s
, void *data
, void *ent
)
59 int len
= *(u32
*)data
>> 16;
62 trace_seq_puts(s
, "(fault)");
64 trace_seq_printf(s
, "\"%s\"",
65 (const char *)get_loc_data(data
, ent
));
66 return !trace_seq_has_overflowed(s
);
69 const char PRINT_TYPE_FMT_NAME(string
)[] = "\\\"%s\\\"";
71 /* Fetch type information table */
72 static const struct fetch_type probe_fetch_types
[] = {
74 __ASSIGN_FETCH_TYPE("string", string
, string
, sizeof(u32
), 1,
77 ASSIGN_FETCH_TYPE(u8
, u8
, 0),
78 ASSIGN_FETCH_TYPE(u16
, u16
, 0),
79 ASSIGN_FETCH_TYPE(u32
, u32
, 0),
80 ASSIGN_FETCH_TYPE(u64
, u64
, 0),
81 ASSIGN_FETCH_TYPE(s8
, u8
, 1),
82 ASSIGN_FETCH_TYPE(s16
, u16
, 1),
83 ASSIGN_FETCH_TYPE(s32
, u32
, 1),
84 ASSIGN_FETCH_TYPE(s64
, u64
, 1),
85 ASSIGN_FETCH_TYPE_ALIAS(x8
, u8
, u8
, 0),
86 ASSIGN_FETCH_TYPE_ALIAS(x16
, u16
, u16
, 0),
87 ASSIGN_FETCH_TYPE_ALIAS(x32
, u32
, u32
, 0),
88 ASSIGN_FETCH_TYPE_ALIAS(x64
, u64
, u64
, 0),
89 ASSIGN_FETCH_TYPE_ALIAS(symbol
, ADDR_FETCH_TYPE
, ADDR_FETCH_TYPE
, 0),
94 static const struct fetch_type
*find_fetch_type(const char *type
)
99 type
= DEFAULT_FETCH_TYPE_STR
;
101 /* Special case: bitfield */
105 type
= strchr(type
, '/');
110 if (kstrtoul(type
, 0, &bs
))
115 return find_fetch_type("u8");
117 return find_fetch_type("u16");
119 return find_fetch_type("u32");
121 return find_fetch_type("u64");
127 for (i
= 0; probe_fetch_types
[i
].name
; i
++) {
128 if (strcmp(type
, probe_fetch_types
[i
].name
) == 0)
129 return &probe_fetch_types
[i
];
136 /* Split symbol and offset. */
137 int traceprobe_split_symbol_offset(char *symbol
, long *offset
)
145 tmp
= strpbrk(symbol
, "+-");
147 ret
= kstrtol(tmp
, 0, offset
);
157 /* @buf must has MAX_EVENT_NAME_LEN size */
158 int traceprobe_parse_event_name(const char **pevent
, const char **pgroup
,
161 const char *slash
, *event
= *pevent
;
164 slash
= strchr(event
, '/');
166 if (slash
== event
) {
167 pr_info("Group name is not specified\n");
170 if (slash
- event
+ 1 > MAX_EVENT_NAME_LEN
) {
171 pr_info("Group name is too long\n");
174 strlcpy(buf
, event
, slash
- event
+ 1);
175 if (!is_good_name(buf
)) {
176 pr_info("Group name must follow the same rules as C identifiers\n");
185 pr_info("Event name is not specified\n");
187 } else if (len
> MAX_EVENT_NAME_LEN
) {
188 pr_info("Event name is too long\n");
191 if (!is_good_name(event
)) {
192 pr_info("Event name must follow the same rules as C identifiers\n");
198 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
200 static int parse_probe_vars(char *arg
, const struct fetch_type
*t
,
201 struct fetch_insn
*code
, unsigned int flags
)
207 if (strcmp(arg
, "retval") == 0) {
208 if (flags
& TPARG_FL_RETURN
)
209 code
->op
= FETCH_OP_RETVAL
;
212 } else if ((len
= str_has_prefix(arg
, "stack"))) {
213 if (arg
[len
] == '\0') {
214 code
->op
= FETCH_OP_STACKP
;
215 } else if (isdigit(arg
[len
])) {
216 ret
= kstrtoul(arg
+ len
, 10, ¶m
);
217 if (ret
|| ((flags
& TPARG_FL_KERNEL
) &&
218 param
> PARAM_MAX_STACK
))
221 code
->op
= FETCH_OP_STACK
;
222 code
->param
= (unsigned int)param
;
226 } else if (strcmp(arg
, "comm") == 0) {
227 code
->op
= FETCH_OP_COMM
;
228 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
229 } else if (((flags
& TPARG_FL_MASK
) ==
230 (TPARG_FL_KERNEL
| TPARG_FL_FENTRY
)) &&
231 (len
= str_has_prefix(arg
, "arg"))) {
232 if (!isdigit(arg
[len
]))
234 ret
= kstrtoul(arg
+ len
, 10, ¶m
);
235 if (ret
|| !param
|| param
> PARAM_MAX_STACK
)
237 code
->op
= FETCH_OP_ARG
;
238 code
->param
= (unsigned int)param
- 1;
246 /* Recursive argument parser */
248 parse_probe_arg(char *arg
, const struct fetch_type
*type
,
249 struct fetch_insn
**pcode
, struct fetch_insn
*end
,
252 struct fetch_insn
*code
= *pcode
;
260 ret
= parse_probe_vars(arg
+ 1, type
, code
, flags
);
263 case '%': /* named register */
264 ret
= regs_query_register_offset(arg
+ 1);
266 code
->op
= FETCH_OP_REG
;
267 code
->param
= (unsigned int)ret
;
272 case '@': /* memory, file-offset or symbol */
273 if (isdigit(arg
[1])) {
274 ret
= kstrtoul(arg
+ 1, 0, ¶m
);
278 code
->op
= FETCH_OP_IMM
;
279 code
->immediate
= param
;
280 } else if (arg
[1] == '+') {
281 /* kprobes don't support file offsets */
282 if (flags
& TPARG_FL_KERNEL
)
285 ret
= kstrtol(arg
+ 2, 0, &offset
);
289 code
->op
= FETCH_OP_FOFFS
;
290 code
->immediate
= (unsigned long)offset
; // imm64?
292 /* uprobes don't support symbols */
293 if (!(flags
& TPARG_FL_KERNEL
))
296 /* Preserve symbol for updating */
297 code
->op
= FETCH_NOP_SYMBOL
;
298 code
->data
= kstrdup(arg
+ 1, GFP_KERNEL
);
304 code
->op
= FETCH_OP_IMM
;
307 /* These are fetching from memory */
311 code
->op
= FETCH_OP_DEREF
;
312 code
->offset
= offset
;
315 case '+': /* deref memory */
316 arg
++; /* Skip '+', because kstrtol() rejects it. */
319 tmp
= strchr(arg
, '(');
324 ret
= kstrtol(arg
, 0, &offset
);
329 tmp
= strrchr(arg
, ')');
332 const struct fetch_type
*t2
= find_fetch_type(NULL
);
335 ret
= parse_probe_arg(arg
, t2
, &code
, end
, flags
);
338 if (code
->op
== FETCH_OP_COMM
)
344 code
->op
= FETCH_OP_DEREF
;
345 code
->offset
= offset
;
349 if (!ret
&& code
->op
== FETCH_OP_NOP
) {
350 /* Parsed, but do not find fetch method */
356 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
358 /* Bitfield type needs to be parsed into a fetch function */
359 static int __parse_bitfield_probe_arg(const char *bf
,
360 const struct fetch_type
*t
,
361 struct fetch_insn
**pcode
)
363 struct fetch_insn
*code
= *pcode
;
364 unsigned long bw
, bo
;
370 bw
= simple_strtoul(bf
+ 1, &tail
, 0); /* Use simple one */
372 if (bw
== 0 || *tail
!= '@')
376 bo
= simple_strtoul(bf
, &tail
, 0);
378 if (tail
== bf
|| *tail
!= '/')
381 if (code
->op
!= FETCH_OP_NOP
)
385 code
->op
= FETCH_OP_MOD_BF
;
386 code
->lshift
= BYTES_TO_BITS(t
->size
) - (bw
+ bo
);
387 code
->rshift
= BYTES_TO_BITS(t
->size
) - bw
;
388 code
->basesize
= t
->size
;
390 return (BYTES_TO_BITS(t
->size
) < (bw
+ bo
)) ? -EINVAL
: 0;
393 /* String length checking wrapper */
394 static int traceprobe_parse_probe_arg_body(char *arg
, ssize_t
*size
,
395 struct probe_arg
*parg
, unsigned int flags
)
397 struct fetch_insn
*code
, *scode
, *tmp
= NULL
;
401 if (strlen(arg
) > MAX_ARGSTR_LEN
) {
402 pr_info("Argument is too long.: %s\n", arg
);
405 parg
->comm
= kstrdup(arg
, GFP_KERNEL
);
407 pr_info("Failed to allocate memory for command '%s'.\n", arg
);
410 t
= strchr(arg
, ':');
413 t2
= strchr(++t
, '[');
416 parg
->count
= simple_strtoul(t2
+ 1, &t2
, 0);
417 if (strcmp(t2
, "]") || parg
->count
== 0)
419 if (parg
->count
> MAX_ARRAY_LEN
)
424 * The default type of $comm should be "string", and it can't be
427 if (!t
&& strcmp(arg
, "$comm") == 0)
428 parg
->type
= find_fetch_type("string");
430 parg
->type
= find_fetch_type(t
);
432 pr_info("Unsupported type: %s\n", t
);
435 parg
->offset
= *size
;
436 *size
+= parg
->type
->size
* (parg
->count
?: 1);
439 len
= strlen(parg
->type
->fmttype
) + 6;
440 parg
->fmt
= kmalloc(len
, GFP_KERNEL
);
443 snprintf(parg
->fmt
, len
, "%s[%d]", parg
->type
->fmttype
,
447 code
= tmp
= kzalloc(sizeof(*code
) * FETCH_INSN_MAX
, GFP_KERNEL
);
450 code
[FETCH_INSN_MAX
- 1].op
= FETCH_OP_END
;
452 ret
= parse_probe_arg(arg
, parg
->type
, &code
, &code
[FETCH_INSN_MAX
- 1],
457 /* Store operation */
458 if (!strcmp(parg
->type
->name
, "string")) {
459 if (code
->op
!= FETCH_OP_DEREF
&& code
->op
!= FETCH_OP_IMM
&&
460 code
->op
!= FETCH_OP_COMM
) {
461 pr_info("string only accepts memory or address.\n");
465 if (code
->op
!= FETCH_OP_DEREF
|| parg
->count
) {
467 * IMM and COMM is pointing actual address, those must
468 * be kept, and if parg->count != 0, this is an array
469 * of string pointers instead of string address itself.
472 if (code
->op
!= FETCH_OP_NOP
) {
477 code
->op
= FETCH_OP_ST_STRING
; /* In DEREF case, replace it */
478 code
->size
= parg
->type
->size
;
479 parg
->dynamic
= true;
480 } else if (code
->op
== FETCH_OP_DEREF
) {
481 code
->op
= FETCH_OP_ST_MEM
;
482 code
->size
= parg
->type
->size
;
485 if (code
->op
!= FETCH_OP_NOP
) {
489 code
->op
= FETCH_OP_ST_RAW
;
490 code
->size
= parg
->type
->size
;
493 /* Modify operation */
495 ret
= __parse_bitfield_probe_arg(t
, parg
->type
, &code
);
499 /* Loop(Array) operation */
501 if (scode
->op
!= FETCH_OP_ST_MEM
&&
502 scode
->op
!= FETCH_OP_ST_STRING
) {
503 pr_info("array only accepts memory or address\n");
508 if (code
->op
!= FETCH_OP_NOP
) {
512 code
->op
= FETCH_OP_LP_ARRAY
;
513 code
->param
= parg
->count
;
516 code
->op
= FETCH_OP_END
;
518 /* Shrink down the code buffer */
519 parg
->code
= kzalloc(sizeof(*code
) * (code
- tmp
+ 1), GFP_KERNEL
);
523 memcpy(parg
->code
, tmp
, sizeof(*code
) * (code
- tmp
+ 1));
527 for (code
= tmp
; code
< tmp
+ FETCH_INSN_MAX
; code
++)
528 if (code
->op
== FETCH_NOP_SYMBOL
)
536 /* Return 1 if name is reserved or already used by another argument */
537 static int traceprobe_conflict_field_name(const char *name
,
538 struct probe_arg
*args
, int narg
)
542 for (i
= 0; i
< ARRAY_SIZE(reserved_field_names
); i
++)
543 if (strcmp(reserved_field_names
[i
], name
) == 0)
546 for (i
= 0; i
< narg
; i
++)
547 if (strcmp(args
[i
].name
, name
) == 0)
553 int traceprobe_parse_probe_arg(struct trace_probe
*tp
, int i
, char *arg
,
556 struct probe_arg
*parg
= &tp
->args
[i
];
560 /* Increment count for freeing args in error case */
563 body
= strchr(arg
, '=');
565 if (body
- arg
> MAX_ARG_NAME_LEN
|| body
== arg
)
567 parg
->name
= kmemdup_nul(arg
, body
- arg
, GFP_KERNEL
);
570 /* If argument name is omitted, set "argN" */
571 parg
->name
= kasprintf(GFP_KERNEL
, "arg%d", i
+ 1);
577 if (!is_good_name(parg
->name
)) {
578 pr_info("Invalid argument[%d] name: %s\n",
583 if (traceprobe_conflict_field_name(parg
->name
, tp
->args
, i
)) {
584 pr_info("Argument[%d]: '%s' conflicts with another field.\n",
589 /* Parse fetch argument */
590 ret
= traceprobe_parse_probe_arg_body(body
, &tp
->size
, parg
, flags
);
592 pr_info("Parse error at argument[%d]. (%d)\n", i
, ret
);
596 void traceprobe_free_probe_arg(struct probe_arg
*arg
)
598 struct fetch_insn
*code
= arg
->code
;
600 while (code
&& code
->op
!= FETCH_OP_END
) {
601 if (code
->op
== FETCH_NOP_SYMBOL
)
611 int traceprobe_update_arg(struct probe_arg
*arg
)
613 struct fetch_insn
*code
= arg
->code
;
619 while (code
&& code
->op
!= FETCH_OP_END
) {
620 if (code
->op
== FETCH_NOP_SYMBOL
) {
621 if (code
[1].op
!= FETCH_OP_IMM
)
624 tmp
= strpbrk(code
->data
, "+-");
627 ret
= traceprobe_split_symbol_offset(code
->data
,
633 (unsigned long)kallsyms_lookup_name(code
->data
);
636 if (!code
[1].immediate
)
638 code
[1].immediate
+= offset
;
645 /* When len=0, we just calculate the needed length */
646 #define LEN_OR_ZERO (len ? len - pos : 0)
647 static int __set_print_fmt(struct trace_probe
*tp
, char *buf
, int len
,
650 struct probe_arg
*parg
;
653 const char *fmt
, *arg
;
657 arg
= "REC->" FIELD_STRING_IP
;
659 fmt
= "(%lx <- %lx)";
660 arg
= "REC->" FIELD_STRING_FUNC
", REC->" FIELD_STRING_RETIP
;
663 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"%s", fmt
);
665 for (i
= 0; i
< tp
->nr_args
; i
++) {
667 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, " %s=", parg
->name
);
669 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "{%s",
671 for (j
= 1; j
< parg
->count
; j
++)
672 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, ",%s",
674 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "}");
676 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "%s",
680 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\", %s", arg
);
682 for (i
= 0; i
< tp
->nr_args
; i
++) {
685 if (strcmp(parg
->type
->name
, "string") == 0)
686 fmt
= ", __get_str(%s[%d])";
688 fmt
= ", REC->%s[%d]";
689 for (j
= 0; j
< parg
->count
; j
++)
690 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
693 if (strcmp(parg
->type
->name
, "string") == 0)
694 fmt
= ", __get_str(%s)";
697 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
702 /* return the length of print_fmt */
707 int traceprobe_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
;
725 int traceprobe_define_arg_fields(struct trace_event_call
*event_call
,
726 size_t offset
, struct trace_probe
*tp
)
730 /* Set argument names as fields */
731 for (i
= 0; i
< tp
->nr_args
; i
++) {
732 struct probe_arg
*parg
= &tp
->args
[i
];
733 const char *fmt
= parg
->type
->fmttype
;
734 int size
= parg
->type
->size
;
740 ret
= trace_define_field(event_call
, fmt
, parg
->name
,
741 offset
+ parg
->offset
, size
,
742 parg
->type
->is_signed
,