mirror of
				https://github.com/torvalds/linux.git
				synced 2025-10-31 16:48:26 +02:00 
			
		
		
		
	tracing/probes: Support BTF field access from $retval
Support BTF argument on '$retval' for function return events including
kretprobe and fprobe for accessing the return value.
This also allows user to access its fields if the return value is a
pointer of a data structure.
E.g.
 # echo 'f getname_flags%return +0($retval->name):string' \
   > dynamic_events
 # echo 1 > events/fprobes/getname_flags__exit/enable
 # ls > /dev/null
 # head -n 40 trace | tail
              ls-87      [000] ...1.  8067.616101: getname_flags__exit: (vfs_fstatat+0x3c/0x70 <- getname_flags) arg1="./function_profile_enabled"
              ls-87      [000] ...1.  8067.616108: getname_flags__exit: (vfs_fstatat+0x3c/0x70 <- getname_flags) arg1="./trace_stat"
              ls-87      [000] ...1.  8067.616115: getname_flags__exit: (vfs_fstatat+0x3c/0x70 <- getname_flags) arg1="./set_graph_notrace"
              ls-87      [000] ...1.  8067.616122: getname_flags__exit: (vfs_fstatat+0x3c/0x70 <- getname_flags) arg1="./set_graph_function"
              ls-87      [000] ...1.  8067.616129: getname_flags__exit: (vfs_fstatat+0x3c/0x70 <- getname_flags) arg1="./set_ftrace_notrace"
              ls-87      [000] ...1.  8067.616135: getname_flags__exit: (vfs_fstatat+0x3c/0x70 <- getname_flags) arg1="./set_ftrace_filter"
              ls-87      [000] ...1.  8067.616143: getname_flags__exit: (vfs_fstatat+0x3c/0x70 <- getname_flags) arg1="./touched_functions"
              ls-87      [000] ...1.  8067.616237: getname_flags__exit: (vfs_fstatat+0x3c/0x70 <- getname_flags) arg1="./enabled_functions"
              ls-87      [000] ...1.  8067.616245: getname_flags__exit: (vfs_fstatat+0x3c/0x70 <- getname_flags) arg1="./available_filter_functions"
              ls-87      [000] ...1.  8067.616253: getname_flags__exit: (vfs_fstatat+0x3c/0x70 <- getname_flags) arg1="./set_ftrace_notrace_pid"
