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