• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 private:
35 #ifdef JS_JDWP_CONNECT
36     static constexpr uint8_t JS_PKG_MIN_SIZE = 15;  // JsMsgHeader + "pkgName:"uint8_t[7~127]
37     static constexpr uint8_t JS_PKG_MX_SIZE = 135;
38     struct JsMsgHeader {
39         uint32_t msgLen;
40         uint32_t pid;
41     };
42     string GetProcessListExtendPkgName();
43 #endif // JS_JDWP_CONNECT
44     struct _PollFd {
45         int fd;
46         short events;
47         short revents;
48     };
49     struct PollNode {
50         _PollFd pollfd;
51         uint32_t ppid;
PollNodePollNode52         PollNode(int fd, uint32_t pid)
53         {
54             Base::ZeroStruct(pollfd);
55             pollfd.fd = fd;
56             pollfd.events = POLLNVAL | POLLRDHUP | POLLHUP | POLLERR;
57             pollfd.revents = 0;
58             ppid = pid;
59         }
60     };
61     struct ContextJdwp {
62         uint32_t pid;
63         uv_pipe_t pipe;
64         HdcJdwp *thisClass;
65         bool finish;
66 #ifdef JS_JDWP_CONNECT
67         char buf[JS_PKG_MX_SIZE];
68         string pkgName;
69 #else
70         char buf[sizeof(uint32_t)];
71 #endif  // JS_JDWP_CONNECT
72         uint8_t dummy;
73         uv_tcp_t jvmTCP;
74     };
75     using HCtxJdwp = struct ContextJdwp *;
76 
77     bool JdwpListen();
78     static int UvPipeBind(uv_pipe_t* handle, const char* name, size_t size);
79     static void AcceptClient(uv_stream_t *server, int status);
80     static void ReadStream(uv_stream_t *pipe, ssize_t nread, const uv_buf_t *buf);
81     static void SendCallbackJdwpNewFD(uv_write_t *req, int status);
82     static void *FdEventPollThread(void *args);
83     void RemoveFdFromPollList(uint32_t pid);
84     size_t JdwpProcessListMsg(char *buffer, size_t bufferlen);
85     void *MallocContext();
86     void FreeContext(HCtxJdwp ctx);
87     void *AdminContext(const uint8_t op, const uint32_t pid, HCtxJdwp ctxJdwp);
88     int CreateFdEventPoll();
89     void ProcessListUpdated(HTaskInfo task = nullptr);
90     void SendProcessList(HTaskInfo t, string data);
91     void DrainAwakenPollThread() const;
92     void WakePollThread();
93     uv_loop_t *loop;
94     uv_pipe_t listenPipe = {};
95     uint32_t refCount;
96     int32_t awakenPollFd;
97     map<uint32_t, HCtxJdwp> mapCtxJdwp;
98     uv_rwlock_t lockMapContext;
99     uv_rwlock_t lockJdwpTrack;
100     std::unordered_map<int, PollNode> pollNodeMap;  // fd, PollNode
101     std::vector<HTaskInfo> jdwpTrackers;
102     bool stop;
103     std::mutex freeContextMutex;
104 };
105 } // namespace Hdc
106 #endif
107