Back to home page

OSCL-LXR

 
 

    


0001 #include <linux/ptrace.h>
0002 #include <linux/version.h>
0003 #include <uapi/linux/bpf.h>
0004 #include <bpf/bpf_helpers.h>
0005 
0006 struct {
0007     __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
0008     __uint(key_size, sizeof(int));
0009     __uint(value_size, sizeof(u32));
0010     __uint(max_entries, 64);
0011 } counters SEC(".maps");
0012 
0013 struct {
0014     __uint(type, BPF_MAP_TYPE_HASH);
0015     __type(key, int);
0016     __type(value, u64);
0017     __uint(max_entries, 64);
0018 } values SEC(".maps");
0019 
0020 struct {
0021     __uint(type, BPF_MAP_TYPE_HASH);
0022     __type(key, int);
0023     __type(value, struct bpf_perf_event_value);
0024     __uint(max_entries, 64);
0025 } values2 SEC(".maps");
0026 
0027 SEC("kprobe/htab_map_get_next_key")
0028 int bpf_prog1(struct pt_regs *ctx)
0029 {
0030     u32 key = bpf_get_smp_processor_id();
0031     u64 count, *val;
0032     s64 error;
0033 
0034     count = bpf_perf_event_read(&counters, key);
0035     error = (s64)count;
0036     if (error <= -2 && error >= -22)
0037         return 0;
0038 
0039     val = bpf_map_lookup_elem(&values, &key);
0040     if (val)
0041         *val = count;
0042     else
0043         bpf_map_update_elem(&values, &key, &count, BPF_NOEXIST);
0044 
0045     return 0;
0046 }
0047 
0048 SEC("kprobe/htab_map_lookup_elem")
0049 int bpf_prog2(struct pt_regs *ctx)
0050 {
0051     u32 key = bpf_get_smp_processor_id();
0052     struct bpf_perf_event_value *val, buf;
0053     int error;
0054 
0055     error = bpf_perf_event_read_value(&counters, key, &buf, sizeof(buf));
0056     if (error)
0057         return 0;
0058 
0059     val = bpf_map_lookup_elem(&values2, &key);
0060     if (val)
0061         *val = buf;
0062     else
0063         bpf_map_update_elem(&values2, &key, &buf, BPF_NOEXIST);
0064 
0065     return 0;
0066 }
0067 
0068 char _license[] SEC("license") = "GPL";
0069 u32 _version SEC("version") = LINUX_VERSION_CODE;