blob: 6f6fbe59ed2139c6e1146a3b17e269946c4e39fb [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 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070016
Elliott Hughes07ed66b2012-12-12 18:34:25 -080017#include <arpa/inet.h>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070018#include <errno.h>
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include <netdb.h>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070020#include <netinet/in.h>
21#include <netinet/tcp.h>
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/socket.h>
26#include <sys/types.h>
27#include <unistd.h>
28
29#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080030#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080031#include "jdwp/jdwp_handler.h"
32#include "jdwp/jdwp_priv.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070033
34#define kBasePort 8000
35#define kMaxPort 8040
36
37#define kInputBufferSize 8192
38
Elliott Hughes872d4ec2011-10-21 17:07:15 -070039namespace art {
40
41namespace JDWP {
42
43// fwd
44static void netShutdown(JdwpNetState* state);
45static void netFree(JdwpNetState* state);
46
47/*
48 * JDWP network state.
49 *
50 * We only talk to one debugger at a time.
51 */
52struct JdwpNetState : public JdwpNetStateBase {
Elliott Hughes74847412012-06-20 18:10:21 -070053 uint16_t listenPort;
54 int listenSock; /* listen for connection from debugger */
55 int wakePipe[2]; /* break out of select */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070056
Elliott Hughes74847412012-06-20 18:10:21 -070057 in_addr remoteAddr;
58 uint16_t remotePort;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070059
Elliott Hughes74847412012-06-20 18:10:21 -070060 bool awaitingHandshake; /* waiting for "JDWP-Handshake" */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070061
Elliott Hughes74847412012-06-20 18:10:21 -070062 /* pending data from the network; would be more efficient as circular buf */
63 unsigned char inputBuffer[kInputBufferSize];
64 size_t inputCount;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070065
Elliott Hughes74847412012-06-20 18:10:21 -070066 JdwpNetState() {
67 listenPort = 0;
68 listenSock = -1;
69 wakePipe[0] = -1;
70 wakePipe[1] = -1;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070071
Elliott Hughes74847412012-06-20 18:10:21 -070072 awaitingHandshake = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070073
Elliott Hughes74847412012-06-20 18:10:21 -070074 inputCount = 0;
75 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -070076};
77
Elliott Hughes6d8dd472012-01-17 18:27:41 -080078static JdwpNetState* netStartup(uint16_t port, bool probe);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070079
80/*
81 * Set up some stuff for transport=dt_socket.
82 */
Elliott Hughes376a7a02011-10-24 18:35:55 -070083static bool prepareSocket(JdwpState* state, const JdwpOptions* options) {
Elliott Hughes6d8dd472012-01-17 18:27:41 -080084 uint16_t port = options->port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070085
Elliott Hughes376a7a02011-10-24 18:35:55 -070086 if (options->server) {
87 if (options->port != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070088 /* try only the specified port */
Elliott Hughes3d30d9b2011-12-07 17:35:48 -080089 state->netState = netStartup(port, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070090 } else {
91 /* scan through a range of ports, binding to the first available */
92 for (port = kBasePort; port <= kMaxPort; port++) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -080093 state->netState = netStartup(port, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070094 if (state->netState != NULL) {
95 break;
96 }
97 }
98 }
99 if (state->netState == NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700100 LOG(ERROR) << "JDWP net startup failed (req port=" << options->port << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700101 return false;
102 }
103 } else {
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800104 state->netState = netStartup(0, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700105 }
106
Elliott Hughes376a7a02011-10-24 18:35:55 -0700107 if (options->suspend) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700108 LOG(INFO) << "JDWP will wait for debugger on port " << port;
109 } else {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700110 LOG(INFO) << "JDWP will " << (options->server ? "listen" : "connect") << " on port " << port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700111 }
112
113 return true;
114}
115
116/*
117 * Are we still waiting for the handshake string?
118 */
119static bool awaitingHandshake(JdwpState* state) {
120 return state->netState->awaitingHandshake;
121}
122
123/*
124 * Initialize JDWP stuff.
125 *
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800126 * Allocates a new state structure. If "port" is non-zero, this also
127 * tries to bind to a listen port. If "port" is zero, we assume
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700128 * we're preparing for an outbound connection, and return without binding
129 * to anything.
130 *
131 * This may be called several times if we're probing for a port.
132 *
133 * Returns 0 on success.
134 */
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800135static JdwpNetState* netStartup(uint16_t port, bool probe) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700136 JdwpNetState* netState = new JdwpNetState;
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800137 if (port == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700138 return netState;
139 }
140
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700141 netState->listenSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
142 if (netState->listenSock < 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800143 PLOG(probe ? ERROR : FATAL) << "Socket create failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700144 goto fail;
145 }
146
147 /* allow immediate re-use */
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800148 {
149 int one = 1;
150 if (setsockopt(netState->listenSock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
151 PLOG(probe ? ERROR : FATAL) << "setsockopt(SO_REUSEADDR) failed";
152 goto fail;
153 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700154 }
155
156 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700157 sockaddr_in addrInet;
158 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700159 } addr;
160 addr.addrInet.sin_family = AF_INET;
161 addr.addrInet.sin_port = htons(port);
162 inet_aton("127.0.0.1", &addr.addrInet.sin_addr);
163
164 if (bind(netState->listenSock, &addr.addrPlain, sizeof(addr)) != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800165 PLOG(probe ? ERROR : FATAL) << "Attempt to bind to port " << port << " failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700166 goto fail;
167 }
168
169 netState->listenPort = port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700170
171 if (listen(netState->listenSock, 5) != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800172 PLOG(probe ? ERROR : FATAL) << "Listen failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700173 goto fail;
174 }
175
176 return netState;
177
Elliott Hughesa21039c2012-06-21 12:09:25 -0700178 fail:
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179 netShutdown(netState);
180 netFree(netState);
181 return NULL;
182}
183
184/*
185 * Shut down JDWP listener. Don't free state.
186 *
187 * Note that "netState" may be partially initialized if "startup" failed.
188 *
189 * This may be called from a non-JDWP thread as part of shutting the
190 * JDWP thread down.
191 *
192 * (This is currently called several times during startup as we probe
193 * for an open port.)
194 */
195static void netShutdown(JdwpNetState* netState) {
196 if (netState == NULL) {
197 return;
198 }
199
200 int listenSock = netState->listenSock;
201 int clientSock = netState->clientSock;
202
203 /* clear these out so it doesn't wake up and try to reuse them */
204 netState->listenSock = netState->clientSock = -1;
205
206 /* "shutdown" dislodges blocking read() and accept() calls */
207 if (listenSock >= 0) {
208 shutdown(listenSock, SHUT_RDWR);
209 close(listenSock);
210 }
211 if (clientSock >= 0) {
212 shutdown(clientSock, SHUT_RDWR);
213 close(clientSock);
214 }
215
216 /* if we might be sitting in select, kick us loose */
217 if (netState->wakePipe[1] >= 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800218 VLOG(jdwp) << "+++ writing to wakePipe";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700219 (void) write(netState->wakePipe[1], "", 1);
220 }
221}
222
223static void netShutdownExtern(JdwpState* state) {
224 netShutdown(state->netState);
225}
226
227/*
228 * Free JDWP state.
229 *
230 * Call this after shutting the network down with netShutdown().
231 */
232static void netFree(JdwpNetState* netState) {
233 if (netState == NULL) {
234 return;
235 }
236 CHECK_EQ(netState->listenSock, -1);
237 CHECK_EQ(netState->clientSock, -1);
238
239 if (netState->wakePipe[0] >= 0) {
240 close(netState->wakePipe[0]);
241 netState->wakePipe[0] = -1;
242 }
243 if (netState->wakePipe[1] >= 0) {
244 close(netState->wakePipe[1]);
245 netState->wakePipe[1] = -1;
246 }
247
248 delete netState;
249}
250
251static void netFreeExtern(JdwpState* state) {
252 netFree(state->netState);
253}
254
255/*
256 * Returns "true" if we're connected to a debugger.
257 */
258static bool isConnected(JdwpState* state) {
259 return (state->netState != NULL && state->netState->clientSock >= 0);
260}
261
262/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700263 * Disable the TCP Nagle algorithm, which delays transmission of outbound
264 * packets until the previous transmissions have been acked. JDWP does a
265 * lot of back-and-forth with small packets, so this may help.
266 */
Elliott Hughes74847412012-06-20 18:10:21 -0700267static int setNoDelay(int fd) {
268 int on = 1;
269 int cc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
270 CHECK_EQ(cc, 0);
271 return cc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700272}
273
274/*
275 * Accept a connection. This will block waiting for somebody to show up.
276 * If that's not desirable, use checkConnection() to make sure something
277 * is pending.
278 */
Elliott Hughes74847412012-06-20 18:10:21 -0700279static bool acceptConnection(JdwpState* state) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700280 JdwpNetState* netState = state->netState;
281 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700282 sockaddr_in addrInet;
283 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700284 } addr;
285 socklen_t addrlen;
286 int sock;
287
288 if (netState->listenSock < 0) {
289 return false; /* you're not listening! */
290 }
291
292 CHECK_LT(netState->clientSock, 0); /* must not already be talking */
293
294 addrlen = sizeof(addr);
295 do {
296 sock = accept(netState->listenSock, &addr.addrPlain, &addrlen);
297 if (sock < 0 && errno != EINTR) {
298 // When we call shutdown() on the socket, accept() returns with
299 // EINVAL. Don't gripe about it.
300 if (errno == EINVAL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800301 if (VLOG_IS_ON(jdwp)) {
302 PLOG(ERROR) << "accept failed";
303 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700304 } else {
305 PLOG(ERROR) << "accept failed";
306 return false;
307 }
308 }
309 } while (sock < 0);
310
311 netState->remoteAddr = addr.addrInet.sin_addr;
312 netState->remotePort = ntohs(addr.addrInet.sin_port);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800313 VLOG(jdwp) << "+++ accepted connection from " << inet_ntoa(netState->remoteAddr) << ":" << netState->remotePort;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700314
315 netState->clientSock = sock;
316 netState->awaitingHandshake = true;
317 netState->inputCount = 0;
318
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800319 VLOG(jdwp) << "Setting TCP_NODELAY on accepted socket";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700320 setNoDelay(netState->clientSock);
321
322 if (pipe(netState->wakePipe) < 0) {
323 PLOG(ERROR) << "pipe failed";
324 return false;
325 }
326
327 return true;
328}
329
330/*
331 * Create a connection to a waiting debugger.
332 */
Elliott Hughesa21039c2012-06-21 12:09:25 -0700333static bool establishConnection(JdwpState* state, const JdwpOptions* options) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700334 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700335 sockaddr_in addrInet;
336 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700337 } addr;
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700338 hostent* pEntry;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700339
340 CHECK(state != NULL && state->netState != NULL);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700341 CHECK(!options->server);
342 CHECK(!options->host.empty());
343 CHECK_NE(options->port, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700344
345 /*
346 * Start by resolving the host name.
347 */
348//#undef HAVE_GETHOSTBYNAME_R
349//#warning "forcing non-R"
350#ifdef HAVE_GETHOSTBYNAME_R
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700351 hostent he;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700352 char auxBuf[128];
353 int error;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700354 int cc = gethostbyname_r(options->host.c_str(), &he, auxBuf, sizeof(auxBuf), &pEntry, &error);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700355 if (cc != 0) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700356 LOG(WARNING) << "gethostbyname_r('" << options->host << "') failed: " << hstrerror(error);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700357 return false;
358 }
359#else
360 h_errno = 0;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700361 pEntry = gethostbyname(options->host.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700362 if (pEntry == NULL) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700363 PLOG(WARNING) << "gethostbyname('" << options->host << "') failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700364 return false;
365 }
366#endif
367
368 /* copy it out ASAP to minimize risk of multithreaded annoyances */
369 memcpy(&addr.addrInet.sin_addr, pEntry->h_addr, pEntry->h_length);
370 addr.addrInet.sin_family = pEntry->h_addrtype;
371
Elliott Hughesa21039c2012-06-21 12:09:25 -0700372 addr.addrInet.sin_port = htons(options->port);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700373
374 LOG(INFO) << "Connecting out to " << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port);
375
376 /*
377 * Create a socket.
378 */
379 JdwpNetState* netState;
380 netState = state->netState;
381 netState->clientSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
382 if (netState->clientSock < 0) {
383 PLOG(ERROR) << "Unable to create socket";
384 return false;
385 }
386
387 /*
388 * Try to connect.
389 */
390 if (connect(netState->clientSock, &addr.addrPlain, sizeof(addr)) != 0) {
391 PLOG(ERROR) << "Unable to connect to " << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port);
392 close(netState->clientSock);
393 netState->clientSock = -1;
394 return false;
395 }
396
Elliott Hughesa21039c2012-06-21 12:09:25 -0700397 LOG(INFO) << "Connection established to " << options->host << " (" << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port) << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700398 netState->awaitingHandshake = true;
399 netState->inputCount = 0;
400
401 setNoDelay(netState->clientSock);
402
403 if (pipe(netState->wakePipe) < 0) {
404 PLOG(ERROR) << "pipe failed";
405 return false;
406 }
407
408 return true;
409}
410
411/*
412 * Close the connection to the debugger.
413 *
414 * Reset the state so we're ready to receive a new connection.
415 */
416static void closeConnection(JdwpState* state) {
417 JdwpNetState* netState;
418
419 CHECK(state != NULL && state->netState != NULL);
420
421 netState = state->netState;
422 if (netState->clientSock < 0) {
423 return;
424 }
425
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800426 VLOG(jdwp) << "+++ closed connection to " << inet_ntoa(netState->remoteAddr) << ":" << netState->remotePort;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700427
428 close(netState->clientSock);
429 netState->clientSock = -1;
430}
431
432/*
433 * Figure out if we have a full packet in the buffer.
434 */
435static bool haveFullPacket(JdwpNetState* netState) {
436 if (netState->awaitingHandshake) {
Elliott Hughes74847412012-06-20 18:10:21 -0700437 return (netState->inputCount >= kMagicHandshakeLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700438 }
439 if (netState->inputCount < 4) {
440 return false;
441 }
Elliott Hughes74847412012-06-20 18:10:21 -0700442 uint32_t length = Get4BE(netState->inputBuffer);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700443 return (netState->inputCount >= length);
444}
445
446/*
447 * Consume bytes from the buffer.
448 *
449 * This would be more efficient with a circular buffer. However, we're
450 * usually only going to find one packet, which is trivial to handle.
451 */
Elliott Hughes74847412012-06-20 18:10:21 -0700452static void consumeBytes(JdwpNetState* netState, size_t count) {
453 CHECK_GT(count, 0U);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700454 CHECK_LE(count, netState->inputCount);
455
456 if (count == netState->inputCount) {
457 netState->inputCount = 0;
458 return;
459 }
460
461 memmove(netState->inputBuffer, netState->inputBuffer + count, netState->inputCount - count);
462 netState->inputCount -= count;
463}
464
465/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700466 * Handle a packet. Returns "false" if we encounter a connection-fatal error.
467 */
Elliott Hughes376a7a02011-10-24 18:35:55 -0700468static bool handlePacket(JdwpState* state) {
469 JdwpNetState* netState = state->netState;
470 const unsigned char* buf = netState->inputBuffer;
Elliott Hughes74847412012-06-20 18:10:21 -0700471 uint8_t cmdSet, cmd;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700472
Elliott Hughes376a7a02011-10-24 18:35:55 -0700473 cmd = cmdSet = 0; // shut up gcc
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700474
Elliott Hughes74847412012-06-20 18:10:21 -0700475 uint32_t length = Read4BE(&buf);
476 uint32_t id = Read4BE(&buf);
477 int8_t flags = Read1(&buf);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700478 if ((flags & kJDWPFlagReply) != 0) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800479 LOG(FATAL) << "reply?!";
Elliott Hughes376a7a02011-10-24 18:35:55 -0700480 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700481 cmdSet = Read1(&buf);
482 cmd = Read1(&buf);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700483 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700484
Elliott Hughes74847412012-06-20 18:10:21 -0700485 CHECK_LE(length, netState->inputCount);
486 int dataLen = length - (buf - netState->inputBuffer);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700487
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800488 ExpandBuf* pReply = expandBufAlloc();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700489
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800490 JdwpReqHeader hdr;
491 hdr.length = length;
492 hdr.id = id;
493 hdr.cmdSet = cmdSet;
494 hdr.cmd = cmd;
495 state->ProcessRequest(&hdr, buf, dataLen, pReply);
496 if (expandBufGetLength(pReply) > 0) {
497 ssize_t cc = netState->writePacket(pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700498
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800499 if (cc != (ssize_t) expandBufGetLength(pReply)) {
500 PLOG(ERROR) << "Failed sending reply to debugger";
501 expandBufFree(pReply);
502 return false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700503 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700504 } else {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800505 LOG(WARNING) << "No reply created for set=" << cmdSet << " cmd=" << cmd;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700506 }
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800507 expandBufFree(pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700508
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800509 VLOG(jdwp) << "----------";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700510
Elliott Hughes376a7a02011-10-24 18:35:55 -0700511 consumeBytes(netState, length);
512 return true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700513}
514
515/*
516 * Process incoming data. If no data is available, this will block until
517 * some arrives.
518 *
519 * If we get a full packet, handle it.
520 *
521 * To take some of the mystery out of life, we want to reject incoming
522 * connections if we already have a debugger attached. If we don't, the
523 * debugger will just mysteriously hang until it times out. We could just
524 * close the listen socket, but there's a good chance we won't be able to
525 * bind to the same port again, which would confuse utilities.
526 *
527 * Returns "false" on error (indicating that the connection has been severed),
528 * "true" if things are still okay.
529 */
530static bool processIncoming(JdwpState* state) {
531 JdwpNetState* netState = state->netState;
532 int readCount;
533
534 CHECK_GE(netState->clientSock, 0);
535
536 if (!haveFullPacket(netState)) {
537 /* read some more, looping until we have data */
538 errno = 0;
539 while (1) {
540 int selCount;
541 fd_set readfds;
542 int maxfd;
543 int fd;
544
545 maxfd = netState->listenSock;
546 if (netState->clientSock > maxfd) {
547 maxfd = netState->clientSock;
548 }
549 if (netState->wakePipe[0] > maxfd) {
550 maxfd = netState->wakePipe[0];
551 }
552
553 if (maxfd < 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800554 VLOG(jdwp) << "+++ all fds are closed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700555 return false;
556 }
557
558 FD_ZERO(&readfds);
559
560 /* configure fds; note these may get zapped by another thread */
561 fd = netState->listenSock;
562 if (fd >= 0) {
563 FD_SET(fd, &readfds);
564 }
565 fd = netState->clientSock;
566 if (fd >= 0) {
567 FD_SET(fd, &readfds);
568 }
569 fd = netState->wakePipe[0];
570 if (fd >= 0) {
571 FD_SET(fd, &readfds);
572 } else {
573 LOG(INFO) << "NOTE: entering select w/o wakepipe";
574 }
575
576 /*
577 * Select blocks until it sees activity on the file descriptors.
578 * Closing the local file descriptor does not count as activity,
579 * so we can't rely on that to wake us up (it works for read()
580 * and accept(), but not select()).
581 *
582 * We can do one of three things: (1) send a signal and catch
583 * EINTR, (2) open an additional fd ("wakePipe") and write to
584 * it when it's time to exit, or (3) time out periodically and
585 * re-issue the select. We're currently using #2, as it's more
586 * reliable than #1 and generally better than #3. Wastes two fds.
587 */
588 selCount = select(maxfd+1, &readfds, NULL, NULL, NULL);
589 if (selCount < 0) {
590 if (errno == EINTR) {
591 continue;
592 }
593 PLOG(ERROR) << "select failed";
594 goto fail;
595 }
596
597 if (netState->wakePipe[0] >= 0 && FD_ISSET(netState->wakePipe[0], &readfds)) {
598 if (netState->listenSock >= 0) {
599 LOG(ERROR) << "Exit wake set, but not exiting?";
600 } else {
601 LOG(DEBUG) << "Got wake-up signal, bailing out of select";
602 }
603 goto fail;
604 }
605 if (netState->listenSock >= 0 && FD_ISSET(netState->listenSock, &readfds)) {
606 LOG(INFO) << "Ignoring second debugger -- accepting and dropping";
607 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700608 sockaddr_in addrInet;
609 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700610 } addr;
611 socklen_t addrlen;
612 int tmpSock;
613 tmpSock = accept(netState->listenSock, &addr.addrPlain, &addrlen);
614 if (tmpSock < 0) {
615 LOG(INFO) << "Weird -- accept failed";
616 } else {
617 close(tmpSock);
618 }
619 }
620 if (netState->clientSock >= 0 && FD_ISSET(netState->clientSock, &readfds)) {
621 readCount = read(netState->clientSock, netState->inputBuffer + netState->inputCount, sizeof(netState->inputBuffer) - netState->inputCount);
622 if (readCount < 0) {
623 /* read failed */
624 if (errno != EINTR) {
625 goto fail;
626 }
627 LOG(DEBUG) << "+++ EINTR hit";
628 return true;
629 } else if (readCount == 0) {
630 /* EOF hit -- far end went away */
631 LOG(DEBUG) << "+++ peer disconnected";
632 goto fail;
633 } else {
634 break;
635 }
636 }
637 }
638
639 netState->inputCount += readCount;
640 if (!haveFullPacket(netState)) {
641 return true; /* still not there yet */
642 }
643 }
644
645 /*
646 * Special-case the initial handshake. For some bizarre reason we're
647 * expected to emulate bad tty settings by echoing the request back
648 * exactly as it was sent. Note the handshake is always initiated by
649 * the debugger, no matter who connects to whom.
650 *
651 * Other than this one case, the protocol [claims to be] stateless.
652 */
653 if (netState->awaitingHandshake) {
654 int cc;
655
656 if (memcmp(netState->inputBuffer, kMagicHandshake, kMagicHandshakeLen) != 0) {
657 LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", netState->inputBuffer);
658 goto fail;
659 }
660
661 errno = 0;
662 cc = write(netState->clientSock, netState->inputBuffer, kMagicHandshakeLen);
663 if (cc != kMagicHandshakeLen) {
664 PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")";
665 goto fail;
666 }
667
668 consumeBytes(netState, kMagicHandshakeLen);
669 netState->awaitingHandshake = false;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800670 VLOG(jdwp) << "+++ handshake complete";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700671 return true;
672 }
673
674 /*
675 * Handle this packet.
676 */
677 return handlePacket(state);
678
Elliott Hughesa21039c2012-06-21 12:09:25 -0700679 fail:
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700680 closeConnection(state);
681 return false;
682}
683
684/*
685 * Send a request.
686 *
687 * The entire packet must be sent with a single write() call to avoid
688 * threading issues.
689 *
690 * Returns "true" if it was sent successfully.
691 */
692static bool sendRequest(JdwpState* state, ExpandBuf* pReq) {
693 JdwpNetState* netState = state->netState;
694
695 /*dumpPacket(expandBufGetBuffer(pReq));*/
696 if (netState->clientSock < 0) {
697 /* can happen with some DDMS events */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800698 VLOG(jdwp) << "NOT sending request -- no debugger is attached";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700699 return false;
700 }
701
702 errno = 0;
703 ssize_t cc = netState->writePacket(pReq);
704
705 if (cc != (ssize_t) expandBufGetLength(pReq)) {
706 PLOG(ERROR) << "Failed sending req to debugger (" << cc << " of " << expandBufGetLength(pReq) << ")";
707 return false;
708 }
709
710 return true;
711}
712
713/*
714 * Send a request that was split into multiple buffers.
715 *
716 * The entire packet must be sent with a single writev() call to avoid
717 * threading issues.
718 *
719 * Returns "true" if it was sent successfully.
720 */
Elliott Hughescccd84f2011-12-05 16:51:54 -0800721static bool sendBufferedRequest(JdwpState* state, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700722 JdwpNetState* netState = state->netState;
723
724 if (netState->clientSock < 0) {
725 /* can happen with some DDMS events */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800726 VLOG(jdwp) << "NOT sending request -- no debugger is attached";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700727 return false;
728 }
729
730 size_t expected = 0;
Elliott Hughescccd84f2011-12-05 16:51:54 -0800731 for (int i = 0; i < iov_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700732 expected += iov[i].iov_len;
733 }
734
Elliott Hughescccd84f2011-12-05 16:51:54 -0800735 ssize_t actual = netState->writeBufferedPacket(iov, iov_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700736
737 if ((size_t)actual != expected) {
738 PLOG(ERROR) << "Failed sending b-req to debugger (" << actual << " of " << expected << ")";
739 return false;
740 }
741
742 return true;
743}
744
745/*
746 * Our functions.
747 *
748 * We can't generally share the implementations with other transports,
749 * even if they're also socket-based, because our JdwpNetState will be
750 * different from theirs.
751 */
752static const JdwpTransport socketTransport = {
753 prepareSocket,
754 acceptConnection,
755 establishConnection,
756 closeConnection,
757 netShutdownExtern,
758 netFreeExtern,
759 isConnected,
760 awaitingHandshake,
761 processIncoming,
762 sendRequest,
763 sendBufferedRequest,
764};
765
766/*
767 * Return our set.
768 */
769const JdwpTransport* SocketTransport() {
770 return &socketTransport;
771}
772
773} // namespace JDWP
774
775} // namespace art