Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/Locatable/location/location005.java
41161 views
/*1* Copyright (c) 2001, 2019, 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.Locatable.location;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdi.*;2829import com.sun.jdi.*;30import java.util.*;31import java.io.*;3233import com.sun.jdi.event.*;34import com.sun.jdi.request.*;3536/**37* The test for the implementation of an object of the type <BR>38* Locatable. <BR>39* <BR>40* The test checks up that results of the method <BR>41* <code>com.sun.jdi.Locatable.location()</code> <BR>42* complies with its spec when applied to an instance method. <BR>43* <BR>44* The cases for testing are as follows. <BR>45* 1) After getting a thread suspended but before to resume it, <BR>46* StackFrame.location() is invoked and its returned value <BR>47* must not be null. <BR>48* Since the thread is not resumed yet, <BR>49* InvalidStackFrameException must not be throw. <BR>50* 2) After resuming the thread, the tested method is invoked <BR>51* second time and InvalidStackFrameException must be thrown.<BR>52* <BR>53*/5455public class location005 {5657//----------------------------------------------------- templete section58static final int PASSED = 0;59static final int FAILED = 2;60static final int PASS_BASE = 95;6162//----------------------------------------------------- templete parameters63static final String64sHeader1 = "\n==> nsk/jdi/Locatable/location/location005 ",65sHeader2 = "--> debugger: ",66sHeader3 = "##> debugger: ";6768//----------------------------------------------------- main method6970public static void main (String argv[]) {71int result = run(argv, System.out);72System.exit(result + PASS_BASE);73}7475public static int run (String argv[], PrintStream out) {76return new location005().runThis(argv, out);77}7879//-------------------------------------------------- log procedures8081//private static boolean verbMode = false;8283private static Log logHandler;8485private static void log1(String message) {86logHandler.display(sHeader1 + message);87}88private static void log2(String message) {89logHandler.display(sHeader2 + message);90}91private static void log3(String message) {92logHandler.complain(sHeader3 + message);93}9495// ************************************************ test parameters9697private String debuggeeName =98"nsk.jdi.Locatable.location.location005a";99100private String testedClassName =101"nsk.jdi.Locatable.location.Threadlocation005a";102103String mName = "nsk.jdi.Locatable.location";104105//====================================================== test program106//------------------------------------------------------ common section107static ArgumentHandler argsHandler;108109static int waitTime;110111static VirtualMachine vm = null;112static EventRequestManager eventRManager = null;113static EventQueue eventQueue = null;114static EventSet eventSet = null;115116ReferenceType testedclass = null;117ThreadReference thread2 = null;118ThreadReference mainThread = null;119120static int testExitCode = PASSED;121122static final int returnCode0 = 0;123static final int returnCode1 = 1;124static final int returnCode2 = 2;125static final int returnCode3 = 3;126static final int returnCode4 = 4;127128//------------------------------------------------------ methods129130private int runThis (String argv[], PrintStream out) {131132Debugee debuggee;133134argsHandler = new ArgumentHandler(argv);135logHandler = new Log(out, argsHandler);136Binder binder = new Binder(argsHandler, logHandler);137138if (argsHandler.verbose()) {139debuggee = binder.bindToDebugee(debuggeeName + " -vbs"); // *** tp140} else {141debuggee = binder.bindToDebugee(debuggeeName); // *** tp142}143144waitTime = argsHandler.getWaitTime();145146147IOPipe pipe = new IOPipe(debuggee);148149debuggee.redirectStderr(out);150log2(debuggeeName + " debuggee launched");151debuggee.resume();152153String line = pipe.readln();154if ((line == null) || !line.equals("ready")) {155log3("signal received is not 'ready' but: " + line);156return FAILED;157} else {158log2("'ready' recieved");159}160161VirtualMachine vm = debuggee.VM();162163//------------------------------------------------------ testing section164log1(" TESTING BEGINS");165166for (int i = 0; ; i++) {167pipe.println("newcheck");168line = pipe.readln();169170if (line.equals("checkend")) {171log2(" : returned string is 'checkend'");172break ;173} else if (!line.equals("checkready")) {174log3("ERROR: returned string is not 'checkready'");175testExitCode = FAILED;176break ;177}178179log1("new check: #" + i);180181//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part182183int expresult = returnCode0;184185eventRManager = vm.eventRequestManager();186eventQueue = vm.eventQueue();187188String threadName = "Thread2";189String breakpointMethod = "runt1";190String bpLine1 = "breakpointLineNumber1";191String breakpointMethod1 = "runt1";192193BreakpointRequest breakpRequest1 = null;194195List allThreads = null;196ListIterator listIterator = null;197List classes = null;198199StackFrame stackFrame = null;200201202label0: {203204log2("getting ThreadReference object");205try {206allThreads = vm.allThreads();207classes = vm.classesByName(testedClassName);208testedclass = (ReferenceType) classes.get(0);209} catch ( Exception e) {210log3("ERROR: Exception at very beginning !? : " + e);211expresult = returnCode1;212break label0;213}214215listIterator = allThreads.listIterator();216for (;;) {217try {218thread2 = (ThreadReference) listIterator.next();219if (thread2.name().equals(threadName))220break ;221} catch ( NoSuchElementException e ) {222log3("ERROR: NoSuchElementException for listIterator.next()");223log3("ERROR: NO THREAD2 ?????????!!!!!!!");224expresult = returnCode1;225break label0;226}227}228229log2("setting up breakpoint");230231breakpRequest1 = settingBreakpoint(breakpointMethod1, bpLine1, "one");232if (breakpRequest1 == null) {233expresult = returnCode1;234break label0;235}236237}238239label1: {240241if (expresult != returnCode0)242break label1;243244log2(" enabling breakpRequest1");245breakpRequest1.enable();246247log2(" forcing the main thread to leave synchronized block");248pipe.println("continue");249line = pipe.readln();250if (!line.equals("docontinue")) {251log3("ERROR: returned string is not 'docontinue'");252expresult = returnCode4;253break label1;254}255256expresult = breakpoint();257if (expresult != returnCode0)258break label1;259260261log2(" stackFrame = thread2.frame(0);");262try {263stackFrame = thread2.frame(0);264} catch ( Exception e ) {265log3("ERROR: Exception: " + e);266expresult = returnCode1;267break label1;268}269270271Location stackLocation = null;272273log2(" checking up: no InvalidStackFrameException when the thread2 is suspended");274try {275log2(" before: stackLocation = stackFrame.location();");276stackLocation = stackFrame.location();277if (stackLocation == null) {278log3("ERROR: stackLocation == null");279expresult = returnCode1;280}281} catch ( InvalidStackFrameException e ) {282log2("ERROR: InvalidStackFrameExceprtion");283}284285log2(" resuming the thread2");286eventSet.resume();287288if (expresult == returnCode1)289break label1;290291log2(" checking up InvalidStackFrameException after resuming the thread2");292try {293log2(" before: stackLocation = stackFrame.location();");294stackLocation = stackFrame.location();295expresult = returnCode1;296log3("ERROR: no InvalidStackFrameExceprtion");297} catch ( InvalidStackFrameException e ) {298log2(" : InvalidStackFrameExceprtion");299}300}301302log2(" resuming the thread2 for case it was suspended but not resumed yet");303eventSet.resume();304305//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~306log2(" the end of testing");307if (expresult != returnCode0)308testExitCode = FAILED;309}310log1(" TESTING ENDS");311312//-------------------------------------------------- test summary section313//------------------------------------------------- standard end section314315pipe.println("quit");316log2("waiting for the debuggee to finish ...");317debuggee.waitFor();318319int status = debuggee.getStatus();320if (status != PASSED + PASS_BASE) {321log3("debuggee returned UNEXPECTED exit status: " +322status + " != PASS_BASE");323testExitCode = FAILED;324} else {325log2("debuggee returned expected exit status: " +326status + " == PASS_BASE");327}328329if (testExitCode != PASSED) {330logHandler.complain("TEST FAILED");331}332return testExitCode;333}334335336/*337* private BreakpointRequest settingBreakpoint(String, String, String)338*339* It sets up a breakpoint within a given method at given line number340* for the thread2 only.341* Third parameter is required for any case in future debugging, as if.342*343* Return codes:344* = BreakpointRequest object in case of success345* = null in case of an Exception thrown within the method346*/347348private BreakpointRequest settingBreakpoint ( String methodName,349String bpLine,350String property) {351352log2("setting up a breakpoint: method: '" + methodName + "' line: " + bpLine );353354List alllineLocations = null;355Location lineLocation = null;356BreakpointRequest breakpRequest = null;357358try {359Method method = (Method) testedclass.methodsByName(methodName).get(0);360361alllineLocations = method.allLineLocations();362363int n =364( (IntegerValue) testedclass.getValue(testedclass.fieldByName(bpLine) ) ).value();365if (n > alllineLocations.size()) {366log3("ERROR: TEST_ERROR_IN_settingBreakpoint(): number is out of bound of method's lines");367} else {368lineLocation = (Location) alllineLocations.get(n);369try {370breakpRequest = eventRManager.createBreakpointRequest(lineLocation);371breakpRequest.putProperty("number", property);372breakpRequest.addThreadFilter(thread2);373breakpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);374} catch ( Exception e1 ) {375log3("ERROR: inner Exception within settingBreakpoint() : " + e1);376breakpRequest = null;377}378}379} catch ( Exception e2 ) {380log3("ERROR: ATTENTION: outer Exception within settingBreakpoint() : " + e2);381breakpRequest = null;382}383384if (breakpRequest == null)385log2(" A BREAKPOINT HAS NOT BEEN SET UP");386else387log2(" a breakpoint has been set up");388389return breakpRequest;390}391392393/*394* private int breakpoint ()395*396* It removes events from EventQueue until gets first BreakpointEvent.397* To get next EventSet value, it uses the method398* EventQueue.remove(int timeout)399* The timeout argument passed to the method, is "waitTime*60000".400* Note: the value of waitTime is set up with401* the method ArgumentHandler.getWaitTime() at the beginning of the test.402*403* Return codes:404* = returnCode0 - success;405* = returnCode2 - Exception when "eventSet = eventQueue.remove()" is executed406* = returnCode3 - default case when loop of processing an event, that is,407* an unspecified event was taken from the EventQueue408*/409410private int breakpoint () {411412int returnCode = returnCode0;413414log2(" waiting for BreakpointEvent");415416labelBP:417for (;;) {418419log2(" new: eventSet = eventQueue.remove();");420try {421eventSet = eventQueue.remove(waitTime*60000);422if (eventSet == null) {423log3("ERROR: timeout for waiting for a BreakpintEvent");424returnCode = returnCode3;425break labelBP;426}427} catch ( Exception e ) {428log3("ERROR: Exception for eventSet = eventQueue.remove(); : " + e);429returnCode = 1;430break labelBP;431}432433if (eventSet != null) {434435log2(" : eventSet != null; size == " + eventSet.size());436437EventIterator eIter = eventSet.eventIterator();438Event ev = null;439440for (; eIter.hasNext(); ) {441442if (returnCode != returnCode0)443break;444445ev = eIter.nextEvent();446447ll: for (int ifor =0; ; ifor++) {448449try {450switch (ifor) {451452case 0: AccessWatchpointEvent awe = (AccessWatchpointEvent) ev;453log2(" AccessWatchpointEvent removed");454break ll;455case 1: BreakpointEvent be = (BreakpointEvent) ev;456log2(" BreakpointEvent removed");457break labelBP;458case 2: ClassPrepareEvent cpe = (ClassPrepareEvent) ev;459log2(" ClassPreparEvent removed");460break ll;461case 3: ClassUnloadEvent cue = (ClassUnloadEvent) ev;462log2(" ClassUnloadEvent removed");463break ll;464case 4: ExceptionEvent ee = (ExceptionEvent) ev;465log2(" ExceptionEvent removed");466break ll;467case 5: MethodEntryEvent mene = (MethodEntryEvent) ev;468log2(" MethodEntryEvent removed");469break ll;470case 6: MethodExitEvent mexe = (MethodExitEvent) ev;471log2(" MethodExiEvent removed");472break ll;473case 7: ModificationWatchpointEvent mwe = (ModificationWatchpointEvent) ev;474log2(" ModificationWatchpointEvent removed");475break ll;476case 8: StepEvent se = (StepEvent) ev;477log2(" StepEvent removed");478break ll;479case 9: ThreadDeathEvent tde = (ThreadDeathEvent) ev;480log2(" ThreadDeathEvent removed");481break ll;482case 10: ThreadStartEvent tse = (ThreadStartEvent) ev;483log2(" ThreadStartEvent removed");484break ll;485case 11: VMDeathEvent vmde = (VMDeathEvent) ev;486log2(" VMDeathEvent removed");487break ll;488case 12: VMStartEvent vmse = (VMStartEvent) ev;489log2(" VMStartEvent removed");490break ll;491case 13: WatchpointEvent we = (WatchpointEvent) ev;492log2(" WatchpointEvent removed");493break ll;494495default: log3("ERROR: default case for casting event");496returnCode = returnCode3;497break ll;498} // switch499} catch ( ClassCastException e ) {500} // try501} // ll: for (int ifor =0; ; ifor++)502} // for (; ev.hasNext(); )503}504}505if (returnCode == returnCode0)506log2(" : eventSet == null: EventQueue is empty");507508return returnCode;509}510511}512513514