Skip to content

Instantly share code, notes, and snippets.

@chenhengqi
Last active June 30, 2024 23:27
Show Gist options
  • Save chenhengqi/b7b877ba263c4fb907eeda7894139501 to your computer and use it in GitHub Desktop.
Save chenhengqi/b7b877ba263c4fb907eeda7894139501 to your computer and use it in GitHub Desktop.
Kprobe-based Event Tracing

Scripts

$ echo global > /sys/kernel/debug/tracing/trace_clock
$ echo 'p:kprobes/tcp_reset tcp_reset port=+12(%di):u16 dst=+0(%di):u32 state=+18(%di):u8' >> /sys/kernel/debug/tracing/kprobe_events
$ echo 1 > /sys/kernel/debug/tracing/events/kprobes/tcp_reset/enable
$ echo 'p:kprobes/tcp_retransmit tcp_retransmit_skb port=+12(%di):u16 dst=+0(%di):u32 state=+18(%di):u8' >> /sys/kernel/debug/tracing/kprobe_events
$ echo 1 > /sys/kernel/debug/tracing/events/kprobes/tcp_retransmit/enable

Logs

$ cat /sys/kernel/debug/tracing/trace

Function Arguments

How do I know which register is used for nth argument ?

See the arch-specific ABI docs.

How to figure out field offset of a kernel struct ?

Use GDB.

$ sudo apt install linux-image-unsigned-5.8.0-37-generic-dbgsym
$ gdb /usr/lib/debug/boot/vmlinux-5.8.0-37-generic
(gdb) ptype struct sock
(gdb) print (int)&((struct sock*)0)->__sk_common.skc_dport

References

Keywords

  • kprobe event trace
  • ftrace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment