blob: 85eeb8f0bed76c4fa8eff25e1766cebd38586094 [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"
Ian Rogers81d425b2012-09-27 16:03:43 -070024#include "mutex.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070025#include "stringprintf.h"
Elliott Hughes46e251b2012-05-22 15:10:45 -070026#include "utils.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070027
28namespace art {
29
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070030struct Backtrace {
31 void Dump(std::ostream& os) {
Elliott Hughes46e251b2012-05-22 15:10:45 -070032 DumpNativeStack(os, GetTid(), "\t", true);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070033 }
34};
35
Elliott Hughes058a6de2012-05-24 19:13:02 -070036struct OS {
37 void Dump(std::ostream& os) {
38 utsname info;
39 uname(&info);
40 // Linux 2.6.38.8-gg784 (x86_64)
41 // Darwin 11.4.0 (x86_64)
42 os << info.sysname << " " << info.release << " (" << info.machine << ")";
43 }
44};
45
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070046static const char* GetSignalName(int signal_number) {
47 switch (signal_number) {
48 case SIGABRT: return "SIGABRT";
49 case SIGBUS: return "SIGBUS";
50 case SIGFPE: return "SIGFPE";
51 case SIGILL: return "SIGILL";
52 case SIGPIPE: return "SIGPIPE";
53 case SIGSEGV: return "SIGSEGV";
Elliott Hughes833770b2012-05-01 15:41:03 -070054#if defined(SIGSTKFLT)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070055 case SIGSTKFLT: return "SIGSTKFLT";
56#endif
57 case SIGTRAP: return "SIGTRAP";
58 }
59 return "??";
Elliott Hughesffe67362011-07-17 12:09:27 -070060}
61
Elliott Hughes457005c2012-04-16 13:54:25 -070062static const char* GetSignalCodeName(int signal_number, int signal_code) {
63 // Try the signal-specific codes...
64 switch (signal_number) {
65 case SIGILL:
66 switch (signal_code) {
67 case ILL_ILLOPC: return "ILL_ILLOPC";
68 case ILL_ILLOPN: return "ILL_ILLOPN";
69 case ILL_ILLADR: return "ILL_ILLADR";
70 case ILL_ILLTRP: return "ILL_ILLTRP";
71 case ILL_PRVOPC: return "ILL_PRVOPC";
72 case ILL_PRVREG: return "ILL_PRVREG";
73 case ILL_COPROC: return "ILL_COPROC";
74 case ILL_BADSTK: return "ILL_BADSTK";
75 }
76 break;
77 case SIGBUS:
78 switch (signal_code) {
79 case BUS_ADRALN: return "BUS_ADRALN";
80 case BUS_ADRERR: return "BUS_ADRERR";
81 case BUS_OBJERR: return "BUS_OBJERR";
82 }
83 break;
84 case SIGFPE:
85 switch (signal_code) {
86 case FPE_INTDIV: return "FPE_INTDIV";
87 case FPE_INTOVF: return "FPE_INTOVF";
88 case FPE_FLTDIV: return "FPE_FLTDIV";
89 case FPE_FLTOVF: return "FPE_FLTOVF";
90 case FPE_FLTUND: return "FPE_FLTUND";
91 case FPE_FLTRES: return "FPE_FLTRES";
92 case FPE_FLTINV: return "FPE_FLTINV";
93 case FPE_FLTSUB: return "FPE_FLTSUB";
94 }
95 break;
96 case SIGSEGV:
97 switch (signal_code) {
98 case SEGV_MAPERR: return "SEGV_MAPERR";
99 case SEGV_ACCERR: return "SEGV_ACCERR";
100 }
101 break;
102 case SIGTRAP:
103 switch (signal_code) {
104 case TRAP_BRKPT: return "TRAP_BRKPT";
105 case TRAP_TRACE: return "TRAP_TRACE";
106 }
107 break;
108 }
109 // Then the other codes...
110 switch (signal_code) {
111 case SI_USER: return "SI_USER";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700112#if defined(SI_KERNEL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700113 case SI_KERNEL: return "SI_KERNEL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700114#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700115 case SI_QUEUE: return "SI_QUEUE";
116 case SI_TIMER: return "SI_TIMER";
117 case SI_MESGQ: return "SI_MESGQ";
118 case SI_ASYNCIO: return "SI_ASYNCIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700119#if defined(SI_SIGIO)
Elliott Hughes457005c2012-04-16 13:54:25 -0700120 case SI_SIGIO: return "SI_SIGIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700121#endif
122#if defined(SI_TKILL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700123 case SI_TKILL: return "SI_TKILL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700124#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700125 }
126 // Then give up...
127 return "?";
128}
129
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700130struct UContext {
Elliott Hughes74847412012-06-20 18:10:21 -0700131 explicit UContext(void* raw_context) : context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {}
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700132
133 void Dump(std::ostream& os) {
134 // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets).
135#if defined(__APPLE__)
136 DumpRegister32(os, "eax", context->__ss.__eax);
137 DumpRegister32(os, "ebx", context->__ss.__ebx);
138 DumpRegister32(os, "ecx", context->__ss.__ecx);
139 DumpRegister32(os, "edx", context->__ss.__edx);
140 os << '\n';
141
142 DumpRegister32(os, "edi", context->__ss.__edi);
143 DumpRegister32(os, "esi", context->__ss.__esi);
144 DumpRegister32(os, "ebp", context->__ss.__ebp);
145 DumpRegister32(os, "esp", context->__ss.__esp);
146 os << '\n';
147
148 DumpRegister32(os, "eip", context->__ss.__eip);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700149 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700150 DumpRegister32(os, "eflags", context->__ss.__eflags);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700151 DumpX86Flags(os, context->__ss.__eflags);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700152 os << '\n';
153
154 DumpRegister32(os, "cs", context->__ss.__cs);
155 DumpRegister32(os, "ds", context->__ss.__ds);
156 DumpRegister32(os, "es", context->__ss.__es);
157 DumpRegister32(os, "fs", context->__ss.__fs);
158 os << '\n';
159 DumpRegister32(os, "gs", context->__ss.__gs);
160 DumpRegister32(os, "ss", context->__ss.__ss);
161#else
162 DumpRegister32(os, "eax", context.gregs[REG_EAX]);
163 DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
164 DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
165 DumpRegister32(os, "edx", context.gregs[REG_EDX]);
166 os << '\n';
167
168 DumpRegister32(os, "edi", context.gregs[REG_EDI]);
169 DumpRegister32(os, "esi", context.gregs[REG_ESI]);
170 DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
171 DumpRegister32(os, "esp", context.gregs[REG_ESP]);
172 os << '\n';
173
174 DumpRegister32(os, "eip", context.gregs[REG_EIP]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700175 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700176 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700177 DumpX86Flags(os, context.gregs[REG_EFL]);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700178 os << '\n';
179
180 DumpRegister32(os, "cs", context.gregs[REG_CS]);
181 DumpRegister32(os, "ds", context.gregs[REG_DS]);
182 DumpRegister32(os, "es", context.gregs[REG_ES]);
183 DumpRegister32(os, "fs", context.gregs[REG_FS]);
184 os << '\n';
185 DumpRegister32(os, "gs", context.gregs[REG_GS]);
186 DumpRegister32(os, "ss", context.gregs[REG_SS]);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700187#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700188 }
189
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700190 void DumpRegister32(std::ostream& os, const char* name, uint32_t value) {
191 os << StringPrintf(" %6s: 0x%08x", name, value);
192 }
193
Elliott Hughes46e251b2012-05-22 15:10:45 -0700194 void DumpX86Flags(std::ostream& os, uint32_t flags) {
195 os << " [";
196 if ((flags & (1 << 0)) != 0) {
197 os << " CF";
198 }
199 if ((flags & (1 << 2)) != 0) {
200 os << " PF";
201 }
202 if ((flags & (1 << 4)) != 0) {
203 os << " AF";
204 }
205 if ((flags & (1 << 6)) != 0) {
206 os << " ZF";
207 }
208 if ((flags & (1 << 7)) != 0) {
209 os << " SF";
210 }
211 if ((flags & (1 << 8)) != 0) {
212 os << " TF";
213 }
214 if ((flags & (1 << 9)) != 0) {
215 os << " IF";
216 }
217 if ((flags & (1 << 10)) != 0) {
218 os << " DF";
219 }
220 if ((flags & (1 << 11)) != 0) {
221 os << " OF";
222 }
223 os << " ]";
224 }
225
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700226 mcontext_t& context;
227};
228
229static void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700230 MutexLock mu(*Locks::unexpected_signal_lock_);
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700231
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