1 /** 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_TOOLING_INSPECTOR_INSPECTOR_HOOKS_H 17 #define PANDA_TOOLING_INSPECTOR_INSPECTOR_HOOKS_H 18 19 #include "os/mutex.h" 20 #include "tooling/debug_interface.h" 21 #include "tooling/pt_thread.h" 22 23 #include <memory> 24 #include <utility> 25 #include <vector> 26 27 namespace panda { 28 class Method; 29 } // namespace panda 30 31 namespace panda::tooling { 32 class PtLocation; 33 } // namespace panda::tooling 34 35 namespace panda::tooling::inspector { 36 class Inspector; 37 38 class InspectorHooks : public PtHooks { 39 public: 40 template <typename... Hooks> InspectorHooks(Inspector & inspector,Hooks...hooks)41 explicit InspectorHooks(Inspector &inspector, Hooks... hooks) : inspector_(inspector) 42 { 43 (hooks_.emplace_back(std::move(hooks)), ...); 44 } 45 Breakpoint(PtThread thread,Method * method,const PtLocation & location)46 void Breakpoint(PtThread thread, Method *method, const PtLocation &location) override 47 { 48 RunHooks<PtThread, Method *, const PtLocation &>(&PtHooks::Breakpoint, thread, method, location); 49 } 50 FramePop(PtThread thread,Method * method,bool wasPoppedByException)51 void FramePop(PtThread thread, Method *method, bool wasPoppedByException) override 52 { 53 RunHooks<PtThread, Method *, bool>(&PtHooks::FramePop, thread, method, wasPoppedByException); 54 } 55 MethodEntry(PtThread thread,Method * method)56 void MethodEntry(PtThread thread, Method *method) override 57 { 58 RunHooks<PtThread, Method *>(&PtHooks::MethodEntry, thread, method); 59 } 60 SingleStep(PtThread thread,Method * method,const PtLocation & location)61 void SingleStep(PtThread thread, Method *method, const PtLocation &location) override 62 { 63 RunHooks<PtThread, Method *, const PtLocation &>(&PtHooks::SingleStep, thread, method, location); 64 } 65 VmInitialization(PtThread thread)66 void VmInitialization(PtThread thread) override 67 { 68 RunHooks<PtThread>(&PtHooks::VmInitialization, thread); 69 } 70 71 private: 72 // Out-of-line definition to break include cycle. 73 bool HandlePendingPause() REQUIRES(mutex_); 74 75 template <typename... Param, typename... Arg> RunHooks(void (PtHooks::* method)(Param...),Arg &&...arg)76 void RunHooks(void (PtHooks::*method)(Param...), Arg &&... arg) 77 { 78 os::memory::LockHolder lock(mutex_); 79 80 for (auto &hooks : hooks_) { 81 (hooks.get()->*method)(std::forward<Arg>(arg)...); 82 83 if (HandlePendingPause()) { 84 return; 85 } 86 } 87 } 88 89 os::memory::Mutex mutex_; 90 Inspector &inspector_ GUARDED_BY(mutex_); 91 std::vector<std::unique_ptr<PtHooks>> hooks_ GUARDED_BY(mutex_); 92 }; 93 } // namespace panda::tooling::inspector 94 95 #endif // PANDA_TOOLING_INSPECTOR_INSPECTOR_HOOKS_H 96