Closed Songchunbo closed 4 months ago
Hi and thanks, but please do not use issues in this repository to ask generic questions about BPF or how to use bpftool. Your question is not related to how bpftool works, let alone to something specific to the GitHub mirror repository. In the future, please consider other channels (check out the “Community” menu at the top of https://ebpf.io/ for some pointers, if necessary).
As for your question, what you observe is because bpftool loads the program but does not attach it, so your program does not run and never produce logs in the trace pipe. This is something you could check by checking whether your program does anything else (such as updating BPF map entries), or by checking the number of times the program ran in the program statistics dumped by bpftool prog show
when stats are enabled, or if I remember correctly, by checking whether the program is attached to either a perf event with bpftool perf list
or a BPF link with bpftool link list
. Bpftool does not support attaching a tracing program that it loaded in a previous step. However, it can attach right after loading under certain circumstances, search for the autoattach
keyword in the man pages.
Hi, apologies for the inconvenience. I'll post it to the eBPF community channel.
Regarding your suggestion, I attempted to enable BPF stats using the command sysctl -w kernel.bpf_stats_enabled=1
, but there were no stats data when dumping the program via bpftool prog show
. Furthermore, executing bpftool perf list
or bpftool link list
yielded no output. Also, there are no mentions of the autoattach
and auto
keywords in the bpftool man pages.
# bpftool prog show
1147: kprobe name do_unlinkat tag 00e8d3ef8d99f9ec gpl
loaded_at 2024-05-07T04:39:10+0800 uid 0
xlated 152B jited 96B memlock 4096B map_ids 261
btf_id 326
# bpftool perf list
# bpftool link list
#
Hi, apologies for the inconvenience. I'll post it to the eBPF community channel.
No inconvenience at all, I just want to avoid Issues in this repo to turn into a Q&A or support channel.
Regarding your suggestion, I attempted to enable BPF stats using the command
sysctl -w kernel.bpf_stats_enabled=1
, but there were no stats data when dumping the program viabpftool prog show
.
If both counters (time spent running and run count) remain at 0, bpftool doesn't display them. So that would confirm your program doesn't run.
Furthermore, executing
bpftool perf list
orbpftool link list
yielded no output.
This confirms the program is not attached. Compare with the output from when you load your programs through ./kprobe
Also, there are no mentions of the
autoattach
andauto
keywords in the bpftool man pages.
This option is relatively recent (bpftool 7.1) and might not be available on the man page shipped on your distro. See here.
Thanks Quentin , with the ./kprobe execution. It has the output results . So it seems the program is not attached via 'bpftool'
loading. I'll check the man pages for autoattach
usage. But why the program is not attached when using bpftool prog load
root@-VirtualBox:~# bpftool perf list
pid 12393 fd 7: prog_id 1227 kprobe func do_unlinkat offset 0
pid 12393 fd 9: prog_id 1229 kretprobe func do_unlinkat offset 0
root@-VirtualBox:~# bpftool link list
38: perf_event prog 1227
bpf_cookie 0
pids kprobe(12393)
39: perf_event prog 1229
bpf_cookie 0
pids kprobe(12393)
Loading and attaching programs are two separate steps, although many user space tools usually handle both at the same time.
Bpftool has commands to attach some program types, for example XDP programs or some other specific types of programs, but not tracing programs.
But why the program is not attached when using
bpftool prog load
Because historically, this was not possible. Tracing programs needed to be attached to a perf event, and they needed a process to keep the file descriptor to that perf event open to remain attached. In other words, if bpftool attached BPF tracing programs, they'd be detached as soon as bpftool returned.
Now that BPF links exist, this requirement is no longer valid and we could attach programs with bpftool. But people don't usually need to attach programs that have been loaded from a previous bpftool invocation, and we just haven't implemented it so far. Last time someone wanted to load+attach they contributed the autoattach
feature, which tells bpftool to do both steps at the same time, provided libbpf can derive the right attach point from the ELF section names; this should get you covered for your use case, I expect.
Tried below instruction, the /sys/kernel/debug/tracing/trace_pipe can get the output.
# bpftool prog load .output/kprobe.bpf.o /sys/fs/bpf/kprobe autoattach
But the bpftool perf list
and bpftool link list
are different from ./kprobe
.
root@-VirtualBox:~# bpftool perf list
root@-VirtualBox:~# bpftool link list
42: perf_event prog 1262
But the
bpftool perf list
andbpftool link list
are different from./kprobe
.
I would expect ./kprobe
to maybe attach your program “the old way”, without a BPF link; so the program would show in bpftool perf list
, but not in bpftool link list
. Conversely, I think bpftool uses BPF links, so your program shows in bpftool link
but not in bpftool perf
. The fact that there are two solutions is also an artefact of the historical BPF evolutions; the most recent (and flexible) approach would be with the BPF links, but this would not otherwise affect your program behaviour or performance. In other words, what you observe is expected, and shouldn't have any impact on your program.
As for how to unify the output from bpftool listings, I haven't figured out a satisfying solution yet :confused:.
Got it. Thanks very much for the detail explanation.
Loading and attaching programs are two separate steps, although many user space tools usually handle both at the same time.
Now that BPF links exist, this requirement is no longer valid and we could attach programs with bpftool. But people don't usually need to attach programs that have been loaded from a previous bpftool invocation, and we just haven't implemented it so far. Last time someone wanted to load+attach they contributed the
autoattach
feature, which tells bpftool to do both steps at the same time, provided libbpf can derive the right attach point from the ELF section names; this should get you covered for your use case, I expect.
Q: When trying 'libbpf/libbpf-bootstrap' project. I tried to load the generated BPF prog with 'bpftool', the 'bpf_trace_printk' seems not output to '/sys/kernel/debug/tracing/trace_pipe'. How to fix it or debug it?