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" 19d3173f34SChaoren Lin #include "lldb/Host/FileSpec.h" 2039de3110SZachary Turner #include "lldb/Host/HostThread.h" 21af245d11STodd Fiala #include "lldb/Target/MemoryRegionInfo.h" 22b9c1b51eSKate Stone #include "lldb/lldb-types.h" 23af245d11STodd Fiala 248c8ff7afSPavel Labath #include "NativeThreadLinux.h" 25b9c1b51eSKate Stone #include "lldb/Host/common/NativeProcessProtocol.h" 26af245d11STodd Fiala 27db264a6dSTamas Berghammer namespace lldb_private { 28af245d11STodd Fiala class Error; 29af245d11STodd Fiala class Scalar; 30af245d11STodd Fiala 31db264a6dSTamas Berghammer namespace process_linux { 32af245d11STodd Fiala /// @class NativeProcessLinux 33af245d11STodd Fiala /// @brief Manages communication with the inferior (debugee) process. 34af245d11STodd Fiala /// 35af245d11STodd Fiala /// Upon construction, this class prepares and launches an inferior process for 36af245d11STodd Fiala /// debugging. 37af245d11STodd Fiala /// 38af245d11STodd Fiala /// Changes in the inferior process state are broadcasted. 39b9c1b51eSKate Stone class NativeProcessLinux : public NativeProcessProtocol { 40b9c1b51eSKate Stone friend Error NativeProcessProtocol::Launch( 41b9c1b51eSKate Stone ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate, 42b9c1b51eSKate Stone MainLoop &mainloop, NativeProcessProtocolSP &process_sp); 43d5b310f2SPavel Labath 44b9c1b51eSKate Stone friend Error NativeProcessProtocol::Attach( 45b9c1b51eSKate Stone lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 46b9c1b51eSKate Stone MainLoop &mainloop, NativeProcessProtocolSP &process_sp); 47d5b310f2SPavel Labath 48c307c270SSean Callanan public: 49af245d11STodd Fiala // --------------------------------------------------------------------- 50af245d11STodd Fiala // NativeProcessProtocol Interface 51af245d11STodd Fiala // --------------------------------------------------------------------- 52b9c1b51eSKate Stone Error Resume(const ResumeActionList &resume_actions) override; 53af245d11STodd Fiala 54b9c1b51eSKate Stone Error Halt() override; 55af245d11STodd Fiala 56b9c1b51eSKate Stone Error Detach() override; 57af245d11STodd Fiala 58b9c1b51eSKate Stone Error Signal(int signo) override; 59af245d11STodd Fiala 60b9c1b51eSKate Stone Error Interrupt() override; 61e9547b80SChaoren Lin 62b9c1b51eSKate Stone Error Kill() override; 63af245d11STodd Fiala 64b9c1b51eSKate Stone Error GetMemoryRegionInfo(lldb::addr_t load_addr, 65b9c1b51eSKate Stone MemoryRegionInfo &range_info) override; 66af245d11STodd Fiala 67b9c1b51eSKate Stone Error ReadMemory(lldb::addr_t addr, void *buf, size_t size, 68b9c1b51eSKate Stone size_t &bytes_read) override; 69af245d11STodd Fiala 70b9c1b51eSKate Stone Error ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, 71b9c1b51eSKate Stone size_t &bytes_read) override; 72af245d11STodd Fiala 73b9c1b51eSKate Stone Error WriteMemory(lldb::addr_t addr, const void *buf, size_t size, 74b9c1b51eSKate Stone size_t &bytes_written) override; 753eb4b458SChaoren Lin 76b9c1b51eSKate Stone Error AllocateMemory(size_t size, uint32_t permissions, 77b9c1b51eSKate Stone lldb::addr_t &addr) override; 78af245d11STodd Fiala 79b9c1b51eSKate Stone Error 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 85b9c1b51eSKate Stone bool GetArchitecture(ArchSpec &arch) const override; 86af245d11STodd Fiala 87b9c1b51eSKate Stone Error SetBreakpoint(lldb::addr_t addr, uint32_t size, bool hardware) override; 88af245d11STodd Fiala 89b9c1b51eSKate Stone void DoStopIDBumped(uint32_t newBumpId) override; 90af245d11STodd Fiala 91b9c1b51eSKate Stone Error GetLoadedModuleFileSpec(const char *module_path, 92b9c1b51eSKate Stone FileSpec &file_spec) override; 93068f8a7eSTamas Berghammer 94b9c1b51eSKate Stone Error GetFileLoadAddress(const llvm::StringRef &file_name, 95b9c1b51eSKate Stone lldb::addr_t &load_addr) override; 96783bfc8cSTamas Berghammer 97b9c1b51eSKate Stone NativeThreadLinuxSP GetThreadByID(lldb::tid_t id); 98f9077782SPavel Labath 99af245d11STodd Fiala // --------------------------------------------------------------------- 100af245d11STodd Fiala // Interface used by NativeRegisterContext-derived classes. 101af245d11STodd Fiala // --------------------------------------------------------------------- 102b9c1b51eSKate Stone static Error PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr, 103b9c1b51eSKate Stone void *data = nullptr, size_t data_size = 0, 1044a9babb2SPavel Labath long *result = nullptr); 1057cb18bf5STamas Berghammer 106b9c1b51eSKate Stone bool SupportHardwareSingleStepping() const; 107605b51b8SPavel Labath 108af245d11STodd Fiala protected: 109af245d11STodd Fiala // --------------------------------------------------------------------- 110af245d11STodd Fiala // NativeProcessProtocol protected interface 111af245d11STodd Fiala // --------------------------------------------------------------------- 112af245d11STodd Fiala Error 113b9c1b51eSKate Stone GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint, 114b9c1b51eSKate Stone size_t &actual_opcode_size, 115b9c1b51eSKate Stone const uint8_t *&trap_opcode_bytes) override; 116af245d11STodd Fiala 117af245d11STodd Fiala private: 11819cbe96aSPavel Labath MainLoop::SignalHandleUP m_sigchld_handle; 119db264a6dSTamas Berghammer ArchSpec m_arch; 120af245d11STodd Fiala 121db264a6dSTamas Berghammer LazyBool m_supports_mem_region; 122*a6f5795aSTamas Berghammer std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache; 123af245d11STodd Fiala 1240e1d729bSPavel Labath lldb::tid_t m_pending_notification_tid; 1250e1d729bSPavel Labath 126d8c338d4STamas Berghammer // List of thread ids stepping with a breakpoint with the address of 127d8c338d4STamas Berghammer // the relevan breakpoint 128d8c338d4STamas Berghammer std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint; 129d8c338d4STamas Berghammer 130af245d11STodd Fiala // --------------------------------------------------------------------- 131af245d11STodd Fiala // Private Instance Methods 132af245d11STodd Fiala // --------------------------------------------------------------------- 133af245d11STodd Fiala NativeProcessLinux(); 134af245d11STodd Fiala 135b9c1b51eSKate Stone Error LaunchInferior(MainLoop &mainloop, ProcessLaunchInfo &launch_info); 136af245d11STodd Fiala 137af245d11STodd Fiala /// Attaches to an existing process. Forms the 1380cbf0b13STamas Berghammer /// implementation of Process::DoAttach 139b9c1b51eSKate Stone void AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, Error &error); 1401107b5a5SPavel Labath 141b9c1b51eSKate Stone ::pid_t Attach(lldb::pid_t pid, Error &error); 142af245d11STodd Fiala 143b9c1b51eSKate Stone static Error SetDefaultPtraceOpts(const lldb::pid_t); 144af245d11STodd Fiala 145b9c1b51eSKate Stone static void *MonitorThread(void *baton); 1461107b5a5SPavel Labath 147b9c1b51eSKate Stone void MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status); 148af245d11STodd Fiala 149b9c1b51eSKate Stone void WaitForNewThread(::pid_t tid); 150426bdf88SPavel Labath 151b9c1b51eSKate Stone void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread); 152af245d11STodd Fiala 153b9c1b51eSKate Stone void MonitorTrace(NativeThreadLinux &thread); 154c16f5dcaSChaoren Lin 155b9c1b51eSKate Stone void MonitorBreakpoint(NativeThreadLinux &thread); 156c16f5dcaSChaoren Lin 157b9c1b51eSKate Stone void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index); 158c16f5dcaSChaoren Lin 159b9c1b51eSKate Stone void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, 160b9c1b51eSKate Stone bool exited); 161af245d11STodd Fiala 162b9c1b51eSKate Stone Error SetupSoftwareSingleStepping(NativeThreadLinux &thread); 163e7708688STamas Berghammer 164af245d11STodd Fiala #if 0 165af245d11STodd Fiala static ::ProcessMessage::CrashReason 166af245d11STodd Fiala GetCrashReasonForSIGSEGV(const siginfo_t *info); 167af245d11STodd Fiala 168af245d11STodd Fiala static ::ProcessMessage::CrashReason 169af245d11STodd Fiala GetCrashReasonForSIGILL(const siginfo_t *info); 170af245d11STodd Fiala 171af245d11STodd Fiala static ::ProcessMessage::CrashReason 172af245d11STodd Fiala GetCrashReasonForSIGFPE(const siginfo_t *info); 173af245d11STodd Fiala 174af245d11STodd Fiala static ::ProcessMessage::CrashReason 175af245d11STodd Fiala GetCrashReasonForSIGBUS(const siginfo_t *info); 176af245d11STodd Fiala #endif 177af245d11STodd Fiala 178b9c1b51eSKate Stone bool HasThreadNoLock(lldb::tid_t thread_id); 179af245d11STodd Fiala 180b9c1b51eSKate Stone bool StopTrackingThread(lldb::tid_t thread_id); 181af245d11STodd Fiala 182b9c1b51eSKate Stone NativeThreadLinuxSP AddThread(lldb::tid_t thread_id); 183af245d11STodd Fiala 184b9c1b51eSKate Stone Error GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size); 185af245d11STodd Fiala 186b9c1b51eSKate Stone Error FixupBreakpointPCAsNeeded(NativeThreadLinux &thread); 187af245d11STodd Fiala 188af245d11STodd Fiala /// Writes a siginfo_t structure corresponding to the given thread ID to the 189af245d11STodd Fiala /// memory region pointed to by @p siginfo. 190b9c1b51eSKate Stone Error GetSignalInfo(lldb::tid_t tid, void *siginfo); 191af245d11STodd Fiala 192af245d11STodd Fiala /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 193af245d11STodd Fiala /// corresponding to the given thread ID to the memory pointed to by @p 194af245d11STodd Fiala /// message. 195b9c1b51eSKate Stone Error GetEventMessage(lldb::tid_t tid, unsigned long *message); 196af245d11STodd Fiala 197b9c1b51eSKate Stone void NotifyThreadDeath(lldb::tid_t tid); 198fa03ad2eSChaoren Lin 199b9c1b51eSKate Stone Error Detach(lldb::tid_t tid); 20086fd8e45SChaoren Lin 201b9c1b51eSKate Stone // This method is requests a stop on all threads which are still running. It 202b9c1b51eSKate Stone // sets up a 203b9c1b51eSKate Stone // deferred delegate notification, which will fire once threads report as 204b9c1b51eSKate Stone // stopped. The 2051dbc6c9cSPavel Labath // triggerring_tid will be set as the current thread (main stop reason). 206b9c1b51eSKate Stone void StopRunningThreads(lldb::tid_t triggering_tid); 207c076559aSPavel Labath 2089eb1ecb9SPavel Labath // Notify the delegate if all threads have stopped. 2099eb1ecb9SPavel Labath void SignalIfAllThreadsStopped(); 210c076559aSPavel Labath 211b9c1b51eSKate Stone // Resume the given thread, optionally passing it the given signal. The type 212b9c1b51eSKate Stone // of resume 2130e1d729bSPavel Labath // operation (continue, single-step) depends on the state parameter. 214b9c1b51eSKate Stone Error ResumeThread(NativeThreadLinux &thread, lldb::StateType state, 215b9c1b51eSKate Stone int signo); 216c076559aSPavel Labath 217b9c1b51eSKate Stone void ThreadWasCreated(NativeThreadLinux &thread); 218c076559aSPavel Labath 219b9c1b51eSKate Stone void SigchldHandler(); 220*a6f5795aSTamas Berghammer 221*a6f5795aSTamas Berghammer Error PopulateMemoryRegionCache(); 222af245d11STodd Fiala }; 223db264a6dSTamas Berghammer 224db264a6dSTamas Berghammer } // namespace process_linux 225db264a6dSTamas Berghammer } // namespace lldb_private 226af245d11STodd Fiala 227af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_ 228