1 //===-- ProcessWindows.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_Plugins_Process_Windows_Common_ProcessWindows_H_
10 #define liblldb_Plugins_Process_Windows_Common_ProcessWindows_H_
11 
12 #include "lldb/Target/Process.h"
13 #include "lldb/Utility/Status.h"
14 #include "lldb/lldb-forward.h"
15 
16 #include "llvm/Support/Mutex.h"
17 
18 #include "IDebugDelegate.h"
19 #include "Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.h"
20 
21 namespace lldb_private {
22 
23 class HostProcess;
24 class ProcessWindowsData;
25 
26 class ProcessWindows : public Process, public IDebugDelegate {
27 public:
28   //------------------------------------------------------------------
29   // Static functions.
30   //------------------------------------------------------------------
31   static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp,
32                                         lldb::ListenerSP listener_sp,
33                                         const FileSpec *);
34 
35   static void Initialize();
36 
37   static void Terminate();
38 
39   static lldb_private::ConstString GetPluginNameStatic();
40 
41   static const char *GetPluginDescriptionStatic();
42 
43   //------------------------------------------------------------------
44   // Constructors and destructors
45   //------------------------------------------------------------------
46   ProcessWindows(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp);
47 
48   ~ProcessWindows();
49 
50   size_t GetSTDOUT(char *buf, size_t buf_size, Status &error) override;
51   size_t GetSTDERR(char *buf, size_t buf_size, Status &error) override;
52   size_t PutSTDIN(const char *buf, size_t buf_size, Status &error) override;
53 
54   // lldb_private::Process overrides
55   ConstString GetPluginName() override;
56   uint32_t GetPluginVersion() override;
57 
58   Status EnableBreakpointSite(BreakpointSite *bp_site) override;
59   Status DisableBreakpointSite(BreakpointSite *bp_site) override;
60 
61   Status DoDetach(bool keep_stopped) override;
62   Status DoLaunch(Module *exe_module, ProcessLaunchInfo &launch_info) override;
63   Status DoAttachToProcessWithID(
64       lldb::pid_t pid,
65       const lldb_private::ProcessAttachInfo &attach_info) override;
66   Status DoResume() override;
67   Status DoDestroy() override;
68   Status DoHalt(bool &caused_stop) override;
69 
70   void DidLaunch() override;
71   void DidAttach(lldb_private::ArchSpec &arch_spec) override;
72 
73   void RefreshStateAfterStop() override;
74 
75   bool CanDebug(lldb::TargetSP target_sp,
76                 bool plugin_specified_by_name) override;
77   bool DestroyRequiresHalt() override { return false; }
78   bool UpdateThreadList(ThreadList &old_thread_list,
79                         ThreadList &new_thread_list) override;
80   bool IsAlive() override;
81 
82   size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
83                       Status &error) override;
84   size_t DoWriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
85                        Status &error) override;
86   lldb::addr_t DoAllocateMemory(size_t size, uint32_t permissions,
87                                 Status &error) override;
88   Status DoDeallocateMemory(lldb::addr_t ptr) override;
89   Status GetMemoryRegionInfo(lldb::addr_t vm_addr,
90                              MemoryRegionInfo &info) override;
91 
92   lldb::addr_t GetImageInfoAddress() override;
93 
94   DynamicLoaderWindowsDYLD *GetDynamicLoader() override;
95 
96   // IDebugDelegate overrides.
97   void OnExitProcess(uint32_t exit_code) override;
98   void OnDebuggerConnected(lldb::addr_t image_base) override;
99   ExceptionResult OnDebugException(bool first_chance,
100                                    const ExceptionRecord &record) override;
101   void OnCreateThread(const HostThread &thread) override;
102   void OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) override;
103   void OnLoadDll(const ModuleSpec &module_spec,
104                  lldb::addr_t module_addr) override;
105   void OnUnloadDll(lldb::addr_t module_addr) override;
106   void OnDebugString(const std::string &string) override;
107   void OnDebuggerError(const Status &error, uint32_t type) override;
108 
109 private:
110   Status WaitForDebuggerConnection(DebuggerThreadSP debugger,
111                                    HostProcess &process);
112 
113   // These decode the page protection bits.
114   static bool IsPageReadable(uint32_t protect);
115   static bool IsPageWritable(uint32_t protect);
116   static bool IsPageExecutable(uint32_t protect);
117 
118   llvm::sys::Mutex m_mutex;
119   std::unique_ptr<ProcessWindowsData> m_session_data;
120 };
121 }
122 
123 #endif // liblldb_Plugins_Process_Windows_Common_ProcessWindows_H_
124