• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(__arm__)
50     return reinterpret_cast<void*>(context->uc_mcontext.arm_pc);
51 #elif defined(__i386__)
52     if (14 < ABSL_ARRAYSIZE(context->uc_mcontext.gregs))
53       return reinterpret_cast<void*>(context->uc_mcontext.gregs[14]);
54 #elif defined(__mips__)
55     return reinterpret_cast<void*>(context->uc_mcontext.pc);
56 #elif defined(__powerpc64__)
57     return reinterpret_cast<void*>(context->uc_mcontext.gp_regs[32]);
58 #elif defined(__powerpc__)
59     return reinterpret_cast<void*>(context->uc_mcontext.regs->nip);
60 #elif defined(__riscv)
61     return reinterpret_cast<void*>(context->uc_mcontext.__gregs[REG_PC]);
62 #elif defined(__s390__) && !defined(__s390x__)
63     return reinterpret_cast<void*>(context->uc_mcontext.psw.addr & 0x7fffffff);
64 #elif defined(__s390__) && defined(__s390x__)
65     return reinterpret_cast<void*>(context->uc_mcontext.psw.addr);
66 #elif defined(__x86_64__)
67     if (16 < ABSL_ARRAYSIZE(context->uc_mcontext.gregs))
68       return reinterpret_cast<void*>(context->uc_mcontext.gregs[16]);
69 #else
70 #error "Undefined Architecture."
71 #endif
72   }
73 #elif defined(__APPLE__)
74   if (vuc != nullptr) {
75     ucontext_t* signal_ucontext = reinterpret_cast<ucontext_t*>(vuc);
76 #if defined(__aarch64__)
77     return reinterpret_cast<void*>(
78         __darwin_arm_thread_state64_get_pc(signal_ucontext->uc_mcontext->__ss));
79 #elif defined(__arm__)
80 #if __DARWIN_UNIX03
81     return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->__ss.__pc);
82 #else
83     return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->ss.pc);
84 #endif
85 #elif defined(__i386__)
86 #if __DARWIN_UNIX03
87     return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->__ss.__eip);
88 #else
89     return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->ss.eip);
90 #endif
91 #elif defined(__x86_64__)
92 #if __DARWIN_UNIX03
93     return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->__ss.__rip);
94 #else
95     return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->ss.rip);
96 #endif
97 #endif
98   }
99 #elif defined(__akaros__)
100   auto* ctx = reinterpret_cast<struct user_context*>(vuc);
101   return reinterpret_cast<void*>(get_user_ctx_pc(ctx));
102 #endif
103   static_cast<void>(vuc);
104   return nullptr;
105 }
106 
107 // The %p field width for printf() functions is two characters per byte,
108 // and two extra for the leading "0x".
109 static constexpr int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
110 
111 // Print a program counter, its stack frame size, and its symbol name.
112 // Note that there is a separate symbolize_pc argument. Return addresses may be
113 // at the end of the function, and this allows the caller to back up from pc if
114 // appropriate.
DumpPCAndFrameSizeAndSymbol(void (* writerfn)(const char *,void *),void * writerfn_arg,void * pc,void * symbolize_pc,int framesize,const char * const prefix)115 static void DumpPCAndFrameSizeAndSymbol(void (*writerfn)(const char*, void*),
116                                         void* writerfn_arg, void* pc,
117                                         void* symbolize_pc, int framesize,
118                                         const char* const prefix) {
119   char tmp[1024];
120   const char* symbol = "(unknown)";
121   if (absl::Symbolize(symbolize_pc, tmp, sizeof(tmp))) {
122     symbol = tmp;
123   }
124   char buf[1024];
125   if (framesize <= 0) {
126     snprintf(buf, sizeof(buf), "%s@ %*p  (unknown)  %s\n", prefix,
127              kPrintfPointerFieldWidth, pc, symbol);
128   } else {
129     snprintf(buf, sizeof(buf), "%s@ %*p  %9d  %s\n", prefix,
130              kPrintfPointerFieldWidth, pc, framesize, symbol);
131   }
132   writerfn(buf, writerfn_arg);
133 }
134 
135 // 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)136 static void DumpPCAndFrameSize(void (*writerfn)(const char*, void*),
137                                void* writerfn_arg, void* pc, int framesize,
138                                const char* const prefix) {
139   char buf[100];
140   if (framesize <= 0) {
141     snprintf(buf, sizeof(buf), "%s@ %*p  (unknown)\n", prefix,
142              kPrintfPointerFieldWidth, pc);
143   } else {
144     snprintf(buf, sizeof(buf), "%s@ %*p  %9d\n", prefix,
145              kPrintfPointerFieldWidth, pc, framesize);
146   }
147   writerfn(buf, writerfn_arg);
148 }
149 
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)150 void DumpPCAndFrameSizesAndStackTrace(
151     void* pc, void* const stack[], int frame_sizes[], int depth,
152     int min_dropped_frames, bool symbolize_stacktrace,
153     void (*writerfn)(const char*, void*), void* writerfn_arg) {
154   if (pc != nullptr) {
155     // We don't know the stack frame size for PC, use 0.
156     if (symbolize_stacktrace) {
157       DumpPCAndFrameSizeAndSymbol(writerfn, writerfn_arg, pc, pc, 0, "PC: ");
158     } else {
159       DumpPCAndFrameSize(writerfn, writerfn_arg, pc, 0, "PC: ");
160     }
161   }
162   for (int i = 0; i < depth; i++) {
163     if (symbolize_stacktrace) {
164       // Pass the previous address of pc as the symbol address because pc is a
165       // return address, and an overrun may occur when the function ends with a
166       // call to a function annotated noreturn (e.g. CHECK). Note that we don't
167       // do this for pc above, as the adjustment is only correct for return
168       // addresses.
169       DumpPCAndFrameSizeAndSymbol(writerfn, writerfn_arg, stack[i],
170                                   reinterpret_cast<char*>(stack[i]) - 1,
171                                   frame_sizes[i], "    ");
172     } else {
173       DumpPCAndFrameSize(writerfn, writerfn_arg, stack[i], frame_sizes[i],
174                          "    ");
175     }
176   }
177   if (min_dropped_frames > 0) {
178     char buf[100];
179     snprintf(buf, sizeof(buf), "    @ ... and at least %d more frames\n",
180              min_dropped_frames);
181     writerfn(buf, writerfn_arg);
182   }
183 }
184 
185 }  // namespace debugging_internal
186 ABSL_NAMESPACE_END
187 }  // namespace absl
188