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_DEBUGGER_API_H 17 #define ECMASCRIPT_DEBUGGER_DEBUGGER_API_H 18 19 #include <functional> 20 21 #include "ecmascript/common.h" 22 #include "ecmascript/debugger/js_debugger_interface.h" 23 #include "ecmascript/debugger/js_pt_method.h" 24 #include "ecmascript/jspandafile/scope_info_extractor.h" 25 #include "ecmascript/napi/include/jsnapi.h" 26 #include "ecmascript/lexical_env.h" 27 28 namespace panda { 29 namespace ecmascript { 30 class EcmaVM; 31 class FrameHandler; 32 class JSThread; 33 class Method; 34 class ModuleManager; 35 class NameDictionary; 36 class SourceTextModule; 37 class DebugInfoExtractor; 38 namespace tooling { 39 class JSDebugger; 40 } 41 } // ecmascript 42 } // panda 43 44 namespace panda::ecmascript::tooling { 45 enum StackState { 46 CONTINUE, 47 FAILED, 48 SUCCESS, 49 }; 50 51 enum class ArkInternalValueType {None, Entry, Scope, ScopeList}; 52 class PUBLIC_API DebuggerApi { 53 public: 54 // FrameHandler 55 static uint32_t GetStackDepth(const EcmaVM *ecmaVm); 56 static std::shared_ptr<FrameHandler> NewFrameHandler(const EcmaVM *ecmaVm); 57 static bool StackWalker(const EcmaVM *ecmaVm, std::function<StackState(const FrameHandler *)> func); 58 59 static uint32_t GetBytecodeOffset(const EcmaVM *ecmaVm); 60 static uint32_t GetBytecodeOffset(const FrameHandler *frameHandler); 61 static std::unique_ptr<PtMethod> GetMethod(const EcmaVM *ecmaVm); 62 static Method *GetMethod(const FrameHandler *frameHandler); 63 static bool IsNativeMethod(const EcmaVM *ecmaVm); 64 static bool IsNativeMethod(const FrameHandler *frameHandler); 65 static JSPandaFile *GetJSPandaFile(const EcmaVM *ecmaVm); 66 67 static JSTaggedValue GetEnv(const FrameHandler *frameHandler); 68 static JSTaggedType *GetSp(const FrameHandler *frameHandler); 69 static int32_t GetVregIndex(const FrameHandler *frameHandler, std::string_view name); 70 static Local<JSValueRef> GetVRegValue(const EcmaVM *ecmaVm, 71 const FrameHandler *frameHandler, size_t index); 72 static void SetVRegValue(FrameHandler *frameHandler, size_t index, Local<JSValueRef> value); 73 74 static Local<JSValueRef> GetProperties(const EcmaVM *ecmaVm, const FrameHandler *frameHandler, 75 int32_t level, uint32_t slot); 76 static void SetProperties(const EcmaVM *ecmaVm, const FrameHandler *frameHandler, int32_t level, 77 uint32_t slot, Local<JSValueRef> value); 78 static std::pair<int32_t, uint32_t> GetLevelSlot(const FrameHandler *frameHandler, std::string_view name); 79 static Local<JSValueRef> GetGlobalValue(const EcmaVM *ecmaVm, Local<StringRef> name); 80 static bool SetGlobalValue(const EcmaVM *ecmaVm, Local<StringRef> name, Local<JSValueRef> value); 81 82 // JSThread 83 static Local<JSValueRef> GetAndClearException(const EcmaVM *ecmaVm); 84 static JSTaggedValue GetCurrentModule(const EcmaVM *ecmaVm); 85 static JSHandle<JSTaggedValue> GetImportModule(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 86 std::string &name); 87 static int32_t GetModuleVariableIndex(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 88 std::string &name); 89 static int32_t GetRequestModuleIndex(const EcmaVM *ecmaVm, const JSTaggedValue moduleRequest, 90 const JSHandle<JSTaggedValue> ¤tModule); 91 static Local<JSValueRef> GetExportVariableValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 92 std::string &name); 93 static bool SetExportVariableValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 94 std::string &name, Local<JSValueRef> value); 95 static Local<JSValueRef> GetModuleValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 96 std::string &name); 97 static bool SetModuleValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 98 std::string &name, Local<JSValueRef> value); 99 static void InitializeExportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj, 100 const JSHandle<JSTaggedValue> ¤tModule); 101 static void GetLocalExportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj, 102 const JSHandle<JSTaggedValue> ¤tModule, bool isImportStar); 103 static void GetIndirectExportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj, 104 const JSHandle<JSTaggedValue> ¤tModule); 105 static void GetImportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj, 106 const JSHandle<JSTaggedValue> ¤tModule); 107 static void SetException(const EcmaVM *ecmaVm, Local<JSValueRef> exception); 108 static void ClearException(const EcmaVM *ecmaVm); 109 static bool IsExceptionCaught(const EcmaVM *ecmaVm); 110 111 // NumberHelper 112 static double StringToDouble(const uint8_t *start, const uint8_t *end, uint8_t radix); 113 114 // JSDebugger 115 static JSDebugger *CreateJSDebugger(const EcmaVM *ecmaVm); 116 static void DestroyJSDebugger(JSDebugger *debugger); 117 static void RegisterHooks(JSDebugger *debugger, PtHooks *hooks); 118 static bool SetBreakpoint(JSDebugger *debugger, const JSPtLocation &location, 119 Local<FunctionRef> condFuncRef); 120 static bool RemoveBreakpoint(JSDebugger *debugger, const JSPtLocation &location); 121 static void RemoveAllBreakpoints(JSDebugger *debugger); 122 static void HandleUncaughtException(const EcmaVM *ecmaVm, std::string &message); 123 static Local<JSValueRef> EvaluateViaFuncCall(EcmaVM *ecmaVm, Local<FunctionRef> funcRef, 124 std::shared_ptr<FrameHandler> &frameHandler); 125 static Local<FunctionRef> GenerateFuncFromBuffer(const EcmaVM *ecmaVm, const void *buffer, size_t size, 126 std::string_view entryPoint); 127 128 // HotReload 129 static DebugInfoExtractor *GetPatchExtractor(const EcmaVM *ecmaVm, const std::string &url); 130 static const JSPandaFile *GetBaseJSPandaFile(const EcmaVM *ecmaVm, const JSPandaFile *jsPandaFile); 131 static std::vector<void *> GetNativePointer(const EcmaVM *ecmaVm); 132 133 // Container 134 static uint32_t GetContainerLength(const EcmaVM *ecmaVm, Local<JSValueRef> value); 135 static void AddInternalProperties(const EcmaVM *ecmaVm, Local<ObjectRef> object, 136 ArkInternalValueType type, Global<MapRef> internalObjects); 137 static Local<JSValueRef> GetArrayListValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 138 Global<MapRef> internalObjects); 139 static Local<JSValueRef> GetDequeValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 140 Global<MapRef> internalObjects); 141 static Local<JSValueRef> GetHashMapValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 142 Global<MapRef> internalObjects); 143 static Local<JSValueRef> GetHashSetValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 144 Global<MapRef> internalObjects); 145 static Local<JSValueRef> GetLightWeightMapValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 146 Global<MapRef> internalObjects); 147 static Local<JSValueRef> GetLightWeightSetValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 148 Global<MapRef> internalObjects); 149 static Local<JSValueRef> GetLinkedListValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 150 Global<MapRef> internalObjects); 151 static Local<JSValueRef> GetListValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 152 Global<MapRef> internalObjects); 153 static Local<JSValueRef> GetPlainArrayValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 154 Global<MapRef> internalObjects); 155 static Local<JSValueRef> GetQueueValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 156 Global<MapRef> internalObjects); 157 static Local<JSValueRef> GetStackValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 158 Global<MapRef> internalObjects); 159 static Local<JSValueRef> GetTreeMapValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 160 Global<MapRef> internalObjects); 161 static Local<JSValueRef> GetTreeSetValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 162 Global<MapRef> internalObjects); 163 static Local<JSValueRef> GetVectorValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 164 Global<MapRef> internalObjects); 165 166 static void DropLastFrame(const EcmaVM *ecmaVm); 167 }; 168 } // namespace panda::ecmascript::tooling 169 170 #endif // ECMASCRIPT_DEBUGGER_DEBUGGER_API_H 171