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" |
| 26 | #include "thread.h" |
| 27 | #include "utils.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | bool SignalCatcher::halt_ = false; |
| 32 | |
| 33 | SignalCatcher::SignalCatcher() { |
| 34 | // Create a raw pthread; its start routine will attach to the runtime. |
| 35 | errno = pthread_create(&thread_, NULL, &Run, NULL); |
| 36 | if (errno != 0) { |
| 37 | PLOG(FATAL) << "pthread_create failed for signal catcher thread"; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | SignalCatcher::~SignalCatcher() { |
| 42 | // Since we know the thread is just sitting around waiting for signals |
| 43 | // to arrive, send it one. |
| 44 | halt_ = true; |
| 45 | pthread_kill(thread_, SIGQUIT); |
| 46 | pthread_join(thread_, NULL); |
| 47 | } |
| 48 | |
| 49 | void SignalCatcher::HandleSigQuit() { |
| 50 | // TODO: suspend all threads |
| 51 | |
| 52 | std::stringstream buffer; |
| 53 | buffer << "\n" |
| 54 | << "\n" |
| 55 | << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n" |
| 56 | << "Cmd line: " << ReadFileToString("/proc/self/cmdline") << "\n"; |
| 57 | |
| 58 | Runtime::Current()->DumpStatistics(buffer); |
| 59 | |
| 60 | // TODO: dump all threads. |
| 61 | // dvmDumpAllThreadsEx(&target, true); |
| 62 | |
| 63 | buffer << "/proc/self/maps:\n" << ReadFileToString("/proc/self/maps"); |
| 64 | buffer << "----- end " << getpid() << " -----"; |
| 65 | |
| 66 | // TODO: resume all threads |
| 67 | |
| 68 | LOG(INFO) << buffer.str(); |
| 69 | } |
| 70 | |
| 71 | void SignalCatcher::HandleSigUsr1() { |
| 72 | LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)"; |
| 73 | Heap::CollectGarbage(); |
| 74 | } |
| 75 | |
| 76 | void* SignalCatcher::Run(void*) { |
| 77 | CHECK(Runtime::Current()->AttachCurrentThread("Signal Catcher", NULL, true)); |
| 78 | Thread* self = Thread::Current(); |
| 79 | CHECK(self != NULL); |
| 80 | |
| 81 | LOG(INFO) << "Signal catcher thread started " << *self; |
| 82 | |
| 83 | // Set up mask with signals we want to handle. |
| 84 | sigset_t mask; |
| 85 | sigemptyset(&mask); |
| 86 | sigaddset(&mask, SIGQUIT); |
| 87 | sigaddset(&mask, SIGUSR1); |
| 88 | |
| 89 | while (true) { |
| 90 | self->SetState(Thread::kWaiting); // TODO: VMWAIT |
| 91 | |
| 92 | // Signals for sigwait() must be blocked but not ignored. We |
| 93 | // block signals like SIGQUIT for all threads, so the condition |
| 94 | // is met. When the signal hits, we wake up, without any signal |
| 95 | // handlers being invoked. |
| 96 | |
| 97 | // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures. |
| 98 | int signal_number; |
| 99 | int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number)); |
| 100 | if (rc != 0) { |
| 101 | PLOG(FATAL) << "sigwait failed"; |
| 102 | } |
| 103 | |
| 104 | if (!halt_) { |
| 105 | LOG(INFO) << *self << ": reacting to signal " << signal_number; |
| 106 | } |
| 107 | |
| 108 | // Set our status to runnable, self-suspending if GC in progress. |
| 109 | self->SetState(Thread::kRunnable); |
| 110 | |
| 111 | if (halt_) { |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | switch (signal_number) { |
| 116 | case SIGQUIT: |
| 117 | HandleSigQuit(); |
| 118 | break; |
| 119 | case SIGUSR1: |
| 120 | HandleSigUsr1(); |
| 121 | break; |
| 122 | default: |
| 123 | LOG(ERROR) << "Unexpected signal %d" << signal_number; |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | } // namespace art |