1 /* 2 * Copyright (C) 2011 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.h" 18 19 #include <signal.h> 20 #include <string.h> 21 #include <sys/utsname.h> 22 #include <inttypes.h> 23 24 #include <sstream> 25 26 #include "base/dumpable.h" 27 #include "base/logging.h" 28 #include "base/macros.h" 29 #include "base/mutex.h" 30 #include "base/stringprintf.h" 31 #include "thread-inl.h" 32 #include "thread_list.h" 33 #include "utils.h" 34 35 namespace art { 36 37 static constexpr bool kDumpHeapObjectOnSigsevg = false; 38 static constexpr bool kUseSigRTTimeout = true; 39 static constexpr bool kDumpNativeStackOnTimeout = true; 40 41 struct Backtrace { 42 public: 43 explicit Backtrace(void* raw_context) : raw_context_(raw_context) {} 44 void Dump(std::ostream& os) const { 45 DumpNativeStack(os, GetTid(), nullptr, "\t", nullptr, raw_context_); 46 } 47 private: 48 // Stores the context of the signal that was unexpected and will terminate the runtime. The 49 // DumpNativeStack code will take care of casting it to the expected type. This is required 50 // as our signal handler runs on an alternate stack. 51 void* raw_context_; 52 }; 53 54 struct OsInfo { 55 void Dump(std::ostream& os) const { 56 utsname info; 57 uname(&info); 58 // Linux 2.6.38.8-gg784 (x86_64) 59 // Darwin 11.4.0 (x86_64) 60 os << info.sysname << " " << info.release << " (" << info.machine << ")"; 61 } 62 }; 63 64 static const char* GetSignalName(int signal_number) { 65 switch (signal_number) { 66 case SIGABRT: return "SIGABRT"; 67 case SIGBUS: return "SIGBUS"; 68 case SIGFPE: return "SIGFPE"; 69 case SIGILL: return "SIGILL"; 70 case SIGPIPE: return "SIGPIPE"; 71 case SIGSEGV: return "SIGSEGV"; 72 #if defined(SIGSTKFLT) 73 case SIGSTKFLT: return "SIGSTKFLT"; 74 #endif 75 case SIGTRAP: return "SIGTRAP"; 76 } 77 return "??"; 78 } 79 80 static const char* GetSignalCodeName(int signal_number, int signal_code) { 81 // Try the signal-specific codes... 82 switch (signal_number) { 83 case SIGILL: 84 switch (signal_code) { 85 case ILL_ILLOPC: return "ILL_ILLOPC"; 86 case ILL_ILLOPN: return "ILL_ILLOPN"; 87 case ILL_ILLADR: return "ILL_ILLADR"; 88 case ILL_ILLTRP: return "ILL_ILLTRP"; 89 case ILL_PRVOPC: return "ILL_PRVOPC"; 90 case ILL_PRVREG: return "ILL_PRVREG"; 91 case ILL_COPROC: return "ILL_COPROC"; 92 case ILL_BADSTK: return "ILL_BADSTK"; 93 } 94 break; 95 case SIGBUS: 96 switch (signal_code) { 97 case BUS_ADRALN: return "BUS_ADRALN"; 98 case BUS_ADRERR: return "BUS_ADRERR"; 99 case BUS_OBJERR: return "BUS_OBJERR"; 100 } 101 break; 102 case SIGFPE: 103 switch (signal_code) { 104 case FPE_INTDIV: return "FPE_INTDIV"; 105 case FPE_INTOVF: return "FPE_INTOVF"; 106 case FPE_FLTDIV: return "FPE_FLTDIV"; 107 case FPE_FLTOVF: return "FPE_FLTOVF"; 108 case FPE_FLTUND: return "FPE_FLTUND"; 109 case FPE_FLTRES: return "FPE_FLTRES"; 110 case FPE_FLTINV: return "FPE_FLTINV"; 111 case FPE_FLTSUB: return "FPE_FLTSUB"; 112 } 113 break; 114 case SIGSEGV: 115 switch (signal_code) { 116 case SEGV_MAPERR: return "SEGV_MAPERR"; 117 case SEGV_ACCERR: return "SEGV_ACCERR"; 118 #if defined(SEGV_BNDERR) 119 case SEGV_BNDERR: return "SEGV_BNDERR"; 120 #endif 121 } 122 break; 123 case SIGTRAP: 124 switch (signal_code) { 125 case TRAP_BRKPT: return "TRAP_BRKPT"; 126 case TRAP_TRACE: return "TRAP_TRACE"; 127 } 128 break; 129 } 130 // Then the other codes... 131 switch (signal_code) { 132 case SI_USER: return "SI_USER"; 133 #if defined(SI_KERNEL) 134 case SI_KERNEL: return "SI_KERNEL"; 135 #endif 136 case SI_QUEUE: return "SI_QUEUE"; 137 case SI_TIMER: return "SI_TIMER"; 138 case SI_MESGQ: return "SI_MESGQ"; 139 case SI_ASYNCIO: return "SI_ASYNCIO"; 140 #if defined(SI_SIGIO) 141 case SI_SIGIO: return "SI_SIGIO"; 142 #endif 143 #if defined(SI_TKILL) 144 case SI_TKILL: return "SI_TKILL"; 145 #endif 146 } 147 // Then give up... 148 return "?"; 149 } 150 151 struct UContext { 152 explicit UContext(void* raw_context) : 153 context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) { 154 } 155 156 void Dump(std::ostream& os) const { 157 // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets). 158 #if defined(__APPLE__) && defined(__i386__) 159 DumpRegister32(os, "eax", context->__ss.__eax); 160 DumpRegister32(os, "ebx", context->__ss.__ebx); 161 DumpRegister32(os, "ecx", context->__ss.__ecx); 162 DumpRegister32(os, "edx", context->__ss.__edx); 163 os << '\n'; 164 165 DumpRegister32(os, "edi", context->__ss.__edi); 166 DumpRegister32(os, "esi", context->__ss.__esi); 167 DumpRegister32(os, "ebp", context->__ss.__ebp); 168 DumpRegister32(os, "esp", context->__ss.__esp); 169 os << '\n'; 170 171 DumpRegister32(os, "eip", context->__ss.__eip); 172 os << " "; 173 DumpRegister32(os, "eflags", context->__ss.__eflags); 174 DumpX86Flags(os, context->__ss.__eflags); 175 os << '\n'; 176 177 DumpRegister32(os, "cs", context->__ss.__cs); 178 DumpRegister32(os, "ds", context->__ss.__ds); 179 DumpRegister32(os, "es", context->__ss.__es); 180 DumpRegister32(os, "fs", context->__ss.__fs); 181 os << '\n'; 182 DumpRegister32(os, "gs", context->__ss.__gs); 183 DumpRegister32(os, "ss", context->__ss.__ss); 184 #elif defined(__linux__) && defined(__i386__) 185 DumpRegister32(os, "eax", context.gregs[REG_EAX]); 186 DumpRegister32(os, "ebx", context.gregs[REG_EBX]); 187 DumpRegister32(os, "ecx", context.gregs[REG_ECX]); 188 DumpRegister32(os, "edx", context.gregs[REG_EDX]); 189 os << '\n'; 190 191 DumpRegister32(os, "edi", context.gregs[REG_EDI]); 192 DumpRegister32(os, "esi", context.gregs[REG_ESI]); 193 DumpRegister32(os, "ebp", context.gregs[REG_EBP]); 194 DumpRegister32(os, "esp", context.gregs[REG_ESP]); 195 os << '\n'; 196 197 DumpRegister32(os, "eip", context.gregs[REG_EIP]); 198 os << " "; 199 DumpRegister32(os, "eflags", context.gregs[REG_EFL]); 200 DumpX86Flags(os, context.gregs[REG_EFL]); 201 os << '\n'; 202 203 DumpRegister32(os, "cs", context.gregs[REG_CS]); 204 DumpRegister32(os, "ds", context.gregs[REG_DS]); 205 DumpRegister32(os, "es", context.gregs[REG_ES]); 206 DumpRegister32(os, "fs", context.gregs[REG_FS]); 207 os << '\n'; 208 DumpRegister32(os, "gs", context.gregs[REG_GS]); 209 DumpRegister32(os, "ss", context.gregs[REG_SS]); 210 #elif defined(__linux__) && defined(__x86_64__) 211 DumpRegister64(os, "rax", context.gregs[REG_RAX]); 212 DumpRegister64(os, "rbx", context.gregs[REG_RBX]); 213 DumpRegister64(os, "rcx", context.gregs[REG_RCX]); 214 DumpRegister64(os, "rdx", context.gregs[REG_RDX]); 215 os << '\n'; 216 217 DumpRegister64(os, "rdi", context.gregs[REG_RDI]); 218 DumpRegister64(os, "rsi", context.gregs[REG_RSI]); 219 DumpRegister64(os, "rbp", context.gregs[REG_RBP]); 220 DumpRegister64(os, "rsp", context.gregs[REG_RSP]); 221 os << '\n'; 222 223 DumpRegister64(os, "r8 ", context.gregs[REG_R8]); 224 DumpRegister64(os, "r9 ", context.gregs[REG_R9]); 225 DumpRegister64(os, "r10", context.gregs[REG_R10]); 226 DumpRegister64(os, "r11", context.gregs[REG_R11]); 227 os << '\n'; 228 229 DumpRegister64(os, "r12", context.gregs[REG_R12]); 230 DumpRegister64(os, "r13", context.gregs[REG_R13]); 231 DumpRegister64(os, "r14", context.gregs[REG_R14]); 232 DumpRegister64(os, "r15", context.gregs[REG_R15]); 233 os << '\n'; 234 235 DumpRegister64(os, "rip", context.gregs[REG_RIP]); 236 os << " "; 237 DumpRegister32(os, "eflags", context.gregs[REG_EFL]); 238 DumpX86Flags(os, context.gregs[REG_EFL]); 239 os << '\n'; 240 241 DumpRegister32(os, "cs", (context.gregs[REG_CSGSFS]) & 0x0FFFF); 242 DumpRegister32(os, "gs", (context.gregs[REG_CSGSFS] >> 16) & 0x0FFFF); 243 DumpRegister32(os, "fs", (context.gregs[REG_CSGSFS] >> 32) & 0x0FFFF); 244 os << '\n'; 245 #else 246 os << "Unknown architecture/word size/OS in ucontext dump"; 247 #endif 248 } 249 250 void DumpRegister32(std::ostream& os, const char* name, uint32_t value) const { 251 os << StringPrintf(" %6s: 0x%08x", name, value); 252 } 253 254 void DumpRegister64(std::ostream& os, const char* name, uint64_t value) const { 255 os << StringPrintf(" %6s: 0x%016" PRIx64, name, value); 256 } 257 258 void DumpX86Flags(std::ostream& os, uint32_t flags) const { 259 os << " ["; 260 if ((flags & (1 << 0)) != 0) { 261 os << " CF"; 262 } 263 if ((flags & (1 << 2)) != 0) { 264 os << " PF"; 265 } 266 if ((flags & (1 << 4)) != 0) { 267 os << " AF"; 268 } 269 if ((flags & (1 << 6)) != 0) { 270 os << " ZF"; 271 } 272 if ((flags & (1 << 7)) != 0) { 273 os << " SF"; 274 } 275 if ((flags & (1 << 8)) != 0) { 276 os << " TF"; 277 } 278 if ((flags & (1 << 9)) != 0) { 279 os << " IF"; 280 } 281 if ((flags & (1 << 10)) != 0) { 282 os << " DF"; 283 } 284 if ((flags & (1 << 11)) != 0) { 285 os << " OF"; 286 } 287 os << " ]"; 288 } 289 290 mcontext_t& context; 291 }; 292 293 // Return the signal number we recognize as timeout. -1 means not active/supported. 294 static int GetTimeoutSignal() { 295 #if defined(__APPLE__) 296 // Mac does not support realtime signals. 297 UNUSED(kUseSigRTTimeout); 298 return -1; 299 #else 300 return kUseSigRTTimeout ? (SIGRTMIN + 2) : -1; 301 #endif 302 } 303 304 static bool IsTimeoutSignal(int signal_number) { 305 return signal_number == GetTimeoutSignal(); 306 } 307 308 void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) { 309 static bool handlingUnexpectedSignal = false; 310 if (handlingUnexpectedSignal) { 311 LogMessage::LogLine(__FILE__, __LINE__, INTERNAL_FATAL, "HandleUnexpectedSignal reentered\n"); 312 if (IsTimeoutSignal(signal_number)) { 313 // Ignore a recursive timeout. 314 return; 315 } 316 _exit(1); 317 } 318 handlingUnexpectedSignal = true; 319 320 gAborting++; // set before taking any locks 321 MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_); 322 323 bool has_address = (signal_number == SIGILL || signal_number == SIGBUS || 324 signal_number == SIGFPE || signal_number == SIGSEGV); 325 326 OsInfo os_info; 327 const char* cmd_line = GetCmdLine(); 328 if (cmd_line == nullptr) { 329 cmd_line = "<unset>"; // Because no-one called InitLogging. 330 } 331 pid_t tid = GetTid(); 332 std::string thread_name(GetThreadName(tid)); 333 UContext thread_context(raw_context); 334 Backtrace thread_backtrace(raw_context); 335 336 LOG(INTERNAL_FATAL) << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n" 337 << StringPrintf("Fatal signal %d (%s), code %d (%s)", 338 signal_number, GetSignalName(signal_number), 339 info->si_code, 340 GetSignalCodeName(signal_number, info->si_code)) 341 << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << "\n" 342 << "OS: " << Dumpable<OsInfo>(os_info) << "\n" 343 << "Cmdline: " << cmd_line << "\n" 344 << "Thread: " << tid << " \"" << thread_name << "\"\n" 345 << "Registers:\n" << Dumpable<UContext>(thread_context) << "\n" 346 << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace); 347 if (kIsDebugBuild && signal_number == SIGSEGV) { 348 PrintFileToLog("/proc/self/maps", LogSeverity::INTERNAL_FATAL); 349 } 350 Runtime* runtime = Runtime::Current(); 351 if (runtime != nullptr) { 352 if (IsTimeoutSignal(signal_number)) { 353 // Special timeout signal. Try to dump all threads. 354 // Note: Do not use DumpForSigQuit, as that might disable native unwind, but the native parts 355 // are of value here. 356 runtime->GetThreadList()->Dump(LOG(INTERNAL_FATAL), kDumpNativeStackOnTimeout); 357 } 358 gc::Heap* heap = runtime->GetHeap(); 359 LOG(INTERNAL_FATAL) << "Fault message: " << runtime->GetFaultMessage(); 360 if (kDumpHeapObjectOnSigsevg && heap != nullptr && info != nullptr) { 361 LOG(INTERNAL_FATAL) << "Dump heap object at fault address: "; 362 heap->DumpObject(LOG(INTERNAL_FATAL), reinterpret_cast<mirror::Object*>(info->si_addr)); 363 } 364 } 365 if (getenv("debug_db_uid") != nullptr || getenv("art_wait_for_gdb_on_crash") != nullptr) { 366 LOG(INTERNAL_FATAL) << "********************************************************\n" 367 << "* Process " << getpid() << " thread " << tid << " \"" << thread_name 368 << "\"" 369 << " has been suspended while crashing.\n" 370 << "* Attach gdb:\n" 371 << "* gdb -p " << tid << "\n" 372 << "********************************************************\n"; 373 // Wait for debugger to attach. 374 while (true) { 375 } 376 } 377 #ifdef __linux__ 378 // Remove our signal handler for this signal... 379 struct sigaction action; 380 memset(&action, 0, sizeof(action)); 381 sigemptyset(&action.sa_mask); 382 action.sa_handler = SIG_DFL; 383 sigaction(signal_number, &action, nullptr); 384 // ...and re-raise so we die with the appropriate status. 385 kill(getpid(), signal_number); 386 #else 387 exit(EXIT_FAILURE); 388 #endif 389 } 390 391 void Runtime::InitPlatformSignalHandlers() { 392 // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens. 393 struct sigaction action; 394 memset(&action, 0, sizeof(action)); 395 sigemptyset(&action.sa_mask); 396 action.sa_sigaction = HandleUnexpectedSignal; 397 // Use the three-argument sa_sigaction handler. 398 action.sa_flags |= SA_SIGINFO; 399 // Use the alternate signal stack so we can catch stack overflows. 400 action.sa_flags |= SA_ONSTACK; 401 402 int rc = 0; 403 rc += sigaction(SIGABRT, &action, nullptr); 404 rc += sigaction(SIGBUS, &action, nullptr); 405 rc += sigaction(SIGFPE, &action, nullptr); 406 rc += sigaction(SIGILL, &action, nullptr); 407 rc += sigaction(SIGPIPE, &action, nullptr); 408 rc += sigaction(SIGSEGV, &action, nullptr); 409 #if defined(SIGSTKFLT) 410 rc += sigaction(SIGSTKFLT, &action, nullptr); 411 #endif 412 rc += sigaction(SIGTRAP, &action, nullptr); 413 // Special dump-all timeout. 414 if (GetTimeoutSignal() != -1) { 415 rc += sigaction(GetTimeoutSignal(), &action, nullptr); 416 } 417 CHECK_EQ(rc, 0); 418 } 419 420 } // namespace art 421