main.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #![no_std]
  2. #![no_main]
  3. use aya_ebpf::{
  4. helpers::bpf_ktime_get_ns,
  5. macros::{kprobe, map},
  6. maps::HashMap,
  7. programs::ProbeContext,
  8. };
  9. use aya_log_ebpf::info;
  10. #[kprobe]
  11. pub fn syscall_ebpf(ctx: ProbeContext) -> u32 {
  12. try_syscall_ebpf(ctx).unwrap_or_else(|ret| ret)
  13. }
  14. fn try_syscall_ebpf(ctx: ProbeContext) -> Result<u32, u32> {
  15. let pt_regs = unsafe { &*ctx.regs };
  16. // first arg -> rdi
  17. // second arg -> rsi
  18. // third arg -> rdx
  19. // four arg -> rcx
  20. let syscall_num = pt_regs.rsi as usize;
  21. if syscall_num != 1 {
  22. unsafe {
  23. if let Some(v) = SYSCALL_LIST.get(&(syscall_num as u32)) {
  24. let new_v = *v + 1;
  25. SYSCALL_LIST
  26. .insert(&(syscall_num as u32), &new_v, 0)
  27. .unwrap();
  28. } else {
  29. SYSCALL_LIST.insert(&(syscall_num as u32), &1, 0).unwrap();
  30. }
  31. }
  32. let time = unsafe { bpf_ktime_get_ns() };
  33. info!(&ctx, "[{}] invoke syscall {}", time, syscall_num);
  34. }
  35. Ok(0)
  36. }
  37. #[map]
  38. static SYSCALL_LIST: HashMap<u32, u32> = HashMap::<u32, u32>::with_max_entries(1024, 0);
  39. #[cfg(not(test))]
  40. #[panic_handler]
  41. fn panic(_info: &core::panic::PanicInfo) -> ! {
  42. // we need use this because the verifier will forbid loop
  43. unsafe { core::hint::unreachable_unchecked() }
  44. // loop{}
  45. }