blob: 1552180173b1c79749f89410c4e5d991750d42c2 [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 Hughese27955c2011-08-26 15:21:24 -070032SignalCatcher::SignalCatcher() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070033 lock_ = Mutex::Create("SignalCatcher lock");
34 SetHaltFlag(false);
35
Elliott Hughese27955c2011-08-26 15:21:24 -070036 // Create a raw pthread; its start routine will attach to the runtime.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070037 errno = pthread_create(&thread_, NULL, &Run, this);
Elliott Hughese27955c2011-08-26 15:21:24 -070038 if (errno != 0) {
39 PLOG(FATAL) << "pthread_create failed for signal catcher thread";
40 }
41}
42
43SignalCatcher::~SignalCatcher() {
44 // Since we know the thread is just sitting around waiting for signals
45 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070046 SetHaltFlag(true);
Elliott Hughese27955c2011-08-26 15:21:24 -070047 pthread_kill(thread_, SIGQUIT);
48 pthread_join(thread_, NULL);
49}
50
Elliott Hughes5fe594f2011-09-08 12:33:17 -070051void SignalCatcher::SetHaltFlag(bool new_value) {
52 MutexLock mu(lock_);
53 halt_ = new_value;
54}
55
56bool SignalCatcher::ShouldHalt() {
57 MutexLock mu(lock_);
58 return halt_;
59}
60
Elliott Hughese27955c2011-08-26 15:21:24 -070061void SignalCatcher::HandleSigQuit() {
62 // TODO: suspend all threads
63
Elliott Hughesd92bec42011-09-02 17:04:36 -070064 std::stringstream os;
65 os << "\n"
66 << "\n"
67 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -070068
Elliott Hughesd92bec42011-09-02 17:04:36 -070069 std::string cmdline;
70 if (ReadFileToString("/proc/self/cmdline", &cmdline)) {
71 std::replace(cmdline.begin(), cmdline.end(), '\0', ' ');
72 os << "Cmd line: " << cmdline << "\n";
73 }
Elliott Hughese27955c2011-08-26 15:21:24 -070074
Elliott Hughesd92bec42011-09-02 17:04:36 -070075 Runtime* runtime = Runtime::Current();
76 runtime->DumpStatistics(os);
77 runtime->GetThreadList()->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -070078
Elliott Hughesd92bec42011-09-02 17:04:36 -070079 std::string maps;
80 if (ReadFileToString("/proc/self/maps", &maps)) {
81 os << "/proc/self/maps:\n" << maps;
82 }
83
84 os << "----- end " << getpid() << " -----";
Elliott Hughese27955c2011-08-26 15:21:24 -070085
86 // TODO: resume all threads
87
Elliott Hughesd92bec42011-09-02 17:04:36 -070088 LOG(INFO) << os.str();
Elliott Hughese27955c2011-08-26 15:21:24 -070089}
90
91void SignalCatcher::HandleSigUsr1() {
92 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
93 Heap::CollectGarbage();
94}
95
Elliott Hughesad7c2a32011-08-31 11:58:10 -070096int WaitForSignal(Thread* thread, sigset_t& mask) {
97 ScopedThreadStateChange tsc(thread, Thread::kWaiting); // TODO: VMWAIT
98
99 // Signals for sigwait() must be blocked but not ignored. We
100 // block signals like SIGQUIT for all threads, so the condition
101 // is met. When the signal hits, we wake up, without any signal
102 // handlers being invoked.
103
104 // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
105 int signal_number;
106 int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
107 if (rc != 0) {
108 PLOG(FATAL) << "sigwait failed";
109 }
110
111 return signal_number;
112}
113
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700114void* SignalCatcher::Run(void* arg) {
115 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
116 CHECK(signal_catcher != NULL);
117
118 Runtime::Current()->AttachCurrentThread("Signal Catcher", true);
Elliott Hughese27955c2011-08-26 15:21:24 -0700119 Thread* self = Thread::Current();
120 CHECK(self != NULL);
121
Elliott Hughese27955c2011-08-26 15:21:24 -0700122 // Set up mask with signals we want to handle.
123 sigset_t mask;
124 sigemptyset(&mask);
125 sigaddset(&mask, SIGQUIT);
126 sigaddset(&mask, SIGUSR1);
127
128 while (true) {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700129 int signal_number = WaitForSignal(self, mask);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700130 if (signal_catcher->ShouldHalt()) {
Elliott Hughesd92bec42011-09-02 17:04:36 -0700131 Runtime::Current()->DetachCurrentThread();
Elliott Hughese27955c2011-08-26 15:21:24 -0700132 return NULL;
133 }
134
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700135 LOG(INFO) << *self << ": reacting to signal " << signal_number;
Elliott Hughese27955c2011-08-26 15:21:24 -0700136 switch (signal_number) {
137 case SIGQUIT:
138 HandleSigQuit();
139 break;
140 case SIGUSR1:
141 HandleSigUsr1();
142 break;
143 default:
144 LOG(ERROR) << "Unexpected signal %d" << signal_number;
145 break;
146 }
147 }
148}
149
150} // namespace art