blob: 010c6e75717b743ad9afc9664aa4119e41691464 [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
Andreas Gampe39b378c2017-12-07 15:44:13 -080029#include "base/aborting.h"
David Sehr891a50e2017-10-27 17:01:07 -070030#include "base/file_utils.h"
Andreas Gampe39b378c2017-12-07 15:44:13 -080031#include "base/logging.h" // For LogHelper, GetCmdLine.
Roland Levillain21482ad2017-01-19 20:04:27 +000032#include "base/macros.h"
33#include "base/mutex.h"
34#include "native_stack_dump.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070035#include "runtime.h"
36#include "thread-current-inl.h"
Roland Levillain21482ad2017-01-19 20:04:27 +000037#include "thread_list.h"
38
39namespace art {
40
41using android::base::StringPrintf;
42
43static constexpr bool kUseSigRTTimeout = true;
Roland Levillain21482ad2017-01-19 20:04:27 +000044
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
Andreas Gampecdd53142018-03-28 21:17:43 -0700373static void HandleUnexpectedSignalCommonDump(int signal_number,
374 siginfo_t* info,
375 void* raw_context,
376 bool handle_timeout_signal,
377 bool dump_on_stderr) {
Andreas Gampec6fe4272017-06-01 20:14:58 -0700378 auto logger = [&](auto& stream) {
379 bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
380 signal_number == SIGFPE || signal_number == SIGSEGV);
381 OsInfo os_info;
382 const char* cmd_line = GetCmdLine();
383 if (cmd_line == nullptr) {
384 cmd_line = "<unset>"; // Because no-one called InitLogging.
385 }
386 pid_t tid = GetTid();
387 std::string thread_name(GetThreadName(tid));
388 UContext thread_context(raw_context);
389 Backtrace thread_backtrace(raw_context);
Roland Levillain21482ad2017-01-19 20:04:27 +0000390
Andreas Gampec6fe4272017-06-01 20:14:58 -0700391 stream << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***" << std::endl
392 << StringPrintf("Fatal signal %d (%s), code %d (%s)",
393 signal_number,
394 GetSignalName(signal_number),
395 info->si_code,
396 GetSignalCodeName(signal_number, info->si_code))
397 << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << std::endl
398 << "OS: " << Dumpable<OsInfo>(os_info) << std::endl
399 << "Cmdline: " << cmd_line << std::endl
400 << "Thread: " << tid << " \"" << thread_name << "\"" << std::endl
401 << "Registers:\n" << Dumpable<UContext>(thread_context) << std::endl
Orion Hodson496b8832017-09-11 16:54:57 +0100402 << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace) << std::endl;
403 stream << std::flush;
Andreas Gampec6fe4272017-06-01 20:14:58 -0700404 };
Roland Levillain21482ad2017-01-19 20:04:27 +0000405
Roland Levillain21482ad2017-01-19 20:04:27 +0000406 if (dump_on_stderr) {
407 // Note: We are using cerr directly instead of LOG macros to ensure even just partial output
408 // makes it out. That means we lose the "dalvikvm..." prefix, but that is acceptable
409 // considering this is an abort situation.
Andreas Gampec6fe4272017-06-01 20:14:58 -0700410 logger(std::cerr);
Roland Levillain21482ad2017-01-19 20:04:27 +0000411 } else {
Andreas Gampec6fe4272017-06-01 20:14:58 -0700412 logger(LOG_STREAM(FATAL_WITHOUT_ABORT));
Roland Levillain21482ad2017-01-19 20:04:27 +0000413 }
414 if (kIsDebugBuild && signal_number == SIGSEGV) {
Andreas Gampe39b378c2017-12-07 15:44:13 -0800415 PrintFileToLog("/proc/self/maps", android::base::LogSeverity::FATAL_WITHOUT_ABORT);
Roland Levillain21482ad2017-01-19 20:04:27 +0000416 }
417
418 Runtime* runtime = Runtime::Current();
419 if (runtime != nullptr) {
420 if (handle_timeout_signal && IsTimeoutSignal(signal_number)) {
421 // Special timeout signal. Try to dump all threads.
422 // Note: Do not use DumpForSigQuit, as that might disable native unwind, but the native parts
423 // are of value here.
Christopher Ferris642e9d82018-03-27 20:30:55 +0000424 runtime->GetThreadList()->Dump(std::cerr);
Roland Levillain21482ad2017-01-19 20:04:27 +0000425 std::cerr << std::endl;
426 }
427
428 if (dump_on_stderr) {
429 std::cerr << "Fault message: " << runtime->GetFaultMessage() << std::endl;
430 } else {
431 LOG(FATAL_WITHOUT_ABORT) << "Fault message: " << runtime->GetFaultMessage();
432 }
433 }
434}
435
Andreas Gampecdd53142018-03-28 21:17:43 -0700436void HandleUnexpectedSignalCommon(int signal_number,
437 siginfo_t* info,
438 void* raw_context,
439 bool handle_timeout_signal,
440 bool dump_on_stderr) {
441 // Local _static_ storing the currently handled signal (or -1).
442 static int handling_unexpected_signal = -1;
443
444 // Whether the dump code should be run under the unexpected-signal lock. For diagnostics we
445 // allow recursive unexpected-signals in certain cases - avoid a deadlock.
446 bool grab_lock = true;
447
448 if (handling_unexpected_signal != -1) {
449 LogHelper::LogLineLowStack(__FILE__,
450 __LINE__,
451 ::android::base::FATAL_WITHOUT_ABORT,
452 "HandleUnexpectedSignal reentered\n");
453 // Print the signal number. Don't use any standard functions, just some arithmetic. Just best
454 // effort, with a minimal buffer.
455 if (0 < signal_number && signal_number < 100) {
456 char buf[] = { ' ',
457 'S',
458 static_cast<char>('0' + (signal_number / 10)),
459 static_cast<char>('0' + (signal_number % 10)),
460 '\n',
461 0 };
462 LogHelper::LogLineLowStack(__FILE__,
463 __LINE__,
464 ::android::base::FATAL_WITHOUT_ABORT,
465 buf);
466 }
467 if (handle_timeout_signal) {
468 if (IsTimeoutSignal(signal_number)) {
469 // Ignore a recursive timeout.
470 return;
471 }
472 }
473 // If we were handling a timeout signal, try to go on. Otherwise hard-exit.
474 // This relies on the expectation that we'll only ever get one timeout signal.
475 if (!handle_timeout_signal || handling_unexpected_signal != GetTimeoutSignal()) {
476 _exit(1);
477 }
478 grab_lock = false; // The "outer" handling instance already holds the lock.
479 }
480 handling_unexpected_signal = signal_number;
481
482 gAborting++; // set before taking any locks
483
484 if (grab_lock) {
485 MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
486
487 HandleUnexpectedSignalCommonDump(signal_number,
488 info,
489 raw_context,
490 handle_timeout_signal,
491 dump_on_stderr);
492 } else {
493 HandleUnexpectedSignalCommonDump(signal_number,
494 info,
495 raw_context,
496 handle_timeout_signal,
497 dump_on_stderr);
498 }
499}
500
Roland Levillain32a5bb22017-01-31 14:33:16 +0000501#if defined(__APPLE__)
502#pragma GCC diagnostic pop
503#endif
504
Roland Levillain21482ad2017-01-19 20:04:27 +0000505void InitPlatformSignalHandlersCommon(void (*newact)(int, siginfo_t*, void*),
506 struct sigaction* oldact,
507 bool handle_timeout_signal) {
508 struct sigaction action;
509 memset(&action, 0, sizeof(action));
510 sigemptyset(&action.sa_mask);
511 action.sa_sigaction = newact;
512 // Use the three-argument sa_sigaction handler.
513 action.sa_flags |= SA_SIGINFO;
514 // Use the alternate signal stack so we can catch stack overflows.
515 action.sa_flags |= SA_ONSTACK;
516
517 int rc = 0;
518 rc += sigaction(SIGABRT, &action, oldact);
519 rc += sigaction(SIGBUS, &action, oldact);
520 rc += sigaction(SIGFPE, &action, oldact);
521 rc += sigaction(SIGILL, &action, oldact);
522 rc += sigaction(SIGPIPE, &action, oldact);
523 rc += sigaction(SIGSEGV, &action, oldact);
524#if defined(SIGSTKFLT)
525 rc += sigaction(SIGSTKFLT, &action, oldact);
526#endif
527 rc += sigaction(SIGTRAP, &action, oldact);
528 // Special dump-all timeout.
529 if (handle_timeout_signal && GetTimeoutSignal() != -1) {
530 rc += sigaction(GetTimeoutSignal(), &action, oldact);
531 }
532 CHECK_EQ(rc, 0);
533}
534
535} // namespace art