blob: b4062d0a56d027e5f2bb7a83e8505f8f55434d10 [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 * JDWP TCP socket network code.
18 */
19#include "jdwp/jdwp_priv.h"
20#include "jdwp/jdwp_handler.h"
21#include "logging.h"
22#include "stringprintf.h"
23
24#include <stdlib.h>
25#include <unistd.h>
26#include <stdio.h>
27#include <string.h>
28#include <errno.h>
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <netinet/tcp.h>
33#include <arpa/inet.h>
34#include <netdb.h>
35
36#define kBasePort 8000
37#define kMaxPort 8040
38
39#define kInputBufferSize 8192
40
41#define kMagicHandshake "JDWP-Handshake"
42#define kMagicHandshakeLen (sizeof(kMagicHandshake)-1)
43
44namespace art {
45
46namespace JDWP {
47
48// fwd
49static void netShutdown(JdwpNetState* state);
50static void netFree(JdwpNetState* state);
51
52/*
53 * JDWP network state.
54 *
55 * We only talk to one debugger at a time.
56 */
57struct JdwpNetState : public JdwpNetStateBase {
58 short listenPort;
59 int listenSock; /* listen for connection from debugger */
60 int wakePipe[2]; /* break out of select */
61
62 struct in_addr remoteAddr;
63 unsigned short remotePort;
64
65 bool awaitingHandshake; /* waiting for "JDWP-Handshake" */
66
67 /* pending data from the network; would be more efficient as circular buf */
68 unsigned char inputBuffer[kInputBufferSize];
69 int inputCount;
70
71 JdwpNetState()
72 {
73 listenPort = 0;
74 listenSock = -1;
75 wakePipe[0] = -1;
76 wakePipe[1] = -1;
77
78 awaitingHandshake = false;
79
80 inputCount = 0;
81 }
82};
83
84static JdwpNetState* netStartup(short port);
85
86/*
87 * Set up some stuff for transport=dt_socket.
88 */
89static bool prepareSocket(JdwpState* state, const JdwpStartupParams* pParams) {
90 unsigned short port;
91
92 if (pParams->server) {
93 if (pParams->port != 0) {
94 /* try only the specified port */
95 port = pParams->port;
96 state->netState = netStartup(port);
97 } else {
98 /* scan through a range of ports, binding to the first available */
99 for (port = kBasePort; port <= kMaxPort; port++) {
100 state->netState = netStartup(port);
101 if (state->netState != NULL) {
102 break;
103 }
104 }
105 }
106 if (state->netState == NULL) {
107 LOG(ERROR) << "JDWP net startup failed (req port=" << pParams->port << ")";
108 return false;
109 }
110 } else {
111 port = pParams->port; // used in a debug msg later
112 state->netState = netStartup(-1);
113 }
114
115 if (pParams->suspend) {
116 LOG(INFO) << "JDWP will wait for debugger on port " << port;
117 } else {
118 LOG(INFO) << "JDWP will " << (pParams->server ? "listen" : "connect") << " on port " << port;
119 }
120
121 return true;
122}
123
124/*
125 * Are we still waiting for the handshake string?
126 */
127static bool awaitingHandshake(JdwpState* state) {
128 return state->netState->awaitingHandshake;
129}
130
131/*
132 * Initialize JDWP stuff.
133 *
134 * Allocates a new state structure. If "port" is non-negative, this also
135 * tries to bind to a listen port. If "port" is less than zero, we assume
136 * we're preparing for an outbound connection, and return without binding
137 * to anything.
138 *
139 * This may be called several times if we're probing for a port.
140 *
141 * Returns 0 on success.
142 */
143static JdwpNetState* netStartup(short port) {
144 int one = 1;
145 JdwpNetState* netState = new JdwpNetState;
146
147 if (port < 0) {
148 return netState;
149 }
150
151 CHECK_NE(port, 0);
152
153 netState->listenSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
154 if (netState->listenSock < 0) {
155 PLOG(ERROR) << "Socket create failed";
156 goto fail;
157 }
158
159 /* allow immediate re-use */
160 if (setsockopt(netState->listenSock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
161 PLOG(ERROR) << "setsockopt(SO_REUSEADDR) failed";
162 goto fail;
163 }
164
165 union {
166 struct sockaddr_in addrInet;
167 struct sockaddr addrPlain;
168 } addr;
169 addr.addrInet.sin_family = AF_INET;
170 addr.addrInet.sin_port = htons(port);
171 inet_aton("127.0.0.1", &addr.addrInet.sin_addr);
172
173 if (bind(netState->listenSock, &addr.addrPlain, sizeof(addr)) != 0) {
174 PLOG(VERBOSE) << "attempt to bind to port " << port << " failed";
175 goto fail;
176 }
177
178 netState->listenPort = port;
179 LOG(VERBOSE) << "+++ bound to port " << netState->listenPort;
180
181 if (listen(netState->listenSock, 5) != 0) {
182 PLOG(ERROR) << "Listen failed";
183 goto fail;
184 }
185
186 return netState;
187
188fail:
189 netShutdown(netState);
190 netFree(netState);
191 return NULL;
192}
193
194/*
195 * Shut down JDWP listener. Don't free state.
196 *
197 * Note that "netState" may be partially initialized if "startup" failed.
198 *
199 * This may be called from a non-JDWP thread as part of shutting the
200 * JDWP thread down.
201 *
202 * (This is currently called several times during startup as we probe
203 * for an open port.)
204 */
205static void netShutdown(JdwpNetState* netState) {
206 if (netState == NULL) {
207 return;
208 }
209
210 int listenSock = netState->listenSock;
211 int clientSock = netState->clientSock;
212
213 /* clear these out so it doesn't wake up and try to reuse them */
214 netState->listenSock = netState->clientSock = -1;
215
216 /* "shutdown" dislodges blocking read() and accept() calls */
217 if (listenSock >= 0) {
218 shutdown(listenSock, SHUT_RDWR);
219 close(listenSock);
220 }
221 if (clientSock >= 0) {
222 shutdown(clientSock, SHUT_RDWR);
223 close(clientSock);
224 }
225
226 /* if we might be sitting in select, kick us loose */
227 if (netState->wakePipe[1] >= 0) {
228 LOG(VERBOSE) << "+++ writing to wakePipe";
229 (void) write(netState->wakePipe[1], "", 1);
230 }
231}
232
233static void netShutdownExtern(JdwpState* state) {
234 netShutdown(state->netState);
235}
236
237/*
238 * Free JDWP state.
239 *
240 * Call this after shutting the network down with netShutdown().
241 */
242static void netFree(JdwpNetState* netState) {
243 if (netState == NULL) {
244 return;
245 }
246 CHECK_EQ(netState->listenSock, -1);
247 CHECK_EQ(netState->clientSock, -1);
248
249 if (netState->wakePipe[0] >= 0) {
250 close(netState->wakePipe[0]);
251 netState->wakePipe[0] = -1;
252 }
253 if (netState->wakePipe[1] >= 0) {
254 close(netState->wakePipe[1]);
255 netState->wakePipe[1] = -1;
256 }
257
258 delete netState;
259}
260
261static void netFreeExtern(JdwpState* state) {
262 netFree(state->netState);
263}
264
265/*
266 * Returns "true" if we're connected to a debugger.
267 */
268static bool isConnected(JdwpState* state) {
269 return (state->netState != NULL && state->netState->clientSock >= 0);
270}
271
272/*
273 * Returns "true" if the fd is ready, "false" if not.
274 */
275#if 0
276static bool isFdReadable(int sock)
277{
278 fd_set readfds;
279 struct timeval tv;
280 int count;
281
282 FD_ZERO(&readfds);
283 FD_SET(sock, &readfds);
284
285 tv.tv_sec = 0;
286 tv.tv_usec = 0;
287 count = select(sock+1, &readfds, NULL, NULL, &tv);
288 if (count <= 0)
289 return false;
290
291 if (FD_ISSET(sock, &readfds)) /* make sure it's our fd */
292 return true;
293
294 LOG(ERROR) << "WEIRD: odd behavior in select (count=" << count << ")";
295 return false;
296}
297#endif
298
299#if 0
300/*
301 * Check to see if we have a pending connection from the debugger.
302 *
303 * Returns true on success (meaning a connection is available).
304 */
305static bool checkConnection(JdwpState* state) {
306 JdwpNetState* netState = state->netState;
307
308 CHECK_GE(netState->listenSock, 0);
309 /* not expecting to be called when debugger is actively connected */
310 CHECK_LT(netState->clientSock, 0);
311
312 if (!isFdReadable(netState->listenSock))
313 return false;
314 return true;
315}
316#endif
317
318/*
319 * Disable the TCP Nagle algorithm, which delays transmission of outbound
320 * packets until the previous transmissions have been acked. JDWP does a
321 * lot of back-and-forth with small packets, so this may help.
322 */
323static int setNoDelay(int fd)
324{
325 int on = 1;
326 int cc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
327 CHECK_EQ(cc, 0);
328 return cc;
329}
330
331/*
332 * Accept a connection. This will block waiting for somebody to show up.
333 * If that's not desirable, use checkConnection() to make sure something
334 * is pending.
335 */
336static bool acceptConnection(JdwpState* state)
337{
338 JdwpNetState* netState = state->netState;
339 union {
340 struct sockaddr_in addrInet;
341 struct sockaddr addrPlain;
342 } addr;
343 socklen_t addrlen;
344 int sock;
345
346 if (netState->listenSock < 0) {
347 return false; /* you're not listening! */
348 }
349
350 CHECK_LT(netState->clientSock, 0); /* must not already be talking */
351
352 addrlen = sizeof(addr);
353 do {
354 sock = accept(netState->listenSock, &addr.addrPlain, &addrlen);
355 if (sock < 0 && errno != EINTR) {
356 // When we call shutdown() on the socket, accept() returns with
357 // EINVAL. Don't gripe about it.
358 if (errno == EINVAL) {
359 PLOG(VERBOSE) << "accept failed";
360 } else {
361 PLOG(ERROR) << "accept failed";
362 return false;
363 }
364 }
365 } while (sock < 0);
366
367 netState->remoteAddr = addr.addrInet.sin_addr;
368 netState->remotePort = ntohs(addr.addrInet.sin_port);
369 LOG(VERBOSE) << "+++ accepted connection from " << inet_ntoa(netState->remoteAddr) << ":" << netState->remotePort;
370
371 netState->clientSock = sock;
372 netState->awaitingHandshake = true;
373 netState->inputCount = 0;
374
375 LOG(VERBOSE) << "Setting TCP_NODELAY on accepted socket";
376 setNoDelay(netState->clientSock);
377
378 if (pipe(netState->wakePipe) < 0) {
379 PLOG(ERROR) << "pipe failed";
380 return false;
381 }
382
383 return true;
384}
385
386/*
387 * Create a connection to a waiting debugger.
388 */
389static bool establishConnection(JdwpState* state) {
390 union {
391 struct sockaddr_in addrInet;
392 struct sockaddr addrPlain;
393 } addr;
394 struct hostent* pEntry;
395
396 CHECK(state != NULL && state->netState != NULL);
397 CHECK(!state->params.server);
398 CHECK_NE(state->params.host[0], '\0');
399 CHECK_NE(state->params.port, 0);
400
401 /*
402 * Start by resolving the host name.
403 */
404//#undef HAVE_GETHOSTBYNAME_R
405//#warning "forcing non-R"
406#ifdef HAVE_GETHOSTBYNAME_R
407 struct hostent he;
408 char auxBuf[128];
409 int error;
410 int cc = gethostbyname_r(state->params.host, &he, auxBuf, sizeof(auxBuf), &pEntry, &error);
411 if (cc != 0) {
412 LOG(WARNING) << "gethostbyname_r('" << state->params.host << "') failed: " << hstrerror(error);
413 return false;
414 }
415#else
416 h_errno = 0;
417 pEntry = gethostbyname(state->params.host);
418 if (pEntry == NULL) {
419 PLOG(WARNING) << "gethostbyname('" << state->params.host << "') failed";
420 return false;
421 }
422#endif
423
424 /* copy it out ASAP to minimize risk of multithreaded annoyances */
425 memcpy(&addr.addrInet.sin_addr, pEntry->h_addr, pEntry->h_length);
426 addr.addrInet.sin_family = pEntry->h_addrtype;
427
428 addr.addrInet.sin_port = htons(state->params.port);
429
430 LOG(INFO) << "Connecting out to " << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port);
431
432 /*
433 * Create a socket.
434 */
435 JdwpNetState* netState;
436 netState = state->netState;
437 netState->clientSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
438 if (netState->clientSock < 0) {
439 PLOG(ERROR) << "Unable to create socket";
440 return false;
441 }
442
443 /*
444 * Try to connect.
445 */
446 if (connect(netState->clientSock, &addr.addrPlain, sizeof(addr)) != 0) {
447 PLOG(ERROR) << "Unable to connect to " << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port);
448 close(netState->clientSock);
449 netState->clientSock = -1;
450 return false;
451 }
452
453 LOG(INFO) << "Connection established to " << state->params.host << " (" << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port) << ")";
454 netState->awaitingHandshake = true;
455 netState->inputCount = 0;
456
457 setNoDelay(netState->clientSock);
458
459 if (pipe(netState->wakePipe) < 0) {
460 PLOG(ERROR) << "pipe failed";
461 return false;
462 }
463
464 return true;
465}
466
467/*
468 * Close the connection to the debugger.
469 *
470 * Reset the state so we're ready to receive a new connection.
471 */
472static void closeConnection(JdwpState* state) {
473 JdwpNetState* netState;
474
475 CHECK(state != NULL && state->netState != NULL);
476
477 netState = state->netState;
478 if (netState->clientSock < 0) {
479 return;
480 }
481
482 LOG(VERBOSE) << "+++ closed connection to " << inet_ntoa(netState->remoteAddr) << ":" << netState->remotePort;
483
484 close(netState->clientSock);
485 netState->clientSock = -1;
486}
487
488/*
489 * Figure out if we have a full packet in the buffer.
490 */
491static bool haveFullPacket(JdwpNetState* netState) {
492 if (netState->awaitingHandshake) {
493 return (netState->inputCount >= (int) kMagicHandshakeLen);
494 }
495 if (netState->inputCount < 4) {
496 return false;
497 }
498 long length = get4BE(netState->inputBuffer);
499 return (netState->inputCount >= length);
500}
501
502/*
503 * Consume bytes from the buffer.
504 *
505 * This would be more efficient with a circular buffer. However, we're
506 * usually only going to find one packet, which is trivial to handle.
507 */
508static void consumeBytes(JdwpNetState* netState, int count) {
509 CHECK_GT(count, 0);
510 CHECK_LE(count, netState->inputCount);
511
512 if (count == netState->inputCount) {
513 netState->inputCount = 0;
514 return;
515 }
516
517 memmove(netState->inputBuffer, netState->inputBuffer + count, netState->inputCount - count);
518 netState->inputCount -= count;
519}
520
521/*
522 * Dump the contents of a packet to stdout.
523 */
524#if 0
525static void dumpPacket(const unsigned char* packetBuf)
526{
527 const unsigned char* buf = packetBuf;
528 uint32_t length, id;
529 uint8_t flags, cmdSet, cmd;
530 uint16_t error;
531 bool reply;
532 int dataLen;
533
534 cmd = cmdSet = 0xcc;
535
536 length = read4BE(&buf);
537 id = read4BE(&buf);
538 flags = read1(&buf);
539 if ((flags & kJDWPFlagReply) != 0) {
540 reply = true;
541 error = read2BE(&buf);
542 } else {
543 reply = false;
544 cmdSet = read1(&buf);
545 cmd = read1(&buf);
546 }
547
548 dataLen = length - (buf - packetBuf);
549
550 LOG(VERBOSE) << StringPrintf("--- %s: dataLen=%u id=0x%08x flags=0x%02x cmd=%d/%d",
551 reply ? "reply" : "req",
552 dataLen, id, flags, cmdSet, cmd);
553 if (dataLen > 0) {
554 HexDump(buf, dataLen);
555 }
556}
557#endif
558
559/*
560 * Handle a packet. Returns "false" if we encounter a connection-fatal error.
561 */
562static bool handlePacket(JdwpState* state)
563{
564 JdwpNetState* netState = state->netState;
565 const unsigned char* buf = netState->inputBuffer;
566 JdwpReqHeader hdr;
567 uint32_t length, id;
568 uint8_t flags, cmdSet, cmd;
569 uint16_t error;
570 bool reply;
571 int dataLen;
572
573 cmd = cmdSet = 0; // shut up gcc
574
575 /*dumpPacket(netState->inputBuffer);*/
576
577 length = read4BE(&buf);
578 id = read4BE(&buf);
579 flags = read1(&buf);
580 if ((flags & kJDWPFlagReply) != 0) {
581 reply = true;
582 error = read2BE(&buf);
583 } else {
584 reply = false;
585 cmdSet = read1(&buf);
586 cmd = read1(&buf);
587 }
588
589 CHECK_LE((int) length, netState->inputCount);
590 dataLen = length - (buf - netState->inputBuffer);
591
592 if (!reply) {
593 ExpandBuf* pReply = expandBufAlloc();
594
595 hdr.length = length;
596 hdr.id = id;
597 hdr.cmdSet = cmdSet;
598 hdr.cmd = cmd;
599 ProcessRequest(state, &hdr, buf, dataLen, pReply);
600 if (expandBufGetLength(pReply) > 0) {
601 ssize_t cc = netState->writePacket(pReply);
602
603 if (cc != (ssize_t) expandBufGetLength(pReply)) {
604 PLOG(ERROR) << "Failed sending reply to debugger";
605 expandBufFree(pReply);
606 return false;
607 }
608 } else {
609 LOG(WARNING) << "No reply created for set=" << cmdSet << " cmd=" << cmd;
610 }
611 expandBufFree(pReply);
612 } else {
613 LOG(ERROR) << "reply?!";
614 DCHECK(false);
615 }
616
617 LOG(VERBOSE) << "----------";
618
619 consumeBytes(netState, length);
620 return true;
621}
622
623/*
624 * Process incoming data. If no data is available, this will block until
625 * some arrives.
626 *
627 * If we get a full packet, handle it.
628 *
629 * To take some of the mystery out of life, we want to reject incoming
630 * connections if we already have a debugger attached. If we don't, the
631 * debugger will just mysteriously hang until it times out. We could just
632 * close the listen socket, but there's a good chance we won't be able to
633 * bind to the same port again, which would confuse utilities.
634 *
635 * Returns "false" on error (indicating that the connection has been severed),
636 * "true" if things are still okay.
637 */
638static bool processIncoming(JdwpState* state) {
639 JdwpNetState* netState = state->netState;
640 int readCount;
641
642 CHECK_GE(netState->clientSock, 0);
643
644 if (!haveFullPacket(netState)) {
645 /* read some more, looping until we have data */
646 errno = 0;
647 while (1) {
648 int selCount;
649 fd_set readfds;
650 int maxfd;
651 int fd;
652
653 maxfd = netState->listenSock;
654 if (netState->clientSock > maxfd) {
655 maxfd = netState->clientSock;
656 }
657 if (netState->wakePipe[0] > maxfd) {
658 maxfd = netState->wakePipe[0];
659 }
660
661 if (maxfd < 0) {
662 LOG(VERBOSE) << "+++ all fds are closed";
663 return false;
664 }
665
666 FD_ZERO(&readfds);
667
668 /* configure fds; note these may get zapped by another thread */
669 fd = netState->listenSock;
670 if (fd >= 0) {
671 FD_SET(fd, &readfds);
672 }
673 fd = netState->clientSock;
674 if (fd >= 0) {
675 FD_SET(fd, &readfds);
676 }
677 fd = netState->wakePipe[0];
678 if (fd >= 0) {
679 FD_SET(fd, &readfds);
680 } else {
681 LOG(INFO) << "NOTE: entering select w/o wakepipe";
682 }
683
684 /*
685 * Select blocks until it sees activity on the file descriptors.
686 * Closing the local file descriptor does not count as activity,
687 * so we can't rely on that to wake us up (it works for read()
688 * and accept(), but not select()).
689 *
690 * We can do one of three things: (1) send a signal and catch
691 * EINTR, (2) open an additional fd ("wakePipe") and write to
692 * it when it's time to exit, or (3) time out periodically and
693 * re-issue the select. We're currently using #2, as it's more
694 * reliable than #1 and generally better than #3. Wastes two fds.
695 */
696 selCount = select(maxfd+1, &readfds, NULL, NULL, NULL);
697 if (selCount < 0) {
698 if (errno == EINTR) {
699 continue;
700 }
701 PLOG(ERROR) << "select failed";
702 goto fail;
703 }
704
705 if (netState->wakePipe[0] >= 0 && FD_ISSET(netState->wakePipe[0], &readfds)) {
706 if (netState->listenSock >= 0) {
707 LOG(ERROR) << "Exit wake set, but not exiting?";
708 } else {
709 LOG(DEBUG) << "Got wake-up signal, bailing out of select";
710 }
711 goto fail;
712 }
713 if (netState->listenSock >= 0 && FD_ISSET(netState->listenSock, &readfds)) {
714 LOG(INFO) << "Ignoring second debugger -- accepting and dropping";
715 union {
716 struct sockaddr_in addrInet;
717 struct sockaddr addrPlain;
718 } addr;
719 socklen_t addrlen;
720 int tmpSock;
721 tmpSock = accept(netState->listenSock, &addr.addrPlain, &addrlen);
722 if (tmpSock < 0) {
723 LOG(INFO) << "Weird -- accept failed";
724 } else {
725 close(tmpSock);
726 }
727 }
728 if (netState->clientSock >= 0 && FD_ISSET(netState->clientSock, &readfds)) {
729 readCount = read(netState->clientSock, netState->inputBuffer + netState->inputCount, sizeof(netState->inputBuffer) - netState->inputCount);
730 if (readCount < 0) {
731 /* read failed */
732 if (errno != EINTR) {
733 goto fail;
734 }
735 LOG(DEBUG) << "+++ EINTR hit";
736 return true;
737 } else if (readCount == 0) {
738 /* EOF hit -- far end went away */
739 LOG(DEBUG) << "+++ peer disconnected";
740 goto fail;
741 } else {
742 break;
743 }
744 }
745 }
746
747 netState->inputCount += readCount;
748 if (!haveFullPacket(netState)) {
749 return true; /* still not there yet */
750 }
751 }
752
753 /*
754 * Special-case the initial handshake. For some bizarre reason we're
755 * expected to emulate bad tty settings by echoing the request back
756 * exactly as it was sent. Note the handshake is always initiated by
757 * the debugger, no matter who connects to whom.
758 *
759 * Other than this one case, the protocol [claims to be] stateless.
760 */
761 if (netState->awaitingHandshake) {
762 int cc;
763
764 if (memcmp(netState->inputBuffer, kMagicHandshake, kMagicHandshakeLen) != 0) {
765 LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", netState->inputBuffer);
766 goto fail;
767 }
768
769 errno = 0;
770 cc = write(netState->clientSock, netState->inputBuffer, kMagicHandshakeLen);
771 if (cc != kMagicHandshakeLen) {
772 PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")";
773 goto fail;
774 }
775
776 consumeBytes(netState, kMagicHandshakeLen);
777 netState->awaitingHandshake = false;
778 LOG(VERBOSE) << "+++ handshake complete";
779 return true;
780 }
781
782 /*
783 * Handle this packet.
784 */
785 return handlePacket(state);
786
787fail:
788 closeConnection(state);
789 return false;
790}
791
792/*
793 * Send a request.
794 *
795 * The entire packet must be sent with a single write() call to avoid
796 * threading issues.
797 *
798 * Returns "true" if it was sent successfully.
799 */
800static bool sendRequest(JdwpState* state, ExpandBuf* pReq) {
801 JdwpNetState* netState = state->netState;
802
803 /*dumpPacket(expandBufGetBuffer(pReq));*/
804 if (netState->clientSock < 0) {
805 /* can happen with some DDMS events */
806 LOG(VERBOSE) << "NOT sending request -- no debugger is attached";
807 return false;
808 }
809
810 errno = 0;
811 ssize_t cc = netState->writePacket(pReq);
812
813 if (cc != (ssize_t) expandBufGetLength(pReq)) {
814 PLOG(ERROR) << "Failed sending req to debugger (" << cc << " of " << expandBufGetLength(pReq) << ")";
815 return false;
816 }
817
818 return true;
819}
820
821/*
822 * Send a request that was split into multiple buffers.
823 *
824 * The entire packet must be sent with a single writev() call to avoid
825 * threading issues.
826 *
827 * Returns "true" if it was sent successfully.
828 */
829static bool sendBufferedRequest(JdwpState* state, const iovec* iov, int iovcnt) {
830 JdwpNetState* netState = state->netState;
831
832 if (netState->clientSock < 0) {
833 /* can happen with some DDMS events */
834 LOG(VERBOSE) << "NOT sending request -- no debugger is attached";
835 return false;
836 }
837
838 size_t expected = 0;
839 for (int i = 0; i < iovcnt; i++) {
840 expected += iov[i].iov_len;
841 }
842
843 ssize_t actual = netState->writeBufferedPacket(iov, iovcnt);
844
845 if ((size_t)actual != expected) {
846 PLOG(ERROR) << "Failed sending b-req to debugger (" << actual << " of " << expected << ")";
847 return false;
848 }
849
850 return true;
851}
852
853/*
854 * Our functions.
855 *
856 * We can't generally share the implementations with other transports,
857 * even if they're also socket-based, because our JdwpNetState will be
858 * different from theirs.
859 */
860static const JdwpTransport socketTransport = {
861 prepareSocket,
862 acceptConnection,
863 establishConnection,
864 closeConnection,
865 netShutdownExtern,
866 netFreeExtern,
867 isConnected,
868 awaitingHandshake,
869 processIncoming,
870 sendRequest,
871 sendBufferedRequest,
872};
873
874/*
875 * Return our set.
876 */
877const JdwpTransport* SocketTransport() {
878 return &socketTransport;
879}
880
881} // namespace JDWP
882
883} // namespace art