From 17bafe386817cff9c7470f92eec3c27a99c2219b Mon Sep 17 00:00:00 2001 From: Charles Zablit Date: Thu, 17 Jul 2025 11:43:46 +0200 Subject: [PATCH 1/4] [windows][lldb] implement system logging on Windows --- lldb/source/Host/common/Host.cpp | 12 ------ lldb/source/Host/windows/Host.cpp | 61 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp index 01fada13fae53..67072e3dcb30a 100644 --- a/lldb/source/Host/common/Host.cpp +++ b/lldb/source/Host/common/Host.cpp @@ -110,18 +110,6 @@ void Host::SystemLog(Severity severity, llvm::StringRef message) { } syslog(level, "%s", message.data()); } -#else -void Host::SystemLog(Severity severity, llvm::StringRef message) { - switch (severity) { - case lldb::eSeverityInfo: - case lldb::eSeverityWarning: - llvm::outs() << message; - break; - case lldb::eSeverityError: - llvm::errs() << message; - break; - } -} #endif #endif diff --git a/lldb/source/Host/windows/Host.cpp b/lldb/source/Host/windows/Host.cpp index a7369e7eade3b..34fec3f875ec7 100644 --- a/lldb/source/Host/windows/Host.cpp +++ b/lldb/source/Host/windows/Host.cpp @@ -22,10 +22,13 @@ #include "lldb/Utility/StreamString.h" #include "lldb/Utility/StructuredData.h" +#include "llvm/ADT/StringRef.h" #include "llvm/Support/ConvertUTF.h" +#include "llvm/Support/ManagedStatic.h" // Windows includes #include +#include using namespace lldb; using namespace lldb_private; @@ -302,3 +305,61 @@ Environment Host::GetEnvironment() { } return env; } + +class WindowsEventLog { +public: + WindowsEventLog() { handle = RegisterEventSource(nullptr, L"LLDB"); } + + ~WindowsEventLog() { + if (handle) + DeregisterEventSource(handle); + } + + HANDLE GetHandle() const { return handle; } + +private: + HANDLE handle; +}; + +static llvm::ManagedStatic event_log; + +static LPCWSTR AnsiToUtf16(const char *ansi) { + if (!ansi) + return nullptr; + const int length = strlen(ansi); + const int unicode_length = + MultiByteToWideChar(CP_ACP, 0, ansi, length, nullptr, 0); + WCHAR *unicode = new WCHAR[unicode_length + 1]; + MultiByteToWideChar(CP_ACP, 0, ansi, length, unicode, unicode_length); + unicode[unicode_length] = 0; + return unicode; +} + +void Host::SystemLog(Severity severity, llvm::StringRef message) { + HANDLE h = event_log->GetHandle(); + LPCWSTR wide_message = AnsiToUtf16(message.str().c_str()); + + if (!h || !wide_message) { + SystemLogFallback(severity, message); + return; + } + + WORD event_type; + WORD event_id; + + switch (severity) { + case lldb::eSeverityWarning: + event_type = EVENTLOG_WARNING_TYPE; + break; + case lldb::eSeverityError: + event_type = EVENTLOG_ERROR_TYPE; + break; + case lldb::eSeverityInfo: + default: + event_type = EVENTLOG_INFORMATION_TYPE; + } + + ReportEventW(h, event_type, 0, 0, nullptr, 1, 0, &wide_message, nullptr); + + delete[] wide_message; +} From 598da83d159441376b6d200ca939f17d3b82f7d8 Mon Sep 17 00:00:00 2001 From: Charles Zablit Date: Thu, 17 Jul 2025 12:44:27 +0200 Subject: [PATCH 2/4] fixup! [windows][lldb] implement system logging on Windows --- lldb/source/Host/windows/Host.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lldb/source/Host/windows/Host.cpp b/lldb/source/Host/windows/Host.cpp index 34fec3f875ec7..c8355f1ff33e0 100644 --- a/lldb/source/Host/windows/Host.cpp +++ b/lldb/source/Host/windows/Host.cpp @@ -340,8 +340,15 @@ void Host::SystemLog(Severity severity, llvm::StringRef message) { LPCWSTR wide_message = AnsiToUtf16(message.str().c_str()); if (!h || !wide_message) { - SystemLogFallback(severity, message); - return; + switch (severity) { + case lldb::eSeverityInfo: + case lldb::eSeverityWarning: + llvm::outs() << message; + return; + case lldb::eSeverityError: + llvm::errs() << message; + return; + } } WORD event_type; From 5be89a17358da14cd51a9bc6dabd2690a323b9ac Mon Sep 17 00:00:00 2001 From: Charles Zablit Date: Thu, 17 Jul 2025 12:47:27 +0200 Subject: [PATCH 3/4] fixup! [windows][lldb] implement system logging on Windows --- lldb/source/Host/windows/Host.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/lldb/source/Host/windows/Host.cpp b/lldb/source/Host/windows/Host.cpp index c8355f1ff33e0..18322064d5acd 100644 --- a/lldb/source/Host/windows/Host.cpp +++ b/lldb/source/Host/windows/Host.cpp @@ -352,7 +352,6 @@ void Host::SystemLog(Severity severity, llvm::StringRef message) { } WORD event_type; - WORD event_id; switch (severity) { case lldb::eSeverityWarning: From f9b20a120722a8db9bf945cd0ea501dd3131cf94 Mon Sep 17 00:00:00 2001 From: Charles Zablit Date: Thu, 17 Jul 2025 14:42:02 +0200 Subject: [PATCH 4/4] refactor the wrapper function --- lldb/include/lldb/Host/Host.h | 4 ++++ lldb/source/Host/common/Host.cpp | 12 +++++++++++ lldb/source/Host/windows/Host.cpp | 36 +++++++++++++------------------ 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/lldb/include/lldb/Host/Host.h b/lldb/include/lldb/Host/Host.h index 23d5383163ffe..482a2270d6a69 100644 --- a/lldb/include/lldb/Host/Host.h +++ b/lldb/include/lldb/Host/Host.h @@ -90,6 +90,10 @@ class Host { /// Emit the given message to the operating system log. static void SystemLog(lldb::Severity severity, llvm::StringRef message); + /// Emit the given message to the stdout or stderr depending on severity. + static void SystemLogFallback(lldb::Severity severity, + llvm::StringRef message); + /// Get the process ID for the calling process. /// /// \return diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp index 67072e3dcb30a..289da773afce5 100644 --- a/lldb/source/Host/common/Host.cpp +++ b/lldb/source/Host/common/Host.cpp @@ -113,6 +113,18 @@ void Host::SystemLog(Severity severity, llvm::StringRef message) { #endif #endif +void Host::SystemLogFallback(Severity severity, llvm::StringRef message) { + switch (severity) { + case lldb::eSeverityInfo: + case lldb::eSeverityWarning: + llvm::outs() << message; + return; + case lldb::eSeverityError: + llvm::errs() << message; + return; + } +} + #if !defined(__APPLE__) && !defined(_WIN32) static thread_result_t MonitorChildProcessThreadFunction(::pid_t pid, diff --git a/lldb/source/Host/windows/Host.cpp b/lldb/source/Host/windows/Host.cpp index 18322064d5acd..90e46a58299c7 100644 --- a/lldb/source/Host/windows/Host.cpp +++ b/lldb/source/Host/windows/Host.cpp @@ -308,7 +308,7 @@ Environment Host::GetEnvironment() { class WindowsEventLog { public: - WindowsEventLog() { handle = RegisterEventSource(nullptr, L"LLDB"); } + WindowsEventLog() : handle(RegisterEventSource(nullptr, L"lldb")) {} ~WindowsEventLog() { if (handle) @@ -323,36 +323,30 @@ class WindowsEventLog { static llvm::ManagedStatic event_log; -static LPCWSTR AnsiToUtf16(const char *ansi) { - if (!ansi) +static LPCWSTR AnsiToUtf16(const std::string &ansi) { + if (ansi.empty()) return nullptr; - const int length = strlen(ansi); const int unicode_length = - MultiByteToWideChar(CP_ACP, 0, ansi, length, nullptr, 0); - WCHAR *unicode = new WCHAR[unicode_length + 1]; - MultiByteToWideChar(CP_ACP, 0, ansi, length, unicode, unicode_length); - unicode[unicode_length] = 0; + MultiByteToWideChar(CP_ACP, 0, ansi.c_str(), -1, nullptr, 0); + WCHAR *unicode = new WCHAR[unicode_length]; + MultiByteToWideChar(CP_ACP, 0, ansi.c_str(), -1, unicode, unicode_length); return unicode; } void Host::SystemLog(Severity severity, llvm::StringRef message) { HANDLE h = event_log->GetHandle(); - LPCWSTR wide_message = AnsiToUtf16(message.str().c_str()); - - if (!h || !wide_message) { - switch (severity) { - case lldb::eSeverityInfo: - case lldb::eSeverityWarning: - llvm::outs() << message; - return; - case lldb::eSeverityError: - llvm::errs() << message; - return; - } + if (!h) { + SystemLogFallback(severity, message); + return; } - WORD event_type; + LPCWSTR wide_message = AnsiToUtf16(message.str()); + if (!wide_message) { + SystemLogFallback(severity, message); + return; + } + WORD event_type; switch (severity) { case lldb::eSeverityWarning: event_type = EVENTLOG_WARNING_TYPE;