1 //===-- DebuggerThread.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DebuggerThread.h"
11 #include "ExceptionRecord.h"
12 #include "IDebugDelegate.h"
14 #include "lldb/Core/Error.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Host/FileSpec.h"
18 #include "lldb/Host/Predicate.h"
19 #include "lldb/Host/ThisThread.h"
20 #include "lldb/Host/ThreadLauncher.h"
21 #include "lldb/Host/windows/HostProcessWindows.h"
22 #include "lldb/Host/windows/HostThreadWindows.h"
23 #include "lldb/Host/windows/ProcessLauncherWindows.h"
24 #include "lldb/Target/Process.h"
25 #include "lldb/Target/ProcessLaunchInfo.h"
27 #include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
29 #include "llvm/ADT/STLExtras.h"
30 #include "llvm/Support/ConvertUTF.h"
31 #include "llvm/Support/raw_ostream.h"
34 using namespace lldb_private;
37 struct DebugLaunchContext {
38 DebugLaunchContext(DebuggerThread *thread,
39 const ProcessLaunchInfo &launch_info)
40 : m_thread(thread), m_launch_info(launch_info) {}
41 DebuggerThread *m_thread;
42 ProcessLaunchInfo m_launch_info;
45 struct DebugAttachContext {
46 DebugAttachContext(DebuggerThread *thread, lldb::pid_t pid,
47 const ProcessAttachInfo &attach_info)
48 : m_thread(thread), m_pid(pid), m_attach_info(attach_info) {}
49 DebuggerThread *m_thread;
51 ProcessAttachInfo m_attach_info;
55 DebuggerThread::DebuggerThread(DebugDelegateSP debug_delegate)
56 : m_debug_delegate(debug_delegate), m_pid_to_detach(0),
57 m_is_shutting_down(false) {
58 m_debugging_ended_event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
61 DebuggerThread::~DebuggerThread() { ::CloseHandle(m_debugging_ended_event); }
63 Error DebuggerThread::DebugLaunch(const ProcessLaunchInfo &launch_info) {
64 WINLOG_IFALL(WINDOWS_LOG_PROCESS,
65 "DebuggerThread::DebugLaunch launching '%s'",
66 launch_info.GetExecutableFile().GetPath().c_str());
69 DebugLaunchContext *context = new DebugLaunchContext(this, launch_info);
70 HostThread slave_thread(ThreadLauncher::LaunchThread(
71 "lldb.plugin.process-windows.slave[?]", DebuggerThreadLaunchRoutine,
74 if (!error.Success()) {
75 WINERR_IFALL(WINDOWS_LOG_PROCESS,
76 "DebugLaunch couldn't launch debugger thread. %s",
83 Error DebuggerThread::DebugAttach(lldb::pid_t pid,
84 const ProcessAttachInfo &attach_info) {
85 WINLOG_IFALL(WINDOWS_LOG_PROCESS,
86 "DebuggerThread::DebugAttach attaching to '%llu'", pid);
89 DebugAttachContext *context = new DebugAttachContext(this, pid, attach_info);
90 HostThread slave_thread(ThreadLauncher::LaunchThread(
91 "lldb.plugin.process-windows.slave[?]", DebuggerThreadAttachRoutine,
94 if (!error.Success()) {
95 WINERR_IFALL(WINDOWS_LOG_PROCESS,
96 "DebugAttach couldn't attach to process '%llu'. %s", pid,
103 lldb::thread_result_t DebuggerThread::DebuggerThreadLaunchRoutine(void *data) {
104 DebugLaunchContext *context = static_cast<DebugLaunchContext *>(data);
105 lldb::thread_result_t result =
106 context->m_thread->DebuggerThreadLaunchRoutine(context->m_launch_info);
111 lldb::thread_result_t DebuggerThread::DebuggerThreadAttachRoutine(void *data) {
112 DebugAttachContext *context = static_cast<DebugAttachContext *>(data);
113 lldb::thread_result_t result = context->m_thread->DebuggerThreadAttachRoutine(
114 context->m_pid, context->m_attach_info);
119 lldb::thread_result_t DebuggerThread::DebuggerThreadLaunchRoutine(
120 const ProcessLaunchInfo &launch_info) {
121 // Grab a shared_ptr reference to this so that we know it won't get deleted
123 // thread routine has exited.
124 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
126 WINLOG_IFALL(WINDOWS_LOG_PROCESS,
127 "DebuggerThread preparing to launch '%s' on background thread.",
128 launch_info.GetExecutableFile().GetPath().c_str());
131 ProcessLauncherWindows launcher;
132 HostProcess process(launcher.LaunchProcess(launch_info, error));
133 // If we couldn't create the process, notify waiters immediately. Otherwise
135 // loop and wait until we get the create process debug notification. Note
136 // that if the process
137 // was created successfully, we can throw away the process handle we got from
139 // because Windows will give us another (potentially more useful?) handle when
141 // CREATE_PROCESS_DEBUG_EVENT.
145 m_debug_delegate->OnDebuggerError(error, 0);
150 lldb::thread_result_t DebuggerThread::DebuggerThreadAttachRoutine(
151 lldb::pid_t pid, const ProcessAttachInfo &attach_info) {
152 // Grab a shared_ptr reference to this so that we know it won't get deleted
154 // thread routine has exited.
155 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
157 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DebuggerThread preparing to attach to "
158 "process '%llu' on background thread.",
161 if (!DebugActiveProcess((DWORD)pid)) {
162 Error error(::GetLastError(), eErrorTypeWin32);
163 m_debug_delegate->OnDebuggerError(error, 0);
167 // The attach was successful, enter the debug loop. From here on out, this is
169 // a create process operation, so all the same comments in DebugLaunch should
177 Error DebuggerThread::StopDebugging(bool terminate) {
180 lldb::pid_t pid = m_process.GetProcessId();
182 WINLOG_IFALL(WINDOWS_LOG_PROCESS,
183 "StopDebugging('%s') called (inferior=%I64u).",
184 (terminate ? "true" : "false"), pid);
186 // Set m_is_shutting_down to true if it was false. Return if it was already
188 bool expected = false;
189 if (!m_is_shutting_down.compare_exchange_strong(expected, true))
192 // Make a copy of the process, since the termination sequence will reset
193 // DebuggerThread's internal copy and it needs to remain open for the Wait
195 HostProcess process_copy = m_process;
196 lldb::process_t handle = m_process.GetNativeProcess().GetSystemHandle();
199 // Initiate the termination before continuing the exception, so that the
201 // event we get is the exit process event, and not some other event.
202 BOOL terminate_suceeded = TerminateProcess(handle, 0);
203 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "StopDebugging called "
204 "TerminateProcess(0x%p, 0) "
205 "(inferior=%I64u), success='%s'",
206 handle, pid, (terminate_suceeded ? "true" : "false"));
209 // If we're stuck waiting for an exception to continue (e.g. the user is at a
211 // messing around in the debugger), continue it now. But only AFTER calling
213 // to make sure that the very next call to WaitForDebugEvent is an exit
215 if (m_active_exception.get()) {
216 WINLOG_IFANY(WINDOWS_LOG_PROCESS | WINDOWS_LOG_EXCEPTION,
217 "StopDebugging masking active exception");
219 ContinueAsyncException(ExceptionResult::MaskException);
223 // Indicate that we want to detach.
224 m_pid_to_detach = GetProcess().GetProcessId();
226 // Force a fresh break so that the detach can happen from the debugger
228 if (!::DebugBreakProcess(
229 GetProcess().GetNativeProcess().GetSystemHandle())) {
230 error.SetError(::GetLastError(), eErrorTypeWin32);
236 "StopDebugging waiting for detach from process %llu to complete.", pid);
238 DWORD wait_result = WaitForSingleObject(m_debugging_ended_event, 5000);
239 if (wait_result != WAIT_OBJECT_0) {
240 error.SetError(GetLastError(), eErrorTypeWin32);
241 WINERR_IFALL(WINDOWS_LOG_PROCESS,
242 "StopDebugging WaitForSingleObject(0x%p, 5000) returned %lu",
243 m_debugging_ended_event, wait_result);
247 "StopDebugging detach from process %llu completed successfully.", pid);
250 if (!error.Success()) {
251 WINERR_IFALL(WINDOWS_LOG_PROCESS, "StopDebugging encountered an error "
252 "while trying to stop process %llu. %s",
253 pid, error.AsCString());
258 void DebuggerThread::ContinueAsyncException(ExceptionResult result) {
259 if (!m_active_exception.get())
263 WINDOWS_LOG_PROCESS | WINDOWS_LOG_EXCEPTION,
264 "ContinueAsyncException called for inferior process %I64u, broadcasting.",
265 m_process.GetProcessId());
267 m_active_exception.reset();
268 m_exception_pred.SetValue(result, eBroadcastAlways);
271 void DebuggerThread::FreeProcessHandles() {
272 m_process = HostProcess();
273 m_main_thread = HostThread();
275 ::CloseHandle(m_image_file);
276 m_image_file = nullptr;
280 void DebuggerThread::DebugLoop() {
281 DEBUG_EVENT dbe = {};
282 bool should_debug = true;
283 WINLOG_IFALL(WINDOWS_LOG_EVENT, "Entering WaitForDebugEvent loop");
284 while (should_debug) {
285 WINLOGD_IFALL(WINDOWS_LOG_EVENT, "Calling WaitForDebugEvent");
286 BOOL wait_result = WaitForDebugEvent(&dbe, INFINITE);
288 DWORD continue_status = DBG_CONTINUE;
289 switch (dbe.dwDebugEventCode) {
290 case EXCEPTION_DEBUG_EVENT: {
291 ExceptionResult status =
292 HandleExceptionEvent(dbe.u.Exception, dbe.dwThreadId);
294 if (status == ExceptionResult::MaskException)
295 continue_status = DBG_CONTINUE;
296 else if (status == ExceptionResult::SendToApplication)
297 continue_status = DBG_EXCEPTION_NOT_HANDLED;
301 case CREATE_THREAD_DEBUG_EVENT:
303 HandleCreateThreadEvent(dbe.u.CreateThread, dbe.dwThreadId);
305 case CREATE_PROCESS_DEBUG_EVENT:
307 HandleCreateProcessEvent(dbe.u.CreateProcessInfo, dbe.dwThreadId);
309 case EXIT_THREAD_DEBUG_EVENT:
311 HandleExitThreadEvent(dbe.u.ExitThread, dbe.dwThreadId);
313 case EXIT_PROCESS_DEBUG_EVENT:
315 HandleExitProcessEvent(dbe.u.ExitProcess, dbe.dwThreadId);
316 should_debug = false;
318 case LOAD_DLL_DEBUG_EVENT:
319 continue_status = HandleLoadDllEvent(dbe.u.LoadDll, dbe.dwThreadId);
321 case UNLOAD_DLL_DEBUG_EVENT:
322 continue_status = HandleUnloadDllEvent(dbe.u.UnloadDll, dbe.dwThreadId);
324 case OUTPUT_DEBUG_STRING_EVENT:
325 continue_status = HandleODSEvent(dbe.u.DebugString, dbe.dwThreadId);
328 continue_status = HandleRipEvent(dbe.u.RipInfo, dbe.dwThreadId);
329 if (dbe.u.RipInfo.dwType == SLE_ERROR)
330 should_debug = false;
336 "DebugLoop calling ContinueDebugEvent(%lu, %lu, %lu) on thread %lu.",
337 dbe.dwProcessId, dbe.dwThreadId, continue_status,
338 ::GetCurrentThreadId());
340 ::ContinueDebugEvent(dbe.dwProcessId, dbe.dwThreadId, continue_status);
343 should_debug = false;
348 "DebugLoop returned FALSE from WaitForDebugEvent. Error = %lu",
351 should_debug = false;
354 FreeProcessHandles();
356 WINLOG_IFALL(WINDOWS_LOG_EVENT, "WaitForDebugEvent loop completed, exiting.");
357 SetEvent(m_debugging_ended_event);
361 DebuggerThread::HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info,
363 if (m_is_shutting_down) {
364 // A breakpoint that occurs while `m_pid_to_detach` is non-zero is a magic
366 // we use simply to wake up the DebuggerThread so that we can close out the
368 if (m_pid_to_detach != 0 &&
369 info.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT) {
370 WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION |
372 "Breakpoint exception is cue to detach from process 0x%lx",
373 m_pid_to_detach.load());
374 ::DebugActiveProcessStop(m_pid_to_detach);
378 // Don't perform any blocking operations while we're shutting down. That
380 // cause TerminateProcess -> WaitForSingleObject to time out.
381 return ExceptionResult::SendToApplication;
384 bool first_chance = (info.dwFirstChance != 0);
386 m_active_exception.reset(
387 new ExceptionRecord(info.ExceptionRecord, thread_id));
388 WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION,
389 "HandleExceptionEvent encountered %s chance exception 0x%lx on "
391 first_chance ? "first" : "second",
392 info.ExceptionRecord.ExceptionCode, thread_id);
394 ExceptionResult result =
395 m_debug_delegate->OnDebugException(first_chance, *m_active_exception);
396 m_exception_pred.SetValue(result, eBroadcastNever);
398 WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION,
399 "DebuggerThread::HandleExceptionEvent waiting for ExceptionPred "
400 "!= BreakInDebugger");
402 m_exception_pred.WaitForValueNotEqualTo(ExceptionResult::BreakInDebugger,
405 WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION,
406 "DebuggerThread::HandleExceptionEvent got ExceptionPred = %u",
407 m_exception_pred.GetValue());
413 DebuggerThread::HandleCreateThreadEvent(const CREATE_THREAD_DEBUG_INFO &info,
415 WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_THREAD,
416 "HandleCreateThreadEvent Thread 0x%lx spawned in process %llu",
417 thread_id, m_process.GetProcessId());
418 HostThread thread(info.hThread);
419 thread.GetNativeThread().SetOwnsHandle(false);
420 m_debug_delegate->OnCreateThread(thread);
425 DebuggerThread::HandleCreateProcessEvent(const CREATE_PROCESS_DEBUG_INFO &info,
427 uint32_t process_id = ::GetProcessId(info.hProcess);
429 WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_PROCESS,
430 "HandleCreateProcessEvent process %u spawned", process_id);
432 std::string thread_name;
433 llvm::raw_string_ostream name_stream(thread_name);
434 name_stream << "lldb.plugin.process-windows.slave[" << process_id << "]";
436 ThisThread::SetName(thread_name.c_str());
438 // info.hProcess and info.hThread are closed automatically by Windows when
439 // EXIT_PROCESS_DEBUG_EVENT is received.
440 m_process = HostProcess(info.hProcess);
441 ((HostProcessWindows &)m_process.GetNativeProcess()).SetOwnsHandle(false);
442 m_main_thread = HostThread(info.hThread);
443 m_main_thread.GetNativeThread().SetOwnsHandle(false);
444 m_image_file = info.hFile;
446 lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfImage);
447 m_debug_delegate->OnDebuggerConnected(load_addr);
453 DebuggerThread::HandleExitThreadEvent(const EXIT_THREAD_DEBUG_INFO &info,
456 WINDOWS_LOG_EVENT | WINDOWS_LOG_THREAD,
457 "HandleExitThreadEvent Thread %lu exited with code %lu in process %llu",
458 thread_id, info.dwExitCode, m_process.GetProcessId());
459 m_debug_delegate->OnExitThread(thread_id, info.dwExitCode);
464 DebuggerThread::HandleExitProcessEvent(const EXIT_PROCESS_DEBUG_INFO &info,
466 WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_THREAD,
467 "HandleExitProcessEvent process %llu exited with code %lu",
468 m_process.GetProcessId(), info.dwExitCode);
470 m_debug_delegate->OnExitProcess(info.dwExitCode);
472 FreeProcessHandles();
477 DebuggerThread::HandleLoadDllEvent(const LOAD_DLL_DEBUG_INFO &info,
479 if (info.hFile == nullptr) {
480 // Not sure what this is, so just ignore it.
481 WINWARN_IFALL(WINDOWS_LOG_EVENT, "Inferior %llu - HandleLoadDllEvent has "
482 "a NULL file handle, returning...",
483 m_process.GetProcessId());
487 std::vector<wchar_t> buffer(1);
488 DWORD required_size =
489 GetFinalPathNameByHandleW(info.hFile, &buffer[0], 0, VOLUME_NAME_DOS);
490 if (required_size > 0) {
491 buffer.resize(required_size + 1);
492 required_size = GetFinalPathNameByHandleW(info.hFile, &buffer[0],
493 required_size, VOLUME_NAME_DOS);
494 std::string path_str_utf8;
495 llvm::convertWideToUTF8(buffer.data(), path_str_utf8);
496 llvm::StringRef path_str = path_str_utf8;
497 const char *path = path_str.data();
498 if (path_str.startswith("\\\\?\\"))
501 FileSpec file_spec(path, false);
502 ModuleSpec module_spec(file_spec);
503 lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll);
505 WINLOG_IFALL(WINDOWS_LOG_EVENT, "Inferior %I64u - HandleLoadDllEvent DLL "
506 "'%s' loaded at address 0x%p...",
507 m_process.GetProcessId(), path, info.lpBaseOfDll);
509 m_debug_delegate->OnLoadDll(module_spec, load_addr);
511 WINERR_IFALL(WINDOWS_LOG_EVENT, "Inferior %llu - HandleLoadDllEvent Error "
512 "%lu occurred calling "
513 "GetFinalPathNameByHandle",
514 m_process.GetProcessId(), ::GetLastError());
516 // Windows does not automatically close info.hFile, so we need to do it.
517 ::CloseHandle(info.hFile);
522 DebuggerThread::HandleUnloadDllEvent(const UNLOAD_DLL_DEBUG_INFO &info,
524 WINLOG_IFALL(WINDOWS_LOG_EVENT,
525 "HandleUnloadDllEvent process %llu unloading DLL at addr 0x%p.",
526 m_process.GetProcessId(), info.lpBaseOfDll);
528 m_debug_delegate->OnUnloadDll(
529 reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll));
534 DebuggerThread::HandleODSEvent(const OUTPUT_DEBUG_STRING_INFO &info,
540 DebuggerThread::HandleRipEvent(const RIP_INFO &info, DWORD thread_id) {
541 WINERR_IFALL(WINDOWS_LOG_EVENT, "HandleRipEvent encountered error %lu "
542 "(type=%lu) in process %llu thread %lu",
543 info.dwError, info.dwType, m_process.GetProcessId(), thread_id);
545 Error error(info.dwError, eErrorTypeWin32);
546 m_debug_delegate->OnDebuggerError(error, info.dwType);