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

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Tue Oct 15 16:59:13 PDT 2024


https://github.com/JDevlieghere updated https://github.com/llvm/llvm-project/pull/111911

>From 55eab57c695d4be99305d2b6cee0c4f44261a473 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Thu, 10 Oct 2024 14:39:44 -0700
Subject: [PATCH 1/4] [lldb] Log errors to the system log if they would
 otherwise get dropped

Log errors to the (always-on) system log if they would otherwise get
dropped by LLDB_LOG_ERROR and LLDB_LOG_ERRORV.
---
 lldb/include/lldb/Utility/Log.h           | 15 +++++++++++++++
 lldb/source/API/SystemInitializerFull.cpp |  3 +++
 lldb/source/Utility/Log.cpp               |  8 ++++++++
 3 files changed, 26 insertions(+)

diff --git a/lldb/include/lldb/Utility/Log.h b/lldb/include/lldb/Utility/Log.h
index ac6347153a1014..f8545cd59a18ba 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.
@@ -387,6 +396,9 @@ template <typename Cat> Log *GetLog(Cat mask) {
     if (log_private && error_private) {                                        \
       log_private->FormatError(::std::move(error_private), __FILE__, __func__, \
                                __VA_ARGS__);                                   \
+    } else if (::lldb_private::Log *log_error = GetLLDBErrorLog()) {           \
+      log_error->FormatError(::std::move(error_private), __FILE__, __func__,   \
+                             __VA_ARGS__);                                     \
     } else                                                                     \
       ::llvm::consumeError(::std::move(error_private));                        \
   } while (0)
@@ -401,6 +413,9 @@ template <typename Cat> Log *GetLog(Cat mask) {
     if (log_private && log_private->GetVerbose() && error_private) {           \
       log_private->FormatError(::std::move(error_private), __FILE__, __func__, \
                                __VA_ARGS__);                                   \
+    } else if (::lldb_private::Log *log_error = GetLLDBErrorLog()) {           \
+      log_error->FormatError(::std::move(error_private), __FILE__, __func__,   \
+                             __VA_ARGS__);                                     \
     } else                                                                     \
       ::llvm::consumeError(::std::move(error_private));                        \
   } while (0)
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..a57d415be033ba 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 and LLDB_LOG_ERRORV. If the given
+// log channel 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.exchange(log); }
+
+Log *lldb_private::GetLLDBErrorLog() { return g_error_log; }

>From 99b70b38050344115c8a6b17929cfd8c93b55571 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Sun, 13 Oct 2024 09:01:08 -0700
Subject: [PATCH 2/4] Address Pavel's feedback

---
 lldb/include/lldb/Utility/Log.h | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/lldb/include/lldb/Utility/Log.h b/lldb/include/lldb/Utility/Log.h
index f8545cd59a18ba..1529178cb83b4b 100644
--- a/lldb/include/lldb/Utility/Log.h
+++ b/lldb/include/lldb/Utility/Log.h
@@ -393,12 +393,11 @@ Log *GetLLDBErrorLog();
   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__);                                   \
-    } else if (::lldb_private::Log *log_error = GetLLDBErrorLog()) {           \
-      log_error->FormatError(::std::move(error_private), __FILE__, __func__,   \
-                             __VA_ARGS__);                                     \
     } else                                                                     \
       ::llvm::consumeError(::std::move(error_private));                        \
   } while (0)
@@ -413,9 +412,6 @@ Log *GetLLDBErrorLog();
     if (log_private && log_private->GetVerbose() && error_private) {           \
       log_private->FormatError(::std::move(error_private), __FILE__, __func__, \
                                __VA_ARGS__);                                   \
-    } else if (::lldb_private::Log *log_error = GetLLDBErrorLog()) {           \
-      log_error->FormatError(::std::move(error_private), __FILE__, __func__,   \
-                             __VA_ARGS__);                                     \
     } else                                                                     \
       ::llvm::consumeError(::std::move(error_private));                        \
   } while (0)

>From 28174fafc09eb45413aa6bf78ec74a0be541fecb Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Sun, 13 Oct 2024 09:03:31 -0700
Subject: [PATCH 3/4] Store instead of exchange

Co-authored-by: Pavel Labath <pavel at labath.sk>
---
 lldb/source/Utility/Log.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lldb/source/Utility/Log.cpp b/lldb/source/Utility/Log.cpp
index a57d415be033ba..01bf04eb84240a 100644
--- a/lldb/source/Utility/Log.cpp
+++ b/lldb/source/Utility/Log.cpp
@@ -465,6 +465,6 @@ void TeeLogHandler::Emit(llvm::StringRef message) {
   m_second_log_handler->Emit(message);
 }
 
-void lldb_private::SetLLDBErrorLog(Log *log) { g_error_log.exchange(log); }
+void lldb_private::SetLLDBErrorLog(Log *log) { g_error_log.store(log); }
 
 Log *lldb_private::GetLLDBErrorLog() { return g_error_log; }

>From 7e560111239fab77b6745b39cf249ea287cdfc54 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Tue, 15 Oct 2024 16:58:25 -0700
Subject: [PATCH 4/4] Update comment

---
 lldb/source/Utility/Log.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lldb/source/Utility/Log.cpp b/lldb/source/Utility/Log.cpp
index 01bf04eb84240a..4c2c33aaafe008 100644
--- a/lldb/source/Utility/Log.cpp
+++ b/lldb/source/Utility/Log.cpp
@@ -43,8 +43,8 @@ char TeeLogHandler::ID;
 
 llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map;
 
-// The error log is used by LLDB_LOG_ERROR and LLDB_LOG_ERRORV. If the given
-// log channel is not enabled, error messages are logged to the error log.
+// 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(



More information about the lldb-commits mailing list