Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/LocatableEvent/thread/thread001.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.LocatableEvent.thread;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* LocatableEvent. <BR>39* <BR>40* The test checks that results of the method <BR>41* <code>com.sun.jdi.LocatableEvent.thread()</code> <BR>42* complies with its spec. <BR>43* <BR>44* The cases for testing include all Locatable events. <BR>45* The test checks that <BR>46* for each type of LocatableEvent received in a debugger, <BR>47* a value returned by the method invoked on <BR>48* a <type>Event object corresponds to a thread <BR>49* which debuggee's counterpart event was generated in. <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 Locatable events, <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 creates a number of threads, one for each check case <BR>69* and invokes the methodForCommunication to be suspended and <BR>70* to inform the debugger with the event. <BR>71* - Upon getting the BreakpointEvent, the debugger <BR>72* gets ThreadReferences mirroring all tested threads in the debuggee,<BR>73* sets up Requests within them to get Events to check up on, <BR>74* resumes the debuggee, waits for events, and upon getting them, <BR>75* compares ThreadReferences put into Requests to ones from Events; <BR>76* if any mismatch, the test FAILED. <BR>77* <BR>78* Note. To inform each other of needed actions, the debugger and <BR>79* and the debuggee use debuggeee's variable "instruction". <BR>80* <BR>81* In third phase when at the end, <BR>82* the debuggee changes the value of the "instruction" <BR>83* to inform the debugger of checks finished, and both end. <BR>84* <BR>85*/8687public class thread001 extends JDIBase {8889public static void main (String argv[]) {9091int result = run(argv, System.out);9293System.exit(result + PASS_BASE);94}9596public static int run (String argv[], PrintStream out) {9798int exitCode = new thread001().runThis(argv, out);99100if (exitCode != PASSED) {101System.out.println("TEST FAILED");102}103return testExitCode;104}105106// ************************************************ test parameters107108private String debuggeeName =109"nsk.jdi.LocatableEvent.thread.thread001a";110111private String testedClassName =112"nsk.jdi.LocatableEvent.thread.TestClass";113114//====================================================== test program115116private int runThis (String argv[], PrintStream out) {117118argsHandler = new ArgumentHandler(argv);119logHandler = new Log(out, argsHandler);120Binder binder = new Binder(argsHandler, logHandler);121122waitTime = argsHandler.getWaitTime() * 60000;123124try {125log2("launching a debuggee :");126log2(" " + debuggeeName);127if (argsHandler.verbose()) {128debuggee = binder.bindToDebugee(debuggeeName + " -vbs");129} else {130debuggee = binder.bindToDebugee(debuggeeName);131}132if (debuggee == null) {133log3("ERROR: no debuggee launched");134return FAILED;135}136log2("debuggee launched");137} catch ( Exception e ) {138log3("ERROR: Exception : " + e);139log2(" test cancelled");140return FAILED;141}142143debuggee.redirectOutput(logHandler);144145vm = debuggee.VM();146147eventQueue = vm.eventQueue();148if (eventQueue == null) {149log3("ERROR: eventQueue == null : TEST ABORTED");150vm.exit(PASS_BASE);151return FAILED;152}153154log2("invocation of the method runTest()");155switch (runTest()) {156157case 0 : log2("test phase has finished normally");158log2(" waiting for the debuggee to finish ...");159debuggee.waitFor();160161log2("......getting the debuggee's exit status");162int status = debuggee.getStatus();163if (status != PASS_BASE) {164log3("ERROR: debuggee returned UNEXPECTED exit status: " +165status + " != PASS_BASE");166testExitCode = FAILED;167} else {168log2("......debuggee returned expected exit status: " +169status + " == PASS_BASE");170}171break;172173default : log3("ERROR: runTest() returned unexpected value");174175case 1 : log3("test phase has not finished normally: debuggee is still alive");176log2("......forcing: vm.exit();");177testExitCode = FAILED;178try {179vm.exit(PASS_BASE);180} catch ( Exception e ) {181log3("ERROR: Exception : e");182}183break;184185case 2 : log3("test cancelled due to VMDisconnectedException");186log2("......trying: vm.process().destroy();");187testExitCode = FAILED;188try {189Process vmProcess = vm.process();190if (vmProcess != null) {191vmProcess.destroy();192}193} catch ( Exception e ) {194log3("ERROR: Exception : e");195}196break;197}198199return testExitCode;200}201202203/*204* Return value: 0 - normal end of the test205* 1 - ubnormal end of the test206* 2 - VMDisconnectedException while test phase207*/208209private int runTest() {210211try {212testRun();213214log2("waiting for VMDeathEvent");215getEventSet();216if (eventIterator.nextEvent() instanceof VMDeathEvent)217return 0;218219log3("ERROR: last event is not the VMDeathEvent");220return 1;221} catch ( VMDisconnectedException e ) {222log3("ERROR: VMDisconnectedException : " + e);223return 2;224} catch ( Exception e ) {225log3("ERROR: Exception : " + e);226return 1;227}228229}230231private void testRun()232throws JDITestRuntimeException, Exception {233234eventRManager = vm.eventRequestManager();235236ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();237cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);238cpRequest.addClassFilter(debuggeeName);239240cpRequest.enable();241vm.resume();242getEventSet();243cpRequest.disable();244245ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();246debuggeeClass = event.referenceType();247248if (!debuggeeClass.name().equals(debuggeeName))249throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");250251log2(" received: ClassPrepareEvent for debuggeeClass");252253String bPointMethod = "methodForCommunication";254String lineForComm = "lineForComm";255BreakpointRequest bpRequest;256257bpRequest = settingBreakpoint(debuggee.threadByNameOrThrow("main"),258debuggeeClass,259bPointMethod, lineForComm, "zero");260bpRequest.enable();261262//------------------------------------------------------ testing section263264log1(" TESTING BEGINS");265266for (int i = 0; ; i++) {267268vm.resume();269breakpointForCommunication();270271int instruction = ((IntegerValue)272(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();273274if (instruction == 0) {275vm.resume();276break;277}278279log1(":::::: case: # " + i);280281//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part282283String accWatchpointName = "var1";284String modWatchpointName = "var2";285String bpLineName = "breakpointLine";286String bpMethodName = "method";287String awFieldName = "awFieldName";288String mwFieldName = "mwFieldName";289String excName = "method";290String menName = "method";291String mexName = "method";292293String namesArray = "threadNames";294295String threadNames[] = {296"awThread" ,297"mwThread" ,298"bpThread" ,299"excThread" ,300"menThread" ,301"mexThread" ,302"stThread"303};304305306EventRequest eRequests[] = new EventRequest[threadNames.length];307int flags = 0;308309ThreadReference eventThreads[] = new ThreadReference[threadNames.length];310311312List allThreads = vm.allThreads();313314log2("......getting: ArrayReference namesRef = (ArrayReference) debuggeeClass.getValue(debuggeeClass.fieldByName(namesArray));");315ArrayReference namesRef = (ArrayReference)316debuggeeClass.getValue(debuggeeClass.fieldByName(namesArray));317log2(" namesRef.length() == " + namesRef.length());318319log2("......getting and chcking up on debuggee threads' names");320for (int n1 = 0; n1 < namesRef.length(); n1++) {321322log2(" String name = ((StringReference) namesRef.getValue(n1)).value();");323String name = ((StringReference) namesRef.getValue(n1)).value();324325label0: {326for (int n2 = 0; n2 < threadNames.length; n2++) {327328if (name.equals(threadNames[n2])) {329ListIterator li = allThreads.listIterator();330for (; li.hasNext(); ) {331ThreadReference thread = (ThreadReference) li.next();332if (thread.name().equals(name)) {333eventThreads[n1] = thread;334break;335}336}337break label0;338}339}340testExitCode = FAILED;341log3("ERROR: no thread found in the debuggee : " + name);342}343}344if (testExitCode == FAILED)345break;346347348log2("......ReferenceType testClass = (ReferenceType) (vm.classesByName(testedClassName)).get(0);");349ReferenceType testClass = (ReferenceType) (vm.classesByName(testedClassName)).get(0);350351log2("......setting up Requests");352for ( int n3 = 0; n3 < namesRef.length(); n3++) {353switch (n3) {354case 0:355if (vm.canWatchFieldAccess()) {356String awName = ( (StringReference) testClass.getValue(357testClass.fieldByName(awFieldName))).value();358eRequests[n3] = settingAccessWatchpoint(eventThreads[n3],359testClass, awName, threadNames[n3]);360eRequests[n3].enable();361flags |= 1;362}363break;364365case 1:366if (vm.canWatchFieldModification() ) {367String mwName = ( (StringReference) testClass.getValue(368testClass.fieldByName(mwFieldName))).value();369eRequests[n3] = settingModificationWatchpoint(eventThreads[n3],370testClass, mwName, threadNames[n3]);371eRequests[n3].enable();372flags |= 1<<1;373}374break;375376case 2:377eRequests[n3] = settingBreakpoint(eventThreads[n3], testClass,378bpMethodName, bpLineName, threadNames[n3]);379eRequests[n3].setSuspendPolicy( EventRequest.SUSPEND_NONE);380eRequests[n3].enable();381flags |= 1<<2;382break;383384case 3:385eRequests[n3] = settingException(eventThreads[n3], debuggeeClass, //testClass,386threadNames[n3]);387eRequests[n3].enable();388flags |= 1<<3;389break;390391case 4:392eRequests[n3] = settingMethodEntry(eventThreads[n3], testClass,393threadNames[n3]);394eRequests[n3].enable();395flags |= 1<<4;396break;397398case 5:399eRequests[n3] = settingMethodExit(eventThreads[n3], testClass,400threadNames[n3]);401eRequests[n3].enable();402flags |= 1<<5;403break;404405case 6:406eRequests[n3] = settingStep(eventThreads[n3], threadNames[n3]);407eRequests[n3].enable();408flags |= 1<<6;409break;410411default:412throw new JDITestRuntimeException("** default case while prepareing requests**");413}414}415416log2(":::::::::vm.resume();");417vm.resume();418419Event event1 = null;420String threadName = null;421int flagsCopy = flags;422String eName = null;423424log2("......getting and checking up on Events");425for (int n4 = 0; n4 < namesRef.length(); n4++) {426int flag;427int index;428getEventSet();429event1 = eventIterator.nextEvent();430431if (event1 instanceof AccessWatchpointEvent) {432eName = "AccessWatchpointEvent";433index = 0;434} else if (event1 instanceof ModificationWatchpointEvent ) {435eName = "ModificationWatchpointEvent";436index = 1;437} else if (event1 instanceof BreakpointEvent ) {438eName = "BreakpointEvent";439index = 2;440} else if (event1 instanceof ExceptionEvent ) {441eName = "ExceptionEvent";442index = 3;443} else if (event1 instanceof MethodEntryEvent ) {444eName = "MethodEntryEvent";445index = 4;446} else if (event1 instanceof MethodExitEvent ) {447eName = "MethodExitEvent";448index = 5;449} else if (event1 instanceof StepEvent ) {450eName = "StepEvent";451index = 6;452} else {453log3("ERROR: else clause in detecting type of event1");454testExitCode = FAILED;455throw new JDITestRuntimeException("** unexpected event **");456}457log2("--------> got: " + eName);458459ThreadReference threadRef = ((LocatableEvent) event1).thread();460461label0: {462for (int n5 = 0; n5 < namesRef.length(); n5++) {463if (threadRef.equals(eventThreads[n5])) {464eventThreads[n5] = null;465threadName = threadNames[index];466break label0;467}468}469testExitCode = FAILED;470log3("ERROR: event's thread is not equal to any tested");471log3(" thread's name == " + threadRef.name());472}473474flag = 1 << index;475if ((flagsCopy & flag) == 0) {476log3("ERROR: event duplication: " + eName);477testExitCode = FAILED;478} else {479flagsCopy ^= flag;480flags |= flag;481}482}483484breakpointForCommunication();485486//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~487}488log1(" TESTING ENDS");489return;490}491492// ============================== test's additional methods493494private AccessWatchpointRequest settingAccessWatchpoint (495ThreadReference thread,496ReferenceType testedClass,497String fieldName,498String property)499throws JDITestRuntimeException {500501log2("......setting up AccessWatchpoint:");502log2(" thread: " + thread + "; class: " + testedClass +503"; fieldName: " + fieldName);504505AccessWatchpointRequest awRequest = null;506try {507Field field = testedClass.fieldByName(fieldName);508awRequest = eventRManager.createAccessWatchpointRequest(field);509awRequest.putProperty("number", property);510awRequest.addThreadFilter(thread);511awRequest.setSuspendPolicy( EventRequest.SUSPEND_NONE);512} catch ( Exception e ) {513log3("ERROR: ATTENTION: Exception within settingAccessWatchpoint() : " + e);514log3(" AN ACCESSWATCHPOINT HAS NOT BEEN SET UP");515throw new JDITestRuntimeException("** FAILURE to set up an AccessWatchpoint **");516}517518log2(" an AccessWatchpoint has been set up");519return awRequest;520}521522private ModificationWatchpointRequest settingModificationWatchpoint (523ThreadReference thread,524ReferenceType testedClass,525String fieldName,526String property)527throws JDITestRuntimeException {528529log2("......setting up ModificationWatchpoint:");530log2(" thread: " + thread + "; class: " + testedClass +531"; fieldName: " + fieldName);532533ModificationWatchpointRequest mwRequest = null;534try {535Field field = testedClass.fieldByName(fieldName);536mwRequest = eventRManager.createModificationWatchpointRequest(field);537mwRequest.putProperty("number", property);538mwRequest.addThreadFilter(thread);539mwRequest.setSuspendPolicy( EventRequest.SUSPEND_NONE);540} catch ( Exception e ) {541log3("ERROR: ATTENTION: Exception within settingModificationWatchpoint() : " + e);542log3(" AN ModificationWATCHPOINT HAS NOT BEEN SET UP");543throw new JDITestRuntimeException("** FAILURE to set up an AccessWatchpoint **");544}545546log2(" a ModificationWatchpoint has been set up");547return mwRequest;548}549550private MethodEntryRequest settingMethodEntry ( ThreadReference thread,551ReferenceType testedClass,552String property)553throws JDITestRuntimeException {554555log2("......setting up MethodEntry:");556log2(" thread: " + thread + "; class: " + testedClass +557"; property: " + property);558559MethodEntryRequest menRequest = null;560try {561menRequest = eventRManager.createMethodEntryRequest();562menRequest.putProperty("number", property);563menRequest.addThreadFilter(thread);564menRequest.addClassFilter(testedClass);565menRequest.setSuspendPolicy( EventRequest.SUSPEND_NONE);566} catch ( Exception e ) {567log3("ERROR: ATTENTION: Exception within settingMethodEntry() : " + e);568log3(" A MethodEntry HAS NOT BEEN SET UP");569throw new JDITestRuntimeException("** FAILURE to set up a MethodEntry **");570}571572log2(" a MethodEntry has been set up");573return menRequest;574}575576private MethodExitRequest settingMethodExit ( ThreadReference thread,577ReferenceType testedClass,578String property)579throws JDITestRuntimeException {580581log2("......setting up MethodExit:");582log2(" thread: " + thread + "; class: " + testedClass +583"; property: " + property);584585MethodExitRequest mexRequest = null;586try {587mexRequest = eventRManager.createMethodExitRequest();588mexRequest.putProperty("number", property);589mexRequest.addThreadFilter(thread);590mexRequest.addClassFilter(testedClass);591mexRequest.setSuspendPolicy( EventRequest.SUSPEND_NONE);592} catch ( Exception e ) {593log3("ERROR: ATTENTION: Exception within settingMethodExit() : " + e);594log3(" A MethodExit HAS NOT BEEN SET UP");595throw new JDITestRuntimeException("** FAILURE to set up a MethodExit **");596}597598log2(" a MethodExit has been set up");599return mexRequest;600}601602private StepRequest settingStep ( ThreadReference thread, String property)603throws JDITestRuntimeException {604605log2("......setting up Step:");606log2(" thread: " + thread + "; property: " + property);607608StepRequest stRequest = null;609try {610stRequest = eventRManager.createStepRequest(thread, StepRequest.STEP_LINE, StepRequest.STEP_OVER);611stRequest.putProperty("number", property);612stRequest.addCountFilter(1);613stRequest.setSuspendPolicy( EventRequest.SUSPEND_NONE);614} catch ( Exception e ) {615log3("ERROR: ATTENTION: Exception within settingStep() : " + e);616log3(" A Step HAS NOT BEEN SET UP");617throw new JDITestRuntimeException("** FAILURE to set up a Step **");618}619620log2(" a Step has been set up");621return stRequest;622}623624625private ExceptionRequest settingException ( ThreadReference thread,626ReferenceType testedClass,627String property)628throws JDITestRuntimeException {629630log2("......setting up Exception:");631log2(" thread: " + thread + "; class: " + testedClass +632"; property: " + property);633634ExceptionRequest excRequest = null;635try {636excRequest = eventRManager.createExceptionRequest(null, true, true);637excRequest.putProperty("number", property);638excRequest.addThreadFilter(thread);639excRequest.addClassFilter(testedClass);640excRequest.setSuspendPolicy( EventRequest.SUSPEND_NONE);641} catch ( Exception e ) {642log3("ERROR: ATTENTION: Exception within settingException() : " + e);643log3(" A Exception HAS NOT BEEN SET UP");644throw new JDITestRuntimeException("** FAILURE to set up a Exception **");645}646647log2(" a Exception has been set up");648return excRequest;649}650651}652653654