1*6777b538SAndroid Build Coastguard Worker // Copyright 2019 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker
5*6777b538SAndroid Build Coastguard Worker #include "base/profiler/thread_delegate_posix.h"
6*6777b538SAndroid Build Coastguard Worker
7*6777b538SAndroid Build Coastguard Worker #include <inttypes.h>
8*6777b538SAndroid Build Coastguard Worker #include <pthread.h>
9*6777b538SAndroid Build Coastguard Worker #include <stdio.h>
10*6777b538SAndroid Build Coastguard Worker
11*6777b538SAndroid Build Coastguard Worker #include <optional>
12*6777b538SAndroid Build Coastguard Worker
13*6777b538SAndroid Build Coastguard Worker #include "base/memory/ptr_util.h"
14*6777b538SAndroid Build Coastguard Worker #include "base/process/process_handle.h"
15*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h"
16*6777b538SAndroid Build Coastguard Worker
17*6777b538SAndroid Build Coastguard Worker #if !(BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS))
18*6777b538SAndroid Build Coastguard Worker #include "base/profiler/stack_base_address_posix.h"
19*6777b538SAndroid Build Coastguard Worker #endif
20*6777b538SAndroid Build Coastguard Worker
21*6777b538SAndroid Build Coastguard Worker namespace base {
22*6777b538SAndroid Build Coastguard Worker // static
Create(SamplingProfilerThreadToken thread_token)23*6777b538SAndroid Build Coastguard Worker std::unique_ptr<ThreadDelegatePosix> ThreadDelegatePosix::Create(
24*6777b538SAndroid Build Coastguard Worker SamplingProfilerThreadToken thread_token) {
25*6777b538SAndroid Build Coastguard Worker std::optional<uintptr_t> base_address;
26*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
27*6777b538SAndroid Build Coastguard Worker base_address = thread_token.stack_base_address;
28*6777b538SAndroid Build Coastguard Worker #else
29*6777b538SAndroid Build Coastguard Worker base_address =
30*6777b538SAndroid Build Coastguard Worker GetThreadStackBaseAddress(thread_token.id, thread_token.pthread_id);
31*6777b538SAndroid Build Coastguard Worker #endif
32*6777b538SAndroid Build Coastguard Worker if (!base_address)
33*6777b538SAndroid Build Coastguard Worker return nullptr;
34*6777b538SAndroid Build Coastguard Worker return base::WrapUnique(
35*6777b538SAndroid Build Coastguard Worker new ThreadDelegatePosix(thread_token.id, *base_address));
36*6777b538SAndroid Build Coastguard Worker }
37*6777b538SAndroid Build Coastguard Worker
38*6777b538SAndroid Build Coastguard Worker ThreadDelegatePosix::~ThreadDelegatePosix() = default;
39*6777b538SAndroid Build Coastguard Worker
GetThreadId() const40*6777b538SAndroid Build Coastguard Worker PlatformThreadId ThreadDelegatePosix::GetThreadId() const {
41*6777b538SAndroid Build Coastguard Worker return thread_id_;
42*6777b538SAndroid Build Coastguard Worker }
43*6777b538SAndroid Build Coastguard Worker
GetStackBaseAddress() const44*6777b538SAndroid Build Coastguard Worker uintptr_t ThreadDelegatePosix::GetStackBaseAddress() const {
45*6777b538SAndroid Build Coastguard Worker return thread_stack_base_address_;
46*6777b538SAndroid Build Coastguard Worker }
47*6777b538SAndroid Build Coastguard Worker
GetRegistersToRewrite(RegisterContext * thread_context)48*6777b538SAndroid Build Coastguard Worker std::vector<uintptr_t*> ThreadDelegatePosix::GetRegistersToRewrite(
49*6777b538SAndroid Build Coastguard Worker RegisterContext* thread_context) {
50*6777b538SAndroid Build Coastguard Worker #if defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_32_BITS)
51*6777b538SAndroid Build Coastguard Worker return {
52*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_r0),
53*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_r1),
54*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_r2),
55*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_r3),
56*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_r4),
57*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_r5),
58*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_r6),
59*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_r7),
60*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_r8),
61*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_r9),
62*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_r10),
63*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_fp),
64*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_ip),
65*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->arm_sp),
66*6777b538SAndroid Build Coastguard Worker // arm_lr and arm_pc do not require rewriting because they contain
67*6777b538SAndroid Build Coastguard Worker // addresses of executable code, not addresses in the stack.
68*6777b538SAndroid Build Coastguard Worker };
69*6777b538SAndroid Build Coastguard Worker #elif defined(ARCH_CPU_ARM_FAMILY) && \
70*6777b538SAndroid Build Coastguard Worker defined(ARCH_CPU_64_BITS) // #if defined(ARCH_CPU_ARM_FAMILY) &&
71*6777b538SAndroid Build Coastguard Worker // defined(ARCH_CPU_32_BITS)
72*6777b538SAndroid Build Coastguard Worker std::vector<uintptr_t*> registers;
73*6777b538SAndroid Build Coastguard Worker registers.reserve(12);
74*6777b538SAndroid Build Coastguard Worker // Return the set of callee-save registers per the ARM 64-bit Procedure Call
75*6777b538SAndroid Build Coastguard Worker // Standard section 5.1.1, plus the stack pointer.
76*6777b538SAndroid Build Coastguard Worker registers.push_back(reinterpret_cast<uintptr_t*>(&thread_context->sp));
77*6777b538SAndroid Build Coastguard Worker for (size_t i = 19; i <= 29; ++i)
78*6777b538SAndroid Build Coastguard Worker registers.push_back(reinterpret_cast<uintptr_t*>(&thread_context->regs[i]));
79*6777b538SAndroid Build Coastguard Worker return registers;
80*6777b538SAndroid Build Coastguard Worker #elif defined(ARCH_CPU_X86_FAMILY) && defined(ARCH_CPU_32_BITS)
81*6777b538SAndroid Build Coastguard Worker return {
82*6777b538SAndroid Build Coastguard Worker // Return the set of callee-save registers per the i386 System V ABI
83*6777b538SAndroid Build Coastguard Worker // section 2.2.3, plus the stack pointer.
84*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_EBX]),
85*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_EBP]),
86*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_ESI]),
87*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_EDI]),
88*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_ESP]),
89*6777b538SAndroid Build Coastguard Worker };
90*6777b538SAndroid Build Coastguard Worker #elif defined(ARCH_CPU_X86_FAMILY) && defined(ARCH_CPU_64_BITS)
91*6777b538SAndroid Build Coastguard Worker return {
92*6777b538SAndroid Build Coastguard Worker // Return the set of callee-save registers per the x86-64 System V ABI
93*6777b538SAndroid Build Coastguard Worker // section 3.2.1, plus the stack pointer.
94*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_RBP]),
95*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_RBX]),
96*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_R12]),
97*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_R13]),
98*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_R14]),
99*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_R15]),
100*6777b538SAndroid Build Coastguard Worker reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_RSP]),
101*6777b538SAndroid Build Coastguard Worker };
102*6777b538SAndroid Build Coastguard Worker #else // #if defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_32_BITS)
103*6777b538SAndroid Build Coastguard Worker // Unimplemented for other architectures.
104*6777b538SAndroid Build Coastguard Worker return {};
105*6777b538SAndroid Build Coastguard Worker #endif
106*6777b538SAndroid Build Coastguard Worker }
107*6777b538SAndroid Build Coastguard Worker
ThreadDelegatePosix(PlatformThreadId id,uintptr_t base_address)108*6777b538SAndroid Build Coastguard Worker ThreadDelegatePosix::ThreadDelegatePosix(PlatformThreadId id,
109*6777b538SAndroid Build Coastguard Worker uintptr_t base_address)
110*6777b538SAndroid Build Coastguard Worker : thread_id_(id), thread_stack_base_address_(base_address) {}
111*6777b538SAndroid Build Coastguard Worker
112*6777b538SAndroid Build Coastguard Worker } // namespace base
113