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 <semaphore.h> 15af245d11STodd Fiala #include <signal.h> 16af245d11STodd Fiala 17af245d11STodd Fiala // C++ Includes 18af245d11STodd Fiala #include <unordered_set> 19af245d11STodd Fiala 20af245d11STodd Fiala // Other libraries and framework includes 21af245d11STodd Fiala #include "lldb/Core/ArchSpec.h" 22af245d11STodd Fiala #include "lldb/lldb-types.h" 23af245d11STodd Fiala #include "lldb/Host/Debug.h" 2439de3110SZachary Turner #include "lldb/Host/HostThread.h" 25af245d11STodd Fiala #include "lldb/Host/Mutex.h" 26af245d11STodd Fiala #include "lldb/Target/MemoryRegionInfo.h" 27af245d11STodd Fiala 28af245d11STodd Fiala #include "Host/common/NativeProcessProtocol.h" 29af245d11STodd Fiala 30af245d11STodd Fiala namespace lldb_private 31af245d11STodd Fiala { 32af245d11STodd Fiala class Error; 33af245d11STodd Fiala class Module; 34af245d11STodd Fiala class Scalar; 35af245d11STodd Fiala 36af245d11STodd Fiala /// @class NativeProcessLinux 37af245d11STodd Fiala /// @brief Manages communication with the inferior (debugee) process. 38af245d11STodd Fiala /// 39af245d11STodd Fiala /// Upon construction, this class prepares and launches an inferior process for 40af245d11STodd Fiala /// debugging. 41af245d11STodd Fiala /// 42af245d11STodd Fiala /// Changes in the inferior process state are broadcasted. 43af245d11STodd Fiala class NativeProcessLinux: public NativeProcessProtocol 44af245d11STodd Fiala { 45af245d11STodd Fiala public: 46af245d11STodd Fiala 47af245d11STodd Fiala // --------------------------------------------------------------------- 48af245d11STodd Fiala // Public Static Methods 49af245d11STodd Fiala // --------------------------------------------------------------------- 50af245d11STodd Fiala static lldb_private::Error 51af245d11STodd Fiala LaunchProcess ( 52af245d11STodd Fiala Module *exe_module, 53af245d11STodd Fiala ProcessLaunchInfo &launch_info, 54af245d11STodd Fiala lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 55af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp); 56af245d11STodd Fiala 57af245d11STodd Fiala static lldb_private::Error 58af245d11STodd Fiala AttachToProcess ( 59af245d11STodd Fiala lldb::pid_t pid, 60af245d11STodd Fiala lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 61af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp); 62af245d11STodd Fiala 63af245d11STodd Fiala // --------------------------------------------------------------------- 64af245d11STodd Fiala // Public Instance Methods 65af245d11STodd Fiala // --------------------------------------------------------------------- 66af245d11STodd Fiala 67af245d11STodd Fiala ~NativeProcessLinux() override; 68af245d11STodd Fiala 69af245d11STodd Fiala // --------------------------------------------------------------------- 70af245d11STodd Fiala // NativeProcessProtocol Interface 71af245d11STodd Fiala // --------------------------------------------------------------------- 72af245d11STodd Fiala Error 73af245d11STodd Fiala Resume (const ResumeActionList &resume_actions) override; 74af245d11STodd Fiala 75af245d11STodd Fiala Error 76af245d11STodd Fiala Halt () override; 77af245d11STodd Fiala 78af245d11STodd Fiala Error 79af245d11STodd Fiala Detach () override; 80af245d11STodd Fiala 81af245d11STodd Fiala Error 82af245d11STodd Fiala Signal (int signo) override; 83af245d11STodd Fiala 84af245d11STodd Fiala Error 85af245d11STodd Fiala Kill () override; 86af245d11STodd Fiala 87af245d11STodd Fiala Error 88af245d11STodd Fiala GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override; 89af245d11STodd Fiala 90af245d11STodd Fiala Error 91af245d11STodd Fiala ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) override; 92af245d11STodd Fiala 93af245d11STodd Fiala Error 94af245d11STodd Fiala WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) override; 95af245d11STodd Fiala 96af245d11STodd Fiala Error 97af245d11STodd Fiala AllocateMemory (lldb::addr_t size, uint32_t permissions, lldb::addr_t &addr) override; 98af245d11STodd Fiala 99af245d11STodd Fiala Error 100af245d11STodd Fiala DeallocateMemory (lldb::addr_t addr) override; 101af245d11STodd Fiala 102af245d11STodd Fiala lldb::addr_t 103af245d11STodd Fiala GetSharedLibraryInfoAddress () override; 104af245d11STodd Fiala 105af245d11STodd Fiala size_t 106af245d11STodd Fiala UpdateThreads () override; 107af245d11STodd Fiala 108af245d11STodd Fiala bool 109af245d11STodd Fiala GetArchitecture (ArchSpec &arch) const override; 110af245d11STodd Fiala 111af245d11STodd Fiala Error 112af245d11STodd Fiala SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override; 113af245d11STodd Fiala 114af245d11STodd Fiala void 115af245d11STodd Fiala DoStopIDBumped (uint32_t newBumpId) override; 116af245d11STodd Fiala 117af245d11STodd Fiala // --------------------------------------------------------------------- 118af245d11STodd Fiala // Interface used by NativeRegisterContext-derived classes. 119af245d11STodd Fiala // --------------------------------------------------------------------- 120af245d11STodd Fiala 121af245d11STodd Fiala /// Reads the contents from the register identified by the given (architecture 122af245d11STodd Fiala /// dependent) offset. 123af245d11STodd Fiala /// 124af245d11STodd Fiala /// This method is provided for use by RegisterContextLinux derivatives. 125af245d11STodd Fiala bool 126af245d11STodd Fiala ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, 127af245d11STodd Fiala unsigned size, lldb_private::RegisterValue &value); 128af245d11STodd Fiala 129af245d11STodd Fiala /// Writes the given value to the register identified by the given 130af245d11STodd Fiala /// (architecture dependent) offset. 131af245d11STodd Fiala /// 132af245d11STodd Fiala /// This method is provided for use by RegisterContextLinux derivatives. 133af245d11STodd Fiala bool 134af245d11STodd Fiala WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, 135af245d11STodd Fiala const lldb_private::RegisterValue &value); 136af245d11STodd Fiala 137af245d11STodd Fiala /// Reads all general purpose registers into the specified buffer. 138af245d11STodd Fiala bool 139af245d11STodd Fiala ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size); 140af245d11STodd Fiala 141af245d11STodd Fiala /// Reads generic floating point registers into the specified buffer. 142af245d11STodd Fiala bool 143af245d11STodd Fiala ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size); 144af245d11STodd Fiala 145af245d11STodd Fiala /// Reads the specified register set into the specified buffer. 146af245d11STodd Fiala /// For instance, the extended floating-point register set. 147af245d11STodd Fiala bool 148af245d11STodd Fiala ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset); 149af245d11STodd Fiala 150af245d11STodd Fiala /// Writes all general purpose registers into the specified buffer. 151af245d11STodd Fiala bool 152af245d11STodd Fiala WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size); 153af245d11STodd Fiala 154af245d11STodd Fiala /// Writes generic floating point registers into the specified buffer. 155af245d11STodd Fiala bool 156af245d11STodd Fiala WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size); 157af245d11STodd Fiala 158af245d11STodd Fiala /// Writes the specified register set into the specified buffer. 159af245d11STodd Fiala /// For instance, the extended floating-point register set. 160af245d11STodd Fiala bool 161af245d11STodd Fiala WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset); 162af245d11STodd Fiala 163af245d11STodd Fiala protected: 164af245d11STodd Fiala // --------------------------------------------------------------------- 165af245d11STodd Fiala // NativeProcessProtocol protected interface 166af245d11STodd Fiala // --------------------------------------------------------------------- 167af245d11STodd Fiala Error 168af245d11STodd Fiala GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override; 169af245d11STodd Fiala 170af245d11STodd Fiala private: 171af245d11STodd Fiala 172af245d11STodd Fiala lldb_private::ArchSpec m_arch; 173af245d11STodd Fiala 17439de3110SZachary Turner HostThread m_operation_thread; 17539de3110SZachary Turner HostThread m_monitor_thread; 176af245d11STodd Fiala 177af245d11STodd Fiala // current operation which must be executed on the priviliged thread 178af245d11STodd Fiala void *m_operation; 179af245d11STodd Fiala lldb_private::Mutex m_operation_mutex; 180af245d11STodd Fiala 181af245d11STodd Fiala // semaphores notified when Operation is ready to be processed and when 182af245d11STodd Fiala // the operation is complete. 183af245d11STodd Fiala sem_t m_operation_pending; 184af245d11STodd Fiala sem_t m_operation_done; 185af245d11STodd Fiala 186af245d11STodd Fiala // Set of tids we're waiting to stop before we notify the delegate of 187af245d11STodd Fiala // the stopped state. We only notify the delegate after all threads 188af245d11STodd Fiala // ordered to stop have signaled their stop. 189af245d11STodd Fiala std::unordered_set<lldb::tid_t> m_wait_for_stop_tids; 190af245d11STodd Fiala lldb_private::Mutex m_wait_for_stop_tids_mutex; 191af245d11STodd Fiala 192*511e5cdcSTodd Fiala std::unordered_set<lldb::tid_t> m_wait_for_group_stop_tids; 193*511e5cdcSTodd Fiala lldb::tid_t m_group_stop_signal_tid; 194*511e5cdcSTodd Fiala int m_group_stop_signal; 195*511e5cdcSTodd Fiala lldb_private::Mutex m_wait_for_group_stop_tids_mutex; 196*511e5cdcSTodd Fiala 197af245d11STodd Fiala lldb_private::LazyBool m_supports_mem_region; 198af245d11STodd Fiala std::vector<MemoryRegionInfo> m_mem_region_cache; 199af245d11STodd Fiala lldb_private::Mutex m_mem_region_cache_mutex; 200af245d11STodd Fiala 201af245d11STodd Fiala 202af245d11STodd Fiala struct OperationArgs 203af245d11STodd Fiala { 204af245d11STodd Fiala OperationArgs(NativeProcessLinux *monitor); 205af245d11STodd Fiala 206af245d11STodd Fiala ~OperationArgs(); 207af245d11STodd Fiala 208af245d11STodd Fiala NativeProcessLinux *m_monitor; // The monitor performing the attach. 209af245d11STodd Fiala sem_t m_semaphore; // Posted to once operation complete. 210af245d11STodd Fiala lldb_private::Error m_error; // Set if process operation failed. 211af245d11STodd Fiala }; 212af245d11STodd Fiala 213af245d11STodd Fiala /// @class LauchArgs 214af245d11STodd Fiala /// 215af245d11STodd Fiala /// @brief Simple structure to pass data to the thread responsible for 216af245d11STodd Fiala /// launching a child process. 217af245d11STodd Fiala struct LaunchArgs : OperationArgs 218af245d11STodd Fiala { 219af245d11STodd Fiala LaunchArgs(NativeProcessLinux *monitor, 220af245d11STodd Fiala lldb_private::Module *module, 221af245d11STodd Fiala char const **argv, 222af245d11STodd Fiala char const **envp, 223af245d11STodd Fiala const char *stdin_path, 224af245d11STodd Fiala const char *stdout_path, 225af245d11STodd Fiala const char *stderr_path, 2260bce1b67STodd Fiala const char *working_dir, 2270bce1b67STodd Fiala const lldb_private::ProcessLaunchInfo &launch_info); 228af245d11STodd Fiala 229af245d11STodd Fiala ~LaunchArgs(); 230af245d11STodd Fiala 231af245d11STodd Fiala lldb_private::Module *m_module; // The executable image to launch. 232af245d11STodd Fiala char const **m_argv; // Process arguments. 233af245d11STodd Fiala char const **m_envp; // Process environment. 234af245d11STodd Fiala const char *m_stdin_path; // Redirect stdin or NULL. 235af245d11STodd Fiala const char *m_stdout_path; // Redirect stdout or NULL. 236af245d11STodd Fiala const char *m_stderr_path; // Redirect stderr or NULL. 237af245d11STodd Fiala const char *m_working_dir; // Working directory or NULL. 2380bce1b67STodd Fiala const lldb_private::ProcessLaunchInfo &m_launch_info; 239af245d11STodd Fiala }; 240af245d11STodd Fiala 241af245d11STodd Fiala struct AttachArgs : OperationArgs 242af245d11STodd Fiala { 243af245d11STodd Fiala AttachArgs(NativeProcessLinux *monitor, 244af245d11STodd Fiala lldb::pid_t pid); 245af245d11STodd Fiala 246af245d11STodd Fiala ~AttachArgs(); 247af245d11STodd Fiala 248af245d11STodd Fiala lldb::pid_t m_pid; // pid of the process to be attached. 249af245d11STodd Fiala }; 250af245d11STodd Fiala 251af245d11STodd Fiala // --------------------------------------------------------------------- 252af245d11STodd Fiala // Private Instance Methods 253af245d11STodd Fiala // --------------------------------------------------------------------- 254af245d11STodd Fiala NativeProcessLinux (); 255af245d11STodd Fiala 256af245d11STodd Fiala /// Launches an inferior process ready for debugging. Forms the 257af245d11STodd Fiala /// implementation of Process::DoLaunch. 258af245d11STodd Fiala void 259af245d11STodd Fiala LaunchInferior ( 260af245d11STodd Fiala Module *module, 261af245d11STodd Fiala char const *argv[], 262af245d11STodd Fiala char const *envp[], 263af245d11STodd Fiala const char *stdin_path, 264af245d11STodd Fiala const char *stdout_path, 265af245d11STodd Fiala const char *stderr_path, 266af245d11STodd Fiala const char *working_dir, 2670bce1b67STodd Fiala const lldb_private::ProcessLaunchInfo &launch_info, 268af245d11STodd Fiala Error &error); 269af245d11STodd Fiala 270af245d11STodd Fiala /// Attaches to an existing process. Forms the 271af245d11STodd Fiala /// implementation of Process::DoLaunch. 272af245d11STodd Fiala void 273af245d11STodd Fiala AttachToInferior (lldb::pid_t pid, Error &error); 274af245d11STodd Fiala 275af245d11STodd Fiala void 276af245d11STodd Fiala StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error); 277af245d11STodd Fiala 278af245d11STodd Fiala static void * 279af245d11STodd Fiala LaunchOpThread(void *arg); 280af245d11STodd Fiala 281af245d11STodd Fiala static bool 282af245d11STodd Fiala Launch(LaunchArgs *args); 283af245d11STodd Fiala 284af245d11STodd Fiala void 285af245d11STodd Fiala StartAttachOpThread(AttachArgs *args, lldb_private::Error &error); 286af245d11STodd Fiala 287af245d11STodd Fiala static void * 288af245d11STodd Fiala AttachOpThread(void *args); 289af245d11STodd Fiala 290af245d11STodd Fiala static bool 291af245d11STodd Fiala Attach(AttachArgs *args); 292af245d11STodd Fiala 293af245d11STodd Fiala static bool 294af245d11STodd Fiala SetDefaultPtraceOpts(const lldb::pid_t); 295af245d11STodd Fiala 296af245d11STodd Fiala static void 297af245d11STodd Fiala ServeOperation(OperationArgs *args); 298af245d11STodd Fiala 299af245d11STodd Fiala static bool 300af245d11STodd Fiala DupDescriptor(const char *path, int fd, int flags); 301af245d11STodd Fiala 302af245d11STodd Fiala static bool 303af245d11STodd Fiala MonitorCallback(void *callback_baton, 304af245d11STodd Fiala lldb::pid_t pid, bool exited, int signal, int status); 305af245d11STodd Fiala 306af245d11STodd Fiala void 307af245d11STodd Fiala MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid); 308af245d11STodd Fiala 309af245d11STodd Fiala void 310af245d11STodd Fiala MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited); 311af245d11STodd Fiala 312af245d11STodd Fiala #if 0 313af245d11STodd Fiala static ::ProcessMessage::CrashReason 314af245d11STodd Fiala GetCrashReasonForSIGSEGV(const siginfo_t *info); 315af245d11STodd Fiala 316af245d11STodd Fiala static ::ProcessMessage::CrashReason 317af245d11STodd Fiala GetCrashReasonForSIGILL(const siginfo_t *info); 318af245d11STodd Fiala 319af245d11STodd Fiala static ::ProcessMessage::CrashReason 320af245d11STodd Fiala GetCrashReasonForSIGFPE(const siginfo_t *info); 321af245d11STodd Fiala 322af245d11STodd Fiala static ::ProcessMessage::CrashReason 323af245d11STodd Fiala GetCrashReasonForSIGBUS(const siginfo_t *info); 324af245d11STodd Fiala #endif 325af245d11STodd Fiala 326af245d11STodd Fiala void 327af245d11STodd Fiala DoOperation(void *op); 328af245d11STodd Fiala 329af245d11STodd Fiala /// Stops the child monitor thread. 330af245d11STodd Fiala void 331af245d11STodd Fiala StopMonitoringChildProcess(); 332af245d11STodd Fiala 333af245d11STodd Fiala /// Stops the operation thread used to attach/launch a process. 334af245d11STodd Fiala void 335af245d11STodd Fiala StopOpThread(); 336af245d11STodd Fiala 337af245d11STodd Fiala /// Stops monitoring the child process thread. 338af245d11STodd Fiala void 339af245d11STodd Fiala StopMonitor(); 340af245d11STodd Fiala 341af245d11STodd Fiala bool 342af245d11STodd Fiala HasThreadNoLock (lldb::tid_t thread_id); 343af245d11STodd Fiala 344af245d11STodd Fiala NativeThreadProtocolSP 345af245d11STodd Fiala MaybeGetThreadNoLock (lldb::tid_t thread_id); 346af245d11STodd Fiala 347af245d11STodd Fiala bool 348af245d11STodd Fiala StopTrackingThread (lldb::tid_t thread_id); 349af245d11STodd Fiala 350af245d11STodd Fiala NativeThreadProtocolSP 351af245d11STodd Fiala AddThread (lldb::tid_t thread_id); 352af245d11STodd Fiala 353af245d11STodd Fiala NativeThreadProtocolSP 354af245d11STodd Fiala GetOrCreateThread (lldb::tid_t thread_id, bool &created); 355af245d11STodd Fiala 356af245d11STodd Fiala Error 357af245d11STodd Fiala GetSoftwareBreakpointSize (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size); 358af245d11STodd Fiala 359af245d11STodd Fiala Error 360af245d11STodd Fiala FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp); 361af245d11STodd Fiala 362af245d11STodd Fiala /// Writes a siginfo_t structure corresponding to the given thread ID to the 363af245d11STodd Fiala /// memory region pointed to by @p siginfo. 364af245d11STodd Fiala bool 365af245d11STodd Fiala GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err); 366af245d11STodd Fiala 367af245d11STodd Fiala /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 368af245d11STodd Fiala /// corresponding to the given thread ID to the memory pointed to by @p 369af245d11STodd Fiala /// message. 370af245d11STodd Fiala bool 371af245d11STodd Fiala GetEventMessage(lldb::tid_t tid, unsigned long *message); 372af245d11STodd Fiala 373af245d11STodd Fiala /// Resumes the given thread. If @p signo is anything but 374af245d11STodd Fiala /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 375af245d11STodd Fiala bool 376af245d11STodd Fiala Resume(lldb::tid_t tid, uint32_t signo); 377af245d11STodd Fiala 378af245d11STodd Fiala /// Single steps the given thread. If @p signo is anything but 379af245d11STodd Fiala /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 380af245d11STodd Fiala bool 381af245d11STodd Fiala SingleStep(lldb::tid_t tid, uint32_t signo); 382af245d11STodd Fiala 383*511e5cdcSTodd Fiala /// Safely mark all existing threads as waiting for group stop. 384*511e5cdcSTodd Fiala /// When the final group stop comes in from the set of group stop threads, 385*511e5cdcSTodd Fiala /// we'll mark the current thread as signaled_thread_tid and set its stop 386*511e5cdcSTodd Fiala /// reason as the given signo. All other threads from group stop notification 387*511e5cdcSTodd Fiala /// will have thread stop reason marked as signaled with no signo. 388*511e5cdcSTodd Fiala void 389*511e5cdcSTodd Fiala SetGroupStopTids (lldb::tid_t signaled_thread_tid, int signo); 390*511e5cdcSTodd Fiala 391*511e5cdcSTodd Fiala void 392*511e5cdcSTodd Fiala OnGroupStop (lldb::tid_t tid); 393*511e5cdcSTodd Fiala 394af245d11STodd Fiala lldb_private::Error 395af245d11STodd Fiala Detach(lldb::tid_t tid); 396af245d11STodd Fiala }; 397af245d11STodd Fiala } // End lldb_private namespace. 398af245d11STodd Fiala 399af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_ 400