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