0001
0002
0003
0004 #include "vmlinux.h"
0005 #include <bpf/bpf_helpers.h>
0006 #include <bpf/bpf_tracing.h>
0007
0008 char _license[] SEC("license") = "GPL";
0009
0010 struct {
0011 __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
0012 __uint(map_flags, BPF_F_NO_PREALLOC);
0013 __type(key, int);
0014 __type(value, long);
0015 } enter_id SEC(".maps");
0016
0017 #define MAGIC_VALUE 0xabcd1234
0018
0019 pid_t target_pid = 0;
0020 int mismatch_cnt = 0;
0021 int enter_cnt = 0;
0022 int exit_cnt = 0;
0023
0024 SEC("tp_btf/sys_enter")
0025 int BPF_PROG(on_enter, struct pt_regs *regs, long id)
0026 {
0027 struct task_struct *task;
0028 long *ptr;
0029
0030 task = bpf_get_current_task_btf();
0031 if (task->pid != target_pid)
0032 return 0;
0033
0034 ptr = bpf_task_storage_get(&enter_id, task, 0,
0035 BPF_LOCAL_STORAGE_GET_F_CREATE);
0036 if (!ptr)
0037 return 0;
0038
0039 __sync_fetch_and_add(&enter_cnt, 1);
0040 *ptr = MAGIC_VALUE + enter_cnt;
0041
0042 return 0;
0043 }
0044
0045 SEC("tp_btf/sys_exit")
0046 int BPF_PROG(on_exit, struct pt_regs *regs, long id)
0047 {
0048 struct task_struct *task;
0049 long *ptr;
0050
0051 task = bpf_get_current_task_btf();
0052 if (task->pid != target_pid)
0053 return 0;
0054
0055 ptr = bpf_task_storage_get(&enter_id, task, 0,
0056 BPF_LOCAL_STORAGE_GET_F_CREATE);
0057 if (!ptr)
0058 return 0;
0059
0060 __sync_fetch_and_add(&exit_cnt, 1);
0061 if (*ptr != MAGIC_VALUE + exit_cnt)
0062 __sync_fetch_and_add(&mismatch_cnt, 1);
0063 return 0;
0064 }