1 //===-- NativeProcessDarwin.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 NativeProcessDarwin_h 11 #define NativeProcessDarwin_h 12 13 // NOTE: this code should only be compiled on Apple Darwin systems. It is 14 // not cross-platform code and is not intended to build on any other platform. 15 // Therefore, platform-specific headers and code are okay here. 16 17 // C includes 18 #include <mach/mach_types.h> 19 20 // C++ includes 21 #include <mutex> 22 #include <unordered_set> 23 24 // Other libraries and framework includes 25 #include "lldb/Core/ArchSpec.h" 26 #include "lldb/Host/Debug.h" 27 #include "lldb/Host/HostThread.h" 28 #include "lldb/Host/Pipe.h" 29 #include "lldb/Host/common/NativeProcessProtocol.h" 30 #include "lldb/Target/MemoryRegionInfo.h" 31 #include "lldb/Utility/FileSpec.h" 32 #include "lldb/lldb-types.h" 33 34 #include "LaunchFlavor.h" 35 #include "MachException.h" 36 #include "NativeThreadDarwin.h" 37 #include "NativeThreadListDarwin.h" 38 39 namespace lldb_private { 40 class Status; 41 class Scalar; 42 43 namespace process_darwin { 44 45 /// @class NativeProcessDarwin 46 /// @brief Manages communication with the inferior (debugee) process. 47 /// 48 /// Upon construction, this class prepares and launches an inferior 49 /// process for debugging. 50 /// 51 /// Changes in the inferior process state are broadcasted. 52 class NativeProcessDarwin : public NativeProcessProtocol { 53 friend Status NativeProcessProtocol::Launch( 54 ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate, 55 MainLoop &mainloop, NativeProcessProtocolSP &process_sp); 56 57 friend Status NativeProcessProtocol::Attach( 58 lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 59 MainLoop &mainloop, NativeProcessProtocolSP &process_sp); 60 61 public: 62 ~NativeProcessDarwin() override; 63 64 // ----------------------------------------------------------------- 65 // NativeProcessProtocol Interface 66 // ----------------------------------------------------------------- 67 Status Resume(const ResumeActionList &resume_actions) override; 68 69 Status Halt() override; 70 71 Status Detach() override; 72 73 Status Signal(int signo) override; 74 75 Status Interrupt() override; 76 77 Status Kill() override; 78 79 Status GetMemoryRegionInfo(lldb::addr_t load_addr, 80 MemoryRegionInfo &range_info) override; 81 82 Status ReadMemory(lldb::addr_t addr, void *buf, size_t size, 83 size_t &bytes_read) override; 84 85 Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, 86 size_t &bytes_read) override; 87 88 Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, 89 size_t &bytes_written) override; 90 91 Status AllocateMemory(size_t size, uint32_t permissions, 92 lldb::addr_t &addr) override; 93 94 Status DeallocateMemory(lldb::addr_t addr) override; 95 96 lldb::addr_t GetSharedLibraryInfoAddress() override; 97 98 size_t UpdateThreads() override; 99 100 bool GetArchitecture(ArchSpec &arch) const override; 101 102 Status SetBreakpoint(lldb::addr_t addr, uint32_t size, 103 bool hardware) override; 104 105 void DoStopIDBumped(uint32_t newBumpId) override; 106 107 Status GetLoadedModuleFileSpec(const char *module_path, 108 FileSpec &file_spec) override; 109 110 Status GetFileLoadAddress(const llvm::StringRef &file_name, 111 lldb::addr_t &load_addr) override; 112 113 NativeThreadDarwinSP GetThreadByID(lldb::tid_t id); 114 115 task_t GetTask() const { return m_task; } 116 117 // ----------------------------------------------------------------- 118 // Interface used by NativeRegisterContext-derived classes. 119 // ----------------------------------------------------------------- 120 static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr, 121 void *data = nullptr, size_t data_size = 0, 122 long *result = nullptr); 123 124 bool SupportHardwareSingleStepping() const; 125 126 protected: 127 // ----------------------------------------------------------------- 128 // NativeProcessProtocol protected interface 129 // ----------------------------------------------------------------- 130 Status 131 GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint, 132 size_t &actual_opcode_size, 133 const uint8_t *&trap_opcode_bytes) override; 134 135 private: 136 // ----------------------------------------------------------------- 137 /// Mach task-related Member Variables 138 // ----------------------------------------------------------------- 139 140 // The task port for the inferior process. 141 mutable task_t m_task; 142 143 // True if the inferior process did an exec since we started 144 // monitoring it. 145 bool m_did_exec; 146 147 // The CPU type of this process. 148 mutable cpu_type_t m_cpu_type; 149 150 // ----------------------------------------------------------------- 151 /// Exception/Signal Handling Member Variables 152 // ----------------------------------------------------------------- 153 154 // Exception port on which we will receive child exceptions 155 mach_port_t m_exception_port; 156 157 // Saved state of the child exception port prior to us installing 158 // our own intercepting port. 159 MachException::PortInfo m_exc_port_info; 160 161 // The thread that runs the Mach exception read and reply handler. 162 pthread_t m_exception_thread; 163 164 // TODO see if we can remove this if we get the exception collection 165 // and distribution to happen in a single-threaded fashion. 166 std::recursive_mutex m_exception_messages_mutex; 167 168 // A collection of exception messages caught when listening to the 169 // exception port. 170 MachException::Message::collection m_exception_messages; 171 172 // When we call MachProcess::Interrupt(), we want to send this 173 // signal (if non-zero). 174 int m_sent_interrupt_signo; 175 176 // If we resume the process and still haven't received our 177 // interrupt signal (if this is non-zero). 178 int m_auto_resume_signo; 179 180 // ----------------------------------------------------------------- 181 /// Thread-related Member Variables 182 // ----------------------------------------------------------------- 183 NativeThreadListDarwin m_thread_list; 184 ResumeActionList m_thread_actions; 185 186 // ----------------------------------------------------------------- 187 /// Process Lifetime Member Variable 188 // ----------------------------------------------------------------- 189 190 // The pipe over which the waitpid thread and the main loop will 191 // communicate. 192 Pipe m_waitpid_pipe; 193 194 // The thread that runs the waitpid handler. 195 pthread_t m_waitpid_thread; 196 197 // waitpid reader callback handle. 198 MainLoop::ReadHandleUP m_waitpid_reader_handle; 199 200 #if 0 201 ArchSpec m_arch; 202 203 LazyBool m_supports_mem_region; 204 std::vector<MemoryRegionInfo> m_mem_region_cache; 205 206 lldb::tid_t m_pending_notification_tid; 207 208 // List of thread ids stepping with a breakpoint with the address of 209 // the relevan breakpoint 210 std::map<lldb::tid_t, lldb::addr_t> 211 m_threads_stepping_with_breakpoint; 212 #endif 213 214 // ----------------------------------------------------------------- 215 // Private Instance Methods 216 // ----------------------------------------------------------------- 217 NativeProcessDarwin(lldb::pid_t pid, int pty_master_fd); 218 219 // ----------------------------------------------------------------- 220 /// Finalize the launch. 221 /// 222 /// This method associates the NativeProcessDarwin instance with 223 /// the host process that was just launched. It peforms actions 224 /// like attaching a listener to the inferior exception port, 225 /// ptracing the process, and the like. 226 /// 227 /// @param[in] launch_flavor 228 /// The launch flavor that was used to launch the process. 229 /// 230 /// @param[in] main_loop 231 /// The main loop that will run the process monitor. Work 232 /// that needs to be done (e.g. reading files) gets registered 233 /// here along with callbacks to process the work. 234 /// 235 /// @return 236 /// Any error that occurred during the aforementioned 237 /// operations. Failure here will force termination of the 238 /// launched process and debugging session. 239 // ----------------------------------------------------------------- 240 Status FinalizeLaunch(LaunchFlavor launch_flavor, MainLoop &main_loop); 241 242 Status SaveExceptionPortInfo(); 243 244 void ExceptionMessageReceived(const MachException::Message &message); 245 246 void MaybeRaiseThreadPriority(); 247 248 Status StartExceptionThread(); 249 250 Status SendInferiorExitStatusToMainLoop(::pid_t pid, int status); 251 252 Status HandleWaitpidResult(); 253 254 bool ProcessUsingSpringBoard() const; 255 256 bool ProcessUsingBackBoard() const; 257 258 static void *ExceptionThread(void *arg); 259 260 void *DoExceptionThread(); 261 262 lldb::addr_t GetDYLDAllImageInfosAddress(Status &error) const; 263 264 static uint32_t GetCPUTypeForLocalProcess(::pid_t pid); 265 266 uint32_t GetCPUType() const; 267 268 task_t ExceptionMessageBundleComplete(); 269 270 void StartSTDIOThread(); 271 272 Status StartWaitpidThread(MainLoop &main_loop); 273 274 static void *WaitpidThread(void *arg); 275 276 void *DoWaitpidThread(); 277 278 task_t TaskPortForProcessID(Status &error, bool force = false) const; 279 280 /// Attaches to an existing process. Forms the 281 /// implementation of Process::DoAttach. 282 void AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, Status &error); 283 284 ::pid_t Attach(lldb::pid_t pid, Status &error); 285 286 Status PrivateResume(); 287 288 Status ReplyToAllExceptions(); 289 290 Status ResumeTask(); 291 292 bool IsTaskValid() const; 293 294 bool IsTaskValid(task_t task) const; 295 296 mach_port_t GetExceptionPort() const; 297 298 bool IsExceptionPortValid() const; 299 300 Status GetTaskBasicInfo(task_t task, struct task_basic_info *info) const; 301 302 Status SuspendTask(); 303 304 static Status SetDefaultPtraceOpts(const lldb::pid_t); 305 306 static void *MonitorThread(void *baton); 307 308 void MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status); 309 310 void WaitForNewThread(::pid_t tid); 311 312 void MonitorSIGTRAP(const siginfo_t &info, NativeThreadDarwin &thread); 313 314 void MonitorTrace(NativeThreadDarwin &thread); 315 316 void MonitorBreakpoint(NativeThreadDarwin &thread); 317 318 void MonitorWatchpoint(NativeThreadDarwin &thread, uint32_t wp_index); 319 320 void MonitorSignal(const siginfo_t &info, NativeThreadDarwin &thread, 321 bool exited); 322 323 Status SetupSoftwareSingleStepping(NativeThreadDarwin &thread); 324 325 #if 0 326 static ::ProcessMessage::CrashReason 327 GetCrashReasonForSIGSEGV(const siginfo_t *info); 328 329 static ::ProcessMessage::CrashReason 330 GetCrashReasonForSIGILL(const siginfo_t *info); 331 332 static ::ProcessMessage::CrashReason 333 GetCrashReasonForSIGFPE(const siginfo_t *info); 334 335 static ::ProcessMessage::CrashReason 336 GetCrashReasonForSIGBUS(const siginfo_t *info); 337 #endif 338 339 bool HasThreadNoLock(lldb::tid_t thread_id); 340 341 bool StopTrackingThread(lldb::tid_t thread_id); 342 343 NativeThreadDarwinSP AddThread(lldb::tid_t thread_id); 344 345 Status GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size); 346 347 Status FixupBreakpointPCAsNeeded(NativeThreadDarwin &thread); 348 349 /// Writes a siginfo_t structure corresponding to the given thread 350 /// ID to the memory region pointed to by @p siginfo. 351 Status GetSignalInfo(lldb::tid_t tid, void *siginfo); 352 353 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 354 /// corresponding to the given thread ID to the memory pointed to 355 /// by @p message. 356 Status GetEventMessage(lldb::tid_t tid, unsigned long *message); 357 358 void NotifyThreadDeath(lldb::tid_t tid); 359 360 Status Detach(lldb::tid_t tid); 361 362 // This method is requests a stop on all threads which are still 363 // running. It sets up a deferred delegate notification, which will 364 // fire once threads report as stopped. The triggerring_tid will be 365 // set as the current thread (main stop reason). 366 void StopRunningThreads(lldb::tid_t triggering_tid); 367 368 // Notify the delegate if all threads have stopped. 369 void SignalIfAllThreadsStopped(); 370 371 // Resume the given thread, optionally passing it the given signal. 372 // The type of resume operation (continue, single-step) depends on 373 // the state parameter. 374 Status ResumeThread(NativeThreadDarwin &thread, lldb::StateType state, 375 int signo); 376 377 void ThreadWasCreated(NativeThreadDarwin &thread); 378 379 void SigchldHandler(); 380 }; 381 382 } // namespace process_darwin 383 } // namespace lldb_private 384 385 #endif /* NativeProcessDarwin_h */ 386