stack_copier_suspend.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/stack_copier_suspend.h"
  5. #include "base/profiler/stack_buffer.h"
  6. #include "base/profiler/suspendable_thread_delegate.h"
  7. namespace base {
  8. StackCopierSuspend::StackCopierSuspend(
  9. std::unique_ptr<SuspendableThreadDelegate> thread_delegate)
  10. : thread_delegate_(std::move(thread_delegate)) {}
  11. StackCopierSuspend::~StackCopierSuspend() = default;
  12. // Suspends the thread, copies the stack state, and resumes the thread. The
  13. // copied stack state includes the stack itself, the top address of the stack
  14. // copy, and the register context. Returns true on success, and returns the
  15. // copied state via the params.
  16. //
  17. // NO HEAP ALLOCATIONS within the ScopedSuspendThread scope.
  18. bool StackCopierSuspend::CopyStack(StackBuffer* stack_buffer,
  19. uintptr_t* stack_top,
  20. TimeTicks* timestamp,
  21. RegisterContext* thread_context,
  22. Delegate* delegate) {
  23. const uintptr_t top = thread_delegate_->GetStackBaseAddress();
  24. uintptr_t bottom = 0;
  25. const uint8_t* stack_copy_bottom = nullptr;
  26. {
  27. // Allocation of the ScopedSuspendThread object itself is OK since it
  28. // necessarily occurs before the thread is suspended by the object.
  29. std::unique_ptr<SuspendableThreadDelegate::ScopedSuspendThread>
  30. suspend_thread = thread_delegate_->CreateScopedSuspendThread();
  31. // TimeTicks::Now() is implemented in terms of reads to the timer tick
  32. // counter or TSC register on x86/x86_64 so is reentrant.
  33. *timestamp = TimeTicks::Now();
  34. if (!suspend_thread->WasSuccessful())
  35. return false;
  36. if (!thread_delegate_->GetThreadContext(thread_context))
  37. return false;
  38. bottom = RegisterContextStackPointer(thread_context);
  39. // The StackBuffer allocation is expected to be at least as large as the
  40. // largest stack region allocation on the platform, but check just in case
  41. // it isn't *and* the actual stack itself exceeds the buffer allocation
  42. // size.
  43. if ((top - bottom) > stack_buffer->size())
  44. return false;
  45. if (!thread_delegate_->CanCopyStack(bottom))
  46. return false;
  47. delegate->OnStackCopy();
  48. stack_copy_bottom = CopyStackContentsAndRewritePointers(
  49. reinterpret_cast<uint8_t*>(bottom), reinterpret_cast<uintptr_t*>(top),
  50. StackBuffer::kPlatformStackAlignment, stack_buffer->buffer());
  51. }
  52. *stack_top = reinterpret_cast<uintptr_t>(stack_copy_bottom) + (top - bottom);
  53. for (uintptr_t* reg :
  54. thread_delegate_->GetRegistersToRewrite(thread_context)) {
  55. *reg = RewritePointerIfInOriginalStack(reinterpret_cast<uint8_t*>(bottom),
  56. reinterpret_cast<uintptr_t*>(top),
  57. stack_copy_bottom, *reg);
  58. }
  59. return true;
  60. }
  61. } // namespace base