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