-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy paththread_interrupter_macos.cc
137 lines (117 loc) · 3.87 KB
/
thread_interrupter_macos.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "platform/globals.h"
#if defined(DART_HOST_OS_MACOS)
#include <assert.h> // NOLINT
#include <errno.h> // NOLINT
#include <mach/kern_return.h> // NOLINT
#include <mach/mach.h> // NOLINT
#include <mach/thread_act.h> // NOLINT
#include <stdbool.h> // NOLINT
#include <sys/sysctl.h> // NOLINT
#include <sys/types.h> // NOLINT
#include <unistd.h> // NOLINT
#include "vm/flags.h"
#include "vm/os.h"
#include "vm/profiler.h"
#include "vm/signal_handler.h"
#include "vm/thread_interrupter.h"
namespace dart {
#ifndef PRODUCT
DECLARE_FLAG(bool, trace_thread_interrupter);
#if defined(HOST_ARCH_X64)
#define THREAD_STATE_FLAVOR x86_THREAD_STATE64
#define THREAD_STATE_FLAVOR_SIZE x86_THREAD_STATE64_COUNT
typedef x86_thread_state64_t thread_state_flavor_t;
#elif defined(HOST_ARCH_ARM64)
#define THREAD_STATE_FLAVOR ARM_THREAD_STATE64
#define THREAD_STATE_FLAVOR_SIZE ARM_THREAD_STATE64_COUNT
typedef arm_thread_state64_t thread_state_flavor_t;
#elif defined(HOST_ARCH_ARM)
#define THREAD_STATE_FLAVOR ARM_THREAD_STATE32
#define THREAD_STATE_FLAVOR_SIZE ARM_THREAD_STATE32_COUNT
typedef arm_thread_state32_t thread_state_flavor_t;
#else
#error "Unsupported architecture."
#endif // HOST_ARCH_...
class ThreadInterrupterMacOS {
public:
explicit ThreadInterrupterMacOS(OSThread* os_thread) : os_thread_(os_thread) {
ASSERT(os_thread != nullptr);
mach_thread_ = pthread_mach_thread_np(os_thread->id());
ASSERT(reinterpret_cast<void*>(mach_thread_) != nullptr);
res = thread_suspend(mach_thread_);
}
void CollectSample() {
if (res != KERN_SUCCESS) {
return;
}
auto count = static_cast<mach_msg_type_number_t>(THREAD_STATE_FLAVOR_SIZE);
thread_state_flavor_t state;
kern_return_t res =
thread_get_state(mach_thread_, THREAD_STATE_FLAVOR,
reinterpret_cast<thread_state_t>(&state), &count);
ASSERT(res == KERN_SUCCESS);
Thread* thread = static_cast<Thread*>(os_thread_->thread());
if (thread == nullptr) {
return;
}
ThreadInterruptScope signal_handler_scope;
Profiler::SampleThread(thread, ProcessState(state));
}
~ThreadInterrupterMacOS() {
if (res != KERN_SUCCESS) {
return;
}
res = thread_resume(mach_thread_);
ASSERT(res == KERN_SUCCESS);
}
private:
static InterruptedThreadState ProcessState(thread_state_flavor_t state) {
InterruptedThreadState its;
#if defined(HOST_ARCH_X64)
its.pc = state.__rip;
its.fp = state.__rbp;
its.csp = state.__rsp;
its.dsp = state.__rsp;
its.lr = 0;
#elif defined(HOST_ARCH_ARM64)
its.pc = state.__pc;
its.fp = state.__fp;
its.csp = state.__sp;
its.dsp = state.__sp;
its.lr = state.__lr;
#elif defined(HOST_ARCH_ARM)
its.pc = state.__pc;
its.fp = state.__r[7];
its.csp = state.__sp;
its.dsp = state.__sp;
its.lr = state.__lr;
#endif // HOST_ARCH_...
#if defined(TARGET_ARCH_ARM64) && !defined(USING_SIMULATOR)
its.dsp = state.__x[SPREG];
#endif
return its;
}
kern_return_t res;
OSThread* os_thread_;
mach_port_t mach_thread_;
};
void ThreadInterrupter::InterruptThread(OSThread* os_thread) {
ASSERT(!OSThread::Compare(OSThread::GetCurrentThreadId(), os_thread->id()));
if (FLAG_trace_thread_interrupter) {
OS::PrintErr("ThreadInterrupter interrupting %p\n", os_thread->id());
}
ThreadInterrupterMacOS interrupter(os_thread);
interrupter.CollectSample();
}
void ThreadInterrupter::InstallSignalHandler() {
// Nothing to do on MacOS.
}
void ThreadInterrupter::RemoveSignalHandler() {
// Nothing to do on MacOS.
}
#endif // !PRODUCT
} // namespace dart
#endif // defined(DART_HOST_OS_MACOS)