blob: 1401f0733dd4fe5998314d2b001359730f4f7eea [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>
Dmitry Petrochenko611c2c32014-02-10 14:48:12 +070022#include <inttypes.h>
Elliott Hughesffe67362011-07-17 12:09:27 -070023
Andreas Gampeef295362016-10-11 20:04:11 -070024#include <iostream>
Ian Rogersc7dd2952014-10-21 23:31:19 -070025#include <sstream>
26
27#include "base/dumpable.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080028#include "base/logging.h"
Andreas Gampefcf81d82015-01-14 20:09:14 -080029#include "base/macros.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080030#include "base/mutex.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080031#include "base/stringprintf.h"
Andreas Gampe5dd44d02016-08-02 17:20:03 -070032#include "native_stack_dump.h"
Ian Rogersb48b9eb2014-02-28 16:20:21 -080033#include "thread-inl.h"
Andreas Gampe038bb222015-01-13 19:48:14 -080034#include "thread_list.h"
Elliott Hughes46e251b2012-05-22 15:10:45 -070035#include "utils.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070036
37namespace art {
38
Mathieu Chartierc2f4d022014-03-03 16:11:42 -080039static constexpr bool kDumpHeapObjectOnSigsevg = false;
Andreas Gampecbfded42015-01-14 18:35:01 -080040static constexpr bool kUseSigRTTimeout = true;
Andreas Gampe0d0ce272016-03-08 20:29:49 -080041static constexpr bool kDumpNativeStackOnTimeout = true;
Mathieu Chartierc2f4d022014-03-03 16:11:42 -080042
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070043struct Backtrace {
Andreas Gampe628a61a2015-01-07 22:08:35 -080044 public:
45 explicit Backtrace(void* raw_context) : raw_context_(raw_context) {}
Ian Rogersc7dd2952014-10-21 23:31:19 -070046 void Dump(std::ostream& os) const {
Christopher Ferris6cff48f2014-01-26 21:36:13 -080047 DumpNativeStack(os, GetTid(), nullptr, "\t", nullptr, raw_context_);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070048 }
Andreas Gampe628a61a2015-01-07 22:08:35 -080049 private:
50 // Stores the context of the signal that was unexpected and will terminate the runtime. The
51 // DumpNativeStack code will take care of casting it to the expected type. This is required
52 // as our signal handler runs on an alternate stack.
53 void* raw_context_;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070054};
55
Elliott Hughes76160052012-12-12 16:31:20 -080056struct OsInfo {
Ian Rogersc7dd2952014-10-21 23:31:19 -070057 void Dump(std::ostream& os) const {
Elliott Hughes058a6de2012-05-24 19:13:02 -070058 utsname info;
59 uname(&info);
60 // Linux 2.6.38.8-gg784 (x86_64)
61 // Darwin 11.4.0 (x86_64)
62 os << info.sysname << " " << info.release << " (" << info.machine << ")";
63 }
64};
65
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070066static const char* GetSignalName(int signal_number) {
67 switch (signal_number) {
68 case SIGABRT: return "SIGABRT";
69 case SIGBUS: return "SIGBUS";
70 case SIGFPE: return "SIGFPE";
71 case SIGILL: return "SIGILL";
72 case SIGPIPE: return "SIGPIPE";
73 case SIGSEGV: return "SIGSEGV";
Elliott Hughes833770b2012-05-01 15:41:03 -070074#if defined(SIGSTKFLT)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070075 case SIGSTKFLT: return "SIGSTKFLT";
76#endif
77 case SIGTRAP: return "SIGTRAP";
78 }
79 return "??";
Elliott Hughesffe67362011-07-17 12:09:27 -070080}
81
Elliott Hughes457005c2012-04-16 13:54:25 -070082static const char* GetSignalCodeName(int signal_number, int signal_code) {
83 // Try the signal-specific codes...
84 switch (signal_number) {
85 case SIGILL:
86 switch (signal_code) {
87 case ILL_ILLOPC: return "ILL_ILLOPC";
88 case ILL_ILLOPN: return "ILL_ILLOPN";
89 case ILL_ILLADR: return "ILL_ILLADR";
90 case ILL_ILLTRP: return "ILL_ILLTRP";
91 case ILL_PRVOPC: return "ILL_PRVOPC";
92 case ILL_PRVREG: return "ILL_PRVREG";
93 case ILL_COPROC: return "ILL_COPROC";
94 case ILL_BADSTK: return "ILL_BADSTK";
95 }
96 break;
97 case SIGBUS:
98 switch (signal_code) {
99 case BUS_ADRALN: return "BUS_ADRALN";
100 case BUS_ADRERR: return "BUS_ADRERR";
101 case BUS_OBJERR: return "BUS_OBJERR";
102 }
103 break;
104 case SIGFPE:
105 switch (signal_code) {
106 case FPE_INTDIV: return "FPE_INTDIV";
107 case FPE_INTOVF: return "FPE_INTOVF";
108 case FPE_FLTDIV: return "FPE_FLTDIV";
109 case FPE_FLTOVF: return "FPE_FLTOVF";
110 case FPE_FLTUND: return "FPE_FLTUND";
111 case FPE_FLTRES: return "FPE_FLTRES";
112 case FPE_FLTINV: return "FPE_FLTINV";
113 case FPE_FLTSUB: return "FPE_FLTSUB";
114 }
115 break;
116 case SIGSEGV:
117 switch (signal_code) {
118 case SEGV_MAPERR: return "SEGV_MAPERR";
119 case SEGV_ACCERR: return "SEGV_ACCERR";
Christopher Ferris12b8c9d2016-02-04 14:06:34 -0800120#if defined(SEGV_BNDERR)
121 case SEGV_BNDERR: return "SEGV_BNDERR";
122#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700123 }
124 break;
125 case SIGTRAP:
126 switch (signal_code) {
127 case TRAP_BRKPT: return "TRAP_BRKPT";
128 case TRAP_TRACE: return "TRAP_TRACE";
129 }
130 break;
131 }
132 // Then the other codes...
133 switch (signal_code) {
134 case SI_USER: return "SI_USER";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700135#if defined(SI_KERNEL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700136 case SI_KERNEL: return "SI_KERNEL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700137#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700138 case SI_QUEUE: return "SI_QUEUE";
139 case SI_TIMER: return "SI_TIMER";
140 case SI_MESGQ: return "SI_MESGQ";
141 case SI_ASYNCIO: return "SI_ASYNCIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700142#if defined(SI_SIGIO)
Elliott Hughes457005c2012-04-16 13:54:25 -0700143 case SI_SIGIO: return "SI_SIGIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700144#endif
145#if defined(SI_TKILL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700146 case SI_TKILL: return "SI_TKILL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700147#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700148 }
149 // Then give up...
150 return "?";
151}
152
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700153struct UContext {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700154 explicit UContext(void* raw_context) :
155 context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {
156 }
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700157
Ian Rogersc7dd2952014-10-21 23:31:19 -0700158 void Dump(std::ostream& os) const {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700159 // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800160#if defined(__APPLE__) && defined(__i386__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700161 DumpRegister32(os, "eax", context->__ss.__eax);
162 DumpRegister32(os, "ebx", context->__ss.__ebx);
163 DumpRegister32(os, "ecx", context->__ss.__ecx);
164 DumpRegister32(os, "edx", context->__ss.__edx);
165 os << '\n';
166
167 DumpRegister32(os, "edi", context->__ss.__edi);
168 DumpRegister32(os, "esi", context->__ss.__esi);
169 DumpRegister32(os, "ebp", context->__ss.__ebp);
170 DumpRegister32(os, "esp", context->__ss.__esp);
171 os << '\n';
172
173 DumpRegister32(os, "eip", context->__ss.__eip);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700174 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700175 DumpRegister32(os, "eflags", context->__ss.__eflags);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700176 DumpX86Flags(os, context->__ss.__eflags);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700177 os << '\n';
178
179 DumpRegister32(os, "cs", context->__ss.__cs);
180 DumpRegister32(os, "ds", context->__ss.__ds);
181 DumpRegister32(os, "es", context->__ss.__es);
182 DumpRegister32(os, "fs", context->__ss.__fs);
183 os << '\n';
184 DumpRegister32(os, "gs", context->__ss.__gs);
185 DumpRegister32(os, "ss", context->__ss.__ss);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800186#elif defined(__linux__) && defined(__i386__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700187 DumpRegister32(os, "eax", context.gregs[REG_EAX]);
188 DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
189 DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
190 DumpRegister32(os, "edx", context.gregs[REG_EDX]);
191 os << '\n';
192
193 DumpRegister32(os, "edi", context.gregs[REG_EDI]);
194 DumpRegister32(os, "esi", context.gregs[REG_ESI]);
195 DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
196 DumpRegister32(os, "esp", context.gregs[REG_ESP]);
197 os << '\n';
198
199 DumpRegister32(os, "eip", context.gregs[REG_EIP]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700200 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700201 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700202 DumpX86Flags(os, context.gregs[REG_EFL]);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700203 os << '\n';
204
205 DumpRegister32(os, "cs", context.gregs[REG_CS]);
206 DumpRegister32(os, "ds", context.gregs[REG_DS]);
207 DumpRegister32(os, "es", context.gregs[REG_ES]);
208 DumpRegister32(os, "fs", context.gregs[REG_FS]);
209 os << '\n';
210 DumpRegister32(os, "gs", context.gregs[REG_GS]);
211 DumpRegister32(os, "ss", context.gregs[REG_SS]);
Dmitry Petrochenko611c2c32014-02-10 14:48:12 +0700212#elif defined(__linux__) && defined(__x86_64__)
213 DumpRegister64(os, "rax", context.gregs[REG_RAX]);
214 DumpRegister64(os, "rbx", context.gregs[REG_RBX]);
215 DumpRegister64(os, "rcx", context.gregs[REG_RCX]);
216 DumpRegister64(os, "rdx", context.gregs[REG_RDX]);
217 os << '\n';
218
219 DumpRegister64(os, "rdi", context.gregs[REG_RDI]);
220 DumpRegister64(os, "rsi", context.gregs[REG_RSI]);
221 DumpRegister64(os, "rbp", context.gregs[REG_RBP]);
222 DumpRegister64(os, "rsp", context.gregs[REG_RSP]);
223 os << '\n';
224
225 DumpRegister64(os, "r8 ", context.gregs[REG_R8]);
226 DumpRegister64(os, "r9 ", context.gregs[REG_R9]);
227 DumpRegister64(os, "r10", context.gregs[REG_R10]);
228 DumpRegister64(os, "r11", context.gregs[REG_R11]);
229 os << '\n';
230
231 DumpRegister64(os, "r12", context.gregs[REG_R12]);
232 DumpRegister64(os, "r13", context.gregs[REG_R13]);
233 DumpRegister64(os, "r14", context.gregs[REG_R14]);
234 DumpRegister64(os, "r15", context.gregs[REG_R15]);
235 os << '\n';
236
237 DumpRegister64(os, "rip", context.gregs[REG_RIP]);
238 os << " ";
239 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
240 DumpX86Flags(os, context.gregs[REG_EFL]);
241 os << '\n';
242
243 DumpRegister32(os, "cs", (context.gregs[REG_CSGSFS]) & 0x0FFFF);
244 DumpRegister32(os, "gs", (context.gregs[REG_CSGSFS] >> 16) & 0x0FFFF);
245 DumpRegister32(os, "fs", (context.gregs[REG_CSGSFS] >> 32) & 0x0FFFF);
246 os << '\n';
Ian Rogersef7d42f2014-01-06 12:55:46 -0800247#else
248 os << "Unknown architecture/word size/OS in ucontext dump";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700249#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700250 }
251
Ian Rogersc7dd2952014-10-21 23:31:19 -0700252 void DumpRegister32(std::ostream& os, const char* name, uint32_t value) const {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700253 os << StringPrintf(" %6s: 0x%08x", name, value);
254 }
255
Ian Rogersc7dd2952014-10-21 23:31:19 -0700256 void DumpRegister64(std::ostream& os, const char* name, uint64_t value) const {
Dmitry Petrochenko611c2c32014-02-10 14:48:12 +0700257 os << StringPrintf(" %6s: 0x%016" PRIx64, name, value);
258 }
259
Ian Rogersc7dd2952014-10-21 23:31:19 -0700260 void DumpX86Flags(std::ostream& os, uint32_t flags) const {
Elliott Hughes46e251b2012-05-22 15:10:45 -0700261 os << " [";
262 if ((flags & (1 << 0)) != 0) {
263 os << " CF";
264 }
265 if ((flags & (1 << 2)) != 0) {
266 os << " PF";
267 }
268 if ((flags & (1 << 4)) != 0) {
269 os << " AF";
270 }
271 if ((flags & (1 << 6)) != 0) {
272 os << " ZF";
273 }
274 if ((flags & (1 << 7)) != 0) {
275 os << " SF";
276 }
277 if ((flags & (1 << 8)) != 0) {
278 os << " TF";
279 }
280 if ((flags & (1 << 9)) != 0) {
281 os << " IF";
282 }
283 if ((flags & (1 << 10)) != 0) {
284 os << " DF";
285 }
286 if ((flags & (1 << 11)) != 0) {
287 os << " OF";
288 }
289 os << " ]";
290 }
291
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700292 mcontext_t& context;
293};
294
Andreas Gampecbfded42015-01-14 18:35:01 -0800295// Return the signal number we recognize as timeout. -1 means not active/supported.
Andreas Gampe038bb222015-01-13 19:48:14 -0800296static int GetTimeoutSignal() {
Andreas Gampecbfded42015-01-14 18:35:01 -0800297#if defined(__APPLE__)
298 // Mac does not support realtime signals.
Andreas Gampefcf81d82015-01-14 20:09:14 -0800299 UNUSED(kUseSigRTTimeout);
Andreas Gampecbfded42015-01-14 18:35:01 -0800300 return -1;
301#else
302 return kUseSigRTTimeout ? (SIGRTMIN + 2) : -1;
303#endif
Andreas Gampe038bb222015-01-13 19:48:14 -0800304}
305
306static bool IsTimeoutSignal(int signal_number) {
307 return signal_number == GetTimeoutSignal();
308}
309
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800310void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
311 static bool handlingUnexpectedSignal = false;
312 if (handlingUnexpectedSignal) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700313 LogHelper::LogLineLowStack(__FILE__,
314 __LINE__,
315 ::android::base::FATAL_WITHOUT_ABORT,
316 "HandleUnexpectedSignal reentered\n");
Andreas Gampe038bb222015-01-13 19:48:14 -0800317 if (IsTimeoutSignal(signal_number)) {
318 // Ignore a recursive timeout.
319 return;
320 }
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800321 _exit(1);
322 }
323 handlingUnexpectedSignal = true;
324
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000325 gAborting++; // set before taking any locks
Ian Rogers50b35e22012-10-04 10:09:15 -0700326 MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700327
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700328 bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
329 signal_number == SIGFPE || signal_number == SIGSEGV);
330
Elliott Hughes76160052012-12-12 16:31:20 -0800331 OsInfo os_info;
Elliott Hughes98eedd82012-06-11 17:52:56 -0700332 const char* cmd_line = GetCmdLine();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700333 if (cmd_line == nullptr) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700334 cmd_line = "<unset>"; // Because no-one called InitLogging.
Elliott Hughes98eedd82012-06-11 17:52:56 -0700335 }
Elliott Hughes289be852012-06-12 13:57:20 -0700336 pid_t tid = GetTid();
337 std::string thread_name(GetThreadName(tid));
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700338 UContext thread_context(raw_context);
Andreas Gampe628a61a2015-01-07 22:08:35 -0800339 Backtrace thread_backtrace(raw_context);
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700340
Andreas Gampeef295362016-10-11 20:04:11 -0700341 // Note: We are using cerr directly instead of LOG macros to ensure even just partial output
342 // makes it out. That means we lose the "dalvikvm..." prefix, but that is acceptable
343 // considering this is an abort situation.
344
345 std::cerr << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"
346 << StringPrintf("Fatal signal %d (%s), code %d (%s)",
347 signal_number, GetSignalName(signal_number),
348 info->si_code,
349 GetSignalCodeName(signal_number, info->si_code))
350 << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << std::endl
351 << "OS: " << Dumpable<OsInfo>(os_info) << std::endl
352 << "Cmdline: " << cmd_line << std::endl
353 << "Thread: " << tid << " \"" << thread_name << "\"" << std::endl
354 << "Registers:\n" << Dumpable<UContext>(thread_context) << std::endl
355 << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace) << std::endl;
Hiroshi Yamauchiae0d7de2015-05-22 18:15:10 -0700356 if (kIsDebugBuild && signal_number == SIGSEGV) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700357 PrintFileToLog("/proc/self/maps", LogSeverity::FATAL_WITHOUT_ABORT);
Hiroshi Yamauchiae0d7de2015-05-22 18:15:10 -0700358 }
Mathieu Chartier15d34022014-02-26 17:16:38 -0800359 Runtime* runtime = Runtime::Current();
360 if (runtime != nullptr) {
Andreas Gampe038bb222015-01-13 19:48:14 -0800361 if (IsTimeoutSignal(signal_number)) {
362 // Special timeout signal. Try to dump all threads.
Andreas Gampe0d0ce272016-03-08 20:29:49 -0800363 // Note: Do not use DumpForSigQuit, as that might disable native unwind, but the native parts
364 // are of value here.
Andreas Gampeef295362016-10-11 20:04:11 -0700365 runtime->GetThreadList()->Dump(std::cerr, kDumpNativeStackOnTimeout);
366 std::cerr << std::endl;
Andreas Gampe038bb222015-01-13 19:48:14 -0800367 }
Mathieu Chartier15d34022014-02-26 17:16:38 -0800368 gc::Heap* heap = runtime->GetHeap();
Andreas Gampeef295362016-10-11 20:04:11 -0700369 std::cerr << "Fault message: " << runtime->GetFaultMessage() << std::endl;
Mathieu Chartierc2f4d022014-03-03 16:11:42 -0800370 if (kDumpHeapObjectOnSigsevg && heap != nullptr && info != nullptr) {
Andreas Gampeef295362016-10-11 20:04:11 -0700371 std::cerr << "Dump heap object at fault address: " << std::endl;
372 heap->DumpObject(std::cerr, reinterpret_cast<mirror::Object*>(info->si_addr));
373 std::cerr << std::endl;
Mathieu Chartier15d34022014-02-26 17:16:38 -0800374 }
375 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700376 if (getenv("debug_db_uid") != nullptr || getenv("art_wait_for_gdb_on_crash") != nullptr) {
Andreas Gampeef295362016-10-11 20:04:11 -0700377 std::cerr << "********************************************************\n"
378 << "* Process " << getpid() << " thread " << tid << " \"" << thread_name
379 << "\""
380 << " has been suspended while crashing.\n"
381 << "* Attach gdb:\n"
382 << "* gdb -p " << tid << "\n"
383 << "********************************************************"
384 << std::endl;
Elliott Hughes2554cb92012-04-18 17:19:26 -0700385 // Wait for debugger to attach.
386 while (true) {
387 }
Elliott Hughes457005c2012-04-16 13:54:25 -0700388 }
Ian Rogersc5f17732014-06-05 20:48:42 -0700389#ifdef __linux__
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700390 // Remove our signal handler for this signal...
391 struct sigaction action;
392 memset(&action, 0, sizeof(action));
393 sigemptyset(&action.sa_mask);
394 action.sa_handler = SIG_DFL;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700395 sigaction(signal_number, &action, nullptr);
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700396 // ...and re-raise so we die with the appropriate status.
397 kill(getpid(), signal_number);
Ian Rogersc5f17732014-06-05 20:48:42 -0700398#else
399 exit(EXIT_FAILURE);
400#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700401}
402
Elliott Hughes457005c2012-04-16 13:54:25 -0700403void Runtime::InitPlatformSignalHandlers() {
404 // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
405 struct sigaction action;
406 memset(&action, 0, sizeof(action));
407 sigemptyset(&action.sa_mask);
408 action.sa_sigaction = HandleUnexpectedSignal;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700409 // Use the three-argument sa_sigaction handler.
410 action.sa_flags |= SA_SIGINFO;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700411 // Use the alternate signal stack so we can catch stack overflows.
412 action.sa_flags |= SA_ONSTACK;
Elliott Hughes457005c2012-04-16 13:54:25 -0700413
414 int rc = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700415 rc += sigaction(SIGABRT, &action, nullptr);
416 rc += sigaction(SIGBUS, &action, nullptr);
417 rc += sigaction(SIGFPE, &action, nullptr);
418 rc += sigaction(SIGILL, &action, nullptr);
419 rc += sigaction(SIGPIPE, &action, nullptr);
420 rc += sigaction(SIGSEGV, &action, nullptr);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700421#if defined(SIGSTKFLT)
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700422 rc += sigaction(SIGSTKFLT, &action, nullptr);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700423#endif
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700424 rc += sigaction(SIGTRAP, &action, nullptr);
Andreas Gampe038bb222015-01-13 19:48:14 -0800425 // Special dump-all timeout.
Andreas Gampecbfded42015-01-14 18:35:01 -0800426 if (GetTimeoutSignal() != -1) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700427 rc += sigaction(GetTimeoutSignal(), &action, nullptr);
Andreas Gampecbfded42015-01-14 18:35:01 -0800428 }
Elliott Hughes457005c2012-04-16 13:54:25 -0700429 CHECK_EQ(rc, 0);
430}
431
Elliott Hughesffe67362011-07-17 12:09:27 -0700432} // namespace art