1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. 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 ARKTS_PLUGIN_H 17 #define ARKTS_PLUGIN_H 18 19 #include "arkts_plugin_config.pb.h" 20 #include "plugin_module_api.h" 21 #include "schedule_task_manager.h" 22 #include "trace_file_writer.h" 23 24 struct WebSocketFrame { 25 uint8_t fin; 26 uint8_t opCode; 27 uint8_t mask; 28 char maskingKey[5]; 29 uint64_t payloadLen; 30 std::unique_ptr<char[]> payload; 31 }; 32 33 class ArkTSPlugin { 34 public: 35 ArkTSPlugin() = default; 36 ~ArkTSPlugin() = default; 37 int32_t Start(const uint8_t* configData, uint32_t configSize); 38 int32_t Stop(); 39 void SetWriter(WriterStruct* writer); 40 41 private: 42 void Snapshot(); 43 void FlushData(const std::string& command = ""); 44 bool ClientConnectUnixWebSocket(const std::string& sockName, uint32_t timeoutLimit = 0); 45 bool ClientSendWSUpgradeReq(); 46 bool ClientRecvWSUpgradeRsp(); 47 bool ClientSendReq(const std::string& message); 48 void Close(); 49 bool SetWebSocketTimeOut(int32_t fd, uint32_t timeoutLimit); 50 std::string Decode(); 51 bool HandleFrame(WebSocketFrame& wsFrame); 52 bool DecodeMessage(WebSocketFrame& wsFrame); 53 uint64_t NetToHostLongLong(char* buf, uint32_t len); 54 bool Recv(int32_t client, char* buf, size_t totalLen, int32_t flags) const; 55 int32_t EnableTimeline(); 56 int32_t EnableSnapshot(); 57 int32_t EnableCpuProfiler(); 58 59 private: 60 ArkTSConfig protoConfig_; 61 WriterStruct* resultWriter_{nullptr}; 62 int32_t pid_{0}; 63 std::vector<char> buffer_; 64 std::string snapshotCmd_; 65 std::string timelineCmd_; 66 int32_t client_{-1}; 67 enum SocketState : uint8_t { 68 UNINITED, 69 INITED, 70 CONNECTED, 71 }; 72 std::atomic<SocketState> socketState_{SocketState::UNINITED}; 73 uint8_t commandResult_{0}; 74 ScheduleTaskManager scheduleTaskManager_; 75 int32_t snapshotScheduleTaskFd_; 76 std::shared_ptr<TraceFileWriter> splitTraceWriter_ {nullptr}; 77 }; 78 79 #endif // ARKTS_PLUGIN_H