1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| ##define BPF_NO_GLOBAL_DATA
##include <vmlinux.h>
##include <bpf/bpf_core_read.h>
##include <bpf/bpf_helpers.h>
##include "task.h"
struct { __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); __uint(key_size, sizeof(u32)); __uint(value_size, sizeof(u32)); } output SEC(".maps");
struct { __uint(type, BPF_MAP_TYPE_HASH); __uint(max_entries, 1024); __type(key, pid_t); __type(value, struct task_info); __uint(pinning, LIBBPF_PIN_BY_NAME); } taskcomm SEC(.maps);
SEC("tracepoint/syscalls/sys_enter_execve")
int bpf_prog(void *ctx) {
struct task_struct *task = (struct task_struct *)bpf_get_current_task(); if (!task) return 0;
int counter = BPF_CORE_READ(task, files, count.counter); struct task_vm vm; vm.start_code = BPF_CORE_READ(task, mm, start_code); vm.end_code = BPF_CORE_READ(task, mm, end_code); vm.start_data = BPF_CORE_READ(task, mm, start_data); vm.end_data = BPF_CORE_READ(task, mm, end_data); vm.start_brk = BPF_CORE_READ(task, mm, start_brk); vm.brk = BPF_CORE_READ(task, mm, brk); vm.start_stack = BPF_CORE_READ(task, mm, start_stack); vm.arg_start = BPF_CORE_READ(task, mm, arg_start); vm.arg_end = BPF_CORE_READ(task, mm, arg_end); vm.env_start = BPF_CORE_READ(task, mm, env_start); vm.env_end = BPF_CORE_READ(task, mm, env_end); vm.pid = pid;
__bpf_printk(" comm: %s, counter %d\n", &comm, counter);
bpf_perf_event_output(ctx, &output, BPF_F_CURRENT_CPU, &vm, sizeof(vm));
__u64 id = bpf_get_current_pid_tgid(); pid_t pid = id >> 32; struct task_info info = {0}; bpf_get_current_comm(&info.comm, sizeof(info.comm)); info.pid = pid;
bpf_map_update_elem(&taskcomm, &pid, &info, BPF_ANY);
return 0; }
char LICENSE[] SEC("license") = "Dual BSD/GPL";
|