1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -48,6 +48,7 @@ public class VirtualMachineImpl extends HotSpotVirtualMachine {
48
48
// Any changes to this needs to be synchronized with HotSpot.
49
49
private static final String tmpdir ;
50
50
String socket_path ;
51
+ private int ver = VERSION_1 ; // updated in ctor depending on detectVersion result
51
52
52
53
/**
53
54
* Attaches to the target VM
@@ -107,14 +108,18 @@ public class VirtualMachineImpl extends HotSpotVirtualMachine {
107
108
// bogus process
108
109
checkPermissions (socket_path );
109
110
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
+ }
118
123
}
119
124
}
120
125
@@ -129,14 +134,10 @@ public void detach() throws IOException {
129
134
}
130
135
}
131
136
132
- // protocol version
133
- private static final String PROTOCOL_VERSION = "1" ;
134
-
135
137
/**
136
138
* Execute the given command in the target VM.
137
139
*/
138
140
InputStream execute (String cmd , Object ... args ) throws AgentLoadException , IOException {
139
- assert args .length <= 3 ; // includes null
140
141
checkNulls (args );
141
142
142
143
// did we detach?
@@ -162,16 +163,8 @@ InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOEx
162
163
// connected - write request
163
164
// <ver> <cmd> <args...>
164
165
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 );
175
168
} catch (IOException x ) {
176
169
ioe = x ;
177
170
}
@@ -187,6 +180,16 @@ InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOEx
187
180
return sis ;
188
181
}
189
182
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
+ }
190
193
/*
191
194
* InputStream for the socket connection to get target VM
192
195
*/
@@ -206,20 +209,6 @@ protected void close(long fd) throws IOException {
206
209
}
207
210
}
208
211
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
-
223
212
private File createAttachFile (int pid ) throws IOException {
224
213
File f = new File (tmpdir , ".attach_pid" + pid );
225
214
createAttachFile0 (f .getPath ());
0 commit comments