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/macros.h" | 12 #include "base/macros.h" |
13 | 13 |
| 14 #if HAVE_TRACE_STACK_FRAME_POINTERS && defined(OS_ANDROID) |
| 15 #include <pthread.h> |
| 16 #include "base/process/process_handle.h" |
| 17 #include "base/threading/platform_thread.h" |
| 18 #endif |
| 19 |
14 namespace base { | 20 namespace base { |
15 namespace debug { | 21 namespace debug { |
16 | 22 |
17 StackTrace::StackTrace(const void* const* trace, size_t count) { | 23 StackTrace::StackTrace(const void* const* trace, size_t count) { |
18 count = std::min(count, arraysize(trace_)); | 24 count = std::min(count, arraysize(trace_)); |
19 if (count) | 25 if (count) |
20 memcpy(trace_, trace, count * sizeof(trace_[0])); | 26 memcpy(trace_, trace, count * sizeof(trace_[0])); |
21 count_ = count; | 27 count_ = count; |
22 } | 28 } |
23 | 29 |
(...skipping 10 matching lines...) Expand all Loading... |
34 std::string StackTrace::ToString() const { | 40 std::string StackTrace::ToString() const { |
35 std::stringstream stream; | 41 std::stringstream stream; |
36 #if !defined(__UCLIBC__) | 42 #if !defined(__UCLIBC__) |
37 OutputToStream(&stream); | 43 OutputToStream(&stream); |
38 #endif | 44 #endif |
39 return stream.str(); | 45 return stream.str(); |
40 } | 46 } |
41 | 47 |
42 #if HAVE_TRACE_STACK_FRAME_POINTERS | 48 #if HAVE_TRACE_STACK_FRAME_POINTERS |
43 | 49 |
| 50 #if defined(OS_ANDROID) |
| 51 |
| 52 static uintptr_t GetStackEnd() { |
| 53 // 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 |
| 55 // acceptable performance. |
| 56 // For all other threads pthread_getattr_np() is fast enough as it just reads |
| 57 // values from its pthread_t argument. |
| 58 static uintptr_t main_stack_end = 0; |
| 59 |
| 60 bool is_main_thread = GetCurrentProcId() == PlatformThread::CurrentId(); |
| 61 |
| 62 if (is_main_thread && main_stack_end) { |
| 63 return main_stack_end; |
| 64 } |
| 65 |
| 66 uintptr_t stack_begin = 0; |
| 67 size_t stack_size = 0; |
| 68 pthread_attr_t attributes; |
| 69 int error = pthread_getattr_np(pthread_self(), &attributes); |
| 70 if (!error) { |
| 71 error = pthread_attr_getstack( |
| 72 &attributes, |
| 73 reinterpret_cast<void**>(&stack_begin), |
| 74 &stack_size); |
| 75 pthread_attr_destroy(&attributes); |
| 76 } |
| 77 DCHECK(!error); |
| 78 |
| 79 uintptr_t stack_end = stack_begin + stack_size; |
| 80 if (is_main_thread) { |
| 81 main_stack_end = stack_end; |
| 82 } |
| 83 return stack_end; |
| 84 } |
| 85 |
| 86 #endif // defined(OS_ANDROID) |
| 87 |
44 size_t TraceStackFramePointers(const void** out_trace, | 88 size_t TraceStackFramePointers(const void** out_trace, |
45 size_t max_depth, | 89 size_t max_depth, |
46 size_t skip_initial) { | 90 size_t skip_initial) { |
47 // Usage of __builtin_frame_address() enables frame pointers in this | 91 // Usage of __builtin_frame_address() enables frame pointers in this |
48 // function even if they are not enabled globally. So 'sp' will always | 92 // function even if they are not enabled globally. So 'sp' will always |
49 // be valid. | 93 // be valid. |
50 uintptr_t sp = reinterpret_cast<uintptr_t>(__builtin_frame_address(0)); | 94 uintptr_t sp = reinterpret_cast<uintptr_t>(__builtin_frame_address(0)); |
51 | 95 |
| 96 #if defined(OS_ANDROID) |
| 97 uintptr_t stack_end = GetStackEnd(); |
| 98 #endif |
| 99 |
52 size_t depth = 0; | 100 size_t depth = 0; |
53 while (depth < max_depth) { | 101 while (depth < max_depth) { |
54 #if defined(__arm__) && defined(__GNUC__) && !defined(__clang__) | 102 #if defined(__arm__) && defined(__GNUC__) && !defined(__clang__) |
55 // GCC and LLVM generate slightly different frames on ARM, see | 103 // GCC and LLVM generate slightly different frames on ARM, see |
56 // https://llvm.org/bugs/show_bug.cgi?id=18505 - LLVM generates | 104 // https://llvm.org/bugs/show_bug.cgi?id=18505 - LLVM generates |
57 // x86-compatible frame, while GCC needs adjustment. | 105 // x86-compatible frame, while GCC needs adjustment. |
58 sp -= sizeof(uintptr_t); | 106 sp -= sizeof(uintptr_t); |
59 #endif | 107 #endif |
60 | 108 |
| 109 #if defined(OS_ANDROID) |
| 110 // Both sp[0] and s[1] must be valid. |
| 111 if (sp + 2 * sizeof(uintptr_t) > stack_end) { |
| 112 break; |
| 113 } |
| 114 #endif |
| 115 |
61 if (skip_initial != 0) { | 116 if (skip_initial != 0) { |
62 skip_initial--; | 117 skip_initial--; |
63 } else { | 118 } else { |
64 out_trace[depth++] = reinterpret_cast<const void**>(sp)[1]; | 119 out_trace[depth++] = reinterpret_cast<const void**>(sp)[1]; |
65 } | 120 } |
66 | 121 |
67 // Find out next frame pointer | 122 // Find out next frame pointer |
68 // (heuristics are from TCMalloc's stacktrace functions) | 123 // (heuristics are from TCMalloc's stacktrace functions) |
69 { | 124 { |
70 uintptr_t next_sp = reinterpret_cast<const uintptr_t*>(sp)[0]; | 125 uintptr_t next_sp = reinterpret_cast<const uintptr_t*>(sp)[0]; |
(...skipping 12 matching lines...) Expand all Loading... |
83 } | 138 } |
84 } | 139 } |
85 | 140 |
86 return depth; | 141 return depth; |
87 } | 142 } |
88 | 143 |
89 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | 144 #endif // HAVE_TRACE_STACK_FRAME_POINTERS |
90 | 145 |
91 } // namespace debug | 146 } // namespace debug |
92 } // namespace base | 147 } // namespace base |
OLD | NEW |