log.rs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #![no_std]
  2. #![no_main]
  3. use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
  4. use aya_ebpf::{macros::uprobe, programs::ProbeContext};
  5. use aya_log_ebpf::{debug, error, info, trace, warn};
  6. #[uprobe]
  7. pub fn test_log(ctx: ProbeContext) {
  8. debug!(&ctx, "Hello from eBPF!");
  9. error!(
  10. &ctx,
  11. "{}, {}, {}, {:x}",
  12. 69,
  13. 420i32,
  14. "wao",
  15. "wao".as_bytes()
  16. );
  17. // 10.0.0.1
  18. let ipv4 = Ipv4Addr::new(10, 0, 0, 1);
  19. // 2001:db8::1
  20. let ipv6 = Ipv6Addr::new(8193, 3512, 0, 0, 0, 0, 0, 1);
  21. info!(
  22. &ctx,
  23. "ip structs, without format hint: ipv4: {}, ipv6: {}", ipv4, ipv6
  24. );
  25. info!(
  26. &ctx,
  27. "ip structs, with format hint: ipv4: {:i}, ipv6: {:i}", ipv4, ipv6
  28. );
  29. let ipv4_enum = IpAddr::V4(ipv4);
  30. let ipv6_enum = IpAddr::V6(ipv6);
  31. info!(
  32. &ctx,
  33. "ip enums, without format hint: ipv4: {}, ipv6: {}", ipv4_enum, ipv6_enum
  34. );
  35. info!(
  36. &ctx,
  37. "ip enums, with format hint: ipv4: {:i}, ipv6: {:i}", ipv4_enum, ipv6_enum
  38. );
  39. // We don't format `Ipv6Addr::to_bits`, because `u128` is not supported by
  40. // eBPF. Even though Rust compiler does not complain, verifier would throw
  41. // an error about returning values not fitting into 64-bit registers.
  42. info!(&ctx, "ip as bits: ipv4: {:i}", ipv4.to_bits());
  43. info!(
  44. &ctx,
  45. "ip as octets: ipv4: {:i}, ipv6: {:i}",
  46. ipv4.octets(),
  47. ipv6.octets()
  48. );
  49. let mac = [4u8, 32u8, 6u8, 9u8, 0u8, 64u8];
  50. trace!(&ctx, "mac lc: {:mac}, mac uc: {:MAC}", mac, mac);
  51. let hex = 0x2f;
  52. warn!(&ctx, "hex lc: {:x}, hex uc: {:X}", hex, hex);
  53. let hex = [0xde, 0xad, 0xbe, 0xef].as_slice();
  54. debug!(&ctx, "hex lc: {:x}, hex uc: {:X}", hex, hex);
  55. let len = 42;
  56. let size = 43;
  57. let slice = 44;
  58. let record = 45;
  59. debug!(&ctx, "{} {} {} {}", len, size, slice, record);
  60. // Testing compilation only.
  61. if false {
  62. struct NoCopy {}
  63. impl NoCopy {
  64. fn consume(self) -> u64 {
  65. 0xdeadbeef
  66. }
  67. }
  68. let no_copy = NoCopy {};
  69. debug!(&ctx, "{:x}", no_copy.consume());
  70. }
  71. }
  72. #[cfg(not(test))]
  73. #[panic_handler]
  74. fn panic(_info: &core::panic::PanicInfo) -> ! {
  75. loop {}
  76. }