Coverage Report

Created: 2026-09-14 06:45

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