test/hotspot/jtreg/vmTestbase/nsk/jdwp/ArrayReference/GetValues/getvalues002.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.ArrayReference.GetValues;
       
    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: ArrayReference.GetValues.
       
    34  *
       
    35  * See getvalues002.README for description of test execution.
       
    36  *
       
    37  * Test is executed by invoking method runIt().
       
    38  * JDWP command is tested in method testCommand().
       
    39  *
       
    40  * @see #runIt()
       
    41  * @see #testCommand()
       
    42  */
       
    43 public class getvalues002 {
       
    44 
       
    45     // exit status constants
       
    46     static final int JCK_STATUS_BASE = 95;
       
    47     static final int PASSED = 0;
       
    48     static final int FAILED = 2;
       
    49 
       
    50     // communication signals constants
       
    51     static final String READY = "ready";
       
    52     static final String QUIT = "quit";
       
    53 
       
    54     // package and classes names constants
       
    55     static final String PACKAGE_NAME = "nsk.jdwp.ArrayReference.GetValues";
       
    56     static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "getvalues002";
       
    57     static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";
       
    58 
       
    59     // tested JDWP command constants
       
    60     static final String JDWP_COMMAND_NAME = "ArrayReference.GetValues";
       
    61     static final int JDWP_COMMAND_ID = JDWP.Command.ArrayReference.GetValues;
       
    62 
       
    63     // tested class name and signature constants
       
    64     static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";
       
    65     static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";
       
    66 
       
    67     // name of the static field in the tested class with the tested object value
       
    68     static final String ARRAY_FIELD_NAME = getvalues002a.ARRAY_FIELD_NAME;
       
    69     static final int ARRAY_LENGTH = getvalues002a.ARRAY_LENGTH;
       
    70 
       
    71     // names of the statc fields with object values
       
    72     static final int FIELDS_COUNT = getvalues002a.FIELDS_COUNT;
       
    73     static final String FIELD_NAMES[] = {
       
    74                             "nullObject",
       
    75                             "baseObject",
       
    76                             "derivedObject",
       
    77                             "stringObject",
       
    78                             "primitiveArrayObject",
       
    79                             "objectArrayObject",
       
    80                             "threadObject",
       
    81                             "threadGroupObject",
       
    82                             "classObject",
       
    83                             "classLoaderObject",
       
    84                         };
       
    85     static final byte FIELD_TAGS[] = {
       
    86                             JDWP.Tag.OBJECT,        // nullObject
       
    87                             JDWP.Tag.OBJECT,        // baseobject
       
    88                             JDWP.Tag.OBJECT,        // derivedObject
       
    89                             JDWP.Tag.STRING,        // stringObject
       
    90                             JDWP.Tag.ARRAY,         // primitiveArrayObject
       
    91                             JDWP.Tag.ARRAY,         // objectArrayObject
       
    92                             JDWP.Tag.THREAD,        // threadObject
       
    93                             JDWP.Tag.THREAD_GROUP,  // threadGroupObject
       
    94                             JDWP.Tag.CLASS_OBJECT,  // classObject
       
    95                             JDWP.Tag.CLASS_LOADER   // classLoaderObject
       
    96                         };
       
    97 
       
    98     // first index and number of array components to get
       
    99     static final int ARRAY_FIRST_INDEX = getvalues002a.ARRAY_FIRST_INDEX;
       
   100     static final int ARRAY_ITEMS_COUNT = FIELDS_COUNT;
       
   101 
       
   102     // usual scaffold objects
       
   103     ArgumentHandler argumentHandler = null;
       
   104     Log log = null;
       
   105     Binder binder = null;
       
   106     Debugee debugee = null;
       
   107     Transport transport = null;
       
   108     IOPipe pipe = null;
       
   109 
       
   110     // test passed or not
       
   111     boolean success = true;
       
   112 
       
   113     // -------------------------------------------------------------------
       
   114 
       
   115     /**
       
   116      * Start test from command line.
       
   117      */
       
   118     public static void main (String argv[]) {
       
   119         System.exit(run(argv,System.out) + JCK_STATUS_BASE);
       
   120     }
       
   121 
       
   122     /**
       
   123      * Start JCK-compilant test.
       
   124      */
       
   125     public static int run(String argv[], PrintStream out) {
       
   126         return new getvalues002().runIt(argv, out);
       
   127     }
       
   128 
       
   129     // -------------------------------------------------------------------
       
   130 
       
   131     /**
       
   132      * Perform test execution.
       
   133      */
       
   134     public int runIt(String argv[], PrintStream out) {
       
   135 
       
   136         // make log for debugger messages
       
   137         argumentHandler = new ArgumentHandler(argv);
       
   138         log = new Log(out, argumentHandler);
       
   139 
       
   140         // execute test and display results
       
   141         try {
       
   142             log.display("\n>>> Preparing debugee for testing \n");
       
   143 
       
   144             // launch debugee
       
   145             binder = new Binder(argumentHandler, log);
       
   146             log.display("Launching debugee");
       
   147             debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);
       
   148             transport = debugee.getTransport();
       
   149             pipe = debugee.createIOPipe();
       
   150 
       
   151             // make debuggee ready for testing
       
   152             prepareDebugee();
       
   153 
       
   154             // work with prepared debugee
       
   155             try {
       
   156                 log.display("\n>>> Obtaining requred data from debugee \n");
       
   157 
       
   158                 // query debugee for TypeID of tested class
       
   159                 log.display("Getting ReferenceTypeID by signature:\n"
       
   160                             + "  " + TESTED_CLASS_SIGNATURE);
       
   161                 long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);
       
   162                 log.display("  got classID: " + classID);
       
   163 
       
   164                 // query debugee for fieldIDs of the class static fields
       
   165                 log.display("Getting fieldIDs for static fields of the class");
       
   166                 long fieldIDs[] = queryClassFieldIDs(classID);
       
   167                 log.display("  got fields: " + fieldIDs.length);
       
   168 
       
   169                 // query debugee for object values of the fields
       
   170                 log.display("Getting objectID values of the static fields");
       
   171                 long objectIDs[] = queryClassFieldValues(classID, fieldIDs);
       
   172                 log.display("  got objectIDs: " + objectIDs.length);
       
   173 
       
   174                 // query debuggee for arrayID value from static field
       
   175                 log.display("Getting arrayID value from static field: "
       
   176                             + ARRAY_FIELD_NAME);
       
   177                 long arrayID = queryObjectID(classID,
       
   178                             ARRAY_FIELD_NAME, JDWP.Tag.ARRAY);
       
   179                 log.display("  got arrayID: " + arrayID);
       
   180 
       
   181                 // perform testing JDWP command
       
   182                 log.display("\n>>> Testing JDWP command \n");
       
   183                 testCommand(arrayID, objectIDs);
       
   184 
       
   185             } finally {
       
   186                 // quit debugee
       
   187                 log.display("\n>>> Finishing test \n");
       
   188                 quitDebugee();
       
   189             }
       
   190 
       
   191         } catch (Failure e) {
       
   192             log.complain("TEST FAILED: " + e.getMessage());
       
   193             e.printStackTrace(out);
       
   194             success = false;
       
   195         } catch (Exception e) {
       
   196             log.complain("Caught unexpected exception:\n" + e);
       
   197             e.printStackTrace(out);
       
   198             success = false;
       
   199         }
       
   200 
       
   201         if (!success) {
       
   202             log.complain("TEST FAILED");
       
   203             return FAILED;
       
   204         }
       
   205 
       
   206         out.println("TEST PASSED");
       
   207         return PASSED;
       
   208 
       
   209     }
       
   210 
       
   211     /**
       
   212      * Prepare debugee for testing and waiting for ready signal.
       
   213      */
       
   214     void prepareDebugee() {
       
   215         // wait for VM_INIT event from debugee
       
   216         log.display("Waiting for VM_INIT event");
       
   217         debugee.waitForVMInit();
       
   218 
       
   219         // query debugee for VM-dependent ID sizes
       
   220         log.display("Querying for IDSizes");
       
   221         debugee.queryForIDSizes();
       
   222 
       
   223         // resume initially suspended debugee
       
   224         log.display("Resuming debugee VM");
       
   225         debugee.resume();
       
   226 
       
   227         // wait for READY signal from debugee
       
   228         log.display("Waiting for signal from debugee: " + READY);
       
   229         String signal = pipe.readln();
       
   230         log.display("Received signal from debugee: " + signal);
       
   231         if (! signal.equals(READY)) {
       
   232             throw new TestBug("Unexpected signal received form debugee: " + signal
       
   233                             + " (expected: " + READY + ")");
       
   234         }
       
   235     }
       
   236 
       
   237     /**
       
   238      * Sending debugee signal to quit and waiting for it exits.
       
   239      */
       
   240     void quitDebugee() {
       
   241         // send debugee signal to quit
       
   242         log.display("Sending signal to debugee: " + QUIT);
       
   243         pipe.println(QUIT);
       
   244 
       
   245         // wait for debugee exits
       
   246         log.display("Waiting for debugee exits");
       
   247         int code = debugee.waitFor();
       
   248 
       
   249         // analize debugee exit status code
       
   250         if (code == JCK_STATUS_BASE + PASSED) {
       
   251             log.display("Debugee PASSED with exit code: " + code);
       
   252         } else {
       
   253             log.complain("Debugee FAILED with exit code: " + code);
       
   254             success = false;
       
   255         }
       
   256     }
       
   257 
       
   258     /**
       
   259      * Query debugee for fieldID's of the class static fields.
       
   260      */
       
   261     long[] queryClassFieldIDs(long classID) {
       
   262 
       
   263         long[] fieldIDs = new long[FIELDS_COUNT];
       
   264         for (int i = 0; i < FIELDS_COUNT; i++) {
       
   265             fieldIDs[i] = 0;
       
   266         }
       
   267 
       
   268         // compose ReferenceType.Fields command packet
       
   269         CommandPacket command = new CommandPacket(JDWP.Command.ReferenceType.Fields);
       
   270         command.addReferenceTypeID(classID);
       
   271         command.setLength();
       
   272 
       
   273         // send the command and receive reply
       
   274         ReplyPacket reply = debugee.receiveReplyFor(command);
       
   275 
       
   276         // extract fieldIDs from the reply packet
       
   277         try {
       
   278             reply.resetPosition();
       
   279 
       
   280             int declared = reply.getInt();
       
   281 
       
   282             for (int i = 0; i < declared; i++ ) {
       
   283                 long fieldID = reply.getFieldID();
       
   284                 String name = reply.getString();
       
   285                 String signature = reply.getString();
       
   286                 int modBits = reply.getInt();
       
   287 
       
   288                 for (int j = 0; j < FIELDS_COUNT; j++) {
       
   289                     if (FIELD_NAMES[j].equals(name)) {
       
   290                         fieldIDs[j] = fieldID;
       
   291                     }
       
   292                 }
       
   293             }
       
   294 
       
   295             for (int i = 0; i < FIELDS_COUNT; i++) {
       
   296                 if (fieldIDs[i] == 0) {
       
   297                     log.complain("Not found fieldID for static field: " + FIELD_NAMES[i]);
       
   298                     throw new Failure("Error occured while getting static fieldIDs for classID: "
       
   299                                     + classID);
       
   300                 }
       
   301             }
       
   302 
       
   303         } catch (BoundException e) {
       
   304             log.complain("Unable to parse reply packet for ReferenceType.Fields command:\n\t"
       
   305                         + e);
       
   306             log.complain("Received reply packet:\n"
       
   307                         + reply);
       
   308             throw new Failure("Error occured while getting static fieldIDs for classID: " + classID);
       
   309         }
       
   310 
       
   311         return fieldIDs;
       
   312     }
       
   313 
       
   314     /**
       
   315      * Query debugee for objectID values of the class fields.
       
   316      */
       
   317     long[] queryClassFieldValues(long classID, long fieldIDs[]) {
       
   318         String error = "Error occured while getting object values for static fields of classID: "
       
   319                                 + classID;
       
   320 
       
   321         // compose ReferenceType.Fields command packet
       
   322         CommandPacket command = new CommandPacket(JDWP.Command.ReferenceType.GetValues);
       
   323         command.addReferenceTypeID(classID);
       
   324         command.addInt(FIELDS_COUNT);
       
   325         for (int i = 0; i < FIELDS_COUNT; i++) {
       
   326             command.addFieldID(fieldIDs[i]);
       
   327         }
       
   328         command.setLength();
       
   329 
       
   330         // send the command and receive reply
       
   331         ReplyPacket reply = debugee.receiveReplyFor(command);
       
   332 
       
   333         // extract values from the reply packet
       
   334         try {
       
   335             reply.resetPosition();
       
   336 
       
   337             int valuesCount = reply.getInt();
       
   338             if (valuesCount != FIELDS_COUNT) {
       
   339                 log.complain("Unexpected number of values for static fields: " + valuesCount
       
   340                             + " (expected: " + FIELDS_COUNT + ")");
       
   341                 throw new Failure(error);
       
   342             }
       
   343 
       
   344             long objectIDs[] = new long[valuesCount];
       
   345             for (int i = 0; i < valuesCount; i++ ) {
       
   346                 JDWP.Value value = reply.getValue();
       
   347                 byte tag = value.getTag();
       
   348                 if (tag != FIELD_TAGS[i]) {
       
   349                     log.complain("Unexpected tag of oblectID value for static field "
       
   350                                 + FIELD_NAMES[i] + ": " + tag
       
   351                                 + " (expected: " + FIELD_TAGS[i] + ")");
       
   352                     throw new Failure(error);
       
   353                 }
       
   354                 long objectID = ((Long)value.getValue()).longValue();
       
   355                 objectIDs[i] = objectID;
       
   356             }
       
   357             return objectIDs;
       
   358         } catch (BoundException e) {
       
   359             log.complain("Unable to parse reply packet for ReferenceType.GetValues command:\n\t"
       
   360                         + e);
       
   361             log.complain("Received reply packet:\n"
       
   362                         + reply);
       
   363             throw new Failure("Error occured while getting static fields values for classID: " + classID);
       
   364         }
       
   365     }
       
   366 
       
   367     /**
       
   368      * Query debuggee for objectID value of static class field.
       
   369      */
       
   370     long queryObjectID(long classID, String fieldName, byte tag) {
       
   371         // get fieledID for static field (declared in the class)
       
   372         long fieldID = debugee.getClassFieldID(classID, fieldName, true);
       
   373         // get value of the field
       
   374         JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);
       
   375 
       
   376         // check that value has THREAD tag
       
   377         if (value.getTag() != tag) {
       
   378             throw new Failure("Wrong objectID tag received from field \"" + fieldName
       
   379                             + "\": " + value.getTag() + " (expected: " + tag + ")");
       
   380         }
       
   381 
       
   382         // extract threadID from the value
       
   383         long objectID = ((Long)value.getValue()).longValue();
       
   384         return objectID;
       
   385     }
       
   386 
       
   387     /**
       
   388      * Perform testing JDWP command for specified objectID.
       
   389      */
       
   390     void testCommand(long arrayID, long objectIDs[]) {
       
   391         // create command packet
       
   392         log.display("Create command packet:");
       
   393         log.display("Command: " + JDWP_COMMAND_NAME);
       
   394         CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);
       
   395 
       
   396         // add out data to the command packet
       
   397         log.display("  arrayID: " + arrayID);
       
   398         command.addObjectID(arrayID);
       
   399         log.display("  firstIndex: " + ARRAY_FIRST_INDEX);
       
   400         command.addInt(ARRAY_FIRST_INDEX);
       
   401         log.display("  length: " + ARRAY_ITEMS_COUNT);
       
   402         command.addInt(ARRAY_ITEMS_COUNT);
       
   403         command.setLength();
       
   404 
       
   405         // send command packet to debugee
       
   406         try {
       
   407             log.display("Sending command packet:\n" + command);
       
   408             transport.write(command);
       
   409         } catch (IOException e) {
       
   410             log.complain("Unable to send command packet:\n" + e);
       
   411             success = false;
       
   412             return;
       
   413         }
       
   414 
       
   415         ReplyPacket reply = new ReplyPacket();
       
   416 
       
   417         // receive reply packet from debugee
       
   418         try {
       
   419             log.display("Waiting for reply packet");
       
   420             transport.read(reply);
       
   421             log.display("Reply packet received:\n" + reply);
       
   422         } catch (IOException e) {
       
   423             log.complain("Unable to read reply packet:\n" + e);
       
   424             success = false;
       
   425             return;
       
   426         }
       
   427 
       
   428         // check reply packet header
       
   429         try{
       
   430             log.display("Checking reply packet header");
       
   431             reply.checkHeader(command.getPacketID());
       
   432         } catch (BoundException e) {
       
   433             log.complain("Bad header of reply packet: " + e.getMessage());
       
   434             success = false;
       
   435         }
       
   436 
       
   437         // start parsing reply packet data
       
   438         log.display("Parsing reply packet:");
       
   439         reply.resetPosition();
       
   440 
       
   441         // extract values tag
       
   442         byte tag = (byte)0;
       
   443         try {
       
   444             tag = reply.getByte();
       
   445             log.display("  tag: " + tag);
       
   446 
       
   447         } catch (BoundException e) {
       
   448             log.complain("Unable to extract values tag from reply packet:\n\t"
       
   449                         + e.getMessage());
       
   450             success = false;
       
   451         }
       
   452 
       
   453         // check if number of values are as expected
       
   454         if (tag != JDWP.Tag.OBJECT) {
       
   455             log.complain("Unexpected values tag received:" + tag
       
   456                         + " (expected: " + JDWP.Tag.OBJECT + ")");
       
   457             success = false;
       
   458         }
       
   459 
       
   460         // extract number of values
       
   461         int values = 0;
       
   462         try {
       
   463             values = reply.getInt();
       
   464             log.display("  values: " + values);
       
   465 
       
   466         } catch (BoundException e) {
       
   467             log.complain("Unable to extract number of values from reply packet:\n\t"
       
   468                         + e.getMessage());
       
   469             success = false;
       
   470         }
       
   471 
       
   472         // check if number of values are as expected
       
   473         if (values < 0) {
       
   474             log.complain("Negative number of values received:" + values
       
   475                         + " (expected: " + ARRAY_ITEMS_COUNT + ")");
       
   476             success = false;
       
   477         } else if (values != ARRAY_ITEMS_COUNT) {
       
   478             log.complain("Unexpected number of values received:" + values
       
   479                         + " (expected: " + ARRAY_ITEMS_COUNT + ")");
       
   480             success = false;
       
   481         }
       
   482 
       
   483         // extract and check each value
       
   484         for (int i = 0; i < values; i++ ) {
       
   485             int index = i + ARRAY_FIRST_INDEX;
       
   486             log.display("  value #" + i + " (index: " + index  + ")");
       
   487 
       
   488             // extract value
       
   489             JDWP.Value value = null;
       
   490             try {
       
   491                 value = reply.getValue();
       
   492                 log.display("    value: " + value);
       
   493             } catch (BoundException e) {
       
   494                 log.complain("Unable to extract " + i + " value from reply packet:\n\t"
       
   495                             + e.getMessage());
       
   496                 success = false;
       
   497                 break;
       
   498             }
       
   499 
       
   500             // check that value's tag is as expected
       
   501             byte valueTag = value.getTag();
       
   502             if (valueTag != FIELD_TAGS[i]) {
       
   503                 log.complain("Unexpected value tag for " + index + " component ("
       
   504                             + FIELD_NAMES[i] + ") received: " + valueTag
       
   505                             + " (expected: " + FIELD_TAGS[i] + ")");
       
   506                 success = false;
       
   507             }
       
   508 
       
   509             // check that value's objectID is as expected
       
   510             long objectID = ((Long)value.getValue()).longValue();
       
   511             if (objectID != objectIDs[i]) {
       
   512                 log.complain("Unexpected objectID for " + index + " component ("
       
   513                             + FIELD_NAMES[i]  + ") received: " + objectID
       
   514                             + " (expected: " + objectIDs[i] + ")");
       
   515                 success = false;
       
   516             }
       
   517         }
       
   518 
       
   519         // check for extra data in reply packet
       
   520         if (! reply.isParsed()) {
       
   521             log.complain("Extra trailing bytes found in reply packet at: "
       
   522                         + "0x" + reply.toHexString(reply.currentDataPosition(), 4));
       
   523             success = false;
       
   524         }
       
   525     }
       
   526 
       
   527 }