blob: d64f11f4f8de8274977bdc6c71d3e527d1530528 [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
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 Hughes872d4ec2011-10-21 17:07:15 -070017#include <errno.h>
18#include <stdio.h>
19#include <sys/socket.h>
20#include <sys/un.h>
21#include <unistd.h>
22
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023#include "android-base/stringprintf.h"
24
Andreas Gampe57943812017-12-06 21:39:13 -080025#include "base/logging.h" // For VLOG.
Elliott Hughesc2efd4d2018-10-25 13:14:55 -070026#include "base/socket_peer_is_trusted.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "jdwp/jdwp_priv.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070028#include "thread-current-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080029
Elliott Hughes872d4ec2011-10-21 17:07:15 -070030/*
31 * The JDWP <-> ADB transport protocol is explained in detail
32 * in system/core/adb/jdwp_service.c. Here's a summary.
33 *
34 * 1/ when the JDWP thread starts, it tries to connect to a Unix
35 * domain stream socket (@jdwp-control) that is opened by the
36 * ADB daemon.
37 *
Josh Gao4b49bb72018-02-12 15:06:42 -080038 * 2/ it then sends the current process PID as an int32_t.
Elliott Hughes872d4ec2011-10-21 17:07:15 -070039 *
40 * 3/ then, it uses recvmsg to receive file descriptors from the
41 * daemon. each incoming file descriptor is a pass-through to
42 * a given JDWP debugger, that can be used to read the usual
43 * JDWP-handshake, etc...
44 */
45
Tao Wu48fe7942017-01-08 01:20:21 -080046static constexpr char kJdwpControlName[] = "\0jdwp-control";
47static constexpr size_t kJdwpControlNameLen = sizeof(kJdwpControlName) - 1;
48/* This timeout is for connect/send with control socket. In practice, the
49 * connect should never timeout since it's just connect to a local unix domain
50 * socket. But in case adb is buggy and doesn't respond to any connection, the
51 * connect will block. For send, actually it would never block since we only send
52 * several bytes and the kernel buffer is big enough to accept it. 10 seconds
53 * should be far enough.
54 */
55static constexpr int kControlSockSendTimeout = 10;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070056
57namespace art {
58
59namespace JDWP {
60
Andreas Gampe46ee31b2016-12-14 10:11:49 -080061using android::base::StringPrintf;
62
Elliott Hughes5d10a872013-04-17 19:26:43 -070063struct JdwpAdbState : public JdwpNetStateBase {
64 public:
Tao Wud0a160d2016-12-13 18:32:17 -080065 explicit JdwpAdbState(JdwpState* state)
66 : JdwpNetStateBase(state),
67 state_lock_("JdwpAdbState lock", kJdwpAdbStateLock) {
Elliott Hughes5d10a872013-04-17 19:26:43 -070068 control_sock_ = -1;
69 shutting_down_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070070
Elliott Hughes5d10a872013-04-17 19:26:43 -070071 control_addr_.controlAddrUn.sun_family = AF_UNIX;
72 control_addr_len_ = sizeof(control_addr_.controlAddrUn.sun_family) + kJdwpControlNameLen;
73 memcpy(control_addr_.controlAddrUn.sun_path, kJdwpControlName, kJdwpControlNameLen);
74 }
75
76 ~JdwpAdbState() {
77 if (clientSock != -1) {
78 shutdown(clientSock, SHUT_RDWR);
79 close(clientSock);
80 }
81 if (control_sock_ != -1) {
82 shutdown(control_sock_, SHUT_RDWR);
83 close(control_sock_);
84 }
85 }
86
Andreas Gampefa6a1b02018-09-07 08:11:55 -070087 bool Accept() override REQUIRES(!state_lock_);
Elliott Hughes5d10a872013-04-17 19:26:43 -070088
Andreas Gampefa6a1b02018-09-07 08:11:55 -070089 bool Establish(const JdwpOptions*) override {
Elliott Hughes5d10a872013-04-17 19:26:43 -070090 return false;
91 }
92
Andreas Gampefa6a1b02018-09-07 08:11:55 -070093 void Shutdown() override REQUIRES(!state_lock_) {
Tao Wud0a160d2016-12-13 18:32:17 -080094 int control_sock;
95 int local_clientSock;
96 {
97 MutexLock mu(Thread::Current(), state_lock_);
98 shutting_down_ = true;
99 control_sock = this->control_sock_;
100 local_clientSock = this->clientSock;
101 /* clear these out so it doesn't wake up and try to reuse them */
102 this->control_sock_ = this->clientSock = -1;
103 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700104
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800105 if (local_clientSock != -1) {
106 shutdown(local_clientSock, SHUT_RDWR);
Elliott Hughes5d10a872013-04-17 19:26:43 -0700107 }
108
109 if (control_sock != -1) {
110 shutdown(control_sock, SHUT_RDWR);
111 }
112
113 WakePipe();
114 }
115
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700116 bool ProcessIncoming() override REQUIRES(!state_lock_);
Elliott Hughes5d10a872013-04-17 19:26:43 -0700117
118 private:
Tao Wud0a160d2016-12-13 18:32:17 -0800119 int ReceiveClientFd() REQUIRES(!state_lock_);
Elliott Hughes5d10a872013-04-17 19:26:43 -0700120
Tao Wud0a160d2016-12-13 18:32:17 -0800121 bool IsDown() REQUIRES(!state_lock_) {
122 MutexLock mu(Thread::Current(), state_lock_);
123 return shutting_down_;
124 }
125
126 int ControlSock() REQUIRES(!state_lock_) {
127 MutexLock mu(Thread::Current(), state_lock_);
128 if (shutting_down_) {
129 CHECK_EQ(control_sock_, -1);
130 }
131 return control_sock_;
132 }
133
134 int control_sock_ GUARDED_BY(state_lock_);
135 bool shutting_down_ GUARDED_BY(state_lock_);
136 Mutex state_lock_;
Elliott Hughes5d10a872013-04-17 19:26:43 -0700137
138 socklen_t control_addr_len_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700139 union {
Elliott Hughes5d10a872013-04-17 19:26:43 -0700140 sockaddr_un controlAddrUn;
141 sockaddr controlAddrPlain;
142 } control_addr_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700143};
144
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700145/*
146 * Do initial prep work, e.g. binding to ports and opening files. This
147 * runs in the main thread, before the JDWP thread starts, so it shouldn't
148 * do anything that might block forever.
149 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700150bool InitAdbTransport(JdwpState* state, const JdwpOptions*) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800151 VLOG(jdwp) << "ADB transport startup";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700152 state->netState = new JdwpAdbState(state);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200153 return (state->netState != nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700154}
155
156/*
157 * Receive a file descriptor from ADB. The fd can be used to communicate
158 * directly with a debugger or DDMS.
159 *
160 * Returns the file descriptor on success. On failure, returns -1 and
Elliott Hughes5d10a872013-04-17 19:26:43 -0700161 * closes netState->control_sock_.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700162 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700163int JdwpAdbState::ReceiveClientFd() {
164 char dummy = '!';
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700165 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700166 cmsghdr cm;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700167 char buffer[CMSG_SPACE(sizeof(int))];
168 } cm_un;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700169
Elliott Hughes5d10a872013-04-17 19:26:43 -0700170 iovec iov;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700171 iov.iov_base = &dummy;
172 iov.iov_len = 1;
Elliott Hughes5d10a872013-04-17 19:26:43 -0700173
174 msghdr msg;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200175 msg.msg_name = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700176 msg.msg_namelen = 0;
177 msg.msg_iov = &iov;
178 msg.msg_iovlen = 1;
179 msg.msg_flags = 0;
180 msg.msg_control = cm_un.buffer;
181 msg.msg_controllen = sizeof(cm_un.buffer);
182
Elliott Hughes5d10a872013-04-17 19:26:43 -0700183 cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700184 cmsg->cmsg_len = msg.msg_controllen;
185 cmsg->cmsg_level = SOL_SOCKET;
186 cmsg->cmsg_type = SCM_RIGHTS;
Brian Carlstrom2d888622013-07-18 17:02:00 -0700187 (reinterpret_cast<int*>(CMSG_DATA(cmsg)))[0] = -1;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700188
Tao Wud0a160d2016-12-13 18:32:17 -0800189 int rc = TEMP_FAILURE_RETRY(recvmsg(ControlSock(), &msg, 0));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700190
Elliott Hughes5d10a872013-04-17 19:26:43 -0700191 if (rc <= 0) {
192 if (rc == -1) {
Tao Wud0a160d2016-12-13 18:32:17 -0800193 PLOG(WARNING) << "Receiving file descriptor from ADB failed (socket " << ControlSock() << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700194 }
Tao Wud0a160d2016-12-13 18:32:17 -0800195 MutexLock mu(Thread::Current(), state_lock_);
Elliott Hughes5d10a872013-04-17 19:26:43 -0700196 close(control_sock_);
197 control_sock_ = -1;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700198 return -1;
199 }
200
Brian Carlstrom2d888622013-07-18 17:02:00 -0700201 return (reinterpret_cast<int*>(CMSG_DATA(cmsg)))[0];
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202}
203
204/*
205 * Block forever, waiting for a debugger to connect to us. Called from the
206 * JDWP thread.
207 *
208 * This needs to un-block and return "false" if the VM is shutting down. It
209 * should return "true" when it successfully accepts a connection.
210 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700211bool JdwpAdbState::Accept() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700212 int retryCount = 0;
213
214 /* first, ensure that we get a connection to the ADB daemon */
215
Elliott Hughesa21039c2012-06-21 12:09:25 -0700216 retry:
Tao Wud0a160d2016-12-13 18:32:17 -0800217 if (IsDown()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700218 return false;
219 }
220
Tao Wud0a160d2016-12-13 18:32:17 -0800221 if (ControlSock() == -1) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700222 int sleep_ms = 500;
223 const int sleep_max_ms = 2*1000;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700224
Josh Gaocbb65ae2017-03-20 11:33:34 -0700225 int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
Tao Wud0a160d2016-12-13 18:32:17 -0800226 if (sock < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700227 PLOG(ERROR) << "Could not create ADB control socket";
228 return false;
229 }
Tao Wu48fe7942017-01-08 01:20:21 -0800230 struct timeval timeout;
231 timeout.tv_sec = kControlSockSendTimeout;
232 timeout.tv_usec = 0;
233 setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
Tao Wud0a160d2016-12-13 18:32:17 -0800234 {
235 MutexLock mu(Thread::Current(), state_lock_);
236 control_sock_ = sock;
237 if (shutting_down_) {
238 return false;
239 }
240 if (!MakePipe()) {
241 return false;
242 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700243 }
244
Josh Gao4b49bb72018-02-12 15:06:42 -0800245 int32_t pid = getpid();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700246
247 for (;;) {
248 /*
249 * If adbd isn't running, because USB debugging was disabled or
250 * perhaps the system is restarting it for "adb root", the
251 * connect() will fail. We loop here forever waiting for it
252 * to come back.
253 *
254 * Waking up and polling every couple of seconds is generally a
255 * bad thing to do, but we only do this if the application is
256 * debuggable *and* adbd isn't running. Still, for the sake
257 * of battery life, we should consider timing out and giving
258 * up after a few minutes in case somebody ships an app with
259 * the debuggable flag set.
260 */
Josh Gaocbb65ae2017-03-20 11:33:34 -0700261 int ret = connect(ControlSock(), &control_addr_.controlAddrPlain, control_addr_len_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700262 if (!ret) {
Tao Wud0a160d2016-12-13 18:32:17 -0800263 int control_sock = ControlSock();
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100264#ifdef ART_TARGET_ANDROID
Elliott Hughesc2efd4d2018-10-25 13:14:55 -0700265 if (control_sock < 0 || !art::SocketPeerIsTrusted(control_sock)) {
Tao Wud0a160d2016-12-13 18:32:17 -0800266 if (control_sock >= 0 && shutdown(control_sock, SHUT_RDWR)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700267 PLOG(ERROR) << "trouble shutting down socket";
268 }
269 return false;
270 }
271#endif
272
273 /* now try to send our pid to the ADB daemon */
Josh Gao4b49bb72018-02-12 15:06:42 -0800274 ret = TEMP_FAILURE_RETRY(send(control_sock, &pid, sizeof(pid), 0));
275 if (ret == sizeof(pid)) {
276 VLOG(jdwp) << "PID " << pid << " sent to ADB";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700277 break;
278 }
279
280 PLOG(ERROR) << "Weird, can't send JDWP process pid to ADB";
281 return false;
282 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800283 if (VLOG_IS_ON(jdwp)) {
284 PLOG(ERROR) << "Can't connect to ADB control socket";
285 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700286
Elliott Hughesa21039c2012-06-21 12:09:25 -0700287 usleep(sleep_ms * 1000);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700288
289 sleep_ms += (sleep_ms >> 1);
290 if (sleep_ms > sleep_max_ms) {
291 sleep_ms = sleep_max_ms;
292 }
Tao Wud0a160d2016-12-13 18:32:17 -0800293 if (IsDown()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700294 return false;
295 }
296 }
297 }
298
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800299 VLOG(jdwp) << "trying to receive file descriptor from ADB";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700300 /* now we can receive a client file descriptor */
Tao Wud0a160d2016-12-13 18:32:17 -0800301 int sock = ReceiveClientFd();
302 {
303 MutexLock mu(Thread::Current(), state_lock_);
304 clientSock = sock;
305 if (shutting_down_) {
306 return false; // suppress logs and additional activity
307 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700308 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700309 if (clientSock == -1) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700310 if (++retryCount > 5) {
311 LOG(ERROR) << "adb connection max retries exceeded";
312 return false;
313 }
314 goto retry;
315 } else {
Elliott Hughes5d10a872013-04-17 19:26:43 -0700316 VLOG(jdwp) << "received file descriptor " << clientSock << " from ADB";
317 SetAwaitingHandshake(true);
318 input_count_ = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700319 return true;
320 }
321}
322
323/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700324 * Process incoming data. If no data is available, this will block until
325 * some arrives.
326 *
327 * If we get a full packet, handle it.
328 *
329 * To take some of the mystery out of life, we want to reject incoming
330 * connections if we already have a debugger attached. If we don't, the
331 * debugger will just mysteriously hang until it times out. We could just
332 * close the listen socket, but there's a good chance we won't be able to
333 * bind to the same port again, which would confuse utilities.
334 *
335 * Returns "false" on error (indicating that the connection has been severed),
336 * "true" if things are still okay.
337 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700338bool JdwpAdbState::ProcessIncoming() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700339 int readCount;
340
Brian Carlstrom42748892013-07-18 18:04:08 -0700341 CHECK_NE(clientSock, -1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700342
Elliott Hughes5d10a872013-04-17 19:26:43 -0700343 if (!HaveFullPacket()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700344 /* read some more, looping until we have data */
345 errno = 0;
Andreas Gampe8351aac2018-09-10 12:37:49 -0700346 while (true) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700347 int selCount;
348 fd_set readfds;
349 int maxfd = -1;
350 int fd;
351
352 FD_ZERO(&readfds);
353
354 /* configure fds; note these may get zapped by another thread */
Tao Wud0a160d2016-12-13 18:32:17 -0800355 fd = ControlSock();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700356 if (fd >= 0) {
357 FD_SET(fd, &readfds);
358 if (maxfd < fd) {
359 maxfd = fd;
360 }
361 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700362 fd = clientSock;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700363 if (fd >= 0) {
364 FD_SET(fd, &readfds);
365 if (maxfd < fd) {
366 maxfd = fd;
367 }
368 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700369 fd = wake_pipe_[0];
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700370 if (fd >= 0) {
371 FD_SET(fd, &readfds);
372 if (maxfd < fd) {
373 maxfd = fd;
374 }
375 } else {
376 LOG(INFO) << "NOTE: entering select w/o wakepipe";
377 }
378
379 if (maxfd < 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800380 VLOG(jdwp) << "+++ all fds are closed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700381 return false;
382 }
383
384 /*
385 * Select blocks until it sees activity on the file descriptors.
386 * Closing the local file descriptor does not count as activity,
387 * so we can't rely on that to wake us up (it works for read()
388 * and accept(), but not select()).
389 *
390 * We can do one of three things: (1) send a signal and catch
Elliott Hughes5d10a872013-04-17 19:26:43 -0700391 * EINTR, (2) open an additional fd ("wake pipe") and write to
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700392 * it when it's time to exit, or (3) time out periodically and
393 * re-issue the select. We're currently using #2, as it's more
394 * reliable than #1 and generally better than #3. Wastes two fds.
395 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200396 selCount = select(maxfd + 1, &readfds, nullptr, nullptr, nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700397 if (selCount < 0) {
398 if (errno == EINTR) {
399 continue;
400 }
401 PLOG(ERROR) << "select failed";
402 goto fail;
403 }
404
Elliott Hughes5d10a872013-04-17 19:26:43 -0700405 if (wake_pipe_[0] >= 0 && FD_ISSET(wake_pipe_[0], &readfds)) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700406 VLOG(jdwp) << "Got wake-up signal, bailing out of select";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700407 goto fail;
408 }
Tao Wud0a160d2016-12-13 18:32:17 -0800409 int control_sock = ControlSock();
410 if (control_sock >= 0 && FD_ISSET(control_sock, &readfds)) {
Elliott Hughes5d10a872013-04-17 19:26:43 -0700411 int sock = ReceiveClientFd();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700412 if (sock >= 0) {
413 LOG(INFO) << "Ignoring second debugger -- accepting and dropping";
414 close(sock);
415 } else {
Tao Wud0a160d2016-12-13 18:32:17 -0800416 CHECK_EQ(ControlSock(), -1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700417 /*
418 * Remote side most likely went away, so our next read
Elliott Hughes5d10a872013-04-17 19:26:43 -0700419 * on clientSock will fail and throw us out of the loop.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700420 */
421 }
422 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700423 if (clientSock >= 0 && FD_ISSET(clientSock, &readfds)) {
424 readCount = read(clientSock, input_buffer_ + input_count_, sizeof(input_buffer_) - input_count_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700425 if (readCount < 0) {
426 /* read failed */
427 if (errno != EINTR) {
428 goto fail;
429 }
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700430 VLOG(jdwp) << "+++ EINTR hit";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700431 return true;
432 } else if (readCount == 0) {
433 /* EOF hit -- far end went away */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800434 VLOG(jdwp) << "+++ peer disconnected";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700435 goto fail;
436 } else {
437 break;
438 }
439 }
440 }
441
Elliott Hughes5d10a872013-04-17 19:26:43 -0700442 input_count_ += readCount;
443 if (!HaveFullPacket()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700444 return true; /* still not there yet */
445 }
446 }
447
448 /*
449 * Special-case the initial handshake. For some bizarre reason we're
450 * expected to emulate bad tty settings by echoing the request back
451 * exactly as it was sent. Note the handshake is always initiated by
452 * the debugger, no matter who connects to whom.
453 *
454 * Other than this one case, the protocol [claims to be] stateless.
455 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700456 if (IsAwaitingHandshake()) {
457 if (memcmp(input_buffer_, kMagicHandshake, kMagicHandshakeLen) != 0) {
458 LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", input_buffer_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700459 goto fail;
460 }
461
462 errno = 0;
Elliott Hughes5d10a872013-04-17 19:26:43 -0700463 int cc = TEMP_FAILURE_RETRY(write(clientSock, input_buffer_, kMagicHandshakeLen));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700464 if (cc != kMagicHandshakeLen) {
465 PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")";
466 goto fail;
467 }
468
Elliott Hughes5d10a872013-04-17 19:26:43 -0700469 ConsumeBytes(kMagicHandshakeLen);
470 SetAwaitingHandshake(false);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800471 VLOG(jdwp) << "+++ handshake complete";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700472 return true;
473 }
474
475 /*
476 * Handle this packet.
477 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700478 return state_->HandlePacket();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700479
Elliott Hughesa21039c2012-06-21 12:09:25 -0700480 fail:
Elliott Hughes5d10a872013-04-17 19:26:43 -0700481 Close();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700482 return false;
483}
484
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700485} // namespace JDWP
486
487} // namespace art