xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/Process/Windows/Common/NativeProcessWindows.h (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
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   Status AllocateMemory(size_t size, uint32_t permissions,
69                         lldb::addr_t &addr) override;
70 
71   Status 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 
79   const ArchSpec &GetArchitecture() const override { return m_arch; }
80 
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:
138   NativeDebugDelegate(NativeProcessWindows &process) : m_process(process) {}
139 
140   void OnExitProcess(uint32_t exit_code) { m_process.OnExitProcess(exit_code); }
141 
142   void OnDebuggerConnected(lldb::addr_t image_base) {
143     m_process.OnDebuggerConnected(image_base);
144   }
145 
146   ExceptionResult OnDebugException(bool first_chance,
147                                    const ExceptionRecord &record) {
148     return m_process.OnDebugException(first_chance, record);
149   }
150 
151   void OnCreateThread(const HostThread &thread) {
152     m_process.OnCreateThread(thread);
153   }
154 
155   void OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) {
156     m_process.OnExitThread(thread_id, exit_code);
157   }
158 
159   void OnLoadDll(const lldb_private::ModuleSpec &module_spec,
160                  lldb::addr_t module_addr) {
161     m_process.OnLoadDll(module_spec, module_addr);
162   }
163 
164   void OnUnloadDll(lldb::addr_t module_addr) {
165     m_process.OnUnloadDll(module_addr);
166   }
167 
168   void OnDebugString(const std::string &string) {
169     m_process.OnDebugString(string);
170   }
171 
172   void OnDebuggerError(const Status &error, uint32_t type) {
173     return m_process.OnDebuggerError(error, type);
174   }
175 
176 private:
177   NativeProcessWindows &m_process;
178 };
179 
180 } // namespace lldb_private
181 
182 #endif // #ifndef liblldb_NativeProcessWindows_h_
183