blob: 8237b06a5623b355b2735c171e4f855a91c0cf9b [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
Ian Rogersc7dd2952014-10-21 23:31:19 -070024#include <sstream>
25
26#include "base/dumpable.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "base/logging.h"
Andreas Gampefcf81d82015-01-14 20:09:14 -080028#include "base/macros.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080029#include "base/mutex.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080030#include "base/stringprintf.h"
Ian Rogersb48b9eb2014-02-28 16:20:21 -080031#include "thread-inl.h"
Andreas Gampe038bb222015-01-13 19:48:14 -080032#include "thread_list.h"
Elliott Hughes46e251b2012-05-22 15:10:45 -070033#include "utils.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070034
35namespace art {
36
Mathieu Chartierc2f4d022014-03-03 16:11:42 -080037static constexpr bool kDumpHeapObjectOnSigsevg = false;
Andreas Gampecbfded42015-01-14 18:35:01 -080038static constexpr bool kUseSigRTTimeout = true;
Mathieu Chartierc2f4d022014-03-03 16:11:42 -080039
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070040struct Backtrace {
Andreas Gampe628a61a2015-01-07 22:08:35 -080041 public:
42 explicit Backtrace(void* raw_context) : raw_context_(raw_context) {}
Ian Rogersc7dd2952014-10-21 23:31:19 -070043 void Dump(std::ostream& os) const {
Christopher Ferris6cff48f2014-01-26 21:36:13 -080044 DumpNativeStack(os, GetTid(), nullptr, "\t", nullptr, raw_context_);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070045 }
Andreas Gampe628a61a2015-01-07 22:08:35 -080046 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_;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070051};
52
Elliott Hughes76160052012-12-12 16:31:20 -080053struct OsInfo {
Ian Rogersc7dd2952014-10-21 23:31:19 -070054 void Dump(std::ostream& os) const {
Elliott Hughes058a6de2012-05-24 19:13:02 -070055 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
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070063static 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";
Elliott Hughes833770b2012-05-01 15:41:03 -070071#if defined(SIGSTKFLT)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070072 case SIGSTKFLT: return "SIGSTKFLT";
73#endif
74 case SIGTRAP: return "SIGTRAP";
75 }
76 return "??";
Elliott Hughesffe67362011-07-17 12:09:27 -070077}
78
Elliott Hughes457005c2012-04-16 13:54:25 -070079static 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";
Christopher Ferris12b8c9d2016-02-04 14:06:34 -0800117#if defined(SEGV_BNDERR)
118 case SEGV_BNDERR: return "SEGV_BNDERR";
119#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700120 }
121 break;
122 case SIGTRAP:
123 switch (signal_code) {
124 case TRAP_BRKPT: return "TRAP_BRKPT";
125 case TRAP_TRACE: return "TRAP_TRACE";
126 }
127 break;
128 }
129 // Then the other codes...
130 switch (signal_code) {
131 case SI_USER: return "SI_USER";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700132#if defined(SI_KERNEL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700133 case SI_KERNEL: return "SI_KERNEL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700134#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700135 case SI_QUEUE: return "SI_QUEUE";
136 case SI_TIMER: return "SI_TIMER";
137 case SI_MESGQ: return "SI_MESGQ";
138 case SI_ASYNCIO: return "SI_ASYNCIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700139#if defined(SI_SIGIO)
Elliott Hughes457005c2012-04-16 13:54:25 -0700140 case SI_SIGIO: return "SI_SIGIO";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700141#endif
142#if defined(SI_TKILL)
Elliott Hughes457005c2012-04-16 13:54:25 -0700143 case SI_TKILL: return "SI_TKILL";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700144#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700145 }
146 // Then give up...
147 return "?";
148}
149
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700150struct UContext {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700151 explicit UContext(void* raw_context) :
152 context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {
153 }
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700154
Ian Rogersc7dd2952014-10-21 23:31:19 -0700155 void Dump(std::ostream& os) const {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700156 // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157#if defined(__APPLE__) && defined(__i386__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700158 DumpRegister32(os, "eax", context->__ss.__eax);
159 DumpRegister32(os, "ebx", context->__ss.__ebx);
160 DumpRegister32(os, "ecx", context->__ss.__ecx);
161 DumpRegister32(os, "edx", context->__ss.__edx);
162 os << '\n';
163
164 DumpRegister32(os, "edi", context->__ss.__edi);
165 DumpRegister32(os, "esi", context->__ss.__esi);
166 DumpRegister32(os, "ebp", context->__ss.__ebp);
167 DumpRegister32(os, "esp", context->__ss.__esp);
168 os << '\n';
169
170 DumpRegister32(os, "eip", context->__ss.__eip);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700171 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700172 DumpRegister32(os, "eflags", context->__ss.__eflags);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700173 DumpX86Flags(os, context->__ss.__eflags);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700174 os << '\n';
175
176 DumpRegister32(os, "cs", context->__ss.__cs);
177 DumpRegister32(os, "ds", context->__ss.__ds);
178 DumpRegister32(os, "es", context->__ss.__es);
179 DumpRegister32(os, "fs", context->__ss.__fs);
180 os << '\n';
181 DumpRegister32(os, "gs", context->__ss.__gs);
182 DumpRegister32(os, "ss", context->__ss.__ss);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800183#elif defined(__linux__) && defined(__i386__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700184 DumpRegister32(os, "eax", context.gregs[REG_EAX]);
185 DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
186 DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
187 DumpRegister32(os, "edx", context.gregs[REG_EDX]);
188 os << '\n';
189
190 DumpRegister32(os, "edi", context.gregs[REG_EDI]);
191 DumpRegister32(os, "esi", context.gregs[REG_ESI]);
192 DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
193 DumpRegister32(os, "esp", context.gregs[REG_ESP]);
194 os << '\n';
195
196 DumpRegister32(os, "eip", context.gregs[REG_EIP]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700197 os << " ";
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700198 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
Elliott Hughes46e251b2012-05-22 15:10:45 -0700199 DumpX86Flags(os, context.gregs[REG_EFL]);
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700200 os << '\n';
201
202 DumpRegister32(os, "cs", context.gregs[REG_CS]);
203 DumpRegister32(os, "ds", context.gregs[REG_DS]);
204 DumpRegister32(os, "es", context.gregs[REG_ES]);
205 DumpRegister32(os, "fs", context.gregs[REG_FS]);
206 os << '\n';
207 DumpRegister32(os, "gs", context.gregs[REG_GS]);
208 DumpRegister32(os, "ss", context.gregs[REG_SS]);
Dmitry Petrochenko611c2c32014-02-10 14:48:12 +0700209#elif defined(__linux__) && defined(__x86_64__)
210 DumpRegister64(os, "rax", context.gregs[REG_RAX]);
211 DumpRegister64(os, "rbx", context.gregs[REG_RBX]);
212 DumpRegister64(os, "rcx", context.gregs[REG_RCX]);
213 DumpRegister64(os, "rdx", context.gregs[REG_RDX]);
214 os << '\n';
215
216 DumpRegister64(os, "rdi", context.gregs[REG_RDI]);
217 DumpRegister64(os, "rsi", context.gregs[REG_RSI]);
218 DumpRegister64(os, "rbp", context.gregs[REG_RBP]);
219 DumpRegister64(os, "rsp", context.gregs[REG_RSP]);
220 os << '\n';
221
222 DumpRegister64(os, "r8 ", context.gregs[REG_R8]);
223 DumpRegister64(os, "r9 ", context.gregs[REG_R9]);
224 DumpRegister64(os, "r10", context.gregs[REG_R10]);
225 DumpRegister64(os, "r11", context.gregs[REG_R11]);
226 os << '\n';
227
228 DumpRegister64(os, "r12", context.gregs[REG_R12]);
229 DumpRegister64(os, "r13", context.gregs[REG_R13]);
230 DumpRegister64(os, "r14", context.gregs[REG_R14]);
231 DumpRegister64(os, "r15", context.gregs[REG_R15]);
232 os << '\n';
233
234 DumpRegister64(os, "rip", context.gregs[REG_RIP]);
235 os << " ";
236 DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
237 DumpX86Flags(os, context.gregs[REG_EFL]);
238 os << '\n';
239
240 DumpRegister32(os, "cs", (context.gregs[REG_CSGSFS]) & 0x0FFFF);
241 DumpRegister32(os, "gs", (context.gregs[REG_CSGSFS] >> 16) & 0x0FFFF);
242 DumpRegister32(os, "fs", (context.gregs[REG_CSGSFS] >> 32) & 0x0FFFF);
243 os << '\n';
Ian Rogersef7d42f2014-01-06 12:55:46 -0800244#else
245 os << "Unknown architecture/word size/OS in ucontext dump";
Elliott Hughesac8097f2012-04-16 14:59:44 -0700246#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700247 }
248
Ian Rogersc7dd2952014-10-21 23:31:19 -0700249 void DumpRegister32(std::ostream& os, const char* name, uint32_t value) const {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700250 os << StringPrintf(" %6s: 0x%08x", name, value);
251 }
252
Ian Rogersc7dd2952014-10-21 23:31:19 -0700253 void DumpRegister64(std::ostream& os, const char* name, uint64_t value) const {
Dmitry Petrochenko611c2c32014-02-10 14:48:12 +0700254 os << StringPrintf(" %6s: 0x%016" PRIx64, name, value);
255 }
256
Ian Rogersc7dd2952014-10-21 23:31:19 -0700257 void DumpX86Flags(std::ostream& os, uint32_t flags) const {
Elliott Hughes46e251b2012-05-22 15:10:45 -0700258 os << " [";
259 if ((flags & (1 << 0)) != 0) {
260 os << " CF";
261 }
262 if ((flags & (1 << 2)) != 0) {
263 os << " PF";
264 }
265 if ((flags & (1 << 4)) != 0) {
266 os << " AF";
267 }
268 if ((flags & (1 << 6)) != 0) {
269 os << " ZF";
270 }
271 if ((flags & (1 << 7)) != 0) {
272 os << " SF";
273 }
274 if ((flags & (1 << 8)) != 0) {
275 os << " TF";
276 }
277 if ((flags & (1 << 9)) != 0) {
278 os << " IF";
279 }
280 if ((flags & (1 << 10)) != 0) {
281 os << " DF";
282 }
283 if ((flags & (1 << 11)) != 0) {
284 os << " OF";
285 }
286 os << " ]";
287 }
288
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700289 mcontext_t& context;
290};
291
Andreas Gampecbfded42015-01-14 18:35:01 -0800292// Return the signal number we recognize as timeout. -1 means not active/supported.
Andreas Gampe038bb222015-01-13 19:48:14 -0800293static int GetTimeoutSignal() {
Andreas Gampecbfded42015-01-14 18:35:01 -0800294#if defined(__APPLE__)
295 // Mac does not support realtime signals.
Andreas Gampefcf81d82015-01-14 20:09:14 -0800296 UNUSED(kUseSigRTTimeout);
Andreas Gampecbfded42015-01-14 18:35:01 -0800297 return -1;
298#else
299 return kUseSigRTTimeout ? (SIGRTMIN + 2) : -1;
300#endif
Andreas Gampe038bb222015-01-13 19:48:14 -0800301}
302
303static bool IsTimeoutSignal(int signal_number) {
304 return signal_number == GetTimeoutSignal();
305}
306
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800307void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
308 static bool handlingUnexpectedSignal = false;
309 if (handlingUnexpectedSignal) {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700310 LogMessage::LogLine(__FILE__, __LINE__, INTERNAL_FATAL, "HandleUnexpectedSignal reentered\n");
Andreas Gampe038bb222015-01-13 19:48:14 -0800311 if (IsTimeoutSignal(signal_number)) {
312 // Ignore a recursive timeout.
313 return;
314 }
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800315 _exit(1);
316 }
317 handlingUnexpectedSignal = true;
318
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000319 gAborting++; // set before taking any locks
Ian Rogers50b35e22012-10-04 10:09:15 -0700320 MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700321
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700322 bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
323 signal_number == SIGFPE || signal_number == SIGSEGV);
324
Elliott Hughes76160052012-12-12 16:31:20 -0800325 OsInfo os_info;
Elliott Hughes98eedd82012-06-11 17:52:56 -0700326 const char* cmd_line = GetCmdLine();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700327 if (cmd_line == nullptr) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700328 cmd_line = "<unset>"; // Because no-one called InitLogging.
Elliott Hughes98eedd82012-06-11 17:52:56 -0700329 }
Elliott Hughes289be852012-06-12 13:57:20 -0700330 pid_t tid = GetTid();
331 std::string thread_name(GetThreadName(tid));
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700332 UContext thread_context(raw_context);
Andreas Gampe628a61a2015-01-07 22:08:35 -0800333 Backtrace thread_backtrace(raw_context);
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700334
Elliott Hughes457005c2012-04-16 13:54:25 -0700335 LOG(INTERNAL_FATAL) << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"
336 << StringPrintf("Fatal signal %d (%s), code %d (%s)",
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700337 signal_number, GetSignalName(signal_number),
Elliott Hughes457005c2012-04-16 13:54:25 -0700338 info->si_code,
339 GetSignalCodeName(signal_number, info->si_code))
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700340 << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << "\n"
Elliott Hughes76160052012-12-12 16:31:20 -0800341 << "OS: " << Dumpable<OsInfo>(os_info) << "\n"
Elliott Hughes98eedd82012-06-11 17:52:56 -0700342 << "Cmdline: " << cmd_line << "\n"
Elliott Hughes289be852012-06-12 13:57:20 -0700343 << "Thread: " << tid << " \"" << thread_name << "\"\n"
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700344 << "Registers:\n" << Dumpable<UContext>(thread_context) << "\n"
345 << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace);
Hiroshi Yamauchiae0d7de2015-05-22 18:15:10 -0700346 if (kIsDebugBuild && signal_number == SIGSEGV) {
347 PrintFileToLog("/proc/self/maps", LogSeverity::INTERNAL_FATAL);
348 }
Mathieu Chartier15d34022014-02-26 17:16:38 -0800349 Runtime* runtime = Runtime::Current();
350 if (runtime != nullptr) {
Andreas Gampe038bb222015-01-13 19:48:14 -0800351 if (IsTimeoutSignal(signal_number)) {
352 // Special timeout signal. Try to dump all threads.
353 runtime->GetThreadList()->DumpForSigQuit(LOG(INTERNAL_FATAL));
354 }
Mathieu Chartier15d34022014-02-26 17:16:38 -0800355 gc::Heap* heap = runtime->GetHeap();
356 LOG(INTERNAL_FATAL) << "Fault message: " << runtime->GetFaultMessage();
Mathieu Chartierc2f4d022014-03-03 16:11:42 -0800357 if (kDumpHeapObjectOnSigsevg && heap != nullptr && info != nullptr) {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800358 LOG(INTERNAL_FATAL) << "Dump heap object at fault address: ";
359 heap->DumpObject(LOG(INTERNAL_FATAL), reinterpret_cast<mirror::Object*>(info->si_addr));
360 }
361 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700362 if (getenv("debug_db_uid") != nullptr || getenv("art_wait_for_gdb_on_crash") != nullptr) {
Elliott Hughes2554cb92012-04-18 17:19:26 -0700363 LOG(INTERNAL_FATAL) << "********************************************************\n"
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700364 << "* Process " << getpid() << " thread " << tid << " \"" << thread_name
365 << "\""
Elliott Hughes289be852012-06-12 13:57:20 -0700366 << " has been suspended while crashing.\n"
367 << "* Attach gdb:\n"
368 << "* gdb -p " << tid << "\n"
Elliott Hughes2554cb92012-04-18 17:19:26 -0700369 << "********************************************************\n";
370 // Wait for debugger to attach.
371 while (true) {
372 }
Elliott Hughes457005c2012-04-16 13:54:25 -0700373 }
Ian Rogersc5f17732014-06-05 20:48:42 -0700374#ifdef __linux__
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700375 // Remove our signal handler for this signal...
376 struct sigaction action;
377 memset(&action, 0, sizeof(action));
378 sigemptyset(&action.sa_mask);
379 action.sa_handler = SIG_DFL;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700380 sigaction(signal_number, &action, nullptr);
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700381 // ...and re-raise so we die with the appropriate status.
382 kill(getpid(), signal_number);
Ian Rogersc5f17732014-06-05 20:48:42 -0700383#else
384 exit(EXIT_FAILURE);
385#endif
Elliott Hughes457005c2012-04-16 13:54:25 -0700386}
387
Elliott Hughes457005c2012-04-16 13:54:25 -0700388void Runtime::InitPlatformSignalHandlers() {
389 // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
390 struct sigaction action;
391 memset(&action, 0, sizeof(action));
392 sigemptyset(&action.sa_mask);
393 action.sa_sigaction = HandleUnexpectedSignal;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700394 // Use the three-argument sa_sigaction handler.
395 action.sa_flags |= SA_SIGINFO;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700396 // Use the alternate signal stack so we can catch stack overflows.
397 action.sa_flags |= SA_ONSTACK;
Elliott Hughes457005c2012-04-16 13:54:25 -0700398
399 int rc = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700400 rc += sigaction(SIGABRT, &action, nullptr);
401 rc += sigaction(SIGBUS, &action, nullptr);
402 rc += sigaction(SIGFPE, &action, nullptr);
403 rc += sigaction(SIGILL, &action, nullptr);
404 rc += sigaction(SIGPIPE, &action, nullptr);
405 rc += sigaction(SIGSEGV, &action, nullptr);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700406#if defined(SIGSTKFLT)
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700407 rc += sigaction(SIGSTKFLT, &action, nullptr);
Elliott Hughesac8097f2012-04-16 14:59:44 -0700408#endif
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700409 rc += sigaction(SIGTRAP, &action, nullptr);
Andreas Gampe038bb222015-01-13 19:48:14 -0800410 // Special dump-all timeout.
Andreas Gampecbfded42015-01-14 18:35:01 -0800411 if (GetTimeoutSignal() != -1) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700412 rc += sigaction(GetTimeoutSignal(), &action, nullptr);
Andreas Gampecbfded42015-01-14 18:35:01 -0800413 }
Elliott Hughes457005c2012-04-16 13:54:25 -0700414 CHECK_EQ(rc, 0);
415}
416
Elliott Hughesffe67362011-07-17 12:09:27 -0700417} // namespace art