Coverage Report

Created: 2025-08-02 06:33

/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, size_t max_depth,
101
                                               int skip_count, const void* uc,
102
0
                                               int* min_dropped_frames) {
103
0
  bool unwind_with_fixup = internal_stacktrace::ShouldFixUpStack();
104
0
  if (unwind_with_fixup) {
105
0
    if constexpr (kHaveAlloca) {
106
      // Some implementations of FixUpStack may need to be passed frame
107
      // information from Unwind, even if the caller doesn't need that
108
      // information. We allocate the necessary buffers for such implementations
109
      // here.
110
0
      if (frames == nullptr) {
111
0
        frames = static_cast<uintptr_t*>(alloca(max_depth * sizeof(*frames)));
112
0
      }
113
0
      if (sizes == nullptr) {
114
0
        sizes = static_cast<int*>(alloca(max_depth * sizeof(*sizes)));
115
0
      }
116
0
    }
117
0
  }
118
119
0
  Unwinder g = custom.load(std::memory_order_acquire);
120
0
  size_t size;
121
  // Add 1 to skip count for the unwinder function itself
122
0
  ++skip_count;
123
0
  if (g != nullptr) {
124
0
    size = static_cast<size_t>((*g)(result, sizes, static_cast<int>(max_depth),
125
0
                                    skip_count, uc, min_dropped_frames));
126
    // Frame pointers aren't returned by existing hooks, so clear them.
127
0
    if (frames != nullptr) {
128
0
      std::fill(frames, frames + size, uintptr_t());
129
0
    }
130
0
  } else {
131
0
    size = static_cast<size_t>(
132
0
        unwind_with_fixup
133
0
            ? UnwindImpl<true, IS_WITH_CONTEXT>(
134
0
                  result, frames, sizes, static_cast<int>(max_depth),
135
0
                  skip_count, uc, min_dropped_frames)
136
0
            : UnwindImpl<IS_STACK_FRAMES, IS_WITH_CONTEXT>(
137
0
                  result, frames, sizes, static_cast<int>(max_depth),
138
0
                  skip_count, uc, min_dropped_frames));
139
0
  }
140
0
  if (unwind_with_fixup) {
141
0
    internal_stacktrace::FixUpStack(result, frames, sizes, max_depth, size);
142
0
  }
143
0
  ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
144
0
  return static_cast<int>(size);
145
0
}
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<true, false>(void**, unsigned long*, int*, unsigned long, int, void const*, int*)
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<true, true>(void**, unsigned long*, int*, unsigned long, int, void const*, int*)
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<false, false>(void**, unsigned long*, int*, unsigned long, int, void const*, int*)
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<false, true>(void**, unsigned long*, int*, unsigned long, int, void const*, int*)
146
147
}  // anonymous namespace
148
149
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
150
internal_stacktrace::GetStackFrames(void** result, uintptr_t* frames,
151
0
                                    int* sizes, int max_depth, int skip_count) {
152
0
  return Unwind<true, false>(result, frames, sizes,
153
0
                             static_cast<size_t>(max_depth), skip_count,
154
0
                             nullptr, nullptr);
155
0
}
156
157
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
158
internal_stacktrace::GetStackFramesWithContext(void** result, uintptr_t* frames,
159
                                               int* sizes, int max_depth,
160
                                               int skip_count, const void* uc,
161
0
                                               int* min_dropped_frames) {
162
0
  return Unwind<true, true>(result, frames, sizes,
163
0
                            static_cast<size_t>(max_depth), skip_count, uc,
164
0
                            min_dropped_frames);
165
0
}
166
167
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackTrace(
168
0
    void** result, int max_depth, int skip_count) {
169
0
  return Unwind<false, false>(result, nullptr, nullptr,
170
0
                              static_cast<size_t>(max_depth), skip_count,
171
0
                              nullptr, nullptr);
172
0
}
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
  return Unwind<false, true>(result, nullptr, nullptr,
178
0
                             static_cast<size_t>(max_depth), skip_count, uc,
179
0
                             min_dropped_frames);
180
0
}
181
182
0
void SetStackUnwinder(Unwinder w) {
183
0
  custom.store(w, std::memory_order_release);
184
0
}
185
186
ABSL_ATTRIBUTE_ALWAYS_INLINE static inline int DefaultStackUnwinderImpl(
187
    void** pcs, uintptr_t* frames, int* sizes, int depth, int skip,
188
0
    const void* uc, int* min_dropped_frames) {
189
0
  skip++;  // For this function
190
0
  decltype(&UnwindImpl<false, false>) f;
191
0
  if (sizes == nullptr) {
192
0
    if (uc == nullptr) {
193
0
      f = &UnwindImpl<false, false>;
194
0
    } else {
195
0
      f = &UnwindImpl<false, true>;
196
0
    }
197
0
  } else {
198
0
    if (uc == nullptr) {
199
0
      f = &UnwindImpl<true, false>;
200
0
    } else {
201
0
      f = &UnwindImpl<true, true>;
202
0
    }
203
0
  }
204
0
  return (*f)(pcs, frames, sizes, depth, skip, uc, min_dropped_frames);
205
0
}
206
207
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
208
internal_stacktrace::DefaultStackUnwinder(void** pcs, uintptr_t* frames,
209
                                          int* sizes, int depth, int skip,
210
                                          const void* uc,
211
0
                                          int* min_dropped_frames) {
212
0
  int n = DefaultStackUnwinderImpl(pcs, frames, sizes, depth, skip, uc,
213
0
                                   min_dropped_frames);
214
0
  ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
215
0
  return n;
216
0
}
217
218
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int DefaultStackUnwinder(
219
    void** pcs, int* sizes, int depth, int skip, const void* uc,
220
0
    int* min_dropped_frames) {
221
0
  int n = DefaultStackUnwinderImpl(pcs, nullptr, sizes, depth, skip, uc,
222
0
                                   min_dropped_frames);
223
0
  ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
224
0
  return n;
225
0
}
226
227
0
ABSL_ATTRIBUTE_WEAK bool internal_stacktrace::ShouldFixUpStack() {
228
0
  return false;
229
0
}
230
231
// Fixes up the stack trace of the current thread, in the first `depth` frames
232
// of each buffer. The buffers need to be larger than `depth`, to accommodate
233
// any newly inserted elements. `depth` is updated to reflect the new number of
234
// elements valid across all the buffers. (It is therefore recommended that all
235
// buffer sizes be equal.)
236
//
237
// The `frames` and `sizes` parameters denote the bounds of the stack frame
238
// corresponding to each instruction pointer in the `pcs`.
239
// Any elements inside these buffers may be zero or null, in which case that
240
// information is assumed to be absent/unavailable.
241
ABSL_ATTRIBUTE_WEAK void internal_stacktrace::FixUpStack(void**, uintptr_t*,
242
                                                         int*, size_t,
243
0
                                                         size_t&) {}
244
245
ABSL_NAMESPACE_END
246
}  // namespace absl