fault_handler.h revision b0f05b9654eb005bc8c8e15f615a7f5a312f640c
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
18#ifndef ART_RUNTIME_FAULT_HANDLER_H_
19#define ART_RUNTIME_FAULT_HANDLER_H_
20
21#include <signal.h>
22#include <vector>
23#include <setjmp.h>
24#include <stdint.h>
25
26#include "base/mutex.h"   // For annotalysis.
27
28namespace art {
29
30namespace mirror {
31class ArtMethod;
32}  // namespace mirror
33
34class FaultHandler;
35
36class FaultManager {
37 public:
38  FaultManager();
39  ~FaultManager();
40
41  void Init();
42
43  void HandleFault(int sig, siginfo_t* info, void* context);
44  void AddHandler(FaultHandler* handler, bool generated_code);
45  void RemoveHandler(FaultHandler* handler);
46
47  // Note that the following two functions are called in the context of a signal handler.
48  // The IsInGeneratedCode() function checks that the mutator lock is held before it
49  // calls GetMethodAndReturnPCAndSP().
50  // TODO: think about adding lock assertions and fake lock and unlock functions.
51  void GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context, mirror::ArtMethod** out_method,
52                                 uintptr_t* out_return_pc, uintptr_t* out_sp)
53                                 NO_THREAD_SAFETY_ANALYSIS;
54  bool IsInGeneratedCode(siginfo_t* siginfo, void *context, bool check_dex_pc)
55                         NO_THREAD_SAFETY_ANALYSIS;
56
57 private:
58  std::vector<FaultHandler*> generated_code_handlers_;
59  std::vector<FaultHandler*> other_handlers_;
60  struct sigaction oldaction_;
61  DISALLOW_COPY_AND_ASSIGN(FaultManager);
62};
63
64class FaultHandler {
65 public:
66  explicit FaultHandler(FaultManager* manager);
67  virtual ~FaultHandler() {}
68  FaultManager* GetFaultManager() {
69    return manager_;
70  }
71
72  virtual bool Action(int sig, siginfo_t* siginfo, void* context) = 0;
73
74 protected:
75  FaultManager* const manager_;
76
77 private:
78  DISALLOW_COPY_AND_ASSIGN(FaultHandler);
79};
80
81class NullPointerHandler FINAL : public FaultHandler {
82 public:
83  explicit NullPointerHandler(FaultManager* manager);
84
85  bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
86
87 private:
88  DISALLOW_COPY_AND_ASSIGN(NullPointerHandler);
89};
90
91class SuspensionHandler FINAL : public FaultHandler {
92 public:
93  explicit SuspensionHandler(FaultManager* manager);
94
95  bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
96
97 private:
98  DISALLOW_COPY_AND_ASSIGN(SuspensionHandler);
99};
100
101class StackOverflowHandler FINAL : public FaultHandler {
102 public:
103  explicit StackOverflowHandler(FaultManager* manager);
104
105  bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
106
107 private:
108  DISALLOW_COPY_AND_ASSIGN(StackOverflowHandler);
109};
110
111class JavaStackTraceHandler FINAL : public FaultHandler {
112 public:
113  explicit JavaStackTraceHandler(FaultManager* manager);
114
115  bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE NO_THREAD_SAFETY_ANALYSIS;
116
117 private:
118  DISALLOW_COPY_AND_ASSIGN(JavaStackTraceHandler);
119};
120
121
122// Statically allocated so the the signal handler can Get access to it.
123extern FaultManager fault_manager;
124
125}       // namespace art
126#endif  // ART_RUNTIME_FAULT_HANDLER_H_
127
128