[Lldb-commits] [lldb] [lldb] Introduce an always-on system log category/channel (PR #108495)
Jonas Devlieghere via lldb-commits
lldb-commits at lists.llvm.org
Wed Sep 25 11:20:01 PDT 2024
https://github.com/JDevlieghere updated https://github.com/llvm/llvm-project/pull/108495
>From c2d2758c68c661153bae0cda857b4c0cb7eddafe Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Wed, 25 Sep 2024 11:04:56 -0700
Subject: [PATCH] [lldb] Introduce an always-on system log category/channel
Add an "always on" log category and channel. Unlike other, existing log
channels, it is not exposed to users. The channel is meant to be used
sparsely and deliberately for logging high-value information to the
system log.
We have a similar concept in the downstream Swift fork and this has
proven to be extremely valuable. This is especially true on macOS where
system log messages are automatically captured as part of a sysdiagnose.
This patch revives https://reviews.llvm.org/D135621 although the purpose
is slightly different (logging to the system log rather than to a ring
buffer dumped as part of the diagnostics). This patch addresses all the
remaining oustanding comments.
---
lldb/include/lldb/Host/Host.h | 19 +++++++++++++++++++
lldb/include/lldb/Utility/Log.h | 11 ++++++-----
lldb/source/API/SystemInitializerFull.cpp | 5 +++++
lldb/source/Host/common/Host.cpp | 16 ++++++++++++++++
lldb/source/Host/common/HostInfoBase.cpp | 2 ++
lldb/test/Shell/Host/TestSytemLogChannel.test | 3 +++
6 files changed, 51 insertions(+), 5 deletions(-)
create mode 100644 lldb/test/Shell/Host/TestSytemLogChannel.test
diff --git a/lldb/include/lldb/Host/Host.h b/lldb/include/lldb/Host/Host.h
index 9d0994978402f7..d8113a5fceeada 100644
--- a/lldb/include/lldb/Host/Host.h
+++ b/lldb/include/lldb/Host/Host.h
@@ -31,6 +31,25 @@ class ProcessInstanceInfo;
class ProcessInstanceInfoMatch;
typedef std::vector<ProcessInstanceInfo> ProcessInstanceInfoList;
+// System log category and channel. This log channel is always enabled and
+// therefore is supposed to be used sparsely. Use this log channel to log
+// critical information that is expected to be relevant to the majority of bug
+// reports.
+enum class SystemLog : Log::MaskType {
+ System = Log::ChannelFlag<0>,
+ LLVM_MARK_AS_BITMASK_ENUM(System)
+};
+
+LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
+
+class LogChannelSystem {
+public:
+ static void Initialize();
+ static void Terminate();
+};
+
+template <> Log::Channel &LogChannelFor<SystemLog>();
+
// Exit Type for inferior processes
struct WaitStatus {
enum Type : uint8_t {
diff --git a/lldb/include/lldb/Utility/Log.h b/lldb/include/lldb/Utility/Log.h
index 27707c17f9b824..d709e8c4f04c6e 100644
--- a/lldb/include/lldb/Utility/Log.h
+++ b/lldb/include/lldb/Utility/Log.h
@@ -272,6 +272,12 @@ class Log final {
void VAFormatf(llvm::StringRef file, llvm::StringRef function,
const char *format, va_list args);
+ void Enable(const std::shared_ptr<LogHandler> &handler_sp,
+ uint32_t options = 0,
+ MaskType flags = std::numeric_limits<Log::MaskType>::max());
+
+ void Disable(MaskType flags = 0);
+
private:
Channel &m_channel;
@@ -297,11 +303,6 @@ class Log final {
return m_handler;
}
- void Enable(const std::shared_ptr<LogHandler> &handler_sp, uint32_t options,
- MaskType flags);
-
- void Disable(MaskType flags);
-
bool Dump(llvm::raw_ostream &stream);
typedef llvm::StringMap<Log> ChannelMap;
diff --git a/lldb/source/API/SystemInitializerFull.cpp b/lldb/source/API/SystemInitializerFull.cpp
index 995d14f7c1fa1e..1958db7291fb64 100644
--- a/lldb/source/API/SystemInitializerFull.cpp
+++ b/lldb/source/API/SystemInitializerFull.cpp
@@ -20,6 +20,8 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/TargetSelect.h"
+#include "lldb/Version/Version.h"
+
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#include "llvm/ExecutionEngine/MCJIT.h"
@@ -83,6 +85,9 @@ llvm::Error SystemInitializerFull::Initialize() {
// Use the Debugger's LLDBAssert callback.
SetLLDBAssertCallback(Debugger::AssertCallback);
+ // Log the LLDB version to the system log.
+ LLDB_LOG(GetLog(SystemLog::System), "{0}", GetVersion());
+
return llvm::Error::success();
}
diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp
index f08adea6546ae1..abca6068d3604a 100644
--- a/lldb/source/Host/common/Host.cpp
+++ b/lldb/source/Host/common/Host.cpp
@@ -125,6 +125,22 @@ void Host::SystemLog(Severity severity, llvm::StringRef message) {
#endif
#endif
+static constexpr Log::Category g_categories[] = {
+ {{"system"}, {"system log"}, SystemLog::System}};
+
+static Log::Channel g_system_channel(g_categories, SystemLog::System);
+static Log g_system_log(g_system_channel);
+
+template <> Log::Channel &lldb_private::LogChannelFor<SystemLog>() {
+ return g_system_channel;
+}
+
+void LogChannelSystem::Initialize() {
+ g_system_log.Enable(std::make_shared<SystemLogHandler>());
+}
+
+void LogChannelSystem::Terminate() { g_system_log.Disable(); }
+
#if !defined(__APPLE__) && !defined(_WIN32)
static thread_result_t
MonitorChildProcessThreadFunction(::pid_t pid,
diff --git a/lldb/source/Host/common/HostInfoBase.cpp b/lldb/source/Host/common/HostInfoBase.cpp
index 5c44c2f38b2879..89dfe4a9e9baa3 100644
--- a/lldb/source/Host/common/HostInfoBase.cpp
+++ b/lldb/source/Host/common/HostInfoBase.cpp
@@ -76,9 +76,11 @@ static HostInfoBase::SharedLibraryDirectoryHelper *g_shlib_dir_helper = nullptr;
void HostInfoBase::Initialize(SharedLibraryDirectoryHelper *helper) {
g_shlib_dir_helper = helper;
g_fields = new HostInfoBaseFields();
+ LogChannelSystem::Initialize();
}
void HostInfoBase::Terminate() {
+ LogChannelSystem::Terminate();
g_shlib_dir_helper = nullptr;
delete g_fields;
g_fields = nullptr;
diff --git a/lldb/test/Shell/Host/TestSytemLogChannel.test b/lldb/test/Shell/Host/TestSytemLogChannel.test
new file mode 100644
index 00000000000000..4de699f0e09a4a
--- /dev/null
+++ b/lldb/test/Shell/Host/TestSytemLogChannel.test
@@ -0,0 +1,3 @@
+RUN: %lldb -o 'log list' -o 'log disable system' 2>&1 | FileCheck %s
+CHECK-NOT: Logging categories for 'system'
+CHECK: Invalid log channel 'system'
More information about the lldb-commits
mailing list