1 /** 2 * Copyright (c) 2022-2025 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_SERVER_H 17 #define PANDA_TOOLING_INSPECTOR_INSPECTOR_SERVER_H 18 19 #include <cstddef> 20 #include <cstdint> 21 #include <functional> 22 #include <optional> 23 #include <set> 24 #include <string_view> 25 #include <vector> 26 27 #include "include/console_call_type.h" 28 #include "include/tooling/pt_thread.h" 29 30 #include "common.h" 31 #include "session_manager.h" 32 #include "source_manager.h" 33 #include "debugger/thread_state.h" 34 #include "types/evaluation_result.h" 35 #include "types/numeric_id.h" 36 #include "types/pause_on_exceptions_state.h" 37 #include "types/profile_result.h" 38 #include "types/property_descriptor.h" 39 #include "types/remote_object.h" 40 #include "types/scope.h" 41 42 namespace ark::tooling::inspector { 43 class UrlBreakpointResponse; 44 class Server; // NOLINT(fuchsia-virtual-inheritance) 45 class UrlBreakpointRequest; 46 47 class InspectorServer final { 48 public: 49 using SetBreakpointHandler = std::optional<BreakpointId>(PtThread, SourceFileFilter &&, size_t, 50 std::set<std::string_view> &, const std::string *); 51 using FrameInfoHandler = std::function<void(FrameId, std::string_view, std::string_view, size_t, 52 const std::vector<Scope> &, const std::optional<RemoteObject> &)>; 53 54 public: 55 explicit InspectorServer(Server &server); 56 ~InspectorServer() = default; 57 58 NO_COPY_SEMANTIC(InspectorServer); 59 NO_MOVE_SEMANTIC(InspectorServer); 60 61 void Kill(); 62 void Run(const std::string& msg); 63 64 void OnValidate(std::function<void()> &&handler); 65 void OnOpen(std::function<void()> &&handler); 66 void OnFail(std::function<void()> &&handler); 67 68 void CallDebuggerPaused(PtThread thread, const std::vector<BreakpointId> &hitBreakpoints, 69 const std::optional<RemoteObject> &exception, PauseReason pauseReason, 70 const std::function<void(const FrameInfoHandler &)> &enumerateFrames); 71 void CallDebuggerResumed(PtThread thread); 72 void CallDebuggerScriptParsed(ScriptId scriptId, std::string_view sourceFile); 73 void CallRuntimeConsoleApiCalled(PtThread thread, ConsoleCallType type, uint64_t timestamp, 74 const std::vector<RemoteObject> &arguments); 75 void CallRuntimeExecutionContextCreated(PtThread thread); 76 void CallRuntimeExecutionContextsCleared(); 77 void CallTargetAttachedToTarget(PtThread thread); 78 void CallTargetDetachedFromTarget(PtThread thread); 79 80 void OnCallDebuggerContinueToLocation(std::function<void(PtThread, std::string_view, size_t)> &&handler); 81 void OnCallDebuggerEnable(std::function<void()> &&handler); 82 void OnCallDebuggerGetPossibleBreakpoints( 83 std::function<std::set<size_t>(std::string_view, size_t, size_t, bool)> &&handler); 84 void OnCallDebuggerGetScriptSource(std::function<std::string(std::string_view)> &&handler); 85 void OnCallDebuggerPause(std::function<void(PtThread)> &&handler); 86 void OnCallDebuggerRemoveBreakpoint(std::function<void(PtThread, BreakpointId)> &&handler); 87 void OnCallDebuggerRemoveBreakpointsByUrl(std::function<void(PtThread, SourceFileFilter)> &&handler); 88 void OnCallDebuggerRestartFrame(std::function<void(PtThread, FrameId)> &&handler); 89 void OnCallDebuggerResume(std::function<void(PtThread)> &&handler); 90 void OnCallDebuggerSetAsyncCallStackDepth(std::function<void(PtThread)> &&handler); 91 void OnCallDebuggerSetBlackboxPatterns(std::function<void(PtThread)> &&handler); 92 void OnCallDebuggerSmartStepInto(std::function<void(PtThread)> &&handler); 93 void OnCallDebuggerSetBreakpoint(std::function<SetBreakpointHandler> &&handler); 94 void OnCallDebuggerSetBreakpointByUrl(std::function<SetBreakpointHandler> &&handler); 95 void OnCallDebuggerGetPossibleAndSetBreakpointByUrl(std::function<SetBreakpointHandler> &&handler); 96 void OnCallDebuggerSetBreakpointsActive(std::function<void(PtThread, bool)> &&handler); 97 void OnCallDebuggerSetSkipAllPauses(std::function<void(PtThread, bool)> &&handler); 98 void OnCallDebuggerSetPauseOnExceptions(std::function<void(PtThread, PauseOnExceptionsState)> &&handler); 99 void OnCallDebuggerStepInto(std::function<void(PtThread)> &&handler); 100 void OnCallDebuggerStepOut(std::function<void(PtThread)> &&handler); 101 void OnCallDebuggerStepOver(std::function<void(PtThread)> &&handler); 102 void OnCallDebuggerEvaluateOnCallFrame( 103 std::function<Expected<EvaluationResult, std::string>(PtThread, const std::string &, size_t)> &&handler); 104 void OnCallDebuggerClientDisconnect(std::function<void(PtThread)> &&handler); 105 void OnCallDebuggerDisable(std::function<void(PtThread)> &&handler); 106 void OnCallDebuggerDropFrame(std::function<void(PtThread)> &&handler); 107 void OnCallDebuggerSetNativeRange(std::function<void(PtThread)> &&handler); 108 void OnCallDebuggerReplyNativeCalling(std::function<void(PtThread)> &&handler); 109 void OnCallDebuggerCallFunctionOn( 110 std::function<Expected<EvaluationResult, std::string>(PtThread, const std::string &, size_t)> &&handler); 111 void OnCallDebuggerSetMixedDebugEnabled(std::function<void(PtThread, bool)> &&handler); 112 void OnCallRuntimeEnable(std::function<void(PtThread)> &&handler); 113 void OnCallRuntimeGetProperties( 114 std::function<std::vector<PropertyDescriptor>(PtThread, RemoteObjectId, bool)> &&handler); 115 void OnCallRuntimeRunIfWaitingForDebugger(std::function<void(PtThread)> &&handler); 116 void OnCallRuntimeEvaluate( 117 std::function<Expected<EvaluationResult, std::string>(PtThread, const std::string &)> &&handler); 118 void OnCallProfilerEnable(); 119 void OnCallProfilerDisable(); 120 void OnCallProfilerSetSamplingInterval(std::function<void(int32_t)> &&handler); 121 void OnCallProfilerStart(std::function<Expected<bool, std::string>()> &&handler); 122 void OnCallProfilerStop(std::function<Expected<Profile, std::string>()> &&handler); 123 GetSourceManager()124 SourceManager &GetSourceManager() 125 { 126 return sourceManager_; 127 } 128 129 private: 130 struct CallFrameInfo { 131 FrameId frameId; 132 std::string_view sourceFile; 133 std::string_view methodName; 134 size_t lineNumber; 135 }; 136 137 private: 138 void SendTargetAttachedToTarget(const std::string &sessionId); 139 void EnumerateCallFrames(JsonArrayBuilder &callFrames, PtThread thread, 140 const std::function<void(const FrameInfoHandler &)> &enumerateFrames); 141 void AddCallFrameInfo(JsonArrayBuilder &callFrames, const CallFrameInfo &callFrameInfo, 142 const std::vector<Scope> &scopeChain, PtThread thread, 143 const std::optional<RemoteObject> &objThis); 144 Expected<std::unique_ptr<UrlBreakpointResponse>, std::string> SetBreakpointByUrl( 145 const std::string &sessionId, const UrlBreakpointRequest &breakpointRequest, 146 const std::function<SetBreakpointHandler> &handler); 147 void AddLocations(UrlBreakpointResponse &response, const std::set<std::string_view> &sourceFiles, size_t lineNumber, 148 PtThread thread); 149 static void AddHitBreakpoints(JsonArrayBuilder &hitBreakpointsBuilder, 150 const std::vector<BreakpointId> &hitBreakpoints); 151 static std::string GetExecutionContextUniqueId(const PtThread &thread); 152 153 Server &server_; 154 SessionManager sessionManager_; 155 SourceManager sourceManager_; 156 }; 157 } // namespace ark::tooling::inspector 158 159 #endif // PANDA_TOOLING_INSPECTOR_INSPECTOR_SERVER_H 160