2
|
1 |
/*
|
|
2 |
* Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
#include <stdio.h>
|
|
26 |
#include <string.h>
|
|
27 |
#include <errno.h>
|
|
28 |
#include <stdlib.h>
|
|
29 |
#include <ctype.h>
|
|
30 |
|
|
31 |
#include "jdwpTransport.h"
|
|
32 |
#include "sysSocket.h"
|
|
33 |
|
|
34 |
/*
|
|
35 |
* The Socket Transport Library.
|
|
36 |
*
|
|
37 |
* This module is an implementation of the Java Debug Wire Protocol Transport
|
|
38 |
* Service Provider Interface - see src/share/javavm/export/jdwpTransport.h.
|
|
39 |
*/
|
|
40 |
|
|
41 |
static int serverSocketFD;
|
|
42 |
static int socketFD = -1;
|
|
43 |
static jdwpTransportCallback *callback;
|
|
44 |
static JavaVM *jvm;
|
|
45 |
static int tlsIndex;
|
|
46 |
static jboolean initialized;
|
|
47 |
static struct jdwpTransportNativeInterface_ interface;
|
|
48 |
static jdwpTransportEnv single_env = (jdwpTransportEnv)&interface;
|
|
49 |
|
|
50 |
#define RETURN_ERROR(err, msg) \
|
|
51 |
if (1==1) { \
|
|
52 |
setLastError(err, msg); \
|
|
53 |
return err; \
|
|
54 |
}
|
|
55 |
|
|
56 |
#define RETURN_IO_ERROR(msg) RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, msg);
|
|
57 |
|
|
58 |
#define RETURN_RECV_ERROR(n) \
|
|
59 |
if (n == 0) { \
|
|
60 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, "premature EOF"); \
|
|
61 |
} else { \
|
|
62 |
RETURN_IO_ERROR("recv error"); \
|
|
63 |
}
|
|
64 |
|
|
65 |
#define HEADER_SIZE 11
|
|
66 |
#define MAX_DATA_SIZE 1000
|
|
67 |
|
|
68 |
/*
|
|
69 |
* Record the last error for this thread.
|
|
70 |
*/
|
|
71 |
static void
|
|
72 |
setLastError(jdwpTransportError err, char *newmsg) {
|
|
73 |
char buf[255];
|
|
74 |
char *msg;
|
|
75 |
|
|
76 |
/* get any I/O first in case any system calls override errno */
|
|
77 |
if (err == JDWPTRANSPORT_ERROR_IO_ERROR) {
|
|
78 |
dbgsysGetLastIOError(buf, sizeof(buf));
|
|
79 |
}
|
|
80 |
|
|
81 |
msg = (char *)dbgsysTlsGet(tlsIndex);
|
|
82 |
if (msg != NULL) {
|
|
83 |
(*callback->free)(msg);
|
|
84 |
}
|
|
85 |
|
|
86 |
if (err == JDWPTRANSPORT_ERROR_IO_ERROR) {
|
|
87 |
char *join_str = ": ";
|
|
88 |
int msg_len = strlen(newmsg) + strlen(join_str) + strlen(buf) + 3;
|
|
89 |
msg = (*callback->alloc)(msg_len);
|
|
90 |
if (msg != NULL) {
|
|
91 |
strcpy(msg, newmsg);
|
|
92 |
strcat(msg, join_str);
|
|
93 |
strcat(msg, buf);
|
|
94 |
}
|
|
95 |
} else {
|
|
96 |
msg = (*callback->alloc)(strlen(newmsg)+1);
|
|
97 |
if (msg != NULL) {
|
|
98 |
strcpy(msg, newmsg);
|
|
99 |
}
|
|
100 |
}
|
|
101 |
|
|
102 |
dbgsysTlsPut(tlsIndex, msg);
|
|
103 |
}
|
|
104 |
|
|
105 |
/*
|
|
106 |
* Return the last error for this thread (may be NULL)
|
|
107 |
*/
|
|
108 |
static char*
|
|
109 |
getLastError() {
|
|
110 |
return (char *)dbgsysTlsGet(tlsIndex);
|
|
111 |
}
|
|
112 |
|
|
113 |
static jdwpTransportError
|
|
114 |
setOptions(int fd)
|
|
115 |
{
|
|
116 |
jvalue dontcare;
|
|
117 |
int err;
|
|
118 |
|
|
119 |
dontcare.i = 0; /* keep compiler happy */
|
|
120 |
|
|
121 |
err = dbgsysSetSocketOption(fd, SO_REUSEADDR, JNI_TRUE, dontcare);
|
|
122 |
if (err < 0) {
|
|
123 |
RETURN_IO_ERROR("setsockopt SO_REUSEADDR failed");
|
|
124 |
}
|
|
125 |
|
|
126 |
err = dbgsysSetSocketOption(fd, TCP_NODELAY, JNI_TRUE, dontcare);
|
|
127 |
if (err < 0) {
|
|
128 |
RETURN_IO_ERROR("setsockopt TCPNODELAY failed");
|
|
129 |
}
|
|
130 |
|
|
131 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
132 |
}
|
|
133 |
|
|
134 |
static jdwpTransportError
|
|
135 |
handshake(int fd, jlong timeout) {
|
|
136 |
char *hello = "JDWP-Handshake";
|
|
137 |
char b[16];
|
|
138 |
int rv, received, i;
|
|
139 |
|
|
140 |
if (timeout > 0) {
|
|
141 |
dbgsysConfigureBlocking(fd, JNI_FALSE);
|
|
142 |
}
|
|
143 |
received = 0;
|
|
144 |
while (received < (int)strlen(hello)) {
|
|
145 |
int n;
|
|
146 |
char *buf;
|
|
147 |
if (timeout > 0) {
|
|
148 |
rv = dbgsysPoll(fd, JNI_TRUE, JNI_FALSE, (long)timeout);
|
|
149 |
if (rv <= 0) {
|
|
150 |
setLastError(0, "timeout during handshake");
|
|
151 |
return JDWPTRANSPORT_ERROR_IO_ERROR;
|
|
152 |
}
|
|
153 |
}
|
|
154 |
buf = b;
|
|
155 |
buf += received;
|
|
156 |
n = dbgsysRecv(fd, buf, strlen(hello)-received, 0);
|
|
157 |
if (n == 0) {
|
|
158 |
setLastError(0, "handshake failed - connection prematurally closed");
|
|
159 |
return JDWPTRANSPORT_ERROR_IO_ERROR;
|
|
160 |
}
|
|
161 |
if (n < 0) {
|
|
162 |
RETURN_IO_ERROR("recv failed during handshake");
|
|
163 |
}
|
|
164 |
received += n;
|
|
165 |
}
|
|
166 |
if (timeout > 0) {
|
|
167 |
dbgsysConfigureBlocking(fd, JNI_TRUE);
|
|
168 |
}
|
|
169 |
for (i=0; i<(int)strlen(hello); i++) {
|
|
170 |
if (b[i] != hello[i]) {
|
|
171 |
char msg[64];
|
|
172 |
strcpy(msg, "handshake failed - received >");
|
|
173 |
strncat(msg, b, strlen(hello));
|
|
174 |
strcat(msg, "< - excepted >");
|
|
175 |
strcat(msg, hello);
|
|
176 |
strcat(msg, "<");
|
|
177 |
setLastError(0, msg);
|
|
178 |
return JDWPTRANSPORT_ERROR_IO_ERROR;
|
|
179 |
}
|
|
180 |
}
|
|
181 |
|
|
182 |
if (dbgsysSend(fd, hello, strlen(hello), 0) != (int)strlen(hello)) {
|
|
183 |
RETURN_IO_ERROR("send failed during handshake");
|
|
184 |
}
|
|
185 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
186 |
}
|
|
187 |
|
|
188 |
static jdwpTransportError
|
|
189 |
parseAddress(const char *address, struct sockaddr_in *sa, UINT32 defaultHost) {
|
|
190 |
char *colon;
|
|
191 |
|
|
192 |
memset((void *)sa,0,sizeof(struct sockaddr_in));
|
|
193 |
sa->sin_family = AF_INET;
|
|
194 |
|
|
195 |
/* check for host:port or port */
|
|
196 |
colon = strchr(address, ':');
|
|
197 |
if (colon == NULL) {
|
|
198 |
u_short port = (u_short)atoi(address);
|
|
199 |
sa->sin_port = dbgsysHostToNetworkShort(port);
|
|
200 |
sa->sin_addr.s_addr = dbgsysHostToNetworkLong(defaultHost);
|
|
201 |
} else {
|
|
202 |
char *buf;
|
|
203 |
char *hostname;
|
|
204 |
u_short port;
|
|
205 |
UINT32 addr;
|
|
206 |
|
|
207 |
buf = (*callback->alloc)(strlen(address)+1);
|
|
208 |
if (buf == NULL) {
|
|
209 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
|
|
210 |
}
|
|
211 |
strcpy(buf, address);
|
|
212 |
buf[colon - address] = '\0';
|
|
213 |
hostname = buf;
|
|
214 |
port = atoi(colon + 1);
|
|
215 |
sa->sin_port = dbgsysHostToNetworkShort(port);
|
|
216 |
|
|
217 |
/*
|
|
218 |
* First see if the host is a literal IP address.
|
|
219 |
* If not then try to resolve it.
|
|
220 |
*/
|
|
221 |
addr = dbgsysInetAddr(hostname);
|
|
222 |
if (addr == 0xffffffff) {
|
|
223 |
struct hostent *hp = dbgsysGetHostByName(hostname);
|
|
224 |
if (hp == NULL) {
|
|
225 |
/* don't use RETURN_IO_ERROR as unknown host is normal */
|
|
226 |
setLastError(0, "gethostbyname: unknown host");
|
|
227 |
(*callback->free)(buf);
|
|
228 |
return JDWPTRANSPORT_ERROR_IO_ERROR;
|
|
229 |
}
|
|
230 |
|
|
231 |
/* lookup was successful */
|
|
232 |
memcpy(&(sa->sin_addr), hp->h_addr_list[0], hp->h_length);
|
|
233 |
} else {
|
|
234 |
sa->sin_addr.s_addr = addr;
|
|
235 |
}
|
|
236 |
|
|
237 |
(*callback->free)(buf);
|
|
238 |
}
|
|
239 |
|
|
240 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
241 |
}
|
|
242 |
|
|
243 |
|
|
244 |
static jdwpTransportError JNICALL
|
|
245 |
socketTransport_getCapabilities(jdwpTransportEnv* env,
|
|
246 |
JDWPTransportCapabilities* capabilitiesPtr)
|
|
247 |
{
|
|
248 |
JDWPTransportCapabilities result;
|
|
249 |
|
|
250 |
memset(&result, 0, sizeof(result));
|
|
251 |
result.can_timeout_attach = JNI_TRUE;
|
|
252 |
result.can_timeout_accept = JNI_TRUE;
|
|
253 |
result.can_timeout_handshake = JNI_TRUE;
|
|
254 |
|
|
255 |
*capabilitiesPtr = result;
|
|
256 |
|
|
257 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
258 |
}
|
|
259 |
|
|
260 |
|
|
261 |
static jdwpTransportError JNICALL
|
|
262 |
socketTransport_startListening(jdwpTransportEnv* env, const char* address,
|
|
263 |
char** actualAddress)
|
|
264 |
{
|
|
265 |
struct sockaddr_in sa;
|
|
266 |
int err;
|
|
267 |
|
|
268 |
memset((void *)&sa,0,sizeof(struct sockaddr_in));
|
|
269 |
sa.sin_family = AF_INET;
|
|
270 |
|
|
271 |
/* no address provided */
|
|
272 |
if ((address == NULL) || (address[0] == '\0')) {
|
|
273 |
address = "0";
|
|
274 |
}
|
|
275 |
|
|
276 |
err = parseAddress(address, &sa, INADDR_ANY);
|
|
277 |
if (err != JDWPTRANSPORT_ERROR_NONE) {
|
|
278 |
return err;
|
|
279 |
}
|
|
280 |
|
|
281 |
serverSocketFD = dbgsysSocket(AF_INET, SOCK_STREAM, 0);
|
|
282 |
if (serverSocketFD < 0) {
|
|
283 |
RETURN_IO_ERROR("socket creation failed");
|
|
284 |
}
|
|
285 |
|
|
286 |
err = setOptions(serverSocketFD);
|
|
287 |
if (err) {
|
|
288 |
return err;
|
|
289 |
}
|
|
290 |
|
|
291 |
err = dbgsysBind(serverSocketFD, (struct sockaddr *)&sa, sizeof(sa));
|
|
292 |
if (err < 0) {
|
|
293 |
RETURN_IO_ERROR("bind failed");
|
|
294 |
}
|
|
295 |
|
|
296 |
err = dbgsysListen(serverSocketFD, 1);
|
|
297 |
if (err < 0) {
|
|
298 |
RETURN_IO_ERROR("listen failed");
|
|
299 |
}
|
|
300 |
|
|
301 |
{
|
|
302 |
char buf[20];
|
|
303 |
int len = sizeof(sa);
|
|
304 |
jint portNum;
|
|
305 |
err = dbgsysGetSocketName(serverSocketFD,
|
|
306 |
(struct sockaddr *)&sa, &len);
|
|
307 |
portNum = dbgsysNetworkToHostShort(sa.sin_port);
|
|
308 |
sprintf(buf, "%d", portNum);
|
|
309 |
*actualAddress = (*callback->alloc)(strlen(buf) + 1);
|
|
310 |
if (*actualAddress == NULL) {
|
|
311 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
|
|
312 |
} else {
|
|
313 |
strcpy(*actualAddress, buf);
|
|
314 |
}
|
|
315 |
}
|
|
316 |
|
|
317 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
318 |
}
|
|
319 |
|
|
320 |
static jdwpTransportError JNICALL
|
|
321 |
socketTransport_accept(jdwpTransportEnv* env, jlong acceptTimeout, jlong handshakeTimeout)
|
|
322 |
{
|
|
323 |
int socketLen, err;
|
|
324 |
struct sockaddr_in socket;
|
|
325 |
jlong startTime = (jlong)0;
|
|
326 |
|
|
327 |
/*
|
|
328 |
* Use a default handshake timeout if not specified - this avoids an indefinite
|
|
329 |
* hang in cases where something other than a debugger connects to our port.
|
|
330 |
*/
|
|
331 |
if (handshakeTimeout == 0) {
|
|
332 |
handshakeTimeout = 2000;
|
|
333 |
}
|
|
334 |
|
|
335 |
do {
|
|
336 |
/*
|
|
337 |
* If there is an accept timeout then we put the socket in non-blocking
|
|
338 |
* mode and poll for a connection.
|
|
339 |
*/
|
|
340 |
if (acceptTimeout > 0) {
|
|
341 |
int rv;
|
|
342 |
dbgsysConfigureBlocking(serverSocketFD, JNI_FALSE);
|
|
343 |
startTime = dbgsysCurrentTimeMillis();
|
|
344 |
rv = dbgsysPoll(serverSocketFD, JNI_TRUE, JNI_FALSE, (long)acceptTimeout);
|
|
345 |
if (rv <= 0) {
|
|
346 |
/* set the last error here as could be overridden by configureBlocking */
|
|
347 |
if (rv == 0) {
|
|
348 |
setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "poll failed");
|
|
349 |
}
|
|
350 |
/* restore blocking state */
|
|
351 |
dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE);
|
|
352 |
if (rv == 0) {
|
|
353 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "timed out waiting for connection");
|
|
354 |
} else {
|
|
355 |
return JDWPTRANSPORT_ERROR_IO_ERROR;
|
|
356 |
}
|
|
357 |
}
|
|
358 |
}
|
|
359 |
|
|
360 |
/*
|
|
361 |
* Accept the connection
|
|
362 |
*/
|
|
363 |
memset((void *)&socket,0,sizeof(struct sockaddr_in));
|
|
364 |
socketLen = sizeof(socket);
|
|
365 |
socketFD = dbgsysAccept(serverSocketFD,
|
|
366 |
(struct sockaddr *)&socket,
|
|
367 |
&socketLen);
|
|
368 |
/* set the last error here as could be overridden by configureBlocking */
|
|
369 |
if (socketFD < 0) {
|
|
370 |
setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "accept failed");
|
|
371 |
}
|
|
372 |
/*
|
|
373 |
* Restore the blocking state - note that the accepted socket may be in
|
|
374 |
* blocking or non-blocking mode (platform dependent). However as there
|
|
375 |
* is a handshake timeout set then it will go into non-blocking mode
|
|
376 |
* anyway for the handshake.
|
|
377 |
*/
|
|
378 |
if (acceptTimeout > 0) {
|
|
379 |
dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE);
|
|
380 |
}
|
|
381 |
if (socketFD < 0) {
|
|
382 |
return JDWPTRANSPORT_ERROR_IO_ERROR;
|
|
383 |
}
|
|
384 |
|
|
385 |
/* handshake with the debugger */
|
|
386 |
err = handshake(socketFD, handshakeTimeout);
|
|
387 |
|
|
388 |
/*
|
|
389 |
* If the handshake fails then close the connection. If there if an accept
|
|
390 |
* timeout then we must adjust the timeout for the next poll.
|
|
391 |
*/
|
|
392 |
if (err) {
|
|
393 |
fprintf(stderr, "Debugger failed to attach: %s\n", getLastError());
|
|
394 |
dbgsysSocketClose(socketFD);
|
|
395 |
socketFD = -1;
|
|
396 |
if (acceptTimeout > 0) {
|
|
397 |
long endTime = dbgsysCurrentTimeMillis();
|
|
398 |
acceptTimeout -= (endTime - startTime);
|
|
399 |
if (acceptTimeout <= 0) {
|
|
400 |
setLastError(JDWPTRANSPORT_ERROR_IO_ERROR,
|
|
401 |
"timeout waiting for debugger to connect");
|
|
402 |
return JDWPTRANSPORT_ERROR_IO_ERROR;
|
|
403 |
}
|
|
404 |
}
|
|
405 |
}
|
|
406 |
} while (socketFD < 0);
|
|
407 |
|
|
408 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
409 |
}
|
|
410 |
|
|
411 |
static jdwpTransportError JNICALL
|
|
412 |
socketTransport_stopListening(jdwpTransportEnv *env)
|
|
413 |
{
|
|
414 |
if (serverSocketFD < 0) {
|
|
415 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_STATE, "connection not open");
|
|
416 |
}
|
|
417 |
if (dbgsysSocketClose(serverSocketFD) < 0) {
|
|
418 |
RETURN_IO_ERROR("close failed");
|
|
419 |
}
|
|
420 |
serverSocketFD = -1;
|
|
421 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
422 |
}
|
|
423 |
|
|
424 |
static jdwpTransportError JNICALL
|
|
425 |
socketTransport_attach(jdwpTransportEnv* env, const char* addressString, jlong attachTimeout,
|
|
426 |
jlong handshakeTimeout)
|
|
427 |
{
|
|
428 |
struct sockaddr_in sa;
|
|
429 |
int err;
|
|
430 |
|
|
431 |
if (addressString == NULL || addressString[0] == '\0') {
|
|
432 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "address is missing");
|
|
433 |
}
|
|
434 |
|
|
435 |
err = parseAddress(addressString, &sa, 0x7f000001);
|
|
436 |
if (err != JDWPTRANSPORT_ERROR_NONE) {
|
|
437 |
return err;
|
|
438 |
}
|
|
439 |
|
|
440 |
socketFD = dbgsysSocket(AF_INET, SOCK_STREAM, 0);
|
|
441 |
if (socketFD < 0) {
|
|
442 |
RETURN_IO_ERROR("unable to create socket");
|
|
443 |
}
|
|
444 |
|
|
445 |
err = setOptions(socketFD);
|
|
446 |
if (err) {
|
|
447 |
return err;
|
|
448 |
}
|
|
449 |
|
|
450 |
/*
|
|
451 |
* To do a timed connect we make the socket non-blocking
|
|
452 |
* and poll with a timeout;
|
|
453 |
*/
|
|
454 |
if (attachTimeout > 0) {
|
|
455 |
dbgsysConfigureBlocking(socketFD, JNI_FALSE);
|
|
456 |
}
|
|
457 |
|
|
458 |
err = dbgsysConnect(socketFD, (struct sockaddr *)&sa, sizeof(sa));
|
|
459 |
if (err == DBG_EINPROGRESS && attachTimeout > 0) {
|
|
460 |
err = dbgsysFinishConnect(socketFD, (long)attachTimeout);
|
|
461 |
|
|
462 |
if (err == DBG_ETIMEOUT) {
|
|
463 |
dbgsysConfigureBlocking(socketFD, JNI_TRUE);
|
|
464 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "connect timed out");
|
|
465 |
}
|
|
466 |
}
|
|
467 |
|
|
468 |
if (err < 0) {
|
|
469 |
RETURN_IO_ERROR("connect failed");
|
|
470 |
}
|
|
471 |
|
|
472 |
if (attachTimeout > 0) {
|
|
473 |
dbgsysConfigureBlocking(socketFD, JNI_TRUE);
|
|
474 |
}
|
|
475 |
|
|
476 |
err = handshake(socketFD, handshakeTimeout);
|
|
477 |
if (err) {
|
|
478 |
dbgsysSocketClose(socketFD);
|
|
479 |
socketFD = -1;
|
|
480 |
return err;
|
|
481 |
}
|
|
482 |
|
|
483 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
484 |
}
|
|
485 |
|
|
486 |
static jboolean JNICALL
|
|
487 |
socketTransport_isOpen(jdwpTransportEnv* env)
|
|
488 |
{
|
|
489 |
if (socketFD >= 0) {
|
|
490 |
return JNI_TRUE;
|
|
491 |
} else {
|
|
492 |
return JNI_FALSE;
|
|
493 |
}
|
|
494 |
}
|
|
495 |
|
|
496 |
static jdwpTransportError JNICALL
|
|
497 |
socketTransport_close(jdwpTransportEnv* env)
|
|
498 |
{
|
|
499 |
int fd = socketFD;
|
|
500 |
socketFD = -1;
|
|
501 |
if (fd < 0) {
|
|
502 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
503 |
}
|
|
504 |
if (dbgsysSocketClose(fd) < 0) {
|
|
505 |
/*
|
|
506 |
* close failed - it's pointless to restore socketFD here because
|
|
507 |
* any subsequent close will likely fail aswell.
|
|
508 |
*/
|
|
509 |
RETURN_IO_ERROR("close failed");
|
|
510 |
}
|
|
511 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
512 |
}
|
|
513 |
|
|
514 |
static jdwpTransportError JNICALL
|
|
515 |
socketTransport_writePacket(jdwpTransportEnv* env, const jdwpPacket *packet)
|
|
516 |
{
|
|
517 |
jint len, data_len, id;
|
|
518 |
/*
|
|
519 |
* room for header and up to MAX_DATA_SIZE data bytes
|
|
520 |
*/
|
|
521 |
char header[HEADER_SIZE + MAX_DATA_SIZE];
|
|
522 |
jbyte *data;
|
|
523 |
|
|
524 |
/* packet can't be null */
|
|
525 |
if (packet == NULL) {
|
|
526 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is NULL");
|
|
527 |
}
|
|
528 |
|
|
529 |
len = packet->type.cmd.len; /* includes header */
|
|
530 |
data_len = len - HEADER_SIZE;
|
|
531 |
|
|
532 |
/* bad packet */
|
|
533 |
if (data_len < 0) {
|
|
534 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "invalid length");
|
|
535 |
}
|
|
536 |
|
|
537 |
/* prepare the header for transmission */
|
|
538 |
len = (jint)dbgsysHostToNetworkLong(len);
|
|
539 |
id = (jint)dbgsysHostToNetworkLong(packet->type.cmd.id);
|
|
540 |
|
|
541 |
memcpy(header + 0, &len, 4);
|
|
542 |
memcpy(header + 4, &id, 4);
|
|
543 |
header[8] = packet->type.cmd.flags;
|
|
544 |
if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
|
|
545 |
jshort errorCode =
|
|
546 |
dbgsysHostToNetworkShort(packet->type.reply.errorCode);
|
|
547 |
memcpy(header + 9, &errorCode, 2);
|
|
548 |
} else {
|
|
549 |
header[9] = packet->type.cmd.cmdSet;
|
|
550 |
header[10] = packet->type.cmd.cmd;
|
|
551 |
}
|
|
552 |
|
|
553 |
data = packet->type.cmd.data;
|
|
554 |
/* Do one send for short packets, two for longer ones */
|
|
555 |
if (data_len <= MAX_DATA_SIZE) {
|
|
556 |
memcpy(header + HEADER_SIZE, data, data_len);
|
|
557 |
if (dbgsysSend(socketFD, (char *)&header, HEADER_SIZE + data_len, 0) !=
|
|
558 |
HEADER_SIZE + data_len) {
|
|
559 |
RETURN_IO_ERROR("send failed");
|
|
560 |
}
|
|
561 |
} else {
|
|
562 |
memcpy(header + HEADER_SIZE, data, MAX_DATA_SIZE);
|
|
563 |
if (dbgsysSend(socketFD, (char *)&header, HEADER_SIZE + MAX_DATA_SIZE, 0) !=
|
|
564 |
HEADER_SIZE + MAX_DATA_SIZE) {
|
|
565 |
RETURN_IO_ERROR("send failed");
|
|
566 |
}
|
|
567 |
/* Send the remaining data bytes right out of the data area. */
|
|
568 |
if (dbgsysSend(socketFD, (char *)data + MAX_DATA_SIZE,
|
|
569 |
data_len - MAX_DATA_SIZE, 0) != data_len - MAX_DATA_SIZE) {
|
|
570 |
RETURN_IO_ERROR("send failed");
|
|
571 |
}
|
|
572 |
}
|
|
573 |
|
|
574 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
575 |
}
|
|
576 |
|
|
577 |
static jint
|
|
578 |
recv_fully(int f, char *buf, int len)
|
|
579 |
{
|
|
580 |
int nbytes = 0;
|
|
581 |
while (nbytes < len) {
|
|
582 |
int res = dbgsysRecv(f, buf + nbytes, len - nbytes, 0);
|
|
583 |
if (res < 0) {
|
|
584 |
return res;
|
|
585 |
} else if (res == 0) {
|
|
586 |
break; /* eof, return nbytes which is less than len */
|
|
587 |
}
|
|
588 |
nbytes += res;
|
|
589 |
}
|
|
590 |
return nbytes;
|
|
591 |
}
|
|
592 |
|
|
593 |
static jdwpTransportError JNICALL
|
|
594 |
socketTransport_readPacket(jdwpTransportEnv* env, jdwpPacket* packet) {
|
|
595 |
jint length, data_len;
|
|
596 |
jint n;
|
|
597 |
|
|
598 |
/* packet can't be null */
|
|
599 |
if (packet == NULL) {
|
|
600 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is null");
|
|
601 |
}
|
|
602 |
|
|
603 |
/* read the length field */
|
|
604 |
n = recv_fully(socketFD, (char *)&length, sizeof(jint));
|
|
605 |
|
|
606 |
/* check for EOF */
|
|
607 |
if (n == 0) {
|
|
608 |
packet->type.cmd.len = 0;
|
|
609 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
610 |
}
|
|
611 |
if (n != sizeof(jint)) {
|
|
612 |
RETURN_RECV_ERROR(n);
|
|
613 |
}
|
|
614 |
|
|
615 |
length = (jint)dbgsysNetworkToHostLong(length);
|
|
616 |
packet->type.cmd.len = length;
|
|
617 |
|
|
618 |
|
|
619 |
n = recv_fully(socketFD,(char *)&(packet->type.cmd.id),sizeof(jint));
|
|
620 |
if (n < (int)sizeof(jint)) {
|
|
621 |
RETURN_RECV_ERROR(n);
|
|
622 |
}
|
|
623 |
|
|
624 |
packet->type.cmd.id = (jint)dbgsysNetworkToHostLong(packet->type.cmd.id);
|
|
625 |
|
|
626 |
n = recv_fully(socketFD,(char *)&(packet->type.cmd.flags),sizeof(jbyte));
|
|
627 |
if (n < (int)sizeof(jbyte)) {
|
|
628 |
RETURN_RECV_ERROR(n);
|
|
629 |
}
|
|
630 |
|
|
631 |
if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
|
|
632 |
n = recv_fully(socketFD,(char *)&(packet->type.reply.errorCode),sizeof(jbyte));
|
|
633 |
if (n < (int)sizeof(jshort)) {
|
|
634 |
RETURN_RECV_ERROR(n);
|
|
635 |
}
|
|
636 |
|
|
637 |
/* FIXME - should the error be converted to host order?? */
|
|
638 |
|
|
639 |
|
|
640 |
} else {
|
|
641 |
n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmdSet),sizeof(jbyte));
|
|
642 |
if (n < (int)sizeof(jbyte)) {
|
|
643 |
RETURN_RECV_ERROR(n);
|
|
644 |
}
|
|
645 |
|
|
646 |
n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmd),sizeof(jbyte));
|
|
647 |
if (n < (int)sizeof(jbyte)) {
|
|
648 |
RETURN_RECV_ERROR(n);
|
|
649 |
}
|
|
650 |
}
|
|
651 |
|
|
652 |
data_len = length - ((sizeof(jint) * 2) + (sizeof(jbyte) * 3));
|
|
653 |
|
|
654 |
if (data_len < 0) {
|
|
655 |
setLastError(0, "Badly formed packet received - invalid length");
|
|
656 |
return JDWPTRANSPORT_ERROR_IO_ERROR;
|
|
657 |
} else if (data_len == 0) {
|
|
658 |
packet->type.cmd.data = NULL;
|
|
659 |
} else {
|
|
660 |
packet->type.cmd.data= (*callback->alloc)(data_len);
|
|
661 |
|
|
662 |
if (packet->type.cmd.data == NULL) {
|
|
663 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
|
|
664 |
}
|
|
665 |
|
|
666 |
n = recv_fully(socketFD,(char *)packet->type.cmd.data, data_len);
|
|
667 |
if (n < data_len) {
|
|
668 |
(*callback->free)(packet->type.cmd.data);
|
|
669 |
RETURN_RECV_ERROR(n);
|
|
670 |
}
|
|
671 |
}
|
|
672 |
|
|
673 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
674 |
}
|
|
675 |
|
|
676 |
static jdwpTransportError JNICALL
|
|
677 |
socketTransport_getLastError(jdwpTransportEnv* env, char** msgP) {
|
|
678 |
char *msg = (char *)dbgsysTlsGet(tlsIndex);
|
|
679 |
if (msg == NULL) {
|
|
680 |
return JDWPTRANSPORT_ERROR_MSG_NOT_AVAILABLE;
|
|
681 |
}
|
|
682 |
*msgP = (*callback->alloc)(strlen(msg)+1);
|
|
683 |
if (*msgP == NULL) {
|
|
684 |
return JDWPTRANSPORT_ERROR_OUT_OF_MEMORY;
|
|
685 |
}
|
|
686 |
strcpy(*msgP, msg);
|
|
687 |
return JDWPTRANSPORT_ERROR_NONE;
|
|
688 |
}
|
|
689 |
|
|
690 |
JNIEXPORT jint JNICALL
|
|
691 |
jdwpTransport_OnLoad(JavaVM *vm, jdwpTransportCallback* cbTablePtr,
|
|
692 |
jint version, jdwpTransportEnv** result)
|
|
693 |
{
|
|
694 |
if (version != JDWPTRANSPORT_VERSION_1_0) {
|
|
695 |
return JNI_EVERSION;
|
|
696 |
}
|
|
697 |
if (initialized) {
|
|
698 |
/*
|
|
699 |
* This library doesn't support multiple environments (yet)
|
|
700 |
*/
|
|
701 |
return JNI_EEXIST;
|
|
702 |
}
|
|
703 |
initialized = JNI_TRUE;
|
|
704 |
jvm = vm;
|
|
705 |
callback = cbTablePtr;
|
|
706 |
|
|
707 |
/* initialize interface table */
|
|
708 |
interface.GetCapabilities = &socketTransport_getCapabilities;
|
|
709 |
interface.Attach = &socketTransport_attach;
|
|
710 |
interface.StartListening = &socketTransport_startListening;
|
|
711 |
interface.StopListening = &socketTransport_stopListening;
|
|
712 |
interface.Accept = &socketTransport_accept;
|
|
713 |
interface.IsOpen = &socketTransport_isOpen;
|
|
714 |
interface.Close = &socketTransport_close;
|
|
715 |
interface.ReadPacket = &socketTransport_readPacket;
|
|
716 |
interface.WritePacket = &socketTransport_writePacket;
|
|
717 |
interface.GetLastError = &socketTransport_getLastError;
|
|
718 |
*result = &single_env;
|
|
719 |
|
|
720 |
/* initialized TLS */
|
|
721 |
tlsIndex = dbgsysTlsAlloc();
|
|
722 |
return JNI_OK;
|
|
723 |
}
|