| 6 |
| 7 | #include <string.h> |
| 8 |
| 9 | #include <algorithm> |
| 10 | #include <sstream> |
| 11 |
| 12 | #include "base/logging.h" |
| 13 | #include "base/stl_util.h" |
| 14 |
| 15 | #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 16 |
| 17 | #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 18 | #include <pthread.h> |
| 19 | #include "base/process/process_handle.h" |
| 20 | #include "base/threading/platform_thread.h" |
| 21 | #endif |
| 22 |
| 23 | #if defined(OS_MACOSX) |
| 24 | #include <pthread.h> |
| 25 | #endif |
| 26 |
| 27 | #if defined(OS_LINUX) && defined(__GLIBC__) |
| 28 | extern "C" void* __libc_stack_end; |
| 29 | #endif |
| 30 |
| 31 | #endif // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 32 |
| 33 | namespace base { |
| 34 | namespace debug { |
| 35 |
| 36 | namespace { |
| 37 |
| 38 | #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 39 |
| 40 | #if defined(__arm__) && defined(__GNUC__) && !defined(__clang__) |
| 41 | // GCC and LLVM generate slightly different frames on ARM, see |
| 42 | // https://llvm.org/bugs/show_bug.cgi?id=18505 - LLVM generates |
| 43 | // x86-compatible frame, while GCC needs adjustment. |
| 44 | constexpr size_t kStackFrameAdjustment = sizeof(uintptr_t); |
| 45 | #else |
| 46 | constexpr size_t kStackFrameAdjustment = 0; |
| 47 | #endif |
| 48 |
| 49 | uintptr_t GetNextStackFrame(uintptr_t fp) { |
| 50 | const uintptr_t* fp_addr = reinterpret_cast<const uintptr_t*>(fp); |
| 51 | MSAN_UNPOISON(fp_addr, sizeof(uintptr_t)); |
| 52 | return fp_addr[0] - kStackFrameAdjustment; |
| 53 | } |
| 54 |
| 55 | uintptr_t GetStackFramePC(uintptr_t fp) { |
| 56 | const uintptr_t* fp_addr = reinterpret_cast<const uintptr_t*>(fp); |
| 57 | MSAN_UNPOISON(&fp_addr[1], sizeof(uintptr_t)); |
| 58 | return fp_addr[1]; |
| 59 | } |
| 60 |
| 61 | bool IsStackFrameValid(uintptr_t fp, uintptr_t prev_fp, uintptr_t stack_end) { |
| 62 | // With the stack growing downwards, older stack frame must be |
| 63 | // at a greater address that the current one. |
| 64 | if (fp <= prev_fp) return false; |
| 65 |
| 66 | // Assume huge stack frames are bogus. |
| 67 | if (fp - prev_fp > 100000) return false; |
| 68 |
| 69 | // Check alignment. |
| 70 | if (fp & (sizeof(uintptr_t) - 1)) return false; |
| 71 |
| 72 | if (stack_end) { |
| 73 | // Both fp[0] and fp[1] must be within the stack. |
| 74 | if (fp > stack_end - 2 * sizeof(uintptr_t)) return false; |
| 75 |
| 76 | // Additional check to filter out false positives. |
| 77 | if (GetStackFramePC(fp) < 32768) return false; |
| 78 | } |
| 79 |
| 80 | return true; |
| 81 | } |
| 82 |
| 83 | // ScanStackForNextFrame() scans the stack for a valid frame to allow unwinding |
| 84 | // past system libraries. Only supported on Linux where system libraries are |
| 85 | // usually in the middle of the trace: |
| 86 | // |
| 87 | // TraceStackFramePointers |
| 88 | // <more frames from Chrome> |
| 89 | // base::WorkSourceDispatch <-- unwinding stops (next frame is invalid), |
| 90 | // g_main_context_dispatch ScanStackForNextFrame() is called |
| 91 | // <more frames from glib> |
| 92 | // g_main_context_iteration |
| 93 | // base::MessagePumpGlib::Run <-- ScanStackForNextFrame() finds valid frame, |
| 94 | // base::RunLoop::Run unwinding resumes |
| 95 | // <more frames from Chrome> |
| 96 | // __libc_start_main |
| 97 | // |
| 98 | // For stack scanning to be efficient it's very important for the thread to |
| 99 | // be started by Chrome. In that case we naturally terminate unwinding once |
| 100 | // we reach the origin of the stack (i.e. GetStackEnd()). If the thread is |
| 101 | // not started by Chrome (e.g. Android's main thread), then we end up always |
| 102 | // scanning area at the origin of the stack, wasting time and not finding any |
| 103 | // frames (since Android libraries don't have frame pointers). |
| 104 | // |
| 105 | // ScanStackForNextFrame() returns 0 if it couldn't find a valid frame |
| 106 | // (or if stack scanning is not supported on the current platform). |
| 107 | uintptr_t ScanStackForNextFrame(uintptr_t fp, uintptr_t stack_end) { |
| 108 | #if defined(OS_LINUX) |
| 109 | // Enough to resume almost all prematurely terminated traces. |
| 110 | constexpr size_t kMaxStackScanArea = 8192; |
| 111 |
| 112 | if (!stack_end) { |
| 113 | // Too dangerous to scan without knowing where the stack ends. |
| 114 | return 0; |
| 115 | } |
| 116 |
| 117 | fp += sizeof(uintptr_t); // current frame is known to be invalid |
| 118 | uintptr_t last_fp_to_scan = std::min(fp + kMaxStackScanArea, stack_end) - |
| 119 | sizeof(uintptr_t); |
| 120 | for (;fp <= last_fp_to_scan; fp += sizeof(uintptr_t)) { |
| 121 | uintptr_t next_fp = GetNextStackFrame(fp); |
| 122 | if (IsStackFrameValid(next_fp, fp, stack_end)) { |
| 123 | // Check two frames deep. Since stack frame is just a pointer to |
| 124 | // a higher address on the stack, it's relatively easy to find |
| 125 | // something that looks like one. However two linked frames are |
| 126 | // far less likely to be bogus. |
| 127 | uintptr_t next2_fp = GetNextStackFrame(next_fp); |
| 128 | if (IsStackFrameValid(next2_fp, next_fp, stack_end)) { |
| 129 | return fp; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | #endif // defined(OS_LINUX) |
| 134 |
| 135 | return 0; |
| 136 | } |
| 137 |
| 138 | // Links stack frame |fp| to |parent_fp|, so that during stack unwinding |
| 139 | // TraceStackFramePointers() visits |parent_fp| after visiting |fp|. |
| 140 | // Both frame pointers must come from __builtin_frame_address(). |
| 141 | // Returns previous stack frame |fp| was linked to. |
| 142 | void* LinkStackFrames(void* fpp, void* parent_fp) { |
| 143 | uintptr_t fp = reinterpret_cast<uintptr_t>(fpp) - kStackFrameAdjustment; |
| 144 | void* prev_parent_fp = reinterpret_cast<void**>(fp)[0]; |
| 145 | reinterpret_cast<void**>(fp)[0] = parent_fp; |
| 146 | return prev_parent_fp; |
| 147 | } |
| 148 |
| 149 | #endif // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 150 |
| 151 | } // namespace |
| 152 |
| 153 | #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 154 | uintptr_t GetStackEnd() { |
| 155 | #if defined(OS_ANDROID) |
| 156 | // Bionic reads proc/maps on every call to pthread_getattr_np() when called |
| 157 | // from the main thread. So we need to cache end of stack in that case to get |
| 158 | // acceptable performance. |
| 159 | // For all other threads pthread_getattr_np() is fast enough as it just reads |
| 160 | // values from its pthread_t argument. |
| 161 | static uintptr_t main_stack_end = 0; |
| 162 |
| 163 | bool is_main_thread = GetCurrentProcId() == PlatformThread::CurrentId(); |
| 164 | if (is_main_thread && main_stack_end) { |
| 165 | return main_stack_end; |
| 166 | } |
| 167 |
| 168 | uintptr_t stack_begin = 0; |
| 169 | size_t stack_size = 0; |
| 170 | pthread_attr_t attributes; |
| 171 | int error = pthread_getattr_np(pthread_self(), &attributes); |
| 172 | if (!error) { |
| 173 | error = pthread_attr_getstack( |
| 174 | &attributes, reinterpret_cast<void**>(&stack_begin), &stack_size); |
| 175 | pthread_attr_destroy(&attributes); |
| 176 | } |
| 177 | DCHECK(!error); |
| 178 |
| 179 | uintptr_t stack_end = stack_begin + stack_size; |
| 180 | if (is_main_thread) { |
| 181 | main_stack_end = stack_end; |
| 182 | } |
| 183 | return stack_end; // 0 in case of error |
| 184 |
| 185 | #elif defined(OS_LINUX) && defined(__GLIBC__) |
| 186 |
| 187 | if (GetCurrentProcId() == PlatformThread::CurrentId()) { |
| 188 | // For the main thread we have a shortcut. |
| 189 | return reinterpret_cast<uintptr_t>(__libc_stack_end); |
| 190 | } |
| 191 |
| 192 | // No easy way to get end of the stack for non-main threads, |
| 193 | // see crbug.com/617730. |
| 194 | #elif defined(OS_MACOSX) |
| 195 | return reinterpret_cast<uintptr_t>(pthread_get_stackaddr_np(pthread_self())); |
| 196 | #endif |
| 197 |
| 198 | // Don't know how to get end of the stack. |
| 199 | return 0; |
| 200 | } |
| 201 | #endif // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 202 |
| 203 | StackTrace::StackTrace() : StackTrace(base::size(trace_)) {} |
| 204 |
| 205 | StackTrace::StackTrace(size_t count) { |
| 206 | count_ = CollectStackTrace(trace_, std::min(count, base::size(trace_))); |
| 207 | } |
| 208 |
| 209 | StackTrace::StackTrace(const void* const* trace, size_t count) { |
| 210 | count = std::min(count, base::size(trace_)); |
| 211 | if (count) |
| 212 | memcpy(trace_, trace, count * sizeof(trace_[0])); |
| 213 | count_ = count; |
| 214 | } |
| 215 |
| 216 | const void *const *StackTrace::Addresses(size_t* count) const { |
| 217 | *count = count_; |
| 218 | if (count_) |
| 219 | return trace_; |
| 220 | return nullptr; |
| 221 | } |
| 222 |
| 223 | void StackTrace::Print() const { |
| 224 | PrintWithPrefix(nullptr); |
| 225 | } |
| 226 |
| 227 | void StackTrace::OutputToStream(std::ostream* os) const { |
| 228 | OutputToStreamWithPrefix(os, nullptr); |
| 229 | } |
| 230 |
| 231 | std::string StackTrace::ToString() const { |
| 232 | return ToStringWithPrefix(nullptr); |
| 233 | } |
| 234 | std::string StackTrace::ToStringWithPrefix(const char* prefix_string) const { |
| 235 | std::stringstream stream; |
| 236 | #if !defined(__UCLIBC__) && !defined(_AIX) |
| 237 | OutputToStreamWithPrefix(&stream, prefix_string); |
| 238 | #endif |
| 239 | return stream.str(); |
| 240 | } |
| 241 |
| 242 | std::ostream& operator<<(std::ostream& os, const StackTrace& s) { |
| 243 | #if !defined(__UCLIBC__) & !defined(_AIX) |
| 244 | s.OutputToStream(&os); |
| 245 | #else |
| 246 | os << "StackTrace::OutputToStream not implemented."; |
| 247 | #endif |
| 248 | return os; |
| 249 | } |
| 250 |
| 251 | #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 252 |
| 253 | size_t TraceStackFramePointers(const void** out_trace, |
| 254 | size_t max_depth, |
| 255 | size_t skip_initial) { |
| 256 | // Usage of __builtin_frame_address() enables frame pointers in this |
| 257 | // function even if they are not enabled globally. So 'fp' will always |
| 258 | // be valid. |
| 259 | uintptr_t fp = reinterpret_cast<uintptr_t>(__builtin_frame_address(0)) - |
| 260 | kStackFrameAdjustment; |
| 261 |
| 262 | uintptr_t stack_end = GetStackEnd(); |
| 263 |
| 264 | size_t depth = 0; |
| 265 | while (depth < max_depth) { |
| 266 | if (skip_initial != 0) { |
| 267 | skip_initial--; |
| 268 | } else { |
| 269 | out_trace[depth++] = reinterpret_cast<const void*>(GetStackFramePC(fp)); |
| 270 | } |
| 271 |
| 272 | uintptr_t next_fp = GetNextStackFrame(fp); |
| 273 | if (IsStackFrameValid(next_fp, fp, stack_end)) { |
| 274 | fp = next_fp; |
| 275 | continue; |
| 276 | } |
| 277 |
| 278 | next_fp = ScanStackForNextFrame(fp, stack_end); |
| 279 | if (next_fp) { |
| 280 | fp = next_fp; |
| 281 | continue; |
| 282 | } |
| 283 |
| 284 | // Failed to find next frame. |
| 285 | break; |
| 286 | } |
| 287 |
| 288 | return depth; |
| 289 | } |
| 290 |
| 291 | ScopedStackFrameLinker::ScopedStackFrameLinker(void* fp, void* parent_fp) |
| 292 | : fp_(fp), |
| 293 | parent_fp_(parent_fp), |
| 294 | original_parent_fp_(LinkStackFrames(fp, parent_fp)) {} |
| 295 |
| 296 | ScopedStackFrameLinker::~ScopedStackFrameLinker() { |
| 297 | void* previous_parent_fp = LinkStackFrames(fp_, original_parent_fp_); |
| 298 | CHECK_EQ(parent_fp_, previous_parent_fp) |
| 299 | << "Stack frame's parent pointer has changed!"; |
| 300 | } |
| 301 |
| 302 | #endif // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 303 |
| 304 | } // namespace debug |
| 305 | } // namespace base |
| 306 |