• 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::tooling {
25 struct JSPtStepRange {
26     uint32_t startBcOffset {0};
27     uint32_t endBcOffset {0};
28 };
29 
30 enum PauseReason {
31     AMBIGUOUS,
32     ASSERT,
33     DEBUGCOMMAND,
34     DOM,
35     EVENTLISTENER,
36     EXCEPTION,
37     INSTRUMENTATION,
38     OOM,
39     OTHER,
40     PROMISEREJECTION,
41     XHR,
42     BREAK_ON_START
43 };
44 
45 class PtHooks {
46 public:
47     PtHooks() = default;
48 
49     /**
50      * \brief called by the ecmavm when the next statement being executed is debugger statement.
51      * Thread where debugger statement hits is paused until continue or step event being received
52      * @param thread Identifier of the thread where debugger statement hits. Now the callback is called
53      * in the same thread
54      * @param location debugger statement location
55      */
56     virtual void DebuggerStmt(const JSPtLocation &location) = 0;
57 
58     /**
59      * \brief called by the ecmavm when breakpoint hits. Thread where breakpoint hits is stopped until
60      * continue or step event will be received
61      * @param thread Identifier of the thread where breakpoint hits. Now the callback is called in the same
62      * thread
63      * @param location Breakpoint location
64      */
65     virtual void Breakpoint(const JSPtLocation &location) = 0;
66 
67     /**
68      * \brief called by the ecmavm when panda file is loaded
69      * @param pandaFileName Path to panda file that is loaded
70      */
71     virtual void LoadModule(std::string_view pandaFileName, std::string_view entryPoint) = 0;
72 
73     /**
74      * \brief called by the ecmavm when virtual machine start initialization
75      */
76     virtual void VmStart() = 0;
77 
78     /**
79      * \brief called by the ecmavm when virtual machine death
80      */
81     virtual void VmDeath() = 0;
82 
83     virtual void Exception(const JSPtLocation &location) = 0;
84 
85     virtual bool SingleStep(const JSPtLocation &location) = 0;
86 
87     virtual void NativeCalling(const void *nativeAddress) = 0;
88 
89     virtual ~PtHooks() = default;
90 
91     NO_COPY_SEMANTIC(PtHooks);
92     NO_MOVE_SEMANTIC(PtHooks);
93 };
94 
95 class JSDebugInterface {
96 public:
97     JSDebugInterface() = default;
98 
99     /**
100      * \brief handle debugger statement event
101      * @param method Current method
102      * @param bcOffset Current bytecode offset
103      */
104     virtual bool HandleDebuggerStmt(JSHandle<Method> method, uint32_t bcOffset) = 0;
105 
106     /**
107      * \brief Register debug hooks in the ecmavm
108      * @param hooks Pointer to object that implements PtHooks interface
109      */
110     virtual void RegisterHooks(PtHooks *hooks) = 0;
111 
112     /**
113      * \brief Unregister debug hooks in the ecmavm
114      */
115     virtual void UnregisterHooks() = 0;
116 
117     /**
118      * \brief Set breakpoint to \param location with an optional \param condition
119      * @param location Breakpoint location
120      * @param condition Optional condition
121      * @return Error if any errors occur
122      */
123     virtual bool SetBreakpoint(const JSPtLocation &location, Local<FunctionRef> condFuncRef) = 0;
124 
125     /**
126      * \brief Remove breakpoint from \param location
127      * @param location Breakpoint location
128      * @return Error if any errors occur
129      */
130     virtual bool RemoveBreakpoint(const JSPtLocation &location) = 0;
131 
132     /**
133      * \brief Remove all breakpoints
134      */
135     virtual void RemoveAllBreakpoints() = 0;
136 
137     virtual ~JSDebugInterface() = default;
138 
139     NO_COPY_SEMANTIC(JSDebugInterface);
140     NO_MOVE_SEMANTIC(JSDebugInterface);
141 };
142 }  // namespace panda::ecmascript::tooling
143 
144 #endif  // ECMASCRIPT_DEBUGGER_JS_DEBUG_INTERFACE_H
145