[Lldb-commits] [lldb] [lldb][Windows] Use WaitForDebugEventEx if available (PR #196817)
via lldb-commits
lldb-commits at lists.llvm.org
Mon May 11 03:16:40 PDT 2026
https://github.com/Nerixyz updated https://github.com/llvm/llvm-project/pull/196817
>From 7ee5d2434e116a370f86ee032fabc874f6fc77a5 Mon Sep 17 00:00:00 2001
From: Nerixyz <nerixdev at outlook.de>
Date: Sun, 10 May 2026 18:14:53 +0200
Subject: [PATCH 1/2] [lldb][Windows] Use WaitForDebugEventEx if available
---
.../Process/Windows/Common/DebuggerThread.cpp | 36 +++++++++++++++++--
1 file changed, 34 insertions(+), 2 deletions(-)
diff --git a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
index 6359a63dfef91..95e9084b602dd 100644
--- a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
@@ -25,6 +25,7 @@
#include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
+#include "lldb/Utility/LLDBLog.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/Threading.h"
@@ -40,9 +41,40 @@
using namespace lldb;
using namespace lldb_private;
+typedef BOOL WINAPI WaitForDebugEventFn(LPDEBUG_EVENT, DWORD);
+static WaitForDebugEventFn *g_wait_for_debug_event = nullptr;
+
+static WaitForDebugEventFn *GetWaitForDebugEventEx() {
+ HMODULE h_kernel32 = LoadLibraryW(L"kernel32.dll");
+ if (!h_kernel32) {
+ llvm::Error err = llvm::errorCodeToError(
+ std::error_code(GetLastError(), std::system_category()));
+ LLDB_LOG_ERROR(GetLog(LLDBLog::Host), std::move(err),
+ "Could not load kernel32: {0}");
+ return nullptr;
+ }
+
+ return reinterpret_cast<WaitForDebugEventFn *>(
+ GetProcAddress(h_kernel32, "WaitForDebugEventEx"));
+}
+
+/// WaitForDebugEventEx is only available on Windows 10+. This lazily checks if
+/// the function is available and falls back to WaitForDebugEvent if
+/// unavailable. The -Ex version ensures correct forwarding of
+/// OutputDebugStringW events.
+static void InitializeWaitForDebugEvent() {
+ if (g_wait_for_debug_event)
+ return;
+
+ g_wait_for_debug_event = GetWaitForDebugEventEx();
+ if (!g_wait_for_debug_event)
+ g_wait_for_debug_event = &WaitForDebugEvent;
+}
+
DebuggerThread::DebuggerThread(DebugDelegateSP debug_delegate)
: m_debug_delegate(debug_delegate), m_pid_to_detach(0),
m_is_shutting_down(false) {
+ InitializeWaitForDebugEvent();
m_debugging_ended_event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
}
@@ -236,7 +268,7 @@ void DebuggerThread::DebugLoop() {
LLDB_LOG_VERBOSE(log, "Entering WaitForDebugEvent loop");
while (should_debug) {
LLDB_LOG_VERBOSE(log, "Calling WaitForDebugEvent");
- BOOL wait_result = WaitForDebugEvent(&dbe, INFINITE);
+ BOOL wait_result = g_wait_for_debug_event(&dbe, INFINITE);
if (wait_result) {
DWORD continue_status = DBG_CONTINUE;
bool shutting_down = m_is_shutting_down;
@@ -314,7 +346,7 @@ void DebuggerThread::DebugLoop() {
// target threads are running at this time, there is possibility to
// have some breakpoint exception between last WaitForDebugEvent and
// DebugActiveProcessStop but ignore for now.
- while (WaitForDebugEvent(&dbe, 0)) {
+ while (g_wait_for_debug_event(&dbe, 0)) {
continue_status = DBG_CONTINUE;
if (dbe.dwDebugEventCode == EXCEPTION_DEBUG_EVENT &&
!(dbe.u.Exception.ExceptionRecord.ExceptionCode ==
>From 1e883b456231118f00a5b9191e4366f5b889ed19 Mon Sep 17 00:00:00 2001
From: Nerixyz <nerixdev at outlook.de>
Date: Mon, 11 May 2026 12:16:26 +0200
Subject: [PATCH 2/2] chore: add warning about WaitForDebugEventEx missing
---
.../Plugins/Process/Windows/Common/DebuggerThread.cpp | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
index 95e9084b602dd..8c35a2b9262cc 100644
--- a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
@@ -67,8 +67,13 @@ static void InitializeWaitForDebugEvent() {
return;
g_wait_for_debug_event = GetWaitForDebugEventEx();
- if (!g_wait_for_debug_event)
+ if (!g_wait_for_debug_event) {
+ LLDB_LOG(
+ GetLog(LLDBLog::Host),
+ "WaitForDebugEventEx unavailable, using WaitForDebugEvent instead. "
+ "Unicode strings from OutputDebugStringW might show incorrectly.");
g_wait_for_debug_event = &WaitForDebugEvent;
+ }
}
DebuggerThread::DebuggerThread(DebugDelegateSP debug_delegate)
More information about the lldb-commits
mailing list