1*bed243d3SAndroid Build Coastguard Worker //===- xray_interface.h -----------------------------------------*- C++ -*-===// 2*bed243d3SAndroid Build Coastguard Worker // 3*bed243d3SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*bed243d3SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information. 5*bed243d3SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*bed243d3SAndroid Build Coastguard Worker // 7*bed243d3SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 8*bed243d3SAndroid Build Coastguard Worker // 9*bed243d3SAndroid Build Coastguard Worker // This file is a part of XRay, a dynamic runtime instrumentation system. 10*bed243d3SAndroid Build Coastguard Worker // 11*bed243d3SAndroid Build Coastguard Worker // APIs for controlling XRay functionality explicitly. 12*bed243d3SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 13*bed243d3SAndroid Build Coastguard Worker 14*bed243d3SAndroid Build Coastguard Worker #ifndef XRAY_XRAY_INTERFACE_H 15*bed243d3SAndroid Build Coastguard Worker #define XRAY_XRAY_INTERFACE_H 16*bed243d3SAndroid Build Coastguard Worker 17*bed243d3SAndroid Build Coastguard Worker #include <cstddef> 18*bed243d3SAndroid Build Coastguard Worker #include <cstdint> 19*bed243d3SAndroid Build Coastguard Worker 20*bed243d3SAndroid Build Coastguard Worker extern "C" { 21*bed243d3SAndroid Build Coastguard Worker 22*bed243d3SAndroid Build Coastguard Worker /// Synchronize this with AsmPrinter::SledKind in LLVM. 23*bed243d3SAndroid Build Coastguard Worker enum XRayEntryType { 24*bed243d3SAndroid Build Coastguard Worker ENTRY = 0, 25*bed243d3SAndroid Build Coastguard Worker EXIT = 1, 26*bed243d3SAndroid Build Coastguard Worker TAIL = 2, 27*bed243d3SAndroid Build Coastguard Worker LOG_ARGS_ENTRY = 3, 28*bed243d3SAndroid Build Coastguard Worker CUSTOM_EVENT = 4, 29*bed243d3SAndroid Build Coastguard Worker TYPED_EVENT = 5, 30*bed243d3SAndroid Build Coastguard Worker }; 31*bed243d3SAndroid Build Coastguard Worker 32*bed243d3SAndroid Build Coastguard Worker /// Provide a function to invoke for when instrumentation points are hit. This 33*bed243d3SAndroid Build Coastguard Worker /// is a user-visible control surface that overrides the default implementation. 34*bed243d3SAndroid Build Coastguard Worker /// The function provided should take the following arguments: 35*bed243d3SAndroid Build Coastguard Worker /// 36*bed243d3SAndroid Build Coastguard Worker /// - function id: an identifier that indicates the id of a function; this id 37*bed243d3SAndroid Build Coastguard Worker /// is generated by xray; the mapping between the function id 38*bed243d3SAndroid Build Coastguard Worker /// and the actual function pointer is available through 39*bed243d3SAndroid Build Coastguard Worker /// __xray_table. 40*bed243d3SAndroid Build Coastguard Worker /// - entry type: identifies what kind of instrumentation point was 41*bed243d3SAndroid Build Coastguard Worker /// encountered (function entry, function exit, etc.). See the 42*bed243d3SAndroid Build Coastguard Worker /// enum XRayEntryType for more details. 43*bed243d3SAndroid Build Coastguard Worker /// 44*bed243d3SAndroid Build Coastguard Worker /// The user handler must handle correctly spurious calls after this handler is 45*bed243d3SAndroid Build Coastguard Worker /// removed or replaced with another handler, because it would be too costly for 46*bed243d3SAndroid Build Coastguard Worker /// XRay runtime to avoid spurious calls. 47*bed243d3SAndroid Build Coastguard Worker /// To prevent circular calling, the handler function itself and all its 48*bed243d3SAndroid Build Coastguard Worker /// direct&indirect callees must not be instrumented with XRay, which can be 49*bed243d3SAndroid Build Coastguard Worker /// achieved by marking them all with: __attribute__((xray_never_instrument)) 50*bed243d3SAndroid Build Coastguard Worker /// 51*bed243d3SAndroid Build Coastguard Worker /// Returns 1 on success, 0 on error. 52*bed243d3SAndroid Build Coastguard Worker extern int __xray_set_handler(void (*entry)(int32_t, XRayEntryType)); 53*bed243d3SAndroid Build Coastguard Worker 54*bed243d3SAndroid Build Coastguard Worker /// This removes whatever the currently provided handler is. Returns 1 on 55*bed243d3SAndroid Build Coastguard Worker /// success, 0 on error. 56*bed243d3SAndroid Build Coastguard Worker extern int __xray_remove_handler(); 57*bed243d3SAndroid Build Coastguard Worker 58*bed243d3SAndroid Build Coastguard Worker /// Use XRay to log the first argument of each (instrumented) function call. 59*bed243d3SAndroid Build Coastguard Worker /// When this function exits, all threads will have observed the effect and 60*bed243d3SAndroid Build Coastguard Worker /// start logging their subsequent affected function calls (if patched). 61*bed243d3SAndroid Build Coastguard Worker /// 62*bed243d3SAndroid Build Coastguard Worker /// Returns 1 on success, 0 on error. 63*bed243d3SAndroid Build Coastguard Worker extern int __xray_set_handler_arg1(void (*entry)(int32_t, XRayEntryType, 64*bed243d3SAndroid Build Coastguard Worker uint64_t)); 65*bed243d3SAndroid Build Coastguard Worker 66*bed243d3SAndroid Build Coastguard Worker /// Disables the XRay handler used to log first arguments of function calls. 67*bed243d3SAndroid Build Coastguard Worker /// Returns 1 on success, 0 on error. 68*bed243d3SAndroid Build Coastguard Worker extern int __xray_remove_handler_arg1(); 69*bed243d3SAndroid Build Coastguard Worker 70*bed243d3SAndroid Build Coastguard Worker /// Provide a function to invoke when XRay encounters a custom event. 71*bed243d3SAndroid Build Coastguard Worker extern int __xray_set_customevent_handler(void (*entry)(void *, std::size_t)); 72*bed243d3SAndroid Build Coastguard Worker 73*bed243d3SAndroid Build Coastguard Worker /// This removes whatever the currently provided custom event handler is. 74*bed243d3SAndroid Build Coastguard Worker /// Returns 1 on success, 0 on error. 75*bed243d3SAndroid Build Coastguard Worker extern int __xray_remove_customevent_handler(); 76*bed243d3SAndroid Build Coastguard Worker 77*bed243d3SAndroid Build Coastguard Worker /// Set a handler for xray typed event logging. The first parameter is a type 78*bed243d3SAndroid Build Coastguard Worker /// identifier, the second is a payload, and the third is the payload size. 79*bed243d3SAndroid Build Coastguard Worker /// NOTE: fdrLoggingHandleTypedEvent only supports uint16_t event type. 80*bed243d3SAndroid Build Coastguard Worker extern int __xray_set_typedevent_handler(void (*entry)(size_t, const void *, 81*bed243d3SAndroid Build Coastguard Worker size_t)); 82*bed243d3SAndroid Build Coastguard Worker 83*bed243d3SAndroid Build Coastguard Worker /// Removes the currently set typed event handler. 84*bed243d3SAndroid Build Coastguard Worker /// Returns 1 on success, 0 on error. 85*bed243d3SAndroid Build Coastguard Worker extern int __xray_remove_typedevent_handler(); 86*bed243d3SAndroid Build Coastguard Worker 87*bed243d3SAndroid Build Coastguard Worker extern uint16_t __xray_register_event_type(const char *event_type); 88*bed243d3SAndroid Build Coastguard Worker 89*bed243d3SAndroid Build Coastguard Worker enum XRayPatchingStatus { 90*bed243d3SAndroid Build Coastguard Worker NOT_INITIALIZED = 0, 91*bed243d3SAndroid Build Coastguard Worker SUCCESS = 1, 92*bed243d3SAndroid Build Coastguard Worker ONGOING = 2, 93*bed243d3SAndroid Build Coastguard Worker FAILED = 3, 94*bed243d3SAndroid Build Coastguard Worker }; 95*bed243d3SAndroid Build Coastguard Worker 96*bed243d3SAndroid Build Coastguard Worker /// This tells XRay to patch the instrumentation points. See XRayPatchingStatus 97*bed243d3SAndroid Build Coastguard Worker /// for possible result values. 98*bed243d3SAndroid Build Coastguard Worker extern XRayPatchingStatus __xray_patch(); 99*bed243d3SAndroid Build Coastguard Worker 100*bed243d3SAndroid Build Coastguard Worker /// Reverses the effect of __xray_patch(). See XRayPatchingStatus for possible 101*bed243d3SAndroid Build Coastguard Worker /// result values. 102*bed243d3SAndroid Build Coastguard Worker extern XRayPatchingStatus __xray_unpatch(); 103*bed243d3SAndroid Build Coastguard Worker 104*bed243d3SAndroid Build Coastguard Worker /// This patches a specific function id. See XRayPatchingStatus for possible 105*bed243d3SAndroid Build Coastguard Worker /// result values. 106*bed243d3SAndroid Build Coastguard Worker extern XRayPatchingStatus __xray_patch_function(int32_t FuncId); 107*bed243d3SAndroid Build Coastguard Worker 108*bed243d3SAndroid Build Coastguard Worker /// This unpatches a specific function id. See XRayPatchingStatus for possible 109*bed243d3SAndroid Build Coastguard Worker /// result values. 110*bed243d3SAndroid Build Coastguard Worker extern XRayPatchingStatus __xray_unpatch_function(int32_t FuncId); 111*bed243d3SAndroid Build Coastguard Worker 112*bed243d3SAndroid Build Coastguard Worker /// This function returns the address of the function provided a valid function 113*bed243d3SAndroid Build Coastguard Worker /// id. We return 0 if we encounter any error, even if 0 may be a valid function 114*bed243d3SAndroid Build Coastguard Worker /// address. 115*bed243d3SAndroid Build Coastguard Worker extern uintptr_t __xray_function_address(int32_t FuncId); 116*bed243d3SAndroid Build Coastguard Worker 117*bed243d3SAndroid Build Coastguard Worker /// This function returns the maximum valid function id. Returns 0 if we 118*bed243d3SAndroid Build Coastguard Worker /// encounter errors (when there are no instrumented functions, etc.). 119*bed243d3SAndroid Build Coastguard Worker extern size_t __xray_max_function_id(); 120*bed243d3SAndroid Build Coastguard Worker 121*bed243d3SAndroid Build Coastguard Worker /// Initialize the required XRay data structures. This is useful in cases where 122*bed243d3SAndroid Build Coastguard Worker /// users want to control precisely when the XRay instrumentation data 123*bed243d3SAndroid Build Coastguard Worker /// structures are initialized, for example when the XRay library is built with 124*bed243d3SAndroid Build Coastguard Worker /// the XRAY_NO_PREINIT preprocessor definition. 125*bed243d3SAndroid Build Coastguard Worker /// 126*bed243d3SAndroid Build Coastguard Worker /// Calling __xray_init() more than once is safe across multiple threads. 127*bed243d3SAndroid Build Coastguard Worker extern void __xray_init(); 128*bed243d3SAndroid Build Coastguard Worker 129*bed243d3SAndroid Build Coastguard Worker } // end extern "C" 130*bed243d3SAndroid Build Coastguard Worker 131*bed243d3SAndroid Build Coastguard Worker #endif // XRAY_XRAY_INTERFACE_H 132