Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/modificationWatchpointRequests/modwtchpreq002.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.EventRequestManager.modificationWatchpointRequests;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* EventRequestManager. <BR>39* <BR>40* The test checks that results of the method <BR>41* <code>com.sun.jdi.EventRequestManager.modificationWatchpointRequests()</code> <BR>42* complies with its spec. <BR>43* <BR>44* The test checks up on the following assertion: <BR>45* - The list is unmodifiable. <BR>46* - The list of the enabled and disabled access watchpoint requests. <BR>47* - This list is changes as requests are added and deleted. <BR>48* <BR>49* The test has three phases and works as follows. <BR>50* <BR>51* In first phase, <BR>52* upon launching debuggee's VM which will be suspended, <BR>53* a debugger waits for the VMStartEvent within a predefined <BR>54* time interval. If no the VMStartEvent received, the test is FAILED. <BR>55* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>56* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>57* and waits for the event within the predefined time interval. <BR>58* If no the ClassPrepareEvent received, the test is FAILED. <BR>59* Upon getting the ClassPrepareEvent, <BR>60* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>61* within debuggee's special methodForCommunication(). <BR>62* <BR>63* In second phase to check the assertion, <BR>64* the debugger and the debuggee perform the following. <BR>65* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>66* - The debuggee prepares the check case and invokes <BR>67* the methodForCommunication to be suspended and <BR>68* to inform the debugger with the event. <BR>69* - Upon getting the BreakpointEvent, <BR>70* the debugger checks up on assertions. <BR>71* <BR>72* In third phase, at the end of the test, the debuggee changes <BR>73* the value of the "instruction" which the debugger and debuggee <BR>74* use to inform each other of needed actions, and both end. <BR>75* <BR>76*/7778public class modwtchpreq002 extends JDIBase {7980public static void main (String argv[]) {8182int result = run(argv, System.out);8384System.exit(result + PASS_BASE);85}8687public static int run (String argv[], PrintStream out) {8889int exitCode = new modwtchpreq002().runThis(argv, out);9091if (exitCode != PASSED) {92System.out.println("TEST FAILED");93}94return testExitCode;95}9697// ************************************************ test parameters9899private String debuggeeName =100"nsk.jdi.EventRequestManager.modificationWatchpointRequests.modwtchpreq002a";101102//====================================================== test program103104private int runThis (String argv[], PrintStream out) {105106argsHandler = new ArgumentHandler(argv);107logHandler = new Log(out, argsHandler);108Binder binder = new Binder(argsHandler, logHandler);109110waitTime = argsHandler.getWaitTime() * 60000;111112try {113log2("launching a debuggee :");114log2(" " + debuggeeName);115if (argsHandler.verbose()) {116debuggee = binder.bindToDebugee(debuggeeName + " -vbs");117} else {118debuggee = binder.bindToDebugee(debuggeeName);119}120if (debuggee == null) {121log3("ERROR: no debuggee launched");122return FAILED;123}124log2("debuggee launched");125} catch ( Exception e ) {126log3("ERROR: Exception : " + e);127log2(" test cancelled");128return FAILED;129}130131debuggee.redirectOutput(logHandler);132133vm = debuggee.VM();134135eventQueue = vm.eventQueue();136if (eventQueue == null) {137log3("ERROR: eventQueue == null : TEST ABORTED");138vm.exit(PASS_BASE);139return FAILED;140}141142log2("invocation of the method runTest()");143switch (runTest()) {144145case 0 : log2("test phase has finished normally");146log2(" waiting for the debuggee to finish ...");147debuggee.waitFor();148149log2("......getting the debuggee's exit status");150int status = debuggee.getStatus();151if (status != PASS_BASE) {152log3("ERROR: debuggee returned UNEXPECTED exit status: " +153status + " != PASS_BASE");154testExitCode = FAILED;155} else {156log2("......debuggee returned expected exit status: " +157status + " == PASS_BASE");158}159break;160161default : log3("ERROR: runTest() returned unexpected value");162163case 1 : log3("test phase has not finished normally: debuggee is still alive");164log2("......forcing: vm.exit();");165testExitCode = FAILED;166try {167vm.exit(PASS_BASE);168} catch ( Exception e ) {169log3("ERROR: Exception : " + e);170}171break;172173case 2 : log3("test cancelled due to VMDisconnectedException");174log2("......trying: vm.process().destroy();");175testExitCode = FAILED;176try {177Process vmProcess = vm.process();178if (vmProcess != null) {179vmProcess.destroy();180}181} catch ( Exception e ) {182log3("ERROR: Exception : " + e);183}184break;185}186187return testExitCode;188}189190191/*192* Return value: 0 - normal end of the test193* 1 - ubnormal end of the test194* 2 - VMDisconnectedException while test phase195*/196197private int runTest() {198199try {200testRun();201202log2("waiting for VMDeathEvent");203getEventSet();204if (eventIterator.nextEvent() instanceof VMDeathEvent)205return 0;206207log3("ERROR: last event is not the VMDeathEvent");208return 1;209} catch ( VMDisconnectedException e ) {210log3("ERROR: VMDisconnectedException : " + e);211return 2;212} catch ( Exception e ) {213log3("ERROR: Exception : " + e);214return 1;215}216217}218219private void testRun()220throws JDITestRuntimeException, Exception {221222if ( !vm.canWatchFieldAccess() ) {223log2("......vm.canWatchFieldAccess == false :: test cancelled");224vm.exit(PASS_BASE);225return;226}227228eventRManager = vm.eventRequestManager();229230ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();231cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);232cpRequest.addClassFilter(debuggeeName);233234cpRequest.enable();235vm.resume();236getEventSet();237cpRequest.disable();238239ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();240debuggeeClass = event.referenceType();241242if (!debuggeeClass.name().equals(debuggeeName))243throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");244245log2(" received: ClassPrepareEvent for debuggeeClass");246247String bPointMethod = "methodForCommunication";248String lineForComm = "lineForComm";249250ThreadReference mainThread = debuggee.threadByNameOrThrow("main");251252BreakpointRequest bpRequest = settingBreakpoint(mainThread,253debuggeeClass,254bPointMethod, lineForComm, "zero");255bpRequest.enable();256257//------------------------------------------------------ testing section258259String fieldName = "testField";260261Field field = null;262263List requests = null;264ListIterator li = null;265266ModificationWatchpointRequest request = null;267ModificationWatchpointRequest awRequests[] = { null, null, null, null, null,268null, null, null, null, null };269int listSize;270int flag;271272273log1(" TESTING BEGINS");274275for (int i = 0; ; i++) {276277vm.resume();278breakpointForCommunication();279280int instruction = ((IntegerValue)281(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();282283if (instruction == 0) {284vm.resume();285break;286}287288log1(":::::: case: # " + i);289290//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part291292field = debuggeeClass.fieldByName(fieldName);293294log2("......creating ModificationWatchpointRequests");295for (int i1 = 0; i1 < awRequests.length; i1++) {296awRequests[i1] = eventRManager.createModificationWatchpointRequest(field);297awRequests[i1].putProperty("number", String.valueOf(i1));298299log2("......checking up on returned List after creating new ModificationWatchpointRequest");300requests = eventRManager.modificationWatchpointRequests();301listSize = requests.size();302if ( listSize != (i1 + 1) ) {303testExitCode = FAILED;304log3("ERROR: size of returned List is not equal to expected : " + listSize + " != " + (i1 + 1));305}306flag = 0;307li = requests.listIterator();308while (li.hasNext()) {309request = (ModificationWatchpointRequest) li.next();310if ( !request.isEnabled() ) {311flag++;312if (flag > 1) {313testExitCode = FAILED;314log3("ERROR: # of disabled requests > 1 : " + flag);315}316if ( !request.getProperty("number").equals(String.valueOf(i1)) ) {317testExitCode = FAILED;318log3("ERROR: in the List, disabled is request expected to be enabled : # == " + i1);319}320} else {321if ( request.getProperty("number").equals(String.valueOf(i1)) ) {322testExitCode = FAILED;323log3("ERROR: in the List, enabled is newly created disabled request : # == " + i1);324}325}326}327328log2(" enabling created ModificationWatchpointRequest");329awRequests[i1].enable();330requests = eventRManager.modificationWatchpointRequests();331listSize = requests.size();332if ( listSize != (i1 + 1) ) {333testExitCode = FAILED;334log3("ERROR: size of returned List is not equal to expected : " + listSize + " != " + (i1 + 1));335}336337li = requests.listIterator();338while (li.hasNext()) {339request = (ModificationWatchpointRequest) li.next();340if ( !request.isEnabled() ) {341testExitCode = FAILED;342log3("ERROR: returned List contains disabled request : " + request);343}344}345346log2(" removing item from the List; UnsupportedOperationException is expected");347try {348requests.remove(i1);349testExitCode = FAILED;350log3("ERROR: NO exception");351} catch ( UnsupportedOperationException e ) {352log2(" UnsupportedOperationException ");353}354}355356log2("......deleting ModificationWatchpointRequests");357for (int i2 = awRequests.length -1; i2 >= 0; i2--) {358eventRManager.deleteEventRequest(awRequests[i2]);359requests = eventRManager.modificationWatchpointRequests();360listSize = requests.size();361if ( listSize != i2 ) {362testExitCode = FAILED;363log3("ERROR: size of returned List is not equal to expected : " + listSize + " != " + i2);364}365log2(" removing item from the List; UnsupportedOperationException is expected");366try {367requests.remove(i2);368testExitCode = FAILED;369log3("ERROR: NO exception");370} catch ( UnsupportedOperationException e ) {371log2(" UnsupportedOperationException ");372}373}374375//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~376}377log1(" TESTING ENDS");378return;379}380381}382383384