[PATCH v2 1/2] libtraceevent: Split out btf func init code from tep_btf_print_args()

Steven Rostedt <[email protected]> Tue, 3 Feb 2026 17:27:26 -0500
Newsgroups org.kernel.vger.linux-trace-devel
Message-ID <[email protected]>
From: "Steven Rostedt (Google)" <[email protected]>

In order to use BTF for various other attributes of functions, move out the
btf function initialization from the tp_btf_print_args() and into its own
function.

Signed-off-by: Steven Rostedt (Google) <[email protected]>
---
 src/trace-btf.c | 70 +++++++++++++++++++++++++++++++------------------
 1 file changed, 45 insertions(+), 25 deletions(-)

diff --git a/src/trace-btf.c b/src/trace-btf.c
index d0231b49d144..0b733bf9cb75 100644
--- a/src/trace-btf.c
+++ b/src/trace-btf.c
@@ -330,35 +330,13 @@ static void assign_arg(unsigned long long *arg, void *args, int size, int a)
 		*(unsigned long long *)(args + a * sizeof(long long));
 }
 
-/**
- * tep_btf_print_args - Print function arguments from BTF info
- * @tep: The tep descriptor to use
- * @s: The trace_seq to write the arguments into
- * @args: The array that holds the arguments
- * @nmem: The number of arguments
- * @size: The size of each item in args (4 or 8).
- * @func: The name of the function.
- *
- * Loads up the @s with a list of arguments for the function based on
- * the @args.
- *
- * If there's no BTF loaded or @func is not found, then it just writes
- * the @args as raw numbers. Otherwise it will pretty print the
- * arguments based on the function info in BTF.
- *
- * Returns: 0 on success and -1 on failure.
- */
-int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
-		       int nmem, int size, const char *func)
+static int init_btf_func(struct tep_btf *btf, struct trace_seq *s,
+			 void *args, int nmem, int size,
+			 const char *func, struct btf_type **p_type)
 {
-	struct tep_btf *btf = tep->btf;
 	struct btf_type *type = tep_btf_find_func(btf, func);
-	struct btf_param *param;
 	unsigned long long arg;
-	unsigned int encode;
-	const char *param_name;
 	const char *fp;
-	int a, p, x, nr;
 
 	if (size != 4 && size != 8)
 		return -1;
@@ -381,6 +359,7 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
 			if (i + 1 < nmem)
 				trace_seq_puts(s, ", ");
 		}
+		*p_type = NULL;
 		return 0;
 	}
 
@@ -391,6 +370,47 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
 		return -1;
 	}
 
+	*p_type = type;
+
+	return 0;
+}
+
+/**
+ * tep_btf_print_args - Print function arguments from BTF info
+ * @tep: The tep descriptor to use
+ * @s: The trace_seq to write the arguments into
+ * @args: The array that holds the arguments
+ * @nmem: The number of arguments
+ * @size: The size of each item in args (4 or 8).
+ * @func: The name of the function.
+ *
+ * Loads up the @s with a list of arguments for the function based on
+ * the @args.
+ *
+ * If there's no BTF loaded or @func is not found, then it just writes
+ * the @args as raw numbers. Otherwise it will pretty print the
+ * arguments based on the function info in BTF.
+ *
+ * Returns: 0 on success and -1 on failure.
+ */
+int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
+		       int nmem, int size, const char *func)
+{
+	struct tep_btf *btf = tep->btf;
+	struct btf_type *type = tep_btf_find_func(btf, func);
+	struct btf_param *param;
+	unsigned long long arg;
+	unsigned int encode;
+	const char *param_name;
+	int a, p, x, nr;
+
+	if (init_btf_func(btf, s, args, nmem, size, func, &type) < 0)
+		return -1;
+
+	/* Type is NULL if function wasn't found */
+	if (!type)
+		return 0;
+
 	/* Get the function proto */
 	type = btf_get_type(btf, type->type);
 
-- 
2.51.0