Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequest/setSuspendPolicy/setsuspendpolicy001.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.EventRequest.setSuspendPolicy;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* EventRequest. <BR>39* <BR>40* The test checks that results of the method <BR>41* <code>com.sun.jdi.EventRequest.setSuspendPolicy()</code> <BR>42* complies with its spec. <BR>43* <BR>44* The test checks up on following assertions: <BR>45* - a value of policy is equal to one assigned by last method <BR>46* setSuspendPolicy(int); <BR>47* - InvalidRequestStateException is thrown <BR>48* if this request is currently enabled; <BR>49* - InvalidRequestStateException is thrown <BR>50* if this request has been deleted. <BR>51* The cases to check include Requests of all sub-types. <BR>52* <BR>53* The test has three phases and works as follows. <BR>54* <BR>55* In first phase, <BR>56* upon launching debuggee's VM which will be suspended, <BR>57* a debugger waits for the VMStartEvent within a predefined <BR>58* time interval. If no the VMStartEvent received, the test is FAILED. <BR>59* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>60* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>61* and waits for the event within the predefined time interval. <BR>62* If no the ClassPrepareEvent received, the test is FAILED. <BR>63* Upon getting the ClassPrepareEvent, <BR>64* the debugger sets up the breakpoint with SUSPEND_ALL <BR>65* within debuggee's special methodForCommunication(). <BR>66* <BR>67* In second phase to check the assertion, <BR>68* the debugger and the debuggee perform the following loop. <BR>69* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>70* - The debuggee prepares new check case (as if) and invokes <BR>71* the methodForCommunication to be suspended and <BR>72* to inform the debugger with the event. <BR>73* - Upon getting the BreakpointEvent, <BR>74* the debugger performs the check case required. <BR>75* <BR>76* In third phase, at the end of the test, the debuggee changes <BR>77* the value of the "instruction" which the debugger and debuggee <BR>78* use to inform each other of needed actions, and both end. <BR>79* <BR>80*/8182public class setsuspendpolicy001 extends JDIBase {8384public static void main (String argv[]) {8586int result = run(argv, System.out);8788System.exit(result + PASS_BASE);89}9091public static int run (String argv[], PrintStream out) {9293int exitCode = new setsuspendpolicy001().runThis(argv, out);9495if (exitCode != PASSED) {96System.out.println("TEST FAILED");97}98return testExitCode;99}100101// ************************************************ test parameters102103private String debuggeeName =104"nsk.jdi.EventRequest.setSuspendPolicy.setsuspendpolicy001a";105106private String testedClassName =107"nsk.jdi.EventRequest.setSuspendPolicy.TestClass11";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)212return 0;213214log3("ERROR: last event is not the VMDeathEvent");215return 1;216} catch ( VMDisconnectedException e ) {217log3("ERROR: VMDisconnectedException : " + e);218return 2;219} catch ( Exception e ) {220log3("ERROR: Exception : " + e);221return 1;222}223224}225226private void testRun()227throws JDITestRuntimeException, Exception {228229eventRManager = vm.eventRequestManager();230231ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();232cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);233cpRequest.addClassFilter(debuggeeName);234235cpRequest.enable();236vm.resume();237getEventSet();238cpRequest.disable();239240ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();241debuggeeClass = event.referenceType();242243if (!debuggeeClass.name().equals(debuggeeName))244throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");245246log2(" received: ClassPrepareEvent for debuggeeClass");247248String bPointMethod = "methodForCommunication";249String lineForComm = "lineForComm";250251ThreadReference mainThread = debuggee.threadByNameOrThrow("main");252253BreakpointRequest bpRequest = settingBreakpoint(mainThread,254debuggeeClass,255bPointMethod, lineForComm, "zero");256bpRequest.enable();257258//------------------------------------------------------ testing section259260261EventRequest eventRequest1 = null;262263String fieldName1 = "var101";264String fieldName2 = "excObj";265266ThreadReference thread1 = null;267String threadName1 = "thread1";268269ReferenceType testClassReference = null;270271String propertyName = "name";272String propertyValue1 = "value1";273String propertyValue2 = "value2";274275276log1(" TESTING BEGINS");277278for (int i = 0; ; i++) {279280vm.resume();281breakpointForCommunication();282283int instruction = ((IntegerValue)284(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();285286if (instruction == 0) {287vm.resume();288break;289}290291log1(":::::: case: # " + i);292293//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part294295296switch (i) {297298case 0:299thread1 = debuggee.threadByNameOrThrow(threadName1);300301log2("......setting up StepRequest");302eventRequest1 = eventRManager.createStepRequest303(thread1, StepRequest.STEP_MIN, StepRequest.STEP_INTO);304break;305306case 1:307testClassReference = (ReferenceType) vm.classesByName(testedClassName).get(0);308309log2("......setting up AccessWatchpointRequest");310eventRequest1 = eventRManager.createAccessWatchpointRequest311(testClassReference.fieldByName(fieldName1));312break;313314case 2:315log2(".....setting up ModificationWatchpointRequest");316eventRequest1 = eventRManager.createModificationWatchpointRequest317(testClassReference.fieldByName(fieldName1));318break;319320case 3:321log2(".....setting up ClassPrepareRequest");322eventRequest1 = eventRManager.createClassPrepareRequest();323break;324325case 4:326log2(".....setting up ClassUnloadRequest");327eventRequest1 = eventRManager.createClassUnloadRequest();328break;329330case 5:331log2(".....setting up MethodEntryRequest");332eventRequest1 = eventRManager.createMethodEntryRequest();333break;334335case 6:336log2(".....setting up MethodExitRequest");337eventRequest1 = eventRManager.createMethodExitRequest();338break;339340case 7:341log2(".....setting up ThreadDeathRequest");342eventRequest1 = eventRManager.createThreadDeathRequest();343break;344345case 8:346log2(".....setting up ThreadStartRequest");347eventRequest1 = eventRManager.createThreadStartRequest();348break;349350case 9:351log2(".....setting up VMDeathRequest");352eventRequest1 = eventRManager.createVMDeathRequest();353break;354355case 10:356log2(".....setting up ExceptionRequest");357eventRequest1 = eventRManager.createExceptionRequest(358(ReferenceType)359(debuggeeClass.getValue(debuggeeClass.fieldByName(fieldName2))).type(),360true, true );361break;362363case 11:364log2(".....setting up BreakpointRequest");365eventRequest1 = eventRManager.createBreakpointRequest(breakpLocation);366break;367368369default:370throw new JDITestRuntimeException("** default case 2 **");371}372373log2("......eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_NONE);");374eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_NONE);375376log2("......eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_ALL);");377eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_ALL);378379log2(" checking up on eventRequest1");380if ( eventRequest1.suspendPolicy() != EventRequest.SUSPEND_ALL) {381testExitCode = FAILED;382log3("ERROR: suspendPolicy() != EventRequest.SUSPEND_ALL");383}384385log2("......eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD );");386eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD );387388log2(" checking up on eventRequest1");389if ( eventRequest1.suspendPolicy() != EventRequest.SUSPEND_EVENT_THREAD ) {390testExitCode = FAILED;391log3("ERROR: suspendPolicy() != EventRequest.SUSPEND_EVENT_THREAD ");392}393394log2("......eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_NONE);");395eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_NONE);396397log2(" checking up on eventRequest1");398if ( eventRequest1.suspendPolicy() != EventRequest.SUSPEND_NONE) {399testExitCode = FAILED;400log3("ERROR: suspendPolicy() != EventRequest.SUSPEND_NONE");401}402403vm.suspend();404log2("......eventRequest1.setEnabled(true);");405eventRequest1.setEnabled(true);406try {407log2(" checking up on eventRequest1; InvalidRequestStateException is expected");408eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_NONE);409testExitCode = FAILED;410log3("ERROR: NO InvalidRequestStateException");411} catch ( InvalidRequestStateException e ) {412log2(" InvalidRequestStateException");413}414log2("......eventRManager.deleteEventRequest(eventRequest1);");415eventRManager.deleteEventRequest(eventRequest1);416try {417log2(" checking up on eventRequest1; InvalidRequestStateException is expected");418eventRequest1.setSuspendPolicy(EventRequest.SUSPEND_NONE);419testExitCode = FAILED;420log3("ERROR: NO InvalidRequestStateException");421} catch ( InvalidRequestStateException e ) {422log2(" InvalidRequestStateException");423}424vm.resume();425426//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~427}428log1(" TESTING ENDS");429return;430}431432}433434435