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 | |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 20 | #include <pthread.h> |
| 21 | #include <signal.h> |
| 22 | #include <stdlib.h> |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 23 | #include <sys/stat.h> |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 24 | #include <sys/time.h> |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 25 | #include <sys/types.h> |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 28 | #include "class_linker.h" |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 29 | #include "file.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 30 | #include "heap.h" |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 31 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 32 | #include "runtime.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 33 | #include "thread.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 34 | #include "thread_list.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 35 | #include "utils.h" |
| 36 | |
| 37 | namespace art { |
| 38 | |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 39 | SignalCatcher::SignalCatcher(const std::string& stack_trace_file) |
| 40 | : stack_trace_file_(stack_trace_file), |
| 41 | lock_("SignalCatcher lock"), |
| 42 | cond_("SignalCatcher::cond_"), |
| 43 | thread_(NULL) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 44 | SetHaltFlag(false); |
| 45 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 46 | // Create a raw pthread; its start routine will attach to the runtime. |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 47 | CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Run, this), "signal catcher thread"); |
| 48 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 49 | MutexLock mu(lock_); |
| 50 | while (thread_ == NULL) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 51 | cond_.Wait(lock_); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | SignalCatcher::~SignalCatcher() { |
| 56 | // Since we know the thread is just sitting around waiting for signals |
| 57 | // to arrive, send it one. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 58 | SetHaltFlag(true); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 59 | CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown"); |
| 60 | CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "signal catcher shutdown"); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 63 | void SignalCatcher::SetHaltFlag(bool new_value) { |
| 64 | MutexLock mu(lock_); |
| 65 | halt_ = new_value; |
| 66 | } |
| 67 | |
| 68 | bool SignalCatcher::ShouldHalt() { |
| 69 | MutexLock mu(lock_); |
| 70 | return halt_; |
| 71 | } |
| 72 | |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 73 | void SignalCatcher::Output(const std::string& s) { |
| 74 | if (stack_trace_file_.empty()) { |
| 75 | LOG(INFO) << s; |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait); |
| 80 | int fd = open(stack_trace_file_.c_str(), O_APPEND | O_CREAT | O_WRONLY, 0666); |
| 81 | if (fd == -1) { |
| 82 | PLOG(ERROR) << "Unable to open stack trace file '" << stack_trace_file_ << "'"; |
| 83 | return; |
| 84 | } |
| 85 | UniquePtr<File> file(OS::FileFromFd(stack_trace_file_.c_str(), fd)); |
| 86 | if (!file->WriteFully(s.data(), s.size())) { |
| 87 | PLOG(ERROR) << "Failed to write stack traces to '" << stack_trace_file_ << "'"; |
| 88 | } else { |
| 89 | LOG(INFO) << "Wrote stack traces to '" << stack_trace_file_ << "'"; |
| 90 | } |
| 91 | close(fd); |
| 92 | } |
| 93 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 94 | void SignalCatcher::HandleSigQuit() { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 95 | Runtime* runtime = Runtime::Current(); |
| 96 | ThreadList* thread_list = runtime->GetThreadList(); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 97 | |
| 98 | thread_list->SuspendAll(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 99 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 100 | std::ostringstream os; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 101 | os << "\n" |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 102 | << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 103 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 104 | std::string cmdline; |
| 105 | if (ReadFileToString("/proc/self/cmdline", &cmdline)) { |
| 106 | std::replace(cmdline.begin(), cmdline.end(), '\0', ' '); |
| 107 | os << "Cmd line: " << cmdline << "\n"; |
| 108 | } |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 109 | |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 110 | runtime->Dump(os); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 111 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 112 | if (false) { |
| 113 | std::string maps; |
| 114 | if (ReadFileToString("/proc/self/maps", &maps)) { |
| 115 | os << "/proc/self/maps:\n" << maps; |
| 116 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 119 | os << "----- end " << getpid() << " -----\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 120 | |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 121 | thread_list->ResumeAll(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 122 | |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 123 | Output(os.str()); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void SignalCatcher::HandleSigUsr1() { |
| 127 | LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)"; |
| 128 | Heap::CollectGarbage(); |
| 129 | } |
| 130 | |
Elliott Hughes | c2f8006 | 2011-11-07 11:57:51 -0800 | [diff] [blame] | 131 | int SignalCatcher::WaitForSignal(sigset_t& mask) { |
| 132 | ScopedThreadStateChange tsc(thread_, Thread::kVmWait); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 133 | |
| 134 | // Signals for sigwait() must be blocked but not ignored. We |
| 135 | // block signals like SIGQUIT for all threads, so the condition |
| 136 | // is met. When the signal hits, we wake up, without any signal |
| 137 | // handlers being invoked. |
| 138 | |
| 139 | // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures. |
| 140 | int signal_number; |
| 141 | int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number)); |
| 142 | if (rc != 0) { |
| 143 | PLOG(FATAL) << "sigwait failed"; |
| 144 | } |
| 145 | |
Elliott Hughes | c2f8006 | 2011-11-07 11:57:51 -0800 | [diff] [blame] | 146 | if (!ShouldHalt()) { |
| 147 | // Let the user know we got the signal, just in case the system's too screwed for us to |
| 148 | // actually do what they want us to do... |
| 149 | LOG(INFO) << *thread_ << ": reacting to signal " << signal_number; |
| 150 | |
| 151 | // If anyone's holding locks (and so we might fail to get back into state Runnable), say so... |
| 152 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 153 | pid_t heap_lock_owner = Heap::GetLockOwner(); |
| 154 | pid_t thread_list_lock_owner = Runtime::Current()->GetThreadList()->GetLockOwner(); |
| 155 | pid_t classes_lock_owner = class_linker->GetClassesLockOwner(); |
| 156 | pid_t dex_lock_owner = class_linker->GetDexLockOwner(); |
| 157 | if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) { |
| 158 | LOG(INFO) << "Heap lock owner tid: " << heap_lock_owner << "\n" |
| 159 | << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n" |
| 160 | << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n" |
| 161 | << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n"; |
| 162 | } |
| 163 | } |
| 164 | |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 165 | return signal_number; |
| 166 | } |
| 167 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 168 | void* SignalCatcher::Run(void* arg) { |
| 169 | SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg); |
| 170 | CHECK(signal_catcher != NULL); |
| 171 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 172 | Runtime* runtime = Runtime::Current(); |
| 173 | runtime->AttachCurrentThread("Signal Catcher", true); |
Brian Carlstrom | f28bc5b | 2011-10-26 01:15:03 -0700 | [diff] [blame] | 174 | Thread::Current()->SetState(Thread::kRunnable); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 175 | |
| 176 | { |
| 177 | MutexLock mu(signal_catcher->lock_); |
| 178 | signal_catcher->thread_ = Thread::Current(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 179 | signal_catcher->cond_.Broadcast(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 180 | } |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 181 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 182 | // Set up mask with signals we want to handle. |
| 183 | sigset_t mask; |
| 184 | sigemptyset(&mask); |
| 185 | sigaddset(&mask, SIGQUIT); |
| 186 | sigaddset(&mask, SIGUSR1); |
| 187 | |
| 188 | while (true) { |
Elliott Hughes | c2f8006 | 2011-11-07 11:57:51 -0800 | [diff] [blame] | 189 | int signal_number = signal_catcher->WaitForSignal(mask); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 190 | if (signal_catcher->ShouldHalt()) { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 191 | runtime->DetachCurrentThread(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 192 | return NULL; |
| 193 | } |
| 194 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 195 | switch (signal_number) { |
| 196 | case SIGQUIT: |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 197 | signal_catcher->HandleSigQuit(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 198 | break; |
| 199 | case SIGUSR1: |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 200 | signal_catcher->HandleSigUsr1(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 201 | break; |
| 202 | default: |
| 203 | LOG(ERROR) << "Unexpected signal %d" << signal_number; |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | } // namespace art |