Fix emulator build error
[platform/framework/web/chromium-efl.git] / base / profiler / stack_copier_suspend.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/stack_copier_suspend.h"
6
7 #include "base/profiler/stack_buffer.h"
8 #include "base/profiler/suspendable_thread_delegate.h"
9
10 namespace base {
11
12 StackCopierSuspend::StackCopierSuspend(
13     std::unique_ptr<SuspendableThreadDelegate> thread_delegate)
14     : thread_delegate_(std::move(thread_delegate)) {}
15
16 StackCopierSuspend::~StackCopierSuspend() = default;
17
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.
22 //
23 // NO HEAP ALLOCATIONS within the ScopedSuspendThread scope.
24 bool StackCopierSuspend::CopyStack(StackBuffer* stack_buffer,
25                                    uintptr_t* stack_top,
26                                    TimeTicks* timestamp,
27                                    RegisterContext* thread_context,
28                                    Delegate* delegate) {
29   const uintptr_t top = thread_delegate_->GetStackBaseAddress();
30   uintptr_t bottom = 0;
31   const uint8_t* stack_copy_bottom = nullptr;
32   {
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();
37
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();
41
42     if (!suspend_thread->WasSuccessful())
43       return false;
44
45     if (!thread_delegate_->GetThreadContext(thread_context))
46       return false;
47
48     bottom = RegisterContextStackPointer(thread_context);
49
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
53     // size.
54     if ((top - bottom) > stack_buffer->size())
55       return false;
56
57     if (!thread_delegate_->CanCopyStack(bottom))
58       return false;
59
60     delegate->OnStackCopy();
61
62     stack_copy_bottom = CopyStackContentsAndRewritePointers(
63         reinterpret_cast<uint8_t*>(bottom), reinterpret_cast<uintptr_t*>(top),
64         StackBuffer::kPlatformStackAlignment, stack_buffer->buffer());
65   }
66
67   *stack_top = reinterpret_cast<uintptr_t>(stack_copy_bottom) + (top - bottom);
68
69   for (uintptr_t* reg :
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);
74   }
75
76   return true;
77 }
78
79 }  // namespace base