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"
19 static const char *trace_probe_err_text
[] = { ERRORS
};
21 static const char *reserved_field_names
[] = {
24 "common_preempt_count",
32 /* Printing in basic type function template */
33 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
34 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
36 trace_seq_printf(s, fmt, *(type *)data); \
37 return !trace_seq_has_overflowed(s); \
39 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
41 DEFINE_BASIC_PRINT_TYPE_FUNC(u8
, u8
, "%u")
42 DEFINE_BASIC_PRINT_TYPE_FUNC(u16
, u16
, "%u")
43 DEFINE_BASIC_PRINT_TYPE_FUNC(u32
, u32
, "%u")
44 DEFINE_BASIC_PRINT_TYPE_FUNC(u64
, u64
, "%Lu")
45 DEFINE_BASIC_PRINT_TYPE_FUNC(s8
, s8
, "%d")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(s16
, s16
, "%d")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(s32
, s32
, "%d")
48 DEFINE_BASIC_PRINT_TYPE_FUNC(s64
, s64
, "%Ld")
49 DEFINE_BASIC_PRINT_TYPE_FUNC(x8
, u8
, "0x%x")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(x16
, u16
, "0x%x")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(x32
, u32
, "0x%x")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(x64
, u64
, "0x%Lx")
54 int PRINT_TYPE_FUNC_NAME(symbol
)(struct trace_seq
*s
, void *data
, void *ent
)
56 trace_seq_printf(s
, "%pS", (void *)*(unsigned long *)data
);
57 return !trace_seq_has_overflowed(s
);
59 const char PRINT_TYPE_FMT_NAME(symbol
)[] = "%pS";
61 /* Print type function for string type */
62 int PRINT_TYPE_FUNC_NAME(string
)(struct trace_seq
*s
, void *data
, void *ent
)
64 int len
= *(u32
*)data
>> 16;
67 trace_seq_puts(s
, "(fault)");
69 trace_seq_printf(s
, "\"%s\"",
70 (const char *)get_loc_data(data
, ent
));
71 return !trace_seq_has_overflowed(s
);
74 const char PRINT_TYPE_FMT_NAME(string
)[] = "\\\"%s\\\"";
76 /* Fetch type information table */
77 static const struct fetch_type probe_fetch_types
[] = {
79 __ASSIGN_FETCH_TYPE("string", string
, string
, sizeof(u32
), 1,
82 ASSIGN_FETCH_TYPE(u8
, u8
, 0),
83 ASSIGN_FETCH_TYPE(u16
, u16
, 0),
84 ASSIGN_FETCH_TYPE(u32
, u32
, 0),
85 ASSIGN_FETCH_TYPE(u64
, u64
, 0),
86 ASSIGN_FETCH_TYPE(s8
, u8
, 1),
87 ASSIGN_FETCH_TYPE(s16
, u16
, 1),
88 ASSIGN_FETCH_TYPE(s32
, u32
, 1),
89 ASSIGN_FETCH_TYPE(s64
, u64
, 1),
90 ASSIGN_FETCH_TYPE_ALIAS(x8
, u8
, u8
, 0),
91 ASSIGN_FETCH_TYPE_ALIAS(x16
, u16
, u16
, 0),
92 ASSIGN_FETCH_TYPE_ALIAS(x32
, u32
, u32
, 0),
93 ASSIGN_FETCH_TYPE_ALIAS(x64
, u64
, u64
, 0),
94 ASSIGN_FETCH_TYPE_ALIAS(symbol
, ADDR_FETCH_TYPE
, ADDR_FETCH_TYPE
, 0),
99 static const struct fetch_type
*find_fetch_type(const char *type
)
104 type
= DEFAULT_FETCH_TYPE_STR
;
106 /* Special case: bitfield */
110 type
= strchr(type
, '/');
115 if (kstrtoul(type
, 0, &bs
))
120 return find_fetch_type("u8");
122 return find_fetch_type("u16");
124 return find_fetch_type("u32");
126 return find_fetch_type("u64");
132 for (i
= 0; probe_fetch_types
[i
].name
; i
++) {
133 if (strcmp(type
, probe_fetch_types
[i
].name
) == 0)
134 return &probe_fetch_types
[i
];
141 static struct trace_probe_log trace_probe_log
;
143 void trace_probe_log_init(const char *subsystem
, int argc
, const char **argv
)
145 trace_probe_log
.subsystem
= subsystem
;
146 trace_probe_log
.argc
= argc
;
147 trace_probe_log
.argv
= argv
;
148 trace_probe_log
.index
= 0;
151 void trace_probe_log_clear(void)
153 memset(&trace_probe_log
, 0, sizeof(trace_probe_log
));
156 void trace_probe_log_set_index(int index
)
158 trace_probe_log
.index
= index
;
161 void __trace_probe_log_err(int offset
, int err_type
)
164 int i
, len
= 0, pos
= 0;
166 if (!trace_probe_log
.argv
)
169 /* Recalcurate the length and allocate buffer */
170 for (i
= 0; i
< trace_probe_log
.argc
; i
++) {
171 if (i
== trace_probe_log
.index
)
173 len
+= strlen(trace_probe_log
.argv
[i
]) + 1;
175 command
= kzalloc(len
, GFP_KERNEL
);
179 /* And make a command string from argv array */
181 for (i
= 0; i
< trace_probe_log
.argc
; i
++) {
182 len
= strlen(trace_probe_log
.argv
[i
]);
183 strcpy(p
, trace_probe_log
.argv
[i
]);
189 tracing_log_err(NULL
, trace_probe_log
.subsystem
, command
,
190 trace_probe_err_text
, err_type
, pos
+ offset
);
195 /* Split symbol and offset. */
196 int traceprobe_split_symbol_offset(char *symbol
, long *offset
)
204 tmp
= strpbrk(symbol
, "+-");
206 ret
= kstrtol(tmp
, 0, offset
);
216 /* @buf must has MAX_EVENT_NAME_LEN size */
217 int traceprobe_parse_event_name(const char **pevent
, const char **pgroup
,
218 char *buf
, int offset
)
220 const char *slash
, *event
= *pevent
;
223 slash
= strchr(event
, '/');
225 if (slash
== event
) {
226 trace_probe_log_err(offset
, NO_GROUP_NAME
);
229 if (slash
- event
+ 1 > MAX_EVENT_NAME_LEN
) {
230 trace_probe_log_err(offset
, GROUP_TOO_LONG
);
233 strlcpy(buf
, event
, slash
- event
+ 1);
234 if (!is_good_name(buf
)) {
235 trace_probe_log_err(offset
, BAD_GROUP_NAME
);
240 offset
+= slash
- event
+ 1;
245 trace_probe_log_err(offset
, NO_EVENT_NAME
);
247 } else if (len
> MAX_EVENT_NAME_LEN
) {
248 trace_probe_log_err(offset
, EVENT_TOO_LONG
);
251 if (!is_good_name(event
)) {
252 trace_probe_log_err(offset
, BAD_EVENT_NAME
);
258 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
260 static int parse_probe_vars(char *arg
, const struct fetch_type
*t
,
261 struct fetch_insn
*code
, unsigned int flags
, int offs
)
267 if (strcmp(arg
, "retval") == 0) {
268 if (flags
& TPARG_FL_RETURN
) {
269 code
->op
= FETCH_OP_RETVAL
;
271 trace_probe_log_err(offs
, RETVAL_ON_PROBE
);
274 } else if ((len
= str_has_prefix(arg
, "stack"))) {
275 if (arg
[len
] == '\0') {
276 code
->op
= FETCH_OP_STACKP
;
277 } else if (isdigit(arg
[len
])) {
278 ret
= kstrtoul(arg
+ len
, 10, ¶m
);
281 } else if ((flags
& TPARG_FL_KERNEL
) &&
282 param
> PARAM_MAX_STACK
) {
283 trace_probe_log_err(offs
, BAD_STACK_NUM
);
286 code
->op
= FETCH_OP_STACK
;
287 code
->param
= (unsigned int)param
;
291 } else if (strcmp(arg
, "comm") == 0) {
292 code
->op
= FETCH_OP_COMM
;
293 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
294 } else if (((flags
& TPARG_FL_MASK
) ==
295 (TPARG_FL_KERNEL
| TPARG_FL_FENTRY
)) &&
296 (len
= str_has_prefix(arg
, "arg"))) {
297 ret
= kstrtoul(arg
+ len
, 10, ¶m
);
300 } else if (!param
|| param
> PARAM_MAX_STACK
) {
301 trace_probe_log_err(offs
, BAD_ARG_NUM
);
304 code
->op
= FETCH_OP_ARG
;
305 code
->param
= (unsigned int)param
- 1;
313 trace_probe_log_err(offs
, BAD_VAR
);
317 /* Recursive argument parser */
319 parse_probe_arg(char *arg
, const struct fetch_type
*type
,
320 struct fetch_insn
**pcode
, struct fetch_insn
*end
,
321 unsigned int flags
, int offs
)
323 struct fetch_insn
*code
= *pcode
;
331 ret
= parse_probe_vars(arg
+ 1, type
, code
, flags
, offs
);
334 case '%': /* named register */
335 ret
= regs_query_register_offset(arg
+ 1);
337 code
->op
= FETCH_OP_REG
;
338 code
->param
= (unsigned int)ret
;
341 trace_probe_log_err(offs
, BAD_REG_NAME
);
344 case '@': /* memory, file-offset or symbol */
345 if (isdigit(arg
[1])) {
346 ret
= kstrtoul(arg
+ 1, 0, ¶m
);
348 trace_probe_log_err(offs
, BAD_MEM_ADDR
);
352 code
->op
= FETCH_OP_IMM
;
353 code
->immediate
= param
;
354 } else if (arg
[1] == '+') {
355 /* kprobes don't support file offsets */
356 if (flags
& TPARG_FL_KERNEL
) {
357 trace_probe_log_err(offs
, FILE_ON_KPROBE
);
360 ret
= kstrtol(arg
+ 2, 0, &offset
);
362 trace_probe_log_err(offs
, BAD_FILE_OFFS
);
366 code
->op
= FETCH_OP_FOFFS
;
367 code
->immediate
= (unsigned long)offset
; // imm64?
369 /* uprobes don't support symbols */
370 if (!(flags
& TPARG_FL_KERNEL
)) {
371 trace_probe_log_err(offs
, SYM_ON_UPROBE
);
374 /* Preserve symbol for updating */
375 code
->op
= FETCH_NOP_SYMBOL
;
376 code
->data
= kstrdup(arg
+ 1, GFP_KERNEL
);
380 trace_probe_log_err(offs
, TOO_MANY_OPS
);
383 code
->op
= FETCH_OP_IMM
;
386 /* These are fetching from memory */
388 trace_probe_log_err(offs
, TOO_MANY_OPS
);
392 code
->op
= FETCH_OP_DEREF
;
393 code
->offset
= offset
;
396 case '+': /* deref memory */
397 arg
++; /* Skip '+', because kstrtol() rejects it. */
400 tmp
= strchr(arg
, '(');
402 trace_probe_log_err(offs
, DEREF_NEED_BRACE
);
406 ret
= kstrtol(arg
, 0, &offset
);
408 trace_probe_log_err(offs
, BAD_DEREF_OFFS
);
411 offs
+= (tmp
+ 1 - arg
) + (arg
[0] != '-' ? 1 : 0);
413 tmp
= strrchr(arg
, ')');
415 trace_probe_log_err(offs
+ strlen(arg
),
419 const struct fetch_type
*t2
= find_fetch_type(NULL
);
422 ret
= parse_probe_arg(arg
, t2
, &code
, end
, flags
, offs
);
425 if (code
->op
== FETCH_OP_COMM
) {
426 trace_probe_log_err(offs
, COMM_CANT_DEREF
);
430 trace_probe_log_err(offs
, TOO_MANY_OPS
);
435 code
->op
= FETCH_OP_DEREF
;
436 code
->offset
= offset
;
440 if (!ret
&& code
->op
== FETCH_OP_NOP
) {
441 /* Parsed, but do not find fetch method */
442 trace_probe_log_err(offs
, BAD_FETCH_ARG
);
448 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
450 /* Bitfield type needs to be parsed into a fetch function */
451 static int __parse_bitfield_probe_arg(const char *bf
,
452 const struct fetch_type
*t
,
453 struct fetch_insn
**pcode
)
455 struct fetch_insn
*code
= *pcode
;
456 unsigned long bw
, bo
;
462 bw
= simple_strtoul(bf
+ 1, &tail
, 0); /* Use simple one */
464 if (bw
== 0 || *tail
!= '@')
468 bo
= simple_strtoul(bf
, &tail
, 0);
470 if (tail
== bf
|| *tail
!= '/')
473 if (code
->op
!= FETCH_OP_NOP
)
477 code
->op
= FETCH_OP_MOD_BF
;
478 code
->lshift
= BYTES_TO_BITS(t
->size
) - (bw
+ bo
);
479 code
->rshift
= BYTES_TO_BITS(t
->size
) - bw
;
480 code
->basesize
= t
->size
;
482 return (BYTES_TO_BITS(t
->size
) < (bw
+ bo
)) ? -EINVAL
: 0;
485 /* String length checking wrapper */
486 static int traceprobe_parse_probe_arg_body(char *arg
, ssize_t
*size
,
487 struct probe_arg
*parg
, unsigned int flags
, int offset
)
489 struct fetch_insn
*code
, *scode
, *tmp
= NULL
;
494 if (len
> MAX_ARGSTR_LEN
) {
495 trace_probe_log_err(offset
, ARG_TOO_LONG
);
497 } else if (len
== 0) {
498 trace_probe_log_err(offset
, NO_ARG_BODY
);
502 parg
->comm
= kstrdup(arg
, GFP_KERNEL
);
506 t
= strchr(arg
, ':');
509 t2
= strchr(++t
, '[');
512 t3
= strchr(t2
, ']');
514 offset
+= t2
+ strlen(t2
) - arg
;
515 trace_probe_log_err(offset
,
518 } else if (t3
[1] != '\0') {
519 trace_probe_log_err(offset
+ t3
+ 1 - arg
,
524 if (kstrtouint(t2
, 0, &parg
->count
) || !parg
->count
) {
525 trace_probe_log_err(offset
+ t2
- arg
,
529 if (parg
->count
> MAX_ARRAY_LEN
) {
530 trace_probe_log_err(offset
+ t2
- arg
,
537 /* Since $comm can not be dereferred, we can find $comm by strcmp */
538 if (strcmp(arg
, "$comm") == 0) {
539 /* The type of $comm must be "string", and not an array. */
540 if (parg
->count
|| (t
&& strcmp(t
, "string")))
542 parg
->type
= find_fetch_type("string");
544 parg
->type
= find_fetch_type(t
);
546 trace_probe_log_err(offset
+ (t
? (t
- arg
) : 0), BAD_TYPE
);
549 parg
->offset
= *size
;
550 *size
+= parg
->type
->size
* (parg
->count
?: 1);
553 len
= strlen(parg
->type
->fmttype
) + 6;
554 parg
->fmt
= kmalloc(len
, GFP_KERNEL
);
557 snprintf(parg
->fmt
, len
, "%s[%d]", parg
->type
->fmttype
,
561 code
= tmp
= kcalloc(FETCH_INSN_MAX
, sizeof(*code
), GFP_KERNEL
);
564 code
[FETCH_INSN_MAX
- 1].op
= FETCH_OP_END
;
566 ret
= parse_probe_arg(arg
, parg
->type
, &code
, &code
[FETCH_INSN_MAX
- 1],
571 /* Store operation */
572 if (!strcmp(parg
->type
->name
, "string")) {
573 if (code
->op
!= FETCH_OP_DEREF
&& code
->op
!= FETCH_OP_IMM
&&
574 code
->op
!= FETCH_OP_COMM
) {
575 trace_probe_log_err(offset
+ (t
? (t
- arg
) : 0),
580 if (code
->op
!= FETCH_OP_DEREF
|| parg
->count
) {
582 * IMM and COMM is pointing actual address, those must
583 * be kept, and if parg->count != 0, this is an array
584 * of string pointers instead of string address itself.
587 if (code
->op
!= FETCH_OP_NOP
) {
588 trace_probe_log_err(offset
, TOO_MANY_OPS
);
593 code
->op
= FETCH_OP_ST_STRING
; /* In DEREF case, replace it */
594 code
->size
= parg
->type
->size
;
595 parg
->dynamic
= true;
596 } else if (code
->op
== FETCH_OP_DEREF
) {
597 code
->op
= FETCH_OP_ST_MEM
;
598 code
->size
= parg
->type
->size
;
601 if (code
->op
!= FETCH_OP_NOP
) {
602 trace_probe_log_err(offset
, TOO_MANY_OPS
);
606 code
->op
= FETCH_OP_ST_RAW
;
607 code
->size
= parg
->type
->size
;
610 /* Modify operation */
612 ret
= __parse_bitfield_probe_arg(t
, parg
->type
, &code
);
614 trace_probe_log_err(offset
+ t
- arg
, BAD_BITFIELD
);
618 /* Loop(Array) operation */
620 if (scode
->op
!= FETCH_OP_ST_MEM
&&
621 scode
->op
!= FETCH_OP_ST_STRING
) {
622 trace_probe_log_err(offset
+ (t
? (t
- arg
) : 0),
628 if (code
->op
!= FETCH_OP_NOP
) {
629 trace_probe_log_err(offset
, TOO_MANY_OPS
);
633 code
->op
= FETCH_OP_LP_ARRAY
;
634 code
->param
= parg
->count
;
637 code
->op
= FETCH_OP_END
;
639 /* Shrink down the code buffer */
640 parg
->code
= kcalloc(code
- tmp
+ 1, sizeof(*code
), GFP_KERNEL
);
644 memcpy(parg
->code
, tmp
, sizeof(*code
) * (code
- tmp
+ 1));
648 for (code
= tmp
; code
< tmp
+ FETCH_INSN_MAX
; code
++)
649 if (code
->op
== FETCH_NOP_SYMBOL
)
657 /* Return 1 if name is reserved or already used by another argument */
658 static int traceprobe_conflict_field_name(const char *name
,
659 struct probe_arg
*args
, int narg
)
663 for (i
= 0; i
< ARRAY_SIZE(reserved_field_names
); i
++)
664 if (strcmp(reserved_field_names
[i
], name
) == 0)
667 for (i
= 0; i
< narg
; i
++)
668 if (strcmp(args
[i
].name
, name
) == 0)
674 int traceprobe_parse_probe_arg(struct trace_probe
*tp
, int i
, char *arg
,
677 struct probe_arg
*parg
= &tp
->args
[i
];
680 /* Increment count for freeing args in error case */
683 body
= strchr(arg
, '=');
685 if (body
- arg
> MAX_ARG_NAME_LEN
) {
686 trace_probe_log_err(0, ARG_NAME_TOO_LONG
);
688 } else if (body
== arg
) {
689 trace_probe_log_err(0, NO_ARG_NAME
);
692 parg
->name
= kmemdup_nul(arg
, body
- arg
, GFP_KERNEL
);
695 /* If argument name is omitted, set "argN" */
696 parg
->name
= kasprintf(GFP_KERNEL
, "arg%d", i
+ 1);
702 if (!is_good_name(parg
->name
)) {
703 trace_probe_log_err(0, BAD_ARG_NAME
);
706 if (traceprobe_conflict_field_name(parg
->name
, tp
->args
, i
)) {
707 trace_probe_log_err(0, USED_ARG_NAME
);
710 /* Parse fetch argument */
711 return traceprobe_parse_probe_arg_body(body
, &tp
->size
, parg
, flags
,
715 void traceprobe_free_probe_arg(struct probe_arg
*arg
)
717 struct fetch_insn
*code
= arg
->code
;
719 while (code
&& code
->op
!= FETCH_OP_END
) {
720 if (code
->op
== FETCH_NOP_SYMBOL
)
730 int traceprobe_update_arg(struct probe_arg
*arg
)
732 struct fetch_insn
*code
= arg
->code
;
738 while (code
&& code
->op
!= FETCH_OP_END
) {
739 if (code
->op
== FETCH_NOP_SYMBOL
) {
740 if (code
[1].op
!= FETCH_OP_IMM
)
743 tmp
= strpbrk(code
->data
, "+-");
746 ret
= traceprobe_split_symbol_offset(code
->data
,
752 (unsigned long)kallsyms_lookup_name(code
->data
);
755 if (!code
[1].immediate
)
757 code
[1].immediate
+= offset
;
764 /* When len=0, we just calculate the needed length */
765 #define LEN_OR_ZERO (len ? len - pos : 0)
766 static int __set_print_fmt(struct trace_probe
*tp
, char *buf
, int len
,
769 struct probe_arg
*parg
;
772 const char *fmt
, *arg
;
776 arg
= "REC->" FIELD_STRING_IP
;
778 fmt
= "(%lx <- %lx)";
779 arg
= "REC->" FIELD_STRING_FUNC
", REC->" FIELD_STRING_RETIP
;
782 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"%s", fmt
);
784 for (i
= 0; i
< tp
->nr_args
; i
++) {
786 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, " %s=", parg
->name
);
788 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "{%s",
790 for (j
= 1; j
< parg
->count
; j
++)
791 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, ",%s",
793 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "}");
795 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "%s",
799 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\", %s", arg
);
801 for (i
= 0; i
< tp
->nr_args
; i
++) {
804 if (strcmp(parg
->type
->name
, "string") == 0)
805 fmt
= ", __get_str(%s[%d])";
807 fmt
= ", REC->%s[%d]";
808 for (j
= 0; j
< parg
->count
; j
++)
809 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
812 if (strcmp(parg
->type
->name
, "string") == 0)
813 fmt
= ", __get_str(%s)";
816 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
821 /* return the length of print_fmt */
826 int traceprobe_set_print_fmt(struct trace_probe
*tp
, bool is_return
)
831 /* First: called with 0 length to calculate the needed length */
832 len
= __set_print_fmt(tp
, NULL
, 0, is_return
);
833 print_fmt
= kmalloc(len
+ 1, GFP_KERNEL
);
837 /* Second: actually write the @print_fmt */
838 __set_print_fmt(tp
, print_fmt
, len
+ 1, is_return
);
839 tp
->call
.print_fmt
= print_fmt
;
844 int traceprobe_define_arg_fields(struct trace_event_call
*event_call
,
845 size_t offset
, struct trace_probe
*tp
)
849 /* Set argument names as fields */
850 for (i
= 0; i
< tp
->nr_args
; i
++) {
851 struct probe_arg
*parg
= &tp
->args
[i
];
852 const char *fmt
= parg
->type
->fmttype
;
853 int size
= parg
->type
->size
;
859 ret
= trace_define_field(event_call
, fmt
, parg
->name
,
860 offset
+ parg
->offset
, size
,
861 parg
->type
->is_signed
,