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> |
(...skipping 21 matching lines...) Expand all Loading... |
32 } | 32 } |
33 | 33 |
34 std::string StackTrace::ToString() const { | 34 std::string StackTrace::ToString() const { |
35 std::stringstream stream; | 35 std::stringstream stream; |
36 #if !defined(__UCLIBC__) | 36 #if !defined(__UCLIBC__) |
37 OutputToStream(&stream); | 37 OutputToStream(&stream); |
38 #endif | 38 #endif |
39 return stream.str(); | 39 return stream.str(); |
40 } | 40 } |
41 | 41 |
42 #if HAVE_TRACE_STACK_FRAME_POINTERS | |
43 | |
44 size_t TraceStackFramePointers(const void** out_trace, | |
45 size_t max_depth, | |
46 size_t skip_initial) { | |
47 // Usage of __builtin_frame_address() enables frame pointers in this | |
48 // function even if they are not enabled globally. So 'sp' will always | |
49 // be valid. | |
50 uintptr_t sp = reinterpret_cast<uintptr_t>(__builtin_frame_address(0)); | |
51 | |
52 size_t depth = 0; | |
53 while (depth < max_depth) { | |
54 if (skip_initial != 0) { | |
55 skip_initial--; | |
56 } else { | |
57 out_trace[depth++] = reinterpret_cast<const void**>(sp)[1]; | |
58 } | |
59 | |
60 #if defined(__arm__) && defined(__GNUC__) && !defined(__clang__) | |
61 // GCC and LLVM generate slightly different frames on ARM, see | |
62 // https://llvm.org/bugs/show_bug.cgi?id=18505 - LLVM generates | |
63 // x86-compatible frame, while GCC needs adjustment. | |
64 sp -= sizeof(uintptr_t); | |
65 #endif | |
66 | |
67 // Find out next frame pointer | |
68 // (heuristics are from TCMalloc's stacktrace functions) | |
69 { | |
70 uintptr_t next_sp = reinterpret_cast<const uintptr_t*>(sp)[0]; | |
71 | |
72 // With the stack growing downwards, older stack frame must be | |
73 // at a greater address that the current one. | |
74 if (next_sp <= sp) break; | |
75 | |
76 // Assume stack frames larger than 100,000 bytes are bogus. | |
77 if (next_sp - sp > 100000) break; | |
78 | |
79 // Check alignment. | |
80 if (sp & (sizeof(void*) - 1)) break; | |
81 | |
82 sp = next_sp; | |
83 } | |
84 } | |
85 | |
86 return depth; | |
87 } | |
88 | |
89 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | |
90 | |
91 } // namespace debug | 42 } // namespace debug |
92 } // namespace base | 43 } // namespace base |
OLD | NEW |