Why your bpftrace programs should not include kernel headers.
Imagine you write a bpftrace program which needs to access a data structure of
some kernel data type, say struct task_struct
. In order to generate correct
offsets for accessing the struct fields, bpftrace needs to know the layout of
the type on the running kernel. Historically, this could be achieved by
providing the correct kernel headers to the program using the #include
directive. With the coming of BTF (BPF Type Information), this is no longer
necessary as bpftrace is able to automatically extract the types layout from
BTF. Therefore, for a vast majority of use-cases, including headers is not only
unnecessary, but can also lead to unexpected problems and should be avoided, if
possible. In this blog post, we will look into the reasons why that is the case
and show that the less headers a bpftrace program includes, the more portable it
is across kernel versions.