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