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