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