diff --git a/test.sh b/test.sh index f62ecfe..9c504d0 100755 --- a/test.sh +++ b/test.sh @@ -6,6 +6,7 @@ TEMPLATE_DIR=$1 if [ -z "${TEMPLATE_DIR}" ]; then echo "template dir required"; exit 1; fi PROG_TYPE=$2 if [ -z "${PROG_TYPE}" ]; then echo "program type required"; exit 1; fi +CRATE_NAME=aya-test-crate TMP_DIR=$(mktemp -d) clean_up() { @@ -26,7 +27,7 @@ case "${PROG_TYPE}" in ADDITIONAL_ARGS=(-d fn_name=try_to_wake_up) ;; "kprobe"|"kretprobe") - ADDITIONAL_ARGS=(-d kprobe=test) + ADDITIONAL_ARGS=(-d kprobe_fn_name=do_unlinkat) ;; "lsm") ADDITIONAL_ARGS=(-d lsm_hook=file_open) @@ -44,17 +45,17 @@ case "${PROG_TYPE}" in ADDITIONAL_ARGS=(-d tracepoint_category=net -d tracepoint_name=net_dev_queue) ;; "uprobe"|"uretprobe") - ADDITIONAL_ARGS=(-d uprobe_target=testlib -d uprobe_fn_name=testfn) + ADDITIONAL_ARGS=(-d uprobe_target=/proc/self/exe -d uprobe_fn_name=main) ;; *) ADDITIONAL_ARGS=() esac -cargo generate --path "${TEMPLATE_DIR}" -n test -d program_type="${PROG_TYPE}" "${ADDITIONAL_ARGS[@]}" -pushd test +cargo generate --path "${TEMPLATE_DIR}" -n "${CRATE_NAME}" -d program_type="${PROG_TYPE}" "${ADDITIONAL_ARGS[@]}" +pushd "${CRATE_NAME}" cargo +nightly fmt --all -- --check -cargo build --package test +cargo build --package "${CRATE_NAME}" expect << EOF set timeout 30 ;# Increase timeout if necessary @@ -79,11 +80,12 @@ expect << EOF } EOF -cargo build --package test --release -# We cannot run clippy over the whole workspace at once due to feature unification. Since both test -# and test-ebpf depend on test-common and test activates test-common's aya dependency, we end up -# trying to compile the panic handler twice: once from the bpf program, and again from std via aya. -cargo clippy --exclude test-ebpf --all-targets --workspace -- --deny warnings -cargo clippy --package test-ebpf --all-targets -- --deny warnings +cargo build --package "${CRATE_NAME}" --release +# We cannot run clippy over the whole workspace at once due to feature unification. Since both +# ${CRATE_NAME} and ${CRATE_NAME}-ebpf depend on ${CRATE_NAME}-common and ${CRATE_NAME} activates +# ${CRATE_NAME}-common's aya dependency, we end up trying to compile the panic handler twice: once +# from the bpf program, and again from std via aya. +cargo clippy --exclude "${CRATE_NAME}-ebpf" --all-targets --workspace -- --deny warnings +cargo clippy --package "${CRATE_NAME}-ebpf" --all-targets -- --deny warnings popd diff --git a/{{project-name}}-ebpf/src/main.rs b/{{project-name}}-ebpf/src/main.rs index 63be060..64cfb70 100644 --- a/{{project-name}}-ebpf/src/main.rs +++ b/{{project-name}}-ebpf/src/main.rs @@ -14,7 +14,7 @@ pub fn {{crate_name}}(ctx: ProbeContext) -> u32 { } fn try_{{crate_name}}(ctx: ProbeContext) -> Result { - info!(&ctx, "function {{kprobe}} called"); + info!(&ctx, "kprobe called"); Ok(0) } {%- when "kretprobe" %} @@ -30,7 +30,7 @@ pub fn {{crate_name}}(ctx: RetProbeContext) -> u32 { } fn try_{{crate_name}}(ctx: RetProbeContext) -> Result { - info!(&ctx, "function {{kprobe}} called"); + info!(&ctx, "kretprobe called"); Ok(0) } {%- when "fentry" %} diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index 6d64c9b..1687553 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -41,8 +41,6 @@ use aya::{ {%- when "tp_btf" -%} use aya::{programs::BtfTracePoint, Btf}; {%- when "socket_filter" -%} -use std::net::TcpStream; - use aya::programs::SocketFilter; {%- when "raw_tracepoint" -%} use aya::programs::RawTracePoint; @@ -106,7 +104,7 @@ async fn main() -> anyhow::Result<()> { use anyhow::Context as _; let program: &mut KProbe = ebpf.program_mut("{{crate_name}}").unwrap().try_into()?; program.load().context("load")?; - program.attach("{{kprobe}}", 0).context("attach")?; + program.attach("{{kprobe_fn_name}}", 0).context("attach")?; {%- when "fentry" -%} let btf = Btf::from_sys_fs().context("BTF from sysfs")?; let program: &mut FEntry = ebpf.program_mut("{{crate_name}}").unwrap().try_into()?; @@ -172,10 +170,10 @@ async fn main() -> anyhow::Result<()> { program.load("{{tracepoint_name}}", &btf)?; program.attach()?; {%- when "socket_filter" -%} - let client = TcpStream::connect("127.0.0.1:1234")?; + let listener = std::net::TcpListener::bind("localhost:0")?; let prog: &mut SocketFilter = ebpf.program_mut("{{crate_name}}").unwrap().try_into()?; prog.load()?; - prog.attach(client)?; + prog.attach(&listener)?; {%- when "cgroup_sysctl" -%} let program: &mut CgroupSysctl = ebpf.program_mut("{{crate_name}}").unwrap().try_into()?; let cgroup = std::fs::File::open(&opt.cgroup_path)