blob: 0703b6105d5e19659782e6e69769fd93c326170b [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 Hughesae80b492012-04-24 10:43:17 -070041static bool ReadCmdLine(std::string& result) {
42 if (!ReadFileToString("/proc/self/cmdline", &result)) {
43 return false;
44 }
45 std::replace(result.begin(), result.end(), '\0', ' ');
46 return true;
47}
48
Elliott Hughes94ce37a2011-10-18 15:07:48 -070049SignalCatcher::SignalCatcher(const std::string& stack_trace_file)
50 : stack_trace_file_(stack_trace_file),
51 lock_("SignalCatcher lock"),
52 cond_("SignalCatcher::cond_"),
53 thread_(NULL) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070054 SetHaltFlag(false);
55
Elliott Hughesae80b492012-04-24 10:43:17 -070056 // Stash the original command line for SIGQUIT reporting.
57 // By then, /proc/self/cmdline will have been rewritten to something like "system_server".
58 CHECK(ReadCmdLine(cmd_line_));
59
Elliott Hughese27955c2011-08-26 15:21:24 -070060 // Create a raw pthread; its start routine will attach to the runtime.
Elliott Hughes8d768a92011-09-14 16:35:25 -070061 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Run, this), "signal catcher thread");
62
Elliott Hughes8d768a92011-09-14 16:35:25 -070063 MutexLock mu(lock_);
64 while (thread_ == NULL) {
Elliott Hughes5f791332011-09-15 17:45:30 -070065 cond_.Wait(lock_);
Elliott Hughese27955c2011-08-26 15:21:24 -070066 }
67}
68
69SignalCatcher::~SignalCatcher() {
70 // Since we know the thread is just sitting around waiting for signals
71 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070072 SetHaltFlag(true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070073 CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown");
74 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "signal catcher shutdown");
Elliott Hughese27955c2011-08-26 15:21:24 -070075}
76
Elliott Hughes5fe594f2011-09-08 12:33:17 -070077void SignalCatcher::SetHaltFlag(bool new_value) {
78 MutexLock mu(lock_);
79 halt_ = new_value;
80}
81
82bool SignalCatcher::ShouldHalt() {
83 MutexLock mu(lock_);
84 return halt_;
85}
86
Elliott Hughes94ce37a2011-10-18 15:07:48 -070087void SignalCatcher::Output(const std::string& s) {
88 if (stack_trace_file_.empty()) {
89 LOG(INFO) << s;
90 return;
91 }
92
Elliott Hughes34e06962012-04-09 13:55:55 -070093 ScopedThreadStateChange tsc(Thread::Current(), kVmWait);
Elliott Hughes94ce37a2011-10-18 15:07:48 -070094 int fd = open(stack_trace_file_.c_str(), O_APPEND | O_CREAT | O_WRONLY, 0666);
95 if (fd == -1) {
96 PLOG(ERROR) << "Unable to open stack trace file '" << stack_trace_file_ << "'";
97 return;
98 }
99 UniquePtr<File> file(OS::FileFromFd(stack_trace_file_.c_str(), fd));
100 if (!file->WriteFully(s.data(), s.size())) {
101 PLOG(ERROR) << "Failed to write stack traces to '" << stack_trace_file_ << "'";
102 } else {
103 LOG(INFO) << "Wrote stack traces to '" << stack_trace_file_ << "'";
104 }
105 close(fd);
106}
107
Elliott Hughese27955c2011-08-26 15:21:24 -0700108void SignalCatcher::HandleSigQuit() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700109 Runtime* runtime = Runtime::Current();
110 ThreadList* thread_list = runtime->GetThreadList();
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700111
Elliott Hughes831afe42011-12-15 17:27:34 -0800112 // We take the heap lock before suspending all threads so we don't end up in a situation where
113 // one of the suspended threads suspended via the implicit FullSuspendCheck on the slow path of
114 // Heap::Lock, which is the only case where a thread can be suspended while holding the heap lock.
115 // (We need the heap lock when we dump the thread list. We could probably fix this by duplicating
116 // more state from java.lang.Thread in struct Thread.)
117 ScopedHeapLock heap_lock;
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700118 thread_list->SuspendAll();
Elliott Hughese27955c2011-08-26 15:21:24 -0700119
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700120 std::ostringstream os;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700121 os << "\n"
Elliott Hughesd92bec42011-09-02 17:04:36 -0700122 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700123
Elliott Hughesae80b492012-04-24 10:43:17 -0700124 std::string current_cmd_line;
125 if (ReadCmdLine(current_cmd_line) && current_cmd_line != cmd_line_) {
126 os << "Cmdline: " << current_cmd_line;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700127 }
Elliott Hughesae80b492012-04-24 10:43:17 -0700128 os << "\n";
129
130 if (current_cmd_line != cmd_line_) {
131 os << "Original command line: " << cmd_line_ << "\n";
132 }
133
134 os << "Build type: " << (kIsDebugBuild ? "debug" : "optimized") << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700135
Elliott Hughesc967f782012-04-16 10:23:15 -0700136 runtime->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700137
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700138 if (false) {
139 std::string maps;
140 if (ReadFileToString("/proc/self/maps", &maps)) {
141 os << "/proc/self/maps:\n" << maps;
142 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700143 }
144
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700145 os << "----- end " << getpid() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700146
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700147 thread_list->ResumeAll();
Elliott Hughese27955c2011-08-26 15:21:24 -0700148
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700149 Output(os.str());
Elliott Hughese27955c2011-08-26 15:21:24 -0700150}
151
152void SignalCatcher::HandleSigUsr1() {
153 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800154 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughese27955c2011-08-26 15:21:24 -0700155}
156
Elliott Hughes457005c2012-04-16 13:54:25 -0700157int SignalCatcher::WaitForSignal(SignalSet& signals) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700158 ScopedThreadStateChange tsc(thread_, kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700159
160 // Signals for sigwait() must be blocked but not ignored. We
161 // block signals like SIGQUIT for all threads, so the condition
162 // is met. When the signal hits, we wake up, without any signal
163 // handlers being invoked.
Elliott Hughes457005c2012-04-16 13:54:25 -0700164 int signal_number = signals.Wait();
Elliott Hughesc2f80062011-11-07 11:57:51 -0800165 if (!ShouldHalt()) {
166 // Let the user know we got the signal, just in case the system's too screwed for us to
167 // actually do what they want us to do...
168 LOG(INFO) << *thread_ << ": reacting to signal " << signal_number;
169
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800170 // If anyone's holding locks (which might prevent us from getting back into state Runnable), say so...
171 Runtime::Current()->DumpLockHolders(LOG(INFO));
Elliott Hughesc2f80062011-11-07 11:57:51 -0800172 }
173
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700174 return signal_number;
175}
176
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700177void* SignalCatcher::Run(void* arg) {
178 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
179 CHECK(signal_catcher != NULL);
180
Elliott Hughes8d768a92011-09-14 16:35:25 -0700181 Runtime* runtime = Runtime::Current();
Elliott Hughes462c9442012-03-23 18:47:50 -0700182 runtime->AttachCurrentThread("Signal Catcher", true, Thread::GetSystemThreadGroup());
Elliott Hughes34e06962012-04-09 13:55:55 -0700183 Thread::Current()->SetState(kRunnable);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700184
185 {
186 MutexLock mu(signal_catcher->lock_);
187 signal_catcher->thread_ = Thread::Current();
Elliott Hughes5f791332011-09-15 17:45:30 -0700188 signal_catcher->cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700189 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700190
Elliott Hughese27955c2011-08-26 15:21:24 -0700191 // Set up mask with signals we want to handle.
Elliott Hughes457005c2012-04-16 13:54:25 -0700192 SignalSet signals;
193 signals.Add(SIGQUIT);
194 signals.Add(SIGUSR1);
Elliott Hughese27955c2011-08-26 15:21:24 -0700195
196 while (true) {
Elliott Hughes457005c2012-04-16 13:54:25 -0700197 int signal_number = signal_catcher->WaitForSignal(signals);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700198 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700199 runtime->DetachCurrentThread();
Elliott Hughese27955c2011-08-26 15:21:24 -0700200 return NULL;
201 }
202
Elliott Hughese27955c2011-08-26 15:21:24 -0700203 switch (signal_number) {
204 case SIGQUIT:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700205 signal_catcher->HandleSigQuit();
Elliott Hughese27955c2011-08-26 15:21:24 -0700206 break;
207 case SIGUSR1:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700208 signal_catcher->HandleSigUsr1();
Elliott Hughese27955c2011-08-26 15:21:24 -0700209 break;
210 default:
211 LOG(ERROR) << "Unexpected signal %d" << signal_number;
212 break;
213 }
214 }
215}
216
217} // namespace art