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 | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 33 | #include "scoped_heap_lock.h" |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 34 | #include "signal_set.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 35 | #include "thread.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 36 | #include "thread_list.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 37 | #include "utils.h" |
| 38 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 39 | #if !defined(__APPLE__) |
| 40 | #define HAVE_PROC_CMDLINE |
| 41 | #endif |
| 42 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 43 | namespace art { |
| 44 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 45 | #if defined(HAVE_PROC_CMDLINE) |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 46 | static bool ReadCmdLine(std::string& result) { |
| 47 | if (!ReadFileToString("/proc/self/cmdline", &result)) { |
| 48 | return false; |
| 49 | } |
| 50 | std::replace(result.begin(), result.end(), '\0', ' '); |
| 51 | return true; |
| 52 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 53 | #endif |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 54 | |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 55 | SignalCatcher::SignalCatcher(const std::string& stack_trace_file) |
| 56 | : stack_trace_file_(stack_trace_file), |
| 57 | lock_("SignalCatcher lock"), |
| 58 | cond_("SignalCatcher::cond_"), |
| 59 | thread_(NULL) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 60 | SetHaltFlag(false); |
| 61 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 62 | #if defined(HAVE_PROC_CMDLINE) |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 63 | // Stash the original command line for SIGQUIT reporting. |
| 64 | // By then, /proc/self/cmdline will have been rewritten to something like "system_server". |
| 65 | CHECK(ReadCmdLine(cmd_line_)); |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 66 | #endif |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 68 | // Create a raw pthread; its start routine will attach to the runtime. |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 69 | CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Run, this), "signal catcher thread"); |
| 70 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 71 | MutexLock mu(lock_); |
| 72 | while (thread_ == NULL) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 73 | cond_.Wait(lock_); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | SignalCatcher::~SignalCatcher() { |
| 78 | // Since we know the thread is just sitting around waiting for signals |
| 79 | // to arrive, send it one. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 80 | SetHaltFlag(true); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 81 | CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown"); |
| 82 | CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "signal catcher shutdown"); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 85 | void SignalCatcher::SetHaltFlag(bool new_value) { |
| 86 | MutexLock mu(lock_); |
| 87 | halt_ = new_value; |
| 88 | } |
| 89 | |
| 90 | bool SignalCatcher::ShouldHalt() { |
| 91 | MutexLock mu(lock_); |
| 92 | return halt_; |
| 93 | } |
| 94 | |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 95 | void SignalCatcher::Output(const std::string& s) { |
| 96 | if (stack_trace_file_.empty()) { |
| 97 | LOG(INFO) << s; |
| 98 | return; |
| 99 | } |
| 100 | |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 101 | ScopedThreadStateChange tsc(Thread::Current(), kVmWait); |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 102 | int fd = open(stack_trace_file_.c_str(), O_APPEND | O_CREAT | O_WRONLY, 0666); |
| 103 | if (fd == -1) { |
| 104 | PLOG(ERROR) << "Unable to open stack trace file '" << stack_trace_file_ << "'"; |
| 105 | return; |
| 106 | } |
| 107 | UniquePtr<File> file(OS::FileFromFd(stack_trace_file_.c_str(), fd)); |
| 108 | if (!file->WriteFully(s.data(), s.size())) { |
| 109 | PLOG(ERROR) << "Failed to write stack traces to '" << stack_trace_file_ << "'"; |
| 110 | } else { |
| 111 | LOG(INFO) << "Wrote stack traces to '" << stack_trace_file_ << "'"; |
| 112 | } |
| 113 | close(fd); |
| 114 | } |
| 115 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 116 | void SignalCatcher::HandleSigQuit() { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 117 | Runtime* runtime = Runtime::Current(); |
| 118 | ThreadList* thread_list = runtime->GetThreadList(); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 119 | |
Elliott Hughes | 831afe4 | 2011-12-15 17:27:34 -0800 | [diff] [blame] | 120 | // We take the heap lock before suspending all threads so we don't end up in a situation where |
| 121 | // one of the suspended threads suspended via the implicit FullSuspendCheck on the slow path of |
| 122 | // Heap::Lock, which is the only case where a thread can be suspended while holding the heap lock. |
| 123 | // (We need the heap lock when we dump the thread list. We could probably fix this by duplicating |
| 124 | // more state from java.lang.Thread in struct Thread.) |
| 125 | ScopedHeapLock heap_lock; |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 126 | thread_list->SuspendAll(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 127 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 128 | std::ostringstream os; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 129 | os << "\n" |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 130 | << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 131 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 132 | #if defined(HAVE_PROC_CMDLINE) |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 133 | std::string current_cmd_line; |
| 134 | if (ReadCmdLine(current_cmd_line) && current_cmd_line != cmd_line_) { |
| 135 | os << "Cmdline: " << current_cmd_line; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 136 | } |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 137 | os << "\n"; |
| 138 | |
| 139 | if (current_cmd_line != cmd_line_) { |
| 140 | os << "Original command line: " << cmd_line_ << "\n"; |
| 141 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 142 | #endif |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 143 | |
| 144 | os << "Build type: " << (kIsDebugBuild ? "debug" : "optimized") << "\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 145 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 146 | runtime->DumpForSigQuit(os); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 147 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 148 | if (false) { |
| 149 | std::string maps; |
| 150 | if (ReadFileToString("/proc/self/maps", &maps)) { |
| 151 | os << "/proc/self/maps:\n" << maps; |
| 152 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 155 | os << "----- end " << getpid() << " -----\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 156 | |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 157 | thread_list->ResumeAll(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 158 | |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 159 | Output(os.str()); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void SignalCatcher::HandleSigUsr1() { |
| 163 | LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)"; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 164 | Runtime::Current()->GetHeap()->CollectGarbage(false); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 167 | int SignalCatcher::WaitForSignal(SignalSet& signals) { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 168 | ScopedThreadStateChange tsc(thread_, kVmWait); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 169 | |
| 170 | // Signals for sigwait() must be blocked but not ignored. We |
| 171 | // block signals like SIGQUIT for all threads, so the condition |
| 172 | // is met. When the signal hits, we wake up, without any signal |
| 173 | // handlers being invoked. |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 174 | int signal_number = signals.Wait(); |
Elliott Hughes | c2f8006 | 2011-11-07 11:57:51 -0800 | [diff] [blame] | 175 | if (!ShouldHalt()) { |
| 176 | // Let the user know we got the signal, just in case the system's too screwed for us to |
| 177 | // actually do what they want us to do... |
| 178 | LOG(INFO) << *thread_ << ": reacting to signal " << signal_number; |
| 179 | |
Elliott Hughes | 21a5bf2 | 2011-12-07 14:35:20 -0800 | [diff] [blame] | 180 | // If anyone's holding locks (which might prevent us from getting back into state Runnable), say so... |
| 181 | Runtime::Current()->DumpLockHolders(LOG(INFO)); |
Elliott Hughes | c2f8006 | 2011-11-07 11:57:51 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 184 | return signal_number; |
| 185 | } |
| 186 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 187 | void* SignalCatcher::Run(void* arg) { |
| 188 | SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg); |
| 189 | CHECK(signal_catcher != NULL); |
| 190 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 191 | Runtime* runtime = Runtime::Current(); |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 192 | runtime->AttachCurrentThread("Signal Catcher", true, Thread::GetSystemThreadGroup()); |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 193 | Thread::Current()->SetState(kRunnable); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 194 | |
| 195 | { |
| 196 | MutexLock mu(signal_catcher->lock_); |
| 197 | signal_catcher->thread_ = Thread::Current(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 198 | signal_catcher->cond_.Broadcast(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 199 | } |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 200 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 201 | // Set up mask with signals we want to handle. |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 202 | SignalSet signals; |
| 203 | signals.Add(SIGQUIT); |
| 204 | signals.Add(SIGUSR1); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 205 | |
| 206 | while (true) { |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 207 | int signal_number = signal_catcher->WaitForSignal(signals); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 208 | if (signal_catcher->ShouldHalt()) { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 209 | runtime->DetachCurrentThread(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 210 | return NULL; |
| 211 | } |
| 212 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 213 | switch (signal_number) { |
| 214 | case SIGQUIT: |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 215 | signal_catcher->HandleSigQuit(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 216 | break; |
| 217 | case SIGUSR1: |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 218 | signal_catcher->HandleSigUsr1(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 219 | break; |
| 220 | default: |
| 221 | LOG(ERROR) << "Unexpected signal %d" << signal_number; |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | } // namespace art |