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 66c7512fdcSPavel Labath /// task. 67c7512fdcSPavel 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 142c7512fdcSPavel Labath DoOperation(const Operation &op); 143068f8a7eSTamas Berghammer 144*4a9babb2SPavel Labath static Error 145*4a9babb2SPavel Labath PtraceWrapper(int req, 146*4a9babb2SPavel Labath lldb::pid_t pid, 147*4a9babb2SPavel Labath void *addr = nullptr, 148*4a9babb2SPavel Labath void *data = nullptr, 149*4a9babb2SPavel Labath size_t data_size = 0, 150*4a9babb2SPavel Labath long *result = nullptr); 1517cb18bf5STamas Berghammer 152af245d11STodd Fiala protected: 153af245d11STodd Fiala // --------------------------------------------------------------------- 154af245d11STodd Fiala // NativeProcessProtocol protected interface 155af245d11STodd Fiala // --------------------------------------------------------------------- 156af245d11STodd Fiala Error 157af245d11STodd Fiala GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override; 158af245d11STodd Fiala 159af245d11STodd Fiala private: 160af245d11STodd Fiala 1611107b5a5SPavel Labath class Monitor; 1621107b5a5SPavel Labath 163db264a6dSTamas Berghammer ArchSpec m_arch; 164af245d11STodd Fiala 1651107b5a5SPavel Labath std::unique_ptr<Monitor> m_monitor_up; 166af245d11STodd Fiala 167db264a6dSTamas Berghammer LazyBool m_supports_mem_region; 168af245d11STodd Fiala std::vector<MemoryRegionInfo> m_mem_region_cache; 169db264a6dSTamas Berghammer Mutex m_mem_region_cache_mutex; 170af245d11STodd Fiala 171d8c338d4STamas Berghammer // List of thread ids stepping with a breakpoint with the address of 172d8c338d4STamas Berghammer // the relevan breakpoint 173d8c338d4STamas Berghammer std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint; 174d8c338d4STamas Berghammer 175af245d11STodd Fiala /// @class LauchArgs 176af245d11STodd Fiala /// 177af245d11STodd Fiala /// @brief Simple structure to pass data to the thread responsible for 178af245d11STodd Fiala /// launching a child process. 179bd7cbc5aSPavel Labath struct LaunchArgs 180af245d11STodd Fiala { 181bd7cbc5aSPavel Labath LaunchArgs(Module *module, 182af245d11STodd Fiala char const **argv, 183af245d11STodd Fiala char const **envp, 184d3173f34SChaoren Lin const FileSpec &stdin_file_spec, 185d3173f34SChaoren Lin const FileSpec &stdout_file_spec, 186d3173f34SChaoren Lin const FileSpec &stderr_file_spec, 187d3173f34SChaoren Lin const FileSpec &working_dir, 188db264a6dSTamas Berghammer const ProcessLaunchInfo &launch_info); 189af245d11STodd Fiala 190af245d11STodd Fiala ~LaunchArgs(); 191af245d11STodd Fiala 192db264a6dSTamas Berghammer Module *m_module; // The executable image to launch. 193af245d11STodd Fiala char const **m_argv; // Process arguments. 194af245d11STodd Fiala char const **m_envp; // Process environment. 195d3173f34SChaoren Lin const FileSpec m_stdin_file_spec; // Redirect stdin if not empty. 196d3173f34SChaoren Lin const FileSpec m_stdout_file_spec; // Redirect stdout if not empty. 197d3173f34SChaoren Lin const FileSpec m_stderr_file_spec; // Redirect stderr if not empty. 198d3173f34SChaoren Lin const FileSpec m_working_dir; // Working directory or empty. 199db264a6dSTamas Berghammer const ProcessLaunchInfo &m_launch_info; 200af245d11STodd Fiala }; 201af245d11STodd Fiala 202bd7cbc5aSPavel Labath typedef std::function<::pid_t(Error &)> InitialOperation; 203af245d11STodd Fiala 204af245d11STodd Fiala // --------------------------------------------------------------------- 205af245d11STodd Fiala // Private Instance Methods 206af245d11STodd Fiala // --------------------------------------------------------------------- 207af245d11STodd Fiala NativeProcessLinux (); 208af245d11STodd Fiala 209af245d11STodd Fiala /// Launches an inferior process ready for debugging. Forms the 210af245d11STodd Fiala /// implementation of Process::DoLaunch. 211af245d11STodd Fiala void 212af245d11STodd Fiala LaunchInferior ( 213af245d11STodd Fiala Module *module, 214af245d11STodd Fiala char const *argv[], 215af245d11STodd Fiala char const *envp[], 216d3173f34SChaoren Lin const FileSpec &stdin_file_spec, 217d3173f34SChaoren Lin const FileSpec &stdout_file_spec, 218d3173f34SChaoren Lin const FileSpec &stderr_file_spec, 219d3173f34SChaoren Lin const FileSpec &working_dir, 220db264a6dSTamas Berghammer const ProcessLaunchInfo &launch_info, 221af245d11STodd Fiala Error &error); 222af245d11STodd Fiala 223af245d11STodd Fiala /// Attaches to an existing process. Forms the 2240cbf0b13STamas Berghammer /// implementation of Process::DoAttach 225af245d11STodd Fiala void 226af245d11STodd Fiala AttachToInferior (lldb::pid_t pid, Error &error); 227af245d11STodd Fiala 228af245d11STodd Fiala void 229bd7cbc5aSPavel Labath StartMonitorThread(const InitialOperation &operation, Error &error); 2301107b5a5SPavel Labath 231bd7cbc5aSPavel Labath ::pid_t 232bd7cbc5aSPavel Labath Launch(LaunchArgs *args, Error &error); 233af245d11STodd Fiala 234bd7cbc5aSPavel Labath ::pid_t 235bd7cbc5aSPavel Labath Attach(lldb::pid_t pid, Error &error); 236af245d11STodd Fiala 23797ccc294SChaoren Lin static Error 238af245d11STodd Fiala SetDefaultPtraceOpts(const lldb::pid_t); 239af245d11STodd Fiala 240af245d11STodd Fiala static bool 241d3173f34SChaoren Lin DupDescriptor(const FileSpec &file_spec, int fd, int flags); 242af245d11STodd Fiala 2431107b5a5SPavel Labath static void * 2441107b5a5SPavel Labath MonitorThread(void *baton); 2451107b5a5SPavel Labath 2461107b5a5SPavel Labath void 2471107b5a5SPavel Labath MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status); 248af245d11STodd Fiala 249af245d11STodd Fiala void 250426bdf88SPavel Labath WaitForNewThread(::pid_t tid); 251426bdf88SPavel Labath 252426bdf88SPavel Labath void 253af245d11STodd Fiala MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid); 254af245d11STodd Fiala 255af245d11STodd Fiala void 256c16f5dcaSChaoren Lin MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp); 257c16f5dcaSChaoren Lin 258c16f5dcaSChaoren Lin void 259c16f5dcaSChaoren Lin MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp); 260c16f5dcaSChaoren Lin 261c16f5dcaSChaoren Lin void 262c16f5dcaSChaoren Lin MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index); 263c16f5dcaSChaoren Lin 264c16f5dcaSChaoren Lin void 265af245d11STodd Fiala MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited); 266af245d11STodd Fiala 267e7708688STamas Berghammer bool 268e7708688STamas Berghammer SupportHardwareSingleStepping() const; 269e7708688STamas Berghammer 270e7708688STamas Berghammer Error 271e7708688STamas Berghammer SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp); 272e7708688STamas Berghammer 273af245d11STodd Fiala #if 0 274af245d11STodd Fiala static ::ProcessMessage::CrashReason 275af245d11STodd Fiala GetCrashReasonForSIGSEGV(const siginfo_t *info); 276af245d11STodd Fiala 277af245d11STodd Fiala static ::ProcessMessage::CrashReason 278af245d11STodd Fiala GetCrashReasonForSIGILL(const siginfo_t *info); 279af245d11STodd Fiala 280af245d11STodd Fiala static ::ProcessMessage::CrashReason 281af245d11STodd Fiala GetCrashReasonForSIGFPE(const siginfo_t *info); 282af245d11STodd Fiala 283af245d11STodd Fiala static ::ProcessMessage::CrashReason 284af245d11STodd Fiala GetCrashReasonForSIGBUS(const siginfo_t *info); 285af245d11STodd Fiala #endif 286af245d11STodd Fiala 287af245d11STodd Fiala bool 288af245d11STodd Fiala HasThreadNoLock (lldb::tid_t thread_id); 289af245d11STodd Fiala 290af245d11STodd Fiala NativeThreadProtocolSP 291af245d11STodd Fiala MaybeGetThreadNoLock (lldb::tid_t thread_id); 292af245d11STodd Fiala 293af245d11STodd Fiala bool 294af245d11STodd Fiala StopTrackingThread (lldb::tid_t thread_id); 295af245d11STodd Fiala 296af245d11STodd Fiala NativeThreadProtocolSP 297af245d11STodd Fiala AddThread (lldb::tid_t thread_id); 298af245d11STodd Fiala 299af245d11STodd Fiala Error 30063c8be95STamas Berghammer GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size); 301af245d11STodd Fiala 302af245d11STodd Fiala Error 303af245d11STodd Fiala FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp); 304af245d11STodd Fiala 305af245d11STodd Fiala /// Writes a siginfo_t structure corresponding to the given thread ID to the 306af245d11STodd Fiala /// memory region pointed to by @p siginfo. 30797ccc294SChaoren Lin Error 30897ccc294SChaoren Lin GetSignalInfo(lldb::tid_t tid, void *siginfo); 309af245d11STodd Fiala 310af245d11STodd Fiala /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 311af245d11STodd Fiala /// corresponding to the given thread ID to the memory pointed to by @p 312af245d11STodd Fiala /// message. 31397ccc294SChaoren Lin Error 314af245d11STodd Fiala GetEventMessage(lldb::tid_t tid, unsigned long *message); 315af245d11STodd Fiala 316af245d11STodd Fiala /// Resumes 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 Resume(lldb::tid_t tid, uint32_t signo); 320af245d11STodd Fiala 321af245d11STodd Fiala /// Single steps the given thread. If @p signo is anything but 322af245d11STodd Fiala /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 32397ccc294SChaoren Lin Error 324af245d11STodd Fiala SingleStep(lldb::tid_t tid, uint32_t signo); 325af245d11STodd Fiala 326511e5cdcSTodd Fiala void 327fa03ad2eSChaoren Lin NotifyThreadDeath (lldb::tid_t tid); 328fa03ad2eSChaoren Lin 329db264a6dSTamas Berghammer Error 330af245d11STodd Fiala Detach(lldb::tid_t tid); 33186fd8e45SChaoren Lin 332c076559aSPavel Labath 333c076559aSPavel Labath // Typedefs. 334c076559aSPavel Labath typedef std::unordered_set<lldb::tid_t> ThreadIDSet; 335c076559aSPavel Labath 3361dbc6c9cSPavel Labath // This method is requests a stop on all threads which are still running. It sets up a 3371dbc6c9cSPavel Labath // deferred delegate notification, which will fire once threads report as stopped. The 3381dbc6c9cSPavel Labath // triggerring_tid will be set as the current thread (main stop reason). 339c076559aSPavel Labath void 340337f3eb9SPavel Labath StopRunningThreads(lldb::tid_t triggering_tid); 341c076559aSPavel Labath 342c076559aSPavel Labath struct PendingNotification 343c076559aSPavel Labath { 344337f3eb9SPavel Labath PendingNotification (lldb::tid_t triggering_tid): 345337f3eb9SPavel Labath triggering_tid (triggering_tid), 346108c325dSPavel Labath wait_for_stop_tids () 347337f3eb9SPavel Labath { 348337f3eb9SPavel Labath } 349337f3eb9SPavel Labath 350c076559aSPavel Labath const lldb::tid_t triggering_tid; 351c076559aSPavel Labath ThreadIDSet wait_for_stop_tids; 352c076559aSPavel Labath }; 353c076559aSPavel Labath typedef std::unique_ptr<PendingNotification> PendingNotificationUP; 354c076559aSPavel Labath 3559eb1ecb9SPavel Labath // Notify the delegate if all threads have stopped. 3569eb1ecb9SPavel Labath void SignalIfAllThreadsStopped(); 357c076559aSPavel Labath 358c076559aSPavel Labath void 359c076559aSPavel Labath RequestStopOnAllRunningThreads(); 360c076559aSPavel Labath 3615eb721edSPavel Labath Error 3625eb721edSPavel Labath ThreadDidStop(lldb::tid_t tid, bool initiated_by_llgs); 363c076559aSPavel Labath 3641dbc6c9cSPavel Labath // Resume the thread with the given thread id using the request_thread_resume_function 3651dbc6c9cSPavel Labath // called. If error_when_already_running is then then an error is raised if we think this 3661dbc6c9cSPavel Labath // thread is already running. 3675eb721edSPavel Labath Error 3681dbc6c9cSPavel Labath ResumeThread(lldb::tid_t tid, NativeThreadLinux::ResumeThreadFunction request_thread_resume_function, 3695eb721edSPavel Labath bool error_when_already_running); 370c076559aSPavel Labath 371c076559aSPavel Labath void 372ed89c7feSPavel Labath DoStopThreads(PendingNotificationUP &¬ification_up); 373c076559aSPavel Labath 374c076559aSPavel Labath void 3758c8ff7afSPavel Labath ThreadWasCreated (lldb::tid_t tid); 376c076559aSPavel Labath 377c076559aSPavel Labath // Member variables. 378c076559aSPavel Labath PendingNotificationUP m_pending_notification_up; 379af245d11STodd Fiala }; 380db264a6dSTamas Berghammer 381db264a6dSTamas Berghammer } // namespace process_linux 382db264a6dSTamas Berghammer } // namespace lldb_private 383af245d11STodd Fiala 384af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_ 385