/src/abseil-cpp/absl/debugging/stacktrace.cc
Line | Count | Source (jump to first uncovered line) |
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 | | |
42 | | #include <algorithm> |
43 | | #include <atomic> |
44 | | |
45 | | #include "absl/base/attributes.h" |
46 | | #include "absl/base/config.h" |
47 | | #include "absl/base/optimization.h" |
48 | | #include "absl/base/port.h" |
49 | | #include "absl/debugging/internal/stacktrace_config.h" |
50 | | |
51 | | #ifdef ABSL_INTERNAL_HAVE_ALLOCA |
52 | | #error ABSL_INTERNAL_HAVE_ALLOCA cannot be directly set |
53 | | #endif |
54 | | |
55 | | #ifdef _WIN32 |
56 | | #include <malloc.h> |
57 | | #define ABSL_INTERNAL_HAVE_ALLOCA 1 |
58 | | #else |
59 | | #ifdef __has_include |
60 | | #if __has_include(<alloca.h>) |
61 | | #include <alloca.h> |
62 | | #define ABSL_INTERNAL_HAVE_ALLOCA 1 |
63 | | #elif !defined(alloca) |
64 | | static void* alloca(size_t) noexcept { return nullptr; } |
65 | | #endif |
66 | | #endif |
67 | | #endif |
68 | | |
69 | | #ifdef ABSL_INTERNAL_HAVE_ALLOCA |
70 | | static constexpr bool kHaveAlloca = true; |
71 | | #else |
72 | | static constexpr bool kHaveAlloca = false; |
73 | | #endif |
74 | | |
75 | | #if defined(ABSL_STACKTRACE_INL_HEADER) |
76 | | #include ABSL_STACKTRACE_INL_HEADER |
77 | | #else |
78 | | # error Cannot calculate stack trace: will need to write for your environment |
79 | | |
80 | | # include "absl/debugging/internal/stacktrace_aarch64-inl.inc" |
81 | | # include "absl/debugging/internal/stacktrace_arm-inl.inc" |
82 | | # include "absl/debugging/internal/stacktrace_emscripten-inl.inc" |
83 | | # include "absl/debugging/internal/stacktrace_generic-inl.inc" |
84 | | # include "absl/debugging/internal/stacktrace_powerpc-inl.inc" |
85 | | # include "absl/debugging/internal/stacktrace_riscv-inl.inc" |
86 | | # include "absl/debugging/internal/stacktrace_unimplemented-inl.inc" |
87 | | # include "absl/debugging/internal/stacktrace_win32-inl.inc" |
88 | | # include "absl/debugging/internal/stacktrace_x86-inl.inc" |
89 | | #endif |
90 | | |
91 | | namespace absl { |
92 | | ABSL_NAMESPACE_BEGIN |
93 | | namespace { |
94 | | |
95 | | typedef int (*Unwinder)(void**, int*, int, int, const void*, int*); |
96 | | std::atomic<Unwinder> custom; |
97 | | |
98 | | template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT> |
99 | | ABSL_ATTRIBUTE_ALWAYS_INLINE inline int Unwind(void** result, uintptr_t* frames, |
100 | | int* sizes, int max_depth, |
101 | | int skip_count, const void* uc, |
102 | 1.18k | int* min_dropped_frames) { |
103 | 1.18k | Unwinder g = custom.load(std::memory_order_acquire); |
104 | 1.18k | int size; |
105 | | // Add 1 to skip count for the unwinder function itself |
106 | 1.18k | ++skip_count; |
107 | 1.18k | if (g != nullptr) { |
108 | 0 | size = (*g)(result, sizes, max_depth, skip_count, uc, min_dropped_frames); |
109 | | // Frame pointers aren't returned by existing hooks, so clear them. |
110 | 0 | if (frames != nullptr) { |
111 | 0 | std::fill(frames, frames + size, uintptr_t()); |
112 | 0 | } |
113 | 1.18k | } else { |
114 | 1.18k | size = UnwindImpl<IS_STACK_FRAMES, IS_WITH_CONTEXT>( |
115 | 1.18k | result, frames, sizes, max_depth, skip_count, uc, min_dropped_frames); |
116 | 1.18k | } |
117 | 1.18k | ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); |
118 | 1.18k | return size; |
119 | 1.18k | } Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<true, true>(void**, unsigned long*, int*, int, int, void const*, int*) Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<true, false>(void**, unsigned long*, int*, int, int, void const*, int*) stacktrace.cc:int absl::(anonymous namespace)::Unwind<false, false>(void**, unsigned long*, int*, int, int, void const*, int*) Line | Count | Source | 102 | 1.18k | int* min_dropped_frames) { | 103 | 1.18k | Unwinder g = custom.load(std::memory_order_acquire); | 104 | 1.18k | int size; | 105 | | // Add 1 to skip count for the unwinder function itself | 106 | 1.18k | ++skip_count; | 107 | 1.18k | if (g != nullptr) { | 108 | 0 | size = (*g)(result, sizes, max_depth, skip_count, uc, min_dropped_frames); | 109 | | // Frame pointers aren't returned by existing hooks, so clear them. | 110 | 0 | if (frames != nullptr) { | 111 | 0 | std::fill(frames, frames + size, uintptr_t()); | 112 | 0 | } | 113 | 1.18k | } else { | 114 | 1.18k | size = UnwindImpl<IS_STACK_FRAMES, IS_WITH_CONTEXT>( | 115 | 1.18k | result, frames, sizes, max_depth, skip_count, uc, min_dropped_frames); | 116 | 1.18k | } | 117 | 1.18k | ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); | 118 | 1.18k | return size; | 119 | 1.18k | } |
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<false, true>(void**, unsigned long*, int*, int, int, void const*, int*) |
120 | | |
121 | | } // anonymous namespace |
122 | | |
123 | | ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int |
124 | | internal_stacktrace::GetStackFrames(void** result, uintptr_t* frames, |
125 | 0 | int* sizes, int max_depth, int skip_count) { |
126 | 0 | if (internal_stacktrace::ShouldFixUpStack()) { |
127 | 0 | size_t depth = static_cast<size_t>(Unwind<true, true>( |
128 | 0 | result, frames, sizes, max_depth, skip_count, nullptr, nullptr)); |
129 | 0 | internal_stacktrace::FixUpStack(result, frames, sizes, |
130 | 0 | static_cast<size_t>(max_depth), depth); |
131 | 0 | return static_cast<int>(depth); |
132 | 0 | } |
133 | | |
134 | 0 | return Unwind<true, false>(result, frames, sizes, max_depth, skip_count, |
135 | 0 | nullptr, nullptr); |
136 | 0 | } |
137 | | |
138 | | ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int |
139 | | internal_stacktrace::GetStackFramesWithContext(void** result, uintptr_t* frames, |
140 | | int* sizes, int max_depth, |
141 | | int skip_count, const void* uc, |
142 | 0 | int* min_dropped_frames) { |
143 | 0 | if (internal_stacktrace::ShouldFixUpStack()) { |
144 | 0 | size_t depth = static_cast<size_t>(Unwind<true, true>( |
145 | 0 | result, frames, sizes, max_depth, skip_count, uc, min_dropped_frames)); |
146 | 0 | internal_stacktrace::FixUpStack(result, frames, sizes, |
147 | 0 | static_cast<size_t>(max_depth), depth); |
148 | 0 | return static_cast<int>(depth); |
149 | 0 | } |
150 | | |
151 | 0 | return Unwind<true, true>(result, frames, sizes, max_depth, skip_count, uc, |
152 | 0 | min_dropped_frames); |
153 | 0 | } |
154 | | |
155 | | ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackTrace( |
156 | 1.18k | void** result, int max_depth, int skip_count) { |
157 | 1.18k | if (internal_stacktrace::ShouldFixUpStack()) { |
158 | 0 | if constexpr (kHaveAlloca) { |
159 | 0 | const size_t nmax = static_cast<size_t>(max_depth); |
160 | 0 | uintptr_t* frames = |
161 | 0 | static_cast<uintptr_t*>(alloca(nmax * sizeof(*frames))); |
162 | 0 | int* sizes = static_cast<int*>(alloca(nmax * sizeof(*sizes))); |
163 | 0 | size_t depth = static_cast<size_t>(Unwind<true, false>( |
164 | 0 | result, frames, sizes, max_depth, skip_count, nullptr, nullptr)); |
165 | 0 | internal_stacktrace::FixUpStack(result, frames, sizes, nmax, depth); |
166 | 0 | return static_cast<int>(depth); |
167 | 0 | } |
168 | 0 | } |
169 | | |
170 | 0 | return Unwind<false, false>(result, nullptr, nullptr, max_depth, skip_count, |
171 | 1.18k | nullptr, nullptr); |
172 | 1.18k | } |
173 | | |
174 | | ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int |
175 | | GetStackTraceWithContext(void** result, int max_depth, int skip_count, |
176 | 0 | const void* uc, int* min_dropped_frames) { |
177 | 0 | if (internal_stacktrace::ShouldFixUpStack()) { |
178 | 0 | if constexpr (kHaveAlloca) { |
179 | 0 | const size_t nmax = static_cast<size_t>(max_depth); |
180 | 0 | uintptr_t* frames = |
181 | 0 | static_cast<uintptr_t*>(alloca(nmax * sizeof(*frames))); |
182 | 0 | int* sizes = static_cast<int*>(alloca(nmax * sizeof(*sizes))); |
183 | 0 | size_t depth = static_cast<size_t>( |
184 | 0 | Unwind<true, true>(result, frames, sizes, max_depth, skip_count, uc, |
185 | 0 | min_dropped_frames)); |
186 | 0 | internal_stacktrace::FixUpStack(result, frames, sizes, nmax, depth); |
187 | 0 | return static_cast<int>(depth); |
188 | 0 | } |
189 | 0 | } |
190 | | |
191 | 0 | return Unwind<false, true>(result, nullptr, nullptr, max_depth, skip_count, |
192 | 0 | uc, min_dropped_frames); |
193 | 0 | } |
194 | | |
195 | 0 | void SetStackUnwinder(Unwinder w) { |
196 | 0 | custom.store(w, std::memory_order_release); |
197 | 0 | } |
198 | | |
199 | | ABSL_ATTRIBUTE_ALWAYS_INLINE static inline int DefaultStackUnwinderImpl( |
200 | | void** pcs, uintptr_t* frames, int* sizes, int depth, int skip, |
201 | 0 | const void* uc, int* min_dropped_frames) { |
202 | 0 | skip++; // For this function |
203 | 0 | decltype(&UnwindImpl<false, false>) f; |
204 | 0 | if (sizes == nullptr) { |
205 | 0 | if (uc == nullptr) { |
206 | 0 | f = &UnwindImpl<false, false>; |
207 | 0 | } else { |
208 | 0 | f = &UnwindImpl<false, true>; |
209 | 0 | } |
210 | 0 | } else { |
211 | 0 | if (uc == nullptr) { |
212 | 0 | f = &UnwindImpl<true, false>; |
213 | 0 | } else { |
214 | 0 | f = &UnwindImpl<true, true>; |
215 | 0 | } |
216 | 0 | } |
217 | 0 | return (*f)(pcs, frames, sizes, depth, skip, uc, min_dropped_frames); |
218 | 0 | } |
219 | | |
220 | | ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int |
221 | | internal_stacktrace::DefaultStackUnwinder(void** pcs, uintptr_t* frames, |
222 | | int* sizes, int depth, int skip, |
223 | | const void* uc, |
224 | 0 | int* min_dropped_frames) { |
225 | 0 | int n = DefaultStackUnwinderImpl(pcs, frames, sizes, depth, skip, uc, |
226 | 0 | min_dropped_frames); |
227 | 0 | ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); |
228 | 0 | return n; |
229 | 0 | } |
230 | | |
231 | | ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int DefaultStackUnwinder( |
232 | | void** pcs, int* sizes, int depth, int skip, const void* uc, |
233 | 0 | int* min_dropped_frames) { |
234 | 0 | int n = DefaultStackUnwinderImpl(pcs, nullptr, sizes, depth, skip, uc, |
235 | 0 | min_dropped_frames); |
236 | 0 | ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); |
237 | 0 | return n; |
238 | 0 | } |
239 | | |
240 | 1.18k | ABSL_ATTRIBUTE_WEAK bool internal_stacktrace::ShouldFixUpStack() { |
241 | 1.18k | return false; |
242 | 1.18k | } |
243 | | |
244 | | // Fixes up the stack trace of the current thread, in the first `depth` frames |
245 | | // of each buffer. The buffers need to be larger than `depth`, to accommodate |
246 | | // any newly inserted elements. `depth` is updated to reflect the new number of |
247 | | // elements valid across all the buffers. (It is therefore recommended that all |
248 | | // buffer sizes be equal.) |
249 | | // |
250 | | // The `frames` and `sizes` parameters denote the bounds of the stack frame |
251 | | // corresponding to each instruction pointer in the `pcs`. |
252 | | // Any elements inside these buffers may be zero or null, in which case that |
253 | | // information is assumed to be absent/unavailable. |
254 | | ABSL_ATTRIBUTE_WEAK void internal_stacktrace::FixUpStack(void**, uintptr_t*, |
255 | | int*, size_t, |
256 | 0 | size_t&) {} |
257 | | |
258 | | ABSL_NAMESPACE_END |
259 | | } // namespace absl |