]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/common/NativeProcessProtocol.h
There is no Python in the FreeBSD base system
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / common / NativeProcessProtocol.h
1 //===-- NativeProcessProtocol.h ---------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_NativeProcessProtocol_h_
11 #define liblldb_NativeProcessProtocol_h_
12
13 #include <vector>
14
15 #include "lldb/lldb-private-forward.h"
16 #include "lldb/lldb-types.h"
17 #include "lldb/Core/Error.h"
18 #include "lldb/Host/Mutex.h"
19
20 #include "NativeBreakpointList.h"
21
22 namespace lldb_private
23 {
24     class MemoryRegionInfo;
25     class ResumeActionList;
26
27     //------------------------------------------------------------------
28     // NativeProcessProtocol
29     //------------------------------------------------------------------
30     class NativeProcessProtocol :
31         public std::enable_shared_from_this<NativeProcessProtocol>
32     {
33         friend class SoftwareBreakpoint;
34
35     public:
36         static NativeProcessProtocol *
37         CreateInstance (lldb::pid_t pid);
38
39         // lldb_private::Host calls should be used to launch a process for debugging, and
40         // then the process should be attached to. When attaching to a process
41         // lldb_private::Host calls should be used to locate the process to attach to,
42         // and then this function should be called.
43         NativeProcessProtocol (lldb::pid_t pid);
44
45     public:
46         virtual ~NativeProcessProtocol ()
47         {
48         }
49
50         virtual Error
51         Resume (const ResumeActionList &resume_actions) = 0;
52
53         virtual Error
54         Halt () = 0;
55
56         virtual Error
57         Detach () = 0;
58
59         //------------------------------------------------------------------
60         /// Sends a process a UNIX signal \a signal.
61         ///
62         /// Implementer note: the WillSignal ()/DidSignal () calls
63         /// from the Process class are not replicated here since no
64         /// concrete classes implemented any behavior for those and
65         /// put all the work in DoSignal (...).
66         ///
67         /// @return
68         ///     Returns an error object.
69         //------------------------------------------------------------------
70         virtual Error
71         Signal (int signo) = 0;
72
73         virtual Error
74         Kill () = 0;
75
76         //----------------------------------------------------------------------
77         // Memory and memory region functions
78         //----------------------------------------------------------------------
79
80         virtual Error
81         GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info);
82
83         virtual Error
84         ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) = 0;
85
86         virtual Error
87         WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) = 0;
88
89         virtual Error
90         AllocateMemory (lldb::addr_t size, uint32_t permissions, lldb::addr_t &addr) = 0;
91
92         virtual Error
93         DeallocateMemory (lldb::addr_t addr) = 0;
94
95         virtual lldb::addr_t
96         GetSharedLibraryInfoAddress () = 0;
97
98         virtual bool
99         IsAlive () const;
100
101         virtual size_t
102         UpdateThreads () = 0;
103
104         virtual bool
105         GetArchitecture (ArchSpec &arch) const = 0;
106
107         //----------------------------------------------------------------------
108         // Breakpoint functions
109         //----------------------------------------------------------------------
110         virtual Error
111         SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) = 0;
112
113         virtual Error
114         RemoveBreakpoint (lldb::addr_t addr);
115
116         virtual Error
117         EnableBreakpoint (lldb::addr_t addr);
118
119         virtual Error
120         DisableBreakpoint (lldb::addr_t addr);
121
122         //----------------------------------------------------------------------
123         // Watchpoint functions
124         //----------------------------------------------------------------------
125         virtual uint32_t
126         GetMaxWatchpoints () const;
127
128         virtual Error
129         SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware);
130
131         virtual Error
132         RemoveWatchpoint (lldb::addr_t addr);
133
134         //----------------------------------------------------------------------
135         // Accessors
136         //----------------------------------------------------------------------
137         lldb::pid_t
138         GetID() const
139         {
140             return m_pid;
141         }
142
143         lldb::StateType
144         GetState () const;
145
146         bool
147         IsRunning () const
148         {
149             return m_state == lldb::eStateRunning || IsStepping();
150         }
151
152         bool
153         IsStepping () const
154         {
155             return m_state == lldb::eStateStepping;
156         }
157
158         bool
159         CanResume () const
160         {
161             return m_state == lldb::eStateStopped;
162         }
163
164         bool
165         GetByteOrder (lldb::ByteOrder &byte_order) const;
166
167         //----------------------------------------------------------------------
168         // Exit Status
169         //----------------------------------------------------------------------
170         virtual bool
171         GetExitStatus (lldb_private::ExitType *exit_type, int *status, std::string &exit_description);
172
173         virtual bool
174         SetExitStatus (lldb_private::ExitType exit_type, int status, const char *exit_description, bool bNotifyStateChange);
175
176         //----------------------------------------------------------------------
177         // Access to threads
178         //----------------------------------------------------------------------
179         NativeThreadProtocolSP
180         GetThreadAtIndex (uint32_t idx);
181
182         NativeThreadProtocolSP
183         GetThreadByID (lldb::tid_t tid);
184
185         void
186         SetCurrentThreadID (lldb::tid_t tid)
187         {
188             m_current_thread_id = tid;
189         }
190
191         lldb::tid_t
192         GetCurrentThreadID ()
193         {
194             return m_current_thread_id;
195         }
196
197         NativeThreadProtocolSP
198         GetCurrentThread ()
199         {
200             return GetThreadByID (m_current_thread_id);
201         }
202
203         //----------------------------------------------------------------------
204         // Access to inferior stdio
205         //----------------------------------------------------------------------
206         virtual
207         int GetTerminalFileDescriptor ()
208         {
209             return m_terminal_fd;
210         }
211
212         //----------------------------------------------------------------------
213         // Stop id interface
214         //----------------------------------------------------------------------
215
216         uint32_t
217         GetStopID () const;
218
219         // ---------------------------------------------------------------------
220         // Callbacks for low-level process state changes
221         // ---------------------------------------------------------------------
222         class NativeDelegate
223         {
224         public:
225             virtual
226             ~NativeDelegate () {}
227
228             virtual void
229             InitializeDelegate (NativeProcessProtocol *process) = 0;
230
231             virtual void
232             ProcessStateChanged (NativeProcessProtocol *process, lldb::StateType state) = 0;
233
234             virtual void
235             DidExec (NativeProcessProtocol *process) = 0;
236         };
237
238         //------------------------------------------------------------------
239         /// Register a native delegate.
240         ///
241         /// Clients can register nofication callbacks by passing in a
242         /// NativeDelegate impl and passing it into this function.
243         ///
244         /// Note: it is required that the lifetime of the
245         /// native_delegate outlive the NativeProcessProtocol.
246         ///
247         /// @param[in] native_delegate
248         ///     A NativeDelegate impl to be called when certain events
249         ///     happen within the NativeProcessProtocol or related threads.
250         ///
251         /// @return
252         ///     true if the delegate was registered successfully;
253         ///     false if the delegate was already registered.
254         ///
255         /// @see NativeProcessProtocol::NativeDelegate.
256         //------------------------------------------------------------------
257         bool
258         RegisterNativeDelegate (NativeDelegate &native_delegate);
259
260         //------------------------------------------------------------------
261         /// Unregister a native delegate previously registered.
262         ///
263         /// @param[in] native_delegate
264         ///     A NativeDelegate impl previously registered with this process.
265         ///
266         /// @return Returns \b true if the NativeDelegate was
267         /// successfully removed from the process, \b false otherwise.
268         ///
269         /// @see NativeProcessProtocol::NativeDelegate
270         //------------------------------------------------------------------
271         bool
272         UnregisterNativeDelegate (NativeDelegate &native_delegate);
273
274     protected:
275         lldb::pid_t m_pid;
276
277         std::vector<NativeThreadProtocolSP> m_threads;
278         lldb::tid_t m_current_thread_id;
279         mutable Mutex m_threads_mutex;
280
281         lldb::StateType m_state;
282         mutable Mutex m_state_mutex;
283
284         lldb_private::ExitType m_exit_type;
285         int m_exit_status;
286         std::string m_exit_description;
287         Mutex m_delegates_mutex;
288         std::vector<NativeDelegate*> m_delegates;
289         NativeBreakpointList m_breakpoint_list;
290         int m_terminal_fd;
291         uint32_t m_stop_id;
292
293         // -----------------------------------------------------------
294         // Internal interface for state handling
295         // -----------------------------------------------------------
296         void
297         SetState (lldb::StateType state, bool notify_delegates = true);
298
299         // Derived classes need not impelment this.  It can be used as a
300         // hook to clear internal caches that should be invalidated when
301         // stop ids change.
302         //
303         // Note this function is called with the state mutex obtained
304         // by the caller.
305         virtual void
306         DoStopIDBumped (uint32_t newBumpId);
307
308         // -----------------------------------------------------------
309         // Internal interface for software breakpoints
310         // -----------------------------------------------------------
311         Error
312         SetSoftwareBreakpoint (lldb::addr_t addr, uint32_t size_hint);
313
314         virtual Error
315         GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) = 0;
316
317         // -----------------------------------------------------------
318         /// Notify the delegate that an exec occurred.
319         ///
320         /// Provide a mechanism for a delegate to clear out any exec-
321         /// sensitive data.
322         // -----------------------------------------------------------
323         void
324         NotifyDidExec ();
325
326     private:
327
328         void
329         SynchronouslyNotifyProcessStateChanged (lldb::StateType state);
330     };
331 }
332
333 #endif // #ifndef liblldb_NativeProcessProtocol_h_