[lldb] Introduce an always-on system log category/channel by JDevlieghere · Pull Request #108495 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/108495.diff

7 Files Affected:

diff --git a/lldb/include/lldb/Host/Host.h b/lldb/include/lldb/Host/Host.h index 9d0994978402f7..1ca89fa056aa93 100644 --- a/lldb/include/lldb/Host/Host.h +++ b/lldb/include/lldb/Host/Host.h @@ -31,6 +31,22 @@ class ProcessInstanceInfo; class ProcessInstanceInfoMatch; typedef std::vector ProcessInstanceInfoList;

+// Always on system log category and channel. +enum class SystemLog : Log::MaskType {

diff --git a/lldb/include/lldb/Utility/Log.h b/lldb/include/lldb/Utility/Log.h index 27707c17f9b824..bcb022093c9001 100644 --- a/lldb/include/lldb/Utility/Log.h +++ b/lldb/include/lldb/Utility/Log.h @@ -165,13 +165,14 @@ class Log final { friend class Log;

public:

diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp index f08adea6546ae1..3baf40a4700ffb 100644 --- a/lldb/source/Host/common/Host.cpp +++ b/lldb/source/Host/common/Host.cpp @@ -125,6 +125,25 @@ void Host::SystemLog(Severity severity, llvm::StringRef message) { #endif #endif

+static constexpr Log::Category g_categories[] = {

+} + +void LogChannelSystem::Terminate() { Log::Unregister("system"); } + #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();

void HostInfoBase::Terminate() {

@@ -296,8 +301,10 @@ void Log::ForEachChannelCategory(

std::vectorllvm::StringRef Log::ListChannels() { std::vectorllvm::StringRef result;

@@ -307,8 +314,10 @@ void Log::ListAllLogChannels(llvm::raw_ostream &stream) { return; }

bool Log::GetVerbose() const { diff --git a/lldb/test/Shell/Host/TestSytemLogChannel.test b/lldb/test/Shell/Host/TestSytemLogChannel.test new file mode 100644 index 00000000000000..9ac4985da6164e --- /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: Cannot disable internal log channel 'system'.