Skip to content

Rust syscalls handled #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: rust
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ clap = "4.5.39"
libbpf-rs = { version = "0.24.8", features = ["generate-test-files", "libbpf-sys", "vendored"] }
libbpf-sys = "1.5.0"
libc = "0.2.172"
num_cpus = "1.17.0"
once_cell = "1.21.3"
plain = "0.2.3"

Expand Down
37 changes: 36 additions & 1 deletion src/bpf/hs_trace.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ struct {
__uint(max_entries, 1024 * 1024);
} output SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__type(key, u32);
__type(value, u32);
__uint(max_entries, 1);
} missed_events SEC(".maps");
// struct {
// __uint(type, BPF_MAP_TYPE_HASH);
// __type(key, struct unique_file_t);
Expand Down Expand Up @@ -154,7 +160,7 @@ BPF_PROG(hs_trace_sys_enter, struct pt_regs *regs, long syscall_id)
#ifdef __NR_inotify_add_watch
case __NR_inotify_add_watch:
path = (char *)PT_REGS_PARM2_CORE(regs);
event_type = SYS_ENTER0;
event_type = SYS_ENTER1;
break;
#endif
#ifdef __NR_execve
Expand Down Expand Up @@ -289,6 +295,14 @@ BPF_PROG(hs_trace_sys_enter, struct pt_regs *regs, long syscall_id)
if (event_type == SYS_ENTER0) {
if ((enter0 = bpf_ringbuf_reserve(
&output, sizeof(struct sys_enter_info0_t), 0)) == NULL) {
bpf_printk("FAILED to reserve space in ring buffer for event_type == SYS_ENTER0\n");
bpf_printk("FAILED to reserve space in ring buffer for event_type == SYS_ENTER1\n");
u32 key = 0;
u32 *missed = bpf_map_lookup_elem(&missed_events, &key);
if(missed) {
__sync_fetch_and_add(missed, 1);
}

return 0;
}
enter0->pid = pid_tgid;
Expand All @@ -298,6 +312,13 @@ BPF_PROG(hs_trace_sys_enter, struct pt_regs *regs, long syscall_id)
} else if (event_type == SYS_ENTER1) {
if ((enter1 = bpf_ringbuf_reserve(
&output, sizeof(struct sys_enter_info1_t), 0)) == NULL) {
bpf_printk("FAILED to reserve space in ring buffer for event_type == SYS_ENTER1\n");
u32 key = 0;
u32 *missed = bpf_map_lookup_elem(&missed_events, &key);
if(missed) {
__sync_fetch_and_add(missed, 1);
}

return 0;
}
enter1->pid = pid_tgid;
Expand All @@ -309,6 +330,13 @@ BPF_PROG(hs_trace_sys_enter, struct pt_regs *regs, long syscall_id)
} else if (event_type == SYS_ENTER2) {
if ((enter2 = bpf_ringbuf_reserve(
&output, sizeof(struct sys_enter_info2_t), 0)) == NULL) {
bpf_printk("FAILED to reserve space in ring buffer for event_type == SYS_ENTER1\n");
u32 key = 0;
u32 *missed = bpf_map_lookup_elem(&missed_events, &key);
if(missed) {
__sync_fetch_and_add(missed, 1);
}

return 0;
}
enter2->pid = pid_tgid;
Expand Down Expand Up @@ -512,6 +540,13 @@ BPF_PROG(hs_trace_sys_exit, struct pt_regs *regs, long ret)
struct sys_exit_info_t *exit;
if ((exit = bpf_ringbuf_reserve(&output, sizeof(struct sys_exit_info_t),
0)) == NULL) {
bpf_printk("FAILED to reserve space in ring buffer for event_type == SYS_ENTER1\n");
u32 key = 0;
u32 *missed = bpf_map_lookup_elem(&missed_events, &key);
if(missed) {
__sync_fetch_and_add(missed, 1);
}

return 0;
}
exit->pid = pid_tgid;
Expand Down
Loading
Loading