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"
19d3173f34SChaoren Lin #include "lldb/Host/FileSpec.h"
2039de3110SZachary Turner #include "lldb/Host/HostThread.h"
21af245d11STodd Fiala #include "lldb/Target/MemoryRegionInfo.h"
22b9c1b51eSKate Stone #include "lldb/lldb-types.h"
23af245d11STodd Fiala 
248c8ff7afSPavel Labath #include "NativeThreadLinux.h"
25b9c1b51eSKate Stone #include "lldb/Host/common/NativeProcessProtocol.h"
26af245d11STodd Fiala 
27db264a6dSTamas Berghammer namespace lldb_private {
28af245d11STodd Fiala class Error;
29af245d11STodd Fiala class Scalar;
30af245d11STodd Fiala 
31db264a6dSTamas Berghammer namespace process_linux {
32af245d11STodd Fiala /// @class NativeProcessLinux
33af245d11STodd Fiala /// @brief Manages communication with the inferior (debugee) process.
34af245d11STodd Fiala ///
35af245d11STodd Fiala /// Upon construction, this class prepares and launches an inferior process for
36af245d11STodd Fiala /// debugging.
37af245d11STodd Fiala ///
38af245d11STodd Fiala /// Changes in the inferior process state are broadcasted.
39b9c1b51eSKate Stone class NativeProcessLinux : public NativeProcessProtocol {
40b9c1b51eSKate Stone   friend Error NativeProcessProtocol::Launch(
41b9c1b51eSKate Stone       ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
42b9c1b51eSKate Stone       MainLoop &mainloop, NativeProcessProtocolSP &process_sp);
43d5b310f2SPavel Labath 
44b9c1b51eSKate Stone   friend Error NativeProcessProtocol::Attach(
45b9c1b51eSKate Stone       lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
46b9c1b51eSKate Stone       MainLoop &mainloop, NativeProcessProtocolSP &process_sp);
47d5b310f2SPavel Labath 
48c307c270SSean Callanan public:
49af245d11STodd Fiala   // ---------------------------------------------------------------------
50af245d11STodd Fiala   // NativeProcessProtocol Interface
51af245d11STodd Fiala   // ---------------------------------------------------------------------
52b9c1b51eSKate Stone   Error Resume(const ResumeActionList &resume_actions) override;
53af245d11STodd Fiala 
54b9c1b51eSKate Stone   Error Halt() override;
55af245d11STodd Fiala 
56b9c1b51eSKate Stone   Error Detach() override;
57af245d11STodd Fiala 
58b9c1b51eSKate Stone   Error Signal(int signo) override;
59af245d11STodd Fiala 
60b9c1b51eSKate Stone   Error Interrupt() override;
61e9547b80SChaoren Lin 
62b9c1b51eSKate Stone   Error Kill() override;
63af245d11STodd Fiala 
64b9c1b51eSKate Stone   Error GetMemoryRegionInfo(lldb::addr_t load_addr,
65b9c1b51eSKate Stone                             MemoryRegionInfo &range_info) override;
66af245d11STodd Fiala 
67b9c1b51eSKate Stone   Error ReadMemory(lldb::addr_t addr, void *buf, size_t size,
68b9c1b51eSKate Stone                    size_t &bytes_read) override;
69af245d11STodd Fiala 
70b9c1b51eSKate Stone   Error ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size,
71b9c1b51eSKate Stone                               size_t &bytes_read) override;
72af245d11STodd Fiala 
73b9c1b51eSKate Stone   Error WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
74b9c1b51eSKate Stone                     size_t &bytes_written) override;
753eb4b458SChaoren Lin 
76b9c1b51eSKate Stone   Error AllocateMemory(size_t size, uint32_t permissions,
77b9c1b51eSKate Stone                        lldb::addr_t &addr) override;
78af245d11STodd Fiala 
79b9c1b51eSKate Stone   Error DeallocateMemory(lldb::addr_t addr) override;
80af245d11STodd Fiala 
81b9c1b51eSKate Stone   lldb::addr_t GetSharedLibraryInfoAddress() override;
82af245d11STodd Fiala 
83b9c1b51eSKate Stone   size_t UpdateThreads() override;
84af245d11STodd Fiala 
85b9c1b51eSKate Stone   bool GetArchitecture(ArchSpec &arch) const override;
86af245d11STodd Fiala 
87b9c1b51eSKate Stone   Error SetBreakpoint(lldb::addr_t addr, uint32_t size, bool hardware) override;
88af245d11STodd Fiala 
89*d5ffbad2SOmair Javaid   Error RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override;
90*d5ffbad2SOmair Javaid 
91b9c1b51eSKate Stone   void DoStopIDBumped(uint32_t newBumpId) override;
92af245d11STodd Fiala 
93b9c1b51eSKate Stone   Error GetLoadedModuleFileSpec(const char *module_path,
94b9c1b51eSKate Stone                                 FileSpec &file_spec) override;
95068f8a7eSTamas Berghammer 
96b9c1b51eSKate Stone   Error GetFileLoadAddress(const llvm::StringRef &file_name,
97b9c1b51eSKate Stone                            lldb::addr_t &load_addr) override;
98783bfc8cSTamas Berghammer 
99b9c1b51eSKate Stone   NativeThreadLinuxSP GetThreadByID(lldb::tid_t id);
100f9077782SPavel Labath 
101af245d11STodd Fiala   // ---------------------------------------------------------------------
102af245d11STodd Fiala   // Interface used by NativeRegisterContext-derived classes.
103af245d11STodd Fiala   // ---------------------------------------------------------------------
104b9c1b51eSKate Stone   static Error PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
105b9c1b51eSKate Stone                              void *data = nullptr, size_t data_size = 0,
1064a9babb2SPavel Labath                              long *result = nullptr);
1077cb18bf5STamas Berghammer 
108b9c1b51eSKate Stone   bool SupportHardwareSingleStepping() const;
109605b51b8SPavel Labath 
110af245d11STodd Fiala protected:
111af245d11STodd Fiala   // ---------------------------------------------------------------------
112af245d11STodd Fiala   // NativeProcessProtocol protected interface
113af245d11STodd Fiala   // ---------------------------------------------------------------------
114af245d11STodd Fiala   Error
115b9c1b51eSKate Stone   GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint,
116b9c1b51eSKate Stone                                   size_t &actual_opcode_size,
117b9c1b51eSKate Stone                                   const uint8_t *&trap_opcode_bytes) override;
118af245d11STodd Fiala 
119af245d11STodd Fiala private:
12019cbe96aSPavel Labath   MainLoop::SignalHandleUP m_sigchld_handle;
121db264a6dSTamas Berghammer   ArchSpec m_arch;
122af245d11STodd Fiala 
123db264a6dSTamas Berghammer   LazyBool m_supports_mem_region;
124a6f5795aSTamas Berghammer   std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache;
125af245d11STodd Fiala 
1260e1d729bSPavel Labath   lldb::tid_t m_pending_notification_tid;
1270e1d729bSPavel Labath 
128d8c338d4STamas Berghammer   // List of thread ids stepping with a breakpoint with the address of
129d8c338d4STamas Berghammer   // the relevan breakpoint
130d8c338d4STamas Berghammer   std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
131d8c338d4STamas Berghammer 
132af245d11STodd Fiala   // ---------------------------------------------------------------------
133af245d11STodd Fiala   // Private Instance Methods
134af245d11STodd Fiala   // ---------------------------------------------------------------------
135af245d11STodd Fiala   NativeProcessLinux();
136af245d11STodd Fiala 
137b9c1b51eSKate Stone   Error LaunchInferior(MainLoop &mainloop, ProcessLaunchInfo &launch_info);
138af245d11STodd Fiala 
139af245d11STodd Fiala   /// Attaches to an existing process.  Forms the
1400cbf0b13STamas Berghammer   /// implementation of Process::DoAttach
141b9c1b51eSKate Stone   void AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, Error &error);
1421107b5a5SPavel Labath 
143b9c1b51eSKate Stone   ::pid_t Attach(lldb::pid_t pid, Error &error);
144af245d11STodd Fiala 
145b9c1b51eSKate Stone   static Error SetDefaultPtraceOpts(const lldb::pid_t);
146af245d11STodd Fiala 
147b9c1b51eSKate Stone   static void *MonitorThread(void *baton);
1481107b5a5SPavel Labath 
149b9c1b51eSKate Stone   void MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
150af245d11STodd Fiala 
151b9c1b51eSKate Stone   void WaitForNewThread(::pid_t tid);
152426bdf88SPavel Labath 
153b9c1b51eSKate Stone   void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread);
154af245d11STodd Fiala 
155b9c1b51eSKate Stone   void MonitorTrace(NativeThreadLinux &thread);
156c16f5dcaSChaoren Lin 
157b9c1b51eSKate Stone   void MonitorBreakpoint(NativeThreadLinux &thread);
158c16f5dcaSChaoren Lin 
159b9c1b51eSKate Stone   void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index);
160c16f5dcaSChaoren Lin 
161b9c1b51eSKate Stone   void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread,
162b9c1b51eSKate Stone                      bool exited);
163af245d11STodd Fiala 
164b9c1b51eSKate Stone   Error SetupSoftwareSingleStepping(NativeThreadLinux &thread);
165e7708688STamas Berghammer 
166af245d11STodd Fiala #if 0
167af245d11STodd Fiala         static ::ProcessMessage::CrashReason
168af245d11STodd Fiala         GetCrashReasonForSIGSEGV(const siginfo_t *info);
169af245d11STodd Fiala 
170af245d11STodd Fiala         static ::ProcessMessage::CrashReason
171af245d11STodd Fiala         GetCrashReasonForSIGILL(const siginfo_t *info);
172af245d11STodd Fiala 
173af245d11STodd Fiala         static ::ProcessMessage::CrashReason
174af245d11STodd Fiala         GetCrashReasonForSIGFPE(const siginfo_t *info);
175af245d11STodd Fiala 
176af245d11STodd Fiala         static ::ProcessMessage::CrashReason
177af245d11STodd Fiala         GetCrashReasonForSIGBUS(const siginfo_t *info);
178af245d11STodd Fiala #endif
179af245d11STodd Fiala 
180b9c1b51eSKate Stone   bool HasThreadNoLock(lldb::tid_t thread_id);
181af245d11STodd Fiala 
182b9c1b51eSKate Stone   bool StopTrackingThread(lldb::tid_t thread_id);
183af245d11STodd Fiala 
184b9c1b51eSKate Stone   NativeThreadLinuxSP AddThread(lldb::tid_t thread_id);
185af245d11STodd Fiala 
186b9c1b51eSKate Stone   Error GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);
187af245d11STodd Fiala 
188b9c1b51eSKate Stone   Error FixupBreakpointPCAsNeeded(NativeThreadLinux &thread);
189af245d11STodd Fiala 
190af245d11STodd Fiala   /// Writes a siginfo_t structure corresponding to the given thread ID to the
191af245d11STodd Fiala   /// memory region pointed to by @p siginfo.
192b9c1b51eSKate Stone   Error GetSignalInfo(lldb::tid_t tid, void *siginfo);
193af245d11STodd Fiala 
194af245d11STodd Fiala   /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
195af245d11STodd Fiala   /// corresponding to the given thread ID to the memory pointed to by @p
196af245d11STodd Fiala   /// message.
197b9c1b51eSKate Stone   Error GetEventMessage(lldb::tid_t tid, unsigned long *message);
198af245d11STodd Fiala 
199b9c1b51eSKate Stone   void NotifyThreadDeath(lldb::tid_t tid);
200fa03ad2eSChaoren Lin 
201b9c1b51eSKate Stone   Error Detach(lldb::tid_t tid);
20286fd8e45SChaoren Lin 
203b9c1b51eSKate Stone   // This method is requests a stop on all threads which are still running. It
204b9c1b51eSKate Stone   // sets up a
205b9c1b51eSKate Stone   // deferred delegate notification, which will fire once threads report as
206b9c1b51eSKate Stone   // stopped. The
2071dbc6c9cSPavel Labath   // triggerring_tid will be set as the current thread (main stop reason).
208b9c1b51eSKate Stone   void StopRunningThreads(lldb::tid_t triggering_tid);
209c076559aSPavel Labath 
2109eb1ecb9SPavel Labath   // Notify the delegate if all threads have stopped.
2119eb1ecb9SPavel Labath   void SignalIfAllThreadsStopped();
212c076559aSPavel Labath 
213b9c1b51eSKate Stone   // Resume the given thread, optionally passing it the given signal. The type
214b9c1b51eSKate Stone   // of resume
2150e1d729bSPavel Labath   // operation (continue, single-step) depends on the state parameter.
216b9c1b51eSKate Stone   Error ResumeThread(NativeThreadLinux &thread, lldb::StateType state,
217b9c1b51eSKate Stone                      int signo);
218c076559aSPavel Labath 
219b9c1b51eSKate Stone   void ThreadWasCreated(NativeThreadLinux &thread);
220c076559aSPavel Labath 
221b9c1b51eSKate Stone   void SigchldHandler();
222a6f5795aSTamas Berghammer 
223a6f5795aSTamas Berghammer   Error PopulateMemoryRegionCache();
224af245d11STodd Fiala };
225db264a6dSTamas Berghammer 
226db264a6dSTamas Berghammer } // namespace process_linux
227db264a6dSTamas Berghammer } // namespace lldb_private
228af245d11STodd Fiala 
229af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_
230