/src/abseil-cpp/absl/debugging/stacktrace.cc
Line | Count | Source |
1 | | // Copyright 2017 The Abseil Authors. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | // Produce stack trace. |
16 | | // |
17 | | // There are three different ways we can try to get the stack trace: |
18 | | // |
19 | | // 1) Our hand-coded stack-unwinder. This depends on a certain stack |
20 | | // layout, which is used by gcc (and those systems using a |
21 | | // gcc-compatible ABI) on x86 systems, at least since gcc 2.95. |
22 | | // It uses the frame pointer to do its work. |
23 | | // |
24 | | // 2) The libunwind library. This is still in development, and as a |
25 | | // separate library adds a new dependency, but doesn't need a frame |
26 | | // pointer. It also doesn't call malloc. |
27 | | // |
28 | | // 3) The gdb unwinder -- also the one used by the c++ exception code. |
29 | | // It's obviously well-tested, but has a fatal flaw: it can call |
30 | | // malloc() from the unwinder. This is a problem because we're |
31 | | // trying to use the unwinder to instrument malloc(). |
32 | | // |
33 | | // Note: if you add a new implementation here, make sure it works |
34 | | // correctly when absl::GetStackTrace() is called with max_depth == 0. |
35 | | // Some code may do that. |
36 | | |
37 | | #include "absl/debugging/stacktrace.h" |
38 | | |
39 | | #include <stddef.h> |
40 | | #include <stdint.h> |
41 | | #include <stdlib.h> |
42 | | |
43 | | #include <algorithm> |
44 | | #include <atomic> |
45 | | |
46 | | #include "absl/base/attributes.h" |
47 | | #include "absl/base/config.h" |
48 | | #include "absl/base/optimization.h" |
49 | | #include "absl/base/port.h" |
50 | | #include "absl/debugging/internal/stacktrace_config.h" |
51 | | |
52 | | #if defined(ABSL_STACKTRACE_INL_HEADER) |
53 | | #include ABSL_STACKTRACE_INL_HEADER |
54 | | #else |
55 | | #error Cannot calculate stack trace: will need to write for your environment |
56 | | |
57 | | #include "absl/debugging/internal/stacktrace_aarch64-inl.inc" |
58 | | #include "absl/debugging/internal/stacktrace_arm-inl.inc" |
59 | | #include "absl/debugging/internal/stacktrace_emscripten-inl.inc" |
60 | | #include "absl/debugging/internal/stacktrace_generic-inl.inc" |
61 | | #include "absl/debugging/internal/stacktrace_powerpc-inl.inc" |
62 | | #include "absl/debugging/internal/stacktrace_riscv-inl.inc" |
63 | | #include "absl/debugging/internal/stacktrace_unimplemented-inl.inc" |
64 | | #include "absl/debugging/internal/stacktrace_win32-inl.inc" |
65 | | #include "absl/debugging/internal/stacktrace_x86-inl.inc" |
66 | | #endif |
67 | | |
68 | | namespace absl { |
69 | | ABSL_NAMESPACE_BEGIN |
70 | | namespace { |
71 | | |
72 | | typedef int (*Unwinder)(void**, int*, int, int, const void*, int*); |
73 | | std::atomic<Unwinder> custom; |
74 | | |
75 | | template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT> |
76 | | ABSL_ATTRIBUTE_ALWAYS_INLINE inline int Unwind(void** result, uintptr_t* frames, |
77 | | int* sizes, size_t max_depth, |
78 | | int skip_count, const void* uc, |
79 | | int* min_dropped_frames, |
80 | 1.15k | bool unwind_with_fixup = true) { |
81 | 1.15k | unwind_with_fixup = |
82 | 1.15k | unwind_with_fixup && internal_stacktrace::ShouldFixUpStack(); |
83 | | |
84 | | #ifdef _WIN32 |
85 | | if (unwind_with_fixup) { |
86 | | // TODO(b/434184677): Fixups are flaky and not supported on Windows |
87 | | unwind_with_fixup = false; |
88 | | #ifndef NDEBUG |
89 | | abort(); |
90 | | #endif |
91 | | } |
92 | | #endif |
93 | | |
94 | 1.15k | Unwinder g = custom.load(std::memory_order_acquire); |
95 | 1.15k | size_t size; |
96 | | // Add 1 to skip count for the unwinder function itself |
97 | 1.15k | ++skip_count; |
98 | 1.15k | if (g != nullptr) { |
99 | 0 | size = static_cast<size_t>((*g)(result, sizes, static_cast<int>(max_depth), |
100 | 0 | skip_count, uc, min_dropped_frames)); |
101 | | // Frame pointers aren't returned by existing hooks, so clear them. |
102 | 0 | if (frames != nullptr) { |
103 | 0 | std::fill(frames, frames + size, uintptr_t()); |
104 | 0 | } |
105 | 1.15k | } else { |
106 | 1.15k | size = static_cast<size_t>(UnwindImpl<IS_STACK_FRAMES, IS_WITH_CONTEXT>( |
107 | 1.15k | result, frames, sizes, static_cast<int>(max_depth), skip_count, uc, |
108 | 1.15k | min_dropped_frames)); |
109 | 1.15k | } |
110 | 1.15k | if (unwind_with_fixup) { |
111 | 0 | internal_stacktrace::FixUpStack(result, frames, sizes, max_depth, size); |
112 | 0 | } |
113 | | |
114 | 1.15k | ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); |
115 | 1.15k | return static_cast<int>(size); |
116 | 1.15k | } Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<true, false>(void**, unsigned long*, int*, unsigned long, int, void const*, int*, bool) stacktrace.cc:int absl::(anonymous namespace)::Unwind<false, false>(void**, unsigned long*, int*, unsigned long, int, void const*, int*, bool) Line | Count | Source | 80 | 1.15k | bool unwind_with_fixup = true) { | 81 | 1.15k | unwind_with_fixup = | 82 | 1.15k | unwind_with_fixup && internal_stacktrace::ShouldFixUpStack(); | 83 | | | 84 | | #ifdef _WIN32 | 85 | | if (unwind_with_fixup) { | 86 | | // TODO(b/434184677): Fixups are flaky and not supported on Windows | 87 | | unwind_with_fixup = false; | 88 | | #ifndef NDEBUG | 89 | | abort(); | 90 | | #endif | 91 | | } | 92 | | #endif | 93 | | | 94 | 1.15k | Unwinder g = custom.load(std::memory_order_acquire); | 95 | 1.15k | size_t size; | 96 | | // Add 1 to skip count for the unwinder function itself | 97 | 1.15k | ++skip_count; | 98 | 1.15k | if (g != nullptr) { | 99 | 0 | size = static_cast<size_t>((*g)(result, sizes, static_cast<int>(max_depth), | 100 | 0 | skip_count, uc, min_dropped_frames)); | 101 | | // Frame pointers aren't returned by existing hooks, so clear them. | 102 | 0 | if (frames != nullptr) { | 103 | 0 | std::fill(frames, frames + size, uintptr_t()); | 104 | 0 | } | 105 | 1.15k | } else { | 106 | 1.15k | size = static_cast<size_t>(UnwindImpl<IS_STACK_FRAMES, IS_WITH_CONTEXT>( | 107 | 1.15k | result, frames, sizes, static_cast<int>(max_depth), skip_count, uc, | 108 | 1.15k | min_dropped_frames)); | 109 | 1.15k | } | 110 | 1.15k | if (unwind_with_fixup) { | 111 | 0 | internal_stacktrace::FixUpStack(result, frames, sizes, max_depth, size); | 112 | 0 | } | 113 | | | 114 | 1.15k | ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); | 115 | 1.15k | return static_cast<int>(size); | 116 | 1.15k | } |
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<true, true>(void**, unsigned long*, int*, unsigned long, int, void const*, int*, bool) Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<false, true>(void**, unsigned long*, int*, unsigned long, int, void const*, int*, bool) |
117 | | |
118 | | } // anonymous namespace |
119 | | |
120 | | ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackFrames( |
121 | 0 | void** result, int* sizes, int max_depth, int skip_count) { |
122 | 0 | return Unwind<true, false>(result, nullptr, sizes, |
123 | 0 | static_cast<size_t>(max_depth), skip_count, |
124 | 0 | nullptr, nullptr); |
125 | 0 | } |
126 | | |
127 | | ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int |
128 | | internal_stacktrace::GetStackTraceNoFixup(void** result, int max_depth, |
129 | 0 | int skip_count) { |
130 | 0 | return Unwind<false, false>(result, nullptr, nullptr, |
131 | 0 | static_cast<size_t>(max_depth), skip_count, |
132 | 0 | nullptr, nullptr, /*unwind_with_fixup=*/false); |
133 | 0 | } |
134 | | |
135 | | ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int |
136 | | GetStackFramesWithContext(void** result, int* sizes, int max_depth, |
137 | | int skip_count, const void* uc, |
138 | 0 | int* min_dropped_frames) { |
139 | 0 | return Unwind<true, true>(result, nullptr, sizes, |
140 | 0 | static_cast<size_t>(max_depth), skip_count, uc, |
141 | 0 | min_dropped_frames); |
142 | 0 | } |
143 | | |
144 | | ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackTrace( |
145 | 1.15k | void** result, int max_depth, int skip_count) { |
146 | 1.15k | return Unwind<false, false>(result, nullptr, nullptr, |
147 | 1.15k | static_cast<size_t>(max_depth), skip_count, |
148 | 1.15k | nullptr, nullptr); |
149 | 1.15k | } |
150 | | |
151 | | ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int |
152 | | GetStackTraceWithContext(void** result, int max_depth, int skip_count, |
153 | 0 | const void* uc, int* min_dropped_frames) { |
154 | 0 | return Unwind<false, true>(result, nullptr, nullptr, |
155 | 0 | static_cast<size_t>(max_depth), skip_count, uc, |
156 | 0 | min_dropped_frames); |
157 | 0 | } |
158 | | |
159 | 0 | void SetStackUnwinder(Unwinder w) { |
160 | 0 | custom.store(w, std::memory_order_release); |
161 | 0 | } |
162 | | |
163 | | int DefaultStackUnwinder(void** pcs, int* sizes, int depth, int skip, |
164 | 0 | const void* uc, int* min_dropped_frames) { |
165 | 0 | skip++; // For this function |
166 | 0 | decltype(&UnwindImpl<false, false>) f; |
167 | 0 | if (sizes == nullptr) { |
168 | 0 | if (uc == nullptr) { |
169 | 0 | f = &UnwindImpl<false, false>; |
170 | 0 | } else { |
171 | 0 | f = &UnwindImpl<false, true>; |
172 | 0 | } |
173 | 0 | } else { |
174 | 0 | if (uc == nullptr) { |
175 | 0 | f = &UnwindImpl<true, false>; |
176 | 0 | } else { |
177 | 0 | f = &UnwindImpl<true, true>; |
178 | 0 | } |
179 | 0 | } |
180 | 0 | int n = (*f)(pcs, nullptr, sizes, depth, skip, uc, min_dropped_frames); |
181 | 0 | ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); |
182 | 0 | return n; |
183 | 0 | } |
184 | | |
185 | 1.15k | ABSL_ATTRIBUTE_WEAK bool internal_stacktrace::ShouldFixUpStack() { |
186 | 1.15k | return false; |
187 | 1.15k | } |
188 | | |
189 | | // Fixes up the stack trace of the current thread, in the first `depth` frames |
190 | | // of each buffer. The buffers need to be larger than `depth`, to accommodate |
191 | | // any newly inserted elements. `depth` is updated to reflect the new number of |
192 | | // elements valid across all the buffers. (It is therefore recommended that all |
193 | | // buffer sizes be equal.) |
194 | | // |
195 | | // The `frames` and `sizes` parameters denote the bounds of the stack frame |
196 | | // corresponding to each instruction pointer in the `pcs`. |
197 | | // Any elements inside these buffers may be zero or null, in which case that |
198 | | // information is assumed to be absent/unavailable. |
199 | | ABSL_ATTRIBUTE_WEAK void internal_stacktrace::FixUpStack(void**, uintptr_t*, |
200 | | int*, size_t, |
201 | 0 | size_t&) {} |
202 | | |
203 | | ABSL_NAMESPACE_END |
204 | | } // namespace absl |