#include "base/profiler/stack_copier_suspend.h"
#include "base/profiler/register_context_registers.h"
#include "base/profiler/stack_buffer.h"
#include "base/profiler/suspendable_thread_delegate.h"
namespace base {
StackCopierSuspend::StackCopierSuspend(
std::unique_ptr<SuspendableThreadDelegate> thread_delegate)
: thread_delegate_(std::move(thread_delegate)) {}
StackCopierSuspend::~StackCopierSuspend() = default;
bool StackCopierSuspend::CopyStack(StackBuffer* stack_buffer,
uintptr_t* stack_top,
TimeTicks* timestamp,
RegisterContext* thread_context,
Delegate* delegate) {
const uintptr_t top = thread_delegate_->GetStackBaseAddress();
uintptr_t bottom = 0;
const uint8_t* stack_copy_bottom = nullptr;
{
std::unique_ptr<SuspendableThreadDelegate::ScopedSuspendThread>
suspend_thread = thread_delegate_->CreateScopedSuspendThread();
*timestamp = TimeTicks::Now();
if (!suspend_thread->WasSuccessful()) {
return false;
}
if (!thread_delegate_->GetThreadContext(thread_context)) {
return false;
}
bottom = RegisterContextStackPointer(thread_context);
if ((top - bottom) > stack_buffer->size()) {
return false;
}
if (!thread_delegate_->CanCopyStack(bottom)) {
return false;
}
delegate->OnStackCopy();
stack_copy_bottom = CopyStackContentsAndRewritePointers(
reinterpret_cast<uint8_t*>(bottom), reinterpret_cast<uintptr_t*>(top),
StackBuffer::kPlatformStackAlignment, stack_buffer->buffer());
}
*stack_top = reinterpret_cast<uintptr_t>(stack_copy_bottom) + (top - bottom);
for (uintptr_t* reg :
thread_delegate_->GetRegistersToRewrite(thread_context)) {
*reg = RewritePointerIfInOriginalStack(reinterpret_cast<uint8_t*>(bottom),
reinterpret_cast<uintptr_t*>(top),
stack_copy_bottom, *reg);
}
return true;
}
std::vector<uintptr_t*> StackCopierSuspend::GetRegistersToRewrite(
RegisterContext* thread_context) {
return thread_delegate_->GetRegistersToRewrite(thread_context);
}
}