1 // Copyright 2019 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_PROFILER_SUSPENDABLE_THREAD_DELEGATE_MAC_H_ 6 #define BASE_PROFILER_SUSPENDABLE_THREAD_DELEGATE_MAC_H_ 7 8 #include <mach/mach.h> 9 #include <memory> 10 #include <vector> 11 12 #include "base/base_export.h" 13 #include "base/profiler/module_cache.h" 14 #include "base/profiler/sampling_profiler_thread_token.h" 15 #include "base/profiler/suspendable_thread_delegate.h" 16 #include "base/threading/platform_thread.h" 17 18 namespace base { 19 20 // Platform- and thread-specific implementation in support of stack sampling on 21 // Mac (X86_64) and iOS (X86_64 and ARM64). 22 class BASE_EXPORT SuspendableThreadDelegateMac 23 : public SuspendableThreadDelegate { 24 public: 25 class ScopedSuspendThread 26 : public SuspendableThreadDelegate::ScopedSuspendThread { 27 public: 28 explicit ScopedSuspendThread(mach_port_t thread_port); 29 ~ScopedSuspendThread() override; 30 31 ScopedSuspendThread(const ScopedSuspendThread&) = delete; 32 ScopedSuspendThread& operator=(const ScopedSuspendThread&) = delete; 33 34 bool WasSuccessful() const override; 35 36 private: 37 mach_port_t thread_port_; 38 }; 39 40 SuspendableThreadDelegateMac(SamplingProfilerThreadToken thread_token); 41 ~SuspendableThreadDelegateMac() override; 42 43 SuspendableThreadDelegateMac(const SuspendableThreadDelegateMac&) = delete; 44 SuspendableThreadDelegateMac& operator=(const SuspendableThreadDelegateMac&) = 45 delete; 46 47 // SuspendableThreadDelegate 48 std::unique_ptr<SuspendableThreadDelegate::ScopedSuspendThread> 49 CreateScopedSuspendThread() override; 50 bool GetThreadContext(RegisterContext* thread_context) override; 51 PlatformThreadId GetThreadId() const override; 52 uintptr_t GetStackBaseAddress() const override; 53 bool CanCopyStack(uintptr_t stack_pointer) override; 54 std::vector<uintptr_t*> GetRegistersToRewrite( 55 RegisterContext* thread_context) override; 56 57 private: 58 // Weak reference: Mach port for thread being profiled. 59 const mach_port_t thread_port_; 60 61 // The stack base address corresponding to |thread_port_|. 62 const uintptr_t thread_stack_base_address_; 63 }; 64 65 } // namespace base 66 67 #endif // BASE_PROFILER_SUSPENDABLE_THREAD_DELEGATE_MAC_H_ 68