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/lldb-types.h" 19af245d11STodd Fiala #include "lldb/Host/Debug.h" 20d3173f34SChaoren Lin #include "lldb/Host/FileSpec.h" 2139de3110SZachary Turner #include "lldb/Host/HostThread.h" 22af245d11STodd Fiala #include "lldb/Host/Mutex.h" 23af245d11STodd Fiala #include "lldb/Target/MemoryRegionInfo.h" 24af245d11STodd Fiala 252fe1d0abSChaoren Lin #include "lldb/Host/common/NativeProcessProtocol.h" 268c8ff7afSPavel Labath #include "NativeThreadLinux.h" 27af245d11STodd Fiala 28db264a6dSTamas Berghammer namespace lldb_private { 29af245d11STodd Fiala class Error; 30af245d11STodd Fiala class Module; 31af245d11STodd Fiala class Scalar; 32af245d11STodd Fiala 33db264a6dSTamas Berghammer namespace process_linux { 34af245d11STodd Fiala /// @class NativeProcessLinux 35af245d11STodd Fiala /// @brief Manages communication with the inferior (debugee) process. 36af245d11STodd Fiala /// 37af245d11STodd Fiala /// Upon construction, this class prepares and launches an inferior process for 38af245d11STodd Fiala /// debugging. 39af245d11STodd Fiala /// 40af245d11STodd Fiala /// Changes in the inferior process state are broadcasted. 41af245d11STodd Fiala class NativeProcessLinux: public NativeProcessProtocol 42af245d11STodd Fiala { 43d5b310f2SPavel Labath friend Error 44d5b310f2SPavel Labath NativeProcessProtocol::Launch (ProcessLaunchInfo &launch_info, 45d5b310f2SPavel Labath NativeDelegate &native_delegate, 4619cbe96aSPavel Labath MainLoop &mainloop, 47d5b310f2SPavel Labath NativeProcessProtocolSP &process_sp); 48d5b310f2SPavel Labath 49d5b310f2SPavel Labath friend Error 50d5b310f2SPavel Labath NativeProcessProtocol::Attach (lldb::pid_t pid, 51d5b310f2SPavel Labath NativeProcessProtocol::NativeDelegate &native_delegate, 5219cbe96aSPavel Labath MainLoop &mainloop, 5319cbe96aSPavel Labath NativeProcessProtocolSP &process_sp); 54d5b310f2SPavel Labath 55c307c270SSean Callanan public: 56af245d11STodd Fiala // --------------------------------------------------------------------- 57af245d11STodd Fiala // NativeProcessProtocol Interface 58af245d11STodd Fiala // --------------------------------------------------------------------- 59af245d11STodd Fiala Error 60af245d11STodd Fiala Resume (const ResumeActionList &resume_actions) override; 61af245d11STodd Fiala 62af245d11STodd Fiala Error 63af245d11STodd Fiala Halt () override; 64af245d11STodd Fiala 65af245d11STodd Fiala Error 66af245d11STodd Fiala Detach () override; 67af245d11STodd Fiala 68af245d11STodd Fiala Error 69af245d11STodd Fiala Signal (int signo) override; 70af245d11STodd Fiala 71af245d11STodd Fiala Error 72e9547b80SChaoren Lin Interrupt () override; 73e9547b80SChaoren Lin 74e9547b80SChaoren Lin Error 75af245d11STodd Fiala Kill () override; 76af245d11STodd Fiala 77af245d11STodd Fiala Error 78af245d11STodd Fiala GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override; 79af245d11STodd Fiala 80af245d11STodd Fiala Error 813eb4b458SChaoren Lin ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; 82af245d11STodd Fiala 83af245d11STodd Fiala Error 843eb4b458SChaoren Lin ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; 85af245d11STodd Fiala 86af245d11STodd Fiala Error 873eb4b458SChaoren Lin WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override; 883eb4b458SChaoren Lin 893eb4b458SChaoren Lin Error 903eb4b458SChaoren Lin AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override; 91af245d11STodd Fiala 92af245d11STodd Fiala Error 93af245d11STodd Fiala DeallocateMemory (lldb::addr_t addr) override; 94af245d11STodd Fiala 95af245d11STodd Fiala lldb::addr_t 96af245d11STodd Fiala GetSharedLibraryInfoAddress () override; 97af245d11STodd Fiala 98af245d11STodd Fiala size_t 99af245d11STodd Fiala UpdateThreads () override; 100af245d11STodd Fiala 101af245d11STodd Fiala bool 102af245d11STodd Fiala GetArchitecture (ArchSpec &arch) const override; 103af245d11STodd Fiala 104af245d11STodd Fiala Error 105af245d11STodd Fiala SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override; 106af245d11STodd Fiala 107af245d11STodd Fiala void 108af245d11STodd Fiala DoStopIDBumped (uint32_t newBumpId) override; 109af245d11STodd Fiala 110068f8a7eSTamas Berghammer Error 111068f8a7eSTamas Berghammer GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override; 112068f8a7eSTamas Berghammer 113783bfc8cSTamas Berghammer Error 114783bfc8cSTamas Berghammer GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) override; 115783bfc8cSTamas Berghammer 116af245d11STodd Fiala // --------------------------------------------------------------------- 117af245d11STodd Fiala // Interface used by NativeRegisterContext-derived classes. 118af245d11STodd Fiala // --------------------------------------------------------------------- 1194a9babb2SPavel Labath static Error 1204a9babb2SPavel Labath PtraceWrapper(int req, 1214a9babb2SPavel Labath lldb::pid_t pid, 1224a9babb2SPavel Labath void *addr = nullptr, 1234a9babb2SPavel Labath void *data = nullptr, 1244a9babb2SPavel Labath size_t data_size = 0, 1254a9babb2SPavel Labath long *result = nullptr); 1267cb18bf5STamas Berghammer 127af245d11STodd Fiala protected: 128af245d11STodd Fiala // --------------------------------------------------------------------- 129af245d11STodd Fiala // NativeProcessProtocol protected interface 130af245d11STodd Fiala // --------------------------------------------------------------------- 131af245d11STodd Fiala Error 132af245d11STodd Fiala GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override; 133af245d11STodd Fiala 134af245d11STodd Fiala private: 135af245d11STodd Fiala 13619cbe96aSPavel Labath MainLoop::SignalHandleUP m_sigchld_handle; 137db264a6dSTamas Berghammer ArchSpec m_arch; 138af245d11STodd Fiala 139db264a6dSTamas Berghammer LazyBool m_supports_mem_region; 140af245d11STodd Fiala std::vector<MemoryRegionInfo> m_mem_region_cache; 141db264a6dSTamas Berghammer Mutex m_mem_region_cache_mutex; 142af245d11STodd Fiala 143*0e1d729bSPavel Labath lldb::tid_t m_pending_notification_tid; 144*0e1d729bSPavel 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 /// @class LauchArgs 150af245d11STodd Fiala /// 151af245d11STodd Fiala /// @brief Simple structure to pass data to the thread responsible for 152af245d11STodd Fiala /// launching a child process. 153bd7cbc5aSPavel Labath struct LaunchArgs 154af245d11STodd Fiala { 155bd7cbc5aSPavel Labath LaunchArgs(Module *module, 156af245d11STodd Fiala char const **argv, 157af245d11STodd Fiala char const **envp, 158d3173f34SChaoren Lin const FileSpec &stdin_file_spec, 159d3173f34SChaoren Lin const FileSpec &stdout_file_spec, 160d3173f34SChaoren Lin const FileSpec &stderr_file_spec, 161d3173f34SChaoren Lin const FileSpec &working_dir, 162db264a6dSTamas Berghammer const ProcessLaunchInfo &launch_info); 163af245d11STodd Fiala 164af245d11STodd Fiala ~LaunchArgs(); 165af245d11STodd Fiala 166db264a6dSTamas Berghammer Module *m_module; // The executable image to launch. 167af245d11STodd Fiala char const **m_argv; // Process arguments. 168af245d11STodd Fiala char const **m_envp; // Process environment. 169d3173f34SChaoren Lin const FileSpec m_stdin_file_spec; // Redirect stdin if not empty. 170d3173f34SChaoren Lin const FileSpec m_stdout_file_spec; // Redirect stdout if not empty. 171d3173f34SChaoren Lin const FileSpec m_stderr_file_spec; // Redirect stderr if not empty. 172d3173f34SChaoren Lin const FileSpec m_working_dir; // Working directory or empty. 173db264a6dSTamas Berghammer const ProcessLaunchInfo &m_launch_info; 174af245d11STodd Fiala }; 175af245d11STodd Fiala 176bd7cbc5aSPavel Labath typedef std::function< ::pid_t(Error &)> InitialOperation; 177af245d11STodd Fiala 178af245d11STodd Fiala // --------------------------------------------------------------------- 179af245d11STodd Fiala // Private Instance Methods 180af245d11STodd Fiala // --------------------------------------------------------------------- 181af245d11STodd Fiala NativeProcessLinux (); 182af245d11STodd Fiala 183af245d11STodd Fiala /// Launches an inferior process ready for debugging. Forms the 184af245d11STodd Fiala /// implementation of Process::DoLaunch. 185af245d11STodd Fiala void 186af245d11STodd Fiala LaunchInferior ( 18719cbe96aSPavel Labath MainLoop &mainloop, 188af245d11STodd Fiala Module *module, 189af245d11STodd Fiala char const *argv[], 190af245d11STodd Fiala char const *envp[], 191d3173f34SChaoren Lin const FileSpec &stdin_file_spec, 192d3173f34SChaoren Lin const FileSpec &stdout_file_spec, 193d3173f34SChaoren Lin const FileSpec &stderr_file_spec, 194d3173f34SChaoren Lin const FileSpec &working_dir, 195db264a6dSTamas Berghammer const ProcessLaunchInfo &launch_info, 196af245d11STodd Fiala Error &error); 197af245d11STodd Fiala 198af245d11STodd Fiala /// Attaches to an existing process. Forms the 1990cbf0b13STamas Berghammer /// implementation of Process::DoAttach 200af245d11STodd Fiala void 20119cbe96aSPavel Labath AttachToInferior (MainLoop &mainloop, lldb::pid_t pid, Error &error); 2021107b5a5SPavel Labath 203bd7cbc5aSPavel Labath ::pid_t 204bd7cbc5aSPavel Labath Launch(LaunchArgs *args, Error &error); 205af245d11STodd Fiala 206bd7cbc5aSPavel Labath ::pid_t 207bd7cbc5aSPavel Labath Attach(lldb::pid_t pid, Error &error); 208af245d11STodd Fiala 20997ccc294SChaoren Lin static Error 210af245d11STodd Fiala SetDefaultPtraceOpts(const lldb::pid_t); 211af245d11STodd Fiala 212af245d11STodd Fiala static bool 213d3173f34SChaoren Lin DupDescriptor(const FileSpec &file_spec, int fd, int flags); 214af245d11STodd Fiala 2151107b5a5SPavel Labath static void * 2161107b5a5SPavel Labath MonitorThread(void *baton); 2171107b5a5SPavel Labath 2181107b5a5SPavel Labath void 2191107b5a5SPavel Labath MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status); 220af245d11STodd Fiala 221af245d11STodd Fiala void 222426bdf88SPavel Labath WaitForNewThread(::pid_t tid); 223426bdf88SPavel Labath 224426bdf88SPavel Labath void 225af245d11STodd Fiala MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid); 226af245d11STodd Fiala 227af245d11STodd Fiala void 228c16f5dcaSChaoren Lin MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp); 229c16f5dcaSChaoren Lin 230c16f5dcaSChaoren Lin void 231c16f5dcaSChaoren Lin MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp); 232c16f5dcaSChaoren Lin 233c16f5dcaSChaoren Lin void 234c16f5dcaSChaoren Lin MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index); 235c16f5dcaSChaoren Lin 236c16f5dcaSChaoren Lin void 237af245d11STodd Fiala MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited); 238af245d11STodd Fiala 239e7708688STamas Berghammer bool 240e7708688STamas Berghammer SupportHardwareSingleStepping() const; 241e7708688STamas Berghammer 242e7708688STamas Berghammer Error 243e7708688STamas Berghammer SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp); 244e7708688STamas Berghammer 245af245d11STodd Fiala #if 0 246af245d11STodd Fiala static ::ProcessMessage::CrashReason 247af245d11STodd Fiala GetCrashReasonForSIGSEGV(const siginfo_t *info); 248af245d11STodd Fiala 249af245d11STodd Fiala static ::ProcessMessage::CrashReason 250af245d11STodd Fiala GetCrashReasonForSIGILL(const siginfo_t *info); 251af245d11STodd Fiala 252af245d11STodd Fiala static ::ProcessMessage::CrashReason 253af245d11STodd Fiala GetCrashReasonForSIGFPE(const siginfo_t *info); 254af245d11STodd Fiala 255af245d11STodd Fiala static ::ProcessMessage::CrashReason 256af245d11STodd Fiala GetCrashReasonForSIGBUS(const siginfo_t *info); 257af245d11STodd Fiala #endif 258af245d11STodd Fiala 259af245d11STodd Fiala bool 260af245d11STodd Fiala HasThreadNoLock (lldb::tid_t thread_id); 261af245d11STodd Fiala 262af245d11STodd Fiala NativeThreadProtocolSP 263af245d11STodd Fiala MaybeGetThreadNoLock (lldb::tid_t thread_id); 264af245d11STodd Fiala 265af245d11STodd Fiala bool 266af245d11STodd Fiala StopTrackingThread (lldb::tid_t thread_id); 267af245d11STodd Fiala 268af245d11STodd Fiala NativeThreadProtocolSP 269af245d11STodd Fiala AddThread (lldb::tid_t thread_id); 270af245d11STodd Fiala 271af245d11STodd Fiala Error 27263c8be95STamas Berghammer GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size); 273af245d11STodd Fiala 274af245d11STodd Fiala Error 275af245d11STodd Fiala FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp); 276af245d11STodd Fiala 277af245d11STodd Fiala /// Writes a siginfo_t structure corresponding to the given thread ID to the 278af245d11STodd Fiala /// memory region pointed to by @p siginfo. 27997ccc294SChaoren Lin Error 28097ccc294SChaoren Lin GetSignalInfo(lldb::tid_t tid, void *siginfo); 281af245d11STodd Fiala 282af245d11STodd Fiala /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 283af245d11STodd Fiala /// corresponding to the given thread ID to the memory pointed to by @p 284af245d11STodd Fiala /// message. 28597ccc294SChaoren Lin Error 286af245d11STodd Fiala GetEventMessage(lldb::tid_t tid, unsigned long *message); 287af245d11STodd Fiala 288af245d11STodd Fiala /// Resumes the given thread. If @p signo is anything but 289af245d11STodd Fiala /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 29097ccc294SChaoren Lin Error 291af245d11STodd Fiala Resume(lldb::tid_t tid, uint32_t signo); 292af245d11STodd Fiala 293af245d11STodd Fiala /// Single steps the given thread. If @p signo is anything but 294af245d11STodd Fiala /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 29597ccc294SChaoren Lin Error 296af245d11STodd Fiala SingleStep(lldb::tid_t tid, uint32_t signo); 297af245d11STodd Fiala 298511e5cdcSTodd Fiala void 299fa03ad2eSChaoren Lin NotifyThreadDeath (lldb::tid_t tid); 300fa03ad2eSChaoren Lin 301db264a6dSTamas Berghammer Error 302af245d11STodd Fiala Detach(lldb::tid_t tid); 30386fd8e45SChaoren Lin 304c076559aSPavel Labath 3051dbc6c9cSPavel Labath // This method is requests a stop on all threads which are still running. It sets up a 3061dbc6c9cSPavel Labath // deferred delegate notification, which will fire once threads report as stopped. The 3071dbc6c9cSPavel Labath // triggerring_tid will be set as the current thread (main stop reason). 308c076559aSPavel Labath void 309337f3eb9SPavel Labath StopRunningThreads(lldb::tid_t triggering_tid); 310c076559aSPavel Labath 3119eb1ecb9SPavel Labath // Notify the delegate if all threads have stopped. 3129eb1ecb9SPavel Labath void SignalIfAllThreadsStopped(); 313c076559aSPavel Labath 314*0e1d729bSPavel Labath // Resume the given thread, optionally passing it the given signal. The type of resume 315*0e1d729bSPavel Labath // operation (continue, single-step) depends on the state parameter. 3165eb721edSPavel Labath Error 317*0e1d729bSPavel Labath ResumeThread(const NativeThreadLinuxSP &thread_sp, lldb::StateType state, int signo); 318c076559aSPavel Labath 319c076559aSPavel Labath void 3208c8ff7afSPavel Labath ThreadWasCreated (lldb::tid_t tid); 321c076559aSPavel Labath 32219cbe96aSPavel Labath void 32319cbe96aSPavel Labath SigchldHandler(); 324af245d11STodd Fiala }; 325db264a6dSTamas Berghammer 326db264a6dSTamas Berghammer } // namespace process_linux 327db264a6dSTamas Berghammer } // namespace lldb_private 328af245d11STodd Fiala 329af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_ 330