1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "signal_catcher.h"
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "class_linker.h"
#include "file.h"
#include "heap.h"
#include "os.h"
#include "runtime.h"
#include "thread.h"
#include "thread_list.h"
#include "utils.h"
namespace art {
SignalCatcher::SignalCatcher(const std::string& stack_trace_file)
: stack_trace_file_(stack_trace_file),
lock_("SignalCatcher lock"),
cond_("SignalCatcher::cond_"),
thread_(NULL) {
SetHaltFlag(false);
// Create a raw pthread; its start routine will attach to the runtime.
CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Run, this), "signal catcher thread");
MutexLock mu(lock_);
while (thread_ == NULL) {
cond_.Wait(lock_);
}
}
SignalCatcher::~SignalCatcher() {
// Since we know the thread is just sitting around waiting for signals
// to arrive, send it one.
SetHaltFlag(true);
CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown");
CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "signal catcher shutdown");
}
void SignalCatcher::SetHaltFlag(bool new_value) {
MutexLock mu(lock_);
halt_ = new_value;
}
bool SignalCatcher::ShouldHalt() {
MutexLock mu(lock_);
return halt_;
}
void SignalCatcher::Output(const std::string& s) {
if (stack_trace_file_.empty()) {
LOG(INFO) << s;
return;
}
ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
int fd = open(stack_trace_file_.c_str(), O_APPEND | O_CREAT | O_WRONLY, 0666);
if (fd == -1) {
PLOG(ERROR) << "Unable to open stack trace file '" << stack_trace_file_ << "'";
return;
}
UniquePtr<File> file(OS::FileFromFd(stack_trace_file_.c_str(), fd));
if (!file->WriteFully(s.data(), s.size())) {
PLOG(ERROR) << "Failed to write stack traces to '" << stack_trace_file_ << "'";
} else {
LOG(INFO) << "Wrote stack traces to '" << stack_trace_file_ << "'";
}
close(fd);
}
void SignalCatcher::HandleSigQuit() {
Runtime* runtime = Runtime::Current();
ThreadList* thread_list = runtime->GetThreadList();
ClassLinker* class_linker = runtime->GetClassLinker();
LOG(INFO) << "Heap lock owner tid: " << Heap::GetLockOwner() << "\n"
<< "ThreadList lock owner tid: " << thread_list->GetLockOwner() << "\n"
<< "ClassLinker classes lock owner tid: " << class_linker->GetClassesLockOwner() << "\n"
<< "ClassLinker dex lock owner tid: " << class_linker->GetDexLockOwner() << "\n";
thread_list->SuspendAll();
std::ostringstream os;
os << "\n"
<< "\n"
<< "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
std::string cmdline;
if (ReadFileToString("/proc/self/cmdline", &cmdline)) {
std::replace(cmdline.begin(), cmdline.end(), '\0', ' ');
os << "Cmd line: " << cmdline << "\n";
}
runtime->Dump(os);
if (false) {
std::string maps;
if (ReadFileToString("/proc/self/maps", &maps)) {
os << "/proc/self/maps:\n" << maps;
}
}
os << "----- end " << getpid() << " -----\n";
thread_list->ResumeAll();
Output(os.str());
}
void SignalCatcher::HandleSigUsr1() {
LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
Heap::CollectGarbage();
}
int WaitForSignal(Thread* thread, sigset_t& mask) {
ScopedThreadStateChange tsc(thread, Thread::kVmWait);
// Signals for sigwait() must be blocked but not ignored. We
// block signals like SIGQUIT for all threads, so the condition
// is met. When the signal hits, we wake up, without any signal
// handlers being invoked.
// Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
int signal_number;
int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
if (rc != 0) {
PLOG(FATAL) << "sigwait failed";
}
return signal_number;
}
void* SignalCatcher::Run(void* arg) {
SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
CHECK(signal_catcher != NULL);
Runtime* runtime = Runtime::Current();
runtime->AttachCurrentThread("Signal Catcher", true);
Thread::Current()->SetState(Thread::kRunnable);
{
MutexLock mu(signal_catcher->lock_);
signal_catcher->thread_ = Thread::Current();
signal_catcher->cond_.Broadcast();
}
// Set up mask with signals we want to handle.
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGQUIT);
sigaddset(&mask, SIGUSR1);
while (true) {
int signal_number = WaitForSignal(signal_catcher->thread_, mask);
if (signal_catcher->ShouldHalt()) {
runtime->DetachCurrentThread();
return NULL;
}
LOG(INFO) << *signal_catcher->thread_ << ": reacting to signal " << signal_number;
switch (signal_number) {
case SIGQUIT:
signal_catcher->HandleSigQuit();
break;
case SIGUSR1:
signal_catcher->HandleSigUsr1();
break;
default:
LOG(ERROR) << "Unexpected signal %d" << signal_number;
break;
}
}
}
} // namespace art
|