1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2024 Jose Fernandez
3 
4 #include "vmlinux.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 
8 struct {
9 	__uint(type, BPF_MAP_TYPE_USER_RINGBUF);
10 	__uint(max_entries, 4096 /* one page */);
11 } user_ringbuf SEC(".maps");
12 
13 struct {
14 	__uint(type, BPF_MAP_TYPE_HASH);
15 	__type(key, u32);
16 	__type(value, u32);
17 	__uint(max_entries, 100);
18 } samples SEC(".maps");
19 
20 struct my_struct_t {
21 	u32 key;
22 	u32 value;
23 };
24 
user_ringbuf_callback(struct bpf_dynptr * dynptr,void * context)25 static long user_ringbuf_callback(struct bpf_dynptr *dynptr, void *context)
26 {
27 	const struct my_struct_t *data;
28 
29 	data = bpf_dynptr_data(dynptr, 0, sizeof(*data));
30 	if (!data)
31 		return 0;
32 
33 	bpf_map_update_elem(&samples, &data->key, &data->value, BPF_ANY);
34 
35 	return 0;
36 }
37 
38 SEC("tp/syscalls/sys_enter_getpid")
handle__sys_enter_getpid(void * ctx)39 int handle__sys_enter_getpid(void *ctx)
40 {
41 	bpf_user_ringbuf_drain(&user_ringbuf, user_ringbuf_callback, NULL, 0);
42 
43 	return 0;
44 }
45 
46 char LICENSE[] SEC("license") = "GPL";
47