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