Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 26 | #include "runtime.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 27 | #include "thread.h" |
| 28 | #include "utils.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame^] | 32 | SignalCatcher::SignalCatcher() : lock_("SignalCatcher lock") { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 33 | SetHaltFlag(false); |
| 34 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 35 | // Create a raw pthread; its start routine will attach to the runtime. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 36 | errno = pthread_create(&thread_, NULL, &Run, this); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 37 | if (errno != 0) { |
| 38 | PLOG(FATAL) << "pthread_create failed for signal catcher thread"; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | SignalCatcher::~SignalCatcher() { |
| 43 | // Since we know the thread is just sitting around waiting for signals |
| 44 | // to arrive, send it one. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 45 | SetHaltFlag(true); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 46 | pthread_kill(thread_, SIGQUIT); |
| 47 | pthread_join(thread_, NULL); |
| 48 | } |
| 49 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 50 | void SignalCatcher::SetHaltFlag(bool new_value) { |
| 51 | MutexLock mu(lock_); |
| 52 | halt_ = new_value; |
| 53 | } |
| 54 | |
| 55 | bool SignalCatcher::ShouldHalt() { |
| 56 | MutexLock mu(lock_); |
| 57 | return halt_; |
| 58 | } |
| 59 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 60 | void SignalCatcher::HandleSigQuit() { |
| 61 | // TODO: suspend all threads |
| 62 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 63 | std::stringstream os; |
| 64 | os << "\n" |
| 65 | << "\n" |
| 66 | << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 68 | 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 Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 73 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame^] | 74 | Runtime::Current()->Dump(os); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 75 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 76 | std::string maps; |
| 77 | if (ReadFileToString("/proc/self/maps", &maps)) { |
| 78 | os << "/proc/self/maps:\n" << maps; |
| 79 | } |
| 80 | |
| 81 | os << "----- end " << getpid() << " -----"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 82 | |
| 83 | // TODO: resume all threads |
| 84 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 85 | LOG(INFO) << os.str(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void SignalCatcher::HandleSigUsr1() { |
| 89 | LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)"; |
| 90 | Heap::CollectGarbage(); |
| 91 | } |
| 92 | |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 93 | int WaitForSignal(Thread* thread, sigset_t& mask) { |
| 94 | ScopedThreadStateChange tsc(thread, Thread::kWaiting); // TODO: VMWAIT |
| 95 | |
| 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 Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 111 | void* 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 Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 116 | Thread* self = Thread::Current(); |
| 117 | CHECK(self != NULL); |
| 118 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 119 | // 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 Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 126 | int signal_number = WaitForSignal(self, mask); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 127 | if (signal_catcher->ShouldHalt()) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 128 | Runtime::Current()->DetachCurrentThread(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 129 | return NULL; |
| 130 | } |
| 131 | |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 132 | LOG(INFO) << *self << ": reacting to signal " << signal_number; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 133 | 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 |