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 #ifndef _WIN32
20 #include <unistd.h>
21 #endif
22
23 #include <csignal>
24 #include <cstdio>
25
26 #include "absl/base/attributes.h"
27 #include "absl/base/internal/raw_logging.h"
28 #include "absl/base/macros.h"
29 #include "absl/debugging/stacktrace.h"
30 #include "absl/debugging/symbolize.h"
31
32 namespace absl {
33 ABSL_NAMESPACE_BEGIN
34 namespace debugging_internal {
35
36 // Returns the program counter from signal context, nullptr if
37 // unknown. vuc is a ucontext_t*. We use void* to avoid the use of
38 // ucontext_t on non-POSIX systems.
GetProgramCounter(void * vuc)39 void* GetProgramCounter(void* vuc) {
40 #ifdef __linux__
41 if (vuc != nullptr) {
42 ucontext_t* context = reinterpret_cast<ucontext_t*>(vuc);
43 #if defined(__aarch64__)
44 return reinterpret_cast<void*>(context->uc_mcontext.pc);
45 #elif defined(__arm__)
46 return reinterpret_cast<void*>(context->uc_mcontext.arm_pc);
47 #elif defined(__i386__)
48 if (14 < ABSL_ARRAYSIZE(context->uc_mcontext.gregs))
49 return reinterpret_cast<void*>(context->uc_mcontext.gregs[14]);
50 #elif defined(__mips__)
51 return reinterpret_cast<void*>(context->uc_mcontext.pc);
52 #elif defined(__powerpc64__)
53 return reinterpret_cast<void*>(context->uc_mcontext.gp_regs[32]);
54 #elif defined(__powerpc__)
55 return reinterpret_cast<void*>(context->uc_mcontext.regs->nip);
56 #elif defined(__riscv)
57 return reinterpret_cast<void*>(context->uc_mcontext.__gregs[REG_PC]);
58 #elif defined(__s390__) && !defined(__s390x__)
59 return reinterpret_cast<void*>(context->uc_mcontext.psw.addr & 0x7fffffff);
60 #elif defined(__s390__) && defined(__s390x__)
61 return reinterpret_cast<void*>(context->uc_mcontext.psw.addr);
62 #elif defined(__x86_64__)
63 if (16 < ABSL_ARRAYSIZE(context->uc_mcontext.gregs))
64 return reinterpret_cast<void*>(context->uc_mcontext.gregs[16]);
65 #else
66 #error "Undefined Architecture."
67 #endif
68 }
69 #elif defined(__akaros__)
70 auto* ctx = reinterpret_cast<struct user_context*>(vuc);
71 return reinterpret_cast<void*>(get_user_ctx_pc(ctx));
72 #endif
73 static_cast<void>(vuc);
74 return nullptr;
75 }
76
77 // The %p field width for printf() functions is two characters per byte,
78 // and two extra for the leading "0x".
79 static constexpr int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
80
81 // Print a program counter, its stack frame size, and its symbol name.
82 // Note that there is a separate symbolize_pc argument. Return addresses may be
83 // at the end of the function, and this allows the caller to back up from pc if
84 // appropriate.
DumpPCAndFrameSizeAndSymbol(void (* writerfn)(const char *,void *),void * writerfn_arg,void * pc,void * symbolize_pc,int framesize,const char * const prefix)85 static void DumpPCAndFrameSizeAndSymbol(void (*writerfn)(const char*, void*),
86 void* writerfn_arg, void* pc,
87 void* symbolize_pc, int framesize,
88 const char* const prefix) {
89 char tmp[1024];
90 const char* symbol = "(unknown)";
91 if (absl::Symbolize(symbolize_pc, tmp, sizeof(tmp))) {
92 symbol = tmp;
93 }
94 char buf[1024];
95 if (framesize <= 0) {
96 snprintf(buf, sizeof(buf), "%s@ %*p (unknown) %s\n", prefix,
97 kPrintfPointerFieldWidth, pc, symbol);
98 } else {
99 snprintf(buf, sizeof(buf), "%s@ %*p %9d %s\n", prefix,
100 kPrintfPointerFieldWidth, pc, framesize, symbol);
101 }
102 writerfn(buf, writerfn_arg);
103 }
104
105 // Print a program counter and the corresponding stack frame size.
DumpPCAndFrameSize(void (* writerfn)(const char *,void *),void * writerfn_arg,void * pc,int framesize,const char * const prefix)106 static void DumpPCAndFrameSize(void (*writerfn)(const char*, void*),
107 void* writerfn_arg, void* pc, int framesize,
108 const char* const prefix) {
109 char buf[100];
110 if (framesize <= 0) {
111 snprintf(buf, sizeof(buf), "%s@ %*p (unknown)\n", prefix,
112 kPrintfPointerFieldWidth, pc);
113 } else {
114 snprintf(buf, sizeof(buf), "%s@ %*p %9d\n", prefix,
115 kPrintfPointerFieldWidth, pc, framesize);
116 }
117 writerfn(buf, writerfn_arg);
118 }
119
DumpPCAndFrameSizesAndStackTrace(void * pc,void * const stack[],int frame_sizes[],int depth,int min_dropped_frames,bool symbolize_stacktrace,void (* writerfn)(const char *,void *),void * writerfn_arg)120 void DumpPCAndFrameSizesAndStackTrace(
121 void* pc, void* const stack[], int frame_sizes[], int depth,
122 int min_dropped_frames, bool symbolize_stacktrace,
123 void (*writerfn)(const char*, void*), void* writerfn_arg) {
124 if (pc != nullptr) {
125 // We don't know the stack frame size for PC, use 0.
126 if (symbolize_stacktrace) {
127 DumpPCAndFrameSizeAndSymbol(writerfn, writerfn_arg, pc, pc, 0, "PC: ");
128 } else {
129 DumpPCAndFrameSize(writerfn, writerfn_arg, pc, 0, "PC: ");
130 }
131 }
132 for (int i = 0; i < depth; i++) {
133 if (symbolize_stacktrace) {
134 // Pass the previous address of pc as the symbol address because pc is a
135 // return address, and an overrun may occur when the function ends with a
136 // call to a function annotated noreturn (e.g. CHECK). Note that we don't
137 // do this for pc above, as the adjustment is only correct for return
138 // addresses.
139 DumpPCAndFrameSizeAndSymbol(writerfn, writerfn_arg, stack[i],
140 reinterpret_cast<char*>(stack[i]) - 1,
141 frame_sizes[i], " ");
142 } else {
143 DumpPCAndFrameSize(writerfn, writerfn_arg, stack[i], frame_sizes[i],
144 " ");
145 }
146 }
147 if (min_dropped_frames > 0) {
148 char buf[100];
149 snprintf(buf, sizeof(buf), " @ ... and at least %d more frames\n",
150 min_dropped_frames);
151 writerfn(buf, writerfn_arg);
152 }
153 }
154
155 } // namespace debugging_internal
156 ABSL_NAMESPACE_END
157 } // namespace absl
158