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