blob: 27f9aaa165e6930fb6127048d227f84598c00555 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080024#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "jdwp/jdwp_handler.h"
26#include "jdwp/jdwp_priv.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027
Elliott Hughes872d4ec2011-10-21 17:07:15 -070028#ifdef HAVE_ANDROID_OS
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
49#define kInputBufferSize 8192
50
Elliott Hughes872d4ec2011-10-21 17:07:15 -070051#define kJdwpControlName "\0jdwp-control"
52#define kJdwpControlNameLen (sizeof(kJdwpControlName)-1)
53
54namespace art {
55
56namespace JDWP {
57
58struct JdwpNetState : public JdwpNetStateBase {
59 int controlSock;
60 bool awaitingHandshake;
61 bool shuttingDown;
62 int wakeFds[2];
63
Elliott Hughes74847412012-06-20 18:10:21 -070064 size_t inputCount;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070065 unsigned char inputBuffer[kInputBufferSize];
66
67 socklen_t controlAddrLen;
68 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -070069 sockaddr_un controlAddrUn;
70 sockaddr controlAddrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070071 } controlAddr;
72
73 JdwpNetState() {
74 controlSock = -1;
75 awaitingHandshake = false;
76 shuttingDown = false;
77 wakeFds[0] = -1;
78 wakeFds[1] = -1;
79
80 inputCount = 0;
81
82 controlAddr.controlAddrUn.sun_family = AF_UNIX;
83 controlAddrLen = sizeof(controlAddr.controlAddrUn.sun_family) + kJdwpControlNameLen;
84 memcpy(controlAddr.controlAddrUn.sun_path, kJdwpControlName, kJdwpControlNameLen);
85 }
86};
87
88static void adbStateFree(JdwpNetState* netState) {
89 if (netState == NULL) {
90 return;
91 }
92
93 if (netState->clientSock >= 0) {
94 shutdown(netState->clientSock, SHUT_RDWR);
95 close(netState->clientSock);
96 }
97 if (netState->controlSock >= 0) {
98 shutdown(netState->controlSock, SHUT_RDWR);
99 close(netState->controlSock);
100 }
101 if (netState->wakeFds[0] >= 0) {
102 close(netState->wakeFds[0]);
103 netState->wakeFds[0] = -1;
104 }
105 if (netState->wakeFds[1] >= 0) {
106 close(netState->wakeFds[1]);
107 netState->wakeFds[1] = -1;
108 }
109
110 delete netState;
111}
112
113/*
114 * Do initial prep work, e.g. binding to ports and opening files. This
115 * runs in the main thread, before the JDWP thread starts, so it shouldn't
116 * do anything that might block forever.
117 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700118static bool startup(JdwpState* state, const JdwpOptions*) {
119 JdwpNetState* netState;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700120
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800121 VLOG(jdwp) << "ADB transport startup";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700122
123 state->netState = netState = new JdwpNetState;
124 if (netState == NULL) {
125 return false;
126 }
127 return true;
128}
129
130/*
131 * Receive a file descriptor from ADB. The fd can be used to communicate
132 * directly with a debugger or DDMS.
133 *
134 * Returns the file descriptor on success. On failure, returns -1 and
135 * closes netState->controlSock.
136 */
137static int receiveClientFd(JdwpNetState* netState) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700138 msghdr msg;
139 cmsghdr* cmsg;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700140 iovec iov;
141 char dummy = '!';
142 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700143 cmsghdr cm;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700144 char buffer[CMSG_SPACE(sizeof(int))];
145 } cm_un;
146 int ret;
147
148 iov.iov_base = &dummy;
149 iov.iov_len = 1;
150 msg.msg_name = NULL;
151 msg.msg_namelen = 0;
152 msg.msg_iov = &iov;
153 msg.msg_iovlen = 1;
154 msg.msg_flags = 0;
155 msg.msg_control = cm_un.buffer;
156 msg.msg_controllen = sizeof(cm_un.buffer);
157
158 cmsg = CMSG_FIRSTHDR(&msg);
159 cmsg->cmsg_len = msg.msg_controllen;
160 cmsg->cmsg_level = SOL_SOCKET;
161 cmsg->cmsg_type = SCM_RIGHTS;
162 ((int*)(void*)CMSG_DATA(cmsg))[0] = -1;
163
164 do {
165 ret = recvmsg(netState->controlSock, &msg, 0);
166 } while (ret < 0 && errno == EINTR);
167
168 if (ret <= 0) {
169 if (ret < 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800170 PLOG(WARNING) << "Receiving file descriptor from ADB failed (socket " << netState->controlSock << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700171 }
172 close(netState->controlSock);
173 netState->controlSock = -1;
174 return -1;
175 }
176
177 return ((int*)(void*)CMSG_DATA(cmsg))[0];
178}
179
180/*
181 * Block forever, waiting for a debugger to connect to us. Called from the
182 * JDWP thread.
183 *
184 * This needs to un-block and return "false" if the VM is shutting down. It
185 * should return "true" when it successfully accepts a connection.
186 */
187static bool acceptConnection(JdwpState* state) {
188 JdwpNetState* netState = state->netState;
189 int retryCount = 0;
190
191 /* first, ensure that we get a connection to the ADB daemon */
192
Elliott Hughesa21039c2012-06-21 12:09:25 -0700193 retry:
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700194 if (netState->shuttingDown) {
195 return false;
196 }
197
198 if (netState->controlSock < 0) {
199 int sleep_ms = 500;
200 const int sleep_max_ms = 2*1000;
201 char buff[5];
202
203 netState->controlSock = socket(PF_UNIX, SOCK_STREAM, 0);
204 if (netState->controlSock < 0) {
205 PLOG(ERROR) << "Could not create ADB control socket";
206 return false;
207 }
208
209 if (pipe(netState->wakeFds) < 0) {
210 PLOG(ERROR) << "pipe failed";
211 return false;
212 }
213
214 snprintf(buff, sizeof(buff), "%04x", getpid());
215 buff[4] = 0;
216
217 for (;;) {
218 /*
219 * If adbd isn't running, because USB debugging was disabled or
220 * perhaps the system is restarting it for "adb root", the
221 * connect() will fail. We loop here forever waiting for it
222 * to come back.
223 *
224 * Waking up and polling every couple of seconds is generally a
225 * bad thing to do, but we only do this if the application is
226 * debuggable *and* adbd isn't running. Still, for the sake
227 * of battery life, we should consider timing out and giving
228 * up after a few minutes in case somebody ships an app with
229 * the debuggable flag set.
230 */
231 int ret = connect(netState->controlSock, &netState->controlAddr.controlAddrPlain, netState->controlAddrLen);
232 if (!ret) {
233#ifdef HAVE_ANDROID_OS
234 if (!socket_peer_is_trusted(netState->controlSock)) {
235 if (shutdown(netState->controlSock, SHUT_RDWR)) {
236 PLOG(ERROR) << "trouble shutting down socket";
237 }
238 return false;
239 }
240#endif
241
242 /* now try to send our pid to the ADB daemon */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700243 ret = TEMP_FAILURE_RETRY(send(netState->controlSock, buff, 4, 0));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700244 if (ret >= 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800245 VLOG(jdwp) << StringPrintf("PID sent as '%.*s' to ADB", 4, buff);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700246 break;
247 }
248
249 PLOG(ERROR) << "Weird, can't send JDWP process pid to ADB";
250 return false;
251 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800252 if (VLOG_IS_ON(jdwp)) {
253 PLOG(ERROR) << "Can't connect to ADB control socket";
254 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700255
Elliott Hughesa21039c2012-06-21 12:09:25 -0700256 usleep(sleep_ms * 1000);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700257
258 sleep_ms += (sleep_ms >> 1);
259 if (sleep_ms > sleep_max_ms) {
260 sleep_ms = sleep_max_ms;
261 }
262 if (netState->shuttingDown) {
263 return false;
264 }
265 }
266 }
267
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800268 VLOG(jdwp) << "trying to receive file descriptor from ADB";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700269 /* now we can receive a client file descriptor */
270 netState->clientSock = receiveClientFd(netState);
271 if (netState->shuttingDown) {
272 return false; // suppress logs and additional activity
273 }
274 if (netState->clientSock < 0) {
275 if (++retryCount > 5) {
276 LOG(ERROR) << "adb connection max retries exceeded";
277 return false;
278 }
279 goto retry;
280 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800281 VLOG(jdwp) << "received file descriptor " << netState->clientSock << " from ADB";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700282 netState->awaitingHandshake = 1;
283 netState->inputCount = 0;
284 return true;
285 }
286}
287
288/*
289 * Connect out to a debugger (for server=n). Not required.
290 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700291static bool establishConnection(JdwpState*, const JdwpOptions*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700292 return false;
293}
294
295/*
296 * Close a connection from a debugger (which may have already dropped us).
297 * Only called from the JDWP thread.
298 */
299static void closeConnection(JdwpState* state) {
300 CHECK(state != NULL && state->netState != NULL);
301
302 JdwpNetState* netState = state->netState;
303 if (netState->clientSock < 0) {
304 return;
305 }
306
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800307 VLOG(jdwp) << "+++ closed JDWP <-> ADB connection";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700308
309 close(netState->clientSock);
310 netState->clientSock = -1;
311}
312
313/*
314 * Close all network stuff, including the socket we use to listen for
315 * new connections.
316 *
317 * May be called from a non-JDWP thread, e.g. when the VM is shutting down.
318 */
319static void adbStateShutdown(JdwpNetState* netState) {
320 int controlSock;
321 int clientSock;
322
323 if (netState == NULL) {
324 return;
325 }
326
327 netState->shuttingDown = true;
328
329 clientSock = netState->clientSock;
330 if (clientSock >= 0) {
331 shutdown(clientSock, SHUT_RDWR);
332 netState->clientSock = -1;
333 }
334
335 controlSock = netState->controlSock;
336 if (controlSock >= 0) {
337 shutdown(controlSock, SHUT_RDWR);
338 netState->controlSock = -1;
339 }
340
341 if (netState->wakeFds[1] >= 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800342 VLOG(jdwp) << "+++ writing to wakePipe";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700343 write(netState->wakeFds[1], "", 1);
344 }
345}
346
347static void netShutdown(JdwpState* state) {
348 adbStateShutdown(state->netState);
349}
350
351/*
352 * Free up anything we put in state->netState. This is called after
353 * "netShutdown", after the JDWP thread has stopped.
354 */
355static void netFree(JdwpState* state) {
356 JdwpNetState* netState = state->netState;
357 adbStateFree(netState);
358}
359
360/*
361 * Is a debugger connected to us?
362 */
363static bool isConnected(JdwpState* state) {
364 return (state->netState != NULL && state->netState->clientSock >= 0);
365}
366
367/*
368 * Are we still waiting for the JDWP handshake?
369 */
370static bool awaitingHandshake(JdwpState* state) {
371 return state->netState->awaitingHandshake;
372}
373
374/*
375 * Figure out if we have a full packet in the buffer.
376 */
377static bool haveFullPacket(JdwpNetState* netState) {
378 if (netState->awaitingHandshake) {
Elliott Hughes74847412012-06-20 18:10:21 -0700379 return (netState->inputCount >= kMagicHandshakeLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700380 }
381 if (netState->inputCount < 4) {
382 return false;
383 }
Elliott Hughes74847412012-06-20 18:10:21 -0700384 uint32_t length = Get4BE(netState->inputBuffer);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700385 return (netState->inputCount >= length);
386}
387
388/*
389 * Consume bytes from the buffer.
390 *
391 * This would be more efficient with a circular buffer. However, we're
392 * usually only going to find one packet, which is trivial to handle.
393 */
Elliott Hughes74847412012-06-20 18:10:21 -0700394static void consumeBytes(JdwpNetState* netState, size_t count) {
395 CHECK_GT(count, 0U);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700396 CHECK_LE(count, netState->inputCount);
397
398 if (count == netState->inputCount) {
399 netState->inputCount = 0;
400 return;
401 }
402
403 memmove(netState->inputBuffer, netState->inputBuffer + count, netState->inputCount - count);
404 netState->inputCount -= count;
405}
406
407/*
408 * Handle a packet. Returns "false" if we encounter a connection-fatal error.
409 */
410static bool handlePacket(JdwpState* state) {
411 JdwpNetState* netState = state->netState;
412 const unsigned char* buf = netState->inputBuffer;
413 JdwpReqHeader hdr;
414 uint32_t length, id;
415 uint8_t flags, cmdSet, cmd;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700416 int dataLen;
417
418 cmd = cmdSet = 0; // shut up gcc
419
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700420 length = Read4BE(&buf);
421 id = Read4BE(&buf);
422 flags = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700423 if ((flags & kJDWPFlagReply) != 0) {
jeffhaobd787442012-09-06 16:24:04 -0700424 LOG(FATAL) << "reply?!";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700425 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700426 cmdSet = Read1(&buf);
427 cmd = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700428 }
429
Elliott Hughes74847412012-06-20 18:10:21 -0700430 CHECK_LE(length, netState->inputCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700431 dataLen = length - (buf - netState->inputBuffer);
432
jeffhaobd787442012-09-06 16:24:04 -0700433 ExpandBuf* pReply = expandBufAlloc();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700434
jeffhaobd787442012-09-06 16:24:04 -0700435 hdr.length = length;
436 hdr.id = id;
437 hdr.cmdSet = cmdSet;
438 hdr.cmd = cmd;
439 state->ProcessRequest(&hdr, buf, dataLen, pReply);
440 if (expandBufGetLength(pReply) > 0) {
441 ssize_t cc = netState->writePacket(pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700442
jeffhaobd787442012-09-06 16:24:04 -0700443 if (cc != (ssize_t) expandBufGetLength(pReply)) {
444 PLOG(ERROR) << "Failed sending reply to debugger";
445 expandBufFree(pReply);
446 return false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700447 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700448 } else {
jeffhaobd787442012-09-06 16:24:04 -0700449 LOG(WARNING) << "No reply created for set=" << cmdSet << " cmd=" << cmd;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700450 }
jeffhaobd787442012-09-06 16:24:04 -0700451 expandBufFree(pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700452
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800453 VLOG(jdwp) << "----------";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700454
455 consumeBytes(netState, length);
456 return true;
457}
458
459/*
460 * Process incoming data. If no data is available, this will block until
461 * some arrives.
462 *
463 * If we get a full packet, handle it.
464 *
465 * To take some of the mystery out of life, we want to reject incoming
466 * connections if we already have a debugger attached. If we don't, the
467 * debugger will just mysteriously hang until it times out. We could just
468 * close the listen socket, but there's a good chance we won't be able to
469 * bind to the same port again, which would confuse utilities.
470 *
471 * Returns "false" on error (indicating that the connection has been severed),
472 * "true" if things are still okay.
473 */
474static bool processIncoming(JdwpState* state) {
475 JdwpNetState* netState = state->netState;
476 int readCount;
477
478 CHECK_GE(netState->clientSock, 0);
479
480 if (!haveFullPacket(netState)) {
481 /* read some more, looping until we have data */
482 errno = 0;
483 while (1) {
484 int selCount;
485 fd_set readfds;
486 int maxfd = -1;
487 int fd;
488
489 FD_ZERO(&readfds);
490
491 /* configure fds; note these may get zapped by another thread */
492 fd = netState->controlSock;
493 if (fd >= 0) {
494 FD_SET(fd, &readfds);
495 if (maxfd < fd) {
496 maxfd = fd;
497 }
498 }
499 fd = netState->clientSock;
500 if (fd >= 0) {
501 FD_SET(fd, &readfds);
502 if (maxfd < fd) {
503 maxfd = fd;
504 }
505 }
506 fd = netState->wakeFds[0];
507 if (fd >= 0) {
508 FD_SET(fd, &readfds);
509 if (maxfd < fd) {
510 maxfd = fd;
511 }
512 } else {
513 LOG(INFO) << "NOTE: entering select w/o wakepipe";
514 }
515
516 if (maxfd < 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800517 VLOG(jdwp) << "+++ all fds are closed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700518 return false;
519 }
520
521 /*
522 * Select blocks until it sees activity on the file descriptors.
523 * Closing the local file descriptor does not count as activity,
524 * so we can't rely on that to wake us up (it works for read()
525 * and accept(), but not select()).
526 *
527 * We can do one of three things: (1) send a signal and catch
528 * EINTR, (2) open an additional fd ("wakePipe") and write to
529 * it when it's time to exit, or (3) time out periodically and
530 * re-issue the select. We're currently using #2, as it's more
531 * reliable than #1 and generally better than #3. Wastes two fds.
532 */
533 selCount = select(maxfd+1, &readfds, NULL, NULL, NULL);
534 if (selCount < 0) {
535 if (errno == EINTR) {
536 continue;
537 }
538 PLOG(ERROR) << "select failed";
539 goto fail;
540 }
541
542 if (netState->wakeFds[0] >= 0 && FD_ISSET(netState->wakeFds[0], &readfds)) {
543 LOG(DEBUG) << "Got wake-up signal, bailing out of select";
544 goto fail;
545 }
546 if (netState->controlSock >= 0 && FD_ISSET(netState->controlSock, &readfds)) {
547 int sock = receiveClientFd(netState);
548 if (sock >= 0) {
549 LOG(INFO) << "Ignoring second debugger -- accepting and dropping";
550 close(sock);
551 } else {
552 CHECK_LT(netState->controlSock, 0);
553 /*
554 * Remote side most likely went away, so our next read
555 * on netState->clientSock will fail and throw us out
556 * of the loop.
557 */
558 }
559 }
560 if (netState->clientSock >= 0 && FD_ISSET(netState->clientSock, &readfds)) {
561 readCount = read(netState->clientSock, netState->inputBuffer + netState->inputCount, sizeof(netState->inputBuffer) - netState->inputCount);
562 if (readCount < 0) {
563 /* read failed */
564 if (errno != EINTR) {
565 goto fail;
566 }
567 LOG(DEBUG) << "+++ EINTR hit";
568 return true;
569 } else if (readCount == 0) {
570 /* EOF hit -- far end went away */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800571 VLOG(jdwp) << "+++ peer disconnected";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700572 goto fail;
573 } else {
574 break;
575 }
576 }
577 }
578
579 netState->inputCount += readCount;
580 if (!haveFullPacket(netState)) {
581 return true; /* still not there yet */
582 }
583 }
584
585 /*
586 * Special-case the initial handshake. For some bizarre reason we're
587 * expected to emulate bad tty settings by echoing the request back
588 * exactly as it was sent. Note the handshake is always initiated by
589 * the debugger, no matter who connects to whom.
590 *
591 * Other than this one case, the protocol [claims to be] stateless.
592 */
593 if (netState->awaitingHandshake) {
594 int cc;
595
596 if (memcmp(netState->inputBuffer, kMagicHandshake, kMagicHandshakeLen) != 0) {
597 LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", netState->inputBuffer);
598 goto fail;
599 }
600
601 errno = 0;
602 cc = write(netState->clientSock, netState->inputBuffer, kMagicHandshakeLen);
603 if (cc != kMagicHandshakeLen) {
604 PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")";
605 goto fail;
606 }
607
608 consumeBytes(netState, kMagicHandshakeLen);
609 netState->awaitingHandshake = false;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800610 VLOG(jdwp) << "+++ handshake complete";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700611 return true;
612 }
613
614 /*
615 * Handle this packet.
616 */
617 return handlePacket(state);
618
Elliott Hughesa21039c2012-06-21 12:09:25 -0700619 fail:
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700620 closeConnection(state);
621 return false;
622}
623
624/*
625 * Send a request.
626 *
627 * The entire packet must be sent with a single write() call to avoid
628 * threading issues.
629 *
630 * Returns "true" if it was sent successfully.
631 */
632static bool sendRequest(JdwpState* state, ExpandBuf* pReq) {
633 JdwpNetState* netState = state->netState;
634
635 if (netState->clientSock < 0) {
636 /* can happen with some DDMS events */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800637 VLOG(jdwp) << "NOT sending request -- no debugger is attached";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700638 return false;
639 }
640
641 errno = 0;
642
643 ssize_t cc = netState->writePacket(pReq);
644
645 if (cc != (ssize_t) expandBufGetLength(pReq)) {
646 PLOG(ERROR) << "Failed sending req to debugger (" << cc << " of " << expandBufGetLength(pReq) << ")";
647 return false;
648 }
649
650 return true;
651}
652
653/*
654 * Send a request that was split into multiple buffers.
655 *
656 * The entire packet must be sent with a single writev() call to avoid
657 * threading issues.
658 *
659 * Returns "true" if it was sent successfully.
660 */
Elliott Hughescccd84f2011-12-05 16:51:54 -0800661static bool sendBufferedRequest(JdwpState* state, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700662 JdwpNetState* netState = state->netState;
663
664 if (netState->clientSock < 0) {
665 /* can happen with some DDMS events */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800666 VLOG(jdwp) << "NOT sending request -- no debugger is attached";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700667 return false;
668 }
669
670 size_t expected = 0;
Elliott Hughescccd84f2011-12-05 16:51:54 -0800671 for (int i = 0; i < iov_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700672 expected += iov[i].iov_len;
673 }
674
Elliott Hughescccd84f2011-12-05 16:51:54 -0800675 ssize_t actual = netState->writeBufferedPacket(iov, iov_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700676 if ((size_t)actual != expected) {
677 PLOG(ERROR) << "Failed sending b-req to debugger (" << actual << " of " << expected << ")";
678 return false;
679 }
680
681 return true;
682}
683
684/*
685 * Our functions.
686 */
687static const JdwpTransport adbTransport = {
688 startup,
689 acceptConnection,
690 establishConnection,
691 closeConnection,
692 netShutdown,
693 netFree,
694 isConnected,
695 awaitingHandshake,
696 processIncoming,
697 sendRequest,
698 sendBufferedRequest
699};
700
701/*
702 * Return our set.
703 */
704const JdwpTransport* AndroidAdbTransport() {
705 return &adbTransport;
706}
707
708} // namespace JDWP
709
710} // namespace art