Skip to content

Commit

Permalink
libbpf-cargo: Add proper logging infrastructure
Browse files Browse the repository at this point in the history
We support a very limited form of logging by printing certain actions
happening when the --debug option is provided. While not particularly
useful at the current stage, this is probably functionality worth
keeping in the long run.
However, the current implementation is questionable at best: we plumb
through some boolean flag everywhere, which convolutes various call
sites and requires potentially excessive changes to support emitting a
new log message somewhere.
With this change we switch over to using proper logging based on the log
crate and other parts of the ecosystem. As a result, we get support for
multiple log levels, proper time stamping, limited output coloring, and
additional filtering capabilities via environment variables. At the same
time, we remove usage of flags that have to be plumbed through
everywhere, because logging state is global state.

Before:
  $ cargo run -- libbpf build --manifest-path libbpf-rs/examples/capable/Cargo.toml --clang-args='-I~/.cargo/git/checkouts/vmlinux.h-ec81e0afb9d5f7e2/83a228/include/x86/' --debug
  > Metadata for package=libbpf-cargo
  >         null
  > Metadata for package=libbpf-rs
  >         null

After:
  $ cargo run -- libbpf build --manifest-path libbpf-rs/examples/capable/Cargo.toml --clang-args='-I~/.cargo/git/checkouts/vmlinux.h-ec81e0afb9d5f7e2/83a228/include/x86/' -vvv
  > [2025-01-16T17:10:44Z DEBUG libbpf_cargo::metadata] Metadata for package=libbpf-cargo
  > [2025-01-16T17:10:44Z DEBUG libbpf_cargo::metadata]     null
  > [2025-01-16T17:10:44Z DEBUG libbpf_cargo::metadata] Metadata for package=libbpf-rs
  > [2025-01-16T17:10:44Z DEBUG libbpf_cargo::metadata]     null

For tests, usage is simple as well:
  $ RUST_LOG=trace cargo test -- test::test_make_basic --nocapture
  > [2025-01-16T17:17:46Z DEBUG libbpf_cargo::metadata] Metadata for package=proj
  > [2025-01-16T17:17:46Z DEBUG libbpf_cargo::metadata]     null
  > [2025-01-16T17:17:46Z DEBUG libbpf_cargo::build] Found bpf progs to compile:
  > [2025-01-16T17:17:46Z DEBUG libbpf_cargo::build]        UnprocessedObj { package: "proj", path: "/tmp/.tmpXhQPS6/proj/src/bpf/prog.bpf.c", out: "/tmp/.tmpXhQPS6/proj/target/bpf", name: "prog" }
  > [2025-01-16T17:17:46Z DEBUG libbpf_cargo::build] Building /tmp/.tmpXhQPS6/proj/src/bpf/prog.bpf.c
  > [2025-01-16T17:17:46Z DEBUG libbpf_cargo::metadata] Metadata for package=proj
  > [2025-01-16T17:17:46Z DEBUG libbpf_cargo::metadata]     null
  > [2025-01-16T17:17:46Z DEBUG libbpf_cargo::gen] Found bpf objs to gen skel:
  > [2025-01-16T17:17:46Z DEBUG libbpf_cargo::gen]  UnprocessedObj { package: "proj", path: "/tmp/.tmpXhQPS6/proj/src/bpf/prog.bpf.c", out: "/tmp/.tmpXhQPS6/proj/target/bpf", name: "prog" }
  > test test::test_make_basic ... ok

Signed-off-by: Daniel Müller <[email protected]>
  • Loading branch information
d-e-s-o authored and danielocfb committed Jan 16, 2025
1 parent 7ac3a07 commit 1885224
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 102 deletions.
193 changes: 193 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions libbpf-cargo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Unreleased
- Removed `SkeletonBuilder::skip_clang_version_check`
- Removed `--skip-clang-version-checks` option of `libbpf build`
sub-command
- Replaced `--debug` option of `libbpf` sub-command with `-v` /
`--verbose`


