blob: 5979a0faecf4e5761f8520120cf68e6cf4987483 [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
32bool SignalCatcher::halt_ = false;
33
34SignalCatcher::SignalCatcher() {
35 // Create a raw pthread; its start routine will attach to the runtime.
36 errno = pthread_create(&thread_, NULL, &Run, NULL);
37 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.
45 halt_ = true;
46 pthread_kill(thread_, SIGQUIT);
47 pthread_join(thread_, NULL);
48}
49
50void SignalCatcher::HandleSigQuit() {
51 // TODO: suspend all threads
52
53 std::stringstream buffer;
54 buffer << "\n"
55 << "\n"
56 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n"
57 << "Cmd line: " << ReadFileToString("/proc/self/cmdline") << "\n";
58
59 Runtime::Current()->DumpStatistics(buffer);
60
61 // TODO: dump all threads.
62 // dvmDumpAllThreadsEx(&target, true);
63
64 buffer << "/proc/self/maps:\n" << ReadFileToString("/proc/self/maps");
65 buffer << "----- end " << getpid() << " -----";
66
67 // TODO: resume all threads
68
69 LOG(INFO) << buffer.str();
70}
71
72void SignalCatcher::HandleSigUsr1() {
73 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
74 Heap::CollectGarbage();
75}
76
Elliott Hughesad7c2a32011-08-31 11:58:10 -070077int WaitForSignal(Thread* thread, sigset_t& mask) {
78 ScopedThreadStateChange tsc(thread, Thread::kWaiting); // TODO: VMWAIT
79
80 // Signals for sigwait() must be blocked but not ignored. We
81 // block signals like SIGQUIT for all threads, so the condition
82 // is met. When the signal hits, we wake up, without any signal
83 // handlers being invoked.
84
85 // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
86 int signal_number;
87 int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
88 if (rc != 0) {
89 PLOG(FATAL) << "sigwait failed";
90 }
91
92 return signal_number;
93}
94
Elliott Hughese27955c2011-08-26 15:21:24 -070095void* SignalCatcher::Run(void*) {
96 CHECK(Runtime::Current()->AttachCurrentThread("Signal Catcher", NULL, true));
97 Thread* self = Thread::Current();
98 CHECK(self != NULL);
99
100 LOG(INFO) << "Signal catcher thread started " << *self;
101
102 // Set up mask with signals we want to handle.
103 sigset_t mask;
104 sigemptyset(&mask);
105 sigaddset(&mask, SIGQUIT);
106 sigaddset(&mask, SIGUSR1);
107
108 while (true) {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700109 int signal_number = WaitForSignal(self, mask);
Elliott Hughese27955c2011-08-26 15:21:24 -0700110 if (halt_) {
111 return NULL;
112 }
113
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700114 LOG(INFO) << *self << ": reacting to signal " << signal_number;
Elliott Hughese27955c2011-08-26 15:21:24 -0700115 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