1 /* 2 * Copyright (c) 2021-2022 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_TOOLING_BACKEND_DEBUGGER_API_H 17 #define ECMASCRIPT_TOOLING_BACKEND_DEBUGGER_API_H 18 19 #include <functional> 20 21 #include "ecmascript/common.h" 22 #include "ecmascript/jspandafile/scope_info_extractor.h" 23 #include "ecmascript/napi/include/jsnapi.h" 24 #include "ecmascript/lexical_env.h" 25 #include "ecmascript/tooling/backend/js_debugger_interface.h" 26 27 namespace panda { 28 namespace ecmascript { 29 class InterpretedFrameHandler; 30 class EcmaVM; 31 class JSMethod; 32 class JSThread; 33 namespace tooling { 34 class JSDebugger; 35 } 36 } // ecmascript 37 } // panda 38 39 namespace panda::ecmascript::tooling { 40 enum StackState { 41 CONTINUE, 42 FAILED, 43 SUCCESS, 44 }; 45 46 class PUBLIC_API DebuggerApi { 47 public: 48 // InterpretedFrameHandler 49 static uint32_t GetStackDepth(const EcmaVM *ecmaVm); 50 static std::shared_ptr<InterpretedFrameHandler> NewFrameHandler(const EcmaVM *ecmaVm); 51 static bool StackWalker(const EcmaVM *ecmaVm, std::function<StackState(const InterpretedFrameHandler *)> func); 52 static uint32_t GetBytecodeOffset(const EcmaVM *ecmaVm); 53 static JSMethod *GetMethod(const EcmaVM *ecmaVm); 54 static uint32_t GetBytecodeOffset(const InterpretedFrameHandler *frameHandler); 55 static JSMethod *GetMethod(const InterpretedFrameHandler *frameHandler); 56 static JSTaggedValue GetEnv(const InterpretedFrameHandler *frameHandler); 57 static JSTaggedType *GetSp(const InterpretedFrameHandler *frameHandler); 58 static int32_t GetVregIndex(const InterpretedFrameHandler *frameHandler, std::string_view name); 59 static Local<JSValueRef> GetVRegValue(const EcmaVM *ecmaVm, 60 const InterpretedFrameHandler *frameHandler, size_t index); 61 static void SetVRegValue(InterpretedFrameHandler *frameHandler, size_t index, Local<JSValueRef> value); 62 63 static Local<JSValueRef> GetProperties(const EcmaVM *ecmaVm, const InterpretedFrameHandler *frameHandler, 64 int32_t level, uint32_t slot); 65 static void SetProperties(const EcmaVM *vm, const InterpretedFrameHandler *frameHandler, int32_t level, 66 uint32_t slot, Local<JSValueRef> value); 67 static std::pair<int32_t, uint32_t> GetLevelSlot(const InterpretedFrameHandler *frameHandler, 68 std::string_view name); 69 static Local<JSValueRef> GetGlobalValue(const EcmaVM *vm, Local<StringRef> name); 70 static bool SetGlobalValue(const EcmaVM *vm, Local<StringRef> name, Local<JSValueRef> value); 71 72 // JSThread 73 static Local<JSValueRef> GetAndClearException(const EcmaVM *ecmaVm); 74 static void SetException(const EcmaVM *ecmaVm, Local<JSValueRef> exception); 75 static void ClearException(const EcmaVM *ecmaVm); 76 77 // NumberHelper 78 static double StringToDouble(const uint8_t *start, const uint8_t *end, uint8_t radix); 79 80 // JSDebugger 81 static JSDebugger *CreateJSDebugger(const EcmaVM *ecmaVm); 82 static void DestroyJSDebugger(JSDebugger *debugger); 83 static void RegisterHooks(JSDebugger *debugger, PtHooks *hooks); 84 static bool SetBreakpoint(JSDebugger *debugger, const JSPtLocation &location, 85 Local<FunctionRef> condFuncRef); 86 static bool RemoveBreakpoint(JSDebugger *debugger, const JSPtLocation &location); 87 static void HandleUncaughtException(const EcmaVM *ecmaVm, std::string &message); 88 static Local<JSValueRef> EvaluateViaFuncCall(EcmaVM *ecmaVm, Local<FunctionRef> funcRef, 89 std::shared_ptr<InterpretedFrameHandler> &frameHandler); 90 static Local<FunctionRef> GenerateFuncFromBuffer(const EcmaVM *ecmaVm, const void *buffer, size_t size); 91 92 // JSMethod 93 static std::string ParseFunctionName(const JSMethod *method); 94 }; 95 } // namespace panda::ecmascript::tooling 96 97 #endif // ECMASCRIPT_TOOLING_BACKEND_DEBUGGER_API_H 98