Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/BreakpointRequest/location/location001.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.BreakpointRequest.location;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* BreakpointRequest. <BR>39* <BR>40* The test checks that results of the method <BR>41* <code>com.sun.jdi.BreakpointRequest.location()</code> <BR>42* complies with its spec. <BR>43* <BR>44* The test checks if a Location object, an argument of the method <BR>45* EventRequestManager.createBreakpointRequest(Location), <BR>46* is equal to one returned by the method BreakpointRequest.location() <BR>47* <BR>48* The test has three phases and works as follows. <BR>49* <BR>50* In first phase, <BR>51* upon launching debuggee's VM which will be suspended, <BR>52* a debugger waits for the VMStartEvent within a predefined <BR>53* time interval. If no the VMStartEvent received, the test is FAILED. <BR>54* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>55* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>56* and waits for the event within the predefined time interval. <BR>57* If no the ClassPrepareEvent received, the test is FAILED. <BR>58* Upon getting the ClassPrepareEvent, <BR>59* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>60* within debuggee's special methodForCommunication(). <BR>61* <BR>62* In second phase to check the assertion, <BR>63* the debugger and the debuggee perform the following. <BR>64* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>65* - The debuggee creates new threads, thread1, and invokes <BR>66* the methodForCommunication to be suspended and <BR>67* to inform the debugger with the event. <BR>68* - Upon getting the BreakpointEvent, the debugger <BR>69* - creates a BreakpointRequest and gets Location object location1, <BR>70* - gets Location object location2 with the method <BR>71* BreakpointRequest.location(), <BR>72* - compares the Location objects. <BR>73* <BR>74* In third phase, at the end of the test, <BR>75* the debuggee changes the value of the "instruction" which <BR>76* the debugger and debuggee use to inform each other of needed actions,<BR>77* and both end.78* <BR>79*/8081public class location001 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 location001().runThis(argv, out);9394if (exitCode != PASSED) {95System.out.println("TEST FAILED");96}97return testExitCode;98}99100// ************************************************ test parameters101102private String debuggeeName =103"nsk.jdi.BreakpointRequest.location.location001a";104105private String testedClassName =106"nsk.jdi.BreakpointRequest.location.TestClass";107108Location location1 = null;109Location location2 = null;110111//====================================================== test program112113private int runThis (String argv[], PrintStream out) {114115argsHandler = new ArgumentHandler(argv);116logHandler = new Log(out, argsHandler);117Binder binder = new Binder(argsHandler, logHandler);118119waitTime = argsHandler.getWaitTime() * 60000;120121try {122log2("launching a debuggee :");123log2(" " + debuggeeName);124if (argsHandler.verbose()) {125debuggee = binder.bindToDebugee(debuggeeName + " -vbs");126} else {127debuggee = binder.bindToDebugee(debuggeeName);128}129if (debuggee == null) {130log3("ERROR: no debuggee launched");131return FAILED;132}133log2("debuggee launched");134} catch ( Exception e ) {135log3("ERROR: Exception : " + e);136log2(" test cancelled");137return FAILED;138}139140debuggee.redirectOutput(logHandler);141142vm = debuggee.VM();143144eventQueue = vm.eventQueue();145if (eventQueue == null) {146log3("ERROR: eventQueue == null : TEST ABORTED");147vm.exit(PASS_BASE);148return FAILED;149}150151log2("invocation of the method runTest()");152switch (runTest()) {153154case 0 : log2("test phase has finished normally");155log2(" waiting for the debuggee to finish ...");156debuggee.waitFor();157158log2("......getting the debuggee's exit status");159int status = debuggee.getStatus();160if (status != PASS_BASE) {161log3("ERROR: debuggee returned UNEXPECTED exit status: " +162status + " != PASS_BASE");163testExitCode = FAILED;164} else {165log2("......debuggee returned expected exit status: " +166status + " == PASS_BASE");167}168break;169170default : log3("ERROR: runTest() returned unexpected value");171172case 1 : log3("test phase has not finished normally: debuggee is still alive");173log2("......forcing: vm.exit();");174testExitCode = FAILED;175try {176vm.exit(PASS_BASE);177} catch ( Exception e ) {178log3("ERROR: Exception : " + e);179}180break;181182case 2 : log3("test cancelled due to VMDisconnectedException");183log2("......trying: vm.process().destroy();");184testExitCode = FAILED;185try {186Process vmProcess = vm.process();187if (vmProcess != null) {188vmProcess.destroy();189}190} catch ( Exception e ) {191log3("ERROR: Exception : " + e);192}193break;194}195196return testExitCode;197}198199200/*201* Return value: 0 - normal end of the test202* 1 - ubnormal end of the test203* 2 - VMDisconnectedException while test phase204*/205206private int runTest() {207208try {209testRun();210211log2("waiting for VMDeathEvent");212getEventSet();213if (eventIterator.nextEvent() instanceof VMDeathEvent)214return 0;215216log3("ERROR: last event is not the VMDeathEvent");217return 1;218} catch ( VMDisconnectedException e ) {219log3("ERROR: VMDisconnectedException : " + e);220return 2;221} catch ( Exception e ) {222log3("ERROR: Exception : " + e);223return 1;224}225226}227228private void testRun()229throws JDITestRuntimeException, Exception {230231eventRManager = vm.eventRequestManager();232233ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();234cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);235cpRequest.addClassFilter(debuggeeName);236237cpRequest.enable();238vm.resume();239getEventSet();240cpRequest.disable();241242ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();243debuggeeClass = event.referenceType();244245if (!debuggeeClass.name().equals(debuggeeName))246throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");247248log2(" received: ClassPrepareEvent for debuggeeClass");249250String bPointMethod = "methodForCommunication";251String lineForComm = "lineForComm";252253ThreadReference mainThread = debuggee.threadByNameOrThrow("main");254255BreakpointRequest bpRequest = settingBreakpoint(mainThread,256257debuggeeClass,258bPointMethod, lineForComm, "zero");259bpRequest.enable();260261//------------------------------------------------------ testing section262263log1(" TESTING BEGINS");264265EventRequest eventRequest1 = null;266267ThreadReference thread1 = null;268String thread1Name = "thread1";269270String property1 = "BreakpointRequest1";271272String methodName = "method";273String bpLineName = "breakpointLine";274275ReferenceType testClassReference = null;276277for (int i = 0; ; i++) {278279vm.resume();280breakpointForCommunication();281282int instruction = ((IntegerValue)283(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();284285if (instruction == 0) {286vm.resume();287break;288}289290log1(":::::: case: # " + i);291292//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part293294switch (i) {295296case 0:297testClassReference =298(ReferenceType) vm.classesByName(testedClassName).get(0);299300thread1 = (ThreadReference) debuggeeClass.getValue(301debuggeeClass.fieldByName(thread1Name));302303log2(".....setting up BreakpointRequest and getting Location object location1");304eventRequest1 = setting2BreakpointRequest (null,305testClassReference, methodName, bpLineName,306EventRequest.SUSPEND_NONE, property1);307308log2("......getting: location2 = ((BreakpointRequest) eventRequest1).location();");309location2 = ((BreakpointRequest) eventRequest1).location();310311log2(" checking up on equality of location1 and location2");312if ( !location1.equals(location2) ) {313testExitCode = FAILED;314log3("ERROR: Location objects are not equal");315}316317break;318319default:320throw new JDITestRuntimeException("** default case 2 **");321}322//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~323}324log1(" TESTING ENDS");325return;326}327328// ============================== test's additional methods329330private BreakpointRequest setting2BreakpointRequest ( ThreadReference thread,331ReferenceType testedClass,332String methodName,333String bpLine,334int suspendPolicy,335String property )336throws JDITestRuntimeException {337try {338log2("......setting up a breakpoint:");339log2(" thread: " + thread + "; class: " + testedClass + "; method: " + methodName + "; line: " + bpLine);340341int n = ( (IntegerValue) testedClass.getValue(testedClass.fieldByName(bpLine) ) ).value();342Location loc = (Location) ((Method) testedClass.methodsByName(methodName).get(0)).allLineLocations().get(n);343344location1 = loc;345346BreakpointRequest347bpr = eventRManager.createBreakpointRequest(loc);348bpr.putProperty("number", property);349if (thread != null)350bpr.addThreadFilter(thread);351bpr.setSuspendPolicy(suspendPolicy);352353log2(" a breakpoint has been set up");354return bpr;355} catch ( Exception e ) {356log3("ERROR: ATTENTION: Exception within settingBreakpointRequest() : " + e);357log3(" BreakpointRequest HAS NOT BEEN SET UP");358throw new JDITestRuntimeException("** FAILURE to set up BreakpointRequest **");359}360}361362}363364365