• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "debugger_service.h"
17 
18 #include "protocol_handler.h"
19 
20 #include "ecmascript/debugger/js_debugger_manager.h"
21 
22 namespace panda::ecmascript::tooling {
InitializeDebugger(::panda::ecmascript::EcmaVM * vm,const std::function<void (const void *,const std::string &)> & onResponse)23 void InitializeDebugger(::panda::ecmascript::EcmaVM *vm,
24                         const std::function<void(const void *, const std::string &)> &onResponse)
25 {
26     if (vm == nullptr || vm->GetJsDebuggerManager() == nullptr) {
27         LOG_DEBUGGER(DEBUG) << "VM has already been destroyed";
28         return;
29     }
30     ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler();
31     if (handler != nullptr) {
32         LOG_DEBUGGER(ERROR) << "JS debugger was initialized";
33         return;
34     }
35     vm->GetJsDebuggerManager()->SetDebuggerHandler(new ProtocolHandler(onResponse, vm));
36 }
37 
UninitializeDebugger(::panda::ecmascript::EcmaVM * vm)38 void UninitializeDebugger(::panda::ecmascript::EcmaVM *vm)
39 {
40     if (vm == nullptr || vm->GetJsDebuggerManager() == nullptr) {
41         LOG_DEBUGGER(DEBUG) << "VM has already been destroyed";
42         return;
43     }
44     ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler();
45     delete handler;
46     vm->GetJsDebuggerManager()->SetDebuggerHandler(nullptr);
47 }
48 
WaitForDebugger(const::panda::ecmascript::EcmaVM * vm)49 void WaitForDebugger(const ::panda::ecmascript::EcmaVM *vm)
50 {
51     if (vm == nullptr || vm->GetJsDebuggerManager() == nullptr) {
52         LOG_DEBUGGER(DEBUG) << "VM has already been destroyed";
53         return;
54     }
55     ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler();
56     if (LIKELY(handler != nullptr)) {
57         handler->WaitForDebugger();
58     }
59 }
60 
OnMessage(const::panda::ecmascript::EcmaVM * vm,std::string && message)61 void OnMessage(const ::panda::ecmascript::EcmaVM *vm, std::string &&message)
62 {
63     if (vm == nullptr || vm->GetJsDebuggerManager() == nullptr) {
64         LOG_DEBUGGER(DEBUG) << "VM has already been destroyed";
65         return;
66     }
67     ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler();
68     if (LIKELY(handler != nullptr)) {
69         handler->DispatchCommand(std::move(message));
70     }
71 }
72 
ProcessMessage(const::panda::ecmascript::EcmaVM * vm)73 void ProcessMessage(const ::panda::ecmascript::EcmaVM *vm)
74 {
75     if (vm == nullptr || vm->GetJsDebuggerManager() == nullptr) {
76         LOG_DEBUGGER(DEBUG) << "VM has already been destroyed";
77         return;
78     }
79     ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler();
80     if (LIKELY(handler != nullptr)) {
81         handler->ProcessCommand();
82     }
83 }
84 
GetDispatchStatus(const::panda::ecmascript::EcmaVM * vm)85 int32_t GetDispatchStatus(const ::panda::ecmascript::EcmaVM *vm)
86 {
87     if (vm == nullptr || vm->GetJsDebuggerManager() == nullptr) {
88         LOG_DEBUGGER(DEBUG) << "VM has already been destroyed";
89         return ProtocolHandler::DispatchStatus::UNKNOWN;
90     }
91     ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler();
92     if (LIKELY(handler != nullptr)) {
93         return handler->GetDispatchStatus();
94     }
95     return ProtocolHandler::DispatchStatus::UNKNOWN;
96 }
97 
98 // strdup allocates memory; caller is responsible for freeing it
99 // Return the dynamically allocated string (must be freed by the caller)
GetCallFrames(const::panda::ecmascript::EcmaVM * vm)100 const char* GetCallFrames(const ::panda::ecmascript::EcmaVM *vm)
101 {
102     if (vm == nullptr || vm->GetJsDebuggerManager() == nullptr) {
103         LOG_DEBUGGER(ERROR) << "VM has already been destroyed";
104         return "";
105     }
106     ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler();
107     if (LIKELY(handler != nullptr)) {
108         auto dispatcher = handler->GetDispatcher();
109         if (LIKELY(dispatcher != nullptr)) {
110             auto mixStack = dispatcher->GetJsFrames();
111             const char* buffer = strdup(mixStack.c_str());
112             return buffer;
113         }
114         return "";
115     }
116     return "";
117 }
118 }  // namespace panda::ecmascript::tooling