blob: 06491e4119a8458d2c2f5c6cbf9aabbb9b2aa589 [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
25#include "heap.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "runtime.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070027#include "thread.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070028#include "thread_list.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070029#include "utils.h"
30
31namespace art {
32
Elliott Hughes5f791332011-09-15 17:45:30 -070033SignalCatcher::SignalCatcher()
34 : lock_("SignalCatcher lock"), cond_("SignalCatcher::cond_"), thread_(NULL) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070035 SetHaltFlag(false);
36
Elliott Hughese27955c2011-08-26 15:21:24 -070037 // Create a raw pthread; its start routine will attach to the runtime.
Elliott Hughes8d768a92011-09-14 16:35:25 -070038 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Run, this), "signal catcher thread");
39
Elliott Hughes8d768a92011-09-14 16:35:25 -070040 MutexLock mu(lock_);
41 while (thread_ == NULL) {
Elliott Hughes5f791332011-09-15 17:45:30 -070042 cond_.Wait(lock_);
Elliott Hughese27955c2011-08-26 15:21:24 -070043 }
44}
45
46SignalCatcher::~SignalCatcher() {
47 // Since we know the thread is just sitting around waiting for signals
48 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070049 SetHaltFlag(true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070050 CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown");
51 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "signal catcher shutdown");
Elliott Hughese27955c2011-08-26 15:21:24 -070052}
53
Elliott Hughes5fe594f2011-09-08 12:33:17 -070054void SignalCatcher::SetHaltFlag(bool new_value) {
55 MutexLock mu(lock_);
56 halt_ = new_value;
57}
58
59bool SignalCatcher::ShouldHalt() {
60 MutexLock mu(lock_);
61 return halt_;
62}
63
Elliott Hughese27955c2011-08-26 15:21:24 -070064void SignalCatcher::HandleSigQuit() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070065 Runtime* runtime = Runtime::Current();
66 ThreadList* thread_list = runtime->GetThreadList();
67
68 LOG(INFO) << "Heap lock owner: " << Heap::GetLockOwner() << "\n"
69 << "Thread lock owner: " << thread_list->GetLockOwner() << "\n";
70
71 thread_list->SuspendAll();
Elliott Hughese27955c2011-08-26 15:21:24 -070072
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070073 std::ostringstream os;
Elliott Hughesd92bec42011-09-02 17:04:36 -070074 os << "\n"
75 << "\n"
76 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -070077
Elliott Hughesd92bec42011-09-02 17:04:36 -070078 std::string cmdline;
79 if (ReadFileToString("/proc/self/cmdline", &cmdline)) {
80 std::replace(cmdline.begin(), cmdline.end(), '\0', ' ');
81 os << "Cmd line: " << cmdline << "\n";
82 }
Elliott Hughese27955c2011-08-26 15:21:24 -070083
Elliott Hughesaccd83d2011-10-17 14:25:58 -070084 runtime->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -070085
Elliott Hughesd92bec42011-09-02 17:04:36 -070086 std::string maps;
87 if (ReadFileToString("/proc/self/maps", &maps)) {
88 os << "/proc/self/maps:\n" << maps;
89 }
90
91 os << "----- end " << getpid() << " -----";
Elliott Hughese27955c2011-08-26 15:21:24 -070092
Elliott Hughesaccd83d2011-10-17 14:25:58 -070093 thread_list->ResumeAll();
Elliott Hughese27955c2011-08-26 15:21:24 -070094
Elliott Hughesd92bec42011-09-02 17:04:36 -070095 LOG(INFO) << os.str();
Elliott Hughese27955c2011-08-26 15:21:24 -070096}
97
98void SignalCatcher::HandleSigUsr1() {
99 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
100 Heap::CollectGarbage();
101}
102
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700103int WaitForSignal(Thread* thread, sigset_t& mask) {
Elliott Hughes93e74e82011-09-13 11:07:03 -0700104 ScopedThreadStateChange tsc(thread, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700105
106 // Signals for sigwait() must be blocked but not ignored. We
107 // block signals like SIGQUIT for all threads, so the condition
108 // is met. When the signal hits, we wake up, without any signal
109 // handlers being invoked.
110
111 // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
112 int signal_number;
113 int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
114 if (rc != 0) {
115 PLOG(FATAL) << "sigwait failed";
116 }
117
118 return signal_number;
119}
120
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700121void* SignalCatcher::Run(void* arg) {
122 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
123 CHECK(signal_catcher != NULL);
124
Elliott Hughes8d768a92011-09-14 16:35:25 -0700125 Runtime* runtime = Runtime::Current();
126 runtime->AttachCurrentThread("Signal Catcher", true);
127
128 {
129 MutexLock mu(signal_catcher->lock_);
130 signal_catcher->thread_ = Thread::Current();
Elliott Hughes5f791332011-09-15 17:45:30 -0700131 signal_catcher->cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700132 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700133
Elliott Hughese27955c2011-08-26 15:21:24 -0700134 // Set up mask with signals we want to handle.
135 sigset_t mask;
136 sigemptyset(&mask);
137 sigaddset(&mask, SIGQUIT);
138 sigaddset(&mask, SIGUSR1);
139
140 while (true) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700141 int signal_number = WaitForSignal(signal_catcher->thread_, mask);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700142 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700143 runtime->DetachCurrentThread();
Elliott Hughese27955c2011-08-26 15:21:24 -0700144 return NULL;
145 }
146
Elliott Hughes8d768a92011-09-14 16:35:25 -0700147 LOG(INFO) << *signal_catcher->thread_ << ": reacting to signal " << signal_number;
Elliott Hughese27955c2011-08-26 15:21:24 -0700148 switch (signal_number) {
149 case SIGQUIT:
150 HandleSigQuit();
151 break;
152 case SIGUSR1:
153 HandleSigUsr1();
154 break;
155 default:
156 LOG(ERROR) << "Unexpected signal %d" << signal_number;
157 break;
158 }
159 }
160}
161
162} // namespace art