Skip to content

Commit 30f8795

Browse files
committed
rex: cleanup: hide BPF_F_CURRENT_CPU, remove ProgramContextPair, add method to grab pointer from tp_ctx, fix KproveFlavor use, fix TaskStruct::get_comm use
Signed-off-by: MinhPhan8803 <[email protected]>
1 parent 70e86aa commit 30f8795

File tree

6 files changed

+50
-42
lines changed

6 files changed

+50
-42
lines changed

rex-macros/src/kprobe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl KProbe {
5353
let attached_function = if self.function.is_some() {
5454
format!("rex/{}/{}", flavor, self.function.as_ref().unwrap())
5555
} else {
56-
"rex/kprobe".to_string()
56+
format!("rex/{}", flavor)
5757
};
5858

5959
let function_body_tokens = quote! {

rex/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,3 @@ define_prog_entry!(sched_cls);
6969

7070
pub use bindings::uapi::*;
7171
pub use utils::Result;
72-
73-
pub static CURRENT_CPU: u64 = BPF_F_CURRENT_CPU;

rex/src/map.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::utils::{
22
to_result, NoRef, PerfEventMaskedCPU, Result, StreamableProgram,
33
};
4-
use crate::CURRENT_CPU;
54
use crate::{
65
base_helper::{
76
bpf_map_delete_elem,

rex/src/tracepoint/tp_impl.rs

+22-27
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ pub enum tp_ctx {
2121
SyscallsExitOpen(&'static SyscallsExitOpenArgs),
2222
}
2323

24+
impl tp_ctx {
25+
unsafe fn get_ptr(&self) -> *const () {
26+
match self {
27+
tp_ctx::Void => null(),
28+
tp_ctx::SyscallsEnterOpen(args) => {
29+
*args as *const SyscallsEnterOpenArgs as *const ()
30+
}
31+
tp_ctx::SyscallsExitOpen(args) => {
32+
*args as *const SyscallsExitOpenArgs as *const ()
33+
}
34+
}
35+
}
36+
}
37+
2438
/// First 3 fields should always be rtti, prog_fn, and name
2539
///
2640
/// rtti should be u64, therefore after compiling the
@@ -88,34 +102,15 @@ impl StreamableProgram for tracepoint {
88102
cpu: PerfEventMaskedCPU,
89103
) -> Result {
90104
let map_kptr = unsafe { core::ptr::read_volatile(&map.kptr) };
105+
let ctx_ptr = unsafe { ctx.get_ptr() };
91106
termination_check!(unsafe {
92-
to_result!(match ctx {
93-
tp_ctx::Void => stub::bpf_perf_event_output_tp(
94-
null(),
95-
map_kptr,
96-
cpu.masked_cpu,
97-
data as *const T as *const (),
98-
mem::size_of::<T>() as u64,
99-
),
100-
tp_ctx::SyscallsEnterOpen(args) => {
101-
stub::bpf_perf_event_output_tp(
102-
*args as *const SyscallsEnterOpenArgs as *const (),
103-
map_kptr,
104-
cpu.masked_cpu,
105-
data as *const T as *const (),
106-
mem::size_of::<T>() as u64,
107-
)
108-
}
109-
tp_ctx::SyscallsExitOpen(args) => {
110-
stub::bpf_perf_event_output_tp(
111-
*args as *const SyscallsExitOpenArgs as *const (),
112-
map_kptr,
113-
cpu.masked_cpu,
114-
data as *const T as *const (),
115-
mem::size_of::<T>() as u64,
116-
)
117-
}
118-
})
107+
to_result!(stub::bpf_perf_event_output_tp(
108+
ctx_ptr,
109+
map_kptr,
110+
cpu.masked_cpu,
111+
data as *const T as *const (),
112+
mem::size_of::<T>() as u64,
113+
))
119114
})
120115
}
121116
}

rex/src/utils.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::bindings::uapi::linux::bpf::BPF_F_INDEX_MASK;
1+
use crate::bindings::uapi::linux::bpf::{BPF_F_CURRENT_CPU, BPF_F_INDEX_MASK};
2+
use crate::bindings::uapi::linux::errno::EINVAL;
3+
use crate::map::RexPerfEventArray;
24
use crate::tracepoint::{tp_ctx, tracepoint};
3-
use crate::{map::RexPerfEventArray, CURRENT_CPU};
4-
use core::ffi::{c_int, c_uchar};
5+
use core::ffi::{c_int, c_uchar, CStr};
56
use core::mem;
67
use core::ops::{Deref, DerefMut, Drop};
78

@@ -262,10 +263,6 @@ where
262263
}
263264
}
264265

265-
pub enum ProgramContextPair {
266-
Tracepoint(),
267-
}
268-
269266
/// programs that can stream data through a
270267
/// RexPerfEventArray will implement this trait
271268
pub trait StreamableProgram {
@@ -289,7 +286,7 @@ pub struct PerfEventMaskedCPU {
289286
impl PerfEventMaskedCPU {
290287
pub fn current_cpu() -> Self {
291288
PerfEventMaskedCPU {
292-
masked_cpu: CURRENT_CPU,
289+
masked_cpu: BPF_F_CURRENT_CPU,
293290
}
294291
}
295292
pub fn any_cpu(cpu: u64) -> Self {
@@ -298,3 +295,20 @@ impl PerfEventMaskedCPU {
298295
}
299296
}
300297
}
298+
299+
pub fn copy_cstr_to_array<const N: usize>(
300+
src: &CStr,
301+
dst: &mut [u8; N],
302+
) -> Result {
303+
if N == 0 {
304+
return Err(-(EINVAL as i32));
305+
}
306+
let src_bytes = src.to_bytes_with_nul();
307+
let size = core::cmp::min::<usize>(N, src_bytes.len()) - 1;
308+
if size == 0 {
309+
return Err(-(EINVAL as i32));
310+
}
311+
dst[..size].copy_from_slice(&src_bytes[..size]);
312+
dst[size] = 0;
313+
Ok(0)
314+
}

samples/trace_event/src/main.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ use rex::linux::bpf::*;
88
use rex::linux::perf_event::PERF_MAX_STACK_DEPTH;
99
use rex::map::*;
1010
use rex::perf_event::*;
11-
use rex::{Result, rex_map, rex_perf_event};
11+
use rex::utils::copy_cstr_to_array;
12+
use rex::{rex_map, rex_perf_event, Result};
1213

1314
pub const TASK_COMM_LEN: usize = 16;
1415

1516
// What if user does not use repr(C)?
1617
#[repr(C)]
1718
#[derive(Copy, Clone)]
1819
pub struct KeyT {
19-
pub comm: [i8; TASK_COMM_LEN],
20+
pub comm: [u8; TASK_COMM_LEN],
2021
pub kernstack: u32,
2122
pub userstack: u32,
2223
}
@@ -51,7 +52,8 @@ fn rex_prog1(obj: &perf_event, ctx: &bpf_perf_event_data) -> Result {
5152

5253
obj.bpf_get_current_task()
5354
.map(|t| {
54-
t.get_comm(&mut key.comm);
55+
let prog_name = t.get_comm().unwrap_or_default();
56+
copy_cstr_to_array(prog_name, &mut key.comm);
5557
0u64
5658
})
5759
.ok_or(0i32)?;

0 commit comments

Comments
 (0)