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