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
24namespace panda::ecmascript {
25class Method;
26
27namespace tooling {
28struct JSPtStepRange {
29    uint32_t startBcOffset {0};
30    uint32_t endBcOffset {0};
31};
32
33enum 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
51class PtHooks {
52public:
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 ~PtHooks() = default;
102
103    NO_COPY_SEMANTIC(PtHooks);
104    NO_MOVE_SEMANTIC(PtHooks);
105};
106
107class JSDebugInterface {
108public:
109    JSDebugInterface() = default;
110
111    /**
112     * \brief handle debugger statement event
113     * @param method Current method
114     * @param bcOffset Current bytecode offset
115     */
116    virtual bool HandleDebuggerStmt(JSHandle<Method> method, uint32_t bcOffset) = 0;
117
118    /**
119     * \brief Register debug hooks in the ecmavm
120     * @param hooks Pointer to object that implements PtHooks interface
121     */
122    virtual void RegisterHooks(PtHooks *hooks) = 0;
123
124    /**
125     * \brief Unregister debug hooks in the ecmavm
126     */
127    virtual void UnregisterHooks() = 0;
128
129    /**
130     * \brief Set breakpoint to \param location with an optional \param condition
131     * @param location Breakpoint location
132     * @param condition Optional condition
133     * @return Error if any errors occur
134     */
135    virtual bool SetBreakpoint(const JSPtLocation &location, Local<FunctionRef> condFuncRef) = 0;
136
137    /**
138     * \brief Remove breakpoint from \param location
139     * @param location Breakpoint location
140     * @return Error if any errors occur
141     */
142    virtual bool RemoveBreakpoint(const JSPtLocation &location) = 0;
143
144    /**
145     * \brief Remove breakpoints specified by url
146     * @param url file url
147     * @return Error if any errors occur
148     */
149    virtual bool RemoveBreakpointsByUrl(const std::string &url) = 0;
150
151    /**
152     * \brief Remove all breakpoints
153     */
154    virtual void RemoveAllBreakpoints() = 0;
155
156    virtual ~JSDebugInterface() = default;
157
158    NO_COPY_SEMANTIC(JSDebugInterface);
159    NO_MOVE_SEMANTIC(JSDebugInterface);
160};
161}  // namespace tooling
162}  // namespace panda::ecmascript
163
164#endif  // ECMASCRIPT_DEBUGGER_JS_DEBUG_INTERFACE_H
165