Fix emulator build error
[platform/framework/web/chromium-efl.git] / base / profiler / suspendable_thread_delegate_mac.cc
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 #include "base/profiler/suspendable_thread_delegate_mac.h"
6
7 #include <mach/mach.h>
8 #include <mach/thread_act.h>
9 #include <pthread.h>
10
11 #include <vector>
12
13 #include "base/apple/mach_logging.h"
14 #include "base/check.h"
15 #include "base/profiler/profile_builder.h"
16 #include "build/build_config.h"
17
18 // IMPORTANT NOTE: Some functions within this implementation are invoked while
19 // the target thread is suspended so it must not do any allocation from the
20 // heap, including indirectly via use of DCHECK/CHECK or other logging
21 // statements. Otherwise this code can deadlock on heap locks acquired by the
22 // target thread before it was suspended. These functions are commented with "NO
23 // HEAP ALLOCATIONS".
24
25 namespace base {
26
27 namespace {
28
29 #if defined(ARCH_CPU_X86_64)
30 constexpr mach_msg_type_number_t kThreadStateCount = x86_THREAD_STATE64_COUNT;
31 constexpr thread_state_flavor_t kThreadStateFlavor = x86_THREAD_STATE64;
32 #elif defined(ARCH_CPU_ARM64)
33 constexpr mach_msg_type_number_t kThreadStateCount = ARM_THREAD_STATE64_COUNT;
34 constexpr thread_state_flavor_t kThreadStateFlavor = ARM_THREAD_STATE64;
35 #endif
36
37 // Fills |state| with |target_thread|'s context. NO HEAP ALLOCATIONS.
38 bool GetThreadContextImpl(thread_act_t target_thread, RegisterContext* state) {
39   auto count = kThreadStateCount;
40   return thread_get_state(target_thread, kThreadStateFlavor,
41                           reinterpret_cast<thread_state_t>(state),
42                           &count) == KERN_SUCCESS;
43 }
44
45 }  // namespace
46
47 // ScopedSuspendThread --------------------------------------------------------
48
49 // NO HEAP ALLOCATIONS after thread_suspend.
50 SuspendableThreadDelegateMac::ScopedSuspendThread::ScopedSuspendThread(
51     mach_port_t thread_port)
52     : thread_port_(thread_suspend(thread_port) == KERN_SUCCESS
53                        ? thread_port
54                        : MACH_PORT_NULL) {}
55
56 // NO HEAP ALLOCATIONS. The MACH_CHECK is OK because it provides a more noisy
57 // failure mode than deadlocking.
58 SuspendableThreadDelegateMac::ScopedSuspendThread::~ScopedSuspendThread() {
59   if (!WasSuccessful())
60     return;
61
62   kern_return_t kr = thread_resume(thread_port_);
63   MACH_CHECK(kr == KERN_SUCCESS, kr) << "thread_resume";
64 }
65
66 bool SuspendableThreadDelegateMac::ScopedSuspendThread::WasSuccessful() const {
67   return thread_port_ != MACH_PORT_NULL;
68 }
69
70 // SuspendableThreadDelegateMac -----------------------------------------------
71
72 SuspendableThreadDelegateMac::SuspendableThreadDelegateMac(
73     SamplingProfilerThreadToken thread_token)
74     : thread_port_(thread_token.id),
75       thread_stack_base_address_(
76           reinterpret_cast<uintptr_t>(pthread_get_stackaddr_np(
77               pthread_from_mach_thread_np(thread_token.id)))) {
78   // This class suspends threads, and those threads might be suspended in dyld.
79   // Therefore, for all the system functions that might be linked in dynamically
80   // that are used while threads are suspended, make calls to them to make sure
81   // that they are linked up.
82   RegisterContext thread_context;
83   GetThreadContextImpl(thread_port_, &thread_context);
84 }
85
86 SuspendableThreadDelegateMac::~SuspendableThreadDelegateMac() = default;
87
88 std::unique_ptr<SuspendableThreadDelegate::ScopedSuspendThread>
89 SuspendableThreadDelegateMac::CreateScopedSuspendThread() {
90   return std::make_unique<ScopedSuspendThread>(thread_port_);
91 }
92
93 PlatformThreadId SuspendableThreadDelegateMac::GetThreadId() const {
94   return thread_port_;
95 }
96
97 // NO HEAP ALLOCATIONS.
98 bool SuspendableThreadDelegateMac::GetThreadContext(
99     RegisterContext* thread_context) {
100   return GetThreadContextImpl(thread_port_, thread_context);
101 }
102
103 // NO HEAP ALLOCATIONS.
104 uintptr_t SuspendableThreadDelegateMac::GetStackBaseAddress() const {
105   return thread_stack_base_address_;
106 }
107
108 // NO HEAP ALLOCATIONS.
109 bool SuspendableThreadDelegateMac::CanCopyStack(uintptr_t stack_pointer) {
110   return true;
111 }
112
113 std::vector<uintptr_t*> SuspendableThreadDelegateMac::GetRegistersToRewrite(
114     RegisterContext* thread_context) {
115 #if defined(ARCH_CPU_X86_64)
116   return {
117       &AsUintPtr(&thread_context->__rbx), &AsUintPtr(&thread_context->__rbp),
118       &AsUintPtr(&thread_context->__rsp), &AsUintPtr(&thread_context->__r12),
119       &AsUintPtr(&thread_context->__r13), &AsUintPtr(&thread_context->__r14),
120       &AsUintPtr(&thread_context->__r15)};
121 #elif defined(ARCH_CPU_ARM64)  // defined(ARCH_CPU_X86_64)
122   return {
123       &AsUintPtr(&thread_context->__fp),
124       &AsUintPtr(&thread_context->__sp),
125       &AsUintPtr(&thread_context->__x[19]),
126       &AsUintPtr(&thread_context->__x[20]),
127       &AsUintPtr(&thread_context->__x[21]),
128       &AsUintPtr(&thread_context->__x[22]),
129       &AsUintPtr(&thread_context->__x[23]),
130       &AsUintPtr(&thread_context->__x[24]),
131       &AsUintPtr(&thread_context->__x[25]),
132       &AsUintPtr(&thread_context->__x[26]),
133       &AsUintPtr(&thread_context->__x[27]),
134       &AsUintPtr(&thread_context->__x[28]),
135   };
136 #endif                         // defined(ARCH_CPU_ARM64)
137 }
138
139 }  // namespace base