1 //===-- NativeProcessWindows.h ----------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef liblldb_NativeProcessWindows_h_ 10 #define liblldb_NativeProcessWindows_h_ 11 12 #include "lldb/Host/common/NativeProcessProtocol.h" 13 #include "lldb/lldb-forward.h" 14 15 #include "IDebugDelegate.h" 16 #include "ProcessDebugger.h" 17 18 namespace lldb_private { 19 20 class HostProcess; 21 class NativeProcessWindows; 22 class NativeThreadWindows; 23 class NativeDebugDelegate; 24 25 typedef std::shared_ptr<NativeDebugDelegate> NativeDebugDelegateSP; 26 27 //------------------------------------------------------------------ 28 // NativeProcessWindows 29 //------------------------------------------------------------------ 30 class NativeProcessWindows : public NativeProcessProtocol, 31 public ProcessDebugger { 32 33 public: 34 class Manager : public NativeProcessProtocol::Manager { 35 public: 36 using NativeProcessProtocol::Manager::Manager; 37 38 llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 39 Launch(ProcessLaunchInfo &launch_info, 40 NativeDelegate &native_delegate) override; 41 42 llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 43 Attach(lldb::pid_t pid, NativeDelegate &native_delegate) override; 44 }; 45 46 Status Resume(const ResumeActionList &resume_actions) override; 47 48 Status Halt() override; 49 50 Status Detach() override; 51 52 Status Signal(int signo) override; 53 54 Status Interrupt() override; 55 56 Status Kill() override; 57 58 Status IgnoreSignals(llvm::ArrayRef<int> signals) override; 59 60 Status GetMemoryRegionInfo(lldb::addr_t load_addr, 61 MemoryRegionInfo &range_info) override; 62 63 Status ReadMemory(lldb::addr_t addr, void *buf, size_t size, 64 size_t &bytes_read) override; 65 66 Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, 67 size_t &bytes_written) override; 68 69 llvm::Expected<lldb::addr_t> AllocateMemory(size_t size, 70 uint32_t permissions) override; 71 72 llvm::Error DeallocateMemory(lldb::addr_t addr) override; 73 74 lldb::addr_t GetSharedLibraryInfoAddress() override; 75 76 bool IsAlive() const override; 77 78 size_t UpdateThreads() override; 79 GetArchitecture()80 const ArchSpec &GetArchitecture() const override { return m_arch; } 81 SetArchitecture(const ArchSpec & arch_spec)82 void SetArchitecture(const ArchSpec &arch_spec) { m_arch = arch_spec; } 83 84 Status SetBreakpoint(lldb::addr_t addr, uint32_t size, 85 bool hardware) override; 86 87 Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override; 88 89 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 90 GetAuxvData() const override; 91 92 Status GetLoadedModuleFileSpec(const char *module_path, 93 FileSpec &file_spec) override; 94 95 Status GetFileLoadAddress(const llvm::StringRef &file_name, 96 lldb::addr_t &load_addr) override; 97 98 // ProcessDebugger Overrides 99 void OnExitProcess(uint32_t exit_code) override; 100 void OnDebuggerConnected(lldb::addr_t image_base) override; 101 ExceptionResult OnDebugException(bool first_chance, 102 const ExceptionRecord &record) override; 103 void OnCreateThread(const HostThread &thread) override; 104 void OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) override; 105 void OnLoadDll(const ModuleSpec &module_spec, 106 lldb::addr_t module_addr) override; 107 void OnUnloadDll(lldb::addr_t module_addr) override; 108 109 protected: 110 NativeThreadWindows *GetThreadByID(lldb::tid_t thread_id); 111 112 llvm::Expected<llvm::ArrayRef<uint8_t>> 113 GetSoftwareBreakpointTrapOpcode(size_t size_hint) override; 114 115 size_t GetSoftwareBreakpointPCOffset() override; 116 117 bool FindSoftwareBreakpoint(lldb::addr_t addr); 118 119 void StopThread(lldb::tid_t thread_id, lldb::StopReason reason, 120 std::string description = ""); 121 122 void SetStopReasonForThread(NativeThreadWindows &thread, 123 lldb::StopReason reason, 124 std::string description = ""); 125 126 private: 127 ArchSpec m_arch; 128 129 NativeProcessWindows(ProcessLaunchInfo &launch_info, NativeDelegate &delegate, 130 llvm::Error &E); 131 132 NativeProcessWindows(lldb::pid_t pid, int terminal_fd, 133 NativeDelegate &delegate, llvm::Error &E); 134 135 Status CacheLoadedModules(); 136 std::map<lldb_private::FileSpec, lldb::addr_t> m_loaded_modules; 137 }; 138 139 //------------------------------------------------------------------ 140 // NativeDebugDelegate 141 //------------------------------------------------------------------ 142 class NativeDebugDelegate : public IDebugDelegate { 143 public: NativeDebugDelegate(NativeProcessWindows & process)144 NativeDebugDelegate(NativeProcessWindows &process) : m_process(process) {} 145 OnExitProcess(uint32_t exit_code)146 void OnExitProcess(uint32_t exit_code) override { 147 m_process.OnExitProcess(exit_code); 148 } 149 OnDebuggerConnected(lldb::addr_t image_base)150 void OnDebuggerConnected(lldb::addr_t image_base) override { 151 m_process.OnDebuggerConnected(image_base); 152 } 153 OnDebugException(bool first_chance,const ExceptionRecord & record)154 ExceptionResult OnDebugException(bool first_chance, 155 const ExceptionRecord &record) override { 156 return m_process.OnDebugException(first_chance, record); 157 } 158 OnCreateThread(const HostThread & thread)159 void OnCreateThread(const HostThread &thread) override { 160 m_process.OnCreateThread(thread); 161 } 162 OnExitThread(lldb::tid_t thread_id,uint32_t exit_code)163 void OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) override { 164 m_process.OnExitThread(thread_id, exit_code); 165 } 166 OnLoadDll(const lldb_private::ModuleSpec & module_spec,lldb::addr_t module_addr)167 void OnLoadDll(const lldb_private::ModuleSpec &module_spec, 168 lldb::addr_t module_addr) override { 169 m_process.OnLoadDll(module_spec, module_addr); 170 } 171 OnUnloadDll(lldb::addr_t module_addr)172 void OnUnloadDll(lldb::addr_t module_addr) override { 173 m_process.OnUnloadDll(module_addr); 174 } 175 OnDebugString(const std::string & string)176 void OnDebugString(const std::string &string) override { 177 m_process.OnDebugString(string); 178 } 179 OnDebuggerError(const Status & error,uint32_t type)180 void OnDebuggerError(const Status &error, uint32_t type) override { 181 return m_process.OnDebuggerError(error, type); 182 } 183 184 private: 185 NativeProcessWindows &m_process; 186 }; 187 188 } // namespace lldb_private 189 190 #endif // #ifndef liblldb_NativeProcessWindows_h_ 191