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 | |
Josh Gao | 691839c | 2017-10-03 13:23:23 -0700 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> |
| 31 | |
| 32 | #if defined(ART_TARGET_ANDROID) |
| 33 | #include <tombstoned/tombstoned.h> |
| 34 | #endif |
| 35 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 36 | #include "arch/instruction_set.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 37 | #include "base/file_utils.h" |
Andreas Gampe | 39b378c | 2017-12-07 15:44:13 -0800 | [diff] [blame] | 38 | #include "base/logging.h" // For GetCmdLine. |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame^] | 39 | #include "base/os.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 40 | #include "base/time_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 41 | #include "base/unix_file/fd_file.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame^] | 42 | #include "base/utils.h" |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 43 | #include "class_linker.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 44 | #include "gc/heap.h" |
Mathieu Chartier | 07ea07e | 2017-04-05 17:23:54 -0700 | [diff] [blame] | 45 | #include "jit/profile_saver.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 46 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 47 | #include "scoped_thread_state_change-inl.h" |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 48 | #include "signal_set.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 49 | #include "thread.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 50 | #include "thread_list.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 51 | |
| 52 | namespace art { |
| 53 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 54 | static void DumpCmdLine(std::ostream& os) { |
| 55 | #if defined(__linux__) |
| 56 | // Show the original command line, and the current command line too if it's changed. |
| 57 | // 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] | 58 | // 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] | 59 | std::string current_cmd_line; |
| 60 | if (ReadFileToString("/proc/self/cmdline", ¤t_cmd_line)) { |
Jeff Brown | b4fffc7 | 2014-09-10 18:41:18 -0700 | [diff] [blame] | 61 | 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] | 62 | std::replace(current_cmd_line.begin(), current_cmd_line.end(), '\0', ' '); |
| 63 | |
Jeff Brown | b4fffc7 | 2014-09-10 18:41:18 -0700 | [diff] [blame] | 64 | os << "Cmd line: " << current_cmd_line << "\n"; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 65 | const char* stashed_cmd_line = GetCmdLine(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 66 | if (stashed_cmd_line != nullptr && current_cmd_line != stashed_cmd_line |
Jeff Brown | b4fffc7 | 2014-09-10 18:41:18 -0700 | [diff] [blame] | 67 | && strcmp(stashed_cmd_line, "<unset>") != 0) { |
| 68 | os << "Original command line: " << stashed_cmd_line << "\n"; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 69 | } |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 70 | } |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 71 | #else |
Jeff Brown | b4fffc7 | 2014-09-10 18:41:18 -0700 | [diff] [blame] | 72 | os << "Cmd line: " << GetCmdLine() << "\n"; |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 73 | #endif |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 74 | } |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 75 | |
Narayan Kamath | eb71033 | 2017-05-10 11:48:46 +0100 | [diff] [blame] | 76 | SignalCatcher::SignalCatcher(const std::string& stack_trace_file, |
| 77 | bool use_tombstoned_stack_trace_fd) |
| 78 | : stack_trace_file_(stack_trace_file), |
| 79 | use_tombstoned_stack_trace_fd_(use_tombstoned_stack_trace_fd), |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 80 | lock_("SignalCatcher lock"), |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 81 | cond_("SignalCatcher::cond_", lock_), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 82 | thread_(nullptr) { |
Narayan Kamath | eb71033 | 2017-05-10 11:48:46 +0100 | [diff] [blame] | 83 | #if !defined(ART_TARGET_ANDROID) |
| 84 | // We're not running on Android, so we can't communicate with tombstoned |
| 85 | // to ask for an open file. |
| 86 | CHECK(!use_tombstoned_stack_trace_fd_); |
| 87 | #endif |
| 88 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 89 | SetHaltFlag(false); |
| 90 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 91 | // Create a raw pthread; its start routine will attach to the runtime. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 92 | CHECK_PTHREAD_CALL(pthread_create, (&pthread_, nullptr, &Run, this), "signal catcher thread"); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 93 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 94 | Thread* self = Thread::Current(); |
| 95 | MutexLock mu(self, lock_); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 96 | while (thread_ == nullptr) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 97 | cond_.Wait(self); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | SignalCatcher::~SignalCatcher() { |
| 102 | // Since we know the thread is just sitting around waiting for signals |
| 103 | // to arrive, send it one. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 104 | SetHaltFlag(true); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 105 | CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown"); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 106 | CHECK_PTHREAD_CALL(pthread_join, (pthread_, nullptr), "signal catcher shutdown"); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 109 | void SignalCatcher::SetHaltFlag(bool new_value) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 110 | MutexLock mu(Thread::Current(), lock_); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 111 | halt_ = new_value; |
| 112 | } |
| 113 | |
| 114 | bool SignalCatcher::ShouldHalt() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 115 | MutexLock mu(Thread::Current(), lock_); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 116 | return halt_; |
| 117 | } |
| 118 | |
Narayan Kamath | eb71033 | 2017-05-10 11:48:46 +0100 | [diff] [blame] | 119 | bool SignalCatcher::OpenStackTraceFile(android::base::unique_fd* tombstone_fd, |
| 120 | android::base::unique_fd* output_fd) { |
| 121 | if (use_tombstoned_stack_trace_fd_) { |
| 122 | #if defined(ART_TARGET_ANDROID) |
Narayan Kamath | e0f02b7 | 2017-05-30 16:22:28 +0100 | [diff] [blame] | 123 | return tombstoned_connect(getpid(), tombstone_fd, output_fd, kDebuggerdJavaBacktrace); |
Narayan Kamath | eb71033 | 2017-05-10 11:48:46 +0100 | [diff] [blame] | 124 | #else |
| 125 | UNUSED(tombstone_fd); |
| 126 | UNUSED(output_fd); |
| 127 | #endif |
Narayan Kamath | 84695ae | 2017-04-07 15:41:41 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Narayan Kamath | eb71033 | 2017-05-10 11:48:46 +0100 | [diff] [blame] | 130 | // The runtime is not configured to dump traces to a file, will LOG(INFO) |
| 131 | // instead. |
| 132 | if (stack_trace_file_.empty()) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | int fd = open(stack_trace_file_.c_str(), O_APPEND | O_CREAT | O_WRONLY, 0666); |
| 137 | if (fd == -1) { |
| 138 | PLOG(ERROR) << "Unable to open stack trace file '" << stack_trace_file_ << "'"; |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | output_fd->reset(fd); |
| 143 | return true; |
Narayan Kamath | 84695ae | 2017-04-07 15:41:41 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 146 | void SignalCatcher::Output(const std::string& s) { |
Narayan Kamath | eb71033 | 2017-05-10 11:48:46 +0100 | [diff] [blame] | 147 | android::base::unique_fd tombstone_fd; |
| 148 | android::base::unique_fd output_fd; |
| 149 | if (!OpenStackTraceFile(&tombstone_fd, &output_fd)) { |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 150 | LOG(INFO) << s; |
| 151 | return; |
| 152 | } |
| 153 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 154 | ScopedThreadStateChange tsc(Thread::Current(), kWaitingForSignalCatcherOutput); |
Narayan Kamath | eb71033 | 2017-05-10 11:48:46 +0100 | [diff] [blame] | 155 | |
| 156 | std::unique_ptr<File> file(new File(output_fd.release(), true /* check_usage */)); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 157 | bool success = file->WriteFully(s.data(), s.size()); |
| 158 | if (success) { |
| 159 | success = file->FlushCloseOrErase() == 0; |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 160 | } else { |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 161 | file->Erase(); |
| 162 | } |
Narayan Kamath | eb71033 | 2017-05-10 11:48:46 +0100 | [diff] [blame] | 163 | |
| 164 | const std::string output_path_msg = (use_tombstoned_stack_trace_fd_) ? |
| 165 | "[tombstoned]" : stack_trace_file_; |
| 166 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 167 | if (success) { |
Narayan Kamath | eb71033 | 2017-05-10 11:48:46 +0100 | [diff] [blame] | 168 | LOG(INFO) << "Wrote stack traces to '" << output_path_msg << "'"; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 169 | } else { |
Narayan Kamath | eb71033 | 2017-05-10 11:48:46 +0100 | [diff] [blame] | 170 | PLOG(ERROR) << "Failed to write stack traces to '" << output_path_msg << "'"; |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 171 | } |
Narayan Kamath | eb71033 | 2017-05-10 11:48:46 +0100 | [diff] [blame] | 172 | |
| 173 | #if defined(ART_TARGET_ANDROID) |
Narayan Kamath | 2cfd612 | 2017-07-11 17:40:56 +0100 | [diff] [blame] | 174 | if (use_tombstoned_stack_trace_fd_ && !tombstoned_notify_completion(tombstone_fd)) { |
Josh Gao | ff9e43f | 2017-10-16 14:10:08 -0700 | [diff] [blame] | 175 | PLOG(WARNING) << "Unable to notify tombstoned of dump completion"; |
Narayan Kamath | eb71033 | 2017-05-10 11:48:46 +0100 | [diff] [blame] | 176 | } |
| 177 | #endif |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 180 | void SignalCatcher::HandleSigQuit() { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 181 | Runtime* runtime = Runtime::Current(); |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 182 | std::ostringstream os; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 183 | os << "\n" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 184 | << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 185 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 186 | DumpCmdLine(os); |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 187 | |
Andreas Gampe | dd67125 | 2015-07-23 14:37:18 -0700 | [diff] [blame] | 188 | // Note: The strings "Build fingerprint:" and "ABI:" are chosen to match the format used by |
| 189 | // debuggerd. This allows, for example, the stack tool to work. |
| 190 | std::string fingerprint = runtime->GetFingerprint(); |
| 191 | os << "Build fingerprint: '" << (fingerprint.empty() ? "unknown" : fingerprint) << "'\n"; |
| 192 | os << "ABI: '" << GetInstructionSetString(runtime->GetInstructionSet()) << "'\n"; |
Jeff Brown | b4fffc7 | 2014-09-10 18:41:18 -0700 | [diff] [blame] | 193 | |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 194 | os << "Build type: " << (kIsDebugBuild ? "debug" : "optimized") << "\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 195 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 196 | runtime->DumpForSigQuit(os); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 197 | |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 198 | if ((false)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 199 | std::string maps; |
| 200 | if (ReadFileToString("/proc/self/maps", &maps)) { |
| 201 | os << "/proc/self/maps:\n" << maps; |
| 202 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 203 | } |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 204 | os << "----- end " << getpid() << " -----\n"; |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 205 | Output(os.str()); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void SignalCatcher::HandleSigUsr1() { |
Mathieu Chartier | 07ea07e | 2017-04-05 17:23:54 -0700 | [diff] [blame] | 209 | LOG(INFO) << "SIGUSR1 forcing GC (no HPROF) and profile save"; |
Roland Levillain | af29031 | 2018-02-27 20:02:17 +0000 | [diff] [blame] | 210 | Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references */ false); |
Mathieu Chartier | 07ea07e | 2017-04-05 17:23:54 -0700 | [diff] [blame] | 211 | ProfileSaver::ForceProcessProfiles(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 214 | int SignalCatcher::WaitForSignal(Thread* self, SignalSet& signals) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 215 | ScopedThreadStateChange tsc(self, kWaitingInMainSignalCatcherLoop); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 216 | |
| 217 | // Signals for sigwait() must be blocked but not ignored. We |
| 218 | // block signals like SIGQUIT for all threads, so the condition |
| 219 | // is met. When the signal hits, we wake up, without any signal |
| 220 | // handlers being invoked. |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 221 | int signal_number = signals.Wait(); |
Elliott Hughes | c2f8006 | 2011-11-07 11:57:51 -0800 | [diff] [blame] | 222 | if (!ShouldHalt()) { |
| 223 | // Let the user know we got the signal, just in case the system's too screwed for us to |
| 224 | // actually do what they want us to do... |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 225 | LOG(INFO) << *self << ": reacting to signal " << signal_number; |
Elliott Hughes | c2f8006 | 2011-11-07 11:57:51 -0800 | [diff] [blame] | 226 | |
Elliott Hughes | 21a5bf2 | 2011-12-07 14:35:20 -0800 | [diff] [blame] | 227 | // If anyone's holding locks (which might prevent us from getting back into state Runnable), say so... |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 228 | Runtime::Current()->DumpLockHolders(LOG_STREAM(INFO)); |
Elliott Hughes | c2f8006 | 2011-11-07 11:57:51 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 231 | return signal_number; |
| 232 | } |
| 233 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 234 | void* SignalCatcher::Run(void* arg) { |
| 235 | SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 236 | CHECK(signal_catcher != nullptr); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 237 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 238 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 664bebf | 2012-11-12 16:54:11 -0800 | [diff] [blame] | 239 | CHECK(runtime->AttachCurrentThread("Signal Catcher", true, runtime->GetSystemThreadGroup(), |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 240 | !runtime->IsAotCompiler())); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 241 | |
| 242 | Thread* self = Thread::Current(); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 243 | DCHECK_NE(self->GetState(), kRunnable); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 244 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 245 | MutexLock mu(self, signal_catcher->lock_); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 246 | signal_catcher->thread_ = self; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 247 | signal_catcher->cond_.Broadcast(self); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 248 | } |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 249 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 250 | // Set up mask with signals we want to handle. |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 251 | SignalSet signals; |
| 252 | signals.Add(SIGQUIT); |
| 253 | signals.Add(SIGUSR1); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 254 | |
| 255 | while (true) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 256 | int signal_number = signal_catcher->WaitForSignal(self, signals); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 257 | if (signal_catcher->ShouldHalt()) { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 258 | runtime->DetachCurrentThread(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 259 | return nullptr; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 262 | switch (signal_number) { |
| 263 | case SIGQUIT: |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 264 | signal_catcher->HandleSigQuit(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 265 | break; |
| 266 | case SIGUSR1: |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 267 | signal_catcher->HandleSigUsr1(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 268 | break; |
| 269 | default: |
| 270 | LOG(ERROR) << "Unexpected signal %d" << signal_number; |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | } // namespace art |