Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <stdio.h> |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/un.h> |
| 21 | #include <unistd.h> |
| 22 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 23 | #include "base/logging.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 24 | #include "base/stringprintf.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 25 | #include "jdwp/jdwp_priv.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 26 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 27 | #ifdef HAVE_ANDROID_OS |
| 28 | #include "cutils/sockets.h" |
| 29 | #endif |
| 30 | |
| 31 | /* |
| 32 | * The JDWP <-> ADB transport protocol is explained in detail |
| 33 | * in system/core/adb/jdwp_service.c. Here's a summary. |
| 34 | * |
| 35 | * 1/ when the JDWP thread starts, it tries to connect to a Unix |
| 36 | * domain stream socket (@jdwp-control) that is opened by the |
| 37 | * ADB daemon. |
| 38 | * |
| 39 | * 2/ it then sends the current process PID as a string of 4 hexadecimal |
| 40 | * chars (no terminating zero) |
| 41 | * |
| 42 | * 3/ then, it uses recvmsg to receive file descriptors from the |
| 43 | * daemon. each incoming file descriptor is a pass-through to |
| 44 | * a given JDWP debugger, that can be used to read the usual |
| 45 | * JDWP-handshake, etc... |
| 46 | */ |
| 47 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 48 | #define kJdwpControlName "\0jdwp-control" |
| 49 | #define kJdwpControlNameLen (sizeof(kJdwpControlName)-1) |
| 50 | |
| 51 | namespace art { |
| 52 | |
| 53 | namespace JDWP { |
| 54 | |
| 55 | struct JdwpNetState : public JdwpNetStateBase { |
| 56 | int controlSock; |
| 57 | bool awaitingHandshake; |
| 58 | bool shuttingDown; |
| 59 | int wakeFds[2]; |
| 60 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 61 | socklen_t controlAddrLen; |
| 62 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 63 | sockaddr_un controlAddrUn; |
| 64 | sockaddr controlAddrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 65 | } controlAddr; |
| 66 | |
| 67 | JdwpNetState() { |
| 68 | controlSock = -1; |
| 69 | awaitingHandshake = false; |
| 70 | shuttingDown = false; |
| 71 | wakeFds[0] = -1; |
| 72 | wakeFds[1] = -1; |
| 73 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 74 | controlAddr.controlAddrUn.sun_family = AF_UNIX; |
| 75 | controlAddrLen = sizeof(controlAddr.controlAddrUn.sun_family) + kJdwpControlNameLen; |
| 76 | memcpy(controlAddr.controlAddrUn.sun_path, kJdwpControlName, kJdwpControlNameLen); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | static void adbStateFree(JdwpNetState* netState) { |
| 81 | if (netState == NULL) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | if (netState->clientSock >= 0) { |
| 86 | shutdown(netState->clientSock, SHUT_RDWR); |
| 87 | close(netState->clientSock); |
| 88 | } |
| 89 | if (netState->controlSock >= 0) { |
| 90 | shutdown(netState->controlSock, SHUT_RDWR); |
| 91 | close(netState->controlSock); |
| 92 | } |
| 93 | if (netState->wakeFds[0] >= 0) { |
| 94 | close(netState->wakeFds[0]); |
| 95 | netState->wakeFds[0] = -1; |
| 96 | } |
| 97 | if (netState->wakeFds[1] >= 0) { |
| 98 | close(netState->wakeFds[1]); |
| 99 | netState->wakeFds[1] = -1; |
| 100 | } |
| 101 | |
| 102 | delete netState; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Do initial prep work, e.g. binding to ports and opening files. This |
| 107 | * runs in the main thread, before the JDWP thread starts, so it shouldn't |
| 108 | * do anything that might block forever. |
| 109 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 110 | static bool startup(JdwpState* state, const JdwpOptions*) { |
| 111 | JdwpNetState* netState; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 112 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 113 | VLOG(jdwp) << "ADB transport startup"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 114 | |
| 115 | state->netState = netState = new JdwpNetState; |
| 116 | if (netState == NULL) { |
| 117 | return false; |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Receive a file descriptor from ADB. The fd can be used to communicate |
| 124 | * directly with a debugger or DDMS. |
| 125 | * |
| 126 | * Returns the file descriptor on success. On failure, returns -1 and |
| 127 | * closes netState->controlSock. |
| 128 | */ |
| 129 | static int receiveClientFd(JdwpNetState* netState) { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 130 | msghdr msg; |
| 131 | cmsghdr* cmsg; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 132 | iovec iov; |
| 133 | char dummy = '!'; |
| 134 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 135 | cmsghdr cm; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 136 | char buffer[CMSG_SPACE(sizeof(int))]; |
| 137 | } cm_un; |
| 138 | int ret; |
| 139 | |
| 140 | iov.iov_base = &dummy; |
| 141 | iov.iov_len = 1; |
| 142 | msg.msg_name = NULL; |
| 143 | msg.msg_namelen = 0; |
| 144 | msg.msg_iov = &iov; |
| 145 | msg.msg_iovlen = 1; |
| 146 | msg.msg_flags = 0; |
| 147 | msg.msg_control = cm_un.buffer; |
| 148 | msg.msg_controllen = sizeof(cm_un.buffer); |
| 149 | |
| 150 | cmsg = CMSG_FIRSTHDR(&msg); |
| 151 | cmsg->cmsg_len = msg.msg_controllen; |
| 152 | cmsg->cmsg_level = SOL_SOCKET; |
| 153 | cmsg->cmsg_type = SCM_RIGHTS; |
| 154 | ((int*)(void*)CMSG_DATA(cmsg))[0] = -1; |
| 155 | |
| 156 | do { |
| 157 | ret = recvmsg(netState->controlSock, &msg, 0); |
| 158 | } while (ret < 0 && errno == EINTR); |
| 159 | |
| 160 | if (ret <= 0) { |
| 161 | if (ret < 0) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 162 | PLOG(WARNING) << "Receiving file descriptor from ADB failed (socket " << netState->controlSock << ")"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 163 | } |
| 164 | close(netState->controlSock); |
| 165 | netState->controlSock = -1; |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | return ((int*)(void*)CMSG_DATA(cmsg))[0]; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Block forever, waiting for a debugger to connect to us. Called from the |
| 174 | * JDWP thread. |
| 175 | * |
| 176 | * This needs to un-block and return "false" if the VM is shutting down. It |
| 177 | * should return "true" when it successfully accepts a connection. |
| 178 | */ |
| 179 | static bool acceptConnection(JdwpState* state) { |
| 180 | JdwpNetState* netState = state->netState; |
| 181 | int retryCount = 0; |
| 182 | |
| 183 | /* first, ensure that we get a connection to the ADB daemon */ |
| 184 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 185 | retry: |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 186 | if (netState->shuttingDown) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | if (netState->controlSock < 0) { |
| 191 | int sleep_ms = 500; |
| 192 | const int sleep_max_ms = 2*1000; |
| 193 | char buff[5]; |
| 194 | |
| 195 | netState->controlSock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 196 | if (netState->controlSock < 0) { |
| 197 | PLOG(ERROR) << "Could not create ADB control socket"; |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | if (pipe(netState->wakeFds) < 0) { |
| 202 | PLOG(ERROR) << "pipe failed"; |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | snprintf(buff, sizeof(buff), "%04x", getpid()); |
| 207 | buff[4] = 0; |
| 208 | |
| 209 | for (;;) { |
| 210 | /* |
| 211 | * If adbd isn't running, because USB debugging was disabled or |
| 212 | * perhaps the system is restarting it for "adb root", the |
| 213 | * connect() will fail. We loop here forever waiting for it |
| 214 | * to come back. |
| 215 | * |
| 216 | * Waking up and polling every couple of seconds is generally a |
| 217 | * bad thing to do, but we only do this if the application is |
| 218 | * debuggable *and* adbd isn't running. Still, for the sake |
| 219 | * of battery life, we should consider timing out and giving |
| 220 | * up after a few minutes in case somebody ships an app with |
| 221 | * the debuggable flag set. |
| 222 | */ |
| 223 | int ret = connect(netState->controlSock, &netState->controlAddr.controlAddrPlain, netState->controlAddrLen); |
| 224 | if (!ret) { |
| 225 | #ifdef HAVE_ANDROID_OS |
| 226 | if (!socket_peer_is_trusted(netState->controlSock)) { |
| 227 | if (shutdown(netState->controlSock, SHUT_RDWR)) { |
| 228 | PLOG(ERROR) << "trouble shutting down socket"; |
| 229 | } |
| 230 | return false; |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | /* now try to send our pid to the ADB daemon */ |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 235 | ret = TEMP_FAILURE_RETRY(send(netState->controlSock, buff, 4, 0)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 236 | if (ret >= 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 237 | VLOG(jdwp) << StringPrintf("PID sent as '%.*s' to ADB", 4, buff); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 238 | break; |
| 239 | } |
| 240 | |
| 241 | PLOG(ERROR) << "Weird, can't send JDWP process pid to ADB"; |
| 242 | return false; |
| 243 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 244 | if (VLOG_IS_ON(jdwp)) { |
| 245 | PLOG(ERROR) << "Can't connect to ADB control socket"; |
| 246 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 247 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 248 | usleep(sleep_ms * 1000); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 249 | |
| 250 | sleep_ms += (sleep_ms >> 1); |
| 251 | if (sleep_ms > sleep_max_ms) { |
| 252 | sleep_ms = sleep_max_ms; |
| 253 | } |
| 254 | if (netState->shuttingDown) { |
| 255 | return false; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 260 | VLOG(jdwp) << "trying to receive file descriptor from ADB"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 261 | /* now we can receive a client file descriptor */ |
| 262 | netState->clientSock = receiveClientFd(netState); |
| 263 | if (netState->shuttingDown) { |
| 264 | return false; // suppress logs and additional activity |
| 265 | } |
| 266 | if (netState->clientSock < 0) { |
| 267 | if (++retryCount > 5) { |
| 268 | LOG(ERROR) << "adb connection max retries exceeded"; |
| 269 | return false; |
| 270 | } |
| 271 | goto retry; |
| 272 | } else { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 273 | VLOG(jdwp) << "received file descriptor " << netState->clientSock << " from ADB"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 274 | netState->awaitingHandshake = 1; |
| 275 | netState->inputCount = 0; |
| 276 | return true; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Connect out to a debugger (for server=n). Not required. |
| 282 | */ |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 283 | static bool establishConnection(JdwpState*, const JdwpOptions*) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Close a connection from a debugger (which may have already dropped us). |
| 289 | * Only called from the JDWP thread. |
| 290 | */ |
| 291 | static void closeConnection(JdwpState* state) { |
| 292 | CHECK(state != NULL && state->netState != NULL); |
| 293 | |
| 294 | JdwpNetState* netState = state->netState; |
| 295 | if (netState->clientSock < 0) { |
| 296 | return; |
| 297 | } |
| 298 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 299 | VLOG(jdwp) << "+++ closed JDWP <-> ADB connection"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 300 | |
| 301 | close(netState->clientSock); |
| 302 | netState->clientSock = -1; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Close all network stuff, including the socket we use to listen for |
| 307 | * new connections. |
| 308 | * |
| 309 | * May be called from a non-JDWP thread, e.g. when the VM is shutting down. |
| 310 | */ |
| 311 | static void adbStateShutdown(JdwpNetState* netState) { |
| 312 | int controlSock; |
| 313 | int clientSock; |
| 314 | |
| 315 | if (netState == NULL) { |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | netState->shuttingDown = true; |
| 320 | |
| 321 | clientSock = netState->clientSock; |
| 322 | if (clientSock >= 0) { |
| 323 | shutdown(clientSock, SHUT_RDWR); |
| 324 | netState->clientSock = -1; |
| 325 | } |
| 326 | |
| 327 | controlSock = netState->controlSock; |
| 328 | if (controlSock >= 0) { |
| 329 | shutdown(controlSock, SHUT_RDWR); |
| 330 | netState->controlSock = -1; |
| 331 | } |
| 332 | |
| 333 | if (netState->wakeFds[1] >= 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 334 | VLOG(jdwp) << "+++ writing to wakePipe"; |
Elliott Hughes | 068193c | 2013-04-15 16:05:28 -0700 | [diff] [blame^] | 335 | TEMP_FAILURE_RETRY(write(netState->wakeFds[1], "", 1)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | static void netShutdown(JdwpState* state) { |
| 340 | adbStateShutdown(state->netState); |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Free up anything we put in state->netState. This is called after |
| 345 | * "netShutdown", after the JDWP thread has stopped. |
| 346 | */ |
| 347 | static void netFree(JdwpState* state) { |
| 348 | JdwpNetState* netState = state->netState; |
| 349 | adbStateFree(netState); |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Is a debugger connected to us? |
| 354 | */ |
| 355 | static bool isConnected(JdwpState* state) { |
| 356 | return (state->netState != NULL && state->netState->clientSock >= 0); |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Are we still waiting for the JDWP handshake? |
| 361 | */ |
| 362 | static bool awaitingHandshake(JdwpState* state) { |
| 363 | return state->netState->awaitingHandshake; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * Figure out if we have a full packet in the buffer. |
| 368 | */ |
| 369 | static bool haveFullPacket(JdwpNetState* netState) { |
| 370 | if (netState->awaitingHandshake) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 371 | return (netState->inputCount >= kMagicHandshakeLen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 372 | } |
| 373 | if (netState->inputCount < 4) { |
| 374 | return false; |
| 375 | } |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 376 | uint32_t length = Get4BE(netState->inputBuffer); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 377 | return (netState->inputCount >= length); |
| 378 | } |
| 379 | |
| 380 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 381 | * Process incoming data. If no data is available, this will block until |
| 382 | * some arrives. |
| 383 | * |
| 384 | * If we get a full packet, handle it. |
| 385 | * |
| 386 | * To take some of the mystery out of life, we want to reject incoming |
| 387 | * connections if we already have a debugger attached. If we don't, the |
| 388 | * debugger will just mysteriously hang until it times out. We could just |
| 389 | * close the listen socket, but there's a good chance we won't be able to |
| 390 | * bind to the same port again, which would confuse utilities. |
| 391 | * |
| 392 | * Returns "false" on error (indicating that the connection has been severed), |
| 393 | * "true" if things are still okay. |
| 394 | */ |
| 395 | static bool processIncoming(JdwpState* state) { |
| 396 | JdwpNetState* netState = state->netState; |
| 397 | int readCount; |
| 398 | |
| 399 | CHECK_GE(netState->clientSock, 0); |
| 400 | |
| 401 | if (!haveFullPacket(netState)) { |
| 402 | /* read some more, looping until we have data */ |
| 403 | errno = 0; |
| 404 | while (1) { |
| 405 | int selCount; |
| 406 | fd_set readfds; |
| 407 | int maxfd = -1; |
| 408 | int fd; |
| 409 | |
| 410 | FD_ZERO(&readfds); |
| 411 | |
| 412 | /* configure fds; note these may get zapped by another thread */ |
| 413 | fd = netState->controlSock; |
| 414 | if (fd >= 0) { |
| 415 | FD_SET(fd, &readfds); |
| 416 | if (maxfd < fd) { |
| 417 | maxfd = fd; |
| 418 | } |
| 419 | } |
| 420 | fd = netState->clientSock; |
| 421 | if (fd >= 0) { |
| 422 | FD_SET(fd, &readfds); |
| 423 | if (maxfd < fd) { |
| 424 | maxfd = fd; |
| 425 | } |
| 426 | } |
| 427 | fd = netState->wakeFds[0]; |
| 428 | if (fd >= 0) { |
| 429 | FD_SET(fd, &readfds); |
| 430 | if (maxfd < fd) { |
| 431 | maxfd = fd; |
| 432 | } |
| 433 | } else { |
| 434 | LOG(INFO) << "NOTE: entering select w/o wakepipe"; |
| 435 | } |
| 436 | |
| 437 | if (maxfd < 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 438 | VLOG(jdwp) << "+++ all fds are closed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 439 | return false; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Select blocks until it sees activity on the file descriptors. |
| 444 | * Closing the local file descriptor does not count as activity, |
| 445 | * so we can't rely on that to wake us up (it works for read() |
| 446 | * and accept(), but not select()). |
| 447 | * |
| 448 | * We can do one of three things: (1) send a signal and catch |
| 449 | * EINTR, (2) open an additional fd ("wakePipe") and write to |
| 450 | * it when it's time to exit, or (3) time out periodically and |
| 451 | * re-issue the select. We're currently using #2, as it's more |
| 452 | * reliable than #1 and generally better than #3. Wastes two fds. |
| 453 | */ |
| 454 | selCount = select(maxfd+1, &readfds, NULL, NULL, NULL); |
| 455 | if (selCount < 0) { |
| 456 | if (errno == EINTR) { |
| 457 | continue; |
| 458 | } |
| 459 | PLOG(ERROR) << "select failed"; |
| 460 | goto fail; |
| 461 | } |
| 462 | |
| 463 | if (netState->wakeFds[0] >= 0 && FD_ISSET(netState->wakeFds[0], &readfds)) { |
| 464 | LOG(DEBUG) << "Got wake-up signal, bailing out of select"; |
| 465 | goto fail; |
| 466 | } |
| 467 | if (netState->controlSock >= 0 && FD_ISSET(netState->controlSock, &readfds)) { |
| 468 | int sock = receiveClientFd(netState); |
| 469 | if (sock >= 0) { |
| 470 | LOG(INFO) << "Ignoring second debugger -- accepting and dropping"; |
| 471 | close(sock); |
| 472 | } else { |
| 473 | CHECK_LT(netState->controlSock, 0); |
| 474 | /* |
| 475 | * Remote side most likely went away, so our next read |
| 476 | * on netState->clientSock will fail and throw us out |
| 477 | * of the loop. |
| 478 | */ |
| 479 | } |
| 480 | } |
| 481 | if (netState->clientSock >= 0 && FD_ISSET(netState->clientSock, &readfds)) { |
| 482 | readCount = read(netState->clientSock, netState->inputBuffer + netState->inputCount, sizeof(netState->inputBuffer) - netState->inputCount); |
| 483 | if (readCount < 0) { |
| 484 | /* read failed */ |
| 485 | if (errno != EINTR) { |
| 486 | goto fail; |
| 487 | } |
| 488 | LOG(DEBUG) << "+++ EINTR hit"; |
| 489 | return true; |
| 490 | } else if (readCount == 0) { |
| 491 | /* EOF hit -- far end went away */ |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 492 | VLOG(jdwp) << "+++ peer disconnected"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 493 | goto fail; |
| 494 | } else { |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | netState->inputCount += readCount; |
| 501 | if (!haveFullPacket(netState)) { |
| 502 | return true; /* still not there yet */ |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Special-case the initial handshake. For some bizarre reason we're |
| 508 | * expected to emulate bad tty settings by echoing the request back |
| 509 | * exactly as it was sent. Note the handshake is always initiated by |
| 510 | * the debugger, no matter who connects to whom. |
| 511 | * |
| 512 | * Other than this one case, the protocol [claims to be] stateless. |
| 513 | */ |
| 514 | if (netState->awaitingHandshake) { |
| 515 | int cc; |
| 516 | |
| 517 | if (memcmp(netState->inputBuffer, kMagicHandshake, kMagicHandshakeLen) != 0) { |
| 518 | LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", netState->inputBuffer); |
| 519 | goto fail; |
| 520 | } |
| 521 | |
| 522 | errno = 0; |
Elliott Hughes | 068193c | 2013-04-15 16:05:28 -0700 | [diff] [blame^] | 523 | cc = TEMP_FAILURE_RETRY(write(netState->clientSock, netState->inputBuffer, kMagicHandshakeLen)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 524 | if (cc != kMagicHandshakeLen) { |
| 525 | PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")"; |
| 526 | goto fail; |
| 527 | } |
| 528 | |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 529 | netState->ConsumeBytes(kMagicHandshakeLen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 530 | netState->awaitingHandshake = false; |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 531 | VLOG(jdwp) << "+++ handshake complete"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 532 | return true; |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * Handle this packet. |
| 537 | */ |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 538 | return state->HandlePacket(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 539 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 540 | fail: |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 541 | closeConnection(state); |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Send a request. |
| 547 | * |
| 548 | * The entire packet must be sent with a single write() call to avoid |
| 549 | * threading issues. |
| 550 | * |
| 551 | * Returns "true" if it was sent successfully. |
| 552 | */ |
| 553 | static bool sendRequest(JdwpState* state, ExpandBuf* pReq) { |
| 554 | JdwpNetState* netState = state->netState; |
| 555 | |
| 556 | if (netState->clientSock < 0) { |
| 557 | /* can happen with some DDMS events */ |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 558 | VLOG(jdwp) << "NOT sending request -- no debugger is attached"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 559 | return false; |
| 560 | } |
| 561 | |
| 562 | errno = 0; |
| 563 | |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 564 | ssize_t cc = netState->WritePacket(pReq); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 565 | |
| 566 | if (cc != (ssize_t) expandBufGetLength(pReq)) { |
| 567 | PLOG(ERROR) << "Failed sending req to debugger (" << cc << " of " << expandBufGetLength(pReq) << ")"; |
| 568 | return false; |
| 569 | } |
| 570 | |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * Send a request that was split into multiple buffers. |
| 576 | * |
| 577 | * The entire packet must be sent with a single writev() call to avoid |
| 578 | * threading issues. |
| 579 | * |
| 580 | * Returns "true" if it was sent successfully. |
| 581 | */ |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 582 | static bool sendBufferedRequest(JdwpState* state, const iovec* iov, int iov_count) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 583 | JdwpNetState* netState = state->netState; |
| 584 | |
| 585 | if (netState->clientSock < 0) { |
| 586 | /* can happen with some DDMS events */ |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 587 | VLOG(jdwp) << "NOT sending request -- no debugger is attached"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 588 | return false; |
| 589 | } |
| 590 | |
| 591 | size_t expected = 0; |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 592 | for (int i = 0; i < iov_count; i++) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 593 | expected += iov[i].iov_len; |
| 594 | } |
| 595 | |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 596 | ssize_t actual = netState->WriteBufferedPacket(iov, iov_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 597 | if ((size_t)actual != expected) { |
| 598 | PLOG(ERROR) << "Failed sending b-req to debugger (" << actual << " of " << expected << ")"; |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | return true; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * Our functions. |
| 607 | */ |
| 608 | static const JdwpTransport adbTransport = { |
| 609 | startup, |
| 610 | acceptConnection, |
| 611 | establishConnection, |
| 612 | closeConnection, |
| 613 | netShutdown, |
| 614 | netFree, |
| 615 | isConnected, |
| 616 | awaitingHandshake, |
| 617 | processIncoming, |
| 618 | sendRequest, |
| 619 | sendBufferedRequest |
| 620 | }; |
| 621 | |
| 622 | /* |
| 623 | * Return our set. |
| 624 | */ |
| 625 | const JdwpTransport* AndroidAdbTransport() { |
| 626 | return &adbTransport; |
| 627 | } |
| 628 | |
| 629 | } // namespace JDWP |
| 630 | |
| 631 | } // namespace art |