pid_iter.bpf.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. /* Copyright (c) 2020 Facebook */
  3. #include <vmlinux.h>
  4. #include <bpf/bpf_helpers.h>
  5. #include <bpf/bpf_core_read.h>
  6. #include <bpf/bpf_tracing.h>
  7. #include "pid_iter.h"
  8. /* keep in sync with the definition in main.h */
  9. enum bpf_obj_type {
  10. BPF_OBJ_UNKNOWN,
  11. BPF_OBJ_PROG,
  12. BPF_OBJ_MAP,
  13. BPF_OBJ_LINK,
  14. BPF_OBJ_BTF,
  15. };
  16. struct bpf_perf_link___local {
  17. struct bpf_link link;
  18. struct file *perf_file;
  19. } __attribute__((preserve_access_index));
  20. struct perf_event___local {
  21. u64 bpf_cookie;
  22. } __attribute__((preserve_access_index));
  23. enum bpf_link_type___local {
  24. BPF_LINK_TYPE_PERF_EVENT___local = 7,
  25. };
  26. extern const void bpf_link_fops __ksym;
  27. extern const void bpf_link_fops_poll __ksym __weak;
  28. extern const void bpf_map_fops __ksym;
  29. extern const void bpf_prog_fops __ksym;
  30. extern const void btf_fops __ksym;
  31. const volatile enum bpf_obj_type obj_type = BPF_OBJ_UNKNOWN;
  32. static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
  33. {
  34. switch (type) {
  35. case BPF_OBJ_PROG:
  36. return BPF_CORE_READ((struct bpf_prog *)ent, aux, id);
  37. case BPF_OBJ_MAP:
  38. return BPF_CORE_READ((struct bpf_map *)ent, id);
  39. case BPF_OBJ_BTF:
  40. return BPF_CORE_READ((struct btf *)ent, id);
  41. case BPF_OBJ_LINK:
  42. return BPF_CORE_READ((struct bpf_link *)ent, id);
  43. default:
  44. return 0;
  45. }
  46. }
  47. /* could be used only with BPF_LINK_TYPE_PERF_EVENT links */
  48. static __u64 get_bpf_cookie(struct bpf_link *link)
  49. {
  50. struct bpf_perf_link___local *perf_link;
  51. struct perf_event___local *event;
  52. perf_link = container_of(link, struct bpf_perf_link___local, link);
  53. event = BPF_CORE_READ(perf_link, perf_file, private_data);
  54. return BPF_CORE_READ(event, bpf_cookie);
  55. }
  56. SEC("iter/task_file")
  57. int iter(struct bpf_iter__task_file *ctx)
  58. {
  59. struct file *file = ctx->file;
  60. struct task_struct *task = ctx->task;
  61. struct pid_iter_entry e;
  62. const void *fops;
  63. if (!file || !task)
  64. return 0;
  65. switch (obj_type) {
  66. case BPF_OBJ_PROG:
  67. fops = &bpf_prog_fops;
  68. break;
  69. case BPF_OBJ_MAP:
  70. fops = &bpf_map_fops;
  71. break;
  72. case BPF_OBJ_BTF:
  73. fops = &btf_fops;
  74. break;
  75. case BPF_OBJ_LINK:
  76. if (&bpf_link_fops_poll &&
  77. file->f_op == &bpf_link_fops_poll)
  78. fops = &bpf_link_fops_poll;
  79. else
  80. fops = &bpf_link_fops;
  81. break;
  82. default:
  83. return 0;
  84. }
  85. if (file->f_op != fops)
  86. return 0;
  87. __builtin_memset(&e, 0, sizeof(e));
  88. e.pid = task->tgid;
  89. e.id = get_obj_id(file->private_data, obj_type);
  90. if (obj_type == BPF_OBJ_LINK &&
  91. bpf_core_enum_value_exists(enum bpf_link_type___local,
  92. BPF_LINK_TYPE_PERF_EVENT___local)) {
  93. struct bpf_link *link = (struct bpf_link *) file->private_data;
  94. if (BPF_CORE_READ(link, type) == bpf_core_enum_value(enum bpf_link_type___local,
  95. BPF_LINK_TYPE_PERF_EVENT___local)) {
  96. e.has_bpf_cookie = true;
  97. e.bpf_cookie = get_bpf_cookie(link);
  98. }
  99. }
  100. bpf_probe_read_kernel_str(&e.comm, sizeof(e.comm),
  101. task->group_leader->comm);
  102. bpf_seq_write(ctx->meta->seq, &e, sizeof(e));
  103. return 0;
  104. }
  105. char LICENSE[] SEC("license") = "Dual BSD/GPL";