diff --git a/lldb/include/lldb/Utility/Log.h b/lldb/include/lldb/Utility/Log.h index ac6347153a101..1529178cb83b4 100644 --- a/lldb/include/lldb/Utility/Log.h +++ b/lldb/include/lldb/Utility/Log.h @@ -335,6 +335,15 @@ template Log *GetLog(Cat mask) { return LogChannelFor().GetLog(Log::MaskType(mask)); } +/// Getter and setter for the error log (see g_error_log). +/// The error log is set to the system log in SystemInitializerFull. We can't +/// use the system log directly because that would violate the layering between +/// Utility and Host. +/// @{ +void SetLLDBErrorLog(Log *log); +Log *GetLLDBErrorLog(); +/// @} + } // namespace lldb_private /// The LLDB_LOG* macros defined below are the way to emit log messages. @@ -384,6 +393,8 @@ template Log *GetLog(Cat mask) { do { \ ::lldb_private::Log *log_private = (log); \ ::llvm::Error error_private = (error); \ + if (!log_private) \ + log_private = lldb_private::GetLLDBErrorLog(); \ if (log_private && error_private) { \ log_private->FormatError(::std::move(error_private), __FILE__, __func__, \ __VA_ARGS__); \ diff --git a/lldb/source/API/SystemInitializerFull.cpp b/lldb/source/API/SystemInitializerFull.cpp index 8a992a6889a91..31f3a9f30b81f 100644 --- a/lldb/source/API/SystemInitializerFull.cpp +++ b/lldb/source/API/SystemInitializerFull.cpp @@ -84,6 +84,9 @@ llvm::Error SystemInitializerFull::Initialize() { // Use the Debugger's LLDBAssert callback. SetLLDBAssertCallback(Debugger::AssertCallback); + // Use the system log to report errors that would otherwise get dropped. + SetLLDBErrorLog(GetLog(SystemLog::System)); + LLDB_LOG(GetLog(SystemLog::System), "{0}", GetVersion()); return llvm::Error::success(); diff --git a/lldb/source/Utility/Log.cpp b/lldb/source/Utility/Log.cpp index 3798f40647637..4c2c33aaafe00 100644 --- a/lldb/source/Utility/Log.cpp +++ b/lldb/source/Utility/Log.cpp @@ -43,6 +43,10 @@ char TeeLogHandler::ID; llvm::ManagedStatic Log::g_channel_map; +// The error log is used by LLDB_LOG_ERROR. If the given log channel passed to +// LLDB_LOG_ERROR is not enabled, error messages are logged to the error log. +static std::atomic g_error_log = nullptr; + void Log::ForEachCategory( const Log::ChannelMap::value_type &entry, llvm::function_ref lambda) { @@ -460,3 +464,7 @@ void TeeLogHandler::Emit(llvm::StringRef message) { m_first_log_handler->Emit(message); m_second_log_handler->Emit(message); } + +void lldb_private::SetLLDBErrorLog(Log *log) { g_error_log.store(log); } + +Log *lldb_private::GetLLDBErrorLog() { return g_error_log; }