• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 panda::tooling::inspector {
40 class Server;  // NOLINT(fuchsia-virtual-inheritance)
41 
42 class InspectorServer final {
43 public:
44     using FrameInfoHandler =
45         std::function<void(FrameId, std::string_view, std::string_view, size_t, const std::vector<Scope> &)>;
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 id, 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 CallTargetAttachedToTarget(PtThread thread);
69     void CallTargetDetachedFromTarget(PtThread thread);
70 
71     void OnCallDebuggerContinueToLocation(std::function<void(PtThread, std::string_view, size_t)> &&handler);
72     void OnCallDebuggerGetPossibleBreakpoints(
73         std::function<std::set<size_t>(std::string_view, size_t, size_t, bool)> &&handler);
74     void OnCallDebuggerGetScriptSource(std::function<std::string(std::string_view)> &&handler);
75     void OnCallDebuggerPause(std::function<void(PtThread)> &&handler);
76     void OnCallDebuggerRemoveBreakpoint(std::function<void(PtThread, BreakpointId)> &&handler);
77     void OnCallDebuggerRestartFrame(std::function<void(PtThread, FrameId)> &&handler);
78     void OnCallDebuggerResume(std::function<void(PtThread)> &&handler);
79     void OnCallDebuggerSetBreakpoint(
80         std::function<std::optional<BreakpointId>(PtThread, const std::function<bool(std::string_view)> &, size_t,
81                                                   std::set<std::string_view> &)> &&handler);
82     void OnCallDebuggerSetBreakpointByUrl(
83         std::function<std::optional<BreakpointId>(PtThread, const std::function<bool(std::string_view)> &, size_t,
84                                                   std::set<std::string_view> &)> &&handler);
85     void OnCallDebuggerSetBreakpointsActive(std::function<void(PtThread, bool)> &&handler);
86     void OnCallDebuggerSetPauseOnExceptions(std::function<void(PtThread, PauseOnExceptionsState)> &&handler);
87     void OnCallDebuggerStepInto(std::function<void(PtThread)> &&handler);
88     void OnCallDebuggerStepOut(std::function<void(PtThread)> &&handler);
89     void OnCallDebuggerStepOver(std::function<void(PtThread)> &&handler);
90 
91     void OnCallRuntimeEnable(std::function<void(PtThread)> &&handler);
92     void OnCallRuntimeGetProperties(
93         std::function<std::vector<PropertyDescriptor>(PtThread, RemoteObjectId, bool)> &&handler);
94     void OnCallRuntimeRunIfWaitingForDebugger(std::function<void(PtThread)> &&handler);
95 
96 private:
97     struct CallFrameInfo {
98         FrameId frameId;
99         std::string_view sourceFile;
100         std::string_view methodName;
101         size_t lineNumber;
102     };
103 
104     void SendTargetAttachedToTarget(const std::string &sessionId);
105     void EnumerateCallFrames(JsonArrayBuilder &callFrames, PtThread thread,
106                              const std::function<void(const FrameInfoHandler &)> &enumerateFrames);
107     void AddCallFrameInfo(JsonArrayBuilder &callFrames, const CallFrameInfo &callFrameInfo,
108                           const std::vector<Scope> &scopeChain, PtThread thread);
109     static void AddHitBreakpoints(JsonArrayBuilder &hitBreakpointsBuilder,
110                                   const std::vector<BreakpointId> &hitBreakpoints);
111     void AddBreakpointByUrlLocations(JsonArrayBuilder &locations, const std::set<std::string_view> &sourceFiles,
112                                      size_t lineNumber, PtThread thread);
113 
114     Server &server_;
115     SessionManager sessionManager_;
116     SourceManager sourceManager_;
117 };
118 }  // namespace panda::tooling::inspector
119 
120 #endif  // PANDA_TOOLING_INSPECTOR_INSPECTOR_SERVER_H
121