#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, __u32); __type(value, __u64); __uint(max_entries, 10240);
} syscall_counts SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, __u32); __type(value, __u32); __uint(max_entries, 65536);
} pid_syscall_map SEC(".maps");
static inline void update_syscall_maps(__u32 pid, __u32 syscall_nr) {
__u64 *count = bpf_map_lookup_elem(&syscall_counts, &pid);
if (count) {
__sync_fetch_and_add(count, 1);
} else {
__u64 initial_count = 1;
bpf_map_update_elem(&syscall_counts, &pid, &initial_count, BPF_ANY);
}
__u32 key = (pid << 16) | (syscall_nr & 0xFFFF);
__u32 *syscall_count = bpf_map_lookup_elem(&pid_syscall_map, &key);
if (syscall_count) {
__sync_fetch_and_add(syscall_count, 1);
} else {
__u32 initial_count = 1;
bpf_map_update_elem(&pid_syscall_map, &key, &initial_count, BPF_ANY);
}
}
SEC("tracepoint/syscalls/sys_enter_openat")
int trace_openat_enter(void *ctx) {
__u32 pid = bpf_get_current_pid_tgid() >> 32;
update_syscall_maps(pid, 257); return 0;
}
SEC("tracepoint/syscalls/sys_enter_read")
int trace_read_enter(void *ctx) {
__u32 pid = bpf_get_current_pid_tgid() >> 32;
update_syscall_maps(pid, 0); return 0;
}
SEC("tracepoint/syscalls/sys_enter_write")
int trace_write_enter(void *ctx) {
__u32 pid = bpf_get_current_pid_tgid() >> 32;
update_syscall_maps(pid, 1); return 0;
}
SEC("tracepoint/syscalls/sys_enter_close")
int trace_close_enter(void *ctx) {
__u32 pid = bpf_get_current_pid_tgid() >> 32;
update_syscall_maps(pid, 3); return 0;
}
SEC("tracepoint/syscalls/sys_enter_mmap")
int trace_mmap_enter(void *ctx) {
__u32 pid = bpf_get_current_pid_tgid() >> 32;
update_syscall_maps(pid, 9); return 0;
}
SEC("tracepoint/syscalls/sys_enter_socket")
int trace_socket_enter(void *ctx) {
__u32 pid = bpf_get_current_pid_tgid() >> 32;
update_syscall_maps(pid, 41); return 0;
}
SEC("tracepoint/syscalls/sys_enter_connect")
int trace_connect_enter(void *ctx) {
__u32 pid = bpf_get_current_pid_tgid() >> 32;
update_syscall_maps(pid, 42); return 0;
}
SEC("tracepoint/syscalls/sys_enter_recvfrom")
int trace_recvfrom_enter(void *ctx) {
__u32 pid = bpf_get_current_pid_tgid() >> 32;
update_syscall_maps(pid, 45); return 0;
}
SEC("tracepoint/syscalls/sys_enter_sendto")
int trace_sendto_enter(void *ctx) {
__u32 pid = bpf_get_current_pid_tgid() >> 32;
update_syscall_maps(pid, 44); return 0;
}
char LICENSE[] SEC("license") = "GPL";