blob: eb69d91dadb26d94cd068d3b9e8b17729f8b460e [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
David Sehr891a50e2017-10-27 17:01:07 -070028#include "base/file_utils.h"
Roland Levillain21482ad2017-01-19 20:04:27 +000029#include "base/logging.h"
30#include "base/macros.h"
31#include "base/mutex.h"
32#include "native_stack_dump.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070033#include "runtime.h"
34#include "thread-current-inl.h"
Roland Levillain21482ad2017-01-19 20:04:27 +000035#include "thread_list.h"
36
37namespace art {
38
39using android::base::StringPrintf;
40
41static constexpr bool kUseSigRTTimeout = true;
42static constexpr bool kDumpNativeStackOnTimeout = true;
43
44const char* GetSignalName(int signal_number) {
45 switch (signal_number) {
46 case SIGABRT: return "SIGABRT";
47 case SIGBUS: return "SIGBUS";
48 case SIGFPE: return "SIGFPE";
49 case SIGILL: return "SIGILL";
50 case SIGPIPE: return "SIGPIPE";
51 case SIGSEGV: return "SIGSEGV";
52#if defined(SIGSTKFLT)
53 case SIGSTKFLT: return "SIGSTKFLT";
54#endif
55 case SIGTRAP: return "SIGTRAP";
56 }
57 return "??";
58}
59
60const char* GetSignalCodeName(int signal_number, int signal_code) {
61 // Try the signal-specific codes...
62 switch (signal_number) {
63 case SIGILL:
64 switch (signal_code) {
65 case ILL_ILLOPC: return "ILL_ILLOPC";
66 case ILL_ILLOPN: return "ILL_ILLOPN";
67 case ILL_ILLADR: return "ILL_ILLADR";
68 case ILL_ILLTRP: return "ILL_ILLTRP";
69 case ILL_PRVOPC: return "ILL_PRVOPC";
70 case ILL_PRVREG: return "ILL_PRVREG";
71 case ILL_COPROC: return "ILL_COPROC";
72 case ILL_BADSTK: return "ILL_BADSTK";
73 }
74 break;
75 case SIGBUS:
76 switch (signal_code) {
77 case BUS_ADRALN: return "BUS_ADRALN";
78 case BUS_ADRERR: return "BUS_ADRERR";
79 case BUS_OBJERR: return "BUS_OBJERR";
80 }
81 break;
82 case SIGFPE:
83 switch (signal_code) {
84 case FPE_INTDIV: return "FPE_INTDIV";
85 case FPE_INTOVF: return "FPE_INTOVF";
86 case FPE_FLTDIV: return "FPE_FLTDIV";
87 case FPE_FLTOVF: return "FPE_FLTOVF";
88 case FPE_FLTUND: return "FPE_FLTUND";
89 case FPE_FLTRES: return "FPE_FLTRES";
90 case FPE_FLTINV: return "FPE_FLTINV";
91 case FPE_FLTSUB: return "FPE_FLTSUB";
92 }
93 break;
94 case SIGSEGV:
95 switch (signal_code) {
96 case SEGV_MAPERR: return "SEGV_MAPERR";
97 case SEGV_ACCERR: return "SEGV_ACCERR";
98#if defined(SEGV_BNDERR)
99 case SEGV_BNDERR: return "SEGV_BNDERR";
100#endif
101 }
102 break;
103 case SIGTRAP:
104 switch (signal_code) {
105 case TRAP_BRKPT: return "TRAP_BRKPT";
106 case TRAP_TRACE: return "TRAP_TRACE";
107 }
108 break;
109 }
110 // Then the other codes...
111 switch (signal_code) {
112 case SI_USER: return "SI_USER";
113#if defined(SI_KERNEL)
114 case SI_KERNEL: return "SI_KERNEL";
115#endif
116 case SI_QUEUE: return "SI_QUEUE";
117 case SI_TIMER: return "SI_TIMER";
118 case SI_MESGQ: return "SI_MESGQ";
119 case SI_ASYNCIO: return "SI_ASYNCIO";
120#if defined(SI_SIGIO)
121 case SI_SIGIO: return "SI_SIGIO";
122#endif
123#if defined(SI_TKILL)
124 case SI_TKILL: return "SI_TKILL";
125#endif
126 }
127 // Then give up...
128 return "?";
129}
130
Roland Levillain32a5bb22017-01-31 14:33:16 +0000131struct UContext {
132 explicit UContext(void* raw_context)
133 : context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {}
134
135 void Dump(std::ostream& os) const;
136
137 void DumpRegister32(std::ostream& os, const char* name, uint32_t value) const;
138 void DumpRegister64(std::ostream& os, const char* name, uint64_t value) const;
139
140 void DumpX86Flags(std::ostream& os, uint32_t flags) const;
Roland Levillaind89411a2017-01-31 17:36:07 +0000141 // Print some of the information from the status register (CPSR on ARMv7, PSTATE on ARMv8).
142 template <typename RegisterType>
143 void DumpArmStatusRegister(std::ostream& os, RegisterType status_register) const;
Roland Levillain32a5bb22017-01-31 14:33:16 +0000144
145 mcontext_t& context;
146};
147
Roland Levillain21482ad2017-01-19 20:04:27 +0000148void UContext::Dump(std::ostream& os) const {
Roland Levillain21482ad2017-01-19 20:04:27 +0000149#if defined(__APPLE__) && defined(__i386__)
150 DumpRegister32(os, "eax", context->__ss.__eax);
151 DumpRegister32(os, "ebx", context->__ss.__ebx);
152 DumpRegister32(os, "ecx", context->__ss.__ecx);
153 DumpRegister32(os, "edx", context->__ss.__edx);
154 os << '\n';
155
156 DumpRegister32(os, "edi", context->__ss.__edi);
157 DumpRegister32(os, "esi", context->__ss.__esi);
158 DumpRegister32(os, "ebp", context->__ss.__ebp);
159 DumpRegister32(os, "esp", context->__ss.__esp);
160 os << '\n';
161
162 DumpRegister32(os, "eip", context->__ss.__eip);
163 os << " ";
164 DumpRegister32(os, "eflags", context->__ss.__eflags);
165 DumpX86Flags(os, context->__ss.__eflags);
166 os << '\n';
167
168 DumpRegister32(os, "cs", context->__ss.__cs);
169 DumpRegister32(os, "ds", context->__ss.__ds);
170 DumpRegister32(os, "es", context->__ss.__es);
171 DumpRegister32(os, "fs", context->__ss.__fs);
172 os << '\n';
173 DumpRegister32(os, "gs", context->__ss.__gs);
174 DumpRegister32(os, "ss", context->__ss.__ss);
175#elif defined(__linux__) && defined(__i386__)
176 DumpRegister32(os, "eax", context.gregs[REG_EAX]);
177 DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
178 DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
179 DumpRegister32(os, "edx", context.gregs[REG_EDX]);
180 os << '\n';
181
182 DumpRegister32(os, "edi", context.gregs[REG_EDI]);
183 DumpRegister32(os, "esi", context.gregs[REG_ESI]);
184 DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
185 DumpRegister32(os, "esp", context.gregs[REG_ESP]);
186 os << '\n';
187
188 DumpRegister32(os, "eip", context.gregs[REG_EIP]);
189 os << " ";
190 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
191 DumpX86Flags(os, context.gregs[REG_EFL]);
192 os << '\n';
193
194 DumpRegister32(os, "cs", context.gregs[REG_CS]);
195 DumpRegister32(os, "ds", context.gregs[REG_DS]);
196 DumpRegister32(os, "es", context.gregs[REG_ES]);
197 DumpRegister32(os, "fs", context.gregs[REG_FS]);
198 os << '\n';
199 DumpRegister32(os, "gs", context.gregs[REG_GS]);
200 DumpRegister32(os, "ss", context.gregs[REG_SS]);
201#elif defined(__linux__) && defined(__x86_64__)
202 DumpRegister64(os, "rax", context.gregs[REG_RAX]);
203 DumpRegister64(os, "rbx", context.gregs[REG_RBX]);
204 DumpRegister64(os, "rcx", context.gregs[REG_RCX]);
205 DumpRegister64(os, "rdx", context.gregs[REG_RDX]);
206 os << '\n';
207
208 DumpRegister64(os, "rdi", context.gregs[REG_RDI]);
209 DumpRegister64(os, "rsi", context.gregs[REG_RSI]);
210 DumpRegister64(os, "rbp", context.gregs[REG_RBP]);
211 DumpRegister64(os, "rsp", context.gregs[REG_RSP]);
212 os << '\n';
213
214 DumpRegister64(os, "r8 ", context.gregs[REG_R8]);
215 DumpRegister64(os, "r9 ", context.gregs[REG_R9]);
216 DumpRegister64(os, "r10", context.gregs[REG_R10]);
217 DumpRegister64(os, "r11", context.gregs[REG_R11]);
218 os << '\n';
219
220 DumpRegister64(os, "r12", context.gregs[REG_R12]);
221 DumpRegister64(os, "r13", context.gregs[REG_R13]);
222 DumpRegister64(os, "r14", context.gregs[REG_R14]);
223 DumpRegister64(os, "r15", context.gregs[REG_R15]);
224 os << '\n';
225
226 DumpRegister64(os, "rip", context.gregs[REG_RIP]);
227 os << " ";
228 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
229 DumpX86Flags(os, context.gregs[REG_EFL]);
230 os << '\n';
231
232 DumpRegister32(os, "cs", (context.gregs[REG_CSGSFS]) & 0x0FFFF);
233 DumpRegister32(os, "gs", (context.gregs[REG_CSGSFS] >> 16) & 0x0FFFF);
234 DumpRegister32(os, "fs", (context.gregs[REG_CSGSFS] >> 32) & 0x0FFFF);
235 os << '\n';
Roland Levillaind89411a2017-01-31 17:36:07 +0000236#elif defined(__linux__) && defined(__arm__)
237 DumpRegister32(os, "r0", context.arm_r0);
238 DumpRegister32(os, "r1", context.arm_r1);
239 DumpRegister32(os, "r2", context.arm_r2);
240 DumpRegister32(os, "r3", context.arm_r3);
241 os << '\n';
242
243 DumpRegister32(os, "r4", context.arm_r4);
244 DumpRegister32(os, "r5", context.arm_r5);
245 DumpRegister32(os, "r6", context.arm_r6);
246 DumpRegister32(os, "r7", context.arm_r7);
247 os << '\n';
248
249 DumpRegister32(os, "r8", context.arm_r8);
250 DumpRegister32(os, "r9", context.arm_r9);
251 DumpRegister32(os, "r10", context.arm_r10);
252 DumpRegister32(os, "fp", context.arm_fp);
253 os << '\n';
254
255 DumpRegister32(os, "ip", context.arm_ip);
256 DumpRegister32(os, "sp", context.arm_sp);
257 DumpRegister32(os, "lr", context.arm_lr);
258 DumpRegister32(os, "pc", context.arm_pc);
259 os << '\n';
260
261 DumpRegister32(os, "cpsr", context.arm_cpsr);
262 DumpArmStatusRegister(os, context.arm_cpsr);
263 os << '\n';
264#elif defined(__linux__) && defined(__aarch64__)
265 for (size_t i = 0; i <= 30; ++i) {
266 std::string reg_name = "x" + std::to_string(i);
267 DumpRegister64(os, reg_name.c_str(), context.regs[i]);
268 if (i % 4 == 3) {
269 os << '\n';
270 }
271 }
272 os << '\n';
273
274 DumpRegister64(os, "sp", context.sp);
275 DumpRegister64(os, "pc", context.pc);
276 os << '\n';
277
278 DumpRegister64(os, "pstate", context.pstate);
279 DumpArmStatusRegister(os, context.pstate);
280 os << '\n';
Roland Levillain21482ad2017-01-19 20:04:27 +0000281#else
Roland Levillaind89411a2017-01-31 17:36:07 +0000282 // TODO: Add support for MIPS32 and MIPS64.
Roland Levillain21482ad2017-01-19 20:04:27 +0000283 os << "Unknown architecture/word size/OS in ucontext dump";
284#endif
285}
286
287void UContext::DumpRegister32(std::ostream& os, const char* name, uint32_t value) const {
288 os << StringPrintf(" %6s: 0x%08x", name, value);
289}
290
291void UContext::DumpRegister64(std::ostream& os, const char* name, uint64_t value) const {
292 os << StringPrintf(" %6s: 0x%016" PRIx64, name, value);
293}
294
295void UContext::DumpX86Flags(std::ostream& os, uint32_t flags) const {
296 os << " [";
297 if ((flags & (1 << 0)) != 0) {
298 os << " CF";
299 }
300 if ((flags & (1 << 2)) != 0) {
301 os << " PF";
302 }
303 if ((flags & (1 << 4)) != 0) {
304 os << " AF";
305 }
306 if ((flags & (1 << 6)) != 0) {
307 os << " ZF";
308 }
309 if ((flags & (1 << 7)) != 0) {
310 os << " SF";
311 }
312 if ((flags & (1 << 8)) != 0) {
313 os << " TF";
314 }
315 if ((flags & (1 << 9)) != 0) {
316 os << " IF";
317 }
318 if ((flags & (1 << 10)) != 0) {
319 os << " DF";
320 }
321 if ((flags & (1 << 11)) != 0) {
322 os << " OF";
323 }
324 os << " ]";
325}
326
Roland Levillaind89411a2017-01-31 17:36:07 +0000327template <typename RegisterType>
328void UContext::DumpArmStatusRegister(std::ostream& os, RegisterType status_register) const {
329 // Condition flags.
330 constexpr RegisterType kFlagV = 1U << 28;
331 constexpr RegisterType kFlagC = 1U << 29;
332 constexpr RegisterType kFlagZ = 1U << 30;
333 constexpr RegisterType kFlagN = 1U << 31;
334
335 os << " [";
336 if ((status_register & kFlagN) != 0) {
337 os << " N";
338 }
339 if ((status_register & kFlagZ) != 0) {
340 os << " Z";
341 }
342 if ((status_register & kFlagC) != 0) {
343 os << " C";
344 }
345 if ((status_register & kFlagV) != 0) {
346 os << " V";
347 }
348 os << " ]";
349}
350
Roland Levillain21482ad2017-01-19 20:04:27 +0000351int GetTimeoutSignal() {
352#if defined(__APPLE__)
353 // Mac does not support realtime signals.
354 UNUSED(kUseSigRTTimeout);
355 return -1;
356#else
357 return kUseSigRTTimeout ? (SIGRTMIN + 2) : -1;
358#endif
359}
360
361static bool IsTimeoutSignal(int signal_number) {
362 return signal_number == GetTimeoutSignal();
363}
364
Roland Levillain32a5bb22017-01-31 14:33:16 +0000365#if defined(__APPLE__)
366// On macOS, clang complains about art::HandleUnexpectedSignalCommon's
367// stack frame size being too large; disable that warning locally.
368#pragma GCC diagnostic push
369#pragma GCC diagnostic ignored "-Wframe-larger-than="
370#endif
371
Roland Levillain21482ad2017-01-19 20:04:27 +0000372void HandleUnexpectedSignalCommon(int signal_number,
373 siginfo_t* info,
374 void* raw_context,
Andreas Gampec6fe4272017-06-01 20:14:58 -0700375 bool handle_timeout_signal,
376 bool dump_on_stderr) {
Roland Levillain21482ad2017-01-19 20:04:27 +0000377 static bool handling_unexpected_signal = false;
378 if (handling_unexpected_signal) {
379 LogHelper::LogLineLowStack(__FILE__,
380 __LINE__,
381 ::android::base::FATAL_WITHOUT_ABORT,
382 "HandleUnexpectedSignal reentered\n");
383 if (handle_timeout_signal) {
384 if (IsTimeoutSignal(signal_number)) {
385 // Ignore a recursive timeout.
386 return;
387 }
388 }
389 _exit(1);
390 }
391 handling_unexpected_signal = true;
392
393 gAborting++; // set before taking any locks
394 MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
395
Andreas Gampec6fe4272017-06-01 20:14:58 -0700396 auto logger = [&](auto& stream) {
397 bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
398 signal_number == SIGFPE || signal_number == SIGSEGV);
399 OsInfo os_info;
400 const char* cmd_line = GetCmdLine();
401 if (cmd_line == nullptr) {
402 cmd_line = "<unset>"; // Because no-one called InitLogging.
403 }
404 pid_t tid = GetTid();
405 std::string thread_name(GetThreadName(tid));
406 UContext thread_context(raw_context);
407 Backtrace thread_backtrace(raw_context);
Roland Levillain21482ad2017-01-19 20:04:27 +0000408
Andreas Gampec6fe4272017-06-01 20:14:58 -0700409 stream << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***" << std::endl
410 << StringPrintf("Fatal signal %d (%s), code %d (%s)",
411 signal_number,
412 GetSignalName(signal_number),
413 info->si_code,
414 GetSignalCodeName(signal_number, info->si_code))
415 << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << std::endl
416 << "OS: " << Dumpable<OsInfo>(os_info) << std::endl
417 << "Cmdline: " << cmd_line << std::endl
418 << "Thread: " << tid << " \"" << thread_name << "\"" << std::endl
419 << "Registers:\n" << Dumpable<UContext>(thread_context) << std::endl
Orion Hodson496b8832017-09-11 16:54:57 +0100420 << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace) << std::endl;
421 stream << std::flush;
Andreas Gampec6fe4272017-06-01 20:14:58 -0700422 };
Roland Levillain21482ad2017-01-19 20:04:27 +0000423
Roland Levillain21482ad2017-01-19 20:04:27 +0000424 if (dump_on_stderr) {
425 // Note: We are using cerr directly instead of LOG macros to ensure even just partial output
426 // makes it out. That means we lose the "dalvikvm..." prefix, but that is acceptable
427 // considering this is an abort situation.
Andreas Gampec6fe4272017-06-01 20:14:58 -0700428 logger(std::cerr);
Roland Levillain21482ad2017-01-19 20:04:27 +0000429 } else {
Andreas Gampec6fe4272017-06-01 20:14:58 -0700430 logger(LOG_STREAM(FATAL_WITHOUT_ABORT));
Roland Levillain21482ad2017-01-19 20:04:27 +0000431 }
432 if (kIsDebugBuild && signal_number == SIGSEGV) {
433 PrintFileToLog("/proc/self/maps", LogSeverity::FATAL_WITHOUT_ABORT);
434 }
435
436 Runtime* runtime = Runtime::Current();
437 if (runtime != nullptr) {
438 if (handle_timeout_signal && IsTimeoutSignal(signal_number)) {
439 // Special timeout signal. Try to dump all threads.
440 // Note: Do not use DumpForSigQuit, as that might disable native unwind, but the native parts
441 // are of value here.
442 runtime->GetThreadList()->Dump(std::cerr, kDumpNativeStackOnTimeout);
443 std::cerr << std::endl;
444 }
445
446 if (dump_on_stderr) {
447 std::cerr << "Fault message: " << runtime->GetFaultMessage() << std::endl;
448 } else {
449 LOG(FATAL_WITHOUT_ABORT) << "Fault message: " << runtime->GetFaultMessage();
450 }
451 }
452}
453
Roland Levillain32a5bb22017-01-31 14:33:16 +0000454#if defined(__APPLE__)
455#pragma GCC diagnostic pop
456#endif
457
Roland Levillain21482ad2017-01-19 20:04:27 +0000458void InitPlatformSignalHandlersCommon(void (*newact)(int, siginfo_t*, void*),
459 struct sigaction* oldact,
460 bool handle_timeout_signal) {
461 struct sigaction action;
462 memset(&action, 0, sizeof(action));
463 sigemptyset(&action.sa_mask);
464 action.sa_sigaction = newact;
465 // Use the three-argument sa_sigaction handler.
466 action.sa_flags |= SA_SIGINFO;
467 // Use the alternate signal stack so we can catch stack overflows.
468 action.sa_flags |= SA_ONSTACK;
469
470 int rc = 0;
471 rc += sigaction(SIGABRT, &action, oldact);
472 rc += sigaction(SIGBUS, &action, oldact);
473 rc += sigaction(SIGFPE, &action, oldact);
474 rc += sigaction(SIGILL, &action, oldact);
475 rc += sigaction(SIGPIPE, &action, oldact);
476 rc += sigaction(SIGSEGV, &action, oldact);
477#if defined(SIGSTKFLT)
478 rc += sigaction(SIGSTKFLT, &action, oldact);
479#endif
480 rc += sigaction(SIGTRAP, &action, oldact);
481 // Special dump-all timeout.
482 if (handle_timeout_signal && GetTimeoutSignal() != -1) {
483 rc += sigaction(GetTimeoutSignal(), &action, oldact);
484 }
485 CHECK_EQ(rc, 0);
486}
487
488} // namespace art