blob: a3e7bb13ab57b1c9dbe38d688d1dfa05212727c3 [file] [log] [blame]
Elliott Hughese27955c2011-08-26 15:21:24 -07001/*
2 * Copyright (C) 2008 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 "signal_catcher.h"
18
19#include <pthread.h>
20#include <signal.h>
21#include <stdlib.h>
22#include <sys/time.h>
23#include <unistd.h>
24
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070025#include "class_linker.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070026#include "heap.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "runtime.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070028#include "thread.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070029#include "thread_list.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070030#include "utils.h"
31
32namespace art {
33
Elliott Hughes5f791332011-09-15 17:45:30 -070034SignalCatcher::SignalCatcher()
35 : lock_("SignalCatcher lock"), cond_("SignalCatcher::cond_"), thread_(NULL) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070036 SetHaltFlag(false);
37
Elliott Hughese27955c2011-08-26 15:21:24 -070038 // Create a raw pthread; its start routine will attach to the runtime.
Elliott Hughes8d768a92011-09-14 16:35:25 -070039 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Run, this), "signal catcher thread");
40
Elliott Hughes8d768a92011-09-14 16:35:25 -070041 MutexLock mu(lock_);
42 while (thread_ == NULL) {
Elliott Hughes5f791332011-09-15 17:45:30 -070043 cond_.Wait(lock_);
Elliott Hughese27955c2011-08-26 15:21:24 -070044 }
45}
46
47SignalCatcher::~SignalCatcher() {
48 // Since we know the thread is just sitting around waiting for signals
49 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070050 SetHaltFlag(true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070051 CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown");
52 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "signal catcher shutdown");
Elliott Hughese27955c2011-08-26 15:21:24 -070053}
54
Elliott Hughes5fe594f2011-09-08 12:33:17 -070055void SignalCatcher::SetHaltFlag(bool new_value) {
56 MutexLock mu(lock_);
57 halt_ = new_value;
58}
59
60bool SignalCatcher::ShouldHalt() {
61 MutexLock mu(lock_);
62 return halt_;
63}
64
Elliott Hughese27955c2011-08-26 15:21:24 -070065void SignalCatcher::HandleSigQuit() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070066 Runtime* runtime = Runtime::Current();
67 ThreadList* thread_list = runtime->GetThreadList();
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070068 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughesaccd83d2011-10-17 14:25:58 -070069
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070070 LOG(INFO) << "Heap lock owner tid: " << Heap::GetLockOwner() << "\n"
71 << "ThreadList lock owner tid: " << thread_list->GetLockOwner() << "\n"
Brian Carlstrom47d237a2011-10-18 15:08:33 -070072 << "ClassLinker classes lock owner tid: " << class_linker->GetClassesLockOwner() << "\n"
73 << "ClassLinker dex lock owner tid: " << class_linker->GetDexLockOwner() << "\n";
Elliott Hughesaccd83d2011-10-17 14:25:58 -070074
75 thread_list->SuspendAll();
Elliott Hughese27955c2011-08-26 15:21:24 -070076
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070077 std::ostringstream os;
Elliott Hughesd92bec42011-09-02 17:04:36 -070078 os << "\n"
79 << "\n"
80 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -070081
Elliott Hughesd92bec42011-09-02 17:04:36 -070082 std::string cmdline;
83 if (ReadFileToString("/proc/self/cmdline", &cmdline)) {
84 std::replace(cmdline.begin(), cmdline.end(), '\0', ' ');
85 os << "Cmd line: " << cmdline << "\n";
86 }
Elliott Hughese27955c2011-08-26 15:21:24 -070087
Elliott Hughesaccd83d2011-10-17 14:25:58 -070088 runtime->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -070089
Elliott Hughesd92bec42011-09-02 17:04:36 -070090 std::string maps;
91 if (ReadFileToString("/proc/self/maps", &maps)) {
92 os << "/proc/self/maps:\n" << maps;
93 }
94
95 os << "----- end " << getpid() << " -----";
Elliott Hughese27955c2011-08-26 15:21:24 -070096
Elliott Hughesaccd83d2011-10-17 14:25:58 -070097 thread_list->ResumeAll();
Elliott Hughese27955c2011-08-26 15:21:24 -070098
Elliott Hughesd92bec42011-09-02 17:04:36 -070099 LOG(INFO) << os.str();
Elliott Hughese27955c2011-08-26 15:21:24 -0700100}
101
102void SignalCatcher::HandleSigUsr1() {
103 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
104 Heap::CollectGarbage();
105}
106
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700107int WaitForSignal(Thread* thread, sigset_t& mask) {
Elliott Hughes93e74e82011-09-13 11:07:03 -0700108 ScopedThreadStateChange tsc(thread, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700109
110 // Signals for sigwait() must be blocked but not ignored. We
111 // block signals like SIGQUIT for all threads, so the condition
112 // is met. When the signal hits, we wake up, without any signal
113 // handlers being invoked.
114
115 // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
116 int signal_number;
117 int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
118 if (rc != 0) {
119 PLOG(FATAL) << "sigwait failed";
120 }
121
122 return signal_number;
123}
124
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700125void* SignalCatcher::Run(void* arg) {
126 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
127 CHECK(signal_catcher != NULL);
128
Elliott Hughes8d768a92011-09-14 16:35:25 -0700129 Runtime* runtime = Runtime::Current();
130 runtime->AttachCurrentThread("Signal Catcher", true);
131
132 {
133 MutexLock mu(signal_catcher->lock_);
134 signal_catcher->thread_ = Thread::Current();
Elliott Hughes5f791332011-09-15 17:45:30 -0700135 signal_catcher->cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700136 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700137
Elliott Hughese27955c2011-08-26 15:21:24 -0700138 // Set up mask with signals we want to handle.
139 sigset_t mask;
140 sigemptyset(&mask);
141 sigaddset(&mask, SIGQUIT);
142 sigaddset(&mask, SIGUSR1);
143
144 while (true) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700145 int signal_number = WaitForSignal(signal_catcher->thread_, mask);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700146 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700147 runtime->DetachCurrentThread();
Elliott Hughese27955c2011-08-26 15:21:24 -0700148 return NULL;
149 }
150
Elliott Hughes8d768a92011-09-14 16:35:25 -0700151 LOG(INFO) << *signal_catcher->thread_ << ": reacting to signal " << signal_number;
Elliott Hughese27955c2011-08-26 15:21:24 -0700152 switch (signal_number) {
153 case SIGQUIT:
154 HandleSigQuit();
155 break;
156 case SIGUSR1:
157 HandleSigUsr1();
158 break;
159 default:
160 LOG(ERROR) << "Unexpected signal %d" << signal_number;
161 break;
162 }
163 }
164}
165
166} // namespace art