| 6 | // the native register context for the platform, plus functions that provide |
| 7 | // access to key registers in the context. |
| 8 |
| 9 | #ifndef BASE_PROFILER_REGISTER_CONTEXT_H_ |
| 10 | #define BASE_PROFILER_REGISTER_CONTEXT_H_ |
| 11 |
| 12 | #include <cstdint> |
| 13 |
| 14 | #include "build/build_config.h" |
| 15 |
| 16 | #if defined(OS_WIN) |
| 17 | #include <windows.h> |
| 18 | #elif defined(OS_MACOSX) |
| 19 | #include <mach/machine/thread_status.h> |
| 20 | #elif defined(OS_ANDROID) || defined(OS_LINUX) |
| 21 | #include <sys/ucontext.h> |
| 22 | #endif |
| 23 |
| 24 | namespace base { |
| 25 |
| 26 | // Helper function to account for the fact that platform-specific register state |
| 27 | // types may be of the same size as uintptr_t, but not of the same type or |
| 28 | // signedness -- e.g. unsigned int vs. unsigned long on 32-bit Windows, unsigned |
| 29 | // long vs. unsigned long long on Mac, long long vs. unsigned long long on |
| 30 | // Linux. |
| 31 | template <typename T> |
| 32 | uintptr_t& AsUintPtr(T* value) { |
| 33 | static_assert(sizeof(T) == sizeof(uintptr_t), |
| 34 | "register state type must be of equivalent size to uintptr_t"); |
| 35 | return *reinterpret_cast<uintptr_t*>(value); |
| 36 | } |
| 37 |
| 38 | #if defined(OS_WIN) |
| 39 |
| 40 | using RegisterContext = ::CONTEXT; |
| 41 |
| 42 | inline uintptr_t& RegisterContextStackPointer(::CONTEXT* context) { |
| 43 | #if defined(ARCH_CPU_X86_64) |
| 44 | return context->Rsp; |
| 45 | #elif defined(ARCH_CPU_ARM64) |
| 46 | return context->Sp; |
| 47 | #else |
| 48 | return AsUintPtr(&context->Esp); |
| 49 | #endif |
| 50 | } |
| 51 |
| 52 | inline uintptr_t& RegisterContextFramePointer(::CONTEXT* context) { |
| 53 | #if defined(ARCH_CPU_X86_64) |
| 54 | return context->Rbp; |
| 55 | #elif defined(ARCH_CPU_ARM64) |
| 56 | return context->Fp; |
| 57 | #else |
| 58 | return AsUintPtr(&context->Ebp); |
| 59 | #endif |
| 60 | } |
| 61 |
| 62 | inline uintptr_t& RegisterContextInstructionPointer(::CONTEXT* context) { |
| 63 | #if defined(ARCH_CPU_X86_64) |
| 64 | return context->Rip; |
| 65 | #elif defined(ARCH_CPU_ARM64) |
| 66 | return context->Pc; |
| 67 | #else |
| 68 | return AsUintPtr(&context->Eip); |
| 69 | #endif |
| 70 | } |
| 71 |
| 72 | #elif defined(OS_MACOSX) && !defined(OS_IOS) // #if defined(OS_WIN) |
| 73 |
| 74 | using RegisterContext = x86_thread_state64_t; |
| 75 |
| 76 | inline uintptr_t& RegisterContextStackPointer(x86_thread_state64_t* context) { |
| 77 | return AsUintPtr(&context->__rsp); |
| 78 | } |
| 79 |
| 80 | inline uintptr_t& RegisterContextFramePointer(x86_thread_state64_t* context) { |
| 81 | return AsUintPtr(&context->__rbp); |
| 82 | } |
| 83 |
| 84 | inline uintptr_t& RegisterContextInstructionPointer( |
| 85 | x86_thread_state64_t* context) { |
| 86 | return AsUintPtr(&context->__rip); |
| 87 | } |
| 88 |
| 89 | #elif defined(OS_ANDROID) || defined(OS_LINUX) // #if defined(OS_WIN) |
| 90 |
| 91 | using RegisterContext = mcontext_t; |
| 92 |
| 93 | #if defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_32_BITS) |
| 94 |
| 95 | inline uintptr_t& RegisterContextStackPointer(mcontext_t* context) { |
| 96 | return AsUintPtr(&context->arm_sp); |
| 97 | } |
| 98 |
| 99 | inline uintptr_t& RegisterContextFramePointer(mcontext_t* context) { |
| 100 | return AsUintPtr(&context->arm_fp); |
| 101 | } |
| 102 |
| 103 | inline uintptr_t& RegisterContextInstructionPointer(mcontext_t* context) { |
| 104 | return AsUintPtr(&context->arm_ip); |
| 105 | } |
| 106 |
| 107 | #elif defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_64_BITS) |
| 108 |
| 109 | inline uintptr_t& RegisterContextStackPointer(mcontext_t* context) { |
| 110 | return AsUintPtr(&context->sp); |
| 111 | } |
| 112 |
| 113 | inline uintptr_t& RegisterContextFramePointer(mcontext_t* context) { |
| 114 | // r29 is the FP register on 64-bit ARM per the Procedure Call Standard, |
| 115 | // section 5.1.1. |
| 116 | return AsUintPtr(&context->regs[29]); |
| 117 | } |
| 118 |
| 119 | inline uintptr_t& RegisterContextInstructionPointer(mcontext_t* context) { |
| 120 | return AsUintPtr(&context->pc); |
| 121 | } |
| 122 |
| 123 | #elif defined(ARCH_CPU_X86_FAMILY) && defined(ARCH_CPU_32_BITS) |
| 124 |
| 125 | inline uintptr_t& RegisterContextStackPointer(mcontext_t* context) { |
| 126 | return AsUintPtr(&context->gregs[REG_ESP]); |
| 127 | } |
| 128 |
| 129 | inline uintptr_t& RegisterContextFramePointer(mcontext_t* context) { |
| 130 | return AsUintPtr(&context->gregs[REG_EBP]); |
| 131 | } |
| 132 |
| 133 | inline uintptr_t& RegisterContextInstructionPointer(mcontext_t* context) { |
| 134 | return AsUintPtr(&context->gregs[REG_EIP]); |
| 135 | } |
| 136 |
| 137 | #elif defined(ARCH_CPU_X86_FAMILY) && defined(ARCH_CPU_64_BITS) |
| 138 |
| 139 | inline uintptr_t& RegisterContextStackPointer(mcontext_t* context) { |
| 140 | return AsUintPtr(&context->gregs[REG_RSP]); |
| 141 | } |
| 142 |
| 143 | inline uintptr_t& RegisterContextFramePointer(mcontext_t* context) { |
| 144 | return AsUintPtr(&context->gregs[REG_RBP]); |
| 145 | } |
| 146 |
| 147 | inline uintptr_t& RegisterContextInstructionPointer(mcontext_t* context) { |
| 148 | return AsUintPtr(&context->gregs[REG_RIP]); |
| 149 | } |
| 150 |
| 151 | #else // #if defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_32_BITS) |
| 152 |
| 153 | // Placeholders for other POSIX platforms that just return the first |
| 154 | // three register slots in the context. |
| 155 | inline uintptr_t& RegisterContextStackPointer(mcontext_t* context) { |
| 156 | return *reinterpret_cast<uintptr_t*>(context); |
| 157 | } |
| 158 |
| 159 | inline uintptr_t& RegisterContextFramePointer(mcontext_t* context) { |
| 160 | return *(reinterpret_cast<uintptr_t*>(context) + 1); |
| 161 | } |
| 162 |
| 163 | inline uintptr_t& RegisterContextInstructionPointer(mcontext_t* context) { |
| 164 | return *(reinterpret_cast<uintptr_t*>(context) + 2); |
| 165 | } |
| 166 |
| 167 | #endif // #if defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_32_BITS) |
| 168 |
| 169 | #else // #if defined(OS_WIN) |
| 170 |
| 171 | // Placeholders for other platforms. |
| 172 | struct RegisterContext { |
| 173 | uintptr_t stack_pointer; |
| 174 | uintptr_t frame_pointer; |
| 175 | uintptr_t instruction_pointer; |
| 176 | }; |
| 177 |
| 178 | inline uintptr_t& RegisterContextStackPointer(RegisterContext* context) { |
| 179 | return context->stack_pointer; |
| 180 | } |
| 181 |
| 182 | inline uintptr_t& RegisterContextFramePointer(RegisterContext* context) { |
| 183 | return context->frame_pointer; |
| 184 | } |
| 185 |
| 186 | inline uintptr_t& RegisterContextInstructionPointer(RegisterContext* context) { |
| 187 | return context->instruction_pointer; |
| 188 | } |
| 189 |
| 190 | #endif // #if defined(OS_WIN) |
| 191 |
| 192 | } // namespace base |
| 193 |
| 194 | #endif // BASE_PROFILER_REGISTER_CONTEXT_H_ |
| 195 |