Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 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 | |
| 28 | namespace art { |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 29 | |
| 30 | namespace mirror { |
| 31 | class ArtMethod; |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 32 | } // namespace mirror |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 33 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 34 | class FaultHandler; |
| 35 | |
| 36 | class FaultManager { |
| 37 | public: |
| 38 | FaultManager(); |
| 39 | ~FaultManager(); |
| 40 | |
| 41 | void Init(); |
| 42 | |
| 43 | void HandleFault(int sig, siginfo_t* info, void* context); |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 44 | void AddHandler(FaultHandler* handler, bool generated_code); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 45 | void RemoveHandler(FaultHandler* handler); |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 46 | void GetMethodAndReturnPCAndSP(void* context, mirror::ArtMethod** out_method, |
| 47 | uintptr_t* out_return_pc, uintptr_t* out_sp); |
| 48 | bool IsInGeneratedCode(void *context, bool check_dex_pc) NO_THREAD_SAFETY_ANALYSIS; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 49 | |
| 50 | private: |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 51 | std::vector<FaultHandler*> generated_code_handlers_; |
| 52 | std::vector<FaultHandler*> other_handlers_; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 53 | struct sigaction oldaction_; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 54 | DISALLOW_COPY_AND_ASSIGN(FaultManager); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | class FaultHandler { |
| 58 | public: |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 59 | explicit FaultHandler(FaultManager* manager); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 60 | virtual ~FaultHandler() {} |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 61 | FaultManager* GetFaultManager() { |
| 62 | return manager_; |
| 63 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 64 | |
| 65 | virtual bool Action(int sig, siginfo_t* siginfo, void* context) = 0; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 66 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 67 | protected: |
| 68 | FaultManager* const manager_; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | DISALLOW_COPY_AND_ASSIGN(FaultHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | class NullPointerHandler FINAL : public FaultHandler { |
| 75 | public: |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 76 | explicit NullPointerHandler(FaultManager* manager); |
| 77 | |
| 78 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 79 | |
| 80 | private: |
| 81 | DISALLOW_COPY_AND_ASSIGN(NullPointerHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | class SuspensionHandler FINAL : public FaultHandler { |
| 85 | public: |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 86 | explicit SuspensionHandler(FaultManager* manager); |
| 87 | |
| 88 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 89 | |
| 90 | private: |
| 91 | DISALLOW_COPY_AND_ASSIGN(SuspensionHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | class StackOverflowHandler FINAL : public FaultHandler { |
| 95 | public: |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 96 | explicit StackOverflowHandler(FaultManager* manager); |
| 97 | |
| 98 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 99 | |
| 100 | private: |
| 101 | DISALLOW_COPY_AND_ASSIGN(StackOverflowHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 104 | class JavaStackTraceHandler FINAL : public FaultHandler { |
| 105 | public: |
| 106 | explicit JavaStackTraceHandler(FaultManager* manager); |
| 107 | |
| 108 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE NO_THREAD_SAFETY_ANALYSIS; |
| 109 | |
| 110 | private: |
| 111 | DISALLOW_COPY_AND_ASSIGN(JavaStackTraceHandler); |
| 112 | }; |
| 113 | |
| 114 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 115 | // Statically allocated so the the signal handler can Get access to it. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 116 | extern FaultManager fault_manager; |
| 117 | |
| 118 | } // namespace art |
| 119 | #endif // ART_RUNTIME_FAULT_HANDLER_H_ |
| 120 | |