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 13b6dbe9a9SPavel Labath #include <csignal> 14af245d11STodd Fiala #include <unordered_set> 15af245d11STodd Fiala 16af245d11STodd Fiala #include "lldb/Host/Debug.h" 1739de3110SZachary Turner #include "lldb/Host/HostThread.h" 18b7f0f45fSPavel Labath #include "lldb/Host/linux/Support.h" 19af245d11STodd Fiala #include "lldb/Target/MemoryRegionInfo.h" 205f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h" 215713a05bSZachary Turner #include "lldb/Utility/FileSpec.h" 22b9c1b51eSKate Stone #include "lldb/lldb-types.h" 23af245d11STodd Fiala 248c8ff7afSPavel Labath #include "NativeThreadLinux.h" 2599e37695SRavitheja Addepally #include "ProcessorTrace.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 34*d8f460e8SAdrian Prantl /// Manages communication with the inferior (debugee) process. 35af245d11STodd Fiala /// 36*d8f460e8SAdrian Prantl /// Upon construction, this class prepares and launches an inferior process 37*d8f460e8SAdrian Prantl /// for debugging. 38af245d11STodd Fiala /// 39af245d11STodd Fiala /// Changes in the inferior process state are broadcasted. 40b9c1b51eSKate Stone class NativeProcessLinux : public NativeProcessProtocol { 41c307c270SSean Callanan public: 4296e600fcSPavel Labath class Factory : public NativeProcessProtocol::Factory { 4396e600fcSPavel Labath public: 4482abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 4596e600fcSPavel Labath Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate, 4696e600fcSPavel Labath MainLoop &mainloop) const override; 4796e600fcSPavel Labath 4882abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 4996e600fcSPavel Labath Attach(lldb::pid_t pid, NativeDelegate &native_delegate, 5096e600fcSPavel Labath MainLoop &mainloop) const override; 5196e600fcSPavel Labath }; 5296e600fcSPavel Labath 53af245d11STodd Fiala // --------------------------------------------------------------------- 54af245d11STodd Fiala // NativeProcessProtocol Interface 55af245d11STodd Fiala // --------------------------------------------------------------------- 5697206d57SZachary Turner Status Resume(const ResumeActionList &resume_actions) override; 57af245d11STodd Fiala 5897206d57SZachary Turner Status Halt() override; 59af245d11STodd Fiala 6097206d57SZachary Turner Status Detach() override; 61af245d11STodd Fiala 6297206d57SZachary Turner Status Signal(int signo) override; 63af245d11STodd Fiala 6497206d57SZachary Turner Status Interrupt() override; 65e9547b80SChaoren Lin 6697206d57SZachary Turner Status Kill() override; 67af245d11STodd Fiala 6897206d57SZachary Turner Status GetMemoryRegionInfo(lldb::addr_t load_addr, 69b9c1b51eSKate Stone MemoryRegionInfo &range_info) override; 70af245d11STodd Fiala 7197206d57SZachary Turner Status ReadMemory(lldb::addr_t addr, void *buf, size_t size, 72b9c1b51eSKate Stone size_t &bytes_read) override; 73af245d11STodd Fiala 7497206d57SZachary Turner Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, 75b9c1b51eSKate Stone size_t &bytes_read) override; 76af245d11STodd Fiala 7797206d57SZachary Turner Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, 78b9c1b51eSKate Stone size_t &bytes_written) override; 793eb4b458SChaoren Lin 8097206d57SZachary Turner Status AllocateMemory(size_t size, uint32_t permissions, 81b9c1b51eSKate Stone lldb::addr_t &addr) override; 82af245d11STodd Fiala 8397206d57SZachary Turner Status DeallocateMemory(lldb::addr_t addr) override; 84af245d11STodd Fiala 85b9c1b51eSKate Stone lldb::addr_t GetSharedLibraryInfoAddress() override; 86af245d11STodd Fiala 87b9c1b51eSKate Stone size_t UpdateThreads() override; 88af245d11STodd Fiala 89578a4258SPavel Labath const ArchSpec &GetArchitecture() const override { return m_arch; } 90af245d11STodd Fiala 9197206d57SZachary Turner Status SetBreakpoint(lldb::addr_t addr, uint32_t size, 9297206d57SZachary Turner bool hardware) override; 93af245d11STodd Fiala 9497206d57SZachary Turner Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override; 95d5ffbad2SOmair Javaid 96b9c1b51eSKate Stone void DoStopIDBumped(uint32_t newBumpId) override; 97af245d11STodd Fiala 9897206d57SZachary Turner Status GetLoadedModuleFileSpec(const char *module_path, 99b9c1b51eSKate Stone FileSpec &file_spec) override; 100068f8a7eSTamas Berghammer 10197206d57SZachary Turner Status GetFileLoadAddress(const llvm::StringRef &file_name, 102b9c1b51eSKate Stone lldb::addr_t &load_addr) override; 103783bfc8cSTamas Berghammer 104a5be48b3SPavel Labath NativeThreadLinux *GetThreadByID(lldb::tid_t id); 105f9077782SPavel Labath 106b7f0f45fSPavel Labath llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 107b7f0f45fSPavel Labath GetAuxvData() const override { 108b7f0f45fSPavel Labath return getProcFile(GetID(), "auxv"); 109b7f0f45fSPavel Labath } 110b7f0f45fSPavel Labath 11199e37695SRavitheja Addepally lldb::user_id_t StartTrace(const TraceOptions &config, 11299e37695SRavitheja Addepally Status &error) override; 11399e37695SRavitheja Addepally 11499e37695SRavitheja Addepally Status StopTrace(lldb::user_id_t traceid, 11599e37695SRavitheja Addepally lldb::tid_t thread) override; 11699e37695SRavitheja Addepally 11799e37695SRavitheja Addepally Status GetData(lldb::user_id_t traceid, lldb::tid_t thread, 11899e37695SRavitheja Addepally llvm::MutableArrayRef<uint8_t> &buffer, 11999e37695SRavitheja Addepally size_t offset = 0) override; 12099e37695SRavitheja Addepally 12199e37695SRavitheja Addepally Status GetMetaData(lldb::user_id_t traceid, lldb::tid_t thread, 12299e37695SRavitheja Addepally llvm::MutableArrayRef<uint8_t> &buffer, 12399e37695SRavitheja Addepally size_t offset = 0) override; 12499e37695SRavitheja Addepally 12599e37695SRavitheja Addepally Status GetTraceConfig(lldb::user_id_t traceid, TraceOptions &config) override; 12699e37695SRavitheja Addepally 127af245d11STodd Fiala // --------------------------------------------------------------------- 128af245d11STodd Fiala // Interface used by NativeRegisterContext-derived classes. 129af245d11STodd Fiala // --------------------------------------------------------------------- 13097206d57SZachary Turner static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr, 131b9c1b51eSKate Stone void *data = nullptr, size_t data_size = 0, 1324a9babb2SPavel Labath long *result = nullptr); 1337cb18bf5STamas Berghammer 134b9c1b51eSKate Stone bool SupportHardwareSingleStepping() const; 135605b51b8SPavel Labath 136af245d11STodd Fiala protected: 137af245d11STodd Fiala // --------------------------------------------------------------------- 138af245d11STodd Fiala // NativeProcessProtocol protected interface 139af245d11STodd Fiala // --------------------------------------------------------------------- 14097206d57SZachary Turner Status 141b9c1b51eSKate Stone GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint, 142b9c1b51eSKate Stone size_t &actual_opcode_size, 143b9c1b51eSKate Stone const uint8_t *&trap_opcode_bytes) override; 144af245d11STodd Fiala 145af245d11STodd Fiala private: 14619cbe96aSPavel Labath MainLoop::SignalHandleUP m_sigchld_handle; 147db264a6dSTamas Berghammer ArchSpec m_arch; 148af245d11STodd Fiala 14996e600fcSPavel Labath LazyBool m_supports_mem_region = eLazyBoolCalculate; 150a6f5795aSTamas Berghammer std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache; 151af245d11STodd Fiala 15296e600fcSPavel Labath lldb::tid_t m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 1530e1d729bSPavel Labath 154d8c338d4STamas Berghammer // List of thread ids stepping with a breakpoint with the address of 155d8c338d4STamas Berghammer // the relevan breakpoint 156d8c338d4STamas Berghammer std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint; 157d8c338d4STamas Berghammer 158af245d11STodd Fiala // --------------------------------------------------------------------- 159af245d11STodd Fiala // Private Instance Methods 160af245d11STodd Fiala // --------------------------------------------------------------------- 16196e600fcSPavel Labath NativeProcessLinux(::pid_t pid, int terminal_fd, NativeDelegate &delegate, 16282abefa4SPavel Labath const ArchSpec &arch, MainLoop &mainloop, 16382abefa4SPavel Labath llvm::ArrayRef<::pid_t> tids); 164af245d11STodd Fiala 16596e600fcSPavel Labath // Returns a list of process threads that we have attached to. 16696e600fcSPavel 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 1703508fc8cSPavel Labath void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status); 171af245d11STodd Fiala 172b9c1b51eSKate Stone void WaitForNewThread(::pid_t tid); 173426bdf88SPavel Labath 174b9c1b51eSKate Stone void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread); 175af245d11STodd Fiala 176b9c1b51eSKate Stone void MonitorTrace(NativeThreadLinux &thread); 177c16f5dcaSChaoren Lin 178b9c1b51eSKate Stone void MonitorBreakpoint(NativeThreadLinux &thread); 179c16f5dcaSChaoren Lin 180b9c1b51eSKate Stone void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index); 181c16f5dcaSChaoren Lin 182b9c1b51eSKate Stone void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, 183b9c1b51eSKate Stone bool exited); 184af245d11STodd Fiala 18597206d57SZachary Turner Status SetupSoftwareSingleStepping(NativeThreadLinux &thread); 186e7708688STamas Berghammer 187af245d11STodd Fiala #if 0 188af245d11STodd Fiala static ::ProcessMessage::CrashReason 189af245d11STodd Fiala GetCrashReasonForSIGSEGV(const siginfo_t *info); 190af245d11STodd Fiala 191af245d11STodd Fiala static ::ProcessMessage::CrashReason 192af245d11STodd Fiala GetCrashReasonForSIGILL(const siginfo_t *info); 193af245d11STodd Fiala 194af245d11STodd Fiala static ::ProcessMessage::CrashReason 195af245d11STodd Fiala GetCrashReasonForSIGFPE(const siginfo_t *info); 196af245d11STodd Fiala 197af245d11STodd Fiala static ::ProcessMessage::CrashReason 198af245d11STodd Fiala GetCrashReasonForSIGBUS(const siginfo_t *info); 199af245d11STodd Fiala #endif 200af245d11STodd Fiala 201b9c1b51eSKate Stone bool HasThreadNoLock(lldb::tid_t thread_id); 202af245d11STodd Fiala 203b9c1b51eSKate Stone bool StopTrackingThread(lldb::tid_t thread_id); 204af245d11STodd Fiala 205a5be48b3SPavel Labath NativeThreadLinux &AddThread(lldb::tid_t thread_id); 206af245d11STodd Fiala 20797206d57SZachary Turner Status GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size); 208af245d11STodd Fiala 20997206d57SZachary Turner Status FixupBreakpointPCAsNeeded(NativeThreadLinux &thread); 210af245d11STodd Fiala 211af245d11STodd Fiala /// Writes a siginfo_t structure corresponding to the given thread ID to the 212af245d11STodd Fiala /// memory region pointed to by @p siginfo. 21397206d57SZachary Turner Status GetSignalInfo(lldb::tid_t tid, void *siginfo); 214af245d11STodd Fiala 215af245d11STodd Fiala /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 216af245d11STodd Fiala /// corresponding to the given thread ID to the memory pointed to by @p 217af245d11STodd Fiala /// message. 21897206d57SZachary Turner Status GetEventMessage(lldb::tid_t tid, unsigned long *message); 219af245d11STodd Fiala 220b9c1b51eSKate Stone void NotifyThreadDeath(lldb::tid_t tid); 221fa03ad2eSChaoren Lin 22297206d57SZachary Turner Status Detach(lldb::tid_t tid); 22386fd8e45SChaoren Lin 224b9c1b51eSKate Stone // This method is requests a stop on all threads which are still running. It 225b9c1b51eSKate Stone // sets up a 226b9c1b51eSKate Stone // deferred delegate notification, which will fire once threads report as 227b9c1b51eSKate Stone // stopped. The 2281dbc6c9cSPavel Labath // triggerring_tid will be set as the current thread (main stop reason). 229b9c1b51eSKate Stone void StopRunningThreads(lldb::tid_t triggering_tid); 230c076559aSPavel Labath 2319eb1ecb9SPavel Labath // Notify the delegate if all threads have stopped. 2329eb1ecb9SPavel Labath void SignalIfAllThreadsStopped(); 233c076559aSPavel Labath 234b9c1b51eSKate Stone // Resume the given thread, optionally passing it the given signal. The type 235b9c1b51eSKate Stone // of resume 2360e1d729bSPavel Labath // operation (continue, single-step) depends on the state parameter. 23797206d57SZachary Turner Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state, 238b9c1b51eSKate Stone int signo); 239c076559aSPavel Labath 240b9c1b51eSKate Stone void ThreadWasCreated(NativeThreadLinux &thread); 241c076559aSPavel Labath 242b9c1b51eSKate Stone void SigchldHandler(); 243a6f5795aSTamas Berghammer 24497206d57SZachary Turner Status PopulateMemoryRegionCache(); 24599e37695SRavitheja Addepally 24699e37695SRavitheja Addepally lldb::user_id_t StartTraceGroup(const TraceOptions &config, 24799e37695SRavitheja Addepally Status &error); 24899e37695SRavitheja Addepally 24999e37695SRavitheja Addepally // This function is intended to be used to stop tracing 25099e37695SRavitheja Addepally // on a thread that exited. 25199e37695SRavitheja Addepally Status StopTracingForThread(lldb::tid_t thread); 25299e37695SRavitheja Addepally 25399e37695SRavitheja Addepally // The below function as the name suggests, looks up a ProcessorTrace 25499e37695SRavitheja Addepally // instance from the m_processor_trace_monitor map. In the case of 25599e37695SRavitheja Addepally // process tracing where the traceid passed would map to the complete 25699e37695SRavitheja Addepally // process, it is mandatory to provide a threadid to obtain a trace 25799e37695SRavitheja Addepally // instance (since ProcessorTrace is tied to a thread). In the other 25899e37695SRavitheja Addepally // scenario that an individual thread is being traced, just the traceid 25999e37695SRavitheja Addepally // is sufficient to obtain the actual ProcessorTrace instance. 26099e37695SRavitheja Addepally llvm::Expected<ProcessorTraceMonitor &> 26199e37695SRavitheja Addepally LookupProcessorTraceInstance(lldb::user_id_t traceid, lldb::tid_t thread); 26299e37695SRavitheja Addepally 26399e37695SRavitheja Addepally // Stops tracing on individual threads being traced. Not intended 26499e37695SRavitheja Addepally // to be used to stop tracing on complete process. 26599e37695SRavitheja Addepally Status StopProcessorTracingOnThread(lldb::user_id_t traceid, 26699e37695SRavitheja Addepally lldb::tid_t thread); 26799e37695SRavitheja Addepally 26899e37695SRavitheja Addepally // Intended to stop tracing on complete process. 26999e37695SRavitheja Addepally // Should not be used for stopping trace on 27099e37695SRavitheja Addepally // individual threads. 27199e37695SRavitheja Addepally void StopProcessorTracingOnProcess(); 27299e37695SRavitheja Addepally 27399e37695SRavitheja Addepally llvm::DenseMap<lldb::tid_t, ProcessorTraceMonitorUP> 27499e37695SRavitheja Addepally m_processor_trace_monitor; 27599e37695SRavitheja Addepally 27699e37695SRavitheja Addepally // Set for tracking threads being traced under 27799e37695SRavitheja Addepally // same process user id. 27899e37695SRavitheja Addepally llvm::DenseSet<lldb::tid_t> m_pt_traced_thread_group; 27999e37695SRavitheja Addepally 28096e600fcSPavel Labath lldb::user_id_t m_pt_proces_trace_id = LLDB_INVALID_UID; 28199e37695SRavitheja Addepally TraceOptions m_pt_process_trace_config; 282af245d11STodd Fiala }; 283db264a6dSTamas Berghammer 284db264a6dSTamas Berghammer } // namespace process_linux 285db264a6dSTamas Berghammer } // namespace lldb_private 286af245d11STodd Fiala 287af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_ 288