blob: 1d8c16d22b321a9b00f7c84c9f4a964c295c7c3e [file] [log] [blame]
Roland Levillain21482ad2017-01-19 20:04:27 +00001/*
2 * Copyright (C) 2017 The Android Open Source Project
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 "runtime_common.h"
18
19#include <signal.h>
20
21#include <cinttypes>
22#include <iostream>
23#include <sstream>
24#include <string>
25
26#include "android-base/stringprintf.h"
27
28#include "base/logging.h"
29#include "base/macros.h"
30#include "base/mutex.h"
31#include "native_stack_dump.h"
32#include "thread-inl.h"
33#include "thread_list.h"
34
35namespace art {
36
37using android::base::StringPrintf;
38
39static constexpr bool kUseSigRTTimeout = true;
40static constexpr bool kDumpNativeStackOnTimeout = true;
41
42const char* GetSignalName(int signal_number) {
43 switch (signal_number) {
44 case SIGABRT: return "SIGABRT";
45 case SIGBUS: return "SIGBUS";
46 case SIGFPE: return "SIGFPE";
47 case SIGILL: return "SIGILL";
48 case SIGPIPE: return "SIGPIPE";
49 case SIGSEGV: return "SIGSEGV";
50#if defined(SIGSTKFLT)
51 case SIGSTKFLT: return "SIGSTKFLT";
52#endif
53 case SIGTRAP: return "SIGTRAP";
54 }
55 return "??";
56}
57
58const char* GetSignalCodeName(int signal_number, int signal_code) {
59 // Try the signal-specific codes...
60 switch (signal_number) {
61 case SIGILL:
62 switch (signal_code) {
63 case ILL_ILLOPC: return "ILL_ILLOPC";
64 case ILL_ILLOPN: return "ILL_ILLOPN";
65 case ILL_ILLADR: return "ILL_ILLADR";
66 case ILL_ILLTRP: return "ILL_ILLTRP";
67 case ILL_PRVOPC: return "ILL_PRVOPC";
68 case ILL_PRVREG: return "ILL_PRVREG";
69 case ILL_COPROC: return "ILL_COPROC";
70 case ILL_BADSTK: return "ILL_BADSTK";
71 }
72 break;
73 case SIGBUS:
74 switch (signal_code) {
75 case BUS_ADRALN: return "BUS_ADRALN";
76 case BUS_ADRERR: return "BUS_ADRERR";
77 case BUS_OBJERR: return "BUS_OBJERR";
78 }
79 break;
80 case SIGFPE:
81 switch (signal_code) {
82 case FPE_INTDIV: return "FPE_INTDIV";
83 case FPE_INTOVF: return "FPE_INTOVF";
84 case FPE_FLTDIV: return "FPE_FLTDIV";
85 case FPE_FLTOVF: return "FPE_FLTOVF";
86 case FPE_FLTUND: return "FPE_FLTUND";
87 case FPE_FLTRES: return "FPE_FLTRES";
88 case FPE_FLTINV: return "FPE_FLTINV";
89 case FPE_FLTSUB: return "FPE_FLTSUB";
90 }
91 break;
92 case SIGSEGV:
93 switch (signal_code) {
94 case SEGV_MAPERR: return "SEGV_MAPERR";
95 case SEGV_ACCERR: return "SEGV_ACCERR";
96#if defined(SEGV_BNDERR)
97 case SEGV_BNDERR: return "SEGV_BNDERR";
98#endif
99 }
100 break;
101 case SIGTRAP:
102 switch (signal_code) {
103 case TRAP_BRKPT: return "TRAP_BRKPT";
104 case TRAP_TRACE: return "TRAP_TRACE";
105 }
106 break;
107 }
108 // Then the other codes...
109 switch (signal_code) {
110 case SI_USER: return "SI_USER";
111#if defined(SI_KERNEL)
112 case SI_KERNEL: return "SI_KERNEL";
113#endif
114 case SI_QUEUE: return "SI_QUEUE";
115 case SI_TIMER: return "SI_TIMER";
116 case SI_MESGQ: return "SI_MESGQ";
117 case SI_ASYNCIO: return "SI_ASYNCIO";
118#if defined(SI_SIGIO)
119 case SI_SIGIO: return "SI_SIGIO";
120#endif
121#if defined(SI_TKILL)
122 case SI_TKILL: return "SI_TKILL";
123#endif
124 }
125 // Then give up...
126 return "?";
127}
128
129void UContext::Dump(std::ostream& os) const {
130 // TODO: support non-x86 hosts.
131#if defined(__APPLE__) && defined(__i386__)
132 DumpRegister32(os, "eax", context->__ss.__eax);
133 DumpRegister32(os, "ebx", context->__ss.__ebx);
134 DumpRegister32(os, "ecx", context->__ss.__ecx);
135 DumpRegister32(os, "edx", context->__ss.__edx);
136 os << '\n';
137
138 DumpRegister32(os, "edi", context->__ss.__edi);
139 DumpRegister32(os, "esi", context->__ss.__esi);
140 DumpRegister32(os, "ebp", context->__ss.__ebp);
141 DumpRegister32(os, "esp", context->__ss.__esp);
142 os << '\n';
143
144 DumpRegister32(os, "eip", context->__ss.__eip);
145 os << " ";
146 DumpRegister32(os, "eflags", context->__ss.__eflags);
147 DumpX86Flags(os, context->__ss.__eflags);
148 os << '\n';
149
150 DumpRegister32(os, "cs", context->__ss.__cs);
151 DumpRegister32(os, "ds", context->__ss.__ds);
152 DumpRegister32(os, "es", context->__ss.__es);
153 DumpRegister32(os, "fs", context->__ss.__fs);
154 os << '\n';
155 DumpRegister32(os, "gs", context->__ss.__gs);
156 DumpRegister32(os, "ss", context->__ss.__ss);
157#elif defined(__linux__) && defined(__i386__)
158 DumpRegister32(os, "eax", context.gregs[REG_EAX]);
159 DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
160 DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
161 DumpRegister32(os, "edx", context.gregs[REG_EDX]);
162 os << '\n';
163
164 DumpRegister32(os, "edi", context.gregs[REG_EDI]);
165 DumpRegister32(os, "esi", context.gregs[REG_ESI]);
166 DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
167 DumpRegister32(os, "esp", context.gregs[REG_ESP]);
168 os << '\n';
169
170 DumpRegister32(os, "eip", context.gregs[REG_EIP]);
171 os << " ";
172 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
173 DumpX86Flags(os, context.gregs[REG_EFL]);
174 os << '\n';
175
176 DumpRegister32(os, "cs", context.gregs[REG_CS]);
177 DumpRegister32(os, "ds", context.gregs[REG_DS]);
178 DumpRegister32(os, "es", context.gregs[REG_ES]);
179 DumpRegister32(os, "fs", context.gregs[REG_FS]);
180 os << '\n';
181 DumpRegister32(os, "gs", context.gregs[REG_GS]);
182 DumpRegister32(os, "ss", context.gregs[REG_SS]);
183#elif defined(__linux__) && defined(__x86_64__)
184 DumpRegister64(os, "rax", context.gregs[REG_RAX]);
185 DumpRegister64(os, "rbx", context.gregs[REG_RBX]);
186 DumpRegister64(os, "rcx", context.gregs[REG_RCX]);
187 DumpRegister64(os, "rdx", context.gregs[REG_RDX]);
188 os << '\n';
189
190 DumpRegister64(os, "rdi", context.gregs[REG_RDI]);
191 DumpRegister64(os, "rsi", context.gregs[REG_RSI]);
192 DumpRegister64(os, "rbp", context.gregs[REG_RBP]);
193 DumpRegister64(os, "rsp", context.gregs[REG_RSP]);
194 os << '\n';
195
196 DumpRegister64(os, "r8 ", context.gregs[REG_R8]);
197 DumpRegister64(os, "r9 ", context.gregs[REG_R9]);
198 DumpRegister64(os, "r10", context.gregs[REG_R10]);
199 DumpRegister64(os, "r11", context.gregs[REG_R11]);
200 os << '\n';
201
202 DumpRegister64(os, "r12", context.gregs[REG_R12]);
203 DumpRegister64(os, "r13", context.gregs[REG_R13]);
204 DumpRegister64(os, "r14", context.gregs[REG_R14]);
205 DumpRegister64(os, "r15", context.gregs[REG_R15]);
206 os << '\n';
207
208 DumpRegister64(os, "rip", context.gregs[REG_RIP]);
209 os << " ";
210 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
211 DumpX86Flags(os, context.gregs[REG_EFL]);
212 os << '\n';
213
214 DumpRegister32(os, "cs", (context.gregs[REG_CSGSFS]) & 0x0FFFF);
215 DumpRegister32(os, "gs", (context.gregs[REG_CSGSFS] >> 16) & 0x0FFFF);
216 DumpRegister32(os, "fs", (context.gregs[REG_CSGSFS] >> 32) & 0x0FFFF);
217 os << '\n';
218#else
219 os << "Unknown architecture/word size/OS in ucontext dump";
220#endif
221}
222
223void UContext::DumpRegister32(std::ostream& os, const char* name, uint32_t value) const {
224 os << StringPrintf(" %6s: 0x%08x", name, value);
225}
226
227void UContext::DumpRegister64(std::ostream& os, const char* name, uint64_t value) const {
228 os << StringPrintf(" %6s: 0x%016" PRIx64, name, value);
229}
230
231void UContext::DumpX86Flags(std::ostream& os, uint32_t flags) const {
232 os << " [";
233 if ((flags & (1 << 0)) != 0) {
234 os << " CF";
235 }
236 if ((flags & (1 << 2)) != 0) {
237 os << " PF";
238 }
239 if ((flags & (1 << 4)) != 0) {
240 os << " AF";
241 }
242 if ((flags & (1 << 6)) != 0) {
243 os << " ZF";
244 }
245 if ((flags & (1 << 7)) != 0) {
246 os << " SF";
247 }
248 if ((flags & (1 << 8)) != 0) {
249 os << " TF";
250 }
251 if ((flags & (1 << 9)) != 0) {
252 os << " IF";
253 }
254 if ((flags & (1 << 10)) != 0) {
255 os << " DF";
256 }
257 if ((flags & (1 << 11)) != 0) {
258 os << " OF";
259 }
260 os << " ]";
261}
262
263int GetTimeoutSignal() {
264#if defined(__APPLE__)
265 // Mac does not support realtime signals.
266 UNUSED(kUseSigRTTimeout);
267 return -1;
268#else
269 return kUseSigRTTimeout ? (SIGRTMIN + 2) : -1;
270#endif
271}
272
273static bool IsTimeoutSignal(int signal_number) {
274 return signal_number == GetTimeoutSignal();
275}
276
277void HandleUnexpectedSignalCommon(int signal_number,
278 siginfo_t* info,
279 void* raw_context,
280 bool running_on_linux) {
281 bool handle_timeout_signal = running_on_linux;
282 bool dump_on_stderr = running_on_linux;
283
284 static bool handling_unexpected_signal = false;
285 if (handling_unexpected_signal) {
286 LogHelper::LogLineLowStack(__FILE__,
287 __LINE__,
288 ::android::base::FATAL_WITHOUT_ABORT,
289 "HandleUnexpectedSignal reentered\n");
290 if (handle_timeout_signal) {
291 if (IsTimeoutSignal(signal_number)) {
292 // Ignore a recursive timeout.
293 return;
294 }
295 }
296 _exit(1);
297 }
298 handling_unexpected_signal = true;
299
300 gAborting++; // set before taking any locks
301 MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
302
303 bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
304 signal_number == SIGFPE || signal_number == SIGSEGV);
305
306 OsInfo os_info;
307 const char* cmd_line = GetCmdLine();
308 if (cmd_line == nullptr) {
309 cmd_line = "<unset>"; // Because no-one called InitLogging.
310 }
311 pid_t tid = GetTid();
312 std::string thread_name(GetThreadName(tid));
313 UContext thread_context(raw_context);
314 Backtrace thread_backtrace(raw_context);
315
316 std::ostringstream stream;
317 stream << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"
318 << StringPrintf("Fatal signal %d (%s), code %d (%s)",
319 signal_number,
320 GetSignalName(signal_number),
321 info->si_code,
322 GetSignalCodeName(signal_number, info->si_code))
323 << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << '\n'
324 << "OS: " << Dumpable<OsInfo>(os_info) << '\n'
325 << "Cmdline: " << cmd_line << '\n'
326 << "Thread: " << tid << " \"" << thread_name << "\"" << '\n'
327 << "Registers:\n" << Dumpable<UContext>(thread_context) << '\n'
328 << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace) << '\n';
329 if (dump_on_stderr) {
330 // Note: We are using cerr directly instead of LOG macros to ensure even just partial output
331 // makes it out. That means we lose the "dalvikvm..." prefix, but that is acceptable
332 // considering this is an abort situation.
333 std::cerr << stream.str() << std::flush;
334 } else {
335 LOG(FATAL_WITHOUT_ABORT) << stream.str() << std::flush;
336 }
337 if (kIsDebugBuild && signal_number == SIGSEGV) {
338 PrintFileToLog("/proc/self/maps", LogSeverity::FATAL_WITHOUT_ABORT);
339 }
340
341 Runtime* runtime = Runtime::Current();
342 if (runtime != nullptr) {
343 if (handle_timeout_signal && IsTimeoutSignal(signal_number)) {
344 // Special timeout signal. Try to dump all threads.
345 // Note: Do not use DumpForSigQuit, as that might disable native unwind, but the native parts
346 // are of value here.
347 runtime->GetThreadList()->Dump(std::cerr, kDumpNativeStackOnTimeout);
348 std::cerr << std::endl;
349 }
350
351 if (dump_on_stderr) {
352 std::cerr << "Fault message: " << runtime->GetFaultMessage() << std::endl;
353 } else {
354 LOG(FATAL_WITHOUT_ABORT) << "Fault message: " << runtime->GetFaultMessage();
355 }
356 }
357}
358
359void InitPlatformSignalHandlersCommon(void (*newact)(int, siginfo_t*, void*),
360 struct sigaction* oldact,
361 bool handle_timeout_signal) {
362 struct sigaction action;
363 memset(&action, 0, sizeof(action));
364 sigemptyset(&action.sa_mask);
365 action.sa_sigaction = newact;
366 // Use the three-argument sa_sigaction handler.
367 action.sa_flags |= SA_SIGINFO;
368 // Use the alternate signal stack so we can catch stack overflows.
369 action.sa_flags |= SA_ONSTACK;
370
371 int rc = 0;
372 rc += sigaction(SIGABRT, &action, oldact);
373 rc += sigaction(SIGBUS, &action, oldact);
374 rc += sigaction(SIGFPE, &action, oldact);
375 rc += sigaction(SIGILL, &action, oldact);
376 rc += sigaction(SIGPIPE, &action, oldact);
377 rc += sigaction(SIGSEGV, &action, oldact);
378#if defined(SIGSTKFLT)
379 rc += sigaction(SIGSTKFLT, &action, oldact);
380#endif
381 rc += sigaction(SIGTRAP, &action, oldact);
382 // Special dump-all timeout.
383 if (handle_timeout_signal && GetTimeoutSignal() != -1) {
384 rc += sigaction(GetTimeoutSignal(), &action, oldact);
385 }
386 CHECK_EQ(rc, 0);
387}
388
389} // namespace art