blob: ecd48cdab3784e762797b9174c17e6c6e9964394 [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 Hughes8d768a92011-09-14 16:35:25 -070065 Runtime::Current()->GetThreadList()->SuspendAll();
Elliott Hughese27955c2011-08-26 15:21:24 -070066
Elliott Hughesd92bec42011-09-02 17:04:36 -070067 std::stringstream os;
68 os << "\n"
69 << "\n"
70 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -070071
Elliott Hughesd92bec42011-09-02 17:04:36 -070072 std::string cmdline;
73 if (ReadFileToString("/proc/self/cmdline", &cmdline)) {
74 std::replace(cmdline.begin(), cmdline.end(), '\0', ' ');
75 os << "Cmd line: " << cmdline << "\n";
76 }
Elliott Hughese27955c2011-08-26 15:21:24 -070077
Elliott Hughes8daa0922011-09-11 13:46:25 -070078 Runtime::Current()->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -070079
Elliott Hughesd92bec42011-09-02 17:04:36 -070080 std::string maps;
81 if (ReadFileToString("/proc/self/maps", &maps)) {
82 os << "/proc/self/maps:\n" << maps;
83 }
84
85 os << "----- end " << getpid() << " -----";
Elliott Hughese27955c2011-08-26 15:21:24 -070086
Elliott Hughes8d768a92011-09-14 16:35:25 -070087 Runtime::Current()->GetThreadList()->ResumeAll();
Elliott Hughese27955c2011-08-26 15:21:24 -070088
Elliott Hughesd92bec42011-09-02 17:04:36 -070089 LOG(INFO) << os.str();
Elliott Hughese27955c2011-08-26 15:21:24 -070090}
91
92void SignalCatcher::HandleSigUsr1() {
93 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
94 Heap::CollectGarbage();
95}
96
Elliott Hughesad7c2a32011-08-31 11:58:10 -070097int WaitForSignal(Thread* thread, sigset_t& mask) {
Elliott Hughes93e74e82011-09-13 11:07:03 -070098 ScopedThreadStateChange tsc(thread, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -070099
100 // Signals for sigwait() must be blocked but not ignored. We
101 // block signals like SIGQUIT for all threads, so the condition
102 // is met. When the signal hits, we wake up, without any signal
103 // handlers being invoked.
104
105 // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
106 int signal_number;
107 int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
108 if (rc != 0) {
109 PLOG(FATAL) << "sigwait failed";
110 }
111
112 return signal_number;
113}
114
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700115void* SignalCatcher::Run(void* arg) {
116 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
117 CHECK(signal_catcher != NULL);
118
Elliott Hughes8d768a92011-09-14 16:35:25 -0700119 Runtime* runtime = Runtime::Current();
120 runtime->AttachCurrentThread("Signal Catcher", true);
121
122 {
123 MutexLock mu(signal_catcher->lock_);
124 signal_catcher->thread_ = Thread::Current();
Elliott Hughes5f791332011-09-15 17:45:30 -0700125 signal_catcher->cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700126 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700127
Elliott Hughese27955c2011-08-26 15:21:24 -0700128 // Set up mask with signals we want to handle.
129 sigset_t mask;
130 sigemptyset(&mask);
131 sigaddset(&mask, SIGQUIT);
132 sigaddset(&mask, SIGUSR1);
133
134 while (true) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700135 int signal_number = WaitForSignal(signal_catcher->thread_, mask);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700136 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700137 runtime->DetachCurrentThread();
Elliott Hughese27955c2011-08-26 15:21:24 -0700138 return NULL;
139 }
140
Elliott Hughes8d768a92011-09-14 16:35:25 -0700141 LOG(INFO) << *signal_catcher->thread_ << ": reacting to signal " << signal_number;
Elliott Hughese27955c2011-08-26 15:21:24 -0700142 switch (signal_number) {
143 case SIGQUIT:
144 HandleSigQuit();
145 break;
146 case SIGUSR1:
147 HandleSigUsr1();
148 break;
149 default:
150 LOG(ERROR) << "Unexpected signal %d" << signal_number;
151 break;
152 }
153 }
154}
155
156} // namespace art