1 //===-- xray_log_interface.cc ---------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of XRay, a function call tracing system.
12 //===----------------------------------------------------------------------===//
13 #include "xray/xray_log_interface.h"
15 #include "sanitizer_common/sanitizer_allocator_internal.h"
16 #include "sanitizer_common/sanitizer_atomic.h"
17 #include "sanitizer_common/sanitizer_mutex.h"
18 #include "xray/xray_interface.h"
19 #include "xray_defs.h"
22 static SpinMutex XRayImplMutex;
23 static XRayLogImpl CurrentXRayImpl{nullptr, nullptr, nullptr, nullptr};
24 static XRayLogImpl *GlobalXRayImpl = nullptr;
26 // This is the default implementation of a buffer iterator, which always yields
28 XRayBuffer NullBufferIterator(XRayBuffer) XRAY_NEVER_INSTRUMENT {
32 // This is the global function responsible for iterating through given buffers.
33 atomic_uintptr_t XRayBufferIterator{
34 reinterpret_cast<uintptr_t>(&NullBufferIterator)};
36 // We use a linked list of Mode to XRayLogImpl mappings. This is a linked list
37 // when it should be a map because we're avoiding having to depend on C++
38 // standard library data structures at this level of the implementation.
45 static ModeImpl SentinelModeImpl{
46 nullptr, nullptr, {nullptr, nullptr, nullptr, nullptr}};
47 static ModeImpl *ModeImpls = &SentinelModeImpl;
48 static const ModeImpl *CurrentMode = nullptr;
52 using namespace __xray;
54 void __xray_log_set_buffer_iterator(XRayBuffer (*Iterator)(XRayBuffer))
55 XRAY_NEVER_INSTRUMENT {
56 atomic_store(&__xray::XRayBufferIterator,
57 reinterpret_cast<uintptr_t>(Iterator), memory_order_release);
60 void __xray_log_remove_buffer_iterator() XRAY_NEVER_INSTRUMENT {
61 __xray_log_set_buffer_iterator(&NullBufferIterator);
65 __xray_log_register_mode(const char *Mode,
66 XRayLogImpl Impl) XRAY_NEVER_INSTRUMENT {
67 if (Impl.flush_log == nullptr || Impl.handle_arg0 == nullptr ||
68 Impl.log_finalize == nullptr || Impl.log_init == nullptr)
69 return XRayLogRegisterStatus::XRAY_INCOMPLETE_IMPL;
71 SpinMutexLock Guard(&XRayImplMutex);
72 // First, look for whether the mode already has a registered implementation.
73 for (ModeImpl *it = ModeImpls; it != &SentinelModeImpl; it = it->Next) {
74 if (!internal_strcmp(Mode, it->Mode))
75 return XRayLogRegisterStatus::XRAY_DUPLICATE_MODE;
77 auto *NewModeImpl = static_cast<ModeImpl *>(InternalAlloc(sizeof(ModeImpl)));
78 NewModeImpl->Next = ModeImpls;
79 NewModeImpl->Mode = internal_strdup(Mode);
80 NewModeImpl->Impl = Impl;
81 ModeImpls = NewModeImpl;
82 return XRayLogRegisterStatus::XRAY_REGISTRATION_OK;
86 __xray_log_select_mode(const char *Mode) XRAY_NEVER_INSTRUMENT {
87 SpinMutexLock Guard(&XRayImplMutex);
88 for (ModeImpl *it = ModeImpls; it != &SentinelModeImpl; it = it->Next) {
89 if (!internal_strcmp(Mode, it->Mode)) {
91 CurrentXRayImpl = it->Impl;
92 GlobalXRayImpl = &CurrentXRayImpl;
93 __xray_set_handler(it->Impl.handle_arg0);
94 return XRayLogRegisterStatus::XRAY_REGISTRATION_OK;
97 return XRayLogRegisterStatus::XRAY_MODE_NOT_FOUND;
100 const char *__xray_log_get_current_mode() XRAY_NEVER_INSTRUMENT {
101 SpinMutexLock Guard(&XRayImplMutex);
102 if (CurrentMode != nullptr)
103 return CurrentMode->Mode;
107 void __xray_set_log_impl(XRayLogImpl Impl) XRAY_NEVER_INSTRUMENT {
108 if (Impl.log_init == nullptr || Impl.log_finalize == nullptr ||
109 Impl.handle_arg0 == nullptr || Impl.flush_log == nullptr) {
110 SpinMutexLock Guard(&XRayImplMutex);
111 GlobalXRayImpl = nullptr;
112 CurrentMode = nullptr;
113 __xray_remove_handler();
114 __xray_remove_handler_arg1();
118 SpinMutexLock Guard(&XRayImplMutex);
119 CurrentXRayImpl = Impl;
120 GlobalXRayImpl = &CurrentXRayImpl;
121 __xray_set_handler(Impl.handle_arg0);
124 void __xray_remove_log_impl() XRAY_NEVER_INSTRUMENT {
125 SpinMutexLock Guard(&XRayImplMutex);
126 GlobalXRayImpl = nullptr;
127 __xray_remove_handler();
128 __xray_remove_handler_arg1();
131 XRayLogInitStatus __xray_log_init(size_t BufferSize, size_t MaxBuffers,
133 size_t ArgsSize) XRAY_NEVER_INSTRUMENT {
134 SpinMutexLock Guard(&XRayImplMutex);
136 return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
137 return GlobalXRayImpl->log_init(BufferSize, MaxBuffers, Args, ArgsSize);
140 XRayLogInitStatus __xray_log_init_mode(const char *Mode, const char *Config)
141 XRAY_NEVER_INSTRUMENT {
142 SpinMutexLock Guard(&XRayImplMutex);
144 return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
146 if (Config == nullptr)
147 return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
149 // Check first whether the current mode is the same as what we expect.
150 if (CurrentMode == nullptr || internal_strcmp(CurrentMode->Mode, Mode) != 0)
151 return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
153 // Here we do some work to coerce the pointer we're provided, so that
154 // the implementations that still take void* pointers can handle the
155 // data provided in the Config argument.
156 return GlobalXRayImpl->log_init(
157 0, 0, const_cast<void *>(static_cast<const void *>(Config)), 0);
161 __xray_log_init_mode_bin(const char *Mode, const char *Config,
162 size_t ConfigSize) XRAY_NEVER_INSTRUMENT {
163 SpinMutexLock Guard(&XRayImplMutex);
165 return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
167 if (Config == nullptr)
168 return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
170 // Check first whether the current mode is the same as what we expect.
171 if (CurrentMode == nullptr || internal_strcmp(CurrentMode->Mode, Mode) != 0)
172 return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
174 // Here we do some work to coerce the pointer we're provided, so that
175 // the implementations that still take void* pointers can handle the
176 // data provided in the Config argument.
177 return GlobalXRayImpl->log_init(
178 0, 0, const_cast<void *>(static_cast<const void *>(Config)), ConfigSize);
181 XRayLogInitStatus __xray_log_finalize() XRAY_NEVER_INSTRUMENT {
182 SpinMutexLock Guard(&XRayImplMutex);
184 return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
185 return GlobalXRayImpl->log_finalize();
188 XRayLogFlushStatus __xray_log_flushLog() XRAY_NEVER_INSTRUMENT {
189 SpinMutexLock Guard(&XRayImplMutex);
191 return XRayLogFlushStatus::XRAY_LOG_NOT_FLUSHING;
192 return GlobalXRayImpl->flush_log();
195 XRayLogFlushStatus __xray_log_process_buffers(
196 void (*Processor)(const char *, XRayBuffer)) XRAY_NEVER_INSTRUMENT {
197 // We want to make sure that there will be no changes to the global state for
198 // the log by synchronising on the XRayBufferIteratorMutex.
200 return XRayLogFlushStatus::XRAY_LOG_NOT_FLUSHING;
201 auto Iterator = reinterpret_cast<XRayBuffer (*)(XRayBuffer)>(
202 atomic_load(&XRayBufferIterator, memory_order_acquire));
203 auto Buffer = (*Iterator)(XRayBuffer{nullptr, 0});
204 auto Mode = CurrentMode ? CurrentMode->Mode : nullptr;
205 while (Buffer.Data != nullptr) {
206 (*Processor)(Mode, Buffer);
207 Buffer = (*Iterator)(Buffer);
209 return XRayLogFlushStatus::XRAY_LOG_FLUSHED;