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