[lldb] Add SystemLogHandler for emitting log messages to the system log

Add a system log handler that emits log messages to the operating system
log. In addition to the log handler itself, this patch also introduces a
new Host::SystemLog helper function to abstract over writing to the
system log.

Differential revision: https://reviews.llvm.org/D128321
This commit is contained in:
Jonas Devlieghere 2022-06-24 10:03:03 -07:00
parent 4821508d4d
commit 1e5d5261e2
No known key found for this signature in database
GPG Key ID: 49CC0BD90FDEED4D
3 changed files with 36 additions and 0 deletions

View File

@ -13,6 +13,7 @@
#include "lldb/Host/HostThread.h" #include "lldb/Host/HostThread.h"
#include "lldb/Utility/Environment.h" #include "lldb/Utility/Environment.h"
#include "lldb/Utility/FileSpec.h" #include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Timeout.h" #include "lldb/Utility/Timeout.h"
#include "lldb/lldb-private-forward.h" #include "lldb/lldb-private-forward.h"
#include "lldb/lldb-private.h" #include "lldb/lldb-private.h"
@ -86,6 +87,9 @@ public:
StartMonitoringChildProcess(const MonitorChildProcessCallback &callback, StartMonitoringChildProcess(const MonitorChildProcessCallback &callback,
lldb::pid_t pid); 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. /// Get the process ID for the calling process.
/// ///
/// \return /// \return
@ -252,6 +256,13 @@ protected:
ProcessInstanceInfoList &proc_infos); 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 lldb_private
namespace llvm { namespace llvm {

View File

@ -106,6 +106,10 @@ llvm::Expected<HostThread> Host::StartMonitoringChildProcess(
}); });
} }
#if !defined(__APPLE__)
void Host::SystemLog(llvm::StringRef message) { llvm::errs() << message; }
#endif
#ifndef __linux__ #ifndef __linux__
// Scoped class that will disable thread canceling when it is constructed, and // 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. // exception safely restore the previous value it when it goes out of scope.
@ -627,3 +631,9 @@ uint32_t Host::FindProcesses(const ProcessInstanceInfoMatch &match_info,
return result; return result;
} }
SystemLogHandler::SystemLogHandler() {}
void SystemLogHandler::Emit(llvm::StringRef message) {
Host::SystemLog(message);
}

View File

@ -82,6 +82,7 @@
#include "../cfcpp/CFCString.h" #include "../cfcpp/CFCString.h"
#include <objc/objc-auto.h> #include <objc/objc-auto.h>
#include <os/log.h>
#include <CoreFoundation/CoreFoundation.h> #include <CoreFoundation/CoreFoundation.h>
#include <Foundation/Foundation.h> #include <Foundation/Foundation.h>
@ -98,6 +99,20 @@ int __pthread_fchdir(int fildes);
using namespace lldb; using namespace lldb;
using namespace lldb_private; 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, bool Host::GetBundleDirectory(const FileSpec &file,
FileSpec &bundle_directory) { FileSpec &bundle_directory) {
#if defined(__APPLE__) #if defined(__APPLE__)