blob: b83e71d1524ad804a03af6a30523f771d9e08f7b [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
77void* SignalCatcher::Run(void*) {
78 CHECK(Runtime::Current()->AttachCurrentThread("Signal Catcher", NULL, true));
79 Thread* self = Thread::Current();
80 CHECK(self != NULL);
81
82 LOG(INFO) << "Signal catcher thread started " << *self;
83
84 // Set up mask with signals we want to handle.
85 sigset_t mask;
86 sigemptyset(&mask);
87 sigaddset(&mask, SIGQUIT);
88 sigaddset(&mask, SIGUSR1);
89
90 while (true) {
91 self->SetState(Thread::kWaiting); // TODO: VMWAIT
92
93 // Signals for sigwait() must be blocked but not ignored. We
94 // block signals like SIGQUIT for all threads, so the condition
95 // is met. When the signal hits, we wake up, without any signal
96 // handlers being invoked.
97
98 // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
99 int signal_number;
100 int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
101 if (rc != 0) {
102 PLOG(FATAL) << "sigwait failed";
103 }
104
105 if (!halt_) {
106 LOG(INFO) << *self << ": reacting to signal " << signal_number;
107 }
108
109 // Set our status to runnable, self-suspending if GC in progress.
110 self->SetState(Thread::kRunnable);
111
112 if (halt_) {
113 return NULL;
114 }
115
116 switch (signal_number) {
117 case SIGQUIT:
118 HandleSigQuit();
119 break;
120 case SIGUSR1:
121 HandleSigUsr1();
122 break;
123 default:
124 LOG(ERROR) << "Unexpected signal %d" << signal_number;
125 break;
126 }
127 }
128}
129
130} // namespace art