Skip to content

Commit 6590fae

Browse files
committed
rex: clean up tracepoint and map impls
Signed-off-by: MinhPhan8803 <[email protected]>
1 parent bf10a1a commit 6590fae

File tree

13 files changed

+89
-87
lines changed

13 files changed

+89
-87
lines changed

rex-macros/src/kprobe.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use std::fmt;
2+
13
use proc_macro2::TokenStream;
24
use quote::{format_ident, quote};
3-
use std::fmt;
45
use syn::{parse2, ItemFn, Result};
56

67
use crate::args::parse_string_args;

rex-macros/src/tracepoint.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ impl TracePoint {
3434
if context_type_ref
3535
.lifetime
3636
.expect_or_abort("Context reference needs to be static")
37-
.ident
38-
!= "static"
37+
.ident !=
38+
"static"
3939
{
4040
abort_call_site!("Context reference needs to be static");
4141
}
@@ -64,6 +64,7 @@ impl TracePoint {
6464
"SyscallsExitOpenCtx" => "syscalls/sys_exit_open",
6565
"SyscallsExitOpenatCtx" => "syscalls/sys_exit_openat",
6666
"SyscallsEnterDupCtx" => "syscalls/sys_enter_dup",
67+
"RawSyscallsEnterCtx" => "raw_syscalls/sys_enter",
6768
_ => abort_call_site!("Please provide a valid context type. If your needed context isn't supported consider opening a PR!"),
6869
};
6970
let attached_name = format!("rex/tracepoint/{}", hook_point_name);

rex/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ mod random32;
3131

3232
extern crate paste;
3333

34-
use crate::bindings::uapi::linux::bpf::BPF_F_CURRENT_CPU;
3534
use paste::paste;
3635
pub use rex_macros::*;
3736

rex/src/map.rs

+7-22
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,17 @@ use crate::base_helper::{
99
termination_check,
1010
};
1111
use crate::ffi;
12-
use crate::utils::{NoRef, PerfEventMaskedCPU, Result, StreamableProgram};
1312
use crate::linux::bpf::{
1413
bpf_map_type, BPF_ANY, BPF_EXIST, BPF_MAP_TYPE_ARRAY, BPF_MAP_TYPE_HASH,
15-
BPF_MAP_TYPE_PERCPU_ARRAY, BPF_MAP_TYPE_QUEUE, BPF_MAP_TYPE_RINGBUF,
16-
BPF_MAP_TYPE_STACK, BPF_MAP_TYPE_STACK_TRACE, BPF_NOEXIST,
17-
BPF_RB_AVAIL_DATA, BPF_RB_CONS_POS, BPF_RB_PROD_POS, BPF_RB_RING_SIZE}
18-
use crate::{
19-
base_helper::{
20-
bpf_map_delete_elem,
21-
bpf_map_lookup_elem,
22-
bpf_map_peek_elem,
23-
bpf_map_pop_elem,
24-
bpf_map_push_elem,
25-
bpf_map_update_elem,
26-
// bpf_ringbuf_discard, bpf_ringbuf_query, bpf_ringbuf_reserve,
27-
// bpf_ringbuf_submit,
28-
},
29-
linux::bpf::{
30-
bpf_map_type, BPF_ANY, BPF_EXIST, BPF_MAP_TYPE_ARRAY,
31-
BPF_MAP_TYPE_HASH, BPF_MAP_TYPE_PERCPU_ARRAY,
32-
BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_MAP_TYPE_QUEUE, BPF_MAP_TYPE_STACK,
33-
BPF_MAP_TYPE_STACK_TRACE, BPF_NOEXIST,
34-
},
14+
BPF_MAP_TYPE_PERCPU_ARRAY, BPF_MAP_TYPE_PERF_EVENT_ARRAY,
15+
BPF_MAP_TYPE_QUEUE, BPF_MAP_TYPE_RINGBUF, BPF_MAP_TYPE_STACK,
16+
BPF_MAP_TYPE_STACK_TRACE, BPF_NOEXIST, BPF_RB_AVAIL_DATA, BPF_RB_CONS_POS,
17+
BPF_RB_PROD_POS, BPF_RB_RING_SIZE,
3518
};
3619
use crate::linux::errno::EINVAL;
37-
use crate::utils::{to_result, NoRef, Result};
20+
use crate::utils::{
21+
to_result, NoRef, PerfEventMaskedCPU, Result, StreamableProgram,
22+
};
3823

3924
/// Rex equivalent to be used for map APIs in place of the `struct bpf_map`.
4025
/// The key and the value type are encoded as generics types `K` and `V`.

rex/src/task_struct.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::bindings::linux::kernel::task_struct;
2-
use crate::bindings::uapi::linux::errno::EINVAL;
32
use crate::per_cpu::{current_task, this_cpu_read};
43
use crate::pt_regs::PtRegs;
54
use core::ffi::{self, CStr};

rex/src/tracepoint/binding.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub struct SyscallsEnterDupCtx {
4545

4646
#[repr(C)]
4747
#[derive(Debug, Copy, Clone)]
48-
pub struct RawSyscallsEnterArgs {
48+
pub struct RawSyscallsEnterCtx {
4949
pub unused: u64,
5050
pub id: i64,
5151
pub args: [u64; 6],

rex/src/tracepoint/tp_impl.rs

+41-25
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,50 @@
1+
use crate::base_helper::termination_check;
12
use crate::bindings::uapi::linux::bpf::{
23
bpf_map_type, BPF_PROG_TYPE_TRACEPOINT,
34
};
45
use crate::ffi;
6+
use crate::map::RexPerfEventArray;
57
use crate::prog_type::rex_prog;
68
use crate::task_struct::TaskStruct;
9+
use crate::utils::{to_result, NoRef, PerfEventMaskedCPU, StreamableProgram};
710
use crate::Result;
811

12+
use super::binding::*;
13+
14+
pub enum TracepointContext {
15+
SyscallsEnterOpen(&'static SyscallsEnterOpenCtx),
16+
SyscallsEnterOpenat(&'static SyscallsEnterOpenatCtx),
17+
SyscallsExitOpen(&'static SyscallsExitOpenCtx),
18+
SyscallsExitOpenat(&'static SyscallsExitOpenatCtx),
19+
SyscallsEnterDup(&'static SyscallsEnterDupCtx),
20+
RawSyscallsEnter(&'static RawSyscallsEnterCtx),
21+
}
22+
23+
impl TracepointContext {
24+
unsafe fn get_ptr(&self) -> *const () {
25+
match self {
26+
TracepointContext::SyscallsEnterOpen(ctx) => {
27+
*ctx as *const SyscallsEnterOpenCtx as *const ()
28+
}
29+
TracepointContext::SyscallsEnterOpenat(ctx) => {
30+
*ctx as *const SyscallsEnterOpenatCtx as *const ()
31+
}
32+
TracepointContext::SyscallsExitOpen(ctx) => {
33+
*ctx as *const SyscallsExitOpenCtx as *const ()
34+
}
35+
TracepointContext::SyscallsExitOpenat(ctx) => {
36+
*ctx as *const SyscallsExitOpenatCtx as *const ()
37+
}
38+
TracepointContext::SyscallsEnterDup(ctx) => {
39+
*ctx as *const SyscallsEnterDupCtx as *const ()
40+
}
41+
TracepointContext::RawSyscallsEnter(ctx) => {
42+
*ctx as *const RawSyscallsEnterCtx as *const ()
43+
}
44+
}
45+
}
46+
}
47+
948
/// First 3 fields should always be rtti, prog_fn, and name
1049
///
1150
/// rtti should be u64, therefore after compiling the
@@ -50,30 +89,7 @@ impl rex_prog for tracepoint {
5089
}
5190

5291
impl StreamableProgram for tracepoint {
53-
type Context = tp_ctx;
54-
fn output_event<T: Copy + NoRef>(
55-
&self,
56-
ctx: &Self::Context,
57-
map: &'static RexPerfEventArray<T>,
58-
data: &T,
59-
cpu: PerfEventMaskedCPU,
60-
) -> Result {
61-
let map_kptr = unsafe { core::ptr::read_volatile(&map.kptr) };
62-
let ctx_ptr = unsafe { ctx.get_ptr() };
63-
termination_check!(unsafe {
64-
to_result!(ffi::bpf_perf_event_output_tp(
65-
ctx_ptr,
66-
map_kptr,
67-
cpu.masked_cpu,
68-
data as *const T as *const (),
69-
mem::size_of::<T>() as u64,
70-
))
71-
})
72-
}
73-
}
74-
75-
impl StreamableProgram for tracepoint {
76-
type Context = tp_ctx;
92+
type Context = TracepointContext;
7793
fn output_event<T: Copy + NoRef>(
7894
&self,
7995
ctx: &Self::Context,
@@ -89,7 +105,7 @@ impl StreamableProgram for tracepoint {
89105
map_kptr,
90106
cpu.masked_cpu,
91107
data as *const T as *const (),
92-
mem::size_of::<T>() as u64,
108+
core::mem::size_of::<T>() as u64,
93109
))
94110
})
95111
}

rex/src/utils.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::bindings::uapi::linux::bpf::{BPF_F_CURRENT_CPU, BPF_F_INDEX_MASK};
22
use crate::bindings::uapi::linux::errno::EINVAL;
33
use crate::map::RexPerfEventArray;
4-
use crate::tracepoint::{tp_ctx, tracepoint};
54
use core::ffi::{c_int, c_uchar, CStr};
65
use core::mem;
76
use core::ops::{Deref, DerefMut, Drop};

samples/harpoon/Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/harpoon/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "hello"
2+
name = "harpoon"
33
version = "0.1.0"
44
edition = "2024"
55

samples/harpoon/meson.build

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ env.set('LINUX_OBJ', kbuild_dir)
1313
env.set('LINUX_SRC', join_paths(meson.project_source_root(), './linux'))
1414
env.set('CARGO_TARGET_DIR', join_paths(build_dir, 'target'))
1515

16-
hello_clippy = custom_target(
17-
'hello-clippy',
16+
harpoon_clippy = custom_target(
17+
'harpoon-clippy',
1818
output: ['target'],
1919
command: [
2020
cargo_wrapper, rust_bin, '-Z',
@@ -27,9 +27,9 @@ hello_clippy = custom_target(
2727
build_by_default: true
2828
)
2929

30-
hello_build = custom_target(
31-
'hello-build',
32-
output: ['hello'],
30+
harpoon_build = custom_target(
31+
'harpoon-build',
32+
output: ['harpoon'],
3333
command: [
3434
cargo_wrapper, rust_bin, '-Z',
3535
'unstable-options',
@@ -43,15 +43,15 @@ hello_build = custom_target(
4343
build_by_default: true
4444
)
4545

46-
hello_loader = executable(
46+
harpoon_loader = executable(
4747
'loader',
4848
'loader.c',
4949
build_by_default: true,
5050
dependencies: [librex_dep, libbpf_dep, kernel_dep],
5151
pie: true
5252
)
5353

54-
hello_trigger = executable(
54+
harpoon_trigger = executable(
5555
'event-trigger',
5656
'event-trigger.c',
5757
build_by_default: true,
@@ -76,7 +76,7 @@ sanity_test_env.set('Q_SCRIPT',
7676
)
7777
sanity_test_env.set('KERNEL_PATH', kbuild_dir)
7878

79-
test('hello_test',
79+
test('harpoon_test',
8080
python3_bin,
8181
args: [sanity_test_scripts],
8282
env: sanity_test_env,

samples/harpoon/src/main.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33

44
extern crate rex;
55

6+
use core::ffi::CStr;
67
use rex::Result;
7-
use rex::rex_printk;
8-
use rex::{rex_tracepoint, rex_map, rex_uprobe};
9-
use rex::tracepoint::*;
108
use rex::kprobe::kprobe;
119
use rex::map::{RexArrayMap, RexHashMap, RexPerfEventArray};
1210
use rex::pt_regs::PtRegs;
11+
use rex::rex_printk;
12+
use rex::tracepoint::*;
1313
use rex::utils::PerfEventMaskedCPU;
14-
use core::ffi::CStr;
14+
use rex::{rex_map, rex_tracepoint, rex_uprobe};
1515

1616
#[repr(C)]
1717
#[derive(Clone, Copy)]
1818
struct Config {
19-
pub values: [u8, 25],
19+
pub values: [u8; 25],
2020
}
2121

2222
#[repr(C)]
@@ -29,7 +29,7 @@ struct SyscallData {
2929
#[derive(Clone, Copy)]
3030
struct Tracing {
3131
status: u32,
32-
};
32+
}
3333

3434
#[rex_map]
3535
static CONFIG_MAP: RexArrayMap<Config> = RexArrayMap::new(1, 0);
@@ -56,16 +56,16 @@ fn exit_function(obj: &kprobe, ctx: &mut PtRegs) -> Result {
5656
rex_printk!("Exit function.\n");
5757
}
5858

59-
#[rex_tracepoint(name = "raw_syscalls/sys_enter", tp_type = "RawSyscallsEnter")]
60-
fn rex_prog1(obj: &tracepoint, ctx: tp_ctx) -> Result {
61-
let mut data = SyscallData::new();
59+
#[rex_tracepoint]
60+
fn rex_prog1(obj: &tracepoint, ctx: &'static RawSyscallsEnterCtx) -> Result {
61+
let mut data = SyscallData { id: 0 };
6262
let key_config = 0;
6363
let key_trace = 0;
6464

6565
let Some(tc) = TRACING_STATUS.get_mut(&key_trace) else {
6666
rex_printk!("Error getting tracing status.\n");
6767
return Err(1);
68-
}
68+
};
6969

7070
if tc.status == 1 {
7171
rex_printk!("Tracing is not active.\n");
@@ -75,35 +75,37 @@ fn rex_prog1(obj: &tracepoint, ctx: tp_ctx) -> Result {
7575
let Some(task) = obj.bpf_get_current_task() else {
7676
rex_printk!("Unable to get current task.\n");
7777
return Err(1);
78-
}
78+
};
7979

8080
let Ok(command) = task.get_comm() else {
8181
rex_printk!("Unable to read current program name.\n");
8282
return Err(1);
83-
}
83+
};
8484

8585
let Some(input_command_raw) = CONFIG_MAP.get_mut(&key_config) else {
8686
rex_printk!("Unable to get config.\n");
8787
return Err(1);
88-
}
88+
};
8989

90-
let Ok(input_command) = CStr::from_bytes_until_nul(input_command_raw.values) else {
90+
let Ok(input_command) =
91+
CStr::from_bytes_until_nul(input_command_raw.values)
92+
else {
9193
rex_printk!("Unable to read input command.\n");
9294
return Err(1);
93-
}
95+
};
9496

9597
if command != input_command {
9698
return Err(1);
9799
}
98100

99-
let id = match ctx {
100-
RawSyscallsEnter(args) => args.id,
101-
_ => 0,
102-
};
103-
104-
data.id = id;
101+
data.id = ctx.id;
105102

106-
EVENTS.output(obj, ctx, data, PerfEventMaskedCPU::current_cpu());
103+
EVENTS.output(
104+
obj,
105+
TracepointContext::RawSyscallsEnter(ctx),
106+
data,
107+
PerfEventMaskedCPU::current_cpu(),
108+
);
107109

108110
rex_printk!("Sending syscall id {}.\n", id);
109111

samples/trace_event/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rex::linux::perf_event::PERF_MAX_STACK_DEPTH;
88
use rex::map::*;
99
use rex::perf_event::*;
1010
use rex::utils::copy_cstr_to_array;
11-
use rex::{rex_map, rex_perf_event, rex_printk, Result};
11+
use rex::{Result, rex_map, rex_perf_event, rex_printk};
1212

1313
pub const TASK_COMM_LEN: usize = 16;
1414

0 commit comments

Comments
 (0)