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 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); 39 40 bool InitializeDebuggerForSocketpair(void* vm); 41 42 void StopDebug(void* vm); 43 44 void StopOldDebug(int tid, const std::string& componentName); 45 46 void WaitForDebugger(void* vm); 47 48 void StoreDebuggerInfo(int tid, void* vm, const DebuggerPostTask& debuggerPostTask); 49 50 // The returned pointer must be released using free() after it is no longer needed. 51 // Failure to release the memory will result in memory leaks. 52 const char* GetJsBacktrace(); 53 #if __cplusplus 54 } 55 #endif 56 57 class Inspector { 58 public: 59 Inspector() = default; 60 ~Inspector() = default; 61 62 void OnMessage(std::string&& msg); 63 #if defined(OHOS_PLATFORM) 64 static uint64_t GetThreadOrTaskId(); 65 #endif // defined(OHOS_PLATFORM) 66 67 static constexpr int32_t DELAY_CHECK_DISPATCH_STATUS = 100; 68 69 pthread_t tid_ = 0; 70 int tidForSocketPair_ = 0; 71 void* vm_ = nullptr; 72 std::unique_ptr<WsServer> websocketServer_; 73 DebuggerPostTask debuggerPostTask_; 74 }; 75 } // namespace OHOS::ArkCompiler::Toolchain 76 77 #endif // ARKCOMPILER_TOOLCHAIN_INSPECTOR_INSPECTOR_H