• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 ARKCOMPILER_TOOLCHAIN_INSPECTOR_INSPECTOR_H
17 #define ARKCOMPILER_TOOLCHAIN_INSPECTOR_INSPECTOR_H
18 
19 #include "inspector/ws_server.h"
20 
21 #include <string>
22 
23 namespace panda::ecmascript {
24 class EcmaVM;
25 }  // namespace panda::ecmascript
26 
27 namespace OHOS::ArkCompiler::Toolchain {
28 using EcmaVM = panda::ecmascript::EcmaVM;
29 using DebuggerPostTask = std::function<void(std::function<void()>&&)>;
30 
31 #if __cplusplus
32 extern "C" {
33 #endif
34 
35 bool StartDebug(const std::string& componentName, void* vm, bool isDebugMode,
36     int32_t instanceId, const DebuggerPostTask& debuggerPostTask, int port);
37 
38 bool StartDebugForSocketpair(int tid, int socketfd, bool isHybrid = false);
39 
40 bool InitializeDebuggerForSocketpair(void* vm, bool isHybrid = false);
41 
42 void StopDebug(void* vm, bool isHybrid = false);
43 
44 void StopOldDebug(int tid, const std::string& componentName);
45 
46 void WaitForDebugger(void* vm);
47 
48 int StartDebugger(uint32_t port, bool breakOnStart);
49 
50 int StopDebugger();
51 
52 void StoreDebuggerInfo(int tid, void* vm, const DebuggerPostTask& debuggerPostTask);
53 
54 // The returned pointer must be released using free() after it is no longer needed.
55 // Failure to release the memory will result in memory leaks.
56 const char* GetJsBacktrace();
57 
58 const char* OperateJsDebugMessage(const char* message);
59 #if __cplusplus
60 }
61 #endif
62 
63 class Inspector {
64 public:
65     Inspector() = default;
66     ~Inspector() = default;
67 
68     void OnMessage(std::string&& msg, bool isHybrid = false);
69 #if defined(OHOS_PLATFORM)
70     static uint64_t GetThreadOrTaskId();
71 #endif // defined(OHOS_PLATFORM)
72 
73     static constexpr int32_t DELAY_CHECK_DISPATCH_STATUS = 100;
74 
75     pthread_t tid_ = 0;
76     int tidForSocketPair_ = 0;
77     void* vm_ = nullptr;
78     std::unique_ptr<WsServer> websocketServer_;
79     DebuggerPostTask debuggerPostTask_;
80 };
81 } // namespace OHOS::ArkCompiler::Toolchain
82 
83 #endif // ARKCOMPILER_TOOLCHAIN_INSPECTOR_INSPECTOR_H