Link: https://lore.kernel.org/all/169272158234.160970.2446691104240645205.stgit@devnote2/
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Acked-by: Steven Rostedt (Google) <rostedt@goodmis.org>
			
			
This commit is contained in:
		
							parent
							
								
									c440adfbe3
								
							
						
					
					
						commit
						d157d76944
					
				
					 2 changed files with 86 additions and 102 deletions
				
			
		|  | @ -364,38 +364,46 @@ static const char *fetch_type_from_btf_type(struct btf *btf, | ||||||
| 	return NULL; | 	return NULL; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, | static int query_btf_context(struct traceprobe_parse_context *ctx) | ||||||
| 						   struct btf **btf_p, bool tracepoint) |  | ||||||
| { | { | ||||||
| 	const struct btf_param *param; | 	const struct btf_param *param; | ||||||
| 	const struct btf_type *t; | 	const struct btf_type *type; | ||||||
| 	struct btf *btf; | 	struct btf *btf; | ||||||
|  | 	s32 nr; | ||||||
| 
 | 
 | ||||||
| 	if (!funcname || !nr) | 	if (ctx->btf) | ||||||
| 		return ERR_PTR(-EINVAL); | 		return 0; | ||||||
| 
 | 
 | ||||||
| 	t = btf_find_func_proto(funcname, &btf); | 	if (!ctx->funcname) | ||||||
| 	if (!t) | 		return -EINVAL; | ||||||
| 		return (const struct btf_param *)t; |  | ||||||
| 
 | 
 | ||||||
| 	param = btf_get_func_param(t, nr); | 	type = btf_find_func_proto(ctx->funcname, &btf); | ||||||
| 	if (IS_ERR_OR_NULL(param)) | 	if (!type) | ||||||
| 		goto err; | 		return -ENOENT; | ||||||
| 
 | 
 | ||||||
| 	/* Hide the first 'data' argument of tracepoint */ | 	ctx->btf = btf; | ||||||
| 	if (tracepoint) { | 	ctx->proto = type; | ||||||
| 		(*nr)--; | 
 | ||||||
| 		param++; | 	/* ctx->params is optional, since func(void) will not have params. */ | ||||||
|  | 	nr = 0; | ||||||
|  | 	param = btf_get_func_param(type, &nr); | ||||||
|  | 	if (!IS_ERR_OR_NULL(param)) { | ||||||
|  | 		/* Hide the first 'data' argument of tracepoint */ | ||||||
|  | 		if (ctx->flags & TPARG_FL_TPOINT) { | ||||||
|  | 			nr--; | ||||||
|  | 			param++; | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if (*nr > 0) { | 	if (nr > 0) { | ||||||
| 		*btf_p = btf; | 		ctx->nr_params = nr; | ||||||
| 		return param; | 		ctx->params = param; | ||||||
|  | 	} else { | ||||||
|  | 		ctx->nr_params = 0; | ||||||
|  | 		ctx->params = NULL; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| err: | 	return 0; | ||||||
| 	btf_put(btf); |  | ||||||
| 	return NULL; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static void clear_btf_context(struct traceprobe_parse_context *ctx) | static void clear_btf_context(struct traceprobe_parse_context *ctx) | ||||||
|  | @ -403,6 +411,7 @@ static void clear_btf_context(struct traceprobe_parse_context *ctx) | ||||||
| 	if (ctx->btf) { | 	if (ctx->btf) { | ||||||
| 		btf_put(ctx->btf); | 		btf_put(ctx->btf); | ||||||
| 		ctx->btf = NULL; | 		ctx->btf = NULL; | ||||||
|  | 		ctx->proto = NULL; | ||||||
| 		ctx->params = NULL; | 		ctx->params = NULL; | ||||||
| 		ctx->nr_params = 0; | 		ctx->nr_params = 0; | ||||||
| 	} | 	} | ||||||
|  | @ -522,7 +531,7 @@ static int parse_btf_arg(char *varname, | ||||||
| 	const struct btf_param *params; | 	const struct btf_param *params; | ||||||
| 	const struct btf_type *type; | 	const struct btf_type *type; | ||||||
| 	char *field = NULL; | 	char *field = NULL; | ||||||
| 	int i, is_ptr; | 	int i, is_ptr, ret; | ||||||
| 	u32 tid; | 	u32 tid; | ||||||
| 
 | 
 | ||||||
| 	if (WARN_ON_ONCE(!ctx->funcname)) | 	if (WARN_ON_ONCE(!ctx->funcname)) | ||||||
|  | @ -538,17 +547,37 @@ static int parse_btf_arg(char *varname, | ||||||
| 		return -EOPNOTSUPP; | 		return -EOPNOTSUPP; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if (!ctx->params) { | 	if (ctx->flags & TPARG_FL_RETURN) { | ||||||
| 		params = find_btf_func_param(ctx->funcname, | 		if (strcmp(varname, "$retval") != 0) { | ||||||
| 					     &ctx->nr_params, &ctx->btf, | 			trace_probe_log_err(ctx->offset, NO_BTFARG); | ||||||
| 					     ctx->flags & TPARG_FL_TPOINT); | 			return -ENOENT; | ||||||
| 		if (IS_ERR_OR_NULL(params)) { | 		} | ||||||
|  | 		code->op = FETCH_OP_RETVAL; | ||||||
|  | 		/* Check whether the function return type is not void */ | ||||||
|  | 		if (query_btf_context(ctx) == 0) { | ||||||
|  | 			if (ctx->proto->type == 0) { | ||||||
|  | 				trace_probe_log_err(ctx->offset, NO_RETVAL); | ||||||
|  | 				return -ENOENT; | ||||||
|  | 			} | ||||||
|  | 			tid = ctx->proto->type; | ||||||
|  | 			goto found; | ||||||
|  | 		} | ||||||
|  | 		if (field) { | ||||||
|  | 			trace_probe_log_err(ctx->offset + field - varname, | ||||||
|  | 					    NO_BTF_ENTRY); | ||||||
|  | 			return -ENOENT; | ||||||
|  | 		} | ||||||
|  | 		return 0; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if (!ctx->btf) { | ||||||
|  | 		ret = query_btf_context(ctx); | ||||||
|  | 		if (ret < 0 || ctx->nr_params == 0) { | ||||||
| 			trace_probe_log_err(ctx->offset, NO_BTF_ENTRY); | 			trace_probe_log_err(ctx->offset, NO_BTF_ENTRY); | ||||||
| 			return PTR_ERR(params); | 			return PTR_ERR(params); | ||||||
| 		} | 		} | ||||||
| 		ctx->params = params; | 	} | ||||||
| 	} else | 	params = ctx->params; | ||||||
| 		params = ctx->params; |  | ||||||
| 
 | 
 | ||||||
| 	for (i = 0; i < ctx->nr_params; i++) { | 	for (i = 0; i < ctx->nr_params; i++) { | ||||||
| 		const char *name = btf_name_by_offset(ctx->btf, params[i].name_off); | 		const char *name = btf_name_by_offset(ctx->btf, params[i].name_off); | ||||||
|  | @ -559,7 +588,6 @@ static int parse_btf_arg(char *varname, | ||||||
| 				code->param = i + 1; | 				code->param = i + 1; | ||||||
| 			else | 			else | ||||||
| 				code->param = i; | 				code->param = i; | ||||||
| 
 |  | ||||||
| 			tid = params[i].type; | 			tid = params[i].type; | ||||||
| 			goto found; | 			goto found; | ||||||
| 		} | 		} | ||||||
|  | @ -584,7 +612,7 @@ static int parse_btf_arg(char *varname, | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static const struct fetch_type *parse_btf_arg_type( | static const struct fetch_type *find_fetch_type_from_btf_type( | ||||||
| 					struct traceprobe_parse_context *ctx) | 					struct traceprobe_parse_context *ctx) | ||||||
| { | { | ||||||
| 	struct btf *btf = ctx->btf; | 	struct btf *btf = ctx->btf; | ||||||
|  | @ -596,27 +624,6 @@ static const struct fetch_type *parse_btf_arg_type( | ||||||
| 	return find_fetch_type(typestr, ctx->flags); | 	return find_fetch_type(typestr, ctx->flags); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static const struct fetch_type *parse_btf_retval_type( |  | ||||||
| 					struct traceprobe_parse_context *ctx) |  | ||||||
| { |  | ||||||
| 	const char *typestr = NULL; |  | ||||||
| 	const struct btf_type *type; |  | ||||||
| 	struct btf *btf; |  | ||||||
| 
 |  | ||||||
| 	if (ctx->funcname) { |  | ||||||
| 		/* Do not use ctx->btf, because it must be used with ctx->param */ |  | ||||||
| 		type = btf_find_func_proto(ctx->funcname, &btf); |  | ||||||
| 		if (type) { |  | ||||||
| 			type = btf_type_skip_modifiers(btf, type->type, NULL); |  | ||||||
| 			if (!IS_ERR_OR_NULL(type)) |  | ||||||
| 				typestr = fetch_type_from_btf_type(btf, type, ctx); |  | ||||||
| 			btf_put(btf); |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return find_fetch_type(typestr, ctx->flags); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| static int parse_btf_bitfield(struct fetch_insn **pcode, | static int parse_btf_bitfield(struct fetch_insn **pcode, | ||||||
| 			      struct traceprobe_parse_context *ctx) | 			      struct traceprobe_parse_context *ctx) | ||||||
| { | { | ||||||
|  | @ -639,30 +646,15 @@ static int parse_btf_bitfield(struct fetch_insn **pcode, | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static bool is_btf_retval_void(const char *funcname) |  | ||||||
| { |  | ||||||
| 	const struct btf_type *t; |  | ||||||
| 	struct btf *btf; |  | ||||||
| 	bool ret; |  | ||||||
| 
 |  | ||||||
| 	t = btf_find_func_proto(funcname, &btf); |  | ||||||
| 	if (!t) |  | ||||||
| 		return false; |  | ||||||
| 
 |  | ||||||
| 	ret = (t->type == 0); |  | ||||||
| 	btf_put(btf); |  | ||||||
| 	return ret; |  | ||||||
| } |  | ||||||
| #else | #else | ||||||
| static void clear_btf_context(struct traceprobe_parse_context *ctx) | static void clear_btf_context(struct traceprobe_parse_context *ctx) | ||||||
| { | { | ||||||
| 	ctx->btf = NULL; | 	ctx->btf = NULL; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, | static int query_btf_context(struct traceprobe_parse_context *ctx) | ||||||
| 						   struct btf **btf_p, bool tracepoint) |  | ||||||
| { | { | ||||||
| 	return ERR_PTR(-EOPNOTSUPP); | 	return -EOPNOTSUPP; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static int parse_btf_arg(char *varname, | static int parse_btf_arg(char *varname, | ||||||
|  | @ -680,24 +672,23 @@ static int parse_btf_bitfield(struct fetch_insn **pcode, | ||||||
| 	return -EOPNOTSUPP; | 	return -EOPNOTSUPP; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #define parse_btf_arg_type(ctx)		\ | #define find_fetch_type_from_btf_type(ctx)		\ | ||||||
| 	find_fetch_type(NULL, ctx->flags) | 	find_fetch_type(NULL, ctx->flags) | ||||||
| 
 | 
 | ||||||
| #define parse_btf_retval_type(ctx)		\ |  | ||||||
| 	find_fetch_type(NULL, ctx->flags) |  | ||||||
| 
 |  | ||||||
| #define is_btf_retval_void(funcname)	(false) |  | ||||||
| 
 |  | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) | ||||||
| 
 | 
 | ||||||
| static int parse_probe_vars(char *arg, const struct fetch_type *t, | /* Parse $vars. @orig_arg points '$', which syncs to @ctx->offset */ | ||||||
| 			    struct fetch_insn *code, | static int parse_probe_vars(char *orig_arg, const struct fetch_type *t, | ||||||
|  | 			    struct fetch_insn **pcode, | ||||||
|  | 			    struct fetch_insn *end, | ||||||
| 			    struct traceprobe_parse_context *ctx) | 			    struct traceprobe_parse_context *ctx) | ||||||
| { | { | ||||||
| 	unsigned long param; | 	struct fetch_insn *code = *pcode; | ||||||
| 	int err = TP_ERR_BAD_VAR; | 	int err = TP_ERR_BAD_VAR; | ||||||
|  | 	char *arg = orig_arg + 1; | ||||||
|  | 	unsigned long param; | ||||||
| 	int ret = 0; | 	int ret = 0; | ||||||
| 	int len; | 	int len; | ||||||
| 
 | 
 | ||||||
|  | @ -716,18 +707,17 @@ static int parse_probe_vars(char *arg, const struct fetch_type *t, | ||||||
| 		goto inval; | 		goto inval; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if (strcmp(arg, "retval") == 0) { | 	if (str_has_prefix(arg, "retval")) { | ||||||
| 		if (ctx->flags & TPARG_FL_RETURN) { | 		if (!(ctx->flags & TPARG_FL_RETURN)) { | ||||||
| 			if ((ctx->flags & TPARG_FL_KERNEL) && | 			err = TP_ERR_RETVAL_ON_PROBE; | ||||||
| 			    is_btf_retval_void(ctx->funcname)) { | 			goto inval; | ||||||
| 				err = TP_ERR_NO_RETVAL; | 		} | ||||||
| 				goto inval; | 		if (!(ctx->flags & TPARG_FL_KERNEL) || | ||||||
| 			} | 		    !IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) { | ||||||
| 			code->op = FETCH_OP_RETVAL; | 			code->op = FETCH_OP_RETVAL; | ||||||
| 			return 0; | 			return 0; | ||||||
| 		} | 		} | ||||||
| 		err = TP_ERR_RETVAL_ON_PROBE; | 		return parse_btf_arg(orig_arg, pcode, end, ctx); | ||||||
| 		goto inval; |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	len = str_has_prefix(arg, "stack"); | 	len = str_has_prefix(arg, "stack"); | ||||||
|  | @ -829,7 +819,7 @@ parse_probe_arg(char *arg, const struct fetch_type *type, | ||||||
| 
 | 
 | ||||||
| 	switch (arg[0]) { | 	switch (arg[0]) { | ||||||
| 	case '$': | 	case '$': | ||||||
| 		ret = parse_probe_vars(arg + 1, type, code, ctx); | 		ret = parse_probe_vars(arg, type, pcode, end, ctx); | ||||||
| 		break; | 		break; | ||||||
| 
 | 
 | ||||||
| 	case '%':	/* named register */ | 	case '%':	/* named register */ | ||||||
|  | @ -1126,12 +1116,9 @@ static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, | ||||||
| 		goto fail; | 		goto fail; | ||||||
| 
 | 
 | ||||||
| 	/* Update storing type if BTF is available */ | 	/* Update storing type if BTF is available */ | ||||||
| 	if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) && !t) { | 	if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) && | ||||||
| 		if (ctx->last_type) | 	    !t && ctx->last_type) | ||||||
| 			parg->type = parse_btf_arg_type(ctx); | 		parg->type = find_fetch_type_from_btf_type(ctx); | ||||||
| 		else if (ctx->flags & TPARG_FL_RETURN) |  | ||||||
| 			parg->type = parse_btf_retval_type(ctx); |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	ret = -EINVAL; | 	ret = -EINVAL; | ||||||
| 	/* Store operation */ | 	/* Store operation */ | ||||||
|  | @ -1420,7 +1407,6 @@ const char **traceprobe_expand_meta_args(int argc, const char *argv[], | ||||||
| 	const struct btf_param *params = NULL; | 	const struct btf_param *params = NULL; | ||||||
| 	int i, j, n, used, ret, args_idx = -1; | 	int i, j, n, used, ret, args_idx = -1; | ||||||
| 	const char **new_argv = NULL; | 	const char **new_argv = NULL; | ||||||
| 	int nr_params; |  | ||||||
| 
 | 
 | ||||||
| 	ret = argv_has_var_arg(argc, argv, &args_idx, ctx); | 	ret = argv_has_var_arg(argc, argv, &args_idx, ctx); | ||||||
| 	if (ret < 0) | 	if (ret < 0) | ||||||
|  | @ -1431,9 +1417,8 @@ const char **traceprobe_expand_meta_args(int argc, const char *argv[], | ||||||
| 		return NULL; | 		return NULL; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	params = find_btf_func_param(ctx->funcname, &nr_params, &ctx->btf, | 	ret = query_btf_context(ctx); | ||||||
| 				     ctx->flags & TPARG_FL_TPOINT); | 	if (ret < 0 || ctx->nr_params == 0) { | ||||||
| 	if (IS_ERR_OR_NULL(params)) { |  | ||||||
| 		if (args_idx != -1) { | 		if (args_idx != -1) { | ||||||
| 			/* $arg* requires BTF info */ | 			/* $arg* requires BTF info */ | ||||||
| 			trace_probe_log_err(0, NOSUP_BTFARG); | 			trace_probe_log_err(0, NOSUP_BTFARG); | ||||||
|  | @ -1442,8 +1427,6 @@ const char **traceprobe_expand_meta_args(int argc, const char *argv[], | ||||||
| 		*new_argc = argc; | 		*new_argc = argc; | ||||||
| 		return NULL; | 		return NULL; | ||||||
| 	} | 	} | ||||||
| 	ctx->params = params; |  | ||||||
| 	ctx->nr_params = nr_params; |  | ||||||
| 
 | 
 | ||||||
| 	if (args_idx >= 0) | 	if (args_idx >= 0) | ||||||
| 		*new_argc = argc + ctx->nr_params - 1; | 		*new_argc = argc + ctx->nr_params - 1; | ||||||
|  | @ -1458,7 +1441,7 @@ const char **traceprobe_expand_meta_args(int argc, const char *argv[], | ||||||
| 	for (i = 0, j = 0; i < argc; i++) { | 	for (i = 0, j = 0; i < argc; i++) { | ||||||
| 		trace_probe_log_set_index(i + 2); | 		trace_probe_log_set_index(i + 2); | ||||||
| 		if (i == args_idx) { | 		if (i == args_idx) { | ||||||
| 			for (n = 0; n < nr_params; n++) { | 			for (n = 0; n < ctx->nr_params; n++) { | ||||||
| 				ret = sprint_nth_btf_arg(n, "", buf + used, | 				ret = sprint_nth_btf_arg(n, "", buf + used, | ||||||
| 							 bufsize - used, ctx); | 							 bufsize - used, ctx); | ||||||
| 				if (ret < 0) | 				if (ret < 0) | ||||||
|  |  | ||||||
|  | @ -385,6 +385,7 @@ struct traceprobe_parse_context { | ||||||
| 	struct trace_event_call *event; | 	struct trace_event_call *event; | ||||||
| 	/* BTF related parameters */ | 	/* BTF related parameters */ | ||||||
| 	const char *funcname;		/* Function name in BTF */ | 	const char *funcname;		/* Function name in BTF */ | ||||||
|  | 	const struct btf_type  *proto;	/* Prototype of the function */ | ||||||
| 	const struct btf_param *params;	/* Parameter of the function */ | 	const struct btf_param *params;	/* Parameter of the function */ | ||||||
| 	s32 nr_params;			/* The number of the parameters */ | 	s32 nr_params;			/* The number of the parameters */ | ||||||
| 	struct btf *btf;		/* The BTF to be used */ | 	struct btf *btf;		/* The BTF to be used */ | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue
	
	 Masami Hiramatsu (Google)
						Masami Hiramatsu (Google)