Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/7158988/FieldMonitor.java
32284 views
1
/*
2
* Copyright 2012 SAP AG. 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test FieldMonitor.java
26
* @bug 7158988
27
* @key regression
28
* @summary verify jvm does not crash while debugging
29
* @run compile TestPostFieldModification.java
30
* @run main/othervm FieldMonitor
31
* @author [email protected]
32
*/
33
import java.io.BufferedReader;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.io.InputStreamReader;
37
import java.util.Iterator;
38
import java.util.List;
39
import java.util.Map;
40
41
import com.sun.jdi.Bootstrap;
42
import com.sun.jdi.Field;
43
import com.sun.jdi.ReferenceType;
44
import com.sun.jdi.VirtualMachine;
45
import com.sun.jdi.connect.Connector;
46
import com.sun.jdi.connect.IllegalConnectorArgumentsException;
47
import com.sun.jdi.connect.LaunchingConnector;
48
import com.sun.jdi.connect.VMStartException;
49
import com.sun.jdi.event.ClassPrepareEvent;
50
import com.sun.jdi.event.Event;
51
import com.sun.jdi.event.EventQueue;
52
import com.sun.jdi.event.EventSet;
53
import com.sun.jdi.event.ModificationWatchpointEvent;
54
import com.sun.jdi.event.VMDeathEvent;
55
import com.sun.jdi.event.VMStartEvent;
56
import com.sun.jdi.event.VMDisconnectEvent;
57
import com.sun.jdi.request.ClassPrepareRequest;
58
import com.sun.jdi.request.EventRequest;
59
import com.sun.jdi.request.EventRequestManager;
60
import com.sun.jdi.request.ModificationWatchpointRequest;
61
62
public class FieldMonitor {
63
64
public static final String CLASS_NAME = "TestPostFieldModification";
65
public static final String FIELD_NAME = "value";
66
public static final String ARGUMENTS = "-Xshare:off -XX:+PrintGC";
67
68
public static void main(String[] args)
69
throws IOException, InterruptedException {
70
71
//VirtualMachine vm = launchTarget(sb.toString());
72
VirtualMachine vm = launchTarget(CLASS_NAME);
73
74
System.out.println("Vm launched");
75
76
// process events
77
EventQueue eventQueue = vm.eventQueue();
78
// resume the vm
79
80
Process process = vm.process();
81
82
83
// Copy target's output and error to our output and error.
84
Thread outThread = new StreamRedirectThread("out reader", process.getInputStream());
85
Thread errThread = new StreamRedirectThread("error reader", process.getErrorStream());
86
87
errThread.start();
88
outThread.start();
89
90
boolean connected = true;
91
int watched = 0;
92
while (connected) {
93
EventSet eventSet = eventQueue.remove();
94
for (Event event : eventSet) {
95
System.out.println("FieldMonitor-main receives: "+event);
96
if (event instanceof VMStartEvent) {
97
addClassWatch(vm);
98
} else if (event instanceof VMDeathEvent
99
|| event instanceof VMDisconnectEvent) {
100
// exit
101
connected = false;
102
} else if (event instanceof ClassPrepareEvent) {
103
// watch field on loaded class
104
System.out.println("ClassPrepareEvent");
105
ClassPrepareEvent classPrepEvent = (ClassPrepareEvent) event;
106
ReferenceType refType = classPrepEvent
107
.referenceType();
108
addFieldWatch(vm, refType);
109
} else if (event instanceof ModificationWatchpointEvent) {
110
watched++;
111
System.out.println("sleep for 500 ms");
112
Thread.sleep(500);
113
114
ModificationWatchpointEvent modEvent = (ModificationWatchpointEvent) event;
115
System.out.println("old="
116
+ modEvent.valueCurrent());
117
System.out.println("new=" + modEvent.valueToBe());
118
}
119
}
120
System.out.println("resume...");
121
eventSet.resume();
122
}
123
// Shutdown begins when event thread terminates
124
try {
125
errThread.join(); // Make sure output is forwarded
126
outThread.join();
127
} catch (InterruptedException exc) {
128
// we don't interrupt
129
}
130
131
if (watched != 11) { // init + 10 modifications in TestPostFieldModification class
132
throw new Error("Expected to receive 11 times ModificationWatchpointEvent, but got "+watched);
133
}
134
}
135
136
/**
137
* Find a com.sun.jdi.CommandLineLaunch connector
138
*/
139
static LaunchingConnector findLaunchingConnector() {
140
List <Connector> connectors = Bootstrap.virtualMachineManager().allConnectors();
141
Iterator <Connector> iter = connectors.iterator();
142
while (iter.hasNext()) {
143
Connector connector = iter.next();
144
if (connector.name().equals("com.sun.jdi.CommandLineLaunch")) {
145
return (LaunchingConnector)connector;
146
}
147
}
148
throw new Error("No launching connector");
149
}
150
/**
151
* Return the launching connector's arguments.
152
*/
153
static Map <String,Connector.Argument> connectorArguments(LaunchingConnector connector, String mainArgs) {
154
Map<String,Connector.Argument> arguments = connector.defaultArguments();
155
for (String key : arguments.keySet()) {
156
System.out.println(key);
157
}
158
159
Connector.Argument mainArg = (Connector.Argument)arguments.get("main");
160
if (mainArg == null) {
161
throw new Error("Bad launching connector");
162
}
163
mainArg.setValue(mainArgs);
164
165
Connector.Argument optionsArg = (Connector.Argument)arguments.get("options");
166
if (optionsArg == null) {
167
throw new Error("Bad launching connector");
168
}
169
optionsArg.setValue(ARGUMENTS);
170
return arguments;
171
}
172
173
static VirtualMachine launchTarget(String mainArgs) {
174
LaunchingConnector connector = findLaunchingConnector();
175
Map arguments = connectorArguments(connector, mainArgs);
176
try {
177
return (VirtualMachine) connector.launch(arguments);
178
} catch (IOException exc) {
179
throw new Error("Unable to launch target VM: " + exc);
180
} catch (IllegalConnectorArgumentsException exc) {
181
throw new Error("Internal error: " + exc);
182
} catch (VMStartException exc) {
183
throw new Error("Target VM failed to initialize: " +
184
exc.getMessage());
185
}
186
}
187
188
189
private static void addClassWatch(VirtualMachine vm) {
190
EventRequestManager erm = vm.eventRequestManager();
191
ClassPrepareRequest classPrepareRequest = erm
192
.createClassPrepareRequest();
193
classPrepareRequest.addClassFilter(CLASS_NAME);
194
classPrepareRequest.setEnabled(true);
195
}
196
197
198
private static void addFieldWatch(VirtualMachine vm,
199
ReferenceType refType) {
200
EventRequestManager erm = vm.eventRequestManager();
201
Field field = refType.fieldByName(FIELD_NAME);
202
ModificationWatchpointRequest modificationWatchpointRequest = erm
203
.createModificationWatchpointRequest(field);
204
modificationWatchpointRequest.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
205
modificationWatchpointRequest.setEnabled(true);
206
}
207
}
208
209
class StreamRedirectThread extends Thread {
210
211
private final BufferedReader in;
212
213
private static final int BUFFER_SIZE = 2048;
214
215
/**
216
* Set up for copy.
217
* @param name Name of the thread
218
* @param in Stream to copy from
219
* @param out Stream to copy to
220
*/
221
StreamRedirectThread(String name, InputStream in) {
222
super(name);
223
this.in = new BufferedReader(new InputStreamReader(in));
224
}
225
226
/**
227
* Copy.
228
*/
229
public void run() {
230
try {
231
String line;
232
while ((line = in.readLine ()) != null) {
233
System.out.println ("testvm: " + line);
234
}
235
System.out.flush();
236
} catch(IOException exc) {
237
System.err.println("Child I/O Transfer - " + exc);
238
}
239
}
240
}
241
242