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