Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 1 | /* |
| 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 Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 17 | /* implement the "debug-ports" and "track-debug-ports" device services */ |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #define TRACE_TAG TRACE_JDWP |
| 20 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include "sysdeps.h" |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 22 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <stdio.h> |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <string.h> |
Teddie Stenvi | f56e7f5 | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 27 | #include <unistd.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 29 | #include "adb.h" |
| 30 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | /* here's how these things work. |
| 32 | |
| 33 | when adbd starts, it creates a unix server socket |
| 34 | named @vm-debug-control (@ is a shortcut for "first byte is zero" |
| 35 | to use the private namespace instead of the file system) |
| 36 | |
| 37 | when a new JDWP daemon thread starts in a new VM process, it creates |
| 38 | a connection to @vm-debug-control to announce its availability. |
| 39 | |
| 40 | |
| 41 | JDWP thread @vm-debug-control |
| 42 | | | |
| 43 | |-------------------------------> | |
| 44 | | hello I'm in process <pid> | |
| 45 | | | |
| 46 | | | |
| 47 | |
| 48 | the connection is kept alive. it will be closed automatically if |
| 49 | the JDWP process terminates (this allows adbd to detect dead |
| 50 | processes). |
| 51 | |
| 52 | adbd thus maintains a list of "active" JDWP processes. it can send |
| 53 | its content to clients through the "device:debug-ports" service, |
| 54 | or even updates through the "device:track-debug-ports" service. |
| 55 | |
| 56 | when a debugger wants to connect, it simply runs the command |
| 57 | equivalent to "adb forward tcp:<hostport> jdwp:<pid>" |
| 58 | |
| 59 | "jdwp:<pid>" is a new forward destination format used to target |
| 60 | a given JDWP process on the device. when sutch a request arrives, |
| 61 | adbd does the following: |
| 62 | |
| 63 | - first, it calls socketpair() to create a pair of equivalent |
| 64 | sockets. |
| 65 | |
| 66 | - it attaches the first socket in the pair to a local socket |
| 67 | which is itself attached to the transport's remote socket: |
| 68 | |
| 69 | |
| 70 | - it sends the file descriptor of the second socket directly |
| 71 | to the JDWP process with the help of sendmsg() |
| 72 | |
| 73 | |
| 74 | JDWP thread @vm-debug-control |
| 75 | | | |
| 76 | | <----------------------| |
| 77 | | OK, try this file descriptor | |
| 78 | | | |
| 79 | | | |
| 80 | |
| 81 | then, the JDWP thread uses this new socket descriptor as its |
| 82 | pass-through connection to the debugger (and receives the |
| 83 | JDWP-Handshake message, answers to it, etc...) |
| 84 | |
| 85 | this gives the following graphics: |
| 86 | ____________________________________ |
| 87 | | | |
| 88 | | ADB Server (host) | |
| 89 | | | |
| 90 | Debugger <---> LocalSocket <----> RemoteSocket | |
| 91 | | ^^ | |
| 92 | |___________________________||_______| |
| 93 | || |
| 94 | Transport || |
| 95 | (TCP for emulator - USB for device) || |
| 96 | || |
| 97 | ___________________________||_______ |
| 98 | | || | |
| 99 | | ADBD (device) || | |
| 100 | | VV | |
| 101 | JDWP <======> LocalSocket <----> RemoteSocket | |
| 102 | | | |
| 103 | |____________________________________| |
| 104 | |
| 105 | due to the way adb works, this doesn't need a special socket |
| 106 | type or fancy handling of socket termination if either the debugger |
| 107 | or the JDWP process closes the connection. |
| 108 | |
| 109 | THIS IS THE SIMPLEST IMPLEMENTATION I COULD FIND, IF YOU HAPPEN |
| 110 | TO HAVE A BETTER IDEA, LET ME KNOW - Digit |
| 111 | |
| 112 | **********************************************************************/ |
| 113 | |
| 114 | /** JDWP PID List Support Code |
| 115 | ** for each JDWP process, we record its pid and its connected socket |
| 116 | **/ |
| 117 | |
| 118 | #define MAX_OUT_FDS 4 |
| 119 | |
| 120 | #if !ADB_HOST |
| 121 | |
| 122 | #include <sys/socket.h> |
| 123 | #include <sys/un.h> |
| 124 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 125 | struct 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 | |
| 139 | static JdwpProcess _jdwp_list; |
| 140 | |
| 141 | static int |
| 142 | jdwp_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 | |
| 165 | static int |
| 166 | jdwp_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 | |
| 176 | static void jdwp_process_list_updated(void); |
| 177 | |
| 178 | static void |
| 179 | jdwp_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 Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 188 | adb_shutdown(proc->socket); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | 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 | |
| 211 | static void jdwp_process_event(int, unsigned, void*); /* forward */ |
| 212 | |
| 213 | |
| 214 | static JdwpProcess* |
| 215 | jdwp_process_alloc( int socket ) |
| 216 | { |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 217 | JdwpProcess* proc = reinterpret_cast<JdwpProcess*>( |
| 218 | calloc(1, sizeof(*proc))); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 219 | |
| 220 | if (proc == NULL) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 221 | D("not enough memory to create new JDWP process"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 232 | D("could not create fdevent for new JDWP process" ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | |
| 255 | static void |
| 256 | jdwp_process_event( int socket, unsigned events, void* _proc ) |
| 257 | { |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 258 | JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(_proc); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 259 | |
| 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 */ |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 274 | D("weird unknown JDWP process failure: %s", |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 275 | strerror(errno)); |
| 276 | |
| 277 | goto CloseProcess; |
| 278 | } |
| 279 | if (len == 0) { /* end of stream ? */ |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 280 | D("weird end-of-stream from unknown JDWP process"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 292 | D("could not decode JDWP %p PID number: '%s'", proc, temp); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 293 | goto CloseProcess; |
| 294 | } |
| 295 | |
| 296 | /* all is well, keep reading to detect connection closure */ |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 297 | D("Adding pid %d to jdwp process list", proc->pid); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 315 | D("terminating JDWP %d connection: %s", proc->pid, |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 316 | strerror(errno)); |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | else { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 321 | D( "ignoring unexpected JDWP %d control socket activity (%d bytes)", |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 322 | proc->pid, len ); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | CloseProcess: |
| 327 | if (proc->pid >= 0) |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 328 | D( "remove pid %d to jdwp process list", proc->pid ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 329 | jdwp_process_free(proc); |
| 330 | return; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | if (events & FDE_WRITE) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 335 | D("trying to write to JDWP pid controli (count=%d first=%d) %d", |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 Stenvi | f56e7f5 | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 345 | int flags; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 346 | |
| 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 Stenvi | f56e7f5 | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 363 | flags = fcntl(proc->socket,F_GETFL,0); |
| 364 | |
| 365 | if (flags == -1) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 366 | D("failed to get cntl flags for socket %d: %s", |
Teddie Stenvi | f56e7f5 | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 367 | proc->pid, strerror(errno)); |
| 368 | goto CloseProcess; |
| 369 | |
| 370 | } |
| 371 | |
| 372 | if (fcntl(proc->socket, F_SETFL, flags & ~O_NONBLOCK) == -1) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 373 | D("failed to remove O_NONBLOCK flag for socket %d: %s", |
Teddie Stenvi | f56e7f5 | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 374 | proc->pid, strerror(errno)); |
| 375 | goto CloseProcess; |
| 376 | } |
| 377 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 378 | for (;;) { |
| 379 | ret = sendmsg(proc->socket, &msg, 0); |
Teddie Stenvi | f56e7f5 | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 380 | if (ret >= 0) { |
| 381 | adb_close(fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 382 | break; |
Teddie Stenvi | f56e7f5 | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 383 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 384 | if (errno == EINTR) |
| 385 | continue; |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 386 | D("sending new file descriptor to JDWP %d failed: %s", |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 387 | proc->pid, strerror(errno)); |
| 388 | goto CloseProcess; |
| 389 | } |
| 390 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 391 | D("sent file descriptor %d to JDWP process %d", |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 Stenvi | f56e7f5 | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 397 | if (fcntl(proc->socket, F_SETFL, flags) == -1) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 398 | D("failed to set O_NONBLOCK flag for socket %d: %s", |
Teddie Stenvi | f56e7f5 | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 399 | proc->pid, strerror(errno)); |
| 400 | goto CloseProcess; |
| 401 | } |
| 402 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 403 | if (--proc->out_count == 0) |
| 404 | fdevent_del( proc->fde, FDE_WRITE ); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | |
| 410 | int |
| 411 | create_jdwp_connection_fd(int pid) |
| 412 | { |
| 413 | JdwpProcess* proc = _jdwp_list.next; |
| 414 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 415 | D("looking for pid %d in JDWP process list", pid); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 416 | for ( ; proc != &_jdwp_list; proc = proc->next ) { |
| 417 | if (proc->pid == pid) { |
| 418 | goto FoundIt; |
| 419 | } |
| 420 | } |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 421 | D("search failed !!"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 422 | return -1; |
| 423 | |
| 424 | FoundIt: |
| 425 | { |
| 426 | int fds[2]; |
| 427 | |
| 428 | if (proc->out_count >= MAX_OUT_FDS) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 429 | D("%s: too many pending JDWP connection for pid %d", |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 430 | __FUNCTION__, pid); |
| 431 | return -1; |
| 432 | } |
| 433 | |
| 434 | if (adb_socketpair(fds) < 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 435 | D("%s: socket pair creation failed: %s", |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 436 | __FUNCTION__, strerror(errno)); |
| 437 | return -1; |
| 438 | } |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 439 | D("socketpair: (%d,%d)", fds[0], fds[1]); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 440 | |
| 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 | |
Elliott Hughes | fe7ff81 | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 458 | struct JdwpControl { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 459 | int listen_socket; |
| 460 | fdevent* fde; |
Elliott Hughes | fe7ff81 | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 461 | }; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 462 | |
| 463 | |
| 464 | static void |
| 465 | jdwp_control_event(int s, unsigned events, void* user); |
| 466 | |
| 467 | |
| 468 | static int |
| 469 | jdwp_control_init( JdwpControl* control, |
| 470 | const char* sockname, |
| 471 | int socknamelen ) |
| 472 | { |
| 473 | struct sockaddr_un addr; |
| 474 | socklen_t addrlen; |
| 475 | int s; |
| 476 | int maxpath = sizeof(addr.sun_path); |
| 477 | int pathlen = socknamelen; |
| 478 | |
| 479 | if (pathlen >= maxpath) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 480 | D( "vm debug control socket name too long (%d extra chars)", |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 481 | pathlen+1-maxpath ); |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | memset(&addr, 0, sizeof(addr)); |
| 486 | addr.sun_family = AF_UNIX; |
| 487 | memcpy(addr.sun_path, sockname, socknamelen); |
| 488 | |
| 489 | s = socket( AF_UNIX, SOCK_STREAM, 0 ); |
| 490 | if (s < 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 491 | D( "could not create vm debug control socket. %d: %s", |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 492 | errno, strerror(errno)); |
| 493 | return -1; |
| 494 | } |
| 495 | |
| 496 | addrlen = (pathlen + sizeof(addr.sun_family)); |
| 497 | |
| 498 | if (bind(s, (struct sockaddr*)&addr, addrlen) < 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 499 | D( "could not bind vm debug control socket: %d: %s", |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 500 | errno, strerror(errno) ); |
| 501 | adb_close(s); |
| 502 | return -1; |
| 503 | } |
| 504 | |
| 505 | if ( listen(s, 4) < 0 ) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 506 | D("listen failed in jdwp control socket: %d: %s", |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 507 | errno, strerror(errno)); |
| 508 | adb_close(s); |
| 509 | return -1; |
| 510 | } |
| 511 | |
| 512 | control->listen_socket = s; |
| 513 | |
| 514 | control->fde = fdevent_create(s, jdwp_control_event, control); |
| 515 | if (control->fde == NULL) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 516 | D( "could not create fdevent for jdwp control socket" ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 517 | adb_close(s); |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | /* only wait for incoming connections */ |
| 522 | fdevent_add(control->fde, FDE_READ); |
Benoit Goby | f4ded74 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 523 | close_on_exec(s); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 524 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 525 | D("jdwp control socket started (%d)", control->listen_socket); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | |
| 530 | static void |
| 531 | jdwp_control_event( int s, unsigned events, void* _control ) |
| 532 | { |
| 533 | JdwpControl* control = (JdwpControl*) _control; |
| 534 | |
| 535 | if (events & FDE_READ) { |
| 536 | struct sockaddr addr; |
| 537 | socklen_t addrlen = sizeof(addr); |
| 538 | int s = -1; |
| 539 | JdwpProcess* proc; |
| 540 | |
| 541 | do { |
| 542 | s = adb_socket_accept( control->listen_socket, &addr, &addrlen ); |
| 543 | if (s < 0) { |
| 544 | if (errno == EINTR) |
| 545 | continue; |
| 546 | if (errno == ECONNABORTED) { |
| 547 | /* oops, the JDWP process died really quick */ |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 548 | D("oops, the JDWP process died really quick"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 549 | return; |
| 550 | } |
| 551 | /* the socket is probably closed ? */ |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 552 | D( "weird accept() failed on jdwp control socket: %s", |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 553 | strerror(errno) ); |
| 554 | return; |
| 555 | } |
| 556 | } |
| 557 | while (s < 0); |
| 558 | |
| 559 | proc = jdwp_process_alloc( s ); |
| 560 | if (proc == NULL) |
| 561 | return; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | |
| 566 | static JdwpControl _jdwp_control; |
| 567 | |
| 568 | /** "jdwp" local service implementation |
| 569 | ** this simply returns the list of known JDWP process pids |
| 570 | **/ |
| 571 | |
Elliott Hughes | fe7ff81 | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 572 | struct JdwpSocket { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 573 | asocket socket; |
| 574 | int pass; |
Elliott Hughes | fe7ff81 | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 575 | }; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 576 | |
| 577 | static void |
| 578 | jdwp_socket_close( asocket* s ) |
| 579 | { |
| 580 | asocket* peer = s->peer; |
| 581 | |
| 582 | remove_socket(s); |
| 583 | |
| 584 | if (peer) { |
| 585 | peer->peer = NULL; |
| 586 | peer->close(peer); |
| 587 | } |
| 588 | free(s); |
| 589 | } |
| 590 | |
| 591 | static int |
| 592 | jdwp_socket_enqueue( asocket* s, apacket* p ) |
| 593 | { |
| 594 | /* you can't write to this asocket */ |
| 595 | put_apacket(p); |
| 596 | s->peer->close(s->peer); |
| 597 | return -1; |
| 598 | } |
| 599 | |
| 600 | |
| 601 | static void |
| 602 | jdwp_socket_ready( asocket* s ) |
| 603 | { |
| 604 | JdwpSocket* jdwp = (JdwpSocket*)s; |
| 605 | asocket* peer = jdwp->socket.peer; |
| 606 | |
| 607 | /* on the first call, send the list of pids, |
| 608 | * on the second one, close the connection |
| 609 | */ |
| 610 | if (jdwp->pass == 0) { |
| 611 | apacket* p = get_apacket(); |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 612 | p->len = jdwp_process_list((char*)p->data, s->get_max_payload()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 613 | peer->enqueue(peer, p); |
| 614 | jdwp->pass = 1; |
| 615 | } |
| 616 | else { |
| 617 | peer->close(peer); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | asocket* |
| 622 | create_jdwp_service_socket( void ) |
| 623 | { |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 624 | JdwpSocket* s = reinterpret_cast<JdwpSocket*>(calloc(sizeof(*s), 1)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 625 | |
| 626 | if (s == NULL) |
| 627 | return NULL; |
| 628 | |
| 629 | install_local_socket(&s->socket); |
| 630 | |
| 631 | s->socket.ready = jdwp_socket_ready; |
| 632 | s->socket.enqueue = jdwp_socket_enqueue; |
| 633 | s->socket.close = jdwp_socket_close; |
| 634 | s->pass = 0; |
| 635 | |
| 636 | return &s->socket; |
| 637 | } |
| 638 | |
| 639 | /** "track-jdwp" local service implementation |
| 640 | ** this periodically sends the list of known JDWP process pids |
| 641 | ** to the client... |
| 642 | **/ |
| 643 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 644 | struct JdwpTracker { |
| 645 | asocket socket; |
| 646 | JdwpTracker* next; |
| 647 | JdwpTracker* prev; |
| 648 | int need_update; |
| 649 | }; |
| 650 | |
| 651 | static JdwpTracker _jdwp_trackers_list; |
| 652 | |
| 653 | |
| 654 | static void |
| 655 | jdwp_process_list_updated(void) |
| 656 | { |
| 657 | char buffer[1024]; |
| 658 | int len; |
| 659 | JdwpTracker* t = _jdwp_trackers_list.next; |
| 660 | |
| 661 | len = jdwp_process_list_msg(buffer, sizeof(buffer)); |
| 662 | |
| 663 | for ( ; t != &_jdwp_trackers_list; t = t->next ) { |
| 664 | apacket* p = get_apacket(); |
| 665 | asocket* peer = t->socket.peer; |
| 666 | memcpy(p->data, buffer, len); |
| 667 | p->len = len; |
| 668 | peer->enqueue( peer, p ); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | static void |
| 673 | jdwp_tracker_close( asocket* s ) |
| 674 | { |
| 675 | JdwpTracker* tracker = (JdwpTracker*) s; |
| 676 | asocket* peer = s->peer; |
| 677 | |
| 678 | if (peer) { |
| 679 | peer->peer = NULL; |
| 680 | peer->close(peer); |
| 681 | } |
| 682 | |
| 683 | remove_socket(s); |
| 684 | |
| 685 | tracker->prev->next = tracker->next; |
| 686 | tracker->next->prev = tracker->prev; |
| 687 | |
| 688 | free(s); |
| 689 | } |
| 690 | |
| 691 | static void |
| 692 | jdwp_tracker_ready( asocket* s ) |
| 693 | { |
| 694 | JdwpTracker* t = (JdwpTracker*) s; |
| 695 | |
| 696 | if (t->need_update) { |
| 697 | apacket* p = get_apacket(); |
| 698 | t->need_update = 0; |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 699 | p->len = jdwp_process_list_msg((char*)p->data, s->get_max_payload()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 700 | s->peer->enqueue(s->peer, p); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | static int |
| 705 | jdwp_tracker_enqueue( asocket* s, apacket* p ) |
| 706 | { |
| 707 | /* you can't write to this socket */ |
| 708 | put_apacket(p); |
| 709 | s->peer->close(s->peer); |
| 710 | return -1; |
| 711 | } |
| 712 | |
| 713 | |
| 714 | asocket* |
| 715 | create_jdwp_tracker_service_socket( void ) |
| 716 | { |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 717 | JdwpTracker* t = reinterpret_cast<JdwpTracker*>(calloc(sizeof(*t), 1)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 718 | |
| 719 | if (t == NULL) |
| 720 | return NULL; |
| 721 | |
| 722 | t->next = &_jdwp_trackers_list; |
| 723 | t->prev = t->next->prev; |
| 724 | |
| 725 | t->next->prev = t; |
| 726 | t->prev->next = t; |
| 727 | |
| 728 | install_local_socket(&t->socket); |
| 729 | |
| 730 | t->socket.ready = jdwp_tracker_ready; |
| 731 | t->socket.enqueue = jdwp_tracker_enqueue; |
| 732 | t->socket.close = jdwp_tracker_close; |
| 733 | t->need_update = 1; |
| 734 | |
| 735 | return &t->socket; |
| 736 | } |
| 737 | |
| 738 | |
| 739 | int |
| 740 | init_jdwp(void) |
| 741 | { |
| 742 | _jdwp_list.next = &_jdwp_list; |
| 743 | _jdwp_list.prev = &_jdwp_list; |
| 744 | |
| 745 | _jdwp_trackers_list.next = &_jdwp_trackers_list; |
| 746 | _jdwp_trackers_list.prev = &_jdwp_trackers_list; |
| 747 | |
| 748 | return jdwp_control_init( &_jdwp_control, |
| 749 | JDWP_CONTROL_NAME, |
| 750 | JDWP_CONTROL_NAME_LEN ); |
| 751 | } |
| 752 | |
| 753 | #endif /* !ADB_HOST */ |