bpf_probe.h 5.4 KB

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