bpf_probe.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 __perf_count
  17. #define __perf_count(c) (c)
  18. #undef __perf_task
  19. #define __perf_task(t) (t)
  20. /* cast any integer, pointer, or small struct to u64 */
  21. #define UINTTYPE(size) \
  22. __typeof__(__builtin_choose_expr(size == 1, (u8)1, \
  23. __builtin_choose_expr(size == 2, (u16)2, \
  24. __builtin_choose_expr(size == 4, (u32)3, \
  25. __builtin_choose_expr(size == 8, (u64)4, \
  26. (void)5)))))
  27. #define __CAST_TO_U64(x) ({ \
  28. typeof(x) __src = (x); \
  29. UINTTYPE(sizeof(x)) __dst; \
  30. memcpy(&__dst, &__src, sizeof(__dst)); \
  31. (u64)__dst; })
  32. #define __CAST1(a,...) __CAST_TO_U64(a)
  33. #define __CAST2(a,...) __CAST_TO_U64(a), __CAST1(__VA_ARGS__)
  34. #define __CAST3(a,...) __CAST_TO_U64(a), __CAST2(__VA_ARGS__)
  35. #define __CAST4(a,...) __CAST_TO_U64(a), __CAST3(__VA_ARGS__)
  36. #define __CAST5(a,...) __CAST_TO_U64(a), __CAST4(__VA_ARGS__)
  37. #define __CAST6(a,...) __CAST_TO_U64(a), __CAST5(__VA_ARGS__)
  38. #define __CAST7(a,...) __CAST_TO_U64(a), __CAST6(__VA_ARGS__)
  39. #define __CAST8(a,...) __CAST_TO_U64(a), __CAST7(__VA_ARGS__)
  40. #define __CAST9(a,...) __CAST_TO_U64(a), __CAST8(__VA_ARGS__)
  41. #define __CAST10(a,...) __CAST_TO_U64(a), __CAST9(__VA_ARGS__)
  42. #define __CAST11(a,...) __CAST_TO_U64(a), __CAST10(__VA_ARGS__)
  43. #define __CAST12(a,...) __CAST_TO_U64(a), __CAST11(__VA_ARGS__)
  44. /* tracepoints with more than 12 arguments will hit build error */
  45. #define CAST_TO_U64(...) CONCATENATE(__CAST, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__)
  46. #undef DECLARE_EVENT_CLASS
  47. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  48. static notrace void \
  49. __bpf_trace_##call(void *__data, proto) \
  50. { \
  51. struct bpf_prog *prog = __data; \
  52. CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(prog, CAST_TO_U64(args)); \
  53. }
  54. /*
  55. * This part is compiled out, it is only here as a build time check
  56. * to make sure that if the tracepoint handling changes, the
  57. * bpf probe will fail to compile unless it too is updated.
  58. */
  59. #undef DEFINE_EVENT
  60. #define DEFINE_EVENT(template, call, proto, args) \
  61. static inline void bpf_test_probe_##call(void) \
  62. { \
  63. check_trace_callback_type_##call(__bpf_trace_##template); \
  64. } \
  65. static struct bpf_raw_event_map __used \
  66. __attribute__((section("__bpf_raw_tp_map"))) \
  67. __bpf_trace_tp_map_##call = { \
  68. .tp = &__tracepoint_##call, \
  69. .bpf_func = (void *)__bpf_trace_##template, \
  70. .num_args = COUNT_ARGS(args), \
  71. };
  72. #undef DEFINE_EVENT_PRINT
  73. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  74. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  75. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  76. #endif /* CONFIG_BPF_EVENTS */