tracex6_kern.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <linux/ptrace.h>
  2. #include <linux/version.h>
  3. #include <uapi/linux/bpf.h>
  4. #include "bpf_helpers.h"
  5. struct bpf_map_def SEC("maps") counters = {
  6. .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
  7. .key_size = sizeof(int),
  8. .value_size = sizeof(u32),
  9. .max_entries = 64,
  10. };
  11. struct bpf_map_def SEC("maps") values = {
  12. .type = BPF_MAP_TYPE_HASH,
  13. .key_size = sizeof(int),
  14. .value_size = sizeof(u64),
  15. .max_entries = 64,
  16. };
  17. struct bpf_map_def SEC("maps") values2 = {
  18. .type = BPF_MAP_TYPE_HASH,
  19. .key_size = sizeof(int),
  20. .value_size = sizeof(struct bpf_perf_event_value),
  21. .max_entries = 64,
  22. };
  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;