blob: 14e557450b1adcb2ed7d5a14fd2e6ed5ddc0c7c5 [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"
24#include "base/stringprintf.h"
25#include "thread-inl.h"
26#include "utils.h"
Elliott Hughesffe67362011-07-17 12:09:27 -070027
28namespace art {
29
Mathieu Chartier15d34022014-02-26 17:16:38 -080030struct sigaction old_action;
31void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
32 static bool handlingUnexpectedSignal = false;
33 if (handlingUnexpectedSignal) {
34 LogMessageData data(__FILE__, __LINE__, INTERNAL_FATAL, -1);
35 LogMessage::LogLine(data, "HandleUnexpectedSignal reentered\n");
36 _exit(1);
37 }
38 handlingUnexpectedSignal = true;
39 gAborting++; // set before taking any locks
40 MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
41
42 Runtime* runtime = Runtime::Current();
43 if (runtime != nullptr) {
44 // Print this out first in case DumpObject faults.
45 LOG(INTERNAL_FATAL) << "Fault message: " << runtime->GetFaultMessage();
46 gc::Heap* heap = runtime->GetHeap();
47 if (heap != nullptr && info != nullptr) {
48 LOG(INTERNAL_FATAL) << "Dump heap object at fault address: ";
49 heap->DumpObject(LOG(INTERNAL_FATAL), reinterpret_cast<mirror::Object*>(info->si_addr));
50 }
51 }
52 // Run the old signal handler.
53 old_action.sa_sigaction(signal_number, info, raw_context);
54}
55
Elliott Hughes457005c2012-04-16 13:54:25 -070056void Runtime::InitPlatformSignalHandlers() {
Mathieu Chartier15d34022014-02-26 17:16:38 -080057 // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
58 struct sigaction action;
59 memset(&action, 0, sizeof(action));
60 sigemptyset(&action.sa_mask);
61 action.sa_sigaction = HandleUnexpectedSignal;
62 // Use the three-argument sa_sigaction handler.
63 action.sa_flags |= SA_SIGINFO;
64 // Use the alternate signal stack so we can catch stack overflows.
65 action.sa_flags |= SA_ONSTACK;
66 int rc = 0;
67 rc += sigaction(SIGSEGV, &action, &old_action);
68 CHECK_EQ(rc, 0);
Elliott Hughes457005c2012-04-16 13:54:25 -070069}
70
Elliott Hughesffe67362011-07-17 12:09:27 -070071} // namespace art