diff options
Diffstat (limited to 'source/Utility/Log.cpp')
-rw-r--r-- | source/Utility/Log.cpp | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/source/Utility/Log.cpp b/source/Utility/Log.cpp index a80b106838bc..f247124f8d63 100644 --- a/source/Utility/Log.cpp +++ b/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 @@ void Log::Warning(const char *format, ...) { 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); @@ -279,8 +287,7 @@ void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file, if (options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) { llvm::SmallString<32> thread_name; llvm::get_thread_name(thread_name); - if (!thread_name.empty()) - OS << thread_name; + OS << llvm::formatv("{0,-16} ", thread_name); } if (options.Test(LLDB_LOG_OPTION_BACKTRACE)) @@ -321,3 +328,11 @@ void Log::Format(llvm::StringRef file, llvm::StringRef function, message << payload << "\n"; WriteMessage(message.str()); } + +void Log::DisableLoggingChild() { + // Disable logging by clearing out the atomic variable after forking -- if we + // forked while another thread held the channel mutex, we would deadlock when + // trying to write to the log. + for (auto &c: *g_channel_map) + c.second.m_channel.log_ptr.store(nullptr, std::memory_order_relaxed); +} |