1af245d11STodd Fiala //===-- NativeProcessLinux.h ---------------------------------- -*- C++ -*-===//
2af245d11STodd Fiala //
3af245d11STodd Fiala //                     The LLVM Compiler Infrastructure
4af245d11STodd Fiala //
5af245d11STodd Fiala // This file is distributed under the University of Illinois Open Source
6af245d11STodd Fiala // License. See LICENSE.TXT for details.
7af245d11STodd Fiala //
8af245d11STodd Fiala //===----------------------------------------------------------------------===//
9af245d11STodd Fiala 
10af245d11STodd Fiala #ifndef liblldb_NativeProcessLinux_H_
11af245d11STodd Fiala #define liblldb_NativeProcessLinux_H_
12af245d11STodd Fiala 
13af245d11STodd Fiala // C++ Includes
14af245d11STodd Fiala #include <unordered_set>
15af245d11STodd Fiala 
16af245d11STodd Fiala // Other libraries and framework includes
17af245d11STodd Fiala #include "lldb/Core/ArchSpec.h"
18af245d11STodd Fiala #include "lldb/Host/Debug.h"
1939de3110SZachary Turner #include "lldb/Host/HostThread.h"
20b7f0f45fSPavel Labath #include "lldb/Host/linux/Support.h"
21af245d11STodd Fiala #include "lldb/Target/MemoryRegionInfo.h"
225713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
23b9c1b51eSKate Stone #include "lldb/lldb-types.h"
24af245d11STodd Fiala 
258c8ff7afSPavel Labath #include "NativeThreadLinux.h"
26b9c1b51eSKate Stone #include "lldb/Host/common/NativeProcessProtocol.h"
27af245d11STodd Fiala 
28db264a6dSTamas Berghammer namespace lldb_private {
2997206d57SZachary Turner class Status;
30af245d11STodd Fiala class Scalar;
31af245d11STodd Fiala 
32db264a6dSTamas Berghammer namespace process_linux {
33af245d11STodd Fiala /// @class NativeProcessLinux
34af245d11STodd Fiala /// @brief Manages communication with the inferior (debugee) process.
35af245d11STodd Fiala ///
36af245d11STodd Fiala /// Upon construction, this class prepares and launches an inferior process for
37af245d11STodd Fiala /// debugging.
38af245d11STodd Fiala ///
39af245d11STodd Fiala /// Changes in the inferior process state are broadcasted.
40b9c1b51eSKate Stone class NativeProcessLinux : public NativeProcessProtocol {
4197206d57SZachary Turner   friend Status NativeProcessProtocol::Launch(
42b9c1b51eSKate Stone       ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
43b9c1b51eSKate Stone       MainLoop &mainloop, NativeProcessProtocolSP &process_sp);
44d5b310f2SPavel Labath 
4597206d57SZachary Turner   friend Status NativeProcessProtocol::Attach(
46b9c1b51eSKate Stone       lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
47b9c1b51eSKate Stone       MainLoop &mainloop, NativeProcessProtocolSP &process_sp);
48d5b310f2SPavel Labath 
49c307c270SSean Callanan public:
50af245d11STodd Fiala   // ---------------------------------------------------------------------
51af245d11STodd Fiala   // NativeProcessProtocol Interface
52af245d11STodd Fiala   // ---------------------------------------------------------------------
5397206d57SZachary Turner   Status Resume(const ResumeActionList &resume_actions) override;
54af245d11STodd Fiala 
5597206d57SZachary Turner   Status Halt() override;
56af245d11STodd Fiala 
5797206d57SZachary Turner   Status Detach() override;
58af245d11STodd Fiala 
5997206d57SZachary Turner   Status Signal(int signo) override;
60af245d11STodd Fiala 
6197206d57SZachary Turner   Status Interrupt() override;
62e9547b80SChaoren Lin 
6397206d57SZachary Turner   Status Kill() override;
64af245d11STodd Fiala 
6597206d57SZachary Turner   Status GetMemoryRegionInfo(lldb::addr_t load_addr,
66b9c1b51eSKate Stone                              MemoryRegionInfo &range_info) override;
67af245d11STodd Fiala 
6897206d57SZachary Turner   Status ReadMemory(lldb::addr_t addr, void *buf, size_t size,
69b9c1b51eSKate Stone                     size_t &bytes_read) override;
70af245d11STodd Fiala 
7197206d57SZachary Turner   Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size,
72b9c1b51eSKate Stone                                size_t &bytes_read) override;
73af245d11STodd Fiala 
7497206d57SZachary Turner   Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
75b9c1b51eSKate Stone                      size_t &bytes_written) override;
763eb4b458SChaoren Lin 
7797206d57SZachary Turner   Status AllocateMemory(size_t size, uint32_t permissions,
78b9c1b51eSKate Stone                         lldb::addr_t &addr) override;
79af245d11STodd Fiala 
8097206d57SZachary Turner   Status DeallocateMemory(lldb::addr_t addr) override;
81af245d11STodd Fiala 
82b9c1b51eSKate Stone   lldb::addr_t GetSharedLibraryInfoAddress() override;
83af245d11STodd Fiala 
84b9c1b51eSKate Stone   size_t UpdateThreads() override;
85af245d11STodd Fiala 
86b9c1b51eSKate Stone   bool GetArchitecture(ArchSpec &arch) const override;
87af245d11STodd Fiala 
8897206d57SZachary Turner   Status SetBreakpoint(lldb::addr_t addr, uint32_t size,
8997206d57SZachary Turner                        bool hardware) override;
90af245d11STodd Fiala 
9197206d57SZachary Turner   Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override;
92d5ffbad2SOmair Javaid 
93b9c1b51eSKate Stone   void DoStopIDBumped(uint32_t newBumpId) override;
94af245d11STodd Fiala 
9597206d57SZachary Turner   Status GetLoadedModuleFileSpec(const char *module_path,
96b9c1b51eSKate Stone                                  FileSpec &file_spec) override;
97068f8a7eSTamas Berghammer 
9897206d57SZachary Turner   Status GetFileLoadAddress(const llvm::StringRef &file_name,
99b9c1b51eSKate Stone                             lldb::addr_t &load_addr) override;
100783bfc8cSTamas Berghammer 
101b9c1b51eSKate Stone   NativeThreadLinuxSP GetThreadByID(lldb::tid_t id);
102f9077782SPavel Labath 
103b7f0f45fSPavel Labath   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
104b7f0f45fSPavel Labath   GetAuxvData() const override {
105b7f0f45fSPavel Labath     return getProcFile(GetID(), "auxv");
106b7f0f45fSPavel Labath   }
107b7f0f45fSPavel Labath 
108af245d11STodd Fiala   // ---------------------------------------------------------------------
109af245d11STodd Fiala   // Interface used by NativeRegisterContext-derived classes.
110af245d11STodd Fiala   // ---------------------------------------------------------------------
11197206d57SZachary Turner   static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
112b9c1b51eSKate Stone                               void *data = nullptr, size_t data_size = 0,
1134a9babb2SPavel Labath                               long *result = nullptr);
1147cb18bf5STamas Berghammer 
115b9c1b51eSKate Stone   bool SupportHardwareSingleStepping() const;
116605b51b8SPavel Labath 
117af245d11STodd Fiala protected:
118af245d11STodd Fiala   // ---------------------------------------------------------------------
119af245d11STodd Fiala   // NativeProcessProtocol protected interface
120af245d11STodd Fiala   // ---------------------------------------------------------------------
12197206d57SZachary Turner   Status
122b9c1b51eSKate Stone   GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint,
123b9c1b51eSKate Stone                                   size_t &actual_opcode_size,
124b9c1b51eSKate Stone                                   const uint8_t *&trap_opcode_bytes) override;
125af245d11STodd Fiala 
126af245d11STodd Fiala private:
12719cbe96aSPavel Labath   MainLoop::SignalHandleUP m_sigchld_handle;
128db264a6dSTamas Berghammer   ArchSpec m_arch;
129af245d11STodd Fiala 
130db264a6dSTamas Berghammer   LazyBool m_supports_mem_region;
131a6f5795aSTamas Berghammer   std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache;
132af245d11STodd Fiala 
1330e1d729bSPavel Labath   lldb::tid_t m_pending_notification_tid;
1340e1d729bSPavel Labath 
135d8c338d4STamas Berghammer   // List of thread ids stepping with a breakpoint with the address of
136d8c338d4STamas Berghammer   // the relevan breakpoint
137d8c338d4STamas Berghammer   std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
138d8c338d4STamas Berghammer 
139af245d11STodd Fiala   // ---------------------------------------------------------------------
140af245d11STodd Fiala   // Private Instance Methods
141af245d11STodd Fiala   // ---------------------------------------------------------------------
142af245d11STodd Fiala   NativeProcessLinux();
143af245d11STodd Fiala 
14497206d57SZachary Turner   Status LaunchInferior(MainLoop &mainloop, ProcessLaunchInfo &launch_info);
145af245d11STodd Fiala 
146af245d11STodd Fiala   /// Attaches to an existing process.  Forms the
1470cbf0b13STamas Berghammer   /// implementation of Process::DoAttach
14897206d57SZachary Turner   void AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, Status &error);
1491107b5a5SPavel Labath 
15097206d57SZachary Turner   ::pid_t Attach(lldb::pid_t pid, Status &error);
151af245d11STodd Fiala 
15297206d57SZachary Turner   static Status SetDefaultPtraceOpts(const lldb::pid_t);
153af245d11STodd Fiala 
154b9c1b51eSKate Stone   static void *MonitorThread(void *baton);
1551107b5a5SPavel Labath 
156*3508fc8cSPavel Labath   void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status);
157af245d11STodd Fiala 
158b9c1b51eSKate Stone   void WaitForNewThread(::pid_t tid);
159426bdf88SPavel Labath 
160b9c1b51eSKate Stone   void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread);
161af245d11STodd Fiala 
162b9c1b51eSKate Stone   void MonitorTrace(NativeThreadLinux &thread);
163c16f5dcaSChaoren Lin 
164b9c1b51eSKate Stone   void MonitorBreakpoint(NativeThreadLinux &thread);
165c16f5dcaSChaoren Lin 
166b9c1b51eSKate Stone   void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index);
167c16f5dcaSChaoren Lin 
168b9c1b51eSKate Stone   void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread,
169b9c1b51eSKate Stone                      bool exited);
170af245d11STodd Fiala 
17197206d57SZachary Turner   Status SetupSoftwareSingleStepping(NativeThreadLinux &thread);
172e7708688STamas Berghammer 
173af245d11STodd Fiala #if 0
174af245d11STodd Fiala         static ::ProcessMessage::CrashReason
175af245d11STodd Fiala         GetCrashReasonForSIGSEGV(const siginfo_t *info);
176af245d11STodd Fiala 
177af245d11STodd Fiala         static ::ProcessMessage::CrashReason
178af245d11STodd Fiala         GetCrashReasonForSIGILL(const siginfo_t *info);
179af245d11STodd Fiala 
180af245d11STodd Fiala         static ::ProcessMessage::CrashReason
181af245d11STodd Fiala         GetCrashReasonForSIGFPE(const siginfo_t *info);
182af245d11STodd Fiala 
183af245d11STodd Fiala         static ::ProcessMessage::CrashReason
184af245d11STodd Fiala         GetCrashReasonForSIGBUS(const siginfo_t *info);
185af245d11STodd Fiala #endif
186af245d11STodd Fiala 
187b9c1b51eSKate Stone   bool HasThreadNoLock(lldb::tid_t thread_id);
188af245d11STodd Fiala 
189b9c1b51eSKate Stone   bool StopTrackingThread(lldb::tid_t thread_id);
190af245d11STodd Fiala 
191b9c1b51eSKate Stone   NativeThreadLinuxSP AddThread(lldb::tid_t thread_id);
192af245d11STodd Fiala 
19397206d57SZachary Turner   Status GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);
194af245d11STodd Fiala 
19597206d57SZachary Turner   Status FixupBreakpointPCAsNeeded(NativeThreadLinux &thread);
196af245d11STodd Fiala 
197af245d11STodd Fiala   /// Writes a siginfo_t structure corresponding to the given thread ID to the
198af245d11STodd Fiala   /// memory region pointed to by @p siginfo.
19997206d57SZachary Turner   Status GetSignalInfo(lldb::tid_t tid, void *siginfo);
200af245d11STodd Fiala 
201af245d11STodd Fiala   /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
202af245d11STodd Fiala   /// corresponding to the given thread ID to the memory pointed to by @p
203af245d11STodd Fiala   /// message.
20497206d57SZachary Turner   Status GetEventMessage(lldb::tid_t tid, unsigned long *message);
205af245d11STodd Fiala 
206b9c1b51eSKate Stone   void NotifyThreadDeath(lldb::tid_t tid);
207fa03ad2eSChaoren Lin 
20897206d57SZachary Turner   Status Detach(lldb::tid_t tid);
20986fd8e45SChaoren Lin 
210b9c1b51eSKate Stone   // This method is requests a stop on all threads which are still running. It
211b9c1b51eSKate Stone   // sets up a
212b9c1b51eSKate Stone   // deferred delegate notification, which will fire once threads report as
213b9c1b51eSKate Stone   // stopped. The
2141dbc6c9cSPavel Labath   // triggerring_tid will be set as the current thread (main stop reason).
215b9c1b51eSKate Stone   void StopRunningThreads(lldb::tid_t triggering_tid);
216c076559aSPavel Labath 
2179eb1ecb9SPavel Labath   // Notify the delegate if all threads have stopped.
2189eb1ecb9SPavel Labath   void SignalIfAllThreadsStopped();
219c076559aSPavel Labath 
220b9c1b51eSKate Stone   // Resume the given thread, optionally passing it the given signal. The type
221b9c1b51eSKate Stone   // of resume
2220e1d729bSPavel Labath   // operation (continue, single-step) depends on the state parameter.
22397206d57SZachary Turner   Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state,
224b9c1b51eSKate Stone                       int signo);
225c076559aSPavel Labath 
226b9c1b51eSKate Stone   void ThreadWasCreated(NativeThreadLinux &thread);
227c076559aSPavel Labath 
228b9c1b51eSKate Stone   void SigchldHandler();
229a6f5795aSTamas Berghammer 
23097206d57SZachary Turner   Status PopulateMemoryRegionCache();
231af245d11STodd Fiala };
232db264a6dSTamas Berghammer 
233db264a6dSTamas Berghammer } // namespace process_linux
234db264a6dSTamas Berghammer } // namespace lldb_private
235af245d11STodd Fiala 
236af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_
237