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