diff --git a/lldb/include/lldb/Host/Host.h b/lldb/include/lldb/Host/Host.h --- a/lldb/include/lldb/Host/Host.h +++ b/lldb/include/lldb/Host/Host.h @@ -13,6 +13,7 @@ #include "lldb/Host/HostThread.h" #include "lldb/Utility/Environment.h" #include "lldb/Utility/FileSpec.h" +#include "lldb/Utility/Log.h" #include "lldb/Utility/Timeout.h" #include "lldb/lldb-private-forward.h" #include "lldb/lldb-private.h" @@ -86,6 +87,9 @@ StartMonitoringChildProcess(const MonitorChildProcessCallback &callback, lldb::pid_t pid); + /// Emit the given message to the operating system log. + static void SystemLog(llvm::StringRef message); + /// Get the process ID for the calling process. /// /// \return @@ -252,6 +256,13 @@ ProcessInstanceInfoList &proc_infos); }; +/// Log handler that emits log messages to the operating system log. +class SystemLogHandler : public LogHandler { +public: + SystemLogHandler(); + void Emit(llvm::StringRef message) override; +}; + } // namespace lldb_private namespace llvm { diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp --- a/lldb/source/Host/common/Host.cpp +++ b/lldb/source/Host/common/Host.cpp @@ -106,6 +106,10 @@ }); } +#if !defined(__APPLE__) +void Host::SystemLog(llvm::StringRef message) { llvm::errs() << message; } +#endif + #ifndef __linux__ // Scoped class that will disable thread canceling when it is constructed, and // exception safely restore the previous value it when it goes out of scope. @@ -627,3 +631,9 @@ return result; } + +SystemLogHandler::SystemLogHandler() {} + +void SystemLogHandler::Emit(llvm::StringRef message) { + Host::SystemLog(message); +} diff --git a/lldb/source/Host/macosx/objcxx/Host.mm b/lldb/source/Host/macosx/objcxx/Host.mm --- a/lldb/source/Host/macosx/objcxx/Host.mm +++ b/lldb/source/Host/macosx/objcxx/Host.mm @@ -82,6 +82,7 @@ #include "../cfcpp/CFCString.h" #include +#include #include #include @@ -98,6 +99,20 @@ using namespace lldb; using namespace lldb_private; +static os_log_t g_os_log; +static std::once_flag g_os_log_once; + +void Host::SystemLog(llvm::StringRef message) { + if (__builtin_available(macos 10.12, iOS 10, tvOS 10, watchOS 3, *)) { + std::call_once(g_os_log_once, []() { + g_os_log = os_log_create("com.apple.dt.lldb", "lldb"); + }); + os_log(g_os_log, "%{public}s", message.str().c_str()); + } else { + llvm::errs() << message; + } +} + bool Host::GetBundleDirectory(const FileSpec &file, FileSpec &bundle_directory) { #if defined(__APPLE__)