Index: compiler-rt/trunk/include/xray/xray_log_interface.h =================================================================== --- compiler-rt/trunk/include/xray/xray_log_interface.h +++ compiler-rt/trunk/include/xray/xray_log_interface.h @@ -128,6 +128,16 @@ XRAY_LOG_FLUSHED = 2, }; +/// This enum indicates the installation state of a logging implementation, when +/// associating a mode to a particular logging implementation through +/// `__xray_log_register_impl(...)` or through `__xray_log_select_mode(...`. +enum XRayLogRegisterStatus { + XRAY_REGISTRATION_OK = 0, + XRAY_DUPLICATE_MODE = 1, + XRAY_MODE_NOT_FOUND = 2, + XRAY_INCOMPLETE_IMPL = 3, +}; + /// A valid XRay logging implementation MUST provide all of the function /// pointers in XRayLogImpl when being installed through `__xray_set_log_impl`. /// To be precise, ALL the functions pointers MUST NOT be nullptr. @@ -189,6 +199,34 @@ /// called while in any other states. void __xray_set_log_impl(XRayLogImpl Impl); +/// This function registers a logging implementation against a "mode" +/// identifier. This allows multiple modes to be registered, and chosen at +/// runtime using the same mode identifier through +/// `__xray_log_select_mode(...)`. +/// +/// We treat the Mode identifier as a null-terminated byte string, as the +/// identifier used when retrieving the log impl. +/// +/// Returns: +/// - XRAY_REGISTRATION_OK on success. +/// - XRAY_DUPLICATE_MODE when an implementation is already associated with +/// the provided Mode; does not update the already-registered +/// implementation. +XRayLogRegisterStatus __xray_log_register_mode(const char *Mode, + XRayLogImpl Impl); + +/// This function selects the implementation associated with Mode that has been +/// registered through __xray_log_register_mode(...) and installs that +/// implementation (as if through calling __xray_set_log_impl(...)). The same +/// caveats apply to __xray_log_select_mode(...) as with +/// __xray_log_set_log_impl(...). +/// +/// Returns: +/// - XRAY_REGISTRATION_OK on success. +/// - XRAY_MODE_NOT_FOUND if there is no implementation associated with Mode; +/// does not update the currently installed implementation. +XRayLogRegisterStatus __xray_log_select_mode(const char *Mode); + /// This function removes the currently installed implementation. It will also /// uninstall any handlers that have been previously installed. It does NOT /// unpatch the instrumentation sleds. Index: compiler-rt/trunk/lib/xray/xray_fdr_logging.cc =================================================================== --- compiler-rt/trunk/lib/xray/xray_fdr_logging.cc +++ compiler-rt/trunk/lib/xray/xray_fdr_logging.cc @@ -362,16 +362,25 @@ return XRayLogInitStatus::XRAY_LOG_INITIALIZED; } -} // namespace __xray - -static auto UNUSED Unused = [] { +bool fdrLogDynamicInitializer() XRAY_NEVER_INSTRUMENT { using namespace __xray; - if (flags()->xray_fdr_log) { - XRayLogImpl Impl{ - fdrLoggingInit, fdrLoggingFinalize, fdrLoggingHandleArg0, - fdrLoggingFlush, - }; + XRayLogImpl Impl{ + fdrLoggingInit, + fdrLoggingFinalize, + fdrLoggingHandleArg0, + fdrLoggingFlush, + }; + auto RegistrationResult = __xray_log_register_mode("xray-fdr", Impl); + if (RegistrationResult != XRayLogRegisterStatus::XRAY_REGISTRATION_OK && + __sanitizer::Verbosity()) + Report("Cannot register XRay FDR mode to 'xray-fdr'; error = %d\n", + RegistrationResult); + if (flags()->xray_fdr_log || + !__sanitizer::internal_strcmp(flags()->xray_mode, "xray-fdr")) __xray_set_log_impl(Impl); - } return true; -}(); +} + +} // namespace __xray + +static auto UNUSED Unused = __xray::fdrLogDynamicInitializer(); Index: compiler-rt/trunk/lib/xray/xray_flags.inc =================================================================== --- compiler-rt/trunk/lib/xray/xray_flags.inc +++ compiler-rt/trunk/lib/xray/xray_flags.inc @@ -18,10 +18,12 @@ "Whether to patch instrumentation points before main.") XRAY_FLAG(const char *, xray_logfile_base, "xray-log.", "Filename base for the xray logfile.") +XRAY_FLAG(const char *, xray_mode, "", "Mode to install by default.") + // Basic (Naive) Mode logging options. XRAY_FLAG(bool, xray_naive_log, false, - "Whether to install the naive log implementation.") + "DEPRECATED: Use xray_mode=xray-basic instead.") XRAY_FLAG(int, xray_naive_log_func_duration_threshold_us, 5, "Naive logging will try to skip functions that execute for fewer " "microseconds than this threshold.") @@ -33,7 +35,7 @@ // FDR (Flight Data Recorder) Mode logging options. XRAY_FLAG(bool, xray_fdr_log, false, - "Whether to install the flight data recorder logging implementation.") + "DEPRECATED: Use xray_mode=xray-fdr instead.") XRAY_FLAG(int, xray_fdr_log_func_duration_threshold_us, 5, "FDR logging will try to skip functions that execute for fewer " "microseconds than this threshold.") Index: compiler-rt/trunk/lib/xray/xray_inmemory_log.cc =================================================================== --- compiler-rt/trunk/lib/xray/xray_inmemory_log.cc +++ compiler-rt/trunk/lib/xray/xray_inmemory_log.cc @@ -334,13 +334,19 @@ } bool basicLogDynamicInitializer() XRAY_NEVER_INSTRUMENT { - if (flags()->xray_naive_log) { - XRayLogImpl Impl{ - basicLoggingInit, - basicLoggingFinalize, - basicLoggingHandleArg0Empty, - basicLoggingFlush, - }; + XRayLogImpl Impl{ + basicLoggingInit, + basicLoggingFinalize, + basicLoggingHandleArg0Empty, + basicLoggingFlush, + }; + auto RegistrationResult = __xray_log_register_mode("xray-basic", Impl); + if (RegistrationResult != XRayLogRegisterStatus::XRAY_REGISTRATION_OK && + __sanitizer::Verbosity()) + Report("Cannot register XRay Basic Mode to 'xray-basic'; error = %d\n", + RegistrationResult); + if (flags()->xray_naive_log || + !__sanitizer::internal_strcmp(flags()->xray_mode, "xray-basic")) { __xray_set_log_impl(Impl); BasicLoggingOptions Options; Options.DurationFilterMicros = Index: compiler-rt/trunk/lib/xray/xray_log_interface.cc =================================================================== --- compiler-rt/trunk/lib/xray/xray_log_interface.cc +++ compiler-rt/trunk/lib/xray/xray_log_interface.cc @@ -12,19 +12,69 @@ //===----------------------------------------------------------------------===// #include "xray/xray_log_interface.h" +#include "sanitizer_common/sanitizer_allocator_internal.h" #include "sanitizer_common/sanitizer_atomic.h" #include "sanitizer_common/sanitizer_mutex.h" #include "xray/xray_interface.h" #include "xray_defs.h" __sanitizer::SpinMutex XRayImplMutex; +XRayLogImpl CurrentXRayImpl{nullptr, nullptr, nullptr, nullptr}; XRayLogImpl *GlobalXRayImpl = nullptr; +// We use a linked list of Mode to XRayLogImpl mappings. This is a linked list +// when it should be a map because we're avoiding having to depend on C++ +// standard library data structures at this level of the implementation. +struct ModeImpl { + ModeImpl *Next; + const char *Mode; + XRayLogImpl Impl; +}; + +ModeImpl SentinelModeImpl{ + nullptr, nullptr, {nullptr, nullptr, nullptr, nullptr}}; +ModeImpl *ModeImpls = &SentinelModeImpl; + +XRayLogRegisterStatus +__xray_log_register_mode(const char *Mode, + XRayLogImpl Impl) XRAY_NEVER_INSTRUMENT { + if (Impl.flush_log == nullptr || Impl.handle_arg0 == nullptr || + Impl.log_finalize == nullptr || Impl.log_init == nullptr) + return XRayLogRegisterStatus::XRAY_INCOMPLETE_IMPL; + + __sanitizer::SpinMutexLock Guard(&XRayImplMutex); + // First, look for whether the mode already has a registered implementation. + for (ModeImpl *it = ModeImpls; it != &SentinelModeImpl; it = it->Next) { + if (!__sanitizer::internal_strcmp(Mode, it->Mode)) + return XRayLogRegisterStatus::XRAY_DUPLICATE_MODE; + } + auto *NewModeImpl = + static_cast(__sanitizer::InternalAlloc(sizeof(ModeImpl))); + NewModeImpl->Next = ModeImpls; + NewModeImpl->Mode = __sanitizer::internal_strdup(Mode); + NewModeImpl->Impl = Impl; + ModeImpls = NewModeImpl; + return XRayLogRegisterStatus::XRAY_REGISTRATION_OK; +} + +XRayLogRegisterStatus +__xray_log_select_mode(const char *Mode) XRAY_NEVER_INSTRUMENT { + __sanitizer::SpinMutexLock Guard(&XRayImplMutex); + for (ModeImpl *it = ModeImpls; it != &SentinelModeImpl; it = it->Next) { + if (!__sanitizer::internal_strcmp(Mode, it->Mode)) { + CurrentXRayImpl = it->Impl; + GlobalXRayImpl = &CurrentXRayImpl; + __xray_set_handler(it->Impl.handle_arg0); + return XRayLogRegisterStatus::XRAY_REGISTRATION_OK; + } + } + return XRayLogRegisterStatus::XRAY_MODE_NOT_FOUND; +} + void __xray_set_log_impl(XRayLogImpl Impl) XRAY_NEVER_INSTRUMENT { if (Impl.log_init == nullptr || Impl.log_finalize == nullptr || Impl.handle_arg0 == nullptr || Impl.flush_log == nullptr) { __sanitizer::SpinMutexLock Guard(&XRayImplMutex); - delete GlobalXRayImpl; GlobalXRayImpl = nullptr; __xray_remove_handler(); __xray_remove_handler_arg1(); @@ -32,14 +82,13 @@ } __sanitizer::SpinMutexLock Guard(&XRayImplMutex); - GlobalXRayImpl = new XRayLogImpl(); - *GlobalXRayImpl = Impl; + CurrentXRayImpl = Impl; + GlobalXRayImpl = &CurrentXRayImpl; __xray_set_handler(Impl.handle_arg0); } void __xray_remove_log_impl() XRAY_NEVER_INSTRUMENT { __sanitizer::SpinMutexLock Guard(&XRayImplMutex); - delete GlobalXRayImpl; GlobalXRayImpl = nullptr; __xray_remove_handler(); __xray_remove_handler_arg1(); Index: compiler-rt/trunk/test/xray/TestCases/Linux/logging-modes.cc =================================================================== --- compiler-rt/trunk/test/xray/TestCases/Linux/logging-modes.cc +++ compiler-rt/trunk/test/xray/TestCases/Linux/logging-modes.cc @@ -0,0 +1,59 @@ +// Check that we can install an implementation associated with a mode. +// +// RUN: %clangxx_xray -std=c++11 %s -o %t +// RUN: %run %t | FileCheck %s +// +// UNSUPPORTED: target-is-mips64,target-is-mips64el + +#include "xray/xray_interface.h" +#include "xray/xray_log_interface.h" +#include +#include + +[[clang::xray_never_instrument]] void printing_handler(int32_t fid, + XRayEntryType) { + thread_local volatile bool printing = false; + if (printing) + return; + printing = true; + std::printf("printing %d\n", fid); + printing = false; +} + +[[clang::xray_never_instrument]] XRayLogInitStatus +printing_init(size_t, size_t, void *, size_t) { + return XRayLogInitStatus::XRAY_LOG_INITIALIZED; +} + +[[clang::xray_never_instrument]] XRayLogInitStatus printing_finalize() { + return XRayLogInitStatus::XRAY_LOG_FINALIZED; +} + +[[clang::xray_never_instrument]] XRayLogFlushStatus printing_flush_log() { + return XRayLogFlushStatus::XRAY_LOG_FLUSHED; +} + +[[clang::xray_always_instrument]] void callme() { std::printf("called me!\n"); } + +static bool unused = [] { + assert(__xray_log_register_mode("custom", + {printing_init, printing_finalize, + printing_handler, printing_flush_log}) == + XRayLogRegisterStatus::XRAY_REGISTRATION_OK); + return true; +}(); + +int main(int argc, char **argv) { + assert(__xray_log_select_mode("custom") == + XRayLogRegisterStatus::XRAY_REGISTRATION_OK); + assert(__xray_patch() == XRayPatchingStatus::SUCCESS); + assert(__xray_log_init(0, 0, nullptr, 0) == + XRayLogInitStatus::XRAY_LOG_INITIALIZED); + // CHECK: printing {{.*}} + callme(); // CHECK: called me! + // CHECK: printing {{.*}} + assert(__xray_log_finalize() == XRayLogInitStatus::XRAY_LOG_FINALIZED); + assert(__xray_log_flushLog() == XRayLogFlushStatus::XRAY_LOG_FLUSHED); + assert(__xray_log_select_mode("not-found") == + XRayLogRegisterStatus::XRAY_MODE_NOT_FOUND); +}