• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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 class PUBLIC_API DebuggerApi {
52 public:
53     // FrameHandler
54     static uint32_t GetStackDepth(const EcmaVM *ecmaVm);
55     static std::shared_ptr<FrameHandler> NewFrameHandler(const EcmaVM *ecmaVm);
56     static bool StackWalker(const EcmaVM *ecmaVm, std::function<StackState(const FrameHandler *)> func);
57 
58     static uint32_t GetBytecodeOffset(const EcmaVM *ecmaVm);
59     static uint32_t GetBytecodeOffset(const FrameHandler *frameHandler);
60     static std::unique_ptr<PtMethod> GetMethod(const EcmaVM *ecmaVm);
61     static Method *GetMethod(const FrameHandler *frameHandler);
62     static bool IsNativeMethod(const EcmaVM *ecmaVm);
63     static bool IsNativeMethod(const FrameHandler *frameHandler);
64     static JSPandaFile *GetJSPandaFile(const EcmaVM *ecmaVm);
65 
66     static JSTaggedValue GetEnv(const FrameHandler *frameHandler);
67     static JSTaggedType *GetSp(const FrameHandler *frameHandler);
68     static int32_t GetVregIndex(const FrameHandler *frameHandler, std::string_view name);
69     static Local<JSValueRef> GetVRegValue(const EcmaVM *ecmaVm,
70                                           const FrameHandler *frameHandler, size_t index);
71     static void SetVRegValue(FrameHandler *frameHandler, size_t index, Local<JSValueRef> value);
72 
73     static Local<JSValueRef> GetProperties(const EcmaVM *ecmaVm, const FrameHandler *frameHandler,
74                                            int32_t level, uint32_t slot);
75     static void SetProperties(const EcmaVM *vm, const FrameHandler *frameHandler, int32_t level,
76                               uint32_t slot, Local<JSValueRef> value);
77     static std::pair<int32_t, uint32_t> GetLevelSlot(const FrameHandler *frameHandler, std::string_view name);
78     static Local<JSValueRef> GetGlobalValue(const EcmaVM *vm, Local<StringRef> name);
79     static bool SetGlobalValue(const EcmaVM *vm, Local<StringRef> name, Local<JSValueRef> value);
80 
81     // JSThread
82     static Local<JSValueRef> GetAndClearException(const EcmaVM *ecmaVm);
83     static JSTaggedValue GetCurrentModule(const EcmaVM *ecmaVm);
84     static void GetModuleVariables(const EcmaVM *vm, Local<ObjectRef> &moduleObj, JSThread *thread);
85     static void SetException(const EcmaVM *ecmaVm, Local<JSValueRef> exception);
86     static void ClearException(const EcmaVM *ecmaVm);
87     static bool IsExceptionCaught(const EcmaVM *ecmaVm);
88 
89     // NumberHelper
90     static double StringToDouble(const uint8_t *start, const uint8_t *end, uint8_t radix);
91 
92     // JSDebugger
93     static JSDebugger *CreateJSDebugger(const EcmaVM *ecmaVm);
94     static void DestroyJSDebugger(JSDebugger *debugger);
95     static void RegisterHooks(JSDebugger *debugger, PtHooks *hooks);
96     static bool SetBreakpoint(JSDebugger *debugger, const JSPtLocation &location,
97         Local<FunctionRef> condFuncRef);
98     static bool RemoveBreakpoint(JSDebugger *debugger, const JSPtLocation &location);
99     static void RemoveAllBreakpoints(JSDebugger *debugger);
100     static void HandleUncaughtException(const EcmaVM *ecmaVm, std::string &message);
101     static Local<JSValueRef> EvaluateViaFuncCall(EcmaVM *ecmaVm, Local<FunctionRef> funcRef,
102         std::shared_ptr<FrameHandler> &frameHandler);
103     static Local<FunctionRef> GenerateFuncFromBuffer(const EcmaVM *ecmaVm, const void *buffer, size_t size,
104         std::string_view entryPoint);
105 
106     // HotReload
107     static DebugInfoExtractor *GetPatchExtractor(const EcmaVM *ecmaVm, const std::string &url);
108     static const JSPandaFile *GetBaseJSPandaFile(const EcmaVM *ecmaVm, const JSPandaFile *jsPandaFile);
109 };
110 }  // namespace panda::ecmascript::tooling
111 
112 #endif  // ECMASCRIPT_TOOLING_BACKEND_DEBUGGER_API_H
113