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