OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/debug/stack_trace.h" | 5 #include "base/debug/stack_trace.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <sstream> | 10 #include <sstream> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 | 14 |
15 #if HAVE_TRACE_STACK_FRAME_POINTERS | 15 #if CAN_UNWIND_WITH_FRAME_POINTERS |
16 | 16 |
17 #if defined(OS_LINUX) || defined(OS_ANDROID) | 17 #if defined(OS_LINUX) || defined(OS_ANDROID) |
18 #include <pthread.h> | 18 #include <pthread.h> |
19 #include "base/process/process_handle.h" | 19 #include "base/process/process_handle.h" |
20 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
21 #endif | 21 #endif |
22 | 22 |
23 #if defined(OS_MACOSX) | 23 #if defined(OS_MACOSX) |
24 #include <pthread.h> | 24 #include <pthread.h> |
25 #endif | 25 #endif |
26 | 26 |
27 #if defined(OS_LINUX) && defined(__GLIBC__) | 27 #if defined(OS_LINUX) && defined(__GLIBC__) |
28 extern "C" void* __libc_stack_end; | 28 extern "C" void* __libc_stack_end; |
29 #endif | 29 #endif |
30 | 30 |
31 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | 31 #endif // CAN_UNWIND_WITH_FRAME_POINTERS |
32 | 32 |
33 namespace base { | 33 namespace base { |
34 namespace debug { | 34 namespace debug { |
35 | 35 |
36 namespace { | 36 namespace { |
37 | 37 |
38 #if HAVE_TRACE_STACK_FRAME_POINTERS && !defined(OS_WIN) | 38 #if CAN_UNWIND_WITH_FRAME_POINTERS && !defined(OS_WIN) |
39 | 39 |
40 #if defined(__arm__) && defined(__GNUC__) && !defined(__clang__) | 40 #if defined(__arm__) && defined(__GNUC__) && !defined(__clang__) |
41 // GCC and LLVM generate slightly different frames on ARM, see | 41 // GCC and LLVM generate slightly different frames on ARM, see |
42 // https://llvm.org/bugs/show_bug.cgi?id=18505 - LLVM generates | 42 // https://llvm.org/bugs/show_bug.cgi?id=18505 - LLVM generates |
43 // x86-compatible frame, while GCC needs adjustment. | 43 // x86-compatible frame, while GCC needs adjustment. |
44 constexpr size_t kStackFrameAdjustment = sizeof(uintptr_t); | 44 constexpr size_t kStackFrameAdjustment = sizeof(uintptr_t); |
45 #else | 45 #else |
46 constexpr size_t kStackFrameAdjustment = 0; | 46 constexpr size_t kStackFrameAdjustment = 0; |
47 #endif | 47 #endif |
48 | 48 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 // TraceStackFramePointers() visits |parent_fp| after visiting |fp|. | 135 // TraceStackFramePointers() visits |parent_fp| after visiting |fp|. |
136 // Both frame pointers must come from __builtin_frame_address(). | 136 // Both frame pointers must come from __builtin_frame_address(). |
137 // Returns previous stack frame |fp| was linked to. | 137 // Returns previous stack frame |fp| was linked to. |
138 void* LinkStackFrames(void* fpp, void* parent_fp) { | 138 void* LinkStackFrames(void* fpp, void* parent_fp) { |
139 uintptr_t fp = reinterpret_cast<uintptr_t>(fpp) - kStackFrameAdjustment; | 139 uintptr_t fp = reinterpret_cast<uintptr_t>(fpp) - kStackFrameAdjustment; |
140 void* prev_parent_fp = reinterpret_cast<void**>(fp)[0]; | 140 void* prev_parent_fp = reinterpret_cast<void**>(fp)[0]; |
141 reinterpret_cast<void**>(fp)[0] = parent_fp; | 141 reinterpret_cast<void**>(fp)[0] = parent_fp; |
142 return prev_parent_fp; | 142 return prev_parent_fp; |
143 } | 143 } |
144 | 144 |
145 #endif // HAVE_TRACE_STACK_FRAME_POINTERS && !defined(OS_WIN) | 145 #endif // CAN_UNWIND_WITH_FRAME_POINTERS && !defined(OS_WIN) |
146 | 146 |
147 } // namespace | 147 } // namespace |
148 | 148 |
149 #if HAVE_TRACE_STACK_FRAME_POINTERS | 149 #if CAN_UNWIND_WITH_FRAME_POINTERS |
150 uintptr_t GetStackEnd() { | 150 uintptr_t GetStackEnd() { |
151 #if defined(OS_ANDROID) | 151 #if defined(OS_ANDROID) |
152 // Bionic reads proc/maps on every call to pthread_getattr_np() when called | 152 // Bionic reads proc/maps on every call to pthread_getattr_np() when called |
153 // from the main thread. So we need to cache end of stack in that case to get | 153 // from the main thread. So we need to cache end of stack in that case to get |
154 // acceptable performance. | 154 // acceptable performance. |
155 // For all other threads pthread_getattr_np() is fast enough as it just reads | 155 // For all other threads pthread_getattr_np() is fast enough as it just reads |
156 // values from its pthread_t argument. | 156 // values from its pthread_t argument. |
157 static uintptr_t main_stack_end = 0; | 157 static uintptr_t main_stack_end = 0; |
158 | 158 |
159 bool is_main_thread = GetCurrentProcId() == PlatformThread::CurrentId(); | 159 bool is_main_thread = GetCurrentProcId() == PlatformThread::CurrentId(); |
(...skipping 27 matching lines...) Expand all Loading... |
187 | 187 |
188 // No easy way to get end of the stack for non-main threads, | 188 // No easy way to get end of the stack for non-main threads, |
189 // see crbug.com/617730. | 189 // see crbug.com/617730. |
190 #elif defined(OS_MACOSX) | 190 #elif defined(OS_MACOSX) |
191 return reinterpret_cast<uintptr_t>(pthread_get_stackaddr_np(pthread_self())); | 191 return reinterpret_cast<uintptr_t>(pthread_get_stackaddr_np(pthread_self())); |
192 #endif | 192 #endif |
193 | 193 |
194 // Don't know how to get end of the stack. | 194 // Don't know how to get end of the stack. |
195 return 0; | 195 return 0; |
196 } | 196 } |
197 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | 197 #endif // CAN_UNWIND_WITH_FRAME_POINTERS |
198 | 198 |
199 StackTrace::StackTrace() : StackTrace(arraysize(trace_)) {} | 199 StackTrace::StackTrace() : StackTrace(arraysize(trace_)) {} |
200 | 200 |
201 StackTrace::StackTrace(const void* const* trace, size_t count) { | 201 StackTrace::StackTrace(const void* const* trace, size_t count) { |
202 count = std::min(count, arraysize(trace_)); | 202 count = std::min(count, arraysize(trace_)); |
203 if (count) | 203 if (count) |
204 memcpy(trace_, trace, count * sizeof(trace_[0])); | 204 memcpy(trace_, trace, count * sizeof(trace_[0])); |
205 count_ = count; | 205 count_ = count; |
206 } | 206 } |
207 | 207 |
208 const void *const *StackTrace::Addresses(size_t* count) const { | 208 const void *const *StackTrace::Addresses(size_t* count) const { |
209 *count = count_; | 209 *count = count_; |
210 if (count_) | 210 if (count_) |
211 return trace_; | 211 return trace_; |
212 return NULL; | 212 return NULL; |
213 } | 213 } |
214 | 214 |
215 std::string StackTrace::ToString() const { | 215 std::string StackTrace::ToString() const { |
216 std::stringstream stream; | 216 std::stringstream stream; |
217 #if !defined(__UCLIBC__) | 217 #if !defined(__UCLIBC__) |
218 OutputToStream(&stream); | 218 OutputToStream(&stream); |
219 #endif | 219 #endif |
220 return stream.str(); | 220 return stream.str(); |
221 } | 221 } |
222 | 222 |
223 #if HAVE_TRACE_STACK_FRAME_POINTERS | 223 #if CAN_UNWIND_WITH_FRAME_POINTERS |
224 | 224 |
225 size_t TraceStackFramePointers(const void** out_trace, | 225 size_t TraceStackFramePointers(const void** out_trace, |
226 size_t max_depth, | 226 size_t max_depth, |
227 size_t skip_initial) { | 227 size_t skip_initial) { |
228 // TODO(699863): Merge the frame-pointer based stack unwinder into the | 228 // TODO(699863): Merge the frame-pointer based stack unwinder into the |
229 // base::debug::StackTrace platform-specific implementation files. | 229 // base::debug::StackTrace platform-specific implementation files. |
230 #if defined(OS_WIN) | 230 #if defined(OS_WIN) |
231 StackTrace stack(max_depth); | 231 StackTrace stack(max_depth); |
232 size_t count = 0; | 232 size_t count = 0; |
233 const void* const* frames = stack.Addresses(&count); | 233 const void* const* frames = stack.Addresses(&count); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 parent_fp_(parent_fp), | 279 parent_fp_(parent_fp), |
280 original_parent_fp_(LinkStackFrames(fp, parent_fp)) {} | 280 original_parent_fp_(LinkStackFrames(fp, parent_fp)) {} |
281 | 281 |
282 ScopedStackFrameLinker::~ScopedStackFrameLinker() { | 282 ScopedStackFrameLinker::~ScopedStackFrameLinker() { |
283 void* previous_parent_fp = LinkStackFrames(fp_, original_parent_fp_); | 283 void* previous_parent_fp = LinkStackFrames(fp_, original_parent_fp_); |
284 CHECK_EQ(parent_fp_, previous_parent_fp) | 284 CHECK_EQ(parent_fp_, previous_parent_fp) |
285 << "Stack frame's parent pointer has changed!"; | 285 << "Stack frame's parent pointer has changed!"; |
286 } | 286 } |
287 #endif // !defined(OS_WIN) | 287 #endif // !defined(OS_WIN) |
288 | 288 |
289 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | 289 #endif // CAN_UNWIND_WITH_FRAME_POINTERS |
290 | 290 |
291 } // namespace debug | 291 } // namespace debug |
292 } // namespace base | 292 } // namespace base |
OLD | NEW |