blob: fe62e25443052565041d7d96c96772ecf4761dbe [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"
Elliott Hughes94ce37a2011-10-18 15:07:48 -070031#include "os.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033#include "scoped_thread_state_change.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070034#include "signal_set.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070035#include "thread.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070036#include "thread_list.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070037#include "utils.h"
38
39namespace art {
40
Elliott Hughes0d39c122012-06-06 16:41:17 -070041static void DumpCmdLine(std::ostream& os) {
42#if defined(__linux__)
43 // Show the original command line, and the current command line too if it's changed.
44 // On Android, /proc/self/cmdline will have been rewritten to something like "system_server".
45 std::string current_cmd_line;
46 if (ReadFileToString("/proc/self/cmdline", &current_cmd_line)) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -070047 current_cmd_line.resize(current_cmd_line.size() - 1); // Lose the trailing '\0'.
Elliott Hughes0d39c122012-06-06 16:41:17 -070048 std::replace(current_cmd_line.begin(), current_cmd_line.end(), '\0', ' ');
49
50 os << "Cmdline: " << current_cmd_line;
51 const char* stashed_cmd_line = GetCmdLine();
52 if (stashed_cmd_line != NULL && current_cmd_line != stashed_cmd_line) {
53 os << "Original command line: " << stashed_cmd_line;
54 }
Elliott Hughesae80b492012-04-24 10:43:17 -070055 }
Elliott Hughes0d39c122012-06-06 16:41:17 -070056 os << "\n";
57#else
58 os << "Cmdline: " << GetCmdLine() << "\n";
Elliott Hughesabbe07d2012-06-05 17:42:23 -070059#endif
Elliott Hughes0d39c122012-06-06 16:41:17 -070060}
Elliott Hughesae80b492012-04-24 10:43:17 -070061
Elliott Hughes94ce37a2011-10-18 15:07:48 -070062SignalCatcher::SignalCatcher(const std::string& stack_trace_file)
63 : stack_trace_file_(stack_trace_file),
64 lock_("SignalCatcher lock"),
Ian Rogersc604d732012-10-14 16:09:54 -070065 cond_("SignalCatcher::cond_", lock_),
Elliott Hughes94ce37a2011-10-18 15:07:48 -070066 thread_(NULL) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070067 SetHaltFlag(false);
68
Elliott Hughese27955c2011-08-26 15:21:24 -070069 // Create a raw pthread; its start routine will attach to the runtime.
Elliott Hughes8d768a92011-09-14 16:35:25 -070070 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Run, this), "signal catcher thread");
71
Ian Rogers81d425b2012-09-27 16:03:43 -070072 Thread* self = Thread::Current();
73 MutexLock mu(self, lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -070074 while (thread_ == NULL) {
Ian Rogersc604d732012-10-14 16:09:54 -070075 cond_.Wait(self);
Elliott Hughese27955c2011-08-26 15:21:24 -070076 }
77}
78
79SignalCatcher::~SignalCatcher() {
80 // Since we know the thread is just sitting around waiting for signals
81 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070082 SetHaltFlag(true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070083 CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown");
84 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "signal catcher shutdown");
Elliott Hughese27955c2011-08-26 15:21:24 -070085}
86
Elliott Hughes5fe594f2011-09-08 12:33:17 -070087void SignalCatcher::SetHaltFlag(bool new_value) {
Ian Rogers50b35e22012-10-04 10:09:15 -070088 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -070089 halt_ = new_value;
90}
91
92bool SignalCatcher::ShouldHalt() {
Ian Rogers50b35e22012-10-04 10:09:15 -070093 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -070094 return halt_;
95}
96
Elliott Hughes94ce37a2011-10-18 15:07:48 -070097void SignalCatcher::Output(const std::string& s) {
98 if (stack_trace_file_.empty()) {
99 LOG(INFO) << s;
100 return;
101 }
102
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700103 ScopedThreadStateChange tsc(Thread::Current(), kWaitingForSignalCatcherOutput);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700104 int fd = open(stack_trace_file_.c_str(), O_APPEND | O_CREAT | O_WRONLY, 0666);
105 if (fd == -1) {
106 PLOG(ERROR) << "Unable to open stack trace file '" << stack_trace_file_ << "'";
107 return;
108 }
Elliott Hughes76160052012-12-12 16:31:20 -0800109 UniquePtr<File> file(new File(fd, stack_trace_file_));
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700110 if (!file->WriteFully(s.data(), s.size())) {
111 PLOG(ERROR) << "Failed to write stack traces to '" << stack_trace_file_ << "'";
112 } else {
113 LOG(INFO) << "Wrote stack traces to '" << stack_trace_file_ << "'";
114 }
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700115}
116
Elliott Hughese27955c2011-08-26 15:21:24 -0700117void SignalCatcher::HandleSigQuit() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700118 Runtime* runtime = Runtime::Current();
119 ThreadList* thread_list = runtime->GetThreadList();
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700120
Ian Rogers120f1c72012-09-28 17:17:10 -0700121 // Grab exclusively the mutator lock, set state to Runnable without checking for a pending
122 // suspend request as we're going to suspend soon anyway. We set the state to Runnable to avoid
123 // giving away the mutator lock.
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700124 thread_list->SuspendAll();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 Thread* self = Thread::Current();
Ian Rogers81d425b2012-09-27 16:03:43 -0700126 Locks::mutator_lock_->AssertExclusiveHeld(self);
Elliott Hughes80537bb2013-01-04 16:37:26 -0800127 const char* old_cause = self->StartAssertNoThreadSuspension("Handling SIGQUIT");
Ian Rogers120f1c72012-09-28 17:17:10 -0700128 ThreadState old_state = self->SetStateUnsafe(kRunnable);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700130 std::ostringstream os;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700131 os << "\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700133
Elliott Hughes0d39c122012-06-06 16:41:17 -0700134 DumpCmdLine(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700135
136 os << "Build type: " << (kIsDebugBuild ? "debug" : "optimized") << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700137
Elliott Hughesc967f782012-04-16 10:23:15 -0700138 runtime->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700139
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700140 if (false) {
141 std::string maps;
142 if (ReadFileToString("/proc/self/maps", &maps)) {
143 os << "/proc/self/maps:\n" << maps;
144 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700145 }
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700146 os << "----- end " << getpid() << " -----\n";
Ian Rogers120f1c72012-09-28 17:17:10 -0700147 CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700148 if (self->ReadFlag(kCheckpointRequest)) {
149 self->RunCheckpointFunction();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700150 }
Ian Rogers120f1c72012-09-28 17:17:10 -0700151 self->EndAssertNoThreadSuspension(old_cause);
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700152 thread_list->ResumeAll();
Elliott Hughese27955c2011-08-26 15:21:24 -0700153
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700154 Output(os.str());
Elliott Hughese27955c2011-08-26 15:21:24 -0700155}
156
157void SignalCatcher::HandleSigUsr1() {
158 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800159 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughese27955c2011-08-26 15:21:24 -0700160}
161
Elliott Hughesf8349362012-06-18 15:00:06 -0700162int SignalCatcher::WaitForSignal(Thread* self, SignalSet& signals) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 ScopedThreadStateChange tsc(self, kWaitingInMainSignalCatcherLoop);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700164
165 // Signals for sigwait() must be blocked but not ignored. We
166 // block signals like SIGQUIT for all threads, so the condition
167 // is met. When the signal hits, we wake up, without any signal
168 // handlers being invoked.
Elliott Hughes457005c2012-04-16 13:54:25 -0700169 int signal_number = signals.Wait();
Elliott Hughesc2f80062011-11-07 11:57:51 -0800170 if (!ShouldHalt()) {
171 // Let the user know we got the signal, just in case the system's too screwed for us to
172 // actually do what they want us to do...
Elliott Hughesf8349362012-06-18 15:00:06 -0700173 LOG(INFO) << *self << ": reacting to signal " << signal_number;
Elliott Hughesc2f80062011-11-07 11:57:51 -0800174
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800175 // If anyone's holding locks (which might prevent us from getting back into state Runnable), say so...
176 Runtime::Current()->DumpLockHolders(LOG(INFO));
Elliott Hughesc2f80062011-11-07 11:57:51 -0800177 }
178
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700179 return signal_number;
180}
181
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700182void* SignalCatcher::Run(void* arg) {
183 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
184 CHECK(signal_catcher != NULL);
185
Elliott Hughes8d768a92011-09-14 16:35:25 -0700186 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800187 CHECK(runtime->AttachCurrentThread("Signal Catcher", true, runtime->GetSystemThreadGroup(),
188 !runtime->IsCompiler()));
Elliott Hughesf8349362012-06-18 15:00:06 -0700189
190 Thread* self = Thread::Current();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700191 DCHECK_NE(self->GetState(), kRunnable);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700192 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700193 MutexLock mu(self, signal_catcher->lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700194 signal_catcher->thread_ = self;
Ian Rogersc604d732012-10-14 16:09:54 -0700195 signal_catcher->cond_.Broadcast(self);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700196 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700197
Elliott Hughese27955c2011-08-26 15:21:24 -0700198 // Set up mask with signals we want to handle.
Elliott Hughes457005c2012-04-16 13:54:25 -0700199 SignalSet signals;
200 signals.Add(SIGQUIT);
201 signals.Add(SIGUSR1);
Elliott Hughese27955c2011-08-26 15:21:24 -0700202
203 while (true) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700204 int signal_number = signal_catcher->WaitForSignal(self, signals);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700205 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700206 runtime->DetachCurrentThread();
Elliott Hughese27955c2011-08-26 15:21:24 -0700207 return NULL;
208 }
209
Elliott Hughese27955c2011-08-26 15:21:24 -0700210 switch (signal_number) {
211 case SIGQUIT:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700212 signal_catcher->HandleSigQuit();
Elliott Hughese27955c2011-08-26 15:21:24 -0700213 break;
214 case SIGUSR1:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700215 signal_catcher->HandleSigUsr1();
Elliott Hughese27955c2011-08-26 15:21:24 -0700216 break;
217 default:
218 LOG(ERROR) << "Unexpected signal %d" << signal_number;
219 break;
220 }
221 }
222}
223
224} // namespace art