6#if defined(DART_HOST_OS_MACOS)
10#include <mach/kern_return.h>
12#include <mach/thread_act.h>
14#include <sys/sysctl.h>
30#if defined(HOST_ARCH_X64)
31#define THREAD_STATE_FLAVOR x86_THREAD_STATE64
32#define THREAD_STATE_FLAVOR_SIZE x86_THREAD_STATE64_COUNT
33typedef x86_thread_state64_t thread_state_flavor_t;
34#elif defined(HOST_ARCH_ARM64)
35#define THREAD_STATE_FLAVOR ARM_THREAD_STATE64
36#define THREAD_STATE_FLAVOR_SIZE ARM_THREAD_STATE64_COUNT
37typedef arm_thread_state64_t thread_state_flavor_t;
38#elif defined(HOST_ARCH_ARM)
39#define THREAD_STATE_FLAVOR ARM_THREAD_STATE32
40#define THREAD_STATE_FLAVOR_SIZE ARM_THREAD_STATE32_COUNT
41typedef arm_thread_state32_t thread_state_flavor_t;
43#error "Unsupported architecture."
46class ThreadInterrupterMacOS {
48 explicit ThreadInterrupterMacOS(OSThread* os_thread) : os_thread_(os_thread) {
49 ASSERT(os_thread !=
nullptr);
50 mach_thread_ = pthread_mach_thread_np(os_thread->id());
51 ASSERT(
reinterpret_cast<void*
>(mach_thread_) !=
nullptr);
52 res = thread_suspend(mach_thread_);
56 if (res != KERN_SUCCESS) {
59 auto count =
static_cast<mach_msg_type_number_t
>(THREAD_STATE_FLAVOR_SIZE);
60 thread_state_flavor_t
state;
62 thread_get_state(mach_thread_, THREAD_STATE_FLAVOR,
63 reinterpret_cast<thread_state_t
>(&
state), &
count);
64 ASSERT(res == KERN_SUCCESS);
65 Thread* thread =
static_cast<Thread*
>(os_thread_->thread());
66 if (thread ==
nullptr) {
69 ThreadInterruptScope signal_handler_scope;
73 ~ThreadInterrupterMacOS() {
74 if (res != KERN_SUCCESS) {
77 res = thread_resume(mach_thread_);
78 ASSERT(res == KERN_SUCCESS);
82 static InterruptedThreadState ProcessState(thread_state_flavor_t
state) {
83 InterruptedThreadState its;
84#if defined(HOST_ARCH_X64)
87 its.csp =
state.__rsp;
88 its.dsp =
state.__rsp;
90#elif defined(HOST_ARCH_ARM64)
96#elif defined(HOST_ARCH_ARM)
98 its.fp =
state.__r[7];
100 its.dsp =
state.__sp;
104#if defined(TARGET_ARCH_ARM64) && !defined(USING_SIMULATOR)
111 OSThread* os_thread_;
112 mach_port_t mach_thread_;
117 if (FLAG_trace_thread_interrupter) {
118 OS::PrintErr(
"ThreadInterrupter interrupting %p\n", os_thread->id());
121 ThreadInterrupterMacOS interrupter(os_thread);
122 interrupter.CollectSample();
125void ThreadInterrupter::InstallSignalHandler() {
129void ThreadInterrupter::RemoveSignalHandler() {
static ThreadId GetCurrentThreadId()
static bool Compare(ThreadId a, ThreadId b)
static void static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
static void SampleThread(Thread *thread, const InterruptedThreadState &state)
static void InterruptThread(OSThread *thread)
static void CollectSample(Isolate *isolate, bool exited_dart_code, bool in_dart_code, Sample *sample, ProfilerNativeStackWalker *native_stack_walker, ProfilerDartStackWalker *dart_stack_walker, uword pc, uword fp, uword sp, ProfilerCounters *counters)
DECLARE_FLAG(bool, show_invisible_frames)