$ cargo generate https://github.com/aya-rs/aya-template ⚠️ Favorite `https://github.com/aya-rs/aya-template` not found in config, using it as a git repository: https://github.com/aya-rs/aya-template 🤷 Project Name: kprobe-rs 🔧 Destination: /home/fanrong/Computer/BPF/practice/kprobe-rs ... 🔧 project-name: kprobe-rs ... 🔧 Generating template ... ✔ 🤷 Which type of eBPF program? · kprobe 🤷 Where to attach the (k|kret)probe? (e.g try_to_wake_up): do_sys_openat2 🔧 Moving generated files into: `/home/fanrong/Computer/BPF/practice/kprobe-rs`... Initializing a fresh Git repository ✨ Done! New project created /home/fanrong/Computer/BPF/practice/kprobe-rs
// Bump the memlock rlimit. This is needed for older kernels that don't use the // new memcg based accounting, see https://lwn.net/Articles/837122/ let rlim = libc::rlimit { rlim_cur: libc::RLIM_INFINITY, rlim_max: libc::RLIM_INFINITY, }; let ret = unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &rlim) }; if ret != 0 { debug!("remove limit on locked memory failed, ret is: {}", ret); }
// This will include your eBPF object file as raw bytes at compile-time and load it at // runtime. This approach is recommended for most real-world use cases. If you would // like to specify the eBPF program at runtime rather than at compile-time, you can // reach for `Bpf::load_file` instead.
// include_bytes_aligned!()在编译时会拷贝BPF ELF目标文件的内容 // Bpf::load()读取前一个命令中BPF ELF目标文件的内容,创建maps,执行BTF重定向 #[cfg(debug_assertions)] letmut bpf = Bpf::load(include_bytes_aligned!( "../../target/bpfel-unknown-none/debug/kprobe-rs" ))?; #[cfg(not(debug_assertions))] letmut bpf = Bpf::load(include_bytes_aligned!( "../../target/bpfel-unknown-none/release/kprobe-rs" ))?; ifletErr(e) = BpfLogger::init(&mut bpf) { // This can happen if you remove all log statements from your eBPF program. warn!("failed to initialize eBPF logger: {}", e); } // 提取kprobe程序 let program: &mut KProbe = bpf.program_mut("kprobe_rs").unwrap().try_into()?; // 把它加载进内核 program.load()?; program.attach("do_sys_openat2", 0)?;
info!("Waiting for Ctrl-C..."); signal::ctrl_c().await?; info!("Exiting...");
Ok(()) }
3.编译运行
1 2 3 4 5 6 7 8 9
$ cargo xtask build-ebpf $ export RUST_LOG=INFO // 设置LOG级别为INFO $ cargo xtask run ... [2023-07-08T19:30:28Z INFO kprobe_rs] Waiting for Ctrl-C... [2023-07-08T19:30:30Z INFO kprobe_rs] function do_sys_openat2 called [2023-07-08T19:30:30Z INFO kprobe_rs] function do_sys_openat2 called [2023-07-08T19:30:30Z INFO kprobe_rs] function do_sys_openat2 called ...
$ adb push target/aarch64-unknown-linux-musl/release/kprobe-rs /data/local/tmp $ adb shell $ su # cd /data/local/tmp # RUST_LOG=INFO ./kprobe-rs [2023-07-07T04:26:28Z INFO kprobe_rs] Waiting for Ctrl-C... [2023-07-07T04:26:30Z INFO kprobe_rs] function do_sys_openat2 called [2023-07-07T04:26:30Z INFO kprobe_rs] function do_sys_openat2 called [2023-07-07T04:26:30Z INFO kprobe_rs] function do_sys_openat2 called [2023-07-07T04:26:30Z INFO kprobe_rs] function do_sys_openat2 called [2023-07-07T04:26:30Z INFO kprobe_rs] function do_sys_openat2 called [2023-07-07T04:26:30Z INFO kprobe_rs] function do_sys_openat2 called [2023-07-07T04:26:30Z INFO kprobe_rs] function do_sys_openat2 called ...