blob: 59bcc2586f949a3ac3c2186bd07af16e31d2f401 [file] [log] [blame]
Alexander Shaposhnikov696bd632016-11-26 05:23:44 +00001//===-- DebuggerThread.cpp --------------------------------------*- C++ -*-===//
Zachary Turner02862bc2014-11-07 23:44:13 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Zachary Turner02862bc2014-11-07 23:44:13 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "DebuggerThread.h"
Zachary Turnerdcd80372014-11-11 00:00:14 +000010#include "ExceptionRecord.h"
Zachary Turner02862bc2014-11-07 23:44:13 +000011#include "IDebugDelegate.h"
Zachary Turner02862bc2014-11-07 23:44:13 +000012
Zachary Turnera32d2ce2014-11-12 19:31:56 +000013#include "lldb/Core/ModuleSpec.h"
Pavel Labathe7404d92019-02-04 15:03:06 +000014#include "lldb/Host/ProcessLaunchInfo.h"
Zachary Turner02862bc2014-11-07 23:44:13 +000015#include "lldb/Host/ThreadLauncher.h"
16#include "lldb/Host/windows/HostProcessWindows.h"
17#include "lldb/Host/windows/HostThreadWindows.h"
18#include "lldb/Host/windows/ProcessLauncherWindows.h"
Zachary Turnerc62733b2015-05-20 18:31:17 +000019#include "lldb/Target/Process.h"
Zachary Turner5713a052017-03-22 18:40:07 +000020#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000021#include "lldb/Utility/Log.h"
Raphael Isemann7fae4932018-08-30 17:51:10 +000022#include "lldb/Utility/Predicate.h"
Zachary Turner97206d52017-05-12 04:51:55 +000023#include "lldb/Utility/Status.h"
Zachary Turner02862bc2014-11-07 23:44:13 +000024
Adrian McCarthy18a9135d2015-10-28 18:21:45 +000025#include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
Zachary Turner610e5292015-05-07 21:39:33 +000026
Zachary Turnera32d2ce2014-11-12 19:31:56 +000027#include "llvm/ADT/STLExtras.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000028#include "llvm/Support/ConvertUTF.h"
Zachary Turnered96be92017-03-04 01:31:06 +000029#include "llvm/Support/Threading.h"
Zachary Turner02862bc2014-11-07 23:44:13 +000030#include "llvm/Support/raw_ostream.h"
31
Aaron Smith5146a9e2019-08-13 22:18:01 +000032#ifndef STATUS_WX86_BREAKPOINT
33#define STATUS_WX86_BREAKPOINT 0x4000001FL // For WOW64
34#endif
35
Zachary Turner02862bc2014-11-07 23:44:13 +000036using namespace lldb;
37using namespace lldb_private;
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039namespace {
40struct DebugLaunchContext {
41 DebugLaunchContext(DebuggerThread *thread,
42 const ProcessLaunchInfo &launch_info)
43 : m_thread(thread), m_launch_info(launch_info) {}
44 DebuggerThread *m_thread;
45 ProcessLaunchInfo m_launch_info;
Zachary Turner02862bc2014-11-07 23:44:13 +000046};
Zachary Turnerc62733b2015-05-20 18:31:17 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048struct DebugAttachContext {
49 DebugAttachContext(DebuggerThread *thread, lldb::pid_t pid,
50 const ProcessAttachInfo &attach_info)
51 : m_thread(thread), m_pid(pid), m_attach_info(attach_info) {}
52 DebuggerThread *m_thread;
53 lldb::pid_t m_pid;
54 ProcessAttachInfo m_attach_info;
Zachary Turnerc62733b2015-05-20 18:31:17 +000055};
Aaron Smithe3037902018-10-10 18:30:32 +000056} // namespace
Zachary Turner02862bc2014-11-07 23:44:13 +000057
58DebuggerThread::DebuggerThread(DebugDelegateSP debug_delegate)
Zachary Turner5a8ad4592016-10-05 17:07:34 +000059 : m_debug_delegate(debug_delegate), m_pid_to_detach(0),
60 m_is_shutting_down(false) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 m_debugging_ended_event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
Zachary Turner02862bc2014-11-07 23:44:13 +000062}
63
Kate Stoneb9c1b512016-09-06 20:57:50 +000064DebuggerThread::~DebuggerThread() { ::CloseHandle(m_debugging_ended_event); }
65
Zachary Turner97206d52017-05-12 04:51:55 +000066Status DebuggerThread::DebugLaunch(const ProcessLaunchInfo &launch_info) {
Pavel Labatha385d2c2017-02-22 10:38:02 +000067 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
68 LLDB_LOG(log, "launching '{0}'", launch_info.GetExecutableFile().GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +000069
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000070 Status result;
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 DebugLaunchContext *context = new DebugLaunchContext(this, launch_info);
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000072
73 llvm::Expected<HostThread> slave_thread = ThreadLauncher::LaunchThread(
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 "lldb.plugin.process-windows.slave[?]", DebuggerThreadLaunchRoutine,
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000075 context);
76 if (!slave_thread) {
77 result = Status(slave_thread.takeError());
78 LLDB_LOG(log, "couldn't launch debugger thread. {0}", result);
79 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000080
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000081 return result;
Zachary Turner02862bc2014-11-07 23:44:13 +000082}
83
Zachary Turner97206d52017-05-12 04:51:55 +000084Status DebuggerThread::DebugAttach(lldb::pid_t pid,
85 const ProcessAttachInfo &attach_info) {
Pavel Labatha385d2c2017-02-22 10:38:02 +000086 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
87 LLDB_LOG(log, "attaching to '{0}'", pid);
Zachary Turner02862bc2014-11-07 23:44:13 +000088
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000089 Status result;
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 DebugAttachContext *context = new DebugAttachContext(this, pid, attach_info);
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000091
92 llvm::Expected<HostThread> slave_thread = ThreadLauncher::LaunchThread(
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 "lldb.plugin.process-windows.slave[?]", DebuggerThreadAttachRoutine,
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000094 context);
95 if (!slave_thread) {
96 result = Status(slave_thread.takeError());
97 LLDB_LOG(log, "couldn't attach to process '{0}'. {1}", pid, result);
98 }
Zachary Turner610e5292015-05-07 21:39:33 +000099
Stella Stamenova631b5f7d2019-07-09 18:10:36 +0000100 return result;
Zachary Turner02862bc2014-11-07 23:44:13 +0000101}
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103lldb::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);
107 delete context;
108 return result;
Zachary Turnerc62733b2015-05-20 18:31:17 +0000109}
110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111lldb::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);
115 delete context;
116 return result;
Zachary Turner02862bc2014-11-07 23:44:13 +0000117}
118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119lldb::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
Adrian Prantl05097242018-04-30 16:49:04 +0000122 // until after the thread routine has exited.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
Zachary Turnerc62733b2015-05-20 18:31:17 +0000124
Pavel Labatha385d2c2017-02-22 10:38:02 +0000125 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
126 LLDB_LOG(log, "preparing to launch '{0}' on background thread.",
127 launch_info.GetExecutableFile().GetPath());
Zachary Turner02862bc2014-11-07 23:44:13 +0000128
Zachary Turner97206d52017-05-12 04:51:55 +0000129 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 ProcessLauncherWindows launcher;
131 HostProcess process(launcher.LaunchProcess(launch_info, error));
132 // If we couldn't create the process, notify waiters immediately. Otherwise
Adrian Prantl05097242018-04-30 16:49:04 +0000133 // enter the debug loop and wait until we get the create process debug
134 // notification. Note that if the process was created successfully, we can
135 // throw away the process handle we got from CreateProcess because Windows
136 // will give us another (potentially more useful?) handle when it sends us
137 // the CREATE_PROCESS_DEBUG_EVENT.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 if (error.Success())
Zachary Turnerc62733b2015-05-20 18:31:17 +0000139 DebugLoop();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 else
141 m_debug_delegate->OnDebuggerError(error, 0);
Zachary Turnerc62733b2015-05-20 18:31:17 +0000142
Konrad Kleine85200642019-05-23 15:17:39 +0000143 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144}
145
146lldb::thread_result_t DebuggerThread::DebuggerThreadAttachRoutine(
147 lldb::pid_t pid, const ProcessAttachInfo &attach_info) {
148 // Grab a shared_ptr reference to this so that we know it won't get deleted
Adrian Prantl05097242018-04-30 16:49:04 +0000149 // until after the thread routine has exited.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
151
Pavel Labatha385d2c2017-02-22 10:38:02 +0000152 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
153 LLDB_LOG(log, "preparing to attach to process '{0}' on background thread.",
154 pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155
156 if (!DebugActiveProcess((DWORD)pid)) {
Zachary Turner97206d52017-05-12 04:51:55 +0000157 Status error(::GetLastError(), eErrorTypeWin32);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 m_debug_delegate->OnDebuggerError(error, 0);
Konrad Kleine85200642019-05-23 15:17:39 +0000159 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 }
161
Adrian Prantl05097242018-04-30 16:49:04 +0000162 // The attach was successful, enter the debug loop. From here on out, this
163 // is no different than a create process operation, so all the same comments
164 // in DebugLaunch should apply from this point out.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 DebugLoop();
166
Konrad Kleine85200642019-05-23 15:17:39 +0000167 return {};
Zachary Turnerc62733b2015-05-20 18:31:17 +0000168}
169
Zachary Turner97206d52017-05-12 04:51:55 +0000170Status DebuggerThread::StopDebugging(bool terminate) {
171 Status error;
Zachary Turnerc6a66532014-12-03 22:04:18 +0000172
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 lldb::pid_t pid = m_process.GetProcessId();
Zachary Turner610e5292015-05-07 21:39:33 +0000174
Pavel Labatha385d2c2017-02-22 10:38:02 +0000175 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
176 LLDB_LOG(log, "terminate = {0}, inferior={1}.", terminate, pid);
Zachary Turner610e5292015-05-07 21:39:33 +0000177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 // Set m_is_shutting_down to true if it was false. Return if it was already
179 // true.
180 bool expected = false;
181 if (!m_is_shutting_down.compare_exchange_strong(expected, true))
Zachary Turnerc6a66532014-12-03 22:04:18 +0000182 return error;
Zachary Turnerc6a66532014-12-03 22:04:18 +0000183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 // Make a copy of the process, since the termination sequence will reset
185 // DebuggerThread's internal copy and it needs to remain open for the Wait
186 // operation.
187 HostProcess process_copy = m_process;
188 lldb::process_t handle = m_process.GetNativeProcess().GetSystemHandle();
Zachary Turnerc6a66532014-12-03 22:04:18 +0000189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 if (terminate) {
Stella Stamenova9d6fabf2018-06-13 19:02:44 +0000191 if (handle != nullptr && handle != LLDB_INVALID_PROCESS) {
192 // Initiate the termination before continuing the exception, so that the
193 // next debug event we get is the exit process event, and not some other
194 // event.
195 BOOL terminate_suceeded = TerminateProcess(handle, 0);
196 LLDB_LOG(log,
197 "calling TerminateProcess({0}, 0) (inferior={1}), success={2}",
198 handle, pid, terminate_suceeded);
199 } else {
200 LLDB_LOG(log,
Aaron Smithe3037902018-10-10 18:30:32 +0000201 "NOT calling TerminateProcess because the inferior is not valid "
202 "({0}, 0) (inferior={1})",
Stella Stamenova9d6fabf2018-06-13 19:02:44 +0000203 handle, pid);
204 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 }
Zachary Turner610e5292015-05-07 21:39:33 +0000206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 // If we're stuck waiting for an exception to continue (e.g. the user is at a
Adrian Prantl05097242018-04-30 16:49:04 +0000208 // breakpoint messing around in the debugger), continue it now. But only
209 // AFTER calling TerminateProcess to make sure that the very next call to
210 // WaitForDebugEvent is an exit process event.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 if (m_active_exception.get()) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000212 LLDB_LOG(log, "masking active exception");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 ContinueAsyncException(ExceptionResult::MaskException);
214 }
215
216 if (!terminate) {
217 // Indicate that we want to detach.
218 m_pid_to_detach = GetProcess().GetProcessId();
219
220 // Force a fresh break so that the detach can happen from the debugger
221 // thread.
222 if (!::DebugBreakProcess(
223 GetProcess().GetNativeProcess().GetSystemHandle())) {
224 error.SetError(::GetLastError(), eErrorTypeWin32);
Zachary Turnerc6a66532014-12-03 22:04:18 +0000225 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 }
227
Pavel Labatha385d2c2017-02-22 10:38:02 +0000228 LLDB_LOG(log, "waiting for detach from process {0} to complete.", pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229
230 DWORD wait_result = WaitForSingleObject(m_debugging_ended_event, 5000);
231 if (wait_result != WAIT_OBJECT_0) {
232 error.SetError(GetLastError(), eErrorTypeWin32);
Pavel Labatha385d2c2017-02-22 10:38:02 +0000233 LLDB_LOG(log, "error: WaitForSingleObject({0}, 5000) returned {1}",
234 m_debugging_ended_event, wait_result);
235 } else
236 LLDB_LOG(log, "detach from process {0} completed successfully.", pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237
238 if (!error.Success()) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000239 LLDB_LOG(log, "encountered an error while trying to stop process {0}. {1}",
240 pid, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 }
242 return error;
Zachary Turnerdcd80372014-11-11 00:00:14 +0000243}
244
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245void DebuggerThread::ContinueAsyncException(ExceptionResult result) {
246 if (!m_active_exception.get())
247 return;
Zachary Turnerdcd80372014-11-11 00:00:14 +0000248
Pavel Labatha385d2c2017-02-22 10:38:02 +0000249 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS |
250 WINDOWS_LOG_EXCEPTION);
251 LLDB_LOG(log, "broadcasting for inferior process {0}.",
252 m_process.GetProcessId());
Zachary Turner6c3c0ed2015-10-02 22:47:04 +0000253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 m_active_exception.reset();
255 m_exception_pred.SetValue(result, eBroadcastAlways);
256}
Zachary Turner02862bc2014-11-07 23:44:13 +0000257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258void DebuggerThread::FreeProcessHandles() {
259 m_process = HostProcess();
260 m_main_thread = HostThread();
261 if (m_image_file) {
262 ::CloseHandle(m_image_file);
263 m_image_file = nullptr;
264 }
265}
Zachary Turner610e5292015-05-07 21:39:33 +0000266
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267void DebuggerThread::DebugLoop() {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000268 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT);
Zachary Turner5a8ad4592016-10-05 17:07:34 +0000269 DEBUG_EVENT dbe = {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 bool should_debug = true;
Pavel Labatha385d2c2017-02-22 10:38:02 +0000271 LLDB_LOGV(log, "Entering WaitForDebugEvent loop");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 while (should_debug) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000273 LLDB_LOGV(log, "Calling WaitForDebugEvent");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 BOOL wait_result = WaitForDebugEvent(&dbe, INFINITE);
275 if (wait_result) {
276 DWORD continue_status = DBG_CONTINUE;
277 switch (dbe.dwDebugEventCode) {
Aaron Smithe3037902018-10-10 18:30:32 +0000278 default:
279 llvm_unreachable("Unhandle debug event code!");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 case EXCEPTION_DEBUG_EVENT: {
281 ExceptionResult status =
282 HandleExceptionEvent(dbe.u.Exception, dbe.dwThreadId);
Adrian McCarthya59a7212015-06-19 18:26:53 +0000283
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 if (status == ExceptionResult::MaskException)
285 continue_status = DBG_CONTINUE;
286 else if (status == ExceptionResult::SendToApplication)
287 continue_status = DBG_EXCEPTION_NOT_HANDLED;
Zachary Turner610e5292015-05-07 21:39:33 +0000288
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 break;
290 }
291 case CREATE_THREAD_DEBUG_EVENT:
292 continue_status =
293 HandleCreateThreadEvent(dbe.u.CreateThread, dbe.dwThreadId);
294 break;
295 case CREATE_PROCESS_DEBUG_EVENT:
296 continue_status =
297 HandleCreateProcessEvent(dbe.u.CreateProcessInfo, dbe.dwThreadId);
298 break;
299 case EXIT_THREAD_DEBUG_EVENT:
300 continue_status =
301 HandleExitThreadEvent(dbe.u.ExitThread, dbe.dwThreadId);
302 break;
303 case EXIT_PROCESS_DEBUG_EVENT:
304 continue_status =
305 HandleExitProcessEvent(dbe.u.ExitProcess, dbe.dwThreadId);
306 should_debug = false;
307 break;
308 case LOAD_DLL_DEBUG_EVENT:
309 continue_status = HandleLoadDllEvent(dbe.u.LoadDll, dbe.dwThreadId);
310 break;
311 case UNLOAD_DLL_DEBUG_EVENT:
312 continue_status = HandleUnloadDllEvent(dbe.u.UnloadDll, dbe.dwThreadId);
313 break;
314 case OUTPUT_DEBUG_STRING_EVENT:
315 continue_status = HandleODSEvent(dbe.u.DebugString, dbe.dwThreadId);
316 break;
317 case RIP_EVENT:
318 continue_status = HandleRipEvent(dbe.u.RipInfo, dbe.dwThreadId);
319 if (dbe.u.RipInfo.dwType == SLE_ERROR)
320 should_debug = false;
321 break;
322 }
323
Pavel Labatha385d2c2017-02-22 10:38:02 +0000324 LLDB_LOGV(log, "calling ContinueDebugEvent({0}, {1}, {2}) on thread {3}.",
325 dbe.dwProcessId, dbe.dwThreadId, continue_status,
326 ::GetCurrentThreadId());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327
328 ::ContinueDebugEvent(dbe.dwProcessId, dbe.dwThreadId, continue_status);
329
330 if (m_detached) {
331 should_debug = false;
332 }
333 } else {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000334 LLDB_LOG(log, "returned FALSE from WaitForDebugEvent. Error = {0}",
335 ::GetLastError());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336
337 should_debug = false;
Zachary Turner02862bc2014-11-07 23:44:13 +0000338 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 }
340 FreeProcessHandles();
Zachary Turner3c1c5b92015-05-21 19:56:26 +0000341
Pavel Labatha385d2c2017-02-22 10:38:02 +0000342 LLDB_LOG(log, "WaitForDebugEvent loop completed, exiting.");
Aaron Smithe3037902018-10-10 18:30:32 +0000343 ::SetEvent(m_debugging_ended_event);
Zachary Turner02862bc2014-11-07 23:44:13 +0000344}
345
Zachary Turnerdcd80372014-11-11 00:00:14 +0000346ExceptionResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347DebuggerThread::HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info,
348 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000349 Log *log =
350 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 if (m_is_shutting_down) {
352 // A breakpoint that occurs while `m_pid_to_detach` is non-zero is a magic
353 // exception that
354 // we use simply to wake up the DebuggerThread so that we can close out the
355 // debug loop.
356 if (m_pid_to_detach != 0 &&
Aaron Smith5146a9e2019-08-13 22:18:01 +0000357 (info.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT ||
358 info.ExceptionRecord.ExceptionCode == STATUS_WX86_BREAKPOINT)) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000359 LLDB_LOG(log, "Breakpoint exception is cue to detach from process {0:x}",
360 m_pid_to_detach.load());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 ::DebugActiveProcessStop(m_pid_to_detach);
362 m_detached = true;
Zachary Turner6c3c0ed2015-10-02 22:47:04 +0000363 }
364
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 // Don't perform any blocking operations while we're shutting down. That
Adrian Prantl05097242018-04-30 16:49:04 +0000366 // will cause TerminateProcess -> WaitForSingleObject to time out.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 return ExceptionResult::SendToApplication;
368 }
Zachary Turnerc6a66532014-12-03 22:04:18 +0000369
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 bool first_chance = (info.dwFirstChance != 0);
Zachary Turnerc6a66532014-12-03 22:04:18 +0000371
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 m_active_exception.reset(
373 new ExceptionRecord(info.ExceptionRecord, thread_id));
Pavel Labatha385d2c2017-02-22 10:38:02 +0000374 LLDB_LOG(log, "encountered {0} chance exception {1:x} on thread {2:x}",
375 first_chance ? "first" : "second",
376 info.ExceptionRecord.ExceptionCode, thread_id);
Zachary Turner610e5292015-05-07 21:39:33 +0000377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 ExceptionResult result =
379 m_debug_delegate->OnDebugException(first_chance, *m_active_exception);
380 m_exception_pred.SetValue(result, eBroadcastNever);
Zachary Turner610e5292015-05-07 21:39:33 +0000381
Pavel Labatha385d2c2017-02-22 10:38:02 +0000382 LLDB_LOG(log, "waiting for ExceptionPred != BreakInDebugger");
Pavel Labath4b130332018-05-09 14:49:43 +0000383 result = *m_exception_pred.WaitForValueNotEqualTo(
384 ExceptionResult::BreakInDebugger);
Zachary Turner610e5292015-05-07 21:39:33 +0000385
Pavel Labatha385d2c2017-02-22 10:38:02 +0000386 LLDB_LOG(log, "got ExceptionPred = {0}", (int)m_exception_pred.GetValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387 return result;
Zachary Turner02862bc2014-11-07 23:44:13 +0000388}
389
390DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391DebuggerThread::HandleCreateThreadEvent(const CREATE_THREAD_DEBUG_INFO &info,
392 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000393 Log *log =
394 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_THREAD);
Aaron Smithe3037902018-10-10 18:30:32 +0000395 LLDB_LOG(log, "Thread {0} spawned in process {1}", thread_id,
Pavel Labatha385d2c2017-02-22 10:38:02 +0000396 m_process.GetProcessId());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 HostThread thread(info.hThread);
398 thread.GetNativeThread().SetOwnsHandle(false);
399 m_debug_delegate->OnCreateThread(thread);
400 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000401}
402
403DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404DebuggerThread::HandleCreateProcessEvent(const CREATE_PROCESS_DEBUG_INFO &info,
405 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000406 Log *log =
407 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_PROCESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408 uint32_t process_id = ::GetProcessId(info.hProcess);
Zachary Turner610e5292015-05-07 21:39:33 +0000409
Pavel Labatha385d2c2017-02-22 10:38:02 +0000410 LLDB_LOG(log, "process {0} spawned", process_id);
Zachary Turner610e5292015-05-07 21:39:33 +0000411
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 std::string thread_name;
413 llvm::raw_string_ostream name_stream(thread_name);
414 name_stream << "lldb.plugin.process-windows.slave[" << process_id << "]";
415 name_stream.flush();
Zachary Turnered96be92017-03-04 01:31:06 +0000416 llvm::set_thread_name(thread_name);
Zachary Turner02862bc2014-11-07 23:44:13 +0000417
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 // info.hProcess and info.hThread are closed automatically by Windows when
419 // EXIT_PROCESS_DEBUG_EVENT is received.
420 m_process = HostProcess(info.hProcess);
421 ((HostProcessWindows &)m_process.GetNativeProcess()).SetOwnsHandle(false);
422 m_main_thread = HostThread(info.hThread);
423 m_main_thread.GetNativeThread().SetOwnsHandle(false);
424 m_image_file = info.hFile;
Zachary Turner02862bc2014-11-07 23:44:13 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfImage);
427 m_debug_delegate->OnDebuggerConnected(load_addr);
Zachary Turner02862bc2014-11-07 23:44:13 +0000428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000430}
431
432DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433DebuggerThread::HandleExitThreadEvent(const EXIT_THREAD_DEBUG_INFO &info,
434 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000435 Log *log =
436 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_THREAD);
437 LLDB_LOG(log, "Thread {0} exited with code {1} in process {2}", thread_id,
438 info.dwExitCode, m_process.GetProcessId());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439 m_debug_delegate->OnExitThread(thread_id, info.dwExitCode);
440 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000441}
442
443DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444DebuggerThread::HandleExitProcessEvent(const EXIT_PROCESS_DEBUG_INFO &info,
445 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000446 Log *log =
447 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_THREAD);
448 LLDB_LOG(log, "process {0} exited with code {1}", m_process.GetProcessId(),
449 info.dwExitCode);
Zachary Turner610e5292015-05-07 21:39:33 +0000450
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 m_debug_delegate->OnExitProcess(info.dwExitCode);
Adrian McCarthy2f8e4c32015-05-18 23:24:32 +0000452
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000454}
455
456DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457DebuggerThread::HandleLoadDllEvent(const LOAD_DLL_DEBUG_INFO &info,
458 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000459 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 if (info.hFile == nullptr) {
461 // Not sure what this is, so just ignore it.
Pavel Labatha385d2c2017-02-22 10:38:02 +0000462 LLDB_LOG(log, "Warning: Inferior {0} has a NULL file handle, returning...",
463 m_process.GetProcessId());
Zachary Turner02862bc2014-11-07 23:44:13 +0000464 return DBG_CONTINUE;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 }
466
467 std::vector<wchar_t> buffer(1);
468 DWORD required_size =
469 GetFinalPathNameByHandleW(info.hFile, &buffer[0], 0, VOLUME_NAME_DOS);
470 if (required_size > 0) {
471 buffer.resize(required_size + 1);
472 required_size = GetFinalPathNameByHandleW(info.hFile, &buffer[0],
473 required_size, VOLUME_NAME_DOS);
474 std::string path_str_utf8;
475 llvm::convertWideToUTF8(buffer.data(), path_str_utf8);
476 llvm::StringRef path_str = path_str_utf8;
477 const char *path = path_str.data();
478 if (path_str.startswith("\\\\?\\"))
479 path += 4;
480
Aleksandr Urakov54bb3162018-11-02 08:47:33 +0000481 FileSpec file_spec(path);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482 ModuleSpec module_spec(file_spec);
483 lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll);
484
Pavel Labatha385d2c2017-02-22 10:38:02 +0000485 LLDB_LOG(log, "Inferior {0} - DLL '{1}' loaded at address {2:x}...",
486 m_process.GetProcessId(), path, info.lpBaseOfDll);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487
488 m_debug_delegate->OnLoadDll(module_spec, load_addr);
489 } else {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000490 LLDB_LOG(
491 log,
492 "Inferior {0} - Error {1} occurred calling GetFinalPathNameByHandle",
493 m_process.GetProcessId(), ::GetLastError());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 }
495 // Windows does not automatically close info.hFile, so we need to do it.
496 ::CloseHandle(info.hFile);
497 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000498}
499
500DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501DebuggerThread::HandleUnloadDllEvent(const UNLOAD_DLL_DEBUG_INFO &info,
502 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000503 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT);
504 LLDB_LOG(log, "process {0} unloading DLL at addr {1:x}.",
505 m_process.GetProcessId(), info.lpBaseOfDll);
Zachary Turner610e5292015-05-07 21:39:33 +0000506
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 m_debug_delegate->OnUnloadDll(
508 reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll));
509 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000510}
511
512DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513DebuggerThread::HandleODSEvent(const OUTPUT_DEBUG_STRING_INFO &info,
514 DWORD thread_id) {
515 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000516}
517
518DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519DebuggerThread::HandleRipEvent(const RIP_INFO &info, DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000520 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT);
521 LLDB_LOG(log, "encountered error {0} (type={1}) in process {2} thread {3}",
522 info.dwError, info.dwType, m_process.GetProcessId(), thread_id);
Zachary Turner610e5292015-05-07 21:39:33 +0000523
Zachary Turner97206d52017-05-12 04:51:55 +0000524 Status error(info.dwError, eErrorTypeWin32);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525 m_debug_delegate->OnDebuggerError(error, info.dwType);
Zachary Turner02862bc2014-11-07 23:44:13 +0000526
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000528}