Home | History | Annotate | Download | only in runtime
      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 "base/memory_tool.h"
     24 #include "runtime_common.h"
     25 
     26 namespace art {
     27 
     28 void HandleUnexpectedSignalLinux(int signal_number, siginfo_t* info, void* raw_context) {
     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,
     34                                /* handle_timeout_signal */ true,
     35                                /* dump_on_stderr */ true);
     36 
     37   if (getenv("debug_db_uid") != nullptr || getenv("art_wait_for_gdb_on_crash") != nullptr) {
     38     pid_t tid = GetTid();
     39     std::string thread_name(GetThreadName(tid));
     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;
     48     // Wait for debugger to attach.
     49     while (true) {
     50     }
     51   }
     52 #ifdef __linux__
     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;
     58   sigaction(signal_number, &action, nullptr);
     59   // ...and re-raise so we die with the appropriate status.
     60   kill(getpid(), signal_number);
     61 #else
     62   exit(EXIT_FAILURE);
     63 #endif
     64 }
     65 
     66 void Runtime::InitPlatformSignalHandlers() {
     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   }
     77   // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
     78   InitPlatformSignalHandlersCommon(HandleUnexpectedSignalLinux,
     79                                    nullptr,
     80                                    /* handle_timeout_signal */ true);
     81 }
     82 
     83 }  // namespace art
     84