tracex6_kern.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <linux/ptrace.h>
  2. #include <linux/version.h>
  3. #include <uapi/linux/bpf.h>
  4. #include <bpf/bpf_helpers.h>
  5. struct {
  6. __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
  7. __uint(key_size, sizeof(int));
  8. __uint(value_size, sizeof(u32));
  9. __uint(max_entries, 64);
  10. } counters SEC(".maps");
  11. struct {
  12. __uint(type, BPF_MAP_TYPE_HASH);
  13. __type(key, int);
  14. __type(value, u64);
  15. __uint(max_entries, 64);
  16. } values SEC(".maps");
  17. struct {
  18. __uint(type, BPF_MAP_TYPE_HASH);
  19. __type(key, int);
  20. __type(value, struct bpf_perf_event_value);
  21. __uint(max_entries, 64);
  22. } values2 SEC(".maps");
  23. SEC("kprobe/htab_map_get_next_key")
  24. int bpf_prog1(struct pt_regs *ctx)
  25. {
  26. u32 key = bpf_get_smp_processor_id();
  27. u64 count, *val;
  28. s64 error;
  29. count = bpf_perf_event_read(&counters, key);
  30. error = (s64)count;
  31. if (error <= -2 && error >= -22)
  32. return 0;
  33. val = bpf_map_lookup_elem(&values, &key);
  34. if (val)
  35. *val = count;
  36. else
  37. bpf_map_update_elem(&values, &key, &count, BPF_NOEXIST);
  38. return 0;
  39. }
  40. SEC("kprobe/htab_map_lookup_elem")
  41. int bpf_prog2(struct pt_regs *ctx)
  42. {
  43. u32 key = bpf_get_smp_processor_id();
  44. struct bpf_perf_event_value *val, buf;
  45. int error;
  46. error = bpf_perf_event_read_value(&counters, key, &buf, sizeof(buf));
  47. if (error)
  48. return 0;
  49. val = bpf_map_lookup_elem(&values2, &key);
  50. if (val)
  51. *val = buf;
  52. else
  53. bpf_map_update_elem(&values2, &key, &buf, BPF_NOEXIST);
  54. return 0;
  55. }
  56. char _license[] SEC("license") = "GPL";
  57. u32 _version SEC("version") = LINUX_VERSION_CODE;