blob: 4f0f740aff129c89caae9afc4b0253d971a69f63 [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
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080017/* implement the "debug-ports" and "track-debug-ports" device services */
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>
Teddie Stenvif56e7f52010-02-15 12:20:44 +010027#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080028
Dan Albertdb6fe642015-03-19 15:21:08 -070029#include "adb.h"
30
The Android Open Source Project9ca14dc2009-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 Project9ca14dc2009-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 Lockwood81ffe172009-10-11 23:04:18 -0400188 adb_shutdown(proc->socket);
The Android Open Source Project9ca14dc2009-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 Albertf30d73c2015-02-25 17:51:28 -0800217 JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(
218 calloc(1, sizeof(*proc)));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800219
220 if (proc == NULL) {
Yabin Cui815ad882015-09-02 17:44:28 -0700221 D("not enough memory to create new JDWP process");
The Android Open Source Project9ca14dc2009-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 Cui815ad882015-09-02 17:44:28 -0700232 D("could not create fdevent for new JDWP process" );
The Android Open Source Project9ca14dc2009-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 Albertf30d73c2015-02-25 17:51:28 -0800258 JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(_proc);
The Android Open Source Project9ca14dc2009-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 Cui815ad882015-09-02 17:44:28 -0700274 D("weird unknown JDWP process failure: %s",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800275 strerror(errno));
276
277 goto CloseProcess;
278 }
279 if (len == 0) { /* end of stream ? */
Yabin Cui815ad882015-09-02 17:44:28 -0700280 D("weird end-of-stream from unknown JDWP process");
The Android Open Source Project9ca14dc2009-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 Cui815ad882015-09-02 17:44:28 -0700292 D("could not decode JDWP %p PID number: '%s'", proc, temp);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800293 goto CloseProcess;
294 }
295
296 /* all is well, keep reading to detect connection closure */
Yabin Cui815ad882015-09-02 17:44:28 -0700297 D("Adding pid %d to jdwp process list", proc->pid);
The Android Open Source Project9ca14dc2009-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 Cui815ad882015-09-02 17:44:28 -0700315 D("terminating JDWP %d connection: %s", proc->pid,
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800316 strerror(errno));
317 break;
318 }
319 }
320 else {
Yabin Cui815ad882015-09-02 17:44:28 -0700321 D( "ignoring unexpected JDWP %d control socket activity (%d bytes)",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800322 proc->pid, len );
323 }
324 }
325
326 CloseProcess:
Yabin Cui19bec5b2015-09-22 15:52:57 -0700327 if (proc->pid >= 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700328 D( "remove pid %d to jdwp process list", proc->pid );
Yabin Cui19bec5b2015-09-22 15:52:57 -0700329 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800330 jdwp_process_free(proc);
331 return;
332 }
333 }
334
335 if (events & FDE_WRITE) {
Yabin Cui815ad882015-09-02 17:44:28 -0700336 D("trying to write to JDWP pid controli (count=%d first=%d) %d",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800337 proc->pid, proc->out_count, proc->out_fds[0]);
338 if (proc->out_count > 0) {
339 int fd = proc->out_fds[0];
340 int n, ret;
341 struct cmsghdr* cmsg;
342 struct msghdr msg;
343 struct iovec iov;
344 char dummy = '!';
345 char buffer[sizeof(struct cmsghdr) + sizeof(int)];
Teddie Stenvif56e7f52010-02-15 12:20:44 +0100346 int flags;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800347
348 iov.iov_base = &dummy;
349 iov.iov_len = 1;
350 msg.msg_name = NULL;
351 msg.msg_namelen = 0;
352 msg.msg_iov = &iov;
353 msg.msg_iovlen = 1;
354 msg.msg_flags = 0;
355 msg.msg_control = buffer;
356 msg.msg_controllen = sizeof(buffer);
357
358 cmsg = CMSG_FIRSTHDR(&msg);
359 cmsg->cmsg_len = msg.msg_controllen;
360 cmsg->cmsg_level = SOL_SOCKET;
361 cmsg->cmsg_type = SCM_RIGHTS;
362 ((int*)CMSG_DATA(cmsg))[0] = fd;
363
Teddie Stenvif56e7f52010-02-15 12:20:44 +0100364 flags = fcntl(proc->socket,F_GETFL,0);
365
366 if (flags == -1) {
Yabin Cui815ad882015-09-02 17:44:28 -0700367 D("failed to get cntl flags for socket %d: %s",
Teddie Stenvif56e7f52010-02-15 12:20:44 +0100368 proc->pid, strerror(errno));
369 goto CloseProcess;
370
371 }
372
373 if (fcntl(proc->socket, F_SETFL, flags & ~O_NONBLOCK) == -1) {
Yabin Cui815ad882015-09-02 17:44:28 -0700374 D("failed to remove O_NONBLOCK flag for socket %d: %s",
Teddie Stenvif56e7f52010-02-15 12:20:44 +0100375 proc->pid, strerror(errno));
376 goto CloseProcess;
377 }
378
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800379 for (;;) {
380 ret = sendmsg(proc->socket, &msg, 0);
Teddie Stenvif56e7f52010-02-15 12:20:44 +0100381 if (ret >= 0) {
382 adb_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800383 break;
Teddie Stenvif56e7f52010-02-15 12:20:44 +0100384 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800385 if (errno == EINTR)
386 continue;
Yabin Cui815ad882015-09-02 17:44:28 -0700387 D("sending new file descriptor to JDWP %d failed: %s",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800388 proc->pid, strerror(errno));
389 goto CloseProcess;
390 }
391
Yabin Cui815ad882015-09-02 17:44:28 -0700392 D("sent file descriptor %d to JDWP process %d",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800393 fd, proc->pid);
394
395 for (n = 1; n < proc->out_count; n++)
396 proc->out_fds[n-1] = proc->out_fds[n];
397
Teddie Stenvif56e7f52010-02-15 12:20:44 +0100398 if (fcntl(proc->socket, F_SETFL, flags) == -1) {
Yabin Cui815ad882015-09-02 17:44:28 -0700399 D("failed to set O_NONBLOCK flag for socket %d: %s",
Teddie Stenvif56e7f52010-02-15 12:20:44 +0100400 proc->pid, strerror(errno));
401 goto CloseProcess;
402 }
403
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800404 if (--proc->out_count == 0)
405 fdevent_del( proc->fde, FDE_WRITE );
406 }
407 }
408}
409
410
411int
412create_jdwp_connection_fd(int pid)
413{
414 JdwpProcess* proc = _jdwp_list.next;
415
Yabin Cui815ad882015-09-02 17:44:28 -0700416 D("looking for pid %d in JDWP process list", pid);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800417 for ( ; proc != &_jdwp_list; proc = proc->next ) {
418 if (proc->pid == pid) {
419 goto FoundIt;
420 }
421 }
Yabin Cui815ad882015-09-02 17:44:28 -0700422 D("search failed !!");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800423 return -1;
424
425FoundIt:
426 {
427 int fds[2];
428
429 if (proc->out_count >= MAX_OUT_FDS) {
Yabin Cui815ad882015-09-02 17:44:28 -0700430 D("%s: too many pending JDWP connection for pid %d",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800431 __FUNCTION__, pid);
432 return -1;
433 }
434
435 if (adb_socketpair(fds) < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700436 D("%s: socket pair creation failed: %s",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800437 __FUNCTION__, strerror(errno));
438 return -1;
439 }
Yabin Cui815ad882015-09-02 17:44:28 -0700440 D("socketpair: (%d,%d)", fds[0], fds[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800441
442 proc->out_fds[ proc->out_count ] = fds[1];
443 if (++proc->out_count == 1)
444 fdevent_add( proc->fde, FDE_WRITE );
445
446 return fds[0];
447 }
448}
449
450/** VM DEBUG CONTROL SOCKET
451 **
452 ** we do implement a custom asocket to receive the data
453 **/
454
455/* name of the debug control Unix socket */
456#define JDWP_CONTROL_NAME "\0jdwp-control"
457#define JDWP_CONTROL_NAME_LEN (sizeof(JDWP_CONTROL_NAME)-1)
458
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700459struct JdwpControl {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800460 int listen_socket;
461 fdevent* fde;
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700462};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800463
464
465static void
466jdwp_control_event(int s, unsigned events, void* user);
467
468
469static int
470jdwp_control_init( JdwpControl* control,
471 const char* sockname,
472 int socknamelen )
473{
474 struct sockaddr_un addr;
475 socklen_t addrlen;
476 int s;
477 int maxpath = sizeof(addr.sun_path);
478 int pathlen = socknamelen;
479
480 if (pathlen >= maxpath) {
Yabin Cui815ad882015-09-02 17:44:28 -0700481 D( "vm debug control socket name too long (%d extra chars)",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800482 pathlen+1-maxpath );
483 return -1;
484 }
485
486 memset(&addr, 0, sizeof(addr));
487 addr.sun_family = AF_UNIX;
488 memcpy(addr.sun_path, sockname, socknamelen);
489
490 s = socket( AF_UNIX, SOCK_STREAM, 0 );
491 if (s < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700492 D( "could not create vm debug control socket. %d: %s",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800493 errno, strerror(errno));
494 return -1;
495 }
496
497 addrlen = (pathlen + sizeof(addr.sun_family));
498
499 if (bind(s, (struct sockaddr*)&addr, addrlen) < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700500 D( "could not bind vm debug control socket: %d: %s",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800501 errno, strerror(errno) );
502 adb_close(s);
503 return -1;
504 }
505
506 if ( listen(s, 4) < 0 ) {
Yabin Cui815ad882015-09-02 17:44:28 -0700507 D("listen failed in jdwp control socket: %d: %s",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800508 errno, strerror(errno));
509 adb_close(s);
510 return -1;
511 }
512
513 control->listen_socket = s;
514
515 control->fde = fdevent_create(s, jdwp_control_event, control);
516 if (control->fde == NULL) {
Yabin Cui815ad882015-09-02 17:44:28 -0700517 D( "could not create fdevent for jdwp control socket" );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800518 adb_close(s);
519 return -1;
520 }
521
522 /* only wait for incoming connections */
523 fdevent_add(control->fde, FDE_READ);
Benoit Gobyf4ded742011-02-01 18:57:41 -0800524 close_on_exec(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800525
Yabin Cui815ad882015-09-02 17:44:28 -0700526 D("jdwp control socket started (%d)", control->listen_socket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800527 return 0;
528}
529
530
531static void
532jdwp_control_event( int s, unsigned events, void* _control )
533{
534 JdwpControl* control = (JdwpControl*) _control;
535
536 if (events & FDE_READ) {
537 struct sockaddr addr;
538 socklen_t addrlen = sizeof(addr);
539 int s = -1;
540 JdwpProcess* proc;
541
542 do {
543 s = adb_socket_accept( control->listen_socket, &addr, &addrlen );
544 if (s < 0) {
545 if (errno == EINTR)
546 continue;
547 if (errno == ECONNABORTED) {
548 /* oops, the JDWP process died really quick */
Yabin Cui815ad882015-09-02 17:44:28 -0700549 D("oops, the JDWP process died really quick");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800550 return;
551 }
552 /* the socket is probably closed ? */
Yabin Cui815ad882015-09-02 17:44:28 -0700553 D( "weird accept() failed on jdwp control socket: %s",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800554 strerror(errno) );
555 return;
556 }
557 }
558 while (s < 0);
559
560 proc = jdwp_process_alloc( s );
561 if (proc == NULL)
562 return;
563 }
564}
565
566
567static JdwpControl _jdwp_control;
568
569/** "jdwp" local service implementation
570 ** this simply returns the list of known JDWP process pids
571 **/
572
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700573struct JdwpSocket {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800574 asocket socket;
575 int pass;
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700576};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800577
578static void
579jdwp_socket_close( asocket* s )
580{
581 asocket* peer = s->peer;
582
583 remove_socket(s);
584
585 if (peer) {
586 peer->peer = NULL;
587 peer->close(peer);
588 }
589 free(s);
590}
591
592static int
593jdwp_socket_enqueue( asocket* s, apacket* p )
594{
595 /* you can't write to this asocket */
596 put_apacket(p);
597 s->peer->close(s->peer);
598 return -1;
599}
600
601
602static void
603jdwp_socket_ready( asocket* s )
604{
605 JdwpSocket* jdwp = (JdwpSocket*)s;
606 asocket* peer = jdwp->socket.peer;
607
608 /* on the first call, send the list of pids,
609 * on the second one, close the connection
610 */
611 if (jdwp->pass == 0) {
612 apacket* p = get_apacket();
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100613 p->len = jdwp_process_list((char*)p->data, s->get_max_payload());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800614 peer->enqueue(peer, p);
615 jdwp->pass = 1;
616 }
617 else {
618 peer->close(peer);
619 }
620}
621
622asocket*
623create_jdwp_service_socket( void )
624{
Dan Albertf30d73c2015-02-25 17:51:28 -0800625 JdwpSocket* s = reinterpret_cast<JdwpSocket*>(calloc(sizeof(*s), 1));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800626
627 if (s == NULL)
628 return NULL;
629
630 install_local_socket(&s->socket);
631
632 s->socket.ready = jdwp_socket_ready;
633 s->socket.enqueue = jdwp_socket_enqueue;
634 s->socket.close = jdwp_socket_close;
635 s->pass = 0;
636
637 return &s->socket;
638}
639
640/** "track-jdwp" local service implementation
641 ** this periodically sends the list of known JDWP process pids
642 ** to the client...
643 **/
644
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800645struct JdwpTracker {
646 asocket socket;
647 JdwpTracker* next;
648 JdwpTracker* prev;
649 int need_update;
650};
651
652static JdwpTracker _jdwp_trackers_list;
653
654
655static void
656jdwp_process_list_updated(void)
657{
658 char buffer[1024];
659 int len;
660 JdwpTracker* t = _jdwp_trackers_list.next;
661
662 len = jdwp_process_list_msg(buffer, sizeof(buffer));
663
664 for ( ; t != &_jdwp_trackers_list; t = t->next ) {
665 apacket* p = get_apacket();
666 asocket* peer = t->socket.peer;
667 memcpy(p->data, buffer, len);
668 p->len = len;
669 peer->enqueue( peer, p );
670 }
671}
672
673static void
674jdwp_tracker_close( asocket* s )
675{
676 JdwpTracker* tracker = (JdwpTracker*) s;
677 asocket* peer = s->peer;
678
679 if (peer) {
680 peer->peer = NULL;
681 peer->close(peer);
682 }
683
684 remove_socket(s);
685
686 tracker->prev->next = tracker->next;
687 tracker->next->prev = tracker->prev;
688
689 free(s);
690}
691
692static void
693jdwp_tracker_ready( asocket* s )
694{
695 JdwpTracker* t = (JdwpTracker*) s;
696
697 if (t->need_update) {
698 apacket* p = get_apacket();
699 t->need_update = 0;
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100700 p->len = jdwp_process_list_msg((char*)p->data, s->get_max_payload());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800701 s->peer->enqueue(s->peer, p);
702 }
703}
704
705static int
706jdwp_tracker_enqueue( asocket* s, apacket* p )
707{
708 /* you can't write to this socket */
709 put_apacket(p);
710 s->peer->close(s->peer);
711 return -1;
712}
713
714
715asocket*
716create_jdwp_tracker_service_socket( void )
717{
Dan Albertf30d73c2015-02-25 17:51:28 -0800718 JdwpTracker* t = reinterpret_cast<JdwpTracker*>(calloc(sizeof(*t), 1));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800719
720 if (t == NULL)
721 return NULL;
722
723 t->next = &_jdwp_trackers_list;
724 t->prev = t->next->prev;
725
726 t->next->prev = t;
727 t->prev->next = t;
728
729 install_local_socket(&t->socket);
730
731 t->socket.ready = jdwp_tracker_ready;
732 t->socket.enqueue = jdwp_tracker_enqueue;
733 t->socket.close = jdwp_tracker_close;
734 t->need_update = 1;
735
736 return &t->socket;
737}
738
739
740int
741init_jdwp(void)
742{
743 _jdwp_list.next = &_jdwp_list;
744 _jdwp_list.prev = &_jdwp_list;
745
746 _jdwp_trackers_list.next = &_jdwp_trackers_list;
747 _jdwp_trackers_list.prev = &_jdwp_trackers_list;
748
749 return jdwp_control_init( &_jdwp_control,
750 JDWP_CONTROL_NAME,
751 JDWP_CONTROL_NAME_LEN );
752}
753
754#endif /* !ADB_HOST */