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; 52*fd0af0cfSMichał Górny 53*fd0af0cfSMichał Górny Extension GetSupportedExtensions() const override; 5496e600fcSPavel Labath }; 5596e600fcSPavel Labath 56af245d11STodd Fiala // NativeProcessProtocol Interface 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 WriteMemory(lldb::addr_t addr, const void *buf, size_t size, 76b9c1b51eSKate Stone size_t &bytes_written) override; 773eb4b458SChaoren Lin 782c4226f8SPavel Labath llvm::Expected<lldb::addr_t> AllocateMemory(size_t size, 792c4226f8SPavel Labath uint32_t permissions) override; 80af245d11STodd Fiala 812c4226f8SPavel Labath llvm::Error DeallocateMemory(lldb::addr_t addr) 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); 1012c4226f8SPavel Labath NativeThreadLinux *GetCurrentThread(); 102f9077782SPavel Labath 103b7f0f45fSPavel Labath llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 104b7f0f45fSPavel Labath GetAuxvData() const override { 105b7f0f45fSPavel Labath return getProcFile(GetID(), "auxv"); 106b7f0f45fSPavel Labath } 107b7f0f45fSPavel Labath 1080b697561SWalter Erquinigo /// Tracing 1090b697561SWalter Erquinigo /// These methods implement the jLLDBTrace packets 1100b697561SWalter Erquinigo /// \{ 1110b697561SWalter Erquinigo llvm::Error TraceStart(llvm::StringRef json_request, 1120b697561SWalter Erquinigo llvm::StringRef type) override; 11399e37695SRavitheja Addepally 1140b697561SWalter Erquinigo llvm::Error TraceStop(const TraceStopRequest &request) override; 11599e37695SRavitheja Addepally 1160b697561SWalter Erquinigo llvm::Expected<llvm::json::Value> 1170b697561SWalter Erquinigo TraceGetState(llvm::StringRef type) override; 11899e37695SRavitheja Addepally 1190b697561SWalter Erquinigo llvm::Expected<std::vector<uint8_t>> 1200b697561SWalter Erquinigo TraceGetBinaryData(const TraceGetBinaryDataRequest &request) override; 12199e37695SRavitheja Addepally 1220b697561SWalter Erquinigo llvm::Expected<TraceSupportedResponse> TraceSupported() override; 1230b697561SWalter Erquinigo /// } 12421555fffSWalter Erquinigo 125af245d11STodd Fiala // Interface used by NativeRegisterContext-derived classes. 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 1362c4226f8SPavel Labath llvm::Expected<uint64_t> Syscall(llvm::ArrayRef<uint64_t> args); 1372c4226f8SPavel Labath 138af245d11STodd Fiala private: 13919cbe96aSPavel Labath MainLoop::SignalHandleUP m_sigchld_handle; 140db264a6dSTamas Berghammer ArchSpec m_arch; 141*fd0af0cfSMichał Górny MainLoop& m_main_loop; 142af245d11STodd Fiala 14396e600fcSPavel Labath LazyBool m_supports_mem_region = eLazyBoolCalculate; 144a6f5795aSTamas Berghammer std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache; 145af245d11STodd Fiala 14696e600fcSPavel Labath lldb::tid_t m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 1470e1d729bSPavel Labath 1482c4226f8SPavel Labath /// Inferior memory (allocated by us) and its size. 1492c4226f8SPavel Labath llvm::DenseMap<lldb::addr_t, lldb::addr_t> m_allocated_memory; 1502c4226f8SPavel Labath 151af245d11STodd Fiala // Private Instance Methods 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 163c8d18cbaSMichał Górny void WaitForCloneNotification(::pid_t pid); 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 176b9c1b51eSKate Stone bool HasThreadNoLock(lldb::tid_t thread_id); 177af245d11STodd Fiala 178b9c1b51eSKate Stone bool StopTrackingThread(lldb::tid_t thread_id); 179af245d11STodd Fiala 1800b697561SWalter Erquinigo /// Create a new thread. 1810b697561SWalter Erquinigo /// 1820b697561SWalter Erquinigo /// If process tracing is enabled and the thread can't be traced, then the 1830b697561SWalter Erquinigo /// thread is left stopped with a \a eStopReasonProcessorTrace status, and 1840b697561SWalter Erquinigo /// then the process is stopped. 1850b697561SWalter Erquinigo /// 1860b697561SWalter Erquinigo /// \param[in] resume 1870b697561SWalter Erquinigo /// If a tracing error didn't happen, then resume the thread after 1880b697561SWalter Erquinigo /// creation if \b true, or leave it stopped with SIGSTOP if \b false. 1890b697561SWalter Erquinigo NativeThreadLinux &AddThread(lldb::tid_t thread_id, bool resume); 1900b697561SWalter Erquinigo 1910b697561SWalter Erquinigo /// Start tracing a new thread if process tracing is enabled. 1920b697561SWalter Erquinigo /// 1930b697561SWalter Erquinigo /// Trace mechanisms should modify this method to provide automatic tracing 1940b697561SWalter Erquinigo /// for new threads. 1950b697561SWalter Erquinigo Status NotifyTracersOfNewThread(lldb::tid_t tid); 1960b697561SWalter Erquinigo 1970b697561SWalter Erquinigo /// Stop tracing threads upon a destroy event. 1980b697561SWalter Erquinigo /// 1990b697561SWalter Erquinigo /// Trace mechanisms should modify this method to provide automatic trace 2000b697561SWalter Erquinigo /// stopping for threads being destroyed. 2010b697561SWalter Erquinigo Status NotifyTracersOfThreadDestroyed(lldb::tid_t tid); 202af245d11STodd Fiala 203af245d11STodd Fiala /// Writes a siginfo_t structure corresponding to the given thread ID to the 204f05b42e9SAdrian Prantl /// memory region pointed to by \p siginfo. 20597206d57SZachary Turner Status GetSignalInfo(lldb::tid_t tid, void *siginfo); 206af245d11STodd Fiala 207af245d11STodd Fiala /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 208af245d11STodd Fiala /// corresponding to the given thread ID to the memory pointed to by @p 209af245d11STodd Fiala /// message. 21097206d57SZachary Turner Status GetEventMessage(lldb::tid_t tid, unsigned long *message); 211af245d11STodd Fiala 212b9c1b51eSKate Stone void NotifyThreadDeath(lldb::tid_t tid); 213fa03ad2eSChaoren Lin 21497206d57SZachary Turner Status Detach(lldb::tid_t tid); 21586fd8e45SChaoren Lin 216b9c1b51eSKate Stone // This method is requests a stop on all threads which are still running. It 217b9c1b51eSKate Stone // sets up a 218b9c1b51eSKate Stone // deferred delegate notification, which will fire once threads report as 219b9c1b51eSKate Stone // stopped. The 2201dbc6c9cSPavel Labath // triggerring_tid will be set as the current thread (main stop reason). 221b9c1b51eSKate Stone void StopRunningThreads(lldb::tid_t triggering_tid); 222c076559aSPavel Labath 2239eb1ecb9SPavel Labath // Notify the delegate if all threads have stopped. 2249eb1ecb9SPavel Labath void SignalIfAllThreadsStopped(); 225c076559aSPavel Labath 226b9c1b51eSKate Stone // Resume the given thread, optionally passing it the given signal. The type 227b9c1b51eSKate Stone // of resume 2280e1d729bSPavel Labath // operation (continue, single-step) depends on the state parameter. 22997206d57SZachary Turner Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state, 230b9c1b51eSKate Stone int signo); 231c076559aSPavel Labath 232b9c1b51eSKate Stone void ThreadWasCreated(NativeThreadLinux &thread); 233c076559aSPavel Labath 234b9c1b51eSKate Stone void SigchldHandler(); 235a6f5795aSTamas Berghammer 23697206d57SZachary Turner Status PopulateMemoryRegionCache(); 23799e37695SRavitheja Addepally 2380b697561SWalter Erquinigo /// Manages Intel PT process and thread traces. 2390b697561SWalter Erquinigo IntelPTManager m_intel_pt_manager; 240c8d18cbaSMichał Górny 241c8d18cbaSMichał Górny struct CloneInfo { 242c8d18cbaSMichał Górny int event; 243c8d18cbaSMichał Górny lldb::tid_t parent_tid; 244c8d18cbaSMichał Górny }; 245c8d18cbaSMichał Górny 246c8d18cbaSMichał Górny // Map of child processes that have been signaled once, and we are 247c8d18cbaSMichał Górny // waiting for the second signal. 248c8d18cbaSMichał Górny llvm::DenseMap<lldb::pid_t, llvm::Optional<CloneInfo>> m_pending_pid_map; 249c8d18cbaSMichał Górny 250c8d18cbaSMichał Górny // Handle a clone()-like event. If received by parent, clone_info contains 251c8d18cbaSMichał Górny // additional info. Returns true if the event is handled, or false if it 252c8d18cbaSMichał Górny // is pending second notification. 253c8d18cbaSMichał Górny bool MonitorClone(lldb::pid_t child_pid, 254c8d18cbaSMichał Górny llvm::Optional<CloneInfo> clone_info); 255af245d11STodd Fiala }; 256db264a6dSTamas Berghammer 257db264a6dSTamas Berghammer } // namespace process_linux 258db264a6dSTamas Berghammer } // namespace lldb_private 259af245d11STodd Fiala 260af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_ 261