ftrace: Add print_function_args()
authorSven Schnelle <svens@linux.ibm.com>
Thu, 27 Feb 2025 18:58:05 +0000 (13:58 -0500)
committerSteven Rostedt (Google) <rostedt@goodmis.org>
Tue, 4 Mar 2025 16:27:23 +0000 (11:27 -0500)
Add a function to decode argument types with the help of BTF. Will
be used to display arguments in the function and function graph
tracer.

It can only handle simply arguments and up to FTRACE_REGS_MAX_ARGS number
of arguments. When it hits a max, it will print ", ...":

   page_to_skb(vi=0xffff8d53842dc980, rq=0xffff8d53843a0800, page=0xfffffc2e04337c00, offset=6160, len=64, truesize=1536, ...)

And if it hits an argument that is not recognized, it will print the raw
value and the type of argument it is:

   make_vfsuid(idmap=0xffffffff87f99db8, fs_userns=0xffffffff87e543c0, kuid=0x0 (STRUCT))
   __pti_set_user_pgtbl(pgdp=0xffff8d5384ab47f8, pgd=0x110e74067 (STRUCT))

Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Guo Ren <guoren@kernel.org>
Cc: Donglin Peng <dolinux.peng@gmail.com>
Cc: Zheng Yejian <zhengyejian@huaweicloud.com>
Link: https://lore.kernel.org/20250227185822.639418500@goodmis.org
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Co-developed-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: Sven Schnelle <svens@linux.ibm.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
include/linux/ftrace_regs.h
kernel/trace/Kconfig
kernel/trace/trace_output.c
kernel/trace/trace_output.h

index bbc1873ca6b8e9dade4c6eb97cf2a839cbde23d6..15627ceea9bcc7e1f79787417df5ce8b605932e7 100644 (file)
@@ -35,4 +35,9 @@ struct ftrace_regs;
 
 #endif /* HAVE_ARCH_FTRACE_REGS */
 
+/* This can be overridden by the architectures */
+#ifndef FTRACE_REGS_MAX_ARGS
+# define FTRACE_REGS_MAX_ARGS  6
+#endif
+
 #endif /* _LINUX_FTRACE_REGS_H */
index d570b8b9c0a9b467c8ae56345c622be1694ec142..60412c1012efbc5bfe6bfbcc64b4ebc1b40d11cd 100644 (file)
@@ -263,6 +263,12 @@ config FUNCTION_GRAPH_RETADDR
          the function is called. This feature is off by default, and you can
          enable it via the trace option funcgraph-retaddr.
 
+config FUNCTION_TRACE_ARGS
+       bool
+       depends on HAVE_FUNCTION_ARG_ACCESS_API
+       depends on DEBUG_INFO_BTF
+       default y
+
 config DYNAMIC_FTRACE
        bool "enable/disable function tracing dynamically"
        depends on FUNCTION_TRACER
index 03d56f711ad14ee0c5b8d7612840af61ed65f2f3..4b721cd4f21dda3259b24fc6e7355d9da4f4ce71 100644 (file)
 #include <linux/sched/clock.h>
 #include <linux/sched/mm.h>
 #include <linux/idr.h>
+#include <linux/btf.h>
+#include <linux/bpf.h>
 
 #include "trace_output.h"
+#include "trace_btf.h"
 
 /* must be a power of 2 */
 #define EVENT_HASHSIZE 128
@@ -684,6 +687,88 @@ int trace_print_lat_context(struct trace_iterator *iter)
        return !trace_seq_has_overflowed(s);
 }
 
+#ifdef CONFIG_FUNCTION_TRACE_ARGS
+void print_function_args(struct trace_seq *s, unsigned long *args,
+                        unsigned long func)
+{
+       const struct btf_param *param;
+       const struct btf_type *t;
+       const char *param_name;
+       char name[KSYM_NAME_LEN];
+       unsigned long arg;
+       struct btf *btf;
+       s32 tid, nr = 0;
+       int a, p, x;
+
+       trace_seq_printf(s, "(");
+
+       if (!args)
+               goto out;
+       if (lookup_symbol_name(func, name))
+               goto out;
+
+       /* TODO: Pass module name here too */
+       t = btf_find_func_proto(name, &btf);
+       if (IS_ERR_OR_NULL(t))
+               goto out;
+
+       param = btf_get_func_param(t, &nr);
+       if (!param)
+               goto out_put;
+
+       for (a = 0, p = 0; p < nr; a++, p++) {
+               if (p)
+                       trace_seq_puts(s, ", ");
+
+               /* This only prints what the arch allows (6 args by default) */
+               if (a == FTRACE_REGS_MAX_ARGS) {
+                       trace_seq_puts(s, "...");
+                       break;
+               }
+
+               arg = args[a];
+
+               param_name = btf_name_by_offset(btf, param[p].name_off);
+               if (param_name)
+                       trace_seq_printf(s, "%s=", param_name);
+               t = btf_type_skip_modifiers(btf, param[p].type, &tid);
+
+               switch (t ? BTF_INFO_KIND(t->info) : BTF_KIND_UNKN) {
+               case BTF_KIND_UNKN:
+                       trace_seq_putc(s, '?');
+                       /* Still print unknown type values */
+                       fallthrough;
+               case BTF_KIND_PTR:
+                       trace_seq_printf(s, "0x%lx", arg);
+                       break;
+               case BTF_KIND_INT:
+                       trace_seq_printf(s, "%ld", arg);
+                       break;
+               case BTF_KIND_ENUM:
+                       trace_seq_printf(s, "%ld", arg);
+                       break;
+               default:
+                       /* This does not handle complex arguments */
+                       trace_seq_printf(s, "(%s)[0x%lx", btf_type_str(t), arg);
+                       for (x = sizeof(long); x < t->size; x += sizeof(long)) {
+                               trace_seq_putc(s, ':');
+                               if (++a == FTRACE_REGS_MAX_ARGS) {
+                                       trace_seq_puts(s, "...]");
+                                       goto out_put;
+                               }
+                               trace_seq_printf(s, "0x%lx", args[a]);
+                       }
+                       trace_seq_putc(s, ']');
+                       break;
+               }
+       }
+out_put:
+       btf_put(btf);
+out:
+       trace_seq_printf(s, ")");
+}
+#endif
+
 /**
  * ftrace_find_event - find a registered event
  * @type: the type of event to look for
index dca40f1f1da4924fce7492ebe088ee28699d1309..2e305364f2a9df3ce7114f4653823641519b0146 100644 (file)
@@ -41,5 +41,14 @@ extern struct rw_semaphore trace_event_sem;
 #define SEQ_PUT_HEX_FIELD(s, x)                                \
        trace_seq_putmem_hex(s, &(x), sizeof(x))
 
+#ifdef CONFIG_FUNCTION_TRACE_ARGS
+void print_function_args(struct trace_seq *s, unsigned long *args,
+                        unsigned long func);
+#else
+static inline void print_function_args(struct trace_seq *s, unsigned long *args,
+                                      unsigned long func) {
+       trace_seq_puts(s, "()");
+}
+#endif
 #endif