blob: 22d08b5e5da7a505ef757e81c414b819ef0df918 [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"
72 << "ClassLinker lock owner tid: " << class_linker->GetLockOwner() << "\n";
Elliott Hughesaccd83d2011-10-17 14:25:58 -070073
74 thread_list->SuspendAll();
Elliott Hughese27955c2011-08-26 15:21:24 -070075
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070076 std::ostringstream os;
Elliott Hughesd92bec42011-09-02 17:04:36 -070077 os << "\n"
78 << "\n"
79 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -070080
Elliott Hughesd92bec42011-09-02 17:04:36 -070081 std::string cmdline;
82 if (ReadFileToString("/proc/self/cmdline", &cmdline)) {
83 std::replace(cmdline.begin(), cmdline.end(), '\0', ' ');
84 os << "Cmd line: " << cmdline << "\n";
85 }
Elliott Hughese27955c2011-08-26 15:21:24 -070086
Elliott Hughesaccd83d2011-10-17 14:25:58 -070087 runtime->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -070088
Elliott Hughesd92bec42011-09-02 17:04:36 -070089 std::string maps;
90 if (ReadFileToString("/proc/self/maps", &maps)) {
91 os << "/proc/self/maps:\n" << maps;
92 }
93
94 os << "----- end " << getpid() << " -----";
Elliott Hughese27955c2011-08-26 15:21:24 -070095
Elliott Hughesaccd83d2011-10-17 14:25:58 -070096 thread_list->ResumeAll();
Elliott Hughese27955c2011-08-26 15:21:24 -070097
Elliott Hughesd92bec42011-09-02 17:04:36 -070098 LOG(INFO) << os.str();
Elliott Hughese27955c2011-08-26 15:21:24 -070099}
100
101void SignalCatcher::HandleSigUsr1() {
102 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
103 Heap::CollectGarbage();
104}
105
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700106int WaitForSignal(Thread* thread, sigset_t& mask) {
Elliott Hughes93e74e82011-09-13 11:07:03 -0700107 ScopedThreadStateChange tsc(thread, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700108
109 // Signals for sigwait() must be blocked but not ignored. We
110 // block signals like SIGQUIT for all threads, so the condition
111 // is met. When the signal hits, we wake up, without any signal
112 // handlers being invoked.
113
114 // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
115 int signal_number;
116 int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
117 if (rc != 0) {
118 PLOG(FATAL) << "sigwait failed";
119 }
120
121 return signal_number;
122}
123
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700124void* SignalCatcher::Run(void* arg) {
125 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
126 CHECK(signal_catcher != NULL);
127
Elliott Hughes8d768a92011-09-14 16:35:25 -0700128 Runtime* runtime = Runtime::Current();
129 runtime->AttachCurrentThread("Signal Catcher", true);
130
131 {
132 MutexLock mu(signal_catcher->lock_);
133 signal_catcher->thread_ = Thread::Current();
Elliott Hughes5f791332011-09-15 17:45:30 -0700134 signal_catcher->cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700135 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700136
Elliott Hughese27955c2011-08-26 15:21:24 -0700137 // Set up mask with signals we want to handle.
138 sigset_t mask;
139 sigemptyset(&mask);
140 sigaddset(&mask, SIGQUIT);
141 sigaddset(&mask, SIGUSR1);
142
143 while (true) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700144 int signal_number = WaitForSignal(signal_catcher->thread_, mask);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700145 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700146 runtime->DetachCurrentThread();
Elliott Hughese27955c2011-08-26 15:21:24 -0700147 return NULL;
148 }
149
Elliott Hughes8d768a92011-09-14 16:35:25 -0700150 LOG(INFO) << *signal_catcher->thread_ << ": reacting to signal " << signal_number;
Elliott Hughese27955c2011-08-26 15:21:24 -0700151 switch (signal_number) {
152 case SIGQUIT:
153 HandleSigQuit();
154 break;
155 case SIGUSR1:
156 HandleSigUsr1();
157 break;
158 default:
159 LOG(ERROR) << "Unexpected signal %d" << signal_number;
160 break;
161 }
162 }
163}
164
165} // namespace art