bpf_probe.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #undef TRACE_SYSTEM_VAR
  3. #ifdef CONFIG_BPF_EVENTS
  4. #include "stages/stage6_event_callback.h"
  5. #undef __perf_count
  6. #define __perf_count(c) (c)
  7. #undef __perf_task
  8. #define __perf_task(t) (t)
  9. #include <linux/args.h>
  10. /* cast any integer, pointer, or small struct to u64 */
  11. #define UINTTYPE(size) \
  12. __typeof__(__builtin_choose_expr(size == 1, (u8)1, \
  13. __builtin_choose_expr(size == 2, (u16)2, \
  14. __builtin_choose_expr(size == 4, (u32)3, \
  15. __builtin_choose_expr(size == 8, (u64)4, \
  16. (void)5)))))
  17. #define __CAST_TO_U64(x) ({ \
  18. typeof(x) __src = (x); \
  19. UINTTYPE(sizeof(x)) __dst; \
  20. memcpy(&__dst, &__src, sizeof(__dst)); \
  21. (u64)__dst; })
  22. #define __CAST1(a,...) __CAST_TO_U64(a)
  23. #define __CAST2(a,...) __CAST_TO_U64(a), __CAST1(__VA_ARGS__)
  24. #define __CAST3(a,...) __CAST_TO_U64(a), __CAST2(__VA_ARGS__)
  25. #define __CAST4(a,...) __CAST_TO_U64(a), __CAST3(__VA_ARGS__)
  26. #define __CAST5(a,...) __CAST_TO_U64(a), __CAST4(__VA_ARGS__)
  27. #define __CAST6(a,...) __CAST_TO_U64(a), __CAST5(__VA_ARGS__)
  28. #define __CAST7(a,...) __CAST_TO_U64(a), __CAST6(__VA_ARGS__)
  29. #define __CAST8(a,...) __CAST_TO_U64(a), __CAST7(__VA_ARGS__)
  30. #define __CAST9(a,...) __CAST_TO_U64(a), __CAST8(__VA_ARGS__)
  31. #define __CAST10(a,...) __CAST_TO_U64(a), __CAST9(__VA_ARGS__)
  32. #define __CAST11(a,...) __CAST_TO_U64(a), __CAST10(__VA_ARGS__)
  33. #define __CAST12(a,...) __CAST_TO_U64(a), __CAST11(__VA_ARGS__)
  34. /* tracepoints with more than 12 arguments will hit build error */
  35. #define CAST_TO_U64(...) CONCATENATE(__CAST, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__)
  36. #define __BPF_DECLARE_TRACE(call, proto, args) \
  37. static notrace void \
  38. __bpf_trace_##call(void *__data, proto) \
  39. { \
  40. CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(__data, CAST_TO_U64(args)); \
  41. }
  42. #undef DECLARE_EVENT_CLASS
  43. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  44. __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))
  45. /*
  46. * This part is compiled out, it is only here as a build time check
  47. * to make sure that if the tracepoint handling changes, the
  48. * bpf probe will fail to compile unless it too is updated.
  49. */
  50. #define __DEFINE_EVENT(template, call, proto, args, size) \
  51. static inline void bpf_test_probe_##call(void) \
  52. { \
  53. check_trace_callback_type_##call(__bpf_trace_##template); \
  54. } \
  55. typedef void (*btf_trace_##call)(void *__data, proto); \
  56. static union { \
  57. struct bpf_raw_event_map event; \
  58. btf_trace_##call handler; \
  59. } __bpf_trace_tp_map_##call __used \
  60. __section("__bpf_raw_tp_map") = { \
  61. .event = { \
  62. .tp = &__tracepoint_##call, \
  63. .bpf_func = __bpf_trace_##template, \
  64. .num_args = COUNT_ARGS(args), \
  65. .writable_size = size, \
  66. }, \
  67. };
  68. #define FIRST(x, ...) x
  69. #define __CHECK_WRITABLE_BUF_SIZE(call, proto, args, size) \
  70. static inline void bpf_test_buffer_##call(void) \
  71. { \
  72. /* BUILD_BUG_ON() is ignored if the code is completely eliminated, but \
  73. * BUILD_BUG_ON_ZERO() uses a different mechanism that is not \
  74. * dead-code-eliminated. \
  75. */ \
  76. FIRST(proto); \
  77. (void)BUILD_BUG_ON_ZERO(size != sizeof(*FIRST(args))); \
  78. }
  79. #undef DEFINE_EVENT_WRITABLE
  80. #define DEFINE_EVENT_WRITABLE(template, call, proto, args, size) \
  81. __CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
  82. __DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size)
  83. #undef DEFINE_EVENT
  84. #define DEFINE_EVENT(template, call, proto, args) \
  85. __DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), 0)
  86. #undef DEFINE_EVENT_PRINT
  87. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  88. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  89. #undef DECLARE_TRACE
  90. #define DECLARE_TRACE(call, proto, args) \
  91. __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \
  92. __DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0)
  93. #undef DECLARE_TRACE_WRITABLE
  94. #define DECLARE_TRACE_WRITABLE(call, proto, args, size) \
  95. __CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
  96. __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \
  97. __DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), size)
  98. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  99. #undef DECLARE_TRACE_WRITABLE
  100. #undef DEFINE_EVENT_WRITABLE
  101. #undef __CHECK_WRITABLE_BUF_SIZE
  102. #undef __DEFINE_EVENT
  103. #undef FIRST
  104. #endif /* CONFIG_BPF_EVENTS */