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