Home | History | Annotate | Download | only in runtime
      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 "android-base/unique_fd.h"
     21 #include "base/mutex.h"
     22 
     23 namespace art {
     24 
     25 class Runtime;
     26 class SignalSet;
     27 class Thread;
     28 
     29 /*
     30  * A daemon thread that catches signals and does something useful. For
     31  * example, when a SIGQUIT (Ctrl-\) arrives, we suspend and dump the
     32  * status of all threads.
     33  */
     34 class SignalCatcher {
     35  public:
     36   // 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.
     41   //
     42   // 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);
     47   ~SignalCatcher();
     48 
     49   void HandleSigQuit() REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_,
     50                                 !Locks::thread_suspend_count_lock_);
     51 
     52 
     53  private:
     54   // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock.
     55   static void* Run(void* arg) NO_THREAD_SAFETY_ANALYSIS;
     56 
     57   // 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);
     61   void HandleSigUsr1();
     62   void Output(const std::string& s);
     63   void SetHaltFlag(bool new_value) REQUIRES(!lock_);
     64   bool ShouldHalt() REQUIRES(!lock_);
     65   int WaitForSignal(Thread* self, SignalSet& signals) REQUIRES(!lock_);
     66 
     67   std::string stack_trace_file_;
     68   const bool use_tombstoned_stack_trace_fd_;
     69 
     70   mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
     71   ConditionVariable cond_ GUARDED_BY(lock_);
     72   bool halt_ GUARDED_BY(lock_);
     73   pthread_t pthread_ GUARDED_BY(lock_);
     74   Thread* thread_ GUARDED_BY(lock_);
     75 };
     76 
     77 }  // namespace art
     78 
     79 #endif  // ART_RUNTIME_SIGNAL_CATCHER_H_
     80