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