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: 43*96e600fcSPavel Labath class Factory : public NativeProcessProtocol::Factory { 44*96e600fcSPavel Labath public: 45*96e600fcSPavel Labath llvm::Expected<NativeProcessProtocolSP> 46*96e600fcSPavel Labath Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate, 47*96e600fcSPavel Labath MainLoop &mainloop) const override; 48*96e600fcSPavel Labath 49*96e600fcSPavel Labath llvm::Expected<NativeProcessProtocolSP> 50*96e600fcSPavel Labath Attach(lldb::pid_t pid, NativeDelegate &native_delegate, 51*96e600fcSPavel Labath MainLoop &mainloop) const override; 52*96e600fcSPavel Labath }; 53*96e600fcSPavel 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 150*96e600fcSPavel Labath LazyBool m_supports_mem_region = eLazyBoolCalculate; 151a6f5795aSTamas Berghammer std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache; 152af245d11STodd Fiala 153*96e600fcSPavel 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 // --------------------------------------------------------------------- 162*96e600fcSPavel Labath NativeProcessLinux(::pid_t pid, int terminal_fd, NativeDelegate &delegate, 163*96e600fcSPavel Labath const ArchSpec &arch, MainLoop &mainloop); 164af245d11STodd Fiala 165*96e600fcSPavel Labath // Returns a list of process threads that we have attached to. 166*96e600fcSPavel Labath static llvm::Expected<std::vector<::pid_t>> Attach(::pid_t pid); 167af245d11STodd Fiala 16897206d57SZachary Turner static Status SetDefaultPtraceOpts(const lldb::pid_t); 169af245d11STodd Fiala 170*96e600fcSPavel Labath void InitializeThreads(llvm::ArrayRef<::pid_t> tids); 1711107b5a5SPavel Labath 1723508fc8cSPavel Labath void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status); 173af245d11STodd Fiala 174b9c1b51eSKate Stone void WaitForNewThread(::pid_t tid); 175426bdf88SPavel Labath 176b9c1b51eSKate Stone void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread); 177af245d11STodd Fiala 178b9c1b51eSKate Stone void MonitorTrace(NativeThreadLinux &thread); 179c16f5dcaSChaoren Lin 180b9c1b51eSKate Stone void MonitorBreakpoint(NativeThreadLinux &thread); 181c16f5dcaSChaoren Lin 182b9c1b51eSKate Stone void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index); 183c16f5dcaSChaoren Lin 184b9c1b51eSKate Stone void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, 185b9c1b51eSKate Stone bool exited); 186af245d11STodd Fiala 18797206d57SZachary Turner Status SetupSoftwareSingleStepping(NativeThreadLinux &thread); 188e7708688STamas Berghammer 189af245d11STodd Fiala #if 0 190af245d11STodd Fiala static ::ProcessMessage::CrashReason 191af245d11STodd Fiala GetCrashReasonForSIGSEGV(const siginfo_t *info); 192af245d11STodd Fiala 193af245d11STodd Fiala static ::ProcessMessage::CrashReason 194af245d11STodd Fiala GetCrashReasonForSIGILL(const siginfo_t *info); 195af245d11STodd Fiala 196af245d11STodd Fiala static ::ProcessMessage::CrashReason 197af245d11STodd Fiala GetCrashReasonForSIGFPE(const siginfo_t *info); 198af245d11STodd Fiala 199af245d11STodd Fiala static ::ProcessMessage::CrashReason 200af245d11STodd Fiala GetCrashReasonForSIGBUS(const siginfo_t *info); 201af245d11STodd Fiala #endif 202af245d11STodd Fiala 203b9c1b51eSKate Stone bool HasThreadNoLock(lldb::tid_t thread_id); 204af245d11STodd Fiala 205b9c1b51eSKate Stone bool StopTrackingThread(lldb::tid_t thread_id); 206af245d11STodd Fiala 207b9c1b51eSKate Stone NativeThreadLinuxSP AddThread(lldb::tid_t thread_id); 208af245d11STodd Fiala 20997206d57SZachary Turner Status GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size); 210af245d11STodd Fiala 21197206d57SZachary Turner Status FixupBreakpointPCAsNeeded(NativeThreadLinux &thread); 212af245d11STodd Fiala 213af245d11STodd Fiala /// Writes a siginfo_t structure corresponding to the given thread ID to the 214af245d11STodd Fiala /// memory region pointed to by @p siginfo. 21597206d57SZachary Turner Status GetSignalInfo(lldb::tid_t tid, void *siginfo); 216af245d11STodd Fiala 217af245d11STodd Fiala /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 218af245d11STodd Fiala /// corresponding to the given thread ID to the memory pointed to by @p 219af245d11STodd Fiala /// message. 22097206d57SZachary Turner Status GetEventMessage(lldb::tid_t tid, unsigned long *message); 221af245d11STodd Fiala 222b9c1b51eSKate Stone void NotifyThreadDeath(lldb::tid_t tid); 223fa03ad2eSChaoren Lin 22497206d57SZachary Turner Status Detach(lldb::tid_t tid); 22586fd8e45SChaoren Lin 226b9c1b51eSKate Stone // This method is requests a stop on all threads which are still running. It 227b9c1b51eSKate Stone // sets up a 228b9c1b51eSKate Stone // deferred delegate notification, which will fire once threads report as 229b9c1b51eSKate Stone // stopped. The 2301dbc6c9cSPavel Labath // triggerring_tid will be set as the current thread (main stop reason). 231b9c1b51eSKate Stone void StopRunningThreads(lldb::tid_t triggering_tid); 232c076559aSPavel Labath 2339eb1ecb9SPavel Labath // Notify the delegate if all threads have stopped. 2349eb1ecb9SPavel Labath void SignalIfAllThreadsStopped(); 235c076559aSPavel Labath 236b9c1b51eSKate Stone // Resume the given thread, optionally passing it the given signal. The type 237b9c1b51eSKate Stone // of resume 2380e1d729bSPavel Labath // operation (continue, single-step) depends on the state parameter. 23997206d57SZachary Turner Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state, 240b9c1b51eSKate Stone int signo); 241c076559aSPavel Labath 242b9c1b51eSKate Stone void ThreadWasCreated(NativeThreadLinux &thread); 243c076559aSPavel Labath 244b9c1b51eSKate Stone void SigchldHandler(); 245a6f5795aSTamas Berghammer 24697206d57SZachary Turner Status PopulateMemoryRegionCache(); 24799e37695SRavitheja Addepally 24899e37695SRavitheja Addepally lldb::user_id_t StartTraceGroup(const TraceOptions &config, 24999e37695SRavitheja Addepally Status &error); 25099e37695SRavitheja Addepally 25199e37695SRavitheja Addepally // This function is intended to be used to stop tracing 25299e37695SRavitheja Addepally // on a thread that exited. 25399e37695SRavitheja Addepally Status StopTracingForThread(lldb::tid_t thread); 25499e37695SRavitheja Addepally 25599e37695SRavitheja Addepally // The below function as the name suggests, looks up a ProcessorTrace 25699e37695SRavitheja Addepally // instance from the m_processor_trace_monitor map. In the case of 25799e37695SRavitheja Addepally // process tracing where the traceid passed would map to the complete 25899e37695SRavitheja Addepally // process, it is mandatory to provide a threadid to obtain a trace 25999e37695SRavitheja Addepally // instance (since ProcessorTrace is tied to a thread). In the other 26099e37695SRavitheja Addepally // scenario that an individual thread is being traced, just the traceid 26199e37695SRavitheja Addepally // is sufficient to obtain the actual ProcessorTrace instance. 26299e37695SRavitheja Addepally llvm::Expected<ProcessorTraceMonitor &> 26399e37695SRavitheja Addepally LookupProcessorTraceInstance(lldb::user_id_t traceid, lldb::tid_t thread); 26499e37695SRavitheja Addepally 26599e37695SRavitheja Addepally // Stops tracing on individual threads being traced. Not intended 26699e37695SRavitheja Addepally // to be used to stop tracing on complete process. 26799e37695SRavitheja Addepally Status StopProcessorTracingOnThread(lldb::user_id_t traceid, 26899e37695SRavitheja Addepally lldb::tid_t thread); 26999e37695SRavitheja Addepally 27099e37695SRavitheja Addepally // Intended to stop tracing on complete process. 27199e37695SRavitheja Addepally // Should not be used for stopping trace on 27299e37695SRavitheja Addepally // individual threads. 27399e37695SRavitheja Addepally void StopProcessorTracingOnProcess(); 27499e37695SRavitheja Addepally 27599e37695SRavitheja Addepally llvm::DenseMap<lldb::tid_t, ProcessorTraceMonitorUP> 27699e37695SRavitheja Addepally m_processor_trace_monitor; 27799e37695SRavitheja Addepally 27899e37695SRavitheja Addepally // Set for tracking threads being traced under 27999e37695SRavitheja Addepally // same process user id. 28099e37695SRavitheja Addepally llvm::DenseSet<lldb::tid_t> m_pt_traced_thread_group; 28199e37695SRavitheja Addepally 282*96e600fcSPavel Labath lldb::user_id_t m_pt_proces_trace_id = LLDB_INVALID_UID; 28399e37695SRavitheja Addepally TraceOptions m_pt_process_trace_config; 284af245d11STodd Fiala }; 285db264a6dSTamas Berghammer 286db264a6dSTamas Berghammer } // namespace process_linux 287db264a6dSTamas Berghammer } // namespace lldb_private 288af245d11STodd Fiala 289af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_ 290