Coverage Report

Created: 2026-08-13 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/debugging/internal/examine_stack.cc
Line
Count
Source
1
//
2
// Copyright 2018 The Abseil Authors.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
//      https://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
//
16
17
#include "absl/debugging/internal/examine_stack.h"
18
19
#include <iterator>
20
21
#ifndef _WIN32
22
#include <unistd.h>
23
#endif
24
25
#include "absl/base/config.h"
26
27
#ifdef ABSL_HAVE_MMAP
28
#include <sys/mman.h>
29
#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
30
#define MAP_ANONYMOUS MAP_ANON
31
#endif
32
#endif
33
34
#if defined(__linux__) || defined(__APPLE__)
35
#include <sys/ucontext.h>
36
#endif
37
38
#include <csignal>
39
#include <cstdio>
40
41
#include "absl/base/attributes.h"
42
#include "absl/base/internal/raw_logging.h"
43
#include "absl/base/macros.h"
44
#include "absl/debugging/stacktrace.h"
45
#include "absl/debugging/symbolize.h"
46
47
namespace absl {
48
ABSL_NAMESPACE_BEGIN
49
namespace debugging_internal {
50
51
namespace {
52
constexpr int kDefaultDumpStackFramesLimit = 64;
53
// The %p field width for printf() functions is two characters per byte,
54
// and two extra for the leading "0x".
55
constexpr int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
56
57
ABSL_CONST_INIT SymbolizeUrlEmitter debug_stack_trace_hook = nullptr;
58
59
// Async-signal safe mmap allocator.
60
0
void* Allocate(size_t num_bytes) {
61
0
#ifdef ABSL_HAVE_MMAP
62
0
  void* p = ::mmap(nullptr, num_bytes, PROT_READ | PROT_WRITE,
63
0
                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
64
0
  return p == MAP_FAILED ? nullptr : p;
65
#else
66
  (void)num_bytes;
67
  return nullptr;
68
#endif  // ABSL_HAVE_MMAP
69
0
}
70
71
0
void Deallocate(void* p, size_t size) {
72
0
#ifdef ABSL_HAVE_MMAP
73
0
  ::munmap(p, size);
74
#else
75
  (void)p;
76
  (void)size;
77
#endif  // ABSL_HAVE_MMAP
78
0
}
79
80
// Print a program counter only.
81
void DumpPC(OutputWriter* writer, void* writer_arg, void* const pc,
82
0
            const char* const prefix) {
83
0
  char buf[100];
84
0
  snprintf(buf, sizeof(buf), "%s@ %*p\n", prefix, kPrintfPointerFieldWidth, pc);
85
0
  writer(buf, writer_arg);
86
0
}
87
88
// Print a program counter and the corresponding stack frame size.
89
void DumpPCAndFrameSize(OutputWriter* writer, void* writer_arg, void* const pc,
90
0
                        int framesize, const char* const prefix) {
91
0
  char buf[100];
92
0
  if (framesize <= 0) {
93
0
    snprintf(buf, sizeof(buf), "%s@ %*p  (unknown)\n", prefix,
94
0
             kPrintfPointerFieldWidth, pc);
95
0
  } else {
96
0
    snprintf(buf, sizeof(buf), "%s@ %*p  %9d\n", prefix,
97
0
             kPrintfPointerFieldWidth, pc, framesize);
98
0
  }
99
0
  writer(buf, writer_arg);
100
0
}
101
102
// Print a program counter and the corresponding symbol.
103
void DumpPCAndSymbol(OutputWriter* writer, void* writer_arg, void* const pc,
104
0
                     const char* const prefix) {
105
0
  char tmp[1024];
106
0
  const char* symbol = "(unknown)";
107
  // Symbolizes the previous address of pc because pc may be in the
108
  // next function.  The overrun happens when the function ends with
109
  // a call to a function annotated noreturn (e.g. CHECK).
110
  // If symbolization of pc-1 fails, also try pc on the off-chance
111
  // that we crashed on the first instruction of a function (that
112
  // actually happens very often for e.g. __restore_rt).
113
0
  const uintptr_t prev_pc = reinterpret_cast<uintptr_t>(pc) - 1;
114
0
  if (absl::Symbolize(reinterpret_cast<const char*>(prev_pc), tmp,
115
0
                      sizeof(tmp)) ||
116
0
      absl::Symbolize(pc, tmp, sizeof(tmp))) {
117
0
    symbol = tmp;
118
0
  }
119
0
  char buf[1024];
120
0
  snprintf(buf, sizeof(buf), "%s@ %*p  %s\n", prefix, kPrintfPointerFieldWidth,
121
0
           pc, symbol);
122
0
  writer(buf, writer_arg);
123
0
}
124
125
// Print a program counter, its stack frame size, and its symbol name.
126
// Note that there is a separate symbolize_pc argument. Return addresses may be
127
// at the end of the function, and this allows the caller to back up from pc if
128
// appropriate.
129
void DumpPCAndFrameSizeAndSymbol(OutputWriter* writer, void* writer_arg,
130
                                 void* const pc, void* const symbolize_pc,
131
0
                                 int framesize, const char* const prefix) {
132
0
  char tmp[1024];
133
0
  const char* symbol = "(unknown)";
134
0
  if (absl::Symbolize(symbolize_pc, tmp, sizeof(tmp))) {
135
0
    symbol = tmp;
136
0
  }
137
0
  char buf[1024];
138
0
  if (framesize <= 0) {
139
0
    snprintf(buf, sizeof(buf), "%s@ %*p  (unknown)  %s\n", prefix,
140
0
             kPrintfPointerFieldWidth, pc, symbol);
141
0
  } else {
142
0
    snprintf(buf, sizeof(buf), "%s@ %*p  %9d  %s\n", prefix,
143
0
             kPrintfPointerFieldWidth, pc, framesize, symbol);
144
0
  }
145
0
  writer(buf, writer_arg);
146
0
}
147
148
}  // namespace
149
150
0
void RegisterDebugStackTraceHook(SymbolizeUrlEmitter hook) {
151
0
  debug_stack_trace_hook = hook;
152
0
}
153
154
0
SymbolizeUrlEmitter GetDebugStackTraceHook() { return debug_stack_trace_hook; }
155
156
// Returns the program counter from signal context, nullptr if
157
// unknown. vuc is a ucontext_t*. We use void* to avoid the use of
158
// ucontext_t on non-POSIX systems.
159
0
void* GetProgramCounter(void* const vuc) {
160
0
#ifdef __linux__
161
0
  if (vuc != nullptr) {
162
0
    ucontext_t* context = reinterpret_cast<ucontext_t*>(vuc);
163
#if defined(__aarch64__)
164
    return reinterpret_cast<void*>(context->uc_mcontext.pc);
165
#elif defined(__alpha__)
166
    return reinterpret_cast<void*>(context->uc_mcontext.sc_pc);
167
#elif defined(__arm__)
168
    return reinterpret_cast<void*>(context->uc_mcontext.arm_pc);
169
#elif defined(__hppa__)
170
    return reinterpret_cast<void*>(context->uc_mcontext.sc_iaoq[0]);
171
#elif defined(__i386__)
172
    if (14 < std::size(context->uc_mcontext.gregs))
173
      return reinterpret_cast<void*>(context->uc_mcontext.gregs[14]);
174
#elif defined(__ia64__)
175
    return reinterpret_cast<void*>(context->uc_mcontext.sc_ip);
176
#elif defined(__m68k__)
177
    return reinterpret_cast<void*>(context->uc_mcontext.gregs[16]);
178
#elif defined(__mips__)
179
    return reinterpret_cast<void*>(context->uc_mcontext.pc);
180
#elif defined(__powerpc64__)
181
    return reinterpret_cast<void*>(context->uc_mcontext.gp_regs[32]);
182
#elif defined(__powerpc__)
183
    return reinterpret_cast<void*>(context->uc_mcontext.uc_regs->gregs[32]);
184
#elif defined(__riscv)
185
    return reinterpret_cast<void*>(context->uc_mcontext.__gregs[REG_PC]);
186
#elif defined(__s390__) && !defined(__s390x__)
187
    return reinterpret_cast<void*>(context->uc_mcontext.psw.addr & 0x7fffffff);
188
#elif defined(__s390__) && defined(__s390x__)
189
    return reinterpret_cast<void*>(context->uc_mcontext.psw.addr);
190
#elif defined(__sh__)
191
    return reinterpret_cast<void*>(context->uc_mcontext.pc);
192
#elif defined(__sparc__) && !defined(__arch64__)
193
    return reinterpret_cast<void*>(context->uc_mcontext.gregs[19]);
194
#elif defined(__sparc__) && defined(__arch64__)
195
    return reinterpret_cast<void*>(context->uc_mcontext.mc_gregs[19]);
196
#elif defined(__x86_64__)
197
0
    if (16 < std::size(context->uc_mcontext.gregs))
198
0
      return reinterpret_cast<void*>(context->uc_mcontext.gregs[16]);
199
#elif defined(__e2k__)
200
    return reinterpret_cast<void*>(context->uc_mcontext.cr0_hi);
201
#elif defined(__loongarch__)
202
    return reinterpret_cast<void*>(context->uc_mcontext.__pc);
203
#else
204
#error "Undefined Architecture."
205
#endif
206
0
  }
207
#elif defined(__APPLE__)
208
  if (vuc != nullptr) {
209
    ucontext_t* signal_ucontext = reinterpret_cast<ucontext_t*>(vuc);
210
#if defined(__aarch64__)
211
    return reinterpret_cast<void*>(
212
        __darwin_arm_thread_state64_get_pc(signal_ucontext->uc_mcontext->__ss));
213
#elif defined(__arm__)
214
#if __DARWIN_UNIX03
215
    return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->__ss.__pc);
216
#else
217
    return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->ss.pc);
218
#endif
219
#elif defined(__i386__)
220
#if __DARWIN_UNIX03
221
    return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->__ss.__eip);
222
#else
223
    return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->ss.eip);
224
#endif
225
#elif defined(__x86_64__)
226
#if __DARWIN_UNIX03
227
    return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->__ss.__rip);
