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