blob: f25a9ba1f0fde7efe6f0e6c803f638544e30e7ce [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 Hughese27955c2011-08-26 15:21:24 -070033#include "thread.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070034#include "thread_list.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070035#include "utils.h"
36
37namespace art {
38
Elliott Hughes94ce37a2011-10-18 15:07:48 -070039SignalCatcher::SignalCatcher(const std::string& stack_trace_file)
40 : stack_trace_file_(stack_trace_file),
41 lock_("SignalCatcher lock"),
42 cond_("SignalCatcher::cond_"),
43 thread_(NULL) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070044 SetHaltFlag(false);
45
Elliott Hughese27955c2011-08-26 15:21:24 -070046 // Create a raw pthread; its start routine will attach to the runtime.
Elliott Hughes8d768a92011-09-14 16:35:25 -070047 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Run, this), "signal catcher thread");
48
Elliott Hughes8d768a92011-09-14 16:35:25 -070049 MutexLock mu(lock_);
50 while (thread_ == NULL) {
Elliott Hughes5f791332011-09-15 17:45:30 -070051 cond_.Wait(lock_);
Elliott Hughese27955c2011-08-26 15:21:24 -070052 }
53}
54
55SignalCatcher::~SignalCatcher() {
56 // Since we know the thread is just sitting around waiting for signals
57 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070058 SetHaltFlag(true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070059 CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown");
60 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "signal catcher shutdown");
Elliott Hughese27955c2011-08-26 15:21:24 -070061}
62
Elliott Hughes5fe594f2011-09-08 12:33:17 -070063void SignalCatcher::SetHaltFlag(bool new_value) {
64 MutexLock mu(lock_);
65 halt_ = new_value;
66}
67
68bool SignalCatcher::ShouldHalt() {
69 MutexLock mu(lock_);
70 return halt_;
71}
72
Elliott Hughes94ce37a2011-10-18 15:07:48 -070073void SignalCatcher::Output(const std::string& s) {
74 if (stack_trace_file_.empty()) {
75 LOG(INFO) << s;
76 return;
77 }
78
79 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
80 int fd = open(stack_trace_file_.c_str(), O_APPEND | O_CREAT | O_WRONLY, 0666);
81 if (fd == -1) {
82 PLOG(ERROR) << "Unable to open stack trace file '" << stack_trace_file_ << "'";
83 return;
84 }
85 UniquePtr<File> file(OS::FileFromFd(stack_trace_file_.c_str(), fd));
86 if (!file->WriteFully(s.data(), s.size())) {
87 PLOG(ERROR) << "Failed to write stack traces to '" << stack_trace_file_ << "'";
88 } else {
89 LOG(INFO) << "Wrote stack traces to '" << stack_trace_file_ << "'";
90 }
91 close(fd);
92}
93
Elliott Hughese27955c2011-08-26 15:21:24 -070094void SignalCatcher::HandleSigQuit() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070095 Runtime* runtime = Runtime::Current();
96 ThreadList* thread_list = runtime->GetThreadList();
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070097 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughesaccd83d2011-10-17 14:25:58 -070098
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070099 LOG(INFO) << "Heap lock owner tid: " << Heap::GetLockOwner() << "\n"
100 << "ThreadList lock owner tid: " << thread_list->GetLockOwner() << "\n"
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700101 << "ClassLinker classes lock owner tid: " << class_linker->GetClassesLockOwner() << "\n"
102 << "ClassLinker dex lock owner tid: " << class_linker->GetDexLockOwner() << "\n";
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700103
104 thread_list->SuspendAll();
Elliott Hughese27955c2011-08-26 15:21:24 -0700105
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700106 std::ostringstream os;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700107 os << "\n"
Elliott Hughesd92bec42011-09-02 17:04:36 -0700108 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700109
Elliott Hughesd92bec42011-09-02 17:04:36 -0700110 std::string cmdline;
111 if (ReadFileToString("/proc/self/cmdline", &cmdline)) {
112 std::replace(cmdline.begin(), cmdline.end(), '\0', ' ');
113 os << "Cmd line: " << cmdline << "\n";
114 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700115
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700116 runtime->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700117
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700118 if (false) {
119 std::string maps;
120 if (ReadFileToString("/proc/self/maps", &maps)) {
121 os << "/proc/self/maps:\n" << maps;
122 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700123 }
124
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700125 os << "----- end " << getpid() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700126
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700127 thread_list->ResumeAll();
Elliott Hughese27955c2011-08-26 15:21:24 -0700128
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700129 Output(os.str());
Elliott Hughese27955c2011-08-26 15:21:24 -0700130}
131
132void SignalCatcher::HandleSigUsr1() {
133 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
134 Heap::CollectGarbage();
135}
136
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700137int WaitForSignal(Thread* thread, sigset_t& mask) {
Elliott Hughes93e74e82011-09-13 11:07:03 -0700138 ScopedThreadStateChange tsc(thread, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700139
140 // Signals for sigwait() must be blocked but not ignored. We
141 // block signals like SIGQUIT for all threads, so the condition
142 // is met. When the signal hits, we wake up, without any signal
143 // handlers being invoked.
144
145 // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
146 int signal_number;
147 int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
148 if (rc != 0) {
149 PLOG(FATAL) << "sigwait failed";
150 }
151
152 return signal_number;
153}
154
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700155void* SignalCatcher::Run(void* arg) {
156 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
157 CHECK(signal_catcher != NULL);
158
Elliott Hughes8d768a92011-09-14 16:35:25 -0700159 Runtime* runtime = Runtime::Current();
160 runtime->AttachCurrentThread("Signal Catcher", true);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700161 Thread::Current()->SetState(Thread::kRunnable);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700162
163 {
164 MutexLock mu(signal_catcher->lock_);
165 signal_catcher->thread_ = Thread::Current();
Elliott Hughes5f791332011-09-15 17:45:30 -0700166 signal_catcher->cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700167 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700168
Elliott Hughese27955c2011-08-26 15:21:24 -0700169 // Set up mask with signals we want to handle.
170 sigset_t mask;
171 sigemptyset(&mask);
172 sigaddset(&mask, SIGQUIT);
173 sigaddset(&mask, SIGUSR1);
174
175 while (true) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700176 int signal_number = WaitForSignal(signal_catcher->thread_, mask);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700177 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700178 runtime->DetachCurrentThread();
Elliott Hughese27955c2011-08-26 15:21:24 -0700179 return NULL;
180 }
181
Elliott Hughes8d768a92011-09-14 16:35:25 -0700182 LOG(INFO) << *signal_catcher->thread_ << ": reacting to signal " << signal_number;
Elliott Hughese27955c2011-08-26 15:21:24 -0700183 switch (signal_number) {
184 case SIGQUIT:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700185 signal_catcher->HandleSigQuit();
Elliott Hughese27955c2011-08-26 15:21:24 -0700186 break;
187 case SIGUSR1:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700188 signal_catcher->HandleSigUsr1();
Elliott Hughese27955c2011-08-26 15:21:24 -0700189 break;
190 default:
191 LOG(ERROR) << "Unexpected signal %d" << signal_number;
192 break;
193 }
194 }
195}
196
197} // namespace art