blob: 20c85c130c8a472411c64f1eab42d4b578519a5f [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
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +010019#include <csignal>
20#include <cstdlib>
Elliott Hughes94ce37a2011-10-18 15:07:48 -070021#include <fcntl.h>
Elliott Hughese27955c2011-08-26 15:21:24 -070022#include <pthread.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
Ian Rogersd582fa42014-11-05 23:46:43 -080032#include "arch/instruction_set.h"
Andreas Gampe39b378c2017-12-07 15:44:13 -080033#include "base/logging.h" // For GetCmdLine.
David Sehrc431b9d2018-03-02 12:01:51 -080034#include "base/os.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010035#include "base/time_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080036#include "base/utils.h"
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070037#include "class_linker.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070038#include "gc/heap.h"
Mathieu Chartier07ea07e2017-04-05 17:23:54 -070039#include "jit/profile_saver.h"
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +010040#include "palette/palette.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070041#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070042#include "scoped_thread_state_change-inl.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070043#include "signal_set.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070044#include "thread.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070045#include "thread_list.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070046
47namespace art {
48
Elliott Hughes0d39c122012-06-06 16:41:17 -070049static void DumpCmdLine(std::ostream& os) {
50#if defined(__linux__)
51 // Show the original command line, and the current command line too if it's changed.
52 // On Android, /proc/self/cmdline will have been rewritten to something like "system_server".
Jeff Brownb4fffc72014-09-10 18:41:18 -070053 // Note: The string "Cmd line:" is chosen to match the format used by debuggerd.
Elliott Hughes0d39c122012-06-06 16:41:17 -070054 std::string current_cmd_line;
55 if (ReadFileToString("/proc/self/cmdline", &current_cmd_line)) {
Jeff Brownb4fffc72014-09-10 18:41:18 -070056 current_cmd_line.resize(current_cmd_line.find_last_not_of('\0') + 1); // trim trailing '\0's
Elliott Hughes0d39c122012-06-06 16:41:17 -070057 std::replace(current_cmd_line.begin(), current_cmd_line.end(), '\0', ' ');
58
Jeff Brownb4fffc72014-09-10 18:41:18 -070059 os << "Cmd line: " << current_cmd_line << "\n";
Elliott Hughes0d39c122012-06-06 16:41:17 -070060 const char* stashed_cmd_line = GetCmdLine();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070061 if (stashed_cmd_line != nullptr && current_cmd_line != stashed_cmd_line
Jeff Brownb4fffc72014-09-10 18:41:18 -070062 && strcmp(stashed_cmd_line, "<unset>") != 0) {
63 os << "Original command line: " << stashed_cmd_line << "\n";
Elliott Hughes0d39c122012-06-06 16:41:17 -070064 }
Elliott Hughesae80b492012-04-24 10:43:17 -070065 }
Elliott Hughes0d39c122012-06-06 16:41:17 -070066#else
Jeff Brownb4fffc72014-09-10 18:41:18 -070067 os << "Cmd line: " << GetCmdLine() << "\n";
Elliott Hughesabbe07d2012-06-05 17:42:23 -070068#endif
Elliott Hughes0d39c122012-06-06 16:41:17 -070069}
Elliott Hughesae80b492012-04-24 10:43:17 -070070
Andreas Gampe53af0402018-05-03 10:40:37 -070071SignalCatcher::SignalCatcher()
72 : lock_("SignalCatcher lock"),
Ian Rogersc604d732012-10-14 16:09:54 -070073 cond_("SignalCatcher::cond_", lock_),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070074 thread_(nullptr) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070075 SetHaltFlag(false);
76
Elliott Hughese27955c2011-08-26 15:21:24 -070077 // Create a raw pthread; its start routine will attach to the runtime.
Mathieu Chartier2cebb242015-04-21 16:50:40 -070078 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, nullptr, &Run, this), "signal catcher thread");
Elliott Hughes8d768a92011-09-14 16:35:25 -070079
Ian Rogers81d425b2012-09-27 16:03:43 -070080 Thread* self = Thread::Current();
81 MutexLock mu(self, lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070082 while (thread_ == nullptr) {
Ian Rogersc604d732012-10-14 16:09:54 -070083 cond_.Wait(self);
Elliott Hughese27955c2011-08-26 15:21:24 -070084 }
85}
86
87SignalCatcher::~SignalCatcher() {
88 // Since we know the thread is just sitting around waiting for signals
89 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070090 SetHaltFlag(true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070091 CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown");
Mathieu Chartier2cebb242015-04-21 16:50:40 -070092 CHECK_PTHREAD_CALL(pthread_join, (pthread_, nullptr), "signal catcher shutdown");
Elliott Hughese27955c2011-08-26 15:21:24 -070093}
94
Elliott Hughes5fe594f2011-09-08 12:33:17 -070095void SignalCatcher::SetHaltFlag(bool new_value) {
Ian Rogers50b35e22012-10-04 10:09:15 -070096 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -070097 halt_ = new_value;
98}
99
100bool SignalCatcher::ShouldHalt() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700101 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700102 return halt_;
103}
104
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700105void SignalCatcher::Output(const std::string& s) {
Andreas Gampe53af0402018-05-03 10:40:37 -0700106#if defined(ART_TARGET_ANDROID)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 ScopedThreadStateChange tsc(Thread::Current(), kWaitingForSignalCatcherOutput);
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +0100108 PaletteStatus status = PaletteTombstonedMessage(s.data(), s.size());
109 if (status == PaletteStatus::kOkay) {
Andreas Gampe53af0402018-05-03 10:40:37 -0700110 LOG(INFO) << "Wrote stack traces to tombstoned";
Andreas Gampe4303ba92014-11-06 01:00:46 -0800111 } else {
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +0100112 CHECK(status == PaletteStatus::kCheckErrno);
Joe Onorato0e5dc342019-04-24 13:28:50 -0700113 PLOG(ERROR) << "Failed to write stack traces to tombstoned";
Narayan Kamatheb710332017-05-10 11:48:46 +0100114 }
Andreas Gampe53af0402018-05-03 10:40:37 -0700115#else
116 LOG(INFO) << s;
Narayan Kamatheb710332017-05-10 11:48:46 +0100117#endif
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700118}
119
Elliott Hughese27955c2011-08-26 15:21:24 -0700120void SignalCatcher::HandleSigQuit() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700121 Runtime* runtime = Runtime::Current();
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700122 std::ostringstream os;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700123 os << "\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700125
Elliott Hughes0d39c122012-06-06 16:41:17 -0700126 DumpCmdLine(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700127
Andreas Gampedd671252015-07-23 14:37:18 -0700128 // Note: The strings "Build fingerprint:" and "ABI:" are chosen to match the format used by
129 // debuggerd. This allows, for example, the stack tool to work.
130 std::string fingerprint = runtime->GetFingerprint();
131 os << "Build fingerprint: '" << (fingerprint.empty() ? "unknown" : fingerprint) << "'\n";
132 os << "ABI: '" << GetInstructionSetString(runtime->GetInstructionSet()) << "'\n";
Jeff Brownb4fffc72014-09-10 18:41:18 -0700133
Elliott Hughesae80b492012-04-24 10:43:17 -0700134 os << "Build type: " << (kIsDebugBuild ? "debug" : "optimized") << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700135
Elliott Hughesc967f782012-04-16 10:23:15 -0700136 runtime->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700137
Ian Rogerscf7f1912014-10-22 22:06:39 -0700138 if ((false)) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700139 std::string maps;
140 if (ReadFileToString("/proc/self/maps", &maps)) {
141 os << "/proc/self/maps:\n" << maps;
142 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700143 }
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700144 os << "----- end " << getpid() << " -----\n";
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700145 Output(os.str());
Elliott Hughese27955c2011-08-26 15:21:24 -0700146}
147
148void SignalCatcher::HandleSigUsr1() {
Mathieu Chartier07ea07e2017-04-05 17:23:54 -0700149 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF) and profile save";
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700150 Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references= */ false);
Mathieu Chartier07ea07e2017-04-05 17:23:54 -0700151 ProfileSaver::ForceProcessProfiles();
Elliott Hughese27955c2011-08-26 15:21:24 -0700152}
153
Elliott Hughesf8349362012-06-18 15:00:06 -0700154int SignalCatcher::WaitForSignal(Thread* self, SignalSet& signals) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700155 ScopedThreadStateChange tsc(self, kWaitingInMainSignalCatcherLoop);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700156
157 // Signals for sigwait() must be blocked but not ignored. We
158 // block signals like SIGQUIT for all threads, so the condition
159 // is met. When the signal hits, we wake up, without any signal
160 // handlers being invoked.
Elliott Hughes457005c2012-04-16 13:54:25 -0700161 int signal_number = signals.Wait();
Elliott Hughesc2f80062011-11-07 11:57:51 -0800162 if (!ShouldHalt()) {
163 // Let the user know we got the signal, just in case the system's too screwed for us to
164 // actually do what they want us to do...
Elliott Hughesf8349362012-06-18 15:00:06 -0700165 LOG(INFO) << *self << ": reacting to signal " << signal_number;
Elliott Hughesc2f80062011-11-07 11:57:51 -0800166
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800167 // If anyone's holding locks (which might prevent us from getting back into state Runnable), say so...
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700168 Runtime::Current()->DumpLockHolders(LOG_STREAM(INFO));
Elliott Hughesc2f80062011-11-07 11:57:51 -0800169 }
170
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700171 return signal_number;
172}
173
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700174void* SignalCatcher::Run(void* arg) {
175 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700176 CHECK(signal_catcher != nullptr);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700177
Elliott Hughes8d768a92011-09-14 16:35:25 -0700178 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800179 CHECK(runtime->AttachCurrentThread("Signal Catcher", true, runtime->GetSystemThreadGroup(),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800180 !runtime->IsAotCompiler()));
Elliott Hughesf8349362012-06-18 15:00:06 -0700181
182 Thread* self = Thread::Current();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700183 DCHECK_NE(self->GetState(), kRunnable);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700184 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700185 MutexLock mu(self, signal_catcher->lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700186 signal_catcher->thread_ = self;
Ian Rogersc604d732012-10-14 16:09:54 -0700187 signal_catcher->cond_.Broadcast(self);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700188 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700189
Elliott Hughese27955c2011-08-26 15:21:24 -0700190 // Set up mask with signals we want to handle.
Elliott Hughes457005c2012-04-16 13:54:25 -0700191 SignalSet signals;
192 signals.Add(SIGQUIT);
193 signals.Add(SIGUSR1);
Elliott Hughese27955c2011-08-26 15:21:24 -0700194
195 while (true) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700196 int signal_number = signal_catcher->WaitForSignal(self, signals);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700197 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700198 runtime->DetachCurrentThread();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700199 return nullptr;
Elliott Hughese27955c2011-08-26 15:21:24 -0700200 }
201
Elliott Hughese27955c2011-08-26 15:21:24 -0700202 switch (signal_number) {
203 case SIGQUIT:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700204 signal_catcher->HandleSigQuit();
Elliott Hughese27955c2011-08-26 15:21:24 -0700205 break;
206 case SIGUSR1:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700207 signal_catcher->HandleSigUsr1();
Elliott Hughese27955c2011-08-26 15:21:24 -0700208 break;
209 default:
210 LOG(ERROR) << "Unexpected signal %d" << signal_number;
211 break;
212 }
213 }
214}
215
216} // namespace art