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/lldb-types.h" 19 #include "lldb/Host/Debug.h" 20 #include "lldb/Host/FileSpec.h" 21 #include "lldb/Host/HostThread.h" 22 #include "lldb/Host/Mutex.h" 23 #include "lldb/Target/MemoryRegionInfo.h" 24 25 #include "lldb/Host/common/NativeProcessProtocol.h" 26 #include "NativeThreadLinux.h" 27 28 namespace lldb_private { 29 class Error; 30 class Module; 31 class Scalar; 32 33 namespace process_linux { 34 /// @class NativeProcessLinux 35 /// @brief Manages communication with the inferior (debugee) process. 36 /// 37 /// Upon construction, this class prepares and launches an inferior process for 38 /// debugging. 39 /// 40 /// Changes in the inferior process state are broadcasted. 41 class NativeProcessLinux: public NativeProcessProtocol 42 { 43 friend Error 44 NativeProcessProtocol::Launch (ProcessLaunchInfo &launch_info, 45 NativeDelegate &native_delegate, 46 MainLoop &mainloop, 47 NativeProcessProtocolSP &process_sp); 48 49 friend Error 50 NativeProcessProtocol::Attach (lldb::pid_t pid, 51 NativeProcessProtocol::NativeDelegate &native_delegate, 52 MainLoop &mainloop, 53 NativeProcessProtocolSP &process_sp); 54 55 public: 56 // --------------------------------------------------------------------- 57 // NativeProcessProtocol Interface 58 // --------------------------------------------------------------------- 59 Error 60 Resume (const ResumeActionList &resume_actions) override; 61 62 Error 63 Halt () override; 64 65 Error 66 Detach () override; 67 68 Error 69 Signal (int signo) override; 70 71 Error 72 Interrupt () override; 73 74 Error 75 Kill () override; 76 77 Error 78 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override; 79 80 Error 81 ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; 82 83 Error 84 ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; 85 86 Error 87 WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override; 88 89 Error 90 AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override; 91 92 Error 93 DeallocateMemory (lldb::addr_t addr) override; 94 95 lldb::addr_t 96 GetSharedLibraryInfoAddress () override; 97 98 size_t 99 UpdateThreads () override; 100 101 bool 102 GetArchitecture (ArchSpec &arch) const override; 103 104 Error 105 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override; 106 107 void 108 DoStopIDBumped (uint32_t newBumpId) override; 109 110 Error 111 GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override; 112 113 Error 114 GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) override; 115 116 // --------------------------------------------------------------------- 117 // Interface used by NativeRegisterContext-derived classes. 118 // --------------------------------------------------------------------- 119 static Error 120 PtraceWrapper(int req, 121 lldb::pid_t pid, 122 void *addr = nullptr, 123 void *data = nullptr, 124 size_t data_size = 0, 125 long *result = nullptr); 126 127 protected: 128 // --------------------------------------------------------------------- 129 // NativeProcessProtocol protected interface 130 // --------------------------------------------------------------------- 131 Error 132 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override; 133 134 private: 135 136 MainLoop::SignalHandleUP m_sigchld_handle; 137 ArchSpec m_arch; 138 139 LazyBool m_supports_mem_region; 140 std::vector<MemoryRegionInfo> m_mem_region_cache; 141 Mutex m_mem_region_cache_mutex; 142 143 // List of thread ids stepping with a breakpoint with the address of 144 // the relevan breakpoint 145 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint; 146 147 /// @class LauchArgs 148 /// 149 /// @brief Simple structure to pass data to the thread responsible for 150 /// launching a child process. 151 struct LaunchArgs 152 { 153 LaunchArgs(Module *module, 154 char const **argv, 155 char const **envp, 156 const FileSpec &stdin_file_spec, 157 const FileSpec &stdout_file_spec, 158 const FileSpec &stderr_file_spec, 159 const FileSpec &working_dir, 160 const ProcessLaunchInfo &launch_info); 161 162 ~LaunchArgs(); 163 164 Module *m_module; // The executable image to launch. 165 char const **m_argv; // Process arguments. 166 char const **m_envp; // Process environment. 167 const FileSpec m_stdin_file_spec; // Redirect stdin if not empty. 168 const FileSpec m_stdout_file_spec; // Redirect stdout if not empty. 169 const FileSpec m_stderr_file_spec; // Redirect stderr if not empty. 170 const FileSpec m_working_dir; // Working directory or empty. 171 const ProcessLaunchInfo &m_launch_info; 172 }; 173 174 typedef std::function<::pid_t(Error &)> InitialOperation; 175 176 // --------------------------------------------------------------------- 177 // Private Instance Methods 178 // --------------------------------------------------------------------- 179 NativeProcessLinux (); 180 181 /// Launches an inferior process ready for debugging. Forms the 182 /// implementation of Process::DoLaunch. 183 void 184 LaunchInferior ( 185 MainLoop &mainloop, 186 Module *module, 187 char const *argv[], 188 char const *envp[], 189 const FileSpec &stdin_file_spec, 190 const FileSpec &stdout_file_spec, 191 const FileSpec &stderr_file_spec, 192 const FileSpec &working_dir, 193 const ProcessLaunchInfo &launch_info, 194 Error &error); 195 196 /// Attaches to an existing process. Forms the 197 /// implementation of Process::DoAttach 198 void 199 AttachToInferior (MainLoop &mainloop, lldb::pid_t pid, Error &error); 200 201 ::pid_t 202 Launch(LaunchArgs *args, Error &error); 203 204 ::pid_t 205 Attach(lldb::pid_t pid, Error &error); 206 207 static Error 208 SetDefaultPtraceOpts(const lldb::pid_t); 209 210 static bool 211 DupDescriptor(const FileSpec &file_spec, int fd, int flags); 212 213 static void * 214 MonitorThread(void *baton); 215 216 void 217 MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status); 218 219 void 220 WaitForNewThread(::pid_t tid); 221 222 void 223 MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid); 224 225 void 226 MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp); 227 228 void 229 MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp); 230 231 void 232 MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index); 233 234 void 235 MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited); 236 237 bool 238 SupportHardwareSingleStepping() const; 239 240 Error 241 SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp); 242 243 #if 0 244 static ::ProcessMessage::CrashReason 245 GetCrashReasonForSIGSEGV(const siginfo_t *info); 246 247 static ::ProcessMessage::CrashReason 248 GetCrashReasonForSIGILL(const siginfo_t *info); 249 250 static ::ProcessMessage::CrashReason 251 GetCrashReasonForSIGFPE(const siginfo_t *info); 252 253 static ::ProcessMessage::CrashReason 254 GetCrashReasonForSIGBUS(const siginfo_t *info); 255 #endif 256 257 bool 258 HasThreadNoLock (lldb::tid_t thread_id); 259 260 NativeThreadProtocolSP 261 MaybeGetThreadNoLock (lldb::tid_t thread_id); 262 263 bool 264 StopTrackingThread (lldb::tid_t thread_id); 265 266 NativeThreadProtocolSP 267 AddThread (lldb::tid_t thread_id); 268 269 Error 270 GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size); 271 272 Error 273 FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp); 274 275 /// Writes a siginfo_t structure corresponding to the given thread ID to the 276 /// memory region pointed to by @p siginfo. 277 Error 278 GetSignalInfo(lldb::tid_t tid, void *siginfo); 279 280 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 281 /// corresponding to the given thread ID to the memory pointed to by @p 282 /// message. 283 Error 284 GetEventMessage(lldb::tid_t tid, unsigned long *message); 285 286 /// Resumes the given thread. If @p signo is anything but 287 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 288 Error 289 Resume(lldb::tid_t tid, uint32_t signo); 290 291 /// Single steps the given thread. If @p signo is anything but 292 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 293 Error 294 SingleStep(lldb::tid_t tid, uint32_t signo); 295 296 void 297 NotifyThreadDeath (lldb::tid_t tid); 298 299 Error 300 Detach(lldb::tid_t tid); 301 302 303 // Typedefs. 304 typedef std::unordered_set<lldb::tid_t> ThreadIDSet; 305 306 // This method is requests a stop on all threads which are still running. It sets up a 307 // deferred delegate notification, which will fire once threads report as stopped. The 308 // triggerring_tid will be set as the current thread (main stop reason). 309 void 310 StopRunningThreads(lldb::tid_t triggering_tid); 311 312 struct PendingNotification 313 { 314 PendingNotification (lldb::tid_t triggering_tid): 315 triggering_tid (triggering_tid), 316 wait_for_stop_tids () 317 { 318 } 319 320 const lldb::tid_t triggering_tid; 321 ThreadIDSet wait_for_stop_tids; 322 }; 323 typedef std::unique_ptr<PendingNotification> PendingNotificationUP; 324 325 // Notify the delegate if all threads have stopped. 326 void SignalIfAllThreadsStopped(); 327 328 void 329 RequestStopOnAllRunningThreads(); 330 331 Error 332 ThreadDidStop(lldb::tid_t tid, bool initiated_by_llgs); 333 334 // Resume the thread with the given thread id using the request_thread_resume_function 335 // called. If error_when_already_running is then then an error is raised if we think this 336 // thread is already running. 337 Error 338 ResumeThread(lldb::tid_t tid, NativeThreadLinux::ResumeThreadFunction request_thread_resume_function, 339 bool error_when_already_running); 340 341 void 342 DoStopThreads(PendingNotificationUP &¬ification_up); 343 344 void 345 ThreadWasCreated (lldb::tid_t tid); 346 347 void 348 SigchldHandler(); 349 350 // Member variables. 351 PendingNotificationUP m_pending_notification_up; 352 }; 353 354 } // namespace process_linux 355 } // namespace lldb_private 356 357 #endif // #ifndef liblldb_NativeProcessLinux_H_ 358