| 6 |
| 7 | #include "base/process/process_handle.h" |
| 8 | #include "base/profiler/thread_delegate_posix.h" |
| 9 | #include "base/stl_util.h" |
| 10 |
| 11 | #include "build/build_config.h" |
| 12 |
| 13 | namespace base { |
| 14 |
| 15 | namespace { |
| 16 |
| 17 | uintptr_t GetThreadStackBaseAddressImpl( |
| 18 | SamplingProfilerThreadToken thread_token) { |
| 19 | pthread_attr_t attr; |
| 20 | pthread_getattr_np(thread_token.pthread_id, &attr); |
| 21 | // See crbug.com/617730 for limitations of this approach on Linux. |
| 22 | void* address; |
| 23 | size_t size; |
| 24 | pthread_attr_getstack(&attr, &address, &size); |
| 25 | pthread_attr_destroy(&attr); |
| 26 | const uintptr_t base_address = reinterpret_cast<uintptr_t>(address) + size; |
| 27 | return base_address; |
| 28 | } |
| 29 |
| 30 | uintptr_t GetThreadStackBaseAddress(SamplingProfilerThreadToken thread_token) { |
| 31 | #if defined(OS_ANDROID) |
| 32 | // Caches the main thread base address on Android since Bionic has to read |
| 33 | // /proc/$PID/maps to obtain it. Other thread base addresses are sourced from |
| 34 | // pthread state so are cheap to get. |
| 35 | const bool is_main_thread = thread_token.id == GetCurrentProcId(); |
| 36 | if (is_main_thread) { |
| 37 | static const uintptr_t main_thread_base_address = |
| 38 | GetThreadStackBaseAddressImpl(thread_token); |
| 39 | return main_thread_base_address; |
| 40 | } |
| 41 | #endif |
| 42 | return GetThreadStackBaseAddressImpl(thread_token); |
| 43 | } |
| 44 |
| 45 | } // namespace |
| 46 |
| 47 | ThreadDelegatePosix::ThreadDelegatePosix( |
| 48 | SamplingProfilerThreadToken thread_token) |
| 49 | : thread_id_(thread_token.id), |
| 50 | thread_stack_base_address_(GetThreadStackBaseAddress(thread_token)) {} |
| 51 |
| 52 | PlatformThreadId ThreadDelegatePosix::GetThreadId() const { |
| 53 | return thread_id_; |
| 54 | } |
| 55 |
| 56 | uintptr_t ThreadDelegatePosix::GetStackBaseAddress() const { |
| 57 | return thread_stack_base_address_; |
| 58 | } |
| 59 |
| 60 | std::vector<uintptr_t*> ThreadDelegatePosix::GetRegistersToRewrite( |
| 61 | RegisterContext* thread_context) { |
| 62 | #if defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_32_BITS) |
| 63 | return { |
| 64 | reinterpret_cast<uintptr_t*>(&thread_context->arm_r0), |
| 65 | reinterpret_cast<uintptr_t*>(&thread_context->arm_r1), |
| 66 | reinterpret_cast<uintptr_t*>(&thread_context->arm_r2), |
| 67 | reinterpret_cast<uintptr_t*>(&thread_context->arm_r3), |
| 68 | reinterpret_cast<uintptr_t*>(&thread_context->arm_r4), |
| 69 | reinterpret_cast<uintptr_t*>(&thread_context->arm_r5), |
| 70 | reinterpret_cast<uintptr_t*>(&thread_context->arm_r6), |
| 71 | reinterpret_cast<uintptr_t*>(&thread_context->arm_r7), |
| 72 | reinterpret_cast<uintptr_t*>(&thread_context->arm_r8), |
| 73 | reinterpret_cast<uintptr_t*>(&thread_context->arm_r9), |
| 74 | reinterpret_cast<uintptr_t*>(&thread_context->arm_r10), |
| 75 | reinterpret_cast<uintptr_t*>(&thread_context->arm_fp), |
| 76 | reinterpret_cast<uintptr_t*>(&thread_context->arm_ip), |
| 77 | reinterpret_cast<uintptr_t*>(&thread_context->arm_sp), |
| 78 | // arm_lr and arm_pc do not require rewriting because they contain |
| 79 | // addresses of executable code, not addresses in the stack. |
| 80 | }; |
| 81 | #elif defined(ARCH_CPU_ARM_FAMILY) && \ |
| 82 | defined(ARCH_CPU_64_BITS) // #if defined(ARCH_CPU_ARM_FAMILY) && |
| 83 | // defined(ARCH_CPU_32_BITS) |
| 84 | std::vector<uintptr_t*> registers; |
| 85 | registers.reserve(12); |
| 86 | // Return the set of callee-save registers per the ARM 64-bit Procedure Call |
| 87 | // Standard section 5.1.1, plus the stack pointer. |
| 88 | registers.push_back(reinterpret_cast<uintptr_t*>(&thread_context->sp)); |
| 89 | for (size_t i = 19; i <= 29; ++i) |
| 90 | registers.push_back(reinterpret_cast<uintptr_t*>(&thread_context->regs[i])); |
| 91 | return registers; |
| 92 | #elif defined(ARCH_CPU_X86_FAMILY) && defined(ARCH_CPU_32_BITS) |
| 93 | return { |
| 94 | // Return the set of callee-save registers per the i386 System V ABI |
| 95 | // section 2.2.3, plus the stack pointer. |
| 96 | reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_EBX]), |
| 97 | reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_EBP]), |
| 98 | reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_ESI]), |
| 99 | reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_EDI]), |
| 100 | reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_ESP]), |
| 101 | }; |
| 102 | #elif defined(ARCH_CPU_X86_FAMILY) && defined(ARCH_CPU_64_BITS) |
| 103 | return { |
| 104 | // Return the set of callee-save registers per the x86-64 System V ABI |
| 105 | // section 3.2.1, plus the stack pointer. |
| 106 | reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_RBP]), |
| 107 | reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_RBX]), |
| 108 | reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_R12]), |
| 109 | reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_R13]), |
| 110 | reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_R14]), |
| 111 | reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_R15]), |
| 112 | reinterpret_cast<uintptr_t*>(&thread_context->gregs[REG_RSP]), |
| 113 | }; |
| 114 | #else // #if defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_32_BITS) |
| 115 | // Unimplemented for other architectures. |
| 116 | return {}; |
| 117 | #endif |
| 118 | } |
| 119 |
| 120 | } // namespace base |
| 121 |