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 { 43af245d11STodd Fiala public: 44af245d11STodd Fiala 45db264a6dSTamas Berghammer static Error 46af245d11STodd Fiala LaunchProcess ( 47af245d11STodd Fiala Module *exe_module, 48af245d11STodd Fiala ProcessLaunchInfo &launch_info, 49db264a6dSTamas Berghammer NativeProcessProtocol::NativeDelegate &native_delegate, 50af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp); 51af245d11STodd Fiala 52db264a6dSTamas Berghammer static Error 53af245d11STodd Fiala AttachToProcess ( 54af245d11STodd Fiala lldb::pid_t pid, 55db264a6dSTamas Berghammer NativeProcessProtocol::NativeDelegate &native_delegate, 56af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp); 57af245d11STodd Fiala 58068f8a7eSTamas Berghammer //------------------------------------------------------------------------------ 59068f8a7eSTamas Berghammer /// @class Operation 60068f8a7eSTamas Berghammer /// @brief Represents a NativeProcessLinux operation. 61068f8a7eSTamas Berghammer /// 62068f8a7eSTamas Berghammer /// Under Linux, it is not possible to ptrace() from any other thread but the 63068f8a7eSTamas Berghammer /// one that spawned or attached to the process from the start. Therefore, when 64068f8a7eSTamas Berghammer /// a NativeProcessLinux is asked to deliver or change the state of an inferior 65068f8a7eSTamas Berghammer /// process the operation must be "funneled" to a specific thread to perform the 66*c7512fdcSPavel Labath /// task. 67*c7512fdcSPavel Labath typedef std::function<Error()> Operation; 68068f8a7eSTamas Berghammer 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 85e9547b80SChaoren Lin Interrupt () override; 86e9547b80SChaoren Lin 87e9547b80SChaoren Lin Error 88af245d11STodd Fiala Kill () override; 89af245d11STodd Fiala 90af245d11STodd Fiala Error 91af245d11STodd Fiala GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override; 92af245d11STodd Fiala 93af245d11STodd Fiala Error 943eb4b458SChaoren Lin ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; 95af245d11STodd Fiala 96af245d11STodd Fiala Error 973eb4b458SChaoren Lin ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; 98af245d11STodd Fiala 99af245d11STodd Fiala Error 1003eb4b458SChaoren Lin WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override; 1013eb4b458SChaoren Lin 1023eb4b458SChaoren Lin Error 1033eb4b458SChaoren Lin AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override; 104af245d11STodd Fiala 105af245d11STodd Fiala Error 106af245d11STodd Fiala DeallocateMemory (lldb::addr_t addr) override; 107af245d11STodd Fiala 108af245d11STodd Fiala lldb::addr_t 109af245d11STodd Fiala GetSharedLibraryInfoAddress () override; 110af245d11STodd Fiala 111af245d11STodd Fiala size_t 112af245d11STodd Fiala UpdateThreads () override; 113af245d11STodd Fiala 114af245d11STodd Fiala bool 115af245d11STodd Fiala GetArchitecture (ArchSpec &arch) const override; 116af245d11STodd Fiala 117af245d11STodd Fiala Error 118af245d11STodd Fiala SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override; 119af245d11STodd Fiala 12045f5cb31SPavel Labath Error 12145f5cb31SPavel Labath SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override; 12245f5cb31SPavel Labath 12345f5cb31SPavel Labath Error 12445f5cb31SPavel Labath RemoveWatchpoint (lldb::addr_t addr) override; 12545f5cb31SPavel Labath 126af245d11STodd Fiala void 127af245d11STodd Fiala DoStopIDBumped (uint32_t newBumpId) override; 128af245d11STodd Fiala 1298bc34f4dSOleksiy Vyalov void 1308bc34f4dSOleksiy Vyalov Terminate () override; 1318bc34f4dSOleksiy Vyalov 132068f8a7eSTamas Berghammer Error 133068f8a7eSTamas Berghammer GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override; 134068f8a7eSTamas Berghammer 135783bfc8cSTamas Berghammer Error 136783bfc8cSTamas Berghammer GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) override; 137783bfc8cSTamas Berghammer 138af245d11STodd Fiala // --------------------------------------------------------------------- 139af245d11STodd Fiala // Interface used by NativeRegisterContext-derived classes. 140af245d11STodd Fiala // --------------------------------------------------------------------- 14197ccc294SChaoren Lin Error 142*c7512fdcSPavel Labath DoOperation(const Operation &op); 143068f8a7eSTamas Berghammer 144068f8a7eSTamas Berghammer static long 145068f8a7eSTamas Berghammer PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error); 1467cb18bf5STamas Berghammer 147af245d11STodd Fiala protected: 148af245d11STodd Fiala // --------------------------------------------------------------------- 149af245d11STodd Fiala // NativeProcessProtocol protected interface 150af245d11STodd Fiala // --------------------------------------------------------------------- 151af245d11STodd Fiala Error 152af245d11STodd Fiala GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override; 153af245d11STodd Fiala 154af245d11STodd Fiala private: 155af245d11STodd Fiala 1561107b5a5SPavel Labath class Monitor; 1571107b5a5SPavel Labath 158db264a6dSTamas Berghammer ArchSpec m_arch; 159af245d11STodd Fiala 1601107b5a5SPavel Labath std::unique_ptr<Monitor> m_monitor_up; 161af245d11STodd Fiala 162db264a6dSTamas Berghammer LazyBool m_supports_mem_region; 163af245d11STodd Fiala std::vector<MemoryRegionInfo> m_mem_region_cache; 164db264a6dSTamas Berghammer Mutex m_mem_region_cache_mutex; 165af245d11STodd Fiala 166d8c338d4STamas Berghammer // List of thread ids stepping with a breakpoint with the address of 167d8c338d4STamas Berghammer // the relevan breakpoint 168d8c338d4STamas Berghammer std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint; 169d8c338d4STamas Berghammer 170af245d11STodd Fiala /// @class LauchArgs 171af245d11STodd Fiala /// 172af245d11STodd Fiala /// @brief Simple structure to pass data to the thread responsible for 173af245d11STodd Fiala /// launching a child process. 174bd7cbc5aSPavel Labath struct LaunchArgs 175af245d11STodd Fiala { 176bd7cbc5aSPavel Labath LaunchArgs(Module *module, 177af245d11STodd Fiala char const **argv, 178af245d11STodd Fiala char const **envp, 179d3173f34SChaoren Lin const FileSpec &stdin_file_spec, 180d3173f34SChaoren Lin const FileSpec &stdout_file_spec, 181d3173f34SChaoren Lin const FileSpec &stderr_file_spec, 182d3173f34SChaoren Lin const FileSpec &working_dir, 183db264a6dSTamas Berghammer const ProcessLaunchInfo &launch_info); 184af245d11STodd Fiala 185af245d11STodd Fiala ~LaunchArgs(); 186af245d11STodd Fiala 187db264a6dSTamas Berghammer Module *m_module; // The executable image to launch. 188af245d11STodd Fiala char const **m_argv; // Process arguments. 189af245d11STodd Fiala char const **m_envp; // Process environment. 190d3173f34SChaoren Lin const FileSpec m_stdin_file_spec; // Redirect stdin if not empty. 191d3173f34SChaoren Lin const FileSpec m_stdout_file_spec; // Redirect stdout if not empty. 192d3173f34SChaoren Lin const FileSpec m_stderr_file_spec; // Redirect stderr if not empty. 193d3173f34SChaoren Lin const FileSpec m_working_dir; // Working directory or empty. 194db264a6dSTamas Berghammer const ProcessLaunchInfo &m_launch_info; 195af245d11STodd Fiala }; 196af245d11STodd Fiala 197bd7cbc5aSPavel Labath typedef std::function<::pid_t(Error &)> InitialOperation; 198af245d11STodd Fiala 199af245d11STodd Fiala // --------------------------------------------------------------------- 200af245d11STodd Fiala // Private Instance Methods 201af245d11STodd Fiala // --------------------------------------------------------------------- 202af245d11STodd Fiala NativeProcessLinux (); 203af245d11STodd Fiala 204af245d11STodd Fiala /// Launches an inferior process ready for debugging. Forms the 205af245d11STodd Fiala /// implementation of Process::DoLaunch. 206af245d11STodd Fiala void 207af245d11STodd Fiala LaunchInferior ( 208af245d11STodd Fiala Module *module, 209af245d11STodd Fiala char const *argv[], 210af245d11STodd Fiala char const *envp[], 211d3173f34SChaoren Lin const FileSpec &stdin_file_spec, 212d3173f34SChaoren Lin const FileSpec &stdout_file_spec, 213d3173f34SChaoren Lin const FileSpec &stderr_file_spec, 214d3173f34SChaoren Lin const FileSpec &working_dir, 215db264a6dSTamas Berghammer const ProcessLaunchInfo &launch_info, 216af245d11STodd Fiala Error &error); 217af245d11STodd Fiala 218af245d11STodd Fiala /// Attaches to an existing process. Forms the 2190cbf0b13STamas Berghammer /// implementation of Process::DoAttach 220af245d11STodd Fiala void 221af245d11STodd Fiala AttachToInferior (lldb::pid_t pid, Error &error); 222af245d11STodd Fiala 223af245d11STodd Fiala void 224bd7cbc5aSPavel Labath StartMonitorThread(const InitialOperation &operation, Error &error); 2251107b5a5SPavel Labath 226bd7cbc5aSPavel Labath ::pid_t 227bd7cbc5aSPavel Labath Launch(LaunchArgs *args, Error &error); 228af245d11STodd Fiala 229bd7cbc5aSPavel Labath ::pid_t 230bd7cbc5aSPavel Labath Attach(lldb::pid_t pid, Error &error); 231af245d11STodd Fiala 23297ccc294SChaoren Lin static Error 233af245d11STodd Fiala SetDefaultPtraceOpts(const lldb::pid_t); 234af245d11STodd Fiala 235af245d11STodd Fiala static bool 236d3173f34SChaoren Lin DupDescriptor(const FileSpec &file_spec, int fd, int flags); 237af245d11STodd Fiala 2381107b5a5SPavel Labath static void * 2391107b5a5SPavel Labath MonitorThread(void *baton); 2401107b5a5SPavel Labath 2411107b5a5SPavel Labath void 2421107b5a5SPavel Labath MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status); 243af245d11STodd Fiala 244af245d11STodd Fiala void 245426bdf88SPavel Labath WaitForNewThread(::pid_t tid); 246426bdf88SPavel Labath 247426bdf88SPavel Labath void 248af245d11STodd Fiala MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid); 249af245d11STodd Fiala 250af245d11STodd Fiala void 251c16f5dcaSChaoren Lin MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp); 252c16f5dcaSChaoren Lin 253c16f5dcaSChaoren Lin void 254c16f5dcaSChaoren Lin MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp); 255c16f5dcaSChaoren Lin 256c16f5dcaSChaoren Lin void 257c16f5dcaSChaoren Lin MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index); 258c16f5dcaSChaoren Lin 259c16f5dcaSChaoren Lin void 260af245d11STodd Fiala MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited); 261af245d11STodd Fiala 262e7708688STamas Berghammer bool 263e7708688STamas Berghammer SupportHardwareSingleStepping() const; 264e7708688STamas Berghammer 265e7708688STamas Berghammer Error 266e7708688STamas Berghammer SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp); 267e7708688STamas Berghammer 268af245d11STodd Fiala #if 0 269af245d11STodd Fiala static ::ProcessMessage::CrashReason 270af245d11STodd Fiala GetCrashReasonForSIGSEGV(const siginfo_t *info); 271af245d11STodd Fiala 272af245d11STodd Fiala static ::ProcessMessage::CrashReason 273af245d11STodd Fiala GetCrashReasonForSIGILL(const siginfo_t *info); 274af245d11STodd Fiala 275af245d11STodd Fiala static ::ProcessMessage::CrashReason 276af245d11STodd Fiala GetCrashReasonForSIGFPE(const siginfo_t *info); 277af245d11STodd Fiala 278af245d11STodd Fiala static ::ProcessMessage::CrashReason 279af245d11STodd Fiala GetCrashReasonForSIGBUS(const siginfo_t *info); 280af245d11STodd Fiala #endif 281af245d11STodd Fiala 282af245d11STodd Fiala bool 283af245d11STodd Fiala HasThreadNoLock (lldb::tid_t thread_id); 284af245d11STodd Fiala 285af245d11STodd Fiala NativeThreadProtocolSP 286af245d11STodd Fiala MaybeGetThreadNoLock (lldb::tid_t thread_id); 287af245d11STodd Fiala 288af245d11STodd Fiala bool 289af245d11STodd Fiala StopTrackingThread (lldb::tid_t thread_id); 290af245d11STodd Fiala 291af245d11STodd Fiala NativeThreadProtocolSP 292af245d11STodd Fiala AddThread (lldb::tid_t thread_id); 293af245d11STodd Fiala 294af245d11STodd Fiala Error 29563c8be95STamas Berghammer GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size); 296af245d11STodd Fiala 297af245d11STodd Fiala Error 298af245d11STodd Fiala FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp); 299af245d11STodd Fiala 300af245d11STodd Fiala /// Writes a siginfo_t structure corresponding to the given thread ID to the 301af245d11STodd Fiala /// memory region pointed to by @p siginfo. 30297ccc294SChaoren Lin Error 30397ccc294SChaoren Lin GetSignalInfo(lldb::tid_t tid, void *siginfo); 304af245d11STodd Fiala 305af245d11STodd Fiala /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 306af245d11STodd Fiala /// corresponding to the given thread ID to the memory pointed to by @p 307af245d11STodd Fiala /// message. 30897ccc294SChaoren Lin Error 309af245d11STodd Fiala GetEventMessage(lldb::tid_t tid, unsigned long *message); 310af245d11STodd Fiala 311af245d11STodd Fiala /// Resumes the given thread. If @p signo is anything but 312af245d11STodd Fiala /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 31397ccc294SChaoren Lin Error 314af245d11STodd Fiala Resume(lldb::tid_t tid, uint32_t signo); 315af245d11STodd Fiala 316af245d11STodd Fiala /// Single steps the given thread. If @p signo is anything but 317af245d11STodd Fiala /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 31897ccc294SChaoren Lin Error 319af245d11STodd Fiala SingleStep(lldb::tid_t tid, uint32_t signo); 320af245d11STodd Fiala 321511e5cdcSTodd Fiala void 322fa03ad2eSChaoren Lin NotifyThreadDeath (lldb::tid_t tid); 323fa03ad2eSChaoren Lin 324db264a6dSTamas Berghammer Error 325af245d11STodd Fiala Detach(lldb::tid_t tid); 32686fd8e45SChaoren Lin 327c076559aSPavel Labath 328c076559aSPavel Labath // Typedefs. 329c076559aSPavel Labath typedef std::unordered_set<lldb::tid_t> ThreadIDSet; 330c076559aSPavel Labath 3311dbc6c9cSPavel Labath // This method is requests a stop on all threads which are still running. It sets up a 3321dbc6c9cSPavel Labath // deferred delegate notification, which will fire once threads report as stopped. The 3331dbc6c9cSPavel Labath // triggerring_tid will be set as the current thread (main stop reason). 334c076559aSPavel Labath void 335337f3eb9SPavel Labath StopRunningThreads(lldb::tid_t triggering_tid); 336c076559aSPavel Labath 337c076559aSPavel Labath struct PendingNotification 338c076559aSPavel Labath { 339337f3eb9SPavel Labath PendingNotification (lldb::tid_t triggering_tid): 340337f3eb9SPavel Labath triggering_tid (triggering_tid), 341108c325dSPavel Labath wait_for_stop_tids () 342337f3eb9SPavel Labath { 343337f3eb9SPavel Labath } 344337f3eb9SPavel Labath 345c076559aSPavel Labath const lldb::tid_t triggering_tid; 346c076559aSPavel Labath ThreadIDSet wait_for_stop_tids; 347c076559aSPavel Labath }; 348c076559aSPavel Labath typedef std::unique_ptr<PendingNotification> PendingNotificationUP; 349c076559aSPavel Labath 3509eb1ecb9SPavel Labath // Notify the delegate if all threads have stopped. 3519eb1ecb9SPavel Labath void SignalIfAllThreadsStopped(); 352c076559aSPavel Labath 353c076559aSPavel Labath void 354c076559aSPavel Labath RequestStopOnAllRunningThreads(); 355c076559aSPavel Labath 3565eb721edSPavel Labath Error 3575eb721edSPavel Labath ThreadDidStop(lldb::tid_t tid, bool initiated_by_llgs); 358c076559aSPavel Labath 3591dbc6c9cSPavel Labath // Resume the thread with the given thread id using the request_thread_resume_function 3601dbc6c9cSPavel Labath // called. If error_when_already_running is then then an error is raised if we think this 3611dbc6c9cSPavel Labath // thread is already running. 3625eb721edSPavel Labath Error 3631dbc6c9cSPavel Labath ResumeThread(lldb::tid_t tid, NativeThreadLinux::ResumeThreadFunction request_thread_resume_function, 3645eb721edSPavel Labath bool error_when_already_running); 365c076559aSPavel Labath 366c076559aSPavel Labath void 367ed89c7feSPavel Labath DoStopThreads(PendingNotificationUP &¬ification_up); 368c076559aSPavel Labath 369c076559aSPavel Labath void 3708c8ff7afSPavel Labath ThreadWasCreated (lldb::tid_t tid); 371c076559aSPavel Labath 372c076559aSPavel Labath // Member variables. 373c076559aSPavel Labath PendingNotificationUP m_pending_notification_up; 374af245d11STodd Fiala }; 375db264a6dSTamas Berghammer 376db264a6dSTamas Berghammer } // namespace process_linux 377db264a6dSTamas Berghammer } // namespace lldb_private 378af245d11STodd Fiala 379af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_ 380