Coverage Report

Created: 2026-07-16 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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/stacktrace_config.h"
51
52
#if defined(ABSL_STACKTRACE_INL_HEADER)
53
#include ABSL_STACKTRACE_INL_HEADER
54
#else
55
#error Cannot calculate stack trace: will need to write for your environment
56
57
#include "absl/debugging/internal/stacktrace_aarch64-inl.inc"
58
#include "absl/debugging/internal/stacktrace_arm-inl.inc"
59
#include "absl/debugging/internal/stacktrace_emscripten-inl.inc"
60
#include "absl/debugging/internal/stacktrace_generic-inl.inc"
61
#include "absl/debugging/internal/stacktrace_powerpc-inl.inc"
62
#include "absl/debugging/internal/stacktrace_riscv-inl.inc"
63
#include "absl/debugging/internal/stacktrace_unimplemented-inl.inc"
64
#include "absl/debugging/internal/stacktrace_win32-inl.inc"
65
#include "absl/debugging/internal/stacktrace_x86-inl.inc"
66
#endif
67
68
namespace absl {
69
ABSL_NAMESPACE_BEGIN
70
namespace {
71
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
1.27k
                                               bool unwind_with_fixup = true) {
82
1.27k
  unwind_with_fixup =
83
1.27k
      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
1.27k
  Unwinder g = custom.load(std::memory_order_acquire);
96
1.27k
  size_t size;
97
  // Add 1 to skip count for the unwinder function itself
98
1.27k
  ++skip_count;
99
1.27k
  if (g != nullptr) {
100
0
    size = static_cast<size_t>((*g)(result, sizes, static_cast<int>(max_depth),
101
0
                                    skip_count, uc, min_dropped_frames));
102
    // Frame pointers aren't returned by existing hooks, so clear them.
103
0
    if (frames != nullptr) {
104
0
      std::fill(frames, frames + size, uintptr_t());
105
0
    }
106
1.27k
  } else {
107
1.27k
    size = static_cast<size_t>(UnwindImpl<IS_STACK_FRAMES, IS_WITH_CONTEXT>(
108
1.27k
        result, frames, sizes, static_cast<int>(max_depth), skip_count, uc,
109
1.27k
        min_dropped_frames));
110
1.27k
  }
111
1.27k
  if (unwind_with_fixup) {
112
0
    internal_stacktrace::FixUpStack(result, frames, sizes, max_depth, size);
113
0
  }
114
115
1.27k
  ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
116
1.27k
  return static_cast<int>(size);
117
1.27k
}
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<true, false>(void**, unsigned long*, int*, unsigned long, int, void const*, int*, bool)
stacktrace.cc:int absl::(anonymous namespace)::Unwind<false, false>(void**, unsigned long*, int*, unsigned long, int, void const*, int*, bool)
Line
Count
Source
81
1.27k
                                               bool unwind_with_fixup = true) {
82
1.27k
  unwind_with_fixup =
83
1.27k
      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
1.27k
  Unwinder g = custom.load(std::memory_order_acquire);
96
1.27k
  size_t size;
97
  // Add 1 to skip count for the unwinder function itself
98
1.27k
  ++skip_count;
99
1.27k
  if (g != nullptr) {
100
0
    size = static_cast<size_t>((*g)(result, sizes, static_cast<int>(max_depth),
101
0
                                    skip_count, uc, min_dropped_frames));
102
    // Frame pointers aren't returned by existing hooks, so clear them.
103
0
    if (frames != nullptr) {
104
0
      std::fill(frames, frames + size, uintptr_t());
105
0
    }
106
1.27k
  } else {
107
1.27k
    size = static_cast<size_t>(UnwindImpl<IS_STACK_FRAMES, IS_WITH_CONTEXT>(
108
1.27k
        result, frames, sizes, static_cast<int>(max_depth), skip_count, uc,
109
1.27k
        min_dropped_frames));
110
1.27k
  }
111
1.27k
  if (unwind_with_fixup) {
112
0
    internal_stacktrace::FixUpStack(result, frames, sizes, max_depth, size);
113
0
  }
114
115
1.27k
  ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
116
1.27k
  return static_cast<int>(size);
117
1.27k
}
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, true>(void**, unsigned long*, int*, unsigned long, int, void const*, int*, bool)
118
119
}  // anonymous namespace
120
121
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackFrames(
122
0
    void** result, int* sizes, int max_depth, int skip_count) {
123
0
  return Unwind<true, false>(result, nullptr, sizes,
124
0
                             static_cast<size_t>(max_depth), skip_count,
125
0
                             nullptr, nullptr);
126
0
}
127
128
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
129
internal_stacktrace::GetStackTraceNoFixup(void** result, int max_depth,
130
0
                                          int skip_count) {
131
0
  return Unwind<false, false>(result, nullptr, nullptr,
132
0
                              static_cast<size_t>(max_depth), skip_count,
133
0
                              nullptr, nullptr, /*unwind_with_fixup=*/false);
134
0
}
135
136
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
137
GetStackFramesWithContext(void** result, int* sizes, int max_depth,
138
                          int skip_count, const void* uc,
139
0
                          int* min_dropped_frames) {
140
0
  return Unwind<true, true>(result, nullptr, sizes,
141
0
                            static_cast<size_t>(max_depth), skip_count, uc,
142
0
                            min_dropped_frames);
143
0
}
144
145
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackTrace(
146
1.27k
    void** result, int max_depth, int skip_count) {
147
1.27k
  return Unwind<false, false>(result, nullptr, nullptr,
148
1.27k
                              static_cast<size_t>(max_depth), skip_count,
149
1.27k
                              nullptr, nullptr);
150
1.27k
}
151
152
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
153
GetStackTraceWithContext(void** result, int max_depth, int skip_count,
154
0
                         const void* uc, int* min_dropped_frames) {
155
0
  return Unwind<false, true>(result, nullptr, nullptr,
156
0
                             static_cast<size_t>(max_depth), skip_count, uc,
157
0
                             min_dropped_frames);
158
0
}
159
160
0
void SetStackUnwinder(Unwinder w) {
161
0
  custom.store(w, std::memory_order_release);
162
0
}
163
164
int DefaultStackUnwinder(void** pcs, int* sizes, int depth, int skip,
165
0
                         const void* uc, int* min_dropped_frames) {
166
0
  skip++;  // For this function
167
0
  decltype(&UnwindImpl<false, false>) f;
168
0
  if (sizes == nullptr) {
169
0
    if (uc == nullptr) {
170
0
      f = &UnwindImpl<false, false>;
171
0
    } else {
172
0
      f = &UnwindImpl<false, true>;
173
0
    }
174
0
  } else {
175
0
    if (uc == nullptr) {
176
0
      f = &UnwindImpl<true, false>;
177
0
    } else {
178
0
      f = &UnwindImpl<true, true>;
179
0
    }
180
0
  }
181
0
  int n = (*f)(pcs, nullptr, sizes, depth, skip, uc, min_dropped_frames);
182
0
  ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
183
0
  return n;
184
0
}
185
186
1.27k
ABSL_ATTRIBUTE_WEAK bool internal_stacktrace::ShouldFixUpStack() {
187
1.27k
  return false;
188
1.27k
}
189
190
// Fixes up the stack trace of the current thread, in the first `depth` frames
191
// of each buffer. The buffers need to be larger than `depth`, to accommodate
192
// any newly inserted elements. `depth` is updated to reflect the new number of
193
// elements valid across all the buffers. (It is therefore recommended that all
194
// buffer sizes be equal.)
195
//
196
// The `frames` and `sizes` parameters denote the bounds of the stack frame
197
// corresponding to each instruction pointer in the `pcs`.
198
// Any elements inside these buffers may be zero or null, in which case that
199
// information is assumed to be absent/unavailable.
200
ABSL_ATTRIBUTE_WEAK void internal_stacktrace::FixUpStack(void**, uintptr_t*,
201
                                                         int*, size_t,
202
0
                                                         size_t&) {}
203
204
ABSL_NAMESPACE_END
205
}  // namespace absl