• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 ECMASCRIPT_DEBUGGER_JS_DEBUG_INTERFACE_H
17 #define ECMASCRIPT_DEBUGGER_JS_DEBUG_INTERFACE_H
18 
19 #include <string_view>
20 
21 #include "ecmascript/debugger/js_pt_location.h"
22 #include "ecmascript/dfx/stackinfo/async_stack_trace.h"
23 #include "ecmascript/napi/include/jsnapi.h"
24 namespace panda::ecmascript {
25 class Method;
26 
27 namespace tooling {
28 struct JSPtStepRange {
29     uint32_t startBcOffset {0};
30     uint32_t endBcOffset {0};
31 };
32 
33 enum PauseReason {
34     AMBIGUOUS,
35     ASSERT,
36     DEBUGCOMMAND,
37     DOM,
38     EVENTLISTENER,
39     EXCEPTION,
40     INSTRUMENTATION,
41     OOM,
42     OTHER,
43     PROMISEREJECTION,
44     XHR,
45     BREAK_ON_START,
46     STEP, // single step
47     DEBUGGERSTMT, // debugger stmt
48     NATIVE_OUT,
49     SYMBOL
50 };
51 
52 class PtHooks {
53 public:
54     PtHooks() = default;
55 
56     /**
57      * \brief called by the ecmavm when the next statement being executed is debugger statement.
58      * Thread where debugger statement hits is paused until continue or step event being received
59      * @param thread Identifier of the thread where debugger statement hits. Now the callback is called
60      * in the same thread
61      * @param location debugger statement location
62      */
63     virtual void DebuggerStmt(const JSPtLocation &location) = 0;
64 
65     /**
66      * \brief called by the ecmavm when breakpoint hits. Thread where breakpoint hits is stopped until
67      * continue or step event will be received
68      * @param thread Identifier of the thread where breakpoint hits. Now the callback is called in the same
69      * thread
70      * @param location Breakpoint location
71      */
72     virtual void Breakpoint(const JSPtLocation &location) = 0;
73 
74     /**
75      * \brief called by the ecmavm when panda file is loaded
76      * @param pandaFileName Path to panda file that is loaded
77      */
78     virtual void LoadModule(std::string_view pandaFileName, std::string_view entryPoint) = 0;
79 
80     /**
81      * \brief called by the ecmavm when virtual machine start initialization
82      */
83     virtual void VmStart() = 0;
84 
85     /**
86      * \brief called by the ecmavm when virtual machine death
87      */
88     virtual void VmDeath() = 0;
89 
90     virtual void Exception(const JSPtLocation &location) = 0;
91 
92     virtual bool SingleStep(const JSPtLocation &location) = 0;
93 
94     virtual void NativeCalling(const void *nativeAddress) = 0;
95 
96     virtual void NativeReturn(const void *nativeAddress) = 0;
97 
98     virtual bool NativeOut() = 0;
99 
100     virtual void SendableMethodEntry(JSHandle<Method> method) = 0;
101 
102     virtual void DisableFirstTimeFlag() = 0;
103 
104     virtual void GenerateAsyncFrames(std::shared_ptr<AsyncStack> asyncStack, bool skipTopFrame) = 0;
105 
106     /**
107     * \brief called by the ecmavm when symbolic breakpoint hits. Thread where symbolic breakpoint hits is stopped until
108     * continue or step event will be received
109     */
110     virtual void HitSymbolicBreakpoint() = 0;
111 
112     virtual const std::unordered_set<std::string> &GetAllRecordNames() const = 0;
113 
114     virtual ~PtHooks() = default;
115 
116     NO_COPY_SEMANTIC(PtHooks);
117     NO_MOVE_SEMANTIC(PtHooks);
118 };
119 
120 class JSDebugInterface {
121 public:
122     JSDebugInterface() = default;
123 
124     /**
125      * \brief handle debugger statement event
126      * @param method Current method
127      * @param bcOffset Current bytecode offset
128      */
129     virtual bool HandleDebuggerStmt(JSHandle<Method> method, uint32_t bcOffset) = 0;
130 
131     /**
132      * \brief Register debug hooks in the ecmavm
133      * @param hooks Pointer to object that implements PtHooks interface
134      */
135     virtual void RegisterHooks(PtHooks *hooks) = 0;
136 
137     /**
138      * \brief Unregister debug hooks in the ecmavm
139      */
140     virtual void UnregisterHooks() = 0;
141 
142     /**
143      * \brief Set breakpoint to \param location with an optional \param condition
144      * @param location Breakpoint location
145      * @param condition Optional condition
146      * @return Error if any errors occur
147      */
148     virtual bool SetBreakpoint(const JSPtLocation &location, Local<FunctionRef> condFuncRef) = 0;
149 
150     /**
151      * \brief Remove breakpoint from \param location
152      * @param location Breakpoint location
153      * @return Error if any errors occur
154      */
155     virtual bool RemoveBreakpoint(const JSPtLocation &location) = 0;
156 
157     /**
158      * \brief Remove breakpoints specified by url
159      * @param url file url
160      * @return Error if any errors occur
161      */
162     virtual bool RemoveBreakpointsByUrl(const std::string &url) = 0;
163 
164     /**
165      * \brief Remove all breakpoints
166      */
167     virtual void RemoveAllBreakpoints() = 0;
168 
169     virtual ~JSDebugInterface() = default;
170 
171     NO_COPY_SEMANTIC(JSDebugInterface);
172     NO_MOVE_SEMANTIC(JSDebugInterface);
173 };
174 }  // namespace tooling
175 }  // namespace panda::ecmascript
176 
177 #endif  // ECMASCRIPT_DEBUGGER_JS_DEBUG_INTERFACE_H
178