blob: 9cf084e0fff21a530b6729d3de8b4bb29ebd3a72 [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>
25#include <string.h>
Teddie Stenvi8f5daad2010-02-15 12:20:44 +010026#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Dan Albert33134262015-03-19 15:21:08 -070028#include "adb.h"
29
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030/* here's how these things work.
31
32 when adbd starts, it creates a unix server socket
33 named @vm-debug-control (@ is a shortcut for "first byte is zero"
34 to use the private namespace instead of the file system)
35
36 when a new JDWP daemon thread starts in a new VM process, it creates
37 a connection to @vm-debug-control to announce its availability.
38
39
40 JDWP thread @vm-debug-control
41 | |
42 |-------------------------------> |
43 | hello I'm in process <pid> |
44 | |
45 | |
46
47 the connection is kept alive. it will be closed automatically if
48 the JDWP process terminates (this allows adbd to detect dead
49 processes).
50
51 adbd thus maintains a list of "active" JDWP processes. it can send
52 its content to clients through the "device:debug-ports" service,
53 or even updates through the "device:track-debug-ports" service.
54
55 when a debugger wants to connect, it simply runs the command
56 equivalent to "adb forward tcp:<hostport> jdwp:<pid>"
57
58 "jdwp:<pid>" is a new forward destination format used to target
59 a given JDWP process on the device. when sutch a request arrives,
60 adbd does the following:
61
62 - first, it calls socketpair() to create a pair of equivalent
63 sockets.
64
65 - it attaches the first socket in the pair to a local socket
66 which is itself attached to the transport's remote socket:
67
68
69 - it sends the file descriptor of the second socket directly
70 to the JDWP process with the help of sendmsg()
71
72
73 JDWP thread @vm-debug-control
74 | |
75 | <----------------------|
76 | OK, try this file descriptor |
77 | |
78 | |
79
80 then, the JDWP thread uses this new socket descriptor as its
81 pass-through connection to the debugger (and receives the
82 JDWP-Handshake message, answers to it, etc...)
83
84 this gives the following graphics:
85 ____________________________________
86 | |
87 | ADB Server (host) |
88 | |
89 Debugger <---> LocalSocket <----> RemoteSocket |
90 | ^^ |
91 |___________________________||_______|
92 ||
93 Transport ||
94 (TCP for emulator - USB for device) ||
95 ||
96 ___________________________||_______
97 | || |
98 | ADBD (device) || |
99 | VV |
100 JDWP <======> LocalSocket <----> RemoteSocket |
101 | |
102 |____________________________________|
103
104 due to the way adb works, this doesn't need a special socket
105 type or fancy handling of socket termination if either the debugger
106 or the JDWP process closes the connection.
107
108 THIS IS THE SIMPLEST IMPLEMENTATION I COULD FIND, IF YOU HAPPEN
109 TO HAVE A BETTER IDEA, LET ME KNOW - Digit
110
111**********************************************************************/
112
113/** JDWP PID List Support Code
114 ** for each JDWP process, we record its pid and its connected socket
115 **/
116
117#define MAX_OUT_FDS 4
118
119#if !ADB_HOST
120
121#include <sys/socket.h>
122#include <sys/un.h>
123
124typedef struct JdwpProcess JdwpProcess;
125struct 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) {
221 D("not enough memory to create new JDWP process\n");
222 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) {
232 D("could not create fdevent for new JDWP process\n" );
233 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 */
274 D("weird unknown JDWP process failure: %s\n",
275 strerror(errno));
276
277 goto CloseProcess;
278 }
279 if (len == 0) { /* end of stream ? */
280 D("weird end-of-stream from unknown JDWP process\n");
281 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) {
292 D("could not decode JDWP %p PID number: '%s'\n", proc, temp);
293 goto CloseProcess;
294 }
295
296 /* all is well, keep reading to detect connection closure */
297 D("Adding pid %d to jdwp process list\n", proc->pid);
298 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 {
315 D("terminating JDWP %d connection: %s\n", proc->pid,
316 strerror(errno));
317 break;
318 }
319 }
320 else {
321 D( "ignoring unexpected JDWP %d control socket activity (%d bytes)\n",
322 proc->pid, len );
323 }
324 }
325
326 CloseProcess:
327 if (proc->pid >= 0)
328 D( "remove pid %d to jdwp process list\n", proc->pid );
329 jdwp_process_free(proc);
330 return;
331 }
332 }
333
334 if (events & FDE_WRITE) {
335 D("trying to write to JDWP pid controli (count=%d first=%d) %d\n",
336 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) {
366 D("failed to get cntl flags for socket %d: %s\n",
367 proc->pid, strerror(errno));
368 goto CloseProcess;
369
370 }
371
372 if (fcntl(proc->socket, F_SETFL, flags & ~O_NONBLOCK) == -1) {
373 D("failed to remove O_NONBLOCK flag for socket %d: %s\n",
374 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;
386 D("sending new file descriptor to JDWP %d failed: %s\n",
387 proc->pid, strerror(errno));
388 goto CloseProcess;
389 }
390
391 D("sent file descriptor %d to JDWP process %d\n",
392 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) {
398 D("failed to set O_NONBLOCK flag for socket %d: %s\n",
399 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
415 D("looking for pid %d in JDWP process list\n", pid);
416 for ( ; proc != &_jdwp_list; proc = proc->next ) {
417 if (proc->pid == pid) {
418 goto FoundIt;
419 }
420 }
421 D("search failed !!\n");
422 return -1;
423
424FoundIt:
425 {
426 int fds[2];
427
428 if (proc->out_count >= MAX_OUT_FDS) {
429 D("%s: too many pending JDWP connection for pid %d\n",
430 __FUNCTION__, pid);
431 return -1;
432 }
433
434 if (adb_socketpair(fds) < 0) {
435 D("%s: socket pair creation failed: %s\n",
436 __FUNCTION__, strerror(errno));
437 return -1;
438 }
leozwangcbf02672014-08-15 09:51:27 -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
458typedef struct {
459 int listen_socket;
460 fdevent* fde;
461
462} JdwpControl;
463
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) {
481 D( "vm debug control socket name too long (%d extra chars)\n",
482 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) {
492 D( "could not create vm debug control socket. %d: %s\n",
493 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) {
500 D( "could not bind vm debug control socket: %d: %s\n",
501 errno, strerror(errno) );
502 adb_close(s);
503 return -1;
504 }
505
506 if ( listen(s, 4) < 0 ) {
507 D("listen failed in jdwp control socket: %d: %s\n",
508 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) {
517 D( "could not create fdevent for jdwp control socket\n" );
518 adb_close(s);
519 return -1;
520 }
521
522 /* only wait for incoming connections */
523 fdevent_add(control->fde, FDE_READ);
Benoit Goby95ef8282011-02-01 18:57:41 -0800524 close_on_exec(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525
526 D("jdwp control socket started (%d)\n", control->listen_socket);
527 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 */
549 D("oops, the JDWP process died really quick\n");
550 return;
551 }
552 /* the socket is probably closed ? */
553 D( "weird accept() failed on jdwp control socket: %s\n",
554 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
573typedef struct {
574 asocket socket;
575 int pass;
576} JdwpSocket;
577
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();
613 p->len = jdwp_process_list((char*)p->data, MAX_PAYLOAD);
614 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 Albertbac34742015-02-25 17:51:28 -0800625 JdwpSocket* s = reinterpret_cast<JdwpSocket*>(calloc(sizeof(*s), 1));
The Android Open Source Projectdd7bc332009-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
645typedef struct JdwpTracker JdwpTracker;
646
647struct JdwpTracker {
648 asocket socket;
649 JdwpTracker* next;
650 JdwpTracker* prev;
651 int need_update;
652};
653
654static JdwpTracker _jdwp_trackers_list;
655
656
657static void
658jdwp_process_list_updated(void)
659{
660 char buffer[1024];
661 int len;
662 JdwpTracker* t = _jdwp_trackers_list.next;
663
664 len = jdwp_process_list_msg(buffer, sizeof(buffer));
665
666 for ( ; t != &_jdwp_trackers_list; t = t->next ) {
667 apacket* p = get_apacket();
668 asocket* peer = t->socket.peer;
669 memcpy(p->data, buffer, len);
670 p->len = len;
671 peer->enqueue( peer, p );
672 }
673}
674
675static void
676jdwp_tracker_close( asocket* s )
677{
678 JdwpTracker* tracker = (JdwpTracker*) s;
679 asocket* peer = s->peer;
680
681 if (peer) {
682 peer->peer = NULL;
683 peer->close(peer);
684 }
685
686 remove_socket(s);
687
688 tracker->prev->next = tracker->next;
689 tracker->next->prev = tracker->prev;
690
691 free(s);
692}
693
694static void
695jdwp_tracker_ready( asocket* s )
696{
697 JdwpTracker* t = (JdwpTracker*) s;
698
699 if (t->need_update) {
700 apacket* p = get_apacket();
701 t->need_update = 0;
702 p->len = jdwp_process_list_msg((char*)p->data, sizeof(p->data));
703 s->peer->enqueue(s->peer, p);
704 }
705}
706
707static int
708jdwp_tracker_enqueue( asocket* s, apacket* p )
709{
710 /* you can't write to this socket */
711 put_apacket(p);
712 s->peer->close(s->peer);
713 return -1;
714}
715
716
717asocket*
718create_jdwp_tracker_service_socket( void )
719{
Dan Albertbac34742015-02-25 17:51:28 -0800720 JdwpTracker* t = reinterpret_cast<JdwpTracker*>(calloc(sizeof(*t), 1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800721
722 if (t == NULL)
723 return NULL;
724
725 t->next = &_jdwp_trackers_list;
726 t->prev = t->next->prev;
727
728 t->next->prev = t;
729 t->prev->next = t;
730
731 install_local_socket(&t->socket);
732
733 t->socket.ready = jdwp_tracker_ready;
734 t->socket.enqueue = jdwp_tracker_enqueue;
735 t->socket.close = jdwp_tracker_close;
736 t->need_update = 1;
737
738 return &t->socket;
739}
740
741
742int
743init_jdwp(void)
744{
745 _jdwp_list.next = &_jdwp_list;
746 _jdwp_list.prev = &_jdwp_list;
747
748 _jdwp_trackers_list.next = &_jdwp_trackers_list;
749 _jdwp_trackers_list.prev = &_jdwp_trackers_list;
750
751 return jdwp_control_init( &_jdwp_control,
752 JDWP_CONTROL_NAME,
753 JDWP_CONTROL_NAME_LEN );
754}
755
756#endif /* !ADB_HOST */
757