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