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  */
16 
17 #include "runtime.h"
18 
19 #include <signal.h>
20 
21 #include <iostream>
22 
23 #include "runtime_common.h"
24 
25 namespace art {
26 
HandleUnexpectedSignalLinux(int signal_number,siginfo_t * info,void * raw_context)27 void HandleUnexpectedSignalLinux(int signal_number, siginfo_t* info, void* raw_context) {
28   HandleUnexpectedSignalCommon(signal_number, info, raw_context, /* running_on_linux */ true);
29 
30   if (getenv("debug_db_uid") != nullptr || getenv("art_wait_for_gdb_on_crash") != nullptr) {
31     pid_t tid = GetTid();
32     std::string thread_name(GetThreadName(tid));
33     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;
41     // Wait for debugger to attach.
42     while (true) {
43     }
44   }
45 #ifdef __linux__
46   // 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;
51   sigaction(signal_number, &action, nullptr);
52   // ...and re-raise so we die with the appropriate status.
53   kill(getpid(), signal_number);
54 #else
55   exit(EXIT_FAILURE);
56 #endif
57 }
58 
InitPlatformSignalHandlers()59 void Runtime::InitPlatformSignalHandlers() {
60   // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
61   InitPlatformSignalHandlersCommon(HandleUnexpectedSignalLinux,
62                                    nullptr,
63                                    /* handle_timeout_signal */ true);
64 }
65 
66 }  // namespace art
67