blob: 1de035c0d58a140ec8c48635b5c98f992fef8d02 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Elliott Hughesffe67362011-07-17 12:09:27 -070016
17#include "runtime.h"
18
Elliott Hughes457005c2012-04-16 13:54:25 -070019#include <signal.h>
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070020#include <string.h>
Elliott Hughes058a6de2012-05-24 19:13:02 -070021#include <sys/utsname.h>
Dmitry Petrochenko611c2c32014-02-10 14:48:12 +070022#include <inttypes.h>
Elliott Hughesffe67362011-07-17 12:09:27 -070023
Ian Rogersc7dd2952014-10-21 23:31:19 -070024#include <sstream>
25
26#include "base/dumpable.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080028#include "base/mutex.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080029#include "base/stringprintf.h"
Ian Rogersb48b9eb2014-02-28 16:20:21 -080030#include "thread-inl.h"
Elliott Hughes46e251b2012-05-22 15:10:45 -070031#include "utils.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070032
33namespace art {
34
Mathieu Chartierc2f4d022014-03-03 16:11:42 -080035static constexpr bool kDumpHeapObjectOnSigsevg = false;
36
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070037struct Backtrace {
Ian Rogersc7dd2952014-10-21 23:31:19 -070038 void Dump(std::ostream& os) const {
Christopher Ferrisa2cee182014-04-16 19:13:59 -070039 DumpNativeStack(os, GetTid(), "\t");
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070040 }
41};
42
Elliott Hughes76160052012-12-12 16:31:20 -080043struct OsInfo {
Ian Rogersc7dd2952014-10-21 23:31:19 -070044 void Dump(std::ostream& os) const {
Elliott Hughes058a6de2012-05-24 19:13:02 -070045 utsname info;
46 uname(&info);
47 // Linux 2.6.38.8-gg784 (x86_64)
48 // Darwin 11.4.0 (x86_64)
49 os << info.sysname << " " << info.release << " (" << info.machine << ")";
50 }
51};
52
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070053static const char* GetSignalName(int signal_number) {
54 switch (signal_number) {
55 case SIGABRT: return "SIGABRT";
56 case SIGBUS: return "SIGBUS";
57 case SIGFPE: return "SIGFPE";
58 case SIGILL: return "SIGILL";
59 case SIGPIPE: return "SIGPIPE";
60 case SIGSEGV: return "SIGSEGV";
Elliott Hughes833770b2012-05-01 15:41:03 -070061#if defined(SIGSTKFLT)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070062 case SIGSTKFLT: return "SIGSTKFLT";
63#endif
64 case SIGTRAP: return "SIGTRAP";
65 }
66 return "??";
Elliott Hughesffe67362011-07-17 12:09:27 -070067}
68
Elliott Hughes457005c2012-04-16 13:54:25 -070069static const char* GetSignalCodeName(int signal_number, int signal_code) {
70 // Try the signal-specific codes...
71 switch (signal_number) {
72 case SIGILL:
73 switch (signal_code) {
74 case ILL_ILLOPC: return "ILL_ILLOPC";
75 case ILL_ILLOPN: return "ILL_ILLOPN";
76 case ILL_ILLADR: return "ILL_ILLADR";
77 case ILL_ILLTRP: return "ILL_ILLTRP";
78 case ILL_PRVOPC: return "ILL_PRVOPC";
79 case ILL_PRVREG: return "ILL_PRVREG";
80 case ILL_COPROC: return "ILL_COPROC";
81 case ILL_BADSTK: return "ILL_BADSTK";
82 }
83 break;
84 case SIGBUS:
85 switch (signal_code) {
86 case BUS_ADRALN: return "BUS_ADRALN";
87 case BUS_ADRERR: return "BUS_ADRERR";
88 case BUS_OBJERR: return "BUS_OBJERR";
89 }
90 break;
91 case SIGFPE:
92 switch (signal_code) {
93 case FPE_INTDIV: return "FPE_INTDIV";
94 case FPE_INTOVF: return "FPE_INTOVF";
95 case FPE_FLTDIV: return "FPE_FLTDIV";
96 case FPE_FLTOVF: return "FPE_FLTOVF";
97 case FPE_FLTUND: return "FPE_FLTUND";
98 case FPE_FLTRES: return "FPE_FLTRES";
99 case FPE_FLTINV: return "FPE_FLTINV";
100 case FPE_FLTSUB: return "FPE_FLTSUB";
101 }
102 break;
103 case SIGSEGV:
104 switch (signal_code) {
105 case SEGV_MAPERR: return "SEGV_MAPERR";
106 case SEGV_ACCERR: return "SEGV_ACCERR";
107 }
108 break;
109 case SIGTRAP:
110 switch (signal_code) {
111 case TRAP_BRKPT: return "TRAP_BRKPT";
112 case TRAP_TRACE: return "TRAP_TRACE";
113 }
114 break;
115 }
116 // Then the other codes...
117 switch (signal_code) {
118 case SI_USER: return "SI_USER";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700119#if defined(SI_KERNEL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700120 case SI_KERNEL: return "SI_KERNEL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700121#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700122 case SI_QUEUE: return "SI_QUEUE";
123 case SI_TIMER: return "SI_TIMER";
124 case SI_MESGQ: return "SI_MESGQ";
125 case SI_ASYNCIO: return "SI_ASYNCIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700126#if defined(SI_SIGIO)
Elliott Hughes457005c2012-04-16 13:54:25 -0700127 case SI_SIGIO: return "SI_SIGIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700128#endif
129#if defined(SI_TKILL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700130 case SI_TKILL: return "SI_TKILL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700131#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700132 }
133 // Then give up...
134 return "?";
135}
136
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700137struct UContext {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700138 explicit UContext(void* raw_context) :
139 context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {
140 }
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700141
Ian Rogersc7dd2952014-10-21 23:31:19 -0700142 void Dump(std::ostream& os) const {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700143 // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800144#if defined(__APPLE__) && defined(__i386__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700145 DumpRegister32(os, "eax", context->__ss.__eax);
146 DumpRegister32(os, "ebx", context->__ss.__ebx);
147 DumpRegister32(os, "ecx", context->__ss.__ecx);
148 DumpRegister32(os, "edx", context->__ss.__edx);
149 os << '\n';
150
151 DumpRegister32(os, "edi", context->__ss.__edi);
152 DumpRegister32(os, "esi", context->__ss.__esi);
153 DumpRegister32(os, "ebp", context->__ss.__ebp);
154 DumpRegister32(os, "esp", context->__ss.__esp);
155 os << '\n';
156
157 DumpRegister32(os, "eip", context->__ss.__eip);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700158 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700159 DumpRegister32(os, "eflags", context->__ss.__eflags);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700160 DumpX86Flags(os, context->__ss.__eflags);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700161 os << '\n';
162
163 DumpRegister32(os, "cs", context->__ss.__cs);
164 DumpRegister32(os, "ds", context->__ss.__ds);
165 DumpRegister32(os, "es", context->__ss.__es);
166 DumpRegister32(os, "fs", context->__ss.__fs);
167 os << '\n';
168 DumpRegister32(os, "gs", context->__ss.__gs);
169 DumpRegister32(os, "ss", context->__ss.__ss);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800170#elif defined(__linux__) && defined(__i386__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700171 DumpRegister32(os, "eax", context.gregs[REG_EAX]);
172 DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
173 DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
174 DumpRegister32(os, "edx", context.gregs[REG_EDX]);
175 os << '\n';
176
177 DumpRegister32(os, "edi", context.gregs[REG_EDI]);
178 DumpRegister32(os, "esi", context.gregs[REG_ESI]);
179 DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
180 DumpRegister32(os, "esp", context.gregs[REG_ESP]);
181 os << '\n';
182
183 DumpRegister32(os, "eip", context.gregs[REG_EIP]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700184 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700185 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700186 DumpX86Flags(os, context.gregs[REG_EFL]);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700187 os << '\n';
188
189 DumpRegister32(os, "cs", context.gregs[REG_CS]);
190 DumpRegister32(os, "ds", context.gregs[REG_DS]);
191 DumpRegister32(os, "es", context.gregs[REG_ES]);
192 DumpRegister32(os, "fs", context.gregs[REG_FS]);
193 os << '\n';
194 DumpRegister32(os, "gs", context.gregs[REG_GS]);
195 DumpRegister32(os, "ss", context.gregs[REG_SS]);
Dmitry Petrochenko611c2c32014-02-10 14:48:12 +0700196#elif defined(__linux__) && defined(__x86_64__)
197 DumpRegister64(os, "rax", context.gregs[REG_RAX]);
198 DumpRegister64(os, "rbx", context.gregs[REG_RBX]);
199 DumpRegister64(os, "rcx", context.gregs[REG_RCX]);
200 DumpRegister64(os, "rdx", context.gregs[REG_RDX]);
201 os << '\n';
202
203 DumpRegister64(os, "rdi", context.gregs[REG_RDI]);
204 DumpRegister64(os, "rsi", context.gregs[REG_RSI]);
205 DumpRegister64(os, "rbp", context.gregs[REG_RBP]);
206 DumpRegister64(os, "rsp", context.gregs[REG_RSP]);
207 os << '\n';
208
209 DumpRegister64(os, "r8 ", context.gregs[REG_R8]);
210 DumpRegister64(os, "r9 ", context.gregs[REG_R9]);
211 DumpRegister64(os, "r10", context.gregs[REG_R10]);
212 DumpRegister64(os, "r11", context.gregs[REG_R11]);
213 os << '\n';
214
215 DumpRegister64(os, "r12", context.gregs[REG_R12]);
216 DumpRegister64(os, "r13", context.gregs[REG_R13]);
217 DumpRegister64(os, "r14", context.gregs[REG_R14]);
218 DumpRegister64(os, "r15", context.gregs[REG_R15]);
219 os << '\n';
220
221 DumpRegister64(os, "rip", context.gregs[REG_RIP]);
222 os << " ";
223 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
224 DumpX86Flags(os, context.gregs[REG_EFL]);
225 os << '\n';
226
227 DumpRegister32(os, "cs", (context.gregs[REG_CSGSFS]) & 0x0FFFF);
228 DumpRegister32(os, "gs", (context.gregs[REG_CSGSFS] >> 16) & 0x0FFFF);
229 DumpRegister32(os, "fs", (context.gregs[REG_CSGSFS] >> 32) & 0x0FFFF);
230 os << '\n';
Ian Rogersef7d42f2014-01-06 12:55:46 -0800231#else
232 os << "Unknown architecture/word size/OS in ucontext dump";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700233#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700234 }
235
Ian Rogersc7dd2952014-10-21 23:31:19 -0700236 void DumpRegister32(std::ostream& os, const char* name, uint32_t value) const {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700237 os << StringPrintf(" %6s: 0x%08x", name, value);
238 }
239
Ian Rogersc7dd2952014-10-21 23:31:19 -0700240 void DumpRegister64(std::ostream& os, const char* name, uint64_t value) const {
Dmitry Petrochenko611c2c32014-02-10 14:48:12 +0700241 os << StringPrintf(" %6s: 0x%016" PRIx64, name, value);
242 }
243
Ian Rogersc7dd2952014-10-21 23:31:19 -0700244 void DumpX86Flags(std::ostream& os, uint32_t flags) const {
Elliott Hughes46e251b2012-05-22 15:10:45 -0700245 os << " [";
246 if ((flags & (1 << 0)) != 0) {
247 os << " CF";
248 }
249 if ((flags & (1 << 2)) != 0) {
250 os << " PF";
251 }
252 if ((flags & (1 << 4)) != 0) {
253 os << " AF";
254 }
255 if ((flags & (1 << 6)) != 0) {
256 os << " ZF";
257 }
258 if ((flags & (1 << 7)) != 0) {
259 os << " SF";
260 }
261 if ((flags & (1 << 8)) != 0) {
262 os << " TF";
263 }
264 if ((flags & (1 << 9)) != 0) {
265 os << " IF";
266 }
267 if ((flags & (1 << 10)) != 0) {
268 os << " DF";
269 }
270 if ((flags & (1 << 11)) != 0) {
271 os << " OF";
272 }
273 os << " ]";
274 }
275
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700276 mcontext_t& context;
277};
278
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800279void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
280 static bool handlingUnexpectedSignal = false;
281 if (handlingUnexpectedSignal) {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700282 LogMessage::LogLine(__FILE__, __LINE__, INTERNAL_FATAL, "HandleUnexpectedSignal reentered\n");
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800283 _exit(1);
284 }
285 handlingUnexpectedSignal = true;
286
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000287 gAborting++; // set before taking any locks
Ian Rogers50b35e22012-10-04 10:09:15 -0700288 MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700289
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700290 bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
291 signal_number == SIGFPE || signal_number == SIGSEGV);
292
Elliott Hughes76160052012-12-12 16:31:20 -0800293 OsInfo os_info;
Elliott Hughes98eedd82012-06-11 17:52:56 -0700294 const char* cmd_line = GetCmdLine();
295 if (cmd_line == NULL) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700296 cmd_line = "<unset>"; // Because no-one called InitLogging.
Elliott Hughes98eedd82012-06-11 17:52:56 -0700297 }
Elliott Hughes289be852012-06-12 13:57:20 -0700298 pid_t tid = GetTid();
299 std::string thread_name(GetThreadName(tid));
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700300 UContext thread_context(raw_context);
301 Backtrace thread_backtrace;
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700302
Elliott Hughes457005c2012-04-16 13:54:25 -0700303 LOG(INTERNAL_FATAL) << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"
304 << StringPrintf("Fatal signal %d (%s), code %d (%s)",
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700305 signal_number, GetSignalName(signal_number),
Elliott Hughes457005c2012-04-16 13:54:25 -0700306 info->si_code,
307 GetSignalCodeName(signal_number, info->si_code))
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700308 << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << "\n"
Elliott Hughes76160052012-12-12 16:31:20 -0800309 << "OS: " << Dumpable<OsInfo>(os_info) << "\n"
Elliott Hughes98eedd82012-06-11 17:52:56 -0700310 << "Cmdline: " << cmd_line << "\n"
Elliott Hughes289be852012-06-12 13:57:20 -0700311 << "Thread: " << tid << " \"" << thread_name << "\"\n"
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700312 << "Registers:\n" << Dumpable<UContext>(thread_context) << "\n"
313 << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace);
Mathieu Chartier15d34022014-02-26 17:16:38 -0800314 Runtime* runtime = Runtime::Current();
315 if (runtime != nullptr) {
316 gc::Heap* heap = runtime->GetHeap();
317 LOG(INTERNAL_FATAL) << "Fault message: " << runtime->GetFaultMessage();
Mathieu Chartierc2f4d022014-03-03 16:11:42 -0800318 if (kDumpHeapObjectOnSigsevg && heap != nullptr && info != nullptr) {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800319 LOG(INTERNAL_FATAL) << "Dump heap object at fault address: ";
320 heap->DumpObject(LOG(INTERNAL_FATAL), reinterpret_cast<mirror::Object*>(info->si_addr));
321 }
322 }
Elliott Hughes4909ba42012-06-14 13:33:49 -0700323 if (getenv("debug_db_uid") != NULL || getenv("art_wait_for_gdb_on_crash") != NULL) {
Elliott Hughes2554cb92012-04-18 17:19:26 -0700324 LOG(INTERNAL_FATAL) << "********************************************************\n"
Elliott Hughes289be852012-06-12 13:57:20 -0700325 << "* Process " << getpid() << " thread " << tid << " \"" << thread_name << "\""
326 << " has been suspended while crashing.\n"
327 << "* Attach gdb:\n"
328 << "* gdb -p " << tid << "\n"
Elliott Hughes2554cb92012-04-18 17:19:26 -0700329 << "********************************************************\n";
330 // Wait for debugger to attach.
331 while (true) {
332 }
Elliott Hughes457005c2012-04-16 13:54:25 -0700333 }
Ian Rogersc5f17732014-06-05 20:48:42 -0700334#ifdef __linux__
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700335 // Remove our signal handler for this signal...
336 struct sigaction action;
337 memset(&action, 0, sizeof(action));
338 sigemptyset(&action.sa_mask);
339 action.sa_handler = SIG_DFL;
340 sigaction(signal_number, &action, NULL);
341 // ...and re-raise so we die with the appropriate status.
342 kill(getpid(), signal_number);
Ian Rogersc5f17732014-06-05 20:48:42 -0700343#else
344 exit(EXIT_FAILURE);
345#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700346}
347
Elliott Hughes457005c2012-04-16 13:54:25 -0700348void Runtime::InitPlatformSignalHandlers() {
349 // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
350 struct sigaction action;
351 memset(&action, 0, sizeof(action));
352 sigemptyset(&action.sa_mask);
353 action.sa_sigaction = HandleUnexpectedSignal;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700354 // Use the three-argument sa_sigaction handler.
355 action.sa_flags |= SA_SIGINFO;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700356 // Use the alternate signal stack so we can catch stack overflows.
357 action.sa_flags |= SA_ONSTACK;
Elliott Hughes457005c2012-04-16 13:54:25 -0700358
359 int rc = 0;
Elliott Hughes457005c2012-04-16 13:54:25 -0700360 rc += sigaction(SIGABRT, &action, NULL);
361 rc += sigaction(SIGBUS, &action, NULL);
362 rc += sigaction(SIGFPE, &action, NULL);
Elliott Hughes058a6de2012-05-24 19:13:02 -0700363 rc += sigaction(SIGILL, &action, NULL);
364 rc += sigaction(SIGPIPE, &action, NULL);
365 rc += sigaction(SIGSEGV, &action, NULL);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700366#if defined(SIGSTKFLT)
Elliott Hughes457005c2012-04-16 13:54:25 -0700367 rc += sigaction(SIGSTKFLT, &action, NULL);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700368#endif
Elliott Hughes058a6de2012-05-24 19:13:02 -0700369 rc += sigaction(SIGTRAP, &action, NULL);
Elliott Hughes457005c2012-04-16 13:54:25 -0700370 CHECK_EQ(rc, 0);
371}
372
Elliott Hughesffe67362011-07-17 12:09:27 -0700373} // namespace art