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