4
0

log.rs 2.2 KB

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