1 //===-- NativeProcessLinux.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_NativeProcessLinux_H_
11 #define liblldb_NativeProcessLinux_H_
14 #include <unordered_set>
16 // Other libraries and framework includes
17 #include "lldb/Core/ArchSpec.h"
18 #include "lldb/Host/Debug.h"
19 #include "lldb/Host/FileSpec.h"
20 #include "lldb/Host/HostThread.h"
21 #include "lldb/Target/MemoryRegionInfo.h"
22 #include "lldb/lldb-types.h"
24 #include "NativeThreadLinux.h"
25 #include "lldb/Host/common/NativeProcessProtocol.h"
27 namespace lldb_private {
31 namespace process_linux {
32 /// @class NativeProcessLinux
33 /// @brief Manages communication with the inferior (debugee) process.
35 /// Upon construction, this class prepares and launches an inferior process for
38 /// Changes in the inferior process state are broadcasted.
39 class NativeProcessLinux : public NativeProcessProtocol {
40 friend Error NativeProcessProtocol::Launch(
41 ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
42 MainLoop &mainloop, NativeProcessProtocolSP &process_sp);
44 friend Error NativeProcessProtocol::Attach(
45 lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
46 MainLoop &mainloop, NativeProcessProtocolSP &process_sp);
49 // ---------------------------------------------------------------------
50 // NativeProcessProtocol Interface
51 // ---------------------------------------------------------------------
52 Error Resume(const ResumeActionList &resume_actions) override;
54 Error Halt() override;
56 Error Detach() override;
58 Error Signal(int signo) override;
60 Error Interrupt() override;
62 Error Kill() override;
64 Error GetMemoryRegionInfo(lldb::addr_t load_addr,
65 MemoryRegionInfo &range_info) override;
67 Error ReadMemory(lldb::addr_t addr, void *buf, size_t size,
68 size_t &bytes_read) override;
70 Error ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size,
71 size_t &bytes_read) override;
73 Error WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
74 size_t &bytes_written) override;
76 Error AllocateMemory(size_t size, uint32_t permissions,
77 lldb::addr_t &addr) override;
79 Error DeallocateMemory(lldb::addr_t addr) override;
81 lldb::addr_t GetSharedLibraryInfoAddress() override;
83 size_t UpdateThreads() override;
85 bool GetArchitecture(ArchSpec &arch) const override;
87 Error SetBreakpoint(lldb::addr_t addr, uint32_t size, bool hardware) override;
89 void DoStopIDBumped(uint32_t newBumpId) override;
91 Error GetLoadedModuleFileSpec(const char *module_path,
92 FileSpec &file_spec) override;
94 Error GetFileLoadAddress(const llvm::StringRef &file_name,
95 lldb::addr_t &load_addr) override;
97 NativeThreadLinuxSP GetThreadByID(lldb::tid_t id);
99 // ---------------------------------------------------------------------
100 // Interface used by NativeRegisterContext-derived classes.
101 // ---------------------------------------------------------------------
102 static Error PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
103 void *data = nullptr, size_t data_size = 0,
104 long *result = nullptr);
106 bool SupportHardwareSingleStepping() const;
109 // ---------------------------------------------------------------------
110 // NativeProcessProtocol protected interface
111 // ---------------------------------------------------------------------
113 GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint,
114 size_t &actual_opcode_size,
115 const uint8_t *&trap_opcode_bytes) override;
118 MainLoop::SignalHandleUP m_sigchld_handle;
121 LazyBool m_supports_mem_region;
122 std::vector<MemoryRegionInfo> m_mem_region_cache;
124 lldb::tid_t m_pending_notification_tid;
126 // List of thread ids stepping with a breakpoint with the address of
127 // the relevan breakpoint
128 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
130 // ---------------------------------------------------------------------
131 // Private Instance Methods
132 // ---------------------------------------------------------------------
133 NativeProcessLinux();
135 Error LaunchInferior(MainLoop &mainloop, ProcessLaunchInfo &launch_info);
137 /// Attaches to an existing process. Forms the
138 /// implementation of Process::DoAttach
139 void AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, Error &error);
141 ::pid_t Attach(lldb::pid_t pid, Error &error);
143 static Error SetDefaultPtraceOpts(const lldb::pid_t);
145 static void *MonitorThread(void *baton);
147 void MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
149 void WaitForNewThread(::pid_t tid);
151 void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread);
153 void MonitorTrace(NativeThreadLinux &thread);
155 void MonitorBreakpoint(NativeThreadLinux &thread);
157 void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index);
159 void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread,
162 Error SetupSoftwareSingleStepping(NativeThreadLinux &thread);
165 static ::ProcessMessage::CrashReason
166 GetCrashReasonForSIGSEGV(const siginfo_t *info);
168 static ::ProcessMessage::CrashReason
169 GetCrashReasonForSIGILL(const siginfo_t *info);
171 static ::ProcessMessage::CrashReason
172 GetCrashReasonForSIGFPE(const siginfo_t *info);
174 static ::ProcessMessage::CrashReason
175 GetCrashReasonForSIGBUS(const siginfo_t *info);
178 bool HasThreadNoLock(lldb::tid_t thread_id);
180 bool StopTrackingThread(lldb::tid_t thread_id);
182 NativeThreadLinuxSP AddThread(lldb::tid_t thread_id);
184 Error GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);
186 Error FixupBreakpointPCAsNeeded(NativeThreadLinux &thread);
188 /// Writes a siginfo_t structure corresponding to the given thread ID to the
189 /// memory region pointed to by @p siginfo.
190 Error GetSignalInfo(lldb::tid_t tid, void *siginfo);
192 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
193 /// corresponding to the given thread ID to the memory pointed to by @p
195 Error GetEventMessage(lldb::tid_t tid, unsigned long *message);
197 void NotifyThreadDeath(lldb::tid_t tid);
199 Error Detach(lldb::tid_t tid);
201 // This method is requests a stop on all threads which are still running. It
203 // deferred delegate notification, which will fire once threads report as
205 // triggerring_tid will be set as the current thread (main stop reason).
206 void StopRunningThreads(lldb::tid_t triggering_tid);
208 // Notify the delegate if all threads have stopped.
209 void SignalIfAllThreadsStopped();
211 // Resume the given thread, optionally passing it the given signal. The type
213 // operation (continue, single-step) depends on the state parameter.
214 Error ResumeThread(NativeThreadLinux &thread, lldb::StateType state,
217 void ThreadWasCreated(NativeThreadLinux &thread);
219 void SigchldHandler();
222 } // namespace process_linux
223 } // namespace lldb_private
225 #endif // #ifndef liblldb_NativeProcessLinux_H_