| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* 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 Stenvi | 8f5daad | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 8 | #include <unistd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 9 |  | 
|  | 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 |  | 
|  | 104 | typedef struct JdwpProcess  JdwpProcess; | 
|  | 105 | struct 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 |  | 
|  | 119 | static JdwpProcess  _jdwp_list; | 
|  | 120 |  | 
|  | 121 | static int | 
|  | 122 | jdwp_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 |  | 
|  | 145 | static int | 
|  | 146 | jdwp_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 |  | 
|  | 156 | static void  jdwp_process_list_updated(void); | 
|  | 157 |  | 
|  | 158 | static void | 
|  | 159 | jdwp_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 Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 168 | adb_shutdown(proc->socket); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 169 | 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 |  | 
|  | 191 | static void  jdwp_process_event(int, unsigned, void*);  /* forward */ | 
|  | 192 |  | 
|  | 193 |  | 
|  | 194 | static JdwpProcess* | 
|  | 195 | jdwp_process_alloc( int  socket ) | 
|  | 196 | { | 
|  | 197 | JdwpProcess*  proc = calloc(1,sizeof(*proc)); | 
|  | 198 |  | 
|  | 199 | if (proc == NULL) { | 
|  | 200 | D("not enough memory to create new JDWP process\n"); | 
|  | 201 | return NULL; | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | proc->socket = socket; | 
|  | 205 | proc->pid    = -1; | 
|  | 206 | proc->next   = proc; | 
|  | 207 | proc->prev   = proc; | 
|  | 208 |  | 
|  | 209 | proc->fde = fdevent_create( socket, jdwp_process_event, proc ); | 
|  | 210 | if (proc->fde == NULL) { | 
|  | 211 | D("could not create fdevent for new JDWP process\n" ); | 
|  | 212 | free(proc); | 
|  | 213 | return NULL; | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | proc->fde->state |= FDE_DONT_CLOSE; | 
|  | 217 | proc->in_len      = 0; | 
|  | 218 | proc->out_count   = 0; | 
|  | 219 |  | 
|  | 220 | /* append to list */ | 
|  | 221 | proc->next = &_jdwp_list; | 
|  | 222 | proc->prev = proc->next->prev; | 
|  | 223 |  | 
|  | 224 | proc->prev->next = proc; | 
|  | 225 | proc->next->prev = proc; | 
|  | 226 |  | 
|  | 227 | /* start by waiting for the PID */ | 
|  | 228 | fdevent_add(proc->fde, FDE_READ); | 
|  | 229 |  | 
|  | 230 | return proc; | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 |  | 
|  | 234 | static void | 
|  | 235 | jdwp_process_event( int  socket, unsigned  events, void*  _proc ) | 
|  | 236 | { | 
|  | 237 | JdwpProcess*  proc = _proc; | 
|  | 238 |  | 
|  | 239 | if (events & FDE_READ) { | 
|  | 240 | if (proc->pid < 0) { | 
|  | 241 | /* read the PID as a 4-hexchar string */ | 
|  | 242 | char*  p    = proc->in_buff + proc->in_len; | 
|  | 243 | int    size = 4 - proc->in_len; | 
|  | 244 | char   temp[5]; | 
|  | 245 | while (size > 0) { | 
|  | 246 | int  len = recv( socket, p, size, 0 ); | 
|  | 247 | if (len < 0) { | 
|  | 248 | if (errno == EINTR) | 
|  | 249 | continue; | 
|  | 250 | if (errno == EAGAIN) | 
|  | 251 | return; | 
|  | 252 | /* this can fail here if the JDWP process crashes very fast */ | 
|  | 253 | D("weird unknown JDWP process failure: %s\n", | 
|  | 254 | strerror(errno)); | 
|  | 255 |  | 
|  | 256 | goto CloseProcess; | 
|  | 257 | } | 
|  | 258 | if (len == 0) {  /* end of stream ? */ | 
|  | 259 | D("weird end-of-stream from unknown JDWP process\n"); | 
|  | 260 | goto CloseProcess; | 
|  | 261 | } | 
|  | 262 | p            += len; | 
|  | 263 | proc->in_len += len; | 
|  | 264 | size         -= len; | 
|  | 265 | } | 
|  | 266 | /* we have read 4 characters, now decode the pid */ | 
|  | 267 | memcpy(temp, proc->in_buff, 4); | 
|  | 268 | temp[4] = 0; | 
|  | 269 |  | 
|  | 270 | if (sscanf( temp, "%04x", &proc->pid ) != 1) { | 
|  | 271 | D("could not decode JDWP %p PID number: '%s'\n", proc, temp); | 
|  | 272 | goto CloseProcess; | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | /* all is well, keep reading to detect connection closure */ | 
|  | 276 | D("Adding pid %d to jdwp process list\n", proc->pid); | 
|  | 277 | jdwp_process_list_updated(); | 
|  | 278 | } | 
|  | 279 | else | 
|  | 280 | { | 
|  | 281 | /* the pid was read, if we get there it's probably because the connection | 
|  | 282 | * was closed (e.g. the JDWP process exited or crashed) */ | 
|  | 283 | char  buf[32]; | 
|  | 284 |  | 
|  | 285 | for (;;) { | 
|  | 286 | int  len = recv(socket, buf, sizeof(buf), 0); | 
|  | 287 |  | 
|  | 288 | if (len <= 0) { | 
|  | 289 | if (len < 0 && errno == EINTR) | 
|  | 290 | continue; | 
|  | 291 | if (len < 0 && errno == EAGAIN) | 
|  | 292 | return; | 
|  | 293 | else { | 
|  | 294 | D("terminating JDWP %d connection: %s\n", proc->pid, | 
|  | 295 | strerror(errno)); | 
|  | 296 | break; | 
|  | 297 | } | 
|  | 298 | } | 
|  | 299 | else { | 
|  | 300 | D( "ignoring unexpected JDWP %d control socket activity (%d bytes)\n", | 
|  | 301 | proc->pid, len ); | 
|  | 302 | } | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | CloseProcess: | 
|  | 306 | if (proc->pid >= 0) | 
|  | 307 | D( "remove pid %d to jdwp process list\n", proc->pid ); | 
|  | 308 | jdwp_process_free(proc); | 
|  | 309 | return; | 
|  | 310 | } | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | if (events & FDE_WRITE) { | 
|  | 314 | D("trying to write to JDWP pid controli (count=%d first=%d) %d\n", | 
|  | 315 | proc->pid, proc->out_count, proc->out_fds[0]); | 
|  | 316 | if (proc->out_count > 0) { | 
|  | 317 | int  fd = proc->out_fds[0]; | 
|  | 318 | int  n, ret; | 
|  | 319 | struct cmsghdr*  cmsg; | 
|  | 320 | struct msghdr    msg; | 
|  | 321 | struct iovec     iov; | 
|  | 322 | char             dummy = '!'; | 
|  | 323 | char             buffer[sizeof(struct cmsghdr) + sizeof(int)]; | 
| Teddie Stenvi | 8f5daad | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 324 | int flags; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 325 |  | 
|  | 326 | iov.iov_base       = &dummy; | 
|  | 327 | iov.iov_len        = 1; | 
|  | 328 | msg.msg_name       = NULL; | 
|  | 329 | msg.msg_namelen    = 0; | 
|  | 330 | msg.msg_iov        = &iov; | 
|  | 331 | msg.msg_iovlen     = 1; | 
|  | 332 | msg.msg_flags      = 0; | 
|  | 333 | msg.msg_control    = buffer; | 
|  | 334 | msg.msg_controllen = sizeof(buffer); | 
|  | 335 |  | 
|  | 336 | cmsg = CMSG_FIRSTHDR(&msg); | 
|  | 337 | cmsg->cmsg_len   = msg.msg_controllen; | 
|  | 338 | cmsg->cmsg_level = SOL_SOCKET; | 
|  | 339 | cmsg->cmsg_type  = SCM_RIGHTS; | 
|  | 340 | ((int*)CMSG_DATA(cmsg))[0] = fd; | 
|  | 341 |  | 
| Teddie Stenvi | 8f5daad | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 342 | flags = fcntl(proc->socket,F_GETFL,0); | 
|  | 343 |  | 
|  | 344 | if (flags == -1) { | 
|  | 345 | D("failed to get cntl flags for socket %d: %s\n", | 
|  | 346 | proc->pid, strerror(errno)); | 
|  | 347 | goto CloseProcess; | 
|  | 348 |  | 
|  | 349 | } | 
|  | 350 |  | 
|  | 351 | if (fcntl(proc->socket, F_SETFL, flags & ~O_NONBLOCK) == -1) { | 
|  | 352 | D("failed to remove O_NONBLOCK flag for socket %d: %s\n", | 
|  | 353 | proc->pid, strerror(errno)); | 
|  | 354 | goto CloseProcess; | 
|  | 355 | } | 
|  | 356 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 357 | for (;;) { | 
|  | 358 | ret = sendmsg(proc->socket, &msg, 0); | 
| Teddie Stenvi | 8f5daad | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 359 | if (ret >= 0) { | 
|  | 360 | adb_close(fd); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 361 | break; | 
| Teddie Stenvi | 8f5daad | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 362 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 363 | if (errno == EINTR) | 
|  | 364 | continue; | 
|  | 365 | D("sending new file descriptor to JDWP %d failed: %s\n", | 
|  | 366 | proc->pid, strerror(errno)); | 
|  | 367 | goto CloseProcess; | 
|  | 368 | } | 
|  | 369 |  | 
|  | 370 | D("sent file descriptor %d to JDWP process %d\n", | 
|  | 371 | fd, proc->pid); | 
|  | 372 |  | 
|  | 373 | for (n = 1; n < proc->out_count; n++) | 
|  | 374 | proc->out_fds[n-1] = proc->out_fds[n]; | 
|  | 375 |  | 
| Teddie Stenvi | 8f5daad | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 376 | if (fcntl(proc->socket, F_SETFL, flags) == -1) { | 
|  | 377 | D("failed to set O_NONBLOCK flag for socket %d: %s\n", | 
|  | 378 | proc->pid, strerror(errno)); | 
|  | 379 | goto CloseProcess; | 
|  | 380 | } | 
|  | 381 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 382 | if (--proc->out_count == 0) | 
|  | 383 | fdevent_del( proc->fde, FDE_WRITE ); | 
|  | 384 | } | 
|  | 385 | } | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 |  | 
|  | 389 | int | 
|  | 390 | create_jdwp_connection_fd(int  pid) | 
|  | 391 | { | 
|  | 392 | JdwpProcess*  proc = _jdwp_list.next; | 
|  | 393 |  | 
|  | 394 | D("looking for pid %d in JDWP process list\n", pid); | 
|  | 395 | for ( ; proc != &_jdwp_list; proc = proc->next ) { | 
|  | 396 | if (proc->pid == pid) { | 
|  | 397 | goto FoundIt; | 
|  | 398 | } | 
|  | 399 | } | 
|  | 400 | D("search failed !!\n"); | 
|  | 401 | return -1; | 
|  | 402 |  | 
|  | 403 | FoundIt: | 
|  | 404 | { | 
|  | 405 | int  fds[2]; | 
|  | 406 |  | 
|  | 407 | if (proc->out_count >= MAX_OUT_FDS) { | 
|  | 408 | D("%s: too many pending JDWP connection for pid %d\n", | 
|  | 409 | __FUNCTION__, pid); | 
|  | 410 | return -1; | 
|  | 411 | } | 
|  | 412 |  | 
|  | 413 | if (adb_socketpair(fds) < 0) { | 
|  | 414 | D("%s: socket pair creation failed: %s\n", | 
|  | 415 | __FUNCTION__, strerror(errno)); | 
|  | 416 | return -1; | 
|  | 417 | } | 
| leozwang | adb09fa | 2014-08-15 09:51:27 -0700 | [diff] [blame] | 418 | D("socketpair: (%d,%d)", fds[0], fds[1]); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 419 |  | 
|  | 420 | proc->out_fds[ proc->out_count ] = fds[1]; | 
|  | 421 | if (++proc->out_count == 1) | 
|  | 422 | fdevent_add( proc->fde, FDE_WRITE ); | 
|  | 423 |  | 
|  | 424 | return fds[0]; | 
|  | 425 | } | 
|  | 426 | } | 
|  | 427 |  | 
|  | 428 | /**  VM DEBUG CONTROL SOCKET | 
|  | 429 | ** | 
|  | 430 | **  we do implement a custom asocket to receive the data | 
|  | 431 | **/ | 
|  | 432 |  | 
|  | 433 | /* name of the debug control Unix socket */ | 
|  | 434 | #define  JDWP_CONTROL_NAME      "\0jdwp-control" | 
|  | 435 | #define  JDWP_CONTROL_NAME_LEN  (sizeof(JDWP_CONTROL_NAME)-1) | 
|  | 436 |  | 
|  | 437 | typedef struct { | 
|  | 438 | int       listen_socket; | 
|  | 439 | fdevent*  fde; | 
|  | 440 |  | 
|  | 441 | } JdwpControl; | 
|  | 442 |  | 
|  | 443 |  | 
|  | 444 | static void | 
|  | 445 | jdwp_control_event(int  s, unsigned events, void*  user); | 
|  | 446 |  | 
|  | 447 |  | 
|  | 448 | static int | 
|  | 449 | jdwp_control_init( JdwpControl*  control, | 
|  | 450 | const char*   sockname, | 
|  | 451 | int           socknamelen ) | 
|  | 452 | { | 
|  | 453 | struct sockaddr_un   addr; | 
|  | 454 | socklen_t            addrlen; | 
|  | 455 | int                  s; | 
|  | 456 | int                  maxpath = sizeof(addr.sun_path); | 
|  | 457 | int                  pathlen = socknamelen; | 
|  | 458 |  | 
|  | 459 | if (pathlen >= maxpath) { | 
|  | 460 | D( "vm debug control socket name too long (%d extra chars)\n", | 
|  | 461 | pathlen+1-maxpath ); | 
|  | 462 | return -1; | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | memset(&addr, 0, sizeof(addr)); | 
|  | 466 | addr.sun_family = AF_UNIX; | 
|  | 467 | memcpy(addr.sun_path, sockname, socknamelen); | 
|  | 468 |  | 
|  | 469 | s = socket( AF_UNIX, SOCK_STREAM, 0 ); | 
|  | 470 | if (s < 0) { | 
|  | 471 | D( "could not create vm debug control socket. %d: %s\n", | 
|  | 472 | errno, strerror(errno)); | 
|  | 473 | return -1; | 
|  | 474 | } | 
|  | 475 |  | 
|  | 476 | addrlen = (pathlen + sizeof(addr.sun_family)); | 
|  | 477 |  | 
|  | 478 | if (bind(s, (struct sockaddr*)&addr, addrlen) < 0) { | 
|  | 479 | D( "could not bind vm debug control socket: %d: %s\n", | 
|  | 480 | errno, strerror(errno) ); | 
|  | 481 | adb_close(s); | 
|  | 482 | return -1; | 
|  | 483 | } | 
|  | 484 |  | 
|  | 485 | if ( listen(s, 4) < 0 ) { | 
|  | 486 | D("listen failed in jdwp control socket: %d: %s\n", | 
|  | 487 | errno, strerror(errno)); | 
|  | 488 | adb_close(s); | 
|  | 489 | return -1; | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | control->listen_socket = s; | 
|  | 493 |  | 
|  | 494 | control->fde = fdevent_create(s, jdwp_control_event, control); | 
|  | 495 | if (control->fde == NULL) { | 
|  | 496 | D( "could not create fdevent for jdwp control socket\n" ); | 
|  | 497 | adb_close(s); | 
|  | 498 | return -1; | 
|  | 499 | } | 
|  | 500 |  | 
|  | 501 | /* only wait for incoming connections */ | 
|  | 502 | fdevent_add(control->fde, FDE_READ); | 
| Benoit Goby | 95ef828 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 503 | close_on_exec(s); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 504 |  | 
|  | 505 | D("jdwp control socket started (%d)\n", control->listen_socket); | 
|  | 506 | return 0; | 
|  | 507 | } | 
|  | 508 |  | 
|  | 509 |  | 
|  | 510 | static void | 
|  | 511 | jdwp_control_event( int  s, unsigned  events, void*  _control ) | 
|  | 512 | { | 
|  | 513 | JdwpControl*  control = (JdwpControl*) _control; | 
|  | 514 |  | 
|  | 515 | if (events & FDE_READ) { | 
|  | 516 | struct sockaddr   addr; | 
|  | 517 | socklen_t         addrlen = sizeof(addr); | 
|  | 518 | int               s = -1; | 
|  | 519 | JdwpProcess*      proc; | 
|  | 520 |  | 
|  | 521 | do { | 
|  | 522 | s = adb_socket_accept( control->listen_socket, &addr, &addrlen ); | 
|  | 523 | if (s < 0) { | 
|  | 524 | if (errno == EINTR) | 
|  | 525 | continue; | 
|  | 526 | if (errno == ECONNABORTED) { | 
|  | 527 | /* oops, the JDWP process died really quick */ | 
|  | 528 | D("oops, the JDWP process died really quick\n"); | 
|  | 529 | return; | 
|  | 530 | } | 
|  | 531 | /* the socket is probably closed ? */ | 
|  | 532 | D( "weird accept() failed on jdwp control socket: %s\n", | 
|  | 533 | strerror(errno) ); | 
|  | 534 | return; | 
|  | 535 | } | 
|  | 536 | } | 
|  | 537 | while (s < 0); | 
|  | 538 |  | 
|  | 539 | proc = jdwp_process_alloc( s ); | 
|  | 540 | if (proc == NULL) | 
|  | 541 | return; | 
|  | 542 | } | 
|  | 543 | } | 
|  | 544 |  | 
|  | 545 |  | 
|  | 546 | static JdwpControl   _jdwp_control; | 
|  | 547 |  | 
|  | 548 | /** "jdwp" local service implementation | 
|  | 549 | ** this simply returns the list of known JDWP process pids | 
|  | 550 | **/ | 
|  | 551 |  | 
|  | 552 | typedef struct { | 
|  | 553 | asocket  socket; | 
|  | 554 | int      pass; | 
|  | 555 | } JdwpSocket; | 
|  | 556 |  | 
|  | 557 | static void | 
|  | 558 | jdwp_socket_close( asocket*  s ) | 
|  | 559 | { | 
|  | 560 | asocket*  peer = s->peer; | 
|  | 561 |  | 
|  | 562 | remove_socket(s); | 
|  | 563 |  | 
|  | 564 | if (peer) { | 
|  | 565 | peer->peer = NULL; | 
|  | 566 | peer->close(peer); | 
|  | 567 | } | 
|  | 568 | free(s); | 
|  | 569 | } | 
|  | 570 |  | 
|  | 571 | static int | 
|  | 572 | jdwp_socket_enqueue( asocket*  s, apacket*  p ) | 
|  | 573 | { | 
|  | 574 | /* you can't write to this asocket */ | 
|  | 575 | put_apacket(p); | 
|  | 576 | s->peer->close(s->peer); | 
|  | 577 | return -1; | 
|  | 578 | } | 
|  | 579 |  | 
|  | 580 |  | 
|  | 581 | static void | 
|  | 582 | jdwp_socket_ready( asocket*  s ) | 
|  | 583 | { | 
|  | 584 | JdwpSocket*  jdwp = (JdwpSocket*)s; | 
|  | 585 | asocket*     peer = jdwp->socket.peer; | 
|  | 586 |  | 
|  | 587 | /* on the first call, send the list of pids, | 
|  | 588 | * on the second one, close the connection | 
|  | 589 | */ | 
|  | 590 | if (jdwp->pass == 0) { | 
|  | 591 | apacket*  p = get_apacket(); | 
|  | 592 | p->len = jdwp_process_list((char*)p->data, MAX_PAYLOAD); | 
|  | 593 | peer->enqueue(peer, p); | 
|  | 594 | jdwp->pass = 1; | 
|  | 595 | } | 
|  | 596 | else { | 
|  | 597 | peer->close(peer); | 
|  | 598 | } | 
|  | 599 | } | 
|  | 600 |  | 
|  | 601 | asocket* | 
|  | 602 | create_jdwp_service_socket( void ) | 
|  | 603 | { | 
|  | 604 | JdwpSocket*  s = calloc(sizeof(*s),1); | 
|  | 605 |  | 
|  | 606 | if (s == NULL) | 
|  | 607 | return NULL; | 
|  | 608 |  | 
|  | 609 | install_local_socket(&s->socket); | 
|  | 610 |  | 
|  | 611 | s->socket.ready   = jdwp_socket_ready; | 
|  | 612 | s->socket.enqueue = jdwp_socket_enqueue; | 
|  | 613 | s->socket.close   = jdwp_socket_close; | 
|  | 614 | s->pass           = 0; | 
|  | 615 |  | 
|  | 616 | return &s->socket; | 
|  | 617 | } | 
|  | 618 |  | 
|  | 619 | /** "track-jdwp" local service implementation | 
|  | 620 | ** this periodically sends the list of known JDWP process pids | 
|  | 621 | ** to the client... | 
|  | 622 | **/ | 
|  | 623 |  | 
|  | 624 | typedef struct JdwpTracker  JdwpTracker; | 
|  | 625 |  | 
|  | 626 | struct JdwpTracker { | 
|  | 627 | asocket       socket; | 
|  | 628 | JdwpTracker*  next; | 
|  | 629 | JdwpTracker*  prev; | 
|  | 630 | int           need_update; | 
|  | 631 | }; | 
|  | 632 |  | 
|  | 633 | static JdwpTracker   _jdwp_trackers_list; | 
|  | 634 |  | 
|  | 635 |  | 
|  | 636 | static void | 
|  | 637 | jdwp_process_list_updated(void) | 
|  | 638 | { | 
|  | 639 | char             buffer[1024]; | 
|  | 640 | int              len; | 
|  | 641 | JdwpTracker*  t = _jdwp_trackers_list.next; | 
|  | 642 |  | 
|  | 643 | len = jdwp_process_list_msg(buffer, sizeof(buffer)); | 
|  | 644 |  | 
|  | 645 | for ( ; t != &_jdwp_trackers_list; t = t->next ) { | 
|  | 646 | apacket*  p    = get_apacket(); | 
|  | 647 | asocket*  peer = t->socket.peer; | 
|  | 648 | memcpy(p->data, buffer, len); | 
|  | 649 | p->len = len; | 
|  | 650 | peer->enqueue( peer, p ); | 
|  | 651 | } | 
|  | 652 | } | 
|  | 653 |  | 
|  | 654 | static void | 
|  | 655 | jdwp_tracker_close( asocket*  s ) | 
|  | 656 | { | 
|  | 657 | JdwpTracker*  tracker = (JdwpTracker*) s; | 
|  | 658 | asocket*      peer    = s->peer; | 
|  | 659 |  | 
|  | 660 | if (peer) { | 
|  | 661 | peer->peer = NULL; | 
|  | 662 | peer->close(peer); | 
|  | 663 | } | 
|  | 664 |  | 
|  | 665 | remove_socket(s); | 
|  | 666 |  | 
|  | 667 | tracker->prev->next = tracker->next; | 
|  | 668 | tracker->next->prev = tracker->prev; | 
|  | 669 |  | 
|  | 670 | free(s); | 
|  | 671 | } | 
|  | 672 |  | 
|  | 673 | static void | 
|  | 674 | jdwp_tracker_ready( asocket*  s ) | 
|  | 675 | { | 
|  | 676 | JdwpTracker*  t = (JdwpTracker*) s; | 
|  | 677 |  | 
|  | 678 | if (t->need_update) { | 
|  | 679 | apacket*  p = get_apacket(); | 
|  | 680 | t->need_update = 0; | 
|  | 681 | p->len = jdwp_process_list_msg((char*)p->data, sizeof(p->data)); | 
|  | 682 | s->peer->enqueue(s->peer, p); | 
|  | 683 | } | 
|  | 684 | } | 
|  | 685 |  | 
|  | 686 | static int | 
|  | 687 | jdwp_tracker_enqueue( asocket*  s, apacket*  p ) | 
|  | 688 | { | 
|  | 689 | /* you can't write to this socket */ | 
|  | 690 | put_apacket(p); | 
|  | 691 | s->peer->close(s->peer); | 
|  | 692 | return -1; | 
|  | 693 | } | 
|  | 694 |  | 
|  | 695 |  | 
|  | 696 | asocket* | 
|  | 697 | create_jdwp_tracker_service_socket( void ) | 
|  | 698 | { | 
|  | 699 | JdwpTracker*  t = calloc(sizeof(*t),1); | 
|  | 700 |  | 
|  | 701 | if (t == NULL) | 
|  | 702 | return NULL; | 
|  | 703 |  | 
|  | 704 | t->next = &_jdwp_trackers_list; | 
|  | 705 | t->prev = t->next->prev; | 
|  | 706 |  | 
|  | 707 | t->next->prev = t; | 
|  | 708 | t->prev->next = t; | 
|  | 709 |  | 
|  | 710 | install_local_socket(&t->socket); | 
|  | 711 |  | 
|  | 712 | t->socket.ready   = jdwp_tracker_ready; | 
|  | 713 | t->socket.enqueue = jdwp_tracker_enqueue; | 
|  | 714 | t->socket.close   = jdwp_tracker_close; | 
|  | 715 | t->need_update    = 1; | 
|  | 716 |  | 
|  | 717 | return &t->socket; | 
|  | 718 | } | 
|  | 719 |  | 
|  | 720 |  | 
|  | 721 | int | 
|  | 722 | init_jdwp(void) | 
|  | 723 | { | 
|  | 724 | _jdwp_list.next = &_jdwp_list; | 
|  | 725 | _jdwp_list.prev = &_jdwp_list; | 
|  | 726 |  | 
|  | 727 | _jdwp_trackers_list.next = &_jdwp_trackers_list; | 
|  | 728 | _jdwp_trackers_list.prev = &_jdwp_trackers_list; | 
|  | 729 |  | 
|  | 730 | return jdwp_control_init( &_jdwp_control, | 
|  | 731 | JDWP_CONTROL_NAME, | 
|  | 732 | JDWP_CONTROL_NAME_LEN ); | 
|  | 733 | } | 
|  | 734 |  | 
|  | 735 | #endif /* !ADB_HOST */ | 
|  | 736 |  |