suspendable_thread_delegate_mac.cc 5.0 KB

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