Tizen 2.1 base
[sdk/target/sdbd.git] / src / jdwp_service.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 /* implement the "debug-ports" and "track-debug-ports" device services */
17 #include "sysdeps.h"
18 #define  TRACE_TAG   TRACE_JDWP
19 #include "sdb.h"
20 #include <errno.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 /* here's how these things work.
26
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)
30
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.
33
34
35      JDWP thread                             @vm-debug-control
36          |                                         |
37          |------------------------------->         |
38          | hello I'm in process <pid>              |
39          |                                         |
40          |                                         |
41
42     the connection is kept alive. it will be closed automatically if
43     the JDWP process terminates (this allows sdbd to detect dead
44     processes).
45
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.
49
50     when a debugger wants to connect, it simply runs the command
51     equivalent to  "sdb forward tcp:<hostport> jdwp:<pid>"
52
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:
56
57       - first, it calls socketpair() to create a pair of equivalent
58         sockets.
59
60       - it attaches the first socket in the pair to a local socket
61         which is itself attached to the transport's remote socket:
62
63
64       - it sends the file descriptor of the second socket directly
65         to the JDWP process with the help of sendmsg()
66
67
68      JDWP thread                             @vm-debug-control
69          |                                         |
70          |                  <----------------------|
71          |           OK, try this file descriptor  |
72          |                                         |
73          |                                         |
74
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...)
78
79    this gives the following graphics:
80                     ____________________________________
81                    |                                    |
82                    |          SDB Server (host)         |
83                    |                                    |
84         Debugger <---> LocalSocket <----> RemoteSocket  |
85                    |                           ^^       |
86                    |___________________________||_______|
87                                                ||
88                                      Transport ||
89            (TCP for emulator - USB for device) ||
90                                                ||
91                     ___________________________||_______
92                    |                           ||       |
93                    |          SDBD  (device)   ||       |
94                    |                           VV       |
95          JDWP <======> LocalSocket <----> RemoteSocket  |
96                    |                                    |
97                    |____________________________________|
98
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.
102
103     THIS IS THE SIMPLEST IMPLEMENTATION I COULD FIND, IF YOU HAPPEN
104     TO HAVE A BETTER IDEA, LET ME KNOW - Digit
105
106 **********************************************************************/
107
108 /** JDWP PID List Support Code
109  ** for each JDWP process, we record its pid and its connected socket
110  **/
111
112 #define  MAX_OUT_FDS   4
113
114 #if !SDB_HOST
115
116 #include <sys/socket.h>
117 #include <sys/un.h>
118
119 typedef struct JdwpProcess  JdwpProcess;
120 struct JdwpProcess {
121     JdwpProcess*  next;
122     JdwpProcess*  prev;
123     int           pid;
124     int           socket;
125     fdevent*      fde;
126
127     char          in_buff[4];  /* input character to read PID */
128     int           in_len;      /* number from JDWP process    */
129
130     int           out_fds[MAX_OUT_FDS]; /* output array of file descriptors */
131     int           out_count;            /* to send to the JDWP process      */
132 };
133
134 static JdwpProcess  _jdwp_list;
135
136 static int
137 jdwp_process_list( char*  buffer, int  bufferlen )
138 {
139     char*         end  = buffer + bufferlen;
140     char*         p    = buffer;
141     JdwpProcess*  proc = _jdwp_list.next;
142
143     for ( ; proc != &_jdwp_list; proc = proc->next ) {
144         int  len;
145
146         /* skip transient connections */
147         if (proc->pid < 0)
148             continue;
149
150         len = snprintf(p, end-p, "%d\n", proc->pid);
151         if (p + len >= end)
152             break;
153         p += len;
154     }
155     p[0] = 0;
156     return (p - buffer);
157 }
158
159
160 static int
161 jdwp_process_list_msg( char*  buffer, int  bufferlen )
162 {
163     char  head[5];
164     int   len = jdwp_process_list( buffer+4, bufferlen-4 );
165     snprintf(head, sizeof head, "%04x", len);
166     memcpy(buffer, head, 4);
167     return len + 4;
168 }
169
170
171 static void  jdwp_process_list_updated(void);
172
173 static void
174 jdwp_process_free( JdwpProcess*  proc )
175 {
176     if (proc) {
177         int  n;
178
179         proc->prev->next = proc->next;
180         proc->next->prev = proc->prev;
181
182         if (proc->socket >= 0) {
183             sdb_shutdown(proc->socket);
184             sdb_close(proc->socket);
185             proc->socket = -1;
186         }
187
188         if (proc->fde != NULL) {
189             fdevent_destroy(proc->fde);
190             proc->fde = NULL;
191         }
192         proc->pid = -1;
193
194         for (n = 0; n < proc->out_count; n++) {
195             sdb_close(proc->out_fds[n]);
196         }
197         proc->out_count = 0;
198
199         free(proc);
200
201         jdwp_process_list_updated();
202     }
203 }
204
205
206 static void  jdwp_process_event(int, unsigned, void*);  /* forward */
207
208
209 static JdwpProcess*
210 jdwp_process_alloc( int  socket )
211 {
212     JdwpProcess*  proc = calloc(1,sizeof(*proc));
213
214     if (proc == NULL) {
215         D("not enough memory to create new JDWP process\n");
216         return NULL;
217     }
218
219     proc->socket = socket;
220     proc->pid    = -1;
221     proc->next   = proc;
222     proc->prev   = proc;
223
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" );
227         free(proc);
228         return NULL;
229     }
230
231     proc->fde->state |= FDE_DONT_CLOSE;
232     proc->in_len      = 0;
233     proc->out_count   = 0;
234
235     /* append to list */
236     proc->next = &_jdwp_list;
237     proc->prev = proc->next->prev;
238
239     proc->prev->next = proc;
240     proc->next->prev = proc;
241
242     /* start by waiting for the PID */
243     fdevent_add(proc->fde, FDE_READ);
244
245     return proc;
246 }
247
248
249 static void
250 jdwp_process_event( int  socket, unsigned  events, void*  _proc )
251 {
252     JdwpProcess*  proc = _proc;
253
254     if (events & FDE_READ) {
255         if (proc->pid < 0) {
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;
259             char   temp[5];
260             while (size > 0) {
261                 int  len = recv( socket, p, size, 0 );
262                 if (len < 0) {
263                     if (errno == EINTR)
264                         continue;
265                     if (errno == EAGAIN)
266                         return;
267                     /* this can fail here if the JDWP process crashes very fast */
268                     D("weird unknown JDWP process failure: %s\n",
269                       strerror(errno));
270
271                     goto CloseProcess;
272                 }
273                 if (len == 0) {  /* end of stream ? */
274                     D("weird end-of-stream from unknown JDWP process\n");
275                     goto CloseProcess;
276                 }
277                 p            += len;
278                 proc->in_len += len;
279                 size         -= len;
280             }
281             /* we have read 4 characters, now decode the pid */
282             memcpy(temp, proc->in_buff, 4);
283             temp[4] = 0;
284
285             if (sscanf( temp, "%04x", &proc->pid ) != 1) {
286                 D("could not decode JDWP %p PID number: '%s'\n", proc, temp);
287                 goto CloseProcess;
288             }
289
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();
293         }
294         else
295         {
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) */
298             char  buf[32];
299
300             for (;;) {
301                 int  len = recv(socket, buf, sizeof(buf), 0);
302
303                 if (len <= 0) {
304                     if (len < 0 && errno == EINTR)
305                         continue;
306                     if (len < 0 && errno == EAGAIN)
307                         return;
308                     else {
309                         D("terminating JDWP %d connection: %s\n", proc->pid,
310                           strerror(errno));
311                         break;
312                     }
313                 }
314                 else {
315                     D( "ignoring unexpected JDWP %d control socket activity (%d bytes)\n",
316                        proc->pid, len );
317                 }
318             }
319
320         CloseProcess:
321             if (proc->pid >= 0)
322                 D( "remove pid %d to jdwp process list\n", proc->pid );
323             jdwp_process_free(proc);
324             return;
325         }
326     }
327
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];
333             int  n, ret;
334             struct cmsghdr*  cmsg;
335             struct msghdr    msg;
336             struct iovec     iov;
337             char             dummy = '!';
338             char             buffer[sizeof(struct cmsghdr) + sizeof(int)];
339             int flags;
340
341             iov.iov_base       = &dummy;
342             iov.iov_len        = 1;
343             msg.msg_name       = NULL;
344             msg.msg_namelen    = 0;
345             msg.msg_iov        = &iov;
346             msg.msg_iovlen     = 1;
347             msg.msg_flags      = 0;
348             msg.msg_control    = buffer;
349             msg.msg_controllen = sizeof(buffer);
350
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;
356
357             flags = fcntl(proc->socket,F_GETFL,0);
358
359             if (flags == -1) {
360                 D("failed to get cntl flags for socket %d: %s\n",
361                   proc->pid, strerror(errno));
362                 goto CloseProcess;
363
364             }
365
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));
369                 goto CloseProcess;
370             }
371
372             for (;;) {
373                 ret = sendmsg(proc->socket, &msg, 0);
374                 if (ret >= 0) {
375                     sdb_close(fd);
376                     break;
377                 }
378                 if (errno == EINTR)
379                     continue;
380                 D("sending new file descriptor to JDWP %d failed: %s\n",
381                   proc->pid, strerror(errno));
382                 goto CloseProcess;
383             }
384
385             D("sent file descriptor %d to JDWP process %d\n",
386               fd, proc->pid);
387
388             for (n = 1; n < proc->out_count; n++)
389                 proc->out_fds[n-1] = proc->out_fds[n];
390
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));
394                 goto CloseProcess;
395             }
396
397             if (--proc->out_count == 0)
398                 fdevent_del( proc->fde, FDE_WRITE );
399         }
400     }
401 }
402
403
404 int
405 create_jdwp_connection_fd(int  pid)
406 {
407     JdwpProcess*  proc = _jdwp_list.next;
408
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) {
412             goto FoundIt;
413         }
414     }
415     D("search failed !!\n");
416     return -1;
417
418 FoundIt:
419     {
420         int  fds[2];
421
422         if (proc->out_count >= MAX_OUT_FDS) {
423             D("%s: too many pending JDWP connection for pid %d\n",
424               __FUNCTION__, pid);
425             return -1;
426         }
427
428         if (sdb_socketpair(fds) < 0) {
429             D("%s: socket pair creation failed: %s\n",
430               __FUNCTION__, strerror(errno));
431             return -1;
432         }
433
434         proc->out_fds[ proc->out_count ] = fds[1];
435         if (++proc->out_count == 1)
436             fdevent_add( proc->fde, FDE_WRITE );
437
438         return fds[0];
439     }
440 }
441
442 /**  VM DEBUG CONTROL SOCKET
443  **
444  **  we do implement a custom asocket to receive the data
445  **/
446
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)
450
451 typedef struct {
452     int       listen_socket;
453     fdevent*  fde;
454
455 } JdwpControl;
456
457
458 static void
459 jdwp_control_event(int  s, unsigned events, void*  user);
460
461
462 static int
463 jdwp_control_init( JdwpControl*  control,
464                    const char*   sockname,
465                    int           socknamelen )
466 {
467     struct sockaddr_un   addr;
468     socklen_t            addrlen;
469     int                  s;
470     int                  maxpath = sizeof(addr.sun_path);
471     int                  pathlen = socknamelen;
472
473     if (pathlen >= maxpath) {
474         D( "vm debug control socket name too long (%d extra chars)\n",
475            pathlen+1-maxpath );
476         return -1;
477     }
478
479     memset(&addr, 0, sizeof(addr));
480     addr.sun_family = AF_UNIX;
481     memcpy(addr.sun_path, sockname, socknamelen);
482
483     s = socket( AF_UNIX, SOCK_STREAM, 0 );
484     if (s < 0) {
485         D( "could not create vm debug control socket. %d: %s\n",
486            errno, strerror(errno));
487         return -1;
488     }
489
490     addrlen = (pathlen + sizeof(addr.sun_family));
491
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) );
495         sdb_close(s);
496         return -1;
497     }
498
499     if ( listen(s, 4) < 0 ) {
500         D("listen failed in jdwp control socket: %d: %s\n",
501           errno, strerror(errno));
502         sdb_close(s);
503         return -1;
504     }
505
506     control->listen_socket = s;
507
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" );
511         sdb_close(s);
512         return -1;
513     }
514
515     /* only wait for incoming connections */
516     fdevent_add(control->fde, FDE_READ);
517     close_on_exec(s);
518
519     D("jdwp control socket started (%d)\n", control->listen_socket);
520     return 0;
521 }
522
523
524 static void
525 jdwp_control_event( int  s, unsigned  events, void*  _control )
526 {
527     JdwpControl*  control = (JdwpControl*) _control;
528
529     if (events & FDE_READ) {
530         struct sockaddr   addr;
531         socklen_t         addrlen = sizeof(addr);
532         int               s = -1;
533         JdwpProcess*      proc;
534
535         do {
536             s = sdb_socket_accept( control->listen_socket, &addr, &addrlen );
537             if (s < 0) {
538                 if (errno == EINTR)
539                     continue;
540                 if (errno == ECONNABORTED) {
541                     /* oops, the JDWP process died really quick */
542                     D("oops, the JDWP process died really quick\n");
543                     return;
544                 }
545                 /* the socket is probably closed ? */
546                 D( "weird accept() failed on jdwp control socket: %s\n",
547                    strerror(errno) );
548                 return;
549             }
550         }
551         while (s < 0);
552
553         proc = jdwp_process_alloc( s );
554         if (proc == NULL)
555             return;
556     }
557 }
558
559
560 static JdwpControl   _jdwp_control;
561
562 /** "jdwp" local service implementation
563  ** this simply returns the list of known JDWP process pids
564  **/
565
566 typedef struct {
567     asocket  socket;
568     int      pass;
569 } JdwpSocket;
570
571 static void
572 jdwp_socket_close( asocket*  s )
573 {
574     asocket*  peer = s->peer;
575
576     remove_socket(s);
577
578     if (peer) {
579         peer->peer = NULL;
580         peer->close(peer);
581     }
582     free(s);
583 }
584
585 static int
586 jdwp_socket_enqueue( asocket*  s, apacket*  p )
587 {
588     /* you can't write to this asocket */
589     put_apacket(p);
590     s->peer->close(s->peer);
591     return -1;
592 }
593
594
595 static void
596 jdwp_socket_ready( asocket*  s )
597 {
598     JdwpSocket*  jdwp = (JdwpSocket*)s;
599     asocket*     peer = jdwp->socket.peer;
600
601    /* on the first call, send the list of pids,
602     * on the second one, close the connection
603     */
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);
608         jdwp->pass = 1;
609     }
610     else {
611         peer->close(peer);
612     }
613 }
614
615 asocket*
616 create_jdwp_service_socket( void )
617 {
618     JdwpSocket*  s = calloc(sizeof(*s),1);
619
620     if (s == NULL)
621         return NULL;
622
623     install_local_socket(&s->socket);
624
625     s->socket.ready   = jdwp_socket_ready;
626     s->socket.enqueue = jdwp_socket_enqueue;
627     s->socket.close   = jdwp_socket_close;
628     s->pass           = 0;
629
630     return &s->socket;
631 }
632
633 /** "track-jdwp" local service implementation
634  ** this periodically sends the list of known JDWP process pids
635  ** to the client...
636  **/
637
638 typedef struct JdwpTracker  JdwpTracker;
639
640 struct JdwpTracker {
641     asocket       socket;
642     JdwpTracker*  next;
643     JdwpTracker*  prev;
644     int           need_update;
645 };
646
647 static JdwpTracker   _jdwp_trackers_list;
648
649
650 static void
651 jdwp_process_list_updated(void)
652 {
653     char             buffer[1024];
654     int              len;
655     JdwpTracker*  t = _jdwp_trackers_list.next;
656
657     len = jdwp_process_list_msg(buffer, sizeof(buffer));
658
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);
663         p->len = len;
664         peer->enqueue( peer, p );
665     }
666 }
667
668 static void
669 jdwp_tracker_close( asocket*  s )
670 {
671     JdwpTracker*  tracker = (JdwpTracker*) s;
672     asocket*      peer    = s->peer;
673
674     if (peer) {
675         peer->peer = NULL;
676         peer->close(peer);
677     }
678
679     remove_socket(s);
680
681     tracker->prev->next = tracker->next;
682     tracker->next->prev = tracker->prev;
683
684     free(s);
685 }
686
687 static void
688 jdwp_tracker_ready( asocket*  s )
689 {
690     JdwpTracker*  t = (JdwpTracker*) s;
691
692     if (t->need_update) {
693         apacket*  p = get_apacket();
694         t->need_update = 0;
695         p->len = jdwp_process_list_msg((char*)p->data, sizeof(p->data));
696         s->peer->enqueue(s->peer, p);
697     }
698 }
699
700 static int
701 jdwp_tracker_enqueue( asocket*  s, apacket*  p )
702 {
703     /* you can't write to this socket */
704     put_apacket(p);
705     s->peer->close(s->peer);
706     return -1;
707 }
708
709
710 asocket*
711 create_jdwp_tracker_service_socket( void )
712 {
713     JdwpTracker*  t = calloc(sizeof(*t),1);
714
715     if (t == NULL)
716         return NULL;
717
718     t->next = &_jdwp_trackers_list;
719     t->prev = t->next->prev;
720
721     t->next->prev = t;
722     t->prev->next = t;
723
724     install_local_socket(&t->socket);
725
726     t->socket.ready   = jdwp_tracker_ready;
727     t->socket.enqueue = jdwp_tracker_enqueue;
728     t->socket.close   = jdwp_tracker_close;
729     t->need_update    = 1;
730
731     return &t->socket;
732 }
733
734
735 int
736 init_jdwp(void)
737 {
738     _jdwp_list.next = &_jdwp_list;
739     _jdwp_list.prev = &_jdwp_list;
740
741     _jdwp_trackers_list.next = &_jdwp_trackers_list;
742     _jdwp_trackers_list.prev = &_jdwp_trackers_list;
743
744     return jdwp_control_init( &_jdwp_control,
745                               JDWP_CONTROL_NAME,
746                               JDWP_CONTROL_NAME_LEN );
747 }
748
749 #endif /* !SDB_HOST */