labath updated this revision to Diff 119710.
labath added a comment.

The previous fix had a problem where a more paraniod libc (e.g., android, but I
suspect freebds would do that as well) would detect that the
thread unlocking the mutex is not the same one as the one that locked, and
refused to comply.

The new fix is a combination of the first two solutions: we disable the logging
via custom code after forking, but we still do it via pthread_atfork to avoid
this detail leaking outside the Log implementation. The added benefit is that
forking code does not need to worry about logging at all, as it will be
automatically disabled.


https://reviews.llvm.org/D38938

Files:
  include/lldb/Utility/Log.h
  include/lldb/Utility/Logging.h
  source/Host/posix/ProcessLauncherPosixFork.cpp
  source/Initialization/SystemInitializerCommon.cpp
  source/Utility/Log.cpp
  source/Utility/Logging.cpp

Index: source/Utility/Logging.cpp
===================================================================
--- source/Utility/Logging.cpp
+++ source/Utility/Logging.cpp
@@ -51,7 +51,7 @@
 
 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);
 }
 
Index: source/Utility/Log.cpp
===================================================================
--- source/Utility/Log.cpp
+++ source/Utility/Log.cpp
@@ -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 @@
   Printf("warning: %s", Content.c_str());
 }
 
+void Log::Initialize() {
+#ifdef LLVM_ON_UNIX
+  pthread_atfork(nullptr, nullptr, &Log::DisableLoggingChild);
+#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,8 @@
   message << payload << "\n";
   WriteMessage(message.str());
 }
+
+void Log::DisableLoggingChild() {
+  for (auto &c: *g_channel_map)
+    c.second.m_channel.log_ptr.store(nullptr, std::memory_order_relaxed);
+}
Index: source/Initialization/SystemInitializerCommon.cpp
===================================================================
--- source/Initialization/SystemInitializerCommon.cpp
+++ source/Initialization/SystemInitializerCommon.cpp
@@ -70,7 +70,7 @@
 #endif
 
   llvm::EnablePrettyStackTrace();
-  InitializeLog();
+  Log::Initialize();
   HostInfo::Initialize();
   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
Index: source/Host/posix/ProcessLauncherPosixFork.cpp
===================================================================
--- source/Host/posix/ProcessLauncherPosixFork.cpp
+++ source/Host/posix/ProcessLauncherPosixFork.cpp
@@ -95,10 +95,6 @@
 
 static void LLVM_ATTRIBUTE_NORETURN ChildFunc(int error_fd,
                                               const ProcessLaunchInfo &info) {
-  // First, make sure we disable all logging. If we are logging to stdout, our
-  // logs can be mistaken for inferior output.
-  Log::DisableAllLogChannels();
-
   // Do not inherit setgid powers.
   if (setgid(getgid()) != 0)
     ExitWithError(error_fd, "setgid");
Index: include/lldb/Utility/Logging.h
===================================================================
--- include/lldb/Utility/Logging.h
+++ include/lldb/Utility/Logging.h
@@ -62,7 +62,7 @@
 
 Log *GetLogIfAnyCategoriesSet(uint32_t mask);
 
-void InitializeLog();
+void InitializeLldbChannel();
 
 } // namespace lldb_private
 
Index: include/lldb/Utility/Log.h
===================================================================
--- include/lldb/Utility/Log.h
+++ include/lldb/Utility/Log.h
@@ -96,6 +96,9 @@
     }
   };
 
+
+  static void Initialize();
+
   //------------------------------------------------------------------
   // Static accessors for logging channels
   //------------------------------------------------------------------
@@ -193,6 +196,8 @@
   static uint32_t GetFlags(llvm::raw_ostream &stream, const ChannelMap::value_type &entry,
                            llvm::ArrayRef<const char *> categories);
 
+  static void DisableLoggingChild();
+
   Log(const Log &) = delete;
   void operator=(const Log &) = delete;
 };
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to