blob: aad6a08b59e63a2e8d9c7b4835817a86dce80a9c [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
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070028#include "class_linker.h"
Elliott Hughes94ce37a2011-10-18 15:07:48 -070029#include "file.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070030#include "heap.h"
Elliott Hughes94ce37a2011-10-18 15:07:48 -070031#include "os.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "runtime.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080033#include "scoped_heap_lock.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070034#include "thread.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070035#include "thread_list.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070036#include "utils.h"
37
38namespace art {
39
Elliott Hughes94ce37a2011-10-18 15:07:48 -070040SignalCatcher::SignalCatcher(const std::string& stack_trace_file)
41 : stack_trace_file_(stack_trace_file),
42 lock_("SignalCatcher lock"),
43 cond_("SignalCatcher::cond_"),
44 thread_(NULL) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070045 SetHaltFlag(false);
46
Elliott Hughese27955c2011-08-26 15:21:24 -070047 // Create a raw pthread; its start routine will attach to the runtime.
Elliott Hughes8d768a92011-09-14 16:35:25 -070048 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Run, this), "signal catcher thread");
49
Elliott Hughes8d768a92011-09-14 16:35:25 -070050 MutexLock mu(lock_);
51 while (thread_ == NULL) {
Elliott Hughes5f791332011-09-15 17:45:30 -070052 cond_.Wait(lock_);
Elliott Hughese27955c2011-08-26 15:21:24 -070053 }
54}
55
56SignalCatcher::~SignalCatcher() {
57 // Since we know the thread is just sitting around waiting for signals
58 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070059 SetHaltFlag(true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070060 CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown");
61 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "signal catcher shutdown");
Elliott Hughese27955c2011-08-26 15:21:24 -070062}
63
Elliott Hughes5fe594f2011-09-08 12:33:17 -070064void SignalCatcher::SetHaltFlag(bool new_value) {
65 MutexLock mu(lock_);
66 halt_ = new_value;
67}
68
69bool SignalCatcher::ShouldHalt() {
70 MutexLock mu(lock_);
71 return halt_;
72}
73
Elliott Hughes94ce37a2011-10-18 15:07:48 -070074void SignalCatcher::Output(const std::string& s) {
75 if (stack_trace_file_.empty()) {
76 LOG(INFO) << s;
77 return;
78 }
79
80 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
81 int fd = open(stack_trace_file_.c_str(), O_APPEND | O_CREAT | O_WRONLY, 0666);
82 if (fd == -1) {
83 PLOG(ERROR) << "Unable to open stack trace file '" << stack_trace_file_ << "'";
84 return;
85 }
86 UniquePtr<File> file(OS::FileFromFd(stack_trace_file_.c_str(), fd));
87 if (!file->WriteFully(s.data(), s.size())) {
88 PLOG(ERROR) << "Failed to write stack traces to '" << stack_trace_file_ << "'";
89 } else {
90 LOG(INFO) << "Wrote stack traces to '" << stack_trace_file_ << "'";
91 }
92 close(fd);
93}
94
Elliott Hughese27955c2011-08-26 15:21:24 -070095void SignalCatcher::HandleSigQuit() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070096 Runtime* runtime = Runtime::Current();
97 ThreadList* thread_list = runtime->GetThreadList();
Elliott Hughesaccd83d2011-10-17 14:25:58 -070098
Elliott Hughes831afe42011-12-15 17:27:34 -080099 // We take the heap lock before suspending all threads so we don't end up in a situation where
100 // one of the suspended threads suspended via the implicit FullSuspendCheck on the slow path of
101 // Heap::Lock, which is the only case where a thread can be suspended while holding the heap lock.
102 // (We need the heap lock when we dump the thread list. We could probably fix this by duplicating
103 // more state from java.lang.Thread in struct Thread.)
104 ScopedHeapLock heap_lock;
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700105 thread_list->SuspendAll();
Elliott Hughese27955c2011-08-26 15:21:24 -0700106
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700107 std::ostringstream os;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700108 os << "\n"
Elliott Hughesd92bec42011-09-02 17:04:36 -0700109 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700110
Elliott Hughesd92bec42011-09-02 17:04:36 -0700111 std::string cmdline;
112 if (ReadFileToString("/proc/self/cmdline", &cmdline)) {
113 std::replace(cmdline.begin(), cmdline.end(), '\0', ' ');
114 os << "Cmd line: " << cmdline << "\n";
115 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700116
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700117 runtime->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700118
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700119 if (false) {
120 std::string maps;
121 if (ReadFileToString("/proc/self/maps", &maps)) {
122 os << "/proc/self/maps:\n" << maps;
123 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700124 }
125
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700126 os << "----- end " << getpid() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700127
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700128 thread_list->ResumeAll();
Elliott Hughese27955c2011-08-26 15:21:24 -0700129
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700130 Output(os.str());
Elliott Hughese27955c2011-08-26 15:21:24 -0700131}
132
133void SignalCatcher::HandleSigUsr1() {
134 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800135 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughese27955c2011-08-26 15:21:24 -0700136}
137
Elliott Hughesc2f80062011-11-07 11:57:51 -0800138int SignalCatcher::WaitForSignal(sigset_t& mask) {
139 ScopedThreadStateChange tsc(thread_, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700140
141 // Signals for sigwait() must be blocked but not ignored. We
142 // block signals like SIGQUIT for all threads, so the condition
143 // is met. When the signal hits, we wake up, without any signal
144 // handlers being invoked.
145
146 // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
147 int signal_number;
148 int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
149 if (rc != 0) {
150 PLOG(FATAL) << "sigwait failed";
151 }
152
Elliott Hughesc2f80062011-11-07 11:57:51 -0800153 if (!ShouldHalt()) {
154 // Let the user know we got the signal, just in case the system's too screwed for us to
155 // actually do what they want us to do...
156 LOG(INFO) << *thread_ << ": reacting to signal " << signal_number;
157
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800158 // If anyone's holding locks (which might prevent us from getting back into state Runnable), say so...
159 Runtime::Current()->DumpLockHolders(LOG(INFO));
Elliott Hughesc2f80062011-11-07 11:57:51 -0800160 }
161
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700162 return signal_number;
163}
164
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700165void* SignalCatcher::Run(void* arg) {
166 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
167 CHECK(signal_catcher != NULL);
168
Elliott Hughes8d768a92011-09-14 16:35:25 -0700169 Runtime* runtime = Runtime::Current();
Elliott Hughes462c9442012-03-23 18:47:50 -0700170 runtime->AttachCurrentThread("Signal Catcher", true, Thread::GetSystemThreadGroup());
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700171 Thread::Current()->SetState(Thread::kRunnable);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700172
173 {
174 MutexLock mu(signal_catcher->lock_);
175 signal_catcher->thread_ = Thread::Current();
Elliott Hughes5f791332011-09-15 17:45:30 -0700176 signal_catcher->cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700177 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700178
Elliott Hughese27955c2011-08-26 15:21:24 -0700179 // Set up mask with signals we want to handle.
180 sigset_t mask;
181 sigemptyset(&mask);
182 sigaddset(&mask, SIGQUIT);
183 sigaddset(&mask, SIGUSR1);
184
185 while (true) {
Elliott Hughesc2f80062011-11-07 11:57:51 -0800186 int signal_number = signal_catcher->WaitForSignal(mask);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700187 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700188 runtime->DetachCurrentThread();
Elliott Hughese27955c2011-08-26 15:21:24 -0700189 return NULL;
190 }
191
Elliott Hughese27955c2011-08-26 15:21:24 -0700192 switch (signal_number) {
193 case SIGQUIT:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700194 signal_catcher->HandleSigQuit();
Elliott Hughese27955c2011-08-26 15:21:24 -0700195 break;
196 case SIGUSR1:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700197 signal_catcher->HandleSigUsr1();
Elliott Hughese27955c2011-08-26 15:21:24 -0700198 break;
199 default:
200 LOG(ERROR) << "Unexpected signal %d" << signal_number;
201 break;
202 }
203 }
204}
205
206} // namespace art