blob: 631355325529b8f46157b874ebb522643565398e [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 Hughesffe67362011-07-17 12:09:27 -070020
Andreas Gampeef295362016-10-11 20:04:11 -070021#include <iostream>
Ian Rogersc7dd2952014-10-21 23:31:19 -070022
Andreas Gampe163355e2017-07-05 12:59:29 -070023#include "base/memory_tool.h"
Roland Levillain21482ad2017-01-19 20:04:27 +000024#include "runtime_common.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070025
26namespace art {
27
Roland Levillain21482ad2017-01-19 20:04:27 +000028void HandleUnexpectedSignalLinux(int signal_number, siginfo_t* info, void* raw_context) {
Andreas Gampec6fe4272017-06-01 20:14:58 -070029 // Linux is mainly used for host testing. Under those conditions, react to the timeout signal,
30 // and dump to stderr to avoid missing output on double-faults.
31 HandleUnexpectedSignalCommon(signal_number,
32 info,
33 raw_context,
34 /* handle_timeout_signal */ true,
35 /* dump_on_stderr */ true);
Andreas Gampe46ee31b2016-12-14 10:11:49 -080036
Mathieu Chartier2cebb242015-04-21 16:50:40 -070037 if (getenv("debug_db_uid") != nullptr || getenv("art_wait_for_gdb_on_crash") != nullptr) {
Roland Levillain21482ad2017-01-19 20:04:27 +000038 pid_t tid = GetTid();
39 std::string thread_name(GetThreadName(tid));
Andreas Gampeef295362016-10-11 20:04:11 -070040 std::cerr << "********************************************************\n"
41 << "* Process " << getpid() << " thread " << tid << " \"" << thread_name
42 << "\""
43 << " has been suspended while crashing.\n"
44 << "* Attach gdb:\n"
45 << "* gdb -p " << tid << "\n"
46 << "********************************************************"
47 << std::endl;
Elliott Hughes2554cb92012-04-18 17:19:26 -070048 // Wait for debugger to attach.
49 while (true) {
50 }
Elliott Hughes457005c2012-04-16 13:54:25 -070051 }
Ian Rogersc5f17732014-06-05 20:48:42 -070052#ifdef __linux__
Elliott Hughesd06a6c72012-05-30 17:59:06 -070053 // Remove our signal handler for this signal...
54 struct sigaction action;
55 memset(&action, 0, sizeof(action));
56 sigemptyset(&action.sa_mask);
57 action.sa_handler = SIG_DFL;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070058 sigaction(signal_number, &action, nullptr);
Elliott Hughesd06a6c72012-05-30 17:59:06 -070059 // ...and re-raise so we die with the appropriate status.
60 kill(getpid(), signal_number);
Ian Rogersc5f17732014-06-05 20:48:42 -070061#else
62 exit(EXIT_FAILURE);
63#endif
Elliott Hughes457005c2012-04-16 13:54:25 -070064}
65
Elliott Hughes457005c2012-04-16 13:54:25 -070066void Runtime::InitPlatformSignalHandlers() {
Andreas Gampe163355e2017-07-05 12:59:29 -070067 constexpr bool kIsASAN =
68#ifdef ADDRESS_SANITIZER
69 true;
70#else
71 false;
72#endif
73 if (!kIsTargetBuild && kIsASAN) {
74 // (Temporarily) try and let ASAN print abort stacks, as our code sometimes fails. b/31098551
75 return;
76 }
Elliott Hughes457005c2012-04-16 13:54:25 -070077 // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
Roland Levillain21482ad2017-01-19 20:04:27 +000078 InitPlatformSignalHandlersCommon(HandleUnexpectedSignalLinux,
79 nullptr,
80 /* handle_timeout_signal */ true);
Elliott Hughes457005c2012-04-16 13:54:25 -070081}
82
Elliott Hughesffe67362011-07-17 12:09:27 -070083} // namespace art