1 /*
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 #ifndef ART_RUNTIME_SIGNAL_CATCHER_H_
18 #define ART_RUNTIME_SIGNAL_CATCHER_H_
19 
20 #include <optional>
21 
22 #include "android-base/unique_fd.h"
23 #include "base/mutex.h"
24 #include "base/macros.h"
25 
26 namespace art HIDDEN {
27 
28 class Runtime;
29 class SignalSet;
30 class Thread;
31 
32 /*
33  * A daemon thread that catches signals and does something useful. For
34  * example, when a SIGQUIT (Ctrl-\) arrives, we suspend and dump the
35  * status of all threads.
36  */
37 class SignalCatcher {
38  public:
39   SignalCatcher();
40   ~SignalCatcher();
41 
42   void HandleSigQuit() REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_,
43                                 !Locks::thread_suspend_count_lock_);
44 
SiqQuitNanoTime()45   std::optional<uint64_t> SiqQuitNanoTime() const { return sigquit_nanotime_; }
46 
47  private:
48   // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock.
49   static void* Run(void* arg) NO_THREAD_SAFETY_ANALYSIS;
50 
51   void HandleSigUsr1();
52   void Output(const std::string& s);
53   void SetHaltFlag(bool new_value) REQUIRES(!lock_);
54   bool ShouldHalt() REQUIRES(!lock_);
55   int WaitForSignal(Thread* self, SignalSet& signals) REQUIRES(!lock_);
56 
57   mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
58   ConditionVariable cond_ GUARDED_BY(lock_);
59   bool halt_ GUARDED_BY(lock_);
60   pthread_t pthread_ GUARDED_BY(lock_);
61   Thread* thread_ GUARDED_BY(lock_);
62   std::optional<uint64_t> sigquit_nanotime_;
63 };
64 
65 }  // namespace art
66 
67 #endif  // ART_RUNTIME_SIGNAL_CATCHER_H_
68