Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 1 | //===-- DebuggerThread.h ----------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef liblldb_Plugins_Process_Windows_DebuggerThread_H_ |
| 10 | #define liblldb_Plugins_Process_Windows_DebuggerThread_H_ |
| 11 | |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 12 | #include <atomic> |
Zachary Turner | fb6c3494 | 2014-12-10 23:25:10 +0000 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 15 | #include "ForwardDecl.h" |
| 16 | #include "lldb/Host/HostProcess.h" |
| 17 | #include "lldb/Host/HostThread.h" |
| 18 | #include "lldb/Host/windows/windows.h" |
Raphael Isemann | 7fae493 | 2018-08-30 17:51:10 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/Predicate.h" |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 20 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 21 | namespace lldb_private { |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 22 | |
| 23 | //---------------------------------------------------------------------- |
| 24 | // DebuggerThread |
| 25 | // |
| 26 | // Debugs a single process, notifying listeners as appropriate when interesting |
| 27 | // things occur. |
| 28 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | class DebuggerThread : public std::enable_shared_from_this<DebuggerThread> { |
| 30 | public: |
| 31 | DebuggerThread(DebugDelegateSP debug_delegate); |
| 32 | virtual ~DebuggerThread(); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 33 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 34 | Status DebugLaunch(const ProcessLaunchInfo &launch_info); |
| 35 | Status DebugAttach(lldb::pid_t pid, const ProcessAttachInfo &attach_info); |
Zachary Turner | 3985f89 | 2014-11-10 22:32:18 +0000 | [diff] [blame] | 36 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | HostProcess GetProcess() const { return m_process; } |
| 38 | HostThread GetMainThread() const { return m_main_thread; } |
| 39 | std::weak_ptr<ExceptionRecord> GetActiveException() { |
| 40 | return m_active_exception; |
| 41 | } |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 42 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 43 | Status StopDebugging(bool terminate); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 44 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | void ContinueAsyncException(ExceptionResult result); |
Zachary Turner | dcd8037 | 2014-11-11 00:00:14 +0000 | [diff] [blame] | 46 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | private: |
| 48 | void FreeProcessHandles(); |
| 49 | void DebugLoop(); |
| 50 | ExceptionResult HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info, |
| 51 | DWORD thread_id); |
| 52 | DWORD HandleCreateThreadEvent(const CREATE_THREAD_DEBUG_INFO &info, |
| 53 | DWORD thread_id); |
| 54 | DWORD HandleCreateProcessEvent(const CREATE_PROCESS_DEBUG_INFO &info, |
| 55 | DWORD thread_id); |
| 56 | DWORD HandleExitThreadEvent(const EXIT_THREAD_DEBUG_INFO &info, |
| 57 | DWORD thread_id); |
| 58 | DWORD HandleExitProcessEvent(const EXIT_PROCESS_DEBUG_INFO &info, |
| 59 | DWORD thread_id); |
| 60 | DWORD HandleLoadDllEvent(const LOAD_DLL_DEBUG_INFO &info, DWORD thread_id); |
| 61 | DWORD HandleUnloadDllEvent(const UNLOAD_DLL_DEBUG_INFO &info, |
| 62 | DWORD thread_id); |
| 63 | DWORD HandleODSEvent(const OUTPUT_DEBUG_STRING_INFO &info, DWORD thread_id); |
| 64 | DWORD HandleRipEvent(const RIP_INFO &info, DWORD thread_id); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 65 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | DebugDelegateSP m_debug_delegate; |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 67 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | HostProcess m_process; // The process being debugged. |
| 69 | HostThread m_main_thread; // The main thread of the inferior. |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 70 | |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 71 | // The image file of the process being debugged. |
| 72 | HANDLE m_image_file = nullptr; |
Zachary Turner | c6a6653 | 2014-12-03 22:04:18 +0000 | [diff] [blame] | 73 | |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 74 | // The current exception waiting to be handled |
| 75 | ExceptionRecordSP m_active_exception; |
Zachary Turner | dcd8037 | 2014-11-11 00:00:14 +0000 | [diff] [blame] | 76 | |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 77 | // A predicate which gets signalled when an exception is finished processing |
| 78 | // and the debug loop can be continued. |
| 79 | Predicate<ExceptionResult> m_exception_pred; |
Zachary Turner | 3c1c5b9 | 2015-05-21 19:56:26 +0000 | [diff] [blame] | 80 | |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 81 | // An event which gets signalled by the debugger thread when it exits the |
| 82 | // debugger loop and is detached from the inferior. |
| 83 | HANDLE m_debugging_ended_event = nullptr; |
| 84 | |
| 85 | // Signals the loop to detach from the process (specified by pid). |
| 86 | std::atomic<DWORD> m_pid_to_detach; |
| 87 | |
| 88 | // Signals the debug loop to stop processing certain types of events that |
| 89 | // block shutdown. |
| 90 | std::atomic<bool> m_is_shutting_down; |
| 91 | |
| 92 | // Indicates we've detached from the inferior process and the debug loop can |
| 93 | // exit. |
| 94 | bool m_detached = false; |
Adrian McCarthy | a59a721 | 2015-06-19 18:26:53 +0000 | [diff] [blame] | 95 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | static lldb::thread_result_t DebuggerThreadLaunchRoutine(void *data); |
| 97 | lldb::thread_result_t |
| 98 | DebuggerThreadLaunchRoutine(const ProcessLaunchInfo &launch_info); |
| 99 | static lldb::thread_result_t DebuggerThreadAttachRoutine(void *data); |
| 100 | lldb::thread_result_t |
| 101 | DebuggerThreadAttachRoutine(lldb::pid_t pid, |
| 102 | const ProcessAttachInfo &launch_info); |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 103 | }; |
| 104 | } |
Zachary Turner | 02862bc | 2014-11-07 23:44:13 +0000 | [diff] [blame] | 105 | #endif |