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_H 17 #define PANDA_TOOLING_INSPECTOR_INSPECTOR_H 18 19 #include "inspector_hooks.h" 20 #include "source_manager.h" 21 22 #include "macros.h" 23 #include "tooling/pt_thread.h" 24 25 #include <functional> 26 #include <optional> 27 #include <string> 28 #include <utility> 29 #include <vector> 30 31 namespace panda { 32 class JsonObjectBuilder; 33 } // namespace panda 34 35 namespace panda::tooling { 36 class DebugInterface; 37 } // namespace panda::tooling 38 39 namespace panda::tooling::inspector { 40 class Server; 41 42 class Inspector { 43 public: 44 Inspector(Server &server, DebugInterface *debugger); 45 ~Inspector(); 46 NO_COPY_SEMANTIC(Inspector); 47 NO_MOVE_SEMANTIC(Inspector); 48 GetDebugger()49 DebugInterface &GetDebugger() const 50 { 51 return *debugger_; 52 } 53 GetServer()54 Server &GetServer() const 55 { 56 return server_; 57 } 58 GetSourceManager()59 SourceManager &GetSourceManager() 60 { 61 return sourceManager_; 62 } 63 64 // Pause the VM if a pause is pending. 65 bool HandlePendingPause(); 66 67 template <typename Handler> OnPause(Handler && handler)68 void OnPause(Handler &&handler) 69 { 70 pauseHandlers_.emplace_back(std::forward<Handler>(handler)); 71 } 72 73 // Set pending pause. If reason is given, notify the client with 74 // "Debugger.paused". SetPause(PtThread thread,std::optional<std::string> reason)75 void SetPause(PtThread thread, std::optional<std::string> reason) 76 { 77 pendingPause_.emplace(PendingPause {thread, std::move(reason)}); 78 } 79 80 // Resume the VM and return resumed thread. 81 PtThread Resume(); 82 83 private: 84 void EnableDebugger(JsonObjectBuilder &result); 85 void EnableRuntime(); 86 87 struct PendingPause { 88 PtThread thread; 89 std::optional<std::string> reason; 90 }; 91 92 Server &server_; 93 DebugInterface *debugger_; 94 SourceManager sourceManager_; 95 std::vector<std::function<void()>> pauseHandlers_; 96 InspectorHooks hooks_; 97 std::optional<PendingPause> pendingPause_; 98 std::optional<PtThread> pausedThread_; 99 }; 100 } // namespace panda::tooling::inspector 101 102 #endif // PANDA_TOOLING_INSPECTOR_INSPECTOR_H 103