Skip to content

Commit 0ae5c6b

Browse files
author
Alex Menkov
committed
8342996: Enhance Attach API to support arbitrary length arguments - OSX
Reviewed-by: sspitsyn, kevinw
1 parent 91b63ca commit 0ae5c6b

File tree

1 file changed

+26
-37
lines changed

1 file changed

+26
-37
lines changed

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

+26-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2025, 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
@@ -48,6 +48,7 @@ public class VirtualMachineImpl extends HotSpotVirtualMachine {
4848
// Any changes to this needs to be synchronized with HotSpot.
4949
private static final String tmpdir;
5050
String socket_path;
51+
private int ver = VERSION_1; // updated in ctor depending on detectVersion result
5152

5253
/**
5354
* Attaches to the target VM
@@ -107,14 +108,18 @@ public class VirtualMachineImpl extends HotSpotVirtualMachine {
107108
// bogus process
108109
checkPermissions(socket_path);
109110

110-
// Check that we can connect to the process
111-
// - this ensures we throw the permission denied error now rather than
112-
// later when we attempt to enqueue a command.
113-
int s = socket();
114-
try {
115-
connect(s, socket_path);
116-
} finally {
117-
close(s);
111+
if (isAPIv2Enabled()) {
112+
ver = detectVersion();
113+
} else {
114+
// Check that we can connect to the process
115+
// - this ensures we throw the permission denied error now rather than
116+
// later when we attempt to enqueue a command.
117+
int s = socket();
118+
try {
119+
connect(s, socket_path);
120+
} finally {
121+
close(s);
122+
}
118123
}
119124
}
120125

@@ -129,14 +134,10 @@ public void detach() throws IOException {
129134
}
130135
}
131136

132-
// protocol version
133-
private static final String PROTOCOL_VERSION = "1";
134-
135137
/**
136138
* Execute the given command in the target VM.
137139
*/
138140
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
139-
assert args.length <= 3; // includes null
140141
checkNulls(args);
141142

142143
// did we detach?
@@ -162,16 +163,8 @@ InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOEx
162163
// connected - write request
163164
// <ver> <cmd> <args...>
164165
try {
165-
writeString(s, PROTOCOL_VERSION);
166-
writeString(s, cmd);
167-
168-
for (int i = 0; i < 3; i++) {
169-
if (i < args.length && args[i] != null) {
170-
writeString(s, (String)args[i]);
171-
} else {
172-
writeString(s, "");
173-
}
174-
}
166+
SocketOutputStream writer = new SocketOutputStream(s);
167+
writeCommand(writer, ver, cmd, args);
175168
} catch (IOException x) {
176169
ioe = x;
177170
}
@@ -187,6 +180,16 @@ InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOEx
187180
return sis;
188181
}
189182

183+
private static class SocketOutputStream implements AttachOutputStream {
184+
private int fd;
185+
public SocketOutputStream(int fd) {
186+
this.fd = fd;
187+
}
188+
@Override
189+
public void write(byte[] buffer, int offset, int length) throws IOException {
190+
VirtualMachineImpl.write(fd, buffer, offset, length);
191+
}
192+
}
190193
/*
191194
* InputStream for the socket connection to get target VM
192195
*/
@@ -206,20 +209,6 @@ protected void close(long fd) throws IOException {
206209
}
207210
}
208211

209-
/*
210-
* Write/sends the given to the target VM. String is transmitted in
211-
* UTF-8 encoding.
212-
*/
213-
private void writeString(int fd, String s) throws IOException {
214-
if (s.length() > 0) {
215-
byte[] b = s.getBytes(UTF_8);
216-
VirtualMachineImpl.write(fd, b, 0, b.length);
217-
}
218-
byte b[] = new byte[1];
219-
b[0] = 0;
220-
write(fd, b, 0, 1);
221-
}
222-
223212
private File createAttachFile(int pid) throws IOException {
224213
File f = new File(tmpdir, ".attach_pid" + pid);
225214
createAttachFile0(f.getPath());

0 commit comments

Comments
 (0)