blob: 0a996a9e550df7a315dff555a90a4ffe16f06c20 [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
Mathieu Chartier15d34022014-02-26 17:16:38 -080017#include <signal.h>
18#include <string.h>
19#include <sys/utsname.h>
20#include <inttypes.h>
21
22#include "base/logging.h"
23#include "base/mutex.h"
Mathieu Chartier15d34022014-02-26 17:16:38 -080024#include "thread-inl.h"
25#include "utils.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070026
27namespace art {
28
Mathieu Chartier28c83592014-03-04 13:40:17 -080029static constexpr bool kUseSignalHandler = false;
Mathieu Chartierc2f4d022014-03-03 16:11:42 -080030
Mathieu Chartier15d34022014-02-26 17:16:38 -080031struct sigaction old_action;
32void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
Ian Rogersc7dd2952014-10-21 23:31:19 -070033 static bool handling_unexpected_signal = false;
34 if (handling_unexpected_signal) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070035 LogHelper::LogLineLowStack(__FILE__,
36 __LINE__,
37 ::android::base::FATAL_WITHOUT_ABORT,
38 "HandleUnexpectedSignal reentered\n");
Mathieu Chartier15d34022014-02-26 17:16:38 -080039 _exit(1);
40 }
Ian Rogersc7dd2952014-10-21 23:31:19 -070041 handling_unexpected_signal = true;
Nicolas Geoffraydb978712014-12-09 13:33:38 +000042 gAborting++; // set before taking any locks
Mathieu Chartier15d34022014-02-26 17:16:38 -080043 MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
44
45 Runtime* runtime = Runtime::Current();
46 if (runtime != nullptr) {
47 // Print this out first in case DumpObject faults.
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070048 LOG(FATAL_WITHOUT_ABORT) << "Fault message: " << runtime->GetFaultMessage();
Mathieu Chartier15d34022014-02-26 17:16:38 -080049 }
50 // Run the old signal handler.
51 old_action.sa_sigaction(signal_number, info, raw_context);
52}
53
Elliott Hughes457005c2012-04-16 13:54:25 -070054void Runtime::InitPlatformSignalHandlers() {
Mathieu Chartier28c83592014-03-04 13:40:17 -080055 if (kUseSignalHandler) {
56 struct sigaction action;
57 memset(&action, 0, sizeof(action));
58 sigemptyset(&action.sa_mask);
59 action.sa_sigaction = HandleUnexpectedSignal;
60 // Use the three-argument sa_sigaction handler.
61 action.sa_flags |= SA_SIGINFO;
62 // Use the alternate signal stack so we can catch stack overflows.
63 action.sa_flags |= SA_ONSTACK;
64 int rc = 0;
65 rc += sigaction(SIGSEGV, &action, &old_action);
66 CHECK_EQ(rc, 0);
67 }
Elliott Hughes457005c2012-04-16 13:54:25 -070068}
69
Elliott Hughesffe67362011-07-17 12:09:27 -070070} // namespace art