Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/tools/attach/SolarisVirtualMachine.java
32288 views
1
/*
2
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
package sun.tools.attach;
26
27
import com.sun.tools.attach.AttachOperationFailedException;
28
import com.sun.tools.attach.AgentLoadException;
29
import com.sun.tools.attach.AttachNotSupportedException;
30
import com.sun.tools.attach.spi.AttachProvider;
31
32
import java.io.InputStream;
33
import java.io.IOException;
34
import java.io.File;
35
import java.io.FileNotFoundException;
36
37
/*
38
* Solaris implementation of HotSpotVirtualMachine.
39
*/
40
public class SolarisVirtualMachine extends HotSpotVirtualMachine {
41
// "/tmp" is used as a global well-known location for the files
42
// .java_pid<pid>. and .attach_pid<pid>. It is important that this
43
// location is the same for all processes, otherwise the tools
44
// will not be able to find all Hotspot processes.
45
// Any changes to this needs to be synchronized with HotSpot.
46
private static final String tmpdir = "/tmp";
47
48
// door descriptor;
49
private int fd = -1;
50
51
/**
52
* Attaches to the target VM
53
*/
54
SolarisVirtualMachine(AttachProvider provider, String vmid)
55
throws AttachNotSupportedException, IOException
56
{
57
super(provider, vmid);
58
// This provider only understands process-ids (pids).
59
int pid;
60
try {
61
pid = Integer.parseInt(vmid);
62
} catch (NumberFormatException x) {
63
throw new AttachNotSupportedException("invalid process identifier");
64
}
65
66
// Opens the door file to the target VM. If the file is not
67
// found it might mean that the attach mechanism isn't started in the
68
// target VM so we attempt to start it and retry.
69
try {
70
fd = openDoor(pid);
71
} catch (FileNotFoundException fnf1) {
72
File f = createAttachFile(pid);
73
try {
74
// kill -QUIT will tickle target VM to check for the
75
// attach file.
76
sigquit(pid);
77
78
// give the target VM time to start the attach mechanism
79
int i = 0;
80
long delay = 200;
81
int retries = (int)(attachTimeout() / delay);
82
do {
83
try {
84
Thread.sleep(delay);
85
} catch (InterruptedException x) { }
86
try {
87
fd = openDoor(pid);
88
} catch (FileNotFoundException fnf2) { }
89
i++;
90
} while (i <= retries && fd == -1);
91
if (fd == -1) {
92
throw new AttachNotSupportedException(
93
"Unable to open door: target process not responding or " +
94
"HotSpot VM not loaded");
95
}
96
} finally {
97
f.delete();
98
}
99
}
100
assert fd >= 0;
101
}
102
103
/**
104
* Detach from the target VM
105
*/
106
public void detach() throws IOException {
107
synchronized (this) {
108
if (fd != -1) {
109
close(fd);
110
fd = -1;
111
}
112
}
113
}
114
115
/**
116
* Execute the given command in the target VM.
117
*/
118
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
119
assert args.length <= 3; // includes null
120
121
// first check that we are still attached
122
int door;
123
synchronized (this) {
124
if (fd == -1) {
125
throw new IOException("Detached from target VM");
126
}
127
door = fd;
128
}
129
130
// enqueue the command via a door call
131
int s = enqueue(door, cmd, args);
132
assert s >= 0; // valid file descriptor
133
134
// The door call returns a file descriptor (one end of a socket pair).
135
// Create an input stream around it.
136
SocketInputStream sis = new SocketInputStream(s);
137
138
// Read the command completion status
139
int completionStatus;
140
try {
141
completionStatus = readInt(sis);
142
} catch (IOException ioe) {
143
sis.close();
144
throw ioe;
145
}
146
147
// If non-0 it means an error but we need to special-case the
148
// "load" command to ensure that the right exception is thrown.
149
if (completionStatus != 0) {
150
// read from the stream and use that as the error message
151
String message = readErrorMessage(sis);
152
sis.close();
153
if (cmd.equals("load")) {
154
throw new AgentLoadException("Failed to load agent library");
155
} else {
156
if (message == null) {
157
throw new AttachOperationFailedException("Command failed in target VM");
158
} else {
159
throw new AttachOperationFailedException(message);
160
}
161
}
162
}
163
164
// Return the input stream so that the command output can be read
165
return sis;
166
}
167
168
// InputStream over a socket
169
private class SocketInputStream extends InputStream {
170
int s;
171
172
public SocketInputStream(int s) {
173
this.s = s;
174
}
175
176
public synchronized int read() throws IOException {
177
byte b[] = new byte[1];
178
int n = this.read(b, 0, 1);
179
if (n == 1) {
180
return b[0] & 0xff;
181
} else {
182
return -1;
183
}
184
}
185
186
public synchronized int read(byte[] bs, int off, int len) throws IOException {
187
if ((off < 0) || (off > bs.length) || (len < 0) ||
188
((off + len) > bs.length) || ((off + len) < 0)) {
189
throw new IndexOutOfBoundsException();
190
} else if (len == 0)
191
return 0;
192
193
return SolarisVirtualMachine.read(s, bs, off, len);
194
}
195
196
public void close() throws IOException {
197
SolarisVirtualMachine.close(s);
198
}
199
}
200
201
// The door is attached to .java_pid<pid> in the temporary directory.
202
private int openDoor(int pid) throws IOException {
203
String path = tmpdir + "/.java_pid" + pid;;
204
fd = open(path);
205
206
// Check that the file owner/permission to avoid attaching to
207
// bogus process
208
try {
209
checkPermissions(path);
210
} catch (IOException ioe) {
211
close(fd);
212
throw ioe;
213
}
214
return fd;
215
}
216
217
// On Solaris/Linux a simple handshake is used to start the attach mechanism
218
// if not already started. The client creates a .attach_pid<pid> file in the
219
// target VM's working directory (or temporary directory), and the SIGQUIT
220
// handler checks for the file.
221
private File createAttachFile(int pid) throws IOException {
222
String fn = ".attach_pid" + pid;
223
String path = "/proc/" + pid + "/cwd/" + fn;
224
File f = new File(path);
225
try {
226
f.createNewFile();
227
} catch (IOException x) {
228
f = new File(tmpdir, fn);
229
f.createNewFile();
230
}
231
return f;
232
}
233
234
//-- native methods
235
236
static native int open(String path) throws IOException;
237
238
static native void close(int fd) throws IOException;
239
240
static native int read(int fd, byte buf[], int off, int buflen) throws IOException;
241
242
static native void checkPermissions(String path) throws IOException;
243
244
static native void sigquit(int pid) throws IOException;
245
246
// enqueue a command (and arguments) to the given door
247
static native int enqueue(int fd, String cmd, Object ... args)
248
throws IOException;
249
250
static {
251
System.loadLibrary("attach");
252
}
253
}
254
255