• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
44   static lldb_private::ConstString GetPluginNameStatic();
45 
46   static const char *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 WillLaunch(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 WillAttachToProcessWithID(lldb::pid_t pid) override;
66 
67   lldb_private::Status
68   WillAttachToProcessWithName(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
88   lldb_private::ConstString GetPluginName() override;
89 
90   uint32_t GetPluginVersion() override;
91 
92   // Process Control
93   lldb_private::Status WillResume() override;
94 
95   lldb_private::Status DoResume() override;
96 
97   lldb_private::Status DoHalt(bool &caused_stop) override;
98 
99   lldb_private::Status DoDetach(bool keep_stopped) override;
100 
101   lldb_private::Status DoSignal(int signal) override;
102 
103   lldb_private::Status DoDestroy() override;
104 
105   void RefreshStateAfterStop() override;
106 
107   // Process Queries
108   bool IsAlive() override;
109 
110   // Process Memory
111   size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
112                       lldb_private::Status &error) override;
113 
114   size_t DoWriteMemory(lldb::addr_t addr, const void *buf, size_t size,
115                        lldb_private::Status &error) override;
116 
117   lldb::addr_t DoAllocateMemory(size_t size, uint32_t permissions,
118                                 lldb_private::Status &error) override;
119 
120   lldb_private::Status DoDeallocateMemory(lldb::addr_t ptr) override;
121 
122   // Process Breakpoints
123   lldb_private::Status
124   EnableBreakpointSite(lldb_private::BreakpointSite *bp_site) override;
125 
126   lldb_private::Status
127   DisableBreakpointSite(lldb_private::BreakpointSite *bp_site) override;
128 
129   // Process Watchpoints
130   lldb_private::Status EnableWatchpoint(lldb_private::Watchpoint *wp,
131                                         bool notify = true) override;
132 
133   lldb_private::Status DisableWatchpoint(lldb_private::Watchpoint *wp,
134                                          bool notify = true) override;
135 
GetCommunication()136   CommunicationKDP &GetCommunication() { return m_comm; }
137 
138 protected:
139   friend class ThreadKDP;
140   friend class CommunicationKDP;
141 
142   // Accessors
IsRunning(lldb::StateType state)143   bool IsRunning(lldb::StateType state) {
144     return state == lldb::eStateRunning || IsStepping(state);
145   }
146 
IsStepping(lldb::StateType state)147   bool IsStepping(lldb::StateType state) {
148     return state == lldb::eStateStepping;
149   }
150 
CanResume(lldb::StateType state)151   bool CanResume(lldb::StateType state) { return state == lldb::eStateStopped; }
152 
HasExited(lldb::StateType state)153   bool HasExited(lldb::StateType state) { return state == lldb::eStateExited; }
154 
155   bool GetHostArchitecture(lldb_private::ArchSpec &arch);
156 
157   bool ProcessIDIsValid() const;
158 
159   void Clear();
160 
161   bool UpdateThreadList(lldb_private::ThreadList &old_thread_list,
162                         lldb_private::ThreadList &new_thread_list) override;
163 
164   enum {
165     eBroadcastBitAsyncContinue = (1 << 0),
166     eBroadcastBitAsyncThreadShouldExit = (1 << 1)
167   };
168 
169   lldb::ThreadSP GetKernelThread();
170 
171   /// Broadcaster event bits definitions.
172   CommunicationKDP m_comm;
173   lldb_private::Broadcaster m_async_broadcaster;
174   lldb_private::HostThread m_async_thread;
175   lldb_private::ConstString m_dyld_plugin_name;
176   lldb::addr_t m_kernel_load_addr;
177   lldb::CommandObjectSP m_command_sp;
178   lldb::ThreadWP m_kernel_thread_wp;
179 
180   bool StartAsyncThread();
181 
182   void StopAsyncThread();
183 
184   static void *AsyncThread(void *arg);
185 
186 private:
187   // For ProcessKDP only
188 
189   ProcessKDP(const ProcessKDP &) = delete;
190   const ProcessKDP &operator=(const ProcessKDP &) = delete;
191 };
192 
193 #endif // LLDB_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_PROCESSKDP_H
194