Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(611)

Side by Side Diff: base/debug/stack_trace.cc

Issue 2276813002: Unwind stack past system libraries on Linux. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@get-stack-end-linux
Patch Set: Fix comment Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
11 #include <sstream> 10 #include <sstream>
12 11
13 #include "base/macros.h" 12 #include "base/macros.h"
14 13
15 #if HAVE_TRACE_STACK_FRAME_POINTERS 14 #if HAVE_TRACE_STACK_FRAME_POINTERS
16 15
17 #if defined(OS_LINUX) || defined(OS_ANDROID) 16 #if defined(OS_LINUX) || defined(OS_ANDROID)
18 #include <pthread.h> 17 #include <pthread.h>
19 #include "base/process/process_handle.h" 18 #include "base/process/process_handle.h"
20 #include "base/threading/platform_thread.h" 19 #include "base/threading/platform_thread.h"
21 #endif 20 #endif
22 21
23 #if defined(OS_LINUX) && defined(__GLIBC__) 22 #if defined(OS_LINUX) && defined(__GLIBC__)
24 extern "C" void* __libc_stack_end; 23 extern "C" void* __libc_stack_end;
25 #endif 24 #endif
26 25
27 #endif // HAVE_TRACE_STACK_FRAME_POINTERS 26 #endif // HAVE_TRACE_STACK_FRAME_POINTERS
28 27
29 namespace base { 28 namespace base {
30 namespace debug { 29 namespace debug {
31 30
32 StackTrace::StackTrace(const void* const* trace, size_t count) { 31 namespace {
33 count = std::min(count, arraysize(trace_));
34 if (count)
35 memcpy(trace_, trace, count * sizeof(trace_[0]));
36 count_ = count;
37 }
38
39 StackTrace::~StackTrace() {
40 }
41
42 const void *const *StackTrace::Addresses(size_t* count) const {
43 *count = count_;
44 if (count_)
45 return trace_;
46 return NULL;
47 }
48
49 std::string StackTrace::ToString() const {
50 std::stringstream stream;
51 #if !defined(__UCLIBC__)
52 OutputToStream(&stream);
53 #endif
54 return stream.str();
55 }
56 32
57 #if HAVE_TRACE_STACK_FRAME_POINTERS 33 #if HAVE_TRACE_STACK_FRAME_POINTERS
58 34
59 static uintptr_t GetStackEnd() { 35 #if defined(OS_LINUX)
36 #define SCAN_STACK_FOR_FRAMES
37 // TraceStackFramePointers() will try to scan stack for frame pointers to
38 // continue unwinding past system libraries. Only supported on Linux where
39 // system libraries are usually in the middle of the trace, for example:
40 //
41 // TraceStackFramePointers
42 // <more frames from Chrome>
43 // g_main_context_dispatch <--- unwinding stops here
44 // g_main_context_iteration
45 // base::MessagePumpGlib::Run
46 // base::RunLoop::Run <--- resumes from here
47 // <more frames from Chrome>
48 // __libc_start_main
49 //
50 // Note that base::MessagePumpGlib::Run() is lost, because its frame was
51 // not saved by g_main_context_iteration().
52 //
53 // For stack scanning to be efficient it's very important for the thread to
54 // be started by Chrome. In that case we naturally terminate unwinding once
55 // we reach the origin of the stack (i.e. GetStackEnd()). If the thread was
56 // not started by Chrome (e.g. Android's main thread), then we end up always
57 // scanning area at the origin of the stack, wasting time and not finding any
58 // frames (since e.g. Android libraries don't have frame pointers).
59
60 // Allows to resume ~95% of all prematurely terminated traces on Linux.
61 constexpr size_t kMaxStackScanArea = 512;
62 #endif
63
64 // Returns end of the stack, or 0 if it's unknown.
65 uintptr_t GetStackEnd() {
60 #if defined(OS_ANDROID) 66 #if defined(OS_ANDROID)
61 // Bionic reads proc/maps on every call to pthread_getattr_np() when called 67 // Bionic reads proc/maps on every call to pthread_getattr_np() when called
62 // from the main thread. So we need to cache end of stack in that case to get 68 // from the main thread. So we need to cache end of stack in that case to get
63 // acceptable performance. 69 // acceptable performance.
64 // For all other threads pthread_getattr_np() is fast enough as it just reads 70 // For all other threads pthread_getattr_np() is fast enough as it just reads
65 // values from its pthread_t argument. 71 // values from its pthread_t argument.
66 static uintptr_t main_stack_end = 0; 72 static uintptr_t main_stack_end = 0;
67 73
68 bool is_main_thread = GetCurrentProcId() == PlatformThread::CurrentId(); 74 bool is_main_thread = GetCurrentProcId() == PlatformThread::CurrentId();
69 if (is_main_thread && main_stack_end) { 75 if (is_main_thread && main_stack_end) {
(...skipping 20 matching lines...) Expand all
90 return stack_end; 96 return stack_end;
91 97
92 #elif defined(OS_LINUX) && defined(__GLIBC__) 98 #elif defined(OS_LINUX) && defined(__GLIBC__)
93 99
94 if (GetCurrentProcId() == PlatformThread::CurrentId()) { 100 if (GetCurrentProcId() == PlatformThread::CurrentId()) {
95 // For the main thread we have a shortcut. 101 // For the main thread we have a shortcut.
96 return reinterpret_cast<uintptr_t>(__libc_stack_end); 102 return reinterpret_cast<uintptr_t>(__libc_stack_end);
97 } else { 103 } else {
98 // No easy way to get stack end for non-main threads, 104 // No easy way to get stack end for non-main threads,
99 // see crbug.com/617730. 105 // see crbug.com/617730.
100 return std::numeric_limits<uintptr_t>::max(); 106 return 0;
101 } 107 }
102 108
103 #else 109 #else
104 110
105 // TODO(dskiba): support Windows, macOS 111 // TODO(dskiba): support Windows, macOS
106 return std::numeric_limits<uintptr_t>::max(); 112 return 0;
107 113
108 #endif 114 #endif
109 } 115 }
110 116
117 #if defined(__arm__) && defined(__GNUC__) && !defined(__clang__)
118 // GCC and LLVM generate slightly different frames on ARM, see
119 // https://llvm.org/bugs/show_bug.cgi?id=18505 - LLVM generates
120 // x86-compatible frame, while GCC needs adjustment.
121 constexpr size_t kStackFrameAdjustment = sizeof(uintptr_t);
122 #else
123 constexpr size_t kStackFrameAdjustment = 0;
124 #endif
125
126 uintptr_t GetNextStackFrame(uintptr_t fp) {
127 return reinterpret_cast<const uintptr_t*>(fp)[0] - kStackFrameAdjustment;
128 }
129
130 uintptr_t GetStackFramePC(uintptr_t fp) {
131 return reinterpret_cast<const uintptr_t*>(fp)[1];
132 }
133
134 bool IsStackFrameValid(uintptr_t fp, uintptr_t prev_fp, uintptr_t stack_end) {
135 // With the stack growing downwards, older stack frame must be
136 // at a greater address that the current one.
137 if (fp <= prev_fp) return false;
138
139 // Assume huge stack frames are bogus.
140 if (fp - prev_fp > 100000) return false;
141
142 // Check alignment.
143 if (fp & (sizeof(uintptr_t) - 1)) return false;
144
145 if (stack_end) {
146 // Both fp[0] and fp[1] must be within the stack.
147 if (fp > stack_end - 2 * sizeof(uintptr_t)) return false;
148
149 #if defined(SCAN_STACK_FOR_FRAMES)
150 // Additional check to filter out false positives.
151 if (GetStackFramePC(fp) < 32768) return false;
152 #endif
153 }
154
155 return true;
156 };
157
158 #endif // HAVE_TRACE_STACK_FRAME_POINTERS
159
160 } // namespace
161
162 StackTrace::StackTrace(const void* const* trace, size_t count) {
163 count = std::min(count, arraysize(trace_));
164 if (count)
165 memcpy(trace_, trace, count * sizeof(trace_[0]));
166 count_ = count;
167 }
168
169 StackTrace::~StackTrace() {
170 }
171
172 const void *const *StackTrace::Addresses(size_t* count) const {
173 *count = count_;
174 if (count_)
175 return trace_;
176 return NULL;
177 }
178
179 std::string StackTrace::ToString() const {
180 std::stringstream stream;
181 #if !defined(__UCLIBC__)
182 OutputToStream(&stream);
183 #endif
184 return stream.str();
185 }
186
187 #if HAVE_TRACE_STACK_FRAME_POINTERS
188
111 size_t TraceStackFramePointers(const void** out_trace, 189 size_t TraceStackFramePointers(const void** out_trace,
112 size_t max_depth, 190 size_t max_depth,
113 size_t skip_initial) { 191 size_t skip_initial) {
114 // Usage of __builtin_frame_address() enables frame pointers in this 192 // Usage of __builtin_frame_address() enables frame pointers in this
115 // function even if they are not enabled globally. So 'sp' will always 193 // function even if they are not enabled globally. So 'fp' will always
116 // be valid. 194 // be valid.
117 uintptr_t sp = reinterpret_cast<uintptr_t>(__builtin_frame_address(0)); 195 uintptr_t fp = reinterpret_cast<uintptr_t>(__builtin_frame_address(0)) -
196 kStackFrameAdjustment;
118 197
119 uintptr_t stack_end = GetStackEnd(); 198 uintptr_t stack_end = GetStackEnd();
120 199
121 size_t depth = 0; 200 size_t depth = 0;
122 while (depth < max_depth) { 201 while (depth < max_depth) {
123 #if defined(__arm__) && defined(__GNUC__) && !defined(__clang__)
124 // GCC and LLVM generate slightly different frames on ARM, see
125 // https://llvm.org/bugs/show_bug.cgi?id=18505 - LLVM generates
126 // x86-compatible frame, while GCC needs adjustment.
127 sp -= sizeof(uintptr_t);
128 #endif
129
130 // Both sp[0] and s[1] must be valid.
131 if (sp + 2 * sizeof(uintptr_t) > stack_end) {
132 break;
133 }
134
135 if (skip_initial != 0) { 202 if (skip_initial != 0) {
136 skip_initial--; 203 skip_initial--;
137 } else { 204 } else {
138 out_trace[depth++] = reinterpret_cast<const void**>(sp)[1]; 205 out_trace[depth++] = reinterpret_cast<const void*>(GetStackFramePC(fp));
139 } 206 }
140 207
141 // Find out next frame pointer 208 uintptr_t next_fp = GetNextStackFrame(fp);
142 // (heuristics are from TCMalloc's stacktrace functions) 209 if (!IsStackFrameValid(next_fp, fp, stack_end)) {
Primiano Tucci (use gerrit) 2016/08/25 11:42:53 I'd reverse the if condition and reduce one leven
Dmitry Skiba 2016/08/30 18:04:21 Done.
143 { 210 #if defined(SCAN_STACK_FOR_FRAMES)
Primiano Tucci (use gerrit) 2016/08/25 11:42:53 Since you introduced all these beautiful helper fu
Dmitry Skiba 2016/08/30 18:04:21 Done.
Primiano Tucci (use gerrit) 2016/08/31 13:48:49 Doesn't seem so :) WHat I mean is for ScanStackFor
Dmitry Skiba 2016/09/07 22:33:24 Ah, I see. Done now :)
144 uintptr_t next_sp = reinterpret_cast<const uintptr_t*>(sp)[0]; 211 if (!stack_end) {
145 212 break;
146 // With the stack growing downwards, older stack frame must be 213 }
147 // at a greater address that the current one. 214 fp += sizeof(uintptr_t);
148 if (next_sp <= sp) break; 215 uintptr_t last_scan_fp = std::min(fp + kMaxStackScanArea, stack_end) -
Primiano Tucci (use gerrit) 2016/08/25 11:42:53 Just a minor naming thing: when I saw "last_scan_f
Dmitry Skiba 2016/08/30 18:04:21 Done.
149 216 sizeof(uintptr_t);
150 // Assume stack frames larger than 100,000 bytes are bogus. 217 for (;fp <= last_scan_fp; fp += sizeof(uintptr_t)) {
151 if (next_sp - sp > 100000) break; 218 next_fp = GetNextStackFrame(fp);
152 219 if (IsStackFrameValid(next_fp, fp, stack_end)) {
153 // Check alignment. 220 // Check two frames deep. Since stack frame is just a pointer to
154 if (sp & (sizeof(void*) - 1)) break; 221 // a higher address on the stack, it's relatively easy to find
155 222 // something that looks like one. However two linked frames are
156 sp = next_sp; 223 // far less likely to be bogus.
224 uintptr_t next2_fp = GetNextStackFrame(next_fp);
Primiano Tucci (use gerrit) 2016/08/25 11:42:53 Hmm I'm missing something here, does this mean tha
Dmitry Skiba 2016/08/30 18:04:21 This tries to get rid of false positives - it's fa
225 if (IsStackFrameValid(next2_fp, next_fp, stack_end)) {
226 break;
227 }
228 }
229 }
230 if (fp > last_scan_fp) {
231 break;
232 }
157 } 233 }
234 fp = next_fp;
235 #else
236 // Stack scanning is not enabled, stop unwinding.
237 break;
238 #endif // defined(SCAN_STACK_FOR_FRAMES)
158 } 239 }
159 240
160 return depth; 241 return depth;
161 } 242 }
162 243
163 #endif // HAVE_TRACE_STACK_FRAME_POINTERS 244 #endif // HAVE_TRACE_STACK_FRAME_POINTERS
164 245
165 } // namespace debug 246 } // namespace debug
166 } // namespace base 247 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698