Skip to contentSkip to conversation

tools: ksnoop: tracing kernel function entry/return with argument/return value display

PATCH In review Alan Maguire 2 participants 7 messages Aug 3, 2021 – Aug 6, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover tools: ksnoop: tracing kernel function entry/return with argument/return value display

Recent functionality added to libbpf [1] enables typed display of kernel data structures; here that functionality is exploited to provide a simple example of how a tracer can support deep argument/return value inspection. The intent is to provide a demonstration of these features to help facilitate tracer adoption, while also providing a tool which can be useful for kernel debugging.

Changes since RFC [2]:

- In the RFC version, kernel data structures were string-ified in BPF program context vi bpf_snprintf_btf(); Alexei pointed out that it would be better to dump memory to userspace and let the interpretation happen there. btf_dump__dump_type_data() in libbpf now supports this (Alexei, patch 1)
- Added the "stack mode" specification where we trace a specific set of functions being called in order (though not necessarily directly). This mode of tracing is useful when debugging issues with a specific stack signature.

[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/

Alan Maguire (3):
tools: ksnoop: kernel argument/return value tracing/display using BTF
tools: ksnoop: document ksnoop tracing of entry/return with value
display
tools: ksnoop: add .gitignore

 tools/bpf/Makefile                        |  20 +-
 tools/bpf/ksnoop/.gitignore               |   1 +
 tools/bpf/ksnoop/Documentation/Makefile   |  58 ++
 tools/bpf/ksnoop/Documentation/ksnoop.rst | 173 ++++++
 tools/bpf/ksnoop/Makefile                 | 107 ++++
 tools/bpf/ksnoop/ksnoop.bpf.c             | 391 +++++++++++++
 tools/bpf/ksnoop/ksnoop.c                 | 890 ++++++++++++++++++++++++++++++
 tools/bpf/ksnoop/ksnoop.h                 | 103 ++++
 8 files changed, 1738 insertions(+), 5 deletions(-)
 create mode 100644 tools/bpf/ksnoop/.gitignore
 create mode 100644 tools/bpf/ksnoop/Documentation/Makefile
 create mode 100644 tools/bpf/ksnoop/Documentation/ksnoop.rst
 create mode 100644 tools/bpf/ksnoop/Makefile
 create mode 100644 tools/bpf/ksnoop/ksnoop.bpf.c
 create mode 100644 tools/bpf/ksnoop/ksnoop.c
 create mode 100644 tools/bpf/ksnoop/ksnoop.h
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/3 tools: ksnoop: kernel argument/return value tracing/display using BTF

BPF Type Format (BTF) provides a description of kernel data structures. libbpf support was recently added - btf_dump__dump_type_data() - that uses the BTF id of the associated type to create a string representation of the data provided. For example, to create a string representation of a "struct sk_buff", the pointer to the skb data is provided along with the type id of "struct sk_buff".

Here that functionality is utilized to support tracing kernel function entry and return using k[ret]probes. The "struct pt_regs" context can be used to derive arguments and return values, and when the user supplies a function name we

- look it up in /proc/kallsyms to find its address/module
- look it up in the BTF kernel/module data to get types of arguments and return value
- store a map representation of the trace information, keyed by function address

On function entry/return we look up info about the arguments (is it a pointer? what size of data do we copy?) and call bpf_probe_read() to copy the data into our trace buffers. These are then sent via perf event to userspace, and since we know the associated BTF id, we can dump the typed data using btf_dump__dump_type_data().

ksnoop can be used to show function signatures; for example:

$ ksnoop info ip_send_skb
int  ip_send_skb(struct net  * net, struct sk_buff  * skb);

Then we can trace the function, for example:

$ ksnoop trace ip_send_skb
           TIME  CPU      PID FUNCTION/ARGS
  78101668506811    1     2813 ip_send_skb(
                                   net = *(0xffffffffb5959840)
                                    (struct net){
                                     .passive = (refcount_t){
                                      .refs = (atomic_t){
                                       .counter = (int)0x2,
                                      },
                                     },
                                     .dev_base_seq = (unsigned int)0x18,
                                     .ifindex = (int)0xf,
                                     .list = (struct list_head){
                                      .next = (struct list_head *)0xffff9895440dc120,
                                      .prev = (struct list_head *)0xffffffffb595a8d0,
                                     },
[output truncated]

78178228354796 1 2813 ip_send_skb(

                                   return =
                                    (int)0x0
                               );

We see the raw value of pointers along with the typed representation of the data they point to.

Up to five arguments are supported.

The arguments are referred to via name (e.g. skb, net), and the return value is referred to as "return" (using the keyword ensures we can never clash with an argument name).

ksnoop can select specific arguments/return value rather than tracing everything; for example:

$ ksnoop "ip_send_skb(skb)"

...will only trace the skb argument. A single level of reference is supported also, for example:

$ ksnoop "ip_send_skb(skb->sk)"

or

$ ksnoop "ip_send_skb(skb->len)"

Multiple functions can be specified also.

In addition, using "stack" (-s) mode, it is possible to specify that a sequence of functions should be traced, but only if function A calls function B (either directly or indirectly). For example, in specifying

$ ksnoop -s tcp_sendmsg __tcp_transmit_skb  ip_output

...we are saying we are only interested in tcp_sendmsg() function calls that in turn issue calls to __tcp_transmit_skb(), and these in turn eventually call ip_output(), and that we only want to see their entry and return. This mode is useful for investigating behaviour with a specific stack signature, allowing us to see function/argument information for specific call chains only.

Finally, module support is included too, provided module BTF is present in /sys/kernel/btf :

$ ksnoop iwl_trans_send_cmd
            TIME  CPU      PID FUNCTION/ARGS
  80046971419383    3     1038 iwl_trans_send_cmd(
                                   trans = *(0xffff989564d20028)
                                    (struct iwl_trans){
                                     .ops = (struct iwl_trans_ops *)0xffffffffc0e02fa0,
                                     .op_mode = (struct iwl_op_mode *)0xffff989566849fc0,
                                     .trans_cfg = (struct iwl_cfg_trans_params *)0xffffffffc0e05280,

The goal pursued here is not to add another tracer to the world - there are plenty of those - but rather to demonstrate feature usage for deep data display in the hope that other tracing technologies make use of this functionality. In the meantime, having a simple tracer like this plugs the gap and can be quite helpful for kernel debugging.

---
 tools/bpf/Makefile            |  20 +-
 tools/bpf/ksnoop/Makefile     |  98 +++++
 tools/bpf/ksnoop/ksnoop.bpf.c | 391 +++++++++++++++++++
 tools/bpf/ksnoop/ksnoop.c     | 890 ++++++++++++++++++++++++++++++++++++++++++
 tools/bpf/ksnoop/ksnoop.h     | 103 +++++
 5 files changed, 1497 insertions(+), 5 deletions(-)
 create mode 100644 tools/bpf/ksnoop/Makefile
 create mode 100644 tools/bpf/ksnoop/ksnoop.bpf.c
 create mode 100644 tools/bpf/ksnoop/ksnoop.c
 create mode 100644 tools/bpf/ksnoop/ksnoop.h
tools/bpf/Makefile +15 -5
index b11cfc8..e72bfdc 100644
@@ -38,7 +38,7 @@ FEATURE_TESTS = libbfd disassembler-four-args
 FEATURE_DISPLAY = libbfd disassembler-four-args
 
 check_feat := 1
-NON_CHECK_FEAT_TARGETS := clean bpftool_clean runqslower_clean resolve_btfids_clean
+NON_CHECK_FEAT_TARGETS := clean bpftool_clean runqslower_clean resolve_btfids_clean ksnoop_clean
 ifdef MAKECMDGOALS
 ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
   check_feat := 0
@@ -73,7 +73,7 @@ $(OUTPUT)%.lex.o: $(OUTPUT)%.lex.c
 
 PROGS = $(OUTPUT)bpf_jit_disasm $(OUTPUT)bpf_dbg $(OUTPUT)bpf_asm
 
-all: $(PROGS) bpftool runqslower
+all: $(PROGS) bpftool runqslower ksnoop
 
 $(OUTPUT)bpf_jit_disasm: CFLAGS += -DPACKAGE='bpf_jit_disasm'
 $(OUTPUT)bpf_jit_disasm: $(OUTPUT)bpf_jit_disasm.o
@@ -89,7 +89,7 @@ $(OUTPUT)bpf_exp.lex.c: $(OUTPUT)bpf_exp.yacc.c
 $(OUTPUT)bpf_exp.yacc.o: $(OUTPUT)bpf_exp.yacc.c
 $(OUTPUT)bpf_exp.lex.o: $(OUTPUT)bpf_exp.lex.c
 
-clean: bpftool_clean runqslower_clean resolve_btfids_clean
+clean: bpftool_clean runqslower_clean resolve_btfids_clean ksnoop_clean
 	$(call QUIET_CLEAN, bpf-progs)
 	$(Q)$(RM) -r -- $(OUTPUT)*.o $(OUTPUT)bpf_jit_disasm $(OUTPUT)bpf_dbg \
 	       $(OUTPUT)bpf_asm $(OUTPUT)bpf_exp.yacc.* $(OUTPUT)bpf_exp.lex.*
@@ -97,7 +97,7 @@ clean: bpftool_clean runqslower_clean resolve_btfids_clean
 	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpf
 	$(Q)$(RM) -r -- $(OUTPUT)feature
 
-install: $(PROGS) bpftool_install
+install: $(PROGS) bpftool_install ksnoop_install
 	$(call QUIET_INSTALL, bpf_jit_disasm)
 	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/bin
 	$(Q)$(INSTALL) $(OUTPUT)bpf_jit_disasm $(DESTDIR)$(prefix)/bin/bpf_jit_disasm
@@ -127,6 +127,16 @@ resolve_btfids:
 resolve_btfids_clean:
 	$(call descend,resolve_btfids,clean)
 
+ksnoop:
+	$(call descend,ksnoop)
+
+ksnoop_install:
+	$(call descend,ksnoop,install)
+
+ksnoop_clean:
+	$(call descend,ksnoop,clean)
+
 .PHONY: all install clean bpftool bpftool_install bpftool_clean \
 	runqslower runqslower_clean \
-	resolve_btfids resolve_btfids_clean
+	resolve_btfids resolve_btfids_clean \
+	ksnoop ksnoop_install ksnoop_clean
tools/bpf/ksnoop/Makefile new file +98 -0

98 lines not on this page · raw message

tools/bpf/ksnoop/ksnoop.bpf.c new file +391 -0

391 lines not on this page · raw message

tools/bpf/ksnoop/ksnoop.c new file +890 -0

890 lines not on this page · raw message

tools/bpf/ksnoop/ksnoop.h new file +103 -0

103 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/3 tools: ksnoop: document ksnoop tracing of entry/return with value display

In reply to Alan Maguire

Add ksnoop.8 describing options for tracing function entry/return with full argument/return value display. Include example use cases with output to demonstrate functionality.

---
 tools/bpf/ksnoop/Documentation/Makefile   |  58 ++++++++++
 tools/bpf/ksnoop/Documentation/ksnoop.rst | 173 ++++++++++++++++++++++++++++++
 tools/bpf/ksnoop/Makefile                 |  17 ++-
 3 files changed, 244 insertions(+), 4 deletions(-)
 create mode 100644 tools/bpf/ksnoop/Documentation/Makefile
 create mode 100644 tools/bpf/ksnoop/Documentation/ksnoop.rst
tools/bpf/ksnoop/Documentation/Makefile new file +58 -0
new file mode 100644
index 0000000..7c2331b9
@@ -0,0 +1,58 @@
+# SPDX-License-Identifier: GPL-2.0-only
+include ../../../scripts/Makefile.include
+include ../../../scripts/utilities.mak
+
+INSTALL ?= install
+RM ?= rm -f
+RMDIR ?= rmdir --ignore-fail-on-non-empty
+
+ifeq ($(V),1)
+  Q =
+else
+  Q = @
+endif
+
+prefix ?= /usr/local
+mandir ?= $(prefix)/man
+man8dir = $(mandir)/man8
+
+MAN8_RST = ksnoop.rst
+
+_DOC_MAN8 = $(patsubst %.rst,%.8,$(MAN8_RST))
+DOC_MAN8 = $(addprefix $(OUTPUT),$(_DOC_MAN8))
+
+man: man8
+man8: $(DOC_MAN8)
+
+RST2MAN_DEP := $(shell command -v rst2man 2>/dev/null)
+RST2MAN_OPTS += --verbose
+
+list_pages = $(sort $(basename $(filter-out $(1),$(MAN8_RST))))
+see_also = $(subst " ",, \
+	"\n" \
+	"SEE ALSO\n" \
+	"========\n" \
+	"\t**bpf**\ (2),\n")
+
+$(OUTPUT)%.8: %.rst
+ifndef RST2MAN_DEP
+	$(error "rst2man not found, but required to generate man pages")
+endif
+	$(QUIET_GEN)( cat $< ; printf "%b" $(call see_also,$<) ) | rst2man $(RST2MAN_OPTS) > $@
+
+clean:
+	$(call QUIET_CLEAN, Documentation)
+	$(Q)$(RM) $(DOC_MAN8)
+
+install: man
+	$(call QUIET_INSTALL, Documentation-man)
+	$(Q)$(INSTALL) -d -m 755 $(DESTDIR)$(man8dir)
+	$(Q)$(INSTALL) -m 644 $(DOC_MAN8) $(DESTDIR)$(man8dir)
+
+uninstall:
+	$(call QUIET_UNINST, Documentation-man)
+	$(Q)$(RM) $(addprefix $(DESTDIR)$(man8dir)/,$(_DOC_MAN8))
+	$(Q)$(RMDIR) $(DESTDIR)$(man8dir)
+
+.PHONY: man man8 clean install uninstall
+.DEFAULT_GOAL := man
tools/bpf/ksnoop/Documentation/ksnoop.rst new file +173 -0

173 lines not on this page · raw message

tools/bpf/ksnoop/Makefile +13 -4
index 0a1420e..149e4be 100644
@@ -42,12 +42,12 @@ endif
 
 .DELETE_ON_ERROR:
 
-.PHONY: all clean ksnoop
+.PHONY: all clean ksnoop doc doc-clean doc-install
 all: ksnoop
 
-ksnoop: $(OUTPUT)/ksnoop
+ksnoop: $(OUTPUT)/ksnoop doc
 
-clean:
+clean: doc-clean
 	$(call QUIET_CLEAN, ksnoop)
 	$(Q)$(RM) -r $(BPFOBJ_OUTPUT) $(BPFTOOL_OUTPUT)
 	$(Q)$(RM) $(OUTPUT)*.o $(OUTPUT)*.d
@@ -55,11 +55,20 @@ clean:
 	$(Q)$(RM) $(OUTPUT)ksnoop
 	$(Q)$(RM) -r .output
 
-install: ksnoop
+install: ksnoop doc-install
 	$(call QUIET_INSTALL, ksnoop)
 	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
 	$(Q)$(INSTALL) $(OUTPUT)/ksnoop $(DESTDIR)$(prefix)/sbin/ksnoop
 
+doc:
+	$(call descend,Documentation)
+
+doc-clean:
+	$(call descend,Documentation,clean)
+
+doc-install:
+	$(call descend,Documentation,install)
+
 $(OUTPUT)/ksnoop: $(OUTPUT)/ksnoop.o $(BPFOBJ)
 	$(QUIET_LINK)$(CC) $(CFLAGS) $^ -lelf -lz -o $@
 
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 3/3 tools: ksnoop: add .gitignore

In reply to Alan Maguire

.gitignore needed for ksnoop.8 manual page generation.

---
 tools/bpf/ksnoop/.gitignore | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 tools/bpf/ksnoop/.gitignore
tools/bpf/ksnoop/.gitignore new file +1 -0
new file mode 100644
index 0000000..8b8f877
@@ -0,0 +1 @@
+ksnoop.8
Signature · 1 line
1.8.3.1
maintainer
#4 lore raw
In reply to Alan Maguire

Hi Alan,

Quoted 6 lines · Alan Maguire

Recent functionality added to libbpf [1] enables typed display of kernel data structures; here that functionality is exploited to provide a simple example of how a tracer can support deep argument/return value inspection. The intent is to provide a demonstration of these features to help facilitate tracer adoption, while also providing a tool which can be useful for kernel debugging.

Thanks a lot for working on this tool, this looks _super useful_! Right now under tools/bpf/ we have bpftool and resolve_btfids as the two main tools, the latter used during kernel build, and the former evolving with the kernel together with libbpf. The runqslower in there was originally thought of as a single/small example tool to demo how to build stand-alone tracing tools with all the modern practices, though the latter has also been added to [0] (thus could be removed). I would rather love if you could add ksnoop for inclusion into bcc's libbpf-based tracing tooling suite under [0] as well which would be a better fit long term rather than kernel tree for the tool to evolve. We don't intend to add a stand-alone tooling collection under the tools/bpf/ long term since these can evolve better outside of kernel tree.

Thanks a lot,
Daniel

[0] https://github.com/iovisor/bcc/tree/master/libbpf-tools

Quoted 38 lines

Changes since RFC [2]:

- In the RFC version, kernel data structures were string-ified in BPF program context vi bpf_snprintf_btf(); Alexei pointed out that it would be better to dump memory to userspace and let the interpretation happen there. btf_dump__dump_type_data() in libbpf now supports this (Alexei, patch 1)
- Added the "stack mode" specification where we trace a specific set of functions being called in order (though not necessarily directly). This mode of tracing is useful when debugging issues with a specific stack signature.

[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/

Alan Maguire (3):
tools: ksnoop: kernel argument/return value tracing/display using BTF
tools: ksnoop: document ksnoop tracing of entry/return with value
display
tools: ksnoop: add .gitignore

  tools/bpf/Makefile                        |  20 +-
  tools/bpf/ksnoop/.gitignore               |   1 +
  tools/bpf/ksnoop/Documentation/Makefile   |  58 ++
  tools/bpf/ksnoop/Documentation/ksnoop.rst | 173 ++++++
  tools/bpf/ksnoop/Makefile                 | 107 ++++
  tools/bpf/ksnoop/ksnoop.bpf.c             | 391 +++++++++++++
  tools/bpf/ksnoop/ksnoop.c                 | 890 ++++++++++++++++++++++++++++++
  tools/bpf/ksnoop/ksnoop.h                 | 103 ++++
  8 files changed, 1738 insertions(+), 5 deletions(-)

create mode 100644 tools/bpf/ksnoop/.gitignore create mode 100644 tools/bpf/ksnoop/Documentation/Makefile create mode 100644 tools/bpf/ksnoop/Documentation/ksnoop.rst create mode 100644 tools/bpf/ksnoop/Makefile create mode 100644 tools/bpf/ksnoop/ksnoop.bpf.c create mode 100644 tools/bpf/ksnoop/ksnoop.c create mode 100644 tools/bpf/ksnoop/ksnoop.h

#5 lore raw
Quoted 10 lines · Daniel Borkmann

Hi Alan,

Quoted 6 lines · Alan Maguire

Recent functionality added to libbpf [1] enables typed display of kernel data structures; here that functionality is exploited to provide a simple example of how a tracer can support deep argument/return value inspection. The intent is to provide a demonstration of these features to help facilitate tracer adoption, while also providing a tool which can be useful for kernel debugging.

Quoted 12 lines

Thanks a lot for working on this tool, this looks _super useful_! Right now under tools/bpf/ we have bpftool and resolve_btfids as the two main tools, the latter used during kernel build, and the former evolving with the kernel together with libbpf. The runqslower in there was originally thought of as a single/small example tool to demo how to build stand-alone tracing tools with all the modern practices, though the latter has also been added to [0] (thus could be removed). I would rather love if you could add ksnoop for inclusion into bcc's libbpf-based tracing tooling suite under [0] as well which would be a better fit long term rather than kernel tree for the tool to evolve. We don't intend to add a stand-alone tooling collection under the tools/bpf/ long term since these can evolve better outside of kernel tree.

Sounds good; I'll look into contributing the tool to bcc.

Thanks!

Alan

maintainer
#6 lore raw
Quoted 21 lines · Alan Maguire
Quoted 8 lines · Daniel Borkmann
Quoted 6 lines · Alan Maguire

Recent functionality added to libbpf [1] enables typed display of kernel data structures; here that functionality is exploited to provide a simple example of how a tracer can support deep argument/return value inspection. The intent is to provide a demonstration of these features to help facilitate tracer adoption, while also providing a tool which can be useful for kernel debugging.

Quoted 11 lines

Thanks a lot for working on this tool, this looks _super useful_! Right now under tools/bpf/ we have bpftool and resolve_btfids as the two main tools, the latter used during kernel build, and the former evolving with the kernel together with libbpf. The runqslower in there was originally thought of as a single/small example tool to demo how to build stand-alone tracing tools with all the modern practices, though the latter has also been added to [0] (thus could be removed). I would rather love if you could add ksnoop for inclusion into bcc's libbpf-based tracing tooling suite under [0] as well which would be a better fit long term rather than kernel tree for the tool to evolve. We don't intend to add a stand-alone tooling collection under the tools/bpf/ long term since these can evolve better outside of kernel tree.

Quoted 1 line

Sounds good; I'll look into contributing the tool to bcc.

Awesome, thanks a lot Alan!