Ebpf
程序员的平行宇宙 —— eBPF 系统级跟踪技术简单入门
· ☕ 5 分钟

image-20210310223153844

Linus Torvalds in 1991

程序员的平行宇宙

程序员有两个世界:

  1. 一个是编码世界,我们很容易认为,我们考虑了一切,也完成了一切的代码。
  2. 然后是运行世界,我们发现,无论我们多么的严谨和考虑一切,世界总是有异常。异常就好像电磁波,一开始,我们只能通过它引发的结果而发现它,而不能直接观察。因为它总出现在那些黑暗的角落,如果我们手中没有电简和电磁波示波器,一切只能依靠猜测和迷信。最后,我们进入了一个用猜测驱动的世界。有时候,我们运气好,猜中了。有时候,我们只能在 release 前多上香。

It is a capital mistake to theorize before one has data. Insensibly one begins to twist facts to suit theories, instead of theories to suit facts.


系统级跟踪 eBPF 工具 —— bpftrace 入门
· ☕ 1 分钟

bpftrace 简介

bpftrace 简单使用

查询可以跟踪的内核函数,以 sleep 为关键字

1
2
3
4
5
6
7
8
$ bpftrace -l '*open*'

tracepoint:syscalls:sys_exit_open_tree
tracepoint:syscalls:sys_enter_open
...
kprobe:vfs_open
kprobe:tcp_try_fastopen
...

跟踪所有 sys_enter_open() 系统调用

1
$ bpftrace -e 'tracepoint:syscalls:sys_enter_open{ printf("%s %s\n", comm,str(args->filename)); }' | grep vi

然后在另外一个终端中

1
$ vi /etc/hosts

可以看到在 bpftrace 终端中输出: