Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/tools/attach/WindowsVirtualMachine.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 sun.tools.attach.HotSpotVirtualMachine;
33
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.util.Random;
37
38
public class WindowsVirtualMachine extends HotSpotVirtualMachine {
39
40
// the enqueue code stub (copied into each target VM)
41
private static byte[] stub;
42
43
private volatile long hProcess; // handle to the process
44
45
WindowsVirtualMachine(AttachProvider provider, String id)
46
throws AttachNotSupportedException, IOException
47
{
48
super(provider, id);
49
50
int pid;
51
try {
52
pid = Integer.parseInt(id);
53
} catch (NumberFormatException x) {
54
throw new AttachNotSupportedException("Invalid process identifier");
55
}
56
hProcess = openProcess(pid);
57
58
// The target VM might be a pre-6.0 VM so we enqueue a "null" command
59
// which minimally tests that the enqueue function exists in the target
60
// VM.
61
try {
62
enqueue(hProcess, stub, null, null);
63
} catch (IOException x) {
64
throw new AttachNotSupportedException(x.getMessage());
65
}
66
}
67
68
public void detach() throws IOException {
69
synchronized (this) {
70
if (hProcess != -1) {
71
closeProcess(hProcess);
72
hProcess = -1;
73
}
74
}
75
}
76
77
InputStream execute(String cmd, Object ... args)
78
throws AgentLoadException, IOException
79
{
80
assert args.length <= 3; // includes null
81
82
// create a pipe using a random name
83
int r = (new Random()).nextInt();
84
String pipename = "\\\\.\\pipe\\javatool" + r;
85
long hPipe = createPipe(pipename);
86
87
// check if we are detached - in theory it's possible that detach is invoked
88
// after this check but before we enqueue the command.
89
if (hProcess == -1) {
90
closePipe(hPipe);
91
throw new IOException("Detached from target VM");
92
}
93
94
try {
95
// enqueue the command to the process
96
enqueue(hProcess, stub, cmd, pipename, args);
97
98
// wait for command to complete - process will connect with the
99
// completion status
100
connectPipe(hPipe);
101
102
// create an input stream for the pipe
103
PipedInputStream is = new PipedInputStream(hPipe);
104
105
// read completion status
106
int status = readInt(is);
107
if (status != 0) {
108
// read from the stream and use that as the error message
109
String message = readErrorMessage(is);
110
// special case the load command so that the right exception is thrown
111
if (cmd.equals("load")) {
112
throw new AgentLoadException("Failed to load agent library");
113
} else {
114
if (message == null) {
115
throw new AttachOperationFailedException("Command failed in target VM");
116
} else {
117
throw new AttachOperationFailedException(message);
118
}
119
}
120
}
121
122
// return the input stream
123
return is;
124
125
} catch (IOException ioe) {
126
closePipe(hPipe);
127
throw ioe;
128
}
129
}
130
131
// An InputStream based on a pipe to the target VM
132
private class PipedInputStream extends InputStream {
133
134
private long hPipe;
135
136
public PipedInputStream(long hPipe) {
137
this.hPipe = hPipe;
138
}
139
140
public synchronized int read() throws IOException {
141
byte b[] = new byte[1];
142
int n = this.read(b, 0, 1);
143
if (n == 1) {
144
return b[0] & 0xff;
145
} else {
146
return -1;
147
}
148
}
149
150
public synchronized int read(byte[] bs, int off, int len) throws IOException {
151
if ((off < 0) || (off > bs.length) || (len < 0) ||
152
((off + len) > bs.length) || ((off + len) < 0)) {
153
throw new IndexOutOfBoundsException();
154
} else if (len == 0)
155
return 0;
156
157
return WindowsVirtualMachine.readPipe(hPipe, bs, off, len);
158
}
159
160
public void close() throws IOException {
161
if (hPipe != -1) {
162
WindowsVirtualMachine.closePipe(hPipe);
163
hPipe = -1;
164
}
165
}
166
}
167
168
169
//-- native methods
170
171
static native void init();
172
173
static native byte[] generateStub();
174
175
static native long openProcess(int pid) throws IOException;
176
177
static native void closeProcess(long hProcess) throws IOException;
178
179
static native long createPipe(String name) throws IOException;
180
181
static native void closePipe(long hPipe) throws IOException;
182
183
static native void connectPipe(long hPipe) throws IOException;
184
185
static native int readPipe(long hPipe, byte buf[], int off, int buflen) throws IOException;
186
187
static native void enqueue(long hProcess, byte[] stub,
188
String cmd, String pipename, Object ... args) throws IOException;
189
190
static {
191
System.loadLibrary("attach");
192
init(); // native initialization
193
stub = generateStub(); // generate stub to copy into target process
194
}
195
}
196
197