Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 1 | //===-- DebuggerThread.DebuggerThread --------------------------------------*- C++ -*-===// |
| 2 | // |
| 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 Turner | dcd8037 | 2014-11-11 00:00:14 +0000 | [diff] [blame] | 11 | #include "ExceptionRecord.h" |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 12 | #include "IDebugDelegate.h" |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 13 | |
| 14 | #include "lldb/Core/Error.h" |
| 15 | #include "lldb/Core/Log.h" |
Zachary Turner | a32d2ce | 2014-11-12 19:31:56 +0000 | [diff] [blame] | 16 | #include "lldb/Core/ModuleSpec.h" |
| 17 | #include "lldb/Host/FileSpec.h" |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 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" |
Zachary Turner | c62733b | 2015-05-20 18:31:17 +0000 | [diff] [blame] | 25 | #include "lldb/Target/Process.h" |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 26 | |
Adrian McCarthy | 18a9135d | 2015-10-28 18:21:45 +0000 | [diff] [blame^] | 27 | #include "Plugins/Process/Windows/Common/ProcessWindowsLog.h" |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 28 | |
Zachary Turner | a32d2ce | 2014-11-12 19:31:56 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/STLExtras.h" |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
| 31 | |
| 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
| 34 | |
| 35 | namespace |
| 36 | { |
| 37 | struct DebugLaunchContext |
| 38 | { |
| 39 | DebugLaunchContext(DebuggerThread *thread, const ProcessLaunchInfo &launch_info) |
| 40 | : m_thread(thread) |
| 41 | , m_launch_info(launch_info) |
| 42 | { |
| 43 | } |
| 44 | DebuggerThread *m_thread; |
| 45 | ProcessLaunchInfo m_launch_info; |
| 46 | }; |
Zachary Turner | c62733b | 2015-05-20 18:31:17 +0000 | [diff] [blame] | 47 | |
| 48 | struct DebugAttachContext |
| 49 | { |
| 50 | DebugAttachContext(DebuggerThread *thread, lldb::pid_t pid, const ProcessAttachInfo &attach_info) |
| 51 | : m_thread(thread) |
| 52 | , m_pid(pid) |
| 53 | , m_attach_info(attach_info) |
| 54 | { |
| 55 | } |
| 56 | DebuggerThread *m_thread; |
| 57 | lldb::pid_t m_pid; |
| 58 | ProcessAttachInfo m_attach_info; |
| 59 | }; |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | DebuggerThread::DebuggerThread(DebugDelegateSP debug_delegate) |
| 63 | : m_debug_delegate(debug_delegate) |
| 64 | , m_image_file(nullptr) |
Zachary Turner | 3c1c5b9 | 2015-05-21 19:56:26 +0000 | [diff] [blame] | 65 | , m_debugging_ended_event(nullptr) |
Zachary Turner | 8d8033c | 2015-09-17 20:18:50 +0000 | [diff] [blame] | 66 | , m_is_shutting_down(false) |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 67 | , m_pid_to_detach(0) |
| 68 | , m_detached(false) |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 69 | { |
Zachary Turner | 3c1c5b9 | 2015-05-21 19:56:26 +0000 | [diff] [blame] | 70 | m_debugging_ended_event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | DebuggerThread::~DebuggerThread() |
| 74 | { |
Zachary Turner | 3c1c5b9 | 2015-05-21 19:56:26 +0000 | [diff] [blame] | 75 | ::CloseHandle(m_debugging_ended_event); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Zachary Turner | 3985f89 | 2014-11-10 22:32:18 +0000 | [diff] [blame] | 78 | Error |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 79 | DebuggerThread::DebugLaunch(const ProcessLaunchInfo &launch_info) |
| 80 | { |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 81 | WINLOG_IFALL(WINDOWS_LOG_PROCESS, |
| 82 | "DebuggerThread::DebugLaunch launching '%s'", launch_info.GetExecutableFile().GetPath().c_str()); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 83 | |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 84 | Error error; |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 85 | DebugLaunchContext *context = new DebugLaunchContext(this, launch_info); |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 86 | HostThread slave_thread(ThreadLauncher::LaunchThread("lldb.plugin.process-windows.slave[?]", |
Zachary Turner | c62733b | 2015-05-20 18:31:17 +0000 | [diff] [blame] | 87 | DebuggerThreadLaunchRoutine, context, &error)); |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 88 | |
| 89 | if (!error.Success()) |
| 90 | { |
| 91 | WINERR_IFALL(WINDOWS_LOG_PROCESS, |
| 92 | "DebugLaunch couldn't launch debugger thread. %s", error.AsCString()); |
| 93 | } |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 94 | |
Zachary Turner | 3985f89 | 2014-11-10 22:32:18 +0000 | [diff] [blame] | 95 | return error; |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Zachary Turner | c62733b | 2015-05-20 18:31:17 +0000 | [diff] [blame] | 98 | Error |
| 99 | DebuggerThread::DebugAttach(lldb::pid_t pid, const ProcessAttachInfo &attach_info) |
| 100 | { |
| 101 | WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DebuggerThread::DebugAttach attaching to '%u'", (DWORD)pid); |
| 102 | |
| 103 | Error error; |
| 104 | DebugAttachContext *context = new DebugAttachContext(this, pid, attach_info); |
| 105 | HostThread slave_thread(ThreadLauncher::LaunchThread("lldb.plugin.process-windows.slave[?]", |
| 106 | DebuggerThreadAttachRoutine, context, &error)); |
| 107 | |
| 108 | if (!error.Success()) |
| 109 | { |
| 110 | WINERR_IFALL(WINDOWS_LOG_PROCESS, "DebugAttach couldn't attach to process '%u'. %s", (DWORD)pid, |
| 111 | error.AsCString()); |
| 112 | } |
| 113 | |
| 114 | return error; |
| 115 | } |
| 116 | |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 117 | lldb::thread_result_t |
Zachary Turner | c62733b | 2015-05-20 18:31:17 +0000 | [diff] [blame] | 118 | DebuggerThread::DebuggerThreadLaunchRoutine(void *data) |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 119 | { |
| 120 | DebugLaunchContext *context = static_cast<DebugLaunchContext *>(data); |
Zachary Turner | c62733b | 2015-05-20 18:31:17 +0000 | [diff] [blame] | 121 | lldb::thread_result_t result = context->m_thread->DebuggerThreadLaunchRoutine(context->m_launch_info); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 122 | delete context; |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | lldb::thread_result_t |
Zachary Turner | c62733b | 2015-05-20 18:31:17 +0000 | [diff] [blame] | 127 | DebuggerThread::DebuggerThreadAttachRoutine(void *data) |
| 128 | { |
| 129 | DebugAttachContext *context = static_cast<DebugAttachContext *>(data); |
| 130 | lldb::thread_result_t result = |
| 131 | context->m_thread->DebuggerThreadAttachRoutine(context->m_pid, context->m_attach_info); |
| 132 | delete context; |
| 133 | return result; |
| 134 | } |
| 135 | |
| 136 | lldb::thread_result_t |
| 137 | DebuggerThread::DebuggerThreadLaunchRoutine(const ProcessLaunchInfo &launch_info) |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 138 | { |
| 139 | // Grab a shared_ptr reference to this so that we know it won't get deleted until after the |
| 140 | // thread routine has exited. |
| 141 | std::shared_ptr<DebuggerThread> this_ref(shared_from_this()); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 142 | |
Zachary Turner | c62733b | 2015-05-20 18:31:17 +0000 | [diff] [blame] | 143 | WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DebuggerThread preparing to launch '%s' on background thread.", |
| 144 | launch_info.GetExecutableFile().GetPath().c_str()); |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 145 | |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 146 | Error error; |
| 147 | ProcessLauncherWindows launcher; |
| 148 | HostProcess process(launcher.LaunchProcess(launch_info, error)); |
| 149 | // If we couldn't create the process, notify waiters immediately. Otherwise enter the debug |
| 150 | // loop and wait until we get the create process debug notification. Note that if the process |
| 151 | // was created successfully, we can throw away the process handle we got from CreateProcess |
| 152 | // because Windows will give us another (potentially more useful?) handle when it sends us the |
| 153 | // CREATE_PROCESS_DEBUG_EVENT. |
| 154 | if (error.Success()) |
| 155 | DebugLoop(); |
| 156 | else |
Zachary Turner | d6a7b63 | 2014-11-12 19:31:39 +0000 | [diff] [blame] | 157 | m_debug_delegate->OnDebuggerError(error, 0); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Zachary Turner | c62733b | 2015-05-20 18:31:17 +0000 | [diff] [blame] | 162 | lldb::thread_result_t |
| 163 | DebuggerThread::DebuggerThreadAttachRoutine(lldb::pid_t pid, const ProcessAttachInfo &attach_info) |
| 164 | { |
| 165 | // Grab a shared_ptr reference to this so that we know it won't get deleted until after the |
| 166 | // thread routine has exited. |
| 167 | std::shared_ptr<DebuggerThread> this_ref(shared_from_this()); |
| 168 | |
| 169 | WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DebuggerThread preparing to attach to process '%u' on background thread.", |
| 170 | (DWORD)pid); |
| 171 | |
| 172 | if (!DebugActiveProcess((DWORD)pid)) |
| 173 | { |
| 174 | Error error(::GetLastError(), eErrorTypeWin32); |
| 175 | m_debug_delegate->OnDebuggerError(error, 0); |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | // The attach was successful, enter the debug loop. From here on out, this is no different than |
| 180 | // a create process operation, so all the same comments in DebugLaunch should apply from this |
| 181 | // point out. |
| 182 | DebugLoop(); |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 187 | Error |
| 188 | DebuggerThread::StopDebugging(bool terminate) |
| 189 | { |
| 190 | Error error; |
| 191 | |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 192 | lldb::pid_t pid = m_process.GetProcessId(); |
| 193 | |
| 194 | WINLOG_IFALL(WINDOWS_LOG_PROCESS, |
| 195 | "StopDebugging('%s') called (inferior=%I64u).", |
| 196 | (terminate ? "true" : "false"), pid); |
| 197 | |
Zachary Turner | 8d8033c | 2015-09-17 20:18:50 +0000 | [diff] [blame] | 198 | // Set m_is_shutting_down to true if it was false. Return if it was already true. |
| 199 | bool expected = false; |
| 200 | if (!m_is_shutting_down.compare_exchange_strong(expected, true)) |
| 201 | return error; |
| 202 | |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 203 | // Make a copy of the process, since the termination sequence will reset |
| 204 | // DebuggerThread's internal copy and it needs to remain open for the Wait operation. |
| 205 | HostProcess process_copy = m_process; |
| 206 | lldb::process_t handle = m_process.GetNativeProcess().GetSystemHandle(); |
| 207 | |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 208 | if (terminate) |
| 209 | { |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 210 | // Initiate the termination before continuing the exception, so that the next debug |
| 211 | // event we get is the exit process event, and not some other event. |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 212 | BOOL terminate_suceeded = TerminateProcess(handle, 0); |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 213 | WINLOG_IFALL(WINDOWS_LOG_PROCESS, |
| 214 | "StopDebugging called TerminateProcess(0x%p, 0) (inferior=%I64u), success='%s'", |
| 215 | handle, pid, (terminate_suceeded ? "true" : "false")); |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 216 | } |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 217 | |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 218 | // If we're stuck waiting for an exception to continue (e.g. the user is at a breakpoint |
| 219 | // messing around in the debugger), continue it now. But only AFTER calling TerminateProcess |
| 220 | // to make sure that the very next call to WaitForDebugEvent is an exit process event. |
| 221 | if (m_active_exception.get()) |
| 222 | { |
| 223 | WINLOG_IFANY(WINDOWS_LOG_PROCESS|WINDOWS_LOG_EXCEPTION, |
| 224 | "StopDebugging masking active exception"); |
| 225 | |
| 226 | ContinueAsyncException(ExceptionResult::MaskException); |
| 227 | } |
| 228 | |
| 229 | if (!terminate) |
| 230 | { |
| 231 | // Indicate that we want to detach. |
| 232 | m_pid_to_detach = GetProcess().GetProcessId(); |
| 233 | |
| 234 | // Force a fresh break so that the detach can happen from the debugger thread. |
| 235 | if (!::DebugBreakProcess(GetProcess().GetNativeProcess().GetSystemHandle())) |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 236 | { |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 237 | error.SetError(::GetLastError(), eErrorTypeWin32); |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 238 | } |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 239 | } |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 240 | |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 241 | WINLOG_IFALL(WINDOWS_LOG_PROCESS, "StopDebugging waiting for detach from process %u to complete.", pid); |
Zachary Turner | 3c1c5b9 | 2015-05-21 19:56:26 +0000 | [diff] [blame] | 242 | |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 243 | DWORD wait_result = WaitForSingleObject(m_debugging_ended_event, 5000); |
| 244 | if (wait_result != WAIT_OBJECT_0) |
| 245 | { |
| 246 | error.SetError(GetLastError(), eErrorTypeWin32); |
| 247 | WINERR_IFALL(WINDOWS_LOG_PROCESS, "StopDebugging WaitForSingleObject(0x%p, 5000) returned %u", |
| 248 | m_debugging_ended_event, wait_result); |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 249 | } |
| 250 | else |
| 251 | { |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 252 | WINLOG_IFALL(WINDOWS_LOG_PROCESS, "StopDebugging detach from process %u completed successfully.", pid); |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 253 | } |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 254 | |
| 255 | if (!error.Success()) |
| 256 | { |
| 257 | WINERR_IFALL(WINDOWS_LOG_PROCESS, |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 258 | "StopDebugging encountered an error while trying to stop process %u. %s", |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 259 | pid, error.AsCString()); |
| 260 | } |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 261 | return error; |
| 262 | } |
| 263 | |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 264 | void |
Zachary Turner | dcd8037 | 2014-11-11 00:00:14 +0000 | [diff] [blame] | 265 | DebuggerThread::ContinueAsyncException(ExceptionResult result) |
| 266 | { |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 267 | if (!m_active_exception.get()) |
| 268 | return; |
| 269 | |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 270 | WINLOG_IFANY(WINDOWS_LOG_PROCESS|WINDOWS_LOG_EXCEPTION, |
| 271 | "ContinueAsyncException called for inferior process %I64u, broadcasting.", |
| 272 | m_process.GetProcessId()); |
| 273 | |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 274 | m_active_exception.reset(); |
| 275 | m_exception_pred.SetValue(result, eBroadcastAlways); |
| 276 | } |
| 277 | |
| 278 | void |
| 279 | DebuggerThread::FreeProcessHandles() |
| 280 | { |
| 281 | m_process = HostProcess(); |
| 282 | m_main_thread = HostThread(); |
| 283 | if (m_image_file) |
| 284 | { |
| 285 | ::CloseHandle(m_image_file); |
| 286 | m_image_file = nullptr; |
| 287 | } |
Zachary Turner | dcd8037 | 2014-11-11 00:00:14 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 291 | DebuggerThread::DebugLoop() |
| 292 | { |
| 293 | DEBUG_EVENT dbe = {0}; |
Zachary Turner | be5e1e5 | 2014-12-04 22:07:02 +0000 | [diff] [blame] | 294 | bool should_debug = true; |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 295 | WINLOG_IFALL(WINDOWS_LOG_EVENT, "Entering WaitForDebugEvent loop"); |
| 296 | while (should_debug) |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 297 | { |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 298 | WINLOGD_IFALL(WINDOWS_LOG_EVENT, "Calling WaitForDebugEvent"); |
| 299 | BOOL wait_result = WaitForDebugEvent(&dbe, INFINITE); |
| 300 | if (wait_result) |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 301 | { |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 302 | DWORD continue_status = DBG_CONTINUE; |
| 303 | switch (dbe.dwDebugEventCode) |
Zachary Turner | dcd8037 | 2014-11-11 00:00:14 +0000 | [diff] [blame] | 304 | { |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 305 | case EXCEPTION_DEBUG_EVENT: |
| 306 | { |
| 307 | ExceptionResult status = HandleExceptionEvent(dbe.u.Exception, dbe.dwThreadId); |
Zachary Turner | dcd8037 | 2014-11-11 00:00:14 +0000 | [diff] [blame] | 308 | |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 309 | if (status == ExceptionResult::MaskException) |
| 310 | continue_status = DBG_CONTINUE; |
| 311 | else if (status == ExceptionResult::SendToApplication) |
| 312 | continue_status = DBG_EXCEPTION_NOT_HANDLED; |
Zachary Turner | 6c3c0ed | 2015-10-02 22:47:04 +0000 | [diff] [blame] | 313 | |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 314 | break; |
| 315 | } |
| 316 | case CREATE_THREAD_DEBUG_EVENT: |
| 317 | continue_status = HandleCreateThreadEvent(dbe.u.CreateThread, dbe.dwThreadId); |
| 318 | break; |
| 319 | case CREATE_PROCESS_DEBUG_EVENT: |
| 320 | continue_status = HandleCreateProcessEvent(dbe.u.CreateProcessInfo, dbe.dwThreadId); |
| 321 | break; |
| 322 | case EXIT_THREAD_DEBUG_EVENT: |
| 323 | continue_status = HandleExitThreadEvent(dbe.u.ExitThread, dbe.dwThreadId); |
| 324 | break; |
| 325 | case EXIT_PROCESS_DEBUG_EVENT: |
| 326 | continue_status = HandleExitProcessEvent(dbe.u.ExitProcess, dbe.dwThreadId); |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 327 | should_debug = false; |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 328 | break; |
| 329 | case LOAD_DLL_DEBUG_EVENT: |
| 330 | continue_status = HandleLoadDllEvent(dbe.u.LoadDll, dbe.dwThreadId); |
| 331 | break; |
| 332 | case UNLOAD_DLL_DEBUG_EVENT: |
| 333 | continue_status = HandleUnloadDllEvent(dbe.u.UnloadDll, dbe.dwThreadId); |
| 334 | break; |
| 335 | case OUTPUT_DEBUG_STRING_EVENT: |
| 336 | continue_status = HandleODSEvent(dbe.u.DebugString, dbe.dwThreadId); |
| 337 | break; |
| 338 | case RIP_EVENT: |
| 339 | continue_status = HandleRipEvent(dbe.u.RipInfo, dbe.dwThreadId); |
| 340 | if (dbe.u.RipInfo.dwType == SLE_ERROR) |
| 341 | should_debug = false; |
| 342 | break; |
| 343 | } |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 344 | |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 345 | WINLOGD_IFALL(WINDOWS_LOG_EVENT, "DebugLoop calling ContinueDebugEvent(%u, %u, %u) on thread %u.", |
| 346 | dbe.dwProcessId, dbe.dwThreadId, continue_status, ::GetCurrentThreadId()); |
| 347 | |
| 348 | ::ContinueDebugEvent(dbe.dwProcessId, dbe.dwThreadId, continue_status); |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 349 | |
| 350 | if (m_detached) |
| 351 | { |
| 352 | should_debug = false; |
| 353 | } |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 354 | } |
| 355 | else |
| 356 | { |
| 357 | WINERR_IFALL(WINDOWS_LOG_EVENT, |
| 358 | "DebugLoop returned FALSE from WaitForDebugEvent. Error = %u", |
| 359 | ::GetCurrentThreadId(), ::GetLastError()); |
| 360 | |
| 361 | should_debug = false; |
| 362 | } |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 363 | } |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 364 | FreeProcessHandles(); |
Zachary Turner | 3c1c5b9 | 2015-05-21 19:56:26 +0000 | [diff] [blame] | 365 | |
| 366 | WINLOG_IFALL(WINDOWS_LOG_EVENT, "WaitForDebugEvent loop completed, exiting."); |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 367 | SetEvent(m_debugging_ended_event); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Zachary Turner | dcd8037 | 2014-11-11 00:00:14 +0000 | [diff] [blame] | 370 | ExceptionResult |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 371 | DebuggerThread::HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info, DWORD thread_id) |
| 372 | { |
Zachary Turner | 6c3c0ed | 2015-10-02 22:47:04 +0000 | [diff] [blame] | 373 | if (m_is_shutting_down) |
| 374 | { |
| 375 | // A breakpoint that occurs while `m_pid_to_detach` is non-zero is a magic exception that |
| 376 | // we use simply to wake up the DebuggerThread so that we can close out the debug loop. |
| 377 | if (m_pid_to_detach != 0 && info.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT) |
| 378 | { |
| 379 | WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION | WINDOWS_LOG_PROCESS, |
| 380 | "Breakpoint exception is cue to detach from process 0x%x", |
| 381 | m_pid_to_detach); |
| 382 | ::DebugActiveProcessStop(m_pid_to_detach); |
| 383 | m_detached = true; |
| 384 | } |
| 385 | |
| 386 | // Don't perform any blocking operations while we're shutting down. That will |
| 387 | // cause TerminateProcess -> WaitForSingleObject to time out. |
| 388 | return ExceptionResult::SendToApplication; |
| 389 | } |
| 390 | |
Zachary Turner | dcd8037 | 2014-11-11 00:00:14 +0000 | [diff] [blame] | 391 | bool first_chance = (info.dwFirstChance != 0); |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 392 | |
Adrian McCarthy | 2f8e4c3 | 2015-05-18 23:24:32 +0000 | [diff] [blame] | 393 | m_active_exception.reset(new ExceptionRecord(info.ExceptionRecord, thread_id)); |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 394 | WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION, |
Adrian McCarthy | 2f8e4c3 | 2015-05-18 23:24:32 +0000 | [diff] [blame] | 395 | "HandleExceptionEvent encountered %s chance exception 0x%x on thread 0x%x", |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 396 | first_chance ? "first" : "second", info.ExceptionRecord.ExceptionCode, thread_id); |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 397 | |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 398 | ExceptionResult result = m_debug_delegate->OnDebugException(first_chance, |
| 399 | *m_active_exception); |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 400 | m_exception_pred.SetValue(result, eBroadcastNever); |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 401 | |
| 402 | WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_EXCEPTION, |
| 403 | "DebuggerThread::HandleExceptionEvent waiting for ExceptionPred != BreakInDebugger"); |
| 404 | |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 405 | m_exception_pred.WaitForValueNotEqualTo(ExceptionResult::BreakInDebugger, result); |
| 406 | |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 407 | WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_EXCEPTION, |
| 408 | "DebuggerThread::HandleExceptionEvent got ExceptionPred = %u", |
| 409 | m_exception_pred.GetValue()); |
| 410 | |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 411 | return result; |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | DWORD |
| 415 | DebuggerThread::HandleCreateThreadEvent(const CREATE_THREAD_DEBUG_INFO &info, DWORD thread_id) |
| 416 | { |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 417 | WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_THREAD, |
Adrian McCarthy | 2f8e4c3 | 2015-05-18 23:24:32 +0000 | [diff] [blame] | 418 | "HandleCreateThreadEvent Thread 0x%x spawned in process %I64u", |
| 419 | thread_id, m_process.GetProcessId()); |
| 420 | HostThread thread(info.hThread); |
| 421 | thread.GetNativeThread().SetOwnsHandle(false); |
| 422 | m_debug_delegate->OnCreateThread(thread); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 423 | return DBG_CONTINUE; |
| 424 | } |
| 425 | |
| 426 | DWORD |
| 427 | DebuggerThread::HandleCreateProcessEvent(const CREATE_PROCESS_DEBUG_INFO &info, DWORD thread_id) |
| 428 | { |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 429 | uint32_t process_id = ::GetProcessId(info.hProcess); |
| 430 | |
| 431 | WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_PROCESS, "HandleCreateProcessEvent process %u spawned", process_id); |
| 432 | |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 433 | std::string thread_name; |
| 434 | llvm::raw_string_ostream name_stream(thread_name); |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 435 | name_stream << "lldb.plugin.process-windows.slave[" << process_id << "]"; |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 436 | name_stream.flush(); |
| 437 | ThisThread::SetName(thread_name.c_str()); |
| 438 | |
| 439 | // info.hProcess and info.hThread are closed automatically by Windows when |
| 440 | // EXIT_PROCESS_DEBUG_EVENT is received. |
| 441 | m_process = HostProcess(info.hProcess); |
| 442 | ((HostProcessWindows &)m_process.GetNativeProcess()).SetOwnsHandle(false); |
| 443 | m_main_thread = HostThread(info.hThread); |
Zachary Turner | c301899 | 2014-11-17 22:42:57 +0000 | [diff] [blame] | 444 | m_main_thread.GetNativeThread().SetOwnsHandle(false); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 445 | m_image_file = info.hFile; |
| 446 | |
Zachary Turner | a32d2ce | 2014-11-12 19:31:56 +0000 | [diff] [blame] | 447 | lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfImage); |
| 448 | m_debug_delegate->OnDebuggerConnected(load_addr); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 449 | |
| 450 | return DBG_CONTINUE; |
| 451 | } |
| 452 | |
| 453 | DWORD |
| 454 | DebuggerThread::HandleExitThreadEvent(const EXIT_THREAD_DEBUG_INFO &info, DWORD thread_id) |
| 455 | { |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 456 | WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_THREAD, |
| 457 | "HandleExitThreadEvent Thread %u exited with code %u in process %I64u", |
| 458 | thread_id, info.dwExitCode, m_process.GetProcessId()); |
Adrian McCarthy | 2f8e4c3 | 2015-05-18 23:24:32 +0000 | [diff] [blame] | 459 | m_debug_delegate->OnExitThread(thread_id, info.dwExitCode); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 460 | return DBG_CONTINUE; |
| 461 | } |
| 462 | |
| 463 | DWORD |
| 464 | DebuggerThread::HandleExitProcessEvent(const EXIT_PROCESS_DEBUG_INFO &info, DWORD thread_id) |
| 465 | { |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 466 | WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_THREAD, |
| 467 | "HandleExitProcessEvent process %I64u exited with code %u", |
| 468 | m_process.GetProcessId(), info.dwExitCode); |
| 469 | |
Oleksiy Vyalov | 9953f08 | 2015-05-15 02:05:10 +0000 | [diff] [blame] | 470 | m_debug_delegate->OnExitProcess(info.dwExitCode); |
Adrian McCarthy | 2f8e4c3 | 2015-05-18 23:24:32 +0000 | [diff] [blame] | 471 | |
| 472 | FreeProcessHandles(); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 473 | return DBG_CONTINUE; |
| 474 | } |
| 475 | |
| 476 | DWORD |
| 477 | DebuggerThread::HandleLoadDllEvent(const LOAD_DLL_DEBUG_INFO &info, DWORD thread_id) |
| 478 | { |
Zachary Turner | a32d2ce | 2014-11-12 19:31:56 +0000 | [diff] [blame] | 479 | if (info.hFile == nullptr) |
| 480 | { |
| 481 | // Not sure what this is, so just ignore it. |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 482 | WINWARN_IFALL(WINDOWS_LOG_EVENT, "Inferior %I64u - HandleLoadDllEvent has a NULL file handle, returning...", |
| 483 | m_process.GetProcessId()); |
Zachary Turner | a32d2ce | 2014-11-12 19:31:56 +0000 | [diff] [blame] | 484 | return DBG_CONTINUE; |
| 485 | } |
| 486 | |
| 487 | std::vector<char> buffer(1); |
| 488 | DWORD required_size = GetFinalPathNameByHandle(info.hFile, &buffer[0], 0, VOLUME_NAME_DOS); |
| 489 | if (required_size > 0) |
| 490 | { |
| 491 | buffer.resize(required_size + 1); |
| 492 | required_size = GetFinalPathNameByHandle(info.hFile, &buffer[0], required_size + 1, VOLUME_NAME_DOS); |
| 493 | llvm::StringRef path_str(&buffer[0]); |
| 494 | const char *path = path_str.data(); |
| 495 | if (path_str.startswith("\\\\?\\")) |
| 496 | path += 4; |
| 497 | |
| 498 | FileSpec file_spec(path, false); |
| 499 | ModuleSpec module_spec(file_spec); |
| 500 | lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll); |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 501 | |
| 502 | WINLOG_IFALL(WINDOWS_LOG_EVENT, "Inferior %I64u - HandleLoadDllEvent DLL '%s' loaded at address 0x%p...", |
| 503 | m_process.GetProcessId(), path, info.lpBaseOfDll); |
| 504 | |
Zachary Turner | a32d2ce | 2014-11-12 19:31:56 +0000 | [diff] [blame] | 505 | m_debug_delegate->OnLoadDll(module_spec, load_addr); |
| 506 | } |
| 507 | else |
| 508 | { |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 509 | WINERR_IFALL(WINDOWS_LOG_EVENT, |
| 510 | "Inferior %I64u - HandleLoadDllEvent Error %u occurred calling GetFinalPathNameByHandle", |
| 511 | m_process.GetProcessId(), ::GetLastError()); |
Zachary Turner | a32d2ce | 2014-11-12 19:31:56 +0000 | [diff] [blame] | 512 | } |
| 513 | // Windows does not automatically close info.hFile, so we need to do it. |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 514 | ::CloseHandle(info.hFile); |
| 515 | return DBG_CONTINUE; |
| 516 | } |
| 517 | |
| 518 | DWORD |
| 519 | DebuggerThread::HandleUnloadDllEvent(const UNLOAD_DLL_DEBUG_INFO &info, DWORD thread_id) |
| 520 | { |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 521 | WINLOG_IFALL(WINDOWS_LOG_EVENT, |
| 522 | "HandleUnloadDllEvent process %I64u unloading DLL at addr 0x%p.", |
| 523 | m_process.GetProcessId(), info.lpBaseOfDll); |
| 524 | |
Zachary Turner | a32d2ce | 2014-11-12 19:31:56 +0000 | [diff] [blame] | 525 | m_debug_delegate->OnUnloadDll(reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll)); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 526 | return DBG_CONTINUE; |
| 527 | } |
| 528 | |
| 529 | DWORD |
| 530 | DebuggerThread::HandleODSEvent(const OUTPUT_DEBUG_STRING_INFO &info, DWORD thread_id) |
| 531 | { |
| 532 | return DBG_CONTINUE; |
| 533 | } |
| 534 | |
| 535 | DWORD |
| 536 | DebuggerThread::HandleRipEvent(const RIP_INFO &info, DWORD thread_id) |
| 537 | { |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 538 | WINERR_IFALL(WINDOWS_LOG_EVENT, |
| 539 | "HandleRipEvent encountered error %u (type=%u) in process %I64u thread %u", |
| 540 | info.dwError, info.dwType, m_process.GetProcessId(), thread_id); |
| 541 | |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 542 | Error error(info.dwError, eErrorTypeWin32); |
Zachary Turner | d6a7b63 | 2014-11-12 19:31:39 +0000 | [diff] [blame] | 543 | m_debug_delegate->OnDebuggerError(error, info.dwType); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 544 | |
| 545 | return DBG_CONTINUE; |
| 546 | } |