blob: 80a864318be52c4d0bce2145419f61591a3e9204 [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 Hughesffe67362011-07-17 12:09:27 -070021
22#include "logging.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070023#include "stringprintf.h"
Elliott Hughes46e251b2012-05-22 15:10:45 -070024#include "utils.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070025
26namespace art {
27
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070028struct Backtrace {
29 void Dump(std::ostream& os) {
Elliott Hughes46e251b2012-05-22 15:10:45 -070030 DumpNativeStack(os, GetTid(), "\t", true);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070031 }
32};
33
34static const char* GetSignalName(int signal_number) {
35 switch (signal_number) {
36 case SIGABRT: return "SIGABRT";
37 case SIGBUS: return "SIGBUS";
38 case SIGFPE: return "SIGFPE";
39 case SIGILL: return "SIGILL";
40 case SIGPIPE: return "SIGPIPE";
41 case SIGSEGV: return "SIGSEGV";
Elliott Hughes833770b2012-05-01 15:41:03 -070042#if defined(SIGSTKFLT)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070043 case SIGSTKFLT: return "SIGSTKFLT";
44#endif
45 case SIGTRAP: return "SIGTRAP";
46 }
47 return "??";
Elliott Hughesffe67362011-07-17 12:09:27 -070048}
49
Elliott Hughes457005c2012-04-16 13:54:25 -070050static const char* GetSignalCodeName(int signal_number, int signal_code) {
51 // Try the signal-specific codes...
52 switch (signal_number) {
53 case SIGILL:
54 switch (signal_code) {
55 case ILL_ILLOPC: return "ILL_ILLOPC";
56 case ILL_ILLOPN: return "ILL_ILLOPN";
57 case ILL_ILLADR: return "ILL_ILLADR";
58 case ILL_ILLTRP: return "ILL_ILLTRP";
59 case ILL_PRVOPC: return "ILL_PRVOPC";
60 case ILL_PRVREG: return "ILL_PRVREG";
61 case ILL_COPROC: return "ILL_COPROC";
62 case ILL_BADSTK: return "ILL_BADSTK";
63 }
64 break;
65 case SIGBUS:
66 switch (signal_code) {
67 case BUS_ADRALN: return "BUS_ADRALN";
68 case BUS_ADRERR: return "BUS_ADRERR";
69 case BUS_OBJERR: return "BUS_OBJERR";
70 }
71 break;
72 case SIGFPE:
73 switch (signal_code) {
74 case FPE_INTDIV: return "FPE_INTDIV";
75 case FPE_INTOVF: return "FPE_INTOVF";
76 case FPE_FLTDIV: return "FPE_FLTDIV";
77 case FPE_FLTOVF: return "FPE_FLTOVF";
78 case FPE_FLTUND: return "FPE_FLTUND";
79 case FPE_FLTRES: return "FPE_FLTRES";
80 case FPE_FLTINV: return "FPE_FLTINV";
81 case FPE_FLTSUB: return "FPE_FLTSUB";
82 }
83 break;
84 case SIGSEGV:
85 switch (signal_code) {
86 case SEGV_MAPERR: return "SEGV_MAPERR";
87 case SEGV_ACCERR: return "SEGV_ACCERR";
88 }
89 break;
90 case SIGTRAP:
91 switch (signal_code) {
92 case TRAP_BRKPT: return "TRAP_BRKPT";
93 case TRAP_TRACE: return "TRAP_TRACE";
94 }
95 break;
96 }
97 // Then the other codes...
98 switch (signal_code) {
99 case SI_USER: return "SI_USER";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700100#if defined(SI_KERNEL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700101 case SI_KERNEL: return "SI_KERNEL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700102#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700103 case SI_QUEUE: return "SI_QUEUE";
104 case SI_TIMER: return "SI_TIMER";
105 case SI_MESGQ: return "SI_MESGQ";
106 case SI_ASYNCIO: return "SI_ASYNCIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700107#if defined(SI_SIGIO)
Elliott Hughes457005c2012-04-16 13:54:25 -0700108 case SI_SIGIO: return "SI_SIGIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700109#endif
110#if defined(SI_TKILL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700111 case SI_TKILL: return "SI_TKILL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700112#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700113 }
114 // Then give up...
115 return "?";
116}
117
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700118struct UContext {
119 UContext(void* raw_context) : context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {}
120
121 void Dump(std::ostream& os) {
122 // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets).
123#if defined(__APPLE__)
124 DumpRegister32(os, "eax", context->__ss.__eax);
125 DumpRegister32(os, "ebx", context->__ss.__ebx);
126 DumpRegister32(os, "ecx", context->__ss.__ecx);
127 DumpRegister32(os, "edx", context->__ss.__edx);
128 os << '\n';
129
130 DumpRegister32(os, "edi", context->__ss.__edi);
131 DumpRegister32(os, "esi", context->__ss.__esi);
132 DumpRegister32(os, "ebp", context->__ss.__ebp);
133 DumpRegister32(os, "esp", context->__ss.__esp);
134 os << '\n';
135
136 DumpRegister32(os, "eip", context->__ss.__eip);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700137 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700138 DumpRegister32(os, "eflags", context->__ss.__eflags);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700139 DumpX86Flags(os, context->__ss.__eflags);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700140 os << '\n';
141
142 DumpRegister32(os, "cs", context->__ss.__cs);
143 DumpRegister32(os, "ds", context->__ss.__ds);
144 DumpRegister32(os, "es", context->__ss.__es);
145 DumpRegister32(os, "fs", context->__ss.__fs);
146 os << '\n';
147 DumpRegister32(os, "gs", context->__ss.__gs);
148 DumpRegister32(os, "ss", context->__ss.__ss);
149#else
150 DumpRegister32(os, "eax", context.gregs[REG_EAX]);
151 DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
152 DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
153 DumpRegister32(os, "edx", context.gregs[REG_EDX]);
154 os << '\n';
155
156 DumpRegister32(os, "edi", context.gregs[REG_EDI]);
157 DumpRegister32(os, "esi", context.gregs[REG_ESI]);
158 DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
159 DumpRegister32(os, "esp", context.gregs[REG_ESP]);
160 os << '\n';
161
162 DumpRegister32(os, "eip", context.gregs[REG_EIP]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700163 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700164 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700165 DumpX86Flags(os, context.gregs[REG_EFL]);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700166 os << '\n';
167
168 DumpRegister32(os, "cs", context.gregs[REG_CS]);
169 DumpRegister32(os, "ds", context.gregs[REG_DS]);
170 DumpRegister32(os, "es", context.gregs[REG_ES]);
171 DumpRegister32(os, "fs", context.gregs[REG_FS]);
172 os << '\n';
173 DumpRegister32(os, "gs", context.gregs[REG_GS]);
174 DumpRegister32(os, "ss", context.gregs[REG_SS]);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700175#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700176 }
177
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700178 void DumpRegister32(std::ostream& os, const char* name, uint32_t value) {
179 os << StringPrintf(" %6s: 0x%08x", name, value);
180 }
181
Elliott Hughes46e251b2012-05-22 15:10:45 -0700182 void DumpX86Flags(std::ostream& os, uint32_t flags) {
183 os << " [";
184 if ((flags & (1 << 0)) != 0) {
185 os << " CF";
186 }
187 if ((flags & (1 << 2)) != 0) {
188 os << " PF";
189 }
190 if ((flags & (1 << 4)) != 0) {
191 os << " AF";
192 }
193 if ((flags & (1 << 6)) != 0) {
194 os << " ZF";
195 }
196 if ((flags & (1 << 7)) != 0) {
197 os << " SF";
198 }
199 if ((flags & (1 << 8)) != 0) {
200 os << " TF";
201 }
202 if ((flags & (1 << 9)) != 0) {
203 os << " IF";
204 }
205 if ((flags & (1 << 10)) != 0) {
206 os << " DF";
207 }
208 if ((flags & (1 << 11)) != 0) {
209 os << " OF";
210 }
211 os << " ]";
212 }
213
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700214 mcontext_t& context;
215};
216
217static void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700218 static Mutex unexpected_signal_lock("unexpected signal lock");
219 MutexLock mu(unexpected_signal_lock);
220
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700221 bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
222 signal_number == SIGFPE || signal_number == SIGSEGV);
223
224 UContext thread_context(raw_context);
225 Backtrace thread_backtrace;
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700226
Elliott Hughes457005c2012-04-16 13:54:25 -0700227 LOG(INTERNAL_FATAL) << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"
228 << StringPrintf("Fatal signal %d (%s), code %d (%s)",
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700229 signal_number, GetSignalName(signal_number),
Elliott Hughes457005c2012-04-16 13:54:25 -0700230 info->si_code,
231 GetSignalCodeName(signal_number, info->si_code))
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700232 << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << "\n"
233 << "Registers:\n" << Dumpable<UContext>(thread_context) << "\n"
234 << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace);
Elliott Hughes457005c2012-04-16 13:54:25 -0700235
Elliott Hughes2554cb92012-04-18 17:19:26 -0700236 // TODO: instead, get debuggerd running on the host, try to connect, and hang around on success.
237 if (getenv("debug_db_uid") != NULL) {
238 LOG(INTERNAL_FATAL) << "********************************************************\n"
239 << "* Process " << getpid() << " has been suspended while crashing. Attach gdb:\n"
240 << "* gdb -p " << getpid() << "\n"
241 << "********************************************************\n";
242 // Wait for debugger to attach.
243 while (true) {
244 }
Elliott Hughes457005c2012-04-16 13:54:25 -0700245 }
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700246
247 // Remove our signal handler for this signal...
248 struct sigaction action;
249 memset(&action, 0, sizeof(action));
250 sigemptyset(&action.sa_mask);
251 action.sa_handler = SIG_DFL;
252 sigaction(signal_number, &action, NULL);
253 // ...and re-raise so we die with the appropriate status.
254 kill(getpid(), signal_number);
Elliott Hughes457005c2012-04-16 13:54:25 -0700255}
256
Elliott Hughes457005c2012-04-16 13:54:25 -0700257void Runtime::InitPlatformSignalHandlers() {
258 // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
259 struct sigaction action;
260 memset(&action, 0, sizeof(action));
261 sigemptyset(&action.sa_mask);
262 action.sa_sigaction = HandleUnexpectedSignal;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700263 // Use the three-argument sa_sigaction handler.
264 action.sa_flags |= SA_SIGINFO;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700265#if !defined(__APPLE__)
266 // Use the alternate signal stack so we can catch stack overflows.
267 action.sa_flags |= SA_ONSTACK;
268#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700269
270 int rc = 0;
271 rc += sigaction(SIGILL, &action, NULL);
272 rc += sigaction(SIGTRAP, &action, NULL);
273 rc += sigaction(SIGABRT, &action, NULL);
274 rc += sigaction(SIGBUS, &action, NULL);
275 rc += sigaction(SIGFPE, &action, NULL);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700276#if defined(SIGSTKFLT)
Elliott Hughes457005c2012-04-16 13:54:25 -0700277 rc += sigaction(SIGSTKFLT, &action, NULL);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700278#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700279 rc += sigaction(SIGPIPE, &action, NULL);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700280
281 // Use the alternate signal stack so we can catch stack overflows.
282 // On Mac OS 10.7, backtrace(3) is broken and will return no frames when called from the alternate stack,
283 // so we only use the alternate stack for SIGSEGV so that we at least get backtraces for other signals.
284 // (glibc does the right thing, so we could use the alternate stack for all signals there.)
285 action.sa_flags |= SA_ONSTACK;
286 rc += sigaction(SIGSEGV, &action, NULL);
287
Elliott Hughes457005c2012-04-16 13:54:25 -0700288 CHECK_EQ(rc, 0);
289}
290
Elliott Hughesffe67362011-07-17 12:09:27 -0700291} // namespace art