Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/createModificationWatchpointRequest/crmodwtchpreq003.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.createModificationWatchpointRequest;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.createModificationWatchpointRequest()</code> <BR>42* complies with its spec. <BR>43* <BR>44* The test checks up on the following assertion: <BR>45* - Multiple watchpoints on the same field are permitted. <BR>46* - Creates a new disabled watchpoint. <BR>47* - The watchpoint watches modification of the specified field. <BR>48* - Throws: UnsupportedOperationException - <BR>49* if the target virtual machine does not support this operation. <BR>50* <BR>51* The test has three phases and works as follows. <BR>52* <BR>53* In first phase, <BR>54* upon launching debuggee's VM which will be suspended, <BR>55* a debugger waits for the VMStartEvent within a predefined <BR>56* time interval. If no the VMStartEvent received, the test is FAILED. <BR>57* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>58* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>59* and waits for the event within the predefined time interval. <BR>60* If no the ClassPrepareEvent received, the test is FAILED. <BR>61* Upon getting the ClassPrepareEvent, <BR>62* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>63* within debuggee's special methodForCommunication(). <BR>64* <BR>65* In second phase to check the assertion, <BR>66* the debugger and the debuggee perform the following. <BR>67* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>68* - The debuggee prepares testcase and invokes <BR>69* the methodForCommunication to be suspended and <BR>70* to inform the debugger with the event. <BR>71* - Upon getting the BreakpointEvent, <BR>72* the debugger performs the checks required. <BR>73* <BR>74* In third phase, at the end of the test, the debuggee changes <BR>75* the value of the "instruction" which the debugger and debuggee <BR>76* use to inform each other of needed actions, and both end. <BR>77* <BR>78*/7980public class crmodwtchpreq003 extends JDIBase {8182public static void main (String argv[]) {8384int result = run(argv, System.out);8586System.exit(result + PASS_BASE);87}8889public static int run (String argv[], PrintStream out) {9091int exitCode = new crmodwtchpreq003().runThis(argv, out);9293if (exitCode != PASSED) {94System.out.println("TEST FAILED");95}96return testExitCode;97}9899// ************************************************ test parameters100101private String debuggeeName =102"nsk.jdi.EventRequestManager.createModificationWatchpointRequest.crmodwtchpreq003a";103104//====================================================== test program105106private int runThis (String argv[], PrintStream out) {107108argsHandler = new ArgumentHandler(argv);109logHandler = new Log(out, argsHandler);110Binder binder = new Binder(argsHandler, logHandler);111112waitTime = argsHandler.getWaitTime() * 60000;113114try {115log2("launching a debuggee :");116log2(" " + debuggeeName);117if (argsHandler.verbose()) {118debuggee = binder.bindToDebugee(debuggeeName + " -vbs");119} else {120debuggee = binder.bindToDebugee(debuggeeName);121}122if (debuggee == null) {123log3("ERROR: no debuggee launched");124return FAILED;125}126log2("debuggee launched");127} catch ( Exception e ) {128log3("ERROR: Exception : " + e);129log2(" test cancelled");130return FAILED;131}132133debuggee.redirectOutput(logHandler);134135vm = debuggee.VM();136137eventQueue = vm.eventQueue();138if (eventQueue == null) {139log3("ERROR: eventQueue == null : TEST ABORTED");140vm.exit(PASS_BASE);141return FAILED;142}143144log2("invocation of the method runTest()");145switch (runTest()) {146147case 0 : log2("test phase has finished normally");148log2(" waiting for the debuggee to finish ...");149debuggee.waitFor();150151log2("......getting the debuggee's exit status");152int status = debuggee.getStatus();153if (status != PASS_BASE) {154log3("ERROR: debuggee returned UNEXPECTED exit status: " +155status + " != PASS_BASE");156testExitCode = FAILED;157} else {158log2("......debuggee returned expected exit status: " +159status + " == PASS_BASE");160}161break;162163default : log3("ERROR: runTest() returned unexpected value");164165case 1 : log3("test phase has not finished normally: debuggee is still alive");166log2("......forcing: vm.exit();");167testExitCode = FAILED;168try {169vm.exit(PASS_BASE);170} catch ( Exception e ) {171log3("ERROR: Exception : " + e);172}173break;174175case 2 : log3("test cancelled due to VMDisconnectedException");176log2("......trying: vm.process().destroy();");177testExitCode = FAILED;178try {179Process vmProcess = vm.process();180if (vmProcess != null) {181vmProcess.destroy();182}183} catch ( Exception e ) {184log3("ERROR: Exception : " + e);185}186break;187}188189return testExitCode;190}191192193/*194* Return value: 0 - normal end of the test195* 1 - ubnormal end of the test196* 2 - VMDisconnectedException while test phase197*/198199private int runTest() {200201try {202testRun();203204log2("waiting for VMDeathEvent");205getEventSet();206if (eventIterator.nextEvent() instanceof VMDeathEvent)207return 0;208209log3("ERROR: last event is not the VMDeathEvent");210return 1;211} catch ( VMDisconnectedException e ) {212log3("ERROR: VMDisconnectedException : " + e);213return 2;214} catch ( Exception e ) {215log3("ERROR: Exception : " + e);216return 1;217}218219}220221private void testRun()222throws JDITestRuntimeException, Exception {223224eventRManager = vm.eventRequestManager();225226ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();227cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);228cpRequest.addClassFilter(debuggeeName);229230cpRequest.enable();231vm.resume();232getEventSet();233cpRequest.disable();234235ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();236debuggeeClass = event.referenceType();237238if (!debuggeeClass.name().equals(debuggeeName))239throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");240241log2(" received: ClassPrepareEvent for debuggeeClass");242243String bPointMethod = "methodForCommunication";244String lineForComm = "lineForComm";245246ThreadReference mainThread = debuggee.threadByNameOrThrow("main");247248BreakpointRequest bpRequest = settingBreakpoint(mainThread,249debuggeeClass,250bPointMethod, lineForComm, "zero");251bpRequest.enable();252253//------------------------------------------------------ testing section254255String fieldName = "testField";256257Field field = null;258259List requests = null;260ListIterator li = null;261262ModificationWatchpointRequest mwRequest1 = null;263ModificationWatchpointRequest mwRequest2 = null;264265266log1(" TESTING BEGINS");267268for (int i = 0; ; i++) {269270vm.resume();271breakpointForCommunication();272273int instruction = ((IntegerValue)274(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();275276if (instruction == 0) {277vm.resume();278break;279}280281log1(":::::: case: # " + i);282283//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part284285field = debuggeeClass.fieldByName(fieldName);286287if ( vm.canWatchFieldModification() ) {288log2("......vm.canWatchFieldModification == true");289log2(" creating two ModificationWatchpointRequests for 'field': mwRequest1 & mwRequest2");290291mwRequest1 = eventRManager.createModificationWatchpointRequest(field);292mwRequest2 = eventRManager.createModificationWatchpointRequest(field);293294log2(" checking up on: requests are disabled");295if (mwRequest1.isEnabled()) {296testExitCode = FAILED;297log3("ERROR: mwRequest1 is enabled");298}299if (mwRequest2.isEnabled()) {300testExitCode = FAILED;301log3("ERROR: mwRequest2 is enabled");302}303log2(" compareing field equality");304if ( !mwRequest1.field().equals(field) ) {305testExitCode = FAILED;306log3("ERROR: !mwRequest1.field().equals(field)");307}308if ( !mwRequest2.field().equals(field) ) {309testExitCode = FAILED;310log3("ERROR: !mwRequest2.field().equals(field)");311}312313} else {314log2("......vm.canWatchFieldModification == false");315log2(" UnsupportedOperationException is expected");316try {317mwRequest1 = eventRManager.createModificationWatchpointRequest(field);318testExitCode = FAILED;319log3("ERROR: NO UnsupportedOperationException");320} catch ( UnsupportedOperationException e ) {321}322}323324//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~325}326log1(" TESTING ENDS");327return;328}329330}331332333