[Lldb-commits] [lldb] r316173 - Logging: Make sure logging machinery is in a consistent state after forking

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu Oct 19 10:40:51 PDT 2017


Author: labath
Date: Thu Oct 19 10:40:51 2017
New Revision: 316173

URL: http://llvm.org/viewvc/llvm-project?rev=316173&view=rev
Log:
Logging: Make sure logging machinery is in a consistent state after forking

Summary:
We had a bug where if we had forked (in the ProcessLauncherPosixFork)
while another thread was writing a log message, we would deadlock. This
happened because the fork child inherited the locked log rwmutex, which
would never get unlocked. This meant the child got stuck trying to
disable all log channels.

The bug existed for a while but only started being apparent after
D37930, which started using ThreadLauncher (which uses logging) instead
of std::thread (which does not) for launching TaskPool threads.

The fix is to use pthread_atfork to make sure noone is writing a log
message while we are forking.

Reviewers: zturner, eugene, clayborg

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D38938

Modified:
    lldb/trunk/include/lldb/Utility/Log.h
    lldb/trunk/include/lldb/Utility/Logging.h
    lldb/trunk/source/Initialization/SystemInitializerCommon.cpp
    lldb/trunk/source/Utility/Log.cpp
    lldb/trunk/source/Utility/Logging.cpp

Modified: lldb/trunk/include/lldb/Utility/Log.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/Log.h?rev=316173&r1=316172&r2=316173&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/Log.h (original)
+++ lldb/trunk/include/lldb/Utility/Log.h Thu Oct 19 10:40:51 2017
@@ -96,6 +96,9 @@ public:
     }
   };
 
+
+  static void Initialize();
+
   //------------------------------------------------------------------
   // Static accessors for logging channels
   //------------------------------------------------------------------
@@ -193,6 +196,9 @@ private:
   static uint32_t GetFlags(llvm::raw_ostream &stream, const ChannelMap::value_type &entry,
                            llvm::ArrayRef<const char *> categories);
 
+  static void LockAllChannels();
+  static void UnlockAllChannels();
+
   Log(const Log &) = delete;
   void operator=(const Log &) = delete;
 };

Modified: lldb/trunk/include/lldb/Utility/Logging.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/Logging.h?rev=316173&r1=316172&r2=316173&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/Logging.h (original)
+++ lldb/trunk/include/lldb/Utility/Logging.h Thu Oct 19 10:40:51 2017
@@ -62,7 +62,7 @@ Log *GetLogIfAllCategoriesSet(uint32_t m
 
 Log *GetLogIfAnyCategoriesSet(uint32_t mask);
 
-void InitializeLog();
+void InitializeLldbChannel();
 
 } // namespace lldb_private
 

Modified: lldb/trunk/source/Initialization/SystemInitializerCommon.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Initialization/SystemInitializerCommon.cpp?rev=316173&r1=316172&r2=316173&view=diff
==============================================================================
--- lldb/trunk/source/Initialization/SystemInitializerCommon.cpp (original)
+++ lldb/trunk/source/Initialization/SystemInitializerCommon.cpp Thu Oct 19 10:40:51 2017
@@ -70,7 +70,7 @@ void SystemInitializerCommon::Initialize
 #endif
 
   llvm::EnablePrettyStackTrace();
-  InitializeLog();
+  Log::Initialize();
   HostInfo::Initialize();
   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);

Modified: lldb/trunk/source/Utility/Log.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/Log.cpp?rev=316173&r1=316172&r2=316173&view=diff
==============================================================================
--- lldb/trunk/source/Utility/Log.cpp (original)
+++ lldb/trunk/source/Utility/Log.cpp Thu Oct 19 10:40:51 2017
@@ -32,6 +32,7 @@
 #include <process.h> // for getpid
 #else
 #include <unistd.h>
+#include <pthread.h>
 #endif
 
 using namespace lldb_private;
@@ -181,6 +182,13 @@ void Log::Warning(const char *format, ..
   Printf("warning: %s", Content.c_str());
 }
 
+void Log::Initialize() {
+#ifdef LLVM_ON_UNIX
+  pthread_atfork(&Log::LockAllChannels, &Log::UnlockAllChannels, &Log::UnlockAllChannels);
+#endif
+  InitializeLldbChannel();
+}
+
 void Log::Register(llvm::StringRef name, Channel &channel) {
   auto iter = g_channel_map->try_emplace(name, channel);
   assert(iter.second == true);
@@ -321,3 +329,13 @@ void Log::Format(llvm::StringRef file, l
   message << payload << "\n";
   WriteMessage(message.str());
 }
+
+void Log::LockAllChannels() {
+  for (auto &c: *g_channel_map)
+    c.second.m_mutex.lock();
+}
+
+void Log::UnlockAllChannels() {
+  for (auto &c: *g_channel_map)
+    c.second.m_mutex.unlock();
+}

Modified: lldb/trunk/source/Utility/Logging.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/Logging.cpp?rev=316173&r1=316172&r2=316173&view=diff
==============================================================================
--- lldb/trunk/source/Utility/Logging.cpp (original)
+++ lldb/trunk/source/Utility/Logging.cpp Thu Oct 19 10:40:51 2017
@@ -51,7 +51,7 @@ static constexpr Log::Category g_categor
 
 static Log::Channel g_log_channel(g_categories, LIBLLDB_LOG_DEFAULT);
 
-void lldb_private::InitializeLog() {
+void lldb_private::InitializeLldbChannel() {
   Log::Register("lldb", g_log_channel);
 }
 




More information about the lldb-commits mailing list