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 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 28 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 29 | #include "class_linker.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 30 | #include "gc/heap.h" |
Jeff Brown | b4fffc7 | 2014-09-10 18:41:18 -0700 | [diff] [blame^] | 31 | #include "instruction_set.h" |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 32 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 33 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 34 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 35 | #include "signal_set.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 36 | #include "thread.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 37 | #include "thread_list.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 38 | #include "utils.h" |
| 39 | |
| 40 | namespace art { |
| 41 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 42 | static void DumpCmdLine(std::ostream& os) { |
| 43 | #if defined(__linux__) |
| 44 | // Show the original command line, and the current command line too if it's changed. |
| 45 | // On Android, /proc/self/cmdline will have been rewritten to something like "system_server". |
Jeff Brown | b4fffc7 | 2014-09-10 18:41:18 -0700 | [diff] [blame^] | 46 | // Note: The string "Cmd line:" is chosen to match the format used by debuggerd. |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 47 | std::string current_cmd_line; |
| 48 | if (ReadFileToString("/proc/self/cmdline", ¤t_cmd_line)) { |
Jeff Brown | b4fffc7 | 2014-09-10 18:41:18 -0700 | [diff] [blame^] | 49 | current_cmd_line.resize(current_cmd_line.find_last_not_of('\0') + 1); // trim trailing '\0's |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 50 | std::replace(current_cmd_line.begin(), current_cmd_line.end(), '\0', ' '); |
| 51 | |
Jeff Brown | b4fffc7 | 2014-09-10 18:41:18 -0700 | [diff] [blame^] | 52 | os << "Cmd line: " << current_cmd_line << "\n"; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 53 | const char* stashed_cmd_line = GetCmdLine(); |
Jeff Brown | b4fffc7 | 2014-09-10 18:41:18 -0700 | [diff] [blame^] | 54 | if (stashed_cmd_line != NULL && current_cmd_line != stashed_cmd_line |
| 55 | && strcmp(stashed_cmd_line, "<unset>") != 0) { |
| 56 | os << "Original command line: " << stashed_cmd_line << "\n"; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 57 | } |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 58 | } |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 59 | #else |
Jeff Brown | b4fffc7 | 2014-09-10 18:41:18 -0700 | [diff] [blame^] | 60 | os << "Cmd line: " << GetCmdLine() << "\n"; |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 61 | #endif |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 62 | } |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 63 | |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 64 | SignalCatcher::SignalCatcher(const std::string& stack_trace_file) |
| 65 | : stack_trace_file_(stack_trace_file), |
| 66 | lock_("SignalCatcher lock"), |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 67 | cond_("SignalCatcher::cond_", lock_), |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 68 | thread_(NULL) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 69 | SetHaltFlag(false); |
| 70 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 71 | // Create a raw pthread; its start routine will attach to the runtime. |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 72 | CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Run, this), "signal catcher thread"); |
| 73 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 74 | Thread* self = Thread::Current(); |
| 75 | MutexLock mu(self, lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 76 | while (thread_ == NULL) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 77 | cond_.Wait(self); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | SignalCatcher::~SignalCatcher() { |
| 82 | // Since we know the thread is just sitting around waiting for signals |
| 83 | // to arrive, send it one. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 84 | SetHaltFlag(true); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 85 | CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown"); |
| 86 | CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "signal catcher shutdown"); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 89 | void SignalCatcher::SetHaltFlag(bool new_value) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 90 | MutexLock mu(Thread::Current(), lock_); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 91 | halt_ = new_value; |
| 92 | } |
| 93 | |
| 94 | bool SignalCatcher::ShouldHalt() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 95 | MutexLock mu(Thread::Current(), lock_); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 96 | return halt_; |
| 97 | } |
| 98 | |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 99 | void SignalCatcher::Output(const std::string& s) { |
| 100 | if (stack_trace_file_.empty()) { |
| 101 | LOG(INFO) << s; |
| 102 | return; |
| 103 | } |
| 104 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 105 | ScopedThreadStateChange tsc(Thread::Current(), kWaitingForSignalCatcherOutput); |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 106 | int fd = open(stack_trace_file_.c_str(), O_APPEND | O_CREAT | O_WRONLY, 0666); |
| 107 | if (fd == -1) { |
| 108 | PLOG(ERROR) << "Unable to open stack trace file '" << stack_trace_file_ << "'"; |
| 109 | return; |
| 110 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 111 | std::unique_ptr<File> file(new File(fd, stack_trace_file_)); |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 112 | if (!file->WriteFully(s.data(), s.size())) { |
| 113 | PLOG(ERROR) << "Failed to write stack traces to '" << stack_trace_file_ << "'"; |
| 114 | } else { |
| 115 | LOG(INFO) << "Wrote stack traces to '" << stack_trace_file_ << "'"; |
| 116 | } |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 119 | void SignalCatcher::HandleSigQuit() { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 120 | Runtime* runtime = Runtime::Current(); |
| 121 | ThreadList* thread_list = runtime->GetThreadList(); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 122 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 123 | // Grab exclusively the mutator lock, set state to Runnable without checking for a pending |
| 124 | // suspend request as we're going to suspend soon anyway. We set the state to Runnable to avoid |
| 125 | // giving away the mutator lock. |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 126 | thread_list->SuspendAll(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 127 | Thread* self = Thread::Current(); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 128 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 129 | const char* old_cause = self->StartAssertNoThreadSuspension("Handling SIGQUIT"); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 130 | ThreadState old_state = self->SetStateUnsafe(kRunnable); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 131 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 132 | std::ostringstream os; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 133 | os << "\n" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 134 | << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 135 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 136 | DumpCmdLine(os); |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 137 | |
Jeff Brown | b4fffc7 | 2014-09-10 18:41:18 -0700 | [diff] [blame^] | 138 | // Note: The string "ABI:" is chosen to match the format used by debuggerd. |
| 139 | os << "ABI: " << GetInstructionSetString(runtime->GetInstructionSet()) << "\n"; |
| 140 | |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 141 | os << "Build type: " << (kIsDebugBuild ? "debug" : "optimized") << "\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 142 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 143 | runtime->DumpForSigQuit(os); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 144 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 145 | if (false) { |
| 146 | std::string maps; |
| 147 | if (ReadFileToString("/proc/self/maps", &maps)) { |
| 148 | os << "/proc/self/maps:\n" << maps; |
| 149 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 150 | } |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 151 | os << "----- end " << getpid() << " -----\n"; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 152 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
Mathieu Chartier | ce4689f | 2014-01-15 13:30:54 -0800 | [diff] [blame] | 153 | self->EndAssertNoThreadSuspension(old_cause); |
| 154 | thread_list->ResumeAll(); |
| 155 | // Run the checkpoints after resuming the threads to prevent deadlocks if the checkpoint function |
| 156 | // acquires the mutator lock. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 157 | if (self->ReadFlag(kCheckpointRequest)) { |
| 158 | self->RunCheckpointFunction(); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 159 | } |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 160 | Output(os.str()); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void SignalCatcher::HandleSigUsr1() { |
| 164 | LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)"; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 165 | Runtime::Current()->GetHeap()->CollectGarbage(false); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 168 | int SignalCatcher::WaitForSignal(Thread* self, SignalSet& signals) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 169 | ScopedThreadStateChange tsc(self, kWaitingInMainSignalCatcherLoop); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 170 | |
| 171 | // Signals for sigwait() must be blocked but not ignored. We |
| 172 | // block signals like SIGQUIT for all threads, so the condition |
| 173 | // is met. When the signal hits, we wake up, without any signal |
| 174 | // handlers being invoked. |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 175 | int signal_number = signals.Wait(); |
Elliott Hughes | c2f8006 | 2011-11-07 11:57:51 -0800 | [diff] [blame] | 176 | if (!ShouldHalt()) { |
| 177 | // Let the user know we got the signal, just in case the system's too screwed for us to |
| 178 | // actually do what they want us to do... |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 179 | LOG(INFO) << *self << ": reacting to signal " << signal_number; |
Elliott Hughes | c2f8006 | 2011-11-07 11:57:51 -0800 | [diff] [blame] | 180 | |
Elliott Hughes | 21a5bf2 | 2011-12-07 14:35:20 -0800 | [diff] [blame] | 181 | // If anyone's holding locks (which might prevent us from getting back into state Runnable), say so... |
| 182 | Runtime::Current()->DumpLockHolders(LOG(INFO)); |
Elliott Hughes | c2f8006 | 2011-11-07 11:57:51 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 185 | return signal_number; |
| 186 | } |
| 187 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 188 | void* SignalCatcher::Run(void* arg) { |
| 189 | SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg); |
| 190 | CHECK(signal_catcher != NULL); |
| 191 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 192 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 664bebf | 2012-11-12 16:54:11 -0800 | [diff] [blame] | 193 | CHECK(runtime->AttachCurrentThread("Signal Catcher", true, runtime->GetSystemThreadGroup(), |
| 194 | !runtime->IsCompiler())); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 195 | |
| 196 | Thread* self = Thread::Current(); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 197 | DCHECK_NE(self->GetState(), kRunnable); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 198 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 199 | MutexLock mu(self, signal_catcher->lock_); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 200 | signal_catcher->thread_ = self; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 201 | signal_catcher->cond_.Broadcast(self); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 202 | } |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 203 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 204 | // Set up mask with signals we want to handle. |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 205 | SignalSet signals; |
| 206 | signals.Add(SIGQUIT); |
| 207 | signals.Add(SIGUSR1); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 208 | |
| 209 | while (true) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 210 | int signal_number = signal_catcher->WaitForSignal(self, signals); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 211 | if (signal_catcher->ShouldHalt()) { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 212 | runtime->DetachCurrentThread(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 213 | return NULL; |
| 214 | } |
| 215 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 216 | switch (signal_number) { |
| 217 | case SIGQUIT: |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 218 | signal_catcher->HandleSigQuit(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 219 | break; |
| 220 | case SIGUSR1: |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 221 | signal_catcher->HandleSigUsr1(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 222 | break; |
| 223 | default: |
| 224 | LOG(ERROR) << "Unexpected signal %d" << signal_number; |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | } // namespace art |