test/hotspot/jtreg/vmTestbase/nsk/jdwp/ThreadReference/Stop/stop001.java
changeset 50317 cf71bff5f533
equal deleted inserted replaced
50316:60ebcc705421 50317:cf71bff5f533
       
     1 /*
       
     2  * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 package nsk.jdwp.ThreadReference.Stop;
       
    25 
       
    26 import java.io.*;
       
    27 
       
    28 import nsk.share.*;
       
    29 import nsk.share.jpda.*;
       
    30 import nsk.share.jdwp.*;
       
    31 
       
    32 /**
       
    33  * Test for JDWP command: ThreadReference.Stop.
       
    34  *
       
    35  * See stop001.README for description of test execution.
       
    36  *
       
    37  * This class represents debugger part of the test.
       
    38  * Test is executed by invoking method runIt().
       
    39  * JDWP command is tested in the method testCommand().
       
    40  *
       
    41  * @see #runIt()
       
    42  * @see #testCommand()
       
    43  */
       
    44 public class stop001 {
       
    45 
       
    46     // exit status constants
       
    47     static final int JCK_STATUS_BASE = 95;
       
    48     static final int PASSED = 0;
       
    49     static final int FAILED = 2;
       
    50 
       
    51     // communication signals constants
       
    52     static final String READY = "ready";
       
    53     static final String ERROR = "error";
       
    54     static final String RUN = "run";
       
    55     static final String STOPPED = "stopped";
       
    56     static final String NOT_STOPPED = "not_stopped";
       
    57     static final String QUIT = "quit";
       
    58 
       
    59     // package and classes names constants
       
    60     static final String PACKAGE_NAME = "nsk.jdwp.ThreadReference.Stop";
       
    61     static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "stop001";
       
    62     static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";
       
    63 
       
    64     // tested JDWP command constants
       
    65     static final String JDWP_COMMAND_NAME = "ThreadReference.Stop";
       
    66     static final int JDWP_COMMAND_ID = JDWP.Command.ThreadReference.Stop;
       
    67 
       
    68     // tested class name and signature constants
       
    69     static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";
       
    70     static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";
       
    71 
       
    72     // name of the tested thread and statioc field with thread value
       
    73     static final String TESTED_THREAD_NAME = stop001a.THREAD_NAME;
       
    74     static final String THREAD_FIELD_NAME = stop001a.THREAD_FIELD_NAME;
       
    75     static final String THROWABLE_FIELD_NAME = stop001a.THROWABLE_FIELD_NAME;
       
    76 
       
    77     // usual scaffold objects
       
    78     ArgumentHandler argumentHandler = null;
       
    79     Log log = null;
       
    80     Binder binder = null;
       
    81     Debugee debugee = null;
       
    82     Transport transport = null;
       
    83     IOPipe pipe = null;
       
    84 
       
    85     // test passed or not
       
    86     boolean success = true;
       
    87 
       
    88     // -------------------------------------------------------------------
       
    89 
       
    90     /**
       
    91      * Start test from command line.
       
    92      */
       
    93     public static void main (String argv[]) {
       
    94         System.exit(run(argv,System.out) + JCK_STATUS_BASE);
       
    95     }
       
    96 
       
    97     /**
       
    98      * Start JCK-compilant test.
       
    99      */
       
   100     public static int run(String argv[], PrintStream out) {
       
   101         return new stop001().runIt(argv, out);
       
   102     }
       
   103 
       
   104     // -------------------------------------------------------------------
       
   105 
       
   106     /**
       
   107      * Perform test execution.
       
   108      */
       
   109     public int runIt(String argv[], PrintStream out) {
       
   110 
       
   111         // make log for debugger messages
       
   112         argumentHandler = new ArgumentHandler(argv);
       
   113         log = new Log(out, argumentHandler);
       
   114 
       
   115         // execute test and display results
       
   116         try {
       
   117             log.display("\n>>> Preparing debugee for testing \n");
       
   118 
       
   119             // launch debuggee
       
   120             binder = new Binder(argumentHandler, log);
       
   121             log.display("Launching debugee");
       
   122             debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);
       
   123             transport = debugee.getTransport();
       
   124             pipe = debugee.createIOPipe();
       
   125 
       
   126             // make debuggee ready for testing
       
   127             prepareDebugee();
       
   128 
       
   129             // work with prepared debuggee
       
   130             try {
       
   131                 log.display("\n>>> Obtaining requred data from debugee \n");
       
   132 
       
   133                 // query debuggee for classID of tested class
       
   134                 log.display("Getting classID by signature:\n"
       
   135                             + "  " + TESTED_CLASS_SIGNATURE);
       
   136                 long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);
       
   137                 log.display("  got classID: " + classID);
       
   138 
       
   139                 // query debuggee for threadID value from a static field
       
   140                 log.display("Getting threadID value from static field: "
       
   141                             + THREAD_FIELD_NAME);
       
   142                 long threadID = queryObjectID(classID, THREAD_FIELD_NAME, JDWP.Tag.THREAD);
       
   143                 log.display("  got threadID: " + threadID);
       
   144 
       
   145                 // query debuggee for throwable objectID value from a static field
       
   146                 log.display("Getting throwable objectID value from static field: "
       
   147                             + THROWABLE_FIELD_NAME);
       
   148                 long objectID = queryObjectID(classID, THROWABLE_FIELD_NAME, JDWP.Tag.OBJECT);
       
   149                 log.display("  got objectID: " + objectID);
       
   150 
       
   151                 // perform testing JDWP command
       
   152                 log.display("\n>>> Testing JDWP command \n");
       
   153                 testCommand(threadID, objectID);
       
   154 
       
   155                 log.display("\n>>> Checking that tested thread was really stopped \n");
       
   156 
       
   157                 // check if the thread was really stopped
       
   158                 confirmThreadStopped();
       
   159 
       
   160             } finally {
       
   161                 log.display("\n>>> Finishing test \n");
       
   162 
       
   163                 // quit debugee
       
   164                 quitDebugee();
       
   165             }
       
   166 
       
   167         } catch (Failure e) {
       
   168             log.complain("TEST FAILED: " + e.getMessage());
       
   169             success = false;
       
   170         } catch (Exception e) {
       
   171             e.printStackTrace(out);
       
   172             log.complain("Caught unexpected exception while running the test:\n\t" + e);
       
   173             success = false;
       
   174         }
       
   175 
       
   176         if (!success) {
       
   177             log.complain("TEST FAILED");
       
   178             return FAILED;
       
   179         }
       
   180 
       
   181         out.println("TEST PASSED");
       
   182         return PASSED;
       
   183 
       
   184     }
       
   185 
       
   186     /**
       
   187      * Prepare debugee for testing and waiting for ready signal.
       
   188      */
       
   189     void prepareDebugee() {
       
   190         // wait for VM_INIT event from debugee
       
   191         log.display("Waiting for VM_INIT event");
       
   192         debugee.waitForVMInit();
       
   193 
       
   194         // query debugee for VM-dependent ID sizes
       
   195         log.display("Querying for IDSizes");
       
   196         debugee.queryForIDSizes();
       
   197 
       
   198         // resume initially suspended debugee
       
   199         log.display("Resuming debugee VM");
       
   200         debugee.resume();
       
   201 
       
   202         // wait for READY signal from debugee
       
   203         log.display("Waiting for signal from debugee: " + READY);
       
   204         String signal = pipe.readln();
       
   205         log.display("Received signal from debugee: " + signal);
       
   206         if (signal == null) {
       
   207             throw new TestBug("Null signal received from debugee: " + signal
       
   208                             + " (expected: " + READY + ")");
       
   209         } else if (signal.equals(ERROR)) {
       
   210             throw new TestBug("Debugee was not able to start tested thread"
       
   211                             + " (received signal: " + signal + ")");
       
   212         } else if (!signal.equals(READY)) {
       
   213             throw new TestBug("Unexpected signal received from debugee: " + signal
       
   214                             + " (expected: " + READY + ")");
       
   215         }
       
   216     }
       
   217 
       
   218     /**
       
   219      * Sending debugee signal to quit and waiting for it exits.
       
   220      */
       
   221     void quitDebugee() {
       
   222         // send debugee signal to quit
       
   223         log.display("Sending signal to debugee: " + QUIT);
       
   224         pipe.println(QUIT);
       
   225 
       
   226         // wait for debugee exits
       
   227         log.display("Waiting for debugee exits");
       
   228         int code = debugee.waitFor();
       
   229 
       
   230         // analize debugee exit status code
       
   231         if (code == JCK_STATUS_BASE + PASSED) {
       
   232             log.display("Debugee PASSED with exit code: " + code);
       
   233         } else {
       
   234             log.complain("Debugee FAILED with exit code: " + code);
       
   235             success = false;
       
   236         }
       
   237     }
       
   238 
       
   239     /**
       
   240      * Query debuggee for objectID value from the class static field.
       
   241      */
       
   242     long queryObjectID(long classID, String fieldName, byte tag) {
       
   243         // get fieledID for static field (declared in the class)
       
   244         long fieldID = debugee.getClassFieldID(classID, fieldName, true);
       
   245         // get value of the field
       
   246         JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);
       
   247 
       
   248         // check that value has THREAD tag
       
   249         if (value.getTag() != tag) {
       
   250             throw new Failure("Not threadID value returned from debuggee: " + value);
       
   251         }
       
   252 
       
   253         // extract threadID from the value
       
   254         long objectID = ((Long)value.getValue()).longValue();
       
   255         return objectID;
       
   256     }
       
   257 
       
   258     /**
       
   259      * Perform testing JDWP command for specified threadID and throwable objectID.
       
   260      */
       
   261     void testCommand(long threadID, long objectID) {
       
   262         // create command packet and fill requred out data
       
   263         log.display("Create command packet:");
       
   264         log.display("Command: " + JDWP_COMMAND_NAME);
       
   265         CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);
       
   266         log.display("  threadID: " + threadID);
       
   267         command.addObjectID(threadID);
       
   268         log.display("  throwable: " + objectID);
       
   269         command.addObjectID(objectID);
       
   270         command.setLength();
       
   271 
       
   272         // send command packet to debugee
       
   273         try {
       
   274             log.display("Sending command packet:\n" + command);
       
   275             transport.write(command);
       
   276         } catch (IOException e) {
       
   277             log.complain("Unable to send command packet:\n\t" + e);
       
   278             success = false;
       
   279             return;
       
   280         }
       
   281 
       
   282         ReplyPacket reply = new ReplyPacket();
       
   283 
       
   284         // receive reply packet from debugee
       
   285         try {
       
   286             log.display("Waiting for reply packet");
       
   287             transport.read(reply);
       
   288             log.display("Reply packet received:\n" + reply);
       
   289         } catch (IOException e) {
       
   290             log.complain("Unable to read reply packet:\n\t" + e);
       
   291             success = false;
       
   292             return;
       
   293         }
       
   294 
       
   295         // check reply packet header
       
   296         try{
       
   297             log.display("Checking reply packet header");
       
   298             reply.checkHeader(command.getPacketID());
       
   299         } catch (BoundException e) {
       
   300             log.complain("Bad header of reply packet:\n\t" + e.getMessage());
       
   301             success = false;
       
   302             return;
       
   303         }
       
   304 
       
   305         // start parsing reply packet data
       
   306         log.display("Parsing reply packet:");
       
   307         reply.resetPosition();
       
   308 
       
   309         // there are no data to extract
       
   310 
       
   311         // check for extra data in reply packet
       
   312         if (!reply.isParsed()) {
       
   313             log.complain("Extra trailing bytes found in reply packet at: "
       
   314                         + reply.offsetString());
       
   315             success = false;
       
   316         }
       
   317 
       
   318     }
       
   319 
       
   320     /**
       
   321      * Wait for signal from debugee that thread was really stopped
       
   322      */
       
   323     void confirmThreadStopped() {
       
   324 
       
   325         // send debugee signal to run
       
   326         log.display("Sending signal to debugee: " + RUN);
       
   327         pipe.println(RUN);
       
   328 
       
   329         // wait for signal DONE from debuggee
       
   330         log.display("Waiting for signal from debugee: " + STOPPED);
       
   331         String signal = pipe.readln();
       
   332         log.display("Received signal from debugee: " + signal);
       
   333 
       
   334         // check received signal
       
   335         if (signal == null) {
       
   336             throw new TestBug("Null signal received from debugee: " + signal
       
   337                             + " (expected: " + STOPPED + ")");
       
   338         } else if (signal.equals(STOPPED)) {
       
   339             log.display("Tested thread was really stopped into debuggee");
       
   340         } else if (signal.equals(NOT_STOPPED)) {
       
   341             log.complain("Tested thread was NOT really stopped into debuggee");
       
   342             success = false;
       
   343         } else {
       
   344             throw new TestBug("Unexpected signal received from debugee: " + signal
       
   345                             + " (expected: " + STOPPED + ")");
       
   346         }
       
   347 
       
   348     }
       
   349 
       
   350 }