1e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes/*
2e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes * Copyright (C) 2008 The Android Open Source Project
3e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes *
4e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes * you may not use this file except in compliance with the License.
6e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes * You may obtain a copy of the License at
7e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes *
8e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes *
10e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes * See the License for the specific language governing permissions and
14e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes * limitations under the License.
15e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes */
16e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_SIGNAL_CATCHER_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_SIGNAL_CATCHER_H_
19e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
2076b6167407c2b6f5d40ad895b2793a6b037f54b2Elliott Hughes#include "base/mutex.h"
21e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
22e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughesnamespace art {
23e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
24e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughesclass Runtime;
25457005c557b8762475db3220ce5a747d629f975bElliott Hughesclass SignalSet;
26e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughesclass Thread;
27e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
28e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes/*
298cf5bc0ab8742df107f0f4fc8ec0e991b6568f11Elliott Hughes * A daemon thread that catches signals and does something useful. For
30e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes * example, when a SIGQUIT (Ctrl-\) arrives, we suspend and dump the
31e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes * status of all threads.
32e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes */
33e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughesclass SignalCatcher {
34e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes public:
35ff17f1fd3ff32f93e45588eb2b158832d73f9afaElliott Hughes  explicit SignalCatcher(const std::string& stack_trace_file);
36e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes  ~SignalCatcher();
37e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
38b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void HandleSigQuit() LOCKS_EXCLUDED(Locks::mutator_lock_,
39b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                                      Locks::thread_list_lock_,
40b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                                      Locks::thread_suspend_count_lock_);
4100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
4242ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes
43e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes private:
44e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes  static void* Run(void* arg);
45e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
4694ce37a3919a0bdb8855a3d3264a50df1dbc41beElliott Hughes  void HandleSigUsr1();
4794ce37a3919a0bdb8855a3d3264a50df1dbc41beElliott Hughes  void Output(const std::string& s);
485fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes  void SetHaltFlag(bool new_value);
495fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes  bool ShouldHalt();
50f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughes  int WaitForSignal(Thread* self, SignalSet& signals);
515fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes
5294ce37a3919a0bdb8855a3d3264a50df1dbc41beElliott Hughes  std::string stack_trace_file_;
53f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughes
54c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
55c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  ConditionVariable cond_ GUARDED_BY(lock_);
56f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughes  bool halt_ GUARDED_BY(lock_);
57f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughes  pthread_t pthread_ GUARDED_BY(lock_);
58f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughes  Thread* thread_ GUARDED_BY(lock_);
59e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes};
60e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
61e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes}  // namespace art
62e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
63fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_SIGNAL_CATCHER_H_
64