blob: 9f8c55c9800f9e3c6b553e2ea57f083815bce8bf [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
Ian Rogersd582fa42014-11-05 23:46:43 -080030#include "arch/instruction_set.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010031#include "base/time_utils.h"
Elliott Hughes76160052012-12-12 16:31:20 -080032#include "base/unix_file/fd_file.h"
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070033#include "class_linker.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/heap.h"
Elliott Hughes94ce37a2011-10-18 15:07:48 -070035#include "os.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037#include "scoped_thread_state_change.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070038#include "signal_set.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070039#include "thread.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070040#include "thread_list.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070041#include "utils.h"
42
43namespace art {
44
Elliott Hughes0d39c122012-06-06 16:41:17 -070045static void DumpCmdLine(std::ostream& os) {
46#if defined(__linux__)
47 // Show the original command line, and the current command line too if it's changed.
48 // On Android, /proc/self/cmdline will have been rewritten to something like "system_server".
Jeff Brownb4fffc72014-09-10 18:41:18 -070049 // Note: The string "Cmd line:" is chosen to match the format used by debuggerd.
Elliott Hughes0d39c122012-06-06 16:41:17 -070050 std::string current_cmd_line;
51 if (ReadFileToString("/proc/self/cmdline", &current_cmd_line)) {
Jeff Brownb4fffc72014-09-10 18:41:18 -070052 current_cmd_line.resize(current_cmd_line.find_last_not_of('\0') + 1); // trim trailing '\0's
Elliott Hughes0d39c122012-06-06 16:41:17 -070053 std::replace(current_cmd_line.begin(), current_cmd_line.end(), '\0', ' ');
54
Jeff Brownb4fffc72014-09-10 18:41:18 -070055 os << "Cmd line: " << current_cmd_line << "\n";
Elliott Hughes0d39c122012-06-06 16:41:17 -070056 const char* stashed_cmd_line = GetCmdLine();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070057 if (stashed_cmd_line != nullptr && current_cmd_line != stashed_cmd_line
Jeff Brownb4fffc72014-09-10 18:41:18 -070058 && strcmp(stashed_cmd_line, "<unset>") != 0) {
59 os << "Original command line: " << stashed_cmd_line << "\n";
Elliott Hughes0d39c122012-06-06 16:41:17 -070060 }
Elliott Hughesae80b492012-04-24 10:43:17 -070061 }
Elliott Hughes0d39c122012-06-06 16:41:17 -070062#else
Jeff Brownb4fffc72014-09-10 18:41:18 -070063 os << "Cmd line: " << GetCmdLine() << "\n";
Elliott Hughesabbe07d2012-06-05 17:42:23 -070064#endif
Elliott Hughes0d39c122012-06-06 16:41:17 -070065}
Elliott Hughesae80b492012-04-24 10:43:17 -070066
Elliott Hughes94ce37a2011-10-18 15:07:48 -070067SignalCatcher::SignalCatcher(const std::string& stack_trace_file)
68 : stack_trace_file_(stack_trace_file),
69 lock_("SignalCatcher lock"),
Ian Rogersc604d732012-10-14 16:09:54 -070070 cond_("SignalCatcher::cond_", lock_),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070071 thread_(nullptr) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070072 SetHaltFlag(false);
73
Elliott Hughese27955c2011-08-26 15:21:24 -070074 // Create a raw pthread; its start routine will attach to the runtime.
Mathieu Chartier2cebb242015-04-21 16:50:40 -070075 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, nullptr, &Run, this), "signal catcher thread");
Elliott Hughes8d768a92011-09-14 16:35:25 -070076
Ian Rogers81d425b2012-09-27 16:03:43 -070077 Thread* self = Thread::Current();
78 MutexLock mu(self, lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070079 while (thread_ == nullptr) {
Ian Rogersc604d732012-10-14 16:09:54 -070080 cond_.Wait(self);
Elliott Hughese27955c2011-08-26 15:21:24 -070081 }
82}
83
84SignalCatcher::~SignalCatcher() {
85 // Since we know the thread is just sitting around waiting for signals
86 // to arrive, send it one.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070087 SetHaltFlag(true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070088 CHECK_PTHREAD_CALL(pthread_kill, (pthread_, SIGQUIT), "signal catcher shutdown");
Mathieu Chartier2cebb242015-04-21 16:50:40 -070089 CHECK_PTHREAD_CALL(pthread_join, (pthread_, nullptr), "signal catcher shutdown");
Elliott Hughese27955c2011-08-26 15:21:24 -070090}
91
Elliott Hughes5fe594f2011-09-08 12:33:17 -070092void SignalCatcher::SetHaltFlag(bool new_value) {
Ian Rogers50b35e22012-10-04 10:09:15 -070093 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -070094 halt_ = new_value;
95}
96
97bool SignalCatcher::ShouldHalt() {
Ian Rogers50b35e22012-10-04 10:09:15 -070098 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -070099 return halt_;
100}
101
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700102void SignalCatcher::Output(const std::string& s) {
103 if (stack_trace_file_.empty()) {
104 LOG(INFO) << s;
105 return;
106 }
107
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700108 ScopedThreadStateChange tsc(Thread::Current(), kWaitingForSignalCatcherOutput);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700109 int fd = open(stack_trace_file_.c_str(), O_APPEND | O_CREAT | O_WRONLY, 0666);
110 if (fd == -1) {
111 PLOG(ERROR) << "Unable to open stack trace file '" << stack_trace_file_ << "'";
112 return;
113 }
Andreas Gampe4303ba92014-11-06 01:00:46 -0800114 std::unique_ptr<File> file(new File(fd, stack_trace_file_, true));
115 bool success = file->WriteFully(s.data(), s.size());
116 if (success) {
117 success = file->FlushCloseOrErase() == 0;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700118 } else {
Andreas Gampe4303ba92014-11-06 01:00:46 -0800119 file->Erase();
120 }
121 if (success) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700122 LOG(INFO) << "Wrote stack traces to '" << stack_trace_file_ << "'";
Andreas Gampe4303ba92014-11-06 01:00:46 -0800123 } else {
124 PLOG(ERROR) << "Failed to write stack traces to '" << stack_trace_file_ << "'";
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700125 }
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700126}
127
Elliott Hughese27955c2011-08-26 15:21:24 -0700128void SignalCatcher::HandleSigQuit() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700129 Runtime* runtime = Runtime::Current();
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700130 std::ostringstream os;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700131 os << "\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132 << "----- pid " << getpid() << " at " << GetIsoDate() << " -----\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700133
Elliott Hughes0d39c122012-06-06 16:41:17 -0700134 DumpCmdLine(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700135
Jeff Brownb4fffc72014-09-10 18:41:18 -0700136 // Note: The string "ABI:" is chosen to match the format used by debuggerd.
137 os << "ABI: " << GetInstructionSetString(runtime->GetInstructionSet()) << "\n";
138
Elliott Hughesae80b492012-04-24 10:43:17 -0700139 os << "Build type: " << (kIsDebugBuild ? "debug" : "optimized") << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700140
Elliott Hughesc967f782012-04-16 10:23:15 -0700141 runtime->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700142
Ian Rogerscf7f1912014-10-22 22:06:39 -0700143 if ((false)) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700144 std::string maps;
145 if (ReadFileToString("/proc/self/maps", &maps)) {
146 os << "/proc/self/maps:\n" << maps;
147 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700148 }
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700149 os << "----- end " << getpid() << " -----\n";
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700150 Output(os.str());
Elliott Hughese27955c2011-08-26 15:21:24 -0700151}
152
153void SignalCatcher::HandleSigUsr1() {
154 LOG(INFO) << "SIGUSR1 forcing GC (no HPROF)";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800155 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughese27955c2011-08-26 15:21:24 -0700156}
157
Elliott Hughesf8349362012-06-18 15:00:06 -0700158int SignalCatcher::WaitForSignal(Thread* self, SignalSet& signals) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 ScopedThreadStateChange tsc(self, kWaitingInMainSignalCatcherLoop);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700160
161 // Signals for sigwait() must be blocked but not ignored. We
162 // block signals like SIGQUIT for all threads, so the condition
163 // is met. When the signal hits, we wake up, without any signal
164 // handlers being invoked.
Elliott Hughes457005c2012-04-16 13:54:25 -0700165 int signal_number = signals.Wait();
Elliott Hughesc2f80062011-11-07 11:57:51 -0800166 if (!ShouldHalt()) {
167 // Let the user know we got the signal, just in case the system's too screwed for us to
168 // actually do what they want us to do...
Elliott Hughesf8349362012-06-18 15:00:06 -0700169 LOG(INFO) << *self << ": reacting to signal " << signal_number;
Elliott Hughesc2f80062011-11-07 11:57:51 -0800170
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800171 // If anyone's holding locks (which might prevent us from getting back into state Runnable), say so...
172 Runtime::Current()->DumpLockHolders(LOG(INFO));
Elliott Hughesc2f80062011-11-07 11:57:51 -0800173 }
174
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700175 return signal_number;
176}
177
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700178void* SignalCatcher::Run(void* arg) {
179 SignalCatcher* signal_catcher = reinterpret_cast<SignalCatcher*>(arg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700180 CHECK(signal_catcher != nullptr);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700181
Elliott Hughes8d768a92011-09-14 16:35:25 -0700182 Runtime* runtime = Runtime::Current();
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800183 CHECK(runtime->AttachCurrentThread("Signal Catcher", true, runtime->GetSystemThreadGroup(),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800184 !runtime->IsAotCompiler()));
Elliott Hughesf8349362012-06-18 15:00:06 -0700185
186 Thread* self = Thread::Current();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700187 DCHECK_NE(self->GetState(), kRunnable);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700188 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700189 MutexLock mu(self, signal_catcher->lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700190 signal_catcher->thread_ = self;
Ian Rogersc604d732012-10-14 16:09:54 -0700191 signal_catcher->cond_.Broadcast(self);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700192 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700193
Elliott Hughese27955c2011-08-26 15:21:24 -0700194 // Set up mask with signals we want to handle.
Elliott Hughes457005c2012-04-16 13:54:25 -0700195 SignalSet signals;
196 signals.Add(SIGQUIT);
197 signals.Add(SIGUSR1);
Elliott Hughese27955c2011-08-26 15:21:24 -0700198
199 while (true) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700200 int signal_number = signal_catcher->WaitForSignal(self, signals);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700201 if (signal_catcher->ShouldHalt()) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700202 runtime->DetachCurrentThread();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700203 return nullptr;
Elliott Hughese27955c2011-08-26 15:21:24 -0700204 }
205
Elliott Hughese27955c2011-08-26 15:21:24 -0700206 switch (signal_number) {
207 case SIGQUIT:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700208 signal_catcher->HandleSigQuit();
Elliott Hughese27955c2011-08-26 15:21:24 -0700209 break;
210 case SIGUSR1:
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700211 signal_catcher->HandleSigUsr1();
Elliott Hughese27955c2011-08-26 15:21:24 -0700212 break;
213 default:
214 LOG(ERROR) << "Unexpected signal %d" << signal_number;
215 break;
216 }
217 }
218}
219
220} // namespace art