Chromium Code Reviews| 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 <limits> | |
| 10 #include <sstream> | 11 #include <sstream> |
| 11 | 12 |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 | 14 |
| 14 #if HAVE_TRACE_STACK_FRAME_POINTERS && defined(OS_ANDROID) | 15 #if HAVE_TRACE_STACK_FRAME_POINTERS |
|
Primiano Tucci (use gerrit)
2016/08/03 09:32:03
I think you still need OS_POSIX otherwise that inc
Dmitry Skiba
2016/08/11 16:52:38
Done.
| |
| 15 #include <pthread.h> | 16 #include <pthread.h> |
| 16 #include "base/process/process_handle.h" | 17 #include "base/process/process_handle.h" |
| 17 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
| 18 #endif | 19 #endif |
| 19 | 20 |
| 20 namespace base { | 21 namespace base { |
| 21 namespace debug { | 22 namespace debug { |
| 22 | 23 |
| 23 StackTrace::StackTrace(const void* const* trace, size_t count) { | 24 StackTrace::StackTrace(const void* const* trace, size_t count) { |
| 24 count = std::min(count, arraysize(trace_)); | 25 count = std::min(count, arraysize(trace_)); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 42 #if !defined(__UCLIBC__) | 43 #if !defined(__UCLIBC__) |
| 43 OutputToStream(&stream); | 44 OutputToStream(&stream); |
| 44 #endif | 45 #endif |
| 45 return stream.str(); | 46 return stream.str(); |
| 46 } | 47 } |
| 47 | 48 |
| 48 #if HAVE_TRACE_STACK_FRAME_POINTERS | 49 #if HAVE_TRACE_STACK_FRAME_POINTERS |
| 49 | 50 |
| 50 #if defined(OS_ANDROID) | 51 #if defined(OS_ANDROID) |
| 51 | 52 |
| 53 #define HAVE_GET_STACK_END 1 | |
| 54 | |
| 52 static uintptr_t GetStackEnd() { | 55 static uintptr_t GetStackEnd() { |
|
Primiano Tucci (use gerrit)
2016/08/03 09:32:03
I think all this become more readable and easy to
Dmitry Skiba
2016/08/11 16:52:38
Done.
| |
| 53 // Bionic reads proc/maps on every call to pthread_getattr_np() when called | 56 // Bionic reads proc/maps on every call to pthread_getattr_np() when called |
| 54 // from the main thread. So we need to cache end of stack in that case to get | 57 // from the main thread. So we need to cache end of stack in that case to get |
| 55 // acceptable performance. | 58 // acceptable performance. |
| 56 // For all other threads pthread_getattr_np() is fast enough as it just reads | 59 // For all other threads pthread_getattr_np() is fast enough as it just reads |
| 57 // values from its pthread_t argument. | 60 // values from its pthread_t argument. |
| 58 static uintptr_t main_stack_end = 0; | 61 static uintptr_t main_stack_end = 0; |
| 59 | 62 |
| 60 bool is_main_thread = GetCurrentProcId() == PlatformThread::CurrentId(); | 63 bool is_main_thread = GetCurrentProcId() == PlatformThread::CurrentId(); |
| 61 | 64 |
| 62 if (is_main_thread && main_stack_end) { | 65 if (is_main_thread && main_stack_end) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 78 | 81 |
| 79 uintptr_t stack_end = stack_begin + stack_size; | 82 uintptr_t stack_end = stack_begin + stack_size; |
| 80 if (is_main_thread) { | 83 if (is_main_thread) { |
| 81 main_stack_end = stack_end; | 84 main_stack_end = stack_end; |
| 82 } | 85 } |
| 83 return stack_end; | 86 return stack_end; |
| 84 } | 87 } |
| 85 | 88 |
| 86 #endif // defined(OS_ANDROID) | 89 #endif // defined(OS_ANDROID) |
| 87 | 90 |
| 91 #if defined(__GLIBC__) | |
|
Primiano Tucci (use gerrit)
2016/08/03 09:32:03
Just change lines 89-91 to be:
#elif defined(OS_L
Dmitry Skiba
2016/08/11 16:52:38
Done.
| |
| 92 | |
| 93 #define HAVE_GET_STACK_END 1 | |
| 94 | |
| 95 extern "C" void* __libc_stack_end; | |
|
Primiano Tucci (use gerrit)
2016/08/03 09:32:04
Ok it seems we already depend on this in StackFram
Dmitry Skiba
2016/08/11 16:52:38
Acknowledged.
| |
| 96 | |
| 97 static uintptr_t GetStackEnd() { | |
| 98 if (GetCurrentProcId() == PlatformThread::CurrentId()) { | |
| 99 // For the main thread we have a shortcut. | |
| 100 return reinterpret_cast<uintptr_t>(__libc_stack_end); | |
| 101 } else { | |
| 102 // No easy way to get stack end for non-main threads, | |
| 103 // see crbug.com/617730. | |
| 104 return std::numeric_limits<uintptr_t>::max(); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 #endif // defined(__GLIBC__) | |
| 109 | |
| 88 size_t TraceStackFramePointers(const void** out_trace, | 110 size_t TraceStackFramePointers(const void** out_trace, |
| 89 size_t max_depth, | 111 size_t max_depth, |
| 90 size_t skip_initial) { | 112 size_t skip_initial) { |
| 91 // Usage of __builtin_frame_address() enables frame pointers in this | 113 // Usage of __builtin_frame_address() enables frame pointers in this |
| 92 // function even if they are not enabled globally. So 'sp' will always | 114 // function even if they are not enabled globally. So 'sp' will always |
| 93 // be valid. | 115 // be valid. |
| 94 uintptr_t sp = reinterpret_cast<uintptr_t>(__builtin_frame_address(0)); | 116 uintptr_t sp = reinterpret_cast<uintptr_t>(__builtin_frame_address(0)); |
| 95 | 117 |
| 96 #if defined(OS_ANDROID) | 118 #if HAVE_GET_STACK_END |
| 97 uintptr_t stack_end = GetStackEnd(); | 119 uintptr_t stack_end = GetStackEnd(); |
| 98 #endif | 120 #endif |
| 99 | 121 |
| 100 size_t depth = 0; | 122 size_t depth = 0; |
| 101 while (depth < max_depth) { | 123 while (depth < max_depth) { |
| 102 #if defined(__arm__) && defined(__GNUC__) && !defined(__clang__) | 124 #if defined(__arm__) && defined(__GNUC__) && !defined(__clang__) |
| 103 // GCC and LLVM generate slightly different frames on ARM, see | 125 // GCC and LLVM generate slightly different frames on ARM, see |
| 104 // https://llvm.org/bugs/show_bug.cgi?id=18505 - LLVM generates | 126 // https://llvm.org/bugs/show_bug.cgi?id=18505 - LLVM generates |
| 105 // x86-compatible frame, while GCC needs adjustment. | 127 // x86-compatible frame, while GCC needs adjustment. |
| 106 sp -= sizeof(uintptr_t); | 128 sp -= sizeof(uintptr_t); |
| 107 #endif | 129 #endif |
| 108 | 130 |
| 109 #if defined(OS_ANDROID) | 131 #if HAVE_GET_STACK_END |
| 110 // Both sp[0] and s[1] must be valid. | 132 // Both sp[0] and s[1] must be valid. |
| 111 if (sp + 2 * sizeof(uintptr_t) > stack_end) { | 133 if (sp + 2 * sizeof(uintptr_t) > stack_end) { |
| 112 break; | 134 break; |
| 113 } | 135 } |
| 114 #endif | 136 #endif |
| 115 | 137 |
| 116 if (skip_initial != 0) { | 138 if (skip_initial != 0) { |
| 117 skip_initial--; | 139 skip_initial--; |
| 118 } else { | 140 } else { |
| 119 out_trace[depth++] = reinterpret_cast<const void**>(sp)[1]; | 141 out_trace[depth++] = reinterpret_cast<const void**>(sp)[1]; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 138 } | 160 } |
| 139 } | 161 } |
| 140 | 162 |
| 141 return depth; | 163 return depth; |
| 142 } | 164 } |
| 143 | 165 |
| 144 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | 166 #endif // HAVE_TRACE_STACK_FRAME_POINTERS |
| 145 | 167 |
| 146 } // namespace debug | 168 } // namespace debug |
| 147 } // namespace base | 169 } // namespace base |
| OLD | NEW |