bloaty/third_party/abseil-cpp/absl/debugging/stacktrace.cc
Go to the documentation of this file.
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 <atomic>
40 
41 #include "absl/base/attributes.h"
42 #include "absl/base/port.h"
43 #include "absl/debugging/internal/stacktrace_config.h"
44 
45 #if defined(ABSL_STACKTRACE_INL_HEADER)
46 #include ABSL_STACKTRACE_INL_HEADER
47 #else
48 # error Cannot calculate stack trace: will need to write for your environment
49 
50 # include "absl/debugging/internal/stacktrace_aarch64-inl.inc"
51 # include "absl/debugging/internal/stacktrace_arm-inl.inc"
52 # include "absl/debugging/internal/stacktrace_generic-inl.inc"
53 # include "absl/debugging/internal/stacktrace_powerpc-inl.inc"
54 # include "absl/debugging/internal/stacktrace_unimplemented-inl.inc"
55 # include "absl/debugging/internal/stacktrace_win32-inl.inc"
56 # include "absl/debugging/internal/stacktrace_x86-inl.inc"
57 #endif
58 
59 namespace absl {
61 namespace {
62 
63 typedef int (*Unwinder)(void**, int*, int, int, const void*, int*);
64 std::atomic<Unwinder> custom;
65 
66 template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
67 ABSL_ATTRIBUTE_ALWAYS_INLINE inline int Unwind(void** result, int* sizes,
68  int max_depth, int skip_count,
69  const void* uc,
70  int* min_dropped_frames) {
71  Unwinder f = &UnwindImpl<IS_STACK_FRAMES, IS_WITH_CONTEXT>;
72  Unwinder g = custom.load(std::memory_order_acquire);
73  if (g != nullptr) f = g;
74 
75  // Add 1 to skip count for the unwinder function itself
76  int size = (*f)(result, sizes, max_depth, skip_count + 1, uc,
77  min_dropped_frames);
78  // To disable tail call to (*f)(...)
80  return size;
81 }
82 
83 } // anonymous namespace
84 
86  void** result, int* sizes, int max_depth, int skip_count) {
87  return Unwind<true, false>(result, sizes, max_depth, skip_count, nullptr,
88  nullptr);
89 }
90 
92 GetStackFramesWithContext(void** result, int* sizes, int max_depth,
93  int skip_count, const void* uc,
94  int* min_dropped_frames) {
95  return Unwind<true, true>(result, sizes, max_depth, skip_count, uc,
96  min_dropped_frames);
97 }
98 
100  void** result, int max_depth, int skip_count) {
101  return Unwind<false, false>(result, nullptr, max_depth, skip_count, nullptr,
102  nullptr);
103 }
104 
106 GetStackTraceWithContext(void** result, int max_depth, int skip_count,
107  const void* uc, int* min_dropped_frames) {
108  return Unwind<false, true>(result, nullptr, max_depth, skip_count, uc,
109  min_dropped_frames);
110 }
111 
112 void SetStackUnwinder(Unwinder w) {
113  custom.store(w, std::memory_order_release);
114 }
115 
116 int DefaultStackUnwinder(void** pcs, int* sizes, int depth, int skip,
117  const void* uc, int* min_dropped_frames) {
118  skip++; // For this function
119  Unwinder f = nullptr;
120  if (sizes == nullptr) {
121  if (uc == nullptr) {
122  f = &UnwindImpl<false, false>;
123  } else {
124  f = &UnwindImpl<false, true>;
125  }
126  } else {
127  if (uc == nullptr) {
128  f = &UnwindImpl<true, false>;
129  } else {
130  f = &UnwindImpl<true, true>;
131  }
132  }
133  volatile int x = 0;
134  int n = (*f)(pcs, sizes, depth, skip, uc, min_dropped_frames);
135  x = 1; (void) x; // To disable tail call to (*f)(...)
136  return n;
137 }
138 
140 } // namespace absl
ABSL_ATTRIBUTE_ALWAYS_INLINE
#define ABSL_ATTRIBUTE_ALWAYS_INLINE
Definition: abseil-cpp/absl/base/attributes.h:105
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
absl::GetStackTrace
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackTrace(void **result, int max_depth, int skip_count)
Definition: abseil-cpp/absl/debugging/stacktrace.cc:101
mkowners.skip
bool skip
Definition: mkowners.py:224
absl::GetStackFrames
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackFrames(void **result, int *sizes, int max_depth, int skip_count)
Definition: abseil-cpp/absl/debugging/stacktrace.cc:87
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
ABSL_ATTRIBUTE_NOINLINE
#define ABSL_ATTRIBUTE_NOINLINE
Definition: abseil-cpp/absl/base/attributes.h:112
absl::ABSL_NAMESPACE_BEGIN::Unwind
ABSL_ATTRIBUTE_ALWAYS_INLINE int Unwind(void **result, int *sizes, int max_depth, int skip_count, const void *uc, int *min_dropped_frames)
Definition: abseil-cpp/absl/debugging/stacktrace.cc:69
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
xds_interop_client.int
int
Definition: xds_interop_client.py:113
absl::SetStackUnwinder
void SetStackUnwinder(Unwinder w)
Definition: abseil-cpp/absl/debugging/stacktrace.cc:114
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
absl::ABSL_NAMESPACE_BEGIN::custom
std::atomic< Unwinder > custom
Definition: abseil-cpp/absl/debugging/stacktrace.cc:66
g
struct @717 g
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
absl::ABSL_NAMESPACE_BEGIN::pcs
void * pcs[kMaxStackDepth]
Definition: stacktrace_benchmark.cc:27
absl::DefaultStackUnwinder
int DefaultStackUnwinder(void **pcs, int *sizes, int depth, int skip, const void *uc, int *min_dropped_frames)
Definition: abseil-cpp/absl/debugging/stacktrace.cc:118
ABSL_BLOCK_TAIL_CALL_OPTIMIZATION
#define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION()
Definition: abseil-cpp/absl/base/optimization.h:55
ABSL_ATTRIBUTE_NO_TAIL_CALL
#define ABSL_ATTRIBUTE_NO_TAIL_CALL
Definition: abseil-cpp/absl/base/attributes.h:127
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
absl::GetStackTraceWithContext
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)
Definition: abseil-cpp/absl/debugging/stacktrace.cc:108
mkowners.depth
depth
Definition: mkowners.py:114
absl::ABSL_NAMESPACE_BEGIN::Unwinder
int(* Unwinder)(void **, int *, int, int, const void *, int *)
Definition: abseil-cpp/absl/debugging/stacktrace.cc:65
absl::GetStackFramesWithContext
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)
Definition: abseil-cpp/absl/debugging/stacktrace.cc:94


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:17