xref: /freebsd/contrib/llvm-project/compiler-rt/include/xray/xray_interface.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===- xray_interface.h -----------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is a part of XRay, a dynamic runtime instrumentation system.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // APIs for controlling XRay functionality explicitly.
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #ifndef XRAY_XRAY_INTERFACE_H
150b57cec5SDimitry Andric #define XRAY_XRAY_INTERFACE_H
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include <cstddef>
180b57cec5SDimitry Andric #include <cstdint>
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric extern "C" {
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric /// Synchronize this with AsmPrinter::SledKind in LLVM.
230b57cec5SDimitry Andric enum XRayEntryType {
240b57cec5SDimitry Andric   ENTRY = 0,
250b57cec5SDimitry Andric   EXIT = 1,
260b57cec5SDimitry Andric   TAIL = 2,
270b57cec5SDimitry Andric   LOG_ARGS_ENTRY = 3,
280b57cec5SDimitry Andric   CUSTOM_EVENT = 4,
290b57cec5SDimitry Andric   TYPED_EVENT = 5,
300b57cec5SDimitry Andric };
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric /// Provide a function to invoke for when instrumentation points are hit. This
330b57cec5SDimitry Andric /// is a user-visible control surface that overrides the default implementation.
340b57cec5SDimitry Andric /// The function provided should take the following arguments:
350b57cec5SDimitry Andric ///
360b57cec5SDimitry Andric ///   - function id: an identifier that indicates the id of a function; this id
370b57cec5SDimitry Andric ///                  is generated by xray; the mapping between the function id
380b57cec5SDimitry Andric ///                  and the actual function pointer is available through
390b57cec5SDimitry Andric ///                  __xray_table.
400b57cec5SDimitry Andric ///   - entry type: identifies what kind of instrumentation point was
410b57cec5SDimitry Andric ///                 encountered (function entry, function exit, etc.). See the
420b57cec5SDimitry Andric ///                 enum XRayEntryType for more details.
430b57cec5SDimitry Andric ///
440b57cec5SDimitry Andric /// The user handler must handle correctly spurious calls after this handler is
450b57cec5SDimitry Andric /// removed or replaced with another handler, because it would be too costly for
460b57cec5SDimitry Andric /// XRay runtime to avoid spurious calls.
470b57cec5SDimitry Andric /// To prevent circular calling, the handler function itself and all its
480b57cec5SDimitry Andric /// direct&indirect callees must not be instrumented with XRay, which can be
490b57cec5SDimitry Andric /// achieved by marking them all with: __attribute__((xray_never_instrument))
500b57cec5SDimitry Andric ///
510b57cec5SDimitry Andric /// Returns 1 on success, 0 on error.
520b57cec5SDimitry Andric extern int __xray_set_handler(void (*entry)(int32_t, XRayEntryType));
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric /// This removes whatever the currently provided handler is. Returns 1 on
550b57cec5SDimitry Andric /// success, 0 on error.
560b57cec5SDimitry Andric extern int __xray_remove_handler();
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric /// Use XRay to log the first argument of each (instrumented) function call.
590b57cec5SDimitry Andric /// When this function exits, all threads will have observed the effect and
600b57cec5SDimitry Andric /// start logging their subsequent affected function calls (if patched).
610b57cec5SDimitry Andric ///
620b57cec5SDimitry Andric /// Returns 1 on success, 0 on error.
630b57cec5SDimitry Andric extern int __xray_set_handler_arg1(void (*entry)(int32_t, XRayEntryType,
640b57cec5SDimitry Andric                                                  uint64_t));
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric /// Disables the XRay handler used to log first arguments of function calls.
670b57cec5SDimitry Andric /// Returns 1 on success, 0 on error.
680b57cec5SDimitry Andric extern int __xray_remove_handler_arg1();
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric /// Provide a function to invoke when XRay encounters a custom event.
710b57cec5SDimitry Andric extern int __xray_set_customevent_handler(void (*entry)(void *, std::size_t));
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric /// This removes whatever the currently provided custom event handler is.
740b57cec5SDimitry Andric /// Returns 1 on success, 0 on error.
750b57cec5SDimitry Andric extern int __xray_remove_customevent_handler();
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric /// Set a handler for xray typed event logging. The first parameter is a type
780b57cec5SDimitry Andric /// identifier, the second is a payload, and the third is the payload size.
79*06c3fb27SDimitry Andric /// NOTE: fdrLoggingHandleTypedEvent only supports uint16_t event type.
80*06c3fb27SDimitry Andric extern int __xray_set_typedevent_handler(void (*entry)(size_t, const void *,
81*06c3fb27SDimitry Andric                                                        size_t));
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric /// Removes the currently set typed event handler.
840b57cec5SDimitry Andric /// Returns 1 on success, 0 on error.
850b57cec5SDimitry Andric extern int __xray_remove_typedevent_handler();
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric extern uint16_t __xray_register_event_type(const char *event_type);
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric enum XRayPatchingStatus {
900b57cec5SDimitry Andric   NOT_INITIALIZED = 0,
910b57cec5SDimitry Andric   SUCCESS = 1,
920b57cec5SDimitry Andric   ONGOING = 2,
930b57cec5SDimitry Andric   FAILED = 3,
940b57cec5SDimitry Andric };
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric /// This tells XRay to patch the instrumentation points. See XRayPatchingStatus
970b57cec5SDimitry Andric /// for possible result values.
980b57cec5SDimitry Andric extern XRayPatchingStatus __xray_patch();
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric /// Reverses the effect of __xray_patch(). See XRayPatchingStatus for possible
1010b57cec5SDimitry Andric /// result values.
1020b57cec5SDimitry Andric extern XRayPatchingStatus __xray_unpatch();
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric /// This patches a specific function id. See XRayPatchingStatus for possible
1050b57cec5SDimitry Andric /// result values.
1060b57cec5SDimitry Andric extern XRayPatchingStatus __xray_patch_function(int32_t FuncId);
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric /// This unpatches a specific function id. See XRayPatchingStatus for possible
1090b57cec5SDimitry Andric /// result values.
1100b57cec5SDimitry Andric extern XRayPatchingStatus __xray_unpatch_function(int32_t FuncId);
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric /// This function returns the address of the function provided a valid function
1130b57cec5SDimitry Andric /// id. We return 0 if we encounter any error, even if 0 may be a valid function
1140b57cec5SDimitry Andric /// address.
1150b57cec5SDimitry Andric extern uintptr_t __xray_function_address(int32_t FuncId);
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric /// This function returns the maximum valid function id. Returns 0 if we
1180b57cec5SDimitry Andric /// encounter errors (when there are no instrumented functions, etc.).
1190b57cec5SDimitry Andric extern size_t __xray_max_function_id();
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric /// Initialize the required XRay data structures. This is useful in cases where
1220b57cec5SDimitry Andric /// users want to control precisely when the XRay instrumentation data
1230b57cec5SDimitry Andric /// structures are initialized, for example when the XRay library is built with
1240b57cec5SDimitry Andric /// the XRAY_NO_PREINIT preprocessor definition.
1250b57cec5SDimitry Andric ///
1260b57cec5SDimitry Andric /// Calling __xray_init() more than once is safe across multiple threads.
1270b57cec5SDimitry Andric extern void __xray_init();
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric } // end extern "C"
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric #endif // XRAY_XRAY_INTERFACE_H
132