Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tools: add a new CLI in rust and related CI tests #12

Merged
merged 5 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .github/workflows/test-cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Test whether CLI works properly

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build-runtime-cli-and-test-cli:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
with:
submodules: 'recursive'
- name: Install dependencies
run: |
sudo apt-get install binutils-dev libboost1.74-all-dev libelf-dev zlib1g-dev
- name: Build and install runtime
run: |
make install
- name: Build and install CLI
run: |
cd tools/cli-rs
RUSTFLAGS="-C target-feature=+crt-static" cargo build --release --target x86_64-unknown-linux-gnu
cp ./target/x86_64-unknown-linux-gnu/release/bpftime ~/.bpftime
- uses: actions/upload-artifact@v3
with:
name: bpftime-cli
path: ~/.bpftime/bpftime
- name: Build test assets
run: |
cd example/malloc
make -j
- name: Test CLI - attach by running
run: |
export PATH=$PATH:~/.bpftime
bpftime --help
cd example/malloc
timeout -s 2 15s bpftime load ./malloc || if [ $? = 124 ]; then exit 0; else exit $?; fi &
sleep 10s
ID1=$!
timeout -s 2 5s bpftime start ./test || if [ $? = 124 ]; then exit 0; else exit $?; fi
fg $ID1 || true
7 changes: 6 additions & 1 deletion tools/cli-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ anyhow = "1.0.75"
clap = { version = "4.4.5", features = ["derive"] }
dirs = "5.0.1"
exec = "0.3.1"
libbpf-rs = { version = "0.21.2" }
libbpf-rs = { version = "0.21.2", optional = true }

[build-dependencies]
bindgen = "0.68.1"

[features]
default = []
support-load-bpf = ["dep:libbpf-rs"]
all = ["support-load-bpf"]
5 changes: 4 additions & 1 deletion tools/cli-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum SubCommand {
},
#[clap(about = "Inject bpftime-agent to a certain pid")]
Attach { pid: i32 },
#[cfg(feature = "support-load-bpf")]
#[clap(about = "Load and attach an eBPF object into kernel")]
LoadBpf {
#[arg(help = "Path to the ELF file")]
Expand Down Expand Up @@ -88,6 +89,7 @@ fn inject_by_frida(pid: i32, agent: impl AsRef<Path>) -> anyhow::Result<()> {
Ok(())
}

#[cfg(feature = "support-load-bpf")]
fn load_ebpf_object_into_kernel(path: impl AsRef<Path>) -> anyhow::Result<()> {
let mut obj = libbpf_rs::ObjectBuilder::default()
.open_file(path.as_ref())
Expand Down Expand Up @@ -139,9 +141,10 @@ fn main() -> anyhow::Result<()> {
if !so_path.exists() {
bail!("Library not found: {:?}", so_path);
}
println!("Inject: {:?}",so_path);
println!("Inject: {:?}", so_path);
inject_by_frida(pid, so_path)
}
#[cfg(feature = "support-load-bpf")]
SubCommand::LoadBpf { path } => load_ebpf_object_into_kernel(PathBuf::from(path))
.with_context(|| anyhow!("Failed to load ebpf object into kernel")),
}
Expand Down
Loading