foniod / redbpf

Rust library for building and running BPF/eBPF modules
Apache License 2.0
1.71k stars 136 forks source link

failed to find valid kernel BTF #360

Open ishanjain28 opened 1 year ago

ishanjain28 commented 1 year ago

Hi! I am trying to write a basic BPF program using this tool and I keep running into a failed to find valid kernel BTF error from libbpf.

I am on arch linux and if I understand correctly, This should have BTF built into it.

» uname -ra
Linux emerald 6.1.2-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 31 Dec 2022 17:40:35 +0000 x86_64 GNU/Linux
» ls -la /sys/kernel/btf/vmlinux
.r--r--r-- 5.0M root  6 Jan 16:55 /sys/kernel/btf/vmlinux

I tried building my program like this,

RUST_BACKTRACE=1 REDBPF_VMLINUX=system cargo bpf build

RUST_BACKTRACE=1 REDBPF_VMLINUX=/sys/kernel/btf/vmlinux cargo bpf build

RUST_BACKTRACE=1 REDBPF_VMLINUX= cargo bpf build

For the first two, I get this failed to find Kernel BTF error and for the last one, Rust bindings return a VmLinuxNotFound but I guess it's coming from the read_elf portion of libbpf rather than read_raw? I am not sure what file it tries to read and fails in the process.

This project currently ships libbpf version 0.6, I also tried libbpf version 1.1 and the libbpf HEAD but I get the same error with all 3 versions of libbpf.

Stacktrace:

emerald :: ~/hobby/dhcp 130 » RUST_BACKTRACE=1 REDBPF_VMLINUX=system cargo bpf build
warning: function `print_cargo_bpf_llvm_version` is never used
 --> /home/ishan/hobby/redbpf/cargo-bpf/build.rs:5:4
  |
5 | fn print_cargo_bpf_llvm_version() {
  |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `cargo-bpf` (build script) generated 1 warning
   Compiling bpf-sys v2.3.0 (/home/ishan/hobby/redbpf/bpf-sys)
   Compiling cargo-bpf v2.3.0 (/home/ishan/hobby/redbpf/cargo-bpf)
   Compiling redbpf-probes v2.3.0 (/home/ishan/hobby/redbpf/redbpf-probes)
error: failed to run custom build command for `redbpf-probes v2.3.0 (/home/ishan/hobby/redbpf/redbpf-probes)`

Caused by:
  process didn't exit successfully: `/home/ishan/hobby/dhcp/target/bpf/release/build/redbpf-probes-e692d5927a744714/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-changed=include/
  cargo:rerun-if-changed=include/bpf_helper_defs.h
  cargo:rerun-if-changed=include/bpf_helpers.h
  cargo:rerun-if-changed=include/bpf_iter.h
  cargo:rerun-if-changed=include/kernel_supplement.h
  cargo:rerun-if-changed=include/redbpf_helpers.h
  cargo:rerun-if-changed=include/vmlinux_supplement.h
  cargo:rerun-if-changed=include/xdp.h
  cargo:rerun-if-env-changed=KERNEL_SOURCE
  cargo:rerun-if-env-changed=KERNEL_VERSION
  cargo:rerun-if-env-changed=REDBPF_VMLINUX
  Jan 06 19:13:10.707 DEBUG build_script_build: Generating bindings with BTF of vmlinux

  --- stderr
  libbpf: failed to find valid kernel BTF
  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: error on bpf_bindgen::get_builder_vmlinux: error on vmlinux_btf_dump: VmlinuxNotFound

  Stack backtrace:
     0: anyhow::error::<impl anyhow::Error>::msg
     1: build_script_build::generate_bindings_vmlinux::{{closure}}
     2: core::result::Result<T,E>::or_else
     3: build_script_build::generate_bindings_vmlinux
     4: build_script_build::main
     5: core::ops::function::FnOnce::call_once
     6: std::sys_common::backtrace::__rust_begin_short_backtrace
     7: std::rt::lang_start::{{closure}}
     8: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once
               at /rustc/388538fc963e07a94e3fc3ac8948627fd2d28d29/library/core/src/ops/function.rs:287:13
     9: std::panicking::try::do_call
               at /rustc/388538fc963e07a94e3fc3ac8948627fd2d28d29/library/std/src/panicking.rs:483:40
    10: std::panicking::try
               at /rustc/388538fc963e07a94e3fc3ac8948627fd2d28d29/library/std/src/panicking.rs:447:19
    11: std::panic::catch_unwind
               at /rustc/388538fc963e07a94e3fc3ac8948627fd2d28d29/library/std/src/panic.rs:140:14
    12: std::rt::lang_start_internal::{{closure}}
               at /rustc/388538fc963e07a94e3fc3ac8948627fd2d28d29/library/std/src/rt.rs:148:48
    13: std::panicking::try::do_call
               at /rustc/388538fc963e07a94e3fc3ac8948627fd2d28d29/library/std/src/panicking.rs:483:40
    14: std::panicking::try
               at /rustc/388538fc963e07a94e3fc3ac8948627fd2d28d29/library/std/src/panicking.rs:447:19
    15: std::panic::catch_unwind
               at /rustc/388538fc963e07a94e3fc3ac8948627fd2d28d29/library/std/src/panic.rs:140:14
    16: std::rt::lang_start_internal
               at /rustc/388538fc963e07a94e3fc3ac8948627fd2d28d29/library/std/src/rt.rs:148:20
    17: std::rt::lang_start
    18: main
    19: <unknown>
    20: __libc_start_main
    21: _start
               at /build/glibc/src/glibc/csu/../sysdeps/x86_64/start.S:115', /home/ishan/hobby/redbpf/redbpf-probes/build.rs:287:37
  stack backtrace:
     0: rust_begin_unwind
               at /rustc/388538fc963e07a94e3fc3ac8948627fd2d28d29/library/std/src/panicking.rs:575:5
     1: core::panicking::panic_fmt
               at /rustc/388538fc963e07a94e3fc3ac8948627fd2d28d29/library/core/src/panicking.rs:64:14
     2: core::result::unwrap_failed
               at /rustc/388538fc963e07a94e3fc3ac8948627fd2d28d29/library/core/src/result.rs:1790:5
     3: core::result::Result<T,E>::unwrap
     4: build_script_build::main
     5: core::ops::function::FnOnce::call_once
  note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
warning: build failed, waiting for other jobs to finish...
error: failed to compile the `src/dhcp' program
emerald :: ~/hobby/dhcp 1 »