Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/suspendPolicy/suspendpolicy002.java
41161 views
/*1* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.jdi.EventSet.suspendPolicy;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdi.*;2829import com.sun.jdi.*;30import com.sun.jdi.event.*;31import com.sun.jdi.request.*;3233import java.util.*;34import java.io.*;3536/**37* The test for the implementation of an object of the type <BR>38* EventSet. <BR>39* <BR>40* The test checks that results of the method <BR>41* <code>com.sun.jdi.EventSet.suspendPolicy()</code> <BR>42* complies with its spec. <BR>43* <BR>44* The test checks that for an AccessWatchpointEvent set, <BR>45* the method returns values corresponding to one <BR>46* which suspends the most threads. <BR>47* The cases to check include event sets containing <BR>48* one to three threads with all possible combinations: <BR>49* NONE, THREAD, ALL, NONE+THREAD, NONE+ALL, <BR>50* THREAD+ALL, NONE+THREAD+ALL <BR>51* <BR>52* The test has three phases and works as follows. <BR>53* <BR>54* In first phase, <BR>55* upon launching debuggee's VM which will be suspended, <BR>56* a debugger waits for the VMStartEvent within a predefined <BR>57* time interval. If no the VMStartEvent received, the test is FAILED. <BR>58* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>59* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>60* and waits for the event within the predefined time interval. <BR>61* If no the ClassPrepareEvent received, the test is FAILED. <BR>62* Upon getting the ClassPrepareEvent, <BR>63* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>64* within debuggee's special methodForCommunication(). <BR>65* <BR>66* In second phase to check the above, <BR>67* the debugger and the debuggee perform the following loop. <BR>68* - The debugger sets up a AccessWatchpointRequest, resumes <BR>69* the debuggee, and waits for the AccessWatchpointEvent. <BR>70* - The debuggee invokes the special method which makes access to <BR>71* a predefined variable to be resulting in the event. <BR>72* - Upon getting new event, the debugger <BR>73* performs the check corresponding to the event. <BR>74* <BR>75* Note. To inform each other of needed actions, the debugger and <BR>76* and the debuggee use debuggee's variable "instruction". <BR>77* In third phase, at the end, <BR>78* the debuggee changes the value of the "instruction" <BR>79* to inform the debugger of checks finished, and both end. <BR>80* <BR>81*/8283public class suspendpolicy002 extends JDIBase {8485public static void main (String argv[]) {8687int result = run(argv, System.out);8889System.exit(result + PASS_BASE);90}9192public static int run (String argv[], PrintStream out) {9394int exitCode = new suspendpolicy002().runThis(argv, out);9596if (exitCode != PASSED) {97System.out.println("TEST FAILED");98}99return testExitCode;100}101// ************************************************ test parameters102103private String debuggeeName =104"nsk.jdi.EventSet.suspendPolicy.suspendpolicy002a";105106private String testedClassName =107"nsk.jdi.EventSet.suspendPolicy.suspendpolicy002aTestClass";108109//====================================================== test program110111private int runThis (String argv[], PrintStream out) {112113argsHandler = new ArgumentHandler(argv);114logHandler = new Log(out, argsHandler);115Binder binder = new Binder(argsHandler, logHandler);116117waitTime = argsHandler.getWaitTime() * 60000;118119try {120log2("launching a debuggee :");121log2(" " + debuggeeName);122if (argsHandler.verbose()) {123debuggee = binder.bindToDebugee(debuggeeName + " -vbs");124} else {125debuggee = binder.bindToDebugee(debuggeeName);126}127if (debuggee == null) {128log3("ERROR: no debuggee launched");129return FAILED;130}131log2("debuggee launched");132} catch ( Exception e ) {133log3("ERROR: Exception : " + e);134log2(" test cancelled");135return FAILED;136}137138debuggee.redirectOutput(logHandler);139140vm = debuggee.VM();141142eventQueue = vm.eventQueue();143if (eventQueue == null) {144log3("ERROR: eventQueue == null : TEST ABORTED");145vm.exit(PASS_BASE);146return FAILED;147}148149log2("invocation of the method runTest()");150switch (runTest()) {151152case 0 : log2("test phase has finished normally");153log2(" waiting for the debuggee to finish ...");154debuggee.waitFor();155156log2("......getting the debuggee's exit status");157int status = debuggee.getStatus();158if (status != PASS_BASE) {159log3("ERROR: debuggee returned UNEXPECTED exit status: " +160status + " != PASS_BASE");161testExitCode = FAILED;162} else {163log2("......debuggee returned expected exit status: " +164status + " == PASS_BASE");165}166break;167168default : log3("ERROR: runTest() returned unexpected value");169170case 1 : log3("test phase has not finished normally: debuggee is still alive");171log2("......forcing: vm.exit();");172testExitCode = FAILED;173try {174vm.exit(PASS_BASE);175} catch ( Exception e ) {176log3("ERROR: Exception : e");177}178break;179180case 2 : log3("test cancelled due to VMDisconnectedException");181log2("......trying: vm.process().destroy();");182testExitCode = FAILED;183try {184Process vmProcess = vm.process();185if (vmProcess != null) {186vmProcess.destroy();187}188} catch ( Exception e ) {189log3("ERROR: Exception : e");190}191break;192}193194return testExitCode;195}196197198/*199* Return value: 0 - normal end of the test200* 1 - ubnormal end of the test201* 2 - VMDisconnectedException while test phase202*/203204private int runTest() {205206try {207testRun();208209log2("waiting for VMDeathEvent");210getEventSet();211if ( !(eventIterator.nextEvent() instanceof VMDeathEvent) ) {212log3("ERROR: last event is not the VMDeathEvent");213return 1;214}215216return 0;217218} catch ( VMDisconnectedException e ) {219log3("ERROR: VMDisconnectedException : " + e);220return 2;221} catch ( Exception e ) {222log3("ERROR: Exception : " + e);223return 1;224}225226}227228private void testRun()229throws JDITestRuntimeException, Exception {230231if ( !vm.canWatchFieldAccess() ) {232log2("......vm.canWatchFieldAccess == false :: test cancelled");233vm.exit(PASS_BASE);234return;235}236237238eventRManager = vm.eventRequestManager();239240log2("......getting ClassPrepareEvent for debuggee's class");241ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();242cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);243cpRequest.addClassFilter(debuggeeName);244cpRequest.enable();245vm.resume();246getEventSet();247cpRequest.disable();248249ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();250debuggeeClass = event.referenceType();251252if (!debuggeeClass.name().equals(debuggeeName))253throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");254log2(" received: ClassPrepareEvent for debuggeeClass");255256log2("......setting up ClassPrepareEvent for breakpointForCommunication");257258String bPointMethod = "methodForCommunication";259String lineForComm = "lineForComm";260BreakpointRequest bpRequest;261ThreadReference mainThread = debuggee.threadByNameOrThrow("main");262bpRequest = settingBreakpoint(mainThread,263debuggeeClass,264bPointMethod, lineForComm, "zero");265bpRequest.enable();266267vm.resume();268269//------------------------------------------------------ testing section270271log1(" TESTING BEGINS");272273EventRequest eventRequest1 = null;274EventRequest eventRequest2 = null;275EventRequest eventRequest3 = null;276277final int SUSPEND_POLICY = EventRequest.SUSPEND_NONE;278final int SUSPEND_NONE = EventRequest.SUSPEND_NONE;279final int SUSPEND_THREAD = EventRequest.SUSPEND_EVENT_THREAD;280final int SUSPEND_ALL = EventRequest.SUSPEND_ALL;281282int policyExpected[] = { SUSPEND_NONE,283SUSPEND_THREAD,284SUSPEND_ALL,285SUSPEND_THREAD,286SUSPEND_ALL,287SUSPEND_ALL,288SUSPEND_ALL };289int policy = 0;290291ReferenceType testClassReference = null;292293String fieldName = "var1";294295296for (int i = 0; ; i++) {297298299breakpointForCommunication();300301int instruction = ((IntegerValue)302(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();303if (instruction == 0) {304vm.resume();305break;306}307308309log1(":::::: case: # " + i);310311//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part312313switch (i) {314315case 0:316testClassReference =317(ReferenceType) vm.classesByName(testedClassName).get(0);318319eventRequest1 = settingAccessWatchpointRequest (mainThread,320testClassReference, fieldName,321SUSPEND_NONE, "AccessWatchpointRequest1");322eventRequest1.enable();323break;324325case 1:326eventRequest2 = settingAccessWatchpointRequest (mainThread,327testClassReference, fieldName,328SUSPEND_THREAD, "AccessWatchpointRequest2");329eventRequest2.enable();330break;331332case 2:333eventRequest3 = settingAccessWatchpointRequest (mainThread,334testClassReference, fieldName,335SUSPEND_ALL, "AccessWatchpointRequest3");336eventRequest3.enable();337break;338339case 3:340eventRequest1 = settingAccessWatchpointRequest (mainThread,341testClassReference, fieldName,342SUSPEND_NONE, "AccessWatchpointRequest4");343eventRequest1.enable();344eventRequest2 = settingAccessWatchpointRequest (mainThread,345testClassReference, fieldName,346SUSPEND_THREAD, "AccessWatchpointRequest5");347eventRequest2.enable();348break;349350case 4:351eventRequest1 = settingAccessWatchpointRequest (mainThread,352testClassReference, fieldName,353SUSPEND_NONE, "AccessWatchpointRequest6");354eventRequest1.enable();355eventRequest3 = settingAccessWatchpointRequest (mainThread,356testClassReference, fieldName,357SUSPEND_ALL, "AccessWatchpointRequest7");358eventRequest3.enable();359break;360361case 5:362eventRequest2 = settingAccessWatchpointRequest (mainThread,363testClassReference, fieldName,364SUSPEND_THREAD, "AccessWatchpointRequest8");365eventRequest2.enable();366eventRequest3 = settingAccessWatchpointRequest (mainThread,367testClassReference, fieldName,368SUSPEND_ALL, "AccessWatchpointRequest9");369eventRequest3.enable();370break;371372case 6:373eventRequest1 = settingAccessWatchpointRequest (mainThread,374testClassReference, fieldName,375SUSPEND_NONE, "AccessWatchpointRequest10");376eventRequest1.enable();377eventRequest2 = settingAccessWatchpointRequest (mainThread,378testClassReference, fieldName,379SUSPEND_THREAD, "AccessWatchpointRequest11");380eventRequest2.enable();381eventRequest3 = settingAccessWatchpointRequest (mainThread,382testClassReference, fieldName,383SUSPEND_ALL, "AccessWatchpointRequest12");384eventRequest3.enable();385break;386387388default:389throw new JDITestRuntimeException("** default case 2 **");390}391392log2("......waiting for new AccessWatchpointEvent : " + i);393mainThread.resume();394getEventSet();395396Event newEvent = eventIterator.nextEvent();397log2(" got new Event with propety 'number' == " + newEvent.request().getProperty("number"));398if ( !( newEvent instanceof AccessWatchpointEvent)) {399log3("ERROR: new event is not AccessWatchpointEvent");400testExitCode = FAILED;401} else {402log2(" Event is instanceof AccessWatchpointEvent");403policy = eventSet.suspendPolicy();404if (policy != policyExpected[i]) {405log3("ERROR: eventSet.suspendPolicy() != policyExpected");406log3(" eventSet.suspendPolicy() == " + policy);407log3(" policyExpected == " + policyExpected[i]);408testExitCode = FAILED;409}410}411412switch (i) {413case 0: eventRequest1.disable(); break;414case 1: eventRequest2.disable(); break;415case 2: eventRequest3.disable(); break;416case 3: eventRequest1.disable(); eventRequest2.disable(); break;417case 4: eventRequest1.disable(); eventRequest3.disable(); break;418case 5: eventRequest2.disable(); eventRequest3.disable(); break;419case 6: eventRequest1.disable(); eventRequest2.disable();420eventRequest3.disable(); break;421}422423switch (policy) {424case SUSPEND_NONE : break;425case SUSPEND_THREAD : mainThread.resume(); break;426case SUSPEND_ALL : vm.resume(); break;427default: throw new JDITestRuntimeException("** default case 3 **");428}429430//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~431}432log1(" TESTING ENDS");433return;434}435436// ============================== test's additional methods437438private AccessWatchpointRequest settingAccessWatchpointRequest (439ThreadReference thread,440ReferenceType testedClass,441String fieldName,442int suspendPolicy,443String property )444throws JDITestRuntimeException {445try {446log2("......setting up AccessWatchpointRequest:");447log2(" thread: " + thread + "; class: " + testedClass + "; fieldName: " + fieldName);448Field field = testedClass.fieldByName(fieldName);449450AccessWatchpointRequest451awr = eventRManager.createAccessWatchpointRequest(field);452awr.putProperty("number", property);453awr.addThreadFilter(thread);454awr.setSuspendPolicy(suspendPolicy);455456log2(" AccessWatchpointRequest has been set up");457return awr;458} catch ( Exception e ) {459log3("ERROR: ATTENTION: Exception within settingAccessWatchpointRequest() : " + e);460log3(" AccessWatchpointRequest HAS NOT BEEN SET UP");461throw new JDITestRuntimeException("** FAILURE to set up AccessWatchpointRequest **");462}463}464465}466467468