b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | |
| 3 | /* |
| 4 | * Note: we intentionally omit include file ifdef protection |
| 5 | * This is due to the way trace events work. If a file includes two |
| 6 | * trace event headers under one "CREATE_TRACE_POINTS" the first include |
| 7 | * will override the DECLARE_RESTRICTED_HOOK and break the second include. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/tracepoint.h> |
| 11 | |
| 12 | #if defined(CONFIG_TRACEPOINTS) && defined(CONFIG_ANDROID_VENDOR_HOOKS) |
| 13 | |
| 14 | #define DECLARE_HOOK DECLARE_TRACE |
| 15 | |
| 16 | #ifdef TRACE_HEADER_MULTI_READ |
| 17 | |
| 18 | #undef DECLARE_RESTRICTED_HOOK |
| 19 | #define DECLARE_RESTRICTED_HOOK(name, proto, args, cond) \ |
| 20 | DEFINE_TRACE(name) |
| 21 | |
| 22 | |
| 23 | /* prevent additional recursion */ |
| 24 | #undef TRACE_HEADER_MULTI_READ |
| 25 | #else /* TRACE_HEADER_MULTI_READ */ |
| 26 | |
| 27 | #define DO_HOOK(tp, proto, args, cond) \ |
| 28 | do { \ |
| 29 | struct tracepoint_func *it_func_ptr; \ |
| 30 | void *it_func; \ |
| 31 | void *__data; \ |
| 32 | \ |
| 33 | if (!(cond)) \ |
| 34 | return; \ |
| 35 | \ |
| 36 | it_func_ptr = (tp)->funcs; \ |
| 37 | if (it_func_ptr) { \ |
| 38 | it_func = (it_func_ptr)->func; \ |
| 39 | __data = (it_func_ptr)->data; \ |
| 40 | ((void(*)(proto))(it_func))(args); \ |
| 41 | WARN_ON(((++it_func_ptr)->func)); \ |
| 42 | } \ |
| 43 | } while (0) |
| 44 | |
| 45 | #define __DECLARE_HOOK(name, proto, args, cond, data_proto, data_args) \ |
| 46 | extern struct tracepoint __tracepoint_##name; \ |
| 47 | static inline void __nocfi trace_##name(proto) \ |
| 48 | { \ |
| 49 | if (static_key_false(&__tracepoint_##name.key)) \ |
| 50 | DO_HOOK(&__tracepoint_##name, \ |
| 51 | TP_PROTO(data_proto), \ |
| 52 | TP_ARGS(data_args), \ |
| 53 | TP_CONDITION(cond)); \ |
| 54 | } \ |
| 55 | static inline bool \ |
| 56 | trace_##name##_enabled(void) \ |
| 57 | { \ |
| 58 | return static_key_false(&__tracepoint_##name.key); \ |
| 59 | } \ |
| 60 | static inline int \ |
| 61 | register_trace_##name(void (*probe)(data_proto), void *data) \ |
| 62 | { \ |
| 63 | /* only allow a single attachment */ \ |
| 64 | if (trace_##name##_enabled()) \ |
| 65 | return -EBUSY; \ |
| 66 | return tracepoint_probe_register(&__tracepoint_##name, \ |
| 67 | (void *)probe, data); \ |
| 68 | } \ |
| 69 | /* vendor hooks cannot be unregistered */ \ |
| 70 | |
| 71 | #undef DECLARE_RESTRICTED_HOOK |
| 72 | #define DECLARE_RESTRICTED_HOOK(name, proto, args, cond) \ |
| 73 | __DECLARE_HOOK(name, PARAMS(proto), PARAMS(args), \ |
| 74 | cond, \ |
| 75 | PARAMS(void *__data, proto), \ |
| 76 | PARAMS(__data, args)) |
| 77 | |
| 78 | #endif /* TRACE_HEADER_MULTI_READ */ |
| 79 | |
| 80 | #else /* !CONFIG_TRACEPOINTS || !CONFIG_ANDROID_VENDOR_HOOKS */ |
| 81 | /* suppress trace hooks */ |
| 82 | #define DECLARE_HOOK DECLARE_EVENT_NOP |
| 83 | #define DECLARE_RESTRICTED_HOOK(name, proto, args, cond) \ |
| 84 | DECLARE_EVENT_NOP(name, PARAMS(proto), PARAMS(args)) |
| 85 | #endif |