blob: f0dff06f0b668fec47c73851e6955125ad9eb4da [file] [log] [blame]
Dan Albertdb6fe642015-03-19 15:21:08 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Josh Gao9eaf33c2016-05-13 15:28:34 -070017#if !ADB_HOST
Dan Albertdb6fe642015-03-19 15:21:08 -070018
Yabin Cui19bec5b2015-09-22 15:52:57 -070019#define TRACE_TAG JDWP
Dan Albertdb6fe642015-03-19 15:21:08 -070020
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080021#include "sysdeps.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070022
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080023#include <errno.h>
24#include <stdio.h>
Elliott Hughes43df1092015-07-23 17:12:58 -070025#include <stdlib.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080026#include <string.h>
Josh Gao9eaf33c2016-05-13 15:28:34 -070027#include <sys/socket.h>
28#include <sys/un.h>
Teddie Stenvif56e7f52010-02-15 12:20:44 +010029#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080030
Josh Gao9eaf33c2016-05-13 15:28:34 -070031#include <list>
32#include <memory>
33#include <vector>
34
Dan Albertdb6fe642015-03-19 15:21:08 -070035#include "adb.h"
Josh Gao9eaf33c2016-05-13 15:28:34 -070036#include "adb_io.h"
Josh Gaoea7457b2016-08-30 15:39:25 -070037#include "adb_unique_fd.h"
Yabin Cui5fc22312015-10-06 15:10:05 -070038#include "adb_utils.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070039
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080040/* here's how these things work.
41
42 when adbd starts, it creates a unix server socket
Josh Gao9eaf33c2016-05-13 15:28:34 -070043 named @jdwp-control (@ is a shortcut for "first byte is zero"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080044 to use the private namespace instead of the file system)
45
46 when a new JDWP daemon thread starts in a new VM process, it creates
Josh Gao9eaf33c2016-05-13 15:28:34 -070047 a connection to @jdwp-control to announce its availability.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080048
49
Josh Gao9eaf33c2016-05-13 15:28:34 -070050 JDWP thread @jdwp-control
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080051 | |
52 |-------------------------------> |
53 | hello I'm in process <pid> |
54 | |
55 | |
56
57 the connection is kept alive. it will be closed automatically if
58 the JDWP process terminates (this allows adbd to detect dead
59 processes).
60
61 adbd thus maintains a list of "active" JDWP processes. it can send
62 its content to clients through the "device:debug-ports" service,
63 or even updates through the "device:track-debug-ports" service.
64
65 when a debugger wants to connect, it simply runs the command
66 equivalent to "adb forward tcp:<hostport> jdwp:<pid>"
67
68 "jdwp:<pid>" is a new forward destination format used to target
69 a given JDWP process on the device. when sutch a request arrives,
70 adbd does the following:
71
72 - first, it calls socketpair() to create a pair of equivalent
73 sockets.
74
75 - it attaches the first socket in the pair to a local socket
76 which is itself attached to the transport's remote socket:
77
78
79 - it sends the file descriptor of the second socket directly
80 to the JDWP process with the help of sendmsg()
81
82
Josh Gao9eaf33c2016-05-13 15:28:34 -070083 JDWP thread @jdwp-control
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080084 | |
85 | <----------------------|
86 | OK, try this file descriptor |
87 | |
88 | |
89
90 then, the JDWP thread uses this new socket descriptor as its
91 pass-through connection to the debugger (and receives the
92 JDWP-Handshake message, answers to it, etc...)
93
94 this gives the following graphics:
95 ____________________________________
96 | |
97 | ADB Server (host) |
98 | |
99 Debugger <---> LocalSocket <----> RemoteSocket |
100 | ^^ |
101 |___________________________||_______|
102 ||
103 Transport ||
104 (TCP for emulator - USB for device) ||
105 ||
106 ___________________________||_______
107 | || |
108 | ADBD (device) || |
109 | VV |
110 JDWP <======> LocalSocket <----> RemoteSocket |
111 | |
112 |____________________________________|
113
114 due to the way adb works, this doesn't need a special socket
115 type or fancy handling of socket termination if either the debugger
116 or the JDWP process closes the connection.
117
118 THIS IS THE SIMPLEST IMPLEMENTATION I COULD FIND, IF YOU HAPPEN
119 TO HAVE A BETTER IDEA, LET ME KNOW - Digit
120
121**********************************************************************/
122
123/** JDWP PID List Support Code
124 ** for each JDWP process, we record its pid and its connected socket
125 **/
126
Josh Gao9eaf33c2016-05-13 15:28:34 -0700127// PIDs are transmitted as 4 hex digits in ascii.
128static constexpr size_t PID_LEN = 4;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800129
Josh Gao9eaf33c2016-05-13 15:28:34 -0700130static void jdwp_process_event(int socket, unsigned events, void* _proc);
131static void jdwp_process_list_updated(void);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800132
Josh Gao9eaf33c2016-05-13 15:28:34 -0700133struct JdwpProcess;
134static std::list<std::unique_ptr<JdwpProcess>> _jdwp_list;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800135
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800136struct JdwpProcess {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700137 explicit JdwpProcess(int socket) {
138 this->socket = socket;
139 this->fde = fdevent_create(socket, jdwp_process_event, this);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800140
Josh Gao9eaf33c2016-05-13 15:28:34 -0700141 if (!this->fde) {
142 fatal("could not create fdevent for new JDWP process");
143 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800144
Josh Gao9eaf33c2016-05-13 15:28:34 -0700145 this->fde->state |= FDE_DONT_CLOSE;
146
147 /* start by waiting for the PID */
148 fdevent_add(this->fde, FDE_READ);
149 }
150
151 ~JdwpProcess() {
152 if (this->socket >= 0) {
153 adb_shutdown(this->socket);
154 adb_close(this->socket);
155 this->socket = -1;
156 }
157
158 if (this->fde) {
159 fdevent_destroy(this->fde);
160 this->fde = nullptr;
161 }
162
163 out_fds.clear();
164 }
165
166 void RemoveFromList() {
167 if (this->pid >= 0) {
168 D("removing pid %d from jdwp process list", this->pid);
169 } else {
170 D("removing transient JdwpProcess from list");
171 }
172
173 auto pred = [this](const auto& proc) { return proc.get() == this; };
174 _jdwp_list.remove_if(pred);
175 }
176
177 int pid = -1;
178 int socket = -1;
179 fdevent* fde = nullptr;
180
181 std::vector<unique_fd> out_fds;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800182};
183
Josh Gao9eaf33c2016-05-13 15:28:34 -0700184static size_t jdwp_process_list(char* buffer, size_t bufferlen) {
185 std::string temp;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800186
Josh Gao9eaf33c2016-05-13 15:28:34 -0700187 for (auto& proc : _jdwp_list) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800188 /* skip transient connections */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700189 if (proc->pid < 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800190 continue;
Josh Gao9eaf33c2016-05-13 15:28:34 -0700191 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800192
Josh Gao9eaf33c2016-05-13 15:28:34 -0700193 std::string next = std::to_string(proc->pid) + "\n";
194 if (temp.length() + next.length() > bufferlen) {
195 D("truncating JDWP process list (max len = %zu)", bufferlen);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800196 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800197 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700198 temp.append(next);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800199 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700200
201 memcpy(buffer, temp.data(), temp.length());
202 return temp.length();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800203}
204
Josh Gao9eaf33c2016-05-13 15:28:34 -0700205static size_t jdwp_process_list_msg(char* buffer, size_t bufferlen) {
206 // Message is length-prefixed with 4 hex digits in ASCII.
207 static constexpr size_t header_len = 4;
208 if (bufferlen < header_len) {
209 fatal("invalid JDWP process list buffer size: %zu", bufferlen);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800210 }
211
Josh Gao9eaf33c2016-05-13 15:28:34 -0700212 char head[header_len + 1];
213 size_t len = jdwp_process_list(buffer + header_len, bufferlen - header_len);
214 snprintf(head, sizeof head, "%04zx", len);
215 memcpy(buffer, head, header_len);
216 return len + header_len;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800217}
218
Josh Gao9eaf33c2016-05-13 15:28:34 -0700219static void jdwp_process_event(int socket, unsigned events, void* _proc) {
220 JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(_proc);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800221
222 if (events & FDE_READ) {
223 if (proc->pid < 0) {
224 /* read the PID as a 4-hexchar string */
Josh Gao6f3fea22017-03-20 13:37:13 -0700225 char buf[PID_LEN + 1];
226 ssize_t rc = TEMP_FAILURE_RETRY(recv(socket, buf, PID_LEN, 0));
227 if (rc != PID_LEN) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700228 D("failed to read jdwp pid: %s", strerror(errno));
229 goto CloseProcess;
230 }
Josh Gao6f3fea22017-03-20 13:37:13 -0700231 buf[PID_LEN] = '\0';
Josh Gao9eaf33c2016-05-13 15:28:34 -0700232
Josh Gao6f3fea22017-03-20 13:37:13 -0700233 if (sscanf(buf, "%04x", &proc->pid) != 1) {
234 D("could not decode JDWP %p PID number: '%s'", proc, buf);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800235 goto CloseProcess;
236 }
237
238 /* all is well, keep reading to detect connection closure */
Yabin Cui815ad882015-09-02 17:44:28 -0700239 D("Adding pid %d to jdwp process list", proc->pid);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800240 jdwp_process_list_updated();
Josh Gao9eaf33c2016-05-13 15:28:34 -0700241 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800242 /* the pid was read, if we get there it's probably because the connection
243 * was closed (e.g. the JDWP process exited or crashed) */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700244 char buf[32];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800245
Josh Gao9eaf33c2016-05-13 15:28:34 -0700246 while (true) {
247 int len = TEMP_FAILURE_RETRY(recv(socket, buf, sizeof(buf), 0));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800248
Josh Gao9eaf33c2016-05-13 15:28:34 -0700249 if (len == 0) {
250 D("terminating JDWP %d connection: EOF", proc->pid);
251 break;
252 } else if (len < 0) {
253 if (len < 0 && errno == EAGAIN) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800254 return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800255 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700256
257 D("terminating JDWP %d connection: EOF", proc->pid);
258 break;
259 } else {
260 D("ignoring unexpected JDWP %d control socket activity (%d bytes)", proc->pid,
261 len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800262 }
263 }
264
Josh Gao9eaf33c2016-05-13 15:28:34 -0700265 goto CloseProcess;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800266 }
267 }
268
269 if (events & FDE_WRITE) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700270 D("trying to send fd to JDWP process (count = %zu)", proc->out_fds.size());
271 if (!proc->out_fds.empty()) {
272 int fd = proc->out_fds.back().get();
273 struct cmsghdr* cmsg;
274 struct msghdr msg;
275 struct iovec iov;
276 char dummy = '!';
277 char buffer[sizeof(struct cmsghdr) + sizeof(int)];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800278
Josh Gao9eaf33c2016-05-13 15:28:34 -0700279 iov.iov_base = &dummy;
280 iov.iov_len = 1;
281 msg.msg_name = NULL;
282 msg.msg_namelen = 0;
283 msg.msg_iov = &iov;
284 msg.msg_iovlen = 1;
285 msg.msg_flags = 0;
286 msg.msg_control = buffer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800287 msg.msg_controllen = sizeof(buffer);
288
289 cmsg = CMSG_FIRSTHDR(&msg);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700290 cmsg->cmsg_len = msg.msg_controllen;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800291 cmsg->cmsg_level = SOL_SOCKET;
Josh Gao9eaf33c2016-05-13 15:28:34 -0700292 cmsg->cmsg_type = SCM_RIGHTS;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800293 ((int*)CMSG_DATA(cmsg))[0] = fd;
294
Yabin Cui5fc22312015-10-06 15:10:05 -0700295 if (!set_file_block_mode(proc->socket, true)) {
296 VLOG(JDWP) << "failed to set blocking mode for fd " << proc->socket;
Teddie Stenvif56e7f52010-02-15 12:20:44 +0100297 goto CloseProcess;
298 }
299
Josh Gao9eaf33c2016-05-13 15:28:34 -0700300 int ret = TEMP_FAILURE_RETRY(sendmsg(proc->socket, &msg, 0));
301 if (ret < 0) {
302 D("sending new file descriptor to JDWP %d failed: %s", proc->pid, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800303 goto CloseProcess;
304 }
305
Josh Gao9eaf33c2016-05-13 15:28:34 -0700306 adb_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800307
Josh Gao9eaf33c2016-05-13 15:28:34 -0700308 D("sent file descriptor %d to JDWP process %d", fd, proc->pid);
309
310 proc->out_fds.pop_back();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800311
Yabin Cui5fc22312015-10-06 15:10:05 -0700312 if (!set_file_block_mode(proc->socket, false)) {
313 VLOG(JDWP) << "failed to set non-blocking mode for fd " << proc->socket;
Teddie Stenvif56e7f52010-02-15 12:20:44 +0100314 goto CloseProcess;
315 }
316
Josh Gao9eaf33c2016-05-13 15:28:34 -0700317 if (proc->out_fds.empty()) {
318 fdevent_del(proc->fde, FDE_WRITE);
319 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800320 }
321 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700322
323 return;
324
325CloseProcess:
326 proc->RemoveFromList();
327 jdwp_process_list_updated();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800328}
329
Josh Gao9eaf33c2016-05-13 15:28:34 -0700330int create_jdwp_connection_fd(int pid) {
Yabin Cui815ad882015-09-02 17:44:28 -0700331 D("looking for pid %d in JDWP process list", pid);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700332
333 for (auto& proc : _jdwp_list) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800334 if (proc->pid == pid) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700335 int fds[2];
336
337 if (adb_socketpair(fds) < 0) {
338 D("%s: socket pair creation failed: %s", __FUNCTION__, strerror(errno));
339 return -1;
340 }
341 D("socketpair: (%d,%d)", fds[0], fds[1]);
342
343 proc->out_fds.emplace_back(fds[1]);
344 if (proc->out_fds.size() == 1) {
345 fdevent_add(proc->fde, FDE_WRITE);
346 }
347
348 return fds[0];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800349 }
350 }
Yabin Cui815ad882015-09-02 17:44:28 -0700351 D("search failed !!");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800352 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800353}
354
355/** VM DEBUG CONTROL SOCKET
356 **
357 ** we do implement a custom asocket to receive the data
358 **/
359
360/* name of the debug control Unix socket */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700361#define JDWP_CONTROL_NAME "\0jdwp-control"
362#define JDWP_CONTROL_NAME_LEN (sizeof(JDWP_CONTROL_NAME) - 1)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800363
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700364struct JdwpControl {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700365 int listen_socket;
366 fdevent* fde;
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700367};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800368
Josh Gao9eaf33c2016-05-13 15:28:34 -0700369static JdwpControl _jdwp_control;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800370
Josh Gao9eaf33c2016-05-13 15:28:34 -0700371static void jdwp_control_event(int s, unsigned events, void* user);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800372
Josh Gao9eaf33c2016-05-13 15:28:34 -0700373static int jdwp_control_init(JdwpControl* control, const char* sockname, int socknamelen) {
374 sockaddr_un addr;
375 socklen_t addrlen;
376 int s;
377 int maxpath = sizeof(addr.sun_path);
378 int pathlen = socknamelen;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800379
380 if (pathlen >= maxpath) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700381 D("vm debug control socket name too long (%d extra chars)", pathlen + 1 - maxpath);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800382 return -1;
383 }
384
385 memset(&addr, 0, sizeof(addr));
386 addr.sun_family = AF_UNIX;
387 memcpy(addr.sun_path, sockname, socknamelen);
388
Josh Gao6f3fea22017-03-20 13:37:13 -0700389 s = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800390 if (s < 0) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700391 D("could not create vm debug control socket. %d: %s", errno, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800392 return -1;
393 }
394
Josh Gao9eaf33c2016-05-13 15:28:34 -0700395 addrlen = pathlen + sizeof(addr.sun_family);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800396
Erik Kline3c8d44d2015-12-01 17:27:59 +0900397 if (bind(s, reinterpret_cast<sockaddr*>(&addr), addrlen) < 0) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700398 D("could not bind vm debug control socket: %d: %s", errno, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800399 adb_close(s);
400 return -1;
401 }
402
Josh Gao9eaf33c2016-05-13 15:28:34 -0700403 if (listen(s, 4) < 0) {
404 D("listen failed in jdwp control socket: %d: %s", errno, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800405 adb_close(s);
406 return -1;
407 }
408
409 control->listen_socket = s;
410
411 control->fde = fdevent_create(s, jdwp_control_event, control);
412 if (control->fde == NULL) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700413 D("could not create fdevent for jdwp control socket");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800414 adb_close(s);
415 return -1;
416 }
417
418 /* only wait for incoming connections */
419 fdevent_add(control->fde, FDE_READ);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800420
Yabin Cui815ad882015-09-02 17:44:28 -0700421 D("jdwp control socket started (%d)", control->listen_socket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800422 return 0;
423}
424
Josh Gao9eaf33c2016-05-13 15:28:34 -0700425static void jdwp_control_event(int s, unsigned events, void* _control) {
426 JdwpControl* control = (JdwpControl*)_control;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800427
428 if (events & FDE_READ) {
Josh Gao782d8d42016-08-23 15:41:56 -0700429 int s = adb_socket_accept(control->listen_socket, nullptr, nullptr);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700430 if (s < 0) {
431 if (errno == ECONNABORTED) {
432 /* oops, the JDWP process died really quick */
433 D("oops, the JDWP process died really quick");
434 return;
435 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800436 /* the socket is probably closed ? */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700437 D("weird accept() failed on jdwp control socket: %s", strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800438 return;
439 }
440 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800441
Josh Gao9eaf33c2016-05-13 15:28:34 -0700442 auto proc = std::make_unique<JdwpProcess>(s);
443 if (!proc) {
444 fatal("failed to allocate JdwpProcess");
445 }
446
447 _jdwp_list.emplace_back(std::move(proc));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800448 }
449}
450
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800451/** "jdwp" local service implementation
452 ** this simply returns the list of known JDWP process pids
453 **/
454
Josh Gao9eaf33c2016-05-13 15:28:34 -0700455struct JdwpSocket : public asocket {
456 bool pass;
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700457};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800458
Josh Gao9eaf33c2016-05-13 15:28:34 -0700459static void jdwp_socket_close(asocket* s) {
460 D("LS(%d): closing jdwp socket", s->id);
461
462 if (s->peer) {
463 D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
464 s->peer->peer = nullptr;
465 s->peer->close(s->peer);
466 s->peer = nullptr;
467 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800468
469 remove_socket(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800470 free(s);
471}
472
Josh Gao9eaf33c2016-05-13 15:28:34 -0700473static int jdwp_socket_enqueue(asocket* s, apacket* p) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800474 /* you can't write to this asocket */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700475 D("LS(%d): JDWP socket received data?", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800476 put_apacket(p);
477 s->peer->close(s->peer);
478 return -1;
479}
480
Josh Gao9eaf33c2016-05-13 15:28:34 -0700481static void jdwp_socket_ready(asocket* s) {
482 JdwpSocket* jdwp = (JdwpSocket*)s;
483 asocket* peer = jdwp->peer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800484
Josh Gao9eaf33c2016-05-13 15:28:34 -0700485 /* on the first call, send the list of pids,
486 * on the second one, close the connection
487 */
488 if (!jdwp->pass) {
489 apacket* p = get_apacket();
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100490 p->len = jdwp_process_list((char*)p->data, s->get_max_payload());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800491 peer->enqueue(peer, p);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700492 jdwp->pass = true;
493 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800494 peer->close(peer);
495 }
496}
497
Josh Gao9eaf33c2016-05-13 15:28:34 -0700498asocket* create_jdwp_service_socket(void) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800499 JdwpSocket* s = reinterpret_cast<JdwpSocket*>(calloc(sizeof(*s), 1));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800500
Josh Gao9eaf33c2016-05-13 15:28:34 -0700501 if (!s) {
502 fatal("failed to allocate JdwpSocket");
503 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800504
Josh Gao9eaf33c2016-05-13 15:28:34 -0700505 install_local_socket(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800506
Josh Gao9eaf33c2016-05-13 15:28:34 -0700507 s->ready = jdwp_socket_ready;
508 s->enqueue = jdwp_socket_enqueue;
509 s->close = jdwp_socket_close;
510 s->pass = false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800511
Josh Gao9eaf33c2016-05-13 15:28:34 -0700512 return s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800513}
514
515/** "track-jdwp" local service implementation
516 ** this periodically sends the list of known JDWP process pids
517 ** to the client...
518 **/
519
Josh Gao9eaf33c2016-05-13 15:28:34 -0700520struct JdwpTracker : public asocket {
521 bool need_initial;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800522};
523
Josh Gao9eaf33c2016-05-13 15:28:34 -0700524static std::vector<std::unique_ptr<JdwpTracker>> _jdwp_trackers;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800525
Josh Gao9eaf33c2016-05-13 15:28:34 -0700526static void jdwp_process_list_updated(void) {
527 char buffer[1024];
528 int len = jdwp_process_list_msg(buffer, sizeof(buffer));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800529
Josh Gao9eaf33c2016-05-13 15:28:34 -0700530 for (auto& t : _jdwp_trackers) {
531 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800532 memcpy(p->data, buffer, len);
533 p->len = len;
Josh Gao9eaf33c2016-05-13 15:28:34 -0700534
535 if (t->peer) {
536 // The tracker might not have been connected yet.
537 t->peer->enqueue(t->peer, p);
538 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800539 }
540}
541
Josh Gao9eaf33c2016-05-13 15:28:34 -0700542static void jdwp_tracker_close(asocket* s) {
543 D("LS(%d): destroying jdwp tracker service", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800544
Josh Gao9eaf33c2016-05-13 15:28:34 -0700545 if (s->peer) {
546 D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
547 s->peer->peer = nullptr;
548 s->peer->close(s->peer);
549 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800550 }
551
552 remove_socket(s);
553
Josh Gao9eaf33c2016-05-13 15:28:34 -0700554 auto pred = [s](const auto& tracker) { return tracker.get() == s; };
Josh Gaoeea5fea2017-03-10 11:19:48 -0800555 _jdwp_trackers.erase(std::remove_if(_jdwp_trackers.begin(), _jdwp_trackers.end(), pred),
556 _jdwp_trackers.end());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800557}
558
Josh Gao9eaf33c2016-05-13 15:28:34 -0700559static void jdwp_tracker_ready(asocket* s) {
560 JdwpTracker* t = (JdwpTracker*)s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800561
Josh Gao9eaf33c2016-05-13 15:28:34 -0700562 if (t->need_initial) {
563 apacket* p = get_apacket();
564 t->need_initial = false;
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100565 p->len = jdwp_process_list_msg((char*)p->data, s->get_max_payload());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800566 s->peer->enqueue(s->peer, p);
567 }
568}
569
Josh Gao9eaf33c2016-05-13 15:28:34 -0700570static int jdwp_tracker_enqueue(asocket* s, apacket* p) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800571 /* you can't write to this socket */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700572 D("LS(%d): JDWP tracker received data?", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800573 put_apacket(p);
574 s->peer->close(s->peer);
575 return -1;
576}
577
Josh Gao9eaf33c2016-05-13 15:28:34 -0700578asocket* create_jdwp_tracker_service_socket(void) {
579 auto t = std::make_unique<JdwpTracker>();
580 if (!t) {
581 fatal("failed to allocate JdwpTracker");
582 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800583
Josh Gao9eaf33c2016-05-13 15:28:34 -0700584 memset(t.get(), 0, sizeof(asocket));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800585
Josh Gao9eaf33c2016-05-13 15:28:34 -0700586 install_local_socket(t.get());
587 D("LS(%d): created new jdwp tracker service", t->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800588
Josh Gao9eaf33c2016-05-13 15:28:34 -0700589 t->ready = jdwp_tracker_ready;
590 t->enqueue = jdwp_tracker_enqueue;
591 t->close = jdwp_tracker_close;
592 t->need_initial = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800593
Josh Gao9eaf33c2016-05-13 15:28:34 -0700594 asocket* result = t.get();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800595
Josh Gao9eaf33c2016-05-13 15:28:34 -0700596 _jdwp_trackers.emplace_back(std::move(t));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800597
Josh Gao9eaf33c2016-05-13 15:28:34 -0700598 return result;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800599}
600
Josh Gao9eaf33c2016-05-13 15:28:34 -0700601int init_jdwp(void) {
602 return jdwp_control_init(&_jdwp_control, JDWP_CONTROL_NAME, JDWP_CONTROL_NAME_LEN);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800603}
604
605#endif /* !ADB_HOST */