Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/createStepRequest/crstepreq010.java
41161 views
1
/*
2
* Copyright (c) 2002, 2018, 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.
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
package nsk.jdi.EventRequestManager.createStepRequest;
25
26
import nsk.share.*;
27
import nsk.share.jpda.*;
28
import nsk.share.jdi.*;
29
30
import com.sun.jdi.*;
31
import com.sun.jdi.connect.*;
32
import com.sun.jdi.request.*;
33
import com.sun.jdi.event.*;
34
import java.io.*;
35
import java.util.*;
36
37
/**
38
*/
39
public class crstepreq010 {
40
41
//----------------------------------------------------- immutable common fields
42
43
static final int PASSED = 0;
44
static final int FAILED = 2;
45
static final int PASS_BASE = 95;
46
static final int quit = -1;
47
48
private int instruction = 1;
49
private int waitTime;
50
private static int exitCode = PASSED;
51
52
private ArgumentHandler argHandler;
53
private Log log;
54
private Debugee debuggee;
55
private VirtualMachine vm;
56
private ReferenceType debuggeeClass;
57
58
private EventRequestManager eventRManager;
59
private EventSet eventSet;
60
private EventIterator eventIterator;
61
62
//------------------------------------------------------ mutable common fields
63
64
private final static String prefix = "nsk.jdi.EventRequestManager.createStepRequest";
65
private final static String className = ".crstepreq010";
66
private final static String debuggerName = prefix + className;
67
private final static String debuggeeName = debuggerName + "a";
68
static final int lineForBreak = 62;
69
70
//------------------------------------------------------ immutable common methods
71
72
public static void main (String argv[]) {
73
System.exit(run(argv, System.out) + PASS_BASE);
74
}
75
76
//------------------------------------------------------ test specific fields
77
78
static final int maxCase = 5;
79
static final String[] brakeMethods = {
80
"caseRun",
81
"m01",
82
"m02",
83
"m03",
84
"caseRun"
85
};
86
static final int[][] checkedLines = {
87
{ 152, 152, 152},
88
{ 182, 182, 182},
89
{ 187, 187, 187},
90
{ 193, 193, 193},
91
{ 169, 169, 196}
92
};
93
94
static final String debuggeeThreadName = prefix + ".Thread0crstepreq010a";
95
96
//------------------------------------------------------ mutable common methods
97
98
public static int run (String argv[], PrintStream out) {
99
100
int exitStatus = new crstepreq010().runThis(argv, out);
101
System.out.println (exitStatus == PASSED ? "TEST PASSED" : "TEST FAILED");
102
return exitCode;
103
}
104
105
private int runThis(String argv[], PrintStream out) {
106
107
argHandler = new ArgumentHandler(argv);
108
log = new Log(out, argHandler);
109
waitTime = argHandler.getWaitTime() * 60000;
110
111
try {
112
113
Binder binder = new Binder(argHandler, log);
114
debuggee = binder.bindToDebugee(debuggeeName);
115
debuggee.redirectStderr(log, "");
116
eventRManager = debuggee.getEventRequestManager();
117
118
vm = debuggee.VM();
119
eventRManager = vm.eventRequestManager();
120
121
debuggeeClass = waitForDebuggeeClassPrepared();
122
123
execTest();
124
125
debuggee.resume();
126
getEventSet();
127
if (eventIterator.nextEvent() instanceof VMDeathEvent) {
128
display("Waiting for the debuggee's finish...");
129
debuggee.waitFor();
130
131
display("Getting the debuggee's exit status.");
132
int status = debuggee.getStatus();
133
if (status != (PASSED + PASS_BASE)) {
134
complain("Debuggee returned UNEXPECTED exit status: " + status);
135
exitCode = Consts.TEST_FAILED;
136
}
137
} else {
138
throw new TestBug("Last event is not the VMDeathEvent");
139
}
140
141
} catch (VMDisconnectedException e) {
142
exitCode = Consts.TEST_FAILED;
143
complain("The test cancelled due to VMDisconnectedException.");
144
e.printStackTrace(out);
145
display("Trying: vm.process().destroy();");
146
if (vm != null) {
147
Process vmProcess = vm.process();
148
if (vmProcess != null) {
149
vmProcess.destroy();
150
}
151
}
152
153
} catch (Exception e) {
154
exitCode = Consts.TEST_FAILED;
155
complain("Unexpected Exception: " + e.getMessage());
156
e.printStackTrace(out);
157
complain("The test has not finished normally. Forcing: vm.exit().");
158
if (vm != null) {
159
vm.exit(PASSED + PASS_BASE);
160
}
161
debuggee.resume();
162
getEventSet();
163
}
164
165
return exitCode;
166
}
167
168
//--------------------------------------------------------- mutable common methods
169
170
private void execTest() {
171
BreakpointRequest bpRequest = setBreakpoint( null,
172
debuggeeClass,
173
"methodForCommunication",
174
lineForBreak,
175
"breakForCommunication");
176
bpRequest.enable();
177
178
StepRequest stepRequest = null;
179
180
display("TESTING BEGINS");
181
for (int testCase = 0; testCase < maxCase && instruction != quit; testCase++) {
182
183
instruction = getInstruction();
184
if (instruction == quit) {
185
vm.resume();
186
break;
187
}
188
189
display(":: CASE # " + testCase);
190
stepRequest = setStepRequest( bpRequest,
191
"thread" + testCase,
192
testCase,
193
"stepRequest" + testCase );
194
195
checkStepEvent( stepRequest,
196
"thread" + testCase,
197
testCase );
198
}
199
display("TESTING ENDS");
200
}
201
202
//--------------------------------------------------------- test specific methods
203
204
private StepRequest setStepRequest ( BreakpointRequest bpRequest,
205
String threadName,
206
int testCase,
207
String property ) {
208
StepRequest stepRequest = null;
209
for (;;) {
210
display("Wait for initial brakepoint event in " + threadName);
211
BreakpointEvent bpEvent = (BreakpointEvent)waitForEvent(bpRequest);
212
213
// check location of breakpoint event
214
int lineOfEvent = ((LocatableEvent)bpEvent).location().lineNumber();
215
if (lineOfEvent != lineForBreak) {
216
complain("Wrong line number of initial brakepoint event for " + threadName);
217
complain("\texpected value : " + lineForBreak + "; got one : " + lineOfEvent);
218
break;
219
}
220
221
display("Getting mirror of thread: " + threadName);
222
ThreadReference thread = debuggee.threadByNameOrThrow(threadName);
223
224
display("Getting ReferenceType of thread: " + threadName);
225
ReferenceType debuggeeThread = debuggee.classByName(debuggeeThreadName);
226
227
// set second breakpoint to suspend checked thread at the right location before
228
// setting step request
229
BreakpointRequest bpRequest1 = setBreakpoint( thread,
230
debuggeeThread,
231
brakeMethods[testCase],
232
checkedLines[testCase][0],
233
"");
234
bpRequest1.addCountFilter(1);
235
bpRequest1.enable();
236
237
display("Wait for additional brakepoint event in " + threadName);
238
bpEvent = (BreakpointEvent)waitForEvent(bpRequest1);
239
240
// check location of breakpoint event
241
lineOfEvent = ((LocatableEvent)bpEvent).location().lineNumber();
242
if (lineOfEvent != checkedLines[testCase][0]) {
243
complain("Wrong line number of additional brakepoint event for " + threadName);
244
complain("\texpected value : " + checkedLines[testCase][0] + "; got one : " + lineOfEvent);
245
break;
246
}
247
248
display("Setting a step request in thread: " + thread);
249
try {
250
stepRequest = eventRManager.createStepRequest ( thread,
251
StepRequest.STEP_MIN,
252
StepRequest.STEP_INTO );
253
stepRequest.putProperty("number", property);
254
} catch ( Exception e1 ) {
255
complain("setStepRequest(): unexpected Exception while creating StepRequest: " + e1);
256
break;
257
}
258
break;
259
}
260
if (stepRequest == null) {
261
throw new Failure("setStepRequest(): StepRequest has not been set up.");
262
}
263
display("setStepRequest(): StepRequest has been set up.");
264
return stepRequest;
265
}
266
267
private void checkStepEvent ( StepRequest stepRequest,
268
String threadName,
269
int testCase ) {
270
stepRequest.enable();
271
272
display("waiting for first StepEvent in " + threadName);
273
Event newEvent = waitForEvent(stepRequest);
274
display("got first StepEvent");
275
276
display("CHECK1 for line location of first StepEvent.");
277
int lineOfEvent = ((LocatableEvent)newEvent).location().lineNumber();
278
if (lineOfEvent != checkedLines[testCase][1]) {
279
complain("CHECK1 for line location of first StepEvent FAILED for CASE # " + testCase);
280
complain("\texpected value : " + checkedLines[testCase][1] + "; got one : " + lineOfEvent);
281
exitCode = FAILED;
282
} else {
283
display("CHECK1 PASSED");
284
}
285
286
display("waiting for second StepEvent in " + threadName);
287
newEvent = waitForEvent(stepRequest);
288
display("got second StepEvent");
289
290
display("CHECK2 for line location of second StepEvent.");
291
lineOfEvent = ((LocatableEvent)newEvent).location().lineNumber();
292
if (lineOfEvent != checkedLines[testCase][2]) {
293
complain("CHECK2 for line location of second StepEvent FAILED for CASE # " + testCase);
294
complain("\texpected value : " + checkedLines[testCase][2] + "; got one : " + lineOfEvent);
295
exitCode = FAILED;
296
} else {
297
display("CHECK2 PASSED");
298
}
299
300
stepRequest.disable();
301
eventRManager.deleteEventRequest(stepRequest);
302
stepRequest = null;
303
display("request for StepEvent in " + threadName + " is deleted");
304
}
305
306
//--------------------------------------------------------- immutable common methods
307
308
void display(String msg) {
309
log.display("debugger > " + msg);
310
}
311
312
void complain(String msg) {
313
log.complain("debugger FAILURE > " + msg);
314
}
315
316
/**
317
* Sets up a breakpoint at given line number within a given method in a given class
318
* for a given thread.
319
*
320
* Returns a BreakpointRequest object in case of success, otherwise throws Failure.
321
*/
322
private BreakpointRequest setBreakpoint ( ThreadReference thread,
323
ReferenceType testedClass,
324
String methodName,
325
int bpLine,
326
String property) {
327
328
display("Setting a breakpoint in :");
329
display(" thread: " + thread + "; class: " + testedClass +
330
"; method: " + methodName + "; line: " + bpLine + "; property: " + property);
331
332
List allLineLocations = null;
333
Location lineLocation = null;
334
BreakpointRequest breakpRequest = null;
335
336
try {
337
Method method = (Method) testedClass.methodsByName(methodName).get(0);
338
339
allLineLocations = method.allLineLocations();
340
341
display("Getting location for breakpoint...");
342
Iterator locIterator = allLineLocations.iterator();
343
while (locIterator.hasNext()) {
344
Location curLocation = (Location)locIterator.next();
345
int curNumber = curLocation.lineNumber();
346
if (curLocation.lineNumber() == bpLine) {
347
lineLocation = curLocation;
348
break;
349
}
350
}
351
if (lineLocation == null) {
352
throw new TestBug("Incorrect line number of methods' location");
353
}
354
355
try {
356
breakpRequest = eventRManager.createBreakpointRequest(lineLocation);
357
if (thread != null) {
358
breakpRequest.addThreadFilter(thread);
359
}
360
breakpRequest.putProperty("number", property);
361
} catch ( Exception e1 ) {
362
complain("setBreakpoint(): unexpected Exception while creating BreakpointRequest: " + e1);
363
breakpRequest = null;
364
}
365
} catch ( Exception e2 ) {
366
complain("setBreakpoint(): unexpected Exception while getting locations: " + e2);
367
breakpRequest = null;
368
}
369
370
if (breakpRequest == null) {
371
throw new Failure("setBreakpoint(): A breakpoint has not been set up.");
372
}
373
374
display("setBreakpoint(): A breakpoint has been set up.");
375
return breakpRequest;
376
}
377
378
private Event waitForEvent (EventRequest eventRequest) {
379
vm.resume();
380
Event resultEvent = null;
381
try {
382
eventSet = null;
383
eventIterator = null;
384
eventSet = vm.eventQueue().remove(waitTime);
385
if (eventSet == null) {
386
throw new Failure("TIMEOUT while waiting for an event");
387
}
388
eventIterator = eventSet.eventIterator();
389
while (eventIterator.hasNext()) {
390
Event curEvent = eventIterator.nextEvent();
391
if (curEvent instanceof VMDisconnectEvent) {
392
throw new Failure("Unexpected VMDisconnectEvent received.");
393
} else {
394
EventRequest evRequest = curEvent.request();
395
if (evRequest != null && evRequest.equals(eventRequest)) {
396
display("Requested event received: " + curEvent.toString() +
397
"; request property: " + (String) curEvent.request().getProperty("number"));
398
resultEvent = curEvent;
399
break;
400
} else {
401
throw new Failure("Unexpected event received: " + curEvent.toString());
402
}
403
}
404
}
405
} catch (Exception e) {
406
throw new Failure("Unexpected exception while waiting for an event: " + e);
407
}
408
return resultEvent;
409
}
410
411
private Event waitForEvent () {
412
vm.resume();
413
Event resultEvent = null;
414
try {
415
eventSet = null;
416
eventIterator = null;
417
eventSet = vm.eventQueue().remove(waitTime);
418
if (eventSet == null) {
419
throw new Failure("TIMEOUT while waiting for an event");
420
}
421
eventIterator = eventSet.eventIterator();
422
while (eventIterator.hasNext()) {
423
resultEvent = eventIterator.nextEvent();
424
if (resultEvent instanceof VMDisconnectEvent) {
425
throw new Failure("Unexpected VMDisconnectEvent received.");
426
}
427
}
428
} catch (Exception e) {
429
throw new Failure("Unexpected exception while waiting for an event: " + e);
430
}
431
return resultEvent;
432
}
433
434
private void getEventSet() {
435
try {
436
eventSet = vm.eventQueue().remove(waitTime);
437
if (eventSet == null) {
438
throw new Failure("TIMEOUT while waiting for an event");
439
}
440
eventIterator = eventSet.eventIterator();
441
} catch (Exception e) {
442
throw new Failure("getEventSet(): Unexpected exception while waiting for an event: " + e);
443
}
444
}
445
446
private ReferenceType waitForDebuggeeClassPrepared () {
447
display("Creating request for ClassPrepareEvent for debuggee.");
448
ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();
449
cpRequest.addClassFilter(debuggeeName);
450
cpRequest.addCountFilter(1);
451
cpRequest.enable();
452
453
ClassPrepareEvent event = (ClassPrepareEvent) waitForEvent(cpRequest);
454
cpRequest.disable();
455
456
if (!event.referenceType().name().equals(debuggeeName)) {
457
throw new Failure("Unexpected class name for ClassPrepareEvent : " + debuggeeClass.name());
458
}
459
return event.referenceType();
460
}
461
462
private int getInstruction () {
463
if (debuggeeClass == null) {
464
throw new Failure("getInstruction() :: debuggeeClass reference is null");
465
}
466
return ((IntegerValue) (debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();
467
}
468
469
private void setInstruction (String instructionField) {
470
if (debuggeeClass == null) {
471
throw new Failure("getInstruction() :: debuggeeClass reference is null");
472
}
473
Field instrField = debuggeeClass.fieldByName("instruction");
474
IntegerValue instrValue = (IntegerValue) (debuggeeClass.getValue(debuggeeClass.fieldByName(instructionField)));
475
try {
476
((ClassType)debuggeeClass).setValue(instrField, instrValue );
477
} catch (InvalidTypeException e1) {
478
throw new Failure("Caught unexpected InvalidTypeException while setting value '" + instructionField + "' for instruction field");
479
} catch (ClassNotLoadedException e2) {
480
throw new Failure("Caught unexpected ClassNotLoadedException while setting value '" + instructionField + "' for instruction field");
481
}
482
}
483
}
484
485