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