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