Coverage Report

Created: 2023-09-25 06:27

/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 <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_emscripten-inl.inc"
53
# include "absl/debugging/internal/stacktrace_generic-inl.inc"
54
# include "absl/debugging/internal/stacktrace_powerpc-inl.inc"
55
# include "absl/debugging/internal/stacktrace_riscv-inl.inc"
56
# include "absl/debugging/internal/stacktrace_unimplemented-inl.inc"
57
# include "absl/debugging/internal/stacktrace_win32-inl.inc"
58
# include "absl/debugging/internal/stacktrace_x86-inl.inc"
59
#endif
60
61
namespace absl {
62
ABSL_NAMESPACE_BEGIN
63
namespace {
64
65
typedef int (*Unwinder)(void**, int*, int, int, const void*, int*);
66
std::atomic<Unwinder> custom;
67
68
template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
69
ABSL_ATTRIBUTE_ALWAYS_INLINE inline int Unwind(void** result, int* sizes,
70
                                               int max_depth, int skip_count,
71
                                               const void* uc,
72
0
                                               int* min_dropped_frames) {
73
0
  Unwinder f = &UnwindImpl<IS_STACK_FRAMES, IS_WITH_CONTEXT>;
74
0
  Unwinder g = custom.load(std::memory_order_acquire);
75
0
  if (g != nullptr) f = g;
76
77
  // Add 1 to skip count for the unwinder function itself
78
0
  int size = (*f)(result, sizes, max_depth, skip_count + 1, uc,
79
0
                  min_dropped_frames);
80
  // To disable tail call to (*f)(...)
81
0
  ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
82
0
  return size;
83
0
}
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<true, false>(void**, int*, int, int, void const*, int*)
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<true, true>(void**, int*, int, int, void const*, int*)
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<false, false>(void**, int*, int, int, void const*, int*)
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<false, true>(void**, int*, int, int, void const*, int*)
84
85
}  // anonymous namespace
86
87
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackFrames(
88
0
    void** result, int* sizes, int max_depth, int skip_count) {
89
0
  return Unwind<true, false>(result, sizes, max_depth, skip_count, nullptr,
90
0
                             nullptr);
91
0
}
92
93
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
94
GetStackFramesWithContext(void** result, int* sizes, int max_depth,
95
                          int skip_count, const void* uc,
96
0
                          int* min_dropped_frames) {
97
0
  return Unwind<true, true>(result, sizes, max_depth, skip_count, uc,
98
0
                            min_dropped_frames);
99
0
}
100
101
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackTrace(
102
0
    void** result, int max_depth, int skip_count) {
103
0
  return Unwind<false, false>(result, nullptr, max_depth, skip_count, nullptr,
104
0
                              nullptr);
105
0
}
106
107
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
108
GetStackTraceWithContext(void** result, int max_depth, int skip_count,
109
0
                         const void* uc, int* min_dropped_frames) {
110
0
  return Unwind<false, true>(result, nullptr, max_depth, skip_count, uc,
111
0
                             min_dropped_frames);
112
0
}
113
114
0
void SetStackUnwinder(Unwinder w) {
115
0
  custom.store(w, std::memory_order_release);
116
0
}
117
118
int DefaultStackUnwinder(void** pcs, int* sizes, int depth, int skip,
119
0
                         const void* uc, int* min_dropped_frames) {
120
0
  skip++;  // For this function
121
0
  Unwinder f = nullptr;
122
0
  if (sizes == nullptr) {
123
0
    if (uc == nullptr) {
124
0
      f = &UnwindImpl<false, false>;
125
0
    } else {
126
0
      f = &UnwindImpl<false, true>;
127
0
    }
128
0
  } else {
129
0
    if (uc == nullptr) {
130
0
      f = &UnwindImpl<true, false>;
131
0
    } else {
132
0
      f = &UnwindImpl<true, true>;
133
0
    }
134
0
  }
135
0
  volatile int x = 0;
136
0
  int n = (*f)(pcs, sizes, depth, skip, uc, min_dropped_frames);
137
0
  x = 1; (void) x;  // To disable tail call to (*f)(...)
138
0
  return n;
139
0
}
140
141
ABSL_NAMESPACE_END
142
}  // namespace absl