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