1 //===-- NativeProcessLinux.h ---------------------------------- -*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_NativeProcessLinux_H_ 11 #define liblldb_NativeProcessLinux_H_ 12 13 // C++ Includes 14 #include <unordered_set> 15 16 // Other libraries and framework includes 17 #include "lldb/Core/ArchSpec.h" 18 #include "lldb/Host/Debug.h" 19 #include "lldb/Host/FileSpec.h" 20 #include "lldb/Host/HostThread.h" 21 #include "lldb/Target/MemoryRegionInfo.h" 22 #include "lldb/lldb-types.h" 23 24 #include "NativeThreadLinux.h" 25 #include "lldb/Host/common/NativeProcessProtocol.h" 26 27 namespace lldb_private { 28 class Error; 29 class Scalar; 30 31 namespace process_linux { 32 /// @class NativeProcessLinux 33 /// @brief Manages communication with the inferior (debugee) process. 34 /// 35 /// Upon construction, this class prepares and launches an inferior process for 36 /// debugging. 37 /// 38 /// Changes in the inferior process state are broadcasted. 39 class NativeProcessLinux : public NativeProcessProtocol { 40 friend Error NativeProcessProtocol::Launch( 41 ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate, 42 MainLoop &mainloop, NativeProcessProtocolSP &process_sp); 43 44 friend Error NativeProcessProtocol::Attach( 45 lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 46 MainLoop &mainloop, NativeProcessProtocolSP &process_sp); 47 48 public: 49 // --------------------------------------------------------------------- 50 // NativeProcessProtocol Interface 51 // --------------------------------------------------------------------- 52 Error Resume(const ResumeActionList &resume_actions) override; 53 54 Error Halt() override; 55 56 Error Detach() override; 57 58 Error Signal(int signo) override; 59 60 Error Interrupt() override; 61 62 Error Kill() override; 63 64 Error GetMemoryRegionInfo(lldb::addr_t load_addr, 65 MemoryRegionInfo &range_info) override; 66 67 Error ReadMemory(lldb::addr_t addr, void *buf, size_t size, 68 size_t &bytes_read) override; 69 70 Error ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, 71 size_t &bytes_read) override; 72 73 Error WriteMemory(lldb::addr_t addr, const void *buf, size_t size, 74 size_t &bytes_written) override; 75 76 Error AllocateMemory(size_t size, uint32_t permissions, 77 lldb::addr_t &addr) override; 78 79 Error DeallocateMemory(lldb::addr_t addr) override; 80 81 lldb::addr_t GetSharedLibraryInfoAddress() override; 82 83 size_t UpdateThreads() override; 84 85 bool GetArchitecture(ArchSpec &arch) const override; 86 87 Error SetBreakpoint(lldb::addr_t addr, uint32_t size, bool hardware) override; 88 89 void DoStopIDBumped(uint32_t newBumpId) override; 90 91 Error GetLoadedModuleFileSpec(const char *module_path, 92 FileSpec &file_spec) override; 93 94 Error GetFileLoadAddress(const llvm::StringRef &file_name, 95 lldb::addr_t &load_addr) override; 96 97 NativeThreadLinuxSP GetThreadByID(lldb::tid_t id); 98 99 // --------------------------------------------------------------------- 100 // Interface used by NativeRegisterContext-derived classes. 101 // --------------------------------------------------------------------- 102 static Error PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr, 103 void *data = nullptr, size_t data_size = 0, 104 long *result = nullptr); 105 106 bool SupportHardwareSingleStepping() const; 107 108 protected: 109 // --------------------------------------------------------------------- 110 // NativeProcessProtocol protected interface 111 // --------------------------------------------------------------------- 112 Error 113 GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint, 114 size_t &actual_opcode_size, 115 const uint8_t *&trap_opcode_bytes) override; 116 117 private: 118 MainLoop::SignalHandleUP m_sigchld_handle; 119 ArchSpec m_arch; 120 121 LazyBool m_supports_mem_region; 122 std::vector<MemoryRegionInfo> m_mem_region_cache; 123 124 lldb::tid_t m_pending_notification_tid; 125 126 // List of thread ids stepping with a breakpoint with the address of 127 // the relevan breakpoint 128 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint; 129 130 // --------------------------------------------------------------------- 131 // Private Instance Methods 132 // --------------------------------------------------------------------- 133 NativeProcessLinux(); 134 135 Error LaunchInferior(MainLoop &mainloop, ProcessLaunchInfo &launch_info); 136 137 /// Attaches to an existing process. Forms the 138 /// implementation of Process::DoAttach 139 void AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, Error &error); 140 141 ::pid_t Attach(lldb::pid_t pid, Error &error); 142 143 static Error SetDefaultPtraceOpts(const lldb::pid_t); 144 145 static void *MonitorThread(void *baton); 146 147 void MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status); 148 149 void WaitForNewThread(::pid_t tid); 150 151 void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread); 152 153 void MonitorTrace(NativeThreadLinux &thread); 154 155 void MonitorBreakpoint(NativeThreadLinux &thread); 156 157 void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index); 158 159 void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, 160 bool exited); 161 162 Error SetupSoftwareSingleStepping(NativeThreadLinux &thread); 163 164 #if 0 165 static ::ProcessMessage::CrashReason 166 GetCrashReasonForSIGSEGV(const siginfo_t *info); 167 168 static ::ProcessMessage::CrashReason 169 GetCrashReasonForSIGILL(const siginfo_t *info); 170 171 static ::ProcessMessage::CrashReason 172 GetCrashReasonForSIGFPE(const siginfo_t *info); 173 174 static ::ProcessMessage::CrashReason 175 GetCrashReasonForSIGBUS(const siginfo_t *info); 176 #endif 177 178 bool HasThreadNoLock(lldb::tid_t thread_id); 179 180 bool StopTrackingThread(lldb::tid_t thread_id); 181 182 NativeThreadLinuxSP AddThread(lldb::tid_t thread_id); 183 184 Error GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size); 185 186 Error FixupBreakpointPCAsNeeded(NativeThreadLinux &thread); 187 188 /// Writes a siginfo_t structure corresponding to the given thread ID to the 189 /// memory region pointed to by @p siginfo. 190 Error GetSignalInfo(lldb::tid_t tid, void *siginfo); 191 192 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 193 /// corresponding to the given thread ID to the memory pointed to by @p 194 /// message. 195 Error GetEventMessage(lldb::tid_t tid, unsigned long *message); 196 197 void NotifyThreadDeath(lldb::tid_t tid); 198 199 Error Detach(lldb::tid_t tid); 200 201 // This method is requests a stop on all threads which are still running. It 202 // sets up a 203 // deferred delegate notification, which will fire once threads report as 204 // stopped. The 205 // triggerring_tid will be set as the current thread (main stop reason). 206 void StopRunningThreads(lldb::tid_t triggering_tid); 207 208 // Notify the delegate if all threads have stopped. 209 void SignalIfAllThreadsStopped(); 210 211 // Resume the given thread, optionally passing it the given signal. The type 212 // of resume 213 // operation (continue, single-step) depends on the state parameter. 214 Error ResumeThread(NativeThreadLinux &thread, lldb::StateType state, 215 int signo); 216 217 void ThreadWasCreated(NativeThreadLinux &thread); 218 219 void SigchldHandler(); 220 }; 221 222 } // namespace process_linux 223 } // namespace lldb_private 224 225 #endif // #ifndef liblldb_NativeProcessLinux_H_ 226