Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 26 | #include "runtime.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 27 | #include "thread.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame^] | 28 | #include "thread_list.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 29 | #include "utils.h" |
| 30 | |
| 31 | namespace art { |
| 32 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame^] | 33 | SignalCatcher::SignalCatcher() : lock_("SignalCatcher lock"), thread_(NULL) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 34 | SetHaltFlag(false); |
| 35 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 36 | // Create a raw pthread; its start routine will attach to the runtime. |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame^] | 37 | 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 Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 43 | } |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame^] | 44 | CHECK_PTHREAD_CALL(pthread_cond_destroy, (&cond_), "SignalCatcher::cond_"); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | SignalCatcher::~SignalCatcher() { |
| 48 | // Since we know the thread is just sitting around waiting for signals |
| 49 | // to arrive, send it one. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 50 | SetHaltFlag(true); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame^] | 51 | CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown"); |
| 52 | CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "signal catcher shutdown"); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 55 | void SignalCatcher::SetHaltFlag(bool new_value) { |
| 56 | MutexLock mu(lock_); |
| 57 | halt_ = new_value; |
| 58 | } |
| 59 | |
| 60 | bool SignalCatcher::ShouldHalt() { |
| 61 | MutexLock mu(lock_); |
| 62 | return halt_; |
| 63 | } |
| 64 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 65 | void SignalCatcher::HandleSigQuit() { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame^] | 66 | Runtime::Current()->GetThreadList()->SuspendAll(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 68 | std::stringstream os; |
| 69 | os << "\n" |
| 70 | << "\n" |
| 71 | << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 72 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 73 | 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 Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 78 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 79 | Runtime::Current()->Dump(os); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 80 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 81 | std::string maps; |
| 82 | if (ReadFileToString("/proc/self/maps", &maps)) { |
| 83 | os << "/proc/self/maps:\n" << maps; |
| 84 | } |
| 85 | |
| 86 | os << "----- end " << getpid() << " -----"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 87 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame^] | 88 | Runtime::Current()->GetThreadList()->ResumeAll(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 89 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 90 | LOG(INFO) << os.str(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void SignalCatcher::HandleSigUsr1() { |
| 94 | LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)"; |
| 95 | Heap::CollectGarbage(); |
| 96 | } |
| 97 | |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 98 | int WaitForSignal(Thread* thread, sigset_t& mask) { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 99 | ScopedThreadStateChange tsc(thread, Thread::kVmWait); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 100 | |
| 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 Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 116 | void* SignalCatcher::Run(void* arg) { |
| 117 | SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg); |
| 118 | CHECK(signal_catcher != NULL); |
| 119 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame^] | 120 | 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 Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 128 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 129 | // 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 Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame^] | 136 | int signal_number = WaitForSignal(signal_catcher->thread_, mask); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 137 | if (signal_catcher->ShouldHalt()) { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame^] | 138 | runtime->DetachCurrentThread(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 139 | return NULL; |
| 140 | } |
| 141 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame^] | 142 | LOG(INFO) << *signal_catcher->thread_ << ": reacting to signal " << signal_number; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 143 | 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 |