blob: badab621925d3fa21d8f6d203d1ef2830dc50ac5 [file] [log] [blame]
Alexander Shaposhnikov696bd632016-11-26 05:23:44 +00001//===-- DebuggerThread.cpp --------------------------------------*- C++ -*-===//
Zachary Turner02862bc2014-11-07 23:44:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "DebuggerThread.h"
Zachary Turnerdcd80372014-11-11 00:00:14 +000011#include "ExceptionRecord.h"
Zachary Turner02862bc2014-11-07 23:44:13 +000012#include "IDebugDelegate.h"
Zachary Turner02862bc2014-11-07 23:44:13 +000013
Zachary Turnera32d2ce2014-11-12 19:31:56 +000014#include "lldb/Core/ModuleSpec.h"
Zachary Turner02862bc2014-11-07 23:44:13 +000015#include "lldb/Host/Predicate.h"
Zachary Turner02862bc2014-11-07 23:44:13 +000016#include "lldb/Host/ThreadLauncher.h"
17#include "lldb/Host/windows/HostProcessWindows.h"
18#include "lldb/Host/windows/HostThreadWindows.h"
19#include "lldb/Host/windows/ProcessLauncherWindows.h"
Zachary Turnerc62733b2015-05-20 18:31:17 +000020#include "lldb/Target/Process.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000021#include "lldb/Target/ProcessLaunchInfo.h"
Zachary Turner5713a052017-03-22 18:40:07 +000022#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000023#include "lldb/Utility/Log.h"
Zachary Turner97206d52017-05-12 04:51:55 +000024#include "lldb/Utility/Status.h"
Zachary Turner02862bc2014-11-07 23:44:13 +000025
Adrian McCarthy18a9135d2015-10-28 18:21:45 +000026#include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
Zachary Turner610e5292015-05-07 21:39:33 +000027
Zachary Turnera32d2ce2014-11-12 19:31:56 +000028#include "llvm/ADT/STLExtras.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000029#include "llvm/Support/ConvertUTF.h"
Zachary Turnered96be92017-03-04 01:31:06 +000030#include "llvm/Support/Threading.h"
Zachary Turner02862bc2014-11-07 23:44:13 +000031#include "llvm/Support/raw_ostream.h"
32
33using namespace lldb;
34using namespace lldb_private;
35
Kate Stoneb9c1b512016-09-06 20:57:50 +000036namespace {
37struct 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;
Zachary Turner02862bc2014-11-07 23:44:13 +000043};
Zachary Turnerc62733b2015-05-20 18:31:17 +000044
Kate Stoneb9c1b512016-09-06 20:57:50 +000045struct 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;
50 lldb::pid_t m_pid;
51 ProcessAttachInfo m_attach_info;
Zachary Turnerc62733b2015-05-20 18:31:17 +000052};
Zachary Turner02862bc2014-11-07 23:44:13 +000053}
54
55DebuggerThread::DebuggerThread(DebugDelegateSP debug_delegate)
Zachary Turner5a8ad4592016-10-05 17:07:34 +000056 : m_debug_delegate(debug_delegate), m_pid_to_detach(0),
57 m_is_shutting_down(false) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 m_debugging_ended_event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
Zachary Turner02862bc2014-11-07 23:44:13 +000059}
60
Kate Stoneb9c1b512016-09-06 20:57:50 +000061DebuggerThread::~DebuggerThread() { ::CloseHandle(m_debugging_ended_event); }
62
Zachary Turner97206d52017-05-12 04:51:55 +000063Status DebuggerThread::DebugLaunch(const ProcessLaunchInfo &launch_info) {
Pavel Labatha385d2c2017-02-22 10:38:02 +000064 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
65 LLDB_LOG(log, "launching '{0}'", launch_info.GetExecutableFile().GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +000066
Zachary Turner97206d52017-05-12 04:51:55 +000067 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 DebugLaunchContext *context = new DebugLaunchContext(this, launch_info);
69 HostThread slave_thread(ThreadLauncher::LaunchThread(
70 "lldb.plugin.process-windows.slave[?]", DebuggerThreadLaunchRoutine,
71 context, &error));
72
Pavel Labatha385d2c2017-02-22 10:38:02 +000073 if (!error.Success())
74 LLDB_LOG(log, "couldn't launch debugger thread. {0}", error);
Kate Stoneb9c1b512016-09-06 20:57:50 +000075
76 return error;
Zachary Turner02862bc2014-11-07 23:44:13 +000077}
78
Zachary Turner97206d52017-05-12 04:51:55 +000079Status DebuggerThread::DebugAttach(lldb::pid_t pid,
80 const ProcessAttachInfo &attach_info) {
Pavel Labatha385d2c2017-02-22 10:38:02 +000081 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
82 LLDB_LOG(log, "attaching to '{0}'", pid);
Zachary Turner02862bc2014-11-07 23:44:13 +000083
Zachary Turner97206d52017-05-12 04:51:55 +000084 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 DebugAttachContext *context = new DebugAttachContext(this, pid, attach_info);
86 HostThread slave_thread(ThreadLauncher::LaunchThread(
87 "lldb.plugin.process-windows.slave[?]", DebuggerThreadAttachRoutine,
88 context, &error));
Zachary Turner610e5292015-05-07 21:39:33 +000089
Pavel Labatha385d2c2017-02-22 10:38:02 +000090 if (!error.Success())
91 LLDB_LOG(log, "couldn't attach to process '{0}'. {1}", pid, error);
Zachary Turner02862bc2014-11-07 23:44:13 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 return error;
Zachary Turner02862bc2014-11-07 23:44:13 +000094}
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096lldb::thread_result_t DebuggerThread::DebuggerThreadLaunchRoutine(void *data) {
97 DebugLaunchContext *context = static_cast<DebugLaunchContext *>(data);
98 lldb::thread_result_t result =
99 context->m_thread->DebuggerThreadLaunchRoutine(context->m_launch_info);
100 delete context;
101 return result;
Zachary Turnerc62733b2015-05-20 18:31:17 +0000102}
103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104lldb::thread_result_t DebuggerThread::DebuggerThreadAttachRoutine(void *data) {
105 DebugAttachContext *context = static_cast<DebugAttachContext *>(data);
106 lldb::thread_result_t result = context->m_thread->DebuggerThreadAttachRoutine(
107 context->m_pid, context->m_attach_info);
108 delete context;
109 return result;
Zachary Turner02862bc2014-11-07 23:44:13 +0000110}
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112lldb::thread_result_t DebuggerThread::DebuggerThreadLaunchRoutine(
113 const ProcessLaunchInfo &launch_info) {
114 // Grab a shared_ptr reference to this so that we know it won't get deleted
Adrian Prantl05097242018-04-30 16:49:04 +0000115 // until after the thread routine has exited.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
Zachary Turnerc62733b2015-05-20 18:31:17 +0000117
Pavel Labatha385d2c2017-02-22 10:38:02 +0000118 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
119 LLDB_LOG(log, "preparing to launch '{0}' on background thread.",
120 launch_info.GetExecutableFile().GetPath());
Zachary Turner02862bc2014-11-07 23:44:13 +0000121
Zachary Turner97206d52017-05-12 04:51:55 +0000122 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 ProcessLauncherWindows launcher;
124 HostProcess process(launcher.LaunchProcess(launch_info, error));
125 // If we couldn't create the process, notify waiters immediately. Otherwise
Adrian Prantl05097242018-04-30 16:49:04 +0000126 // enter the debug loop and wait until we get the create process debug
127 // notification. Note that if the process was created successfully, we can
128 // throw away the process handle we got from CreateProcess because Windows
129 // will give us another (potentially more useful?) handle when it sends us
130 // the CREATE_PROCESS_DEBUG_EVENT.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 if (error.Success())
Zachary Turnerc62733b2015-05-20 18:31:17 +0000132 DebugLoop();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 else
134 m_debug_delegate->OnDebuggerError(error, 0);
Zachary Turnerc62733b2015-05-20 18:31:17 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 return 0;
137}
138
139lldb::thread_result_t DebuggerThread::DebuggerThreadAttachRoutine(
140 lldb::pid_t pid, const ProcessAttachInfo &attach_info) {
141 // Grab a shared_ptr reference to this so that we know it won't get deleted
Adrian Prantl05097242018-04-30 16:49:04 +0000142 // until after the thread routine has exited.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
144
Pavel Labatha385d2c2017-02-22 10:38:02 +0000145 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
146 LLDB_LOG(log, "preparing to attach to process '{0}' on background thread.",
147 pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148
149 if (!DebugActiveProcess((DWORD)pid)) {
Zachary Turner97206d52017-05-12 04:51:55 +0000150 Status error(::GetLastError(), eErrorTypeWin32);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 m_debug_delegate->OnDebuggerError(error, 0);
Zachary Turnerc62733b2015-05-20 18:31:17 +0000152 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 }
154
Adrian Prantl05097242018-04-30 16:49:04 +0000155 // The attach was successful, enter the debug loop. From here on out, this
156 // is no different than a create process operation, so all the same comments
157 // in DebugLaunch should apply from this point out.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 DebugLoop();
159
160 return 0;
Zachary Turnerc62733b2015-05-20 18:31:17 +0000161}
162
Zachary Turner97206d52017-05-12 04:51:55 +0000163Status DebuggerThread::StopDebugging(bool terminate) {
164 Status error;
Zachary Turnerc6a66532014-12-03 22:04:18 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 lldb::pid_t pid = m_process.GetProcessId();
Zachary Turner610e5292015-05-07 21:39:33 +0000167
Pavel Labatha385d2c2017-02-22 10:38:02 +0000168 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
169 LLDB_LOG(log, "terminate = {0}, inferior={1}.", terminate, pid);
Zachary Turner610e5292015-05-07 21:39:33 +0000170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 // Set m_is_shutting_down to true if it was false. Return if it was already
172 // true.
173 bool expected = false;
174 if (!m_is_shutting_down.compare_exchange_strong(expected, true))
Zachary Turnerc6a66532014-12-03 22:04:18 +0000175 return error;
Zachary Turnerc6a66532014-12-03 22:04:18 +0000176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 // Make a copy of the process, since the termination sequence will reset
178 // DebuggerThread's internal copy and it needs to remain open for the Wait
179 // operation.
180 HostProcess process_copy = m_process;
181 lldb::process_t handle = m_process.GetNativeProcess().GetSystemHandle();
Zachary Turnerc6a66532014-12-03 22:04:18 +0000182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 if (terminate) {
184 // Initiate the termination before continuing the exception, so that the
Adrian Prantl05097242018-04-30 16:49:04 +0000185 // next debug event we get is the exit process event, and not some other
186 // event.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 BOOL terminate_suceeded = TerminateProcess(handle, 0);
Pavel Labatha385d2c2017-02-22 10:38:02 +0000188 LLDB_LOG(log,
189 "calling TerminateProcess({0}, 0) (inferior={1}), success={2}",
190 handle, pid, terminate_suceeded);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 }
Zachary Turner610e5292015-05-07 21:39:33 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 // If we're stuck waiting for an exception to continue (e.g. the user is at a
Adrian Prantl05097242018-04-30 16:49:04 +0000194 // breakpoint messing around in the debugger), continue it now. But only
195 // AFTER calling TerminateProcess to make sure that the very next call to
196 // WaitForDebugEvent is an exit process event.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 if (m_active_exception.get()) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000198 LLDB_LOG(log, "masking active exception");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 ContinueAsyncException(ExceptionResult::MaskException);
200 }
201
202 if (!terminate) {
203 // Indicate that we want to detach.
204 m_pid_to_detach = GetProcess().GetProcessId();
205
206 // Force a fresh break so that the detach can happen from the debugger
207 // thread.
208 if (!::DebugBreakProcess(
209 GetProcess().GetNativeProcess().GetSystemHandle())) {
210 error.SetError(::GetLastError(), eErrorTypeWin32);
Zachary Turnerc6a66532014-12-03 22:04:18 +0000211 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 }
213
Pavel Labatha385d2c2017-02-22 10:38:02 +0000214 LLDB_LOG(log, "waiting for detach from process {0} to complete.", pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215
216 DWORD wait_result = WaitForSingleObject(m_debugging_ended_event, 5000);
217 if (wait_result != WAIT_OBJECT_0) {
218 error.SetError(GetLastError(), eErrorTypeWin32);
Pavel Labatha385d2c2017-02-22 10:38:02 +0000219 LLDB_LOG(log, "error: WaitForSingleObject({0}, 5000) returned {1}",
220 m_debugging_ended_event, wait_result);
221 } else
222 LLDB_LOG(log, "detach from process {0} completed successfully.", pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223
224 if (!error.Success()) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000225 LLDB_LOG(log, "encountered an error while trying to stop process {0}. {1}",
226 pid, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 }
228 return error;
Zachary Turnerdcd80372014-11-11 00:00:14 +0000229}
230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231void DebuggerThread::ContinueAsyncException(ExceptionResult result) {
232 if (!m_active_exception.get())
233 return;
Zachary Turnerdcd80372014-11-11 00:00:14 +0000234
Pavel Labatha385d2c2017-02-22 10:38:02 +0000235 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS |
236 WINDOWS_LOG_EXCEPTION);
237 LLDB_LOG(log, "broadcasting for inferior process {0}.",
238 m_process.GetProcessId());
Zachary Turner6c3c0ed2015-10-02 22:47:04 +0000239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 m_active_exception.reset();
241 m_exception_pred.SetValue(result, eBroadcastAlways);
242}
Zachary Turner02862bc2014-11-07 23:44:13 +0000243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244void DebuggerThread::FreeProcessHandles() {
245 m_process = HostProcess();
246 m_main_thread = HostThread();
247 if (m_image_file) {
248 ::CloseHandle(m_image_file);
249 m_image_file = nullptr;
250 }
251}
Zachary Turner610e5292015-05-07 21:39:33 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253void DebuggerThread::DebugLoop() {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000254 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT);
Zachary Turner5a8ad4592016-10-05 17:07:34 +0000255 DEBUG_EVENT dbe = {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 bool should_debug = true;
Pavel Labatha385d2c2017-02-22 10:38:02 +0000257 LLDB_LOGV(log, "Entering WaitForDebugEvent loop");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 while (should_debug) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000259 LLDB_LOGV(log, "Calling WaitForDebugEvent");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 BOOL wait_result = WaitForDebugEvent(&dbe, INFINITE);
261 if (wait_result) {
262 DWORD continue_status = DBG_CONTINUE;
263 switch (dbe.dwDebugEventCode) {
264 case EXCEPTION_DEBUG_EVENT: {
265 ExceptionResult status =
266 HandleExceptionEvent(dbe.u.Exception, dbe.dwThreadId);
Adrian McCarthya59a7212015-06-19 18:26:53 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 if (status == ExceptionResult::MaskException)
269 continue_status = DBG_CONTINUE;
270 else if (status == ExceptionResult::SendToApplication)
271 continue_status = DBG_EXCEPTION_NOT_HANDLED;
Zachary Turner610e5292015-05-07 21:39:33 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 break;
274 }
275 case CREATE_THREAD_DEBUG_EVENT:
276 continue_status =
277 HandleCreateThreadEvent(dbe.u.CreateThread, dbe.dwThreadId);
278 break;
279 case CREATE_PROCESS_DEBUG_EVENT:
280 continue_status =
281 HandleCreateProcessEvent(dbe.u.CreateProcessInfo, dbe.dwThreadId);
282 break;
283 case EXIT_THREAD_DEBUG_EVENT:
284 continue_status =
285 HandleExitThreadEvent(dbe.u.ExitThread, dbe.dwThreadId);
286 break;
287 case EXIT_PROCESS_DEBUG_EVENT:
288 continue_status =
289 HandleExitProcessEvent(dbe.u.ExitProcess, dbe.dwThreadId);
290 should_debug = false;
291 break;
292 case LOAD_DLL_DEBUG_EVENT:
293 continue_status = HandleLoadDllEvent(dbe.u.LoadDll, dbe.dwThreadId);
294 break;
295 case UNLOAD_DLL_DEBUG_EVENT:
296 continue_status = HandleUnloadDllEvent(dbe.u.UnloadDll, dbe.dwThreadId);
297 break;
298 case OUTPUT_DEBUG_STRING_EVENT:
299 continue_status = HandleODSEvent(dbe.u.DebugString, dbe.dwThreadId);
300 break;
301 case RIP_EVENT:
302 continue_status = HandleRipEvent(dbe.u.RipInfo, dbe.dwThreadId);
303 if (dbe.u.RipInfo.dwType == SLE_ERROR)
304 should_debug = false;
305 break;
306 }
307
Pavel Labatha385d2c2017-02-22 10:38:02 +0000308 LLDB_LOGV(log, "calling ContinueDebugEvent({0}, {1}, {2}) on thread {3}.",
309 dbe.dwProcessId, dbe.dwThreadId, continue_status,
310 ::GetCurrentThreadId());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311
312 ::ContinueDebugEvent(dbe.dwProcessId, dbe.dwThreadId, continue_status);
313
314 if (m_detached) {
315 should_debug = false;
316 }
317 } else {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000318 LLDB_LOG(log, "returned FALSE from WaitForDebugEvent. Error = {0}",
319 ::GetLastError());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320
321 should_debug = false;
Zachary Turner02862bc2014-11-07 23:44:13 +0000322 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 }
324 FreeProcessHandles();
Zachary Turner3c1c5b92015-05-21 19:56:26 +0000325
Pavel Labatha385d2c2017-02-22 10:38:02 +0000326 LLDB_LOG(log, "WaitForDebugEvent loop completed, exiting.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327 SetEvent(m_debugging_ended_event);
Zachary Turner02862bc2014-11-07 23:44:13 +0000328}
329
Zachary Turnerdcd80372014-11-11 00:00:14 +0000330ExceptionResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331DebuggerThread::HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info,
332 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000333 Log *log =
334 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 if (m_is_shutting_down) {
336 // A breakpoint that occurs while `m_pid_to_detach` is non-zero is a magic
337 // exception that
338 // we use simply to wake up the DebuggerThread so that we can close out the
339 // debug loop.
340 if (m_pid_to_detach != 0 &&
341 info.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000342 LLDB_LOG(log, "Breakpoint exception is cue to detach from process {0:x}",
343 m_pid_to_detach.load());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 ::DebugActiveProcessStop(m_pid_to_detach);
345 m_detached = true;
Zachary Turner6c3c0ed2015-10-02 22:47:04 +0000346 }
347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 // Don't perform any blocking operations while we're shutting down. That
Adrian Prantl05097242018-04-30 16:49:04 +0000349 // will cause TerminateProcess -> WaitForSingleObject to time out.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 return ExceptionResult::SendToApplication;
351 }
Zachary Turnerc6a66532014-12-03 22:04:18 +0000352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 bool first_chance = (info.dwFirstChance != 0);
Zachary Turnerc6a66532014-12-03 22:04:18 +0000354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 m_active_exception.reset(
356 new ExceptionRecord(info.ExceptionRecord, thread_id));
Pavel Labatha385d2c2017-02-22 10:38:02 +0000357 LLDB_LOG(log, "encountered {0} chance exception {1:x} on thread {2:x}",
358 first_chance ? "first" : "second",
359 info.ExceptionRecord.ExceptionCode, thread_id);
Zachary Turner610e5292015-05-07 21:39:33 +0000360
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 ExceptionResult result =
362 m_debug_delegate->OnDebugException(first_chance, *m_active_exception);
363 m_exception_pred.SetValue(result, eBroadcastNever);
Zachary Turner610e5292015-05-07 21:39:33 +0000364
Pavel Labatha385d2c2017-02-22 10:38:02 +0000365 LLDB_LOG(log, "waiting for ExceptionPred != BreakInDebugger");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 m_exception_pred.WaitForValueNotEqualTo(ExceptionResult::BreakInDebugger,
367 result);
Zachary Turner610e5292015-05-07 21:39:33 +0000368
Pavel Labatha385d2c2017-02-22 10:38:02 +0000369 LLDB_LOG(log, "got ExceptionPred = {0}", (int)m_exception_pred.GetValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 return result;
Zachary Turner02862bc2014-11-07 23:44:13 +0000371}
372
373DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374DebuggerThread::HandleCreateThreadEvent(const CREATE_THREAD_DEBUG_INFO &info,
375 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000376 Log *log =
377 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_THREAD);
378 LLDB_LOG(log, "Thread {0:x} spawned in process {1}", thread_id,
379 m_process.GetProcessId());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 HostThread thread(info.hThread);
381 thread.GetNativeThread().SetOwnsHandle(false);
382 m_debug_delegate->OnCreateThread(thread);
383 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000384}
385
386DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387DebuggerThread::HandleCreateProcessEvent(const CREATE_PROCESS_DEBUG_INFO &info,
388 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000389 Log *log =
390 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_PROCESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 uint32_t process_id = ::GetProcessId(info.hProcess);
Zachary Turner610e5292015-05-07 21:39:33 +0000392
Pavel Labatha385d2c2017-02-22 10:38:02 +0000393 LLDB_LOG(log, "process {0} spawned", process_id);
Zachary Turner610e5292015-05-07 21:39:33 +0000394
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 std::string thread_name;
396 llvm::raw_string_ostream name_stream(thread_name);
397 name_stream << "lldb.plugin.process-windows.slave[" << process_id << "]";
398 name_stream.flush();
Zachary Turnered96be92017-03-04 01:31:06 +0000399 llvm::set_thread_name(thread_name);
Zachary Turner02862bc2014-11-07 23:44:13 +0000400
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 // info.hProcess and info.hThread are closed automatically by Windows when
402 // EXIT_PROCESS_DEBUG_EVENT is received.
403 m_process = HostProcess(info.hProcess);
404 ((HostProcessWindows &)m_process.GetNativeProcess()).SetOwnsHandle(false);
405 m_main_thread = HostThread(info.hThread);
406 m_main_thread.GetNativeThread().SetOwnsHandle(false);
407 m_image_file = info.hFile;
Zachary Turner02862bc2014-11-07 23:44:13 +0000408
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfImage);
410 m_debug_delegate->OnDebuggerConnected(load_addr);
Zachary Turner02862bc2014-11-07 23:44:13 +0000411
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000413}
414
415DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416DebuggerThread::HandleExitThreadEvent(const EXIT_THREAD_DEBUG_INFO &info,
417 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000418 Log *log =
419 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_THREAD);
420 LLDB_LOG(log, "Thread {0} exited with code {1} in process {2}", thread_id,
421 info.dwExitCode, m_process.GetProcessId());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 m_debug_delegate->OnExitThread(thread_id, info.dwExitCode);
423 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000424}
425
426DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427DebuggerThread::HandleExitProcessEvent(const EXIT_PROCESS_DEBUG_INFO &info,
428 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000429 Log *log =
430 ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT | WINDOWS_LOG_THREAD);
431 LLDB_LOG(log, "process {0} exited with code {1}", m_process.GetProcessId(),
432 info.dwExitCode);
Zachary Turner610e5292015-05-07 21:39:33 +0000433
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 m_debug_delegate->OnExitProcess(info.dwExitCode);
Adrian McCarthy2f8e4c32015-05-18 23:24:32 +0000435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 FreeProcessHandles();
437 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000438}
439
440DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441DebuggerThread::HandleLoadDllEvent(const LOAD_DLL_DEBUG_INFO &info,
442 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000443 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 if (info.hFile == nullptr) {
445 // Not sure what this is, so just ignore it.
Pavel Labatha385d2c2017-02-22 10:38:02 +0000446 LLDB_LOG(log, "Warning: Inferior {0} has a NULL file handle, returning...",
447 m_process.GetProcessId());
Zachary Turner02862bc2014-11-07 23:44:13 +0000448 return DBG_CONTINUE;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 }
450
451 std::vector<wchar_t> buffer(1);
452 DWORD required_size =
453 GetFinalPathNameByHandleW(info.hFile, &buffer[0], 0, VOLUME_NAME_DOS);
454 if (required_size > 0) {
455 buffer.resize(required_size + 1);
456 required_size = GetFinalPathNameByHandleW(info.hFile, &buffer[0],
457 required_size, VOLUME_NAME_DOS);
458 std::string path_str_utf8;
459 llvm::convertWideToUTF8(buffer.data(), path_str_utf8);
460 llvm::StringRef path_str = path_str_utf8;
461 const char *path = path_str.data();
462 if (path_str.startswith("\\\\?\\"))
463 path += 4;
464
465 FileSpec file_spec(path, false);
466 ModuleSpec module_spec(file_spec);
467 lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll);
468
Pavel Labatha385d2c2017-02-22 10:38:02 +0000469 LLDB_LOG(log, "Inferior {0} - DLL '{1}' loaded at address {2:x}...",
470 m_process.GetProcessId(), path, info.lpBaseOfDll);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471
472 m_debug_delegate->OnLoadDll(module_spec, load_addr);
473 } else {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000474 LLDB_LOG(
475 log,
476 "Inferior {0} - Error {1} occurred calling GetFinalPathNameByHandle",
477 m_process.GetProcessId(), ::GetLastError());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 }
479 // Windows does not automatically close info.hFile, so we need to do it.
480 ::CloseHandle(info.hFile);
481 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000482}
483
484DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485DebuggerThread::HandleUnloadDllEvent(const UNLOAD_DLL_DEBUG_INFO &info,
486 DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000487 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT);
488 LLDB_LOG(log, "process {0} unloading DLL at addr {1:x}.",
489 m_process.GetProcessId(), info.lpBaseOfDll);
Zachary Turner610e5292015-05-07 21:39:33 +0000490
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 m_debug_delegate->OnUnloadDll(
492 reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll));
493 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000494}
495
496DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497DebuggerThread::HandleODSEvent(const OUTPUT_DEBUG_STRING_INFO &info,
498 DWORD thread_id) {
499 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000500}
501
502DWORD
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503DebuggerThread::HandleRipEvent(const RIP_INFO &info, DWORD thread_id) {
Pavel Labatha385d2c2017-02-22 10:38:02 +0000504 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EVENT);
505 LLDB_LOG(log, "encountered error {0} (type={1}) in process {2} thread {3}",
506 info.dwError, info.dwType, m_process.GetProcessId(), thread_id);
Zachary Turner610e5292015-05-07 21:39:33 +0000507
Zachary Turner97206d52017-05-12 04:51:55 +0000508 Status error(info.dwError, eErrorTypeWin32);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509 m_debug_delegate->OnDebuggerError(error, info.dwType);
Zachary Turner02862bc2014-11-07 23:44:13 +0000510
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 return DBG_CONTINUE;
Zachary Turner02862bc2014-11-07 23:44:13 +0000512}