• 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_H
17 #define PANDA_TOOLING_INSPECTOR_INSPECTOR_H
18 
19 #include "debug_info_cache.h"
20 #include "inspector_server.h"
21 #include "debuggable_thread.h"
22 #include "types/numeric_id.h"
23 
24 #include "tooling/debug_interface.h"
25 #include "tooling/inspector/object_repository.h"
26 #include "tooling/inspector/types/pause_on_exceptions_state.h"
27 #include "tooling/inspector/types/property_descriptor.h"
28 #include "tooling/inspector/types/remote_object.h"
29 #include "tooling/pt_thread.h"
30 
31 #include <array>
32 #include <cstddef>
33 #include <functional>
34 #include <memory>
35 #include <optional>
36 #include <set>
37 #include <string_view>
38 #include <thread>
39 #include <vector>
40 
41 namespace ark::tooling {
42 class DebugInterface;
43 
44 namespace inspector {
45 // NOLINTNEXTLINE(fuchsia-virtual-inheritance)
46 class Server;
47 
48 class Inspector final : public PtHooks {
49 public:
50     Inspector(Server &server, DebugInterface &debugger, bool breakOnStart);
51     ~Inspector() override;
52 
53     NO_COPY_SEMANTIC(Inspector);
54     NO_MOVE_SEMANTIC(Inspector);
55 
56     void ConsoleCall(PtThread thread, ConsoleCallType type, uint64_t timestamp,
57                      const PandaVector<TypedValue> &arguments) override;
58     void Exception(PtThread thread, Method *method, const PtLocation &location, ObjectHeader *exception,
59                    Method *catchMethod, const PtLocation &catchLocation) override;
60     void FramePop(PtThread thread, Method *method, bool wasPoppedByException) override;
61     void MethodEntry(PtThread thread, Method *method) override;
62     void LoadModule(std::string_view fileName) override;
63     void SingleStep(PtThread thread, Method *method, const PtLocation &location) override;
64     void ThreadStart(PtThread thread) override;
65     void ThreadEnd(PtThread thread) override;
66 
67 private:
68     void RuntimeEnable(PtThread thread);
69 
70     void RunIfWaitingForDebugger(PtThread thread);
71 
72     void Pause(PtThread thread);
73     void Continue(PtThread thread);
74 
75     void SetBreakpointsActive(PtThread thread, bool active);
76     std::set<size_t> GetPossibleBreakpoints(std::string_view sourceFile, size_t startLine, size_t endLine,
77                                             bool restrictToFunction);
78     std::optional<BreakpointId> SetBreakpoint(PtThread thread,
79                                               const std::function<bool(std::string_view)> &sourceFilesFilter,
80                                               size_t lineNumber, std::set<std::string_view> &sourceFiles);
81     void RemoveBreakpoint(PtThread thread, BreakpointId id);
82 
83     void SetPauseOnExceptions(PtThread thread, PauseOnExceptionsState state);
84 
85     void StepInto(PtThread thread);
86     void StepOver(PtThread thread);
87     void StepOut(PtThread thread);
88     void ContinueToLocation(PtThread thread, std::string_view sourceFile, size_t lineNumber);
89 
90     void RestartFrame(PtThread thread, FrameId frameId);
91 
92     std::vector<PropertyDescriptor> GetProperties(PtThread thread, RemoteObjectId objectId, bool generatePreview);
93     std::string GetSourceCode(std::string_view sourceFile);
94 
95     void DebuggableThreadPostSuspend(PtThread thread, ObjectRepository &objectRepository,
96                                      const std::vector<BreakpointId> &hitBreakpoints, ObjectHeader *exception);
97 
98     void NotifyExecutionEnded();
99 
100 private:
101     bool breakOnStart_;
102 
103     os::memory::RWLock debuggerEventsLock_;
104     bool connecting_ {false};  // Should be accessed only from the server thread
105 
106     InspectorServer inspectorServer_;  // NOLINT(misc-non-private-member-variables-in-classes)
107     DebugInterface &debugger_;
108     DebugInfoCache debugInfoCache_;
109     std::map<PtThread, DebuggableThread> threads_;
110 
111     std::thread serverThread_;
112 };
113 }  // namespace inspector
114 }  // namespace ark::tooling
115 
116 #endif  // PANDA_TOOLING_INSPECTOR_INSPECTOR_H
117