blob: a5a02042acb8871c16da04864aa1f582dd303bf7 [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 {
Andreas Gampe628a61a2015-01-07 22:08:35 -080038 public:
39 explicit Backtrace(void* raw_context) : raw_context_(raw_context) {}
Ian Rogersc7dd2952014-10-21 23:31:19 -070040 void Dump(std::ostream& os) const {
Andreas Gampe628a61a2015-01-07 22:08:35 -080041 DumpNativeStack(os, GetTid(), "\t", nullptr, raw_context_);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070042 }
Andreas Gampe628a61a2015-01-07 22:08:35 -080043 private:
44 // Stores the context of the signal that was unexpected and will terminate the runtime. The
45 // DumpNativeStack code will take care of casting it to the expected type. This is required
46 // as our signal handler runs on an alternate stack.
47 void* raw_context_;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070048};
49
Elliott Hughes76160052012-12-12 16:31:20 -080050struct OsInfo {
Ian Rogersc7dd2952014-10-21 23:31:19 -070051 void Dump(std::ostream& os) const {
Elliott Hughes058a6de2012-05-24 19:13:02 -070052 utsname info;
53 uname(&info);
54 // Linux 2.6.38.8-gg784 (x86_64)
55 // Darwin 11.4.0 (x86_64)
56 os << info.sysname << " " << info.release << " (" << info.machine << ")";
57 }
58};
59
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070060static const char* GetSignalName(int signal_number) {
61 switch (signal_number) {
62 case SIGABRT: return "SIGABRT";
63 case SIGBUS: return "SIGBUS";
64 case SIGFPE: return "SIGFPE";
65 case SIGILL: return "SIGILL";
66 case SIGPIPE: return "SIGPIPE";
67 case SIGSEGV: return "SIGSEGV";
Elliott Hughes833770b2012-05-01 15:41:03 -070068#if defined(SIGSTKFLT)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070069 case SIGSTKFLT: return "SIGSTKFLT";
70#endif
71 case SIGTRAP: return "SIGTRAP";
72 }
73 return "??";
Elliott Hughesffe67362011-07-17 12:09:27 -070074}
75
Elliott Hughes457005c2012-04-16 13:54:25 -070076static const char* GetSignalCodeName(int signal_number, int signal_code) {
77 // Try the signal-specific codes...
78 switch (signal_number) {
79 case SIGILL:
80 switch (signal_code) {
81 case ILL_ILLOPC: return "ILL_ILLOPC";
82 case ILL_ILLOPN: return "ILL_ILLOPN";
83 case ILL_ILLADR: return "ILL_ILLADR";
84 case ILL_ILLTRP: return "ILL_ILLTRP";
85 case ILL_PRVOPC: return "ILL_PRVOPC";
86 case ILL_PRVREG: return "ILL_PRVREG";
87 case ILL_COPROC: return "ILL_COPROC";
88 case ILL_BADSTK: return "ILL_BADSTK";
89 }
90 break;
91 case SIGBUS:
92 switch (signal_code) {
93 case BUS_ADRALN: return "BUS_ADRALN";
94 case BUS_ADRERR: return "BUS_ADRERR";
95 case BUS_OBJERR: return "BUS_OBJERR";
96 }
97 break;
98 case SIGFPE:
99 switch (signal_code) {
100 case FPE_INTDIV: return "FPE_INTDIV";
101 case FPE_INTOVF: return "FPE_INTOVF";
102 case FPE_FLTDIV: return "FPE_FLTDIV";
103 case FPE_FLTOVF: return "FPE_FLTOVF";
104 case FPE_FLTUND: return "FPE_FLTUND";
105 case FPE_FLTRES: return "FPE_FLTRES";
106 case FPE_FLTINV: return "FPE_FLTINV";
107 case FPE_FLTSUB: return "FPE_FLTSUB";
108 }
109 break;
110 case SIGSEGV:
111 switch (signal_code) {
112 case SEGV_MAPERR: return "SEGV_MAPERR";
113 case SEGV_ACCERR: return "SEGV_ACCERR";
114 }
115 break;
116 case SIGTRAP:
117 switch (signal_code) {
118 case TRAP_BRKPT: return "TRAP_BRKPT";
119 case TRAP_TRACE: return "TRAP_TRACE";
120 }
121 break;
122 }
123 // Then the other codes...
124 switch (signal_code) {
125 case SI_USER: return "SI_USER";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700126#if defined(SI_KERNEL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700127 case SI_KERNEL: return "SI_KERNEL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700128#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700129 case SI_QUEUE: return "SI_QUEUE";
130 case SI_TIMER: return "SI_TIMER";
131 case SI_MESGQ: return "SI_MESGQ";
132 case SI_ASYNCIO: return "SI_ASYNCIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700133#if defined(SI_SIGIO)
Elliott Hughes457005c2012-04-16 13:54:25 -0700134 case SI_SIGIO: return "SI_SIGIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700135#endif
136#if defined(SI_TKILL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700137 case SI_TKILL: return "SI_TKILL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700138#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700139 }
140 // Then give up...
141 return "?";
142}
143
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700144struct UContext {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700145 explicit UContext(void* raw_context) :
146 context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {
147 }
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700148
Ian Rogersc7dd2952014-10-21 23:31:19 -0700149 void Dump(std::ostream& os) const {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700150 // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800151#if defined(__APPLE__) && defined(__i386__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700152 DumpRegister32(os, "eax", context->__ss.__eax);
153 DumpRegister32(os, "ebx", context->__ss.__ebx);
154 DumpRegister32(os, "ecx", context->__ss.__ecx);
155 DumpRegister32(os, "edx", context->__ss.__edx);
156 os << '\n';
157
158 DumpRegister32(os, "edi", context->__ss.__edi);
159 DumpRegister32(os, "esi", context->__ss.__esi);
160 DumpRegister32(os, "ebp", context->__ss.__ebp);
161 DumpRegister32(os, "esp", context->__ss.__esp);
162 os << '\n';
163
164 DumpRegister32(os, "eip", context->__ss.__eip);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700165 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700166 DumpRegister32(os, "eflags", context->__ss.__eflags);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700167 DumpX86Flags(os, context->__ss.__eflags);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700168 os << '\n';
169
170 DumpRegister32(os, "cs", context->__ss.__cs);
171 DumpRegister32(os, "ds", context->__ss.__ds);
172 DumpRegister32(os, "es", context->__ss.__es);
173 DumpRegister32(os, "fs", context->__ss.__fs);
174 os << '\n';
175 DumpRegister32(os, "gs", context->__ss.__gs);
176 DumpRegister32(os, "ss", context->__ss.__ss);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800177#elif defined(__linux__) && defined(__i386__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700178 DumpRegister32(os, "eax", context.gregs[REG_EAX]);
179 DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
180 DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
181 DumpRegister32(os, "edx", context.gregs[REG_EDX]);
182 os << '\n';
183
184 DumpRegister32(os, "edi", context.gregs[REG_EDI]);
185 DumpRegister32(os, "esi", context.gregs[REG_ESI]);
186 DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
187 DumpRegister32(os, "esp", context.gregs[REG_ESP]);
188 os << '\n';
189
190 DumpRegister32(os, "eip", context.gregs[REG_EIP]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700191 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700192 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700193 DumpX86Flags(os, context.gregs[REG_EFL]);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700194 os << '\n';
195
196 DumpRegister32(os, "cs", context.gregs[REG_CS]);
197 DumpRegister32(os, "ds", context.gregs[REG_DS]);
198 DumpRegister32(os, "es", context.gregs[REG_ES]);
199 DumpRegister32(os, "fs", context.gregs[REG_FS]);
200 os << '\n';
201 DumpRegister32(os, "gs", context.gregs[REG_GS]);
202 DumpRegister32(os, "ss", context.gregs[REG_SS]);
Dmitry Petrochenko611c2c32014-02-10 14:48:12 +0700203#elif defined(__linux__) && defined(__x86_64__)
204 DumpRegister64(os, "rax", context.gregs[REG_RAX]);
205 DumpRegister64(os, "rbx", context.gregs[REG_RBX]);
206 DumpRegister64(os, "rcx", context.gregs[REG_RCX]);
207 DumpRegister64(os, "rdx", context.gregs[REG_RDX]);
208 os << '\n';
209
210 DumpRegister64(os, "rdi", context.gregs[REG_RDI]);
211 DumpRegister64(os, "rsi", context.gregs[REG_RSI]);
212 DumpRegister64(os, "rbp", context.gregs[REG_RBP]);
213 DumpRegister64(os, "rsp", context.gregs[REG_RSP]);
214 os << '\n';
215
216 DumpRegister64(os, "r8 ", context.gregs[REG_R8]);
217 DumpRegister64(os, "r9 ", context.gregs[REG_R9]);
218 DumpRegister64(os, "r10", context.gregs[REG_R10]);
219 DumpRegister64(os, "r11", context.gregs[REG_R11]);
220 os << '\n';
221
222 DumpRegister64(os, "r12", context.gregs[REG_R12]);
223 DumpRegister64(os, "r13", context.gregs[REG_R13]);
224 DumpRegister64(os, "r14", context.gregs[REG_R14]);
225 DumpRegister64(os, "r15", context.gregs[REG_R15]);
226 os << '\n';
227
228 DumpRegister64(os, "rip", context.gregs[REG_RIP]);
229 os << " ";
230 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
231 DumpX86Flags(os, context.gregs[REG_EFL]);
232 os << '\n';
233
234 DumpRegister32(os, "cs", (context.gregs[REG_CSGSFS]) & 0x0FFFF);
235 DumpRegister32(os, "gs", (context.gregs[REG_CSGSFS] >> 16) & 0x0FFFF);
236 DumpRegister32(os, "fs", (context.gregs[REG_CSGSFS] >> 32) & 0x0FFFF);
237 os << '\n';
Ian Rogersef7d42f2014-01-06 12:55:46 -0800238#else
239 os << "Unknown architecture/word size/OS in ucontext dump";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700240#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700241 }
242
Ian Rogersc7dd2952014-10-21 23:31:19 -0700243 void DumpRegister32(std::ostream& os, const char* name, uint32_t value) const {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700244 os << StringPrintf(" %6s: 0x%08x", name, value);
245 }
246
Ian Rogersc7dd2952014-10-21 23:31:19 -0700247 void DumpRegister64(std::ostream& os, const char* name, uint64_t value) const {
Dmitry Petrochenko611c2c32014-02-10 14:48:12 +0700248 os << StringPrintf(" %6s: 0x%016" PRIx64, name, value);
249 }
250
Ian Rogersc7dd2952014-10-21 23:31:19 -0700251 void DumpX86Flags(std::ostream& os, uint32_t flags) const {
Elliott Hughes46e251b2012-05-22 15:10:45 -0700252 os << " [";
253 if ((flags & (1 << 0)) != 0) {
254 os << " CF";
255 }
256 if ((flags & (1 << 2)) != 0) {
257 os << " PF";
258 }
259 if ((flags & (1 << 4)) != 0) {
260 os << " AF";
261 }
262 if ((flags & (1 << 6)) != 0) {
263 os << " ZF";
264 }
265 if ((flags & (1 << 7)) != 0) {
266 os << " SF";
267 }
268 if ((flags & (1 << 8)) != 0) {
269 os << " TF";
270 }
271 if ((flags & (1 << 9)) != 0) {
272 os << " IF";
273 }
274 if ((flags & (1 << 10)) != 0) {
275 os << " DF";
276 }
277 if ((flags & (1 << 11)) != 0) {
278 os << " OF";
279 }
280 os << " ]";
281 }
282
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700283 mcontext_t& context;
284};
285
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800286void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
287 static bool handlingUnexpectedSignal = false;
288 if (handlingUnexpectedSignal) {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700289 LogMessage::LogLine(__FILE__, __LINE__, INTERNAL_FATAL, "HandleUnexpectedSignal reentered\n");
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800290 _exit(1);
291 }
292 handlingUnexpectedSignal = true;
293
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000294 gAborting++; // set before taking any locks
Ian Rogers50b35e22012-10-04 10:09:15 -0700295 MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700296
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700297 bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
298 signal_number == SIGFPE || signal_number == SIGSEGV);
299
Elliott Hughes76160052012-12-12 16:31:20 -0800300 OsInfo os_info;
Elliott Hughes98eedd82012-06-11 17:52:56 -0700301 const char* cmd_line = GetCmdLine();
302 if (cmd_line == NULL) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700303 cmd_line = "<unset>"; // Because no-one called InitLogging.
Elliott Hughes98eedd82012-06-11 17:52:56 -0700304 }
Elliott Hughes289be852012-06-12 13:57:20 -0700305 pid_t tid = GetTid();
306 std::string thread_name(GetThreadName(tid));
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700307 UContext thread_context(raw_context);
Andreas Gampe628a61a2015-01-07 22:08:35 -0800308 Backtrace thread_backtrace(raw_context);
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700309
Elliott Hughes457005c2012-04-16 13:54:25 -0700310 LOG(INTERNAL_FATAL) << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"
311 << StringPrintf("Fatal signal %d (%s), code %d (%s)",
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700312 signal_number, GetSignalName(signal_number),
Elliott Hughes457005c2012-04-16 13:54:25 -0700313 info->si_code,
314 GetSignalCodeName(signal_number, info->si_code))
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700315 << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << "\n"
Elliott Hughes76160052012-12-12 16:31:20 -0800316 << "OS: " << Dumpable<OsInfo>(os_info) << "\n"
Elliott Hughes98eedd82012-06-11 17:52:56 -0700317 << "Cmdline: " << cmd_line << "\n"
Elliott Hughes289be852012-06-12 13:57:20 -0700318 << "Thread: " << tid << " \"" << thread_name << "\"\n"
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700319 << "Registers:\n" << Dumpable<UContext>(thread_context) << "\n"
320 << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace);
Mathieu Chartier15d34022014-02-26 17:16:38 -0800321 Runtime* runtime = Runtime::Current();
322 if (runtime != nullptr) {
323 gc::Heap* heap = runtime->GetHeap();
324 LOG(INTERNAL_FATAL) << "Fault message: " << runtime->GetFaultMessage();
Mathieu Chartierc2f4d022014-03-03 16:11:42 -0800325 if (kDumpHeapObjectOnSigsevg && heap != nullptr && info != nullptr) {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800326 LOG(INTERNAL_FATAL) << "Dump heap object at fault address: ";
327 heap->DumpObject(LOG(INTERNAL_FATAL), reinterpret_cast<mirror::Object*>(info->si_addr));
328 }
329 }
Elliott Hughes4909ba42012-06-14 13:33:49 -0700330 if (getenv("debug_db_uid") != NULL || getenv("art_wait_for_gdb_on_crash") != NULL) {
Elliott Hughes2554cb92012-04-18 17:19:26 -0700331 LOG(INTERNAL_FATAL) << "********************************************************\n"
Elliott Hughes289be852012-06-12 13:57:20 -0700332 << "* Process " << getpid() << " thread " << tid << " \"" << thread_name << "\""
333 << " has been suspended while crashing.\n"
334 << "* Attach gdb:\n"
335 << "* gdb -p " << tid << "\n"
Elliott Hughes2554cb92012-04-18 17:19:26 -0700336 << "********************************************************\n";
337 // Wait for debugger to attach.
338 while (true) {
339 }
Elliott Hughes457005c2012-04-16 13:54:25 -0700340 }
Ian Rogersc5f17732014-06-05 20:48:42 -0700341#ifdef __linux__
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700342 // Remove our signal handler for this signal...
343 struct sigaction action;
344 memset(&action, 0, sizeof(action));
345 sigemptyset(&action.sa_mask);
346 action.sa_handler = SIG_DFL;
347 sigaction(signal_number, &action, NULL);
348 // ...and re-raise so we die with the appropriate status.
349 kill(getpid(), signal_number);
Ian Rogersc5f17732014-06-05 20:48:42 -0700350#else
351 exit(EXIT_FAILURE);
352#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700353}
354
Elliott Hughes457005c2012-04-16 13:54:25 -0700355void Runtime::InitPlatformSignalHandlers() {
356 // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
357 struct sigaction action;
358 memset(&action, 0, sizeof(action));
359 sigemptyset(&action.sa_mask);
360 action.sa_sigaction = HandleUnexpectedSignal;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700361 // Use the three-argument sa_sigaction handler.
362 action.sa_flags |= SA_SIGINFO;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700363 // Use the alternate signal stack so we can catch stack overflows.
364 action.sa_flags |= SA_ONSTACK;
Elliott Hughes457005c2012-04-16 13:54:25 -0700365
366 int rc = 0;
Elliott Hughes457005c2012-04-16 13:54:25 -0700367 rc += sigaction(SIGABRT, &action, NULL);
368 rc += sigaction(SIGBUS, &action, NULL);
369 rc += sigaction(SIGFPE, &action, NULL);
Elliott Hughes058a6de2012-05-24 19:13:02 -0700370 rc += sigaction(SIGILL, &action, NULL);
371 rc += sigaction(SIGPIPE, &action, NULL);
372 rc += sigaction(SIGSEGV, &action, NULL);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700373#if defined(SIGSTKFLT)
Elliott Hughes457005c2012-04-16 13:54:25 -0700374 rc += sigaction(SIGSTKFLT, &action, NULL);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700375#endif
Elliott Hughes058a6de2012-05-24 19:13:02 -0700376 rc += sigaction(SIGTRAP, &action, NULL);
Elliott Hughes457005c2012-04-16 13:54:25 -0700377 CHECK_EQ(rc, 0);
378}
379
Elliott Hughesffe67362011-07-17 12:09:27 -0700380} // namespace art