/libbtf

Utility library for parsing BPF Type Format data

Primary LanguageC++MIT LicenseMIT

libbtf

CI/CD Coverage Status

Utility library for parsing BPF Type Format data

This project permits parsing the BTF data generated by LLVM and "pahole" and returning it as C++ types.

Building

Run cmake -S . -B build to configure the project, then run cmake --build build to build the project.

Running the test

Linux build/test/tests -d yes

Windows build/test/Debug/tests.exe -d yes

Using the library

  1. Add reference to your cmake project
Include(FetchContent)

FetchContent_Declare(
  libbtf
  GIT_REPOSITORY https://github.com/Alan-Jowett/libbtf.git
  GIT_TAG        0.0.1 # or a later release
)

FetchContent_MakeAvailable(libbtf)

include_directories(${libbtf_SOURCE_DIR})

target_link_libraries(<your project> PRIVATE libbtf)
  1. Obtain pointer to vector of BTF data.
    auto reader = ELFIO::elfio();
    if (!reader.load(argv[1])) {
        std::cerr << "Failed to load " << argv[1] << "\n";
        return 1;
    }

    auto btf = reader.sections[".BTF"];
  1. Parse the BTF data.
    libbtf::btf_type_data btf_data = std::vector<std::byte>(
      {reinterpret_cast<const std::byte *>(btf->get_data()),
       reinterpret_cast<const std::byte *>(btf->get_data() + btf->get_size())});
  1. Use the BTF data.
    std::ostringstream json;
    btf_data.to_json(json);

    std::cout << libbtf::pretty_print_json(json.str()) << "\n";