blob: b50879f0a9c82767c5a46ba6f25220d74ce6bd43 [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
Josh Gao691839c2017-10-03 13:23:23 -070030#include <android-base/stringprintf.h>
31
32#if defined(ART_TARGET_ANDROID)
33#include <tombstoned/tombstoned.h>
34#endif
35
Ian Rogersd582fa42014-11-05 23:46:43 -080036#include "arch/instruction_set.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010037#include "base/time_utils.h"
Elliott Hughes76160052012-12-12 16:31:20 -080038#include "base/unix_file/fd_file.h"
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070039#include "class_linker.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070040#include "gc/heap.h"
Mathieu Chartier07ea07e2017-04-05 17:23:54 -070041#include "jit/profile_saver.h"
Elliott Hughes94ce37a2011-10-18 15:07:48 -070042#include "os.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070043#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070044#include "scoped_thread_state_change-inl.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070045#include "signal_set.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070046#include "thread.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070047#include "thread_list.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070048#include "utils.h"
49
50namespace art {
51
Elliott Hughes0d39c122012-06-06 16:41:17 -070052static void DumpCmdLine(std::ostream& os) {
53#if defined(__linux__)
54 // Show the original command line, and the current command line too if it's changed.
55 // On Android, /proc/self/cmdline will have been rewritten to something like "system_server".
Jeff Brownb4fffc72014-09-10 18:41:18 -070056 // Note: The string "Cmd line:" is chosen to match the format used by debuggerd.
Elliott Hughes0d39c122012-06-06 16:41:17 -070057 std::string current_cmd_line;
58 if (ReadFileToString("/proc/self/cmdline", &current_cmd_line)) {
Jeff Brownb4fffc72014-09-10 18:41:18 -070059 current_cmd_line.resize(current_cmd_line.find_last_not_of('\0') + 1); // trim trailing '\0's
Elliott Hughes0d39c122012-06-06 16:41:17 -070060 std::replace(current_cmd_line.begin(), current_cmd_line.end(), '\0', ' ');
61
Jeff Brownb4fffc72014-09-10 18:41:18 -070062 os << "Cmd line: " << current_cmd_line << "\n";
Elliott Hughes0d39c122012-06-06 16:41:17 -070063 const char* stashed_cmd_line = GetCmdLine();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070064 if (stashed_cmd_line != nullptr && current_cmd_line != stashed_cmd_line
Jeff Brownb4fffc72014-09-10 18:41:18 -070065 && strcmp(stashed_cmd_line, "<unset>") != 0) {
66 os << "Original command line: " << stashed_cmd_line << "\n";
Elliott Hughes0d39c122012-06-06 16:41:17 -070067 }
Elliott Hughesae80b492012-04-24 10:43:17 -070068 }
Elliott Hughes0d39c122012-06-06 16:41:17 -070069#else
Jeff Brownb4fffc72014-09-10 18:41:18 -070070 os << "Cmd line: " << GetCmdLine() << "\n";
Elliott Hughesabbe07d2012-06-05 17:42:23 -070071#endif
Elliott Hughes0d39c122012-06-06 16:41:17 -070072}
Elliott Hughesae80b492012-04-24 10:43:17 -070073
Narayan Kamatheb710332017-05-10 11:48:46 +010074SignalCatcher::SignalCatcher(const std::string& stack_trace_file,
75 bool use_tombstoned_stack_trace_fd)
76 : stack_trace_file_(stack_trace_file),
77 use_tombstoned_stack_trace_fd_(use_tombstoned_stack_trace_fd),
Elliott Hughes94ce37a2011-10-18 15:07:48 -070078 lock_("SignalCatcher lock"),
Ian Rogersc604d732012-10-14 16:09:54 -070079 cond_("SignalCatcher::cond_", lock_),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070080 thread_(nullptr) {
Narayan Kamatheb710332017-05-10 11:48:46 +010081#if !defined(ART_TARGET_ANDROID)
82 // We're not running on Android, so we can't communicate with tombstoned
83 // to ask for an open file.
84 CHECK(!use_tombstoned_stack_trace_fd_);
85#endif
86
Elliott Hughes5fe594f2011-09-08 12:33:17 -070087 SetHaltFlag(false);
88
Elliott Hughese27955c2011-08-26 15:21:24 -070089 // Create a raw pthread; its start routine will attach to the runtime.
Mathieu Chartier2cebb242015-04-21 16:50:40 -070090 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, nullptr, &Run, this), "signal catcher thread");
Elliott Hughes8d768a92011-09-14 16:35:25 -070091
Ian Rogers81d425b2012-09-27 16:03:43 -070092 Thread* self = Thread::Current();
93 MutexLock mu(self, lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070094 while (thread_ == nullptr) {
Ian Rogersc604d732012-10-14 16:09:54 -070095 cond_.Wait(self);
Elliott Hughese27955c2011-08-26 15:21:24 -070096 }
97}
98
99SignalCatcher::~SignalCatcher() {
100 // Since we know the thread is just sitting around waiting for signals
101 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700102 SetHaltFlag(true);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700103 CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700104 CHECK_PTHREAD_CALL(pthread_join, (pthread_, nullptr), "signal catcher shutdown");
Elliott Hughese27955c2011-08-26 15:21:24 -0700105}
106
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700107void SignalCatcher::SetHaltFlag(bool new_value) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700108 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700109 halt_ = new_value;
110}
111
112bool SignalCatcher::ShouldHalt() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700113 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700114 return halt_;
115}
116
Narayan Kamatheb710332017-05-10 11:48:46 +0100117bool SignalCatcher::OpenStackTraceFile(android::base::unique_fd* tombstone_fd,
118 android::base::unique_fd* output_fd) {
119 if (use_tombstoned_stack_trace_fd_) {
120#if defined(ART_TARGET_ANDROID)
Narayan Kamathe0f02b72017-05-30 16:22:28 +0100121 return tombstoned_connect(getpid(), tombstone_fd, output_fd, kDebuggerdJavaBacktrace);
Narayan Kamatheb710332017-05-10 11:48:46 +0100122#else
123 UNUSED(tombstone_fd);
124 UNUSED(output_fd);
125#endif
Narayan Kamath84695ae2017-04-07 15:41:41 +0100126 }
127
Narayan Kamatheb710332017-05-10 11:48:46 +0100128 // The runtime is not configured to dump traces to a file, will LOG(INFO)
129 // instead.
130 if (stack_trace_file_.empty()) {
131 return false;
132 }
133
134 int fd = open(stack_trace_file_.c_str(), O_APPEND | O_CREAT | O_WRONLY, 0666);
135 if (fd == -1) {
136 PLOG(ERROR) << "Unable to open stack trace file '" << stack_trace_file_ << "'";
137 return false;
138 }
139
140 output_fd->reset(fd);
141 return true;
Narayan Kamath84695ae2017-04-07 15:41:41 +0100142}
143
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700144void SignalCatcher::Output(const std::string& s) {
Narayan Kamatheb710332017-05-10 11:48:46 +0100145 android::base::unique_fd tombstone_fd;
146 android::base::unique_fd output_fd;
147 if (!OpenStackTraceFile(&tombstone_fd, &output_fd)) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700148 LOG(INFO) << s;
149 return;
150 }
151
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700152 ScopedThreadStateChange tsc(Thread::Current(), kWaitingForSignalCatcherOutput);
Narayan Kamatheb710332017-05-10 11:48:46 +0100153
154 std::unique_ptr<File> file(new File(output_fd.release(), true /* check_usage */));
Andreas Gampe4303ba92014-11-06 01:00:46 -0800155 bool success = file->WriteFully(s.data(), s.size());
156 if (success) {
157 success = file->FlushCloseOrErase() == 0;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700158 } else {
Andreas Gampe4303ba92014-11-06 01:00:46 -0800159 file->Erase();
160 }
Narayan Kamatheb710332017-05-10 11:48:46 +0100161
162 const std::string output_path_msg = (use_tombstoned_stack_trace_fd_) ?
163 "[tombstoned]" : stack_trace_file_;
164
Andreas Gampe4303ba92014-11-06 01:00:46 -0800165 if (success) {
Narayan Kamatheb710332017-05-10 11:48:46 +0100166 LOG(INFO) << "Wrote stack traces to '" << output_path_msg << "'";
Andreas Gampe4303ba92014-11-06 01:00:46 -0800167 } else {
Narayan Kamatheb710332017-05-10 11:48:46 +0100168 PLOG(ERROR) << "Failed to write stack traces to '" << output_path_msg << "'";
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700169 }
Narayan Kamatheb710332017-05-10 11:48:46 +0100170
171#if defined(ART_TARGET_ANDROID)
Narayan Kamath2cfd6122017-07-11 17:40:56 +0100172 if (use_tombstoned_stack_trace_fd_ && !tombstoned_notify_completion(tombstone_fd)) {
Narayan Kamatheb710332017-05-10 11:48:46 +0100173 LOG(WARNING) << "Unable to notify tombstoned of dump completion.";
174 }
175#endif
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700176}
177
Elliott Hughese27955c2011-08-26 15:21:24 -0700178void SignalCatcher::HandleSigQuit() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700179 Runtime* runtime = Runtime::Current();
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700180 std::ostringstream os;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700181 os << "\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700182 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700183
Elliott Hughes0d39c122012-06-06 16:41:17 -0700184 DumpCmdLine(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700185
Andreas Gampedd671252015-07-23 14:37:18 -0700186 // Note: The strings "Build fingerprint:" and "ABI:" are chosen to match the format used by
187 // debuggerd. This allows, for example, the stack tool to work.
188 std::string fingerprint = runtime->GetFingerprint();
189 os << "Build fingerprint: '" << (fingerprint.empty() ? "unknown" : fingerprint) << "'\n";
190 os << "ABI: '" << GetInstructionSetString(runtime->GetInstructionSet()) << "'\n";
Jeff Brownb4fffc72014-09-10 18:41:18 -0700191
Elliott Hughesae80b492012-04-24 10:43:17 -0700192 os << "Build type: " << (kIsDebugBuild ? "debug" : "optimized") << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700193
Elliott Hughesc967f782012-04-16 10:23:15 -0700194 runtime->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700195
Ian Rogerscf7f1912014-10-22 22:06:39 -0700196 if ((false)) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700197 std::string maps;
198 if (ReadFileToString("/proc/self/maps", &maps)) {
199 os << "/proc/self/maps:\n" << maps;
200 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700201 }
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700202 os << "----- end " << getpid() << " -----\n";
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700203 Output(os.str());
Elliott Hughese27955c2011-08-26 15:21:24 -0700204}
205
206void SignalCatcher::HandleSigUsr1() {
Mathieu Chartier07ea07e2017-04-05 17:23:54 -0700207 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF) and profile save";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800208 Runtime::Current()->GetHeap()->CollectGarbage(false);
Mathieu Chartier07ea07e2017-04-05 17:23:54 -0700209 ProfileSaver::ForceProcessProfiles();
Elliott Hughese27955c2011-08-26 15:21:24 -0700210}
211
Elliott Hughesf8349362012-06-18 15:00:06 -0700212int SignalCatcher::WaitForSignal(Thread* self, SignalSet& signals) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700213 ScopedThreadStateChange tsc(self, kWaitingInMainSignalCatcherLoop);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700214
215 // Signals for sigwait() must be blocked but not ignored. We
216 // block signals like SIGQUIT for all threads, so the condition
217 // is met. When the signal hits, we wake up, without any signal
218 // handlers being invoked.
Elliott Hughes457005c2012-04-16 13:54:25 -0700219 int signal_number = signals.Wait();
Elliott Hughesc2f80062011-11-07 11:57:51 -0800220 if (!ShouldHalt()) {
221 // Let the user know we got the signal, just in case the system's too screwed for us to
222 // actually do what they want us to do...
Elliott Hughesf8349362012-06-18 15:00:06 -0700223 LOG(INFO) << *self << ": reacting to signal " << signal_number;
Elliott Hughesc2f80062011-11-07 11:57:51 -0800224
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800225 // If anyone's holding locks (which might prevent us from getting back into state Runnable), say so...
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700226 Runtime::Current()->DumpLockHolders(LOG_STREAM(INFO));
Elliott Hughesc2f80062011-11-07 11:57:51 -0800227 }
228
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700229 return signal_number;
230}
231
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700232void* SignalCatcher::Run(void* arg) {
233 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700234 CHECK(signal_catcher != nullptr);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700235
Elliott Hughes8d768a92011-09-14 16:35:25 -0700236 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800237 CHECK(runtime->AttachCurrentThread("Signal Catcher", true, runtime->GetSystemThreadGroup(),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800238 !runtime->IsAotCompiler()));
Elliott Hughesf8349362012-06-18 15:00:06 -0700239
240 Thread* self = Thread::Current();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700241 DCHECK_NE(self->GetState(), kRunnable);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700242 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700243 MutexLock mu(self, signal_catcher->lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700244 signal_catcher->thread_ = self;
Ian Rogersc604d732012-10-14 16:09:54 -0700245 signal_catcher->cond_.Broadcast(self);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700246 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700247
Elliott Hughese27955c2011-08-26 15:21:24 -0700248 // Set up mask with signals we want to handle.
Elliott Hughes457005c2012-04-16 13:54:25 -0700249 SignalSet signals;
250 signals.Add(SIGQUIT);
251 signals.Add(SIGUSR1);
Elliott Hughese27955c2011-08-26 15:21:24 -0700252
253 while (true) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700254 int signal_number = signal_catcher->WaitForSignal(self, signals);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700255 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700256 runtime->DetachCurrentThread();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700257 return nullptr;
Elliott Hughese27955c2011-08-26 15:21:24 -0700258 }
259
Elliott Hughese27955c2011-08-26 15:21:24 -0700260 switch (signal_number) {
261 case SIGQUIT:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700262 signal_catcher->HandleSigQuit();
Elliott Hughese27955c2011-08-26 15:21:24 -0700263 break;
264 case SIGUSR1:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700265 signal_catcher->HandleSigUsr1();
Elliott Hughese27955c2011-08-26 15:21:24 -0700266 break;
267 default:
268 LOG(ERROR) << "Unexpected signal %d" << signal_number;
269 break;
270 }
271 }
272}
273
274} // namespace art