Kernel 开启BTF 方式
背景
BTF
(BPF Type Format)是一种元数据格式,对与BPF程序/map有关的调试信息进行编码。与BPF程序/map有关的元数据格式。BTF这个名字最初是用来描述数据类型的。后来,BTF被扩展到包括已定义的子程序的函数信息和源/行信息的行信息。
检查内核是否开启BTF调试
检查内核是否开启BTF调试:
$ cat /boot/config-`uname -r` | grep BTF
CONFIG_DEBUG_INFO_BTF=y
返回CONFIG_DEBUG_INFO_BTF=y
表示内核已经开启的BTF.
内核开启BTF
启用CONFIG_DEBUG_INFO_BTF=y
内核选项即可。内核本身可以使用BTF功能,用于增强BPF验证程序自身的功能。
检查内核是否BTF vmlinux
$ pwd
/sys/kernel
$ ls btf
vmlinux
安装 bpf依赖包
安装依赖包:
$ yum install bpftool libbpf llvm-libs -y
Last metadata expiration check: 1 day, 0:30:54 ago on Sat 07 Oct 2023 02:32:23 PM CST.
Package bpftool-5.10.134-12.3.el8.bclinux.x86_64 is already installed.
Package libbpf-0.0.4-5.el8.x86_64 is already installed.
Package llvm-libs-9.0.1-5.module+el8.2.0+10180+9e405137.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
生成vmlinux.h文件:
$ bpftool btf dump file /sys/kernel/btf/vmlinux format c > /usr/local/include/vmlinux.h
$ ll /usr/local/include/vmlinux.h
-rw-r--r-- 1 root root 2033898 Oct 8 15:06 /usr/local/include/vmlinux.h
检查环境是否可用
$ bpftool prog show
217501: tracepoint name do_count tag 164821634e7375ea gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 256B jited 166B memlock 4096B map_ids 207631
btf_id 139734
217504: kprobe name do_count tag 65e3ff56bff52e68 gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 256B jited 167B memlock 4096B map_ids 207632
btf_id 139735
217508: kprobe name do_count tag 9d7cf2bf46590f5a gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 1640B jited 1016B memlock 4096B map_ids 207636,207638,207635,207634
btf_id 139736
217510: kprobe name shrink_node_ent tag a2782e80c654b953 gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 112B jited 85B memlock 4096B map_ids 207639
btf_id 139737
217511: kprobe name shrink_node_exi tag af425d8de6a189fd gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 1240B jited 666B memlock 4096B map_ids 207639,207640
btf_id 139737
217513: raw_tracepoint name block_rq_insert tag b50a05896dc733b9 gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 136B jited 98B memlock 4096B map_ids 207643,207641
btf_id 139738
217514: raw_tracepoint name block_rq_issue tag b50a05896dc733b9 gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 136B jited 98B memlock 4096B map_ids 207643,207641
btf_id 139738
217515: raw_tracepoint name block_rq_comple tag 7293da2116c096ea gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 1568B jited 870B memlock 4096B map_ids 207641,207642
btf_id 139738
217517: kprobe name kprobe__inet_cs tag c5bb1723820097a7 gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 112B jited 85B memlock 4096B map_ids 207644
btf_id 139739
217518: kprobe name kprobe__inet_cs tag 9b1903fee906166a gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 1288B jited 736B memlock 4096B map_ids 207644,207645
btf_id 139739
217520: kprobe name trace_change tag f5d2be95f6a869a3 gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 184B jited 118B memlock 4096B map_ids 207647,207646
btf_id 139740
217522: kprobe name do_init tag f5d2be95f6a869a3 gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 184B jited 118B memlock 4096B map_ids 207647,207646
btf_id 139740
217524: kprobe name add_to_page_cac tag ec7027a0bc58cb0c gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 320B jited 198B memlock 4096B map_ids 207649
btf_id 139741
217525: kprobe name mark_page_acces tag ec7027a0bc58cb0c gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 320B jited 198B memlock 4096B map_ids 207649
btf_id 139741
217526: kprobe name folio_account_d tag ec7027a0bc58cb0c gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 320B jited 198B memlock 4096B map_ids 207649
btf_id 139741
217527: kprobe name mark_buffer_dir tag ec7027a0bc58cb0c gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 320B jited 198B memlock 4096B map_ids 207649
btf_id 139741
217529: tracepoint name do_count tag 65e3ff56bff52e68 gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 304B jited 195B memlock 4096B map_ids 207650
btf_id 139742
217531: perf_event name on_cache_miss tag d2fe31242294692d gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 272B jited 172B memlock 4096B map_ids 207651
btf_id 139743
217532: perf_event name on_cache_refere tag d2fe31242294692d gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 272B jited 172B memlock 4096B map_ids 207652
btf_id 139743
217534: raw_tracepoint name do_count tag 2bcaa1e785b61833 gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 296B jited 186B memlock 4096B map_ids 207653
btf_id 139744
217536: kprobe name kprobe__tcp_v4_ tag 415b2a1f2d0052ad gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 552B jited 364B memlock 4096B map_ids 207654
btf_id 139745
217537: kprobe name kprobe__tcp_v6_ tag 415b2a1f2d0052ad gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 552B jited 364B memlock 4096B map_ids 207654
btf_id 139745
217539: kprobe name tcp_try_rmem_sc tag d25d2e6301497809 gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 192B jited 124B memlock 4096B map_ids 207656
btf_id 139746
217540: kprobe name tcp_try_rmem_sc tag e9b4e55b2a20492e gpl
loaded_at 2023-07-28T12:03:14+0800 uid 0
xlated 632B jited 366B memlock 4096B map_ids 207656,207655
btf_id 139746
其他
- https://www.kernel.org/doc/html/latest/bpf/btf.html
- https://cloud.tencent.com/developer/article/2211655
- https://nakryiko.com/posts/bpf-portability-and-co-re/
「如果这篇文章对你有用,请随意打赏」
如果这篇文章对你有用,请随意打赏
使用微信扫描二维码完成支付