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