[Lldb-commits] [lldb] cd4b33c - [lldb] Log errors to the system log if they would otherwise get dropped (#111911)

via lldb-commits lldb-commits at lists.llvm.org
Tue Oct 22 09:00:12 PDT 2024


Author: Jonas Devlieghere
Date: 2024-10-22T09:00:08-07:00
New Revision: cd4b33c9a976543171bb7b3455c485f99cfe654b

URL: https://github.com/llvm/llvm-project/commit/cd4b33c9a976543171bb7b3455c485f99cfe654b
DIFF: https://github.com/llvm/llvm-project/commit/cd4b33c9a976543171bb7b3455c485f99cfe654b.diff

LOG: [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.

Added: 
    

Modified: 
    lldb/include/lldb/Utility/Log.h
    lldb/source/API/SystemInitializerFull.cpp
    lldb/source/Utility/Log.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Utility/Log.h b/lldb/include/lldb/Utility/Log.h
index ac6347153a1014..1529178cb83b4b 100644
--- a/lldb/include/lldb/Utility/Log.h
+++ b/lldb/include/lldb/Utility/Log.h
@@ -335,6 +335,15 @@ template <typename Cat> Log *GetLog(Cat mask) {
   return LogChannelFor<Cat>().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 <typename Cat> 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 8a992a6889a91b..31f3a9f30b81f0 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 3798f406476370..4c2c33aaafe008 100644
--- a/lldb/source/Utility/Log.cpp
+++ b/lldb/source/Utility/Log.cpp
@@ -43,6 +43,10 @@ char TeeLogHandler::ID;
 
 llvm::ManagedStatic<Log::ChannelMap> 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<Log *> g_error_log = nullptr;
+
 void Log::ForEachCategory(
     const Log::ChannelMap::value_type &entry,
     llvm::function_ref<void(llvm::StringRef, llvm::StringRef)> 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; }


        


More information about the lldb-commits mailing list