blob: 71c99771cd84535bfe75d51455f0cf624831fff6 [file] [log] [blame]
Dave Allisonb373e092014-02-20 16:06:36 -08001/*
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 {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070029
30namespace mirror {
31class ArtMethod;
Ian Rogers576ca0c2014-06-06 15:58:22 -070032} // namespace mirror
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070033
Dave Allisonb373e092014-02-20 16:06:36 -080034class 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);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070044 void AddHandler(FaultHandler* handler, bool generated_code);
Dave Allisonb373e092014-02-20 16:06:36 -080045 void RemoveHandler(FaultHandler* handler);
Dave Allison69dfe512014-07-11 17:11:58 +000046 void GetMethodAndReturnPCAndSP(siginfo_t* siginfo, void* context, mirror::ArtMethod** out_method,
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070047 uintptr_t* out_return_pc, uintptr_t* out_sp);
Dave Allison69dfe512014-07-11 17:11:58 +000048 bool IsInGeneratedCode(siginfo_t* siginfo, void *context, bool check_dex_pc)
49 NO_THREAD_SAFETY_ANALYSIS;
Dave Allisonb373e092014-02-20 16:06:36 -080050
51 private:
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070052 std::vector<FaultHandler*> generated_code_handlers_;
53 std::vector<FaultHandler*> other_handlers_;
Dave Allisonb373e092014-02-20 16:06:36 -080054 struct sigaction oldaction_;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070055 DISALLOW_COPY_AND_ASSIGN(FaultManager);
Dave Allisonb373e092014-02-20 16:06:36 -080056};
57
58class FaultHandler {
59 public:
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070060 explicit FaultHandler(FaultManager* manager);
Dave Allisonb373e092014-02-20 16:06:36 -080061 virtual ~FaultHandler() {}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070062 FaultManager* GetFaultManager() {
63 return manager_;
64 }
Dave Allisonb373e092014-02-20 16:06:36 -080065
66 virtual bool Action(int sig, siginfo_t* siginfo, void* context) = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070067
Dave Allisonb373e092014-02-20 16:06:36 -080068 protected:
69 FaultManager* const manager_;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070070
71 private:
72 DISALLOW_COPY_AND_ASSIGN(FaultHandler);
Dave Allisonb373e092014-02-20 16:06:36 -080073};
74
75class NullPointerHandler FINAL : public FaultHandler {
76 public:
Dave Allisonb373e092014-02-20 16:06:36 -080077 explicit NullPointerHandler(FaultManager* manager);
78
79 bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070080
81 private:
82 DISALLOW_COPY_AND_ASSIGN(NullPointerHandler);
Dave Allisonb373e092014-02-20 16:06:36 -080083};
84
85class SuspensionHandler FINAL : public FaultHandler {
86 public:
Dave Allisonb373e092014-02-20 16:06:36 -080087 explicit SuspensionHandler(FaultManager* manager);
88
89 bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070090
91 private:
92 DISALLOW_COPY_AND_ASSIGN(SuspensionHandler);
Dave Allisonb373e092014-02-20 16:06:36 -080093};
94
95class StackOverflowHandler FINAL : public FaultHandler {
96 public:
Dave Allisonb373e092014-02-20 16:06:36 -080097 explicit StackOverflowHandler(FaultManager* manager);
98
99 bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700100
101 private:
102 DISALLOW_COPY_AND_ASSIGN(StackOverflowHandler);
Dave Allisonb373e092014-02-20 16:06:36 -0800103};
104
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700105class JavaStackTraceHandler FINAL : public FaultHandler {
106 public:
107 explicit JavaStackTraceHandler(FaultManager* manager);
108
109 bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE NO_THREAD_SAFETY_ANALYSIS;
110
111 private:
112 DISALLOW_COPY_AND_ASSIGN(JavaStackTraceHandler);
113};
114
115
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700116// Statically allocated so the the signal handler can Get access to it.
Dave Allisonb373e092014-02-20 16:06:36 -0800117extern FaultManager fault_manager;
118
119} // namespace art
120#endif // ART_RUNTIME_FAULT_HANDLER_H_
121