xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/Process/Windows/Common/NativeProcessWindows.h (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
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   llvm::Expected<llvm::ArrayRef<uint8_t>>
112   GetSoftwareBreakpointTrapOpcode(size_t size_hint) override;
113 
114   size_t GetSoftwareBreakpointPCOffset() override;
115 
116   bool FindSoftwareBreakpoint(lldb::addr_t addr);
117 
118   void StopThread(lldb::tid_t thread_id, lldb::StopReason reason,
119                   std::string description = "");
120 
121   void SetStopReasonForThread(NativeThreadWindows &thread,
122                               lldb::StopReason reason,
123                               std::string description = "");
124 
125 private:
126   ArchSpec m_arch;
127 
128   NativeProcessWindows(ProcessLaunchInfo &launch_info, NativeDelegate &delegate,
129                        llvm::Error &E);
130 
131   NativeProcessWindows(lldb::pid_t pid, int terminal_fd,
132                        NativeDelegate &delegate, llvm::Error &E);
133 
134   Status CacheLoadedModules();
135   std::map<lldb_private::FileSpec, lldb::addr_t> m_loaded_modules;
136 };
137 
138 //------------------------------------------------------------------
139 // NativeDebugDelegate
140 //------------------------------------------------------------------
141 class NativeDebugDelegate : public IDebugDelegate {
142 public:
NativeDebugDelegate(NativeProcessWindows & process)143   NativeDebugDelegate(NativeProcessWindows &process) : m_process(process) {}
144 
OnExitProcess(uint32_t exit_code)145   void OnExitProcess(uint32_t exit_code) override {
146     m_process.OnExitProcess(exit_code);
147   }
148 
OnDebuggerConnected(lldb::addr_t image_base)149   void OnDebuggerConnected(lldb::addr_t image_base) override {
150     m_process.OnDebuggerConnected(image_base);
151   }
152 
OnDebugException(bool first_chance,const ExceptionRecord & record)153   ExceptionResult OnDebugException(bool first_chance,
154                                    const ExceptionRecord &record) override {
155     return m_process.OnDebugException(first_chance, record);
156   }
157 
OnCreateThread(const HostThread & thread)158   void OnCreateThread(const HostThread &thread) override {
159     m_process.OnCreateThread(thread);
160   }
161 
OnExitThread(lldb::tid_t thread_id,uint32_t exit_code)162   void OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) override {
163     m_process.OnExitThread(thread_id, exit_code);
164   }
165 
OnLoadDll(const lldb_private::ModuleSpec & module_spec,lldb::addr_t module_addr)166   void OnLoadDll(const lldb_private::ModuleSpec &module_spec,
167                  lldb::addr_t module_addr) override {
168     m_process.OnLoadDll(module_spec, module_addr);
169   }
170 
OnUnloadDll(lldb::addr_t module_addr)171   void OnUnloadDll(lldb::addr_t module_addr) override {
172     m_process.OnUnloadDll(module_addr);
173   }
174 
OnDebugString(const std::string & string)175   void OnDebugString(const std::string &string) override {
176     m_process.OnDebugString(string);
177   }
178 
OnDebuggerError(const Status & error,uint32_t type)179   void OnDebuggerError(const Status &error, uint32_t type) override {
180     return m_process.OnDebuggerError(error, type);
181   }
182 
183 private:
184   NativeProcessWindows &m_process;
185 };
186 
187 } // namespace lldb_private
188 
189 #endif // #ifndef liblldb_NativeProcessWindows_h_
190