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