xref: /aosp_15_r20/external/abseil-cpp/absl/debugging/stacktrace.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker // Produce stack trace.
16*9356374aSAndroid Build Coastguard Worker //
17*9356374aSAndroid Build Coastguard Worker // There are three different ways we can try to get the stack trace:
18*9356374aSAndroid Build Coastguard Worker //
19*9356374aSAndroid Build Coastguard Worker // 1) Our hand-coded stack-unwinder.  This depends on a certain stack
20*9356374aSAndroid Build Coastguard Worker //    layout, which is used by gcc (and those systems using a
21*9356374aSAndroid Build Coastguard Worker //    gcc-compatible ABI) on x86 systems, at least since gcc 2.95.
22*9356374aSAndroid Build Coastguard Worker //    It uses the frame pointer to do its work.
23*9356374aSAndroid Build Coastguard Worker //
24*9356374aSAndroid Build Coastguard Worker // 2) The libunwind library.  This is still in development, and as a
25*9356374aSAndroid Build Coastguard Worker //    separate library adds a new dependency, but doesn't need a frame
26*9356374aSAndroid Build Coastguard Worker //    pointer.  It also doesn't call malloc.
27*9356374aSAndroid Build Coastguard Worker //
28*9356374aSAndroid Build Coastguard Worker // 3) The gdb unwinder -- also the one used by the c++ exception code.
29*9356374aSAndroid Build Coastguard Worker //    It's obviously well-tested, but has a fatal flaw: it can call
30*9356374aSAndroid Build Coastguard Worker //    malloc() from the unwinder.  This is a problem because we're
31*9356374aSAndroid Build Coastguard Worker //    trying to use the unwinder to instrument malloc().
32*9356374aSAndroid Build Coastguard Worker //
33*9356374aSAndroid Build Coastguard Worker // Note: if you add a new implementation here, make sure it works
34*9356374aSAndroid Build Coastguard Worker // correctly when absl::GetStackTrace() is called with max_depth == 0.
35*9356374aSAndroid Build Coastguard Worker // Some code may do that.
36*9356374aSAndroid Build Coastguard Worker 
37*9356374aSAndroid Build Coastguard Worker #include "absl/debugging/stacktrace.h"
38*9356374aSAndroid Build Coastguard Worker 
39*9356374aSAndroid Build Coastguard Worker #include <atomic>
40*9356374aSAndroid Build Coastguard Worker 
41*9356374aSAndroid Build Coastguard Worker #include "absl/base/attributes.h"
42*9356374aSAndroid Build Coastguard Worker #include "absl/base/port.h"
43*9356374aSAndroid Build Coastguard Worker #include "absl/debugging/internal/stacktrace_config.h"
44*9356374aSAndroid Build Coastguard Worker 
45*9356374aSAndroid Build Coastguard Worker #if defined(ABSL_STACKTRACE_INL_HEADER)
46*9356374aSAndroid Build Coastguard Worker #include ABSL_STACKTRACE_INL_HEADER
47*9356374aSAndroid Build Coastguard Worker #else
48*9356374aSAndroid Build Coastguard Worker # error Cannot calculate stack trace: will need to write for your environment
49*9356374aSAndroid Build Coastguard Worker 
50*9356374aSAndroid Build Coastguard Worker # include "absl/debugging/internal/stacktrace_aarch64-inl.inc"
51*9356374aSAndroid Build Coastguard Worker # include "absl/debugging/internal/stacktrace_arm-inl.inc"
52*9356374aSAndroid Build Coastguard Worker # include "absl/debugging/internal/stacktrace_emscripten-inl.inc"
53*9356374aSAndroid Build Coastguard Worker # include "absl/debugging/internal/stacktrace_generic-inl.inc"
54*9356374aSAndroid Build Coastguard Worker # include "absl/debugging/internal/stacktrace_powerpc-inl.inc"
55*9356374aSAndroid Build Coastguard Worker # include "absl/debugging/internal/stacktrace_riscv-inl.inc"
56*9356374aSAndroid Build Coastguard Worker # include "absl/debugging/internal/stacktrace_unimplemented-inl.inc"
57*9356374aSAndroid Build Coastguard Worker # include "absl/debugging/internal/stacktrace_win32-inl.inc"
58*9356374aSAndroid Build Coastguard Worker # include "absl/debugging/internal/stacktrace_x86-inl.inc"
59*9356374aSAndroid Build Coastguard Worker #endif
60*9356374aSAndroid Build Coastguard Worker 
61*9356374aSAndroid Build Coastguard Worker namespace absl {
62*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
63*9356374aSAndroid Build Coastguard Worker namespace {
64*9356374aSAndroid Build Coastguard Worker 
65*9356374aSAndroid Build Coastguard Worker typedef int (*Unwinder)(void**, int*, int, int, const void*, int*);
66*9356374aSAndroid Build Coastguard Worker std::atomic<Unwinder> custom;
67*9356374aSAndroid Build Coastguard Worker 
68*9356374aSAndroid Build Coastguard Worker template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
Unwind(void ** result,int * sizes,int max_depth,int skip_count,const void * uc,int * min_dropped_frames)69*9356374aSAndroid Build Coastguard Worker ABSL_ATTRIBUTE_ALWAYS_INLINE inline int Unwind(void** result, int* sizes,
70*9356374aSAndroid Build Coastguard Worker                                                int max_depth, int skip_count,
71*9356374aSAndroid Build Coastguard Worker                                                const void* uc,
72*9356374aSAndroid Build Coastguard Worker                                                int* min_dropped_frames) {
73*9356374aSAndroid Build Coastguard Worker   Unwinder f = &UnwindImpl<IS_STACK_FRAMES, IS_WITH_CONTEXT>;
74*9356374aSAndroid Build Coastguard Worker   Unwinder g = custom.load(std::memory_order_acquire);
75*9356374aSAndroid Build Coastguard Worker   if (g != nullptr) f = g;
76*9356374aSAndroid Build Coastguard Worker 
77*9356374aSAndroid Build Coastguard Worker   // Add 1 to skip count for the unwinder function itself
78*9356374aSAndroid Build Coastguard Worker   int size = (*f)(result, sizes, max_depth, skip_count + 1, uc,
79*9356374aSAndroid Build Coastguard Worker                   min_dropped_frames);
80*9356374aSAndroid Build Coastguard Worker   // To disable tail call to (*f)(...)
81*9356374aSAndroid Build Coastguard Worker   ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
82*9356374aSAndroid Build Coastguard Worker   return size;
83*9356374aSAndroid Build Coastguard Worker }
84*9356374aSAndroid Build Coastguard Worker 
85*9356374aSAndroid Build Coastguard Worker }  // anonymous namespace
86*9356374aSAndroid Build Coastguard Worker 
GetStackFrames(void ** result,int * sizes,int max_depth,int skip_count)87*9356374aSAndroid Build Coastguard Worker ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackFrames(
88*9356374aSAndroid Build Coastguard Worker     void** result, int* sizes, int max_depth, int skip_count) {
89*9356374aSAndroid Build Coastguard Worker   return Unwind<true, false>(result, sizes, max_depth, skip_count, nullptr,
90*9356374aSAndroid Build Coastguard Worker                              nullptr);
91*9356374aSAndroid Build Coastguard Worker }
92*9356374aSAndroid Build Coastguard Worker 
93*9356374aSAndroid Build Coastguard Worker ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
GetStackFramesWithContext(void ** result,int * sizes,int max_depth,int skip_count,const void * uc,int * min_dropped_frames)94*9356374aSAndroid Build Coastguard Worker GetStackFramesWithContext(void** result, int* sizes, int max_depth,
95*9356374aSAndroid Build Coastguard Worker                           int skip_count, const void* uc,
96*9356374aSAndroid Build Coastguard Worker                           int* min_dropped_frames) {
97*9356374aSAndroid Build Coastguard Worker   return Unwind<true, true>(result, sizes, max_depth, skip_count, uc,
98*9356374aSAndroid Build Coastguard Worker                             min_dropped_frames);
99*9356374aSAndroid Build Coastguard Worker }
100*9356374aSAndroid Build Coastguard Worker 
GetStackTrace(void ** result,int max_depth,int skip_count)101*9356374aSAndroid Build Coastguard Worker ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackTrace(
102*9356374aSAndroid Build Coastguard Worker     void** result, int max_depth, int skip_count) {
103*9356374aSAndroid Build Coastguard Worker   return Unwind<false, false>(result, nullptr, max_depth, skip_count, nullptr,
104*9356374aSAndroid Build Coastguard Worker                               nullptr);
105*9356374aSAndroid Build Coastguard Worker }
106*9356374aSAndroid Build Coastguard Worker 
107*9356374aSAndroid Build Coastguard Worker ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
GetStackTraceWithContext(void ** result,int max_depth,int skip_count,const void * uc,int * min_dropped_frames)108*9356374aSAndroid Build Coastguard Worker GetStackTraceWithContext(void** result, int max_depth, int skip_count,
109*9356374aSAndroid Build Coastguard Worker                          const void* uc, int* min_dropped_frames) {
110*9356374aSAndroid Build Coastguard Worker   return Unwind<false, true>(result, nullptr, max_depth, skip_count, uc,
111*9356374aSAndroid Build Coastguard Worker                              min_dropped_frames);
112*9356374aSAndroid Build Coastguard Worker }
113*9356374aSAndroid Build Coastguard Worker 
SetStackUnwinder(Unwinder w)114*9356374aSAndroid Build Coastguard Worker void SetStackUnwinder(Unwinder w) {
115*9356374aSAndroid Build Coastguard Worker   custom.store(w, std::memory_order_release);
116*9356374aSAndroid Build Coastguard Worker }
117*9356374aSAndroid Build Coastguard Worker 
DefaultStackUnwinder(void ** pcs,int * sizes,int depth,int skip,const void * uc,int * min_dropped_frames)118*9356374aSAndroid Build Coastguard Worker int DefaultStackUnwinder(void** pcs, int* sizes, int depth, int skip,
119*9356374aSAndroid Build Coastguard Worker                          const void* uc, int* min_dropped_frames) {
120*9356374aSAndroid Build Coastguard Worker   skip++;  // For this function
121*9356374aSAndroid Build Coastguard Worker   Unwinder f = nullptr;
122*9356374aSAndroid Build Coastguard Worker   if (sizes == nullptr) {
123*9356374aSAndroid Build Coastguard Worker     if (uc == nullptr) {
124*9356374aSAndroid Build Coastguard Worker       f = &UnwindImpl<false, false>;
125*9356374aSAndroid Build Coastguard Worker     } else {
126*9356374aSAndroid Build Coastguard Worker       f = &UnwindImpl<false, true>;
127*9356374aSAndroid Build Coastguard Worker     }
128*9356374aSAndroid Build Coastguard Worker   } else {
129*9356374aSAndroid Build Coastguard Worker     if (uc == nullptr) {
130*9356374aSAndroid Build Coastguard Worker       f = &UnwindImpl<true, false>;
131*9356374aSAndroid Build Coastguard Worker     } else {
132*9356374aSAndroid Build Coastguard Worker       f = &UnwindImpl<true, true>;
133*9356374aSAndroid Build Coastguard Worker     }
134*9356374aSAndroid Build Coastguard Worker   }
135*9356374aSAndroid Build Coastguard Worker   volatile int x = 0;
136*9356374aSAndroid Build Coastguard Worker   int n = (*f)(pcs, sizes, depth, skip, uc, min_dropped_frames);
137*9356374aSAndroid Build Coastguard Worker   x = 1; (void) x;  // To disable tail call to (*f)(...)
138*9356374aSAndroid Build Coastguard Worker   return n;
139*9356374aSAndroid Build Coastguard Worker }
140*9356374aSAndroid Build Coastguard Worker 
141*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
142*9356374aSAndroid Build Coastguard Worker }  // namespace absl
143