blob: 0fa07321b8ab9904e063e379d878c16cd314e800 [file] [log] [blame]
Dan Albert33134262015-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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080017/* implement the "debug-ports" and "track-debug-ports" device services */
Dan Albert33134262015-03-19 15:21:08 -070018
19#define TRACE_TAG TRACE_JDWP
20
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include "sysdeps.h"
Dan Albert33134262015-03-19 15:21:08 -070022
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <errno.h>
24#include <stdio.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070025#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include <string.h>
Teddie Stenvi8f5daad2010-02-15 12:20:44 +010027#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Dan Albert33134262015-03-19 15:21:08 -070029#include "adb.h"
30
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031/* here's how these things work.
32
33 when adbd starts, it creates a unix server socket
34 named @vm-debug-control (@ is a shortcut for "first byte is zero"
35 to use the private namespace instead of the file system)
36
37 when a new JDWP daemon thread starts in a new VM process, it creates
38 a connection to @vm-debug-control to announce its availability.
39
40
41 JDWP thread @vm-debug-control
42 | |
43 |-------------------------------> |
44 | hello I'm in process <pid> |
45 | |
46 | |
47
48 the connection is kept alive. it will be closed automatically if
49 the JDWP process terminates (this allows adbd to detect dead
50 processes).
51
52 adbd thus maintains a list of "active" JDWP processes. it can send
53 its content to clients through the "device:debug-ports" service,
54 or even updates through the "device:track-debug-ports" service.
55
56 when a debugger wants to connect, it simply runs the command
57 equivalent to "adb forward tcp:<hostport> jdwp:<pid>"
58
59 "jdwp:<pid>" is a new forward destination format used to target
60 a given JDWP process on the device. when sutch a request arrives,
61 adbd does the following:
62
63 - first, it calls socketpair() to create a pair of equivalent
64 sockets.
65
66 - it attaches the first socket in the pair to a local socket
67 which is itself attached to the transport's remote socket:
68
69
70 - it sends the file descriptor of the second socket directly
71 to the JDWP process with the help of sendmsg()
72
73
74 JDWP thread @vm-debug-control
75 | |
76 | <----------------------|
77 | OK, try this file descriptor |
78 | |
79 | |
80
81 then, the JDWP thread uses this new socket descriptor as its
82 pass-through connection to the debugger (and receives the
83 JDWP-Handshake message, answers to it, etc...)
84
85 this gives the following graphics:
86 ____________________________________
87 | |
88 | ADB Server (host) |
89 | |
90 Debugger <---> LocalSocket <----> RemoteSocket |
91 | ^^ |
92 |___________________________||_______|
93 ||
94 Transport ||
95 (TCP for emulator - USB for device) ||
96 ||
97 ___________________________||_______
98 | || |
99 | ADBD (device) || |
100 | VV |
101 JDWP <======> LocalSocket <----> RemoteSocket |
102 | |
103 |____________________________________|
104
105 due to the way adb works, this doesn't need a special socket
106 type or fancy handling of socket termination if either the debugger
107 or the JDWP process closes the connection.
108
109 THIS IS THE SIMPLEST IMPLEMENTATION I COULD FIND, IF YOU HAPPEN
110 TO HAVE A BETTER IDEA, LET ME KNOW - Digit
111
112**********************************************************************/
113
114/** JDWP PID List Support Code
115 ** for each JDWP process, we record its pid and its connected socket
116 **/
117
118#define MAX_OUT_FDS 4
119
120#if !ADB_HOST
121
122#include <sys/socket.h>
123#include <sys/un.h>
124
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125struct JdwpProcess {
126 JdwpProcess* next;
127 JdwpProcess* prev;
128 int pid;
129 int socket;
130 fdevent* fde;
131
132 char in_buff[4]; /* input character to read PID */
133 int in_len; /* number from JDWP process */
134
135 int out_fds[MAX_OUT_FDS]; /* output array of file descriptors */
136 int out_count; /* to send to the JDWP process */
137};
138
139static JdwpProcess _jdwp_list;
140
141static int
142jdwp_process_list( char* buffer, int bufferlen )
143{
144 char* end = buffer + bufferlen;
145 char* p = buffer;
146 JdwpProcess* proc = _jdwp_list.next;
147
148 for ( ; proc != &_jdwp_list; proc = proc->next ) {
149 int len;
150
151 /* skip transient connections */
152 if (proc->pid < 0)
153 continue;
154
155 len = snprintf(p, end-p, "%d\n", proc->pid);
156 if (p + len >= end)
157 break;
158 p += len;
159 }
160 p[0] = 0;
161 return (p - buffer);
162}
163
164
165static int
166jdwp_process_list_msg( char* buffer, int bufferlen )
167{
168 char head[5];
169 int len = jdwp_process_list( buffer+4, bufferlen-4 );
170 snprintf(head, sizeof head, "%04x", len);
171 memcpy(buffer, head, 4);
172 return len + 4;
173}
174
175
176static void jdwp_process_list_updated(void);
177
178static void
179jdwp_process_free( JdwpProcess* proc )
180{
181 if (proc) {
182 int n;
183
184 proc->prev->next = proc->next;
185 proc->next->prev = proc->prev;
186
187 if (proc->socket >= 0) {
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400188 adb_shutdown(proc->socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 adb_close(proc->socket);
190 proc->socket = -1;
191 }
192
193 if (proc->fde != NULL) {
194 fdevent_destroy(proc->fde);
195 proc->fde = NULL;
196 }
197 proc->pid = -1;
198
199 for (n = 0; n < proc->out_count; n++) {
200 adb_close(proc->out_fds[n]);
201 }
202 proc->out_count = 0;
203
204 free(proc);
205
206 jdwp_process_list_updated();
207 }
208}
209
210
211static void jdwp_process_event(int, unsigned, void*); /* forward */
212
213
214static JdwpProcess*
215jdwp_process_alloc( int socket )
216{
Dan Albertbac34742015-02-25 17:51:28 -0800217 JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(
218 calloc(1, sizeof(*proc)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219
220 if (proc == NULL) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700221 D("not enough memory to create new JDWP process");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 return NULL;
223 }
224
225 proc->socket = socket;
226 proc->pid = -1;
227 proc->next = proc;
228 proc->prev = proc;
229
230 proc->fde = fdevent_create( socket, jdwp_process_event, proc );
231 if (proc->fde == NULL) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700232 D("could not create fdevent for new JDWP process" );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 free(proc);
234 return NULL;
235 }
236
237 proc->fde->state |= FDE_DONT_CLOSE;
238 proc->in_len = 0;
239 proc->out_count = 0;
240
241 /* append to list */
242 proc->next = &_jdwp_list;
243 proc->prev = proc->next->prev;
244
245 proc->prev->next = proc;
246 proc->next->prev = proc;
247
248 /* start by waiting for the PID */
249 fdevent_add(proc->fde, FDE_READ);
250
251 return proc;
252}
253
254
255static void
256jdwp_process_event( int socket, unsigned events, void* _proc )
257{
Dan Albertbac34742015-02-25 17:51:28 -0800258 JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(_proc);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259
260 if (events & FDE_READ) {
261 if (proc->pid < 0) {
262 /* read the PID as a 4-hexchar string */
263 char* p = proc->in_buff + proc->in_len;
264 int size = 4 - proc->in_len;
265 char temp[5];
266 while (size > 0) {
267 int len = recv( socket, p, size, 0 );
268 if (len < 0) {
269 if (errno == EINTR)
270 continue;
271 if (errno == EAGAIN)
272 return;
273 /* this can fail here if the JDWP process crashes very fast */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700274 D("weird unknown JDWP process failure: %s",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275 strerror(errno));
276
277 goto CloseProcess;
278 }
279 if (len == 0) { /* end of stream ? */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700280 D("weird end-of-stream from unknown JDWP process");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281 goto CloseProcess;
282 }
283 p += len;
284 proc->in_len += len;
285 size -= len;
286 }
287 /* we have read 4 characters, now decode the pid */
288 memcpy(temp, proc->in_buff, 4);
289 temp[4] = 0;
290
291 if (sscanf( temp, "%04x", &proc->pid ) != 1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700292 D("could not decode JDWP %p PID number: '%s'", proc, temp);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 goto CloseProcess;
294 }
295
296 /* all is well, keep reading to detect connection closure */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700297 D("Adding pid %d to jdwp process list", proc->pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 jdwp_process_list_updated();
299 }
300 else
301 {
302 /* the pid was read, if we get there it's probably because the connection
303 * was closed (e.g. the JDWP process exited or crashed) */
304 char buf[32];
305
306 for (;;) {
307 int len = recv(socket, buf, sizeof(buf), 0);
308
309 if (len <= 0) {
310 if (len < 0 && errno == EINTR)
311 continue;
312 if (len < 0 && errno == EAGAIN)
313 return;
314 else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700315 D("terminating JDWP %d connection: %s", proc->pid,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 strerror(errno));
317 break;
318 }
319 }
320 else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700321 D( "ignoring unexpected JDWP %d control socket activity (%d bytes)",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 proc->pid, len );
323 }
324 }
325
326 CloseProcess:
327 if (proc->pid >= 0)
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700328 D( "remove pid %d to jdwp process list", proc->pid );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 jdwp_process_free(proc);
330 return;
331 }
332 }
333
334 if (events & FDE_WRITE) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700335 D("trying to write to JDWP pid controli (count=%d first=%d) %d",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 proc->pid, proc->out_count, proc->out_fds[0]);
337 if (proc->out_count > 0) {
338 int fd = proc->out_fds[0];
339 int n, ret;
340 struct cmsghdr* cmsg;
341 struct msghdr msg;
342 struct iovec iov;
343 char dummy = '!';
344 char buffer[sizeof(struct cmsghdr) + sizeof(int)];
Teddie Stenvi8f5daad2010-02-15 12:20:44 +0100345 int flags;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346
347 iov.iov_base = &dummy;
348 iov.iov_len = 1;
349 msg.msg_name = NULL;
350 msg.msg_namelen = 0;
351 msg.msg_iov = &iov;
352 msg.msg_iovlen = 1;
353 msg.msg_flags = 0;
354 msg.msg_control = buffer;
355 msg.msg_controllen = sizeof(buffer);
356
357 cmsg = CMSG_FIRSTHDR(&msg);
358 cmsg->cmsg_len = msg.msg_controllen;
359 cmsg->cmsg_level = SOL_SOCKET;
360 cmsg->cmsg_type = SCM_RIGHTS;
361 ((int*)CMSG_DATA(cmsg))[0] = fd;
362
Teddie Stenvi8f5daad2010-02-15 12:20:44 +0100363 flags = fcntl(proc->socket,F_GETFL,0);
364
365 if (flags == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700366 D("failed to get cntl flags for socket %d: %s",
Teddie Stenvi8f5daad2010-02-15 12:20:44 +0100367 proc->pid, strerror(errno));
368 goto CloseProcess;
369
370 }
371
372 if (fcntl(proc->socket, F_SETFL, flags & ~O_NONBLOCK) == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700373 D("failed to remove O_NONBLOCK flag for socket %d: %s",
Teddie Stenvi8f5daad2010-02-15 12:20:44 +0100374 proc->pid, strerror(errno));
375 goto CloseProcess;
376 }
377
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 for (;;) {
379 ret = sendmsg(proc->socket, &msg, 0);
Teddie Stenvi8f5daad2010-02-15 12:20:44 +0100380 if (ret >= 0) {
381 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382 break;
Teddie Stenvi8f5daad2010-02-15 12:20:44 +0100383 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384 if (errno == EINTR)
385 continue;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700386 D("sending new file descriptor to JDWP %d failed: %s",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 proc->pid, strerror(errno));
388 goto CloseProcess;
389 }
390
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700391 D("sent file descriptor %d to JDWP process %d",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 fd, proc->pid);
393
394 for (n = 1; n < proc->out_count; n++)
395 proc->out_fds[n-1] = proc->out_fds[n];
396
Teddie Stenvi8f5daad2010-02-15 12:20:44 +0100397 if (fcntl(proc->socket, F_SETFL, flags) == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700398 D("failed to set O_NONBLOCK flag for socket %d: %s",
Teddie Stenvi8f5daad2010-02-15 12:20:44 +0100399 proc->pid, strerror(errno));
400 goto CloseProcess;
401 }
402
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403 if (--proc->out_count == 0)
404 fdevent_del( proc->fde, FDE_WRITE );
405 }
406 }
407}
408
409
410int
411create_jdwp_connection_fd(int pid)
412{
413 JdwpProcess* proc = _jdwp_list.next;
414
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700415 D("looking for pid %d in JDWP process list", pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 for ( ; proc != &_jdwp_list; proc = proc->next ) {
417 if (proc->pid == pid) {
418 goto FoundIt;
419 }
420 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700421 D("search failed !!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 return -1;
423
424FoundIt:
425 {
426 int fds[2];
427
428 if (proc->out_count >= MAX_OUT_FDS) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700429 D("%s: too many pending JDWP connection for pid %d",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 __FUNCTION__, pid);
431 return -1;
432 }
433
434 if (adb_socketpair(fds) < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700435 D("%s: socket pair creation failed: %s",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 __FUNCTION__, strerror(errno));
437 return -1;
438 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700439 D("socketpair: (%d,%d)", fds[0], fds[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440
441 proc->out_fds[ proc->out_count ] = fds[1];
442 if (++proc->out_count == 1)
443 fdevent_add( proc->fde, FDE_WRITE );
444
445 return fds[0];
446 }
447}
448
449/** VM DEBUG CONTROL SOCKET
450 **
451 ** we do implement a custom asocket to receive the data
452 **/
453
454/* name of the debug control Unix socket */
455#define JDWP_CONTROL_NAME "\0jdwp-control"
456#define JDWP_CONTROL_NAME_LEN (sizeof(JDWP_CONTROL_NAME)-1)
457
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700458struct JdwpControl {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 int listen_socket;
460 fdevent* fde;
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700461};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462
463
464static void
465jdwp_control_event(int s, unsigned events, void* user);
466
467
468static int
469jdwp_control_init( JdwpControl* control,
470 const char* sockname,
471 int socknamelen )
472{
473 struct sockaddr_un addr;
474 socklen_t addrlen;
475 int s;
476 int maxpath = sizeof(addr.sun_path);
477 int pathlen = socknamelen;
478
479 if (pathlen >= maxpath) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700480 D( "vm debug control socket name too long (%d extra chars)",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 pathlen+1-maxpath );
482 return -1;
483 }
484
485 memset(&addr, 0, sizeof(addr));
486 addr.sun_family = AF_UNIX;
487 memcpy(addr.sun_path, sockname, socknamelen);
488
489 s = socket( AF_UNIX, SOCK_STREAM, 0 );
490 if (s < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700491 D( "could not create vm debug control socket. %d: %s",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 errno, strerror(errno));
493 return -1;
494 }
495
496 addrlen = (pathlen + sizeof(addr.sun_family));
497
498 if (bind(s, (struct sockaddr*)&addr, addrlen) < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700499 D( "could not bind vm debug control socket: %d: %s",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 errno, strerror(errno) );
501 adb_close(s);
502 return -1;
503 }
504
505 if ( listen(s, 4) < 0 ) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700506 D("listen failed in jdwp control socket: %d: %s",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507 errno, strerror(errno));
508 adb_close(s);
509 return -1;
510 }
511
512 control->listen_socket = s;
513
514 control->fde = fdevent_create(s, jdwp_control_event, control);
515 if (control->fde == NULL) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700516 D( "could not create fdevent for jdwp control socket" );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 adb_close(s);
518 return -1;
519 }
520
521 /* only wait for incoming connections */
522 fdevent_add(control->fde, FDE_READ);
Benoit Goby95ef8282011-02-01 18:57:41 -0800523 close_on_exec(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700525 D("jdwp control socket started (%d)", control->listen_socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 return 0;
527}
528
529
530static void
531jdwp_control_event( int s, unsigned events, void* _control )
532{
533 JdwpControl* control = (JdwpControl*) _control;
534
535 if (events & FDE_READ) {
536 struct sockaddr addr;
537 socklen_t addrlen = sizeof(addr);
538 int s = -1;
539 JdwpProcess* proc;
540
541 do {
542 s = adb_socket_accept( control->listen_socket, &addr, &addrlen );
543 if (s < 0) {
544 if (errno == EINTR)
545 continue;
546 if (errno == ECONNABORTED) {
547 /* oops, the JDWP process died really quick */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700548 D("oops, the JDWP process died really quick");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800549 return;
550 }
551 /* the socket is probably closed ? */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700552 D( "weird accept() failed on jdwp control socket: %s",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553 strerror(errno) );
554 return;
555 }
556 }
557 while (s < 0);
558
559 proc = jdwp_process_alloc( s );
560 if (proc == NULL)
561 return;
562 }
563}
564
565
566static JdwpControl _jdwp_control;
567
568/** "jdwp" local service implementation
569 ** this simply returns the list of known JDWP process pids
570 **/
571
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700572struct JdwpSocket {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800573 asocket socket;
574 int pass;
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700575};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800576
577static void
578jdwp_socket_close( asocket* s )
579{
580 asocket* peer = s->peer;
581
582 remove_socket(s);
583
584 if (peer) {
585 peer->peer = NULL;
586 peer->close(peer);
587 }
588 free(s);
589}
590
591static int
592jdwp_socket_enqueue( asocket* s, apacket* p )
593{
594 /* you can't write to this asocket */
595 put_apacket(p);
596 s->peer->close(s->peer);
597 return -1;
598}
599
600
601static void
602jdwp_socket_ready( asocket* s )
603{
604 JdwpSocket* jdwp = (JdwpSocket*)s;
605 asocket* peer = jdwp->socket.peer;
606
607 /* on the first call, send the list of pids,
608 * on the second one, close the connection
609 */
610 if (jdwp->pass == 0) {
611 apacket* p = get_apacket();
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100612 p->len = jdwp_process_list((char*)p->data, s->get_max_payload());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800613 peer->enqueue(peer, p);
614 jdwp->pass = 1;
615 }
616 else {
617 peer->close(peer);
618 }
619}
620
621asocket*
622create_jdwp_service_socket( void )
623{
Dan Albertbac34742015-02-25 17:51:28 -0800624 JdwpSocket* s = reinterpret_cast<JdwpSocket*>(calloc(sizeof(*s), 1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800625
626 if (s == NULL)
627 return NULL;
628
629 install_local_socket(&s->socket);
630
631 s->socket.ready = jdwp_socket_ready;
632 s->socket.enqueue = jdwp_socket_enqueue;
633 s->socket.close = jdwp_socket_close;
634 s->pass = 0;
635
636 return &s->socket;
637}
638
639/** "track-jdwp" local service implementation
640 ** this periodically sends the list of known JDWP process pids
641 ** to the client...
642 **/
643
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800644struct JdwpTracker {
645 asocket socket;
646 JdwpTracker* next;
647 JdwpTracker* prev;
648 int need_update;
649};
650
651static JdwpTracker _jdwp_trackers_list;
652
653
654static void
655jdwp_process_list_updated(void)
656{
657 char buffer[1024];
658 int len;
659 JdwpTracker* t = _jdwp_trackers_list.next;
660
661 len = jdwp_process_list_msg(buffer, sizeof(buffer));
662
663 for ( ; t != &_jdwp_trackers_list; t = t->next ) {
664 apacket* p = get_apacket();
665 asocket* peer = t->socket.peer;
666 memcpy(p->data, buffer, len);
667 p->len = len;
668 peer->enqueue( peer, p );
669 }
670}
671
672static void
673jdwp_tracker_close( asocket* s )
674{
675 JdwpTracker* tracker = (JdwpTracker*) s;
676 asocket* peer = s->peer;
677
678 if (peer) {
679 peer->peer = NULL;
680 peer->close(peer);
681 }
682
683 remove_socket(s);
684
685 tracker->prev->next = tracker->next;
686 tracker->next->prev = tracker->prev;
687
688 free(s);
689}
690
691static void
692jdwp_tracker_ready( asocket* s )
693{
694 JdwpTracker* t = (JdwpTracker*) s;
695
696 if (t->need_update) {
697 apacket* p = get_apacket();
698 t->need_update = 0;
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100699 p->len = jdwp_process_list_msg((char*)p->data, s->get_max_payload());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800700 s->peer->enqueue(s->peer, p);
701 }
702}
703
704static int
705jdwp_tracker_enqueue( asocket* s, apacket* p )
706{
707 /* you can't write to this socket */
708 put_apacket(p);
709 s->peer->close(s->peer);
710 return -1;
711}
712
713
714asocket*
715create_jdwp_tracker_service_socket( void )
716{
Dan Albertbac34742015-02-25 17:51:28 -0800717 JdwpTracker* t = reinterpret_cast<JdwpTracker*>(calloc(sizeof(*t), 1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718
719 if (t == NULL)
720 return NULL;
721
722 t->next = &_jdwp_trackers_list;
723 t->prev = t->next->prev;
724
725 t->next->prev = t;
726 t->prev->next = t;
727
728 install_local_socket(&t->socket);
729
730 t->socket.ready = jdwp_tracker_ready;
731 t->socket.enqueue = jdwp_tracker_enqueue;
732 t->socket.close = jdwp_tracker_close;
733 t->need_update = 1;
734
735 return &t->socket;
736}
737
738
739int
740init_jdwp(void)
741{
742 _jdwp_list.next = &_jdwp_list;
743 _jdwp_list.prev = &_jdwp_list;
744
745 _jdwp_trackers_list.next = &_jdwp_trackers_list;
746 _jdwp_trackers_list.prev = &_jdwp_trackers_list;
747
748 return jdwp_control_init( &_jdwp_control,
749 JDWP_CONTROL_NAME,
750 JDWP_CONTROL_NAME_LEN );
751}
752
753#endif /* !ADB_HOST */