Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 #undef TRACE_SYSTEM_VAR
0004 
0005 #ifdef CONFIG_BPF_EVENTS
0006 
0007 #undef __entry
0008 #define __entry entry
0009 
0010 #undef __get_dynamic_array
0011 #define __get_dynamic_array(field)  \
0012         ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
0013 
0014 #undef __get_dynamic_array_len
0015 #define __get_dynamic_array_len(field)  \
0016         ((__entry->__data_loc_##field >> 16) & 0xffff)
0017 
0018 #undef __get_str
0019 #define __get_str(field) ((char *)__get_dynamic_array(field))
0020 
0021 #undef __get_bitmask
0022 #define __get_bitmask(field) (char *)__get_dynamic_array(field)
0023 
0024 #undef __get_sockaddr
0025 #define __get_sockaddr(field) ((struct sockaddr *)__get_dynamic_array(field))
0026 
0027 #undef __get_rel_dynamic_array
0028 #define __get_rel_dynamic_array(field)  \
0029         ((void *)(&__entry->__rel_loc_##field) +    \
0030          sizeof(__entry->__rel_loc_##field) +       \
0031          (__entry->__rel_loc_##field & 0xffff))
0032 
0033 #undef __get_rel_dynamic_array_len
0034 #define __get_rel_dynamic_array_len(field)  \
0035         ((__entry->__rel_loc_##field >> 16) & 0xffff)
0036 
0037 #undef __get_rel_str
0038 #define __get_rel_str(field) ((char *)__get_rel_dynamic_array(field))
0039 
0040 #undef __get_rel_bitmask
0041 #define __get_rel_bitmask(field) (char *)__get_rel_dynamic_array(field)
0042 
0043 #undef __get_rel_sockaddr
0044 #define __get_rel_sockaddr(field) ((struct sockaddr *)__get_rel_dynamic_array(field))
0045 
0046 #undef __perf_count
0047 #define __perf_count(c) (c)
0048 
0049 #undef __perf_task
0050 #define __perf_task(t)  (t)
0051 
0052 /* cast any integer, pointer, or small struct to u64 */
0053 #define UINTTYPE(size) \
0054     __typeof__(__builtin_choose_expr(size == 1,  (u8)1, \
0055            __builtin_choose_expr(size == 2, (u16)2, \
0056            __builtin_choose_expr(size == 4, (u32)3, \
0057            __builtin_choose_expr(size == 8, (u64)4, \
0058                      (void)5)))))
0059 #define __CAST_TO_U64(x) ({ \
0060     typeof(x) __src = (x); \
0061     UINTTYPE(sizeof(x)) __dst; \
0062     memcpy(&__dst, &__src, sizeof(__dst)); \
0063     (u64)__dst; })
0064 
0065 #define __CAST1(a,...) __CAST_TO_U64(a)
0066 #define __CAST2(a,...) __CAST_TO_U64(a), __CAST1(__VA_ARGS__)
0067 #define __CAST3(a,...) __CAST_TO_U64(a), __CAST2(__VA_ARGS__)
0068 #define __CAST4(a,...) __CAST_TO_U64(a), __CAST3(__VA_ARGS__)
0069 #define __CAST5(a,...) __CAST_TO_U64(a), __CAST4(__VA_ARGS__)
0070 #define __CAST6(a,...) __CAST_TO_U64(a), __CAST5(__VA_ARGS__)
0071 #define __CAST7(a,...) __CAST_TO_U64(a), __CAST6(__VA_ARGS__)
0072 #define __CAST8(a,...) __CAST_TO_U64(a), __CAST7(__VA_ARGS__)
0073 #define __CAST9(a,...) __CAST_TO_U64(a), __CAST8(__VA_ARGS__)
0074 #define __CAST10(a,...) __CAST_TO_U64(a), __CAST9(__VA_ARGS__)
0075 #define __CAST11(a,...) __CAST_TO_U64(a), __CAST10(__VA_ARGS__)
0076 #define __CAST12(a,...) __CAST_TO_U64(a), __CAST11(__VA_ARGS__)
0077 /* tracepoints with more than 12 arguments will hit build error */
0078 #define CAST_TO_U64(...) CONCATENATE(__CAST, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__)
0079 
0080 #define __BPF_DECLARE_TRACE(call, proto, args)              \
0081 static notrace void                         \
0082 __bpf_trace_##call(void *__data, proto)                 \
0083 {                                   \
0084     struct bpf_prog *prog = __data;                 \
0085     CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(prog, CAST_TO_U64(args));  \
0086 }
0087 
0088 #undef DECLARE_EVENT_CLASS
0089 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)  \
0090     __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))
0091 
0092 /*
0093  * This part is compiled out, it is only here as a build time check
0094  * to make sure that if the tracepoint handling changes, the
0095  * bpf probe will fail to compile unless it too is updated.
0096  */
0097 #define __DEFINE_EVENT(template, call, proto, args, size)       \
0098 static inline void bpf_test_probe_##call(void)              \
0099 {                                   \
0100     check_trace_callback_type_##call(__bpf_trace_##template);   \
0101 }                                   \
0102 typedef void (*btf_trace_##call)(void *__data, proto);          \
0103 static union {                              \
0104     struct bpf_raw_event_map event;                 \
0105     btf_trace_##call handler;                   \
0106 } __bpf_trace_tp_map_##call __used                  \
0107 __section("__bpf_raw_tp_map") = {                   \
0108     .event = {                          \
0109         .tp     = &__tracepoint_##call,         \
0110         .bpf_func   = __bpf_trace_##template,       \
0111         .num_args   = COUNT_ARGS(args),         \
0112         .writable_size  = size,                 \
0113     },                              \
0114 };
0115 
0116 #define FIRST(x, ...) x
0117 
0118 #define __CHECK_WRITABLE_BUF_SIZE(call, proto, args, size)      \
0119 static inline void bpf_test_buffer_##call(void)             \
0120 {                                   \
0121     /* BUILD_BUG_ON() is ignored if the code is completely eliminated, but \
0122      * BUILD_BUG_ON_ZERO() uses a different mechanism that is not   \
0123      * dead-code-eliminated.                    \
0124      */                             \
0125     FIRST(proto);                           \
0126     (void)BUILD_BUG_ON_ZERO(size != sizeof(*FIRST(args)));      \
0127 }
0128 
0129 #undef DEFINE_EVENT_WRITABLE
0130 #define DEFINE_EVENT_WRITABLE(template, call, proto, args, size) \
0131     __CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
0132     __DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size)
0133 
0134 #undef DEFINE_EVENT
0135 #define DEFINE_EVENT(template, call, proto, args)           \
0136     __DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), 0)
0137 
0138 #undef DEFINE_EVENT_PRINT
0139 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)  \
0140     DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
0141 
0142 #undef DECLARE_TRACE
0143 #define DECLARE_TRACE(call, proto, args)                \
0144     __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))      \
0145     __DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0)
0146 
0147 #undef DECLARE_TRACE_WRITABLE
0148 #define DECLARE_TRACE_WRITABLE(call, proto, args, size) \
0149     __CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
0150     __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \
0151     __DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), size)
0152 
0153 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
0154 
0155 #undef DECLARE_TRACE_WRITABLE
0156 #undef DEFINE_EVENT_WRITABLE
0157 #undef __CHECK_WRITABLE_BUF_SIZE
0158 #undef __DEFINE_EVENT
0159 #undef FIRST
0160 
0161 #endif /* CONFIG_BPF_EVENTS */