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"
2699e37695SRavitheja Addepally #include "ProcessorTrace.h"
27b9c1b51eSKate Stone #include "lldb/Host/common/NativeProcessProtocol.h"
28af245d11STodd Fiala 
29db264a6dSTamas Berghammer namespace lldb_private {
3097206d57SZachary Turner class Status;
31af245d11STodd Fiala class Scalar;
32af245d11STodd Fiala 
33db264a6dSTamas Berghammer namespace process_linux {
34af245d11STodd Fiala /// @class NativeProcessLinux
35af245d11STodd Fiala /// @brief Manages communication with the inferior (debugee) process.
36af245d11STodd Fiala ///
37af245d11STodd Fiala /// Upon construction, this class prepares and launches an inferior process for
38af245d11STodd Fiala /// debugging.
39af245d11STodd Fiala ///
40af245d11STodd Fiala /// Changes in the inferior process state are broadcasted.
41b9c1b51eSKate Stone class NativeProcessLinux : public NativeProcessProtocol {
42c307c270SSean Callanan public:
4396e600fcSPavel Labath   class Factory : public NativeProcessProtocol::Factory {
4496e600fcSPavel Labath   public:
45*82abefa4SPavel Labath     llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
4696e600fcSPavel Labath     Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
4796e600fcSPavel Labath            MainLoop &mainloop) const override;
4896e600fcSPavel Labath 
49*82abefa4SPavel Labath     llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
5096e600fcSPavel Labath     Attach(lldb::pid_t pid, NativeDelegate &native_delegate,
5196e600fcSPavel Labath            MainLoop &mainloop) const override;
5296e600fcSPavel Labath   };
5396e600fcSPavel Labath 
54af245d11STodd Fiala   // ---------------------------------------------------------------------
55af245d11STodd Fiala   // NativeProcessProtocol Interface
56af245d11STodd Fiala   // ---------------------------------------------------------------------
5797206d57SZachary Turner   Status Resume(const ResumeActionList &resume_actions) override;
58af245d11STodd Fiala 
5997206d57SZachary Turner   Status Halt() override;
60af245d11STodd Fiala 
6197206d57SZachary Turner   Status Detach() override;
62af245d11STodd Fiala 
6397206d57SZachary Turner   Status Signal(int signo) override;
64af245d11STodd Fiala 
6597206d57SZachary Turner   Status Interrupt() override;
66e9547b80SChaoren Lin 
6797206d57SZachary Turner   Status Kill() override;
68af245d11STodd Fiala 
6997206d57SZachary Turner   Status GetMemoryRegionInfo(lldb::addr_t load_addr,
70b9c1b51eSKate Stone                              MemoryRegionInfo &range_info) override;
71af245d11STodd Fiala 
7297206d57SZachary Turner   Status ReadMemory(lldb::addr_t addr, void *buf, size_t size,
73b9c1b51eSKate Stone                     size_t &bytes_read) override;
74af245d11STodd Fiala 
7597206d57SZachary Turner   Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size,
76b9c1b51eSKate Stone                                size_t &bytes_read) override;
77af245d11STodd Fiala 
7897206d57SZachary Turner   Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
79b9c1b51eSKate Stone                      size_t &bytes_written) override;
803eb4b458SChaoren Lin 
8197206d57SZachary Turner   Status AllocateMemory(size_t size, uint32_t permissions,
82b9c1b51eSKate Stone                         lldb::addr_t &addr) override;
83af245d11STodd Fiala 
8497206d57SZachary Turner   Status DeallocateMemory(lldb::addr_t addr) override;
85af245d11STodd Fiala 
86b9c1b51eSKate Stone   lldb::addr_t GetSharedLibraryInfoAddress() override;
87af245d11STodd Fiala 
88b9c1b51eSKate Stone   size_t UpdateThreads() override;
89af245d11STodd Fiala 
90b9c1b51eSKate Stone   bool GetArchitecture(ArchSpec &arch) const override;
91af245d11STodd Fiala 
9297206d57SZachary Turner   Status SetBreakpoint(lldb::addr_t addr, uint32_t size,
9397206d57SZachary Turner                        bool hardware) override;
94af245d11STodd Fiala 
9597206d57SZachary Turner   Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override;
96d5ffbad2SOmair Javaid 
97b9c1b51eSKate Stone   void DoStopIDBumped(uint32_t newBumpId) override;
98af245d11STodd Fiala 
9997206d57SZachary Turner   Status GetLoadedModuleFileSpec(const char *module_path,
100b9c1b51eSKate Stone                                  FileSpec &file_spec) override;
101068f8a7eSTamas Berghammer 
10297206d57SZachary Turner   Status GetFileLoadAddress(const llvm::StringRef &file_name,
103b9c1b51eSKate Stone                             lldb::addr_t &load_addr) override;
104783bfc8cSTamas Berghammer 
105b9c1b51eSKate Stone   NativeThreadLinuxSP GetThreadByID(lldb::tid_t id);
106f9077782SPavel Labath 
107b7f0f45fSPavel Labath   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
108b7f0f45fSPavel Labath   GetAuxvData() const override {
109b7f0f45fSPavel Labath     return getProcFile(GetID(), "auxv");
110b7f0f45fSPavel Labath   }
111b7f0f45fSPavel Labath 
11299e37695SRavitheja Addepally   lldb::user_id_t StartTrace(const TraceOptions &config,
11399e37695SRavitheja Addepally                              Status &error) override;
11499e37695SRavitheja Addepally 
11599e37695SRavitheja Addepally   Status StopTrace(lldb::user_id_t traceid,
11699e37695SRavitheja Addepally                    lldb::tid_t thread) override;
11799e37695SRavitheja Addepally 
11899e37695SRavitheja Addepally   Status GetData(lldb::user_id_t traceid, lldb::tid_t thread,
11999e37695SRavitheja Addepally                  llvm::MutableArrayRef<uint8_t> &buffer,
12099e37695SRavitheja Addepally                  size_t offset = 0) override;
12199e37695SRavitheja Addepally 
12299e37695SRavitheja Addepally   Status GetMetaData(lldb::user_id_t traceid, lldb::tid_t thread,
12399e37695SRavitheja Addepally                      llvm::MutableArrayRef<uint8_t> &buffer,
12499e37695SRavitheja Addepally                      size_t offset = 0) override;
12599e37695SRavitheja Addepally 
12699e37695SRavitheja Addepally   Status GetTraceConfig(lldb::user_id_t traceid, TraceOptions &config) override;
12799e37695SRavitheja Addepally 
128af245d11STodd Fiala   // ---------------------------------------------------------------------
129af245d11STodd Fiala   // Interface used by NativeRegisterContext-derived classes.
130af245d11STodd Fiala   // ---------------------------------------------------------------------
13197206d57SZachary Turner   static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
132b9c1b51eSKate Stone                               void *data = nullptr, size_t data_size = 0,
1334a9babb2SPavel Labath                               long *result = nullptr);
1347cb18bf5STamas Berghammer 
135b9c1b51eSKate Stone   bool SupportHardwareSingleStepping() const;
136605b51b8SPavel Labath 
137af245d11STodd Fiala protected:
138af245d11STodd Fiala   // ---------------------------------------------------------------------
139af245d11STodd Fiala   // NativeProcessProtocol protected interface
140af245d11STodd Fiala   // ---------------------------------------------------------------------
14197206d57SZachary Turner   Status
142b9c1b51eSKate Stone   GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint,
143b9c1b51eSKate Stone                                   size_t &actual_opcode_size,
144b9c1b51eSKate Stone                                   const uint8_t *&trap_opcode_bytes) override;
145af245d11STodd Fiala 
146af245d11STodd Fiala private:
14719cbe96aSPavel Labath   MainLoop::SignalHandleUP m_sigchld_handle;
148db264a6dSTamas Berghammer   ArchSpec m_arch;
149af245d11STodd Fiala 
15096e600fcSPavel Labath   LazyBool m_supports_mem_region = eLazyBoolCalculate;
151a6f5795aSTamas Berghammer   std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache;
152af245d11STodd Fiala 
15396e600fcSPavel Labath   lldb::tid_t m_pending_notification_tid = LLDB_INVALID_THREAD_ID;
1540e1d729bSPavel Labath 
155d8c338d4STamas Berghammer   // List of thread ids stepping with a breakpoint with the address of
156d8c338d4STamas Berghammer   // the relevan breakpoint
157d8c338d4STamas Berghammer   std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
158d8c338d4STamas Berghammer 
159af245d11STodd Fiala   // ---------------------------------------------------------------------
160af245d11STodd Fiala   // Private Instance Methods
161af245d11STodd Fiala   // ---------------------------------------------------------------------
16296e600fcSPavel Labath   NativeProcessLinux(::pid_t pid, int terminal_fd, NativeDelegate &delegate,
163*82abefa4SPavel Labath                      const ArchSpec &arch, MainLoop &mainloop,
164*82abefa4SPavel Labath                      llvm::ArrayRef<::pid_t> tids);
165af245d11STodd Fiala 
16696e600fcSPavel Labath   // Returns a list of process threads that we have attached to.
16796e600fcSPavel Labath   static llvm::Expected<std::vector<::pid_t>> Attach(::pid_t pid);
168af245d11STodd Fiala 
16997206d57SZachary Turner   static Status SetDefaultPtraceOpts(const lldb::pid_t);
170af245d11STodd Fiala 
1713508fc8cSPavel Labath   void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status);
172af245d11STodd Fiala 
173b9c1b51eSKate Stone   void WaitForNewThread(::pid_t tid);
174426bdf88SPavel Labath 
175b9c1b51eSKate Stone   void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread);
176af245d11STodd Fiala 
177b9c1b51eSKate Stone   void MonitorTrace(NativeThreadLinux &thread);
178c16f5dcaSChaoren Lin 
179b9c1b51eSKate Stone   void MonitorBreakpoint(NativeThreadLinux &thread);
180c16f5dcaSChaoren Lin 
181b9c1b51eSKate Stone   void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index);
182c16f5dcaSChaoren Lin 
183b9c1b51eSKate Stone   void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread,
184b9c1b51eSKate Stone                      bool exited);
185af245d11STodd Fiala 
18697206d57SZachary Turner   Status SetupSoftwareSingleStepping(NativeThreadLinux &thread);
187e7708688STamas Berghammer 
188af245d11STodd Fiala #if 0
189af245d11STodd Fiala         static ::ProcessMessage::CrashReason
190af245d11STodd Fiala         GetCrashReasonForSIGSEGV(const siginfo_t *info);
191af245d11STodd Fiala 
192af245d11STodd Fiala         static ::ProcessMessage::CrashReason
193af245d11STodd Fiala         GetCrashReasonForSIGILL(const siginfo_t *info);
194af245d11STodd Fiala 
195af245d11STodd Fiala         static ::ProcessMessage::CrashReason
196af245d11STodd Fiala         GetCrashReasonForSIGFPE(const siginfo_t *info);
197af245d11STodd Fiala 
198af245d11STodd Fiala         static ::ProcessMessage::CrashReason
199af245d11STodd Fiala         GetCrashReasonForSIGBUS(const siginfo_t *info);
200af245d11STodd Fiala #endif
201af245d11STodd Fiala 
202b9c1b51eSKate Stone   bool HasThreadNoLock(lldb::tid_t thread_id);
203af245d11STodd Fiala 
204b9c1b51eSKate Stone   bool StopTrackingThread(lldb::tid_t thread_id);
205af245d11STodd Fiala 
206b9c1b51eSKate Stone   NativeThreadLinuxSP AddThread(lldb::tid_t thread_id);
207af245d11STodd Fiala 
20897206d57SZachary Turner   Status GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);
209af245d11STodd Fiala 
21097206d57SZachary Turner   Status FixupBreakpointPCAsNeeded(NativeThreadLinux &thread);
211af245d11STodd Fiala 
212af245d11STodd Fiala   /// Writes a siginfo_t structure corresponding to the given thread ID to the
213af245d11STodd Fiala   /// memory region pointed to by @p siginfo.
21497206d57SZachary Turner   Status GetSignalInfo(lldb::tid_t tid, void *siginfo);
215af245d11STodd Fiala 
216af245d11STodd Fiala   /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
217af245d11STodd Fiala   /// corresponding to the given thread ID to the memory pointed to by @p
218af245d11STodd Fiala   /// message.
21997206d57SZachary Turner   Status GetEventMessage(lldb::tid_t tid, unsigned long *message);
220af245d11STodd Fiala 
221b9c1b51eSKate Stone   void NotifyThreadDeath(lldb::tid_t tid);
222fa03ad2eSChaoren Lin 
22397206d57SZachary Turner   Status Detach(lldb::tid_t tid);
22486fd8e45SChaoren Lin 
225b9c1b51eSKate Stone   // This method is requests a stop on all threads which are still running. It
226b9c1b51eSKate Stone   // sets up a
227b9c1b51eSKate Stone   // deferred delegate notification, which will fire once threads report as
228b9c1b51eSKate Stone   // stopped. The
2291dbc6c9cSPavel Labath   // triggerring_tid will be set as the current thread (main stop reason).
230b9c1b51eSKate Stone   void StopRunningThreads(lldb::tid_t triggering_tid);
231c076559aSPavel Labath 
2329eb1ecb9SPavel Labath   // Notify the delegate if all threads have stopped.
2339eb1ecb9SPavel Labath   void SignalIfAllThreadsStopped();
234c076559aSPavel Labath 
235b9c1b51eSKate Stone   // Resume the given thread, optionally passing it the given signal. The type
236b9c1b51eSKate Stone   // of resume
2370e1d729bSPavel Labath   // operation (continue, single-step) depends on the state parameter.
23897206d57SZachary Turner   Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state,
239b9c1b51eSKate Stone                       int signo);
240c076559aSPavel Labath 
241b9c1b51eSKate Stone   void ThreadWasCreated(NativeThreadLinux &thread);
242c076559aSPavel Labath 
243b9c1b51eSKate Stone   void SigchldHandler();
244a6f5795aSTamas Berghammer 
24597206d57SZachary Turner   Status PopulateMemoryRegionCache();
24699e37695SRavitheja Addepally 
24799e37695SRavitheja Addepally   lldb::user_id_t StartTraceGroup(const TraceOptions &config,
24899e37695SRavitheja Addepally                                          Status &error);
24999e37695SRavitheja Addepally 
25099e37695SRavitheja Addepally   // This function is intended to be used to stop tracing
25199e37695SRavitheja Addepally   // on a thread that exited.
25299e37695SRavitheja Addepally   Status StopTracingForThread(lldb::tid_t thread);
25399e37695SRavitheja Addepally 
25499e37695SRavitheja Addepally   // The below function as the name suggests, looks up a ProcessorTrace
25599e37695SRavitheja Addepally   // instance from the m_processor_trace_monitor map. In the case of
25699e37695SRavitheja Addepally   // process tracing where the traceid passed would map to the complete
25799e37695SRavitheja Addepally   // process, it is mandatory to provide a threadid to obtain a trace
25899e37695SRavitheja Addepally   // instance (since ProcessorTrace is tied to a thread). In the other
25999e37695SRavitheja Addepally   // scenario that an individual thread is being traced, just the traceid
26099e37695SRavitheja Addepally   // is sufficient to obtain the actual ProcessorTrace instance.
26199e37695SRavitheja Addepally   llvm::Expected<ProcessorTraceMonitor &>
26299e37695SRavitheja Addepally   LookupProcessorTraceInstance(lldb::user_id_t traceid, lldb::tid_t thread);
26399e37695SRavitheja Addepally 
26499e37695SRavitheja Addepally   // Stops tracing on individual threads being traced. Not intended
26599e37695SRavitheja Addepally   // to be used to stop tracing on complete process.
26699e37695SRavitheja Addepally   Status StopProcessorTracingOnThread(lldb::user_id_t traceid,
26799e37695SRavitheja Addepally                                       lldb::tid_t thread);
26899e37695SRavitheja Addepally 
26999e37695SRavitheja Addepally   // Intended to stop tracing on complete process.
27099e37695SRavitheja Addepally   // Should not be used for stopping trace on
27199e37695SRavitheja Addepally   // individual threads.
27299e37695SRavitheja Addepally   void StopProcessorTracingOnProcess();
27399e37695SRavitheja Addepally 
27499e37695SRavitheja Addepally   llvm::DenseMap<lldb::tid_t, ProcessorTraceMonitorUP>
27599e37695SRavitheja Addepally       m_processor_trace_monitor;
27699e37695SRavitheja Addepally 
27799e37695SRavitheja Addepally   // Set for tracking threads being traced under
27899e37695SRavitheja Addepally   // same process user id.
27999e37695SRavitheja Addepally   llvm::DenseSet<lldb::tid_t> m_pt_traced_thread_group;
28099e37695SRavitheja Addepally 
28196e600fcSPavel Labath   lldb::user_id_t m_pt_proces_trace_id = LLDB_INVALID_UID;
28299e37695SRavitheja Addepally   TraceOptions m_pt_process_trace_config;
283af245d11STodd Fiala };
284db264a6dSTamas Berghammer 
285db264a6dSTamas Berghammer } // namespace process_linux
286db264a6dSTamas Berghammer } // namespace lldb_private
287af245d11STodd Fiala 
288af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_
289