• 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/napi/include/jsnapi.h"
22 #include "ecmascript/debugger/js_pt_location.h"
23 
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 };
50 
51 class PtHooks {
52 public:
53     PtHooks() = default;
54 
55     /**
56      * \brief called by the ecmavm when the next statement being executed is debugger statement.
57      * Thread where debugger statement hits is paused until continue or step event being received
58      * @param thread Identifier of the thread where debugger statement hits. Now the callback is called
59      * in the same thread
60      * @param location debugger statement location
61      */
62     virtual void DebuggerStmt(const JSPtLocation &location) = 0;
63 
64     /**
65      * \brief called by the ecmavm when breakpoint hits. Thread where breakpoint hits is stopped until
66      * continue or step event will be received
67      * @param thread Identifier of the thread where breakpoint hits. Now the callback is called in the same
68      * thread
69      * @param location Breakpoint location
70      */
71     virtual void Breakpoint(const JSPtLocation &location) = 0;
72 
73     /**
74      * \brief called by the ecmavm when panda file is loaded
75      * @param pandaFileName Path to panda file that is loaded
76      */
77     virtual void LoadModule(std::string_view pandaFileName, std::string_view entryPoint) = 0;
78 
79     /**
80      * \brief called by the ecmavm when virtual machine start initialization
81      */
82     virtual void VmStart() = 0;
83 
84     /**
85      * \brief called by the ecmavm when virtual machine death
86      */
87     virtual void VmDeath() = 0;
88 
89     virtual void Exception(const JSPtLocation &location) = 0;
90 
91     virtual bool SingleStep(const JSPtLocation &location) = 0;
92 
93     virtual void NativeCalling(const void *nativeAddress) = 0;
94 
95     virtual void NativeReturn(const void *nativeAddress) = 0;
96 
97     virtual bool NativeOut() = 0;
98 
99     virtual void SendableMethodEntry(JSHandle<Method> method) = 0;
100 
101     virtual void DisableFirstTimeFlag() = 0;
102 
103     virtual ~PtHooks() = default;
104 
105     NO_COPY_SEMANTIC(PtHooks);
106     NO_MOVE_SEMANTIC(PtHooks);
107 };
108 
109 class JSDebugInterface {
110 public:
111     JSDebugInterface() = default;
112 
113     /**
114      * \brief handle debugger statement event
115      * @param method Current method
116      * @param bcOffset Current bytecode offset
117      */
118     virtual bool HandleDebuggerStmt(JSHandle<Method> method, uint32_t bcOffset) = 0;
119 
120     /**
121      * \brief Register debug hooks in the ecmavm
122      * @param hooks Pointer to object that implements PtHooks interface
123      */
124     virtual void RegisterHooks(PtHooks *hooks) = 0;
125 
126     /**
127      * \brief Unregister debug hooks in the ecmavm
128      */
129     virtual void UnregisterHooks() = 0;
130 
131     /**
132      * \brief Set breakpoint to \param location with an optional \param condition
133      * @param location Breakpoint location
134      * @param condition Optional condition
135      * @return Error if any errors occur
136      */
137     virtual bool SetBreakpoint(const JSPtLocation &location, Local<FunctionRef> condFuncRef) = 0;
138 
139     /**
140      * \brief Remove breakpoint from \param location
141      * @param location Breakpoint location
142      * @return Error if any errors occur
143      */
144     virtual bool RemoveBreakpoint(const JSPtLocation &location) = 0;
145 
146     /**
147      * \brief Remove breakpoints specified by url
148      * @param url file url
149      * @return Error if any errors occur
150      */
151     virtual bool RemoveBreakpointsByUrl(const std::string &url) = 0;
152 
153     /**
154      * \brief Remove all breakpoints
155      */
156     virtual void RemoveAllBreakpoints() = 0;
157 
158     virtual ~JSDebugInterface() = default;
159 
160     NO_COPY_SEMANTIC(JSDebugInterface);
161     NO_MOVE_SEMANTIC(JSDebugInterface);
162 };
163 }  // namespace tooling
164 }  // namespace panda::ecmascript
165 
166 #endif  // ECMASCRIPT_DEBUGGER_JS_DEBUG_INTERFACE_H
167