Skip to content

Commit 81d75ef

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 <nhatminhvinh8803@gmail.com>
1 parent 70e86aa commit 81d75ef

File tree

6 files changed

+34
-41
lines changed

6 files changed

+34
-41
lines changed

rex-macros/src/kprobe.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 22 additions & 27 deletions
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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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::map::RexPerfEventArray;
23
use crate::tracepoint::{tp_ctx, tracepoint};
3-
use crate::{map::RexPerfEventArray, CURRENT_CPU};
44
use core::ffi::{c_int, c_uchar};
55
use core::mem;
66
use core::ops::{Deref, DerefMut, Drop};
@@ -262,10 +262,6 @@ where
262262
}
263263
}
264264

265-
pub enum ProgramContextPair {
266-
Tracepoint(),
267-
}
268-
269265
/// programs that can stream data through a
270266
/// RexPerfEventArray will implement this trait
271267
pub trait StreamableProgram {
@@ -289,7 +285,7 @@ pub struct PerfEventMaskedCPU {
289285
impl PerfEventMaskedCPU {
290286
pub fn current_cpu() -> Self {
291287
PerfEventMaskedCPU {
292-
masked_cpu: CURRENT_CPU,
288+
masked_cpu: BPF_F_CURRENT_CPU,
293289
}
294290
}
295291
pub fn any_cpu(cpu: u64) -> Self {

samples/trace_event/src/main.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ 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::{rex_map, rex_perf_event, Result};
1212

1313
pub const TASK_COMM_LEN: usize = 16;
1414

1515
// What if user does not use repr(C)?
1616
#[repr(C)]
1717
#[derive(Copy, Clone)]
1818
pub struct KeyT {
19-
pub comm: [i8; TASK_COMM_LEN],
19+
pub comm: [u8; TASK_COMM_LEN],
2020
pub kernstack: u32,
2121
pub userstack: u32,
2222
}
@@ -51,7 +51,12 @@ fn rex_prog1(obj: &perf_event, ctx: &bpf_perf_event_data) -> Result {
5151

5252
obj.bpf_get_current_task()
5353
.map(|t| {
54-
t.get_comm(&mut key.comm);
54+
let prog_name =
55+
t.get_comm().unwrap_or_default().to_bytes_with_nul();
56+
let size =
57+
core::cmp::min::<usize>(TASK_COMM_LEN, prog_name.len()) - 1;
58+
key.comm[..size].copy_from_slice(&prog_name[..size]);
59+
key.comm[size] = 0;
5560
0u64
5661
})
5762
.ok_or(0i32)?;

0 commit comments

Comments
 (0)