Debian Codegraph @

tools/CrashHandler.cpp

raw
1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "tools/CrashHandler.h"
9
10 #include "include/private/base/SkDebug.h"
11 #include "src/base/SkLeanWindows.h"
12
13 #include <array> // for std::size
14 #include <stdlib.h>
15
16 #if defined(SK_BUILD_FOR_MAC)
17 // We only use local unwinding, so we can define this to select a faster implementation.
18 #define UNW_LOCAL_ONLY
19 #include <libunwind.h>
20 #include <cxxabi.h>
21
22 static void handler(int sig) {
23 unw_context_t context;
24 unw_getcontext(&context);
25
26 unw_cursor_t cursor;
27 unw_init_local(&cursor, &context);
28
29 SkDebugf("\nSignal %d:\n", sig);
30 while (unw_step(&cursor) > 0) {
31 static const size_t kMax = 256;
32 char mangled[kMax], demangled[kMax];
33 unw_word_t offset;
34 unw_get_proc_name(&cursor, mangled, kMax, &offset);
35
36 int ok;
37 size_t len = kMax;
38 abi::__cxa_demangle(mangled, demangled, &len, &ok);
39
40 SkDebugf("%s (+0x%zx)\n", ok == 0 ? demangled : mangled, (size_t)offset);
41 }
42 SkDebugf("\n");
43
44 // Exit NOW. Don't notify other threads, don't call anything registered with atexit().
45 _Exit(sig);
46 }
47
48 #elif defined(SK_BUILD_FOR_UNIX)
49 // We'd use libunwind here too, but it's a pain to get installed for
50 // both 32 and 64 bit on bots. Doesn't matter much: catchsegv is best anyway.
51 #include <cxxabi.h>
52 #include <dlfcn.h>
53 #include <string.h>
54 #if defined(__Fuchsia__)
55 #include <stdint.h>
56
57 // syslog crash reporting from Fuchsia's backtrace_request.h
58 //
59 // Special value we put in the first register to let the exception handler know
60 // that we are just requesting a backtrace and we should resume the thread.
61 #define BACKTRACE_REQUEST_MAGIC ((uint64_t)0xee726573756d65ee)
62
63 // Prints a backtrace, resuming the thread without killing the process.
64 __attribute__((always_inline)) static inline void backtrace_request(void) {
65 // Two instructions: one that sets a software breakpoint ("int3" on x64,
66 // "brk" on arm64) and one that writes the "magic" value in the first
67 // register ("a" on x64, "x0" on arm64).
68 //
69 // We set a software breakpoint to trigger the exception handling in
70 // crashsvc, which will print the debug info, including the backtrace.
71 //
72 // We write the "magic" value in the first register so that the exception
73 // handler can check for it and resume the thread if present.
74 #ifdef __x86_64__
75 __asm__("int3" : : "a"(BACKTRACE_REQUEST_MAGIC));
76 #endif
77 #ifdef __aarch64__
78 // This is what gdb uses.
79 __asm__(
80 "mov x0, %0\n"
81 "\tbrk 0"
82 :
83 : "r"(BACKTRACE_REQUEST_MAGIC)
84 : "x0");
85 #endif
86 }
87 #else
88 #include <execinfo.h>
89 #endif
90
91 static void handler(int sig) {
92 #if defined(__Fuchsia__)
93 backtrace_request();
94 #else
95 void* stack[64];
96 const int count = backtrace(stack, std::size(stack));
97 char** symbols = backtrace_symbols(stack, count);
98
99 SkDebugf("\nSignal %d [%s]:\n", sig, strsignal(sig));
100 for (int i = 0; i < count; i++) {
101 Dl_info info;
102 if (dladdr(stack[i], &info) && info.dli_sname) {
103 char demangled[256];
104 size_t len = std::size(demangled);
105 int ok;
106
107 abi::__cxa_demangle(info.dli_sname, demangled, &len, &ok);
108 if (ok == 0) {
109 SkDebugf(" %s\n", demangled);
110 continue;
111 }
112 }
113 SkDebugf(" %s\n", symbols[i]);
114 }
115 #endif
116 // Exit NOW. Don't notify other threads, don't call anything registered with
117 // atexit().
118 _Exit(sig);
119 }
120 #endif
121
122 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX)
123 #include <signal.h>
124
125 void SetupCrashHandler() {
126 static const int kSignals[] = {
127 SIGABRT,
128 SIGBUS,
129 SIGFPE,
130 SIGILL,
131 SIGSEGV,
132 SIGTRAP,
133 };
134
135 for (size_t i = 0; i < sizeof(kSignals) / sizeof(kSignals[0]); i++) {
136 // Register our signal handler unless something's already done so (e.g. catchsegv).
137 void (*prev)(int) = signal(kSignals[i], handler);
138 if (prev != SIG_DFL) {
139 signal(kSignals[i], prev);
140 }
141 }
142 }
143
144 #elif defined(SK_BUILD_FOR_WIN)
145
146 #include <DbgHelp.h>
147 #include <stdint.h>
148 #include "include/private/base/SkMalloc.h"
149
150 static const struct {
151 const char* name;
152 const DWORD code;
153 } kExceptions[] = {
154 #define _(E) {#E, E}
155 _(EXCEPTION_ACCESS_VIOLATION),
156 _(EXCEPTION_BREAKPOINT),
157 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
158 _(EXCEPTION_STACK_OVERFLOW),
159 // TODO: more?
160 #undef _
161 };
162
163 static LONG WINAPI handler(EXCEPTION_POINTERS* e) {
164 const DWORD code = e->ExceptionRecord->ExceptionCode;
165 SkDebugf("\nCaught exception %lu", code);
166 for (size_t i = 0; i < std::size(kExceptions); i++) {
167 if (kExceptions[i].code == code) {
168 SkDebugf(" %s", kExceptions[i].name);
169 }
170 }
171 SkDebugf("\n");
172
173 // We need to run SymInitialize before doing any of the stack walking below.
174 HANDLE hProcess = GetCurrentProcess();
175 SymInitialize(hProcess, 0, true);
176
177 STACKFRAME64 frame;
178 sk_bzero(&frame, sizeof(frame));
179 // Start frame off from the frame that triggered the exception.
180 CONTEXT* c = e->ContextRecord;
181 frame.AddrPC.Mode = AddrModeFlat;
182 frame.AddrStack.Mode = AddrModeFlat;
183 frame.AddrFrame.Mode = AddrModeFlat;
184 #if defined(_X86_)
185 frame.AddrPC.Offset = c->Eip;
186 frame.AddrStack.Offset = c->Esp;
187 frame.AddrFrame.Offset = c->Ebp;
188 const DWORD machineType = IMAGE_FILE_MACHINE_I386;
189 #elif defined(_AMD64_)
190 frame.AddrPC.Offset = c->Rip;
191 frame.AddrStack.Offset = c->Rsp;
192 frame.AddrFrame.Offset = c->Rbp;
193 const DWORD machineType = IMAGE_FILE_MACHINE_AMD64;
194 #elif defined(_M_ARM64)
195 frame.AddrPC.Offset = c->Pc;
196 frame.AddrStack.Offset = c->Sp;
197 frame.AddrFrame.Offset = c->Fp;
198 const DWORD machineType = IMAGE_FILE_MACHINE_ARM64;
199 #endif
200
201 #if !defined(SK_WINUWP)
202 while (StackWalk64(machineType,
203 GetCurrentProcess(),
204 GetCurrentThread(),
205 &frame,
206 c,
207 nullptr,
208 SymFunctionTableAccess64,
209 SymGetModuleBase64,
210 nullptr)) {
211 // Buffer to store symbol name in.
212 static const int kMaxNameLength = 1024;
213 uint8_t buffer[sizeof(IMAGEHLP_SYMBOL64) + kMaxNameLength];
214 sk_bzero(buffer, sizeof(buffer));
215
216 // We have to place IMAGEHLP_SYMBOL64 at the front, and fill in
217 // how much space it can use.
218 IMAGEHLP_SYMBOL64* symbol = reinterpret_cast<IMAGEHLP_SYMBOL64*>(&buffer);
219 symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
220 symbol->MaxNameLength = kMaxNameLength - 1;
221
222 // Translate the current PC into a symbol and byte offset from the symbol.
223 DWORD64 offset;
224 SymGetSymFromAddr64(hProcess, frame.AddrPC.Offset, &offset, symbol);
225
226 SkDebugf("%s +%llx\n", symbol->Name, offset);
227 }
228 #endif //SK_WINUWP
229
230 // Exit NOW. Don't notify other threads, don't call anything registered with atexit().
231 _exit(1);
232
233 // The compiler wants us to return something. This is what we'd do
234 // if we didn't _exit().
235 return EXCEPTION_EXECUTE_HANDLER;
236 }
237
238 void SetupCrashHandler() {
239 SetUnhandledExceptionFilter(handler);
240 }
241
242 #else
243
244 void SetupCrashHandler() { }
245
246 #endif