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 230b697561SWalter Erquinigo #include "IntelPTManager.h" 248c8ff7afSPavel Labath #include "NativeThreadLinux.h" 25f4335b8eSAntonio Afonso #include "Plugins/Process/POSIX/NativeProcessELF.h" 268244fc50SMichał Górny #include "Plugins/Process/Utility/NativeProcessSoftwareSingleStep.h" 27af245d11STodd Fiala 28db264a6dSTamas Berghammer namespace lldb_private { 2997206d57SZachary Turner class Status; 30af245d11STodd Fiala class Scalar; 31af245d11STodd Fiala 32db264a6dSTamas Berghammer namespace process_linux { 33f05b42e9SAdrian Prantl /// \class NativeProcessLinux 34d8f460e8SAdrian Prantl /// Manages communication with the inferior (debugee) process. 35af245d11STodd Fiala /// 36d8f460e8SAdrian Prantl /// Upon construction, this class prepares and launches an inferior process 37d8f460e8SAdrian Prantl /// for debugging. 38af245d11STodd Fiala /// 39af245d11STodd Fiala /// Changes in the inferior process state are broadcasted. 408244fc50SMichał Górny class NativeProcessLinux : public NativeProcessELF, 418244fc50SMichał Górny private NativeProcessSoftwareSingleStep { 42c307c270SSean Callanan public: 4396e600fcSPavel Labath class Factory : public NativeProcessProtocol::Factory { 4496e600fcSPavel Labath public: 4582abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 4696e600fcSPavel Labath Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate, 4796e600fcSPavel Labath MainLoop &mainloop) const override; 4896e600fcSPavel Labath 4982abefa4SPavel 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 // NativeProcessProtocol Interface 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 762c4226f8SPavel Labath llvm::Expected<lldb::addr_t> AllocateMemory(size_t size, 772c4226f8SPavel Labath uint32_t permissions) override; 78af245d11STodd Fiala 792c4226f8SPavel Labath llvm::Error DeallocateMemory(lldb::addr_t addr) override; 80af245d11STodd Fiala 81b9c1b51eSKate Stone size_t UpdateThreads() override; 82af245d11STodd Fiala 83578a4258SPavel Labath const ArchSpec &GetArchitecture() const override { return m_arch; } 84af245d11STodd Fiala 8597206d57SZachary Turner Status SetBreakpoint(lldb::addr_t addr, uint32_t size, 8697206d57SZachary Turner bool hardware) override; 87af245d11STodd Fiala 8897206d57SZachary Turner Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override; 89d5ffbad2SOmair Javaid 90b9c1b51eSKate Stone void DoStopIDBumped(uint32_t newBumpId) override; 91af245d11STodd Fiala 9297206d57SZachary Turner Status GetLoadedModuleFileSpec(const char *module_path, 93b9c1b51eSKate Stone FileSpec &file_spec) override; 94068f8a7eSTamas Berghammer 9597206d57SZachary Turner Status GetFileLoadAddress(const llvm::StringRef &file_name, 96b9c1b51eSKate Stone lldb::addr_t &load_addr) override; 97783bfc8cSTamas Berghammer 98a5be48b3SPavel Labath NativeThreadLinux *GetThreadByID(lldb::tid_t id); 992c4226f8SPavel Labath NativeThreadLinux *GetCurrentThread(); 100f9077782SPavel Labath 101b7f0f45fSPavel Labath llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 102b7f0f45fSPavel Labath GetAuxvData() const override { 103b7f0f45fSPavel Labath return getProcFile(GetID(), "auxv"); 104b7f0f45fSPavel Labath } 105b7f0f45fSPavel Labath 1060b697561SWalter Erquinigo /// Tracing 1070b697561SWalter Erquinigo /// These methods implement the jLLDBTrace packets 1080b697561SWalter Erquinigo /// \{ 1090b697561SWalter Erquinigo llvm::Error TraceStart(llvm::StringRef json_request, 1100b697561SWalter Erquinigo llvm::StringRef type) override; 11199e37695SRavitheja Addepally 1120b697561SWalter Erquinigo llvm::Error TraceStop(const TraceStopRequest &request) override; 11399e37695SRavitheja Addepally 1140b697561SWalter Erquinigo llvm::Expected<llvm::json::Value> 1150b697561SWalter Erquinigo TraceGetState(llvm::StringRef type) override; 11699e37695SRavitheja Addepally 1170b697561SWalter Erquinigo llvm::Expected<std::vector<uint8_t>> 1180b697561SWalter Erquinigo TraceGetBinaryData(const TraceGetBinaryDataRequest &request) override; 11999e37695SRavitheja Addepally 1200b697561SWalter Erquinigo llvm::Expected<TraceSupportedResponse> TraceSupported() override; 1210b697561SWalter Erquinigo /// } 12221555fffSWalter Erquinigo 123af245d11STodd Fiala // Interface used by NativeRegisterContext-derived classes. 12497206d57SZachary Turner static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr, 125b9c1b51eSKate Stone void *data = nullptr, size_t data_size = 0, 1264a9babb2SPavel Labath long *result = nullptr); 1277cb18bf5STamas Berghammer 128b9c1b51eSKate Stone bool SupportHardwareSingleStepping() const; 129605b51b8SPavel Labath 130af245d11STodd Fiala protected: 131f8b825f6SPavel Labath llvm::Expected<llvm::ArrayRef<uint8_t>> 132f8b825f6SPavel Labath GetSoftwareBreakpointTrapOpcode(size_t size_hint) override; 133af245d11STodd Fiala 1342c4226f8SPavel Labath llvm::Expected<uint64_t> Syscall(llvm::ArrayRef<uint64_t> args); 1352c4226f8SPavel Labath 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 1452c4226f8SPavel Labath /// Inferior memory (allocated by us) and its size. 1462c4226f8SPavel Labath llvm::DenseMap<lldb::addr_t, lldb::addr_t> m_allocated_memory; 1472c4226f8SPavel Labath 148af245d11STodd Fiala // Private Instance Methods 14996e600fcSPavel Labath NativeProcessLinux(::pid_t pid, int terminal_fd, NativeDelegate &delegate, 15082abefa4SPavel Labath const ArchSpec &arch, MainLoop &mainloop, 15182abefa4SPavel Labath llvm::ArrayRef<::pid_t> tids); 152af245d11STodd Fiala 15396e600fcSPavel Labath // Returns a list of process threads that we have attached to. 15496e600fcSPavel Labath static llvm::Expected<std::vector<::pid_t>> Attach(::pid_t pid); 155af245d11STodd Fiala 15697206d57SZachary Turner static Status SetDefaultPtraceOpts(const lldb::pid_t); 157af245d11STodd Fiala 1583508fc8cSPavel Labath void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status); 159af245d11STodd Fiala 160*c8d18cbaSMichał Górny void WaitForCloneNotification(::pid_t pid); 161426bdf88SPavel Labath 162b9c1b51eSKate Stone void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread); 163af245d11STodd Fiala 164b9c1b51eSKate Stone void MonitorTrace(NativeThreadLinux &thread); 165c16f5dcaSChaoren Lin 166b9c1b51eSKate Stone void MonitorBreakpoint(NativeThreadLinux &thread); 167c16f5dcaSChaoren Lin 168b9c1b51eSKate Stone void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index); 169c16f5dcaSChaoren Lin 170b9c1b51eSKate Stone void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, 171b9c1b51eSKate Stone bool exited); 172af245d11STodd Fiala 173b9c1b51eSKate Stone bool HasThreadNoLock(lldb::tid_t thread_id); 174af245d11STodd Fiala 175b9c1b51eSKate Stone bool StopTrackingThread(lldb::tid_t thread_id); 176af245d11STodd Fiala 1770b697561SWalter Erquinigo /// Create a new thread. 1780b697561SWalter Erquinigo /// 1790b697561SWalter Erquinigo /// If process tracing is enabled and the thread can't be traced, then the 1800b697561SWalter Erquinigo /// thread is left stopped with a \a eStopReasonProcessorTrace status, and 1810b697561SWalter Erquinigo /// then the process is stopped. 1820b697561SWalter Erquinigo /// 1830b697561SWalter Erquinigo /// \param[in] resume 1840b697561SWalter Erquinigo /// If a tracing error didn't happen, then resume the thread after 1850b697561SWalter Erquinigo /// creation if \b true, or leave it stopped with SIGSTOP if \b false. 1860b697561SWalter Erquinigo NativeThreadLinux &AddThread(lldb::tid_t thread_id, bool resume); 1870b697561SWalter Erquinigo 1880b697561SWalter Erquinigo /// Start tracing a new thread if process tracing is enabled. 1890b697561SWalter Erquinigo /// 1900b697561SWalter Erquinigo /// Trace mechanisms should modify this method to provide automatic tracing 1910b697561SWalter Erquinigo /// for new threads. 1920b697561SWalter Erquinigo Status NotifyTracersOfNewThread(lldb::tid_t tid); 1930b697561SWalter Erquinigo 1940b697561SWalter Erquinigo /// Stop tracing threads upon a destroy event. 1950b697561SWalter Erquinigo /// 1960b697561SWalter Erquinigo /// Trace mechanisms should modify this method to provide automatic trace 1970b697561SWalter Erquinigo /// stopping for threads being destroyed. 1980b697561SWalter Erquinigo Status NotifyTracersOfThreadDestroyed(lldb::tid_t tid); 199af245d11STodd Fiala 200af245d11STodd Fiala /// Writes a siginfo_t structure corresponding to the given thread ID to the 201f05b42e9SAdrian Prantl /// memory region pointed to by \p siginfo. 20297206d57SZachary Turner Status GetSignalInfo(lldb::tid_t tid, void *siginfo); 203af245d11STodd Fiala 204af245d11STodd Fiala /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 205af245d11STodd Fiala /// corresponding to the given thread ID to the memory pointed to by @p 206af245d11STodd Fiala /// message. 20797206d57SZachary Turner Status GetEventMessage(lldb::tid_t tid, unsigned long *message); 208af245d11STodd Fiala 209b9c1b51eSKate Stone void NotifyThreadDeath(lldb::tid_t tid); 210fa03ad2eSChaoren Lin 21197206d57SZachary Turner Status Detach(lldb::tid_t tid); 21286fd8e45SChaoren Lin 213b9c1b51eSKate Stone // This method is requests a stop on all threads which are still running. It 214b9c1b51eSKate Stone // sets up a 215b9c1b51eSKate Stone // deferred delegate notification, which will fire once threads report as 216b9c1b51eSKate Stone // stopped. The 2171dbc6c9cSPavel Labath // triggerring_tid will be set as the current thread (main stop reason). 218b9c1b51eSKate Stone void StopRunningThreads(lldb::tid_t triggering_tid); 219c076559aSPavel Labath 2209eb1ecb9SPavel Labath // Notify the delegate if all threads have stopped. 2219eb1ecb9SPavel Labath void SignalIfAllThreadsStopped(); 222c076559aSPavel Labath 223b9c1b51eSKate Stone // Resume the given thread, optionally passing it the given signal. The type 224b9c1b51eSKate Stone // of resume 2250e1d729bSPavel Labath // operation (continue, single-step) depends on the state parameter. 22697206d57SZachary Turner Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state, 227b9c1b51eSKate Stone int signo); 228c076559aSPavel Labath 229b9c1b51eSKate Stone void ThreadWasCreated(NativeThreadLinux &thread); 230c076559aSPavel Labath 231b9c1b51eSKate Stone void SigchldHandler(); 232a6f5795aSTamas Berghammer 23397206d57SZachary Turner Status PopulateMemoryRegionCache(); 23499e37695SRavitheja Addepally 2350b697561SWalter Erquinigo /// Manages Intel PT process and thread traces. 2360b697561SWalter Erquinigo IntelPTManager m_intel_pt_manager; 237*c8d18cbaSMichał Górny 238*c8d18cbaSMichał Górny struct CloneInfo { 239*c8d18cbaSMichał Górny int event; 240*c8d18cbaSMichał Górny lldb::tid_t parent_tid; 241*c8d18cbaSMichał Górny }; 242*c8d18cbaSMichał Górny 243*c8d18cbaSMichał Górny // Map of child processes that have been signaled once, and we are 244*c8d18cbaSMichał Górny // waiting for the second signal. 245*c8d18cbaSMichał Górny llvm::DenseMap<lldb::pid_t, llvm::Optional<CloneInfo>> m_pending_pid_map; 246*c8d18cbaSMichał Górny 247*c8d18cbaSMichał Górny // Handle a clone()-like event. If received by parent, clone_info contains 248*c8d18cbaSMichał Górny // additional info. Returns true if the event is handled, or false if it 249*c8d18cbaSMichał Górny // is pending second notification. 250*c8d18cbaSMichał Górny bool MonitorClone(lldb::pid_t child_pid, 251*c8d18cbaSMichał Górny llvm::Optional<CloneInfo> clone_info); 252af245d11STodd Fiala }; 253db264a6dSTamas Berghammer 254db264a6dSTamas Berghammer } // namespace process_linux 255db264a6dSTamas Berghammer } // namespace lldb_private 256af245d11STodd Fiala 257af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_ 258