Skip to content

Commit cd4b33c

Browse files
authored
[lldb] Log errors to the system log if they would otherwise get dropped (#111911)
Log errors to the (always-on) system log if they would otherwise get dropped by LLDB_LOG_ERROR.
1 parent db21bd4 commit cd4b33c

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

lldb/include/lldb/Utility/Log.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,15 @@ template <typename Cat> Log *GetLog(Cat mask) {
335335
return LogChannelFor<Cat>().GetLog(Log::MaskType(mask));
336336
}
337337

338+
/// Getter and setter for the error log (see g_error_log).
339+
/// The error log is set to the system log in SystemInitializerFull. We can't
340+
/// use the system log directly because that would violate the layering between
341+
/// Utility and Host.
342+
/// @{
343+
void SetLLDBErrorLog(Log *log);
344+
Log *GetLLDBErrorLog();
345+
/// @}
346+
338347
} // namespace lldb_private
339348

340349
/// The LLDB_LOG* macros defined below are the way to emit log messages.
@@ -384,6 +393,8 @@ template <typename Cat> Log *GetLog(Cat mask) {
384393
do { \
385394
::lldb_private::Log *log_private = (log); \
386395
::llvm::Error error_private = (error); \
396+
if (!log_private) \
397+
log_private = lldb_private::GetLLDBErrorLog(); \
387398
if (log_private && error_private) { \
388399
log_private->FormatError(::std::move(error_private), __FILE__, __func__, \
389400
__VA_ARGS__); \

lldb/source/API/SystemInitializerFull.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ llvm::Error SystemInitializerFull::Initialize() {
8484
// Use the Debugger's LLDBAssert callback.
8585
SetLLDBAssertCallback(Debugger::AssertCallback);
8686

87+
// Use the system log to report errors that would otherwise get dropped.
88+
SetLLDBErrorLog(GetLog(SystemLog::System));
89+
8790
LLDB_LOG(GetLog(SystemLog::System), "{0}", GetVersion());
8891

8992
return llvm::Error::success();

lldb/source/Utility/Log.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ char TeeLogHandler::ID;
4343

4444
llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map;
4545

46+
// The error log is used by LLDB_LOG_ERROR. If the given log channel passed to
47+
// LLDB_LOG_ERROR is not enabled, error messages are logged to the error log.
48+
static std::atomic<Log *> g_error_log = nullptr;
49+
4650
void Log::ForEachCategory(
4751
const Log::ChannelMap::value_type &entry,
4852
llvm::function_ref<void(llvm::StringRef, llvm::StringRef)> lambda) {
@@ -460,3 +464,7 @@ void TeeLogHandler::Emit(llvm::StringRef message) {
460464
m_first_log_handler->Emit(message);
461465
m_second_log_handler->Emit(message);
462466
}
467+
468+
void lldb_private::SetLLDBErrorLog(Log *log) { g_error_log.store(log); }
469+
470+
Log *lldb_private::GetLLDBErrorLog() { return g_error_log; }

0 commit comments

Comments
 (0)