Skip to content

Commit 533c20b

Browse files
svens-s390rostedt
authored andcommitted
ftrace: Add print_function_args()
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 <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Paul Walmsley <[email protected]> Cc: Palmer Dabbelt <[email protected]> Cc: Albert Ou <[email protected]> Cc: Guo Ren <[email protected]> Cc: Donglin Peng <[email protected]> Cc: Zheng Yejian <[email protected]> Link: https://lore.kernel.org/[email protected] Reviewed-by: Masami Hiramatsu (Google) <[email protected]> Co-developed-by: Steven Rostedt (Google) <[email protected]> Signed-off-by: Sven Schnelle <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 0c66777 commit 533c20b

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

include/linux/ftrace_regs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ struct ftrace_regs;
3535

3636
#endif /* HAVE_ARCH_FTRACE_REGS */
3737

38+
/* This can be overridden by the architectures */
39+
#ifndef FTRACE_REGS_MAX_ARGS
40+
# define FTRACE_REGS_MAX_ARGS 6
41+
#endif
42+
3843
#endif /* _LINUX_FTRACE_REGS_H */

kernel/trace/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ config FUNCTION_GRAPH_RETADDR
263263
the function is called. This feature is off by default, and you can
264264
enable it via the trace option funcgraph-retaddr.
265265

266+
config FUNCTION_TRACE_ARGS
267+
bool
268+
depends on HAVE_FUNCTION_ARG_ACCESS_API
269+
depends on DEBUG_INFO_BTF
270+
default y
271+
266272
config DYNAMIC_FTRACE
267273
bool "enable/disable function tracing dynamically"
268274
depends on FUNCTION_TRACER

kernel/trace/trace_output.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
#include <linux/sched/clock.h>
1313
#include <linux/sched/mm.h>
1414
#include <linux/idr.h>
15+
#include <linux/btf.h>
16+
#include <linux/bpf.h>
1517

1618
#include "trace_output.h"
19+
#include "trace_btf.h"
1720

1821
/* must be a power of 2 */
1922
#define EVENT_HASHSIZE 128
@@ -684,6 +687,88 @@ int trace_print_lat_context(struct trace_iterator *iter)
684687
return !trace_seq_has_overflowed(s);
685688
}
686689

690+
#ifdef CONFIG_FUNCTION_TRACE_ARGS
691+
void print_function_args(struct trace_seq *s, unsigned long *args,
692+
unsigned long func)
693+
{
694+
const struct btf_param *param;
695+
const struct btf_type *t;
696+
const char *param_name;
697+
char name[KSYM_NAME_LEN];
698+
unsigned long arg;
699+
struct btf *btf;
700+
s32 tid, nr = 0;
701+
int a, p, x;
702+
703+
trace_seq_printf(s, "(");
704+
705+
if (!args)
706+
goto out;
707+
if (lookup_symbol_name(func, name))
708+
goto out;
709+
710+
/* TODO: Pass module name here too */
711+
t = btf_find_func_proto(name, &btf);
712+
if (IS_ERR_OR_NULL(t))
713+
goto out;
714+
715+
param = btf_get_func_param(t, &nr);
716+
if (!param)
717+
goto out_put;
718+
719+
for (a = 0, p = 0; p < nr; a++, p++) {
720+
if (p)
721+
trace_seq_puts(s, ", ");
722+
723+
/* This only prints what the arch allows (6 args by default) */
724+
if (a == FTRACE_REGS_MAX_ARGS) {
725+
trace_seq_puts(s, "...");
726+
break;
727+
}
728+
729+
arg = args[a];
730+
731+
param_name = btf_name_by_offset(btf, param[p].name_off);
732+
if (param_name)
733+
trace_seq_printf(s, "%s=", param_name);
734+
t = btf_type_skip_modifiers(btf, param[p].type, &tid);
735+
736+
switch (t ? BTF_INFO_KIND(t->info) : BTF_KIND_UNKN) {
737+
case BTF_KIND_UNKN:
738+
trace_seq_putc(s, '?');
739+
/* Still print unknown type values */
740+
fallthrough;
741+
case BTF_KIND_PTR:
742+
trace_seq_printf(s, "0x%lx", arg);
743+
break;
744+
case BTF_KIND_INT:
745+
trace_seq_printf(s, "%ld", arg);
746+
break;
747+
case BTF_KIND_ENUM:
748+
trace_seq_printf(s, "%ld", arg);
749+
break;
750+
default:
751+
/* This does not handle complex arguments */
752+
trace_seq_printf(s, "(%s)[0x%lx", btf_type_str(t), arg);
753+
for (x = sizeof(long); x < t->size; x += sizeof(long)) {
754+
trace_seq_putc(s, ':');
755+
if (++a == FTRACE_REGS_MAX_ARGS) {
756+
trace_seq_puts(s, "...]");
757+
goto out_put;
758+
}
759+
trace_seq_printf(s, "0x%lx", args[a]);
760+
}
761+
trace_seq_putc(s, ']');
762+
break;
763+
}
764+
}
765+
out_put:
766+
btf_put(btf);
767+
out:
768+
trace_seq_printf(s, ")");
769+
}
770+
#endif
771+
687772
/**
688773
* ftrace_find_event - find a registered event
689774
* @type: the type of event to look for

kernel/trace/trace_output.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,14 @@ extern struct rw_semaphore trace_event_sem;
4141
#define SEQ_PUT_HEX_FIELD(s, x) \
4242
trace_seq_putmem_hex(s, &(x), sizeof(x))
4343

44+
#ifdef CONFIG_FUNCTION_TRACE_ARGS
45+
void print_function_args(struct trace_seq *s, unsigned long *args,
46+
unsigned long func);
47+
#else
48+
static inline void print_function_args(struct trace_seq *s, unsigned long *args,
49+
unsigned long func) {
50+
trace_seq_puts(s, "()");
51+
}
52+
#endif
4453
#endif
4554

0 commit comments

Comments
 (0)