blob: 604ba8377060b48b47e6c3ea57cf752b2d6b33f7 [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"
30#include "jdwp/jdwp_handler.h"
31#include "jdwp/jdwp_priv.h"
32#include "stringprintf.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 Hughes376a7a02011-10-24 18:35:55 -0700472 bool reply;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700473
Elliott Hughes376a7a02011-10-24 18:35:55 -0700474 cmd = cmdSet = 0; // shut up gcc
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700475
Elliott Hughes74847412012-06-20 18:10:21 -0700476 uint32_t length = Read4BE(&buf);
477 uint32_t id = Read4BE(&buf);
478 int8_t flags = Read1(&buf);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700479 if ((flags & kJDWPFlagReply) != 0) {
480 reply = true;
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700481 Read2BE(&buf); // error
Elliott Hughes376a7a02011-10-24 18:35:55 -0700482 } else {
483 reply = false;
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700484 cmdSet = Read1(&buf);
485 cmd = Read1(&buf);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700486 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700487
Elliott Hughes74847412012-06-20 18:10:21 -0700488 CHECK_LE(length, netState->inputCount);
489 int dataLen = length - (buf - netState->inputBuffer);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700490
Elliott Hughes376a7a02011-10-24 18:35:55 -0700491 if (!reply) {
492 ExpandBuf* pReply = expandBufAlloc();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700493
Elliott Hughes74847412012-06-20 18:10:21 -0700494 JdwpReqHeader hdr;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700495 hdr.length = length;
496 hdr.id = id;
497 hdr.cmdSet = cmdSet;
498 hdr.cmd = cmd;
499 state->ProcessRequest(&hdr, buf, dataLen, pReply);
500 if (expandBufGetLength(pReply) > 0) {
501 ssize_t cc = netState->writePacket(pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700502
Elliott Hughes376a7a02011-10-24 18:35:55 -0700503 if (cc != (ssize_t) expandBufGetLength(pReply)) {
504 PLOG(ERROR) << "Failed sending reply to debugger";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700505 expandBufFree(pReply);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700506 return false;
507 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700508 } else {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700509 LOG(WARNING) << "No reply created for set=" << cmdSet << " cmd=" << cmd;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700510 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700511 expandBufFree(pReply);
512 } else {
513 LOG(ERROR) << "reply?!";
514 DCHECK(false);
515 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700516
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800517 VLOG(jdwp) << "----------";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700518
Elliott Hughes376a7a02011-10-24 18:35:55 -0700519 consumeBytes(netState, length);
520 return true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700521}
522
523/*
524 * Process incoming data. If no data is available, this will block until
525 * some arrives.
526 *
527 * If we get a full packet, handle it.
528 *
529 * To take some of the mystery out of life, we want to reject incoming
530 * connections if we already have a debugger attached. If we don't, the
531 * debugger will just mysteriously hang until it times out. We could just
532 * close the listen socket, but there's a good chance we won't be able to
533 * bind to the same port again, which would confuse utilities.
534 *
535 * Returns "false" on error (indicating that the connection has been severed),
536 * "true" if things are still okay.
537 */
538static bool processIncoming(JdwpState* state) {
539 JdwpNetState* netState = state->netState;
540 int readCount;
541
542 CHECK_GE(netState->clientSock, 0);
543
544 if (!haveFullPacket(netState)) {
545 /* read some more, looping until we have data */
546 errno = 0;
547 while (1) {
548 int selCount;
549 fd_set readfds;
550 int maxfd;
551 int fd;
552
553 maxfd = netState->listenSock;
554 if (netState->clientSock > maxfd) {
555 maxfd = netState->clientSock;
556 }
557 if (netState->wakePipe[0] > maxfd) {
558 maxfd = netState->wakePipe[0];
559 }
560
561 if (maxfd < 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800562 VLOG(jdwp) << "+++ all fds are closed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700563 return false;
564 }
565
566 FD_ZERO(&readfds);
567
568 /* configure fds; note these may get zapped by another thread */
569 fd = netState->listenSock;
570 if (fd >= 0) {
571 FD_SET(fd, &readfds);
572 }
573 fd = netState->clientSock;
574 if (fd >= 0) {
575 FD_SET(fd, &readfds);
576 }
577 fd = netState->wakePipe[0];
578 if (fd >= 0) {
579 FD_SET(fd, &readfds);
580 } else {
581 LOG(INFO) << "NOTE: entering select w/o wakepipe";
582 }
583
584 /*
585 * Select blocks until it sees activity on the file descriptors.
586 * Closing the local file descriptor does not count as activity,
587 * so we can't rely on that to wake us up (it works for read()
588 * and accept(), but not select()).
589 *
590 * We can do one of three things: (1) send a signal and catch
591 * EINTR, (2) open an additional fd ("wakePipe") and write to
592 * it when it's time to exit, or (3) time out periodically and
593 * re-issue the select. We're currently using #2, as it's more
594 * reliable than #1 and generally better than #3. Wastes two fds.
595 */
596 selCount = select(maxfd+1, &readfds, NULL, NULL, NULL);
597 if (selCount < 0) {
598 if (errno == EINTR) {
599 continue;
600 }
601 PLOG(ERROR) << "select failed";
602 goto fail;
603 }
604
605 if (netState->wakePipe[0] >= 0 && FD_ISSET(netState->wakePipe[0], &readfds)) {
606 if (netState->listenSock >= 0) {
607 LOG(ERROR) << "Exit wake set, but not exiting?";
608 } else {
609 LOG(DEBUG) << "Got wake-up signal, bailing out of select";
610 }
611 goto fail;
612 }
613 if (netState->listenSock >= 0 && FD_ISSET(netState->listenSock, &readfds)) {
614 LOG(INFO) << "Ignoring second debugger -- accepting and dropping";
615 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700616 sockaddr_in addrInet;
617 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700618 } addr;
619 socklen_t addrlen;
620 int tmpSock;
621 tmpSock = accept(netState->listenSock, &addr.addrPlain, &addrlen);
622 if (tmpSock < 0) {
623 LOG(INFO) << "Weird -- accept failed";
624 } else {
625 close(tmpSock);
626 }
627 }
628 if (netState->clientSock >= 0 && FD_ISSET(netState->clientSock, &readfds)) {
629 readCount = read(netState->clientSock, netState->inputBuffer + netState->inputCount, sizeof(netState->inputBuffer) - netState->inputCount);
630 if (readCount < 0) {
631 /* read failed */
632 if (errno != EINTR) {
633 goto fail;
634 }
635 LOG(DEBUG) << "+++ EINTR hit";
636 return true;
637 } else if (readCount == 0) {
638 /* EOF hit -- far end went away */
639 LOG(DEBUG) << "+++ peer disconnected";
640 goto fail;
641 } else {
642 break;
643 }
644 }
645 }
646
647 netState->inputCount += readCount;
648 if (!haveFullPacket(netState)) {
649 return true; /* still not there yet */
650 }
651 }
652
653 /*
654 * Special-case the initial handshake. For some bizarre reason we're
655 * expected to emulate bad tty settings by echoing the request back
656 * exactly as it was sent. Note the handshake is always initiated by
657 * the debugger, no matter who connects to whom.
658 *
659 * Other than this one case, the protocol [claims to be] stateless.
660 */
661 if (netState->awaitingHandshake) {
662 int cc;
663
664 if (memcmp(netState->inputBuffer, kMagicHandshake, kMagicHandshakeLen) != 0) {
665 LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", netState->inputBuffer);
666 goto fail;
667 }
668
669 errno = 0;
670 cc = write(netState->clientSock, netState->inputBuffer, kMagicHandshakeLen);
671 if (cc != kMagicHandshakeLen) {
672 PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")";
673 goto fail;
674 }
675
676 consumeBytes(netState, kMagicHandshakeLen);
677 netState->awaitingHandshake = false;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800678 VLOG(jdwp) << "+++ handshake complete";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700679 return true;
680 }
681
682 /*
683 * Handle this packet.
684 */
685 return handlePacket(state);
686
Elliott Hughesa21039c2012-06-21 12:09:25 -0700687 fail:
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700688 closeConnection(state);
689 return false;
690}
691
692/*
693 * Send a request.
694 *
695 * The entire packet must be sent with a single write() call to avoid
696 * threading issues.
697 *
698 * Returns "true" if it was sent successfully.
699 */
700static bool sendRequest(JdwpState* state, ExpandBuf* pReq) {
701 JdwpNetState* netState = state->netState;
702
703 /*dumpPacket(expandBufGetBuffer(pReq));*/
704 if (netState->clientSock < 0) {
705 /* can happen with some DDMS events */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800706 VLOG(jdwp) << "NOT sending request -- no debugger is attached";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700707 return false;
708 }
709
710 errno = 0;
711 ssize_t cc = netState->writePacket(pReq);
712
713 if (cc != (ssize_t) expandBufGetLength(pReq)) {
714 PLOG(ERROR) << "Failed sending req to debugger (" << cc << " of " << expandBufGetLength(pReq) << ")";
715 return false;
716 }
717
718 return true;
719}
720
721/*
722 * Send a request that was split into multiple buffers.
723 *
724 * The entire packet must be sent with a single writev() call to avoid
725 * threading issues.
726 *
727 * Returns "true" if it was sent successfully.
728 */
Elliott Hughescccd84f2011-12-05 16:51:54 -0800729static bool sendBufferedRequest(JdwpState* state, const iovec* iov, int iov_count) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700730 JdwpNetState* netState = state->netState;
731
732 if (netState->clientSock < 0) {
733 /* can happen with some DDMS events */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800734 VLOG(jdwp) << "NOT sending request -- no debugger is attached";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700735 return false;
736 }
737
738 size_t expected = 0;
Elliott Hughescccd84f2011-12-05 16:51:54 -0800739 for (int i = 0; i < iov_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700740 expected += iov[i].iov_len;
741 }
742
Elliott Hughescccd84f2011-12-05 16:51:54 -0800743 ssize_t actual = netState->writeBufferedPacket(iov, iov_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700744
745 if ((size_t)actual != expected) {
746 PLOG(ERROR) << "Failed sending b-req to debugger (" << actual << " of " << expected << ")";
747 return false;
748 }
749
750 return true;
751}
752
753/*
754 * Our functions.
755 *
756 * We can't generally share the implementations with other transports,
757 * even if they're also socket-based, because our JdwpNetState will be
758 * different from theirs.
759 */
760static const JdwpTransport socketTransport = {
761 prepareSocket,
762 acceptConnection,
763 establishConnection,
764 closeConnection,
765 netShutdownExtern,
766 netFreeExtern,
767 isConnected,
768 awaitingHandshake,
769 processIncoming,
770 sendRequest,
771 sendBufferedRequest,
772};
773
774/*
775 * Return our set.
776 */
777const JdwpTransport* SocketTransport() {
778 return &socketTransport;
779}
780
781} // namespace JDWP
782
783} // namespace art