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/macros.h" | 13 #include "base/macros.h" |
13 | 14 |
14 #if HAVE_TRACE_STACK_FRAME_POINTERS | 15 #if HAVE_TRACE_STACK_FRAME_POINTERS |
15 | 16 |
16 #if defined(OS_LINUX) || defined(OS_ANDROID) | 17 #if defined(OS_LINUX) || defined(OS_ANDROID) |
17 #include <pthread.h> | 18 #include <pthread.h> |
18 #include "base/process/process_handle.h" | 19 #include "base/process/process_handle.h" |
19 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
20 #endif | 21 #endif |
21 | 22 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 if (IsStackFrameValid(next2_fp, next_fp, stack_end)) { | 170 if (IsStackFrameValid(next2_fp, next_fp, stack_end)) { |
170 return fp; | 171 return fp; |
171 } | 172 } |
172 } | 173 } |
173 } | 174 } |
174 #endif // defined(OS_LINUX) | 175 #endif // defined(OS_LINUX) |
175 | 176 |
176 return 0; | 177 return 0; |
177 } | 178 } |
178 | 179 |
| 180 // Links stack frame |fp| to |parent_fp|, so that during stack unwinding |
| 181 // TraceStackFramePointers() visits |parent_fp| after visiting |fp|. |
| 182 // Both frame pointers must come from __builtin_frame_address(). |
| 183 // Returns previous stack frame |fp| was linked to. |
| 184 void* LinkStackFrames(void* fpp, void* parent_fp) { |
| 185 uintptr_t fp = reinterpret_cast<uintptr_t>(fpp) - kStackFrameAdjustment; |
| 186 void* prev_parent_fp = reinterpret_cast<void**>(fp)[0]; |
| 187 reinterpret_cast<void**>(fp)[0] = parent_fp; |
| 188 return prev_parent_fp; |
| 189 } |
| 190 |
179 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | 191 #endif // HAVE_TRACE_STACK_FRAME_POINTERS |
180 | 192 |
181 } // namespace | 193 } // namespace |
182 | 194 |
183 StackTrace::StackTrace(const void* const* trace, size_t count) { | 195 StackTrace::StackTrace(const void* const* trace, size_t count) { |
184 count = std::min(count, arraysize(trace_)); | 196 count = std::min(count, arraysize(trace_)); |
185 if (count) | 197 if (count) |
186 memcpy(trace_, trace, count * sizeof(trace_[0])); | 198 memcpy(trace_, trace, count * sizeof(trace_[0])); |
187 count_ = count; | 199 count_ = count; |
188 } | 200 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 continue; | 250 continue; |
239 } | 251 } |
240 | 252 |
241 // Failed to find next frame. | 253 // Failed to find next frame. |
242 break; | 254 break; |
243 } | 255 } |
244 | 256 |
245 return depth; | 257 return depth; |
246 } | 258 } |
247 | 259 |
| 260 ScopedStackFrameLinker::ScopedStackFrameLinker(void* fp, void* parent_fp) |
| 261 : fp_(fp), |
| 262 parent_fp_(parent_fp), |
| 263 original_parent_fp_(LinkStackFrames(fp, parent_fp)) {} |
| 264 |
| 265 ScopedStackFrameLinker::~ScopedStackFrameLinker() { |
| 266 void* previous_parent_fp = LinkStackFrames(fp_, original_parent_fp_); |
| 267 CHECK_EQ(parent_fp_, previous_parent_fp) |
| 268 << "Stack frame's parent pointer has changed!"; |
| 269 } |
| 270 |
248 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | 271 #endif // HAVE_TRACE_STACK_FRAME_POINTERS |
249 | 272 |
250 } // namespace debug | 273 } // namespace debug |
251 } // namespace base | 274 } // namespace base |
OLD | NEW |