Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/suspendPolicy/suspendpolicy003.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 a ModificationWatchpointEvent 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 ModificationWatchpointRequest, resumes <BR>69* the debuggee, and waits for the ModificationWatchpointEvent. <BR>70* - The debuggee invokes the special method which makes modification <BR>71* of 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 suspendpolicy003 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 suspendpolicy003().runThis(argv, out);9596if (exitCode != PASSED) {97System.out.println("TEST FAILED");98}99return testExitCode;100}101102// ************************************************ test parameters103104private String debuggeeName =105"nsk.jdi.EventSet.suspendPolicy.suspendpolicy003a";106107private String testedClassName =108"nsk.jdi.EventSet.suspendPolicy.suspendpolicy003aTestClass";109110//====================================================== test program111112private int runThis (String argv[], PrintStream out) {113114argsHandler = new ArgumentHandler(argv);115logHandler = new Log(out, argsHandler);116Binder binder = new Binder(argsHandler, logHandler);117118waitTime = argsHandler.getWaitTime() * 60000;119120try {121log2("launching a debuggee :");122log2(" " + debuggeeName);123if (argsHandler.verbose()) {124debuggee = binder.bindToDebugee(debuggeeName + " -vbs");125} else {126debuggee = binder.bindToDebugee(debuggeeName);127}128if (debuggee == null) {129log3("ERROR: no debuggee launched");130return FAILED;131}132log2("debuggee launched");133} catch ( Exception e ) {134log3("ERROR: Exception : " + e);135log2(" test cancelled");136return FAILED;137}138139debuggee.redirectOutput(logHandler);140141vm = debuggee.VM();142143eventQueue = vm.eventQueue();144if (eventQueue == null) {145log3("ERROR: eventQueue == null : TEST ABORTED");146vm.exit(PASS_BASE);147return FAILED;148}149150log2("invocation of the method runTest()");151switch (runTest()) {152153case 0 : log2("test phase has finished normally");154log2(" waiting for the debuggee to finish ...");155debuggee.waitFor();156157log2("......getting the debuggee's exit status");158int status = debuggee.getStatus();159if (status != PASS_BASE) {160log3("ERROR: debuggee returned UNEXPECTED exit status: " +161status + " != PASS_BASE");162testExitCode = FAILED;163} else {164log2("......debuggee returned expected exit status: " +165status + " == PASS_BASE");166}167break;168169default : log3("ERROR: runTest() returned unexpected value");170171case 1 : log3("test phase has not finished normally: debuggee is still alive");172log2("......forcing: vm.exit();");173testExitCode = FAILED;174try {175vm.exit(PASS_BASE);176} catch ( Exception e ) {177log3("ERROR: Exception : e");178}179break;180181case 2 : log3("test cancelled due to VMDisconnectedException");182log2("......trying: vm.process().destroy();");183testExitCode = FAILED;184try {185Process vmProcess = vm.process();186if (vmProcess != null) {187vmProcess.destroy();188}189} catch ( Exception e ) {190log3("ERROR: Exception : e");191}192break;193}194195return testExitCode;196}197198199/*200* Return value: 0 - normal end of the test201* 1 - ubnormal end of the test202* 2 - VMDisconnectedException while test phase203*/204205private int runTest() {206207try {208testRun();209210log2("waiting for VMDeathEvent");211getEventSet();212if ( !(eventIterator.nextEvent() instanceof VMDeathEvent) ) {213log3("ERROR: last event is not the VMDeathEvent");214return 1;215}216217log2("waiting for VMDisconnectEvent");218getEventSet();219if ( !(eventIterator.nextEvent() instanceof VMDisconnectEvent) ) {220log3("ERROR: last event is not the VMDisconnectEvent");221return 1;222}223224return 0;225226} catch ( VMDisconnectedException e ) {227log3("ERROR: VMDisconnectedException : " + e);228return 2;229} catch ( Exception e ) {230log3("ERROR: Exception : " + e);231return 1;232}233234}235236private void testRun()237throws JDITestRuntimeException, Exception {238239if ( !vm.canWatchFieldModification() ) {240log2("......vm.canWatchFieldModification == false :: test cancelled");241vm.exit(PASS_BASE);242return;243}244245eventRManager = vm.eventRequestManager();246247log2("......getting ClassPrepareEvent for debuggee's class");248ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();249cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);250cpRequest.addClassFilter(debuggeeName);251cpRequest.enable();252vm.resume();253getEventSet();254cpRequest.disable();255256ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();257debuggeeClass = event.referenceType();258259if (!debuggeeClass.name().equals(debuggeeName))260throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");261log2(" received: ClassPrepareEvent for debuggeeClass");262263log2("......setting up ClassPrepareEvent for breakpointForCommunication");264265String bPointMethod = "methodForCommunication";266String lineForComm = "lineForComm";267BreakpointRequest bpRequest;268ThreadReference mainThread = debuggee.threadByNameOrThrow("main");269bpRequest = settingBreakpoint(mainThread,270debuggeeClass,271bPointMethod, lineForComm, "zero");272bpRequest.enable();273274vm.resume();275276//------------------------------------------------------ testing section277278log1(" TESTING BEGINS");279280EventRequest eventRequest1 = null;281EventRequest eventRequest2 = null;282EventRequest eventRequest3 = null;283284final int SUSPEND_POLICY = EventRequest.SUSPEND_NONE;285final int SUSPEND_NONE = EventRequest.SUSPEND_NONE;286final int SUSPEND_THREAD = EventRequest.SUSPEND_EVENT_THREAD;287final int SUSPEND_ALL = EventRequest.SUSPEND_ALL;288289int policyExpected[] = { SUSPEND_NONE,290SUSPEND_THREAD,291SUSPEND_ALL,292SUSPEND_THREAD,293SUSPEND_ALL,294SUSPEND_ALL,295SUSPEND_ALL };296int policy = 0;297298299ReferenceType testClassReference = null;300301String fieldName = "var2";302303304for (int i = 0; ; i++) {305306307breakpointForCommunication();308309int instruction = ((IntegerValue)310(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();311if (instruction == 0) {312vm.resume();313break;314}315316317log1(":::::: case: # " + i);318319//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part320321switch (i) {322323case 0:324testClassReference =325(ReferenceType) vm.classesByName(testedClassName).get(0);326327eventRequest1 = settingModificationWatchpointRequest (mainThread,328testClassReference, fieldName,329SUSPEND_NONE, "ModificationWatchpointRequest1");330eventRequest1.enable();331break;332333case 1:334eventRequest2 = settingModificationWatchpointRequest (mainThread,335testClassReference, fieldName,336SUSPEND_THREAD, "ModificationWatchpointRequest2");337eventRequest2.enable();338break;339340case 2:341eventRequest3 = settingModificationWatchpointRequest (mainThread,342testClassReference, fieldName,343SUSPEND_ALL, "ModificationWatchpointRequest3");344eventRequest3.enable();345break;346347case 3:348eventRequest1 = settingModificationWatchpointRequest (mainThread,349testClassReference, fieldName,350SUSPEND_NONE, "ModificationWatchpointRequest4");351eventRequest1.enable();352eventRequest2 = settingModificationWatchpointRequest (mainThread,353testClassReference, fieldName,354SUSPEND_THREAD, "ModificationWatchpointRequest5");355eventRequest2.enable();356break;357358case 4:359eventRequest1 = settingModificationWatchpointRequest (mainThread,360testClassReference, fieldName,361SUSPEND_NONE, "ModificationWatchpointRequest6");362eventRequest1.enable();363eventRequest3 = settingModificationWatchpointRequest (mainThread,364testClassReference, fieldName,365SUSPEND_ALL, "ModificationWatchpointRequest7");366eventRequest3.enable();367break;368369case 5:370eventRequest2 = settingModificationWatchpointRequest (mainThread,371testClassReference, fieldName,372SUSPEND_THREAD, "ModificationWatchpointRequest8");373eventRequest2.enable();374eventRequest3 = settingModificationWatchpointRequest (mainThread,375testClassReference, fieldName,376SUSPEND_ALL, "ModificationWatchpointRequest9");377eventRequest3.enable();378break;379380case 6:381eventRequest1 = settingModificationWatchpointRequest (mainThread,382testClassReference, fieldName,383SUSPEND_NONE, "ModificationWatchpointRequest10");384eventRequest1.enable();385eventRequest2 = settingModificationWatchpointRequest (mainThread,386testClassReference, fieldName,387SUSPEND_THREAD, "ModificationWatchpointRequest11");388eventRequest2.enable();389eventRequest3 = settingModificationWatchpointRequest (mainThread,390testClassReference, fieldName,391SUSPEND_ALL, "ModificationWatchpointRequest12");392eventRequest3.enable();393break;394395396default:397throw new JDITestRuntimeException("** default case 2 **");398}399400log2("......waiting for new AccessWatchpointEvent : " + i);401mainThread.resume();402getEventSet();403404Event newEvent = eventIterator.nextEvent();405log2(" got new Event with propety 'number' == " + newEvent.request().getProperty("number"));406if ( !( newEvent instanceof ModificationWatchpointEvent)) {407log3("ERROR: new event is not ModificationWatchpointEvent");408testExitCode = FAILED;409} else {410log2(" Event is instanceof ModificationWatchpointEvent");411policy = eventSet.suspendPolicy();412if (policy != policyExpected[i]) {413log3("ERROR: eventSet.suspendPolicy() != policyExpected");414log3(" eventSet.suspendPolicy() == " + policy);415log3(" policyExpected == " + policyExpected[i]);416testExitCode = FAILED;417}418}419420switch (i) {421case 0: eventRequest1.disable(); break;422case 1: eventRequest2.disable(); break;423case 2: eventRequest3.disable(); break;424case 3: eventRequest1.disable(); eventRequest2.disable(); break;425case 4: eventRequest1.disable(); eventRequest3.disable(); break;426case 5: eventRequest2.disable(); eventRequest3.disable(); break;427case 6: eventRequest1.disable(); eventRequest2.disable();428eventRequest3.disable(); break;429}430431switch (policy) {432case SUSPEND_NONE : break;433case SUSPEND_THREAD : mainThread.resume(); break;434case SUSPEND_ALL : vm.resume(); break;435default: throw new JDITestRuntimeException("** default case 3 **");436}437438//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~439}440log1(" TESTING ENDS");441return;442}443444445// ============================== test's additional methods446447private ModificationWatchpointRequest settingModificationWatchpointRequest (448ThreadReference thread,449ReferenceType testedClass,450String fieldName,451int suspendPolicy,452String property )453throws JDITestRuntimeException {454try {455log2("......setting up ModificationWatchpointRequest:");456log2(" thread: " + thread + "; class: " + testedClass + "; fieldName: " + fieldName);457Field field = testedClass.fieldByName(fieldName);458459ModificationWatchpointRequest460awr = eventRManager.createModificationWatchpointRequest(field);461awr.putProperty("number", property);462awr.addThreadFilter(thread);463awr.setSuspendPolicy(suspendPolicy);464465log2(" ModificationWatchpointRequest has been set up");466return awr;467} catch ( Exception e ) {468log3("ERROR: ATTENTION: Exception within settingModificationWatchpointRequest() : " + e);469log3(" ModificationWatchpointRequest HAS NOT BEEN SET UP");470throw new JDITestRuntimeException("** FAILURE to set up ModificationWatchpointRequest **");471}472}473474}475476477