From 188522437ac9012764df77f40e554f6e7ecb4c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20M=C3=BCller?= Date: Wed, 15 Jan 2025 16:29:10 -0800 Subject: [PATCH] libbpf-cargo: Add proper logging infrastructure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- Cargo.lock | 193 +++++++++++++++++++++++++++++++++++ libbpf-cargo/CHANGELOG.md | 2 + libbpf-cargo/Cargo.toml | 3 + libbpf-cargo/src/build.rs | 25 ++--- libbpf-cargo/src/gen/mod.rs | 21 ++-- libbpf-cargo/src/lib.rs | 1 - libbpf-cargo/src/main.rs | 32 +++--- libbpf-cargo/src/make.rs | 6 +- libbpf-cargo/src/metadata.rs | 29 ++---- libbpf-cargo/src/test.rs | 54 ++++------ 10 files changed, 264 insertions(+), 102 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f26ec1a0..c9765aac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "0.6.11" @@ -220,6 +229,28 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "env_filter" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0" +dependencies = [ + "log", +] + +[[package]] +name = "env_logger" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcaee3d8e3cfc3fd92428d477bc97fc29ec8716d180c0d74c643bb26166660e0" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "errno" version = "0.3.8" @@ -264,6 +295,12 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "iced-x86" version = "1.21.0" @@ -292,12 +329,15 @@ dependencies = [ "anyhow", "cargo_metadata", "clap", + "env_logger", "goblin", "libbpf-rs", + "log", "memmap2", "serde", "serde_json", "tempfile", + "test-log", "vmlinux", ] @@ -371,6 +411,15 @@ version = "0.4.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "memchr" version = "2.6.3" @@ -525,6 +574,12 @@ dependencies = [ "siphasher", ] +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + [[package]] name = "pkg-config" version = "0.3.30" @@ -591,6 +646,50 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata 0.4.9", + "regex-syntax 0.8.5", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax 0.8.5", +] + +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "runqslower" version = "0.1.0" @@ -729,6 +828,15 @@ dependencies = [ "syn", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "siphasher" version = "1.0.1" @@ -809,6 +917,28 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "test-log" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3dffced63c2b5c7be278154d76b479f9f9920ed34e7574201407f0b14e2bbb93" +dependencies = [ + "env_logger", + "test-log-macros", + "tracing-subscriber", +] + +[[package]] +name = "test-log-macros" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5999e24eaa32083191ba4e425deb75cdf25efefabe5aaccb7446dd0d4122a3f5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "test-tag" version = "0.1.4" @@ -840,6 +970,16 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "time" version = "0.3.37" @@ -886,6 +1026,53 @@ dependencies = [ "vmlinux", ] +[[package]] +name = "tracing" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008" +dependencies = [ + "matchers", + "once_cell", + "regex", + "sharded-slab", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + [[package]] name = "unicode-ident" version = "1.0.5" @@ -898,6 +1085,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "vmlinux" version = "0.0.0" diff --git a/libbpf-cargo/CHANGELOG.md b/libbpf-cargo/CHANGELOG.md index b67fdc45..0a5dfc4a 100644 --- a/libbpf-cargo/CHANGELOG.md +++ b/libbpf-cargo/CHANGELOG.md @@ -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 diff --git a/libbpf-cargo/Cargo.toml b/libbpf-cargo/Cargo.toml index 454e3211..86a2bf04 100644 --- a/libbpf-cargo/Cargo.toml +++ b/libbpf-cargo/Cargo.toml @@ -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" @@ -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" } diff --git a/libbpf-cargo/src/build.rs b/libbpf-cargo/src/build.rs index 16bb5acc..b439c4a4 100644 --- a/libbpf-cargo/src/build.rs +++ b/libbpf-cargo/src/build.rs @@ -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; @@ -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 { - if debug { - println!("Building {}", source.display()); - } + debug!("Building {}", source.display()); let mut cmd = Command::new(clang.as_os_str()); cmd.args(clang_args); @@ -204,7 +202,6 @@ fn compile_one( } fn compile( - debug: bool, objs: &[UnprocessedObj], clang: &Path, mut clang_args: Vec, @@ -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::>() } @@ -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, ) -> 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"); @@ -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>, @@ -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) } diff --git a/libbpf-cargo/src/gen/mod.rs b/libbpf-cargo/src/gen/mod.rs index baf80465..e68c4e5e 100644 --- a/libbpf-cargo/src/gen/mod.rs +++ b/libbpf-cargo/src/gen/mod.rs @@ -38,6 +38,8 @@ use libbpf_rs::MapType; use libbpf_rs::Object; use libbpf_rs::Program; +use log::debug; +use log::info; use memmap2::Mmap; use crate::metadata; @@ -289,7 +291,7 @@ pub(crate) fn canonicalize_internal_map_name(s: &str) -> Option, - rustfmt_path: Option<&PathBuf>, -) -> Result<()> { - let (_target_dir, to_gen) = metadata::get(debug, manifest_path)?; - if debug && !to_gen.is_empty() { - println!("Found bpf objs to gen skel:"); +fn gen_project(manifest_path: Option<&PathBuf>, rustfmt_path: Option<&PathBuf>) -> Result<()> { + let (_target_dir, to_gen) = metadata::get(manifest_path)?; + if !to_gen.is_empty() { + debug!("Found bpf objs to gen skel:"); for obj in &to_gen { - println!("\t{obj:?}"); + debug!("\t{obj:?}"); } } else if to_gen.is_empty() { bail!("Did not find any bpf objects to generate skeleton"); @@ -1348,7 +1346,6 @@ fn gen_project( } pub fn gen( - debug: bool, manifest_path: Option<&PathBuf>, rustfmt_path: Option<&PathBuf>, object: Option<&PathBuf>, @@ -1360,6 +1357,6 @@ pub fn gen( if let Some(obj_file) = object { gen_single(obj_file, OutputDest::Stdout, rustfmt_path) } else { - gen_project(debug, manifest_path, rustfmt_path) + gen_project(manifest_path, rustfmt_path) } } diff --git a/libbpf-cargo/src/lib.rs b/libbpf-cargo/src/lib.rs index 80340299..81068c45 100644 --- a/libbpf-cargo/src/lib.rs +++ b/libbpf-cargo/src/lib.rs @@ -241,7 +241,6 @@ impl SkeletonBuilder { } build::build_single( - self.debug, source, // Unwrap is safe here since we guarantee that obj.is_some() above self.obj.as_ref().unwrap(), diff --git a/libbpf-cargo/src/main.rs b/libbpf-cargo/src/main.rs index 4c233ca2..e18c6627 100644 --- a/libbpf-cargo/src/main.rs +++ b/libbpf-cargo/src/main.rs @@ -4,7 +4,9 @@ use std::ffi::OsString; use std::path::PathBuf; +use anyhow::Context as _; use anyhow::Result; +use clap::ArgAction; use clap::Args; use clap::Parser; use clap::Subcommand; @@ -12,6 +14,7 @@ use clap::Subcommand; use libbpf_cargo::__private::build; use libbpf_cargo::__private::gen; use libbpf_cargo::__private::make; +use log::Level; #[doc(hidden)] @@ -21,9 +24,9 @@ use libbpf_cargo::__private::make; struct Opt { #[command(subcommand)] wrapper: Wrapper, - /// Enable debug output. - #[clap(short, long, global = true)] - debug: bool, + /// Increase verbosity (can be supplied multiple times). + #[clap(short = 'v', long = "verbose", global = true, action = ArgAction::Count)] + verbosity: u8, } // cargo invokes subcommands with the first argument as @@ -103,7 +106,19 @@ enum Command { #[doc(hidden)] fn main() -> Result<()> { let opts = Opt::parse(); - let Opt { wrapper, debug } = opts; + let Opt { wrapper, verbosity } = opts; + + let level = match verbosity { + 0 => Level::Warn, + 1 => Level::Info, + 2 => Level::Debug, + _ => Level::Trace, + }; + + let () = env_logger::builder() + .parse_env(env_logger::Env::default().default_filter_or(level.as_str())) + .try_init() + .context("failed to initialize logging infrastructure")?; match wrapper { Wrapper::Libbpf(cmd) => match cmd { @@ -114,18 +129,12 @@ fn main() -> Result<()> { clang_path, clang_args, }, - } => build::build( - debug, - manifest_path.as_ref(), - clang_path.as_ref(), - clang_args, - ), + } => build::build(manifest_path.as_ref(), clang_path.as_ref(), clang_args), Command::Gen { manifest_path, rustfmt_path, object, } => gen::gen( - debug, manifest_path.as_ref(), rustfmt_path.as_ref(), object.as_ref(), @@ -141,7 +150,6 @@ fn main() -> Result<()> { cargo_build_args, rustfmt_path, } => make::make( - debug, manifest_path.as_ref(), clang_path.as_ref(), clang_args, diff --git a/libbpf-cargo/src/make.rs b/libbpf-cargo/src/make.rs index 0db41b0e..f97d8c65 100644 --- a/libbpf-cargo/src/make.rs +++ b/libbpf-cargo/src/make.rs @@ -11,7 +11,6 @@ use crate::gen; #[allow(clippy::too_many_arguments)] pub fn make( - debug: bool, manifest_path: Option<&PathBuf>, clang: Option<&PathBuf>, clang_args: Vec, @@ -22,13 +21,12 @@ pub fn make( if !quiet { println!("Compiling BPF objects"); } - build::build(debug, manifest_path, clang, clang_args) - .context("Failed to compile BPF objects")?; + build::build(manifest_path, clang, clang_args).context("Failed to compile BPF objects")?; if !quiet { println!("Generating skeletons"); } - gen::gen(debug, manifest_path, None, rustfmt_path).context("Failed to generate skeletons")?; + gen::gen(manifest_path, None, rustfmt_path).context("Failed to generate skeletons")?; let mut cmd = Command::new("cargo"); cmd.arg("build"); diff --git a/libbpf-cargo/src/metadata.rs b/libbpf-cargo/src/metadata.rs index 27e1b52d..23ff6297 100644 --- a/libbpf-cargo/src/metadata.rs +++ b/libbpf-cargo/src/metadata.rs @@ -7,6 +7,7 @@ use anyhow::Context as _; use anyhow::Result; use cargo_metadata::MetadataCommand; use cargo_metadata::Package; +use log::debug; use serde::Deserialize; use serde_json::value::Value; @@ -35,15 +36,9 @@ pub(crate) struct UnprocessedObj { pub name: String, } -fn get_package( - debug: bool, - package: &Package, - workspace_target_dir: &Path, -) -> Result> { - if debug { - println!("Metadata for package={}", package.name); - println!("\t{}", package.metadata); - } +fn get_package(package: &Package, workspace_target_dir: &Path) -> Result> { + debug!("Metadata for package={}", package.name); + debug!("\t{}", package.metadata); let package_metadata = if package.metadata != Value::Null { let PackageMetadata { libbpf } = serde_json::from_value(package.metadata.clone())?; @@ -57,9 +52,7 @@ fn get_package( // Remove "Cargo.toml" package_root.pop(); if let Some(d) = package_metadata.prog_dir { - if debug { - println!("Custom prog_dir={}", d.to_string_lossy()); - } + debug!("Custom prog_dir={}", d.to_string_lossy()); // Add requested path package_root.push(d); } else { @@ -70,10 +63,7 @@ fn get_package( // Respect custom target directories specified by package let mut target_dir = workspace_target_dir.to_path_buf(); if let Some(d) = package_metadata.target_dir { - if debug { - println!("Custom target_dir={}", d.to_string_lossy()); - } - + debug!("Custom target_dir={}", d.to_string_lossy()); // Add requested path target_dir.push(d); } else { @@ -142,10 +132,7 @@ fn get_package( } /// Returns the `target_directory` and a list of objects to compile. -pub(crate) fn get( - debug: bool, - manifest_path: Option<&PathBuf>, -) -> Result<(PathBuf, Vec)> { +pub(crate) fn get(manifest_path: Option<&PathBuf>) -> Result<(PathBuf, Vec)> { let mut cmd = MetadataCommand::new(); if let Some(path) = manifest_path { @@ -162,7 +149,7 @@ pub(crate) fn get( for id in &metadata.workspace_members { for package in &metadata.packages { if id == &package.id { - let vv = &mut get_package(debug, package, &target_directory) + let vv = &mut get_package(package, &target_directory) .with_context(|| format!("Failed to process package={}", package.name))?; let () = v.append(vv); } diff --git a/libbpf-cargo/src/test.rs b/libbpf-cargo/src/test.rs index 353f5d26..7d3eb8b9 100644 --- a/libbpf-cargo/src/test.rs +++ b/libbpf-cargo/src/test.rs @@ -18,6 +18,7 @@ use memmap2::Mmap; use tempfile::tempdir; use tempfile::NamedTempFile; use tempfile::TempDir; +use test_log::test; use crate::build::build; use crate::gen::GenBtf; @@ -139,17 +140,17 @@ fn test_build_default() { let (_dir, proj_dir, cargo_toml) = setup_temp_project(); // No bpf progs yet - build(true, Some(&cargo_toml), None, Vec::new()).unwrap_err(); + build(Some(&cargo_toml), None, Vec::new()).unwrap_err(); // Add prog dir create_dir(proj_dir.join("src/bpf")).expect("failed to create prog dir"); - build(true, Some(&cargo_toml), None, Vec::new()).unwrap_err(); + build(Some(&cargo_toml), None, Vec::new()).unwrap_err(); // Add a prog let _prog_file = File::create(proj_dir.join("src/bpf/prog.bpf.c")).expect("failed to create prog file"); - build(true, Some(&cargo_toml), None, Vec::new()).unwrap(); + build(Some(&cargo_toml), None, Vec::new()).unwrap(); // Validate generated object file validate_bpf_o(proj_dir.as_path().join("target/bpf/prog.bpf.o").as_path()); @@ -167,7 +168,7 @@ fn test_build_invalid_prog() { File::create(proj_dir.join("src/bpf/prog.bpf.c")).expect("failed to create prog file"); writeln!(prog_file, "1").expect("write to prog file failed"); - build(true, Some(&cargo_toml), None, Vec::new()).unwrap_err(); + build(Some(&cargo_toml), None, Vec::new()).unwrap_err(); } #[test] @@ -186,14 +187,14 @@ fn test_build_custom() { .expect("write to Cargo.toml failed"); // No bpf progs yet - build(true, Some(&cargo_toml), None, Vec::new()).unwrap_err(); + build(Some(&cargo_toml), None, Vec::new()).unwrap_err(); // Add a prog create_dir(proj_dir.join("src/other_bpf_dir")).expect("failed to create prog dir"); let _prog_file = File::create(proj_dir.join("src/other_bpf_dir/prog.bpf.c")) .expect("failed to create prog file"); - build(true, Some(&cargo_toml), None, Vec::new()).unwrap(); + build(Some(&cargo_toml), None, Vec::new()).unwrap(); // Validate generated object file validate_bpf_o( @@ -223,13 +224,13 @@ fn test_unknown_metadata_section() { // Add prog dir create_dir(proj_dir.join("src/bpf")).expect("failed to create prog dir"); - build(true, Some(&cargo_toml), None, Vec::new()).unwrap_err(); + build(Some(&cargo_toml), None, Vec::new()).unwrap_err(); // Add a prog let _prog_file = File::create(proj_dir.join("src/bpf/prog.bpf.c")).expect("failed to create prog file"); - build(true, Some(&cargo_toml), None, Vec::new()).unwrap(); + build(Some(&cargo_toml), None, Vec::new()).unwrap(); // Validate generated object file validate_bpf_o(proj_dir.as_path().join("target/bpf/prog.bpf.o").as_path()); @@ -241,15 +242,15 @@ fn test_enforce_file_extension() { // Add prog dir create_dir(proj_dir.join("src/bpf")).expect("failed to create prog dir"); - build(true, Some(&cargo_toml), None, Vec::new()).unwrap_err(); + build(Some(&cargo_toml), None, Vec::new()).unwrap_err(); let _prog_file = File::create(proj_dir.join("src/bpf/prog_BAD_EXTENSION.c")) .expect("failed to create prog file"); - build(true, Some(&cargo_toml), None, Vec::new()).unwrap_err(); + build(Some(&cargo_toml), None, Vec::new()).unwrap_err(); let _prog_file_again = File::create(proj_dir.join("src/bpf/prog_GOOD_EXTENSION.bpf.c")) .expect("failed to create prog file"); - build(true, Some(&cargo_toml), None, Vec::new()).unwrap(); + build(Some(&cargo_toml), None, Vec::new()).unwrap(); } #[test] @@ -257,7 +258,7 @@ fn test_build_workspace() { let (_dir, _, workspace_cargo_toml, proj_one_dir, proj_two_dir) = setup_temp_workspace(); // No bpf progs yet - build(true, Some(&workspace_cargo_toml), None, Vec::new()).unwrap_err(); + build(Some(&workspace_cargo_toml), None, Vec::new()).unwrap_err(); // Create bpf prog for project one create_dir(proj_one_dir.join("src/bpf")).expect("failed to create prog dir"); @@ -269,7 +270,7 @@ fn test_build_workspace() { let _prog_file_2 = File::create(proj_two_dir.join("src/bpf/prog2.bpf.c")) .expect("failed to create prog file 2"); - build(true, Some(&workspace_cargo_toml), None, Vec::new()).unwrap(); + build(Some(&workspace_cargo_toml), None, Vec::new()).unwrap(); } #[test] @@ -286,7 +287,7 @@ fn test_build_workspace_collision() { let _prog_file_2 = File::create(proj_two_dir.join("src/bpf/prog.bpf.c")) .expect("failed to create prog file 2"); - build(true, Some(&workspace_cargo_toml), None, Vec::new()).unwrap_err(); + build(Some(&workspace_cargo_toml), None, Vec::new()).unwrap_err(); } #[test] @@ -300,16 +301,7 @@ fn test_make_basic() { let _prog_file = File::create(proj_dir.join("src/bpf/prog.bpf.c")).expect("failed to create prog file"); - make( - true, - Some(&cargo_toml), - None, - Vec::new(), - true, - Vec::new(), - None, - ) - .unwrap(); + make(Some(&cargo_toml), None, Vec::new(), true, Vec::new(), None).unwrap(); // Validate generated object file validate_bpf_o(proj_dir.as_path().join("target/bpf/prog.bpf.o").as_path()); @@ -338,7 +330,6 @@ fn test_make_workspace() { .expect("failed to create prog file 2"); make( - true, Some(&workspace_cargo_toml), None, Vec::new(), @@ -394,16 +385,7 @@ fn build_rust_project_from_bpf_c_impl(bpf_c: &str, rust: &str, run: bool) { // Lay down the necessary header files add_vmlinux_header(&proj_dir); - make( - true, - Some(&cargo_toml), - None, - Vec::new(), - true, - Vec::new(), - None, - ) - .unwrap(); + make(Some(&cargo_toml), None, Vec::new(), true, Vec::new(), None).unwrap(); let mut cargo = OpenOptions::new() .append(true) @@ -1217,7 +1199,7 @@ fn build_btf_mmap(prog_text: &str) -> Mmap { add_vmlinux_header(&proj_dir); // Build the .bpf.o - build(true, Some(&cargo_toml), None, Vec::new()).expect("failed to compile"); + build(Some(&cargo_toml), None, Vec::new()).expect("failed to compile"); let obj = OpenOptions::new() .read(true)