2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 /* implement the "debug-ports" and "track-debug-ports" device services */
18 #define TRACE_TAG TRACE_JDWP
25 /* here's how these things work.
27 when sdbd starts, it creates a unix server socket
28 named @vm-debug-control (@ is a shortcut for "first byte is zero"
29 to use the private namespace instead of the file system)
31 when a new JDWP daemon thread starts in a new VM process, it creates
32 a connection to @vm-debug-control to announce its availability.
35 JDWP thread @vm-debug-control
37 |-------------------------------> |
38 | hello I'm in process <pid> |
42 the connection is kept alive. it will be closed automatically if
43 the JDWP process terminates (this allows sdbd to detect dead
46 sdbd thus maintains a list of "active" JDWP processes. it can send
47 its content to clients through the "device:debug-ports" service,
48 or even updates through the "device:track-debug-ports" service.
50 when a debugger wants to connect, it simply runs the command
51 equivalent to "sdb forward tcp:<hostport> jdwp:<pid>"
53 "jdwp:<pid>" is a new forward destination format used to target
54 a given JDWP process on the device. when sutch a request arrives,
55 sdbd does the following:
57 - first, it calls socketpair() to create a pair of equivalent
60 - it attaches the first socket in the pair to a local socket
61 which is itself attached to the transport's remote socket:
64 - it sends the file descriptor of the second socket directly
65 to the JDWP process with the help of sendmsg()
68 JDWP thread @vm-debug-control
70 | <----------------------|
71 | OK, try this file descriptor |
75 then, the JDWP thread uses this new socket descriptor as its
76 pass-through connection to the debugger (and receives the
77 JDWP-Handshake message, answers to it, etc...)
79 this gives the following graphics:
80 ____________________________________
84 Debugger <---> LocalSocket <----> RemoteSocket |
86 |___________________________||_______|
89 (TCP for emulator - USB for device) ||
91 ___________________________||_______
95 JDWP <======> LocalSocket <----> RemoteSocket |
97 |____________________________________|
99 due to the way sdb works, this doesn't need a special socket
100 type or fancy handling of socket termination if either the debugger
101 or the JDWP process closes the connection.
103 THIS IS THE SIMPLEST IMPLEMENTATION I COULD FIND, IF YOU HAPPEN
104 TO HAVE A BETTER IDEA, LET ME KNOW - Digit
106 **********************************************************************/
108 /** JDWP PID List Support Code
109 ** for each JDWP process, we record its pid and its connected socket
112 #define MAX_OUT_FDS 4
116 #include <sys/socket.h>
119 typedef struct JdwpProcess JdwpProcess;
127 char in_buff[4]; /* input character to read PID */
128 int in_len; /* number from JDWP process */
130 int out_fds[MAX_OUT_FDS]; /* output array of file descriptors */
131 int out_count; /* to send to the JDWP process */
134 static JdwpProcess _jdwp_list;
137 jdwp_process_list( char* buffer, int bufferlen )
139 char* end = buffer + bufferlen;
141 JdwpProcess* proc = _jdwp_list.next;
143 for ( ; proc != &_jdwp_list; proc = proc->next ) {
146 /* skip transient connections */
150 len = snprintf(p, end-p, "%d\n", proc->pid);
161 jdwp_process_list_msg( char* buffer, int bufferlen )
164 int len = jdwp_process_list( buffer+4, bufferlen-4 );
165 snprintf(head, sizeof head, "%04x", len);
166 memcpy(buffer, head, 4);
171 static void jdwp_process_list_updated(void);
174 jdwp_process_free( JdwpProcess* proc )
179 proc->prev->next = proc->next;
180 proc->next->prev = proc->prev;
182 if (proc->socket >= 0) {
183 sdb_shutdown(proc->socket);
184 sdb_close(proc->socket);
188 if (proc->fde != NULL) {
189 fdevent_destroy(proc->fde);
194 for (n = 0; n < proc->out_count; n++) {
195 sdb_close(proc->out_fds[n]);
201 jdwp_process_list_updated();
206 static void jdwp_process_event(int, unsigned, void*); /* forward */
210 jdwp_process_alloc( int socket )
212 JdwpProcess* proc = calloc(1,sizeof(*proc));
215 D("not enough memory to create new JDWP process\n");
219 proc->socket = socket;
224 proc->fde = fdevent_create( socket, jdwp_process_event, proc );
225 if (proc->fde == NULL) {
226 D("could not create fdevent for new JDWP process\n" );
231 proc->fde->state |= FDE_DONT_CLOSE;
236 proc->next = &_jdwp_list;
237 proc->prev = proc->next->prev;
239 proc->prev->next = proc;
240 proc->next->prev = proc;
242 /* start by waiting for the PID */
243 fdevent_add(proc->fde, FDE_READ);
250 jdwp_process_event( int socket, unsigned events, void* _proc )
252 JdwpProcess* proc = _proc;
254 if (events & FDE_READ) {
256 /* read the PID as a 4-hexchar string */
257 char* p = proc->in_buff + proc->in_len;
258 int size = 4 - proc->in_len;
261 int len = recv( socket, p, size, 0 );
267 /* this can fail here if the JDWP process crashes very fast */
268 D("weird unknown JDWP process failure: %s\n",
273 if (len == 0) { /* end of stream ? */
274 D("weird end-of-stream from unknown JDWP process\n");
281 /* we have read 4 characters, now decode the pid */
282 memcpy(temp, proc->in_buff, 4);
285 if (sscanf( temp, "%04x", &proc->pid ) != 1) {
286 D("could not decode JDWP %p PID number: '%s'\n", proc, temp);
290 /* all is well, keep reading to detect connection closure */
291 D("Adding pid %d to jdwp process list\n", proc->pid);
292 jdwp_process_list_updated();
296 /* the pid was read, if we get there it's probably because the connection
297 * was closed (e.g. the JDWP process exited or crashed) */
301 int len = recv(socket, buf, sizeof(buf), 0);
304 if (len < 0 && errno == EINTR)
306 if (len < 0 && errno == EAGAIN)
309 D("terminating JDWP %d connection: %s\n", proc->pid,
315 D( "ignoring unexpected JDWP %d control socket activity (%d bytes)\n",
322 D( "remove pid %d to jdwp process list\n", proc->pid );
323 jdwp_process_free(proc);
328 if (events & FDE_WRITE) {
329 D("trying to write to JDWP pid controli (count=%d first=%d) %d\n",
330 proc->pid, proc->out_count, proc->out_fds[0]);
331 if (proc->out_count > 0) {
332 int fd = proc->out_fds[0];
334 struct cmsghdr* cmsg;
338 char buffer[sizeof(struct cmsghdr) + sizeof(int)];
341 iov.iov_base = &dummy;
348 msg.msg_control = buffer;
349 msg.msg_controllen = sizeof(buffer);
351 cmsg = CMSG_FIRSTHDR(&msg);
352 cmsg->cmsg_len = msg.msg_controllen;
353 cmsg->cmsg_level = SOL_SOCKET;
354 cmsg->cmsg_type = SCM_RIGHTS;
355 ((int*)CMSG_DATA(cmsg))[0] = fd;
357 flags = fcntl(proc->socket,F_GETFL,0);
360 D("failed to get cntl flags for socket %d: %s\n",
361 proc->pid, strerror(errno));
366 if (fcntl(proc->socket, F_SETFL, flags & ~O_NONBLOCK) == -1) {
367 D("failed to remove O_NONBLOCK flag for socket %d: %s\n",
368 proc->pid, strerror(errno));
373 ret = sendmsg(proc->socket, &msg, 0);
380 D("sending new file descriptor to JDWP %d failed: %s\n",
381 proc->pid, strerror(errno));
385 D("sent file descriptor %d to JDWP process %d\n",
388 for (n = 1; n < proc->out_count; n++)
389 proc->out_fds[n-1] = proc->out_fds[n];
391 if (fcntl(proc->socket, F_SETFL, flags) == -1) {
392 D("failed to set O_NONBLOCK flag for socket %d: %s\n",
393 proc->pid, strerror(errno));
397 if (--proc->out_count == 0)
398 fdevent_del( proc->fde, FDE_WRITE );
405 create_jdwp_connection_fd(int pid)
407 JdwpProcess* proc = _jdwp_list.next;
409 D("looking for pid %d in JDWP process list\n", pid);
410 for ( ; proc != &_jdwp_list; proc = proc->next ) {
411 if (proc->pid == pid) {
415 D("search failed !!\n");
422 if (proc->out_count >= MAX_OUT_FDS) {
423 D("%s: too many pending JDWP connection for pid %d\n",
428 if (sdb_socketpair(fds) < 0) {
429 D("%s: socket pair creation failed: %s\n",
430 __FUNCTION__, strerror(errno));
434 proc->out_fds[ proc->out_count ] = fds[1];
435 if (++proc->out_count == 1)
436 fdevent_add( proc->fde, FDE_WRITE );
442 /** VM DEBUG CONTROL SOCKET
444 ** we do implement a custom asocket to receive the data
447 /* name of the debug control Unix socket */
448 #define JDWP_CONTROL_NAME "\0jdwp-control"
449 #define JDWP_CONTROL_NAME_LEN (sizeof(JDWP_CONTROL_NAME)-1)
459 jdwp_control_event(int s, unsigned events, void* user);
463 jdwp_control_init( JdwpControl* control,
464 const char* sockname,
467 struct sockaddr_un addr;
470 int maxpath = sizeof(addr.sun_path);
471 int pathlen = socknamelen;
473 if (pathlen >= maxpath) {
474 D( "vm debug control socket name too long (%d extra chars)\n",
479 memset(&addr, 0, sizeof(addr));
480 addr.sun_family = AF_UNIX;
481 memcpy(addr.sun_path, sockname, socknamelen);
483 s = socket( AF_UNIX, SOCK_STREAM, 0 );
485 D( "could not create vm debug control socket. %d: %s\n",
486 errno, strerror(errno));
490 addrlen = (pathlen + sizeof(addr.sun_family));
492 if (bind(s, (struct sockaddr*)&addr, addrlen) < 0) {
493 D( "could not bind vm debug control socket: %d: %s\n",
494 errno, strerror(errno) );
499 if ( listen(s, 4) < 0 ) {
500 D("listen failed in jdwp control socket: %d: %s\n",
501 errno, strerror(errno));
506 control->listen_socket = s;
508 control->fde = fdevent_create(s, jdwp_control_event, control);
509 if (control->fde == NULL) {
510 D( "could not create fdevent for jdwp control socket\n" );
515 /* only wait for incoming connections */
516 fdevent_add(control->fde, FDE_READ);
519 D("jdwp control socket started (%d)\n", control->listen_socket);
525 jdwp_control_event( int s, unsigned events, void* _control )
527 JdwpControl* control = (JdwpControl*) _control;
529 if (events & FDE_READ) {
530 struct sockaddr addr;
531 socklen_t addrlen = sizeof(addr);
536 s = sdb_socket_accept( control->listen_socket, &addr, &addrlen );
540 if (errno == ECONNABORTED) {
541 /* oops, the JDWP process died really quick */
542 D("oops, the JDWP process died really quick\n");
545 /* the socket is probably closed ? */
546 D( "weird accept() failed on jdwp control socket: %s\n",
553 proc = jdwp_process_alloc( s );
560 static JdwpControl _jdwp_control;
562 /** "jdwp" local service implementation
563 ** this simply returns the list of known JDWP process pids
572 jdwp_socket_close( asocket* s )
574 asocket* peer = s->peer;
586 jdwp_socket_enqueue( asocket* s, apacket* p )
588 /* you can't write to this asocket */
590 s->peer->close(s->peer);
596 jdwp_socket_ready( asocket* s )
598 JdwpSocket* jdwp = (JdwpSocket*)s;
599 asocket* peer = jdwp->socket.peer;
601 /* on the first call, send the list of pids,
602 * on the second one, close the connection
604 if (jdwp->pass == 0) {
605 apacket* p = get_apacket();
606 p->len = jdwp_process_list((char*)p->data, MAX_PAYLOAD);
607 peer->enqueue(peer, p);
616 create_jdwp_service_socket( void )
618 JdwpSocket* s = calloc(sizeof(*s),1);
623 install_local_socket(&s->socket);
625 s->socket.ready = jdwp_socket_ready;
626 s->socket.enqueue = jdwp_socket_enqueue;
627 s->socket.close = jdwp_socket_close;
633 /** "track-jdwp" local service implementation
634 ** this periodically sends the list of known JDWP process pids
638 typedef struct JdwpTracker JdwpTracker;
647 static JdwpTracker _jdwp_trackers_list;
651 jdwp_process_list_updated(void)
655 JdwpTracker* t = _jdwp_trackers_list.next;
657 len = jdwp_process_list_msg(buffer, sizeof(buffer));
659 for ( ; t != &_jdwp_trackers_list; t = t->next ) {
660 apacket* p = get_apacket();
661 asocket* peer = t->socket.peer;
662 memcpy(p->data, buffer, len);
664 peer->enqueue( peer, p );
669 jdwp_tracker_close( asocket* s )
671 JdwpTracker* tracker = (JdwpTracker*) s;
672 asocket* peer = s->peer;
681 tracker->prev->next = tracker->next;
682 tracker->next->prev = tracker->prev;
688 jdwp_tracker_ready( asocket* s )
690 JdwpTracker* t = (JdwpTracker*) s;
692 if (t->need_update) {
693 apacket* p = get_apacket();
695 p->len = jdwp_process_list_msg((char*)p->data, sizeof(p->data));
696 s->peer->enqueue(s->peer, p);
701 jdwp_tracker_enqueue( asocket* s, apacket* p )
703 /* you can't write to this socket */
705 s->peer->close(s->peer);
711 create_jdwp_tracker_service_socket( void )
713 JdwpTracker* t = calloc(sizeof(*t),1);
718 t->next = &_jdwp_trackers_list;
719 t->prev = t->next->prev;
724 install_local_socket(&t->socket);
726 t->socket.ready = jdwp_tracker_ready;
727 t->socket.enqueue = jdwp_tracker_enqueue;
728 t->socket.close = jdwp_tracker_close;
738 _jdwp_list.next = &_jdwp_list;
739 _jdwp_list.prev = &_jdwp_list;
741 _jdwp_trackers_list.next = &_jdwp_trackers_list;
742 _jdwp_trackers_list.prev = &_jdwp_trackers_list;
744 return jdwp_control_init( &_jdwp_control,
746 JDWP_CONTROL_NAME_LEN );
749 #endif /* !SDB_HOST */