#ebpf #btf

btfparse

A BTF parser library capable of navigating types

4 stable releases

1.3.3 Nov 26, 2024
1.3.2 Jun 23, 2024
1.3.1 Feb 18, 2024

#340 in Development tools

Download history 2/week @ 2024-09-25 1/week @ 2024-10-09 1/week @ 2024-10-16 1/week @ 2024-10-30 1/week @ 2024-11-06 71/week @ 2024-11-20 82/week @ 2024-11-27

154 downloads per month

Apache-2.0

160KB
3.5K SLoC

btfparse

btfparse is a library that can be used to parse the BPF Type Format (BTF)

Examples

Retrieving member offsets

The full source code for this example can be found in the ./src/bin/get-type-offset.rs file.

fn main() {
    let argument_list: Vec<String> = env::args().collect();
    if argument_list.len() != 4 {
        println!("Usage:\n\tget-type-offset /path/to/btf/file <type_name> <path>\n");
        return;
    }

    let btf_file_path = Path::new(&argument_list[1]);
    let btf_type_name = &argument_list[2];
    let type_path = &argument_list[3];

    println!("Opening BTF file: {:?}", btf_file_path);

    let vmlinux_btf_file = ReadableFile::new(btf_file_path);
    let type_information = TypeInformation::new(&vmlinux_btf_file).unwrap();
    let offset = type_information
        .offset_of(type_information.id_of(btf_type_name).unwrap(), type_path)
        .unwrap();

    println!("{} => {}: {:?}", btf_type_name, type_path, offset);
}

Example output:

sh-5.2$ get-type-offset /sys/kernel/btf/vmlinux 'dentry' 'd_name.len'
Opening BTF file: "/sys/kernel/btf/vmlinux"
dentry => d_name.len: (23, ByteOffset(36))

No runtime deps