blob: d72002f8e2c3ba2b93f7a862c0f1854936504d26 [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 Hughes8d768a92011-09-14 16:35:25 -070033SignalCatcher::SignalCatcher() : lock_("SignalCatcher lock"), thread_(NULL) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070034 SetHaltFlag(false);
35
Elliott Hughese27955c2011-08-26 15:21:24 -070036 // Create a raw pthread; its start routine will attach to the runtime.
Elliott Hughes8d768a92011-09-14 16:35:25 -070037 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Run, this), "signal catcher thread");
38
39 CHECK_PTHREAD_CALL(pthread_cond_init, (&cond_, NULL), "SignalCatcher::cond_");
40 MutexLock mu(lock_);
41 while (thread_ == NULL) {
42 CHECK_PTHREAD_CALL(pthread_cond_wait, (&cond_, lock_.GetImpl()), __FUNCTION__);
Elliott Hughese27955c2011-08-26 15:21:24 -070043 }
Elliott Hughes8d768a92011-09-14 16:35:25 -070044 CHECK_PTHREAD_CALL(pthread_cond_destroy, (&cond_), "SignalCatcher::cond_");
Elliott Hughese27955c2011-08-26 15:21:24 -070045}
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 Hughes8d768a92011-09-14 16:35:25 -070066 Runtime::Current()->GetThreadList()->SuspendAll();
Elliott Hughese27955c2011-08-26 15:21:24 -070067
Elliott Hughesd92bec42011-09-02 17:04:36 -070068 std::stringstream os;
69 os << "\n"
70 << "\n"
71 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -070072
Elliott Hughesd92bec42011-09-02 17:04:36 -070073 std::string cmdline;
74 if (ReadFileToString("/proc/self/cmdline", &cmdline)) {
75 std::replace(cmdline.begin(), cmdline.end(), '\0', ' ');
76 os << "Cmd line: " << cmdline << "\n";
77 }
Elliott Hughese27955c2011-08-26 15:21:24 -070078
Elliott Hughes8daa0922011-09-11 13:46:25 -070079 Runtime::Current()->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -070080
Elliott Hughesd92bec42011-09-02 17:04:36 -070081 std::string maps;
82 if (ReadFileToString("/proc/self/maps", &maps)) {
83 os << "/proc/self/maps:\n" << maps;
84 }
85
86 os << "----- end " << getpid() << " -----";
Elliott Hughese27955c2011-08-26 15:21:24 -070087
Elliott Hughes8d768a92011-09-14 16:35:25 -070088 Runtime::Current()->GetThreadList()->ResumeAll();
Elliott Hughese27955c2011-08-26 15:21:24 -070089
Elliott Hughesd92bec42011-09-02 17:04:36 -070090 LOG(INFO) << os.str();
Elliott Hughese27955c2011-08-26 15:21:24 -070091}
92
93void SignalCatcher::HandleSigUsr1() {
94 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
95 Heap::CollectGarbage();
96}
97
Elliott Hughesad7c2a32011-08-31 11:58:10 -070098int WaitForSignal(Thread* thread, sigset_t& mask) {
Elliott Hughes93e74e82011-09-13 11:07:03 -070099 ScopedThreadStateChange tsc(thread, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700100
101 // Signals for sigwait() must be blocked but not ignored. We
102 // block signals like SIGQUIT for all threads, so the condition
103 // is met. When the signal hits, we wake up, without any signal
104 // handlers being invoked.
105
106 // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
107 int signal_number;
108 int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
109 if (rc != 0) {
110 PLOG(FATAL) << "sigwait failed";
111 }
112
113 return signal_number;
114}
115
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700116void* SignalCatcher::Run(void* arg) {
117 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
118 CHECK(signal_catcher != NULL);
119
Elliott Hughes8d768a92011-09-14 16:35:25 -0700120 Runtime* runtime = Runtime::Current();
121 runtime->AttachCurrentThread("Signal Catcher", true);
122
123 {
124 MutexLock mu(signal_catcher->lock_);
125 signal_catcher->thread_ = Thread::Current();
126 CHECK_PTHREAD_CALL(pthread_cond_broadcast, (&signal_catcher->cond_), __FUNCTION__);
127 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700128
Elliott Hughese27955c2011-08-26 15:21:24 -0700129 // Set up mask with signals we want to handle.
130 sigset_t mask;
131 sigemptyset(&mask);
132 sigaddset(&mask, SIGQUIT);
133 sigaddset(&mask, SIGUSR1);
134
135 while (true) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700136 int signal_number = WaitForSignal(signal_catcher->thread_, mask);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700137 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700138 runtime->DetachCurrentThread();
Elliott Hughese27955c2011-08-26 15:21:24 -0700139 return NULL;
140 }
141
Elliott Hughes8d768a92011-09-14 16:35:25 -0700142 LOG(INFO) << *signal_catcher->thread_ << ": reacting to signal " << signal_number;
Elliott Hughese27955c2011-08-26 15:21:24 -0700143 switch (signal_number) {
144 case SIGQUIT:
145 HandleSigQuit();
146 break;
147 case SIGUSR1:
148 HandleSigUsr1();
149 break;
150 default:
151 LOG(ERROR) << "Unexpected signal %d" << signal_number;
152 break;
153 }
154 }
155}
156
157} // namespace art