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_
13 #include "NativeBreakpointList.h"
14 #include "NativeThreadProtocol.h"
15 #include "NativeWatchpointList.h"
16 #include "lldb/Host/Host.h"
17 #include "lldb/Host/MainLoop.h"
18 #include "lldb/Utility/ArchSpec.h"
19 #include "lldb/Utility/Status.h"
20 #include "lldb/Utility/TraceOptions.h"
21 #include "lldb/lldb-private-forward.h"
22 #include "lldb/lldb-types.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/DenseSet.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/Support/Error.h"
27 #include "llvm/Support/MemoryBuffer.h"
30 namespace lldb_private {
31 class MemoryRegionInfo;
32 class ResumeActionList;
34 //------------------------------------------------------------------
35 // NativeProcessProtocol
36 //------------------------------------------------------------------
37 class NativeProcessProtocol {
38 friend class SoftwareBreakpoint;
41 virtual ~NativeProcessProtocol() {}
43 virtual Status Resume(const ResumeActionList &resume_actions) = 0;
45 virtual Status Halt() = 0;
47 virtual Status Detach() = 0;
49 //------------------------------------------------------------------
50 /// Sends a process a UNIX signal \a signal.
53 /// Returns an error object.
54 //------------------------------------------------------------------
55 virtual Status Signal(int signo) = 0;
57 //------------------------------------------------------------------
58 /// Tells a process to interrupt all operations as if by a Ctrl-C.
60 /// The default implementation will send a local host's equivalent of
61 /// a SIGSTOP to the process via the NativeProcessProtocol::Signal()
65 /// Returns an error object.
66 //------------------------------------------------------------------
67 virtual Status Interrupt();
69 virtual Status Kill() = 0;
71 //------------------------------------------------------------------
72 // Tells a process not to stop the inferior on given signals
73 // and just reinject them back.
74 //------------------------------------------------------------------
75 virtual Status IgnoreSignals(llvm::ArrayRef<int> signals);
77 //----------------------------------------------------------------------
78 // Memory and memory region functions
79 //----------------------------------------------------------------------
81 virtual Status GetMemoryRegionInfo(lldb::addr_t load_addr,
82 MemoryRegionInfo &range_info);
84 virtual Status ReadMemory(lldb::addr_t addr, void *buf, size_t size,
85 size_t &bytes_read) = 0;
87 virtual Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf,
88 size_t size, size_t &bytes_read) = 0;
90 virtual Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
91 size_t &bytes_written) = 0;
93 virtual Status AllocateMemory(size_t size, uint32_t permissions,
94 lldb::addr_t &addr) = 0;
96 virtual Status DeallocateMemory(lldb::addr_t addr) = 0;
98 virtual lldb::addr_t GetSharedLibraryInfoAddress() = 0;
100 virtual bool IsAlive() const;
102 virtual size_t UpdateThreads() = 0;
104 virtual const ArchSpec &GetArchitecture() const = 0;
106 //----------------------------------------------------------------------
107 // Breakpoint functions
108 //----------------------------------------------------------------------
109 virtual Status SetBreakpoint(lldb::addr_t addr, uint32_t size,
112 virtual Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false);
114 virtual Status EnableBreakpoint(lldb::addr_t addr);
116 virtual Status DisableBreakpoint(lldb::addr_t addr);
118 //----------------------------------------------------------------------
119 // Hardware Breakpoint functions
120 //----------------------------------------------------------------------
121 virtual const HardwareBreakpointMap &GetHardwareBreakpointMap() const;
123 virtual Status SetHardwareBreakpoint(lldb::addr_t addr, size_t size);
125 virtual Status RemoveHardwareBreakpoint(lldb::addr_t addr);
127 //----------------------------------------------------------------------
128 // Watchpoint functions
129 //----------------------------------------------------------------------
130 virtual const NativeWatchpointList::WatchpointMap &GetWatchpointMap() const;
132 virtual llvm::Optional<std::pair<uint32_t, uint32_t>>
133 GetHardwareDebugSupportInfo() const;
135 virtual Status SetWatchpoint(lldb::addr_t addr, size_t size,
136 uint32_t watch_flags, bool hardware);
138 virtual Status RemoveWatchpoint(lldb::addr_t addr);
140 //----------------------------------------------------------------------
142 //----------------------------------------------------------------------
143 lldb::pid_t GetID() const { return m_pid; }
145 lldb::StateType GetState() const;
147 bool IsRunning() const {
148 return m_state == lldb::eStateRunning || IsStepping();
151 bool IsStepping() const { return m_state == lldb::eStateStepping; }
153 bool CanResume() const { return m_state == lldb::eStateStopped; }
155 lldb::ByteOrder GetByteOrder() const {
156 return GetArchitecture().GetByteOrder();
159 virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
160 GetAuxvData() const = 0;
162 //----------------------------------------------------------------------
164 //----------------------------------------------------------------------
165 virtual llvm::Optional<WaitStatus> GetExitStatus();
167 virtual bool SetExitStatus(WaitStatus status, bool bNotifyStateChange);
169 //----------------------------------------------------------------------
171 //----------------------------------------------------------------------
172 NativeThreadProtocol *GetThreadAtIndex(uint32_t idx);
174 NativeThreadProtocol *GetThreadByID(lldb::tid_t tid);
176 void SetCurrentThreadID(lldb::tid_t tid) { m_current_thread_id = tid; }
178 lldb::tid_t GetCurrentThreadID() { return m_current_thread_id; }
180 NativeThreadProtocol *GetCurrentThread() {
181 return GetThreadByID(m_current_thread_id);
184 //----------------------------------------------------------------------
185 // Access to inferior stdio
186 //----------------------------------------------------------------------
187 virtual int GetTerminalFileDescriptor() { return m_terminal_fd; }
189 //----------------------------------------------------------------------
191 //----------------------------------------------------------------------
193 uint32_t GetStopID() const;
195 // ---------------------------------------------------------------------
196 // Callbacks for low-level process state changes
197 // ---------------------------------------------------------------------
198 class NativeDelegate {
200 virtual ~NativeDelegate() {}
202 virtual void InitializeDelegate(NativeProcessProtocol *process) = 0;
204 virtual void ProcessStateChanged(NativeProcessProtocol *process,
205 lldb::StateType state) = 0;
207 virtual void DidExec(NativeProcessProtocol *process) = 0;
210 //------------------------------------------------------------------
211 /// Register a native delegate.
213 /// Clients can register nofication callbacks by passing in a
214 /// NativeDelegate impl and passing it into this function.
216 /// Note: it is required that the lifetime of the
217 /// native_delegate outlive the NativeProcessProtocol.
219 /// @param[in] native_delegate
220 /// A NativeDelegate impl to be called when certain events
221 /// happen within the NativeProcessProtocol or related threads.
224 /// true if the delegate was registered successfully;
225 /// false if the delegate was already registered.
227 /// @see NativeProcessProtocol::NativeDelegate.
228 //------------------------------------------------------------------
229 bool RegisterNativeDelegate(NativeDelegate &native_delegate);
231 //------------------------------------------------------------------
232 /// Unregister a native delegate previously registered.
234 /// @param[in] native_delegate
235 /// A NativeDelegate impl previously registered with this process.
237 /// @return Returns \b true if the NativeDelegate was
238 /// successfully removed from the process, \b false otherwise.
240 /// @see NativeProcessProtocol::NativeDelegate
241 //------------------------------------------------------------------
242 bool UnregisterNativeDelegate(NativeDelegate &native_delegate);
244 virtual Status GetLoadedModuleFileSpec(const char *module_path,
245 FileSpec &file_spec) = 0;
247 virtual Status GetFileLoadAddress(const llvm::StringRef &file_name,
248 lldb::addr_t &load_addr) = 0;
253 //------------------------------------------------------------------
254 /// Launch a process for debugging.
256 /// @param[in] launch_info
257 /// Information required to launch the process.
259 /// @param[in] native_delegate
260 /// The delegate that will receive messages regarding the
261 /// inferior. Must outlive the NativeProcessProtocol
264 /// @param[in] mainloop
265 /// The mainloop instance with which the process can register
266 /// callbacks. Must outlive the NativeProcessProtocol
270 /// A NativeProcessProtocol shared pointer if the operation succeeded or
271 /// an error object if it failed.
272 //------------------------------------------------------------------
273 virtual llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
274 Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
275 MainLoop &mainloop) const = 0;
277 //------------------------------------------------------------------
278 /// Attach to an existing process.
281 /// pid of the process locatable
283 /// @param[in] native_delegate
284 /// The delegate that will receive messages regarding the
285 /// inferior. Must outlive the NativeProcessProtocol
288 /// @param[in] mainloop
289 /// The mainloop instance with which the process can register
290 /// callbacks. Must outlive the NativeProcessProtocol
294 /// A NativeProcessProtocol shared pointer if the operation succeeded or
295 /// an error object if it failed.
296 //------------------------------------------------------------------
297 virtual llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
298 Attach(lldb::pid_t pid, NativeDelegate &native_delegate,
299 MainLoop &mainloop) const = 0;
302 //------------------------------------------------------------------
303 /// StartTracing API for starting a tracing instance with the
304 /// TraceOptions on a specific thread or process.
306 /// @param[in] config
307 /// The configuration to use when starting tracing.
309 /// @param[out] error
310 /// Status indicates what went wrong.
313 /// The API returns a user_id which can be used to get trace
314 /// data, trace configuration or stopping the trace instance.
315 /// The user_id is a key to identify and operate with a tracing
316 /// instance. It may refer to the complete process or a single
318 //------------------------------------------------------------------
319 virtual lldb::user_id_t StartTrace(const TraceOptions &config,
321 error.SetErrorString("Not implemented");
322 return LLDB_INVALID_UID;
325 //------------------------------------------------------------------
326 /// StopTracing API as the name suggests stops a tracing instance.
328 /// @param[in] traceid
329 /// The user id of the trace intended to be stopped. Now a
330 /// user_id may map to multiple threads in which case this API
331 /// could be used to stop the tracing for a specific thread by
332 /// supplying its thread id.
334 /// @param[in] thread
335 /// Thread is needed when the complete process is being traced
336 /// and the user wishes to stop tracing on a particular thread.
339 /// Status indicating what went wrong.
340 //------------------------------------------------------------------
341 virtual Status StopTrace(lldb::user_id_t traceid,
342 lldb::tid_t thread = LLDB_INVALID_THREAD_ID) {
343 return Status("Not implemented");
346 //------------------------------------------------------------------
347 /// This API provides the trace data collected in the form of raw
350 /// @param[in] traceid thread
351 /// The traceid and thread provide the context for the trace
354 /// @param[in] buffer
355 /// The buffer provides the destination buffer where the trace
356 /// data would be read to. The buffer should be truncated to the
357 /// filled length by this function.
359 /// @param[in] offset
360 /// There is possibility to read partially the trace data from
361 /// a specified offset where in such cases the buffer provided
362 /// may be smaller than the internal trace collection container.
365 /// The size of the data actually read.
366 //------------------------------------------------------------------
367 virtual Status GetData(lldb::user_id_t traceid, lldb::tid_t thread,
368 llvm::MutableArrayRef<uint8_t> &buffer,
370 return Status("Not implemented");
373 //------------------------------------------------------------------
374 /// Similar API as above except it aims to provide any extra data
375 /// useful for decoding the actual trace data.
376 //------------------------------------------------------------------
377 virtual Status GetMetaData(lldb::user_id_t traceid, lldb::tid_t thread,
378 llvm::MutableArrayRef<uint8_t> &buffer,
380 return Status("Not implemented");
383 //------------------------------------------------------------------
384 /// API to query the TraceOptions for a given user id
386 /// @param[in] traceid
387 /// The user id of the tracing instance.
389 /// @param[in] config
390 /// The thread id of the tracing instance, in case configuration
391 /// for a specific thread is needed should be specified in the
394 /// @param[out] error
395 /// Status indicates what went wrong.
397 /// @param[out] config
398 /// The actual configuration being used for tracing.
399 //------------------------------------------------------------------
400 virtual Status GetTraceConfig(lldb::user_id_t traceid, TraceOptions &config) {
401 return Status("Not implemented");
407 std::vector<std::unique_ptr<NativeThreadProtocol>> m_threads;
408 lldb::tid_t m_current_thread_id = LLDB_INVALID_THREAD_ID;
409 mutable std::recursive_mutex m_threads_mutex;
411 lldb::StateType m_state = lldb::eStateInvalid;
412 mutable std::recursive_mutex m_state_mutex;
414 llvm::Optional<WaitStatus> m_exit_status;
416 std::recursive_mutex m_delegates_mutex;
417 std::vector<NativeDelegate *> m_delegates;
418 NativeBreakpointList m_breakpoint_list;
419 NativeWatchpointList m_watchpoint_list;
420 HardwareBreakpointMap m_hw_breakpoints_map;
422 uint32_t m_stop_id = 0;
424 // Set of signal numbers that LLDB directly injects back to inferior
425 // without stopping it.
426 llvm::DenseSet<int> m_signals_to_ignore;
428 // lldb_private::Host calls should be used to launch a process for debugging,
430 // then the process should be attached to. When attaching to a process
431 // lldb_private::Host calls should be used to locate the process to attach to,
432 // and then this function should be called.
433 NativeProcessProtocol(lldb::pid_t pid, int terminal_fd,
434 NativeDelegate &delegate);
436 // -----------------------------------------------------------
437 // Internal interface for state handling
438 // -----------------------------------------------------------
439 void SetState(lldb::StateType state, bool notify_delegates = true);
441 // Derived classes need not implement this. It can be used as a
442 // hook to clear internal caches that should be invalidated when
445 // Note this function is called with the state mutex obtained
447 virtual void DoStopIDBumped(uint32_t newBumpId);
449 // -----------------------------------------------------------
450 // Internal interface for software breakpoints
451 // -----------------------------------------------------------
452 Status SetSoftwareBreakpoint(lldb::addr_t addr, uint32_t size_hint);
455 GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint,
456 size_t &actual_opcode_size,
457 const uint8_t *&trap_opcode_bytes) = 0;
459 // -----------------------------------------------------------
460 /// Notify the delegate that an exec occurred.
462 /// Provide a mechanism for a delegate to clear out any exec-
464 // -----------------------------------------------------------
465 void NotifyDidExec();
467 NativeThreadProtocol *GetThreadByIDUnlocked(lldb::tid_t tid);
469 // -----------------------------------------------------------
470 // Static helper methods for derived classes.
471 // -----------------------------------------------------------
472 static Status ResolveProcessArchitecture(lldb::pid_t pid, ArchSpec &arch);
475 void SynchronouslyNotifyProcessStateChanged(lldb::StateType state);
477 } // namespace lldb_private
479 #endif // #ifndef liblldb_NativeProcessProtocol_h_