blob: a84dfc9ae8a3865bbaa3c0b7af444a0bbd5e2b45 [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
23#include "logging.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070024#include "stringprintf.h"
Elliott Hughes46e251b2012-05-22 15:10:45 -070025#include "utils.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070026
27namespace art {
28
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070029struct Backtrace {
30 void Dump(std::ostream& os) {
Elliott Hughes46e251b2012-05-22 15:10:45 -070031 DumpNativeStack(os, GetTid(), "\t", true);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070032 }
33};
34
Elliott Hughes058a6de2012-05-24 19:13:02 -070035struct OS {
36 void Dump(std::ostream& os) {
37 utsname info;
38 uname(&info);
39 // Linux 2.6.38.8-gg784 (x86_64)
40 // Darwin 11.4.0 (x86_64)
41 os << info.sysname << " " << info.release << " (" << info.machine << ")";
42 }
43};
44
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070045static const char* GetSignalName(int signal_number) {
46 switch (signal_number) {
47 case SIGABRT: return "SIGABRT";
48 case SIGBUS: return "SIGBUS";
49 case SIGFPE: return "SIGFPE";
50 case SIGILL: return "SIGILL";
51 case SIGPIPE: return "SIGPIPE";
52 case SIGSEGV: return "SIGSEGV";
Elliott Hughes833770b2012-05-01 15:41:03 -070053#if defined(SIGSTKFLT)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070054 case SIGSTKFLT: return "SIGSTKFLT";
55#endif
56 case SIGTRAP: return "SIGTRAP";
57 }
58 return "??";
Elliott Hughesffe67362011-07-17 12:09:27 -070059}
60
Elliott Hughes457005c2012-04-16 13:54:25 -070061static const char* GetSignalCodeName(int signal_number, int signal_code) {
62 // Try the signal-specific codes...
63 switch (signal_number) {
64 case SIGILL:
65 switch (signal_code) {
66 case ILL_ILLOPC: return "ILL_ILLOPC";
67 case ILL_ILLOPN: return "ILL_ILLOPN";
68 case ILL_ILLADR: return "ILL_ILLADR";
69 case ILL_ILLTRP: return "ILL_ILLTRP";
70 case ILL_PRVOPC: return "ILL_PRVOPC";
71 case ILL_PRVREG: return "ILL_PRVREG";
72 case ILL_COPROC: return "ILL_COPROC";
73 case ILL_BADSTK: return "ILL_BADSTK";
74 }
75 break;
76 case SIGBUS:
77 switch (signal_code) {
78 case BUS_ADRALN: return "BUS_ADRALN";
79 case BUS_ADRERR: return "BUS_ADRERR";
80 case BUS_OBJERR: return "BUS_OBJERR";
81 }
82 break;
83 case SIGFPE:
84 switch (signal_code) {
85 case FPE_INTDIV: return "FPE_INTDIV";
86 case FPE_INTOVF: return "FPE_INTOVF";
87 case FPE_FLTDIV: return "FPE_FLTDIV";
88 case FPE_FLTOVF: return "FPE_FLTOVF";
89 case FPE_FLTUND: return "FPE_FLTUND";
90 case FPE_FLTRES: return "FPE_FLTRES";
91 case FPE_FLTINV: return "FPE_FLTINV";
92 case FPE_FLTSUB: return "FPE_FLTSUB";
93 }
94 break;
95 case SIGSEGV:
96 switch (signal_code) {
97 case SEGV_MAPERR: return "SEGV_MAPERR";
98 case SEGV_ACCERR: return "SEGV_ACCERR";
99 }
100 break;
101 case SIGTRAP:
102 switch (signal_code) {
103 case TRAP_BRKPT: return "TRAP_BRKPT";
104 case TRAP_TRACE: return "TRAP_TRACE";
105 }
106 break;
107 }
108 // Then the other codes...
109 switch (signal_code) {
110 case SI_USER: return "SI_USER";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700111#if defined(SI_KERNEL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700112 case SI_KERNEL: return "SI_KERNEL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700113#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700114 case SI_QUEUE: return "SI_QUEUE";
115 case SI_TIMER: return "SI_TIMER";
116 case SI_MESGQ: return "SI_MESGQ";
117 case SI_ASYNCIO: return "SI_ASYNCIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700118#if defined(SI_SIGIO)
Elliott Hughes457005c2012-04-16 13:54:25 -0700119 case SI_SIGIO: return "SI_SIGIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700120#endif
121#if defined(SI_TKILL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700122 case SI_TKILL: return "SI_TKILL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700123#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700124 }
125 // Then give up...
126 return "?";
127}
128
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700129struct UContext {
Elliott Hughes74847412012-06-20 18:10:21 -0700130 explicit UContext(void* raw_context) : context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {}
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700131
132 void Dump(std::ostream& os) {
133 // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets).
134#if defined(__APPLE__)
135 DumpRegister32(os, "eax", context->__ss.__eax);
136 DumpRegister32(os, "ebx", context->__ss.__ebx);
137 DumpRegister32(os, "ecx", context->__ss.__ecx);
138 DumpRegister32(os, "edx", context->__ss.__edx);
139 os << '\n';
140
141 DumpRegister32(os, "edi", context->__ss.__edi);
142 DumpRegister32(os, "esi", context->__ss.__esi);
143 DumpRegister32(os, "ebp", context->__ss.__ebp);
144 DumpRegister32(os, "esp", context->__ss.__esp);
145 os << '\n';
146
147 DumpRegister32(os, "eip", context->__ss.__eip);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700148 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700149 DumpRegister32(os, "eflags", context->__ss.__eflags);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700150 DumpX86Flags(os, context->__ss.__eflags);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700151 os << '\n';
152
153 DumpRegister32(os, "cs", context->__ss.__cs);
154 DumpRegister32(os, "ds", context->__ss.__ds);
155 DumpRegister32(os, "es", context->__ss.__es);
156 DumpRegister32(os, "fs", context->__ss.__fs);
157 os << '\n';
158 DumpRegister32(os, "gs", context->__ss.__gs);
159 DumpRegister32(os, "ss", context->__ss.__ss);
160#else
161 DumpRegister32(os, "eax", context.gregs[REG_EAX]);
162 DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
163 DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
164 DumpRegister32(os, "edx", context.gregs[REG_EDX]);
165 os << '\n';
166
167 DumpRegister32(os, "edi", context.gregs[REG_EDI]);
168 DumpRegister32(os, "esi", context.gregs[REG_ESI]);
169 DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
170 DumpRegister32(os, "esp", context.gregs[REG_ESP]);
171 os << '\n';
172
173 DumpRegister32(os, "eip", context.gregs[REG_EIP]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700174 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700175 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700176 DumpX86Flags(os, context.gregs[REG_EFL]);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700177 os << '\n';
178
179 DumpRegister32(os, "cs", context.gregs[REG_CS]);
180 DumpRegister32(os, "ds", context.gregs[REG_DS]);
181 DumpRegister32(os, "es", context.gregs[REG_ES]);
182 DumpRegister32(os, "fs", context.gregs[REG_FS]);
183 os << '\n';
184 DumpRegister32(os, "gs", context.gregs[REG_GS]);
185 DumpRegister32(os, "ss", context.gregs[REG_SS]);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700186#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700187 }
188
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700189 void DumpRegister32(std::ostream& os, const char* name, uint32_t value) {
190 os << StringPrintf(" %6s: 0x%08x", name, value);
191 }
192
Elliott Hughes46e251b2012-05-22 15:10:45 -0700193 void DumpX86Flags(std::ostream& os, uint32_t flags) {
194 os << " [";
195 if ((flags & (1 << 0)) != 0) {
196 os << " CF";
197 }
198 if ((flags & (1 << 2)) != 0) {
199 os << " PF";
200 }
201 if ((flags & (1 << 4)) != 0) {
202 os << " AF";
203 }
204 if ((flags & (1 << 6)) != 0) {
205 os << " ZF";
206 }
207 if ((flags & (1 << 7)) != 0) {
208 os << " SF";
209 }
210 if ((flags & (1 << 8)) != 0) {
211 os << " TF";
212 }
213 if ((flags & (1 << 9)) != 0) {
214 os << " IF";
215 }
216 if ((flags & (1 << 10)) != 0) {
217 os << " DF";
218 }
219 if ((flags & (1 << 11)) != 0) {
220 os << " OF";
221 }
222 os << " ]";
223 }
224
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700225 mcontext_t& context;
226};
227
228static void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700229 static Mutex unexpected_signal_lock("unexpected signal lock");
230 MutexLock mu(unexpected_signal_lock);
231
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700232 bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
233 signal_number == SIGFPE || signal_number == SIGSEGV);
234
Elliott Hughes058a6de2012-05-24 19:13:02 -0700235 OS os_info;
Elliott Hughes98eedd82012-06-11 17:52:56 -0700236 const char* cmd_line = GetCmdLine();
237 if (cmd_line == NULL) {
Elliott Hughes289be852012-06-12 13:57:20 -0700238 cmd_line = "<unset>"; // Because no-one called InitLogging.
Elliott Hughes98eedd82012-06-11 17:52:56 -0700239 }
Elliott Hughes289be852012-06-12 13:57:20 -0700240 pid_t tid = GetTid();
241 std::string thread_name(GetThreadName(tid));
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700242 UContext thread_context(raw_context);
243 Backtrace thread_backtrace;
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700244
Elliott Hughes457005c2012-04-16 13:54:25 -0700245 LOG(INTERNAL_FATAL) << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"
246 << StringPrintf("Fatal signal %d (%s), code %d (%s)",
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700247 signal_number, GetSignalName(signal_number),
Elliott Hughes457005c2012-04-16 13:54:25 -0700248 info->si_code,
249 GetSignalCodeName(signal_number, info->si_code))
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700250 << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << "\n"
Elliott Hughes058a6de2012-05-24 19:13:02 -0700251 << "OS: " << Dumpable<OS>(os_info) << "\n"
Elliott Hughes98eedd82012-06-11 17:52:56 -0700252 << "Cmdline: " << cmd_line << "\n"
Elliott Hughes289be852012-06-12 13:57:20 -0700253 << "Thread: " << tid << " \"" << thread_name << "\"\n"
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700254 << "Registers:\n" << Dumpable<UContext>(thread_context) << "\n"
255 << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace);
Elliott Hughes457005c2012-04-16 13:54:25 -0700256
Elliott Hughes4909ba42012-06-14 13:33:49 -0700257 if (getenv("debug_db_uid") != NULL || getenv("art_wait_for_gdb_on_crash") != NULL) {
Elliott Hughes2554cb92012-04-18 17:19:26 -0700258 LOG(INTERNAL_FATAL) << "********************************************************\n"
Elliott Hughes289be852012-06-12 13:57:20 -0700259 << "* Process " << getpid() << " thread " << tid << " \"" << thread_name << "\""
260 << " has been suspended while crashing.\n"
261 << "* Attach gdb:\n"
262 << "* gdb -p " << tid << "\n"
Elliott Hughes2554cb92012-04-18 17:19:26 -0700263 << "********************************************************\n";
264 // Wait for debugger to attach.
265 while (true) {
266 }
Elliott Hughes457005c2012-04-16 13:54:25 -0700267 }
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700268
269 // Remove our signal handler for this signal...
270 struct sigaction action;
271 memset(&action, 0, sizeof(action));
272 sigemptyset(&action.sa_mask);
273 action.sa_handler = SIG_DFL;
274 sigaction(signal_number, &action, NULL);
275 // ...and re-raise so we die with the appropriate status.
276 kill(getpid(), signal_number);
Elliott Hughes457005c2012-04-16 13:54:25 -0700277}
278
Elliott Hughes457005c2012-04-16 13:54:25 -0700279void Runtime::InitPlatformSignalHandlers() {
280 // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
281 struct sigaction action;
282 memset(&action, 0, sizeof(action));
283 sigemptyset(&action.sa_mask);
284 action.sa_sigaction = HandleUnexpectedSignal;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700285 // Use the three-argument sa_sigaction handler.
286 action.sa_flags |= SA_SIGINFO;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700287 // Use the alternate signal stack so we can catch stack overflows.
288 action.sa_flags |= SA_ONSTACK;
Elliott Hughes457005c2012-04-16 13:54:25 -0700289
290 int rc = 0;
Elliott Hughes457005c2012-04-16 13:54:25 -0700291 rc += sigaction(SIGABRT, &action, NULL);
292 rc += sigaction(SIGBUS, &action, NULL);
293 rc += sigaction(SIGFPE, &action, NULL);
Elliott Hughes058a6de2012-05-24 19:13:02 -0700294 rc += sigaction(SIGILL, &action, NULL);
295 rc += sigaction(SIGPIPE, &action, NULL);
296 rc += sigaction(SIGSEGV, &action, NULL);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700297#if defined(SIGSTKFLT)
Elliott Hughes457005c2012-04-16 13:54:25 -0700298 rc += sigaction(SIGSTKFLT, &action, NULL);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700299#endif
Elliott Hughes058a6de2012-05-24 19:13:02 -0700300 rc += sigaction(SIGTRAP, &action, NULL);
Elliott Hughes457005c2012-04-16 13:54:25 -0700301 CHECK_EQ(rc, 0);
302}
303
Elliott Hughesffe67362011-07-17 12:09:27 -0700304} // namespace art