bpftrace 2020
(ãã®è¨äºã¯Linux Advent Calendar 2020 - Qiitaã®2æ¥ç®ã®è¨äºã§ãï¼åæ¥ã¯masami256ããã®Tiny Core Linuxã§Linuxã®initããã»ã¹ãå®è¡ããããããã調ã¹ãã§ããï¼)
ã¿ãªããããã«ã¡ã¯ããã°ãã¯ï¼bpftraceã使ã£ã¦ãã¾ãã? (e)BPFã¨ããã°æè¿ã¯å°ãCiliumãªã©ãããã¯ã¼ã¯ã話é¡ã§ããï¼bpftraceã¯BPF*1ãå©ç¨ãããã¬ã¼ã·ã³ã°ãã¼ã«ã§ãï¼ç§ã¯æ¨å¹´ããã¼ã¡ã¼ã¡ä½¿ãå§ãã¦ï¼æ©è½è¿½å ããã°ä¿®æ£ã®ããããããããã¨æãã¦ãã(130+)ãæ°ã¥ãããã³ããã¿ã«ãªã£ã¦ãã¾ããï¼ã¨ãããã¨ã§bpftraceã«ã¤ãã¦ã¯æ¯è¼çããç¥ã£ã¦ããã¨æãã®ã§ï¼ä»å¹´ã«bpftraceã«è¿½å ããã主è¦ãªæ©è½ãå¤æ´ç¹ãç´¹ä»ãããã¨æãã¾ãï¼
bpftraceç¥ããªããªãã¨ããæ¹ã¯ãã¡ã(ææã§ã)ãï¼bpftrace使ã£ã¦ãªããªãã¨ããæ¹ã¯æè§ã§ãããæå¾ã®å°è©±ã ãã§ãèªãã§ã¿ã¦ã¯ãããã§ããããï¼ã¾ãæè¿bpftraceãmacããç°¡åã«è©¦ãããã®ãä½ã£ãã®ã§ï¼macã¦ã¼ã¶ã§bpftraceã«èå³ã®ããæ¹ã¯ãã¡ããã©ããï¼å¿è«M1ã¯å¯¾è±¡å¤ã§ã..ï¼ï¼
ç®æ¬¡
- ç®æ¬¡
- æ¬é¡ã«å ¥ãåã«
- BTFãµãã¼ãã®å¼·å
- Docker ãã«ãã»ã¹ã¿ãã£ãã¯ãã«ãã®æä¾
- ç¥ã£ã¦ããã¨ä¾¿å©ãªæ©è½
- ãã®ä»ä¸»è¦ãªå¤æ´ã»æ´æ°ç¹
- ãã°æ å ±
- ãã¾ã
- æå¾ã«-- bpftraceã«é¢ãã£ã¦ãã¦
- æ¥å¹´ã®æ±è²
æ¬é¡ã«å ¥ãåã«
bpftraceã¯ãã®ä¸å¹´éã§å¤ãã®æ©è½è¿½å ï¼ãã°ä¿®æ£ããããªããã¾ããï¼bpftraceãã使ãã®æ¹ã¯ä»ä¸åº¦ã¢ãããã¼ãã®ç¢ºèªããããããã¾ãï¼ãã以ä¸ã§ç´¹ä»ããæ©è½ã使ããªãå ´åï¼bpftraceã®ãã¼ã¸ã§ã³ãå¤ãå¯è½æ§ãé«ãã§ãï¼ã¾ãä¸é¨æ©è½ã¯ã«ã¼ãã«ãã¼ã¸ã§ã³ï¼ä¸»ã«ã«ã¼ãã«5.6以ä¸ï¼ãï¼libbpfã¨ä¸ç·ã«bpftraceãã³ã³ãã¤ã«ããå¿ è¦ãããã¾ãï¼ç¹ã«BTFãããã«è©²å½ãã¾ãï¼ï¼
bpftraceã®ãã¼ã¸ã§ã³ããµãã¼ããã¦ããæ©è½ã®ç¢ºèª
bpftraceã®ãã¼ã¸ã§ã³ãï¼ãµãã¼ããã¦ããæ©è½ã¯ï¼bpftrace --info
ã³ãã³ããã確èªã§ãã¾ãï¼
$ sudo bpftrace --info System OS: Linux 5.9.0+ #26 SMP Mon Nov 16 11:50:54 JST 2020 Arch: x86_64 Build version: v0.11.0-287-gc62f LLVM: 10.0.1 foreach_sym: yes unsafe uprobe: no bfd: yes bpf_attach_kfunc: yes bcc_usdt_addsem: yes bcc bpf_attach_uprobe refcount: no libbpf: yes libbpf btf dump: yes libbpf btf dump type decl: yes Kernel helpers probe_read: yes probe_read_str: yes probe_read_user: yes probe_read_user_str: yes probe_read_kernel: yes probe_read_kernel_str: yes get_current_cgroup_id: yes send_signal: yes override_return: yes get_boot_ns: yes dpath: no Kernel features Instruction limit: 1000000 Loop support: yes btf (depends on Build:libbpf): yes map batch (depends on Build:libbpf): yes uprobe refcount (depends on Build:bcc bpf_attach_uprobe refcount): no Map types hash: yes percpu hash: yes array: yes percpu array: yes stack_trace: yes perf_event_array: yes Probe types kprobe: yes tracepoint: yes perf_event: yes kfunc: yes
BTFãµãã¼ãã®å¼·å
ããã§ã¯æ©é追å ããã主è¦ãªæ©è½ãæ¯ãè¿ã£ã¦ããããã¨æãã¾ãï¼ä»å¹´ã¯ãªãã¨ãã£ã¦ãBTF (BPF Type Format) ãå©ç¨ããæ©è½ãããããã¨è¿½å ããã¾ããï¼ä»¥ä¸ã§ã¯ã©ããªãã¨ãã§ãããç°¡åã«ç´¹ä»ãã¦ããã¾ãï¼
ããããBTFã¨ã¯
BTFã«ã¯ãããããªç¨éãããããï¼ç°¡åã«èª¬æããã®ã¯é£ããã®ã§ããï¼bpftraceã®æèã§ããã¨/proc/kallsyms ãç¾å¨åãã¦ããã«ã¼ãã«ã·ã³ãã«ã®ã¢ãã¬ã¹ãæä¾ããã®ã¨åãããã«ï¼BTFã¯ç¾å¨åãã¦ããã«ã¼ãã«ãå©ç¨ãã¦ããé¢æ°ããã¼ã¿æ§é ã®æ
å ±ãæä¾ãã¾ãï¼ä¾ãã°ï¼BTFãåç
§ããã°ãã®ã«ã¼ãã«ãå©ç¨ãã¦ããï¼ããã»ã¹ã®ç¶æ
ãä¿æãã struct task_struct
ã®æ§é ãç¥ããã¨ãã§ãï¼ã©ã®ã¡ã³ãã«ã¢ã¯ã»ã¹ããã«ã¯ã©ãã ãã®ãªãã»ãããå¿
è¦ããåããã¾ãï¼ä»ã«ã¯ä¾ãã°ï¼vfs_open()
é¢æ°ã®å¼æ°ã®æ°ãå¼æ°ã®åãåããã¾ãï¼ããã§ä½ãå¬ããã®? ã¨æãããããã¾ãããï¼ã«ã¼ãã«å
ã®é¢æ°ãæ§é ä½ã¯unstableã§å¸¸ã«å¤åãã¾ãï¼ã«ã¼ãã«ãã¬ã¼ã·ã³ã°ãããå ´åï¼å¤ãã®å ´åã¯ã«ã¼ãã«ã®é¢æ°ãæ§é ä½ã«ã¢ã¯ã»ã¹ããããï¼é¢æ°ãæ§é ä½ãå¤åãã¦ãã¾ãã¨æè§æ¸ãããã¬ã¼ã·ã³ã°ã¹ã¯ãªãããç¹å®ã®ã«ã¼ãã«ã§ããåããªãã¨ãããã¨ã«ãªãã¾ãï¼BTFã«ãããã®ã«ã¼ãã«ãå©ç¨ãã¦ããé¢æ°ãã«ã¼ãã«ãåããã°ï¼ãã®æ
å ±ãå©ç¨ãã¦ã¹ã¯ãªãããè£æ£ãããã¨ã§ï¼è¤æ°ã«ã¼ãã«ã§åä½ãããã¬ã¼ã·ã³ã°ã¹ã¯ãªãããåä½ã§ããããã«ãªãã¨ããããã§ãï¼éãæ¹ã¯ããã£ã¦DWARFã§ã§ããã®ã§ã¯?ã¨æããã¨ã§ãããï¼å¿è«DWARFã§ãå¯è½ã§ããï¼BTFã¯ãã¬ã¼ã·ã³ã°ç¨éã«ç¹åãã¦ãããã¯ã·ã§ã³ç°å¢ã§ãå©ç¨ã§ãããã¨ãä¸ã¤ã®ç®æ¨ã«ãã¦ããããï¼ãµã¤ãºãé常ã«å°ããã§ãï¼æ°MBç¨åº¦ï¼ï¼ãã ããã®ä»£ããDWARFã¨æ¯ã¹ã¦å½ç¶æ
å ±éãå°ãªãããï¼ã§ãããã¨ã¯å¿
ç¶çã«éããã¾ãï¼
bpftraceã¯å®è¡æã«ãã¬ã¼ã·ã³ã°ã¹ã¯ãªãããã³ã³ãã¤ã«ããå½¢ã§ãï¼BTFãå©ç¨å¯è½ãªå ´åã¯ï¼bpftraceã¯ã³ã³ãã¤ã«æã«BTFæ å ±ãå©ç¨ãã¦ï¼é©åãªãªãã»ããã§ãã¼ã¿ã«ã¢ã¯ã»ã¹ããBPFããã°ã©ã ãçæãã¾ãï¼ããBTFãå©ç¨ã§ããªãå ´åã¯ï¼includeãããããã«å«ã¾ããå®ç¾©æ å ±ãå©ç¨ãã¾ãï¼ï¼ä¸æ¹ã§ï¼å®è¡åã«ãã¬ã¼ã¹ããã°ã©ã ãããã«ã¼ãã«ãã¼ã¸ã§ã³ä¸ã§ã³ã³ãã¤ã«ãã¦ããï¼BTFãå©ç¨ãã¦ãã®ãã¬ã¼ã¹ããã°ã©ã ããã¼ãããåã«ï¼ãã¼ã¿æ§é ã¢ã¯ã»ã¹ã®ãªãã»ããã®ä¿®æ£ããããªãã¨ããæ¹æ³ãããã¾ãï¼ãã¡ã㯠BPF CO-REã¨å¼ã°ããæ©è½ã§ï¼ãã¡ãã¯ãã¡ãã§é常ã«éè¦ã§ç©æ¥µçã«éçºãé²ãããã¦ãã¾ããï¼ç¹ã«iovisor/bccã®æ¹ã§ï¼ï¼bpftraceã¨ã¯ç´æ¥ã¯é¢ä¿ããã¾ããï¼
BTFã¯ã«ã¼ãã«ã®é¢æ°ãæ§é ä½ã®æ å ±ãæä¾ããã ãã®ãã®ã§ã¯ããã¾ããï¼BTFã«ã¤ãã¦ãã詳ããã¯ãã®è¾ºã(ææã§ã)ãã«ã¼ãã«ã®ããã¥ã¡ã³ãããåç §ãã ããï¼
BTFã使ç¨ããã«ã¯
BTFã®æ©è½ãå©ç¨ããã«ã¯ï¼CONFIG_DEBUG_INFO_BTF
ãæå¹ã«ããã«ã¼ãã«ãå¿
è¦ã§ãï¼ã¾ãã«ã¼ãã«5.6以ä¸ã§ãªãã¨ä»¥ä¸ã§èª¬æããkfuncã®æ©è½ã¯ä½¿ãã¾ããï¼bpftraceèªä½ãlibbpfã¨ä¸ç·ã«ã³ã³ãã¤ã«ããå¿
è¦ãããã¾ãï¼ã
bpftrace --info
ã§BTFã®æ©è½ãå©ç¨ã§ããã確èªã§ãã¾ãï¼ç¹ã«ï¼
- BTFã使ãããå ´å㯠"Kernel features" ã®btf ã yes
- kfuncã使ãããå ´å㯠Probe typesã® kfunc ãyes
ã§ããå¿ è¦ããã¾ãï¼
BTFã§ã§ãããã¨
ããã§ã¯BTFã§ã§ãããã¨ãã¿ã¦ããã¾ãããï¼
ã«ã¼ãã«æ§é ä½ã®èªå解決
ä¾ã¨ãã¦ï¼vfs_open(struct path*, strust file *)
é¢æ°ã®å¼ã³åºããããã¯ãã¦ï¼vfs_openãå¼ã°ããæã®path
ã®ãã¡ã¤ã«åãå¼ã³åºãã¹ã¯ãªãããèãã¾ã(ããã¯bptraceã®reference guideããã®å¼ç¨ã§ã)ï¼BTFãç¡ã以åã¯ï¼ã¹ã¯ãªãããå©ç¨ãã struct path
ãï¼ struct dentry
ãå®ç¾©ãã¦ããããããã¤ã³ã¯ã«ã¼ãããå¿
è¦ãããã¾ããï¼
#include <linux/path.h> #include <linux/dcache.h> kprobe:vfs_open { printf("open path: %s\n", str(((struct path *)arg0)->dentry->d_name.name)); }
ããBTFã使ç¨ã§ããç°å¢ã§ããã°ï¼ä»¥ä¸ã®ããã«ããããã¤ã³ã¯ã«ã¼ãããªãã¦ãï¼ã¹ã¯ãªããå´ã§å©ç¨ããã«ã¼ãã«æ§é ä½ãï¼ç¹ã«ä½ãããªãã¦ãå©ç¨ã§ããããã«ãªãã¾ãï¼
kprobe:vfs_open { printf("open path: %s\n", str(((struct path *)arg0)->dentry->d_name.name)); }
å
é¨çã«ã¯ã¾ãBTFãã struct path
ã®æ
å ±ãåã£ã¦ãã¦ï¼ããããã¹ã¯ãªãããã¢ã¯ã»ã¹ããæ§é ä½æ
å ±ãéãã¦ãã¦ãããå©ç¨ãã¦ãã¾ãï¼
kfuncã®å©ç¨
ä¸è¨ã®ã¹ã¯ãªããä¾ã§ã¯kprobeãå©ç¨ãã¦ãã¾ããï¼kprobeã¯åºæ¬çã«ã«ã¼ãã«å
ã®ã©ã®ã¢ãã¬ã¹ã«ãã¢ã¿ããã§ããã®é常ã«ä¾¿å©ã§ããï¼ä¸æ¹ã§bpftraceã§ã¯é¢æ°ã«ã¢ã¿ããããã¨ãï¼èªåã§å¼æ°ããã£ã¹ãããå¿
è¦ï¼ä¸è¨ã®ä¾ã 㨠(struct path *)arg0)
ãããã¾ããï¼ï¼ã«ã¼ãã«5.6ããï¼kfuncã¨ããï¼kprobeã¨ã¯ã¾ãéããã¬ã¼ã¹æ¹æ³ã追å ããã¾ããï¼kfuncã¯kprobeã¨éãé¢æ°ã®entryã¾ãã¯exitã«ããã¢ã¿ããã§ãã¾ãããï¼BTFã®æ
å ±ãå©ç¨ãããã¨ã§ï¼ä»¥ä¸ã®ããã«æ§é ä½ã®ãã£ã¹ããªãã«é¢æ°ã®å¼æ°ã«ã¢ã¯ã»ã¹ã§ãã¾ãï¼
kfunc:vfs_open { printf("open path: %s\n", str(args->path->dentry->d_name.name)); }
ãã¤ã³ã㯠args->path
ã§ï¼ããã«ãã vfs_open()
ã®å¼æ° path
ã«ã¢ã¯ã»ã¹ãã¦ãã¾ãï¼ä½æ
ãããã§ãããã¨ããã¨ï¼BTFã«ã¯é¢æ°ã®æ
å ±ãå
¥ã£ã¦ããã®ã§ï¼ã¾ã vfs_open
ã®é¢æ°ã®ãããã¿ã¤ãæ
å ±ãã¨ã¦ãã¦ï¼ãããã args->path
ã§vfs_open
ã®pathã«ã¢ã¯ã»ã¹ãã¦ããã¨ãåããã®ã§ï¼ããããã¹ã¯ãªãããçæã§ããã¨ããããã§ãï¼ ã¾ãkfuncã®å ´åkprobeã¨éã£ã¦å¼æ°ãèªåã§ãã£ã¹ããããã®ã§ï¼ééã£ããã£ã¹ãããã¦ãã¾ãã¨ãããã¹ããªããªãã¾ãï¼
ã«ã¼ãã«é¢æ°ã®å¼æ°ãæ§é ä½ã®ç¢ºèª
ãããã¾ãçæ©è½ã§ããï¼ä»¥ä¸ã®ããã«ãã¦BTFãå©ç¨ãã¦é¢æ°ã®ãããã¿ã¤ããï¼æ§é ä½ã®ã¡ã³ãã確èªã§ãã¾ãï¼
% sudo bpftrace -lv "kfunc:vfs_open" kfunc:vfs_open const struct path * path; struct file * file;
% sudo ./src/bpftrace -lv "struct path" struct path { struct vfsmount *mnt; struct dentry *dentry; };
ãã®æ§é ä½ã®ã¡ã³ããªãã ã£ãï¼ã¨ãªã£ãã¨ãã«ã¼ãã«ã½ã¼ã¹ã確èªããå¿ è¦ããªããªãã¾ãï¼
ç¾ç¶ã®BTFã®å¶ç´
BTFã¯é常ã«ä¾¿å©ã§ããï¼ä»¥ä¸ã®ãããªå¶ç´ã»èª²é¡ãããã¾ãï¼
- ç¾ç¶BTFã«å«ã¾ããé¢æ°ã¯non-statitcãªãã®ã®ã¿ (kprobeã¯staticãªé¢æ°ã«ã対å¿ãã¦ãã¾ã)
- ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã«æªå¯¾å¿ (ããã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãBTFã§ãã¬ã¼ã·ã³ã°ãããå ´åã¯ï¼ã«ã¼ãã«å´ã«ã¢ã¸ã¥ã¼ã«ãã¯ãªãçµã¿è¾¼ãã§ãã«ãããå¿ è¦ãããã¾ã)
- define å¤ã®æ¬ å¦
ãã®è¾ºãã®åé¡ã¯ããããã¨è§£æ±ºã«åãã¦è°è«ãããã¦ãã¾ãï¼ç¹ã«æå¾ã®defineããã¦ããå¤ãæ¬ å¦ããã¦ããã¨ããã®ã¯å°å³ã«ãã¬ã¼ã¹ããã°ã©ã ãæ¸ãæã«é害ã«ãªãã¾ãï¼ä¾ãã° AF_INET
ã®å¤ãç¥ãããã¨ãã¯ï¼BTFããã£ã¦ããããdefineããã¦ããããããã¤ã³ã¯ã«ã¼ãããå¿
è¦ãããããã§ãï¼è§£æ±ºçã®ä¸ã¤ã¯#defineã§ã¯ãªãenumã使ããã¨ã§ (enumã®æ
å ±ã¯BTFã«å«ã¾ãã¾ã)ï¼å®ã¯ä¸é¨ãããã¯BTFã«å¯¾å¿ã§ããããã«ãã®ãããªå¤æ´ãå ãããã¦ãã¾ãï¼å°æ¥çã«ã¯ãã®è¾ºãã®åé¡ã解決ãããã¨å¬ããã§ããï¼
Docker ãã«ãã»ã¹ã¿ãã£ãã¯ãã«ãã®æä¾
x86_64ç¨ã§ããï¼ãã¹ãããã¬ã¼ã¹ããããã®Dockerãã«ããããã¾ãï¼ä»¥ä¸ã®ããã«ãã¦å©ç¨ã§ãã¾ãï¼
% sudo docker run -it --privileged -v /sys/kernel/debug:/sys/kernel/debug:rw -v /lib/modules/:/lib/modules:ro -v /usr/src:/usr/src:ro quay.io/iovisor/bpftrace tcpconnect.bt
é©åã«debugfsãã«ã¼ãã«ãããã®å ´æãã³ã³ããå´ã«ãã¦ã³ãããå¿ è¦ãããã¾ããï¼docker buildã使ãã¨bpftraceã®ææ°çã使ãã¦ä¾¿å©ã§ãï¼ãã æ®å¿µãªããç¾ç¶BTFã®ãµãã¼ã(ã¨ãããlibbpfã¨ã®ãã«ã)ã¯ããã¾ããï¼
ã¾ãï¼github actionã«ããã¹ã¿ãã£ãã¯ãã«ããæä¾ãããããã«ãªã£ã¦ãã¾ãï¼Releaseãããªãªã¼ã¹ãã¼ã¸ã§ã³ã®ã¹ã¿ãã£ãã¯ãã«ããåå¾ã§ããä»ï¼ããåããã«ããã§ããgithubã¢ã¯ã·ã§ã³ã®embedded buildã®é ç®ããææ°ã®ã¹ã¿ãã£ãã¯ãã«ããåå¾ã§ãã¾ãï¼
ç¥ã£ã¦ããã¨ä¾¿å©ãªæ©è½
ã¿ãã«
以ä¸ã®ããã«ã¿ãã«ã使ããããã«ãªãã¾ããï¼ããããè¤æ°ããããä½ããªãã¦ãå¤ãæ ¼ç´ã§ãã¾ãï¼
% bpftrace -e 'BEGIN { @ = (0, 1, "abc"); printf("%d\n", @.0); print(@); }' Attaching 1 probe... 0 @: (0, 1, abc)
ãããã®è¤æ°ãã¼ã®å©ç¨
å¥ã«ããèªä½ã¯åãããã£ãæ©è½ãªã®ã§ããï¼ãã¾ãã¡èªç¥ããã¦ããªããããªã®ã§æ¸ãã¦ããã¾ãï¼ä»¥ä¸ã®ããã« @[]
ã«ä½¿ããã¼ã¯è¤æ°åæå®ã§ãã¾ãï¼
% sudo ./share/bpftrace -e 'BEGIN { @[pid, cpu] = 1; print(@); }' Attaching 1 probe... @[10189, 35]: 1
ããã使ãã¨æ¡ä»¶ï¼ä¾ãã°CPUçªå·ï¼ã«å¿ããéè¨ãªã©ãç°¡åã«ã§ãã¾ãï¼
ãã¤ããªå¤ã®ãã³ã
buf()
ã使ãã¨ï¼ãã¤ã³ã¿å
ã®ãã¼ã¿ããã³ããã§ãã¾ãï¼ä¼¼ããããªé¢æ°ã« str()
ãããã¾ããï¼ str()
ã¯NULLçµç«¯ãããæååãæ±ãç¹ãç°ãªãã¾ãï¼
% bpftrace -e 'tracepoint:syscalls:sys_enter_sendto { printf("Datagram bytes: %r\n", buf(args->buff, args->len)); }' -c 'ping 8.8.8.8 -c1' Attaching 1 probe... PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. Datagram bytes: \x08\x00@\xc3\x05\xb6\x00\x01\xe9\x0aW_\x00\x00\x00\x00\xb0H\x02\x00\x00\x00\x00\x00\x10\x11\x12\x13\x14\x15\x1 6\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&'()*+,-./01234567 ^C --- 8.8.8.8 ping statistics --- 1 packets transmitted, 0 received, 100% packet loss, time 0ms
ã¨ã©ã¼ãã§ãã¯
-k
ããã㯠-kk
ãªãã·ã§ã³ã§BPFããã°ã©ã å®è¡ä¸ã®ã¨ã©ã¼ããã§ãã¯ããããã«ãªãã¾ãï¼
% bpftrace -kk -e 'BEGIN { @[0] = 1; @[1]++; }' Attaching 1 probe... stdin:1:19-25: WARNING: Failed to map_lookup_elem: 0 BEGIN { @[0] = 0; @[1]++;} ~~~~~~
ããbpftraceã®ã¹ã¯ãªãããæå¾
ããã¨ããã«åããªãå ´åï¼ã¨ãããã -kk
ãã¤ãã¦å®è¡ãã¦ã¿ãã¨è¯ãã¨æãã¾ãï¼
ãã®æ©è½ã«ã¤ãã¦è£è¶³ããã¨ï¼BPFããã°ã©ã ã«ã¯ãã«ãã¼é¢æ°ã¨å¼ã°ããé¢æ°ãå©ç¨ãã¦ï¼ã«ã¼ãã«ã®å¦çãå¼ã³åºãæ©è½ãããã¾ãï¼ããã¯ããã°ã¦ã¼ã¶ããã°ã©ã ã«ãããã·ã¹ãã ã³ã¼ã«ã®ãããªãã®ã§ï¼BPFãããã®æä½ãåºåå¦çãªã©ã®æ©è½ã¯å
¨ã¦ãã®ãã«ãã¼é¢æ°ã«ãã£ã¦æãç«ã£ã¦ãã¾ãï¼bpftraceãå©ç¨ããå ´åã¯ç¹ã«ãã®ãã«ãã¼é¢æ°ãæèããå¿
è¦ã¯ãªãã§ããï¼éè¦ãªã®ã¯ãã«ãã¼é¢æ°ã®å¦çã¯ä½ããã®çç±ã«ãã£ã¦å¤±æãããã¨ãããã¨ãããã¨ã§ãï¼ãã®ã¨ãï¼ãã«ãã¼é¢æ°ã¯æ»ãå¤ã¨ãã¦ã¨ã©ã¼ãè¿ãã¾ããï¼ããã©ã«ãã§ã¯ãã®ã¨ã©ã¼å¤ã¯ç¡è¦ããã¾ãï¼ããããã§ãã¯ããã®ã -k
ããã³ -kk
ãªãã·ã§ã³ã§ãï¼ -k
㨠-kk
ã®éãã¯ï¼ãã§ãã¯ãããã«ãã¼é¢æ°ã®æ°ã§ãï¼bpftraceã¯ãã¼ã¿ã®èªã¿åºãããããªããã«ãã¼é¢æ°ã失æããã¨ï¼å¤ã0ã¨ãã¦è¿ãã¨ããä»æ§ã«ãªã£ã¦ãã¾ãï¼ããã«ãã£ã¦ï¼ä¾ãã° / @[tid] /
ã®ããã«æ¸ãã¨ï¼ @[tid]
ãåå¨ããªãå ´åã¯ãã®å¦çãã¹ãããã§ãããã¨ã«ãªãã¾ãï¼ãã®ãããªãã¯ããã¯ã¯bpftraceã®ã¹ã¯ãªããã§åºã使ããã¦ãã¦ï¼ãã®ã¨ã©ã¼ããã§ãã¯ãã¦ãã¾ãã¨åé·ãªå ´åãããããï¼ -k
ãªãã·ã§ã³ã§ã¯ãã®ãããªãã¼ã¿èªã¿åºãã®é¢æ°ã«é¢ãã¦ã¨ã©ã¼ã¯ãã§ãã¯ãã¾ããï¼ -kk
ã ã¨å
¨ã¦ã®ãã«ãã¼é¢æ°ã®ã¨ã©ã¼ããã§ãã¯ãã¾ãï¼ä¸ã¤æ³¨æç¹ã¨ãã¦ï¼ã¨ã©ã¼ãã§ãã¯æ©è½ãæå¹ã«ããã¨BPFããã°ã©ã ããã®åé·ããªãã¾ãï¼å ´åã«ãã£ã¦ã¯ãã®ãªã¼ãããããåé¡ã«ãªãå¯è½æ§ãããã¾ãï¼ã¾ãï¼ãã¼ãã§ããBPFããã°ã©ã é·ã«ã¯å¶éãããããï¼å¤§ããªbpftraceã¹ã¯ãªããã§ã¯ã¨ã©ã¼ãã§ãã¯æ©è½ãå©ç¨ã§ããªãå¯è½æ§ãããã¾ãï¼
whileã«ã¼ã
ãã¨ãã¨BPFããã°ã©ã ã¯åæ¢æ§ä¿è¨¼ã®ããã«ã«ã¼ãï¼å¾æ¹ã¸ã£ã³ãï¼ãä¸å許å¯ããã¦ãã¾ããã§ãããï¼verifierãè³¢ããªã£ã¦æéåã§åæ¢ããã«ã¼ãå¦çãæ¸ããããã«ãªãã¾ããï¼ã«ã¼ãã«5.3以ä¸ãå¿
è¦ã§ãï¼bpftraceã§ã¯ while()
ã使ãã¾ãï¼
% bpftrace -e 'i:ms:100 { $i = 0; while ($i <= 100) { printf("%d ", $i); $i++} exit(); }'
ããã§ï¼verifierã¯å®éã«ã«ã¼ãå¦çãä¸ã¤ä¸ã¤è§£éãã¦ããï¼ãã®ã«ã¼ããåæ¢ããããã¿ã¦ãã¾ãï¼å¾ã£ã¦ï¼æ¥µç«¯ã«å¤§ããªã«ã¼ãã¯verifierã®å½ä»¤è§£éæ°ä¸éã«å¼ã£ãããããå®è¡ã§ãã¾ããï¼ä¾ãã°ï¼ä»¥ä¸ã®ããã°ã©ã ã¯ã¨ã©ã¼ã«ãªãã¾ãï¼
$i = 0; while ($i <= rand) { printf("%d ", $i); $i++} exit(); }
ãã㯠rand
ã¯uint64ã®å¤ãè¿ãé¢æ°ã§ï¼verifierã¯ãã®å¤ã®æ大ãæ³å®ãã¦verificationããããªãããã§ãï¼ä»¥ä¸ã®ããã«ï¼æ示çã«ã«ã¼ãä¸éãå®ãã¦ãããã¨ããã°ã©ã ãå®è¡ã§ããããã«ãªãã¾ãï¼
$i = 0; $max = rand; if ($max > 100) { $max = 100;} while ($i <= $max) { printf("%d ", $i); $i++} exit();}
ãã®ä»ä¸»è¦ãªå¤æ´ã»æ´æ°ç¹
s390x, aarch64ã®ãµãã¼ã
ããã°ã¨ã³ãã£ã¢ã³ã®ã¢ã¼ããã¯ãã£ã«åå¨ããä¸é¨åé¡ãä¿®æ£ããã¾ããï¼ã¾ãaarch64ãªã©ã®ã¢ã¼ããã¯ãã£ã§ã¯ï¼ã«ã¼ãã«ã¢ã¼ãã¨ã¦ã¼ã¶ã¢ã¼ãã®ã¢ãã¬ã¹ã¹ãã¼ã¹ãç°ãªããã¨ã«èµ·å ãã¦ï¼ããããã®ãã¼ã¿ã®èªã¿åºãã«å°ç¨ã®ãã«ãã¼é¢æ°*2ãå©ç¨ããå¿ è¦ãããï¼ãã°ããã®ébpftraceã¯ããããµãã¼ããã¦ãã¾ããã§ãããï¼ã¤ãæè¿ãã®æ©è½ããµãã¼ãããã¾ããï¼çµæã¨ãã¦aarch64ã§ãåé¡ãªãbpftraceã使ããããã«ãªã£ã¦ããã¯ãã§ãï¼
æ¥æ¬èªçããã¥ã¡ã³ã
ã¯ã³ã©ã¤ãã¼ãã¥ã¼ããªã¢ã«ã«ã¯æ¥æ¬èªè¨³(æ訳ã§ã)ãããã¾ãï¼Reference Guideã¯è±èªã®ã¿ã§ãï¼
Discourse ãã©ã¼ã©ã
Discourceã«bpftraceã®ãã©ã¼ã©ã ãã§ãã¾ããï¼è³ªåã¯ããã«æããã¨è¯ãã¨æãã¾ãï¼éçºè ã®æ¹ãè¦ã¦ãã¾ãï¼
ãã詳ããç¥ãããæ¹
å®éã«ã¯ä»ã«ãããããã¨æ©è½ã追å ããã¦ãã¾ãï¼è©³ãã㯠CHANGELOG ãã©ããï¼
ãã°æ å ±
æå¾ã«ï¼ç¾å¨ã®bpftraceã«ã¯ printf()ããã¨ãã«ééã£ãå¤ãããã¾ãã«è¡¨ç¤ºããã ã¨ããå²ã¨è´å½çãªãã°ãããã¾ãï¼é·ãéåå ãåãããéçºè
ãå°ããã¦ãã¾ããããï¼ã©ãããLLVMã®ããã¯ã¨ã³ãã«åé¡ãããã¨ãããã¨ãåããã¾ããï¼è©³ãã㯠#1305, Bugzilla â Bug 47591 ãã©ããï¼ï¼2020å¹´12æç¾å¨ãã®ãã°ã¯ä¿®æ£ããã¦ãããï¼ç¾ç¶ããã«å¯¾å¿ããå®ç§ãªæ¹æ³ã¯ãªãã®ã§ããï¼printf()ã®ä½ç½®ãå¤ãããããã¨çµæçã«BPFã®ã³ã¼ããå¤ããã®ã§åé¡ã解決ããå ´åãããã¾ãï¼ä¸è¨ã§ç´¹ä»ãã -k
ããã㯠-kk
ãªãã·ã§ã³ãæå¹ã§ãï¼ããbpftraceã使ã£ã¦ãã¦æããã«åºåãããããå ´åã¯ãã®ãã°ãçã£ã¦ã¿ã¦ãã ããï¼ã¡ãªã¿ã«æ¬å½ã«ãã®ãã°ãã©ããã¯BPFããã°ã©ã ã®ã¢ã»ã³ãã©ã³ã¼ããçºãã¦ã¿ãã¨åããã¾ãï¼ä¸èªç¶ã«loadã¨storeå½ä»¤ãå
¥ãæ¿ãã£ã¦ããç®æãããã¯ãã§ãï¼
2020-12-4 追è¨
ãã®ãã°ã§ããï¼LLVM12ã§ä¿®æ£ãããããã§ãï¼ãã ãLLVM12ãå®éã«å©ç¨ãããããã«ãªãã®ã¯æ°å¹´ãããã§ããããï¼bpftraceå´ã§ããªãã¨ãä¿®æ£ã§ããªããããããã¨æ¤è¨ãã¦ãã¾ãï¼
追è¨ããã¾ã§
ãã¾ã
bpftaceã«ããBitVisorã®ãã¬ã¼ã¹
å æ¥BitVisorãbpftraceãã¬ã¼ã¹ããã¨ããå 容ã®çºè¡¨ãBitVisor Summit 9ã§ãããªãã¾ããï¼è¨ã£ã¦ãæå³ãä¸æããããã¾ãããï¼èå³ã®ããæ¹ã¯ãç¬è¦§ãã ãã â ã¹ã©ã¤ã
macããbpftraceã試ã
æåã«æ¸ãã¾ãããï¼Intel macããbpftraceãç°¡åã«è©¦ãããã®ãä½ãã¾ããï¼è©³ããã¯ãã¡ããã©ããï¼
ã·ã¹ãã ç³»è«æã®æ å ±åéæ¹æ³
ãã¾ãBPFã¨é¢ä¿ãªãã¦æ縮ã§ããï¼æ¨æ¥ã·ã¹ãã ç³»è«æã®æ å ±åéæ¹æ³ã¨ããã¨ã³ããªãæ¸ãã¾ããï¼å¦è¡çã«ãBPFã使ã£ãè«æã¯çµæ§åºã¦ããã®ã§ï¼æ°ã«ãªãæ¹ã¯ãã§ãã¯ãã¦ã¿ã¦ãã ããï¼
æå¾ã«-- bpftraceã«é¢ãã£ã¦ãã¦
æå¾ã«æè§ã®æ©ä¼ãªã®ã§ä¸å¹´ébpftraceã«é¢ãã£ã¦ããææ³ãæ¸ãã¦ã¿ããã¨æãã¾ãï¼ã¾ãï¼bpftraceã¯ãã¨ãã¨Alastair Robertsonããä¸äººã«ãã£ã¦2017å¹´ããéçºãããããã¸ã§ã¯ãã§ãï¼ãããï¼ï¼ãã®å¾ï¼Brendan Greggããã«ããå¤ãã®ãã¼ã«ã追å ããï¼ããã«Netflixã®ã¨ã³ã¸ãã¢ã®æ¹ã«ãã£ã¦åºæ¬çãªæ©è½ã®å¤ãã追å ããã¾ããï¼ã ãããå»å¹´ã®æ¥å ããã®ãã¨ã ã¨æãã¾ãï¼æ¸ç±"BPF Performance Tools" (Brendan Gregg箸)ã¯ãã®è¾ºã®ãã¼ã¸ã§ã³ã®bpftraceããã¨ã«æ¸ããã¦ãã¾ãï¼ãã®ããã¨æ¯ã¹ã¦ï¼bpftraceã¯çµæ§é²åãã¦ããã¨æãã¾ãï¼ç¾å¨ã¯åè¿°ã®3人ã¯ã³ãããã«é¢ãã¦ã¯æ§ããã§ï¼ä»ã¯ç§ãå«ããå¥ã®3人ã主ã«ããã¸ã§ã¯ãã«é¢ãã£ã¦ãã¾ãï¼ç¹ã«ãã®ä¸ã®ä¸äººã®FBã®ã¨ã³ã¸ãã¢ã®æ¹ãã¡ã¤ã³ã®ã¡ã³ããã¨ãã£ãæãã§ãï¼ãã®ä»Red HatãIBMã®æ¹ãããå®æçã«ã³ã³ããªãã¥ã¼ã·ã§ã³ãããã¾ãï¼
ããã¾ã§ã«ç§ãã³ããããã主ãªå 容ã«ã¯ä»¥ä¸ãããã¾ãï¼
- æ´æ°åãã¤ã³ã¿ã®ãã£ã¹ãã®ãµãã¼ã#942
- kprobe offset ã«å¯¾ããã¢ã¿ããæ©è½ã®è¿½å #956
- BPFã³ã³ããã¹ãã¢ã¯ã»ã¹ã®ä¿®æ£ #1104
- bpftrace oneliner ããã¥ã¡ã³ã翻訳 #1176, 訳æ
- bpf helper functionã¨ã©ã¼ã®ã¬ãã¼ãæ©è½ #1276
- fuzzingã®ãµãã¼ã #1601, #1633, ããã¥ã¡ã³ã
- ãã®ä»ã®ãã°ä¿®æ£ãªã©
ä¸çªæåã®PRã¯æ´æ°åãã¤ã³ã¿ã®ãã£ã¹ãã®ãµãã¼ã ((int8*)$var
ã¿ãããªãã¤) ã§ããï¼ããã¯bpftraceã使ãå§ãã¦ããã«ãã®æ©è½ããªãã¦ä¸ä¾¿ã ã£ãã®ã§ï¼ãªãã¨ãªãã½ã¼ã¹ãè¦ã¦ä¿®æ£ãã¦PRãéã£ããã®ã§ãï¼C++ãªãã¦3年以ä¸ã¾ã¨ãã«æ¸ãã¦ãªãã£ããï¼ããããC++ã¯ç解ãã¦ãªããï¼ãã®ã¨ãå¤å1å²ãããããbpftraceã®ãã¨ãåãã£ã¦ãªãã£ããã§ããï¼å°ããã£ã¼ãããã¯ããã£ããã¨ã«ãã¼ã¸ãããã®ãã¯ãã¾ãã§ããï¼bpftraceã¯ä½¿ã£ã¦ããã»ã©ããããã¨åé¡ã«ééããã®ã§ï¼æ¯æãã¨ãã¦ç©ºãã¦ãæéã«ãããç´ãã¦è¡ã£ããä»ã®ç¶æ
ã«ãªã£ãã¨ããã®ãå®æ
ã§ãï¼ããã¾ã§é¢ãã£ãOSSããã¸ã§ã¯ãã¯bpftraceãåãã¦ã§ããï¼ãã¯ãä¸çä¸ã®äººã¨ä½æ¥ããã¦ããã¨ããã®ã¯OSSã®ééå³ã§ï¼ã¨ã¦ã楽ããçµé¨ã§ãï¼ä½è
ã®æ¹ã«"good work!"ã¨è¨ãããã¨ãã¯ãå¬ãããã®ã§ãï¼
bpftraceã«ã³ããããããã¨ã§BPFã«é¢ãã¦ãã詳ãããªã£ãã®ã¯å¿è«ã®ãã¨ï¼ãã¨ãã¨ãç§ã¯Linuxã®ãã¬ã¼ã·ã³ã°ææ³ã«ã¤ãã¦ããããã¨èª¿æ»ãã¦ä½¿ã£ã¦ãã¾ãããï¼å ·ä½çã«ããããå©ç¨ããããã«ã¤ãã¦ã®ç解ãæ·±ã¾ãã¾ããï¼æè¿æ¹ãã¦"BPF Performance Tools"ãèªã¿è¿ãã¦ã¿ãã®ã§ããï¼ã¤ãã«çã«ç解ã§ããããã«ãªã£ããããªæ°ããã¦ãã¾ãï¼
ã¾ãbpftraceèªä½ã¯flex/bisonã«ããåå¥è§£æï¼æ§æ解æããããªã£ãå¾ï¼LLVMã«ããIRãåºåããã³ã³ãã¤ã©ã§ãï¼ããCPU, OS, ã³ã³ãã¤ã©ãèªä½ããã¨è¯ããªãã¦è©±ãããã¾ããï¼ç§ã¯ç°¡åãªCPUãOSã¯ä½ã£ããã¨ããã£ããã®ã®*3ï¼ã³ã³ãã¤ã©ã¯çé¢ç®ã«åãçµãã ãã¨ããªãã£ãã®ã§ï¼ãã®è¾ºãããããã¨åå¼·ã«ãªãã¾ããï¼ï¼ã¨åæã«LLVMã®é£ãããç¥ããã¨ã«...ï¼bpftraceã¯ã³ã³ãã¯ãã«ããããã¨ã¾ã¨ã¾ã£ã¦ãã¨æãã®ã§ï¼ãããã«ã¤ãã¦èå³ã®ããæ¹ã¯ã³ã¼ããè¦ã¦ã¿ãã¨æ¥½ããã®ã§ã¯ãªãããªã¨æãã¾ãï¼
æ¥å¹´ã®æ±è²
æ¥å¹´ã¯ä»å¹´ã¨åæ§ï¼æ¬æ¥ãä¸çªãªã®ã¯å¿è«ã§ããï¼bpftraceã«é¢ããã³ãããã¯ç¶ç¶ãã¤ã¤ï¼ã«ã¼ãã«ã®æ¹ã«å¯¾ãã¦ãã³ããããã¦ãããã...ãªãã¦æãã¾ãï¼FBã®BPFãã£ã¦ãã¨ã³ã¸ãã¢ã¯ã«ã¼ãã«ãããããã®ã¯å¿è«ã®ãã¨ï¼LLVMï¼æ¬ä½ã®æ¹ï¼ãã§ããã®ã§ã¿ããªããããªãã¨æã次第ã§ãï¼ã¾ã ã¾ã èªåãé å¼µããªãã¨ããã¾ãããï¼
ããã§ã¯ã¿ãªããï¼ã¡ãã£ã¨æ©ãã§ãããããå¹´ãï¼