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