blob: 3135aa42a392dff27d813f7baceb58f5b100f02e [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;
182 char in_buf[PID_LEN + 1];
183 ssize_t in_len = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800184};
185
Josh Gao9eaf33c2016-05-13 15:28:34 -0700186static size_t jdwp_process_list(char* buffer, size_t bufferlen) {
187 std::string temp;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800188
Josh Gao9eaf33c2016-05-13 15:28:34 -0700189 for (auto& proc : _jdwp_list) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800190 /* skip transient connections */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700191 if (proc->pid < 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800192 continue;
Josh Gao9eaf33c2016-05-13 15:28:34 -0700193 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800194
Josh Gao9eaf33c2016-05-13 15:28:34 -0700195 std::string next = std::to_string(proc->pid) + "\n";
196 if (temp.length() + next.length() > bufferlen) {
197 D("truncating JDWP process list (max len = %zu)", bufferlen);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800198 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800199 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700200 temp.append(next);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800201 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700202
203 memcpy(buffer, temp.data(), temp.length());
204 return temp.length();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800205}
206
Josh Gao9eaf33c2016-05-13 15:28:34 -0700207static size_t jdwp_process_list_msg(char* buffer, size_t bufferlen) {
208 // Message is length-prefixed with 4 hex digits in ASCII.
209 static constexpr size_t header_len = 4;
210 if (bufferlen < header_len) {
211 fatal("invalid JDWP process list buffer size: %zu", bufferlen);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800212 }
213
Josh Gao9eaf33c2016-05-13 15:28:34 -0700214 char head[header_len + 1];
215 size_t len = jdwp_process_list(buffer + header_len, bufferlen - header_len);
216 snprintf(head, sizeof head, "%04zx", len);
217 memcpy(buffer, head, header_len);
218 return len + header_len;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800219}
220
Josh Gao9eaf33c2016-05-13 15:28:34 -0700221static void jdwp_process_event(int socket, unsigned events, void* _proc) {
222 JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(_proc);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800223
224 if (events & FDE_READ) {
225 if (proc->pid < 0) {
226 /* read the PID as a 4-hexchar string */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700227 if (proc->in_len < 0) {
228 fatal("attempting to read JDWP pid again?");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800229 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800230
Josh Gao9eaf33c2016-05-13 15:28:34 -0700231 char* p = proc->in_buf + proc->in_len;
232 size_t size = PID_LEN - proc->in_len;
233
234 ssize_t rc = TEMP_FAILURE_RETRY(recv(socket, p, size, 0));
235 if (rc < 0) {
236 if (errno == EAGAIN) {
237 return;
238 }
239
240 D("failed to read jdwp pid: %s", strerror(errno));
241 goto CloseProcess;
242 }
243
244 proc->in_len += rc;
245 if (proc->in_len != PID_LEN) {
246 return;
247 }
248
249 proc->in_buf[PID_LEN] = '\0';
250 proc->in_len = -1;
251
252 if (sscanf(proc->in_buf, "%04x", &proc->pid) != 1) {
253 D("could not decode JDWP %p PID number: '%s'", proc, p);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800254 goto CloseProcess;
255 }
256
257 /* all is well, keep reading to detect connection closure */
Yabin Cui815ad882015-09-02 17:44:28 -0700258 D("Adding pid %d to jdwp process list", proc->pid);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800259 jdwp_process_list_updated();
Josh Gao9eaf33c2016-05-13 15:28:34 -0700260 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800261 /* the pid was read, if we get there it's probably because the connection
262 * was closed (e.g. the JDWP process exited or crashed) */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700263 char buf[32];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800264
Josh Gao9eaf33c2016-05-13 15:28:34 -0700265 while (true) {
266 int len = TEMP_FAILURE_RETRY(recv(socket, buf, sizeof(buf), 0));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800267
Josh Gao9eaf33c2016-05-13 15:28:34 -0700268 if (len == 0) {
269 D("terminating JDWP %d connection: EOF", proc->pid);
270 break;
271 } else if (len < 0) {
272 if (len < 0 && errno == EAGAIN) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800273 return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800274 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700275
276 D("terminating JDWP %d connection: EOF", proc->pid);
277 break;
278 } else {
279 D("ignoring unexpected JDWP %d control socket activity (%d bytes)", proc->pid,
280 len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800281 }
282 }
283
Josh Gao9eaf33c2016-05-13 15:28:34 -0700284 goto CloseProcess;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800285 }
286 }
287
288 if (events & FDE_WRITE) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700289 D("trying to send fd to JDWP process (count = %zu)", proc->out_fds.size());
290 if (!proc->out_fds.empty()) {
291 int fd = proc->out_fds.back().get();
292 struct cmsghdr* cmsg;
293 struct msghdr msg;
294 struct iovec iov;
295 char dummy = '!';
296 char buffer[sizeof(struct cmsghdr) + sizeof(int)];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800297
Josh Gao9eaf33c2016-05-13 15:28:34 -0700298 iov.iov_base = &dummy;
299 iov.iov_len = 1;
300 msg.msg_name = NULL;
301 msg.msg_namelen = 0;
302 msg.msg_iov = &iov;
303 msg.msg_iovlen = 1;
304 msg.msg_flags = 0;
305 msg.msg_control = buffer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800306 msg.msg_controllen = sizeof(buffer);
307
308 cmsg = CMSG_FIRSTHDR(&msg);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700309 cmsg->cmsg_len = msg.msg_controllen;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800310 cmsg->cmsg_level = SOL_SOCKET;
Josh Gao9eaf33c2016-05-13 15:28:34 -0700311 cmsg->cmsg_type = SCM_RIGHTS;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800312 ((int*)CMSG_DATA(cmsg))[0] = fd;
313
Yabin Cui5fc22312015-10-06 15:10:05 -0700314 if (!set_file_block_mode(proc->socket, true)) {
315 VLOG(JDWP) << "failed to set blocking mode for fd " << proc->socket;
Teddie Stenvif56e7f52010-02-15 12:20:44 +0100316 goto CloseProcess;
317 }
318
Josh Gao9eaf33c2016-05-13 15:28:34 -0700319 int ret = TEMP_FAILURE_RETRY(sendmsg(proc->socket, &msg, 0));
320 if (ret < 0) {
321 D("sending new file descriptor to JDWP %d failed: %s", proc->pid, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800322 goto CloseProcess;
323 }
324
Josh Gao9eaf33c2016-05-13 15:28:34 -0700325 adb_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800326
Josh Gao9eaf33c2016-05-13 15:28:34 -0700327 D("sent file descriptor %d to JDWP process %d", fd, proc->pid);
328
329 proc->out_fds.pop_back();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800330
Yabin Cui5fc22312015-10-06 15:10:05 -0700331 if (!set_file_block_mode(proc->socket, false)) {
332 VLOG(JDWP) << "failed to set non-blocking mode for fd " << proc->socket;
Teddie Stenvif56e7f52010-02-15 12:20:44 +0100333 goto CloseProcess;
334 }
335
Josh Gao9eaf33c2016-05-13 15:28:34 -0700336 if (proc->out_fds.empty()) {
337 fdevent_del(proc->fde, FDE_WRITE);
338 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800339 }
340 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700341
342 return;
343
344CloseProcess:
345 proc->RemoveFromList();
346 jdwp_process_list_updated();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800347}
348
Josh Gao9eaf33c2016-05-13 15:28:34 -0700349int create_jdwp_connection_fd(int pid) {
Yabin Cui815ad882015-09-02 17:44:28 -0700350 D("looking for pid %d in JDWP process list", pid);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700351
352 for (auto& proc : _jdwp_list) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800353 if (proc->pid == pid) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700354 int fds[2];
355
356 if (adb_socketpair(fds) < 0) {
357 D("%s: socket pair creation failed: %s", __FUNCTION__, strerror(errno));
358 return -1;
359 }
360 D("socketpair: (%d,%d)", fds[0], fds[1]);
361
362 proc->out_fds.emplace_back(fds[1]);
363 if (proc->out_fds.size() == 1) {
364 fdevent_add(proc->fde, FDE_WRITE);
365 }
366
367 return fds[0];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800368 }
369 }
Yabin Cui815ad882015-09-02 17:44:28 -0700370 D("search failed !!");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800371 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800372}
373
374/** VM DEBUG CONTROL SOCKET
375 **
376 ** we do implement a custom asocket to receive the data
377 **/
378
379/* name of the debug control Unix socket */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700380#define JDWP_CONTROL_NAME "\0jdwp-control"
381#define JDWP_CONTROL_NAME_LEN (sizeof(JDWP_CONTROL_NAME) - 1)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800382
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700383struct JdwpControl {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700384 int listen_socket;
385 fdevent* fde;
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700386};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800387
Josh Gao9eaf33c2016-05-13 15:28:34 -0700388static JdwpControl _jdwp_control;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800389
Josh Gao9eaf33c2016-05-13 15:28:34 -0700390static void jdwp_control_event(int s, unsigned events, void* user);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800391
Josh Gao9eaf33c2016-05-13 15:28:34 -0700392static int jdwp_control_init(JdwpControl* control, const char* sockname, int socknamelen) {
393 sockaddr_un addr;
394 socklen_t addrlen;
395 int s;
396 int maxpath = sizeof(addr.sun_path);
397 int pathlen = socknamelen;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800398
399 if (pathlen >= maxpath) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700400 D("vm debug control socket name too long (%d extra chars)", pathlen + 1 - maxpath);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800401 return -1;
402 }
403
404 memset(&addr, 0, sizeof(addr));
405 addr.sun_family = AF_UNIX;
406 memcpy(addr.sun_path, sockname, socknamelen);
407
Josh Gao9eaf33c2016-05-13 15:28:34 -0700408 s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800409 if (s < 0) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700410 D("could not create vm debug control socket. %d: %s", errno, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800411 return -1;
412 }
413
Josh Gao9eaf33c2016-05-13 15:28:34 -0700414 addrlen = pathlen + sizeof(addr.sun_family);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800415
Erik Kline3c8d44d2015-12-01 17:27:59 +0900416 if (bind(s, reinterpret_cast<sockaddr*>(&addr), addrlen) < 0) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700417 D("could not bind vm debug control socket: %d: %s", errno, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800418 adb_close(s);
419 return -1;
420 }
421
Josh Gao9eaf33c2016-05-13 15:28:34 -0700422 if (listen(s, 4) < 0) {
423 D("listen failed in jdwp control socket: %d: %s", errno, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800424 adb_close(s);
425 return -1;
426 }
427
428 control->listen_socket = s;
429
430 control->fde = fdevent_create(s, jdwp_control_event, control);
431 if (control->fde == NULL) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700432 D("could not create fdevent for jdwp control socket");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800433 adb_close(s);
434 return -1;
435 }
436
437 /* only wait for incoming connections */
438 fdevent_add(control->fde, FDE_READ);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800439
Yabin Cui815ad882015-09-02 17:44:28 -0700440 D("jdwp control socket started (%d)", control->listen_socket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800441 return 0;
442}
443
Josh Gao9eaf33c2016-05-13 15:28:34 -0700444static void jdwp_control_event(int s, unsigned events, void* _control) {
445 JdwpControl* control = (JdwpControl*)_control;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800446
447 if (events & FDE_READ) {
Josh Gao782d8d42016-08-23 15:41:56 -0700448 int s = adb_socket_accept(control->listen_socket, nullptr, nullptr);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700449 if (s < 0) {
450 if (errno == ECONNABORTED) {
451 /* oops, the JDWP process died really quick */
452 D("oops, the JDWP process died really quick");
453 return;
454 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800455 /* the socket is probably closed ? */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700456 D("weird accept() failed on jdwp control socket: %s", strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800457 return;
458 }
459 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800460
Josh Gao9eaf33c2016-05-13 15:28:34 -0700461 auto proc = std::make_unique<JdwpProcess>(s);
462 if (!proc) {
463 fatal("failed to allocate JdwpProcess");
464 }
465
466 _jdwp_list.emplace_back(std::move(proc));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800467 }
468}
469
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800470/** "jdwp" local service implementation
471 ** this simply returns the list of known JDWP process pids
472 **/
473
Josh Gao9eaf33c2016-05-13 15:28:34 -0700474struct JdwpSocket : public asocket {
475 bool pass;
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700476};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800477
Josh Gao9eaf33c2016-05-13 15:28:34 -0700478static void jdwp_socket_close(asocket* s) {
479 D("LS(%d): closing jdwp socket", s->id);
480
481 if (s->peer) {
482 D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
483 s->peer->peer = nullptr;
484 s->peer->close(s->peer);
485 s->peer = nullptr;
486 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800487
488 remove_socket(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800489 free(s);
490}
491
Josh Gao9eaf33c2016-05-13 15:28:34 -0700492static int jdwp_socket_enqueue(asocket* s, apacket* p) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800493 /* you can't write to this asocket */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700494 D("LS(%d): JDWP socket received data?", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800495 put_apacket(p);
496 s->peer->close(s->peer);
497 return -1;
498}
499
Josh Gao9eaf33c2016-05-13 15:28:34 -0700500static void jdwp_socket_ready(asocket* s) {
501 JdwpSocket* jdwp = (JdwpSocket*)s;
502 asocket* peer = jdwp->peer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800503
Josh Gao9eaf33c2016-05-13 15:28:34 -0700504 /* on the first call, send the list of pids,
505 * on the second one, close the connection
506 */
507 if (!jdwp->pass) {
508 apacket* p = get_apacket();
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100509 p->len = jdwp_process_list((char*)p->data, s->get_max_payload());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800510 peer->enqueue(peer, p);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700511 jdwp->pass = true;
512 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800513 peer->close(peer);
514 }
515}
516
Josh Gao9eaf33c2016-05-13 15:28:34 -0700517asocket* create_jdwp_service_socket(void) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800518 JdwpSocket* s = reinterpret_cast<JdwpSocket*>(calloc(sizeof(*s), 1));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800519
Josh Gao9eaf33c2016-05-13 15:28:34 -0700520 if (!s) {
521 fatal("failed to allocate JdwpSocket");
522 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800523
Josh Gao9eaf33c2016-05-13 15:28:34 -0700524 install_local_socket(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800525
Josh Gao9eaf33c2016-05-13 15:28:34 -0700526 s->ready = jdwp_socket_ready;
527 s->enqueue = jdwp_socket_enqueue;
528 s->close = jdwp_socket_close;
529 s->pass = false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800530
Josh Gao9eaf33c2016-05-13 15:28:34 -0700531 return s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800532}
533
534/** "track-jdwp" local service implementation
535 ** this periodically sends the list of known JDWP process pids
536 ** to the client...
537 **/
538
Josh Gao9eaf33c2016-05-13 15:28:34 -0700539struct JdwpTracker : public asocket {
540 bool need_initial;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800541};
542
Josh Gao9eaf33c2016-05-13 15:28:34 -0700543static std::vector<std::unique_ptr<JdwpTracker>> _jdwp_trackers;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800544
Josh Gao9eaf33c2016-05-13 15:28:34 -0700545static void jdwp_process_list_updated(void) {
546 char buffer[1024];
547 int len = jdwp_process_list_msg(buffer, sizeof(buffer));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800548
Josh Gao9eaf33c2016-05-13 15:28:34 -0700549 for (auto& t : _jdwp_trackers) {
550 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800551 memcpy(p->data, buffer, len);
552 p->len = len;
Josh Gao9eaf33c2016-05-13 15:28:34 -0700553
554 if (t->peer) {
555 // The tracker might not have been connected yet.
556 t->peer->enqueue(t->peer, p);
557 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800558 }
559}
560
Josh Gao9eaf33c2016-05-13 15:28:34 -0700561static void jdwp_tracker_close(asocket* s) {
562 D("LS(%d): destroying jdwp tracker service", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800563
Josh Gao9eaf33c2016-05-13 15:28:34 -0700564 if (s->peer) {
565 D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
566 s->peer->peer = nullptr;
567 s->peer->close(s->peer);
568 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800569 }
570
571 remove_socket(s);
572
Josh Gao9eaf33c2016-05-13 15:28:34 -0700573 auto pred = [s](const auto& tracker) { return tracker.get() == s; };
574 std::remove_if(_jdwp_trackers.begin(), _jdwp_trackers.end(), pred);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800575}
576
Josh Gao9eaf33c2016-05-13 15:28:34 -0700577static void jdwp_tracker_ready(asocket* s) {
578 JdwpTracker* t = (JdwpTracker*)s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800579
Josh Gao9eaf33c2016-05-13 15:28:34 -0700580 if (t->need_initial) {
581 apacket* p = get_apacket();
582 t->need_initial = false;
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100583 p->len = jdwp_process_list_msg((char*)p->data, s->get_max_payload());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800584 s->peer->enqueue(s->peer, p);
585 }
586}
587
Josh Gao9eaf33c2016-05-13 15:28:34 -0700588static int jdwp_tracker_enqueue(asocket* s, apacket* p) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800589 /* you can't write to this socket */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700590 D("LS(%d): JDWP tracker received data?", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800591 put_apacket(p);
592 s->peer->close(s->peer);
593 return -1;
594}
595
Josh Gao9eaf33c2016-05-13 15:28:34 -0700596asocket* create_jdwp_tracker_service_socket(void) {
597 auto t = std::make_unique<JdwpTracker>();
598 if (!t) {
599 fatal("failed to allocate JdwpTracker");
600 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800601
Josh Gao9eaf33c2016-05-13 15:28:34 -0700602 memset(t.get(), 0, sizeof(asocket));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800603
Josh Gao9eaf33c2016-05-13 15:28:34 -0700604 install_local_socket(t.get());
605 D("LS(%d): created new jdwp tracker service", t->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800606
Josh Gao9eaf33c2016-05-13 15:28:34 -0700607 t->ready = jdwp_tracker_ready;
608 t->enqueue = jdwp_tracker_enqueue;
609 t->close = jdwp_tracker_close;
610 t->need_initial = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800611
Josh Gao9eaf33c2016-05-13 15:28:34 -0700612 asocket* result = t.get();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800613
Josh Gao9eaf33c2016-05-13 15:28:34 -0700614 _jdwp_trackers.emplace_back(std::move(t));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800615
Josh Gao9eaf33c2016-05-13 15:28:34 -0700616 return result;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800617}
618
Josh Gao9eaf33c2016-05-13 15:28:34 -0700619int init_jdwp(void) {
620 return jdwp_control_init(&_jdwp_control, JDWP_CONTROL_NAME, JDWP_CONTROL_NAME_LEN);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800621}
622
623#endif /* !ADB_HOST */