aya-log: add DST test

reviewable/pr1294/r12
Tamir Duberstein 2 months ago
parent f537dc6684
commit 8e2632921f
No known key found for this signature in database

@ -14,6 +14,20 @@ pub mod bpf_probe_read {
unsafe impl aya::Pod for TestResult {}
}
pub mod log {
pub const BUF_LEN: usize = 1024;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct Buffer {
pub buf: [u8; BUF_LEN], // 64 KiB, one more than LogValueLength::MAX.
pub len: usize,
}
#[cfg(feature = "user")]
unsafe impl aya::Pod for Buffer {}
}
pub mod raw_tracepoint {
#[repr(C)]
#[derive(Clone, Copy)]

@ -6,11 +6,19 @@ use core::{
net::{IpAddr, Ipv4Addr, Ipv6Addr},
};
use aya_ebpf::{macros::uprobe, programs::ProbeContext};
use aya_ebpf::{
macros::{map, uprobe},
maps::Array,
programs::ProbeContext,
};
use aya_log_ebpf::{debug, error, info, trace, warn};
use integration_common::log::Buffer;
#[cfg(not(test))]
extern crate ebpf_panic;
#[map]
static BUFFER: Array<Buffer> = Array::with_max_entries(1, 0);
#[uprobe]
pub fn test_log(ctx: ProbeContext) {
debug!(&ctx, "Hello from eBPF!");
@ -86,6 +94,13 @@ pub fn test_log(ctx: ProbeContext) {
// Check usage in expression position.
let () = debug!(&ctx, "{:x}", no_copy.consume());
}
let Some(Buffer { buf, len }) = BUFFER.get(0) else {
return;
};
let len = *len;
let buf = &buf[..core::cmp::min(len, buf.len())];
info!(&ctx, "variable length buffer: {:x}", buf);
}
#[uprobe]

@ -1,7 +1,8 @@
use std::{borrow::Cow, sync::Mutex};
use aya::{Ebpf, EbpfLoader, programs::UProbe};
use aya::{Ebpf, EbpfLoader, maps::Array, programs::UProbe};
use aya_log::EbpfLogger;
use integration_common::log::{BUF_LEN, Buffer};
use log::{Level, Log, Record};
#[unsafe(no_mangle)]
@ -39,6 +40,21 @@ struct CapturedLog<'a> {
fn log() {
let mut bpf = Ebpf::load(crate::LOG).unwrap();
{
let buffer = bpf.map_mut("BUFFER").unwrap();
let mut buffer: Array<_, Buffer> = Array::try_from(buffer).unwrap();
buffer
.set(
0,
Buffer {
buf: [0xff; BUF_LEN],
len: 3,
},
0,
)
.unwrap();
}
let mut captured_logs = Vec::new();
let logger = TestingLogger {
log: Mutex::new(|record: &Record| {
@ -171,6 +187,15 @@ fn log() {
})
);
assert_eq!(
records.next(),
Some(&CapturedLog {
body: "variable length buffer: ffffff".into(),
level: Level::Info,
target: "log".into(),
})
);
assert_eq!(records.next(), None);
}

Loading…
Cancel
Save