blob: 6bc78209b186a03d14f8c206eadcfb525c7df937 [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
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;
416 uint16_t error;
417 bool reply;
418 int dataLen;
419
420 cmd = cmdSet = 0; // shut up gcc
421
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700422 length = Read4BE(&buf);
423 id = Read4BE(&buf);
424 flags = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700425 if ((flags & kJDWPFlagReply) != 0) {
426 reply = true;
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700427 error = Read2BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700428 } else {
429 reply = false;
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700430 cmdSet = Read1(&buf);
431 cmd = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700432 }
433
Elliott Hughes74847412012-06-20 18:10:21 -0700434 CHECK_LE(length, netState->inputCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700435 dataLen = length - (buf - netState->inputBuffer);
436
437 if (!reply) {
438 ExpandBuf* pReply = expandBufAlloc();
439
440 hdr.length = length;
441 hdr.id = id;
442 hdr.cmdSet = cmdSet;
443 hdr.cmd = cmd;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700444 state->ProcessRequest(&hdr, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700445 if (expandBufGetLength(pReply) > 0) {
446 ssize_t cc = netState->writePacket(pReply);
447
448 if (cc != (ssize_t) expandBufGetLength(pReply)) {
449 PLOG(ERROR) << "Failed sending reply to debugger";
450 expandBufFree(pReply);
451 return false;
452 }
453 } else {
454 LOG(WARNING) << "No reply created for set=" << cmdSet << " cmd=" << cmd;
455 }
456 expandBufFree(pReply);
457 } else {
458 LOG(FATAL) << "reply?!";
459 }
460
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800461 VLOG(jdwp) << "----------";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700462
463 consumeBytes(netState, length);
464 return true;
465}
466
467/*
468 * Process incoming data. If no data is available, this will block until
469 * some arrives.
470 *
471 * If we get a full packet, handle it.
472 *
473 * To take some of the mystery out of life, we want to reject incoming
474 * connections if we already have a debugger attached. If we don't, the
475 * debugger will just mysteriously hang until it times out. We could just
476 * close the listen socket, but there's a good chance we won't be able to
477 * bind to the same port again, which would confuse utilities.
478 *
479 * Returns "false" on error (indicating that the connection has been severed),
480 * "true" if things are still okay.
481 */
482static bool processIncoming(JdwpState* state) {
483 JdwpNetState* netState = state->netState;
484 int readCount;
485
486 CHECK_GE(netState->clientSock, 0);
487
488 if (!haveFullPacket(netState)) {
489 /* read some more, looping until we have data */
490 errno = 0;
491 while (1) {
492 int selCount;
493 fd_set readfds;
494 int maxfd = -1;
495 int fd;
496
497 FD_ZERO(&readfds);
498
499 /* configure fds; note these may get zapped by another thread */
500 fd = netState->controlSock;
501 if (fd >= 0) {
502 FD_SET(fd, &readfds);
503 if (maxfd < fd) {
504 maxfd = fd;
505 }
506 }
507 fd = netState->clientSock;
508 if (fd >= 0) {
509 FD_SET(fd, &readfds);
510 if (maxfd < fd) {
511 maxfd = fd;
512 }
513 }
514 fd = netState->wakeFds[0];
515 if (fd >= 0) {
516 FD_SET(fd, &readfds);
517 if (maxfd < fd) {
518 maxfd = fd;
519 }
520 } else {
521 LOG(INFO) << "NOTE: entering select w/o wakepipe";
522 }
523
524 if (maxfd < 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800525 VLOG(jdwp) << "+++ all fds are closed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700526 return false;
527 }
528
529 /*
530 * Select blocks until it sees activity on the file descriptors.
531 * Closing the local file descriptor does not count as activity,
532 * so we can't rely on that to wake us up (it works for read()
533 * and accept(), but not select()).
534 *
535 * We can do one of three things: (1) send a signal and catch
536 * EINTR, (2) open an additional fd ("wakePipe") and write to
537 * it when it's time to exit, or (3) time out periodically and
538 * re-issue the select. We're currently using #2, as it's more
539 * reliable than #1 and generally better than #3. Wastes two fds.
540 */
541 selCount = select(maxfd+1, &readfds, NULL, NULL, NULL);
542 if (selCount < 0) {
543 if (errno == EINTR) {
544 continue;
545 }
546 PLOG(ERROR) << "select failed";
547 goto fail;
548 }
549
550 if (netState->wakeFds[0] >= 0 && FD_ISSET(netState->wakeFds[0], &readfds)) {
551 LOG(DEBUG) << "Got wake-up signal, bailing out of select";
552 goto fail;
553 }
554 if (netState->controlSock >= 0 && FD_ISSET(netState->controlSock, &readfds)) {
555 int sock = receiveClientFd(netState);
556 if (sock >= 0) {
557 LOG(INFO) << "Ignoring second debugger -- accepting and dropping";
558 close(sock);
559 } else {
560 CHECK_LT(netState->controlSock, 0);
561 /*
562 * Remote side most likely went away, so our next read
563 * on netState->clientSock will fail and throw us out
564 * of the loop.
565 */
566 }
567 }
568 if (netState->clientSock >= 0 && FD_ISSET(netState->clientSock, &readfds)) {
569 readCount = read(netState->clientSock, netState->inputBuffer + netState->inputCount, sizeof(netState->inputBuffer) - netState->inputCount);
570 if (readCount < 0) {
571 /* read failed */
572 if (errno != EINTR) {
573 goto fail;
574 }
575 LOG(DEBUG) << "+++ EINTR hit";
576 return true;
577 } else if (readCount == 0) {
578 /* EOF hit -- far end went away */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800579 VLOG(jdwp) << "+++ peer disconnected";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700580 goto fail;
581 } else {
582 break;
583 }
584 }
585 }
586
587 netState->inputCount += readCount;
588 if (!haveFullPacket(netState)) {
589 return true; /* still not there yet */
590 }
591 }
592
593 /*
594 * Special-case the initial handshake. For some bizarre reason we're
595 * expected to emulate bad tty settings by echoing the request back
596 * exactly as it was sent. Note the handshake is always initiated by
597 * the debugger, no matter who connects to whom.
598 *
599 * Other than this one case, the protocol [claims to be] stateless.
600 */
601 if (netState->awaitingHandshake) {
602 int cc;
603
604 if (memcmp(netState->inputBuffer, kMagicHandshake, kMagicHandshakeLen) != 0) {
605 LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", netState->inputBuffer);
606 goto fail;
607 }
608
609 errno = 0;
610 cc = write(netState->clientSock, netState->inputBuffer, kMagicHandshakeLen);
611 if (cc != kMagicHandshakeLen) {
612 PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")";
613 goto fail;
614 }
615
616 consumeBytes(netState, kMagicHandshakeLen);
617 netState->awaitingHandshake = false;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800618 VLOG(jdwp) << "+++ handshake complete";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700619 return true;
620 }
621
622 /*
623 * Handle this packet.
624 */
625 return handlePacket(state);
626
Elliott Hughesa21039c2012-06-21 12:09:25 -0700627 fail:
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700628 closeConnection(state);
629 return false;
630}
631
632/*
633 * Send a request.
634 *
635 * The entire packet must be sent with a single write() call to avoid
636 * threading issues.
637 *
638 * Returns "true" if it was sent successfully.
639 */
640static bool sendRequest(JdwpState* state, ExpandBuf* pReq) {
641 JdwpNetState* netState = state->netState;
642
643 if (netState->clientSock < 0) {
644 /* can happen with some DDMS events */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800645 VLOG(jdwp) << "NOT sending request -- no debugger is attached";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700646 return false;
647 }
648
649 errno = 0;
650
651 ssize_t cc = netState->writePacket(pReq);
652
653 if (cc != (ssize_t) expandBufGetLength(pReq)) {
654 PLOG(ERROR) << "Failed sending req to debugger (" << cc << " of " << expandBufGetLength(pReq) << ")";
655 return false;
656 }
657
658 return true;
659}
660
661/*
662 * Send a request that was split into multiple buffers.
663 *
664 * The entire packet must be sent with a single writev() call to avoid
665 * threading issues.
666 *
667 * Returns "true" if it was sent successfully.
668 */
Elliott Hughescccd84f2011-12-05 16:51:54 -0800669static bool sendBufferedRequest(JdwpState* state, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700670 JdwpNetState* netState = state->netState;
671
672 if (netState->clientSock < 0) {
673 /* can happen with some DDMS events */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800674 VLOG(jdwp) << "NOT sending request -- no debugger is attached";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700675 return false;
676 }
677
678 size_t expected = 0;
Elliott Hughescccd84f2011-12-05 16:51:54 -0800679 for (int i = 0; i < iov_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700680 expected += iov[i].iov_len;
681 }
682
Elliott Hughescccd84f2011-12-05 16:51:54 -0800683 ssize_t actual = netState->writeBufferedPacket(iov, iov_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700684 if ((size_t)actual != expected) {
685 PLOG(ERROR) << "Failed sending b-req to debugger (" << actual << " of " << expected << ")";
686 return false;
687 }
688
689 return true;
690}
691
692/*
693 * Our functions.
694 */
695static const JdwpTransport adbTransport = {
696 startup,
697 acceptConnection,
698 establishConnection,
699 closeConnection,
700 netShutdown,
701 netFree,
702 isConnected,
703 awaitingHandshake,
704 processIncoming,
705 sendRequest,
706 sendBufferedRequest
707};
708
709/*
710 * Return our set.
711 */
712const JdwpTransport* AndroidAdbTransport() {
713 return &adbTransport;
714}
715
716} // namespace JDWP
717
718} // namespace art