1 /* 2 * Copyright (C) 2021 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 #ifndef HDC_JDWP_H 16 #define HDC_JDWP_H 17 #include "daemon_common.h" 18 #include <poll.h> 19 #include <unordered_map> 20 namespace Hdc { 21 class HdcJdwp { 22 public: 23 HdcJdwp(uv_loop_t *loopIn); 24 virtual ~HdcJdwp(); 25 int Initial(); 26 void Stop(); 27 bool CreateJdwpTracker(HTaskInfo taskInfo); 28 void RemoveJdwpTracker(HTaskInfo taskInfo); 29 bool ReadyForRelease(); 30 string GetProcessList(); 31 bool SendJdwpNewFD(uint32_t targetPID, int fd); 32 bool CheckPIDExist(uint32_t targetPID); 33 34 #ifdef FUZZ_TEST 35 public: 36 #else 37 private: 38 #endif 39 #ifdef JS_JDWP_CONNECT 40 static constexpr uint8_t JS_PKG_MIN_SIZE = 15; // JsMsgHeader + "pkgName:"uint8_t[7~127] 41 static constexpr uint8_t JS_PKG_MX_SIZE = 135; 42 struct JsMsgHeader { 43 uint32_t msgLen; 44 uint32_t pid; 45 }; 46 string GetProcessListExtendPkgName(); 47 #endif // JS_JDWP_CONNECT 48 struct _PollFd { 49 int fd; 50 short events; 51 short revents; 52 }; 53 struct PollNode { 54 _PollFd pollfd; 55 uint32_t ppid; PollNodePollNode56 PollNode(int fd, uint32_t pid) 57 { 58 Base::ZeroStruct(pollfd); 59 pollfd.fd = fd; 60 pollfd.events = POLLNVAL | POLLRDHUP | POLLHUP | POLLERR; 61 pollfd.revents = 0; 62 ppid = pid; 63 } 64 }; 65 struct ContextJdwp { 66 uint32_t pid; 67 uv_pipe_t pipe; 68 HdcJdwp *thisClass; 69 bool finish; 70 #ifdef JS_JDWP_CONNECT 71 char buf[JS_PKG_MX_SIZE]; 72 string pkgName; 73 #else 74 char buf[sizeof(uint32_t)]; 75 #endif // JS_JDWP_CONNECT 76 uint8_t dummy; 77 uv_tcp_t jvmTCP; 78 }; 79 using HCtxJdwp = struct ContextJdwp *; 80 81 bool JdwpListen(); 82 static int UvPipeBind(uv_pipe_t* handle, const char* name, size_t size); 83 static void AcceptClient(uv_stream_t *server, int status); 84 static void ReadStream(uv_stream_t *pipe, ssize_t nread, const uv_buf_t *buf); 85 static void SendCallbackJdwpNewFD(uv_write_t *req, int status); 86 static void *FdEventPollThread(void *args); 87 void RemoveFdFromPollList(uint32_t pid); 88 size_t JdwpProcessListMsg(char *buffer, size_t bufferlen); 89 void *MallocContext(); 90 void FreeContext(HCtxJdwp ctx); 91 void *AdminContext(const uint8_t op, const uint32_t pid, HCtxJdwp ctxJdwp); 92 int CreateFdEventPoll(); 93 void ProcessListUpdated(HTaskInfo task = nullptr); 94 void SendProcessList(HTaskInfo t, string data); 95 void DrainAwakenPollThread() const; 96 void WakePollThread(); 97 uv_loop_t *loop; 98 uv_pipe_t listenPipe = {}; 99 uint32_t refCount; 100 int32_t awakenPollFd; 101 map<uint32_t, HCtxJdwp> mapCtxJdwp; 102 uv_rwlock_t lockMapContext; 103 uv_rwlock_t lockJdwpTrack; 104 std::unordered_map<int, PollNode> pollNodeMap; // fd, PollNode 105 std::vector<HTaskInfo> jdwpTrackers; 106 bool stop; 107 std::mutex freeContextMutex; 108 }; 109 } // namespace Hdc 110 #endif 111