228
#else
229
    return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->ss.rip);
230
#endif
231
#endif
232
  }
233
#elif defined(__akaros__)
234
  auto* ctx = reinterpret_cast<struct user_context*>(vuc);
235
  return reinterpret_cast<void*>(get_user_ctx_pc(ctx));
236
#endif
237
0
  static_cast<void>(vuc);
238
0
  return nullptr;
239
0
}
240
241
void DumpPCAndFrameSizesAndStackTrace(void* const pc, void* const stack[],
242
                                      int frame_sizes[], int depth,
243
                                      int min_dropped_frames,
244
                                      bool symbolize_stacktrace,
245
0
                                      OutputWriter* writer, void* writer_arg) {
246
0
  if (pc != nullptr) {
247
    // We don't know the stack frame size for PC, use 0.
248
0
    if (symbolize_stacktrace) {
249
0
      DumpPCAndFrameSizeAndSymbol(writer, writer_arg, pc, pc, 0, "PC: ");
250
0
    } else {
251
0
      DumpPCAndFrameSize(writer, writer_arg, pc, 0, "PC: ");
252
0
    }
253
0
  }
254
0
  for (int i = 0; i < depth; i++) {
255
0
    if (symbolize_stacktrace) {
256
      // Pass the previous address of pc as the symbol address because pc is a
257
      // return address, and an overrun may occur when the function ends with a
258
      // call to a function annotated noreturn (e.g. CHECK). Note that we don't
259
      // do this for pc above, as the adjustment is only correct for return
260
      // addresses.
261
0
      DumpPCAndFrameSizeAndSymbol(writer, writer_arg, stack[i],
262
0
                                  reinterpret_cast<char*>(stack[i]) - 1,
263
0
                                  frame_sizes[i], "    ");
264
0
    } else {
265
0
      DumpPCAndFrameSize(writer, writer_arg, stack[i], frame_sizes[i], "    ");
266
0
    }
267
0
  }
268
0
  if (min_dropped_frames > 0) {
269
0
    char buf[100];
270
0
    snprintf(buf, sizeof(buf), "    @ ... and at least %d more frames\n",
271
0
             min_dropped_frames);
272
0
    writer(buf, writer_arg);
273
0
  }
274
0
}
275
276
// Dump current stack trace as directed by writer.
277
// Make sure this function is not inlined to avoid skipping too many top frames.
278
ABSL_ATTRIBUTE_NOINLINE
279
void DumpStackTrace(int min_dropped_frames, int max_num_frames,
280
                    bool symbolize_stacktrace, OutputWriter* writer,
281
0
                    void* writer_arg) {
282
  // Print stack trace
283
0
  void* stack_buf[kDefaultDumpStackFramesLimit];
284
0
  void** stack = stack_buf;
285
0
  int num_stack = kDefaultDumpStackFramesLimit;
286
0
  size_t allocated_bytes = 0;
287
288
0
  if (num_stack >= max_num_frames) {
289
    // User requested fewer frames than we already have space for.
290
0
    num_stack = max_num_frames;
291
0
  } else {
292
0
    const size_t needed_bytes =
293
0
        static_cast<size_t>(max_num_frames) * sizeof(stack[0]);
294
0
    void* p = Allocate(needed_bytes);
295
0
    if (p != nullptr) {  // We got the space.
296
0
      num_stack = max_num_frames;
297
0
      stack = reinterpret_cast<void**>(p);
298
0
      allocated_bytes = needed_bytes;
299
0
    }
300
0
  }
301
302
0
  int depth = absl::GetStackTrace(stack, num_stack, min_dropped_frames + 1);
303
0
  for (int i = 0; i < depth; i++) {
304
0
    if (symbolize_stacktrace) {
305
0
      DumpPCAndSymbol(writer, writer_arg, stack[static_cast<size_t>(i)],
306
0
                      "    ");
307
0
    } else {
308
0
      DumpPC(writer, writer_arg, stack[static_cast<size_t>(i)], "    ");
309
0
    }
310
0
  }
311
312
0
  auto hook = GetDebugStackTraceHook();
313
0
  if (hook != nullptr) {
314
0
    hook(stack, depth, /*crash_pc=*/nullptr, writer, writer_arg);
315
0
  }
316
317
0
  if (allocated_bytes != 0) Deallocate(stack, allocated_bytes);
318
0
}
319
320
}  // namespace debugging_internal
321
ABSL_NAMESPACE_END
322
}  // namespace absl