github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/testdata/manyprogs.c (about) 1 /* This file is used for benchmarking NewCollection(). 2 */ 3 4 #include "../btf/testdata/bpf_core_read.h" 5 #include "common.h" 6 7 char __license[] __section("license") = "Dual MIT/GPL"; 8 9 struct bpf_map_def __section("maps") kprobe_map = { 10 .type = BPF_MAP_TYPE_HASH, 11 .key_size = sizeof(uint32_t), 12 .value_size = sizeof(uint64_t), 13 .max_entries = 128, 14 }; 15 16 static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *)1; 17 static long (*bpf_map_update_elem)(void *map, const void *key, const void *value, uint64_t flags) = (void *)2; 18 static void *(*bpf_get_current_task)() = (void *)35; 19 static long (*bpf_probe_read_kernel)(void *dst, uint32_t size, const void *unsafe_ptr) = (void *)113; 20 21 #pragma clang attribute push(__attribute__((preserve_access_index)), apply_to = record) 22 struct ns_common { 23 unsigned int inum; 24 }; 25 struct mnt_namespace { 26 struct ns_common ns; 27 }; 28 struct nsproxy { 29 struct mnt_namespace *mnt_ns; 30 }; 31 struct task_struct { 32 struct nsproxy *nsproxy; 33 }; 34 #pragma clang attribute pop 35 36 static inline int impl() { 37 uint64_t initval = 1, *valp; 38 39 struct task_struct *task = (struct task_struct *)bpf_get_current_task(); 40 uint32_t mntns = BPF_CORE_READ(task, nsproxy, mnt_ns, ns.inum); 41 42 valp = bpf_map_lookup_elem(&kprobe_map, &mntns); 43 if (!valp) { 44 bpf_map_update_elem(&kprobe_map, &mntns, &initval, 0); 45 return 0; 46 } 47 __sync_fetch_and_add(valp, 1); 48 49 return 0; 50 } 51 52 #define DEFINE_PROBE(i) \ 53 __section("kprobe/sys_execvea" #i) int kprobe_execve##i() { \ 54 return impl(); \ 55 } 56 57 DEFINE_PROBE(0); 58 DEFINE_PROBE(1); 59 DEFINE_PROBE(2); 60 DEFINE_PROBE(3); 61 DEFINE_PROBE(4); 62 DEFINE_PROBE(5); 63 DEFINE_PROBE(6); 64 DEFINE_PROBE(7); 65 DEFINE_PROBE(8); 66 DEFINE_PROBE(9); 67 68 DEFINE_PROBE(10); 69 DEFINE_PROBE(11); 70 DEFINE_PROBE(12); 71 DEFINE_PROBE(13); 72 DEFINE_PROBE(14); 73 DEFINE_PROBE(15); 74 DEFINE_PROBE(16); 75 DEFINE_PROBE(17); 76 DEFINE_PROBE(18); 77 DEFINE_PROBE(19); 78 79 DEFINE_PROBE(20); 80 DEFINE_PROBE(21); 81 DEFINE_PROBE(22); 82 DEFINE_PROBE(23); 83 DEFINE_PROBE(24); 84 DEFINE_PROBE(25); 85 DEFINE_PROBE(26); 86 DEFINE_PROBE(27); 87 DEFINE_PROBE(28); 88 DEFINE_PROBE(29);