blob: ad61cf373bad5d82d347a5b1c6a5d83083a8b70b [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
Roland Levillain21482ad2017-01-19 20:04:27 +000023#include "runtime_common.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070024
25namespace art {
26
Roland Levillain21482ad2017-01-19 20:04:27 +000027void HandleUnexpectedSignalLinux(int signal_number, siginfo_t* info, void* raw_context) {
28 HandleUnexpectedSignalCommon(signal_number, info, raw_context, /* running_on_linux */ true);
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029
Mathieu Chartier2cebb242015-04-21 16:50:40 -070030 if (getenv("debug_db_uid") != nullptr || getenv("art_wait_for_gdb_on_crash") != nullptr) {
Roland Levillain21482ad2017-01-19 20:04:27 +000031 pid_t tid = GetTid();
32 std::string thread_name(GetThreadName(tid));
Andreas Gampeef295362016-10-11 20:04:11 -070033 std::cerr << "********************************************************\n"
34 << "* Process " << getpid() << " thread " << tid << " \"" << thread_name
35 << "\""
36 << " has been suspended while crashing.\n"
37 << "* Attach gdb:\n"
38 << "* gdb -p " << tid << "\n"
39 << "********************************************************"
40 << std::endl;
Elliott Hughes2554cb92012-04-18 17:19:26 -070041 // Wait for debugger to attach.
42 while (true) {
43 }
Elliott Hughes457005c2012-04-16 13:54:25 -070044 }
Ian Rogersc5f17732014-06-05 20:48:42 -070045#ifdef __linux__
Elliott Hughesd06a6c72012-05-30 17:59:06 -070046 // Remove our signal handler for this signal...
47 struct sigaction action;
48 memset(&action, 0, sizeof(action));
49 sigemptyset(&action.sa_mask);
50 action.sa_handler = SIG_DFL;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070051 sigaction(signal_number, &action, nullptr);
Elliott Hughesd06a6c72012-05-30 17:59:06 -070052 // ...and re-raise so we die with the appropriate status.
53 kill(getpid(), signal_number);
Ian Rogersc5f17732014-06-05 20:48:42 -070054#else
55 exit(EXIT_FAILURE);
56#endif
Elliott Hughes457005c2012-04-16 13:54:25 -070057}
58
Elliott Hughes457005c2012-04-16 13:54:25 -070059void Runtime::InitPlatformSignalHandlers() {
60 // 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 +000061 InitPlatformSignalHandlersCommon(HandleUnexpectedSignalLinux,
62 nullptr,
63 /* handle_timeout_signal */ true);
Elliott Hughes457005c2012-04-16 13:54:25 -070064}
65
Elliott Hughesffe67362011-07-17 12:09:27 -070066} // namespace art