[lldb][Windows] Use WaitForDebugEventEx if available#196817
Merged
Merged
Conversation
|
@llvm/pr-subscribers-lldb Author: Nerixyz (Nerixyz) ChangesThis makes use of The two functions are identical except for the handling of Split from #196395. 1 Files Affected:
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 ==
|
|
|
||
| g_wait_for_debug_event = GetWaitForDebugEventEx(); | ||
| if (!g_wait_for_debug_event) | ||
| g_wait_for_debug_event = &WaitForDebugEvent; |
Contributor
There was a problem hiding this comment.
I think we should eventually add a log here to warn the user that unicode won't be supported.
It won't hit on windows 10+ once #196395 lands. However, the log could be useful for users of other versions of Windows.
charles-zablit
approved these changes
May 11, 2026
EuphoricThinking
pushed a commit
to EuphoricThinking/llvm-project
that referenced
this pull request
May 14, 2026
This makes use of [`WaitForDebugEventEx`](https://learn.microsoft.com/en-us/windows/win32/api/debugapi/nf-debugapi-waitfordebugeventex) over `WaitForDebugEvent` if available (Windows 10+). The two functions are identical except for the handling of `OutputDebugStringW`. The `-Ex` version forwards the string as Unicode whereas the other version forwards ASCII strings. Since we don't handle these outputs yet, it shouldn't make any difference. Split from llvm#196395.
pedroMVicente
pushed a commit
to pedroMVicente/llvm-project
that referenced
this pull request
May 19, 2026
This makes use of [`WaitForDebugEventEx`](https://learn.microsoft.com/en-us/windows/win32/api/debugapi/nf-debugapi-waitfordebugeventex) over `WaitForDebugEvent` if available (Windows 10+). The two functions are identical except for the handling of `OutputDebugStringW`. The `-Ex` version forwards the string as Unicode whereas the other version forwards ASCII strings. Since we don't handle these outputs yet, it shouldn't make any difference. Split from llvm#196395.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This makes use of
WaitForDebugEventExoverWaitForDebugEventif available (Windows 10+).The two functions are identical except for the handling of
OutputDebugStringW. The-Exversion forwards the string as Unicode whereas the other version forwards ASCII strings. Since we don't handle these outputs yet, it shouldn't make any difference.Split from #196395.