blob: 11e06fe885cfbdde1b5802c38f2521c50b52b9f6 [file] [log] [blame]
Elliott Hughese27955c2011-08-26 15:21:24 -07001/*
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 Hughes94ce37a2011-10-18 15:07:48 -070019#include <fcntl.h>
Elliott Hughese27955c2011-08-26 15:21:24 -070020#include <pthread.h>
21#include <signal.h>
22#include <stdlib.h>
Elliott Hughes94ce37a2011-10-18 15:07:48 -070023#include <sys/stat.h>
Elliott Hughese27955c2011-08-26 15:21:24 -070024#include <sys/time.h>
Elliott Hughes94ce37a2011-10-18 15:07:48 -070025#include <sys/types.h>
Elliott Hughese27955c2011-08-26 15:21:24 -070026#include <unistd.h>
27
Elliott Hughes76160052012-12-12 16:31:20 -080028#include "base/unix_file/fd_file.h"
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070029#include "class_linker.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070030#include "gc/heap.h"
Jeff Brownb4fffc72014-09-10 18:41:18 -070031#include "instruction_set.h"
Elliott Hughes94ce37a2011-10-18 15:07:48 -070032#include "os.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070033#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034#include "scoped_thread_state_change.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070035#include "signal_set.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070036#include "thread.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070037#include "thread_list.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070038#include "utils.h"
39
40namespace art {
41
Elliott Hughes0d39c122012-06-06 16:41:17 -070042static 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 Brownb4fffc72014-09-10 18:41:18 -070046 // Note: The string "Cmd line:" is chosen to match the format used by debuggerd.
Elliott Hughes0d39c122012-06-06 16:41:17 -070047 std::string current_cmd_line;
48 if (ReadFileToString("/proc/self/cmdline", &current_cmd_line)) {
Jeff Brownb4fffc72014-09-10 18:41:18 -070049 current_cmd_line.resize(current_cmd_line.find_last_not_of('\0') + 1); // trim trailing '\0's
Elliott Hughes0d39c122012-06-06 16:41:17 -070050 std::replace(current_cmd_line.begin(), current_cmd_line.end(), '\0', ' ');
51
Jeff Brownb4fffc72014-09-10 18:41:18 -070052 os << "Cmd line: " << current_cmd_line << "\n";
Elliott Hughes0d39c122012-06-06 16:41:17 -070053 const char* stashed_cmd_line = GetCmdLine();
Jeff Brownb4fffc72014-09-10 18:41:18 -070054 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 Hughes0d39c122012-06-06 16:41:17 -070057 }
Elliott Hughesae80b492012-04-24 10:43:17 -070058 }
Elliott Hughes0d39c122012-06-06 16:41:17 -070059#else
Jeff Brownb4fffc72014-09-10 18:41:18 -070060 os << "Cmd line: " << GetCmdLine() << "\n";
Elliott Hughesabbe07d2012-06-05 17:42:23 -070061#endif
Elliott Hughes0d39c122012-06-06 16:41:17 -070062}
Elliott Hughesae80b492012-04-24 10:43:17 -070063
Elliott Hughes94ce37a2011-10-18 15:07:48 -070064SignalCatcher::SignalCatcher(const std::string& stack_trace_file)
65 : stack_trace_file_(stack_trace_file),
66 lock_("SignalCatcher lock"),
Ian Rogersc604d732012-10-14 16:09:54 -070067 cond_("SignalCatcher::cond_", lock_),
Elliott Hughes94ce37a2011-10-18 15:07:48 -070068 thread_(NULL) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070069 SetHaltFlag(false);
70
Elliott Hughese27955c2011-08-26 15:21:24 -070071 // Create a raw pthread; its start routine will attach to the runtime.
Elliott Hughes8d768a92011-09-14 16:35:25 -070072 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Run, this), "signal catcher thread");
73
Ian Rogers81d425b2012-09-27 16:03:43 -070074 Thread* self = Thread::Current();
75 MutexLock mu(self, lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -070076 while (thread_ == NULL) {
Ian Rogersc604d732012-10-14 16:09:54 -070077 cond_.Wait(self);
Elliott Hughese27955c2011-08-26 15:21:24 -070078 }
79}
80
81SignalCatcher::~SignalCatcher() {
82 // Since we know the thread is just sitting around waiting for signals
83 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070084 SetHaltFlag(true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070085 CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown");
86 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "signal catcher shutdown");
Elliott Hughese27955c2011-08-26 15:21:24 -070087}
88
Elliott Hughes5fe594f2011-09-08 12:33:17 -070089void SignalCatcher::SetHaltFlag(bool new_value) {
Ian Rogers50b35e22012-10-04 10:09:15 -070090 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -070091 halt_ = new_value;
92}
93
94bool SignalCatcher::ShouldHalt() {
Ian Rogers50b35e22012-10-04 10:09:15 -070095 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -070096 return halt_;
97}
98
Elliott Hughes94ce37a2011-10-18 15:07:48 -070099void SignalCatcher::Output(const std::string& s) {
100 if (stack_trace_file_.empty()) {
101 LOG(INFO) << s;
102 return;
103 }
104
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105 ScopedThreadStateChange tsc(Thread::Current(), kWaitingForSignalCatcherOutput);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700106 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 Rogers700a4022014-05-19 16:49:03 -0700111 std::unique_ptr<File> file(new File(fd, stack_trace_file_));
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700112 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 Hughes94ce37a2011-10-18 15:07:48 -0700117}
118
Elliott Hughese27955c2011-08-26 15:21:24 -0700119void SignalCatcher::HandleSigQuit() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700120 Runtime* runtime = Runtime::Current();
121 ThreadList* thread_list = runtime->GetThreadList();
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700122
Ian Rogers120f1c72012-09-28 17:17:10 -0700123 // 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 Hughesaccd83d2011-10-17 14:25:58 -0700126 thread_list->SuspendAll();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127 Thread* self = Thread::Current();
Ian Rogers81d425b2012-09-27 16:03:43 -0700128 Locks::mutator_lock_->AssertExclusiveHeld(self);
Elliott Hughes80537bb2013-01-04 16:37:26 -0800129 const char* old_cause = self->StartAssertNoThreadSuspension("Handling SIGQUIT");
Ian Rogers120f1c72012-09-28 17:17:10 -0700130 ThreadState old_state = self->SetStateUnsafe(kRunnable);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700132 std::ostringstream os;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700133 os << "\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700134 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700135
Elliott Hughes0d39c122012-06-06 16:41:17 -0700136 DumpCmdLine(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700137
Jeff Brownb4fffc72014-09-10 18:41:18 -0700138 // Note: The string "ABI:" is chosen to match the format used by debuggerd.
139 os << "ABI: " << GetInstructionSetString(runtime->GetInstructionSet()) << "\n";
140
Elliott Hughesae80b492012-04-24 10:43:17 -0700141 os << "Build type: " << (kIsDebugBuild ? "debug" : "optimized") << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700142
Elliott Hughesc967f782012-04-16 10:23:15 -0700143 runtime->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700144
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700145 if (false) {
146 std::string maps;
147 if (ReadFileToString("/proc/self/maps", &maps)) {
148 os << "/proc/self/maps:\n" << maps;
149 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700150 }
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700151 os << "----- end " << getpid() << " -----\n";
Ian Rogers120f1c72012-09-28 17:17:10 -0700152 CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
Mathieu Chartierce4689f2014-01-15 13:30:54 -0800153 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 Chartier858f1c52012-10-17 17:45:55 -0700157 if (self->ReadFlag(kCheckpointRequest)) {
158 self->RunCheckpointFunction();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700159 }
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700160 Output(os.str());
Elliott Hughese27955c2011-08-26 15:21:24 -0700161}
162
163void SignalCatcher::HandleSigUsr1() {
164 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800165 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughese27955c2011-08-26 15:21:24 -0700166}
167
Elliott Hughesf8349362012-06-18 15:00:06 -0700168int SignalCatcher::WaitForSignal(Thread* self, SignalSet& signals) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700169 ScopedThreadStateChange tsc(self, kWaitingInMainSignalCatcherLoop);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700170
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 Hughes457005c2012-04-16 13:54:25 -0700175 int signal_number = signals.Wait();
Elliott Hughesc2f80062011-11-07 11:57:51 -0800176 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 Hughesf8349362012-06-18 15:00:06 -0700179 LOG(INFO) << *self << ": reacting to signal " << signal_number;
Elliott Hughesc2f80062011-11-07 11:57:51 -0800180
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800181 // If anyone's holding locks (which might prevent us from getting back into state Runnable), say so...
182 Runtime::Current()->DumpLockHolders(LOG(INFO));
Elliott Hughesc2f80062011-11-07 11:57:51 -0800183 }
184
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700185 return signal_number;
186}
187
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700188void* SignalCatcher::Run(void* arg) {
189 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
190 CHECK(signal_catcher != NULL);
191
Elliott Hughes8d768a92011-09-14 16:35:25 -0700192 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800193 CHECK(runtime->AttachCurrentThread("Signal Catcher", true, runtime->GetSystemThreadGroup(),
194 !runtime->IsCompiler()));
Elliott Hughesf8349362012-06-18 15:00:06 -0700195
196 Thread* self = Thread::Current();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700197 DCHECK_NE(self->GetState(), kRunnable);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700198 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700199 MutexLock mu(self, signal_catcher->lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700200 signal_catcher->thread_ = self;
Ian Rogersc604d732012-10-14 16:09:54 -0700201 signal_catcher->cond_.Broadcast(self);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700202 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700203
Elliott Hughese27955c2011-08-26 15:21:24 -0700204 // Set up mask with signals we want to handle.
Elliott Hughes457005c2012-04-16 13:54:25 -0700205 SignalSet signals;
206 signals.Add(SIGQUIT);
207 signals.Add(SIGUSR1);
Elliott Hughese27955c2011-08-26 15:21:24 -0700208
209 while (true) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700210 int signal_number = signal_catcher->WaitForSignal(self, signals);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700211 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700212 runtime->DetachCurrentThread();
Elliott Hughese27955c2011-08-26 15:21:24 -0700213 return NULL;
214 }
215
Elliott Hughese27955c2011-08-26 15:21:24 -0700216 switch (signal_number) {
217 case SIGQUIT:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700218 signal_catcher->HandleSigQuit();
Elliott Hughese27955c2011-08-26 15:21:24 -0700219 break;
220 case SIGUSR1:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700221 signal_catcher->HandleSigUsr1();
Elliott Hughese27955c2011-08-26 15:21:24 -0700222 break;
223 default:
224 LOG(ERROR) << "Unexpected signal %d" << signal_number;
225 break;
226 }
227 }
228}
229
230} // namespace art