blob: 4577b590b222f91b816b5eb137b43dc0d4a627a0 [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
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
193retry:
194 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 */
243 do {
244 ret = send( netState->controlSock, buff, 4, 0 );
245 } while (ret < 0 && errno == EINTR);
246
247 if (ret >= 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800248 VLOG(jdwp) << StringPrintf("PID sent as '%.*s' to ADB", 4, buff);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700249 break;
250 }
251
252 PLOG(ERROR) << "Weird, can't send JDWP process pid to ADB";
253 return false;
254 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800255 if (VLOG_IS_ON(jdwp)) {
256 PLOG(ERROR) << "Can't connect to ADB control socket";
257 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700258
259 usleep( sleep_ms*1000 );
260
261 sleep_ms += (sleep_ms >> 1);
262 if (sleep_ms > sleep_max_ms) {
263 sleep_ms = sleep_max_ms;
264 }
265 if (netState->shuttingDown) {
266 return false;
267 }
268 }
269 }
270
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800271 VLOG(jdwp) << "trying to receive file descriptor from ADB";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700272 /* now we can receive a client file descriptor */
273 netState->clientSock = receiveClientFd(netState);
274 if (netState->shuttingDown) {
275 return false; // suppress logs and additional activity
276 }
277 if (netState->clientSock < 0) {
278 if (++retryCount > 5) {
279 LOG(ERROR) << "adb connection max retries exceeded";
280 return false;
281 }
282 goto retry;
283 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800284 VLOG(jdwp) << "received file descriptor " << netState->clientSock << " from ADB";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700285 netState->awaitingHandshake = 1;
286 netState->inputCount = 0;
287 return true;
288 }
289}
290
291/*
292 * Connect out to a debugger (for server=n). Not required.
293 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700294static bool establishConnection(JdwpState*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700295 return false;
296}
297
298/*
299 * Close a connection from a debugger (which may have already dropped us).
300 * Only called from the JDWP thread.
301 */
302static void closeConnection(JdwpState* state) {
303 CHECK(state != NULL && state->netState != NULL);
304
305 JdwpNetState* netState = state->netState;
306 if (netState->clientSock < 0) {
307 return;
308 }
309
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800310 VLOG(jdwp) << "+++ closed JDWP <-> ADB connection";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700311
312 close(netState->clientSock);
313 netState->clientSock = -1;
314}
315
316/*
317 * Close all network stuff, including the socket we use to listen for
318 * new connections.
319 *
320 * May be called from a non-JDWP thread, e.g. when the VM is shutting down.
321 */
322static void adbStateShutdown(JdwpNetState* netState) {
323 int controlSock;
324 int clientSock;
325
326 if (netState == NULL) {
327 return;
328 }
329
330 netState->shuttingDown = true;
331
332 clientSock = netState->clientSock;
333 if (clientSock >= 0) {
334 shutdown(clientSock, SHUT_RDWR);
335 netState->clientSock = -1;
336 }
337
338 controlSock = netState->controlSock;
339 if (controlSock >= 0) {
340 shutdown(controlSock, SHUT_RDWR);
341 netState->controlSock = -1;
342 }
343
344 if (netState->wakeFds[1] >= 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800345 VLOG(jdwp) << "+++ writing to wakePipe";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700346 write(netState->wakeFds[1], "", 1);
347 }
348}
349
350static void netShutdown(JdwpState* state) {
351 adbStateShutdown(state->netState);
352}
353
354/*
355 * Free up anything we put in state->netState. This is called after
356 * "netShutdown", after the JDWP thread has stopped.
357 */
358static void netFree(JdwpState* state) {
359 JdwpNetState* netState = state->netState;
360 adbStateFree(netState);
361}
362
363/*
364 * Is a debugger connected to us?
365 */
366static bool isConnected(JdwpState* state) {
367 return (state->netState != NULL && state->netState->clientSock >= 0);
368}
369
370/*
371 * Are we still waiting for the JDWP handshake?
372 */
373static bool awaitingHandshake(JdwpState* state) {
374 return state->netState->awaitingHandshake;
375}
376
377/*
378 * Figure out if we have a full packet in the buffer.
379 */
380static bool haveFullPacket(JdwpNetState* netState) {
381 if (netState->awaitingHandshake) {
Elliott Hughes74847412012-06-20 18:10:21 -0700382 return (netState->inputCount >= kMagicHandshakeLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700383 }
384 if (netState->inputCount < 4) {
385 return false;
386 }
Elliott Hughes74847412012-06-20 18:10:21 -0700387 uint32_t length = Get4BE(netState->inputBuffer);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700388 return (netState->inputCount >= length);
389}
390
391/*
392 * Consume bytes from the buffer.
393 *
394 * This would be more efficient with a circular buffer. However, we're
395 * usually only going to find one packet, which is trivial to handle.
396 */
Elliott Hughes74847412012-06-20 18:10:21 -0700397static void consumeBytes(JdwpNetState* netState, size_t count) {
398 CHECK_GT(count, 0U);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700399 CHECK_LE(count, netState->inputCount);
400
401 if (count == netState->inputCount) {
402 netState->inputCount = 0;
403 return;
404 }
405
406 memmove(netState->inputBuffer, netState->inputBuffer + count, netState->inputCount - count);
407 netState->inputCount -= count;
408}
409
410/*
411 * Handle a packet. Returns "false" if we encounter a connection-fatal error.
412 */
413static bool handlePacket(JdwpState* state) {
414 JdwpNetState* netState = state->netState;
415 const unsigned char* buf = netState->inputBuffer;
416 JdwpReqHeader hdr;
417 uint32_t length, id;
418 uint8_t flags, cmdSet, cmd;
419 uint16_t error;
420 bool reply;
421 int dataLen;
422
423 cmd = cmdSet = 0; // shut up gcc
424
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700425 length = Read4BE(&buf);
426 id = Read4BE(&buf);
427 flags = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700428 if ((flags & kJDWPFlagReply) != 0) {
429 reply = true;
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700430 error = Read2BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700431 } else {
432 reply = false;
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700433 cmdSet = Read1(&buf);
434 cmd = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700435 }
436
Elliott Hughes74847412012-06-20 18:10:21 -0700437 CHECK_LE(length, netState->inputCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700438 dataLen = length - (buf - netState->inputBuffer);
439
440 if (!reply) {
441 ExpandBuf* pReply = expandBufAlloc();
442
443 hdr.length = length;
444 hdr.id = id;
445 hdr.cmdSet = cmdSet;
446 hdr.cmd = cmd;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700447 state->ProcessRequest(&hdr, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700448 if (expandBufGetLength(pReply) > 0) {
449 ssize_t cc = netState->writePacket(pReply);
450
451 if (cc != (ssize_t) expandBufGetLength(pReply)) {
452 PLOG(ERROR) << "Failed sending reply to debugger";
453 expandBufFree(pReply);
454 return false;
455 }
456 } else {
457 LOG(WARNING) << "No reply created for set=" << cmdSet << " cmd=" << cmd;
458 }
459 expandBufFree(pReply);
460 } else {
461 LOG(FATAL) << "reply?!";
462 }
463
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800464 VLOG(jdwp) << "----------";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700465
466 consumeBytes(netState, length);
467 return true;
468}
469
470/*
471 * Process incoming data. If no data is available, this will block until
472 * some arrives.
473 *
474 * If we get a full packet, handle it.
475 *
476 * To take some of the mystery out of life, we want to reject incoming
477 * connections if we already have a debugger attached. If we don't, the
478 * debugger will just mysteriously hang until it times out. We could just
479 * close the listen socket, but there's a good chance we won't be able to
480 * bind to the same port again, which would confuse utilities.
481 *
482 * Returns "false" on error (indicating that the connection has been severed),
483 * "true" if things are still okay.
484 */
485static bool processIncoming(JdwpState* state) {
486 JdwpNetState* netState = state->netState;
487 int readCount;
488
489 CHECK_GE(netState->clientSock, 0);
490
491 if (!haveFullPacket(netState)) {
492 /* read some more, looping until we have data */
493 errno = 0;
494 while (1) {
495 int selCount;
496 fd_set readfds;
497 int maxfd = -1;
498 int fd;
499
500 FD_ZERO(&readfds);
501
502 /* configure fds; note these may get zapped by another thread */
503 fd = netState->controlSock;
504 if (fd >= 0) {
505 FD_SET(fd, &readfds);
506 if (maxfd < fd) {
507 maxfd = fd;
508 }
509 }
510 fd = netState->clientSock;
511 if (fd >= 0) {
512 FD_SET(fd, &readfds);
513 if (maxfd < fd) {
514 maxfd = fd;
515 }
516 }
517 fd = netState->wakeFds[0];
518 if (fd >= 0) {
519 FD_SET(fd, &readfds);
520 if (maxfd < fd) {
521 maxfd = fd;
522 }
523 } else {
524 LOG(INFO) << "NOTE: entering select w/o wakepipe";
525 }
526
527 if (maxfd < 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800528 VLOG(jdwp) << "+++ all fds are closed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700529 return false;
530 }
531
532 /*
533 * Select blocks until it sees activity on the file descriptors.
534 * Closing the local file descriptor does not count as activity,
535 * so we can't rely on that to wake us up (it works for read()
536 * and accept(), but not select()).
537 *
538 * We can do one of three things: (1) send a signal and catch
539 * EINTR, (2) open an additional fd ("wakePipe") and write to
540 * it when it's time to exit, or (3) time out periodically and
541 * re-issue the select. We're currently using #2, as it's more
542 * reliable than #1 and generally better than #3. Wastes two fds.
543 */
544 selCount = select(maxfd+1, &readfds, NULL, NULL, NULL);
545 if (selCount < 0) {
546 if (errno == EINTR) {
547 continue;
548 }
549 PLOG(ERROR) << "select failed";
550 goto fail;
551 }
552
553 if (netState->wakeFds[0] >= 0 && FD_ISSET(netState->wakeFds[0], &readfds)) {
554 LOG(DEBUG) << "Got wake-up signal, bailing out of select";
555 goto fail;
556 }
557 if (netState->controlSock >= 0 && FD_ISSET(netState->controlSock, &readfds)) {
558 int sock = receiveClientFd(netState);
559 if (sock >= 0) {
560 LOG(INFO) << "Ignoring second debugger -- accepting and dropping";
561 close(sock);
562 } else {
563 CHECK_LT(netState->controlSock, 0);
564 /*
565 * Remote side most likely went away, so our next read
566 * on netState->clientSock will fail and throw us out
567 * of the loop.
568 */
569 }
570 }
571 if (netState->clientSock >= 0 && FD_ISSET(netState->clientSock, &readfds)) {
572 readCount = read(netState->clientSock, netState->inputBuffer + netState->inputCount, sizeof(netState->inputBuffer) - netState->inputCount);
573 if (readCount < 0) {
574 /* read failed */
575 if (errno != EINTR) {
576 goto fail;
577 }
578 LOG(DEBUG) << "+++ EINTR hit";
579 return true;
580 } else if (readCount == 0) {
581 /* EOF hit -- far end went away */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800582 VLOG(jdwp) << "+++ peer disconnected";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700583 goto fail;
584 } else {
585 break;
586 }
587 }
588 }
589
590 netState->inputCount += readCount;
591 if (!haveFullPacket(netState)) {
592 return true; /* still not there yet */
593 }
594 }
595
596 /*
597 * Special-case the initial handshake. For some bizarre reason we're
598 * expected to emulate bad tty settings by echoing the request back
599 * exactly as it was sent. Note the handshake is always initiated by
600 * the debugger, no matter who connects to whom.
601 *
602 * Other than this one case, the protocol [claims to be] stateless.
603 */
604 if (netState->awaitingHandshake) {
605 int cc;
606
607 if (memcmp(netState->inputBuffer, kMagicHandshake, kMagicHandshakeLen) != 0) {
608 LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", netState->inputBuffer);
609 goto fail;
610 }
611
612 errno = 0;
613 cc = write(netState->clientSock, netState->inputBuffer, kMagicHandshakeLen);
614 if (cc != kMagicHandshakeLen) {
615 PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")";
616 goto fail;
617 }
618
619 consumeBytes(netState, kMagicHandshakeLen);
620 netState->awaitingHandshake = false;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800621 VLOG(jdwp) << "+++ handshake complete";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700622 return true;
623 }
624
625 /*
626 * Handle this packet.
627 */
628 return handlePacket(state);
629
630fail:
631 closeConnection(state);
632 return false;
633}
634
635/*
636 * Send a request.
637 *
638 * The entire packet must be sent with a single write() call to avoid
639 * threading issues.
640 *
641 * Returns "true" if it was sent successfully.
642 */
643static bool sendRequest(JdwpState* state, ExpandBuf* pReq) {
644 JdwpNetState* netState = state->netState;
645
646 if (netState->clientSock < 0) {
647 /* can happen with some DDMS events */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800648 VLOG(jdwp) << "NOT sending request -- no debugger is attached";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700649 return false;
650 }
651
652 errno = 0;
653
654 ssize_t cc = netState->writePacket(pReq);
655
656 if (cc != (ssize_t) expandBufGetLength(pReq)) {
657 PLOG(ERROR) << "Failed sending req to debugger (" << cc << " of " << expandBufGetLength(pReq) << ")";
658 return false;
659 }
660
661 return true;
662}
663
664/*
665 * Send a request that was split into multiple buffers.
666 *
667 * The entire packet must be sent with a single writev() call to avoid
668 * threading issues.
669 *
670 * Returns "true" if it was sent successfully.
671 */
Elliott Hughescccd84f2011-12-05 16:51:54 -0800672static bool sendBufferedRequest(JdwpState* state, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700673 JdwpNetState* netState = state->netState;
674
675 if (netState->clientSock < 0) {
676 /* can happen with some DDMS events */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800677 VLOG(jdwp) << "NOT sending request -- no debugger is attached";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700678 return false;
679 }
680
681 size_t expected = 0;
Elliott Hughescccd84f2011-12-05 16:51:54 -0800682 for (int i = 0; i < iov_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700683 expected += iov[i].iov_len;
684 }
685
Elliott Hughescccd84f2011-12-05 16:51:54 -0800686 ssize_t actual = netState->writeBufferedPacket(iov, iov_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700687 if ((size_t)actual != expected) {
688 PLOG(ERROR) << "Failed sending b-req to debugger (" << actual << " of " << expected << ")";
689 return false;
690 }
691
692 return true;
693}
694
695/*
696 * Our functions.
697 */
698static const JdwpTransport adbTransport = {
699 startup,
700 acceptConnection,
701 establishConnection,
702 closeConnection,
703 netShutdown,
704 netFree,
705 isConnected,
706 awaitingHandshake,
707 processIncoming,
708 sendRequest,
709 sendBufferedRequest
710};
711
712/*
713 * Return our set.
714 */
715const JdwpTransport* AndroidAdbTransport() {
716 return &adbTransport;
717}
718
719} // namespace JDWP
720
721} // namespace art