| 6 |
| 7 | #include <linux/futex.h> |
| 8 | #include <signal.h> |
| 9 | #include <sys/ucontext.h> |
| 10 | #include <syscall.h> |
| 11 |
| 12 | #include <atomic> |
| 13 |
| 14 | #include "base/profiler/register_context.h" |
| 15 | #include "base/profiler/stack_buffer.h" |
| 16 | #include "base/profiler/suspendable_thread_delegate.h" |
| 17 | #include "base/trace_event/trace_event.h" |
| 18 | #include "build/build_config.h" |
| 19 |
| 20 | namespace base { |
| 21 |
| 22 | namespace { |
| 23 |
| 24 | // Waitable event implementation with futex and without DCHECK(s), since signal |
| 25 | // handlers cannot allocate memory or use pthread api. |
| 26 | class AsyncSafeWaitableEvent { |
| 27 | public: |
| 28 | AsyncSafeWaitableEvent() { futex_.store(0, std::memory_order_release); } |
| 29 | ~AsyncSafeWaitableEvent() {} |
| 30 |
| 31 | bool Wait() { |
| 32 | // futex() can wake up spuriously if this memory address was previously used |
| 33 | // for a pthread mutex. So, also check the condition. |
| 34 | while (true) { |
| 35 | int res = |
| 36 | syscall(SYS_futex, futex_int_ptr(), FUTEX_WAIT | FUTEX_PRIVATE_FLAG, |
| 37 | 0, nullptr, nullptr, 0); |
| 38 | if (futex_.load(std::memory_order_acquire) != 0) |
| 39 | return true; |
| 40 | if (res != 0) |
| 41 | return false; |
| 42 | } |
| 43 | } |
| 44 |
| 45 | void Signal() { |
| 46 | futex_.store(1, std::memory_order_release); |
| 47 | syscall(SYS_futex, futex_int_ptr(), FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1, |
| 48 | nullptr, nullptr, 0); |
| 49 | } |
| 50 |
| 51 | private: |
| 52 | // Provides a pointer to the atomic's storage. std::atomic_int has standard |
| 53 | // layout so its address can be used for the pointer as long as it only |
| 54 | // contains the int. |
| 55 | int* futex_int_ptr() { |
| 56 | static_assert(sizeof(futex_) == sizeof(int), |
| 57 | "Expected std::atomic_int to be the same size as int"); |
| 58 | return reinterpret_cast<int*>(&futex_); |
| 59 | } |
| 60 |
| 61 | std::atomic_int futex_{0}; |
| 62 | }; |
| 63 |
| 64 | // Scoped signal event that calls Signal on the AsyncSafeWaitableEvent at |
| 65 | // destructor. |
| 66 | class ScopedEventSignaller { |
| 67 | public: |
| 68 | ScopedEventSignaller(AsyncSafeWaitableEvent* event) : event_(event) {} |
| 69 | ~ScopedEventSignaller() { event_->Signal(); } |
| 70 |
| 71 | private: |
| 72 | AsyncSafeWaitableEvent* event_; |
| 73 | }; |
| 74 |
| 75 | // Struct to store the arguments to the signal handler. |
| 76 | struct HandlerParams { |
| 77 | uintptr_t stack_base_address; |
| 78 |
| 79 | // The event is signalled when signal handler is done executing. |
| 80 | AsyncSafeWaitableEvent* event; |
| 81 |
| 82 | // Return values: |
| 83 |
| 84 | // Successfully copied the stack segment. |
| 85 | bool* success; |
| 86 |
| 87 | // The thread context of the leaf function. |
| 88 | mcontext_t* context; |
| 89 |
| 90 | // Buffer to copy the stack segment. |
| 91 | StackBuffer* stack_buffer; |
| 92 | const uint8_t** stack_copy_bottom; |
| 93 |
| 94 | // The timestamp when the stack was copied. |
| 95 | TimeTicks* timestamp; |
| 96 |
| 97 | // The delegate provided to the StackCopier. |
| 98 | StackCopier::Delegate* stack_copier_delegate; |
| 99 | }; |
| 100 |
| 101 | // Pointer to the parameters to be "passed" to the CopyStackSignalHandler() from |
| 102 | // the sampling thread to the sampled (stopped) thread. This value is set just |
| 103 | // before sending the signal to the thread and reset when the handler is done. |
| 104 | std::atomic<HandlerParams*> g_handler_params; |
| 105 |
| 106 | // CopyStackSignalHandler is invoked on the stopped thread and records the |
| 107 | // thread's stack and register context at the time the signal was received. This |
| 108 | // function may only call reentrant code. |
| 109 | void CopyStackSignalHandler(int n, siginfo_t* siginfo, void* sigcontext) { |
| 110 | HandlerParams* params = g_handler_params.load(std::memory_order_acquire); |
| 111 |
| 112 | // TimeTicks::Now() is implemented in terms of clock_gettime on Linux, which |
| 113 | // is signal safe per the signal-safety(7) man page. |
| 114 | *params->timestamp = TimeTicks::Now(); |
| 115 |
| 116 | ScopedEventSignaller e(params->event); |
| 117 | *params->success = false; |
| 118 |
| 119 | const ucontext_t* ucontext = static_cast<ucontext_t*>(sigcontext); |
| 120 | memcpy(params->context, &ucontext->uc_mcontext, sizeof(mcontext_t)); |
| 121 |
| 122 | const uintptr_t bottom = RegisterContextStackPointer(params->context); |
| 123 | const uintptr_t top = params->stack_base_address; |
| 124 | if ((top - bottom) > params->stack_buffer->size()) { |
| 125 | // The stack exceeds the size of the allocated buffer. The buffer is sized |
| 126 | // such that this shouldn't happen under typical execution so we can safely |
| 127 | // punt in this situation. |
| 128 | return; |
| 129 | } |
| 130 |
| 131 | params->stack_copier_delegate->OnStackCopy(); |
| 132 |
| 133 | *params->stack_copy_bottom = |
| 134 | StackCopierSignal::CopyStackContentsAndRewritePointers( |
| 135 | reinterpret_cast<uint8_t*>(bottom), reinterpret_cast<uintptr_t*>(top), |
| 136 | StackBuffer::kPlatformStackAlignment, params->stack_buffer->buffer()); |
| 137 |
| 138 | *params->success = true; |
| 139 | } |
| 140 |
| 141 | // Sets the global handler params for the signal handler function. |
| 142 | class ScopedSetSignalHandlerParams { |
| 143 | public: |
| 144 | ScopedSetSignalHandlerParams(HandlerParams* params) { |
| 145 | g_handler_params.store(params, std::memory_order_release); |
| 146 | } |
| 147 |
| 148 | ~ScopedSetSignalHandlerParams() { |
| 149 | g_handler_params.store(nullptr, std::memory_order_release); |
| 150 | } |
| 151 | }; |
| 152 |
| 153 | class ScopedSigaction { |
| 154 | public: |
| 155 | ScopedSigaction(int signal, |
| 156 | struct sigaction* action, |
| 157 | struct sigaction* original_action) |
| 158 | : signal_(signal), |
| 159 | action_(action), |
| 160 | original_action_(original_action), |
| 161 | succeeded_(sigaction(signal, action, original_action) == 0) {} |
| 162 |
| 163 | bool succeeded() const { return succeeded_; } |
| 164 |
| 165 | ~ScopedSigaction() { |
| 166 | if (!succeeded_) |
| 167 | return; |
| 168 |
| 169 | bool reset_succeeded = sigaction(signal_, original_action_, action_) == 0; |
| 170 | DCHECK(reset_succeeded); |
| 171 | } |
| 172 |
| 173 | private: |
| 174 | const int signal_; |
| 175 | struct sigaction* const action_; |
| 176 | struct sigaction* const original_action_; |
| 177 | const bool succeeded_; |
| 178 | }; |
| 179 |
| 180 | } // namespace |
| 181 |
| 182 | StackCopierSignal::StackCopierSignal( |
| 183 | std::unique_ptr<ThreadDelegate> thread_delegate) |
| 184 | : thread_delegate_(std::move(thread_delegate)) {} |
| 185 |
| 186 | StackCopierSignal::~StackCopierSignal() = default; |
| 187 |
| 188 | bool StackCopierSignal::CopyStack(StackBuffer* stack_buffer, |
| 189 | uintptr_t* stack_top, |
| 190 | TimeTicks* timestamp, |
| 191 | RegisterContext* thread_context, |
| 192 | Delegate* delegate) { |
| 193 | AsyncSafeWaitableEvent wait_event; |
| 194 | bool copied = false; |
| 195 | const uint8_t* stack_copy_bottom = nullptr; |
| 196 | const uintptr_t stack_base_address = thread_delegate_->GetStackBaseAddress(); |
| 197 | HandlerParams params = {stack_base_address, &wait_event, &copied, |
| 198 | thread_context, stack_buffer, &stack_copy_bottom, |
| 199 | timestamp, delegate}; |
| 200 | { |
| 201 | ScopedSetSignalHandlerParams scoped_handler_params(¶ms); |
| 202 |
| 203 | // Set the signal handler for the thread to the stack copy function. |
| 204 | struct sigaction action; |
| 205 | struct sigaction original_action; |
| 206 | memset(&action, 0, sizeof(action)); |
| 207 | action.sa_sigaction = CopyStackSignalHandler; |
| 208 | action.sa_flags = SA_RESTART | SA_SIGINFO; |
| 209 | sigemptyset(&action.sa_mask); |
| 210 | TRACE_EVENT_BEGIN0(TRACE_DISABLED_BY_DEFAULT("cpu_profiler.debug"), |
| 211 | "StackCopierSignal copy stack"); |
| 212 | // SIGURG is chosen here because we observe no crashes with this signal and |
| 213 | // neither Chrome or the AOSP sets up a special handler for this signal. |
| 214 | ScopedSigaction scoped_sigaction(SIGURG, &action, &original_action); |
| 215 | if (!scoped_sigaction.succeeded()) |
| 216 | return false; |
| 217 |
| 218 | if (syscall(SYS_tgkill, getpid(), thread_delegate_->GetThreadId(), |
| 219 | SIGURG) != 0) { |
| 220 | NOTREACHED(); |
| 221 | return false; |
| 222 | } |
| 223 | bool finished_waiting = wait_event.Wait(); |
| 224 | TRACE_EVENT_END0(TRACE_DISABLED_BY_DEFAULT("cpu_profiler.debug"), |
| 225 | "StackCopierSignal copy stack"); |
| 226 | if (!finished_waiting) { |
| 227 | NOTREACHED(); |
| 228 | return false; |
| 229 | } |
| 230 | } |
| 231 |
| 232 | delegate->OnThreadResume(); |
| 233 |
| 234 | const uintptr_t bottom = RegisterContextStackPointer(params.context); |
| 235 | for (uintptr_t* reg : |
| 236 | thread_delegate_->GetRegistersToRewrite(thread_context)) { |
| 237 | *reg = StackCopierSignal::RewritePointerIfInOriginalStack( |
| 238 | reinterpret_cast<uint8_t*>(bottom), |
| 239 | reinterpret_cast<uintptr_t*>(stack_base_address), stack_copy_bottom, |
| 240 | *reg); |
| 241 | } |
| 242 |
| 243 | *stack_top = reinterpret_cast<uintptr_t>(stack_copy_bottom) + |
| 244 | (stack_base_address - bottom); |
| 245 |
| 246 | return copied; |
| 247 | } |
| 248 |
| 249 | } // namespace base |
| 250 |