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 { |
| 29 | class FaultHandler; |
| 30 | |
| 31 | class FaultManager { |
| 32 | public: |
| 33 | FaultManager(); |
| 34 | ~FaultManager(); |
| 35 | |
| 36 | void Init(); |
| 37 | |
| 38 | void HandleFault(int sig, siginfo_t* info, void* context); |
| 39 | void AddHandler(FaultHandler* handler); |
| 40 | void RemoveHandler(FaultHandler* handler); |
| 41 | |
| 42 | private: |
| 43 | bool IsInGeneratedCode(void *context) NO_THREAD_SAFETY_ANALYSIS; |
| 44 | void GetMethodAndReturnPC(void* context, uintptr_t& method, uintptr_t& return_pc); |
| 45 | |
| 46 | typedef std::vector<FaultHandler*> Handlers; |
| 47 | Handlers handlers_; |
| 48 | struct sigaction oldaction_; |
| 49 | }; |
| 50 | |
| 51 | class FaultHandler { |
| 52 | public: |
| 53 | FaultHandler() : manager_(nullptr) {} |
| 54 | explicit FaultHandler(FaultManager* manager) : manager_(manager) {} |
| 55 | virtual ~FaultHandler() {} |
| 56 | |
| 57 | virtual bool Action(int sig, siginfo_t* siginfo, void* context) = 0; |
| 58 | protected: |
| 59 | FaultManager* const manager_; |
| 60 | }; |
| 61 | |
| 62 | class NullPointerHandler FINAL : public FaultHandler { |
| 63 | public: |
| 64 | NullPointerHandler() {} |
| 65 | explicit NullPointerHandler(FaultManager* manager); |
| 66 | |
| 67 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE; |
| 68 | }; |
| 69 | |
| 70 | class SuspensionHandler FINAL : public FaultHandler { |
| 71 | public: |
| 72 | SuspensionHandler() {} |
| 73 | explicit SuspensionHandler(FaultManager* manager); |
| 74 | |
| 75 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE; |
| 76 | }; |
| 77 | |
| 78 | class StackOverflowHandler FINAL : public FaultHandler { |
| 79 | public: |
| 80 | StackOverflowHandler() {} |
| 81 | explicit StackOverflowHandler(FaultManager* manager); |
| 82 | |
| 83 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE; |
| 84 | }; |
| 85 | |
| 86 | // Statically allocated so the the signal handler can get access to it. |
| 87 | extern FaultManager fault_manager; |
| 88 | |
| 89 | } // namespace art |
| 90 | #endif // ART_RUNTIME_FAULT_HANDLER_H_ |
| 91 | |