blob: 47b72e9b177c6b53d799291b4ef079079d216f9f [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>
Elliott Hughesffe67362011-07-17 12:09:27 -070022
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080024#include "base/mutex.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080025#include "base/stringprintf.h"
Ian Rogers50b35e22012-10-04 10:09:15 -070026#include "thread.h"
Elliott Hughes46e251b2012-05-22 15:10:45 -070027#include "utils.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070028
29namespace art {
30
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070031struct Backtrace {
32 void Dump(std::ostream& os) {
Elliott Hughes46e251b2012-05-22 15:10:45 -070033 DumpNativeStack(os, GetTid(), "\t", true);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070034 }
35};
36
Elliott Hughes76160052012-12-12 16:31:20 -080037struct OsInfo {
Elliott Hughes058a6de2012-05-24 19:13:02 -070038 void Dump(std::ostream& os) {
39 utsname info;
40 uname(&info);
41 // Linux 2.6.38.8-gg784 (x86_64)
42 // Darwin 11.4.0 (x86_64)
43 os << info.sysname << " " << info.release << " (" << info.machine << ")";
44 }
45};
46
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070047static const char* GetSignalName(int signal_number) {
48 switch (signal_number) {
49 case SIGABRT: return "SIGABRT";
50 case SIGBUS: return "SIGBUS";
51 case SIGFPE: return "SIGFPE";
52 case SIGILL: return "SIGILL";
53 case SIGPIPE: return "SIGPIPE";
54 case SIGSEGV: return "SIGSEGV";
Elliott Hughes833770b2012-05-01 15:41:03 -070055#if defined(SIGSTKFLT)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070056 case SIGSTKFLT: return "SIGSTKFLT";
57#endif
58 case SIGTRAP: return "SIGTRAP";
59 }
60 return "??";
Elliott Hughesffe67362011-07-17 12:09:27 -070061}
62
Elliott Hughes457005c2012-04-16 13:54:25 -070063static const char* GetSignalCodeName(int signal_number, int signal_code) {
64 // Try the signal-specific codes...
65 switch (signal_number) {
66 case SIGILL:
67 switch (signal_code) {
68 case ILL_ILLOPC: return "ILL_ILLOPC";
69 case ILL_ILLOPN: return "ILL_ILLOPN";
70 case ILL_ILLADR: return "ILL_ILLADR";
71 case ILL_ILLTRP: return "ILL_ILLTRP";
72 case ILL_PRVOPC: return "ILL_PRVOPC";
73 case ILL_PRVREG: return "ILL_PRVREG";
74 case ILL_COPROC: return "ILL_COPROC";
75 case ILL_BADSTK: return "ILL_BADSTK";
76 }
77 break;
78 case SIGBUS:
79 switch (signal_code) {
80 case BUS_ADRALN: return "BUS_ADRALN";
81 case BUS_ADRERR: return "BUS_ADRERR";
82 case BUS_OBJERR: return "BUS_OBJERR";
83 }
84 break;
85 case SIGFPE:
86 switch (signal_code) {
87 case FPE_INTDIV: return "FPE_INTDIV";
88 case FPE_INTOVF: return "FPE_INTOVF";
89 case FPE_FLTDIV: return "FPE_FLTDIV";
90 case FPE_FLTOVF: return "FPE_FLTOVF";
91 case FPE_FLTUND: return "FPE_FLTUND";
92 case FPE_FLTRES: return "FPE_FLTRES";
93 case FPE_FLTINV: return "FPE_FLTINV";
94 case FPE_FLTSUB: return "FPE_FLTSUB";
95 }
96 break;
97 case SIGSEGV:
98 switch (signal_code) {
99 case SEGV_MAPERR: return "SEGV_MAPERR";
100 case SEGV_ACCERR: return "SEGV_ACCERR";
101 }
102 break;
103 case SIGTRAP:
104 switch (signal_code) {
105 case TRAP_BRKPT: return "TRAP_BRKPT";
106 case TRAP_TRACE: return "TRAP_TRACE";
107 }
108 break;
109 }
110 // Then the other codes...
111 switch (signal_code) {
112 case SI_USER: return "SI_USER";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700113#if defined(SI_KERNEL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700114 case SI_KERNEL: return "SI_KERNEL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700115#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700116 case SI_QUEUE: return "SI_QUEUE";
117 case SI_TIMER: return "SI_TIMER";
118 case SI_MESGQ: return "SI_MESGQ";
119 case SI_ASYNCIO: return "SI_ASYNCIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700120#if defined(SI_SIGIO)
Elliott Hughes457005c2012-04-16 13:54:25 -0700121 case SI_SIGIO: return "SI_SIGIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700122#endif
123#if defined(SI_TKILL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700124 case SI_TKILL: return "SI_TKILL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700125#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700126 }
127 // Then give up...
128 return "?";
129}
130
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700131struct UContext {
Elliott Hughes74847412012-06-20 18:10:21 -0700132 explicit UContext(void* raw_context) : context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {}
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700133
134 void Dump(std::ostream& os) {
135 // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets).
136#if defined(__APPLE__)
137 DumpRegister32(os, "eax", context->__ss.__eax);
138 DumpRegister32(os, "ebx", context->__ss.__ebx);
139 DumpRegister32(os, "ecx", context->__ss.__ecx);
140 DumpRegister32(os, "edx", context->__ss.__edx);
141 os << '\n';
142
143 DumpRegister32(os, "edi", context->__ss.__edi);
144 DumpRegister32(os, "esi", context->__ss.__esi);
145 DumpRegister32(os, "ebp", context->__ss.__ebp);
146 DumpRegister32(os, "esp", context->__ss.__esp);
147 os << '\n';
148
149 DumpRegister32(os, "eip", context->__ss.__eip);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700150 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700151 DumpRegister32(os, "eflags", context->__ss.__eflags);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700152 DumpX86Flags(os, context->__ss.__eflags);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700153 os << '\n';
154
155 DumpRegister32(os, "cs", context->__ss.__cs);
156 DumpRegister32(os, "ds", context->__ss.__ds);
157 DumpRegister32(os, "es", context->__ss.__es);
158 DumpRegister32(os, "fs", context->__ss.__fs);
159 os << '\n';
160 DumpRegister32(os, "gs", context->__ss.__gs);
161 DumpRegister32(os, "ss", context->__ss.__ss);
162#else
163 DumpRegister32(os, "eax", context.gregs[REG_EAX]);
164 DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
165 DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
166 DumpRegister32(os, "edx", context.gregs[REG_EDX]);
167 os << '\n';
168
169 DumpRegister32(os, "edi", context.gregs[REG_EDI]);
170 DumpRegister32(os, "esi", context.gregs[REG_ESI]);
171 DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
172 DumpRegister32(os, "esp", context.gregs[REG_ESP]);
173 os << '\n';
174
175 DumpRegister32(os, "eip", context.gregs[REG_EIP]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700176 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700177 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700178 DumpX86Flags(os, context.gregs[REG_EFL]);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700179 os << '\n';
180
181 DumpRegister32(os, "cs", context.gregs[REG_CS]);
182 DumpRegister32(os, "ds", context.gregs[REG_DS]);
183 DumpRegister32(os, "es", context.gregs[REG_ES]);
184 DumpRegister32(os, "fs", context.gregs[REG_FS]);
185 os << '\n';
186 DumpRegister32(os, "gs", context.gregs[REG_GS]);
187 DumpRegister32(os, "ss", context.gregs[REG_SS]);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700188#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700189 }
190
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700191 void DumpRegister32(std::ostream& os, const char* name, uint32_t value) {
192 os << StringPrintf(" %6s: 0x%08x", name, value);
193 }
194
Elliott Hughes46e251b2012-05-22 15:10:45 -0700195 void DumpX86Flags(std::ostream& os, uint32_t flags) {
196 os << " [";
197 if ((flags & (1 << 0)) != 0) {
198 os << " CF";
199 }
200 if ((flags & (1 << 2)) != 0) {
201 os << " PF";
202 }
203 if ((flags & (1 << 4)) != 0) {
204 os << " AF";
205 }
206 if ((flags & (1 << 6)) != 0) {
207 os << " ZF";
208 }
209 if ((flags & (1 << 7)) != 0) {
210 os << " SF";
211 }
212 if ((flags & (1 << 8)) != 0) {
213 os << " TF";
214 }
215 if ((flags & (1 << 9)) != 0) {
216 os << " IF";
217 }
218 if ((flags & (1 << 10)) != 0) {
219 os << " DF";
220 }
221 if ((flags & (1 << 11)) != 0) {
222 os << " OF";
223 }
224 os << " ]";
225 }
226
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700227 mcontext_t& context;
228};
229
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800230void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
231 static bool handlingUnexpectedSignal = false;
232 if (handlingUnexpectedSignal) {
233 LogMessageData data(__FILE__, __LINE__, INTERNAL_FATAL, -1);
234 LogMessage::LogLine(data, "HandleUnexpectedSignal reentered\n");
235 _exit(1);
236 }
237 handlingUnexpectedSignal = true;
238
Ian Rogersf08e4732013-04-09 09:45:49 -0700239 gAborting++; // set before taking any locks
Ian Rogers50b35e22012-10-04 10:09:15 -0700240 MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700241
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700242 bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
243 signal_number == SIGFPE || signal_number == SIGSEGV);
244
Elliott Hughes76160052012-12-12 16:31:20 -0800245 OsInfo os_info;
Elliott Hughes98eedd82012-06-11 17:52:56 -0700246 const char* cmd_line = GetCmdLine();
247 if (cmd_line == NULL) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700248 cmd_line = "<unset>"; // Because no-one called InitLogging.
Elliott Hughes98eedd82012-06-11 17:52:56 -0700249 }
Elliott Hughes289be852012-06-12 13:57:20 -0700250 pid_t tid = GetTid();
251 std::string thread_name(GetThreadName(tid));
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700252 UContext thread_context(raw_context);
253 Backtrace thread_backtrace;
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700254
Elliott Hughes457005c2012-04-16 13:54:25 -0700255 LOG(INTERNAL_FATAL) << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"
256 << StringPrintf("Fatal signal %d (%s), code %d (%s)",
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700257 signal_number, GetSignalName(signal_number),
Elliott Hughes457005c2012-04-16 13:54:25 -0700258 info->si_code,
259 GetSignalCodeName(signal_number, info->si_code))
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700260 << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << "\n"
Elliott Hughes76160052012-12-12 16:31:20 -0800261 << "OS: " << Dumpable<OsInfo>(os_info) << "\n"
Elliott Hughes98eedd82012-06-11 17:52:56 -0700262 << "Cmdline: " << cmd_line << "\n"
Elliott Hughes289be852012-06-12 13:57:20 -0700263 << "Thread: " << tid << " \"" << thread_name << "\"\n"
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700264 << "Registers:\n" << Dumpable<UContext>(thread_context) << "\n"
265 << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace);
Elliott Hughes457005c2012-04-16 13:54:25 -0700266
Elliott Hughes4909ba42012-06-14 13:33:49 -0700267 if (getenv("debug_db_uid") != NULL || getenv("art_wait_for_gdb_on_crash") != NULL) {
Elliott Hughes2554cb92012-04-18 17:19:26 -0700268 LOG(INTERNAL_FATAL) << "********************************************************\n"
Elliott Hughes289be852012-06-12 13:57:20 -0700269 << "* Process " << getpid() << " thread " << tid << " \"" << thread_name << "\""
270 << " has been suspended while crashing.\n"
271 << "* Attach gdb:\n"
272 << "* gdb -p " << tid << "\n"
Elliott Hughes2554cb92012-04-18 17:19:26 -0700273 << "********************************************************\n";
274 // Wait for debugger to attach.
275 while (true) {
276 }
Elliott Hughes457005c2012-04-16 13:54:25 -0700277 }
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700278
279 // Remove our signal handler for this signal...
280 struct sigaction action;
281 memset(&action, 0, sizeof(action));
282 sigemptyset(&action.sa_mask);
283 action.sa_handler = SIG_DFL;
284 sigaction(signal_number, &action, NULL);
285 // ...and re-raise so we die with the appropriate status.
286 kill(getpid(), signal_number);
Elliott Hughes457005c2012-04-16 13:54:25 -0700287}
288
Elliott Hughes457005c2012-04-16 13:54:25 -0700289void Runtime::InitPlatformSignalHandlers() {
290 // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
291 struct sigaction action;
292 memset(&action, 0, sizeof(action));
293 sigemptyset(&action.sa_mask);
294 action.sa_sigaction = HandleUnexpectedSignal;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700295 // Use the three-argument sa_sigaction handler.
296 action.sa_flags |= SA_SIGINFO;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700297 // Use the alternate signal stack so we can catch stack overflows.
298 action.sa_flags |= SA_ONSTACK;
Elliott Hughes457005c2012-04-16 13:54:25 -0700299
300 int rc = 0;
Elliott Hughes457005c2012-04-16 13:54:25 -0700301 rc += sigaction(SIGABRT, &action, NULL);
302 rc += sigaction(SIGBUS, &action, NULL);
303 rc += sigaction(SIGFPE, &action, NULL);
Elliott Hughes058a6de2012-05-24 19:13:02 -0700304 rc += sigaction(SIGILL, &action, NULL);
305 rc += sigaction(SIGPIPE, &action, NULL);
306 rc += sigaction(SIGSEGV, &action, NULL);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700307#if defined(SIGSTKFLT)
Elliott Hughes457005c2012-04-16 13:54:25 -0700308 rc += sigaction(SIGSTKFLT, &action, NULL);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700309#endif
Elliott Hughes058a6de2012-05-24 19:13:02 -0700310 rc += sigaction(SIGTRAP, &action, NULL);
Elliott Hughes457005c2012-04-16 13:54:25 -0700311 CHECK_EQ(rc, 0);
312}
313
Elliott Hughesffe67362011-07-17 12:09:27 -0700314} // namespace art