blob: 382643314c3534f4435f179e783fcc29163875ab [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
Elliott Hughes94ce37a2011-10-18 15:07:48 -070019#include <fcntl.h>
Elliott Hughese27955c2011-08-26 15:21:24 -070020#include <pthread.h>
21#include <signal.h>
22#include <stdlib.h>
Elliott Hughes94ce37a2011-10-18 15:07:48 -070023#include <sys/stat.h>
Elliott Hughese27955c2011-08-26 15:21:24 -070024#include <sys/time.h>
Elliott Hughes94ce37a2011-10-18 15:07:48 -070025#include <sys/types.h>
Elliott Hughese27955c2011-08-26 15:21:24 -070026#include <unistd.h>
27
Ian Rogersc7dd2952014-10-21 23:31:19 -070028#include <sstream>
29
Narayan Kamath84695ae2017-04-07 15:41:41 +010030#include "android-base/stringprintf.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080031#include "arch/instruction_set.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010032#include "base/time_utils.h"
Elliott Hughes76160052012-12-12 16:31:20 -080033#include "base/unix_file/fd_file.h"
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070034#include "class_linker.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "gc/heap.h"
Mathieu Chartier07ea07e2017-04-05 17:23:54 -070036#include "jit/profile_saver.h"
Elliott Hughes94ce37a2011-10-18 15:07:48 -070037#include "os.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070038#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070039#include "scoped_thread_state_change-inl.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070040#include "signal_set.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070041#include "thread.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070042#include "thread_list.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070043#include "utils.h"
44
45namespace art {
46
Elliott Hughes0d39c122012-06-06 16:41:17 -070047static void DumpCmdLine(std::ostream& os) {
48#if defined(__linux__)
49 // Show the original command line, and the current command line too if it's changed.
50 // On Android, /proc/self/cmdline will have been rewritten to something like "system_server".
Jeff Brownb4fffc72014-09-10 18:41:18 -070051 // Note: The string "Cmd line:" is chosen to match the format used by debuggerd.
Elliott Hughes0d39c122012-06-06 16:41:17 -070052 std::string current_cmd_line;
53 if (ReadFileToString("/proc/self/cmdline", &current_cmd_line)) {
Jeff Brownb4fffc72014-09-10 18:41:18 -070054 current_cmd_line.resize(current_cmd_line.find_last_not_of('\0') + 1); // trim trailing '\0's
Elliott Hughes0d39c122012-06-06 16:41:17 -070055 std::replace(current_cmd_line.begin(), current_cmd_line.end(), '\0', ' ');
56
Jeff Brownb4fffc72014-09-10 18:41:18 -070057 os << "Cmd line: " << current_cmd_line << "\n";
Elliott Hughes0d39c122012-06-06 16:41:17 -070058 const char* stashed_cmd_line = GetCmdLine();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070059 if (stashed_cmd_line != nullptr && current_cmd_line != stashed_cmd_line
Jeff Brownb4fffc72014-09-10 18:41:18 -070060 && strcmp(stashed_cmd_line, "<unset>") != 0) {
61 os << "Original command line: " << stashed_cmd_line << "\n";
Elliott Hughes0d39c122012-06-06 16:41:17 -070062 }
Elliott Hughesae80b492012-04-24 10:43:17 -070063 }
Elliott Hughes0d39c122012-06-06 16:41:17 -070064#else
Jeff Brownb4fffc72014-09-10 18:41:18 -070065 os << "Cmd line: " << GetCmdLine() << "\n";
Elliott Hughesabbe07d2012-06-05 17:42:23 -070066#endif
Elliott Hughes0d39c122012-06-06 16:41:17 -070067}
Elliott Hughesae80b492012-04-24 10:43:17 -070068
Narayan Kamath84695ae2017-04-07 15:41:41 +010069SignalCatcher::SignalCatcher(const std::string& stack_trace_dir,
70 const std::string& stack_trace_file)
71 : stack_trace_dir_(stack_trace_dir),
72 stack_trace_file_(stack_trace_file),
Elliott Hughes94ce37a2011-10-18 15:07:48 -070073 lock_("SignalCatcher lock"),
Ian Rogersc604d732012-10-14 16:09:54 -070074 cond_("SignalCatcher::cond_", lock_),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070075 thread_(nullptr) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070076 SetHaltFlag(false);
77
Elliott Hughese27955c2011-08-26 15:21:24 -070078 // Create a raw pthread; its start routine will attach to the runtime.
Mathieu Chartier2cebb242015-04-21 16:50:40 -070079 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, nullptr, &Run, this), "signal catcher thread");
Elliott Hughes8d768a92011-09-14 16:35:25 -070080
Ian Rogers81d425b2012-09-27 16:03:43 -070081 Thread* self = Thread::Current();
82 MutexLock mu(self, lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070083 while (thread_ == nullptr) {
Ian Rogersc604d732012-10-14 16:09:54 -070084 cond_.Wait(self);
Elliott Hughese27955c2011-08-26 15:21:24 -070085 }
86}
87
88SignalCatcher::~SignalCatcher() {
89 // Since we know the thread is just sitting around waiting for signals
90 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070091 SetHaltFlag(true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070092 CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown");
Mathieu Chartier2cebb242015-04-21 16:50:40 -070093 CHECK_PTHREAD_CALL(pthread_join, (pthread_, nullptr), "signal catcher shutdown");
Elliott Hughese27955c2011-08-26 15:21:24 -070094}
95
Elliott Hughes5fe594f2011-09-08 12:33:17 -070096void SignalCatcher::SetHaltFlag(bool new_value) {
Ian Rogers50b35e22012-10-04 10:09:15 -070097 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -070098 halt_ = new_value;
99}
100
101bool SignalCatcher::ShouldHalt() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700102 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700103 return halt_;
104}
105
Narayan Kamath84695ae2017-04-07 15:41:41 +0100106std::string SignalCatcher::GetStackTraceFileName() {
107 if (!stack_trace_dir_.empty()) {
108 // We'll try a maximum of ten times (arbitrarily selected) to create a file
109 // with a unique name, seeding the pseudo random generator each time.
110 //
111 // If this doesn't work, give up and log to stdout. Note that we could try
112 // indefinitely, but that would make problems in this code harder to detect
113 // since we'd be spinning in the signal catcher thread.
114 static constexpr uint32_t kMaxRetries = 10;
115
116 for (uint32_t i = 0; i < kMaxRetries; ++i) {
117 std::srand(NanoTime());
118 // Sample output for PID 1234 : /data/anr-pid1234-cafeffee.txt
119 const std::string file_name = android::base::StringPrintf(
120 "%s/anr-pid%" PRId32 "-%08" PRIx32 ".txt",
121 stack_trace_dir_.c_str(),
122 static_cast<int32_t>(getpid()),
123 static_cast<uint32_t>(std::rand()));
124
125 if (!OS::FileExists(file_name.c_str())) {
126 return file_name;
127 }
128 }
129
130 LOG(ERROR) << "Unable to obtain stack trace filename at path : " << stack_trace_dir_;
131 return "";
132 }
133
134 return stack_trace_file_;
135}
136
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700137void SignalCatcher::Output(const std::string& s) {
Narayan Kamath84695ae2017-04-07 15:41:41 +0100138 const std::string stack_trace_file = GetStackTraceFileName();
139 if (stack_trace_file.empty()) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700140 LOG(INFO) << s;
141 return;
142 }
143
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 ScopedThreadStateChange tsc(Thread::Current(), kWaitingForSignalCatcherOutput);
Narayan Kamath84695ae2017-04-07 15:41:41 +0100145 int fd = open(stack_trace_file.c_str(), O_APPEND | O_CREAT | O_WRONLY, 0666);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700146 if (fd == -1) {
147 PLOG(ERROR) << "Unable to open stack trace file '" << stack_trace_file_ << "'";
148 return;
149 }
Narayan Kamath84695ae2017-04-07 15:41:41 +0100150 std::unique_ptr<File> file(new File(fd, stack_trace_file, true));
Andreas Gampe4303ba92014-11-06 01:00:46 -0800151 bool success = file->WriteFully(s.data(), s.size());
152 if (success) {
153 success = file->FlushCloseOrErase() == 0;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700154 } else {
Andreas Gampe4303ba92014-11-06 01:00:46 -0800155 file->Erase();
156 }
157 if (success) {
Narayan Kamath84695ae2017-04-07 15:41:41 +0100158 LOG(INFO) << "Wrote stack traces to '" << stack_trace_file << "'";
Andreas Gampe4303ba92014-11-06 01:00:46 -0800159 } else {
Narayan Kamath84695ae2017-04-07 15:41:41 +0100160 PLOG(ERROR) << "Failed to write stack traces to '" << stack_trace_file << "'";
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700161 }
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700162}
163
Elliott Hughese27955c2011-08-26 15:21:24 -0700164void SignalCatcher::HandleSigQuit() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700165 Runtime* runtime = Runtime::Current();
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700166 std::ostringstream os;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700167 os << "\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700169
Elliott Hughes0d39c122012-06-06 16:41:17 -0700170 DumpCmdLine(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700171
Andreas Gampedd671252015-07-23 14:37:18 -0700172 // Note: The strings "Build fingerprint:" and "ABI:" are chosen to match the format used by
173 // debuggerd. This allows, for example, the stack tool to work.
174 std::string fingerprint = runtime->GetFingerprint();
175 os << "Build fingerprint: '" << (fingerprint.empty() ? "unknown" : fingerprint) << "'\n";
176 os << "ABI: '" << GetInstructionSetString(runtime->GetInstructionSet()) << "'\n";
Jeff Brownb4fffc72014-09-10 18:41:18 -0700177
Elliott Hughesae80b492012-04-24 10:43:17 -0700178 os << "Build type: " << (kIsDebugBuild ? "debug" : "optimized") << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700179
Elliott Hughesc967f782012-04-16 10:23:15 -0700180 runtime->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700181
Ian Rogerscf7f1912014-10-22 22:06:39 -0700182 if ((false)) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700183 std::string maps;
184 if (ReadFileToString("/proc/self/maps", &maps)) {
185 os << "/proc/self/maps:\n" << maps;
186 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700187 }
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700188 os << "----- end " << getpid() << " -----\n";
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700189 Output(os.str());
Elliott Hughese27955c2011-08-26 15:21:24 -0700190}
191
192void SignalCatcher::HandleSigUsr1() {
Mathieu Chartier07ea07e2017-04-05 17:23:54 -0700193 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF) and profile save";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800194 Runtime::Current()->GetHeap()->CollectGarbage(false);
Mathieu Chartier07ea07e2017-04-05 17:23:54 -0700195 ProfileSaver::ForceProcessProfiles();
Elliott Hughese27955c2011-08-26 15:21:24 -0700196}
197
Elliott Hughesf8349362012-06-18 15:00:06 -0700198int SignalCatcher::WaitForSignal(Thread* self, SignalSet& signals) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199 ScopedThreadStateChange tsc(self, kWaitingInMainSignalCatcherLoop);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700200
201 // Signals for sigwait() must be blocked but not ignored. We
202 // block signals like SIGQUIT for all threads, so the condition
203 // is met. When the signal hits, we wake up, without any signal
204 // handlers being invoked.
Elliott Hughes457005c2012-04-16 13:54:25 -0700205 int signal_number = signals.Wait();
Elliott Hughesc2f80062011-11-07 11:57:51 -0800206 if (!ShouldHalt()) {
207 // Let the user know we got the signal, just in case the system's too screwed for us to
208 // actually do what they want us to do...
Elliott Hughesf8349362012-06-18 15:00:06 -0700209 LOG(INFO) << *self << ": reacting to signal " << signal_number;
Elliott Hughesc2f80062011-11-07 11:57:51 -0800210
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800211 // If anyone's holding locks (which might prevent us from getting back into state Runnable), say so...
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700212 Runtime::Current()->DumpLockHolders(LOG_STREAM(INFO));
Elliott Hughesc2f80062011-11-07 11:57:51 -0800213 }
214
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700215 return signal_number;
216}
217
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700218void* SignalCatcher::Run(void* arg) {
219 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700220 CHECK(signal_catcher != nullptr);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700221
Elliott Hughes8d768a92011-09-14 16:35:25 -0700222 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800223 CHECK(runtime->AttachCurrentThread("Signal Catcher", true, runtime->GetSystemThreadGroup(),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800224 !runtime->IsAotCompiler()));
Elliott Hughesf8349362012-06-18 15:00:06 -0700225
226 Thread* self = Thread::Current();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700227 DCHECK_NE(self->GetState(), kRunnable);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700228 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700229 MutexLock mu(self, signal_catcher->lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700230 signal_catcher->thread_ = self;
Ian Rogersc604d732012-10-14 16:09:54 -0700231 signal_catcher->cond_.Broadcast(self);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700232 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700233
Elliott Hughese27955c2011-08-26 15:21:24 -0700234 // Set up mask with signals we want to handle.
Elliott Hughes457005c2012-04-16 13:54:25 -0700235 SignalSet signals;
236 signals.Add(SIGQUIT);
237 signals.Add(SIGUSR1);
Elliott Hughese27955c2011-08-26 15:21:24 -0700238
239 while (true) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700240 int signal_number = signal_catcher->WaitForSignal(self, signals);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700241 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700242 runtime->DetachCurrentThread();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700243 return nullptr;
Elliott Hughese27955c2011-08-26 15:21:24 -0700244 }
245
Elliott Hughese27955c2011-08-26 15:21:24 -0700246 switch (signal_number) {
247 case SIGQUIT:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700248 signal_catcher->HandleSigQuit();
Elliott Hughese27955c2011-08-26 15:21:24 -0700249 break;
250 case SIGUSR1:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700251 signal_catcher->HandleSigUsr1();
Elliott Hughese27955c2011-08-26 15:21:24 -0700252 break;
253 default:
254 LOG(ERROR) << "Unexpected signal %d" << signal_number;
255 break;
256 }
257 }
258}
259
260} // namespace art