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