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 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
| 24 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 25 | #include "base/logging.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 26 | #include "jdwp/jdwp_priv.h" |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 27 | #include "thread-inl.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 28 | |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 29 | #ifdef ART_TARGET_ANDROID |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 30 | #include "cutils/sockets.h" |
| 31 | #endif |
| 32 | |
| 33 | /* |
| 34 | * The JDWP <-> ADB transport protocol is explained in detail |
| 35 | * in system/core/adb/jdwp_service.c. Here's a summary. |
| 36 | * |
| 37 | * 1/ when the JDWP thread starts, it tries to connect to a Unix |
| 38 | * domain stream socket (@jdwp-control) that is opened by the |
| 39 | * ADB daemon. |
| 40 | * |
| 41 | * 2/ it then sends the current process PID as a string of 4 hexadecimal |
| 42 | * chars (no terminating zero) |
| 43 | * |
| 44 | * 3/ then, it uses recvmsg to receive file descriptors from the |
| 45 | * daemon. each incoming file descriptor is a pass-through to |
| 46 | * a given JDWP debugger, that can be used to read the usual |
| 47 | * JDWP-handshake, etc... |
| 48 | */ |
| 49 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 50 | #define kJdwpControlName "\0jdwp-control" |
| 51 | #define kJdwpControlNameLen (sizeof(kJdwpControlName)-1) |
| 52 | |
| 53 | namespace art { |
| 54 | |
| 55 | namespace JDWP { |
| 56 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 57 | using android::base::StringPrintf; |
| 58 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 59 | struct JdwpAdbState : public JdwpNetStateBase { |
| 60 | public: |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 61 | explicit JdwpAdbState(JdwpState* state) |
| 62 | : JdwpNetStateBase(state), |
| 63 | state_lock_("JdwpAdbState lock", kJdwpAdbStateLock) { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 64 | control_sock_ = -1; |
| 65 | shutting_down_ = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 66 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 67 | control_addr_.controlAddrUn.sun_family = AF_UNIX; |
| 68 | control_addr_len_ = sizeof(control_addr_.controlAddrUn.sun_family) + kJdwpControlNameLen; |
| 69 | memcpy(control_addr_.controlAddrUn.sun_path, kJdwpControlName, kJdwpControlNameLen); |
| 70 | } |
| 71 | |
| 72 | ~JdwpAdbState() { |
| 73 | if (clientSock != -1) { |
| 74 | shutdown(clientSock, SHUT_RDWR); |
| 75 | close(clientSock); |
| 76 | } |
| 77 | if (control_sock_ != -1) { |
| 78 | shutdown(control_sock_, SHUT_RDWR); |
| 79 | close(control_sock_); |
| 80 | } |
| 81 | } |
| 82 | |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 83 | virtual bool Accept() REQUIRES(!state_lock_); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 84 | |
| 85 | virtual bool Establish(const JdwpOptions*) { |
| 86 | return false; |
| 87 | } |
| 88 | |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 89 | virtual void Shutdown() REQUIRES(!state_lock_) { |
| 90 | int control_sock; |
| 91 | int local_clientSock; |
| 92 | { |
| 93 | MutexLock mu(Thread::Current(), state_lock_); |
| 94 | shutting_down_ = true; |
| 95 | control_sock = this->control_sock_; |
| 96 | local_clientSock = this->clientSock; |
| 97 | /* clear these out so it doesn't wake up and try to reuse them */ |
| 98 | this->control_sock_ = this->clientSock = -1; |
| 99 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 100 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 101 | if (local_clientSock != -1) { |
| 102 | shutdown(local_clientSock, SHUT_RDWR); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if (control_sock != -1) { |
| 106 | shutdown(control_sock, SHUT_RDWR); |
| 107 | } |
| 108 | |
| 109 | WakePipe(); |
| 110 | } |
| 111 | |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 112 | virtual bool ProcessIncoming() REQUIRES(!state_lock_); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 113 | |
| 114 | private: |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 115 | int ReceiveClientFd() REQUIRES(!state_lock_); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 116 | |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 117 | bool IsDown() REQUIRES(!state_lock_) { |
| 118 | MutexLock mu(Thread::Current(), state_lock_); |
| 119 | return shutting_down_; |
| 120 | } |
| 121 | |
| 122 | int ControlSock() REQUIRES(!state_lock_) { |
| 123 | MutexLock mu(Thread::Current(), state_lock_); |
| 124 | if (shutting_down_) { |
| 125 | CHECK_EQ(control_sock_, -1); |
| 126 | } |
| 127 | return control_sock_; |
| 128 | } |
| 129 | |
| 130 | int control_sock_ GUARDED_BY(state_lock_); |
| 131 | bool shutting_down_ GUARDED_BY(state_lock_); |
| 132 | Mutex state_lock_; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 133 | |
| 134 | socklen_t control_addr_len_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 135 | union { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 136 | sockaddr_un controlAddrUn; |
| 137 | sockaddr controlAddrPlain; |
| 138 | } control_addr_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 141 | /* |
| 142 | * Do initial prep work, e.g. binding to ports and opening files. This |
| 143 | * runs in the main thread, before the JDWP thread starts, so it shouldn't |
| 144 | * do anything that might block forever. |
| 145 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 146 | bool InitAdbTransport(JdwpState* state, const JdwpOptions*) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 147 | VLOG(jdwp) << "ADB transport startup"; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 148 | state->netState = new JdwpAdbState(state); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 149 | return (state->netState != nullptr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Receive a file descriptor from ADB. The fd can be used to communicate |
| 154 | * directly with a debugger or DDMS. |
| 155 | * |
| 156 | * Returns the file descriptor on success. On failure, returns -1 and |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 157 | * closes netState->control_sock_. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 158 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 159 | int JdwpAdbState::ReceiveClientFd() { |
| 160 | char dummy = '!'; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 161 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 162 | cmsghdr cm; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 163 | char buffer[CMSG_SPACE(sizeof(int))]; |
| 164 | } cm_un; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 165 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 166 | iovec iov; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 167 | iov.iov_base = &dummy; |
| 168 | iov.iov_len = 1; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 169 | |
| 170 | msghdr msg; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 171 | msg.msg_name = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 172 | msg.msg_namelen = 0; |
| 173 | msg.msg_iov = &iov; |
| 174 | msg.msg_iovlen = 1; |
| 175 | msg.msg_flags = 0; |
| 176 | msg.msg_control = cm_un.buffer; |
| 177 | msg.msg_controllen = sizeof(cm_un.buffer); |
| 178 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 179 | cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 180 | cmsg->cmsg_len = msg.msg_controllen; |
| 181 | cmsg->cmsg_level = SOL_SOCKET; |
| 182 | cmsg->cmsg_type = SCM_RIGHTS; |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 183 | (reinterpret_cast<int*>(CMSG_DATA(cmsg)))[0] = -1; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 184 | |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 185 | int rc = TEMP_FAILURE_RETRY(recvmsg(ControlSock(), &msg, 0)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 186 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 187 | if (rc <= 0) { |
| 188 | if (rc == -1) { |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 189 | PLOG(WARNING) << "Receiving file descriptor from ADB failed (socket " << ControlSock() << ")"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 190 | } |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 191 | MutexLock mu(Thread::Current(), state_lock_); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 192 | close(control_sock_); |
| 193 | control_sock_ = -1; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 197 | return (reinterpret_cast<int*>(CMSG_DATA(cmsg)))[0]; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Block forever, waiting for a debugger to connect to us. Called from the |
| 202 | * JDWP thread. |
| 203 | * |
| 204 | * This needs to un-block and return "false" if the VM is shutting down. It |
| 205 | * should return "true" when it successfully accepts a connection. |
| 206 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 207 | bool JdwpAdbState::Accept() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 208 | int retryCount = 0; |
| 209 | |
| 210 | /* first, ensure that we get a connection to the ADB daemon */ |
| 211 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 212 | retry: |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 213 | if (IsDown()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 214 | return false; |
| 215 | } |
| 216 | |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 217 | if (ControlSock() == -1) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 218 | int sleep_ms = 500; |
| 219 | const int sleep_max_ms = 2*1000; |
| 220 | char buff[5]; |
| 221 | |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 222 | int sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 223 | if (sock < 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 224 | PLOG(ERROR) << "Could not create ADB control socket"; |
| 225 | return false; |
| 226 | } |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 227 | { |
| 228 | MutexLock mu(Thread::Current(), state_lock_); |
| 229 | control_sock_ = sock; |
| 230 | if (shutting_down_) { |
| 231 | return false; |
| 232 | } |
| 233 | if (!MakePipe()) { |
| 234 | return false; |
| 235 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | snprintf(buff, sizeof(buff), "%04x", getpid()); |
| 239 | buff[4] = 0; |
| 240 | |
| 241 | for (;;) { |
| 242 | /* |
| 243 | * If adbd isn't running, because USB debugging was disabled or |
| 244 | * perhaps the system is restarting it for "adb root", the |
| 245 | * connect() will fail. We loop here forever waiting for it |
| 246 | * to come back. |
| 247 | * |
| 248 | * Waking up and polling every couple of seconds is generally a |
| 249 | * bad thing to do, but we only do this if the application is |
| 250 | * debuggable *and* adbd isn't running. Still, for the sake |
| 251 | * of battery life, we should consider timing out and giving |
| 252 | * up after a few minutes in case somebody ships an app with |
| 253 | * the debuggable flag set. |
| 254 | */ |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 255 | int ret = connect(ControlSock(), &control_addr_.controlAddrPlain, control_addr_len_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 256 | if (!ret) { |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 257 | int control_sock = ControlSock(); |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 258 | #ifdef ART_TARGET_ANDROID |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 259 | if (control_sock < 0 || !socket_peer_is_trusted(control_sock)) { |
| 260 | if (control_sock >= 0 && shutdown(control_sock, SHUT_RDWR)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 261 | PLOG(ERROR) << "trouble shutting down socket"; |
| 262 | } |
| 263 | return false; |
| 264 | } |
| 265 | #endif |
| 266 | |
| 267 | /* now try to send our pid to the ADB daemon */ |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 268 | ret = TEMP_FAILURE_RETRY(send(control_sock, buff, 4, 0)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 269 | if (ret >= 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 270 | VLOG(jdwp) << StringPrintf("PID sent as '%.*s' to ADB", 4, buff); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 271 | break; |
| 272 | } |
| 273 | |
| 274 | PLOG(ERROR) << "Weird, can't send JDWP process pid to ADB"; |
| 275 | return false; |
| 276 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 277 | if (VLOG_IS_ON(jdwp)) { |
| 278 | PLOG(ERROR) << "Can't connect to ADB control socket"; |
| 279 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 280 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 281 | usleep(sleep_ms * 1000); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 282 | |
| 283 | sleep_ms += (sleep_ms >> 1); |
| 284 | if (sleep_ms > sleep_max_ms) { |
| 285 | sleep_ms = sleep_max_ms; |
| 286 | } |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 287 | if (IsDown()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 293 | VLOG(jdwp) << "trying to receive file descriptor from ADB"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 294 | /* now we can receive a client file descriptor */ |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 295 | int sock = ReceiveClientFd(); |
| 296 | { |
| 297 | MutexLock mu(Thread::Current(), state_lock_); |
| 298 | clientSock = sock; |
| 299 | if (shutting_down_) { |
| 300 | return false; // suppress logs and additional activity |
| 301 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 302 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 303 | if (clientSock == -1) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 304 | if (++retryCount > 5) { |
| 305 | LOG(ERROR) << "adb connection max retries exceeded"; |
| 306 | return false; |
| 307 | } |
| 308 | goto retry; |
| 309 | } else { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 310 | VLOG(jdwp) << "received file descriptor " << clientSock << " from ADB"; |
| 311 | SetAwaitingHandshake(true); |
| 312 | input_count_ = 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 313 | return true; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 318 | * Process incoming data. If no data is available, this will block until |
| 319 | * some arrives. |
| 320 | * |
| 321 | * If we get a full packet, handle it. |
| 322 | * |
| 323 | * To take some of the mystery out of life, we want to reject incoming |
| 324 | * connections if we already have a debugger attached. If we don't, the |
| 325 | * debugger will just mysteriously hang until it times out. We could just |
| 326 | * close the listen socket, but there's a good chance we won't be able to |
| 327 | * bind to the same port again, which would confuse utilities. |
| 328 | * |
| 329 | * Returns "false" on error (indicating that the connection has been severed), |
| 330 | * "true" if things are still okay. |
| 331 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 332 | bool JdwpAdbState::ProcessIncoming() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 333 | int readCount; |
| 334 | |
Brian Carlstrom | 4274889 | 2013-07-18 18:04:08 -0700 | [diff] [blame] | 335 | CHECK_NE(clientSock, -1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 336 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 337 | if (!HaveFullPacket()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 338 | /* read some more, looping until we have data */ |
| 339 | errno = 0; |
| 340 | while (1) { |
| 341 | int selCount; |
| 342 | fd_set readfds; |
| 343 | int maxfd = -1; |
| 344 | int fd; |
| 345 | |
| 346 | FD_ZERO(&readfds); |
| 347 | |
| 348 | /* configure fds; note these may get zapped by another thread */ |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 349 | fd = ControlSock(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 350 | if (fd >= 0) { |
| 351 | FD_SET(fd, &readfds); |
| 352 | if (maxfd < fd) { |
| 353 | maxfd = fd; |
| 354 | } |
| 355 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 356 | fd = clientSock; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 357 | if (fd >= 0) { |
| 358 | FD_SET(fd, &readfds); |
| 359 | if (maxfd < fd) { |
| 360 | maxfd = fd; |
| 361 | } |
| 362 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 363 | fd = wake_pipe_[0]; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 364 | if (fd >= 0) { |
| 365 | FD_SET(fd, &readfds); |
| 366 | if (maxfd < fd) { |
| 367 | maxfd = fd; |
| 368 | } |
| 369 | } else { |
| 370 | LOG(INFO) << "NOTE: entering select w/o wakepipe"; |
| 371 | } |
| 372 | |
| 373 | if (maxfd < 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 374 | VLOG(jdwp) << "+++ all fds are closed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 375 | return false; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Select blocks until it sees activity on the file descriptors. |
| 380 | * Closing the local file descriptor does not count as activity, |
| 381 | * so we can't rely on that to wake us up (it works for read() |
| 382 | * and accept(), but not select()). |
| 383 | * |
| 384 | * We can do one of three things: (1) send a signal and catch |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 385 | * EINTR, (2) open an additional fd ("wake pipe") and write to |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 386 | * it when it's time to exit, or (3) time out periodically and |
| 387 | * re-issue the select. We're currently using #2, as it's more |
| 388 | * reliable than #1 and generally better than #3. Wastes two fds. |
| 389 | */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 390 | selCount = select(maxfd + 1, &readfds, nullptr, nullptr, nullptr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 391 | if (selCount < 0) { |
| 392 | if (errno == EINTR) { |
| 393 | continue; |
| 394 | } |
| 395 | PLOG(ERROR) << "select failed"; |
| 396 | goto fail; |
| 397 | } |
| 398 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 399 | if (wake_pipe_[0] >= 0 && FD_ISSET(wake_pipe_[0], &readfds)) { |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 400 | VLOG(jdwp) << "Got wake-up signal, bailing out of select"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 401 | goto fail; |
| 402 | } |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 403 | int control_sock = ControlSock(); |
| 404 | if (control_sock >= 0 && FD_ISSET(control_sock, &readfds)) { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 405 | int sock = ReceiveClientFd(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 406 | if (sock >= 0) { |
| 407 | LOG(INFO) << "Ignoring second debugger -- accepting and dropping"; |
| 408 | close(sock); |
| 409 | } else { |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 410 | CHECK_EQ(ControlSock(), -1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 411 | /* |
| 412 | * Remote side most likely went away, so our next read |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 413 | * on clientSock will fail and throw us out of the loop. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 414 | */ |
| 415 | } |
| 416 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 417 | if (clientSock >= 0 && FD_ISSET(clientSock, &readfds)) { |
| 418 | readCount = read(clientSock, input_buffer_ + input_count_, sizeof(input_buffer_) - input_count_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 419 | if (readCount < 0) { |
| 420 | /* read failed */ |
| 421 | if (errno != EINTR) { |
| 422 | goto fail; |
| 423 | } |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 424 | VLOG(jdwp) << "+++ EINTR hit"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 425 | return true; |
| 426 | } else if (readCount == 0) { |
| 427 | /* EOF hit -- far end went away */ |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 428 | VLOG(jdwp) << "+++ peer disconnected"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 429 | goto fail; |
| 430 | } else { |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 436 | input_count_ += readCount; |
| 437 | if (!HaveFullPacket()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 438 | return true; /* still not there yet */ |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Special-case the initial handshake. For some bizarre reason we're |
| 444 | * expected to emulate bad tty settings by echoing the request back |
| 445 | * exactly as it was sent. Note the handshake is always initiated by |
| 446 | * the debugger, no matter who connects to whom. |
| 447 | * |
| 448 | * Other than this one case, the protocol [claims to be] stateless. |
| 449 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 450 | if (IsAwaitingHandshake()) { |
| 451 | if (memcmp(input_buffer_, kMagicHandshake, kMagicHandshakeLen) != 0) { |
| 452 | LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", input_buffer_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 453 | goto fail; |
| 454 | } |
| 455 | |
| 456 | errno = 0; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 457 | int cc = TEMP_FAILURE_RETRY(write(clientSock, input_buffer_, kMagicHandshakeLen)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 458 | if (cc != kMagicHandshakeLen) { |
| 459 | PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")"; |
| 460 | goto fail; |
| 461 | } |
| 462 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 463 | ConsumeBytes(kMagicHandshakeLen); |
| 464 | SetAwaitingHandshake(false); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 465 | VLOG(jdwp) << "+++ handshake complete"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 466 | return true; |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Handle this packet. |
| 471 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 472 | return state_->HandlePacket(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 473 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 474 | fail: |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 475 | Close(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 476 | return false; |
| 477 | } |
| 478 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 479 | } // namespace JDWP |
| 480 | |
| 481 | } // namespace art |