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 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 25 | #include "class_linker.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 26 | #include "heap.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 27 | #include "runtime.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 28 | #include "thread.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 29 | #include "thread_list.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 30 | #include "utils.h" |
| 31 | |
| 32 | namespace art { |
| 33 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 34 | SignalCatcher::SignalCatcher() |
| 35 | : lock_("SignalCatcher lock"), cond_("SignalCatcher::cond_"), thread_(NULL) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 36 | SetHaltFlag(false); |
| 37 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 38 | // Create a raw pthread; its start routine will attach to the runtime. |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 39 | CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Run, this), "signal catcher thread"); |
| 40 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 41 | MutexLock mu(lock_); |
| 42 | while (thread_ == NULL) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 43 | cond_.Wait(lock_); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 44 | } |
| 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 | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 66 | Runtime* runtime = Runtime::Current(); |
| 67 | ThreadList* thread_list = runtime->GetThreadList(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 68 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 69 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 70 | LOG(INFO) << "Heap lock owner tid: " << Heap::GetLockOwner() << "\n" |
| 71 | << "ThreadList lock owner tid: " << thread_list->GetLockOwner() << "\n" |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame^] | 72 | << "ClassLinker classes lock owner tid: " << class_linker->GetClassesLockOwner() << "\n" |
| 73 | << "ClassLinker dex lock owner tid: " << class_linker->GetDexLockOwner() << "\n"; |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 74 | |
| 75 | thread_list->SuspendAll(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 76 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 77 | std::ostringstream os; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 78 | os << "\n" |
| 79 | << "\n" |
| 80 | << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 81 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 82 | std::string cmdline; |
| 83 | if (ReadFileToString("/proc/self/cmdline", &cmdline)) { |
| 84 | std::replace(cmdline.begin(), cmdline.end(), '\0', ' '); |
| 85 | os << "Cmd line: " << cmdline << "\n"; |
| 86 | } |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 87 | |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 88 | runtime->Dump(os); |
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 | std::string maps; |
| 91 | if (ReadFileToString("/proc/self/maps", &maps)) { |
| 92 | os << "/proc/self/maps:\n" << maps; |
| 93 | } |
| 94 | |
| 95 | os << "----- end " << getpid() << " -----"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 96 | |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 97 | thread_list->ResumeAll(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 98 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 99 | LOG(INFO) << os.str(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void SignalCatcher::HandleSigUsr1() { |
| 103 | LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)"; |
| 104 | Heap::CollectGarbage(); |
| 105 | } |
| 106 | |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 107 | int WaitForSignal(Thread* thread, sigset_t& mask) { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 108 | ScopedThreadStateChange tsc(thread, Thread::kVmWait); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 109 | |
| 110 | // Signals for sigwait() must be blocked but not ignored. We |
| 111 | // block signals like SIGQUIT for all threads, so the condition |
| 112 | // is met. When the signal hits, we wake up, without any signal |
| 113 | // handlers being invoked. |
| 114 | |
| 115 | // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures. |
| 116 | int signal_number; |
| 117 | int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number)); |
| 118 | if (rc != 0) { |
| 119 | PLOG(FATAL) << "sigwait failed"; |
| 120 | } |
| 121 | |
| 122 | return signal_number; |
| 123 | } |
| 124 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 125 | void* SignalCatcher::Run(void* arg) { |
| 126 | SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg); |
| 127 | CHECK(signal_catcher != NULL); |
| 128 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 129 | Runtime* runtime = Runtime::Current(); |
| 130 | runtime->AttachCurrentThread("Signal Catcher", true); |
| 131 | |
| 132 | { |
| 133 | MutexLock mu(signal_catcher->lock_); |
| 134 | signal_catcher->thread_ = Thread::Current(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 135 | signal_catcher->cond_.Broadcast(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 136 | } |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 137 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 138 | // Set up mask with signals we want to handle. |
| 139 | sigset_t mask; |
| 140 | sigemptyset(&mask); |
| 141 | sigaddset(&mask, SIGQUIT); |
| 142 | sigaddset(&mask, SIGUSR1); |
| 143 | |
| 144 | while (true) { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 145 | int signal_number = WaitForSignal(signal_catcher->thread_, mask); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 146 | if (signal_catcher->ShouldHalt()) { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 147 | runtime->DetachCurrentThread(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 148 | return NULL; |
| 149 | } |
| 150 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 151 | LOG(INFO) << *signal_catcher->thread_ << ": reacting to signal " << signal_number; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 152 | switch (signal_number) { |
| 153 | case SIGQUIT: |
| 154 | HandleSigQuit(); |
| 155 | break; |
| 156 | case SIGUSR1: |
| 157 | HandleSigUsr1(); |
| 158 | break; |
| 159 | default: |
| 160 | LOG(ERROR) << "Unexpected signal %d" << signal_number; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | } // namespace art |