blob: 08db89a664e72e0a4b2f66ade2e2f8870c352481 [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
19#include <pthread.h>
20#include <signal.h>
21#include <stdlib.h>
22#include <sys/time.h>
23#include <unistd.h>
24
25#include "heap.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "runtime.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070027#include "thread.h"
28#include "utils.h"
29
30namespace art {
31
Elliott Hughes8daa0922011-09-11 13:46:25 -070032SignalCatcher::SignalCatcher() : lock_("SignalCatcher lock") {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070033 SetHaltFlag(false);
34
Elliott Hughese27955c2011-08-26 15:21:24 -070035 // Create a raw pthread; its start routine will attach to the runtime.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070036 errno = pthread_create(&thread_, NULL, &Run, this);
Elliott Hughese27955c2011-08-26 15:21:24 -070037 if (errno != 0) {
38 PLOG(FATAL) << "pthread_create failed for signal catcher thread";
39 }
40}
41
42SignalCatcher::~SignalCatcher() {
43 // Since we know the thread is just sitting around waiting for signals
44 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070045 SetHaltFlag(true);
Elliott Hughese27955c2011-08-26 15:21:24 -070046 pthread_kill(thread_, SIGQUIT);
47 pthread_join(thread_, NULL);
48}
49
Elliott Hughes5fe594f2011-09-08 12:33:17 -070050void SignalCatcher::SetHaltFlag(bool new_value) {
51 MutexLock mu(lock_);
52 halt_ = new_value;
53}
54
55bool SignalCatcher::ShouldHalt() {
56 MutexLock mu(lock_);
57 return halt_;
58}
59
Elliott Hughese27955c2011-08-26 15:21:24 -070060void SignalCatcher::HandleSigQuit() {
61 // TODO: suspend all threads
62
Elliott Hughesd92bec42011-09-02 17:04:36 -070063 std::stringstream os;
64 os << "\n"
65 << "\n"
66 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -070067
Elliott Hughesd92bec42011-09-02 17:04:36 -070068 std::string cmdline;
69 if (ReadFileToString("/proc/self/cmdline", &cmdline)) {
70 std::replace(cmdline.begin(), cmdline.end(), '\0', ' ');
71 os << "Cmd line: " << cmdline << "\n";
72 }
Elliott Hughese27955c2011-08-26 15:21:24 -070073
Elliott Hughes8daa0922011-09-11 13:46:25 -070074 Runtime::Current()->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -070075
Elliott Hughesd92bec42011-09-02 17:04:36 -070076 std::string maps;
77 if (ReadFileToString("/proc/self/maps", &maps)) {
78 os << "/proc/self/maps:\n" << maps;
79 }
80
81 os << "----- end " << getpid() << " -----";
Elliott Hughese27955c2011-08-26 15:21:24 -070082
83 // TODO: resume all threads
84
Elliott Hughesd92bec42011-09-02 17:04:36 -070085 LOG(INFO) << os.str();
Elliott Hughese27955c2011-08-26 15:21:24 -070086}
87
88void SignalCatcher::HandleSigUsr1() {
89 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
90 Heap::CollectGarbage();
91}
92
Elliott Hughesad7c2a32011-08-31 11:58:10 -070093int WaitForSignal(Thread* thread, sigset_t& mask) {
Elliott Hughes93e74e82011-09-13 11:07:03 -070094 ScopedThreadStateChange tsc(thread, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -070095
96 // Signals for sigwait() must be blocked but not ignored. We
97 // block signals like SIGQUIT for all threads, so the condition
98 // is met. When the signal hits, we wake up, without any signal
99 // handlers being invoked.
100
101 // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
102 int signal_number;
103 int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
104 if (rc != 0) {
105 PLOG(FATAL) << "sigwait failed";
106 }
107
108 return signal_number;
109}
110
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700111void* SignalCatcher::Run(void* arg) {
112 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
113 CHECK(signal_catcher != NULL);
114
115 Runtime::Current()->AttachCurrentThread("Signal Catcher", true);
Elliott Hughese27955c2011-08-26 15:21:24 -0700116 Thread* self = Thread::Current();
117 CHECK(self != NULL);
118
Elliott Hughese27955c2011-08-26 15:21:24 -0700119 // Set up mask with signals we want to handle.
120 sigset_t mask;
121 sigemptyset(&mask);
122 sigaddset(&mask, SIGQUIT);
123 sigaddset(&mask, SIGUSR1);
124
125 while (true) {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700126 int signal_number = WaitForSignal(self, mask);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700127 if (signal_catcher->ShouldHalt()) {
Elliott Hughesd92bec42011-09-02 17:04:36 -0700128 Runtime::Current()->DetachCurrentThread();
Elliott Hughese27955c2011-08-26 15:21:24 -0700129 return NULL;
130 }
131
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700132 LOG(INFO) << *self << ": reacting to signal " << signal_number;
Elliott Hughese27955c2011-08-26 15:21:24 -0700133 switch (signal_number) {
134 case SIGQUIT:
135 HandleSigQuit();
136 break;
137 case SIGUSR1:
138 HandleSigUsr1();
139 break;
140 default:
141 LOG(ERROR) << "Unexpected signal %d" << signal_number;
142 break;
143 }
144 }
145}
146
147} // namespace art