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 static uint32_t GetStackDepthOverBuiltin(const EcmaVM *ecmaVm); 59 60 static uint32_t GetBytecodeOffset(const EcmaVM *ecmaVm); 61 static uint32_t GetBytecodeOffset(const FrameHandler *frameHandler); 62 static std::unique_ptr<PtMethod> GetMethod(const EcmaVM *ecmaVm); 63 static Method *GetMethod(const FrameHandler *frameHandler); 64 static bool IsNativeMethod(const EcmaVM *ecmaVm); 65 static bool IsNativeMethod(const FrameHandler *frameHandler); 66 static JSPandaFile *GetJSPandaFile(const EcmaVM *ecmaVm); 67 68 static JSTaggedValue GetEnv(const FrameHandler *frameHandler); 69 static JSTaggedType *GetSp(const FrameHandler *frameHandler); 70 static int32_t GetVregIndex(const FrameHandler *frameHandler, std::string_view name); 71 static Local<JSValueRef> GetVRegValue(const EcmaVM *ecmaVm, 72 const FrameHandler *frameHandler, size_t index); 73 static void SetVRegValue(FrameHandler *frameHandler, size_t index, Local<JSValueRef> value); 74 75 static Local<JSValueRef> GetProperties(const EcmaVM *ecmaVm, const FrameHandler *frameHandler, 76 int32_t level, uint32_t slot); 77 static void SetProperties(const EcmaVM *ecmaVm, const FrameHandler *frameHandler, int32_t level, 78 uint32_t slot, Local<JSValueRef> value); 79 static std::pair<int32_t, uint32_t> GetLevelSlot(const FrameHandler *frameHandler, std::string_view name); 80 static Local<JSValueRef> GetGlobalValue(const EcmaVM *ecmaVm, Local<StringRef> name); 81 static bool SetGlobalValue(const EcmaVM *ecmaVm, Local<StringRef> name, Local<JSValueRef> value); 82 83 // JSThread 84 static Local<JSValueRef> GetAndClearException(const EcmaVM *ecmaVm); 85 static JSTaggedValue GetCurrentModule(const EcmaVM *ecmaVm); 86 static JSHandle<JSTaggedValue> GetImportModule(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 87 std::string &name); 88 static int32_t GetModuleVariableIndex(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 89 std::string &name); 90 static int32_t GetRequestModuleIndex(const EcmaVM *ecmaVm, const JSTaggedValue moduleRequest, 91 const JSHandle<JSTaggedValue> ¤tModule); 92 static Local<JSValueRef> GetExportVariableValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 93 std::string &name); 94 static bool SetExportVariableValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 95 std::string &name, Local<JSValueRef> value); 96 static Local<JSValueRef> GetModuleValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 97 std::string &name); 98 static bool SetModuleValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 99 std::string &name, Local<JSValueRef> value); 100 static void InitializeExportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj, 101 const JSHandle<JSTaggedValue> ¤tModule); 102 static void GetLocalExportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj, 103 const JSHandle<JSTaggedValue> ¤tModule, bool isImportStar); 104 static void GetIndirectExportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj, 105 const JSHandle<JSTaggedValue> ¤tModule); 106 static void GetImportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj, 107 const JSHandle<JSTaggedValue> ¤tModule); 108 static void SetException(const EcmaVM *ecmaVm, Local<JSValueRef> exception); 109 static void ClearException(const EcmaVM *ecmaVm); 110 static bool IsExceptionCaught(const EcmaVM *ecmaVm); 111 112 // NumberHelper 113 static double StringToDouble(const uint8_t *start, const uint8_t *end, uint8_t radix); 114 115 // JSDebugger 116 static JSDebugger *CreateJSDebugger(const EcmaVM *ecmaVm); 117 static void DestroyJSDebugger(JSDebugger *debugger); 118 static void RegisterHooks(JSDebugger *debugger, PtHooks *hooks); 119 static bool SetBreakpoint(JSDebugger *debugger, const JSPtLocation &location, 120 Local<FunctionRef> condFuncRef); 121 static bool RemoveBreakpoint(JSDebugger *debugger, const JSPtLocation &location); 122 static void RemoveAllBreakpoints(JSDebugger *debugger); 123 static void HandleUncaughtException(const EcmaVM *ecmaVm, std::string &message); 124 static Local<JSValueRef> EvaluateViaFuncCall(EcmaVM *ecmaVm, Local<FunctionRef> funcRef, 125 std::shared_ptr<FrameHandler> &frameHandler); 126 static Local<JSValueRef> CallFunctionOnCall(EcmaVM *ecmaVm, Local<FunctionRef> funcRef, 127 std::shared_ptr<FrameHandler> &frameHandler); 128 static Local<FunctionRef> GenerateFuncFromBuffer(const EcmaVM *ecmaVm, const void *buffer, size_t size, 129 std::string_view entryPoint); 130 static void SetSingleStepStatus(JSDebugger *debugger, bool status); 131 static bool GetSingleStepStatus(JSDebugger *debugger); 132 133 static int32_t GetObjectHash(const EcmaVM *ecmaVM, const JSHandle<JSTaggedValue> &tagged); 134 135 // HotReload 136 static std::vector<DebugInfoExtractor *> GetPatchExtractors(const EcmaVM *ecmaVm, const std::string &url); 137 static const JSPandaFile *GetBaseJSPandaFile(const EcmaVM *ecmaVm, const JSPandaFile *jsPandaFile); 138 static std::vector<void *> GetNativePointer(const EcmaVM *ecmaVm); 139 140 // Container 141 static uint32_t GetContainerLength(const EcmaVM *ecmaVm, Local<JSValueRef> value); 142 static void AddInternalProperties(const EcmaVM *ecmaVm, Local<ObjectRef> object, 143 ArkInternalValueType type, Global<MapRef> internalObjects); 144 static Local<JSValueRef> GetArrayListValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 145 Global<MapRef> internalObjects); 146 static Local<JSValueRef> GetDequeValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 147 Global<MapRef> internalObjects); 148 static Local<JSValueRef> GetHashMapValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 149 Global<MapRef> internalObjects); 150 static Local<JSValueRef> GetHashSetValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 151 Global<MapRef> internalObjects); 152 static Local<JSValueRef> GetLightWeightMapValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 153 Global<MapRef> internalObjects); 154 static Local<JSValueRef> GetLightWeightSetValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 155 Global<MapRef> internalObjects); 156 static Local<JSValueRef> GetLinkedListValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 157 Global<MapRef> internalObjects); 158 static Local<JSValueRef> GetListValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 159 Global<MapRef> internalObjects); 160 static Local<JSValueRef> GetPlainArrayValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 161 Global<MapRef> internalObjects); 162 static Local<JSValueRef> GetQueueValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 163 Global<MapRef> internalObjects); 164 static Local<JSValueRef> GetStackValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 165 Global<MapRef> internalObjects); 166 static Local<JSValueRef> GetTreeMapValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 167 Global<MapRef> internalObjects); 168 static Local<JSValueRef> GetTreeSetValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 169 Global<MapRef> internalObjects); 170 static Local<JSValueRef> GetVectorValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 171 Global<MapRef> internalObjects); 172 173 static bool CheckPromiseQueueSize(const EcmaVM *ecmaVm); 174 static void DropLastFrame(const EcmaVM *ecmaVm); 175 }; 176 } // namespace panda::ecmascript::tooling 177 178 #endif // ECMASCRIPT_DEBUGGER_DEBUGGER_API_H 179