1 //===-- NativeProcessProtocol.h ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_NativeProcessProtocol_h_
11 #define liblldb_NativeProcessProtocol_h_
16 #include "lldb/lldb-private-forward.h"
17 #include "lldb/lldb-types.h"
18 #include "lldb/Core/Error.h"
19 #include "lldb/Host/MainLoop.h"
20 #include "llvm/ADT/StringRef.h"
22 #include "NativeBreakpointList.h"
23 #include "NativeWatchpointList.h"
25 namespace lldb_private
27 class MemoryRegionInfo;
28 class ResumeActionList;
30 //------------------------------------------------------------------
31 // NativeProcessProtocol
32 //------------------------------------------------------------------
33 class NativeProcessProtocol :
34 public std::enable_shared_from_this<NativeProcessProtocol>
36 friend class SoftwareBreakpoint;
40 // lldb_private::Host calls should be used to launch a process for debugging, and
41 // then the process should be attached to. When attaching to a process
42 // lldb_private::Host calls should be used to locate the process to attach to,
43 // and then this function should be called.
44 NativeProcessProtocol (lldb::pid_t pid);
46 virtual ~NativeProcessProtocol ()
51 Resume (const ResumeActionList &resume_actions) = 0;
59 //------------------------------------------------------------------
60 /// Sends a process a UNIX signal \a signal.
63 /// Returns an error object.
64 //------------------------------------------------------------------
66 Signal (int signo) = 0;
68 //------------------------------------------------------------------
69 /// Tells a process to interrupt all operations as if by a Ctrl-C.
71 /// The default implementation will send a local host's equivalent of
72 /// a SIGSTOP to the process via the NativeProcessProtocol::Signal()
76 /// Returns an error object.
77 //------------------------------------------------------------------
84 //----------------------------------------------------------------------
85 // Memory and memory region functions
86 //----------------------------------------------------------------------
89 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info);
92 ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) = 0;
95 ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) = 0;
98 WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) = 0;
101 AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) = 0;
104 DeallocateMemory (lldb::addr_t addr) = 0;
107 GetSharedLibraryInfoAddress () = 0;
113 UpdateThreads () = 0;
116 GetArchitecture (ArchSpec &arch) const = 0;
118 //----------------------------------------------------------------------
119 // Breakpoint functions
120 //----------------------------------------------------------------------
122 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) = 0;
125 RemoveBreakpoint (lldb::addr_t addr);
128 EnableBreakpoint (lldb::addr_t addr);
131 DisableBreakpoint (lldb::addr_t addr);
133 //----------------------------------------------------------------------
134 // Watchpoint functions
135 //----------------------------------------------------------------------
136 virtual const NativeWatchpointList::WatchpointMap&
137 GetWatchpointMap () const;
140 GetMaxWatchpoints () const;
143 SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware);
146 RemoveWatchpoint (lldb::addr_t addr);
148 //----------------------------------------------------------------------
150 //----------------------------------------------------------------------
163 return m_state == lldb::eStateRunning || IsStepping();
169 return m_state == lldb::eStateStepping;
175 return m_state == lldb::eStateStopped;
179 GetByteOrder (lldb::ByteOrder &byte_order) const;
181 //----------------------------------------------------------------------
183 //----------------------------------------------------------------------
185 GetExitStatus (lldb_private::ExitType *exit_type, int *status, std::string &exit_description);
188 SetExitStatus (lldb_private::ExitType exit_type, int status, const char *exit_description, bool bNotifyStateChange);
190 //----------------------------------------------------------------------
192 //----------------------------------------------------------------------
193 NativeThreadProtocolSP
194 GetThreadAtIndex (uint32_t idx);
196 NativeThreadProtocolSP
197 GetThreadByID (lldb::tid_t tid);
200 SetCurrentThreadID (lldb::tid_t tid)
202 m_current_thread_id = tid;
206 GetCurrentThreadID ()
208 return m_current_thread_id;
211 NativeThreadProtocolSP
214 return GetThreadByID (m_current_thread_id);
217 //----------------------------------------------------------------------
218 // Access to inferior stdio
219 //----------------------------------------------------------------------
221 int GetTerminalFileDescriptor ()
223 return m_terminal_fd;
226 //----------------------------------------------------------------------
228 //----------------------------------------------------------------------
233 // ---------------------------------------------------------------------
234 // Callbacks for low-level process state changes
235 // ---------------------------------------------------------------------
240 ~NativeDelegate () {}
243 InitializeDelegate (NativeProcessProtocol *process) = 0;
246 ProcessStateChanged (NativeProcessProtocol *process, lldb::StateType state) = 0;
249 DidExec (NativeProcessProtocol *process) = 0;
252 //------------------------------------------------------------------
253 /// Register a native delegate.
255 /// Clients can register nofication callbacks by passing in a
256 /// NativeDelegate impl and passing it into this function.
258 /// Note: it is required that the lifetime of the
259 /// native_delegate outlive the NativeProcessProtocol.
261 /// @param[in] native_delegate
262 /// A NativeDelegate impl to be called when certain events
263 /// happen within the NativeProcessProtocol or related threads.
266 /// true if the delegate was registered successfully;
267 /// false if the delegate was already registered.
269 /// @see NativeProcessProtocol::NativeDelegate.
270 //------------------------------------------------------------------
272 RegisterNativeDelegate (NativeDelegate &native_delegate);
274 //------------------------------------------------------------------
275 /// Unregister a native delegate previously registered.
277 /// @param[in] native_delegate
278 /// A NativeDelegate impl previously registered with this process.
280 /// @return Returns \b true if the NativeDelegate was
281 /// successfully removed from the process, \b false otherwise.
283 /// @see NativeProcessProtocol::NativeDelegate
284 //------------------------------------------------------------------
286 UnregisterNativeDelegate (NativeDelegate &native_delegate);
289 GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) = 0;
292 GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) = 0;
294 //------------------------------------------------------------------
295 /// Launch a process for debugging. This method will create an concrete
296 /// instance of NativeProcessProtocol, based on the host platform.
297 /// (e.g. NativeProcessLinux on linux, etc.)
299 /// @param[in] launch_info
300 /// Information required to launch the process.
302 /// @param[in] native_delegate
303 /// The delegate that will receive messages regarding the
304 /// inferior. Must outlive the NativeProcessProtocol
307 /// @param[in] mainloop
308 /// The mainloop instance with which the process can register
309 /// callbacks. Must outlive the NativeProcessProtocol
312 /// @param[out] process_sp
313 /// On successful return from the method, this parameter
314 /// contains the shared pointer to the
315 /// NativeProcessProtocol that can be used to manipulate
316 /// the native process.
319 /// An error object indicating if the operation succeeded,
320 /// and if not, what error occurred.
321 //------------------------------------------------------------------
323 Launch (ProcessLaunchInfo &launch_info,
324 NativeDelegate &native_delegate,
326 NativeProcessProtocolSP &process_sp);
328 //------------------------------------------------------------------
329 /// Attach to an existing process. This method will create an concrete
330 /// instance of NativeProcessProtocol, based on the host platform.
331 /// (e.g. NativeProcessLinux on linux, etc.)
334 /// pid of the process locatable
336 /// @param[in] native_delegate
337 /// The delegate that will receive messages regarding the
338 /// inferior. Must outlive the NativeProcessProtocol
341 /// @param[in] mainloop
342 /// The mainloop instance with which the process can register
343 /// callbacks. Must outlive the NativeProcessProtocol
346 /// @param[out] process_sp
347 /// On successful return from the method, this parameter
348 /// contains the shared pointer to the
349 /// NativeProcessProtocol that can be used to manipulate
350 /// the native process.
353 /// An error object indicating if the operation succeeded,
354 /// and if not, what error occurred.
355 //------------------------------------------------------------------
357 Attach (lldb::pid_t pid,
358 NativeDelegate &native_delegate,
360 NativeProcessProtocolSP &process_sp);
365 std::vector<NativeThreadProtocolSP> m_threads;
366 lldb::tid_t m_current_thread_id;
367 mutable std::recursive_mutex m_threads_mutex;
369 lldb::StateType m_state;
370 mutable std::recursive_mutex m_state_mutex;
372 lldb_private::ExitType m_exit_type;
374 std::string m_exit_description;
375 std::recursive_mutex m_delegates_mutex;
376 std::vector<NativeDelegate*> m_delegates;
377 NativeBreakpointList m_breakpoint_list;
378 NativeWatchpointList m_watchpoint_list;
382 // -----------------------------------------------------------
383 // Internal interface for state handling
384 // -----------------------------------------------------------
386 SetState (lldb::StateType state, bool notify_delegates = true);
388 // Derived classes need not implement this. It can be used as a
389 // hook to clear internal caches that should be invalidated when
392 // Note this function is called with the state mutex obtained
395 DoStopIDBumped (uint32_t newBumpId);
397 // -----------------------------------------------------------
398 // Internal interface for software breakpoints
399 // -----------------------------------------------------------
401 SetSoftwareBreakpoint (lldb::addr_t addr, uint32_t size_hint);
404 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) = 0;
406 // -----------------------------------------------------------
407 /// Notify the delegate that an exec occurred.
409 /// Provide a mechanism for a delegate to clear out any exec-
411 // -----------------------------------------------------------
415 NativeThreadProtocolSP
416 GetThreadByIDUnlocked (lldb::tid_t tid);
421 SynchronouslyNotifyProcessStateChanged (lldb::StateType state);
425 #endif // #ifndef liblldb_NativeProcessProtocol_h_