blob: 73ac03463356c583f942fb38a42e8bc5baf1091b [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080025#include "base/mutex.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080026#include "base/stringprintf.h"
Ian Rogers50b35e22012-10-04 10:09:15 -070027#include "thread.h"
Elliott Hughes46e251b2012-05-22 15:10:45 -070028#include "utils.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070029
30namespace art {
31
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070032struct Backtrace {
33 void Dump(std::ostream& os) {
Elliott Hughes46e251b2012-05-22 15:10:45 -070034 DumpNativeStack(os, GetTid(), "\t", true);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070035 }
36};
37
Elliott Hughes76160052012-12-12 16:31:20 -080038struct OsInfo {
Elliott Hughes058a6de2012-05-24 19:13:02 -070039 void Dump(std::ostream& os) {
40 utsname info;
41 uname(&info);
42 // Linux 2.6.38.8-gg784 (x86_64)
43 // Darwin 11.4.0 (x86_64)
44 os << info.sysname << " " << info.release << " (" << info.machine << ")";
45 }
46};
47
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070048static const char* GetSignalName(int signal_number) {
49 switch (signal_number) {
50 case SIGABRT: return "SIGABRT";
51 case SIGBUS: return "SIGBUS";
52 case SIGFPE: return "SIGFPE";
53 case SIGILL: return "SIGILL";
54 case SIGPIPE: return "SIGPIPE";
55 case SIGSEGV: return "SIGSEGV";
Elliott Hughes833770b2012-05-01 15:41:03 -070056#if defined(SIGSTKFLT)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070057 case SIGSTKFLT: return "SIGSTKFLT";
58#endif
59 case SIGTRAP: return "SIGTRAP";
60 }
61 return "??";
Elliott Hughesffe67362011-07-17 12:09:27 -070062}
63
Elliott Hughes457005c2012-04-16 13:54:25 -070064static const char* GetSignalCodeName(int signal_number, int signal_code) {
65 // Try the signal-specific codes...
66 switch (signal_number) {
67 case SIGILL:
68 switch (signal_code) {
69 case ILL_ILLOPC: return "ILL_ILLOPC";
70 case ILL_ILLOPN: return "ILL_ILLOPN";
71 case ILL_ILLADR: return "ILL_ILLADR";
72 case ILL_ILLTRP: return "ILL_ILLTRP";
73 case ILL_PRVOPC: return "ILL_PRVOPC";
74 case ILL_PRVREG: return "ILL_PRVREG";
75 case ILL_COPROC: return "ILL_COPROC";
76 case ILL_BADSTK: return "ILL_BADSTK";
77 }
78 break;
79 case SIGBUS:
80 switch (signal_code) {
81 case BUS_ADRALN: return "BUS_ADRALN";
82 case BUS_ADRERR: return "BUS_ADRERR";
83 case BUS_OBJERR: return "BUS_OBJERR";
84 }
85 break;
86 case SIGFPE:
87 switch (signal_code) {
88 case FPE_INTDIV: return "FPE_INTDIV";
89 case FPE_INTOVF: return "FPE_INTOVF";
90 case FPE_FLTDIV: return "FPE_FLTDIV";
91 case FPE_FLTOVF: return "FPE_FLTOVF";
92 case FPE_FLTUND: return "FPE_FLTUND";
93 case FPE_FLTRES: return "FPE_FLTRES";
94 case FPE_FLTINV: return "FPE_FLTINV";
95 case FPE_FLTSUB: return "FPE_FLTSUB";
96 }
97 break;
98 case SIGSEGV:
99 switch (signal_code) {
100 case SEGV_MAPERR: return "SEGV_MAPERR";
101 case SEGV_ACCERR: return "SEGV_ACCERR";
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";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700114#if defined(SI_KERNEL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700115 case SI_KERNEL: return "SI_KERNEL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700116#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700117 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";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700121#if defined(SI_SIGIO)
Elliott Hughes457005c2012-04-16 13:54:25 -0700122 case SI_SIGIO: return "SI_SIGIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700123#endif
124#if defined(SI_TKILL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700125 case SI_TKILL: return "SI_TKILL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700126#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700127 }
128 // Then give up...
129 return "?";
130}
131
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700132struct UContext {
Elliott Hughes74847412012-06-20 18:10:21 -0700133 explicit UContext(void* raw_context) : context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {}
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700134
135 void Dump(std::ostream& os) {
136 // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800137#if defined(__APPLE__) && defined(__i386__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700138 DumpRegister32(os, "eax", context->__ss.__eax);
139 DumpRegister32(os, "ebx", context->__ss.__ebx);
140 DumpRegister32(os, "ecx", context->__ss.__ecx);
141 DumpRegister32(os, "edx", context->__ss.__edx);
142 os << '\n';
143
144 DumpRegister32(os, "edi", context->__ss.__edi);
145 DumpRegister32(os, "esi", context->__ss.__esi);
146 DumpRegister32(os, "ebp", context->__ss.__ebp);
147 DumpRegister32(os, "esp", context->__ss.__esp);
148 os << '\n';
149
150 DumpRegister32(os, "eip", context->__ss.__eip);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700151 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700152 DumpRegister32(os, "eflags", context->__ss.__eflags);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700153 DumpX86Flags(os, context->__ss.__eflags);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700154 os << '\n';
155
156 DumpRegister32(os, "cs", context->__ss.__cs);
157 DumpRegister32(os, "ds", context->__ss.__ds);
158 DumpRegister32(os, "es", context->__ss.__es);
159 DumpRegister32(os, "fs", context->__ss.__fs);
160 os << '\n';
161 DumpRegister32(os, "gs", context->__ss.__gs);
162 DumpRegister32(os, "ss", context->__ss.__ss);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800163#elif defined(__linux__) && defined(__i386__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700164 DumpRegister32(os, "eax", context.gregs[REG_EAX]);
165 DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
166 DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
167 DumpRegister32(os, "edx", context.gregs[REG_EDX]);
168 os << '\n';
169
170 DumpRegister32(os, "edi", context.gregs[REG_EDI]);
171 DumpRegister32(os, "esi", context.gregs[REG_ESI]);
172 DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
173 DumpRegister32(os, "esp", context.gregs[REG_ESP]);
174 os << '\n';
175
176 DumpRegister32(os, "eip", context.gregs[REG_EIP]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700177 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700178 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700179 DumpX86Flags(os, context.gregs[REG_EFL]);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700180 os << '\n';
181
182 DumpRegister32(os, "cs", context.gregs[REG_CS]);
183 DumpRegister32(os, "ds", context.gregs[REG_DS]);
184 DumpRegister32(os, "es", context.gregs[REG_ES]);
185 DumpRegister32(os, "fs", context.gregs[REG_FS]);
186 os << '\n';
187 DumpRegister32(os, "gs", context.gregs[REG_GS]);
188 DumpRegister32(os, "ss", context.gregs[REG_SS]);
Dmitry Petrochenko611c2c32014-02-10 14:48:12 +0700189#elif defined(__linux__) && defined(__x86_64__)
190 DumpRegister64(os, "rax", context.gregs[REG_RAX]);
191 DumpRegister64(os, "rbx", context.gregs[REG_RBX]);
192 DumpRegister64(os, "rcx", context.gregs[REG_RCX]);
193 DumpRegister64(os, "rdx", context.gregs[REG_RDX]);
194 os << '\n';
195
196 DumpRegister64(os, "rdi", context.gregs[REG_RDI]);
197 DumpRegister64(os, "rsi", context.gregs[REG_RSI]);
198 DumpRegister64(os, "rbp", context.gregs[REG_RBP]);
199 DumpRegister64(os, "rsp", context.gregs[REG_RSP]);
200 os << '\n';
201
202 DumpRegister64(os, "r8 ", context.gregs[REG_R8]);
203 DumpRegister64(os, "r9 ", context.gregs[REG_R9]);
204 DumpRegister64(os, "r10", context.gregs[REG_R10]);
205 DumpRegister64(os, "r11", context.gregs[REG_R11]);
206 os << '\n';
207
208 DumpRegister64(os, "r12", context.gregs[REG_R12]);
209 DumpRegister64(os, "r13", context.gregs[REG_R13]);
210 DumpRegister64(os, "r14", context.gregs[REG_R14]);
211 DumpRegister64(os, "r15", context.gregs[REG_R15]);
212 os << '\n';
213
214 DumpRegister64(os, "rip", context.gregs[REG_RIP]);
215 os << " ";
216 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
217 DumpX86Flags(os, context.gregs[REG_EFL]);
218 os << '\n';
219
220 DumpRegister32(os, "cs", (context.gregs[REG_CSGSFS]) & 0x0FFFF);
221 DumpRegister32(os, "gs", (context.gregs[REG_CSGSFS] >> 16) & 0x0FFFF);
222 DumpRegister32(os, "fs", (context.gregs[REG_CSGSFS] >> 32) & 0x0FFFF);
223 os << '\n';
Ian Rogersef7d42f2014-01-06 12:55:46 -0800224#else
225 os << "Unknown architecture/word size/OS in ucontext dump";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700226#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700227 }
228
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700229 void DumpRegister32(std::ostream& os, const char* name, uint32_t value) {
230 os << StringPrintf(" %6s: 0x%08x", name, value);
231 }
232
Dmitry Petrochenko611c2c32014-02-10 14:48:12 +0700233 void DumpRegister64(std::ostream& os, const char* name, uint64_t value) {
234 os << StringPrintf(" %6s: 0x%016" PRIx64, name, value);
235 }
236
Elliott Hughes46e251b2012-05-22 15:10:45 -0700237 void DumpX86Flags(std::ostream& os, uint32_t flags) {
238 os << " [";
239 if ((flags & (1 << 0)) != 0) {
240 os << " CF";
241 }
242 if ((flags & (1 << 2)) != 0) {
243 os << " PF";
244 }
245 if ((flags & (1 << 4)) != 0) {
246 os << " AF";
247 }
248 if ((flags & (1 << 6)) != 0) {
249 os << " ZF";
250 }
251 if ((flags & (1 << 7)) != 0) {
252 os << " SF";
253 }
254 if ((flags & (1 << 8)) != 0) {
255 os << " TF";
256 }
257 if ((flags & (1 << 9)) != 0) {
258 os << " IF";
259 }
260 if ((flags & (1 << 10)) != 0) {
261 os << " DF";
262 }
263 if ((flags & (1 << 11)) != 0) {
264 os << " OF";
265 }
266 os << " ]";
267 }
268
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700269 mcontext_t& context;
270};
271
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800272void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
273 static bool handlingUnexpectedSignal = false;
274 if (handlingUnexpectedSignal) {
275 LogMessageData data(__FILE__, __LINE__, INTERNAL_FATAL, -1);
276 LogMessage::LogLine(data, "HandleUnexpectedSignal reentered\n");
277 _exit(1);
278 }
279 handlingUnexpectedSignal = true;
280
Ian Rogersf08e4732013-04-09 09:45:49 -0700281 gAborting++; // set before taking any locks
Ian Rogers50b35e22012-10-04 10:09:15 -0700282 MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700283
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700284 bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
285 signal_number == SIGFPE || signal_number == SIGSEGV);
286
Elliott Hughes76160052012-12-12 16:31:20 -0800287 OsInfo os_info;
Elliott Hughes98eedd82012-06-11 17:52:56 -0700288 const char* cmd_line = GetCmdLine();
289 if (cmd_line == NULL) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700290 cmd_line = "<unset>"; // Because no-one called InitLogging.
Elliott Hughes98eedd82012-06-11 17:52:56 -0700291 }
Elliott Hughes289be852012-06-12 13:57:20 -0700292 pid_t tid = GetTid();
293 std::string thread_name(GetThreadName(tid));
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700294 UContext thread_context(raw_context);
295 Backtrace thread_backtrace;
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700296
Elliott Hughes457005c2012-04-16 13:54:25 -0700297 LOG(INTERNAL_FATAL) << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"
298 << StringPrintf("Fatal signal %d (%s), code %d (%s)",
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700299 signal_number, GetSignalName(signal_number),
Elliott Hughes457005c2012-04-16 13:54:25 -0700300 info->si_code,
301 GetSignalCodeName(signal_number, info->si_code))
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700302 << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << "\n"
Elliott Hughes76160052012-12-12 16:31:20 -0800303 << "OS: " << Dumpable<OsInfo>(os_info) << "\n"
Elliott Hughes98eedd82012-06-11 17:52:56 -0700304 << "Cmdline: " << cmd_line << "\n"
Elliott Hughes289be852012-06-12 13:57:20 -0700305 << "Thread: " << tid << " \"" << thread_name << "\"\n"
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700306 << "Registers:\n" << Dumpable<UContext>(thread_context) << "\n"
307 << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace);
Elliott Hughes457005c2012-04-16 13:54:25 -0700308
Elliott Hughes4909ba42012-06-14 13:33:49 -0700309 if (getenv("debug_db_uid") != NULL || getenv("art_wait_for_gdb_on_crash") != NULL) {
Elliott Hughes2554cb92012-04-18 17:19:26 -0700310 LOG(INTERNAL_FATAL) << "********************************************************\n"
Elliott Hughes289be852012-06-12 13:57:20 -0700311 << "* Process " << getpid() << " thread " << tid << " \"" << thread_name << "\""
312 << " has been suspended while crashing.\n"
313 << "* Attach gdb:\n"
314 << "* gdb -p " << tid << "\n"
Elliott Hughes2554cb92012-04-18 17:19:26 -0700315 << "********************************************************\n";
316 // Wait for debugger to attach.
317 while (true) {
318 }
Elliott Hughes457005c2012-04-16 13:54:25 -0700319 }
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700320
321 // Remove our signal handler for this signal...
322 struct sigaction action;
323 memset(&action, 0, sizeof(action));
324 sigemptyset(&action.sa_mask);
325 action.sa_handler = SIG_DFL;
326 sigaction(signal_number, &action, NULL);
327 // ...and re-raise so we die with the appropriate status.
328 kill(getpid(), signal_number);
Elliott Hughes457005c2012-04-16 13:54:25 -0700329}
330
Elliott Hughes457005c2012-04-16 13:54:25 -0700331void Runtime::InitPlatformSignalHandlers() {
332 // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
333 struct sigaction action;
334 memset(&action, 0, sizeof(action));
335 sigemptyset(&action.sa_mask);
336 action.sa_sigaction = HandleUnexpectedSignal;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700337 // Use the three-argument sa_sigaction handler.
338 action.sa_flags |= SA_SIGINFO;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700339 // Use the alternate signal stack so we can catch stack overflows.
340 action.sa_flags |= SA_ONSTACK;
Elliott Hughes457005c2012-04-16 13:54:25 -0700341
342 int rc = 0;
Elliott Hughes457005c2012-04-16 13:54:25 -0700343 rc += sigaction(SIGABRT, &action, NULL);
344 rc += sigaction(SIGBUS, &action, NULL);
345 rc += sigaction(SIGFPE, &action, NULL);
Elliott Hughes058a6de2012-05-24 19:13:02 -0700346 rc += sigaction(SIGILL, &action, NULL);
347 rc += sigaction(SIGPIPE, &action, NULL);
348 rc += sigaction(SIGSEGV, &action, NULL);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700349#if defined(SIGSTKFLT)
Elliott Hughes457005c2012-04-16 13:54:25 -0700350 rc += sigaction(SIGSTKFLT, &action, NULL);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700351#endif
Elliott Hughes058a6de2012-05-24 19:13:02 -0700352 rc += sigaction(SIGTRAP, &action, NULL);
Elliott Hughes457005c2012-04-16 13:54:25 -0700353 CHECK_EQ(rc, 0);
354}
355
Elliott Hughesffe67362011-07-17 12:09:27 -0700356} // namespace art