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