1 //===-- ProcessKDP.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 LLDB_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_PROCESSKDP_H
10 #define LLDB_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_PROCESSKDP_H
11 
12 #include <list>
13 #include <vector>
14 
15 #include "lldb/Core/ThreadSafeValue.h"
16 #include "lldb/Host/HostThread.h"
17 #include "lldb/Target/Process.h"
18 #include "lldb/Target/Thread.h"
19 #include "lldb/Utility/ArchSpec.h"
20 #include "lldb/Utility/Broadcaster.h"
21 #include "lldb/Utility/ConstString.h"
22 #include "lldb/Utility/Status.h"
23 #include "lldb/Utility/StreamString.h"
24 #include "lldb/Utility/StringList.h"
25 
26 #include "CommunicationKDP.h"
27 
28 class ThreadKDP;
29 
30 class ProcessKDP : public lldb_private::Process {
31 public:
32   // Constructors and Destructors
33   static lldb::ProcessSP
34   CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
35                  const lldb_private::FileSpec *crash_file_path,
36                  bool can_connect);
37 
38   static void Initialize();
39 
40   static void DebuggerInitialize(lldb_private::Debugger &debugger);
41 
42   static void Terminate();
43 
GetPluginNameStatic()44   static llvm::StringRef GetPluginNameStatic() { return "kdp-remote"; }
45 
46   static llvm::StringRef GetPluginDescriptionStatic();
47 
48   // Constructors and Destructors
49   ProcessKDP(lldb::TargetSP target_sp, lldb::ListenerSP listener);
50 
51   ~ProcessKDP() override;
52 
53   // Check if a given Process
54   bool CanDebug(lldb::TargetSP target_sp,
55                 bool plugin_specified_by_name) override;
56   lldb_private::CommandObject *GetPluginCommandObject() override;
57 
58   // Creating a new process, or attaching to an existing one
59   lldb_private::Status DoWillLaunch(lldb_private::Module *module) override;
60 
61   lldb_private::Status
62   DoLaunch(lldb_private::Module *exe_module,
63            lldb_private::ProcessLaunchInfo &launch_info) override;
64 
65   lldb_private::Status DoWillAttachToProcessWithID(lldb::pid_t pid) override;
66 
67   lldb_private::Status
68   DoWillAttachToProcessWithName(const char *process_name,
69                                 bool wait_for_launch) override;
70 
71   lldb_private::Status DoConnectRemote(llvm::StringRef remote_url) override;
72 
73   lldb_private::Status DoAttachToProcessWithID(
74       lldb::pid_t pid,
75       const lldb_private::ProcessAttachInfo &attach_info) override;
76 
77   lldb_private::Status DoAttachToProcessWithName(
78       const char *process_name,
79       const lldb_private::ProcessAttachInfo &attach_info) override;
80 
81   void DidAttach(lldb_private::ArchSpec &process_arch) override;
82 
83   lldb::addr_t GetImageInfoAddress() override;
84 
85   lldb_private::DynamicLoader *GetDynamicLoader() override;
86 
87   // PluginInterface protocol
GetPluginName()88   llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
89 
90   // Process Control
91   lldb_private::Status WillResume() override;
92 
93   lldb_private::Status DoResume() override;
94 
95   lldb_private::Status DoHalt(bool &caused_stop) override;
96 
97   lldb_private::Status DoDetach(bool keep_stopped) override;
98 
99   lldb_private::Status DoSignal(int signal) override;
100 
101   lldb_private::Status DoDestroy() override;
102 
103   void RefreshStateAfterStop() override;
104 
105   // Process Queries
106   bool IsAlive() override;
107 
108   // Process Memory
109   size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
110                       lldb_private::Status &error) override;
111 
112   size_t DoWriteMemory(lldb::addr_t addr, const void *buf, size_t size,
113                        lldb_private::Status &error) override;
114 
115   lldb::addr_t DoAllocateMemory(size_t size, uint32_t permissions,
116                                 lldb_private::Status &error) override;
117 
118   lldb_private::Status DoDeallocateMemory(lldb::addr_t ptr) override;
119 
120   // Process Breakpoints
121   lldb_private::Status
122   EnableBreakpointSite(lldb_private::BreakpointSite *bp_site) override;
123 
124   lldb_private::Status
125   DisableBreakpointSite(lldb_private::BreakpointSite *bp_site) override;
126 
127   // Process Watchpoints
128   lldb_private::Status EnableWatchpoint(lldb_private::Watchpoint *wp,
129                                         bool notify = true) override;
130 
131   lldb_private::Status DisableWatchpoint(lldb_private::Watchpoint *wp,
132                                          bool notify = true) override;
133 
GetCommunication()134   CommunicationKDP &GetCommunication() { return m_comm; }
135 
136 protected:
137   friend class ThreadKDP;
138   friend class CommunicationKDP;
139 
140   // Accessors
IsRunning(lldb::StateType state)141   bool IsRunning(lldb::StateType state) {
142     return state == lldb::eStateRunning || IsStepping(state);
143   }
144 
IsStepping(lldb::StateType state)145   bool IsStepping(lldb::StateType state) {
146     return state == lldb::eStateStepping;
147   }
148 
CanResume(lldb::StateType state)149   bool CanResume(lldb::StateType state) { return state == lldb::eStateStopped; }
150 
HasExited(lldb::StateType state)151   bool HasExited(lldb::StateType state) { return state == lldb::eStateExited; }
152 
153   bool GetHostArchitecture(lldb_private::ArchSpec &arch);
154 
155   bool ProcessIDIsValid() const;
156 
157   void Clear();
158 
159   bool DoUpdateThreadList(lldb_private::ThreadList &old_thread_list,
160                           lldb_private::ThreadList &new_thread_list) override;
161 
162   enum {
163     eBroadcastBitAsyncContinue = (1 << 0),
164     eBroadcastBitAsyncThreadShouldExit = (1 << 1)
165   };
166 
167   lldb::ThreadSP GetKernelThread();
168 
169   /// Broadcaster event bits definitions.
170   CommunicationKDP m_comm;
171   lldb_private::Broadcaster m_async_broadcaster;
172   lldb_private::HostThread m_async_thread;
173   llvm::StringRef m_dyld_plugin_name;
174   lldb::addr_t m_kernel_load_addr;
175   lldb::CommandObjectSP m_command_sp;
176   lldb::ThreadWP m_kernel_thread_wp;
177 
178   bool StartAsyncThread();
179 
180   void StopAsyncThread();
181 
182   void *AsyncThread();
183 
184 private:
185   // For ProcessKDP only
186 
187   ProcessKDP(const ProcessKDP &) = delete;
188   const ProcessKDP &operator=(const ProcessKDP &) = delete;
189 };
190 
191 #endif // LLDB_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_PROCESSKDP_H
192