Skip to content

Commit 10d6a8e

Browse files
committed
8299518: HotSpotVirtualMachine shared code across different platforms
Reviewed-by: cjplummer, dholmes
1 parent 148900c commit 10d6a8e

File tree

5 files changed

+156
-302
lines changed

5 files changed

+156
-302
lines changed

src/jdk.attach/aix/classes/sun/tools/attach/VirtualMachineImpl.java

+15-80
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2015, 2019 SAP SE. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -25,7 +25,6 @@
2525
*/
2626
package sun.tools.attach;
2727

28-
import com.sun.tools.attach.AttachOperationFailedException;
2928
import com.sun.tools.attach.AgentLoadException;
3029
import com.sun.tools.attach.AttachNotSupportedException;
3130
import com.sun.tools.attach.spi.AttachProvider;
@@ -57,13 +56,8 @@ public class VirtualMachineImpl extends HotSpotVirtualMachine {
5756
super(provider, vmid);
5857

5958
// This provider only understands pids
60-
int pid;
61-
try {
62-
pid = Integer.parseInt(vmid);
63-
if (pid < 1) {
64-
throw new NumberFormatException();
65-
}
66-
} catch (NumberFormatException x) {
59+
int pid = Integer.parseInt(vmid);
60+
if (pid < 1) {
6761
throw new AttachNotSupportedException("Invalid process identifier: " + vmid);
6862
}
6963

@@ -137,9 +131,6 @@ public void detach() throws IOException {
137131
// protocol version
138132
private static final String PROTOCOL_VERSION = "1";
139133

140-
// known errors
141-
private static final int ATTACH_ERROR_BADVERSION = 101;
142-
143134
/**
144135
* Execute the given command in the target VM.
145136
*/
@@ -185,46 +176,10 @@ InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOEx
185176

186177

187178
// Create an input stream to read reply
188-
SocketInputStream sis = new SocketInputStream(s);
189-
190-
// Read the command completion status
191-
int completionStatus;
192-
try {
193-
completionStatus = readInt(sis);
194-
} catch (IOException x) {
195-
sis.close();
196-
if (ioe != null) {
197-
throw ioe;
198-
} else {
199-
throw x;
200-
}
201-
}
179+
SocketInputStreamImpl sis = new SocketInputStreamImpl(s);
202180

203-
if (completionStatus != 0) {
204-
// read from the stream and use that as the error message
205-
String message = readErrorMessage(sis);
206-
sis.close();
207-
208-
// In the event of a protocol mismatch then the target VM
209-
// returns a known error so that we can throw a reasonable
210-
// error.
211-
if (completionStatus == ATTACH_ERROR_BADVERSION) {
212-
throw new IOException("Protocol mismatch with target VM");
213-
}
214-
215-
// Special-case the "load" command so that the right exception is
216-
// thrown.
217-
if (cmd.equals("load")) {
218-
String msg = "Failed to load agent library";
219-
if (!message.isEmpty())
220-
msg += ": " + message;
221-
throw new AgentLoadException(msg);
222-
} else {
223-
if (message.isEmpty())
224-
message = "Command failed in target VM";
225-
throw new AttachOperationFailedException(message);
226-
}
227-
}
181+
// Process the command completion status
182+
processCompletionStatus(ioe, cmd, sis);
228183

229184
// Return the input stream so that the command output can be read
230185
return sis;
@@ -233,39 +188,19 @@ InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOEx
233188
/*
234189
* InputStream for the socket connection to get target VM
235190
*/
236-
private static class SocketInputStream extends InputStream {
237-
int s;
238-
239-
public SocketInputStream(int s) {
240-
this.s = s;
191+
private static class SocketInputStreamImpl extends SocketInputStream {
192+
public SocketInputStreamImpl(long fd) {
193+
super(fd);
241194
}
242195

243-
public synchronized int read() throws IOException {
244-
byte b[] = new byte[1];
245-
int n = this.read(b, 0, 1);
246-
if (n == 1) {
247-
return b[0] & 0xff;
248-
} else {
249-
return -1;
250-
}
196+
@Override
197+
protected int read(long fd, byte[] bs, int off, int len) throws IOException {
198+
return VirtualMachineImpl.read((int)fd, bs, off, len);
251199
}
252200

253-
public synchronized int read(byte[] bs, int off, int len) throws IOException {
254-
if ((off < 0) || (off > bs.length) || (len < 0) ||
255-
((off + len) > bs.length) || ((off + len) < 0)) {
256-
throw new IndexOutOfBoundsException();
257-
} else if (len == 0)
258-
return 0;
259-
260-
return VirtualMachineImpl.read(s, bs, off, len);
261-
}
262-
263-
public synchronized void close() throws IOException {
264-
if (s != -1) {
265-
int toClose = s;
266-
s = -1;
267-
VirtualMachineImpl.close(toClose);
268-
}
201+
@Override
202+
protected void close(long fd) throws IOException {
203+
VirtualMachineImpl.close((int)fd);
269204
}
270205
}
271206

src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java

+15-81
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,7 +24,6 @@
2424
*/
2525
package sun.tools.attach;
2626

27-
import com.sun.tools.attach.AttachOperationFailedException;
2827
import com.sun.tools.attach.AgentLoadException;
2928
import com.sun.tools.attach.AttachNotSupportedException;
3029
import com.sun.tools.attach.spi.AttachProvider;
@@ -58,13 +57,8 @@ public class VirtualMachineImpl extends HotSpotVirtualMachine {
5857
super(provider, vmid);
5958

6059
// This provider only understands pids
61-
int pid;
62-
try {
63-
pid = Integer.parseInt(vmid);
64-
if (pid < 1) {
65-
throw new NumberFormatException();
66-
}
67-
} catch (NumberFormatException x) {
60+
int pid = Integer.parseInt(vmid);
61+
if (pid < 1) {
6862
throw new AttachNotSupportedException("Invalid process identifier: " + vmid);
6963
}
7064

@@ -141,9 +135,6 @@ public void detach() throws IOException {
141135
// protocol version
142136
private static final String PROTOCOL_VERSION = "1";
143137

144-
// known errors
145-
private static final int ATTACH_ERROR_BADVERSION = 101;
146-
147138
/**
148139
* Execute the given command in the target VM.
149140
*/
@@ -189,46 +180,10 @@ InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOEx
189180

190181

191182
// Create an input stream to read reply
192-
SocketInputStream sis = new SocketInputStream(s);
193-
194-
// Read the command completion status
195-
int completionStatus;
196-
try {
197-
completionStatus = readInt(sis);
198-
} catch (IOException x) {
199-
sis.close();
200-
if (ioe != null) {
201-
throw ioe;
202-
} else {
203-
throw x;
204-
}
205-
}
183+
SocketInputStreamImpl sis = new SocketInputStreamImpl(s);
206184

207-
if (completionStatus != 0) {
208-
// read from the stream and use that as the error message
209-
String message = readErrorMessage(sis);
210-
sis.close();
211-
212-
// In the event of a protocol mismatch then the target VM
213-
// returns a known error so that we can throw a reasonable
214-
// error.
215-
if (completionStatus == ATTACH_ERROR_BADVERSION) {
216-
throw new IOException("Protocol mismatch with target VM");
217-
}
218-
219-
// Special-case the "load" command so that the right exception is
220-
// thrown.
221-
if (cmd.equals("load")) {
222-
String msg = "Failed to load agent library";
223-
if (!message.isEmpty())
224-
msg += ": " + message;
225-
throw new AgentLoadException(msg);
226-
} else {
227-
if (message.isEmpty())
228-
message = "Command failed in target VM";
229-
throw new AttachOperationFailedException(message);
230-
}
231-
}
185+
// Process the command completion status
186+
processCompletionStatus(ioe, cmd, sis);
232187

233188
// Return the input stream so that the command output can be read
234189
return sis;
@@ -237,40 +192,19 @@ InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOEx
237192
/*
238193
* InputStream for the socket connection to get target VM
239194
*/
240-
private static class SocketInputStream extends InputStream {
241-
int s = -1;
242-
243-
public SocketInputStream(int s) {
244-
this.s = s;
195+
private static class SocketInputStreamImpl extends SocketInputStream {
196+
public SocketInputStreamImpl(long fd) {
197+
super(fd);
245198
}
246199

247-
public synchronized int read() throws IOException {
248-
byte b[] = new byte[1];
249-
int n = this.read(b, 0, 1);
250-
if (n == 1) {
251-
return b[0] & 0xff;
252-
} else {
253-
return -1;
254-
}
200+
@Override
201+
protected int read(long fd, byte[] bs, int off, int len) throws IOException {
202+
return VirtualMachineImpl.read((int)fd, bs, off, len);
255203
}
256204

257-
public synchronized int read(byte[] bs, int off, int len) throws IOException {
258-
if ((off < 0) || (off > bs.length) || (len < 0) ||
259-
((off + len) > bs.length) || ((off + len) < 0)) {
260-
throw new IndexOutOfBoundsException();
261-
} else if (len == 0) {
262-
return 0;
263-
}
264-
265-
return VirtualMachineImpl.read(s, bs, off, len);
266-
}
267-
268-
public synchronized void close() throws IOException {
269-
if (s != -1) {
270-
int toClose = s;
271-
s = -1;
272-
VirtualMachineImpl.close(toClose);
273-
}
205+
@Override
206+
protected void close(long fd) throws IOException {
207+
VirtualMachineImpl.close((int)fd);
274208
}
275209
}
276210

0 commit comments

Comments
 (0)