diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..4947287f7b5ccb5d1e8b7b2d3aa5d89f322c160d --- /dev/null +++ b/LICENSE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/README.en.md b/README.en.md deleted file mode 100644 index 3d3cd55023e2b94e20b8cae68e51c886a11c1937..0000000000000000000000000000000000000000 --- a/README.en.md +++ /dev/null @@ -1,36 +0,0 @@ -# arkcompiler_toolchain - -#### Description -{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README.md b/README.md deleted file mode 100644 index 5da4d7dd33d28dd29011c45cc60ca2b5912417db..0000000000000000000000000000000000000000 --- a/README.md +++ /dev/null @@ -1,39 +0,0 @@ -# arkcompiler_toolchain - -#### 介绍 -{**以下是 Gitee 平台说明,您可以替换此简介** -Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 -无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} - -#### 软件架构 -软件架构说明 - - -#### 安装教程 - -1. xxxx -2. xxxx -3. xxxx - -#### 使用说明 - -1. xxxx -2. xxxx -3. xxxx - -#### 参与贡献 - -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request - - -#### 特技 - -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README_zh.md b/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..ad90cd8c3834e81cae5465834325871fa3903b84 --- /dev/null +++ b/README_zh.md @@ -0,0 +1,38 @@ +# 方舟编译器工具链组件 + +- [方舟编译器工具链](#方舟编译器工具链) + - [简介](#简介) + - [目录](#目录) + - [使用说明](#使用说明) + - [约束](#约束) + - [相关仓](#相关仓) + +## 简介 + +方舟编译器工具链(ArkCompiler Toolchain)为开发者提供了一套OpenHarmony应用程序调试调优工具,其功能包括单步调试、断点调试、watch变量及表达式、cpu profiler和heap profiler等,并支持多实例和worker调试。 + + +## 目录 + +``` +/arkcompiler/toolchain +├─ tooling # 调试器实现 +└─ inspector # inspector检查器,包括会话连接,协议消息转发等 +``` + +## 使用说明 + +1. 连接好设备,新建或将已有工程导入[DevEco Studio](https://developer.harmonyos.com/cn/develop/deveco-studio)进行编译构建; +2. 编译成功后,点击右上角Debug按钮; +3. 安装运行后,即可在设备上查看应用示例运行效果,以及进行相关调试。 + + +## 约束 +1. IDE版本及配套SDK问题,可前往[DevEco Studio版本说明](https://developer.harmonyos.com/cn/docs/documentation/doc-releases/ohos-release-notes-0000001226452454)来查看详细内容。 + + +## 相关仓 + +**[arkcompiler\_toolchain](https://gitee.com/openharmony/arkcompiler_toolchain)** + +[arkcompiler\_ets\_runtime](https://gitee.com/openharmony/arkcompiler_ets_runtime) diff --git a/bundle.json b/bundle.json new file mode 100644 index 0000000000000000000000000000000000000000..8c7d1d46888cc900398e8e7c6fb227a6a11f35aa --- /dev/null +++ b/bundle.json @@ -0,0 +1,35 @@ +{ + "name": "@ohos/arkcompiler_toolchain", + "version": "3.1", + "description": "ark compiler toolchain", + "homePage": "https://gitee.com/openharmony", + "license": "Apache Liscense 2.0", + "publishAs": "code-segment", + "scripts": {}, + "dirs": [], + "segment": { + "destPath": "arkcompiler/toolchain" + }, + "component": { + "name": "arkcompiler_toolchain", + "subsystem": "arkcompiler", + "syscap": [], + "features": [], + "adapted_system_type": [ + "standard" + ], + "rom": "", + "ram": "", + "deps": { + "components": [], + "third_party": [] + }, + "build": { + "sub_component": [ + "//arkcompiler/toolchain/inspector:ark_debugger" + ], + "inner_kits": [], + "test": [] + } + } +} \ No newline at end of file diff --git a/inspector/BUILD.gn b/inspector/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e07c092aed02cd6a42d2d603590fcab403514bf5 --- /dev/null +++ b/inspector/BUILD.gn @@ -0,0 +1,56 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//arkcompiler/toolchain/toolchain.gni") +import("//build/ohos.gni") +import("//foundation/arkui/ace_engine/ace_config.gni") + +config("ark_debugger_config") { + cflags_cc = [ + "-fexceptions", + "-fno-complete-member-pointers", + "-Wno-implicit-fallthrough", + "-fvisibility=default", + "-frtti", + ] +} + +ohos_shared_library("ark_debugger") { + deps = [] + defines = [ + "BOOST_ERROR_CODE_HEADER_ONLY", + "BOOST_CLANG", + ] + + defines += [ "ACE_LOG_TAG=\"ArkDebugger\"" ] + + external_deps = hilog_deps + + include_dirs = [ + "$toolchain_root", + "$ace_root/frameworks", + "//third_party/boost", + "//third_party/boost/boost", + ] + + sources = [ + "$ace_root/adapter/ohos/osal/log_wrapper.cpp", + "inspector.cpp", + "ws_server.cpp", + ] + + configs = [ ":ark_debugger_config" ] + + subsystem_name = "arkcompiler" + part_name = "toolchain" +} diff --git a/inspector/inspector.cpp b/inspector/inspector.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28fc64396176afc51a88fba4f4854245e428c26b --- /dev/null +++ b/inspector/inspector.cpp @@ -0,0 +1,269 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "inspector/inspector.h" + +#include +#include +#include +#include + +#include "base/log/log.h" +#include "inspector/ws_server.h" + +namespace OHOS::ArkCompiler::Toolchain { +namespace { +enum DispatchStatus : int32_t { + UNKNOWN = 0, + DISPATCHING, + DISPATCHED +}; + +using InitializeDebugger = void(*)(void*, const std::function&); +using UninitializeDebugger = void(*)(void*); +using WaitForDebugger = void(*)(void*); +using DispatchMessage = void(*)(void*, std::string&&); +using ProcessMessage = void(*)(void*); +using GetDispatchStatus = int32_t(*)(void*); + +DispatchMessage g_dispatchMessage = nullptr; +InitializeDebugger g_initializeDebugger = nullptr; +UninitializeDebugger g_uninitializeDebugger = nullptr; +WaitForDebugger g_waitForDebugger = nullptr; +ProcessMessage g_processMessage = nullptr; +GetDispatchStatus g_getDispatchStatus = nullptr; + +std::atomic g_hasArkFuncsInited = false; +std::unordered_map g_inspectors; +std::shared_mutex g_mutex; + +thread_local void* g_handle = nullptr; +thread_local void* g_vm = nullptr; + +constexpr char ARK_DEBUGGER_SHARED_LIB[] = "libark_ecma_debugger.so"; + +void* HandleClient(void* const server) +{ + LOGI("HandleClient"); + if (server == nullptr) { + LOGE("HandleClient server nullptr"); + return nullptr; + } + static_cast(server)->RunServer(); + return nullptr; +} + +bool LoadArkDebuggerLibrary() +{ + if (g_handle != nullptr) { + LOGE("Already opened"); + return false; + } + g_handle = dlopen(ARK_DEBUGGER_SHARED_LIB, RTLD_LAZY); + if (g_handle == nullptr) { + LOGE("Failed to open %{public}s, reason: %{public}sn", ARK_DEBUGGER_SHARED_LIB, dlerror()); + return false; + } + return true; +} + +void* GetArkDynFunction(const char* symbol) +{ + auto function = dlsym(g_handle, symbol); + if (function == nullptr) { + LOGE("Failed to get symbol %{public}s in %{public}s", symbol, ARK_DEBUGGER_SHARED_LIB); + } + return function; +} + +void SendReply(const void* vm, const std::string& message) +{ + std::shared_lock lock(g_mutex); + auto iter = g_inspectors.find(vm); + if (iter != g_inspectors.end() && iter->second != nullptr && + iter->second->websocketServer_ != nullptr) { + iter->second->websocketServer_->SendReply(message); + } +} + +void ResetServiceLocked() +{ + auto iter = g_inspectors.find(g_vm); + if (iter != g_inspectors.end() && iter->second != nullptr && + iter->second->websocketServer_ != nullptr) { + iter->second->websocketServer_->StopServer(); + delete iter->second; + iter->second = nullptr; + g_inspectors.erase(iter); + } + if (g_handle != nullptr) { + dlclose(g_handle); + g_handle = nullptr; + } +} + +bool InitializeInspector(void* vm, const std::string& componentName, int32_t instanceId, + const DebuggerPostTask& debuggerPostTask) +{ + std::unique_lock lock(g_mutex); + auto iter = g_inspectors.find(vm); + if (iter != g_inspectors.end()) { + LOGE("Already have the same vm in the map"); + return false; + } + + Inspector *newInspector = new Inspector(); + if (!g_inspectors.emplace(vm, newInspector).second) { + delete newInspector; + return false; + } + + newInspector->tid_ = pthread_self(); + newInspector->vm_ = vm; + newInspector->debuggerPostTask_ = debuggerPostTask; + newInspector->websocketServer_ = std::make_unique(componentName, + std::bind(&Inspector::OnMessage, newInspector, std::placeholders::_1), instanceId); + + pthread_t tid; + if (pthread_create(&tid, nullptr, &HandleClient, static_cast( + newInspector->websocketServer_.get())) != 0) { + LOGE("Create inspector thread failed"); + return false; + } + + return true; +} + +bool InitializeArkFunctions() +{ + // no need to initialize again in case of multi-instance + if (g_hasArkFuncsInited) { + return true; + } + + std::unique_lock lock(g_mutex); + if (g_hasArkFuncsInited) { + return true; + } + g_initializeDebugger = reinterpret_cast( + GetArkDynFunction("InitializeDebugger")); + if (g_initializeDebugger == nullptr) { + ResetServiceLocked(); + return false; + } + g_uninitializeDebugger = reinterpret_cast( + GetArkDynFunction("UninitializeDebugger")); + if (g_uninitializeDebugger == nullptr) { + ResetServiceLocked(); + return false; + } + g_waitForDebugger = reinterpret_cast( + GetArkDynFunction("WaitForDebugger")); + if (g_waitForDebugger == nullptr) { + ResetServiceLocked(); + return false; + } + g_dispatchMessage = reinterpret_cast( + GetArkDynFunction("DispatchMessage")); + if (g_dispatchMessage == nullptr) { + ResetServiceLocked(); + return false; + } + g_getDispatchStatus = reinterpret_cast( + GetArkDynFunction("GetDispatchStatus")); + if (g_getDispatchStatus == nullptr) { + ResetServiceLocked(); + return false; + } + g_processMessage = reinterpret_cast( + GetArkDynFunction("ProcessMessage")); + if (g_processMessage == nullptr) { + ResetServiceLocked(); + return false; + } + + g_hasArkFuncsInited = true; + return true; +} +} // namespace + +void Inspector::OnMessage(std::string&& msg) +{ + g_dispatchMessage(vm_, std::move(msg)); + + // message will be processed soon if the debugger thread is in running or waiting status + if (g_getDispatchStatus(vm_) != DispatchStatus::UNKNOWN) { + return; + } + usleep(DELAY_CHECK_DISPATCH_STATUS); + if (g_getDispatchStatus(vm_) != DispatchStatus::UNKNOWN) { + return; + } + + // the debugger thread maybe in idle status, so try to post a task to wake it up + if (debuggerPostTask_ != nullptr) { + debuggerPostTask_([tid = tid_, vm = vm_] { + if (tid != pthread_self()) { + LOGE("Task not in debugger thread"); + return; + } + g_processMessage(vm); + }); + } else { + LOGW("No debuggerPostTask provided"); + } +} + +bool StartDebug(const std::string& componentName, void* vm, bool isDebugMode, int32_t instanceId, + const DebuggerPostTask& debuggerPostTask) +{ + LOGI("StartDebug: %{private}s", componentName.c_str()); + g_vm = vm; + if (!LoadArkDebuggerLibrary()) { + return false; + } + if (!InitializeArkFunctions()) { + LOGE("Initialize ark functions failed"); + return false; + } + + g_initializeDebugger(vm, std::bind(&SendReply, vm, std::placeholders::_2)); + + if (!InitializeInspector(vm, componentName, instanceId, debuggerPostTask)) { + LOGE("Initialize inspector failed"); + return false; + } + + if (isDebugMode) { + g_waitForDebugger(vm); + } + LOGI("StartDebug Continue"); + return true; +} + +void StopDebug(const std::string& componentName) +{ + LOGI("StopDebug: %{private}s", componentName.c_str()); + std::unique_lock lock(g_mutex); + auto iter = g_inspectors.find(g_vm); + if (iter == g_inspectors.end() || iter->second == nullptr) { + return; + } + + g_uninitializeDebugger(g_vm); + ResetServiceLocked(); + LOGI("StopDebug end"); +} +} // namespace OHOS::ArkCompiler::Toolchain diff --git a/inspector/inspector.h b/inspector/inspector.h new file mode 100644 index 0000000000000000000000000000000000000000..5bf32192c35cc544a86e431442334df67e90155b --- /dev/null +++ b/inspector/inspector.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ARKCOMPILER_TOOLCHAIN_INSPECTOR_INSPECTOR_H +#define ARKCOMPILER_TOOLCHAIN_INSPECTOR_INSPECTOR_H + +#include "ws_server.h" + +#include + +namespace panda::ecmascript { +class EcmaVM; +} // namespace panda::ecmascript + +namespace OHOS::ArkCompiler::Toolchain { +using EcmaVM = panda::ecmascript::EcmaVM; +using DebuggerPostTask = std::function&&)>; + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif + +bool StartDebug(const std::string& componentName, void* vm, bool isDebugMode, int32_t instanceId, + const DebuggerPostTask& debuggerPostTask); + +void StopDebug(const std::string& componentName); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif + +class Inspector { +public: + Inspector() = default; + ~Inspector() = default; + + void OnMessage(std::string&& msg); + + static constexpr int32_t DELAY_CHECK_DISPATCH_STATUS = 100; + + pthread_t tid_ = 0; + void* vm_ = nullptr; + std::unique_ptr websocketServer_; + DebuggerPostTask debuggerPostTask_; +}; +} // namespace OHOS::ArkCompiler::Toolchain + +#endif // ARKCOMPILER_TOOLCHAIN_INSPECTOR_INSPECTOR_H \ No newline at end of file diff --git a/inspector/ws_server.cpp b/inspector/ws_server.cpp new file mode 100644 index 0000000000000000000000000000000000000000..200dd832d1d0f89631661d93ab01128ddf56d2e8 --- /dev/null +++ b/inspector/ws_server.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "inspector/ws_server.h" + +#include +#include +#include + +#include "base/log/log.h" + +namespace OHOS::ArkCompiler::Toolchain { +void WsServer::RunServer() +{ + terminateExecution_ = false; + try { + ioContext_ = std::make_unique(); + int appPid = getpid(); + tid_ = pthread_self(); + std::string pidStr = std::to_string(appPid); + std::string instanceIdStr(""); + auto& connectFlag = connectState_; + /** + * The old version of IDE is not compatible with the new images due to the connect server. + * The First instance will use "pid" instead of "pid + instanceId" to avoid this. + * If old version of IDE does not get the instanceId by connect server, it can still connect the debug server. + */ + if (instanceId_ != 0) { + instanceIdStr = std::to_string(instanceId_); + } + std::string sockName = '\0' + pidStr + instanceIdStr + componentName_; + LOGI("WsServer RunServer: %{public}d%{public}d%{public}s", appPid, instanceId_, componentName_.c_str()); + localSocket::endpoint endPoint(sockName); + localSocket::socket socket(*ioContext_); + localSocket::acceptor acceptor(*ioContext_, endPoint); + acceptor.async_accept(socket, [&connectFlag](const boost::system::error_code& error) { + if (!error) { + connectFlag = true; + } + }); + ioContext_->run(); + if (terminateExecution_ || !connectState_) { + return; + } + webSocket_ = std::unique_ptr>( + std::make_unique>(std::move(socket))); + webSocket_->accept(); + while (!terminateExecution_) { + beast::flat_buffer buffer; + boost::system::error_code error; + webSocket_->read(buffer, error); + if (error) { + webSocket_.reset(); + return; + } + std::string message = boost::beast::buffers_to_string(buffer.data()); + LOGI("WsServer OnMessage: %{public}s", message.c_str()); + wsOnMessage_(std::move(message)); + } + } catch (const beast::system_error& se) { + if (se.code() != websocket::error::closed) { + webSocket_.reset(); + LOGE("Error system_error"); + } + } catch (const std::exception& e) { + LOGE("Error exception, %{public}s", e.what()); + } +} + +void WsServer::StopServer() +{ + LOGI("WsServer StopServer"); + terminateExecution_ = true; + if (!connectState_) { + ioContext_->stop(); + } else { + boost::system::error_code error; + webSocket_->close(websocket::close_code::normal, error); + } + pthread_join(tid_, NULL); +} + +void WsServer::SendReply(const std::string& message) const +{ + LOGI("WsServer SendReply: %{public}s", message.c_str()); + try { + boost::beast::multi_buffer buffer; + boost::beast::ostream(buffer) << message; + + webSocket_->text(webSocket_->got_text()); + webSocket_->write(buffer.data()); + } catch (const beast::system_error& se) { + if (se.code() != websocket::error::closed) { + LOGE("SendReply Error system_error"); + } + } catch (const std::exception& e) { + LOGE("SendReply Error exception"); + } +} +} // namespace OHOS::ArkCompiler::Toolchain diff --git a/inspector/ws_server.h b/inspector/ws_server.h new file mode 100644 index 0000000000000000000000000000000000000000..304f8883b714cf95451ef88b53c5acda79bf8901 --- /dev/null +++ b/inspector/ws_server.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ARKCOMPILER_TOOLCHAIN_INSPECTOR_WS_SERVER_H +#define ARKCOMPILER_TOOLCHAIN_INSPECTOR_WS_SERVER_H + +#include +#include +#include + +#include +#include +#include +#include + +namespace OHOS::ArkCompiler::Toolchain { +namespace beast = boost::beast; +namespace websocket = beast::websocket; +using localSocket = boost::asio::local::stream_protocol; + +class WsServer { +public: + WsServer(const std::string& component, const std::function& onMessage, int32_t instanceId) + : instanceId_(instanceId), componentName_(component), wsOnMessage_(onMessage) + {} + ~WsServer() = default; + void RunServer(); + void StopServer(); + void SendReply(const std::string& message) const; + +private: + std::atomic connectState_ {false}; + std::atomic terminateExecution_ { false }; + int32_t instanceId_ {0}; + pthread_t tid_ {0}; + std::string componentName_ {}; + std::function wsOnMessage_ {}; + std::unique_ptr> webSocket_ { nullptr }; + std::unique_ptr ioContext_ { nullptr }; +}; +} // namespace OHOS::ArkCompiler::Toolchain + +#endif // ARKCOMPILER_TOOLCHAIN_INSPECTOR_WS_SERVER_H diff --git a/toolchain.gni b/toolchain.gni new file mode 100644 index 0000000000000000000000000000000000000000..991c3d65ee16e51d5c2a6a2d5e1ee4912ac13aad --- /dev/null +++ b/toolchain.gni @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +toolchain_root = "//arkcompiler//toolchain"