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
23namespace panda::ecmascript {
24class EcmaVM;
25}  // namespace panda::ecmascript
26
27namespace OHOS::ArkCompiler::Toolchain {
28using EcmaVM = panda::ecmascript::EcmaVM;
29using DebuggerPostTask = std::function<void(std::function<void()>&&)>;
30
31#if __cplusplus
32extern "C" {
33#endif
34
35bool StartDebug(const std::string& componentName, void* vm, bool isDebugMode,
36    int32_t instanceId, const DebuggerPostTask& debuggerPostTask, int port);
37
38bool StartDebugForSocketpair(int tid, int socketfd);
39
40bool InitializeDebuggerForSocketpair(void* vm);
41
42void StopDebug(void* vm);
43
44void StopOldDebug(int tid, const std::string& componentName);
45
46void WaitForDebugger(void* vm);
47
48void StoreDebuggerInfo(int tid, void* vm, const DebuggerPostTask& debuggerPostTask);
49
50#if __cplusplus
51}
52#endif
53
54class Inspector {
55public:
56    Inspector() = default;
57    ~Inspector() = default;
58
59    void OnMessage(std::string&& msg);
60#if defined(OHOS_PLATFORM)
61    uint64_t GetThreadOrTaskId();
62#endif // defined(OHOS_PLATFORM)
63
64    static constexpr int32_t DELAY_CHECK_DISPATCH_STATUS = 100;
65
66    pthread_t tid_ = 0;
67    int tidForSocketPair_ = 0;
68    void* vm_ = nullptr;
69    std::unique_ptr<WsServer> websocketServer_;
70    DebuggerPostTask debuggerPostTask_;
71};
72} // namespace OHOS::ArkCompiler::Toolchain
73
74#endif // ARKCOMPILER_TOOLCHAIN_INSPECTOR_INSPECTOR_H