Path: blob/main/contrib/llvm-project/compiler-rt/lib/xray/xray_log_interface.cpp
35265 views
//===-- xray_log_interface.cpp --------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of XRay, a function call tracing system.9//10//===----------------------------------------------------------------------===//11#include "xray/xray_log_interface.h"1213#include "sanitizer_common/sanitizer_allocator_internal.h"14#include "sanitizer_common/sanitizer_atomic.h"15#include "sanitizer_common/sanitizer_mutex.h"16#include "xray/xray_interface.h"17#include "xray_defs.h"1819namespace __xray {20static SpinMutex XRayImplMutex;21static XRayLogImpl CurrentXRayImpl{nullptr, nullptr, nullptr, nullptr};22static XRayLogImpl *GlobalXRayImpl = nullptr;2324// This is the default implementation of a buffer iterator, which always yields25// a null buffer.26XRayBuffer NullBufferIterator(XRayBuffer) XRAY_NEVER_INSTRUMENT {27return {nullptr, 0};28}2930// This is the global function responsible for iterating through given buffers.31atomic_uintptr_t XRayBufferIterator{32reinterpret_cast<uintptr_t>(&NullBufferIterator)};3334// We use a linked list of Mode to XRayLogImpl mappings. This is a linked list35// when it should be a map because we're avoiding having to depend on C++36// standard library data structures at this level of the implementation.37struct ModeImpl {38ModeImpl *Next;39const char *Mode;40XRayLogImpl Impl;41};4243static ModeImpl SentinelModeImpl{44nullptr, nullptr, {nullptr, nullptr, nullptr, nullptr}};45static ModeImpl *ModeImpls = &SentinelModeImpl;46static const ModeImpl *CurrentMode = nullptr;4748} // namespace __xray4950using namespace __xray;5152void __xray_log_set_buffer_iterator(XRayBuffer (*Iterator)(XRayBuffer))53XRAY_NEVER_INSTRUMENT {54atomic_store(&__xray::XRayBufferIterator,55reinterpret_cast<uintptr_t>(Iterator), memory_order_release);56}5758void __xray_log_remove_buffer_iterator() XRAY_NEVER_INSTRUMENT {59__xray_log_set_buffer_iterator(&NullBufferIterator);60}6162XRayLogRegisterStatus63__xray_log_register_mode(const char *Mode,64XRayLogImpl Impl) XRAY_NEVER_INSTRUMENT {65if (Impl.flush_log == nullptr || Impl.handle_arg0 == nullptr ||66Impl.log_finalize == nullptr || Impl.log_init == nullptr)67return XRayLogRegisterStatus::XRAY_INCOMPLETE_IMPL;6869SpinMutexLock Guard(&XRayImplMutex);70// First, look for whether the mode already has a registered implementation.71for (ModeImpl *it = ModeImpls; it != &SentinelModeImpl; it = it->Next) {72if (!internal_strcmp(Mode, it->Mode))73return XRayLogRegisterStatus::XRAY_DUPLICATE_MODE;74}75auto *NewModeImpl = static_cast<ModeImpl *>(InternalAlloc(sizeof(ModeImpl)));76NewModeImpl->Next = ModeImpls;77NewModeImpl->Mode = internal_strdup(Mode);78NewModeImpl->Impl = Impl;79ModeImpls = NewModeImpl;80return XRayLogRegisterStatus::XRAY_REGISTRATION_OK;81}8283XRayLogRegisterStatus84__xray_log_select_mode(const char *Mode) XRAY_NEVER_INSTRUMENT {85SpinMutexLock Guard(&XRayImplMutex);86for (ModeImpl *it = ModeImpls; it != &SentinelModeImpl; it = it->Next) {87if (!internal_strcmp(Mode, it->Mode)) {88CurrentMode = it;89CurrentXRayImpl = it->Impl;90GlobalXRayImpl = &CurrentXRayImpl;91__xray_set_handler(it->Impl.handle_arg0);92return XRayLogRegisterStatus::XRAY_REGISTRATION_OK;93}94}95return XRayLogRegisterStatus::XRAY_MODE_NOT_FOUND;96}9798const char *__xray_log_get_current_mode() XRAY_NEVER_INSTRUMENT {99SpinMutexLock Guard(&XRayImplMutex);100if (CurrentMode != nullptr)101return CurrentMode->Mode;102return nullptr;103}104105void __xray_set_log_impl(XRayLogImpl Impl) XRAY_NEVER_INSTRUMENT {106if (Impl.log_init == nullptr || Impl.log_finalize == nullptr ||107Impl.handle_arg0 == nullptr || Impl.flush_log == nullptr) {108SpinMutexLock Guard(&XRayImplMutex);109GlobalXRayImpl = nullptr;110CurrentMode = nullptr;111__xray_remove_handler();112__xray_remove_handler_arg1();113return;114}115116SpinMutexLock Guard(&XRayImplMutex);117CurrentXRayImpl = Impl;118GlobalXRayImpl = &CurrentXRayImpl;119__xray_set_handler(Impl.handle_arg0);120}121122void __xray_remove_log_impl() XRAY_NEVER_INSTRUMENT {123SpinMutexLock Guard(&XRayImplMutex);124GlobalXRayImpl = nullptr;125__xray_remove_handler();126__xray_remove_handler_arg1();127}128129XRayLogInitStatus __xray_log_init(size_t BufferSize, size_t MaxBuffers,130void *Args,131size_t ArgsSize) XRAY_NEVER_INSTRUMENT {132SpinMutexLock Guard(&XRayImplMutex);133if (!GlobalXRayImpl)134return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;135return GlobalXRayImpl->log_init(BufferSize, MaxBuffers, Args, ArgsSize);136}137138XRayLogInitStatus __xray_log_init_mode(const char *Mode, const char *Config)139XRAY_NEVER_INSTRUMENT {140SpinMutexLock Guard(&XRayImplMutex);141if (!GlobalXRayImpl)142return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;143144if (Config == nullptr)145return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;146147// Check first whether the current mode is the same as what we expect.148if (CurrentMode == nullptr || internal_strcmp(CurrentMode->Mode, Mode) != 0)149return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;150151// Here we do some work to coerce the pointer we're provided, so that152// the implementations that still take void* pointers can handle the153// data provided in the Config argument.154return GlobalXRayImpl->log_init(1550, 0, const_cast<void *>(static_cast<const void *>(Config)), 0);156}157158XRayLogInitStatus159__xray_log_init_mode_bin(const char *Mode, const char *Config,160size_t ConfigSize) XRAY_NEVER_INSTRUMENT {161SpinMutexLock Guard(&XRayImplMutex);162if (!GlobalXRayImpl)163return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;164165if (Config == nullptr)166return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;167168// Check first whether the current mode is the same as what we expect.169if (CurrentMode == nullptr || internal_strcmp(CurrentMode->Mode, Mode) != 0)170return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;171172// Here we do some work to coerce the pointer we're provided, so that173// the implementations that still take void* pointers can handle the174// data provided in the Config argument.175return GlobalXRayImpl->log_init(1760, 0, const_cast<void *>(static_cast<const void *>(Config)), ConfigSize);177}178179XRayLogInitStatus __xray_log_finalize() XRAY_NEVER_INSTRUMENT {180SpinMutexLock Guard(&XRayImplMutex);181if (!GlobalXRayImpl)182return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;183return GlobalXRayImpl->log_finalize();184}185186XRayLogFlushStatus __xray_log_flushLog() XRAY_NEVER_INSTRUMENT {187SpinMutexLock Guard(&XRayImplMutex);188if (!GlobalXRayImpl)189return XRayLogFlushStatus::XRAY_LOG_NOT_FLUSHING;190return GlobalXRayImpl->flush_log();191}192193XRayLogFlushStatus __xray_log_process_buffers(194void (*Processor)(const char *, XRayBuffer)) XRAY_NEVER_INSTRUMENT {195// We want to make sure that there will be no changes to the global state for196// the log by synchronising on the XRayBufferIteratorMutex.197if (!GlobalXRayImpl)198return XRayLogFlushStatus::XRAY_LOG_NOT_FLUSHING;199auto Iterator = reinterpret_cast<XRayBuffer (*)(XRayBuffer)>(200atomic_load(&XRayBufferIterator, memory_order_acquire));201auto Buffer = (*Iterator)(XRayBuffer{nullptr, 0});202auto Mode = CurrentMode ? CurrentMode->Mode : nullptr;203while (Buffer.Data != nullptr) {204(*Processor)(Mode, Buffer);205Buffer = (*Iterator)(Buffer);206}207return XRayLogFlushStatus::XRAY_LOG_FLUSHED;208}209210211