• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022-2024 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 "session_manager.h"
20 #include "source_manager.h"
21 #include "types/numeric_id.h"
22 
23 #include "console_call_type.h"
24 #include "tooling/inspector/types/pause_on_exceptions_state.h"
25 #include "tooling/inspector/types/property_descriptor.h"
26 #include "tooling/inspector/types/remote_object.h"
27 #include "tooling/inspector/types/scope.h"
28 #include "tooling/pt_thread.h"
29 
30 #include <cstddef>
31 #include <cstdint>
32 #include <deque>
33 #include <functional>
34 #include <optional>
35 #include <set>
36 #include <string_view>
37 #include <vector>
38 
39 namespace ark::tooling::inspector {
40 class Server;  // NOLINT(fuchsia-virtual-inheritance)
41 
42 class InspectorServer final {
43 public:
44     using FrameInfoHandler = std::function<void(FrameId, std::string_view, std::string_view, size_t,
45                                                 const std::vector<Scope> &, const std::optional<RemoteObject> &)>;
46 
47     explicit InspectorServer(Server &server);
48     ~InspectorServer() = default;
49 
50     NO_COPY_SEMANTIC(InspectorServer);
51     NO_MOVE_SEMANTIC(InspectorServer);
52 
53     void Kill();
54     void Run();
55 
56     void OnValidate(std::function<void()> &&handler);
57     void OnOpen(std::function<void()> &&handler);
58     void OnFail(std::function<void()> &&handler);
59 
60     void CallDebuggerPaused(PtThread thread, const std::vector<BreakpointId> &hitBreakpoints,
61                             const std::optional<RemoteObject> &exception,
62                             const std::function<void(const FrameInfoHandler &)> &enumerateFrames);
63     void CallDebuggerResumed(PtThread thread);
64     void CallDebuggerScriptParsed(PtThread thread, ScriptId scriptId, std::string_view sourceFile);
65     void CallRuntimeConsoleApiCalled(PtThread thread, ConsoleCallType type, uint64_t timestamp,
66                                      const std::vector<RemoteObject> &arguments);
67     void CallRuntimeExecutionContextCreated(PtThread thread);
68     void CallRuntimeExecutionContextsCleared();
69     void CallTargetAttachedToTarget(PtThread thread);
70     void CallTargetDetachedFromTarget(PtThread thread);
71 
72     void OnCallDebuggerContinueToLocation(std::function<void(PtThread, std::string_view, size_t)> &&handler);
73     void OnCallDebuggerGetPossibleBreakpoints(
74         std::function<std::set<size_t>(std::string_view, size_t, size_t, bool)> &&handler);
75     void OnCallDebuggerGetScriptSource(std::function<std::string(std::string_view)> &&handler);
76     void OnCallDebuggerPause(std::function<void(PtThread)> &&handler);
77     void OnCallDebuggerRemoveBreakpoint(std::function<void(PtThread, BreakpointId)> &&handler);
78     void OnCallDebuggerRestartFrame(std::function<void(PtThread, FrameId)> &&handler);
79     void OnCallDebuggerResume(std::function<void(PtThread)> &&handler);
80     void OnCallDebuggerSetBreakpoint(
81         std::function<std::optional<BreakpointId>(PtThread, const std::function<bool(std::string_view)> &, size_t,
82                                                   std::set<std::string_view> &)> &&handler);
83     void OnCallDebuggerSetBreakpointByUrl(
84         std::function<std::optional<BreakpointId>(PtThread, const std::function<bool(std::string_view)> &, size_t,
85                                                   std::set<std::string_view> &)> &&handler);
86     void OnCallDebuggerSetBreakpointsActive(std::function<void(PtThread, bool)> &&handler);
87     void OnCallDebuggerSetPauseOnExceptions(std::function<void(PtThread, PauseOnExceptionsState)> &&handler);
88     void OnCallDebuggerStepInto(std::function<void(PtThread)> &&handler);
89     void OnCallDebuggerStepOut(std::function<void(PtThread)> &&handler);
90     void OnCallDebuggerStepOver(std::function<void(PtThread)> &&handler);
91 
92     void OnCallRuntimeEnable(std::function<void(PtThread)> &&handler);
93     void OnCallRuntimeGetProperties(
94         std::function<std::vector<PropertyDescriptor>(PtThread, RemoteObjectId, bool)> &&handler);
95     void OnCallRuntimeRunIfWaitingForDebugger(std::function<void(PtThread)> &&handler);
96 
97 private:
98     struct CallFrameInfo {
99         FrameId frameId;
100         std::string_view sourceFile;
101         std::string_view methodName;
102         size_t lineNumber;
103     };
104 
105     void SendTargetAttachedToTarget(const std::string &sessionId);
106     void EnumerateCallFrames(JsonArrayBuilder &callFrames, PtThread thread,
107                              const std::function<void(const FrameInfoHandler &)> &enumerateFrames);
108     void AddCallFrameInfo(JsonArrayBuilder &callFrames, const CallFrameInfo &callFrameInfo,
109                           const std::vector<Scope> &scopeChain, PtThread thread,
110                           const std::optional<RemoteObject> &objThis);
111     void AddBreakpointByUrlLocations(JsonArrayBuilder &locations, const std::set<std::string_view> &sourceFiles,
112                                      size_t lineNumber, PtThread thread);
113     static void AddHitBreakpoints(JsonArrayBuilder &hitBreakpointsBuilder,
114                                   const std::vector<BreakpointId> &hitBreakpoints);
115     static std::string GetExecutionContextUniqueId(const PtThread &thread);
116 
117     Server &server_;
118     SessionManager sessionManager_;
119     SourceManager sourceManager_;
120 };
121 }  // namespace ark::tooling::inspector
122 
123 #endif  // PANDA_TOOLING_INSPECTOR_INSPECTOR_SERVER_H
124