1af245d11STodd Fiala //===-- NativeProcessLinux.h ---------------------------------- -*- C++ -*-===// 2af245d11STodd Fiala // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6af245d11STodd Fiala // 7af245d11STodd Fiala //===----------------------------------------------------------------------===// 8af245d11STodd Fiala 9af245d11STodd Fiala #ifndef liblldb_NativeProcessLinux_H_ 10af245d11STodd Fiala #define liblldb_NativeProcessLinux_H_ 11af245d11STodd Fiala 12b6dbe9a9SPavel Labath #include <csignal> 13af245d11STodd Fiala #include <unordered_set> 14af245d11STodd Fiala 15af245d11STodd Fiala #include "lldb/Host/Debug.h" 1639de3110SZachary Turner #include "lldb/Host/HostThread.h" 17b7f0f45fSPavel Labath #include "lldb/Host/linux/Support.h" 18af245d11STodd Fiala #include "lldb/Target/MemoryRegionInfo.h" 195f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h" 205713a05bSZachary Turner #include "lldb/Utility/FileSpec.h" 21b9c1b51eSKate Stone #include "lldb/lldb-types.h" 22af245d11STodd Fiala 238c8ff7afSPavel Labath #include "NativeThreadLinux.h" 2499e37695SRavitheja Addepally #include "ProcessorTrace.h" 25b9c1b51eSKate Stone #include "lldb/Host/common/NativeProcessProtocol.h" 26af245d11STodd Fiala 27db264a6dSTamas Berghammer namespace lldb_private { 2897206d57SZachary Turner class Status; 29af245d11STodd Fiala class Scalar; 30af245d11STodd Fiala 31db264a6dSTamas Berghammer namespace process_linux { 32*f05b42e9SAdrian Prantl /// \class NativeProcessLinux 33d8f460e8SAdrian Prantl /// Manages communication with the inferior (debugee) process. 34af245d11STodd Fiala /// 35d8f460e8SAdrian Prantl /// Upon construction, this class prepares and launches an inferior process 36d8f460e8SAdrian Prantl /// for debugging. 37af245d11STodd Fiala /// 38af245d11STodd Fiala /// Changes in the inferior process state are broadcasted. 39b9c1b51eSKate Stone class NativeProcessLinux : public NativeProcessProtocol { 40c307c270SSean Callanan public: 4196e600fcSPavel Labath class Factory : public NativeProcessProtocol::Factory { 4296e600fcSPavel Labath public: 4382abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 4496e600fcSPavel Labath Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate, 4596e600fcSPavel Labath MainLoop &mainloop) const override; 4696e600fcSPavel Labath 4782abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 4896e600fcSPavel Labath Attach(lldb::pid_t pid, NativeDelegate &native_delegate, 4996e600fcSPavel Labath MainLoop &mainloop) const override; 5096e600fcSPavel Labath }; 5196e600fcSPavel Labath 52af245d11STodd Fiala // --------------------------------------------------------------------- 53af245d11STodd Fiala // NativeProcessProtocol Interface 54af245d11STodd Fiala // --------------------------------------------------------------------- 5597206d57SZachary Turner Status Resume(const ResumeActionList &resume_actions) override; 56af245d11STodd Fiala 5797206d57SZachary Turner Status Halt() override; 58af245d11STodd Fiala 5997206d57SZachary Turner Status Detach() override; 60af245d11STodd Fiala 6197206d57SZachary Turner Status Signal(int signo) override; 62af245d11STodd Fiala 6397206d57SZachary Turner Status Interrupt() override; 64e9547b80SChaoren Lin 6597206d57SZachary Turner Status Kill() override; 66af245d11STodd Fiala 6797206d57SZachary Turner Status GetMemoryRegionInfo(lldb::addr_t load_addr, 68b9c1b51eSKate Stone MemoryRegionInfo &range_info) override; 69af245d11STodd Fiala 7097206d57SZachary Turner Status ReadMemory(lldb::addr_t addr, void *buf, size_t size, 71b9c1b51eSKate Stone size_t &bytes_read) override; 72af245d11STodd Fiala 7397206d57SZachary Turner Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, 74b9c1b51eSKate Stone size_t &bytes_written) override; 753eb4b458SChaoren Lin 7697206d57SZachary Turner Status AllocateMemory(size_t size, uint32_t permissions, 77b9c1b51eSKate Stone lldb::addr_t &addr) override; 78af245d11STodd Fiala 7997206d57SZachary Turner Status 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 85578a4258SPavel Labath const ArchSpec &GetArchitecture() const override { return m_arch; } 86af245d11STodd Fiala 8797206d57SZachary Turner Status SetBreakpoint(lldb::addr_t addr, uint32_t size, 8897206d57SZachary Turner bool hardware) override; 89af245d11STodd Fiala 9097206d57SZachary Turner Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override; 91d5ffbad2SOmair Javaid 92b9c1b51eSKate Stone void DoStopIDBumped(uint32_t newBumpId) override; 93af245d11STodd Fiala 9497206d57SZachary Turner Status GetLoadedModuleFileSpec(const char *module_path, 95b9c1b51eSKate Stone FileSpec &file_spec) override; 96068f8a7eSTamas Berghammer 9797206d57SZachary Turner Status GetFileLoadAddress(const llvm::StringRef &file_name, 98b9c1b51eSKate Stone lldb::addr_t &load_addr) override; 99783bfc8cSTamas Berghammer 100a5be48b3SPavel Labath NativeThreadLinux *GetThreadByID(lldb::tid_t id); 101f9077782SPavel Labath 102b7f0f45fSPavel Labath llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 103b7f0f45fSPavel Labath GetAuxvData() const override { 104b7f0f45fSPavel Labath return getProcFile(GetID(), "auxv"); 105b7f0f45fSPavel Labath } 106b7f0f45fSPavel Labath 10799e37695SRavitheja Addepally lldb::user_id_t StartTrace(const TraceOptions &config, 10899e37695SRavitheja Addepally Status &error) override; 10999e37695SRavitheja Addepally 11099e37695SRavitheja Addepally Status StopTrace(lldb::user_id_t traceid, 11199e37695SRavitheja Addepally lldb::tid_t thread) override; 11299e37695SRavitheja Addepally 11399e37695SRavitheja Addepally Status GetData(lldb::user_id_t traceid, lldb::tid_t thread, 11499e37695SRavitheja Addepally llvm::MutableArrayRef<uint8_t> &buffer, 11599e37695SRavitheja Addepally size_t offset = 0) override; 11699e37695SRavitheja Addepally 11799e37695SRavitheja Addepally Status GetMetaData(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 GetTraceConfig(lldb::user_id_t traceid, TraceOptions &config) override; 12299e37695SRavitheja Addepally 123af245d11STodd Fiala // --------------------------------------------------------------------- 124af245d11STodd Fiala // Interface used by NativeRegisterContext-derived classes. 125af245d11STodd Fiala // --------------------------------------------------------------------- 12697206d57SZachary Turner static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr, 127b9c1b51eSKate Stone void *data = nullptr, size_t data_size = 0, 1284a9babb2SPavel Labath long *result = nullptr); 1297cb18bf5STamas Berghammer 130b9c1b51eSKate Stone bool SupportHardwareSingleStepping() const; 131605b51b8SPavel Labath 132af245d11STodd Fiala protected: 133f8b825f6SPavel Labath llvm::Expected<llvm::ArrayRef<uint8_t>> 134f8b825f6SPavel Labath GetSoftwareBreakpointTrapOpcode(size_t size_hint) override; 135af245d11STodd Fiala 136af245d11STodd Fiala private: 13719cbe96aSPavel Labath MainLoop::SignalHandleUP m_sigchld_handle; 138db264a6dSTamas Berghammer ArchSpec m_arch; 139af245d11STodd Fiala 14096e600fcSPavel Labath LazyBool m_supports_mem_region = eLazyBoolCalculate; 141a6f5795aSTamas Berghammer std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache; 142af245d11STodd Fiala 14396e600fcSPavel Labath lldb::tid_t m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 1440e1d729bSPavel Labath 145d8c338d4STamas Berghammer // List of thread ids stepping with a breakpoint with the address of 146d8c338d4STamas Berghammer // the relevan breakpoint 147d8c338d4STamas Berghammer std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint; 148d8c338d4STamas Berghammer 149af245d11STodd Fiala // --------------------------------------------------------------------- 150af245d11STodd Fiala // Private Instance Methods 151af245d11STodd Fiala // --------------------------------------------------------------------- 15296e600fcSPavel Labath NativeProcessLinux(::pid_t pid, int terminal_fd, NativeDelegate &delegate, 15382abefa4SPavel Labath const ArchSpec &arch, MainLoop &mainloop, 15482abefa4SPavel Labath llvm::ArrayRef<::pid_t> tids); 155af245d11STodd Fiala 15696e600fcSPavel Labath // Returns a list of process threads that we have attached to. 15796e600fcSPavel Labath static llvm::Expected<std::vector<::pid_t>> Attach(::pid_t pid); 158af245d11STodd Fiala 15997206d57SZachary Turner static Status SetDefaultPtraceOpts(const lldb::pid_t); 160af245d11STodd Fiala 1613508fc8cSPavel Labath void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status); 162af245d11STodd Fiala 163b9c1b51eSKate Stone void WaitForNewThread(::pid_t tid); 164426bdf88SPavel Labath 165b9c1b51eSKate Stone void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread); 166af245d11STodd Fiala 167b9c1b51eSKate Stone void MonitorTrace(NativeThreadLinux &thread); 168c16f5dcaSChaoren Lin 169b9c1b51eSKate Stone void MonitorBreakpoint(NativeThreadLinux &thread); 170c16f5dcaSChaoren Lin 171b9c1b51eSKate Stone void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index); 172c16f5dcaSChaoren Lin 173b9c1b51eSKate Stone void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, 174b9c1b51eSKate Stone bool exited); 175af245d11STodd Fiala 17697206d57SZachary Turner Status SetupSoftwareSingleStepping(NativeThreadLinux &thread); 177e7708688STamas Berghammer 178b9c1b51eSKate Stone bool HasThreadNoLock(lldb::tid_t thread_id); 179af245d11STodd Fiala 180b9c1b51eSKate Stone bool StopTrackingThread(lldb::tid_t thread_id); 181af245d11STodd Fiala 182a5be48b3SPavel Labath NativeThreadLinux &AddThread(lldb::tid_t thread_id); 183af245d11STodd Fiala 184af245d11STodd Fiala /// Writes a siginfo_t structure corresponding to the given thread ID to the 185*f05b42e9SAdrian Prantl /// memory region pointed to by \p siginfo. 18697206d57SZachary Turner Status GetSignalInfo(lldb::tid_t tid, void *siginfo); 187af245d11STodd Fiala 188af245d11STodd Fiala /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 189af245d11STodd Fiala /// corresponding to the given thread ID to the memory pointed to by @p 190af245d11STodd Fiala /// message. 19197206d57SZachary Turner Status GetEventMessage(lldb::tid_t tid, unsigned long *message); 192af245d11STodd Fiala 193b9c1b51eSKate Stone void NotifyThreadDeath(lldb::tid_t tid); 194fa03ad2eSChaoren Lin 19597206d57SZachary Turner Status Detach(lldb::tid_t tid); 19686fd8e45SChaoren Lin 197b9c1b51eSKate Stone // This method is requests a stop on all threads which are still running. It 198b9c1b51eSKate Stone // sets up a 199b9c1b51eSKate Stone // deferred delegate notification, which will fire once threads report as 200b9c1b51eSKate Stone // stopped. The 2011dbc6c9cSPavel Labath // triggerring_tid will be set as the current thread (main stop reason). 202b9c1b51eSKate Stone void StopRunningThreads(lldb::tid_t triggering_tid); 203c076559aSPavel Labath 2049eb1ecb9SPavel Labath // Notify the delegate if all threads have stopped. 2059eb1ecb9SPavel Labath void SignalIfAllThreadsStopped(); 206c076559aSPavel Labath 207b9c1b51eSKate Stone // Resume the given thread, optionally passing it the given signal. The type 208b9c1b51eSKate Stone // of resume 2090e1d729bSPavel Labath // operation (continue, single-step) depends on the state parameter. 21097206d57SZachary Turner Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state, 211b9c1b51eSKate Stone int signo); 212c076559aSPavel Labath 213b9c1b51eSKate Stone void ThreadWasCreated(NativeThreadLinux &thread); 214c076559aSPavel Labath 215b9c1b51eSKate Stone void SigchldHandler(); 216a6f5795aSTamas Berghammer 21797206d57SZachary Turner Status PopulateMemoryRegionCache(); 21899e37695SRavitheja Addepally 21999e37695SRavitheja Addepally lldb::user_id_t StartTraceGroup(const TraceOptions &config, 22099e37695SRavitheja Addepally Status &error); 22199e37695SRavitheja Addepally 22299e37695SRavitheja Addepally // This function is intended to be used to stop tracing 22399e37695SRavitheja Addepally // on a thread that exited. 22499e37695SRavitheja Addepally Status StopTracingForThread(lldb::tid_t thread); 22599e37695SRavitheja Addepally 22699e37695SRavitheja Addepally // The below function as the name suggests, looks up a ProcessorTrace 22799e37695SRavitheja Addepally // instance from the m_processor_trace_monitor map. In the case of 22899e37695SRavitheja Addepally // process tracing where the traceid passed would map to the complete 22999e37695SRavitheja Addepally // process, it is mandatory to provide a threadid to obtain a trace 23099e37695SRavitheja Addepally // instance (since ProcessorTrace is tied to a thread). In the other 23199e37695SRavitheja Addepally // scenario that an individual thread is being traced, just the traceid 23299e37695SRavitheja Addepally // is sufficient to obtain the actual ProcessorTrace instance. 23399e37695SRavitheja Addepally llvm::Expected<ProcessorTraceMonitor &> 23499e37695SRavitheja Addepally LookupProcessorTraceInstance(lldb::user_id_t traceid, lldb::tid_t thread); 23599e37695SRavitheja Addepally 23699e37695SRavitheja Addepally // Stops tracing on individual threads being traced. Not intended 23799e37695SRavitheja Addepally // to be used to stop tracing on complete process. 23899e37695SRavitheja Addepally Status StopProcessorTracingOnThread(lldb::user_id_t traceid, 23999e37695SRavitheja Addepally lldb::tid_t thread); 24099e37695SRavitheja Addepally 24199e37695SRavitheja Addepally // Intended to stop tracing on complete process. 24299e37695SRavitheja Addepally // Should not be used for stopping trace on 24399e37695SRavitheja Addepally // individual threads. 24499e37695SRavitheja Addepally void StopProcessorTracingOnProcess(); 24599e37695SRavitheja Addepally 24699e37695SRavitheja Addepally llvm::DenseMap<lldb::tid_t, ProcessorTraceMonitorUP> 24799e37695SRavitheja Addepally m_processor_trace_monitor; 24899e37695SRavitheja Addepally 24999e37695SRavitheja Addepally // Set for tracking threads being traced under 25099e37695SRavitheja Addepally // same process user id. 25199e37695SRavitheja Addepally llvm::DenseSet<lldb::tid_t> m_pt_traced_thread_group; 25299e37695SRavitheja Addepally 25396e600fcSPavel Labath lldb::user_id_t m_pt_proces_trace_id = LLDB_INVALID_UID; 25499e37695SRavitheja Addepally TraceOptions m_pt_process_trace_config; 255af245d11STodd Fiala }; 256db264a6dSTamas Berghammer 257db264a6dSTamas Berghammer } // namespace process_linux 258db264a6dSTamas Berghammer } // namespace lldb_private 259af245d11STodd Fiala 260af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_ 261