Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/accessWatchpointRequests/accwtchpreq002.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.accessWatchpointRequests;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.accessWatchpointRequests()</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 checking 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 accwtchpreq002 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 accwtchpreq002().runThis(argv, out);9091if (exitCode != PASSED) {92System.out.println("TEST FAILED");93}94return testExitCode;95}9697private String debuggeeName =98"nsk.jdi.EventRequestManager.accessWatchpointRequests.accwtchpreq002a";99100//====================================================== test program101102private int runThis (String argv[], PrintStream out) {103104argsHandler = new ArgumentHandler(argv);105logHandler = new Log(out, argsHandler);106Binder binder = new Binder(argsHandler, logHandler);107108waitTime = argsHandler.getWaitTime() * 60000;109110try {111log2("launching a debuggee :");112log2(" " + debuggeeName);113if (argsHandler.verbose()) {114debuggee = binder.bindToDebugee(debuggeeName + " -vbs");115} else {116debuggee = binder.bindToDebugee(debuggeeName);117}118if (debuggee == null) {119log3("ERROR: no debuggee launched");120return FAILED;121}122log2("debuggee launched");123} catch ( Exception e ) {124log3("ERROR: Exception : " + e);125log2(" test cancelled");126return FAILED;127}128129debuggee.redirectOutput(logHandler);130131vm = debuggee.VM();132133eventQueue = vm.eventQueue();134if (eventQueue == null) {135log3("ERROR: eventQueue == null : TEST ABORTED");136vm.exit(PASS_BASE);137return FAILED;138}139140log2("invocation of the method runTest()");141switch (runTest()) {142143case 0 : log2("test phase has finished normally");144log2(" waiting for the debuggee to finish ...");145debuggee.waitFor();146147log2("......getting the debuggee's exit status");148int status = debuggee.getStatus();149if (status != PASS_BASE) {150log3("ERROR: debuggee returned UNEXPECTED exit status: " +151status + " != PASS_BASE");152testExitCode = FAILED;153} else {154log2("......debuggee returned expected exit status: " +155status + " == PASS_BASE");156}157break;158159default : log3("ERROR: runTest() returned unexpected value");160161case 1 : log3("test phase has not finished normally: debuggee is still alive");162log2("......forcing: vm.exit();");163testExitCode = FAILED;164try {165vm.exit(PASS_BASE);166} catch ( Exception e ) {167log3("ERROR: Exception : " + e);168}169break;170171case 2 : log3("test cancelled due to VMDisconnectedException");172log2("......trying: vm.process().destroy();");173testExitCode = FAILED;174try {175Process vmProcess = vm.process();176if (vmProcess != null) {177vmProcess.destroy();178}179} catch ( Exception e ) {180log3("ERROR: Exception : " + e);181}182break;183}184185return testExitCode;186}187188189/*190* Return value: 0 - normal end of the test191* 1 - ubnormal end of the test192* 2 - VMDisconnectedException while test phase193*/194195private int runTest() {196197try {198testRun();199200log2("waiting for VMDeathEvent");201getEventSet();202if (eventIterator.nextEvent() instanceof VMDeathEvent)203return 0;204205log3("ERROR: last event is not the VMDeathEvent");206return 1;207} catch ( VMDisconnectedException e ) {208log3("ERROR: VMDisconnectedException : " + e);209return 2;210} catch ( Exception e ) {211log3("ERROR: Exception : " + e);212return 1;213}214215}216217private void testRun()218throws JDITestRuntimeException, Exception {219220if ( !vm.canWatchFieldAccess() ) {221log2("......vm.canWatchFieldAccess == false :: test cancelled");222vm.exit(PASS_BASE);223return;224}225226eventRManager = vm.eventRequestManager();227228ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();229cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);230cpRequest.addClassFilter(debuggeeName);231232cpRequest.enable();233vm.resume();234getEventSet();235cpRequest.disable();236237ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();238debuggeeClass = event.referenceType();239240if (!debuggeeClass.name().equals(debuggeeName))241throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");242243log2(" received: ClassPrepareEvent for debuggeeClass");244245String bPointMethod = "methodForCommunication";246String lineForComm = "lineForComm";247248ThreadReference mainThread = debuggee.threadByNameOrThrow("main");249250BreakpointRequest bpRequest = settingBreakpoint(mainThread,251debuggeeClass,252bPointMethod, lineForComm, "zero");253bpRequest.enable();254255//------------------------------------------------------ testing section256257String fieldName = "testField";258259Field field = null;260261List requests = null;262ListIterator li = null;263264AccessWatchpointRequest request = null;265AccessWatchpointRequest awRequests[] = { null, null, null, null, null,266null, null, null, null, null };267int listSize;268int flag;269270271log1(" TESTING BEGINS");272273for (int i = 0; ; i++) {274275vm.resume();276breakpointForCommunication();277278int instruction = ((IntegerValue)279(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();280281if (instruction == 0) {282vm.resume();283break;284}285286log1(":::::: case: # " + i);287288//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part289290field = debuggeeClass.fieldByName(fieldName);291292log2("......creating AccessWatchpointRequests");293for (int i1 = 0; i1 < awRequests.length; i1++) {294awRequests[i1] = eventRManager.createAccessWatchpointRequest(field);295awRequests[i1].putProperty("number", String.valueOf(i1));296297log2("......checking up on returned List after creating new AccessWatchpointRequest");298requests = eventRManager.accessWatchpointRequests();299listSize = requests.size();300if ( listSize != (i1 + 1) ) {301testExitCode = FAILED;302log3("ERROR: size of returned List is not equal to expected : " + listSize + " != " + (i1 + 1));303}304flag = 0;305li = requests.listIterator();306while (li.hasNext()) {307request = (AccessWatchpointRequest) li.next();308if ( !request.isEnabled() ) {309flag++;310if (flag > 1) {311testExitCode = FAILED;312log3("ERROR: # of disabled requests > 1 : " + flag);313}314if ( !request.getProperty("number").equals(String.valueOf(i1)) ) {315testExitCode = FAILED;316log3("ERROR: in the List, disabled is request expected to be enabled : # == " + i1);317}318} else {319if ( request.getProperty("number").equals(String.valueOf(i1)) ) {320testExitCode = FAILED;321log3("ERROR: in the List, enabled is newly created disabled request : # == " + i1);322}323}324}325326log2(" enabling created AccessWatchpointRequest");327awRequests[i1].enable();328requests = eventRManager.accessWatchpointRequests();329listSize = requests.size();330if ( listSize != (i1 + 1) ) {331testExitCode = FAILED;332log3("ERROR: size of returned List is not equal to expected : " + listSize + " != " + (i1 + 1));333}334335li = requests.listIterator();336while (li.hasNext()) {337request = (AccessWatchpointRequest) li.next();338if ( !request.isEnabled() ) {339testExitCode = FAILED;340log3("ERROR: returned List contains disabled request : " + request);341}342}343344log2(" removing item from the List; UnsupportedOperationException is expected");345try {346requests.remove(i1);347testExitCode = FAILED;348log3("ERROR: NO exception");349} catch ( UnsupportedOperationException e ) {350log2(" UnsupportedOperationException ");351}352}353354log2("......deleting AccessWatchpointRequests");355for (int i2 = awRequests.length -1; i2 >= 0; i2--) {356eventRManager.deleteEventRequest(awRequests[i2]);357requests = eventRManager.accessWatchpointRequests();358listSize = requests.size();359if ( listSize != i2 ) {360testExitCode = FAILED;361log3("ERROR: size of returned List is not equal to expected : " + listSize + " != " + i2);362}363log2(" removing item from the List; UnsupportedOperationException is expected");364try {365requests.remove(i2);366testExitCode = FAILED;367log3("ERROR: NO exception");368} catch ( UnsupportedOperationException e ) {369log2(" UnsupportedOperationException ");370}371}372373//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~374}375log1(" TESTING ENDS");376return;377}378379}380381382