14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_DEBUGGER_DEBUGGER_API_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_DEBUGGER_DEBUGGER_API_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include <functional> 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_ci#include "ecmascript/common.h" 224514f5e3Sopenharmony_ci#include "ecmascript/debugger/js_debugger_interface.h" 234514f5e3Sopenharmony_ci#include "ecmascript/debugger/js_pt_method.h" 244514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/scope_info_extractor.h" 254514f5e3Sopenharmony_ci#include "ecmascript/napi/include/jsnapi.h" 264514f5e3Sopenharmony_ci 274514f5e3Sopenharmony_cinamespace panda { 284514f5e3Sopenharmony_cinamespace ecmascript { 294514f5e3Sopenharmony_ciclass EcmaVM; 304514f5e3Sopenharmony_ciclass FrameHandler; 314514f5e3Sopenharmony_ciclass JSThread; 324514f5e3Sopenharmony_ciclass Method; 334514f5e3Sopenharmony_ciclass ModuleManager; 344514f5e3Sopenharmony_ciclass NameDictionary; 354514f5e3Sopenharmony_ciclass SourceTextModule; 364514f5e3Sopenharmony_ciclass DebugInfoExtractor; 374514f5e3Sopenharmony_cinamespace tooling { 384514f5e3Sopenharmony_ciclass JSDebugger; 394514f5e3Sopenharmony_ci} 404514f5e3Sopenharmony_ci} // ecmascript 414514f5e3Sopenharmony_ci} // panda 424514f5e3Sopenharmony_ci 434514f5e3Sopenharmony_cinamespace panda::ecmascript::tooling { 444514f5e3Sopenharmony_cienum StackState { 454514f5e3Sopenharmony_ci CONTINUE, 464514f5e3Sopenharmony_ci FAILED, 474514f5e3Sopenharmony_ci SUCCESS, 484514f5e3Sopenharmony_ci}; 494514f5e3Sopenharmony_ci 504514f5e3Sopenharmony_cienum class ArkInternalValueType {None, Entry, Scope, ScopeList}; 514514f5e3Sopenharmony_ciclass PUBLIC_API DebuggerApi { 524514f5e3Sopenharmony_cipublic: 534514f5e3Sopenharmony_ci class PUBLIC_API DebuggerNativeScope { 544514f5e3Sopenharmony_ci public: 554514f5e3Sopenharmony_ci explicit DebuggerNativeScope(const EcmaVM *vm); 564514f5e3Sopenharmony_ci ~DebuggerNativeScope(); 574514f5e3Sopenharmony_ci ECMA_DISALLOW_COPY(DebuggerNativeScope); 584514f5e3Sopenharmony_ci ECMA_DISALLOW_MOVE(DebuggerNativeScope); 594514f5e3Sopenharmony_ci 604514f5e3Sopenharmony_ci private: 614514f5e3Sopenharmony_ci JSThread *thread_ {nullptr}; 624514f5e3Sopenharmony_ci uint16_t oldThreadState_ {0}; 634514f5e3Sopenharmony_ci bool hasSwitchState_ {false}; 644514f5e3Sopenharmony_ci }; 654514f5e3Sopenharmony_ci class PUBLIC_API DebuggerManagedScope { 664514f5e3Sopenharmony_ci public: 674514f5e3Sopenharmony_ci explicit DebuggerManagedScope(const EcmaVM *vm); 684514f5e3Sopenharmony_ci ~DebuggerManagedScope(); 694514f5e3Sopenharmony_ci ECMA_DISALLOW_COPY(DebuggerManagedScope); 704514f5e3Sopenharmony_ci ECMA_DISALLOW_MOVE(DebuggerManagedScope); 714514f5e3Sopenharmony_ci 724514f5e3Sopenharmony_ci private: 734514f5e3Sopenharmony_ci JSThread *thread_ {nullptr}; 744514f5e3Sopenharmony_ci uint16_t oldThreadState_ {0}; 754514f5e3Sopenharmony_ci bool hasSwitchState_ {false}; 764514f5e3Sopenharmony_ci }; 774514f5e3Sopenharmony_ci 784514f5e3Sopenharmony_ci // FrameHandler 794514f5e3Sopenharmony_ci static uint32_t GetStackDepth(const EcmaVM *ecmaVm); 804514f5e3Sopenharmony_ci static std::shared_ptr<FrameHandler> NewFrameHandler(const EcmaVM *ecmaVm); 814514f5e3Sopenharmony_ci static bool StackWalker(const EcmaVM *ecmaVm, std::function<StackState(const FrameHandler *)> func); 824514f5e3Sopenharmony_ci static uint32_t GetStackDepthOverBuiltin(const EcmaVM *ecmaVm); 834514f5e3Sopenharmony_ci 844514f5e3Sopenharmony_ci static uint32_t GetBytecodeOffset(const EcmaVM *ecmaVm); 854514f5e3Sopenharmony_ci static uint32_t GetBytecodeOffset(const FrameHandler *frameHandler); 864514f5e3Sopenharmony_ci static std::unique_ptr<PtMethod> GetMethod(const EcmaVM *ecmaVm); 874514f5e3Sopenharmony_ci static Method *GetMethod(const FrameHandler *frameHandler); 884514f5e3Sopenharmony_ci static bool IsNativeMethod(const EcmaVM *ecmaVm); 894514f5e3Sopenharmony_ci static bool IsNativeMethod(const FrameHandler *frameHandler); 904514f5e3Sopenharmony_ci static JSPandaFile *GetJSPandaFile(const EcmaVM *ecmaVm); 914514f5e3Sopenharmony_ci 924514f5e3Sopenharmony_ci static JSTaggedValue GetEnv(const FrameHandler *frameHandler); 934514f5e3Sopenharmony_ci static JSTaggedType *GetSp(const FrameHandler *frameHandler); 944514f5e3Sopenharmony_ci static int32_t GetVregIndex(const FrameHandler *frameHandler, std::string_view name); 954514f5e3Sopenharmony_ci static Local<JSValueRef> GetVRegValue(const EcmaVM *ecmaVm, 964514f5e3Sopenharmony_ci const FrameHandler *frameHandler, size_t index); 974514f5e3Sopenharmony_ci static void SetVRegValue(FrameHandler *frameHandler, size_t index, Local<JSValueRef> value); 984514f5e3Sopenharmony_ci 994514f5e3Sopenharmony_ci static Local<JSValueRef> GetProperties(const EcmaVM *ecmaVm, const FrameHandler *frameHandler, 1004514f5e3Sopenharmony_ci int32_t level, uint32_t slot); 1014514f5e3Sopenharmony_ci static void SetProperties(const EcmaVM *ecmaVm, const FrameHandler *frameHandler, int32_t level, 1024514f5e3Sopenharmony_ci uint32_t slot, Local<JSValueRef> value); 1034514f5e3Sopenharmony_ci static std::pair<int32_t, uint32_t> GetLevelSlot(const FrameHandler *frameHandler, std::string_view name); 1044514f5e3Sopenharmony_ci static Local<JSValueRef> GetGlobalValue(const EcmaVM *ecmaVm, Local<StringRef> name); 1054514f5e3Sopenharmony_ci static bool SetGlobalValue(const EcmaVM *ecmaVm, Local<StringRef> name, Local<JSValueRef> value); 1064514f5e3Sopenharmony_ci 1074514f5e3Sopenharmony_ci // JSThread 1084514f5e3Sopenharmony_ci static Local<JSValueRef> GetAndClearException(const EcmaVM *ecmaVm); 1094514f5e3Sopenharmony_ci static JSTaggedValue GetCurrentModule(const EcmaVM *ecmaVm); 1104514f5e3Sopenharmony_ci static JSHandle<JSTaggedValue> GetImportModule(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 1114514f5e3Sopenharmony_ci std::string &name); 1124514f5e3Sopenharmony_ci static int32_t GetModuleVariableIndex(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 1134514f5e3Sopenharmony_ci std::string &name); 1144514f5e3Sopenharmony_ci static int32_t GetRequestModuleIndex(const EcmaVM *ecmaVm, const JSTaggedValue moduleRequest, 1154514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> ¤tModule); 1164514f5e3Sopenharmony_ci static Local<JSValueRef> GetExportVariableValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 1174514f5e3Sopenharmony_ci std::string &name); 1184514f5e3Sopenharmony_ci static bool SetExportVariableValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 1194514f5e3Sopenharmony_ci std::string &name, Local<JSValueRef> value); 1204514f5e3Sopenharmony_ci static Local<JSValueRef> GetModuleValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 1214514f5e3Sopenharmony_ci std::string &name); 1224514f5e3Sopenharmony_ci static bool SetModuleValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> ¤tModule, 1234514f5e3Sopenharmony_ci std::string &name, Local<JSValueRef> value); 1244514f5e3Sopenharmony_ci static void InitializeExportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj, 1254514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> ¤tModule); 1264514f5e3Sopenharmony_ci static void GetLocalExportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj, 1274514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> ¤tModule, bool isImportStar); 1284514f5e3Sopenharmony_ci static void GetIndirectExportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj, 1294514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> ¤tModule); 1304514f5e3Sopenharmony_ci static void GetImportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj, 1314514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> ¤tModule); 1324514f5e3Sopenharmony_ci static void SetException(const EcmaVM *ecmaVm, Local<JSValueRef> exception); 1334514f5e3Sopenharmony_ci static void ClearException(const EcmaVM *ecmaVm); 1344514f5e3Sopenharmony_ci static bool IsExceptionCaught(const EcmaVM *ecmaVm); 1354514f5e3Sopenharmony_ci 1364514f5e3Sopenharmony_ci // NumberHelper 1374514f5e3Sopenharmony_ci static double StringToDouble(const uint8_t *start, const uint8_t *end, uint8_t radix); 1384514f5e3Sopenharmony_ci 1394514f5e3Sopenharmony_ci // JSDebugger 1404514f5e3Sopenharmony_ci static JSDebugger *CreateJSDebugger(const EcmaVM *ecmaVm); 1414514f5e3Sopenharmony_ci static void DestroyJSDebugger(JSDebugger *debugger); 1424514f5e3Sopenharmony_ci static void RegisterHooks(JSDebugger *debugger, PtHooks *hooks); 1434514f5e3Sopenharmony_ci static bool SetBreakpoint(JSDebugger *debugger, const JSPtLocation &location, 1444514f5e3Sopenharmony_ci Local<FunctionRef> condFuncRef, bool isSmartBreakPoint = false); 1454514f5e3Sopenharmony_ci static bool RemoveBreakpoint(JSDebugger *debugger, const JSPtLocation &location); 1464514f5e3Sopenharmony_ci static void RemoveAllBreakpoints(JSDebugger *debugger); 1474514f5e3Sopenharmony_ci static void HandleUncaughtException(const EcmaVM *ecmaVm, std::string &message); 1484514f5e3Sopenharmony_ci static Local<JSValueRef> EvaluateViaFuncCall(EcmaVM *ecmaVm, Local<FunctionRef> funcRef, 1494514f5e3Sopenharmony_ci std::shared_ptr<FrameHandler> &frameHandler); 1504514f5e3Sopenharmony_ci static Local<JSValueRef> CallFunctionOnCall(EcmaVM *ecmaVm, Local<FunctionRef> funcRef, 1514514f5e3Sopenharmony_ci std::shared_ptr<FrameHandler> &frameHandler); 1524514f5e3Sopenharmony_ci static Local<FunctionRef> GenerateFuncFromBuffer(const EcmaVM *ecmaVm, const void *buffer, size_t size, 1534514f5e3Sopenharmony_ci std::string_view entryPoint); 1544514f5e3Sopenharmony_ci static void SetSingleStepStatus(JSDebugger *debugger, bool status); 1554514f5e3Sopenharmony_ci static bool GetSingleStepStatus(JSDebugger *debugger); 1564514f5e3Sopenharmony_ci 1574514f5e3Sopenharmony_ci static int32_t GetObjectHash(const EcmaVM *ecmaVM, const JSHandle<JSTaggedValue> &tagged); 1584514f5e3Sopenharmony_ci static void GetObjectClassName(const EcmaVM *ecmaVM, Local<JSValueRef> &tagged, std::string &className); 1594514f5e3Sopenharmony_ci static bool RemoveBreakpointsByUrl(JSDebugger *debugger, const std::string &url); 1604514f5e3Sopenharmony_ci 1614514f5e3Sopenharmony_ci // HotReload 1624514f5e3Sopenharmony_ci static std::vector<DebugInfoExtractor *> GetPatchExtractors(const EcmaVM *ecmaVm, const std::string &url); 1634514f5e3Sopenharmony_ci static const JSPandaFile *GetBaseJSPandaFile(const EcmaVM *ecmaVm, const JSPandaFile *jsPandaFile); 1644514f5e3Sopenharmony_ci static std::vector<void *> GetNativePointer(const EcmaVM *ecmaVm); 1654514f5e3Sopenharmony_ci 1664514f5e3Sopenharmony_ci // Container 1674514f5e3Sopenharmony_ci static uint32_t GetContainerLength(const EcmaVM *ecmaVm, Local<JSValueRef> value); 1684514f5e3Sopenharmony_ci static void AddInternalProperties(const EcmaVM *ecmaVm, Local<ObjectRef> object, 1694514f5e3Sopenharmony_ci ArkInternalValueType type, Global<MapRef> internalObjects); 1704514f5e3Sopenharmony_ci static Local<JSValueRef> GetArrayListValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1714514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1724514f5e3Sopenharmony_ci static Local<JSValueRef> GetDequeValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1734514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1744514f5e3Sopenharmony_ci static Local<JSValueRef> GetHashMapValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1754514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1764514f5e3Sopenharmony_ci static Local<JSValueRef> GetHashSetValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1774514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1784514f5e3Sopenharmony_ci static Local<JSValueRef> GetLightWeightMapValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1794514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1804514f5e3Sopenharmony_ci static Local<JSValueRef> GetLightWeightSetValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1814514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1824514f5e3Sopenharmony_ci static Local<JSValueRef> GetLinkedListValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1834514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1844514f5e3Sopenharmony_ci static Local<JSValueRef> GetListValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1854514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1864514f5e3Sopenharmony_ci static Local<JSValueRef> GetPlainArrayValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1874514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1884514f5e3Sopenharmony_ci static Local<JSValueRef> GetQueueValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1894514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1904514f5e3Sopenharmony_ci static Local<JSValueRef> GetStackValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1914514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1924514f5e3Sopenharmony_ci static Local<JSValueRef> GetTreeMapValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1934514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1944514f5e3Sopenharmony_ci static Local<JSValueRef> GetTreeSetValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1954514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1964514f5e3Sopenharmony_ci static Local<JSValueRef> GetVectorValue(const EcmaVM *ecmaVm, Local<JSValueRef> value, 1974514f5e3Sopenharmony_ci Global<MapRef> internalObjects); 1984514f5e3Sopenharmony_ci 1994514f5e3Sopenharmony_ci static bool CheckPromiseQueueSize(const EcmaVM *ecmaVm); 2004514f5e3Sopenharmony_ci static bool CheckIsSendableMethod(const EcmaVM *ecmaVm); 2014514f5e3Sopenharmony_ci static bool IsMainThread(); 2024514f5e3Sopenharmony_ci static void DropLastFrame(const EcmaVM *ecmaVm); 2034514f5e3Sopenharmony_ci}; 2044514f5e3Sopenharmony_ci} // namespace panda::ecmascript::tooling 2054514f5e3Sopenharmony_ci 2064514f5e3Sopenharmony_ci#endif // ECMASCRIPT_DEBUGGER_DEBUGGER_API_H 207