Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/LocalVariable/isVisible/isvisible001.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.LocalVariable.isVisible;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* LocalVariable. <BR>39* <BR>40* The test checks up that results of the method <BR>41* <code>com.sun.jdi.LocalVariable.isVisible()</code> <BR>42* complies with its spec in case when a tested program <BR>43* is prepared with full information, <BR>44* hence, AbsentInformationException is not expected to happen. <BR>45* <BR>46* The cases for testing are as follows. <BR>47* After being started up, <BR>48* a debuggee creates a 'lockingObject' for synchronizing threads, <BR>49* enters a synchronized block in which it creates new thread, thread2, <BR>50* informs a debugger of the thread2 creation, and is waiting for reply.<BR>51* Since the thread2 uses the same locking object as main one <BR>52* it is locked up until the main thread leaves the synchronized block. <BR>53* Upon the receiption a message from the debuggee, the debugger <BR>54* sets up a breakpoint, instructs the debuggee to leave synchronized <BR>55* block, and after getting the thread2 suspended at breakpoint, <BR>56* StackFrame.variablesByName() is used to form LocalVariable <BR>57* objects for the following method variables: <BR>58* - visible i0 and invisible i2 within the method run <BR>59* whose StackFrame # = 1; <BR>60* - visible i2 and invisible i3 within the method runt <BR>61* whose StackFrame # = 0. <BR>62* Then a debugger checks up whether the LocalVariable objects <BR>63* are visible or not for corresponding StackFrames. <BR>64* <BR>65*/6667public class isvisible001 {6869//----------------------------------------------------- templete section70static final int PASSED = 0;71static final int FAILED = 2;72static final int PASS_BASE = 95;7374//----------------------------------------------------- templete parameters75static final String76sHeader1 = "\n==> nsk/jdi/LocalVariable/isVisible/isvisible001 ",77sHeader2 = "--> debugger: ",78sHeader3 = "##> debugger: ";79//----------------------------------------------------- main method8081public static void main (String argv[]) {82int result = run(argv, System.out);83System.exit(result + PASS_BASE);84}8586public static int run (String argv[], PrintStream out) {87return new isvisible001().runThis(argv, out);88}8990//-------------------------------------------------- log procedures9192//private static boolean verbMode = false;9394private static Log logHandler;9596private static void log1(String message) {97logHandler.display(sHeader1 + message);98}99private static void log2(String message) {100logHandler.display(sHeader2 + message);101}102private static void log3(String message) {103logHandler.complain(sHeader3 + message);104}105106// ************************************************ test parameters107108private String debuggeeName =109"nsk.jdi.LocalVariable.isVisible.isvisible001a";110111private String testedClassName =112"nsk.jdi.LocalVariable.isVisible.Threadisvisible001a";113114String mName = "nsk.jdi.LocalVariable.isVisible";115116//====================================================== test program117//------------------------------------------------------ common section118static ArgumentHandler argsHandler;119120static int waitTime;121122static VirtualMachine vm = null;123static EventRequestManager eventRManager = null;124static EventQueue eventQueue = null;125static EventSet eventSet = null;126127ReferenceType testedclass = null;128ThreadReference thread2 = null;129ThreadReference mainThread = null;130131static int testExitCode = PASSED;132133static final int returnCode0 = 0;134static final int returnCode1 = 1;135static final int returnCode2 = 2;136static final int returnCode3 = 3;137static final int returnCode4 = 4;138139//------------------------------------------------------ methods140141private int runThis (String argv[], PrintStream out) {142143Debugee debuggee;144145argsHandler = new ArgumentHandler(argv);146logHandler = new Log(out, argsHandler);147Binder binder = new Binder(argsHandler, logHandler);148149if (argsHandler.verbose()) {150debuggee = binder.bindToDebugee(debuggeeName + " -vbs"); // *** tp151} else {152debuggee = binder.bindToDebugee(debuggeeName); // *** tp153}154155waitTime = argsHandler.getWaitTime();156157158IOPipe pipe = new IOPipe(debuggee);159160debuggee.redirectStderr(out);161log2(debuggeeName + " debuggee launched");162debuggee.resume();163164String line = pipe.readln();165if ((line == null) || !line.equals("ready")) {166log3("signal received is not 'ready' but: " + line);167return FAILED;168} else {169log2("'ready' recieved");170}171172VirtualMachine vm = debuggee.VM();173174//------------------------------------------------------ testing section175log1(" TESTING BEGINS");176177for (int i = 0; ; i++) {178pipe.println("newcheck");179line = pipe.readln();180181if (line.equals("checkend")) {182log2(" : returned string is 'checkend'");183break ;184} else if (!line.equals("checkready")) {185log3("ERROR: returned string is not 'checkready'");186testExitCode = FAILED;187break ;188}189190log1("new check: #" + i);191192//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part193194eventRManager = vm.eventRequestManager();195eventQueue = vm.eventQueue();196197String threadName = "Thread2";198199String breakpointMethod1 = "runt1";200String bpLine1 = "breakpointLineNumber1";201202BreakpointRequest breakpRequest1 = null;203204List allThreads = null;205ListIterator listIterator = null;206List classes = null;207208StackFrame stackFrame = null;209StackFrame stackFrame1 = null;210211List methods = null;212213Method runmethod = null;214Method runt1method = null;215216List lVars = null;217218LocalVariable l_i0 = null;219LocalVariable l_i1 = null;220LocalVariable l_i2 = null;221LocalVariable l_i3 = null;222223int expresult = returnCode0;224225226label0: {227228log2("getting ThreadReference object");229try {230allThreads = vm.allThreads();231classes = vm.classesByName(testedClassName);232testedclass = (ReferenceType) classes.get(0);233} catch ( Exception e) {234log3("ERROR: Exception at very beginning !? : " + e);235expresult = returnCode1;236break label0;237}238239listIterator = allThreads.listIterator();240for (;;) {241try {242thread2 = (ThreadReference) listIterator.next();243if (thread2.name().equals(threadName))244break ;245} catch ( NoSuchElementException e ) {246log3("ERROR: NoSuchElementException for listIterator.next()");247log3("ERROR: NO THREAD2 ?????????!!!!!!!");248expresult = returnCode1;249break label0;250}251}252253log2("setting up breakpoint");254255breakpRequest1 = settingBreakpoint(breakpointMethod1, bpLine1, "one");256if (breakpRequest1 == null) {257expresult = returnCode1;258break label0;259}260261}262263label1: {264265if (expresult != returnCode0)266break label1;267268log2(" enabling breakpRequest1");269breakpRequest1.enable();270271log2(" forcing the main thread to leave synchronized block");272pipe.println("continue");273line = pipe.readln();274if (!line.equals("docontinue")) {275log3("ERROR: returned string is not 'docontinue'");276expresult = returnCode4;277break label1;278}279280log2(" getting a breakpoint event");281expresult = breakpoint();282if (expresult != returnCode0)283break label1;284285log2(" getting StackFrame");286try {287stackFrame = thread2.frame(0);288stackFrame1 = thread2.frame(1);289} catch ( Exception e ) {290log3("ERROR: Exception for stackFrame = thread2.frame(0) :" + e);291expresult = returnCode1;292break label1;293}294295}296297label2: {298299if (expresult != returnCode0)300break label2;301302methods = ((ReferenceType) classes.get(0)).methodsByName("run");303runmethod = (Method) methods.get(0);304305methods = ((ReferenceType) classes.get(0)).methodsByName("runt1");306runt1method = (Method) methods.get(0);307308try {309log2(" lVars = runmethod.variablesByName('i0');");310lVars = runmethod.variablesByName("i0");311if (lVars.size() != 1) {312log3("ERROR: lVars.size() != 1");313expresult = returnCode1;314}315l_i0 = (LocalVariable) lVars.get(0);316317log2(" lVars = runmethod.variablesByName('i1');");318lVars = runmethod.variablesByName("i1");319if (lVars.size() != 1) {320log3("ERROR: lVars.size() != 1");321expresult = returnCode1;322}323l_i1 = (LocalVariable) lVars.get(0);324325log2(" lVars = runmethod.variablesByName('i2');");326lVars = runt1method.variablesByName("i2");327if (lVars.size() != 1) {328log3("ERROR: lVars.size() != 1");329expresult = returnCode1;330}331l_i2 = (LocalVariable) lVars.get(0);332333log2(" lVars = runmethod.variablesByName('i3');");334lVars = runt1method.variablesByName("i3");335if (lVars.size() != 1) {336log3("ERROR: lVars.size() != 1");337expresult = returnCode1;338}339l_i3 = (LocalVariable) lVars.get(0);340} catch ( AbsentInformationException e ) {341log3("ERROR: AbsentInformationException");342expresult = returnCode1;343}344if (expresult != returnCode0)345break label2;346347348log2(" checkin up: l_i0.isVisible(stackFrame1); expected: true");349try {350if (!l_i0.isVisible(stackFrame1)) {351log3("ERROR: !l_i0.isVisible(stackFrame1)");352expresult = returnCode1;353}354} catch ( IllegalArgumentException e ) {355log3("ERROR: IllegalArgumentException");356expresult = returnCode1;357}358359log2(" checkin up: l_i1.isVisible(stackFrame1); expected: false");360try {361if (l_i1.isVisible(stackFrame1)) {362log3("ERROR: l_i1.isVisible(stackFrame1)");363expresult = returnCode1;364}365} catch ( IllegalArgumentException e ) {366log3("ERROR: IllegalArgumentException");367expresult = returnCode1;368}369370log2(" checkin up: l_i2.isVisible(stackFrame1); expected: IllegalArgumentException");371try {372if (l_i2.isVisible(stackFrame1)) {373log3("ERROR: no IllegalArgumentException");374expresult = returnCode1;375}376} catch ( IllegalArgumentException e ) {377log2(" : IllegalArgumentException");378}379380log2(" checkin up: l_i3.isVisible(stackFrame1); expected: IllegalArgumentException");381try {382if (l_i3.isVisible(stackFrame1)) {383log3("ERROR: no IllegalArgumentException");384expresult = returnCode1;385}386} catch ( IllegalArgumentException e ) {387log2(" : IllegalArgumentException");388}389390log2(" checkin up: l_i1.isVisible(stackFrame); expected: IllegalArgumentException");391try {392if (l_i1.isVisible(stackFrame)) {393log3("ERROR: no IllegalArgumentException");394expresult = returnCode1;395}396} catch ( IllegalArgumentException e ) {397log2(" : IllegalArgumentException");398}399400log2(" checkin up: l_i2.isVisible(stackFrame); expected: true");401try {402if (!l_i2.isVisible(stackFrame)) {403log3("ERROR: !l_i2.isVisible(stackFrame)");404expresult = returnCode1;405}406} catch ( IllegalArgumentException e ) {407log3("ERROR: IllegalArgumentException for i2 in stackFrame");408expresult = returnCode1;409}410log2(" checkin up: l_i3.isVisible(stackFrame); expected: false");411try {412if (l_i3.isVisible(stackFrame)) {413log3("ERROR: l_i3.isVisible(stackFrame)");414expresult = returnCode1;415}416} catch ( IllegalArgumentException e ) {417log3("ERROR: IllegalArgumentException for i3 in stackFrame");;418expresult = returnCode1;419}420}421422log2(" resuming the thread2 for case it was suspended but not resumed yet");423eventSet.resume();424425//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~426log2(" the end of testing");427if (expresult != returnCode0)428testExitCode = FAILED;429}430log1(" TESTING ENDS");431432//-------------------------------------------------- test summary section433//------------------------------------------------- standard end section434435pipe.println("quit");436log2("waiting for the debuggee to finish ...");437debuggee.waitFor();438439int status = debuggee.getStatus();440if (status != PASSED + PASS_BASE) {441log3("debuggee returned UNEXPECTED exit status: " +442status + " != PASS_BASE");443testExitCode = FAILED;444} else {445log2("debuggee returned expected exit status: " +446status + " == PASS_BASE");447}448449if (testExitCode != PASSED) {450logHandler.complain("TEST FAILED");451}452return testExitCode;453}454455456/*457* private BreakpointRequest settingBreakpoint(String, String, String)458*459* It sets up a breakpoint within a given method at given line number460* for the thread2 only.461* Third parameter is required for any case in future debugging, as if.462*463* Return codes:464* = BreakpointRequest object in case of success465* = null in case of an Exception thrown within the method466*/467468private BreakpointRequest settingBreakpoint ( String methodName,469String bpLine,470String property) {471472log2("setting up a breakpoint: method: '" + methodName + "' line: " + bpLine );473474List alllineLocations = null;475Location lineLocation = null;476BreakpointRequest breakpRequest = null;477478try {479Method method = (Method) testedclass.methodsByName(methodName).get(0);480481alllineLocations = method.allLineLocations();482483int n =484( (IntegerValue) testedclass.getValue(testedclass.fieldByName(bpLine) ) ).value();485if (n > alllineLocations.size()) {486log3("ERROR: TEST_ERROR_IN_settingBreakpoint(): number is out of bound of method's lines");487} else {488lineLocation = (Location) alllineLocations.get(n);489try {490breakpRequest = eventRManager.createBreakpointRequest(lineLocation);491breakpRequest.putProperty("number", property);492breakpRequest.addThreadFilter(thread2);493breakpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);494} catch ( Exception e1 ) {495log3("ERROR: inner Exception within settingBreakpoint() : " + e1);496breakpRequest = null;497}498}499} catch ( Exception e2 ) {500log3("ERROR: ATTENTION: outer Exception within settingBreakpoint() : " + e2);501breakpRequest = null;502}503504if (breakpRequest == null)505log2(" A BREAKPOINT HAS NOT BEEN SET UP");506else507log2(" a breakpoint has been set up");508509return breakpRequest;510}511512513/*514* private int breakpoint ()515*516* It removes events from EventQueue until gets first BreakpointEvent.517* To get next EventSet value, it uses the method518* EventQueue.remove(int timeout)519* The timeout argument passed to the method, is "waitTime*60000".520* Note: the value of waitTime is set up with521* the method ArgumentHandler.getWaitTime() at the beginning of the test.522*523* Return codes:524* = returnCode0 - success;525* = returnCode2 - Exception when "eventSet = eventQueue.remove()" is executed526* = returnCode3 - default case when loop of processing an event, that is,527* an unspecified event was taken from the EventQueue528*/529530private int breakpoint () {531532int returnCode = returnCode0;533534log2(" waiting for BreakpointEvent");535536labelBP:537for (;;) {538539log2(" new: eventSet = eventQueue.remove();");540try {541eventSet = eventQueue.remove(waitTime*60000);542if (eventSet == null) {543log3("ERROR: timeout for waiting for a BreakpintEvent");544returnCode = returnCode3;545break labelBP;546}547} catch ( Exception e ) {548log3("ERROR: Exception for eventSet = eventQueue.remove(); : " + e);549returnCode = 1;550break labelBP;551}552553if (eventSet != null) {554555log2(" : eventSet != null; size == " + eventSet.size());556557EventIterator eIter = eventSet.eventIterator();558Event ev = null;559560for (; eIter.hasNext(); ) {561562if (returnCode != returnCode0)563break;564565ev = eIter.nextEvent();566567ll: for (int ifor =0; ; ifor++) {568569try {570switch (ifor) {571572case 0: AccessWatchpointEvent awe = (AccessWatchpointEvent) ev;573log2(" AccessWatchpointEvent removed");574break ll;575case 1: BreakpointEvent be = (BreakpointEvent) ev;576log2(" BreakpointEvent removed");577break labelBP;578case 2: ClassPrepareEvent cpe = (ClassPrepareEvent) ev;579log2(" ClassPreparEvent removed");580break ll;581case 3: ClassUnloadEvent cue = (ClassUnloadEvent) ev;582log2(" ClassUnloadEvent removed");583break ll;584case 4: ExceptionEvent ee = (ExceptionEvent) ev;585log2(" ExceptionEvent removed");586break ll;587case 5: MethodEntryEvent mene = (MethodEntryEvent) ev;588log2(" MethodEntryEvent removed");589break ll;590case 6: MethodExitEvent mexe = (MethodExitEvent) ev;591log2(" MethodExiEvent removed");592break ll;593case 7: ModificationWatchpointEvent mwe = (ModificationWatchpointEvent) ev;594log2(" ModificationWatchpointEvent removed");595break ll;596case 8: StepEvent se = (StepEvent) ev;597log2(" StepEvent removed");598break ll;599case 9: ThreadDeathEvent tde = (ThreadDeathEvent) ev;600log2(" ThreadDeathEvent removed");601break ll;602case 10: ThreadStartEvent tse = (ThreadStartEvent) ev;603log2(" ThreadStartEvent removed");604break ll;605case 11: VMDeathEvent vmde = (VMDeathEvent) ev;606log2(" VMDeathEvent removed");607break ll;608case 12: VMStartEvent vmse = (VMStartEvent) ev;609log2(" VMStartEvent removed");610break ll;611case 13: WatchpointEvent we = (WatchpointEvent) ev;612log2(" WatchpointEvent removed");613break ll;614615default: log3("ERROR: default case for casting event");616returnCode = returnCode3;617break ll;618} // switch619} catch ( ClassCastException e ) {620} // try621} // ll: for (int ifor =0; ; ifor++)622} // for (; ev.hasNext(); )623}624}625if (returnCode == returnCode0)626log2(" : eventSet == null: EventQueue is empty");627628return returnCode;629}630631}632633634