Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/createAccessWatchpointRequest/craccwtchpreq003.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.createAccessWatchpointRequest;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.createAccessWatchpointRequest()</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 accesses to the specified field. <BR>48* - Throws: UnsupportedOperationException - <BR>49* if the target virtual machine does not support <BR>50* this operation. <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* <BR>66* In second phase to check the assertion, <BR>67* the debugger and the debuggee perform the following. <BR>68* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>69* - The debuggee prepares testcase and invokes <BR>70* the methodForCommunication to be suspended and <BR>71* to inform the debugger with the event. <BR>72* - Upon getting the BreakpointEvent, <BR>73* the debugger performs the checks required. <BR>74* <BR>75* In third phase, at the end of the test, the debuggee changes <BR>76* the value of the "instruction" which the debugger and debuggee <BR>77* use to inform each other of needed actions, and both end. <BR>78* <BR>79*/8081public class craccwtchpreq003 extends JDIBase {8283public static void main (String argv[]) {8485int result = run(argv, System.out);8687System.exit(result + PASS_BASE);88}8990public static int run (String argv[], PrintStream out) {9192int exitCode = new craccwtchpreq003().runThis(argv, out);9394if (exitCode != PASSED) {95System.out.println("TEST FAILED");96}97return testExitCode;98}99100// ************************************************ test parameters101102private String debuggeeName =103"nsk.jdi.EventRequestManager.createAccessWatchpointRequest.craccwtchpreq003a";104105//====================================================== test program106107private int runThis (String argv[], PrintStream out) {108109argsHandler = new ArgumentHandler(argv);110logHandler = new Log(out, argsHandler);111Binder binder = new Binder(argsHandler, logHandler);112113waitTime = argsHandler.getWaitTime() * 60000;114115try {116log2("launching a debuggee :");117log2(" " + debuggeeName);118if (argsHandler.verbose()) {119debuggee = binder.bindToDebugee(debuggeeName + " -vbs");120} else {121debuggee = binder.bindToDebugee(debuggeeName);122}123if (debuggee == null) {124log3("ERROR: no debuggee launched");125return FAILED;126}127log2("debuggee launched");128} catch ( Exception e ) {129log3("ERROR: Exception : " + e);130log2(" test cancelled");131return FAILED;132}133134debuggee.redirectOutput(logHandler);135136vm = debuggee.VM();137138eventQueue = vm.eventQueue();139if (eventQueue == null) {140log3("ERROR: eventQueue == null : TEST ABORTED");141vm.exit(PASS_BASE);142return FAILED;143}144145log2("invocation of the method runTest()");146switch (runTest()) {147148case 0 : log2("test phase has finished normally");149log2(" waiting for the debuggee to finish ...");150debuggee.waitFor();151152log2("......getting the debuggee's exit status");153int status = debuggee.getStatus();154if (status != PASS_BASE) {155log3("ERROR: debuggee returned UNEXPECTED exit status: " +156status + " != PASS_BASE");157testExitCode = FAILED;158} else {159log2("......debuggee returned expected exit status: " +160status + " == PASS_BASE");161}162break;163164default : log3("ERROR: runTest() returned unexpected value");165166case 1 : log3("test phase has not finished normally: debuggee is still alive");167log2("......forcing: vm.exit();");168testExitCode = FAILED;169try {170vm.exit(PASS_BASE);171} catch ( Exception e ) {172log3("ERROR: Exception : " + e);173}174break;175176case 2 : log3("test cancelled due to VMDisconnectedException");177log2("......trying: vm.process().destroy();");178testExitCode = FAILED;179try {180Process vmProcess = vm.process();181if (vmProcess != null) {182vmProcess.destroy();183}184} catch ( Exception e ) {185log3("ERROR: Exception : " + e);186}187break;188}189190return testExitCode;191}192193194/*195* Return value: 0 - normal end of the test196* 1 - ubnormal end of the test197* 2 - VMDisconnectedException while test phase198*/199200private int runTest() {201202try {203testRun();204205log2("waiting for VMDeathEvent");206getEventSet();207if (eventIterator.nextEvent() instanceof VMDeathEvent)208return 0;209210log3("ERROR: last event is not the VMDeathEvent");211return 1;212} catch ( VMDisconnectedException e ) {213log3("ERROR: VMDisconnectedException : " + e);214return 2;215} catch ( Exception e ) {216log3("ERROR: Exception : " + e);217return 1;218}219220}221222private void testRun()223throws JDITestRuntimeException, Exception {224225eventRManager = vm.eventRequestManager();226227ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();228cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);229cpRequest.addClassFilter(debuggeeName);230231cpRequest.enable();232vm.resume();233getEventSet();234cpRequest.disable();235236ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();237debuggeeClass = event.referenceType();238239if (!debuggeeClass.name().equals(debuggeeName))240throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");241242log2(" received: ClassPrepareEvent for debuggeeClass");243244String bPointMethod = "methodForCommunication";245String lineForComm = "lineForComm";246247ThreadReference mainThread = debuggee.threadByNameOrThrow("main");248249BreakpointRequest bpRequest = settingBreakpoint(mainThread,250debuggeeClass,251bPointMethod, lineForComm, "zero");252bpRequest.enable();253254//------------------------------------------------------ testing section255256String fieldName = "testField";257258Field field = null;259260List requests = null;261ListIterator li = null;262263AccessWatchpointRequest awRequest1 = null;264AccessWatchpointRequest awRequest2 = null;265266267log1(" TESTING BEGINS");268269for (int i = 0; ; i++) {270271vm.resume();272breakpointForCommunication();273274int instruction = ((IntegerValue)275(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();276277if (instruction == 0) {278vm.resume();279break;280}281282log1(":::::: case: # " + i);283284//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part285286field = debuggeeClass.fieldByName(fieldName);287288if ( vm.canWatchFieldAccess() ) {289log2("......vm.canWatchFieldAccess == true");290log2(" creating two AccessWatchpointRequests for 'field': awRequest1 & awRequest2");291292awRequest1 = eventRManager.createAccessWatchpointRequest(field);293awRequest2 = eventRManager.createAccessWatchpointRequest(field);294295log2(" checking up on: requests are disabled");296if (awRequest1.isEnabled()) {297testExitCode = FAILED;298log3("ERROR: awRequest1 is enabled");299}300if (awRequest2.isEnabled()) {301testExitCode = FAILED;302log3("ERROR: awRequest2 is enabled");303}304log2(" compareing field equality");305if ( !awRequest1.field().equals(field) ) {306testExitCode = FAILED;307log3("ERROR: !awRequest1.field().equals(field)");308}309if ( !awRequest2.field().equals(field) ) {310testExitCode = FAILED;311log3("ERROR: !awRequest2.field().equals(field)");312}313314} else {315log2("......vm.canWatchFieldAccess == false");316log2(" UnsupportedOperationException is expected");317try {318awRequest1 = eventRManager.createAccessWatchpointRequest(field);319testExitCode = FAILED;320log3("ERROR: NO UnsupportedOperationException");321} catch ( UnsupportedOperationException e ) {322}323}324325//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~326}327log1(" TESTING ENDS");328return;329}330331}332333334