1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
5 #define BPF_NO_GLOBAL_DATA
6 #include <bpf/bpf_helpers.h>
8 char LICENSE[] SEC("license") = "GPL";
11 __uint(type, BPF_MAP_TYPE_ARRAY);
14 __uint(max_entries, 1);
15 } my_pid_map SEC(".maps");
18 __uint(type, BPF_MAP_TYPE_ARRAY);
21 __uint(max_entries, 1);
22 } res_map SEC(".maps");
24 volatile int my_pid_var = 0;
25 volatile int res_var = 0;
27 SEC("tp/raw_syscalls/sys_enter")
28 int handle_legacy(void *ctx)
30 int zero = 0, *my_pid, cur_pid, *my_res;
32 my_pid = bpf_map_lookup_elem(&my_pid_map, &zero);
36 cur_pid = bpf_get_current_pid_tgid() >> 32;
37 if (cur_pid != *my_pid)
40 my_res = bpf_map_lookup_elem(&res_map, &zero);
45 /* use bpf_printk() in combination with BPF_NO_GLOBAL_DATA to
46 * force .rodata.str1.1 section that previously caused
47 * problems on old kernels due to libbpf always tried to
48 * create a global data map for it
50 bpf_printk("Legacy-case bpf_printk test, pid %d\n", cur_pid);
56 SEC("tp/raw_syscalls/sys_enter")
57 int handle_modern(void *ctx)
61 cur_pid = bpf_get_current_pid_tgid() >> 32;
62 if (cur_pid != my_pid_var)
66 /* we need bpf_printk() to validate libbpf logic around unused
67 * global maps and legacy kernels; see comment in handle_legacy()
69 bpf_printk("Modern-case bpf_printk test, pid %d\n", cur_pid);