blob: 8a2a7289de57fd8753431315d830e51924f1151e [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_SIGNAL_CATCHER_H_
18#define ART_RUNTIME_SIGNAL_CATCHER_H_
Elliott Hughese27955c2011-08-26 15:21:24 -070019
Narayan Kamatheb710332017-05-10 11:48:46 +010020#include "android-base/unique_fd.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080021#include "base/mutex.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070022
23namespace art {
24
25class Runtime;
Elliott Hughes457005c2012-04-16 13:54:25 -070026class SignalSet;
Elliott Hughese27955c2011-08-26 15:21:24 -070027class Thread;
28
29/*
Elliott Hughes8cf5bc02012-02-02 16:32:16 -080030 * A daemon thread that catches signals and does something useful. For
Elliott Hughese27955c2011-08-26 15:21:24 -070031 * example, when a SIGQUIT (Ctrl-\) arrives, we suspend and dump the
32 * status of all threads.
33 */
34class SignalCatcher {
35 public:
Narayan Kamatheb710332017-05-10 11:48:46 +010036 // If |use_tombstoned_stack_trace_fd| is |true|, traces will be
37 // written to a file descriptor provided by tombstoned. The process
38 // will communicate with tombstoned via a unix domain socket. This
39 // mode of stack trace dumping is only supported in an Android
40 // environment.
Narayan Kamath84695ae2017-04-07 15:41:41 +010041 //
Narayan Kamatheb710332017-05-10 11:48:46 +010042 // If false, all traces will be dumped to |stack_trace_file| if it's
43 // non-empty. If |stack_trace_file| is empty, all traces will be written
44 // to the log buffer.
45 SignalCatcher(const std::string& stack_trace_file,
46 const bool use_tombstoned_stack_trace_fd);
Elliott Hughese27955c2011-08-26 15:21:24 -070047 ~SignalCatcher();
48
Mathieu Chartier90443472015-07-16 20:32:27 -070049 void HandleSigQuit() REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_,
50 !Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051
Elliott Hughes42ee1422011-09-06 12:33:32 -070052
Elliott Hughese27955c2011-08-26 15:21:24 -070053 private:
Mathieu Chartier90443472015-07-16 20:32:27 -070054 // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock.
55 static void* Run(void* arg) NO_THREAD_SAFETY_ANALYSIS;
Elliott Hughese27955c2011-08-26 15:21:24 -070056
Narayan Kamatheb710332017-05-10 11:48:46 +010057 // NOTE: We're using android::base::unique_fd here for easier
58 // interoperability with tombstoned client APIs.
59 bool OpenStackTraceFile(android::base::unique_fd* tombstone_fd,
60 android::base::unique_fd* output_fd);
Elliott Hughes94ce37a2011-10-18 15:07:48 -070061 void HandleSigUsr1();
62 void Output(const std::string& s);
Mathieu Chartier90443472015-07-16 20:32:27 -070063 void SetHaltFlag(bool new_value) REQUIRES(!lock_);
64 bool ShouldHalt() REQUIRES(!lock_);
65 int WaitForSignal(Thread* self, SignalSet& signals) REQUIRES(!lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -070066
Elliott Hughes94ce37a2011-10-18 15:07:48 -070067 std::string stack_trace_file_;
Narayan Kamatheb710332017-05-10 11:48:46 +010068 const bool use_tombstoned_stack_trace_fd_;
Elliott Hughesf8349362012-06-18 15:00:06 -070069
Ian Rogersc604d732012-10-14 16:09:54 -070070 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
71 ConditionVariable cond_ GUARDED_BY(lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -070072 bool halt_ GUARDED_BY(lock_);
73 pthread_t pthread_ GUARDED_BY(lock_);
74 Thread* thread_ GUARDED_BY(lock_);
Elliott Hughese27955c2011-08-26 15:21:24 -070075};
76
77} // namespace art
78
Brian Carlstromfc0e3212013-07-17 14:40:12 -070079#endif // ART_RUNTIME_SIGNAL_CATCHER_H_