Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 17 | #include <arpa/inet.h> |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 18 | #include <errno.h> |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 19 | #include <netdb.h> |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 20 | #include <netinet/in.h> |
| 21 | #include <netinet/tcp.h> |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 22 | #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 Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 33 | |
| 34 | #define kBasePort 8000 |
| 35 | #define kMaxPort 8040 |
| 36 | |
| 37 | #define kInputBufferSize 8192 |
| 38 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 39 | namespace art { |
| 40 | |
| 41 | namespace JDWP { |
| 42 | |
| 43 | // fwd |
| 44 | static void netShutdown(JdwpNetState* state); |
| 45 | static void netFree(JdwpNetState* state); |
| 46 | |
| 47 | /* |
| 48 | * JDWP network state. |
| 49 | * |
| 50 | * We only talk to one debugger at a time. |
| 51 | */ |
| 52 | struct JdwpNetState : public JdwpNetStateBase { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 53 | uint16_t listenPort; |
| 54 | int listenSock; /* listen for connection from debugger */ |
| 55 | int wakePipe[2]; /* break out of select */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 56 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 57 | in_addr remoteAddr; |
| 58 | uint16_t remotePort; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 59 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 60 | bool awaitingHandshake; /* waiting for "JDWP-Handshake" */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 61 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 62 | /* pending data from the network; would be more efficient as circular buf */ |
| 63 | unsigned char inputBuffer[kInputBufferSize]; |
| 64 | size_t inputCount; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 65 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 66 | JdwpNetState() { |
| 67 | listenPort = 0; |
| 68 | listenSock = -1; |
| 69 | wakePipe[0] = -1; |
| 70 | wakePipe[1] = -1; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 71 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 72 | awaitingHandshake = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 73 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 74 | inputCount = 0; |
| 75 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 78 | static JdwpNetState* netStartup(uint16_t port, bool probe); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * Set up some stuff for transport=dt_socket. |
| 82 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 83 | static bool prepareSocket(JdwpState* state, const JdwpOptions* options) { |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 84 | uint16_t port = options->port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 85 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 86 | if (options->server) { |
| 87 | if (options->port != 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 88 | /* try only the specified port */ |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 89 | state->netState = netStartup(port, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 90 | } else { |
| 91 | /* scan through a range of ports, binding to the first available */ |
| 92 | for (port = kBasePort; port <= kMaxPort; port++) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 93 | state->netState = netStartup(port, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 94 | if (state->netState != NULL) { |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | if (state->netState == NULL) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 100 | LOG(ERROR) << "JDWP net startup failed (req port=" << options->port << ")"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | } else { |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 104 | state->netState = netStartup(0, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 107 | if (options->suspend) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 108 | LOG(INFO) << "JDWP will wait for debugger on port " << port; |
| 109 | } else { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 110 | LOG(INFO) << "JDWP will " << (options->server ? "listen" : "connect") << " on port " << port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Are we still waiting for the handshake string? |
| 118 | */ |
| 119 | static bool awaitingHandshake(JdwpState* state) { |
| 120 | return state->netState->awaitingHandshake; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Initialize JDWP stuff. |
| 125 | * |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 126 | * 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 Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 128 | * 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 Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 135 | static JdwpNetState* netStartup(uint16_t port, bool probe) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 136 | JdwpNetState* netState = new JdwpNetState; |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 137 | if (port == 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 138 | return netState; |
| 139 | } |
| 140 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 141 | netState->listenSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 142 | if (netState->listenSock < 0) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 143 | PLOG(probe ? ERROR : FATAL) << "Socket create failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 144 | goto fail; |
| 145 | } |
| 146 | |
| 147 | /* allow immediate re-use */ |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 148 | { |
| 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 Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 157 | sockaddr_in addrInet; |
| 158 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 159 | } 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 Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 165 | PLOG(probe ? ERROR : FATAL) << "Attempt to bind to port " << port << " failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 166 | goto fail; |
| 167 | } |
| 168 | |
| 169 | netState->listenPort = port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 170 | |
| 171 | if (listen(netState->listenSock, 5) != 0) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 172 | PLOG(probe ? ERROR : FATAL) << "Listen failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 173 | goto fail; |
| 174 | } |
| 175 | |
| 176 | return netState; |
| 177 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 178 | fail: |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 179 | 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 | */ |
| 195 | static 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 Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 218 | VLOG(jdwp) << "+++ writing to wakePipe"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 219 | (void) write(netState->wakePipe[1], "", 1); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | static 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 | */ |
| 232 | static 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 | |
| 251 | static void netFreeExtern(JdwpState* state) { |
| 252 | netFree(state->netState); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Returns "true" if we're connected to a debugger. |
| 257 | */ |
| 258 | static bool isConnected(JdwpState* state) { |
| 259 | return (state->netState != NULL && state->netState->clientSock >= 0); |
| 260 | } |
| 261 | |
| 262 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 263 | * 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 Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 267 | static 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 Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 272 | } |
| 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 Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 279 | static bool acceptConnection(JdwpState* state) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 280 | JdwpNetState* netState = state->netState; |
| 281 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 282 | sockaddr_in addrInet; |
| 283 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 284 | } 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 Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 301 | if (VLOG_IS_ON(jdwp)) { |
| 302 | PLOG(ERROR) << "accept failed"; |
| 303 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 304 | } 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 Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 313 | VLOG(jdwp) << "+++ accepted connection from " << inet_ntoa(netState->remoteAddr) << ":" << netState->remotePort; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 314 | |
| 315 | netState->clientSock = sock; |
| 316 | netState->awaitingHandshake = true; |
| 317 | netState->inputCount = 0; |
| 318 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 319 | VLOG(jdwp) << "Setting TCP_NODELAY on accepted socket"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 320 | 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 Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 333 | static bool establishConnection(JdwpState* state, const JdwpOptions* options) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 334 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 335 | sockaddr_in addrInet; |
| 336 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 337 | } addr; |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 338 | hostent* pEntry; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 339 | |
| 340 | CHECK(state != NULL && state->netState != NULL); |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 341 | CHECK(!options->server); |
| 342 | CHECK(!options->host.empty()); |
| 343 | CHECK_NE(options->port, 0); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 344 | |
| 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 Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 351 | hostent he; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 352 | char auxBuf[128]; |
| 353 | int error; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 354 | int cc = gethostbyname_r(options->host.c_str(), &he, auxBuf, sizeof(auxBuf), &pEntry, &error); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 355 | if (cc != 0) { |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 356 | LOG(WARNING) << "gethostbyname_r('" << options->host << "') failed: " << hstrerror(error); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | #else |
| 360 | h_errno = 0; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 361 | pEntry = gethostbyname(options->host.c_str()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 362 | if (pEntry == NULL) { |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 363 | PLOG(WARNING) << "gethostbyname('" << options->host << "') failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 364 | 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 Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 372 | addr.addrInet.sin_port = htons(options->port); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 373 | |
| 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 Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 397 | LOG(INFO) << "Connection established to " << options->host << " (" << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port) << ")"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 398 | 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 | */ |
| 416 | static 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 Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 426 | VLOG(jdwp) << "+++ closed connection to " << inet_ntoa(netState->remoteAddr) << ":" << netState->remotePort; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 427 | |
| 428 | close(netState->clientSock); |
| 429 | netState->clientSock = -1; |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * Figure out if we have a full packet in the buffer. |
| 434 | */ |
| 435 | static bool haveFullPacket(JdwpNetState* netState) { |
| 436 | if (netState->awaitingHandshake) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 437 | return (netState->inputCount >= kMagicHandshakeLen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 438 | } |
| 439 | if (netState->inputCount < 4) { |
| 440 | return false; |
| 441 | } |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 442 | uint32_t length = Get4BE(netState->inputBuffer); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 443 | 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 Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 452 | static void consumeBytes(JdwpNetState* netState, size_t count) { |
| 453 | CHECK_GT(count, 0U); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 454 | 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 Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 466 | * Handle a packet. Returns "false" if we encounter a connection-fatal error. |
| 467 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 468 | static bool handlePacket(JdwpState* state) { |
| 469 | JdwpNetState* netState = state->netState; |
| 470 | const unsigned char* buf = netState->inputBuffer; |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 471 | uint8_t cmdSet, cmd; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 472 | bool reply; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 473 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 474 | cmd = cmdSet = 0; // shut up gcc |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 475 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 476 | uint32_t length = Read4BE(&buf); |
| 477 | uint32_t id = Read4BE(&buf); |
| 478 | int8_t flags = Read1(&buf); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 479 | if ((flags & kJDWPFlagReply) != 0) { |
| 480 | reply = true; |
Brian Carlstrom | fd2ec54 | 2012-05-02 15:08:57 -0700 | [diff] [blame] | 481 | Read2BE(&buf); // error |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 482 | } else { |
| 483 | reply = false; |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 484 | cmdSet = Read1(&buf); |
| 485 | cmd = Read1(&buf); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 486 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 487 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 488 | CHECK_LE(length, netState->inputCount); |
| 489 | int dataLen = length - (buf - netState->inputBuffer); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 490 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 491 | if (!reply) { |
| 492 | ExpandBuf* pReply = expandBufAlloc(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 493 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 494 | JdwpReqHeader hdr; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 495 | 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 Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 502 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 503 | if (cc != (ssize_t) expandBufGetLength(pReply)) { |
| 504 | PLOG(ERROR) << "Failed sending reply to debugger"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 505 | expandBufFree(pReply); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 506 | return false; |
| 507 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 508 | } else { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 509 | LOG(WARNING) << "No reply created for set=" << cmdSet << " cmd=" << cmd; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 510 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 511 | expandBufFree(pReply); |
| 512 | } else { |
| 513 | LOG(ERROR) << "reply?!"; |
| 514 | DCHECK(false); |
| 515 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 516 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 517 | VLOG(jdwp) << "----------"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 518 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 519 | consumeBytes(netState, length); |
| 520 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 521 | } |
| 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 | */ |
| 538 | static 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 Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 562 | VLOG(jdwp) << "+++ all fds are closed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 563 | 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 Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 616 | sockaddr_in addrInet; |
| 617 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 618 | } 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 Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 678 | VLOG(jdwp) << "+++ handshake complete"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 679 | return true; |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | * Handle this packet. |
| 684 | */ |
| 685 | return handlePacket(state); |
| 686 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 687 | fail: |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 688 | 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 | */ |
| 700 | static 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 Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 706 | VLOG(jdwp) << "NOT sending request -- no debugger is attached"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 707 | 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 Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 729 | static bool sendBufferedRequest(JdwpState* state, const iovec* iov, int iov_count) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 730 | JdwpNetState* netState = state->netState; |
| 731 | |
| 732 | if (netState->clientSock < 0) { |
| 733 | /* can happen with some DDMS events */ |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 734 | VLOG(jdwp) << "NOT sending request -- no debugger is attached"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 735 | return false; |
| 736 | } |
| 737 | |
| 738 | size_t expected = 0; |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 739 | for (int i = 0; i < iov_count; i++) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 740 | expected += iov[i].iov_len; |
| 741 | } |
| 742 | |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 743 | ssize_t actual = netState->writeBufferedPacket(iov, iov_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 744 | |
| 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 | */ |
| 760 | static 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 | */ |
| 777 | const JdwpTransport* SocketTransport() { |
| 778 | return &socketTransport; |
| 779 | } |
| 780 | |
| 781 | } // namespace JDWP |
| 782 | |
| 783 | } // namespace art |