0.25.0-beta.1
Expand Down
3 changes: 3 additions & 0 deletions libbpf-cargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ default = ["libbpf-rs/default"]
[dependencies]
anyhow = "1.0.1"
cargo_metadata = "0.15.0"
env_logger = { version = "0.11.6", default-features = false, features = ["auto-color", "humantime"] }
libbpf-rs = { version = "=0.25.0-beta.1", default-features = false, path = "../libbpf-rs" }
log = "0.4.25"
memmap2 = "0.5"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand All @@ -40,4 +42,5 @@ clap = { version = "4.0.32", features = ["derive"] }

[dev-dependencies]
goblin = "0.9"
test-log = { version = "0.2.16", default-features = false, features = ["log"] }
vmlinux = { git = "https://github.com/libbpf/vmlinux.h.git", rev = "83a228cf37fc65f2d14e4896a04922b5ee531a94" }
25 changes: 9 additions & 16 deletions libbpf-cargo/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use anyhow::anyhow;
use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
use log::debug;
use tempfile::tempdir;

use crate::metadata;
Expand Down Expand Up @@ -127,15 +128,12 @@ fn format_command(command: &Command) -> String {
///
/// for each prog.
fn compile_one(
debug: bool,
source: &Path,
out: &Path,
clang: &Path,
clang_args: &[OsString],
) -> Result<CompilationOutput> {
if debug {
println!("Building {}", source.display());
}
debug!("Building {}", source.display());

let mut cmd = Command::new(clang.as_os_str());
cmd.args(clang_args);
Expand Down Expand Up @@ -204,7 +202,6 @@ fn compile_one(
}

fn compile(
debug: bool,
objs: &[UnprocessedObj],
clang: &Path,
mut clang_args: Vec<OsString>,
Expand All @@ -231,7 +228,7 @@ fn compile(
let mut dest_path = obj.out.to_path_buf();
dest_path.push(&dest_name);
fs::create_dir_all(&obj.out)?;
compile_one(debug, &obj.path, &dest_path, clang, &clang_args)
compile_one(&obj.path, &dest_path, clang, &clang_args)
})
.collect::<Result<_, _>>()
}
Expand All @@ -244,19 +241,17 @@ fn extract_clang_or_default(clang: Option<&PathBuf>) -> PathBuf {
}
}

#[allow(clippy::too_many_arguments)]
pub fn build(
debug: bool,
manifest_path: Option<&PathBuf>,
clang: Option<&PathBuf>,
clang_args: Vec<OsString>,
) -> Result<()> {
let (target_dir, to_compile) = metadata::get(debug, manifest_path)?;
let (target_dir, to_compile) = metadata::get(manifest_path)?;

if debug && !to_compile.is_empty() {
println!("Found bpf progs to compile:");
if !to_compile.is_empty() {
debug!("Found bpf progs to compile:");
for obj in &to_compile {
println!("\t{obj:?}");
debug!("\t{obj:?}");
}
} else if to_compile.is_empty() {
bail!("Did not find any bpf progs to compile");
Expand All @@ -265,15 +260,13 @@ pub fn build(
check_progs(&to_compile)?;

let clang = extract_clang_or_default(clang);
compile(debug, &to_compile, &clang, clang_args, &target_dir)
.context("Failed to compile progs")?;
compile(&to_compile, &clang, clang_args, &target_dir).context("Failed to compile progs")?;

Ok(())
}

// Only used in libbpf-cargo library
pub(crate) fn build_single(
debug: bool,
source: &Path,
out: &Path,
clang: Option<&PathBuf>,
Expand All @@ -292,5 +285,5 @@ pub(crate) fn build_single(
// BPF. See https://lkml.org/lkml/2020/2/21/1000.
clang_args.push(OsString::from("-fno-stack-protector"));

compile_one(debug, source, out, &clang, &clang_args)
compile_one(source, out, &clang, &clang_args)
}
Loading

0 comments on commit 1885224

Please sign in to comment.