blob: 58769bdd70da0bd9a8bd03487bd03b02157afd63 [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
32using namespace lldb;
33using namespace lldb_private;
34
Kate Stoneb9c1b512016-09-06 20:57:50 +000035namespace {
36struct DebugLaunchContext {
37 DebugLaunchContext(DebuggerThread *thread,
38 const ProcessLaunchInfo &launch_info)
39 : m_thread(thread), m_launch_info(launch_info) {}
40 DebuggerThread *m_thread;
41 ProcessLaunchInfo m_launch_info;
Zachary Turner02862bc2014-11-07 23:44:13 +000042};
Zachary Turnerc62733b2015-05-20 18:31:17 +000043
Kate Stoneb9c1b512016-09-06 20:57:50 +000044struct DebugAttachContext {
45 DebugAttachContext(DebuggerThread *thread, lldb::pid_t pid,
46 const ProcessAttachInfo &attach_info)
47 : m_thread(thread), m_pid(pid), m_attach_info(attach_info) {}
48 DebuggerThread *m_thread;
49 lldb::pid_t m_pid;
50 ProcessAttachInfo m_attach_info;
Zachary Turnerc62733b2015-05-20 18:31:17 +000051};
Aaron Smithe3037902018-10-10 18:30:32 +000052} // namespace
Zachary Turner02862bc2014-11-07 23:44:13 +000053
54DebuggerThread::DebuggerThread(DebugDelegateSP debug_delegate)
Zachary Turner5a8ad4592016-10-05 17:07:34 +000055 : m_debug_delegate(debug_delegate), m_pid_to_detach(0),
56 m_is_shutting_down(false) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 m_debugging_ended_event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
Zachary Turner02862bc2014-11-07 23:44:13 +000058}
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060DebuggerThread::~DebuggerThread() { ::CloseHandle(m_debugging_ended_event); }
61
Zachary Turner97206d52017-05-12 04:51:55 +000062Status DebuggerThread::DebugLaunch(const ProcessLaunchInfo &launch_info) {
Pavel Labatha385d2c2017-02-22 10:38:02 +000063 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
64 LLDB_LOG(log, "launching '{0}'", launch_info.GetExecutableFile().GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +000065
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000066 Status result;
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 DebugLaunchContext *context = new DebugLaunchContext(this, launch_info);
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000068
69 llvm::Expected<HostThread> slave_thread = ThreadLauncher::LaunchThread(
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 "lldb.plugin.process-windows.slave[?]", DebuggerThreadLaunchRoutine,
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000071 context);
72 if (!slave_thread) {
73 result = Status(slave_thread.takeError());
74 LLDB_LOG(log, "couldn't launch debugger thread. {0}", result);
75 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000076
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000077 return result;
Zachary Turner02862bc2014-11-07 23:44:13 +000078}
79
Zachary Turner97206d52017-05-12 04:51:55 +000080Status DebuggerThread::DebugAttach(lldb::pid_t pid,
81 const ProcessAttachInfo &attach_info) {
Pavel Labatha385d2c2017-02-22 10:38:02 +000082 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
83 LLDB_LOG(log, "attaching to '{0}'", pid);
Zachary Turner02862bc2014-11-07 23:44:13 +000084
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000085 Status result;
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 DebugAttachContext *context = new DebugAttachContext(this, pid, attach_info);
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000087
88 llvm::Expected<HostThread> slave_thread = ThreadLauncher::LaunchThread(
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 "lldb.plugin.process-windows.slave[?]", DebuggerThreadAttachRoutine,
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000090 context);
91 if (!slave_thread) {
92 result = Status(slave_thread.takeError());
93 LLDB_LOG(log, "couldn't attach to process '{0}'. {1}", pid, result);
94 }
Zachary Turner610e5292015-05-07 21:39:33 +000095
Stella Stamenova631b5f7d2019-07-09 18:10:36 +000096 return result;
Zachary Turner02862bc2014-11-07 23:44:13 +000097}
98
Kate Stoneb9c1b512016-09-06 20:57:50 +000099lldb::thread_result_t DebuggerThread::DebuggerThreadLaunchRoutine(void *data) {
100 DebugLaunchContext *context = static_cast<DebugLaunchContext *>(data);
101 lldb::thread_result_t result =
102 context->m_thread->DebuggerThreadLaunchRoutine(context->m_launch_info);
103 delete context;
104 return result;
Zachary Turnerc62733b2015-05-20 18:31:17 +0000105}
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107lldb::thread_result_t DebuggerThread::DebuggerThreadAttachRoutine(void *data) {
108 DebugAttachContext *context = static_cast<DebugAttachContext *>(data);
109 lldb::thread_result_t result = context->m_thread->DebuggerThreadAttachRoutine(
110 context->m_pid, context->m_attach_info);
111 delete context;
112 return result;
Zachary Turner02862bc2014-11-07 23:44:13 +0000113}
114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115lldb::thread_result_t DebuggerThread::DebuggerThreadLaunchRoutine(
116 const ProcessLaunchInfo &launch_info) {
117 // Grab a shared_ptr reference to this so that we know it won't get deleted
Adrian Prantl05097242018-04-30 16:49:04 +0000118 // until after the thread routine has exited.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
Zachary Turnerc62733b2015-05-20 18:31:17 +0000120
Pavel Labatha385d2c2017-02-22 10:38:02 +0000121 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
122 LLDB_LOG(log, "preparing to launch '{0}' on background thread.",
123 launch_info.GetExecutableFile().GetPath());
Zachary Turner02862bc2014-11-07 23:44:13 +0000124
Zachary Turner97206d52017-05-12 04:51:55 +0000125 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 ProcessLauncherWindows launcher;
127 HostProcess process(launcher.LaunchProcess(launch_info, error));
128 // If we couldn't create the process, notify waiters immediately. Otherwise
Adrian Prantl05097242018-04-30 16:49:04 +0000129 // enter the debug loop and wait until we get the create process debug
130 // notification. Note that if the process was created successfully, we can
131 // throw away the process handle we got from CreateProcess because Windows
132 // will give us another (potentially more useful?) handle when it sends us
133 // the CREATE_PROCESS_DEBUG_EVENT.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 if (error.Success())
Zachary Turnerc62733b2015-05-20 18:31:17 +0000135 DebugLoop();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 else
137 m_debug_delegate->OnDebuggerError(error, 0);
Zachary Turnerc62733b2015-05-20 18:31:17 +0000138
Konrad Kleine85200642019-05-23 15:17:39 +0000139 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140}
141
142lldb::thread_result_t DebuggerThread::DebuggerThreadAttachRoutine(
143 lldb::pid_t pid, const ProcessAttachInfo &attach_info) {
144 // Grab a shared_ptr reference to this so that we know it won't get deleted
Adrian Prantl05097242018-04-30 16:49:04 +0000145 // until after the thread routine has exited.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
147
Pavel Labatha385d2c2017-02-22 10:38:02 +0000148 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
149 LLDB_LOG(log, "preparing to attach to process '{0}' on background thread.",
150 pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151
152 if (!DebugActiveProcess((DWORD)pid)) {
Zachary Turner97206d52017-05-12 04:51:55 +0000153 Status error(::GetLastError(), eErrorTypeWin32);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 m_debug_delegate->OnDebuggerError(error, 0);
Konrad Kleine85200642019-05-23 15:17:39 +0000155 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 }
157
Adrian Prantl05097242018-04-30 16:49:04 +0000158 // The attach was successful, enter the debug loop. From here on out, this
159 // is no different than a create process operation, so all the same comments
160 // in DebugLaunch should apply from this point out.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 DebugLoop();
162
Konrad Kleine85200642019-05-23 15:17:39 +0000163 return {};
Zachary Turnerc62733b2015-05-20 18:31:17 +0000164}
165
Zachary Turner97206d52017-05-12 04:51:55 +0000166Status DebuggerThread::StopDebugging(bool terminate) {
167 Status error;
Zachary Turnerc6a66532014-12-03 22:04:18 +0000168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 lldb::pid_t pid = m_process.GetProcessId();
Zachary Turner610e5292015-05-07 21:39:33 +0000170
Pavel Labatha385d2c2017-02-22 10:38:02 +0000171 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
172 LLDB_LOG(log, "terminate = {0}, inferior={1}.", terminate, pid);
Zachary Turner610e5292015-05-07 21:39:33 +0000173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 // Set m_is_shutting_down to true if it was false. Return if it was already
175 // true.
176 bool expected = false;
177 if (!m_is_shutting_down.compare_exchange_strong(expected, true))
Zachary Turnerc6a66532014-12-03 22:04:18 +0000178 return error;
Zachary Turnerc6a66532014-12-03 22:04:18 +0000179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 // Make a copy of the process, since the termination sequence will reset
181 // DebuggerThread's internal copy and it needs to remain open for the Wait
182 // operation.
183 HostProcess process_copy = m_process;
184 lldb::process_t handle = m_process.GetNativeProcess().GetSystemHandle();
Zachary Turnerc6a66532014-12-03 22:04:18 +0000185
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 if (terminate) {
Stella Stamenova9d6fabf2018-06-13 19:02:44 +0000187 if (handle != nullptr && handle != LLDB_INVALID_PROCESS) {
188 // Initiate the termination before continuing the exception, so that the
189 // next debug event we get is the exit process event, and not some other
190 // event.
191 BOOL terminate_suceeded = TerminateProcess(handle, 0);
192 LLDB_LOG(log,
193 "calling TerminateProcess({0}, 0) (inferior={1}), success={2}",
194 handle, pid, terminate_suceeded);
195 } else {
196 LLDB_LOG(log,
Aaron Smithe3037902018-10-10 18:30:32 +0000197 "NOT calling TerminateProcess because the inferior is not valid "
198 "({0}, 0) (inferior={1})",
Stella Stamenova9d6fabf2018-06-13 19:02:44 +0000199 handle, pid);
200 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 }
Zachary Turner610e5292015-05-07 21:39:33 +0000202
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 // If we're stuck waiting for an exception to continue (e.g. the user is at a
Adrian Prantl05097242018-04-30 16:49:04 +0000204 // breakpoint messing around in the debugger), continue it now. But only
205 // AFTER calling TerminateProcess to make sure that the very next call to
206 // WaitForDebugEvent is an exit process event.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 if (m_active_exception.get()) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000208 LLDB_LOG(log, "masking active exception");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 ContinueAsyncException(ExceptionResult::MaskException);
210 }
211
212 if (!terminate) {
213 // Indicate that we want to detach.
214 m_pid_to_detach = GetProcess().GetProcessId();
215
216 // Force a fresh break so that the detach can happen from the debugger
217 // thread.
218 if (!::DebugBreakProcess(
219 GetProcess().GetNativeProcess().GetSystemHandle())) {
220 error.SetError(::GetLastError(), eErrorTypeWin32);
Zachary Turnerc6a66532014-12-03 22:04:18 +0000221 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 }
223
Pavel Labatha385d2c2017-02-22 10:38:02 +0000224 LLDB_LOG(log, "waiting for detach from process {0} to complete.", pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225
226 DWORD wait_result = WaitForSingleObject(m_debugging_ended_event, 5000);
227 if (wait_result != WAIT_OBJECT_0) {
228 error.SetError(GetLastError(), eErrorTypeWin32);
Pavel Labatha385d2c2017-02-22 10:38:02 +0000229 LLDB_LOG(log, "error: WaitForSingleObject({0}, 5000) returned {1}",
230 m_debugging_ended_event, wait_result);
231 } else
232 LLDB_LOG(log, "detach from process {0} completed successfully.", pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233
234 if (!error.Success()) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000235 LLDB_LOG(log, "encountered an error while trying to stop process {0}. {1}",
236 pid, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 }
238 return error;
Zachary Turnerdcd80372014-11-11 00:00:14 +0000239}
240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241void DebuggerThread::ContinueAsyncException(ExceptionResult result) {
242 if (!m_active_exception.get())
243 return;
Zachary Turnerdcd80372014-11-11 00:00:14 +0000244
Pavel Labatha385d2c2017-02-22 10:38:02 +0000245 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS |
246 WINDOWS_LOG_EXCEPTION);
247 LLDB_LOG(log, "broadcasting for inferior process {0}.",
248 m_process.GetProcessId());
Zachary Turner6c3c0ed2015-10-02 22:47:04 +0000249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 m_active_exception.reset();
251 m_exception_pred.SetValue(result, eBroadcastAlways);
252}
Zachary Turner02862bc2014-11-07 23:44:13 +0000253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254void DebuggerThread::FreeProcessHandles() {
255 m_process = HostProcess();
256 m_main_thread = HostThread();
257 if (m_image_file) {
258 ::CloseHandle(m_image_file);
259 m_image_file = nullptr;
260 }
261}
Zachary Turner610e5292015-05-07 21:39:33 +0000262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263void DebuggerThread::DebugLoop() {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000264 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT);
Zachary Turner5a8ad4592016-10-05 17:07:34 +0000265 DEBUG_EVENT dbe = {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 bool should_debug = true;
Pavel Labatha385d2c2017-02-22 10:38:02 +0000267 LLDB_LOGV(log, "Entering WaitForDebugEvent loop");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 while (should_debug) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000269 LLDB_LOGV(log, "Calling WaitForDebugEvent");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 BOOL wait_result = WaitForDebugEvent(&dbe, INFINITE);
271 if (wait_result) {
272 DWORD continue_status = DBG_CONTINUE;
273 switch (dbe.dwDebugEventCode) {
Aaron Smithe3037902018-10-10 18:30:32 +0000274 default:
275 llvm_unreachable("Unhandle debug event code!");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 case EXCEPTION_DEBUG_EVENT: {
277 ExceptionResult status =
278 HandleExceptionEvent(dbe.u.Exception, dbe.dwThreadId);
Adrian McCarthya59a7212015-06-19 18:26:53 +0000279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 if (status == ExceptionResult::MaskException)
281 continue_status = DBG_CONTINUE;
282 else if (status == ExceptionResult::SendToApplication)
283 continue_status = DBG_EXCEPTION_NOT_HANDLED;
Zachary Turner610e5292015-05-07 21:39:33 +0000284
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 break;
286 }
287 case CREATE_THREAD_DEBUG_EVENT:
288 continue_status =
289 HandleCreateThreadEvent(dbe.u.CreateThread, dbe.dwThreadId);
290 break;
291 case CREATE_PROCESS_DEBUG_EVENT:
292 continue_status =
293 HandleCreateProcessEvent(dbe.u.CreateProcessInfo, dbe.dwThreadId);
294 break;
295 case EXIT_THREAD_DEBUG_EVENT:
296 continue_status =
297 HandleExitThreadEvent(dbe.u.ExitThread, dbe.dwThreadId);
298 break;
299 case EXIT_PROCESS_DEBUG_EVENT:
300 continue_status =
301 HandleExitProcessEvent(dbe.u.ExitProcess, dbe.dwThreadId);
302 should_debug = false;
303 break;
304 case LOAD_DLL_DEBUG_EVENT:
305 continue_status = HandleLoadDllEvent(dbe.u.LoadDll, dbe.dwThreadId);
306 break;
307 case UNLOAD_DLL_DEBUG_EVENT:
308 continue_status = HandleUnloadDllEvent(dbe.u.UnloadDll, dbe.dwThreadId);
309 break;
310 case OUTPUT_DEBUG_STRING_EVENT:
311 continue_status = HandleODSEvent(dbe.u.DebugString, dbe.dwThreadId);
312 break;
313 case RIP_EVENT:
314 continue_status = HandleRipEvent(dbe.u.RipInfo, dbe.dwThreadId);
315 if (dbe.u.RipInfo.dwType == SLE_ERROR)
316 should_debug = false;
317 break;
318 }
319
Pavel Labatha385d2c2017-02-22 10:38:02 +0000320 LLDB_LOGV(log, "calling ContinueDebugEvent({0}, {1}, {2}) on thread {3}.",
321 dbe.dwProcessId, dbe.dwThreadId, continue_status,
322 ::GetCurrentThreadId());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323
324 ::ContinueDebugEvent(dbe.dwProcessId, dbe.dwThreadId, continue_status);
325
326 if (m_detached) {
327 should_debug = false;
328 }
329 } else {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000330 LLDB_LOG(log, "returned FALSE from WaitForDebugEvent. Error = {0}",
331 ::GetLastError());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332
333 should_debug = false;
Zachary Turner02862bc2014-11-07 23:44:13 +0000334 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 }
336 FreeProcessHandles();
Zachary Turner3c1c5b92015-05-21 19:56:26 +0000337
Pavel Labatha385d2c2017-02-22 10:38:02 +0000338 LLDB_LOG(log, "WaitForDebugEvent loop completed, exiting.");
Aaron Smithe3037902018-10-10 18:30:32 +0000339 ::SetEvent(m_debugging_ended_event);
Zachary Turner02862bc2014-11-07 23:44:13 +0000340}
341
Zachary Turnerdcd80372014-11-11 00:00:14 +0000342ExceptionResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343DebuggerThread::HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info,
344 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000345 Log *log =
346 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 if (m_is_shutting_down) {
348 // A breakpoint that occurs while `m_pid_to_detach` is non-zero is a magic
349 // exception that
350 // we use simply to wake up the DebuggerThread so that we can close out the
351 // debug loop.
352 if (m_pid_to_detach != 0 &&
353 info.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000354 LLDB_LOG(log, "Breakpoint exception is cue to detach from process {0:x}",
355 m_pid_to_detach.load());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 ::DebugActiveProcessStop(m_pid_to_detach);
357 m_detached = true;
Zachary Turner6c3c0ed2015-10-02 22:47:04 +0000358 }
359
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 // Don't perform any blocking operations while we're shutting down. That
Adrian Prantl05097242018-04-30 16:49:04 +0000361 // will cause TerminateProcess -> WaitForSingleObject to time out.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 return ExceptionResult::SendToApplication;
363 }
Zachary Turnerc6a66532014-12-03 22:04:18 +0000364
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 bool first_chance = (info.dwFirstChance != 0);
Zachary Turnerc6a66532014-12-03 22:04:18 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 m_active_exception.reset(
368 new ExceptionRecord(info.ExceptionRecord, thread_id));
Pavel Labatha385d2c2017-02-22 10:38:02 +0000369 LLDB_LOG(log, "encountered {0} chance exception {1:x} on thread {2:x}",
370 first_chance ? "first" : "second",
371 info.ExceptionRecord.ExceptionCode, thread_id);
Zachary Turner610e5292015-05-07 21:39:33 +0000372
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373 ExceptionResult result =
374 m_debug_delegate->OnDebugException(first_chance, *m_active_exception);
375 m_exception_pred.SetValue(result, eBroadcastNever);
Zachary Turner610e5292015-05-07 21:39:33 +0000376
Pavel Labatha385d2c2017-02-22 10:38:02 +0000377 LLDB_LOG(log, "waiting for ExceptionPred != BreakInDebugger");
Pavel Labath4b130332018-05-09 14:49:43 +0000378 result = *m_exception_pred.WaitForValueNotEqualTo(
379 ExceptionResult::BreakInDebugger);
Zachary Turner610e5292015-05-07 21:39:33 +0000380
Pavel Labatha385d2c2017-02-22 10:38:02 +0000381 LLDB_LOG(log, "got ExceptionPred = {0}", (int)m_exception_pred.GetValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 return result;
Zachary Turner02862bc2014-11-07 23:44:13 +0000383}
384
385DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386DebuggerThread::HandleCreateThreadEvent(const CREATE_THREAD_DEBUG_INFO &info,
387 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000388 Log *log =
389 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_THREAD);
Aaron Smithe3037902018-10-10 18:30:32 +0000390 LLDB_LOG(log, "Thread {0} spawned in process {1}", thread_id,
Pavel Labatha385d2c2017-02-22 10:38:02 +0000391 m_process.GetProcessId());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392 HostThread thread(info.hThread);
393 thread.GetNativeThread().SetOwnsHandle(false);
394 m_debug_delegate->OnCreateThread(thread);
395 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000396}
397
398DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399DebuggerThread::HandleCreateProcessEvent(const CREATE_PROCESS_DEBUG_INFO &info,
400 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000401 Log *log =
402 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_PROCESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 uint32_t process_id = ::GetProcessId(info.hProcess);
Zachary Turner610e5292015-05-07 21:39:33 +0000404
Pavel Labatha385d2c2017-02-22 10:38:02 +0000405 LLDB_LOG(log, "process {0} spawned", process_id);
Zachary Turner610e5292015-05-07 21:39:33 +0000406
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407 std::string thread_name;
408 llvm::raw_string_ostream name_stream(thread_name);
409 name_stream << "lldb.plugin.process-windows.slave[" << process_id << "]";
410 name_stream.flush();
Zachary Turnered96be92017-03-04 01:31:06 +0000411 llvm::set_thread_name(thread_name);
Zachary Turner02862bc2014-11-07 23:44:13 +0000412
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413 // info.hProcess and info.hThread are closed automatically by Windows when
414 // EXIT_PROCESS_DEBUG_EVENT is received.
415 m_process = HostProcess(info.hProcess);
416 ((HostProcessWindows &)m_process.GetNativeProcess()).SetOwnsHandle(false);
417 m_main_thread = HostThread(info.hThread);
418 m_main_thread.GetNativeThread().SetOwnsHandle(false);
419 m_image_file = info.hFile;
Zachary Turner02862bc2014-11-07 23:44:13 +0000420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfImage);
422 m_debug_delegate->OnDebuggerConnected(load_addr);
Zachary Turner02862bc2014-11-07 23:44:13 +0000423
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000425}
426
427DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428DebuggerThread::HandleExitThreadEvent(const EXIT_THREAD_DEBUG_INFO &info,
429 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000430 Log *log =
431 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_THREAD);
432 LLDB_LOG(log, "Thread {0} exited with code {1} in process {2}", thread_id,
433 info.dwExitCode, m_process.GetProcessId());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 m_debug_delegate->OnExitThread(thread_id, info.dwExitCode);
435 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000436}
437
438DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439DebuggerThread::HandleExitProcessEvent(const EXIT_PROCESS_DEBUG_INFO &info,
440 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000441 Log *log =
442 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_THREAD);
443 LLDB_LOG(log, "process {0} exited with code {1}", m_process.GetProcessId(),
444 info.dwExitCode);
Zachary Turner610e5292015-05-07 21:39:33 +0000445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 m_debug_delegate->OnExitProcess(info.dwExitCode);
Adrian McCarthy2f8e4c32015-05-18 23:24:32 +0000447
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000449}
450
451DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452DebuggerThread::HandleLoadDllEvent(const LOAD_DLL_DEBUG_INFO &info,
453 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000454 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 if (info.hFile == nullptr) {
456 // Not sure what this is, so just ignore it.
Pavel Labatha385d2c2017-02-22 10:38:02 +0000457 LLDB_LOG(log, "Warning: Inferior {0} has a NULL file handle, returning...",
458 m_process.GetProcessId());
Zachary Turner02862bc2014-11-07 23:44:13 +0000459 return DBG_CONTINUE;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 }
461
462 std::vector<wchar_t> buffer(1);
463 DWORD required_size =
464 GetFinalPathNameByHandleW(info.hFile, &buffer[0], 0, VOLUME_NAME_DOS);
465 if (required_size > 0) {
466 buffer.resize(required_size + 1);
467 required_size = GetFinalPathNameByHandleW(info.hFile, &buffer[0],
468 required_size, VOLUME_NAME_DOS);
469 std::string path_str_utf8;
470 llvm::convertWideToUTF8(buffer.data(), path_str_utf8);
471 llvm::StringRef path_str = path_str_utf8;
472 const char *path = path_str.data();
473 if (path_str.startswith("\\\\?\\"))
474 path += 4;
475
Aleksandr Urakov54bb3162018-11-02 08:47:33 +0000476 FileSpec file_spec(path);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 ModuleSpec module_spec(file_spec);
478 lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll);
479
Pavel Labatha385d2c2017-02-22 10:38:02 +0000480 LLDB_LOG(log, "Inferior {0} - DLL '{1}' loaded at address {2:x}...",
481 m_process.GetProcessId(), path, info.lpBaseOfDll);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482
483 m_debug_delegate->OnLoadDll(module_spec, load_addr);
484 } else {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000485 LLDB_LOG(
486 log,
487 "Inferior {0} - Error {1} occurred calling GetFinalPathNameByHandle",
488 m_process.GetProcessId(), ::GetLastError());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 }
490 // Windows does not automatically close info.hFile, so we need to do it.
491 ::CloseHandle(info.hFile);
492 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000493}
494
495DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496DebuggerThread::HandleUnloadDllEvent(const UNLOAD_DLL_DEBUG_INFO &info,
497 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000498 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT);
499 LLDB_LOG(log, "process {0} unloading DLL at addr {1:x}.",
500 m_process.GetProcessId(), info.lpBaseOfDll);
Zachary Turner610e5292015-05-07 21:39:33 +0000501
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502 m_debug_delegate->OnUnloadDll(
503 reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll));
504 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000505}
506
507DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508DebuggerThread::HandleODSEvent(const OUTPUT_DEBUG_STRING_INFO &info,
509 DWORD thread_id) {
510 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000511}
512
513DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514DebuggerThread::HandleRipEvent(const RIP_INFO &info, DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000515 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT);
516 LLDB_LOG(log, "encountered error {0} (type={1}) in process {2} thread {3}",
517 info.dwError, info.dwType, m_process.GetProcessId(), thread_id);
Zachary Turner610e5292015-05-07 21:39:33 +0000518
Zachary Turner97206d52017-05-12 04:51:55 +0000519 Status error(info.dwError, eErrorTypeWin32);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 m_debug_delegate->OnDebuggerError(error, info.dwType);
Zachary Turner02862bc2014-11-07 23:44:13 +0000521
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000523}