Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 16 | |
| 17 | #include "runtime.h" |
| 18 | |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 19 | #include <signal.h> |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 20 | |
Andreas Gampe | ef29536 | 2016-10-11 20:04:11 -0700 | [diff] [blame] | 21 | #include <iostream> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 22 | |
Andreas Gampe | 163355e | 2017-07-05 12:59:29 -0700 | [diff] [blame] | 23 | #include "base/memory_tool.h" |
Roland Levillain | 21482ad | 2017-01-19 20:04:27 +0000 | [diff] [blame] | 24 | #include "runtime_common.h" |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Roland Levillain | 21482ad | 2017-01-19 20:04:27 +0000 | [diff] [blame] | 28 | void HandleUnexpectedSignalLinux(int signal_number, siginfo_t* info, void* raw_context) { |
Andreas Gampe | c6fe427 | 2017-06-01 20:14:58 -0700 | [diff] [blame] | 29 | // 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, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 34 | /* handle_timeout_signal= */ true, |
| 35 | /* dump_on_stderr= */ true); |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 36 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 37 | if (getenv("debug_db_uid") != nullptr || getenv("art_wait_for_gdb_on_crash") != nullptr) { |
Roland Levillain | 21482ad | 2017-01-19 20:04:27 +0000 | [diff] [blame] | 38 | pid_t tid = GetTid(); |
| 39 | std::string thread_name(GetThreadName(tid)); |
Andreas Gampe | ef29536 | 2016-10-11 20:04:11 -0700 | [diff] [blame] | 40 | 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 Hughes | 2554cb9 | 2012-04-18 17:19:26 -0700 | [diff] [blame] | 48 | // Wait for debugger to attach. |
| 49 | while (true) { |
| 50 | } |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 51 | } |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 52 | #ifdef __linux__ |
Elliott Hughes | d06a6c7 | 2012-05-30 17:59:06 -0700 | [diff] [blame] | 53 | // 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 Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 58 | sigaction(signal_number, &action, nullptr); |
Elliott Hughes | d06a6c7 | 2012-05-30 17:59:06 -0700 | [diff] [blame] | 59 | // ...and re-raise so we die with the appropriate status. |
| 60 | kill(getpid(), signal_number); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 61 | #else |
| 62 | exit(EXIT_FAILURE); |
| 63 | #endif |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 66 | void Runtime::InitPlatformSignalHandlers() { |
Andreas Gampe | 163355e | 2017-07-05 12:59:29 -0700 | [diff] [blame] | 67 | 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 Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 77 | // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens. |
Roland Levillain | 21482ad | 2017-01-19 20:04:27 +0000 | [diff] [blame] | 78 | InitPlatformSignalHandlersCommon(HandleUnexpectedSignalLinux, |
| 79 | nullptr, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 80 | /* handle_timeout_signal= */ true); |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 83 | } // namespace art |