1 /*
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 */
16
17 #include "runtime.h"
18
19 #include <signal.h>
20 #include <string.h>
21 #include <sys/utsname.h>
22 #include <inttypes.h>
23
24 #include <sstream>
25
26 #include "base/dumpable.h"
27 #include "base/logging.h"
28 #include "base/macros.h"
29 #include "base/mutex.h"
30 #include "base/stringprintf.h"
31 #include "thread-inl.h"
32 #include "thread_list.h"
33 #include "utils.h"
34
35 namespace art {
36
37 static constexpr bool kDumpHeapObjectOnSigsevg = false;
38 static constexpr bool kUseSigRTTimeout = true;
39
40 struct Backtrace {
41 public:
Backtraceart::Backtrace42 explicit Backtrace(void* raw_context) : raw_context_(raw_context) {}
Dumpart::Backtrace43 void Dump(std::ostream& os) const {
44 DumpNativeStack(os, GetTid(), "\t", nullptr, raw_context_);
45 }
46 private:
47 // Stores the context of the signal that was unexpected and will terminate the runtime. The
48 // DumpNativeStack code will take care of casting it to the expected type. This is required
49 // as our signal handler runs on an alternate stack.
50 void* raw_context_;
51 };
52
53 struct OsInfo {
Dumpart::OsInfo54 void Dump(std::ostream& os) const {
55 utsname info;
56 uname(&info);
57 // Linux 2.6.38.8-gg784 (x86_64)
58 // Darwin 11.4.0 (x86_64)
59 os << info.sysname << " " << info.release << " (" << info.machine << ")";
60 }
61 };
62
GetSignalName(int signal_number)63 static const char* GetSignalName(int signal_number) {
64 switch (signal_number) {
65 case SIGABRT: return "SIGABRT";
66 case SIGBUS: return "SIGBUS";
67 case SIGFPE: return "SIGFPE";
68 case SIGILL: return "SIGILL";
69 case SIGPIPE: return "SIGPIPE";
70 case SIGSEGV: return "SIGSEGV";
71 #if defined(SIGSTKFLT)
72 case SIGSTKFLT: return "SIGSTKFLT";
73 #endif
74 case SIGTRAP: return "SIGTRAP";
75 }
76 return "??";
77 }
78
GetSignalCodeName(int signal_number,int signal_code)79 static const char* GetSignalCodeName(int signal_number, int signal_code) {
80 // Try the signal-specific codes...
81 switch (signal_number) {
82 case SIGILL:
83 switch (signal_code) {
84 case ILL_ILLOPC: return "ILL_ILLOPC";
85 case ILL_ILLOPN: return "ILL_ILLOPN";
86 case ILL_ILLADR: return "ILL_ILLADR";
87 case ILL_ILLTRP: return "ILL_ILLTRP";
88 case ILL_PRVOPC: return "ILL_PRVOPC";
89 case ILL_PRVREG: return "ILL_PRVREG";
90 case ILL_COPROC: return "ILL_COPROC";
91 case ILL_BADSTK: return "ILL_BADSTK";
92 }
93 break;
94 case SIGBUS:
95 switch (signal_code) {
96 case BUS_ADRALN: return "BUS_ADRALN";
97 case BUS_ADRERR: return "BUS_ADRERR";
98 case BUS_OBJERR: return "BUS_OBJERR";
99 }
100 break;
101 case SIGFPE:
102 switch (signal_code) {
103 case FPE_INTDIV: return "FPE_INTDIV";
104 case FPE_INTOVF: return "FPE_INTOVF";
105 case FPE_FLTDIV: return "FPE_FLTDIV";
106 case FPE_FLTOVF: return "FPE_FLTOVF";
107 case FPE_FLTUND: return "FPE_FLTUND";
108 case FPE_FLTRES: return "FPE_FLTRES";
109 case FPE_FLTINV: return "FPE_FLTINV";
110 case FPE_FLTSUB: return "FPE_FLTSUB";
111 }
112 break;
113 case SIGSEGV:
114 switch (signal_code) {
115 case SEGV_MAPERR: return "SEGV_MAPERR";
116 case SEGV_ACCERR: return "SEGV_ACCERR";
117 }
118 break;
119 case SIGTRAP:
120 switch (signal_code) {
121 case TRAP_BRKPT: return "TRAP_BRKPT";
122 case TRAP_TRACE: return "TRAP_TRACE";
123 }
124 break;
125 }
126 // Then the other codes...
127 switch (signal_code) {
128 case SI_USER: return "SI_USER";
129 #if defined(SI_KERNEL)
130 case SI_KERNEL: return "SI_KERNEL";
131 #endif
132 case SI_QUEUE: return "SI_QUEUE";
133 case SI_TIMER: return "SI_TIMER";
134 case SI_MESGQ: return "SI_MESGQ";
135 case SI_ASYNCIO: return "SI_ASYNCIO";
136 #if defined(SI_SIGIO)
137 case SI_SIGIO: return "SI_SIGIO";
138 #endif
139 #if defined(SI_TKILL)
140 case SI_TKILL: return "SI_TKILL";
141 #endif
142 }
143 // Then give up...
144 return "?";
145 }
146
147 struct UContext {
UContextart::UContext148 explicit UContext(void* raw_context) :
149 context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {
150 }
151
Dumpart::UContext152 void Dump(std::ostream& os) const {
153 // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets).
154 #if defined(__APPLE__) && defined(__i386__)
155 DumpRegister32(os, "eax", context->__ss.__eax);
156 DumpRegister32(os, "ebx", context->__ss.__ebx);
157 DumpRegister32(os, "ecx", context->__ss.__ecx);
158 DumpRegister32(os, "edx", context->__ss.__edx);
159 os << '\n';
160
161 DumpRegister32(os, "edi", context->__ss.__edi);
162 DumpRegister32(os, "esi", context->__ss.__esi);
163 DumpRegister32(os, "ebp", context->__ss.__ebp);
164 DumpRegister32(os, "esp", context->__ss.__esp);
165 os << '\n';
166
167 DumpRegister32(os, "eip", context->__ss.__eip);
168 os << " ";
169 DumpRegister32(os, "eflags", context->__ss.__eflags);
170 DumpX86Flags(os, context->__ss.__eflags);
171 os << '\n';
172
173 DumpRegister32(os, "cs", context->__ss.__cs);
174 DumpRegister32(os, "ds", context->__ss.__ds);
175 DumpRegister32(os, "es", context->__ss.__es);
176 DumpRegister32(os, "fs", context->__ss.__fs);
177 os << '\n';
178 DumpRegister32(os, "gs", context->__ss.__gs);
179 DumpRegister32(os, "ss", context->__ss.__ss);
180 #elif defined(__linux__) && defined(__i386__)
181 DumpRegister32(os, "eax", context.gregs[REG_EAX]);
182 DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
183 DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
184 DumpRegister32(os, "edx", context.gregs[REG_EDX]);
185 os << '\n';
186
187 DumpRegister32(os, "edi", context.gregs[REG_EDI]);
188 DumpRegister32(os, "esi", context.gregs[REG_ESI]);
189 DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
190 DumpRegister32(os, "esp", context.gregs[REG_ESP]);
191 os << '\n';
192
193 DumpRegister32(os, "eip", context.gregs[REG_EIP]);
194 os << " ";
195 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
196 DumpX86Flags(os, context.gregs[REG_EFL]);
197 os << '\n';
198
199 DumpRegister32(os, "cs", context.gregs[REG_CS]);
200 DumpRegister32(os, "ds", context.gregs[REG_DS]);
201 DumpRegister32(os, "es", context.gregs[REG_ES]);
202 DumpRegister32(os, "fs", context.gregs[REG_FS]);
203 os << '\n';
204 DumpRegister32(os, "gs", context.gregs[REG_GS]);
205 DumpRegister32(os, "ss", context.gregs[REG_SS]);
206 #elif defined(__linux__) && defined(__x86_64__)
207 DumpRegister64(os, "rax", context.gregs[REG_RAX]);
208 DumpRegister64(os, "rbx", context.gregs[REG_RBX]);
209 DumpRegister64(os, "rcx", context.gregs[REG_RCX]);
210 DumpRegister64(os, "rdx", context.gregs[REG_RDX]);
211 os << '\n';
212
213 DumpRegister64(os, "rdi", context.gregs[REG_RDI]);
214 DumpRegister64(os, "rsi", context.gregs[REG_RSI]);
215 DumpRegister64(os, "rbp", context.gregs[REG_RBP]);
216 DumpRegister64(os, "rsp", context.gregs[REG_RSP]);
217 os << '\n';
218
219 DumpRegister64(os, "r8 ", context.gregs[REG_R8]);
220 DumpRegister64(os, "r9 ", context.gregs[REG_R9]);
221 DumpRegister64(os, "r10", context.gregs[REG_R10]);
222 DumpRegister64(os, "r11", context.gregs[REG_R11]);
223 os << '\n';
224
225 DumpRegister64(os, "r12", context.gregs[REG_R12]);
226 DumpRegister64(os, "r13", context.gregs[REG_R13]);
227 DumpRegister64(os, "r14", context.gregs[REG_R14]);
228 DumpRegister64(os, "r15", context.gregs[REG_R15]);
229 os << '\n';
230
231 DumpRegister64(os, "rip", context.gregs[REG_RIP]);
232 os << " ";
233 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
234 DumpX86Flags(os, context.gregs[REG_EFL]);
235 os << '\n';
236
237 DumpRegister32(os, "cs", (context.gregs[REG_CSGSFS]) & 0x0FFFF);
238 DumpRegister32(os, "gs", (context.gregs[REG_CSGSFS] >> 16) & 0x0FFFF);
239 DumpRegister32(os, "fs", (context.gregs[REG_CSGSFS] >> 32) & 0x0FFFF);
240 os << '\n';
241 #else
242 os << "Unknown architecture/word size/OS in ucontext dump";
243 #endif
244 }
245
DumpRegister32art::UContext246 void DumpRegister32(std::ostream& os, const char* name, uint32_t value) const {
247 os << StringPrintf(" %6s: 0x%08x", name, value);
248 }
249
DumpRegister64art::UContext250 void DumpRegister64(std::ostream& os, const char* name, uint64_t value) const {
251 os << StringPrintf(" %6s: 0x%016" PRIx64, name, value);
252 }
253
DumpX86Flagsart::UContext254 void DumpX86Flags(std::ostream& os, uint32_t flags) const {
255 os << " [";
256 if ((flags & (1 << 0)) != 0) {
257 os << " CF";
258 }
259 if ((flags & (1 << 2)) != 0) {
260 os << " PF";
261 }
262 if ((flags & (1 << 4)) != 0) {
263 os << " AF";
264 }
265 if ((flags & (1 << 6)) != 0) {
266 os << " ZF";
267 }
268 if ((flags & (1 << 7)) != 0) {
269 os << " SF";
270 }
271 if ((flags & (1 << 8)) != 0) {
272 os << " TF";
273 }
274 if ((flags & (1 << 9)) != 0) {
275 os << " IF";
276 }
277 if ((flags & (1 << 10)) != 0) {
278 os << " DF";
279 }
280 if ((flags & (1 << 11)) != 0) {
281 os << " OF";
282 }
283 os << " ]";
284 }
285
286 mcontext_t& context;
287 };
288
289 // Return the signal number we recognize as timeout. -1 means not active/supported.
GetTimeoutSignal()290 static int GetTimeoutSignal() {
291 #if defined(__APPLE__)
292 // Mac does not support realtime signals.
293 UNUSED(kUseSigRTTimeout);
294 return -1;
295 #else
296 return kUseSigRTTimeout ? (SIGRTMIN + 2) : -1;
297 #endif
298 }
299
IsTimeoutSignal(int signal_number)300 static bool IsTimeoutSignal(int signal_number) {
301 return signal_number == GetTimeoutSignal();
302 }
303
HandleUnexpectedSignal(int signal_number,siginfo_t * info,void * raw_context)304 void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
305 static bool handlingUnexpectedSignal = false;
306 if (handlingUnexpectedSignal) {
307 LogMessage::LogLine(__FILE__, __LINE__, INTERNAL_FATAL, "HandleUnexpectedSignal reentered\n");
308 if (IsTimeoutSignal(signal_number)) {
309 // Ignore a recursive timeout.
310 return;
311 }
312 _exit(1);
313 }
314 handlingUnexpectedSignal = true;
315
316 gAborting++; // set before taking any locks
317 MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
318
319 bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
320 signal_number == SIGFPE || signal_number == SIGSEGV);
321
322 OsInfo os_info;
323 const char* cmd_line = GetCmdLine();
324 if (cmd_line == nullptr) {
325 cmd_line = "<unset>"; // Because no-one called InitLogging.
326 }
327 pid_t tid = GetTid();
328 std::string thread_name(GetThreadName(tid));
329 UContext thread_context(raw_context);
330 Backtrace thread_backtrace(raw_context);
331
332 LOG(INTERNAL_FATAL) << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"
333 << StringPrintf("Fatal signal %d (%s), code %d (%s)",
334 signal_number, GetSignalName(signal_number),
335 info->si_code,
336 GetSignalCodeName(signal_number, info->si_code))
337 << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << "\n"
338 << "OS: " << Dumpable<OsInfo>(os_info) << "\n"
339 << "Cmdline: " << cmd_line << "\n"
340 << "Thread: " << tid << " \"" << thread_name << "\"\n"
341 << "Registers:\n" << Dumpable<UContext>(thread_context) << "\n"
342 << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace);
343 Runtime* runtime = Runtime::Current();
344 if (runtime != nullptr) {
345 if (IsTimeoutSignal(signal_number)) {
346 // Special timeout signal. Try to dump all threads.
347 runtime->GetThreadList()->DumpForSigQuit(LOG(INTERNAL_FATAL));
348 }
349 gc::Heap* heap = runtime->GetHeap();
350 LOG(INTERNAL_FATAL) << "Fault message: " << runtime->GetFaultMessage();
351 if (kDumpHeapObjectOnSigsevg && heap != nullptr && info != nullptr) {
352 LOG(INTERNAL_FATAL) << "Dump heap object at fault address: ";
353 heap->DumpObject(LOG(INTERNAL_FATAL), reinterpret_cast<mirror::Object*>(info->si_addr));
354 }
355 }
356 if (getenv("debug_db_uid") != nullptr || getenv("art_wait_for_gdb_on_crash") != nullptr) {
357 LOG(INTERNAL_FATAL) << "********************************************************\n"
358 << "* Process " << getpid() << " thread " << tid << " \"" << thread_name
359 << "\""
360 << " has been suspended while crashing.\n"
361 << "* Attach gdb:\n"
362 << "* gdb -p " << tid << "\n"
363 << "********************************************************\n";
364 // Wait for debugger to attach.
365 while (true) {
366 }
367 }
368 #ifdef __linux__
369 // Remove our signal handler for this signal...
370 struct sigaction action;
371 memset(&action, 0, sizeof(action));
372 sigemptyset(&action.sa_mask);
373 action.sa_handler = SIG_DFL;
374 sigaction(signal_number, &action, nullptr);
375 // ...and re-raise so we die with the appropriate status.
376 kill(getpid(), signal_number);
377 #else
378 exit(EXIT_FAILURE);
379 #endif
380 }
381
InitPlatformSignalHandlers()382 void Runtime::InitPlatformSignalHandlers() {
383 // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
384 struct sigaction action;
385 memset(&action, 0, sizeof(action));
386 sigemptyset(&action.sa_mask);
387 action.sa_sigaction = HandleUnexpectedSignal;
388 // Use the three-argument sa_sigaction handler.
389 action.sa_flags |= SA_SIGINFO;
390 // Use the alternate signal stack so we can catch stack overflows.
391 action.sa_flags |= SA_ONSTACK;
392
393 int rc = 0;
394 rc += sigaction(SIGABRT, &action, nullptr);
395 rc += sigaction(SIGBUS, &action, nullptr);
396 rc += sigaction(SIGFPE, &action, nullptr);
397 rc += sigaction(SIGILL, &action, nullptr);
398 rc += sigaction(SIGPIPE, &action, nullptr);
399 rc += sigaction(SIGSEGV, &action, nullptr);
400 #if defined(SIGSTKFLT)
401 rc += sigaction(SIGSTKFLT, &action, nullptr);
402 #endif
403 rc += sigaction(SIGTRAP, &action, nullptr);
404 // Special dump-all timeout.
405 if (GetTimeoutSignal() != -1) {
406 rc += sigaction(GetTimeoutSignal(), &action, nullptr);
407 }
408 CHECK_EQ(rc, 0);
409 }
410
411 } // namespace art
412