1 //===-- DebuggerThread.DebuggerThread --------------------------------------*- 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/ProcessLaunchInfo.h"
25 #include "lldb/Target/Process.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;
38 struct DebugLaunchContext
40 DebugLaunchContext(DebuggerThread *thread, const ProcessLaunchInfo &launch_info)
42 , m_launch_info(launch_info)
45 DebuggerThread *m_thread;
46 ProcessLaunchInfo m_launch_info;
49 struct DebugAttachContext
51 DebugAttachContext(DebuggerThread *thread, lldb::pid_t pid, const ProcessAttachInfo &attach_info)
54 , m_attach_info(attach_info)
57 DebuggerThread *m_thread;
59 ProcessAttachInfo m_attach_info;
63 DebuggerThread::DebuggerThread(DebugDelegateSP debug_delegate)
64 : m_debug_delegate(debug_delegate)
65 , m_image_file(nullptr)
66 , m_debugging_ended_event(nullptr)
67 , m_is_shutting_down(false)
71 m_debugging_ended_event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
74 DebuggerThread::~DebuggerThread()
76 ::CloseHandle(m_debugging_ended_event);
80 DebuggerThread::DebugLaunch(const ProcessLaunchInfo &launch_info)
82 WINLOG_IFALL(WINDOWS_LOG_PROCESS,
83 "DebuggerThread::DebugLaunch launching '%s'", launch_info.GetExecutableFile().GetPath().c_str());
86 DebugLaunchContext *context = new DebugLaunchContext(this, launch_info);
87 HostThread slave_thread(ThreadLauncher::LaunchThread("lldb.plugin.process-windows.slave[?]",
88 DebuggerThreadLaunchRoutine, context, &error));
92 WINERR_IFALL(WINDOWS_LOG_PROCESS,
93 "DebugLaunch couldn't launch debugger thread. %s", error.AsCString());
100 DebuggerThread::DebugAttach(lldb::pid_t pid, const ProcessAttachInfo &attach_info)
102 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DebuggerThread::DebugAttach attaching to '%u'", (DWORD)pid);
105 DebugAttachContext *context = new DebugAttachContext(this, pid, attach_info);
106 HostThread slave_thread(ThreadLauncher::LaunchThread("lldb.plugin.process-windows.slave[?]",
107 DebuggerThreadAttachRoutine, context, &error));
109 if (!error.Success())
111 WINERR_IFALL(WINDOWS_LOG_PROCESS, "DebugAttach couldn't attach to process '%u'. %s", (DWORD)pid,
118 lldb::thread_result_t
119 DebuggerThread::DebuggerThreadLaunchRoutine(void *data)
121 DebugLaunchContext *context = static_cast<DebugLaunchContext *>(data);
122 lldb::thread_result_t result = context->m_thread->DebuggerThreadLaunchRoutine(context->m_launch_info);
127 lldb::thread_result_t
128 DebuggerThread::DebuggerThreadAttachRoutine(void *data)
130 DebugAttachContext *context = static_cast<DebugAttachContext *>(data);
131 lldb::thread_result_t result =
132 context->m_thread->DebuggerThreadAttachRoutine(context->m_pid, context->m_attach_info);
137 lldb::thread_result_t
138 DebuggerThread::DebuggerThreadLaunchRoutine(const ProcessLaunchInfo &launch_info)
140 // Grab a shared_ptr reference to this so that we know it won't get deleted until after the
141 // thread routine has exited.
142 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
144 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DebuggerThread preparing to launch '%s' on background thread.",
145 launch_info.GetExecutableFile().GetPath().c_str());
148 ProcessLauncherWindows launcher;
149 HostProcess process(launcher.LaunchProcess(launch_info, error));
150 // If we couldn't create the process, notify waiters immediately. Otherwise enter the debug
151 // loop and wait until we get the create process debug notification. Note that if the process
152 // was created successfully, we can throw away the process handle we got from CreateProcess
153 // because Windows will give us another (potentially more useful?) handle when it sends us the
154 // CREATE_PROCESS_DEBUG_EVENT.
158 m_debug_delegate->OnDebuggerError(error, 0);
163 lldb::thread_result_t
164 DebuggerThread::DebuggerThreadAttachRoutine(lldb::pid_t pid, const ProcessAttachInfo &attach_info)
166 // Grab a shared_ptr reference to this so that we know it won't get deleted until after the
167 // thread routine has exited.
168 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
170 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DebuggerThread preparing to attach to process '%u' on background thread.",
173 if (!DebugActiveProcess((DWORD)pid))
175 Error error(::GetLastError(), eErrorTypeWin32);
176 m_debug_delegate->OnDebuggerError(error, 0);
180 // The attach was successful, enter the debug loop. From here on out, this is no different than
181 // a create process operation, so all the same comments in DebugLaunch should apply from this
189 DebuggerThread::StopDebugging(bool terminate)
193 lldb::pid_t pid = m_process.GetProcessId();
195 WINLOG_IFALL(WINDOWS_LOG_PROCESS,
196 "StopDebugging('%s') called (inferior=%I64u).",
197 (terminate ? "true" : "false"), pid);
199 // Set m_is_shutting_down to true if it was false. Return if it was already true.
200 bool expected = false;
201 if (!m_is_shutting_down.compare_exchange_strong(expected, true))
204 // Make a copy of the process, since the termination sequence will reset
205 // DebuggerThread's internal copy and it needs to remain open for the Wait operation.
206 HostProcess process_copy = m_process;
207 lldb::process_t handle = m_process.GetNativeProcess().GetSystemHandle();
211 // Initiate the termination before continuing the exception, so that the next debug
212 // event we get is the exit process event, and not some other event.
213 BOOL terminate_suceeded = TerminateProcess(handle, 0);
214 WINLOG_IFALL(WINDOWS_LOG_PROCESS,
215 "StopDebugging called TerminateProcess(0x%p, 0) (inferior=%I64u), success='%s'",
216 handle, pid, (terminate_suceeded ? "true" : "false"));
219 // If we're stuck waiting for an exception to continue (e.g. the user is at a breakpoint
220 // messing around in the debugger), continue it now. But only AFTER calling TerminateProcess
221 // to make sure that the very next call to WaitForDebugEvent is an exit process event.
222 if (m_active_exception.get())
224 WINLOG_IFANY(WINDOWS_LOG_PROCESS|WINDOWS_LOG_EXCEPTION,
225 "StopDebugging masking active exception");
227 ContinueAsyncException(ExceptionResult::MaskException);
232 // Indicate that we want to detach.
233 m_pid_to_detach = GetProcess().GetProcessId();
235 // Force a fresh break so that the detach can happen from the debugger thread.
236 if (!::DebugBreakProcess(GetProcess().GetNativeProcess().GetSystemHandle()))
238 error.SetError(::GetLastError(), eErrorTypeWin32);
242 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "StopDebugging waiting for detach from process %u to complete.", pid);
244 DWORD wait_result = WaitForSingleObject(m_debugging_ended_event, 5000);
245 if (wait_result != WAIT_OBJECT_0)
247 error.SetError(GetLastError(), eErrorTypeWin32);
248 WINERR_IFALL(WINDOWS_LOG_PROCESS, "StopDebugging WaitForSingleObject(0x%p, 5000) returned %u",
249 m_debugging_ended_event, wait_result);
253 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "StopDebugging detach from process %u completed successfully.", pid);
256 if (!error.Success())
258 WINERR_IFALL(WINDOWS_LOG_PROCESS,
259 "StopDebugging encountered an error while trying to stop process %u. %s",
260 pid, error.AsCString());
266 DebuggerThread::ContinueAsyncException(ExceptionResult result)
268 if (!m_active_exception.get())
271 WINLOG_IFANY(WINDOWS_LOG_PROCESS|WINDOWS_LOG_EXCEPTION,
272 "ContinueAsyncException called for inferior process %I64u, broadcasting.",
273 m_process.GetProcessId());
275 m_active_exception.reset();
276 m_exception_pred.SetValue(result, eBroadcastAlways);
280 DebuggerThread::FreeProcessHandles()
282 m_process = HostProcess();
283 m_main_thread = HostThread();
286 ::CloseHandle(m_image_file);
287 m_image_file = nullptr;
292 DebuggerThread::DebugLoop()
294 DEBUG_EVENT dbe = {0};
295 bool should_debug = true;
296 WINLOG_IFALL(WINDOWS_LOG_EVENT, "Entering WaitForDebugEvent loop");
299 WINLOGD_IFALL(WINDOWS_LOG_EVENT, "Calling WaitForDebugEvent");
300 BOOL wait_result = WaitForDebugEvent(&dbe, INFINITE);
303 DWORD continue_status = DBG_CONTINUE;
304 switch (dbe.dwDebugEventCode)
306 case EXCEPTION_DEBUG_EVENT:
308 ExceptionResult status = HandleExceptionEvent(dbe.u.Exception, dbe.dwThreadId);
310 if (status == ExceptionResult::MaskException)
311 continue_status = DBG_CONTINUE;
312 else if (status == ExceptionResult::SendToApplication)
313 continue_status = DBG_EXCEPTION_NOT_HANDLED;
317 case CREATE_THREAD_DEBUG_EVENT:
318 continue_status = HandleCreateThreadEvent(dbe.u.CreateThread, dbe.dwThreadId);
320 case CREATE_PROCESS_DEBUG_EVENT:
321 continue_status = HandleCreateProcessEvent(dbe.u.CreateProcessInfo, dbe.dwThreadId);
323 case EXIT_THREAD_DEBUG_EVENT:
324 continue_status = HandleExitThreadEvent(dbe.u.ExitThread, dbe.dwThreadId);
326 case EXIT_PROCESS_DEBUG_EVENT:
327 continue_status = HandleExitProcessEvent(dbe.u.ExitProcess, dbe.dwThreadId);
328 should_debug = false;
330 case LOAD_DLL_DEBUG_EVENT:
331 continue_status = HandleLoadDllEvent(dbe.u.LoadDll, dbe.dwThreadId);
333 case UNLOAD_DLL_DEBUG_EVENT:
334 continue_status = HandleUnloadDllEvent(dbe.u.UnloadDll, dbe.dwThreadId);
336 case OUTPUT_DEBUG_STRING_EVENT:
337 continue_status = HandleODSEvent(dbe.u.DebugString, dbe.dwThreadId);
340 continue_status = HandleRipEvent(dbe.u.RipInfo, dbe.dwThreadId);
341 if (dbe.u.RipInfo.dwType == SLE_ERROR)
342 should_debug = false;
346 WINLOGD_IFALL(WINDOWS_LOG_EVENT, "DebugLoop calling ContinueDebugEvent(%u, %u, %u) on thread %u.",
347 dbe.dwProcessId, dbe.dwThreadId, continue_status, ::GetCurrentThreadId());
349 ::ContinueDebugEvent(dbe.dwProcessId, dbe.dwThreadId, continue_status);
353 should_debug = false;
358 WINERR_IFALL(WINDOWS_LOG_EVENT,
359 "DebugLoop returned FALSE from WaitForDebugEvent. Error = %u",
360 ::GetCurrentThreadId(), ::GetLastError());
362 should_debug = false;
365 FreeProcessHandles();
367 WINLOG_IFALL(WINDOWS_LOG_EVENT, "WaitForDebugEvent loop completed, exiting.");
368 SetEvent(m_debugging_ended_event);
372 DebuggerThread::HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info, DWORD thread_id)
374 if (m_is_shutting_down)
376 // A breakpoint that occurs while `m_pid_to_detach` is non-zero is a magic exception that
377 // we use simply to wake up the DebuggerThread so that we can close out the debug loop.
378 if (m_pid_to_detach != 0 && info.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
380 WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION | WINDOWS_LOG_PROCESS,
381 "Breakpoint exception is cue to detach from process 0x%x",
382 m_pid_to_detach.load());
383 ::DebugActiveProcessStop(m_pid_to_detach);
387 // Don't perform any blocking operations while we're shutting down. That will
388 // cause TerminateProcess -> WaitForSingleObject to time out.
389 return ExceptionResult::SendToApplication;
392 bool first_chance = (info.dwFirstChance != 0);
394 m_active_exception.reset(new ExceptionRecord(info.ExceptionRecord, thread_id));
395 WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION,
396 "HandleExceptionEvent encountered %s chance exception 0x%x on thread 0x%x",
397 first_chance ? "first" : "second", info.ExceptionRecord.ExceptionCode, thread_id);
399 ExceptionResult result = m_debug_delegate->OnDebugException(first_chance,
400 *m_active_exception);
401 m_exception_pred.SetValue(result, eBroadcastNever);
403 WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_EXCEPTION,
404 "DebuggerThread::HandleExceptionEvent waiting for ExceptionPred != BreakInDebugger");
406 m_exception_pred.WaitForValueNotEqualTo(ExceptionResult::BreakInDebugger, result);
408 WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_EXCEPTION,
409 "DebuggerThread::HandleExceptionEvent got ExceptionPred = %u",
410 m_exception_pred.GetValue());
416 DebuggerThread::HandleCreateThreadEvent(const CREATE_THREAD_DEBUG_INFO &info, DWORD thread_id)
418 WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_THREAD,
419 "HandleCreateThreadEvent Thread 0x%x spawned in process %I64u",
420 thread_id, m_process.GetProcessId());
421 HostThread thread(info.hThread);
422 thread.GetNativeThread().SetOwnsHandle(false);
423 m_debug_delegate->OnCreateThread(thread);
428 DebuggerThread::HandleCreateProcessEvent(const CREATE_PROCESS_DEBUG_INFO &info, DWORD thread_id)
430 uint32_t process_id = ::GetProcessId(info.hProcess);
432 WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_PROCESS, "HandleCreateProcessEvent process %u spawned", process_id);
434 std::string thread_name;
435 llvm::raw_string_ostream name_stream(thread_name);
436 name_stream << "lldb.plugin.process-windows.slave[" << process_id << "]";
438 ThisThread::SetName(thread_name.c_str());
440 // info.hProcess and info.hThread are closed automatically by Windows when
441 // EXIT_PROCESS_DEBUG_EVENT is received.
442 m_process = HostProcess(info.hProcess);
443 ((HostProcessWindows &)m_process.GetNativeProcess()).SetOwnsHandle(false);
444 m_main_thread = HostThread(info.hThread);
445 m_main_thread.GetNativeThread().SetOwnsHandle(false);
446 m_image_file = info.hFile;
448 lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfImage);
449 m_debug_delegate->OnDebuggerConnected(load_addr);
455 DebuggerThread::HandleExitThreadEvent(const EXIT_THREAD_DEBUG_INFO &info, DWORD thread_id)
457 WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_THREAD,
458 "HandleExitThreadEvent Thread %u exited with code %u in process %I64u",
459 thread_id, info.dwExitCode, m_process.GetProcessId());
460 m_debug_delegate->OnExitThread(thread_id, info.dwExitCode);
465 DebuggerThread::HandleExitProcessEvent(const EXIT_PROCESS_DEBUG_INFO &info, DWORD thread_id)
467 WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_THREAD,
468 "HandleExitProcessEvent process %I64u exited with code %u",
469 m_process.GetProcessId(), info.dwExitCode);
471 m_debug_delegate->OnExitProcess(info.dwExitCode);
473 FreeProcessHandles();
478 DebuggerThread::HandleLoadDllEvent(const LOAD_DLL_DEBUG_INFO &info, DWORD thread_id)
480 if (info.hFile == nullptr)
482 // Not sure what this is, so just ignore it.
483 WINWARN_IFALL(WINDOWS_LOG_EVENT, "Inferior %I64u - HandleLoadDllEvent has a NULL file handle, returning...",
484 m_process.GetProcessId());
488 std::vector<wchar_t> buffer(1);
489 DWORD required_size = GetFinalPathNameByHandleW(info.hFile, &buffer[0], 0, VOLUME_NAME_DOS);
490 if (required_size > 0)
492 buffer.resize(required_size + 1);
493 required_size = GetFinalPathNameByHandleW(info.hFile, &buffer[0], 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 '%s' loaded at address 0x%p...",
506 m_process.GetProcessId(), path, info.lpBaseOfDll);
508 m_debug_delegate->OnLoadDll(module_spec, load_addr);
512 WINERR_IFALL(WINDOWS_LOG_EVENT,
513 "Inferior %I64u - HandleLoadDllEvent Error %u occurred calling 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, DWORD thread_id)
524 WINLOG_IFALL(WINDOWS_LOG_EVENT,
525 "HandleUnloadDllEvent process %I64u unloading DLL at addr 0x%p.",
526 m_process.GetProcessId(), info.lpBaseOfDll);
528 m_debug_delegate->OnUnloadDll(reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll));
533 DebuggerThread::HandleODSEvent(const OUTPUT_DEBUG_STRING_INFO &info, DWORD thread_id)
539 DebuggerThread::HandleRipEvent(const RIP_INFO &info, DWORD thread_id)
541 WINERR_IFALL(WINDOWS_LOG_EVENT,
542 "HandleRipEvent encountered error %u (type=%u) in process %I64u thread %u",
543 info.dwError, info.dwType, m_process.GetProcessId(), thread_id);
545 Error error(info.dwError, eErrorTypeWin32);
546 m_debug_delegate->OnDebuggerError(error, info.dwType);