blob: 481aff91f825d01006c519aff39562c6b6347a08 [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 Hughes07ed66b2012-12-12 18:34:25 -080026#include "jdwp/jdwp_priv.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070027#include "thread-current-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080028
Bilyan Borisovbb661c02016-04-04 16:27:32 +010029#ifdef ART_TARGET_ANDROID
Elliott Hughes872d4ec2011-10-21 17:07:15 -070030#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 *
Josh Gao4b49bb72018-02-12 15:06:42 -080041 * 2/ it then sends the current process PID as an int32_t.
Elliott Hughes872d4ec2011-10-21 17:07:15 -070042 *
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
Tao Wu48fe7942017-01-08 01:20:21 -080049static constexpr char kJdwpControlName[] = "\0jdwp-control";
50static constexpr size_t kJdwpControlNameLen = sizeof(kJdwpControlName) - 1;
51/* This timeout is for connect/send with control socket. In practice, the
52 * connect should never timeout since it's just connect to a local unix domain
53 * socket. But in case adb is buggy and doesn't respond to any connection, the
54 * connect will block. For send, actually it would never block since we only send
55 * several bytes and the kernel buffer is big enough to accept it. 10 seconds
56 * should be far enough.
57 */
58static constexpr int kControlSockSendTimeout = 10;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070059
60namespace art {
61
62namespace JDWP {
63
Andreas Gampe46ee31b2016-12-14 10:11:49 -080064using android::base::StringPrintf;
65
Elliott Hughes5d10a872013-04-17 19:26:43 -070066struct JdwpAdbState : public JdwpNetStateBase {
67 public:
Tao Wud0a160d2016-12-13 18:32:17 -080068 explicit JdwpAdbState(JdwpState* state)
69 : JdwpNetStateBase(state),
70 state_lock_("JdwpAdbState lock", kJdwpAdbStateLock) {
Elliott Hughes5d10a872013-04-17 19:26:43 -070071 control_sock_ = -1;
72 shutting_down_ = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070073
Elliott Hughes5d10a872013-04-17 19:26:43 -070074 control_addr_.controlAddrUn.sun_family = AF_UNIX;
75 control_addr_len_ = sizeof(control_addr_.controlAddrUn.sun_family) + kJdwpControlNameLen;
76 memcpy(control_addr_.controlAddrUn.sun_path, kJdwpControlName, kJdwpControlNameLen);
77 }
78
79 ~JdwpAdbState() {
80 if (clientSock != -1) {
81 shutdown(clientSock, SHUT_RDWR);
82 close(clientSock);
83 }
84 if (control_sock_ != -1) {
85 shutdown(control_sock_, SHUT_RDWR);
86 close(control_sock_);
87 }
88 }
89
Tao Wud0a160d2016-12-13 18:32:17 -080090 virtual bool Accept() REQUIRES(!state_lock_);
Elliott Hughes5d10a872013-04-17 19:26:43 -070091
92 virtual bool Establish(const JdwpOptions*) {
93 return false;
94 }
95
Tao Wud0a160d2016-12-13 18:32:17 -080096 virtual void Shutdown() REQUIRES(!state_lock_) {
97 int control_sock;
98 int local_clientSock;
99 {
100 MutexLock mu(Thread::Current(), state_lock_);
101 shutting_down_ = true;
102 control_sock = this->control_sock_;
103 local_clientSock = this->clientSock;
104 /* clear these out so it doesn't wake up and try to reuse them */
105 this->control_sock_ = this->clientSock = -1;
106 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700107
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800108 if (local_clientSock != -1) {
109 shutdown(local_clientSock, SHUT_RDWR);
Elliott Hughes5d10a872013-04-17 19:26:43 -0700110 }
111
112 if (control_sock != -1) {
113 shutdown(control_sock, SHUT_RDWR);
114 }
115
116 WakePipe();
117 }
118
Tao Wud0a160d2016-12-13 18:32:17 -0800119 virtual bool ProcessIncoming() REQUIRES(!state_lock_);
Elliott Hughes5d10a872013-04-17 19:26:43 -0700120
121 private:
Tao Wud0a160d2016-12-13 18:32:17 -0800122 int ReceiveClientFd() REQUIRES(!state_lock_);
Elliott Hughes5d10a872013-04-17 19:26:43 -0700123
Tao Wud0a160d2016-12-13 18:32:17 -0800124 bool IsDown() REQUIRES(!state_lock_) {
125 MutexLock mu(Thread::Current(), state_lock_);
126 return shutting_down_;
127 }
128
129 int ControlSock() REQUIRES(!state_lock_) {
130 MutexLock mu(Thread::Current(), state_lock_);
131 if (shutting_down_) {
132 CHECK_EQ(control_sock_, -1);
133 }
134 return control_sock_;
135 }
136
137 int control_sock_ GUARDED_BY(state_lock_);
138 bool shutting_down_ GUARDED_BY(state_lock_);
139 Mutex state_lock_;
Elliott Hughes5d10a872013-04-17 19:26:43 -0700140
141 socklen_t control_addr_len_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700142 union {
Elliott Hughes5d10a872013-04-17 19:26:43 -0700143 sockaddr_un controlAddrUn;
144 sockaddr controlAddrPlain;
145 } control_addr_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700146};
147
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700148/*
149 * Do initial prep work, e.g. binding to ports and opening files. This
150 * runs in the main thread, before the JDWP thread starts, so it shouldn't
151 * do anything that might block forever.
152 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700153bool InitAdbTransport(JdwpState* state, const JdwpOptions*) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800154 VLOG(jdwp) << "ADB transport startup";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700155 state->netState = new JdwpAdbState(state);
Sebastien Hertz7d955652014-10-22 10:57:10 +0200156 return (state->netState != nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700157}
158
159/*
160 * Receive a file descriptor from ADB. The fd can be used to communicate
161 * directly with a debugger or DDMS.
162 *
163 * Returns the file descriptor on success. On failure, returns -1 and
Elliott Hughes5d10a872013-04-17 19:26:43 -0700164 * closes netState->control_sock_.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700165 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700166int JdwpAdbState::ReceiveClientFd() {
167 char dummy = '!';
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700168 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700169 cmsghdr cm;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700170 char buffer[CMSG_SPACE(sizeof(int))];
171 } cm_un;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700172
Elliott Hughes5d10a872013-04-17 19:26:43 -0700173 iovec iov;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700174 iov.iov_base = &dummy;
175 iov.iov_len = 1;
Elliott Hughes5d10a872013-04-17 19:26:43 -0700176
177 msghdr msg;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200178 msg.msg_name = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179 msg.msg_namelen = 0;
180 msg.msg_iov = &iov;
181 msg.msg_iovlen = 1;
182 msg.msg_flags = 0;
183 msg.msg_control = cm_un.buffer;
184 msg.msg_controllen = sizeof(cm_un.buffer);
185
Elliott Hughes5d10a872013-04-17 19:26:43 -0700186 cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700187 cmsg->cmsg_len = msg.msg_controllen;
188 cmsg->cmsg_level = SOL_SOCKET;
189 cmsg->cmsg_type = SCM_RIGHTS;
Brian Carlstrom2d888622013-07-18 17:02:00 -0700190 (reinterpret_cast<int*>(CMSG_DATA(cmsg)))[0] = -1;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700191
Tao Wud0a160d2016-12-13 18:32:17 -0800192 int rc = TEMP_FAILURE_RETRY(recvmsg(ControlSock(), &msg, 0));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700193
Elliott Hughes5d10a872013-04-17 19:26:43 -0700194 if (rc <= 0) {
195 if (rc == -1) {
Tao Wud0a160d2016-12-13 18:32:17 -0800196 PLOG(WARNING) << "Receiving file descriptor from ADB failed (socket " << ControlSock() << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700197 }
Tao Wud0a160d2016-12-13 18:32:17 -0800198 MutexLock mu(Thread::Current(), state_lock_);
Elliott Hughes5d10a872013-04-17 19:26:43 -0700199 close(control_sock_);
200 control_sock_ = -1;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700201 return -1;
202 }
203
Brian Carlstrom2d888622013-07-18 17:02:00 -0700204 return (reinterpret_cast<int*>(CMSG_DATA(cmsg)))[0];
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700205}
206
207/*
208 * Block forever, waiting for a debugger to connect to us. Called from the
209 * JDWP thread.
210 *
211 * This needs to un-block and return "false" if the VM is shutting down. It
212 * should return "true" when it successfully accepts a connection.
213 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700214bool JdwpAdbState::Accept() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700215 int retryCount = 0;
216
217 /* first, ensure that we get a connection to the ADB daemon */
218
Elliott Hughesa21039c2012-06-21 12:09:25 -0700219 retry:
Tao Wud0a160d2016-12-13 18:32:17 -0800220 if (IsDown()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700221 return false;
222 }
223
Tao Wud0a160d2016-12-13 18:32:17 -0800224 if (ControlSock() == -1) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700225 int sleep_ms = 500;
226 const int sleep_max_ms = 2*1000;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700227
Josh Gaocbb65ae2017-03-20 11:33:34 -0700228 int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
Tao Wud0a160d2016-12-13 18:32:17 -0800229 if (sock < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700230 PLOG(ERROR) << "Could not create ADB control socket";
231 return false;
232 }
Tao Wu48fe7942017-01-08 01:20:21 -0800233 struct timeval timeout;
234 timeout.tv_sec = kControlSockSendTimeout;
235 timeout.tv_usec = 0;
236 setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
Tao Wud0a160d2016-12-13 18:32:17 -0800237 {
238 MutexLock mu(Thread::Current(), state_lock_);
239 control_sock_ = sock;
240 if (shutting_down_) {
241 return false;
242 }
243 if (!MakePipe()) {
244 return false;
245 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700246 }
247
Josh Gao4b49bb72018-02-12 15:06:42 -0800248 int32_t pid = getpid();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700249
250 for (;;) {
251 /*
252 * If adbd isn't running, because USB debugging was disabled or
253 * perhaps the system is restarting it for "adb root", the
254 * connect() will fail. We loop here forever waiting for it
255 * to come back.
256 *
257 * Waking up and polling every couple of seconds is generally a
258 * bad thing to do, but we only do this if the application is
259 * debuggable *and* adbd isn't running. Still, for the sake
260 * of battery life, we should consider timing out and giving
261 * up after a few minutes in case somebody ships an app with
262 * the debuggable flag set.
263 */
Josh Gaocbb65ae2017-03-20 11:33:34 -0700264 int ret = connect(ControlSock(), &control_addr_.controlAddrPlain, control_addr_len_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700265 if (!ret) {
Tao Wud0a160d2016-12-13 18:32:17 -0800266 int control_sock = ControlSock();
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100267#ifdef ART_TARGET_ANDROID
Tao Wud0a160d2016-12-13 18:32:17 -0800268 if (control_sock < 0 || !socket_peer_is_trusted(control_sock)) {
269 if (control_sock >= 0 && shutdown(control_sock, SHUT_RDWR)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700270 PLOG(ERROR) << "trouble shutting down socket";
271 }
272 return false;
273 }
274#endif
275
276 /* now try to send our pid to the ADB daemon */
Josh Gao4b49bb72018-02-12 15:06:42 -0800277 ret = TEMP_FAILURE_RETRY(send(control_sock, &pid, sizeof(pid), 0));
278 if (ret == sizeof(pid)) {
279 VLOG(jdwp) << "PID " << pid << " sent to ADB";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700280 break;
281 }
282
283 PLOG(ERROR) << "Weird, can't send JDWP process pid to ADB";
284 return false;
285 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800286 if (VLOG_IS_ON(jdwp)) {
287 PLOG(ERROR) << "Can't connect to ADB control socket";
288 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700289
Elliott Hughesa21039c2012-06-21 12:09:25 -0700290 usleep(sleep_ms * 1000);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700291
292 sleep_ms += (sleep_ms >> 1);
293 if (sleep_ms > sleep_max_ms) {
294 sleep_ms = sleep_max_ms;
295 }
Tao Wud0a160d2016-12-13 18:32:17 -0800296 if (IsDown()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700297 return false;
298 }
299 }
300 }
301
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800302 VLOG(jdwp) << "trying to receive file descriptor from ADB";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700303 /* now we can receive a client file descriptor */
Tao Wud0a160d2016-12-13 18:32:17 -0800304 int sock = ReceiveClientFd();
305 {
306 MutexLock mu(Thread::Current(), state_lock_);
307 clientSock = sock;
308 if (shutting_down_) {
309 return false; // suppress logs and additional activity
310 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700311 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700312 if (clientSock == -1) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700313 if (++retryCount > 5) {
314 LOG(ERROR) << "adb connection max retries exceeded";
315 return false;
316 }
317 goto retry;
318 } else {
Elliott Hughes5d10a872013-04-17 19:26:43 -0700319 VLOG(jdwp) << "received file descriptor " << clientSock << " from ADB";
320 SetAwaitingHandshake(true);
321 input_count_ = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700322 return true;
323 }
324}
325
326/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700327 * Process incoming data. If no data is available, this will block until
328 * some arrives.
329 *
330 * If we get a full packet, handle it.
331 *
332 * To take some of the mystery out of life, we want to reject incoming
333 * connections if we already have a debugger attached. If we don't, the
334 * debugger will just mysteriously hang until it times out. We could just
335 * close the listen socket, but there's a good chance we won't be able to
336 * bind to the same port again, which would confuse utilities.
337 *
338 * Returns "false" on error (indicating that the connection has been severed),
339 * "true" if things are still okay.
340 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700341bool JdwpAdbState::ProcessIncoming() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700342 int readCount;
343
Brian Carlstrom42748892013-07-18 18:04:08 -0700344 CHECK_NE(clientSock, -1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700345
Elliott Hughes5d10a872013-04-17 19:26:43 -0700346 if (!HaveFullPacket()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700347 /* read some more, looping until we have data */
348 errno = 0;
349 while (1) {
350 int selCount;
351 fd_set readfds;
352 int maxfd = -1;
353 int fd;
354
355 FD_ZERO(&readfds);
356
357 /* configure fds; note these may get zapped by another thread */
Tao Wud0a160d2016-12-13 18:32:17 -0800358 fd = ControlSock();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700359 if (fd >= 0) {
360 FD_SET(fd, &readfds);
361 if (maxfd < fd) {
362 maxfd = fd;
363 }
364 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700365 fd = clientSock;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700366 if (fd >= 0) {
367 FD_SET(fd, &readfds);
368 if (maxfd < fd) {
369 maxfd = fd;
370 }
371 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700372 fd = wake_pipe_[0];
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700373 if (fd >= 0) {
374 FD_SET(fd, &readfds);
375 if (maxfd < fd) {
376 maxfd = fd;
377 }
378 } else {
379 LOG(INFO) << "NOTE: entering select w/o wakepipe";
380 }
381
382 if (maxfd < 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800383 VLOG(jdwp) << "+++ all fds are closed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700384 return false;
385 }
386
387 /*
388 * Select blocks until it sees activity on the file descriptors.
389 * Closing the local file descriptor does not count as activity,
390 * so we can't rely on that to wake us up (it works for read()
391 * and accept(), but not select()).
392 *
393 * We can do one of three things: (1) send a signal and catch
Elliott Hughes5d10a872013-04-17 19:26:43 -0700394 * EINTR, (2) open an additional fd ("wake pipe") and write to
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700395 * it when it's time to exit, or (3) time out periodically and
396 * re-issue the select. We're currently using #2, as it's more
397 * reliable than #1 and generally better than #3. Wastes two fds.
398 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200399 selCount = select(maxfd + 1, &readfds, nullptr, nullptr, nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700400 if (selCount < 0) {
401 if (errno == EINTR) {
402 continue;
403 }
404 PLOG(ERROR) << "select failed";
405 goto fail;
406 }
407
Elliott Hughes5d10a872013-04-17 19:26:43 -0700408 if (wake_pipe_[0] >= 0 && FD_ISSET(wake_pipe_[0], &readfds)) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700409 VLOG(jdwp) << "Got wake-up signal, bailing out of select";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700410 goto fail;
411 }
Tao Wud0a160d2016-12-13 18:32:17 -0800412 int control_sock = ControlSock();
413 if (control_sock >= 0 && FD_ISSET(control_sock, &readfds)) {
Elliott Hughes5d10a872013-04-17 19:26:43 -0700414 int sock = ReceiveClientFd();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700415 if (sock >= 0) {
416 LOG(INFO) << "Ignoring second debugger -- accepting and dropping";
417 close(sock);
418 } else {
Tao Wud0a160d2016-12-13 18:32:17 -0800419 CHECK_EQ(ControlSock(), -1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700420 /*
421 * Remote side most likely went away, so our next read
Elliott Hughes5d10a872013-04-17 19:26:43 -0700422 * on clientSock will fail and throw us out of the loop.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700423 */
424 }
425 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700426 if (clientSock >= 0 && FD_ISSET(clientSock, &readfds)) {
427 readCount = read(clientSock, input_buffer_ + input_count_, sizeof(input_buffer_) - input_count_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700428 if (readCount < 0) {
429 /* read failed */
430 if (errno != EINTR) {
431 goto fail;
432 }
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700433 VLOG(jdwp) << "+++ EINTR hit";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700434 return true;
435 } else if (readCount == 0) {
436 /* EOF hit -- far end went away */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800437 VLOG(jdwp) << "+++ peer disconnected";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700438 goto fail;
439 } else {
440 break;
441 }
442 }
443 }
444
Elliott Hughes5d10a872013-04-17 19:26:43 -0700445 input_count_ += readCount;
446 if (!HaveFullPacket()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700447 return true; /* still not there yet */
448 }
449 }
450
451 /*
452 * Special-case the initial handshake. For some bizarre reason we're
453 * expected to emulate bad tty settings by echoing the request back
454 * exactly as it was sent. Note the handshake is always initiated by
455 * the debugger, no matter who connects to whom.
456 *
457 * Other than this one case, the protocol [claims to be] stateless.
458 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700459 if (IsAwaitingHandshake()) {
460 if (memcmp(input_buffer_, kMagicHandshake, kMagicHandshakeLen) != 0) {
461 LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", input_buffer_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700462 goto fail;
463 }
464
465 errno = 0;
Elliott Hughes5d10a872013-04-17 19:26:43 -0700466 int cc = TEMP_FAILURE_RETRY(write(clientSock, input_buffer_, kMagicHandshakeLen));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700467 if (cc != kMagicHandshakeLen) {
468 PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")";
469 goto fail;
470 }
471
Elliott Hughes5d10a872013-04-17 19:26:43 -0700472 ConsumeBytes(kMagicHandshakeLen);
473 SetAwaitingHandshake(false);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800474 VLOG(jdwp) << "+++ handshake complete";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700475 return true;
476 }
477
478 /*
479 * Handle this packet.
480 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700481 return state_->HandlePacket();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700482
Elliott Hughesa21039c2012-06-21 12:09:25 -0700483 fail:
Elliott Hughes5d10a872013-04-17 19:26:43 -0700484 Close();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700485 return false;
486}
487
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700488} // namespace JDWP
489
490} // namespace art