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 <linux/bpf.h>
16 #include "trace_btf.h"
18 #include "trace_probe.h"
23 static const char *trace_probe_err_text[] = { ERRORS };
25 static const char *reserved_field_names[] = {
28 "common_preempt_count",
36 /* Printing in basic type function template */
37 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
38 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
40 trace_seq_printf(s, fmt, *(type *)data); \
41 return !trace_seq_has_overflowed(s); \
43 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
45 DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
48 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
49 DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
53 DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x")
54 DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
55 DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
56 DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
57 DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8, "'%c'")
59 int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
61 trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
62 return !trace_seq_has_overflowed(s);
64 const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
66 /* Print type function for string type */
67 int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
69 int len = *(u32 *)data >> 16;
72 trace_seq_puts(s, FAULT_STRING);
74 trace_seq_printf(s, "\"%s\"",
75 (const char *)get_loc_data(data, ent));
76 return !trace_seq_has_overflowed(s);
79 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
81 /* Fetch type information table */
82 static const struct fetch_type probe_fetch_types[] = {
84 __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, 1,
86 __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, 1,
88 __ASSIGN_FETCH_TYPE("symstr", string, string, sizeof(u32), 1, 1,
91 ASSIGN_FETCH_TYPE(u8, u8, 0),
92 ASSIGN_FETCH_TYPE(u16, u16, 0),
93 ASSIGN_FETCH_TYPE(u32, u32, 0),
94 ASSIGN_FETCH_TYPE(u64, u64, 0),
95 ASSIGN_FETCH_TYPE(s8, u8, 1),
96 ASSIGN_FETCH_TYPE(s16, u16, 1),
97 ASSIGN_FETCH_TYPE(s32, u32, 1),
98 ASSIGN_FETCH_TYPE(s64, u64, 1),
99 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
100 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
101 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
102 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
103 ASSIGN_FETCH_TYPE_ALIAS(char, u8, u8, 0),
104 ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
106 ASSIGN_FETCH_TYPE_END
109 static const struct fetch_type *find_fetch_type(const char *type, unsigned long flags)
113 /* Reject the symbol/symstr for uprobes */
114 if (type && (flags & TPARG_FL_USER) &&
115 (!strcmp(type, "symbol") || !strcmp(type, "symstr")))
119 type = DEFAULT_FETCH_TYPE_STR;
121 /* Special case: bitfield */
125 type = strchr(type, '/');
130 if (kstrtoul(type, 0, &bs))
135 return find_fetch_type("u8", flags);
137 return find_fetch_type("u16", flags);
139 return find_fetch_type("u32", flags);
141 return find_fetch_type("u64", flags);
147 for (i = 0; probe_fetch_types[i].name; i++) {
148 if (strcmp(type, probe_fetch_types[i].name) == 0)
149 return &probe_fetch_types[i];
156 static struct trace_probe_log trace_probe_log;
157 extern struct mutex dyn_event_ops_mutex;
159 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
161 lockdep_assert_held(&dyn_event_ops_mutex);
163 trace_probe_log.subsystem = subsystem;
164 trace_probe_log.argc = argc;
165 trace_probe_log.argv = argv;
166 trace_probe_log.index = 0;
169 void trace_probe_log_clear(void)
171 lockdep_assert_held(&dyn_event_ops_mutex);
173 memset(&trace_probe_log, 0, sizeof(trace_probe_log));
176 void trace_probe_log_set_index(int index)
178 lockdep_assert_held(&dyn_event_ops_mutex);
180 trace_probe_log.index = index;
183 void __trace_probe_log_err(int offset, int err_type)
186 int i, len = 0, pos = 0;
188 lockdep_assert_held(&dyn_event_ops_mutex);
190 if (!trace_probe_log.argv)
193 /* Recalculate the length and allocate buffer */
194 for (i = 0; i < trace_probe_log.argc; i++) {
195 if (i == trace_probe_log.index)
197 len += strlen(trace_probe_log.argv[i]) + 1;
199 command = kzalloc(len, GFP_KERNEL);
203 if (trace_probe_log.index >= trace_probe_log.argc) {
205 * Set the error position is next to the last arg + space.
206 * Note that len includes the terminal null and the cursor
207 * appears at pos + 1.
213 /* And make a command string from argv array */
215 for (i = 0; i < trace_probe_log.argc; i++) {
216 len = strlen(trace_probe_log.argv[i]);
217 strcpy(p, trace_probe_log.argv[i]);
223 tracing_log_err(NULL, trace_probe_log.subsystem, command,
224 trace_probe_err_text, err_type, pos + offset);
229 /* Split symbol and offset. */
230 int traceprobe_split_symbol_offset(char *symbol, long *offset)
238 tmp = strpbrk(symbol, "+-");
240 ret = kstrtol(tmp, 0, offset);
250 /* @buf must has MAX_EVENT_NAME_LEN size */
251 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
252 char *buf, int offset)
254 const char *slash, *event = *pevent;
257 slash = strchr(event, '/');
259 slash = strchr(event, '.');
262 if (slash == event) {
263 trace_probe_log_err(offset, NO_GROUP_NAME);
266 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
267 trace_probe_log_err(offset, GROUP_TOO_LONG);
270 strscpy(buf, event, slash - event + 1);
271 if (!is_good_system_name(buf)) {
272 trace_probe_log_err(offset, BAD_GROUP_NAME);
277 offset += slash - event + 1;
286 trace_probe_log_err(offset, NO_EVENT_NAME);
288 } else if (len >= MAX_EVENT_NAME_LEN) {
289 trace_probe_log_err(offset, EVENT_TOO_LONG);
292 if (!is_good_name(event)) {
293 trace_probe_log_err(offset, BAD_EVENT_NAME);
299 static int parse_trace_event_arg(char *arg, struct fetch_insn *code,
300 struct traceprobe_parse_context *ctx)
302 struct ftrace_event_field *field;
303 struct list_head *head;
305 head = trace_get_fields(ctx->event);
306 list_for_each_entry(field, head, link) {
307 if (!strcmp(arg, field->name)) {
308 code->op = FETCH_OP_TP_ARG;
316 #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
318 static u32 btf_type_int(const struct btf_type *t)
320 return *(u32 *)(t + 1);
323 static bool btf_type_is_char_ptr(struct btf *btf, const struct btf_type *type)
325 const struct btf_type *real_type;
329 real_type = btf_type_skip_modifiers(btf, type->type, &tid);
333 if (BTF_INFO_KIND(real_type->info) != BTF_KIND_INT)
336 intdata = btf_type_int(real_type);
337 return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED)
338 && BTF_INT_BITS(intdata) == 8;
341 static bool btf_type_is_char_array(struct btf *btf, const struct btf_type *type)
343 const struct btf_type *real_type;
344 const struct btf_array *array;
348 if (BTF_INFO_KIND(type->info) != BTF_KIND_ARRAY)
351 array = (const struct btf_array *)(type + 1);
353 real_type = btf_type_skip_modifiers(btf, array->type, &tid);
355 intdata = btf_type_int(real_type);
356 return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED)
357 && BTF_INT_BITS(intdata) == 8;
360 static int check_prepare_btf_string_fetch(char *typename,
361 struct fetch_insn **pcode,
362 struct traceprobe_parse_context *ctx)
364 struct btf *btf = ctx->btf;
366 if (!btf || !ctx->last_type)
369 /* char [] does not need any change. */
370 if (btf_type_is_char_array(btf, ctx->last_type))
373 /* char * requires dereference the pointer. */
374 if (btf_type_is_char_ptr(btf, ctx->last_type)) {
375 struct fetch_insn *code = *pcode + 1;
377 if (code->op == FETCH_OP_END) {
378 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
381 if (typename[0] == 'u')
382 code->op = FETCH_OP_UDEREF;
384 code->op = FETCH_OP_DEREF;
389 /* Other types are not available for string */
390 trace_probe_log_err(ctx->offset, BAD_TYPE4STR);
394 static const char *fetch_type_from_btf_type(struct btf *btf,
395 const struct btf_type *type,
396 struct traceprobe_parse_context *ctx)
400 /* TODO: const char * could be converted as a string */
401 switch (BTF_INFO_KIND(type->info)) {
403 /* enum is "int", so convert to "s32" */
405 case BTF_KIND_ENUM64:
408 /* pointer will be converted to "x??" */
409 if (IS_ENABLED(CONFIG_64BIT))
414 intdata = btf_type_int(type);
415 if (BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) {
416 switch (BTF_INT_BITS(intdata)) {
426 } else { /* unsigned */
427 switch (BTF_INT_BITS(intdata)) {
437 /* bitfield, size is encoded in the type */
438 ctx->last_bitsize = BTF_INT_BITS(intdata);
439 ctx->last_bitoffs += BTF_INT_OFFSET(intdata);
443 /* TODO: support other types */
448 static int query_btf_context(struct traceprobe_parse_context *ctx)
450 const struct btf_param *param;
451 const struct btf_type *type;
461 type = btf_find_func_proto(ctx->funcname, &btf);
468 /* ctx->params is optional, since func(void) will not have params. */
470 param = btf_get_func_param(type, &nr);
471 if (!IS_ERR_OR_NULL(param)) {
472 /* Hide the first 'data' argument of tracepoint */
473 if (ctx->flags & TPARG_FL_TPOINT) {
490 static void clear_btf_context(struct traceprobe_parse_context *ctx)
501 /* Return 1 if the field separater is arrow operator ('->') */
502 static int split_next_field(char *varname, char **next_field,
503 struct traceprobe_parse_context *ctx)
508 field = strpbrk(varname, ".-");
510 if (field[0] == '-' && field[1] == '>') {
514 } else if (field[0] == '.') {
518 trace_probe_log_err(ctx->offset + field - varname, BAD_HYPHEN);
528 * Parse the field of data structure. The @type must be a pointer type
529 * pointing the target data structure type.
531 static int parse_btf_field(char *fieldname, const struct btf_type *type,
532 struct fetch_insn **pcode, struct fetch_insn *end,
533 struct traceprobe_parse_context *ctx)
535 struct fetch_insn *code = *pcode;
536 const struct btf_member *field;
537 u32 bitoffs, anon_offs;
543 /* Outer loop for solving arrow operator ('->') */
544 if (BTF_INFO_KIND(type->info) != BTF_KIND_PTR) {
545 trace_probe_log_err(ctx->offset, NO_PTR_STRCT);
548 /* Convert a struct pointer type to a struct type */
549 type = btf_type_skip_modifiers(ctx->btf, type->type, &tid);
551 trace_probe_log_err(ctx->offset, BAD_BTF_TID);
557 /* Inner loop for solving dot operator ('.') */
559 is_ptr = split_next_field(fieldname, &next, ctx);
564 field = btf_find_struct_member(ctx->btf, type, fieldname,
567 trace_probe_log_err(ctx->offset, BAD_BTF_TID);
568 return PTR_ERR(field);
571 trace_probe_log_err(ctx->offset, NO_BTF_FIELD);
574 /* Add anonymous structure/union offset */
575 bitoffs += anon_offs;
577 /* Accumulate the bit-offsets of the dot-connected fields */
578 if (btf_type_kflag(type)) {
579 bitoffs += BTF_MEMBER_BIT_OFFSET(field->offset);
580 ctx->last_bitsize = BTF_MEMBER_BITFIELD_SIZE(field->offset);
582 bitoffs += field->offset;
583 ctx->last_bitsize = 0;
586 type = btf_type_skip_modifiers(ctx->btf, field->type, &tid);
588 trace_probe_log_err(ctx->offset, BAD_BTF_TID);
592 ctx->offset += next - fieldname;
594 } while (!is_ptr && fieldname);
597 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
600 code->op = FETCH_OP_DEREF; /* TODO: user deref support */
601 code->offset = bitoffs / 8;
604 ctx->last_bitoffs = bitoffs % 8;
605 ctx->last_type = type;
611 static int __store_entry_arg(struct trace_probe *tp, int argnum);
613 static int parse_btf_arg(char *varname,
614 struct fetch_insn **pcode, struct fetch_insn *end,
615 struct traceprobe_parse_context *ctx)
617 struct fetch_insn *code = *pcode;
618 const struct btf_param *params;
619 const struct btf_type *type;
624 if (WARN_ON_ONCE(!ctx->funcname))
627 is_ptr = split_next_field(varname, &field, ctx);
630 if (!is_ptr && field) {
631 /* dot-connected field on an argument is not supported. */
632 trace_probe_log_err(ctx->offset + field - varname,
637 if (ctx->flags & TPARG_FL_RETURN && !strcmp(varname, "$retval")) {
638 code->op = FETCH_OP_RETVAL;
639 /* Check whether the function return type is not void */
640 if (query_btf_context(ctx) == 0) {
641 if (ctx->proto->type == 0) {
642 trace_probe_log_err(ctx->offset, NO_RETVAL);
645 tid = ctx->proto->type;
649 trace_probe_log_err(ctx->offset + field - varname,
657 ret = query_btf_context(ctx);
658 if (ret < 0 || ctx->nr_params == 0) {
659 trace_probe_log_err(ctx->offset, NO_BTF_ENTRY);
660 return PTR_ERR(params);
663 params = ctx->params;
665 for (i = 0; i < ctx->nr_params; i++) {
666 const char *name = btf_name_by_offset(ctx->btf, params[i].name_off);
668 if (name && !strcmp(name, varname)) {
669 if (tparg_is_function_entry(ctx->flags)) {
670 code->op = FETCH_OP_ARG;
671 if (ctx->flags & TPARG_FL_TPOINT)
675 } else if (tparg_is_function_return(ctx->flags)) {
676 code->op = FETCH_OP_EDATA;
677 ret = __store_entry_arg(ctx->tp, i);
684 tid = params[i].type;
688 trace_probe_log_err(ctx->offset, NO_BTFARG);
692 type = btf_type_skip_modifiers(ctx->btf, tid, &tid);
694 trace_probe_log_err(ctx->offset, BAD_BTF_TID);
697 /* Initialize the last type information */
698 ctx->last_type = type;
699 ctx->last_bitoffs = 0;
700 ctx->last_bitsize = 0;
702 ctx->offset += field - varname;
703 return parse_btf_field(field, type, pcode, end, ctx);
708 static const struct fetch_type *find_fetch_type_from_btf_type(
709 struct traceprobe_parse_context *ctx)
711 struct btf *btf = ctx->btf;
712 const char *typestr = NULL;
714 if (btf && ctx->last_type)
715 typestr = fetch_type_from_btf_type(btf, ctx->last_type, ctx);
717 return find_fetch_type(typestr, ctx->flags);
720 static int parse_btf_bitfield(struct fetch_insn **pcode,
721 struct traceprobe_parse_context *ctx)
723 struct fetch_insn *code = *pcode;
725 if ((ctx->last_bitsize % 8 == 0) && ctx->last_bitoffs == 0)
729 if (code->op != FETCH_OP_NOP) {
730 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
735 code->op = FETCH_OP_MOD_BF;
736 code->lshift = 64 - (ctx->last_bitsize + ctx->last_bitoffs);
737 code->rshift = 64 - ctx->last_bitsize;
738 code->basesize = 64 / 8;
743 static void clear_btf_context(struct traceprobe_parse_context *ctx)
748 static int query_btf_context(struct traceprobe_parse_context *ctx)
753 static int parse_btf_arg(char *varname,
754 struct fetch_insn **pcode, struct fetch_insn *end,
755 struct traceprobe_parse_context *ctx)
757 trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
761 static int parse_btf_bitfield(struct fetch_insn **pcode,
762 struct traceprobe_parse_context *ctx)
764 trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
768 #define find_fetch_type_from_btf_type(ctx) \
769 find_fetch_type(NULL, ctx->flags)
771 static int check_prepare_btf_string_fetch(char *typename,
772 struct fetch_insn **pcode,
773 struct traceprobe_parse_context *ctx)
780 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
783 * Add the entry code to store the 'argnum'th parameter and return the offset
784 * in the entry data buffer where the data will be stored.
786 static int __store_entry_arg(struct trace_probe *tp, int argnum)
788 struct probe_entry_arg *earg = tp->entry_arg;
793 earg = kzalloc(sizeof(*tp->entry_arg), GFP_KERNEL);
796 earg->size = 2 * tp->nr_args + 1;
797 earg->code = kcalloc(earg->size, sizeof(struct fetch_insn),
803 /* Fill the code buffer with 'end' to simplify it */
804 for (i = 0; i < earg->size; i++)
805 earg->code[i].op = FETCH_OP_END;
806 tp->entry_arg = earg;
810 * The entry code array is repeating the pair of
811 * [FETCH_OP_ARG(argnum)][FETCH_OP_ST_EDATA(offset of entry data buffer)]
812 * and the rest of entries are filled with [FETCH_OP_END].
814 * To reduce the redundant function parameter fetching, we scan the entry
815 * code array to find the FETCH_OP_ARG which already fetches the 'argnum'
816 * parameter. If it doesn't match, update 'offset' to find the last
818 * If we find the FETCH_OP_END without matching FETCH_OP_ARG entry, we
819 * will save the entry with FETCH_OP_ARG and FETCH_OP_ST_EDATA, and
820 * return data offset so that caller can find the data offset in the entry
824 for (i = 0; i < earg->size - 1; i++) {
825 switch (earg->code[i].op) {
827 earg->code[i].op = FETCH_OP_ARG;
828 earg->code[i].param = argnum;
829 earg->code[i + 1].op = FETCH_OP_ST_EDATA;
830 earg->code[i + 1].offset = offset;
833 match = (earg->code[i].param == argnum);
835 case FETCH_OP_ST_EDATA:
836 offset = earg->code[i].offset;
839 offset += sizeof(unsigned long);
848 int traceprobe_get_entry_data_size(struct trace_probe *tp)
850 struct probe_entry_arg *earg = tp->entry_arg;
857 * earg->code[] array has an operation sequence which is run in
859 * The sequence stopped by FETCH_OP_END and each data stored in
860 * the entry data buffer by FETCH_OP_ST_EDATA. The FETCH_OP_ST_EDATA
861 * stores the data at the data buffer + its offset, and all data are
862 * "unsigned long" size. The offset must be increased when a data is
863 * stored. Thus we need to find the last FETCH_OP_ST_EDATA in the
866 for (i = 0; i < earg->size; i++) {
867 switch (earg->code[i].op) {
870 case FETCH_OP_ST_EDATA:
871 size = earg->code[i].offset + sizeof(unsigned long);
881 void store_trace_entry_data(void *edata, struct trace_probe *tp, struct pt_regs *regs)
883 struct probe_entry_arg *earg = tp->entry_arg;
884 unsigned long val = 0;
890 for (i = 0; i < earg->size; i++) {
891 struct fetch_insn *code = &earg->code[i];
895 val = regs_get_kernel_argument(regs, code->param);
897 case FETCH_OP_ST_EDATA:
898 *(unsigned long *)((unsigned long)edata + code->offset) = val;
909 NOKPROBE_SYMBOL(store_trace_entry_data)
912 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
914 /* Parse $vars. @orig_arg points '$', which syncs to @ctx->offset */
915 static int parse_probe_vars(char *orig_arg, const struct fetch_type *t,
916 struct fetch_insn **pcode,
917 struct fetch_insn *end,
918 struct traceprobe_parse_context *ctx)
920 struct fetch_insn *code = *pcode;
921 int err = TP_ERR_BAD_VAR;
922 char *arg = orig_arg + 1;
927 if (ctx->flags & TPARG_FL_TEVENT) {
930 ret = parse_trace_event_arg(arg, code, ctx);
933 if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
934 code->op = FETCH_OP_COMM;
937 /* backward compatibility */
942 if (str_has_prefix(arg, "retval")) {
943 if (!(ctx->flags & TPARG_FL_RETURN)) {
944 err = TP_ERR_RETVAL_ON_PROBE;
947 if (!(ctx->flags & TPARG_FL_KERNEL) ||
948 !IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
949 code->op = FETCH_OP_RETVAL;
952 return parse_btf_arg(orig_arg, pcode, end, ctx);
955 len = str_has_prefix(arg, "stack");
958 if (arg[len] == '\0') {
959 code->op = FETCH_OP_STACKP;
963 if (isdigit(arg[len])) {
964 ret = kstrtoul(arg + len, 10, ¶m);
968 if ((ctx->flags & TPARG_FL_KERNEL) &&
969 param > PARAM_MAX_STACK) {
970 err = TP_ERR_BAD_STACK_NUM;
973 code->op = FETCH_OP_STACK;
974 code->param = (unsigned int)param;
980 if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
981 code->op = FETCH_OP_COMM;
985 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
986 len = str_has_prefix(arg, "arg");
988 ret = kstrtoul(arg + len, 10, ¶m);
992 if (!param || param > PARAM_MAX_STACK) {
993 err = TP_ERR_BAD_ARG_NUM;
996 param--; /* argN starts from 1, but internal arg[N] starts from 0 */
998 if (tparg_is_function_entry(ctx->flags)) {
999 code->op = FETCH_OP_ARG;
1000 code->param = (unsigned int)param;
1002 * The tracepoint probe will probe a stub function, and the
1003 * first parameter of the stub is a dummy and should be ignored.
1005 if (ctx->flags & TPARG_FL_TPOINT)
1007 } else if (tparg_is_function_return(ctx->flags)) {
1008 /* function entry argument access from return probe */
1009 ret = __store_entry_arg(ctx->tp, param);
1010 if (ret < 0) /* This error should be an internal error */
1013 code->op = FETCH_OP_EDATA;
1016 err = TP_ERR_NOFENTRY_ARGS;
1024 __trace_probe_log_err(ctx->offset, err);
1028 static int str_to_immediate(char *str, unsigned long *imm)
1030 if (isdigit(str[0]))
1031 return kstrtoul(str, 0, imm);
1032 else if (str[0] == '-')
1033 return kstrtol(str, 0, (long *)imm);
1034 else if (str[0] == '+')
1035 return kstrtol(str + 1, 0, (long *)imm);
1039 static int __parse_imm_string(char *str, char **pbuf, int offs)
1041 size_t len = strlen(str);
1043 if (str[len - 1] != '"') {
1044 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
1047 *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
1053 /* Recursive argument parser */
1055 parse_probe_arg(char *arg, const struct fetch_type *type,
1056 struct fetch_insn **pcode, struct fetch_insn *end,
1057 struct traceprobe_parse_context *ctx)
1059 struct fetch_insn *code = *pcode;
1060 unsigned long param;
1061 int deref = FETCH_OP_DEREF;
1068 ret = parse_probe_vars(arg, type, pcode, end, ctx);
1071 case '%': /* named register */
1072 if (ctx->flags & (TPARG_FL_TEVENT | TPARG_FL_FPROBE)) {
1073 /* eprobe and fprobe do not handle registers */
1074 trace_probe_log_err(ctx->offset, BAD_VAR);
1077 ret = regs_query_register_offset(arg + 1);
1079 code->op = FETCH_OP_REG;
1080 code->param = (unsigned int)ret;
1083 trace_probe_log_err(ctx->offset, BAD_REG_NAME);
1086 case '@': /* memory, file-offset or symbol */
1087 if (isdigit(arg[1])) {
1088 ret = kstrtoul(arg + 1, 0, ¶m);
1090 trace_probe_log_err(ctx->offset, BAD_MEM_ADDR);
1094 code->op = FETCH_OP_IMM;
1095 code->immediate = param;
1096 } else if (arg[1] == '+') {
1097 /* kprobes don't support file offsets */
1098 if (ctx->flags & TPARG_FL_KERNEL) {
1099 trace_probe_log_err(ctx->offset, FILE_ON_KPROBE);
1102 ret = kstrtol(arg + 2, 0, &offset);
1104 trace_probe_log_err(ctx->offset, BAD_FILE_OFFS);
1108 code->op = FETCH_OP_FOFFS;
1109 code->immediate = (unsigned long)offset; // imm64?
1111 /* uprobes don't support symbols */
1112 if (!(ctx->flags & TPARG_FL_KERNEL)) {
1113 trace_probe_log_err(ctx->offset, SYM_ON_UPROBE);
1116 /* Preserve symbol for updating */
1117 code->op = FETCH_NOP_SYMBOL;
1118 code->data = kstrdup(arg + 1, GFP_KERNEL);
1121 if (++code == end) {
1122 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1125 code->op = FETCH_OP_IMM;
1126 code->immediate = 0;
1128 /* These are fetching from memory */
1129 if (++code == end) {
1130 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1134 code->op = FETCH_OP_DEREF;
1135 code->offset = offset;
1138 case '+': /* deref memory */
1140 if (arg[1] == 'u') {
1141 deref = FETCH_OP_UDEREF;
1146 arg++; /* Skip '+', because kstrtol() rejects it. */
1147 tmp = strchr(arg, '(');
1149 trace_probe_log_err(ctx->offset, DEREF_NEED_BRACE);
1153 ret = kstrtol(arg, 0, &offset);
1155 trace_probe_log_err(ctx->offset, BAD_DEREF_OFFS);
1158 ctx->offset += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
1160 tmp = strrchr(arg, ')');
1162 trace_probe_log_err(ctx->offset + strlen(arg),
1166 const struct fetch_type *t2 = find_fetch_type(NULL, ctx->flags);
1167 int cur_offs = ctx->offset;
1170 ret = parse_probe_arg(arg, t2, &code, end, ctx);
1173 ctx->offset = cur_offs;
1174 if (code->op == FETCH_OP_COMM ||
1175 code->op == FETCH_OP_DATA) {
1176 trace_probe_log_err(ctx->offset, COMM_CANT_DEREF);
1179 if (++code == end) {
1180 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1186 code->offset = offset;
1187 /* Reset the last type if used */
1188 ctx->last_type = NULL;
1191 case '\\': /* Immediate value */
1192 if (arg[1] == '"') { /* Immediate string */
1193 ret = __parse_imm_string(arg + 2, &tmp, ctx->offset + 2);
1196 code->op = FETCH_OP_DATA;
1199 ret = str_to_immediate(arg + 1, &code->immediate);
1201 trace_probe_log_err(ctx->offset + 1, BAD_IMM);
1203 code->op = FETCH_OP_IMM;
1207 if (isalpha(arg[0]) || arg[0] == '_') { /* BTF variable */
1208 if (!tparg_is_function_entry(ctx->flags) &&
1209 !tparg_is_function_return(ctx->flags)) {
1210 trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
1213 ret = parse_btf_arg(arg, pcode, end, ctx);
1217 if (!ret && code->op == FETCH_OP_NOP) {
1218 /* Parsed, but do not find fetch method */
1219 trace_probe_log_err(ctx->offset, BAD_FETCH_ARG);
1225 /* Bitfield type needs to be parsed into a fetch function */
1226 static int __parse_bitfield_probe_arg(const char *bf,
1227 const struct fetch_type *t,
1228 struct fetch_insn **pcode)
1230 struct fetch_insn *code = *pcode;
1231 unsigned long bw, bo;
1237 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
1239 if (bw == 0 || *tail != '@')
1243 bo = simple_strtoul(bf, &tail, 0);
1245 if (tail == bf || *tail != '/')
1248 if (code->op != FETCH_OP_NOP)
1252 code->op = FETCH_OP_MOD_BF;
1253 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
1254 code->rshift = BYTES_TO_BITS(t->size) - bw;
1255 code->basesize = t->size;
1257 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
1260 /* Split type part from @arg and return it. */
1261 static char *parse_probe_arg_type(char *arg, struct probe_arg *parg,
1262 struct traceprobe_parse_context *ctx)
1264 char *t = NULL, *t2, *t3;
1267 t = strchr(arg, ':');
1270 t2 = strchr(t, '[');
1273 t3 = strchr(t2, ']');
1275 offs = t2 + strlen(t2) - arg;
1277 trace_probe_log_err(ctx->offset + offs,
1279 return ERR_PTR(-EINVAL);
1280 } else if (t3[1] != '\0') {
1281 trace_probe_log_err(ctx->offset + t3 + 1 - arg,
1283 return ERR_PTR(-EINVAL);
1286 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
1287 trace_probe_log_err(ctx->offset + t2 - arg,
1289 return ERR_PTR(-EINVAL);
1291 if (parg->count > MAX_ARRAY_LEN) {
1292 trace_probe_log_err(ctx->offset + t2 - arg,
1294 return ERR_PTR(-EINVAL);
1298 offs = t ? t - arg : 0;
1301 * Since $comm and immediate string can not be dereferenced,
1302 * we can find those by strcmp. But ignore for eprobes.
1304 if (!(ctx->flags & TPARG_FL_TEVENT) &&
1305 (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
1306 strncmp(arg, "\\\"", 2) == 0)) {
1307 /* The type of $comm must be "string", and not an array type. */
1308 if (parg->count || (t && strcmp(t, "string"))) {
1309 trace_probe_log_err(ctx->offset + offs, NEED_STRING_TYPE);
1310 return ERR_PTR(-EINVAL);
1312 parg->type = find_fetch_type("string", ctx->flags);
1314 parg->type = find_fetch_type(t, ctx->flags);
1317 trace_probe_log_err(ctx->offset + offs, BAD_TYPE);
1318 return ERR_PTR(-EINVAL);
1324 /* After parsing, adjust the fetch_insn according to the probe_arg */
1325 static int finalize_fetch_insn(struct fetch_insn *code,
1326 struct probe_arg *parg,
1329 struct traceprobe_parse_context *ctx)
1331 struct fetch_insn *scode;
1334 /* Store operation */
1335 if (parg->type->is_string) {
1336 /* Check bad combination of the type and the last fetch_insn. */
1337 if (!strcmp(parg->type->name, "symstr")) {
1338 if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
1339 code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
1340 code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
1341 trace_probe_log_err(ctx->offset + type_offset,
1346 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
1347 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
1348 code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
1349 trace_probe_log_err(ctx->offset + type_offset,
1355 if (!strcmp(parg->type->name, "symstr") ||
1356 (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
1357 code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
1360 * IMM, DATA and COMM is pointing actual address, those
1361 * must be kept, and if parg->count != 0, this is an
1362 * array of string pointers instead of string address
1364 * For the symstr, it doesn't need to dereference, thus
1365 * it just get the value.
1368 if (code->op != FETCH_OP_NOP) {
1369 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1374 /* If op == DEREF, replace it with STRING */
1375 if (!strcmp(parg->type->name, "ustring") ||
1376 code->op == FETCH_OP_UDEREF)
1377 code->op = FETCH_OP_ST_USTRING;
1378 else if (!strcmp(parg->type->name, "symstr"))
1379 code->op = FETCH_OP_ST_SYMSTR;
1381 code->op = FETCH_OP_ST_STRING;
1382 code->size = parg->type->size;
1383 parg->dynamic = true;
1384 } else if (code->op == FETCH_OP_DEREF) {
1385 code->op = FETCH_OP_ST_MEM;
1386 code->size = parg->type->size;
1387 } else if (code->op == FETCH_OP_UDEREF) {
1388 code->op = FETCH_OP_ST_UMEM;
1389 code->size = parg->type->size;
1392 if (code->op != FETCH_OP_NOP) {
1393 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1396 code->op = FETCH_OP_ST_RAW;
1397 code->size = parg->type->size;
1400 /* Save storing fetch_insn. */
1403 /* Modify operation */
1405 /* Bitfield needs a special fetch_insn. */
1406 ret = __parse_bitfield_probe_arg(type, parg->type, &code);
1408 trace_probe_log_err(ctx->offset + type_offset, BAD_BITFIELD);
1411 } else if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) &&
1413 /* If user not specified the type, try parsing BTF bitfield. */
1414 ret = parse_btf_bitfield(&code, ctx);
1419 /* Loop(Array) operation */
1421 if (scode->op != FETCH_OP_ST_MEM &&
1422 scode->op != FETCH_OP_ST_STRING &&
1423 scode->op != FETCH_OP_ST_USTRING) {
1424 trace_probe_log_err(ctx->offset + type_offset, BAD_STRING);
1428 if (code->op != FETCH_OP_NOP) {
1429 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1432 code->op = FETCH_OP_LP_ARRAY;
1433 code->param = parg->count;
1436 /* Finalize the fetch_insn array. */
1438 code->op = FETCH_OP_END;
1443 /* String length checking wrapper */
1444 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
1445 struct probe_arg *parg,
1446 struct traceprobe_parse_context *ctx)
1448 struct fetch_insn *code, *tmp = NULL;
1449 char *type, *arg __free(kfree) = NULL;
1453 if (len > MAX_ARGSTR_LEN) {
1454 trace_probe_log_err(ctx->offset, ARG_TOO_LONG);
1456 } else if (len == 0) {
1457 trace_probe_log_err(ctx->offset, NO_ARG_BODY);
1461 arg = kstrdup(argv, GFP_KERNEL);
1465 parg->comm = kstrdup(arg, GFP_KERNEL);
1469 type = parse_probe_arg_type(arg, parg, ctx);
1471 return PTR_ERR(type);
1473 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
1476 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
1478 ctx->last_type = NULL;
1479 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
1484 /* Update storing type if BTF is available */
1485 if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) &&
1488 parg->type = find_fetch_type_from_btf_type(ctx);
1489 } else if (strstr(type, "string")) {
1490 ret = check_prepare_btf_string_fetch(type, &code, ctx);
1495 parg->offset = *size;
1496 *size += parg->type->size * (parg->count ?: 1);
1499 len = strlen(parg->type->fmttype) + 6;
1500 parg->fmt = kmalloc(len, GFP_KERNEL);
1505 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
1509 ret = finalize_fetch_insn(code, parg, type, type ? type - arg : 0, ctx);
1513 for (; code < tmp + FETCH_INSN_MAX; code++)
1514 if (code->op == FETCH_OP_END)
1516 /* Shrink down the code buffer */
1517 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
1521 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
1525 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
1526 if (code->op == FETCH_NOP_SYMBOL ||
1527 code->op == FETCH_OP_DATA)
1535 /* Return 1 if name is reserved or already used by another argument */
1536 static int traceprobe_conflict_field_name(const char *name,
1537 struct probe_arg *args, int narg)
1541 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
1542 if (strcmp(reserved_field_names[i], name) == 0)
1545 for (i = 0; i < narg; i++)
1546 if (strcmp(args[i].name, name) == 0)
1552 static char *generate_probe_arg_name(const char *arg, int idx)
1558 * If argument name is omitted, try arg as a name (BTF variable)
1561 if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
1562 end = strchr(arg, ':');
1564 end = arg + strlen(arg);
1566 name = kmemdup_nul(arg, end - arg, GFP_KERNEL);
1567 if (!name || !is_good_name(name)) {
1574 name = kasprintf(GFP_KERNEL, "arg%d", idx + 1);
1579 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
1580 struct traceprobe_parse_context *ctx)
1582 struct probe_arg *parg = &tp->args[i];
1586 body = strchr(arg, '=');
1588 if (body - arg > MAX_ARG_NAME_LEN) {
1589 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
1591 } else if (body == arg) {
1592 trace_probe_log_err(0, NO_ARG_NAME);
1595 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
1598 parg->name = generate_probe_arg_name(arg, i);
1604 if (!is_good_name(parg->name)) {
1605 trace_probe_log_err(0, BAD_ARG_NAME);
1608 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
1609 trace_probe_log_err(0, USED_ARG_NAME);
1612 ctx->offset = body - arg;
1613 /* Parse fetch argument */
1614 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, ctx);
1617 void traceprobe_free_probe_arg(struct probe_arg *arg)
1619 struct fetch_insn *code = arg->code;
1621 while (code && code->op != FETCH_OP_END) {
1622 if (code->op == FETCH_NOP_SYMBOL ||
1623 code->op == FETCH_OP_DATA)
1633 static int argv_has_var_arg(int argc, const char *argv[], int *args_idx,
1634 struct traceprobe_parse_context *ctx)
1638 for (i = 0; i < argc; i++)
1639 if (str_has_prefix(argv[i], "$arg")) {
1640 trace_probe_log_set_index(i + 2);
1642 if (!tparg_is_function_entry(ctx->flags) &&
1643 !tparg_is_function_return(ctx->flags)) {
1644 trace_probe_log_err(0, NOFENTRY_ARGS);
1648 if (isdigit(argv[i][4])) {
1653 if (argv[i][4] != '*') {
1654 trace_probe_log_err(0, BAD_VAR);
1658 if (*args_idx >= 0 && *args_idx < argc) {
1659 trace_probe_log_err(0, DOUBLE_ARGS);
1669 static int sprint_nth_btf_arg(int idx, const char *type,
1670 char *buf, int bufsize,
1671 struct traceprobe_parse_context *ctx)
1676 if (idx >= ctx->nr_params) {
1677 trace_probe_log_err(0, NO_BTFARG);
1680 name = btf_name_by_offset(ctx->btf, ctx->params[idx].name_off);
1682 trace_probe_log_err(0, NO_BTF_ENTRY);
1685 ret = snprintf(buf, bufsize, "%s%s", name, type);
1686 if (ret >= bufsize) {
1687 trace_probe_log_err(0, ARGS_2LONG);
1693 /* Return new_argv which must be freed after use */
1694 const char **traceprobe_expand_meta_args(int argc, const char *argv[],
1695 int *new_argc, char *buf, int bufsize,
1696 struct traceprobe_parse_context *ctx)
1698 const struct btf_param *params = NULL;
1699 int i, j, n, used, ret, args_idx = -1;
1700 const char **new_argv __free(kfree) = NULL;
1702 ret = argv_has_var_arg(argc, argv, &args_idx, ctx);
1704 return ERR_PTR(ret);
1711 ret = query_btf_context(ctx);
1712 if (ret < 0 || ctx->nr_params == 0) {
1713 if (args_idx != -1) {
1714 /* $arg* requires BTF info */
1715 trace_probe_log_err(0, NOSUP_BTFARG);
1716 return (const char **)params;
1723 *new_argc = argc + ctx->nr_params - 1;
1727 new_argv = kcalloc(*new_argc, sizeof(char *), GFP_KERNEL);
1729 return ERR_PTR(-ENOMEM);
1732 for (i = 0, j = 0; i < argc; i++) {
1733 trace_probe_log_set_index(i + 2);
1734 if (i == args_idx) {
1735 for (n = 0; n < ctx->nr_params; n++) {
1736 ret = sprint_nth_btf_arg(n, "", buf + used,
1737 bufsize - used, ctx);
1739 return ERR_PTR(ret);
1741 new_argv[j++] = buf + used;
1747 if (str_has_prefix(argv[i], "$arg")) {
1750 n = simple_strtoul(argv[i] + 4, &type, 10);
1751 if (type && !(*type == ':' || *type == '\0')) {
1752 trace_probe_log_err(0, BAD_VAR);
1753 return ERR_PTR(-ENOENT);
1755 /* Note: $argN starts from $arg1 */
1756 ret = sprint_nth_btf_arg(n - 1, type, buf + used,
1757 bufsize - used, ctx);
1759 return ERR_PTR(ret);
1760 new_argv[j++] = buf + used;
1763 new_argv[j++] = argv[i];
1766 return_ptr(new_argv);
1769 /* @buf: *buf must be equal to NULL. Caller must to free *buf */
1770 int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf)
1773 const int bufsize = MAX_DENTRY_ARGS_LEN;
1774 char *tmpbuf __free(kfree) = NULL;
1780 for (i = 0; i < argc; i++) {
1781 char *tmp __free(kfree) = NULL;
1785 if (!glob_match("*:%p[dD]", argv[i]))
1789 tmpbuf = kmalloc(bufsize, GFP_KERNEL);
1794 tmp = kstrdup(argv[i], GFP_KERNEL);
1798 equal = strchr(tmp, '=');
1801 arg_len = strlen(argv[i]);
1802 tmp[arg_len - 4] = '\0';
1803 if (argv[i][arg_len - 1] == 'd')
1804 ret = snprintf(tmpbuf + used, bufsize - used,
1805 "%s%s+0x0(+0x%zx(%s)):string",
1806 equal ? tmp : "", equal ? "=" : "",
1807 offsetof(struct dentry, d_name.name),
1808 equal ? equal + 1 : tmp);
1810 ret = snprintf(tmpbuf + used, bufsize - used,
1811 "%s%s+0x0(+0x%zx(+0x%zx(%s))):string",
1812 equal ? tmp : "", equal ? "=" : "",
1813 offsetof(struct dentry, d_name.name),
1814 offsetof(struct file, f_path.dentry),
1815 equal ? equal + 1 : tmp);
1817 if (ret >= bufsize - used)
1819 argv[i] = tmpbuf + used;
1823 *buf = no_free_ptr(tmpbuf);
1827 void traceprobe_finish_parse(struct traceprobe_parse_context *ctx)
1829 clear_btf_context(ctx);
1832 int traceprobe_update_arg(struct probe_arg *arg)
1834 struct fetch_insn *code = arg->code;
1840 while (code && code->op != FETCH_OP_END) {
1841 if (code->op == FETCH_NOP_SYMBOL) {
1842 if (code[1].op != FETCH_OP_IMM)
1845 tmp = strpbrk(code->data, "+-");
1848 ret = traceprobe_split_symbol_offset(code->data,
1854 (unsigned long)kallsyms_lookup_name(code->data);
1857 if (!code[1].immediate)
1859 code[1].immediate += offset;
1866 /* When len=0, we just calculate the needed length */
1867 #define LEN_OR_ZERO (len ? len - pos : 0)
1868 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
1869 enum probe_print_type ptype)
1871 struct probe_arg *parg;
1874 const char *fmt, *arg;
1877 case PROBE_PRINT_NORMAL:
1879 arg = ", REC->" FIELD_STRING_IP;
1881 case PROBE_PRINT_RETURN:
1882 fmt = "(%lx <- %lx)";
1883 arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1885 case PROBE_PRINT_EVENT:
1894 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
1896 for (i = 0; i < tp->nr_args; i++) {
1897 parg = tp->args + i;
1898 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
1900 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
1902 for (j = 1; j < parg->count; j++)
1903 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
1905 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
1907 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
1911 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg);
1913 for (i = 0; i < tp->nr_args; i++) {
1914 parg = tp->args + i;
1916 if (parg->type->is_string)
1917 fmt = ", __get_str(%s[%d])";
1919 fmt = ", REC->%s[%d]";
1920 for (j = 0; j < parg->count; j++)
1921 pos += snprintf(buf + pos, LEN_OR_ZERO,
1922 fmt, parg->name, j);
1924 if (parg->type->is_string)
1925 fmt = ", __get_str(%s)";
1928 pos += snprintf(buf + pos, LEN_OR_ZERO,
1933 /* return the length of print_fmt */
1938 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
1940 struct trace_event_call *call = trace_probe_event_call(tp);
1944 /* First: called with 0 length to calculate the needed length */
1945 len = __set_print_fmt(tp, NULL, 0, ptype);
1946 print_fmt = kmalloc(len + 1, GFP_KERNEL);
1950 /* Second: actually write the @print_fmt */
1951 __set_print_fmt(tp, print_fmt, len + 1, ptype);
1952 call->print_fmt = print_fmt;
1957 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
1958 size_t offset, struct trace_probe *tp)
1962 /* Set argument names as fields */
1963 for (i = 0; i < tp->nr_args; i++) {
1964 struct probe_arg *parg = &tp->args[i];
1965 const char *fmt = parg->type->fmttype;
1966 int size = parg->type->size;
1971 size *= parg->count;
1972 ret = trace_define_field(event_call, fmt, parg->name,
1973 offset + parg->offset, size,
1974 parg->type->is_signed,
1982 static void trace_probe_event_free(struct trace_probe_event *tpe)
1984 kfree(tpe->class.system);
1985 kfree(tpe->call.name);
1986 kfree(tpe->call.print_fmt);
1990 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
1992 if (trace_probe_has_sibling(tp))
1995 list_del_init(&tp->list);
1996 trace_probe_event_free(tp->event);
1998 tp->event = to->event;
1999 list_add_tail(&tp->list, trace_probe_probe_list(to));
2004 void trace_probe_unlink(struct trace_probe *tp)
2006 list_del_init(&tp->list);
2007 if (list_empty(trace_probe_probe_list(tp)))
2008 trace_probe_event_free(tp->event);
2012 void trace_probe_cleanup(struct trace_probe *tp)
2016 for (i = 0; i < tp->nr_args; i++)
2017 traceprobe_free_probe_arg(&tp->args[i]);
2019 if (tp->entry_arg) {
2020 kfree(tp->entry_arg->code);
2021 kfree(tp->entry_arg);
2022 tp->entry_arg = NULL;
2026 trace_probe_unlink(tp);
2029 int trace_probe_init(struct trace_probe *tp, const char *event,
2030 const char *group, bool alloc_filter, int nargs)
2032 struct trace_event_call *call;
2033 size_t size = sizeof(struct trace_probe_event);
2036 if (!event || !group)
2040 size += sizeof(struct trace_uprobe_filter);
2042 tp->event = kzalloc(size, GFP_KERNEL);
2046 INIT_LIST_HEAD(&tp->event->files);
2047 INIT_LIST_HEAD(&tp->event->class.fields);
2048 INIT_LIST_HEAD(&tp->event->probes);
2049 INIT_LIST_HEAD(&tp->list);
2050 list_add(&tp->list, &tp->event->probes);
2052 call = trace_probe_event_call(tp);
2053 call->class = &tp->event->class;
2054 call->name = kstrdup(event, GFP_KERNEL);
2060 tp->event->class.system = kstrdup(group, GFP_KERNEL);
2061 if (!tp->event->class.system) {
2066 tp->nr_args = nargs;
2067 /* Make sure pointers in args[] are NULL */
2069 memset(tp->args, 0, sizeof(tp->args[0]) * nargs);
2074 trace_probe_cleanup(tp);
2078 static struct trace_event_call *
2079 find_trace_event_call(const char *system, const char *event_name)
2081 struct trace_event_call *tp_event;
2084 list_for_each_entry(tp_event, &ftrace_events, list) {
2085 if (!tp_event->class->system ||
2086 strcmp(system, tp_event->class->system))
2088 name = trace_event_name(tp_event);
2089 if (!name || strcmp(event_name, name))
2097 int trace_probe_register_event_call(struct trace_probe *tp)
2099 struct trace_event_call *call = trace_probe_event_call(tp);
2102 lockdep_assert_held(&event_mutex);
2104 if (find_trace_event_call(trace_probe_group_name(tp),
2105 trace_probe_name(tp)))
2108 ret = register_trace_event(&call->event);
2112 ret = trace_add_event_call(call);
2114 unregister_trace_event(&call->event);
2119 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
2121 struct event_file_link *link;
2123 link = kmalloc(sizeof(*link), GFP_KERNEL);
2128 INIT_LIST_HEAD(&link->list);
2129 list_add_tail_rcu(&link->list, &tp->event->files);
2130 trace_probe_set_flag(tp, TP_FLAG_TRACE);
2134 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
2135 struct trace_event_file *file)
2137 struct event_file_link *link;
2139 trace_probe_for_each_link(link, tp) {
2140 if (link->file == file)
2147 int trace_probe_remove_file(struct trace_probe *tp,
2148 struct trace_event_file *file)
2150 struct event_file_link *link;
2152 link = trace_probe_get_file_link(tp, file);
2156 list_del_rcu(&link->list);
2157 kvfree_rcu_mightsleep(link);
2159 if (list_empty(&tp->event->files))
2160 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
2166 * Return the smallest index of different type argument (start from 1).
2167 * If all argument types and name are same, return 0.
2169 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
2173 /* In case of more arguments */
2174 if (a->nr_args < b->nr_args)
2175 return a->nr_args + 1;
2176 if (a->nr_args > b->nr_args)
2177 return b->nr_args + 1;
2179 for (i = 0; i < a->nr_args; i++) {
2180 if ((b->nr_args <= i) ||
2181 ((a->args[i].type != b->args[i].type) ||
2182 (a->args[i].count != b->args[i].count) ||
2183 strcmp(a->args[i].name, b->args[i].name)))
2190 bool trace_probe_match_command_args(struct trace_probe *tp,
2191 int argc, const char **argv)
2193 char buf[MAX_ARGSTR_LEN + 1];
2196 if (tp->nr_args < argc)
2199 for (i = 0; i < argc; i++) {
2200 snprintf(buf, sizeof(buf), "%s=%s",
2201 tp->args[i].name, tp->args[i].comm);
2202 if (strcmp(buf, argv[i]))
2208 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
2210 int argc = 0, ret = 0;
2213 argv = argv_split(GFP_KERNEL, raw_command, &argc);
2218 ret = createfn(argc, (const char **)argv);
2225 int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_args,
2226 u8 *data, void *field)
2231 for (i = 0; i < nr_args; i++) {
2232 struct probe_arg *a = args + i;
2234 trace_seq_printf(s, " %s=", a->name);
2235 if (likely(!a->count)) {
2236 if (!a->type->print(s, data + a->offset, field))
2240 trace_seq_putc(s, '{');
2241 p = data + a->offset;
2242 for (j = 0; j < a->count; j++) {
2243 if (!a->type->print(s, p, field))
2245 trace_seq_putc(s, j == a->count - 1 ? '}' : ',');