Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/suspendPolicy/suspendpolicy001.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 ClassPrepareEvent 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* The debugger then prepares an array containing sets of <BR>66* enabled requests for each combination to check. <BR>67* <BR>68* In second phase to check the above, <BR>69* the debugger and the debuggee perform the following. <BR>70* - The debugger resumes the debuggee and waits for ClassPrepareEvents.<BR>71* - The debuggee makes new checked classes prepared and invokes then <BR>72* the methodForCommunication to be suspended and <BR>73* to inform the debugger with the event. <BR>74* - Upon getting the ClassPrepareEvents, <BR>75* the debugger performs the check required. <BR>76* Note. To inform each other of needed actions, the debugger and <BR>77* and the debuggee use debuggee's variable "instruction". <BR>78* <BR>79* In third phase when at the end, <BR>80* the debuggee changes the value of the "instruction" <BR>81* to inform the debugger of checks finished, and both end. <BR>82* <BR>83*/8485public class suspendpolicy001 extends JDIBase {8687public static void main (String argv[]) {8889int result = run(argv, System.out);9091System.exit(result + PASS_BASE);92}9394public static int run (String argv[], PrintStream out) {9596int exitCode = new suspendpolicy001().runThis(argv, out);9798if (exitCode != PASSED) {99System.out.println("TEST FAILED");100}101return testExitCode;102}103104// ************************************************ test parameters105106private String debuggeeName =107"nsk.jdi.EventSet.suspendPolicy.suspendpolicy001a";108109//====================================================== test program110private int runThis (String argv[], PrintStream out) {111112argsHandler = new ArgumentHandler(argv);113logHandler = new Log(out, argsHandler);114Binder binder = new Binder(argsHandler, logHandler);115116waitTime = argsHandler.getWaitTime() * 60000;117118try {119log2("launching a debuggee :");120log2(" " + debuggeeName);121if (argsHandler.verbose()) {122debuggee = binder.bindToDebugee(debuggeeName + " -vbs");123} else {124debuggee = binder.bindToDebugee(debuggeeName);125}126if (debuggee == null) {127log3("ERROR: no debuggee launched");128return FAILED;129}130log2("debuggee launched");131} catch ( Exception e ) {132log3("ERROR: Exception : " + e);133log2(" test cancelled");134return FAILED;135}136137debuggee.redirectOutput(logHandler);138139vm = debuggee.VM();140141eventQueue = vm.eventQueue();142if (eventQueue == null) {143log3("ERROR: eventQueue == null : TEST ABORTED");144vm.exit(PASS_BASE);145return FAILED;146}147148log2("invocation of the method runTest()");149switch (runTest()) {150151case 0 : log2("test phase has finished normally");152log2(" waiting for the debuggee to finish ...");153debuggee.waitFor();154155log2("......getting the debuggee's exit status");156int status = debuggee.getStatus();157if (status != PASS_BASE) {158log3("ERROR: debuggee returned UNEXPECTED exit status: " +159status + " != PASS_BASE");160testExitCode = FAILED;161} else {162log2("......debuggee returned expected exit status: " +163status + " == PASS_BASE");164}165break;166167default : log3("ERROR: runTest() returned unexpected value");168169case 1 : log3("test phase has not finished normally: debuggee is still alive");170log2("......forcing: vm.exit();");171testExitCode = FAILED;172try {173vm.exit(PASS_BASE);174} catch ( Exception e ) {175log3("ERROR: Exception : e");176}177break;178179case 2 : log3("test cancelled due to VMDisconnectedException");180log2("......trying: vm.process().destroy();");181testExitCode = FAILED;182try {183Process vmProcess = vm.process();184if (vmProcess != null) {185vmProcess.destroy();186}187} catch ( Exception e ) {188log3("ERROR: Exception : e");189}190break;191}192193return testExitCode;194}195196197/*198* Return value: 0 - normal end of the test199* 1 - ubnormal end of the test200* 2 - VMDisconnectedException while test phase201*/202203private int runTest() {204205try {206testRun();207208log2("waiting for VMDeathEvent");209getEventSet();210if ( !(eventIterator.nextEvent() instanceof VMDeathEvent) ) {211log3("ERROR: last event is not the VMDeathEvent");212return 1;213}214215log2("waiting for VMDisconnectEvent");216getEventSet();217if ( !(eventIterator.nextEvent() instanceof VMDisconnectEvent) ) {218log3("ERROR: last event is not the VMDisconnectEvent");219return 1;220}221222return 0;223224} catch ( VMDisconnectedException e ) {225log3("ERROR: VMDisconnectedException : " + e);226return 2;227} catch ( Exception e ) {228log3("ERROR: Exception : " + e);229return 1;230}231232}233234private void testRun()235throws JDITestRuntimeException, Exception {236237eventRManager = vm.eventRequestManager();238239log2("......getting ClassPrepareEvent for debuggee's class");240ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();241cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);242cpRequest.addClassFilter(debuggeeName);243cpRequest.enable();244vm.resume();245getEventSet();246cpRequest.disable();247248ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();249debuggeeClass = event.referenceType();250251if (!debuggeeClass.name().equals(debuggeeName))252throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");253log2(" received: ClassPrepareEvent for debuggeeClass");254255log2("......setting up ClassPrepareEvent for breakpointForCommunication");256257String bPointMethod = "methodForCommunication";258String lineForComm = "lineForComm";259BreakpointRequest bpRequest;260ThreadReference mainThread = debuggee.threadByNameOrThrow("main");261bpRequest = settingBreakpoint(mainThread,262debuggeeClass,263bPointMethod, lineForComm, "zero");264bpRequest.enable();265266//------------------------------------------------------ testing section267268log1(" TESTING BEGINS");269270int classFlags = 0;271272String classNames[] = {273"nsk.jdi.EventSet.suspendPolicy.TestClass0",274"nsk.jdi.EventSet.suspendPolicy.TestClass1",275"nsk.jdi.EventSet.suspendPolicy.TestClass2",276"nsk.jdi.EventSet.suspendPolicy.TestClass3",277"nsk.jdi.EventSet.suspendPolicy.TestClass4",278"nsk.jdi.EventSet.suspendPolicy.TestClass5",279"nsk.jdi.EventSet.suspendPolicy.TestClass6"280};281282final int SUSPEND_POLICY = EventRequest.SUSPEND_NONE;283final int SUSPEND_NONE = EventRequest.SUSPEND_NONE;284final int SUSPEND_THREAD = EventRequest.SUSPEND_EVENT_THREAD;285final int SUSPEND_ALL = EventRequest.SUSPEND_ALL;286287int policyExpected[] = { SUSPEND_NONE,288SUSPEND_THREAD,289SUSPEND_ALL,290SUSPEND_THREAD,291SUSPEND_ALL,292SUSPEND_ALL,293SUSPEND_ALL };294int policy = 0;295296EventRequest eventRequests[][] = new EventRequest[classNames.length][3];297298for ( int nr = 0; nr < eventRequests.length; nr++) {299log2("......setting up ClassPrepareRequest for TestClass" + nr);300switch (nr) {301302case 0:303eventRequests[nr][0] = settingClassPrepareRequest(classNames[nr],304SUSPEND_NONE, "ClassPrepareRequest" + nr);305eventRequests[nr][0].enable();306break;307308case 1:309eventRequests[nr][1] = settingClassPrepareRequest(classNames[nr],310SUSPEND_THREAD, "ClassPrepareRequest" + nr);311eventRequests[nr][1].enable();312break;313314case 2:315eventRequests[nr][2] = settingClassPrepareRequest(classNames[nr],316SUSPEND_ALL, "ClassPrepareRequest" + nr);317eventRequests[nr][2].enable();318break;319320case 3:321eventRequests[nr][0] = settingClassPrepareRequest(classNames[nr],322SUSPEND_NONE, "ClassPrepareRequest" + nr);323eventRequests[nr][0].enable();324eventRequests[nr][1] = settingClassPrepareRequest(classNames[nr],325SUSPEND_THREAD, "ClassPrepareRequest" + nr);326eventRequests[nr][1].enable();327break;328329case 4:330eventRequests[nr][0] = settingClassPrepareRequest(classNames[nr],331SUSPEND_NONE, "ClassPrepareRequest" + nr);332eventRequests[nr][0].enable();333eventRequests[nr][2] = settingClassPrepareRequest(classNames[nr],334SUSPEND_ALL, "ClassPrepareRequest" + nr);335eventRequests[nr][2].enable();336break;337338case 5:339eventRequests[nr][1] = settingClassPrepareRequest(classNames[nr],340SUSPEND_THREAD, "ClassPrepareRequest" + nr);341eventRequests[nr][1].enable();342eventRequests[nr][2] = settingClassPrepareRequest(classNames[nr],343SUSPEND_ALL, "ClassPrepareRequest" + nr);344eventRequests[nr][2].enable();345break;346347case 6:348eventRequests[nr][0] = settingClassPrepareRequest(classNames[nr],349SUSPEND_NONE, "ClassPrepareRequest" + nr);350eventRequests[nr][0].enable();351eventRequests[nr][1] = settingClassPrepareRequest(classNames[nr],352SUSPEND_THREAD, "ClassPrepareRequest" + nr);353eventRequests[nr][1].enable();354eventRequests[nr][2] = settingClassPrepareRequest(classNames[nr],355SUSPEND_ALL, "ClassPrepareRequest" + nr);356eventRequests[nr][2].enable();357break;358359default:360throw new JDITestRuntimeException("** default case 2 **");361}362classFlags |= 1 << nr;363}364365log2("......checking up on eventSets");366367vm.resume();368369for (int ne = 0; ne < eventRequests.length; ne++) {370371getEventSet();372373Event ev = eventIterator.nextEvent();374if ( !( ev instanceof ClassPrepareEvent)) {375log3("ERROR: new event is not ClassPrepareEvent");376testExitCode = FAILED;377break;378}379380String property = (String) ev.request().getProperty("number");381382int np = 0;383for (; np < eventRequests.length; np++) {384if (property.equals("ClassPrepareRequest" + np))385break;386}387388policy = eventSet.suspendPolicy();389390if (policy != policyExpected[np]) {391log3("ERROR: eventSet.suspendPolicy() != policyExpected");392log3(" eventSet.suspendPolicy() == " + policy);393log3(" policyExpected == " + policyExpected[np]);394testExitCode = FAILED;395}396397switch (np) {398case 0: eventRequests[np][0].disable(); break;399case 1: eventRequests[np][1].disable(); break;400case 2: eventRequests[np][2].disable(); break;401case 3: eventRequests[np][0].disable(); eventRequests[np][1].disable(); break;402case 4: eventRequests[np][0].disable(); eventRequests[np][2].disable(); break;403case 5: eventRequests[np][1].disable(); eventRequests[np][2].disable(); break;404case 6: eventRequests[np][0].disable(); eventRequests[np][1].disable();405eventRequests[np][2].disable(); break;406}407408switch (policy) {409case SUSPEND_NONE : break;410case SUSPEND_THREAD : mainThread.resume(); break;411case SUSPEND_ALL : vm.resume(); break;412default: throw new JDITestRuntimeException("** default case 1 **");413}414classFlags &= 1 << ne;415}416if (classFlags != 0) {417log3("ERROR: not all events received : " + classFlags);418testExitCode = FAILED;419}420421for (int i = 0; ; i++) {422423log1(":::::: case: # " + i);424425//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part426427breakpointForCommunication();428429int instruction = ((IntegerValue)430(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();431if (instruction == 0) {432vm.resume();433break;434}435//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~436}437log1(" TESTING ENDS");438return;439}440441// ============================== test's additional methods442443private ClassPrepareRequest settingClassPrepareRequest ( String testedClass,444int suspendPolicy,445String property )446throws JDITestRuntimeException {447try {448log2("......setting up ClassPrepareRequest:");449log2(" class: " + testedClass + "; property: " + property);450451ClassPrepareRequest452cpr = eventRManager.createClassPrepareRequest();453cpr.putProperty("number", property);454cpr.addClassFilter(testedClass);455cpr.setSuspendPolicy(suspendPolicy);456457log2(" ClassPrepareRequest has been set up");458return cpr;459} catch ( Exception e ) {460log3("ERROR: ATTENTION: Exception within settingClassPrepareRequest() : " + e);461log3(" ClassPreparenRequest HAS NOT BEEN SET UP");462throw new JDITestRuntimeException("** FAILURE to set up ClassPrepareRequest **");463}464}465466}467468469