blob: 5c7a30f14e75eea50d78fd1b7ac29a40a06e8ed1 [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 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)) {
47 current_cmd_line.resize(current_cmd_line.size() - 1); // Lose the trailing '\0'.
48 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"),
65 cond_("SignalCatcher::cond_"),
66 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
Elliott Hughes8d768a92011-09-14 16:35:25 -070072 MutexLock mu(lock_);
73 while (thread_ == NULL) {
Elliott Hughes5f791332011-09-15 17:45:30 -070074 cond_.Wait(lock_);
Elliott Hughese27955c2011-08-26 15:21:24 -070075 }
76}
77
78SignalCatcher::~SignalCatcher() {
79 // Since we know the thread is just sitting around waiting for signals
80 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070081 SetHaltFlag(true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070082 CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown");
83 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "signal catcher shutdown");
Elliott Hughese27955c2011-08-26 15:21:24 -070084}
85
Elliott Hughes5fe594f2011-09-08 12:33:17 -070086void SignalCatcher::SetHaltFlag(bool new_value) {
87 MutexLock mu(lock_);
88 halt_ = new_value;
89}
90
91bool SignalCatcher::ShouldHalt() {
92 MutexLock mu(lock_);
93 return halt_;
94}
95
Elliott Hughes94ce37a2011-10-18 15:07:48 -070096void SignalCatcher::Output(const std::string& s) {
97 if (stack_trace_file_.empty()) {
98 LOG(INFO) << s;
99 return;
100 }
101
Elliott Hughes34e06962012-04-09 13:55:55 -0700102 ScopedThreadStateChange tsc(Thread::Current(), kVmWait);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700103 int fd = open(stack_trace_file_.c_str(), O_APPEND | O_CREAT | O_WRONLY, 0666);
104 if (fd == -1) {
105 PLOG(ERROR) << "Unable to open stack trace file '" << stack_trace_file_ << "'";
106 return;
107 }
108 UniquePtr<File> file(OS::FileFromFd(stack_trace_file_.c_str(), fd));
109 if (!file->WriteFully(s.data(), s.size())) {
110 PLOG(ERROR) << "Failed to write stack traces to '" << stack_trace_file_ << "'";
111 } else {
112 LOG(INFO) << "Wrote stack traces to '" << stack_trace_file_ << "'";
113 }
114 close(fd);
115}
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
Elliott Hughes831afe42011-12-15 17:27:34 -0800121 // We take the heap lock before suspending all threads so we don't end up in a situation where
122 // one of the suspended threads suspended via the implicit FullSuspendCheck on the slow path of
123 // Heap::Lock, which is the only case where a thread can be suspended while holding the heap lock.
124 // (We need the heap lock when we dump the thread list. We could probably fix this by duplicating
125 // more state from java.lang.Thread in struct Thread.)
126 ScopedHeapLock heap_lock;
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700127 thread_list->SuspendAll();
Elliott Hughese27955c2011-08-26 15:21:24 -0700128
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700129 std::ostringstream os;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700130 os << "\n"
Elliott Hughesd92bec42011-09-02 17:04:36 -0700131 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700132
Elliott Hughes0d39c122012-06-06 16:41:17 -0700133 DumpCmdLine(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700134
135 os << "Build type: " << (kIsDebugBuild ? "debug" : "optimized") << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700136
Elliott Hughesc967f782012-04-16 10:23:15 -0700137 runtime->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700138
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700139 if (false) {
140 std::string maps;
141 if (ReadFileToString("/proc/self/maps", &maps)) {
142 os << "/proc/self/maps:\n" << maps;
143 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700144 }
145
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700146 os << "----- end " << getpid() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700147
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700148 thread_list->ResumeAll();
Elliott Hughese27955c2011-08-26 15:21:24 -0700149
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700150 Output(os.str());
Elliott Hughese27955c2011-08-26 15:21:24 -0700151}
152
153void SignalCatcher::HandleSigUsr1() {
154 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800155 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughese27955c2011-08-26 15:21:24 -0700156}
157
Elliott Hughes457005c2012-04-16 13:54:25 -0700158int SignalCatcher::WaitForSignal(SignalSet& signals) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700159 ScopedThreadStateChange tsc(thread_, kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700160
161 // Signals for sigwait() must be blocked but not ignored. We
162 // block signals like SIGQUIT for all threads, so the condition
163 // is met. When the signal hits, we wake up, without any signal
164 // handlers being invoked.
Elliott Hughes457005c2012-04-16 13:54:25 -0700165 int signal_number = signals.Wait();
Elliott Hughesc2f80062011-11-07 11:57:51 -0800166 if (!ShouldHalt()) {
167 // Let the user know we got the signal, just in case the system's too screwed for us to
168 // actually do what they want us to do...
169 LOG(INFO) << *thread_ << ": reacting to signal " << signal_number;
170
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800171 // If anyone's holding locks (which might prevent us from getting back into state Runnable), say so...
172 Runtime::Current()->DumpLockHolders(LOG(INFO));
Elliott Hughesc2f80062011-11-07 11:57:51 -0800173 }
174
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700175 return signal_number;
176}
177
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700178void* SignalCatcher::Run(void* arg) {
179 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
180 CHECK(signal_catcher != NULL);
181
Elliott Hughes8d768a92011-09-14 16:35:25 -0700182 Runtime* runtime = Runtime::Current();
Elliott Hughes462c9442012-03-23 18:47:50 -0700183 runtime->AttachCurrentThread("Signal Catcher", true, Thread::GetSystemThreadGroup());
Elliott Hughes34e06962012-04-09 13:55:55 -0700184 Thread::Current()->SetState(kRunnable);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700185
186 {
187 MutexLock mu(signal_catcher->lock_);
188 signal_catcher->thread_ = Thread::Current();
Elliott Hughes5f791332011-09-15 17:45:30 -0700189 signal_catcher->cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700190 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700191
Elliott Hughese27955c2011-08-26 15:21:24 -0700192 // Set up mask with signals we want to handle.
Elliott Hughes457005c2012-04-16 13:54:25 -0700193 SignalSet signals;
194 signals.Add(SIGQUIT);
195 signals.Add(SIGUSR1);
Elliott Hughese27955c2011-08-26 15:21:24 -0700196
197 while (true) {
Elliott Hughes457005c2012-04-16 13:54:25 -0700198 int signal_number = signal_catcher->WaitForSignal(signals);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700199 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700200 runtime->DetachCurrentThread();
Elliott Hughese27955c2011-08-26 15:21:24 -0700201 return NULL;
202 }
203
Elliott Hughese27955c2011-08-26 15:21:24 -0700204 switch (signal_number) {
205 case SIGQUIT:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700206 signal_catcher->HandleSigQuit();
Elliott Hughese27955c2011-08-26 15:21:24 -0700207 break;
208 case SIGUSR1:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700209 signal_catcher->HandleSigUsr1();
Elliott Hughese27955c2011-08-26 15:21:24 -0700210 break;
211 default:
212 LOG(ERROR) << "Unexpected signal %d" << signal_number;
213 break;
214 }
215 }
216}
217
218} // namespace art