1 /*
   2  * Copyright (c) 2007, 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 /*
  25  * @test
  26  *
  27  * @summary converted from VM Testbase nsk/jdwp/ReferenceType/Instances/instances001.
  28  * VM Testbase keywords: [quick, jpda, jdwp, feature_jdk6_jpda, vm6, monitoring]
  29  * VM Testbase readme:
  30  * DESCRIPTION
  31  *     This test performs checking for
  32  *         command set: ReferenceType
  33  *         command: Instances
  34  *     Test checks that debuggee accept the command packet and
  35  *     replies with correct reply packet.
  36  *     Test consists of two compoments:
  37  *         debugger: instances001
  38  *         debuggee: instances001a
  39  *     Debugger uses nsk.share support classes to launch debuggee
  40  *     and obtain Transport object, that represents JDWP transport channel.
  41  *     Also communication channel (IOPipe) is established between
  42  *     debugger and debuggee to exchange with execution commands.
  43  *     For maxInstances in [1, 0, Interger.MAX_VALUE]
  44  *     do
  45  *         Debugger obtains referenceTypeID for 'nsk.share.jdwp.ReferenceType.instances.instances001.TestClass'.
  46  *         Then, debugger creates command packet for Instances command with the
  47  *         found referenceTypeID and maxInstances as an arguments, writes packet to the transport
  48  *         channel, and waits for a reply packet.
  49  *         When reply packet is received, debugger parses the packet structure
  50  *         and extracts number of instances and instance's ids.
  51  *         Debugger checks that received number of instances is correct:
  52  *             - if maxInstances=1 only 1 instance should be returned
  53  *             - if maxInstances=0 or maxInstances=Integer.MAX_VALUE all instances should be returned
  54  *     done
  55  *     Also, test performs checks for cases when incorrect data is sent in command.
  56  *     Following cases are tested:
  57  *         - create command with maxInstances < 0, expect ILLEGAL_ARGUMENT error
  58  *         - create command with typeID = -1, expect INVALID_OBJECT error
  59  *         - create command with threadID instead of referenceTypeID, expect INVALID_CLASS error
  60  *     Finally, debugger sends debuggee signal to quit, waits for it exits
  61  *     and exits too with the proper exit code.
  62  *
  63  * @requires vm.gc != "Z"
  64  * @library /vmTestbase /test/hotspot/jtreg/vmTestbase
  65  *          /test/lib
  66  * @run driver jdk.test.lib.FileInstaller . .
  67  * @build nsk.jdwp.ReferenceType.Instances.instances001.instances001
  68  * @run main/othervm/native PropertyResolvingWrapper
  69  *      nsk.jdwp.ReferenceType.Instances.instances001.instances001
  70  *      -arch=${os.family}-${os.simpleArch}
  71  *      -verbose
  72  *      -waittime=5
  73  *      -debugee.vmkind=java
  74  *      -transport.address=dynamic
  75  *      -debugee.vmkeys="-Xmx128M ${test.vm.opts} ${test.java.opts}"
  76  */
  77 
  78 package nsk.jdwp.ReferenceType.Instances.instances001;
  79 
  80 import java.io.*;
  81 import nsk.share.Consts;
  82 import nsk.share.jdwp.*;
  83 import nsk.share.jpda.AbstractDebuggeeTest;
  84 
  85 public class instances001 extends TestDebuggerType1 {
  86     protected String getDebugeeClassName() {
  87         return nsk.jdwp.ReferenceType.Instances.instances001.instances001a.class.getName();
  88     }
  89 
  90     public static void main(String argv[]) {
  91         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
  92     }
  93 
  94     public static int run(String argv[], PrintStream out) {
  95         return new instances001().runIt(argv, out);
  96     }
  97 
  98     private void testClass(long typeID, int maxInstances, int expectedInstances, boolean expectError, int errorCode) {
  99         try {
 100             int JDWP_COMMAND_ID = JDWP.Command.ReferenceType.Instances;
 101 
 102             log.display("Create command: " + JDWP.commandNames.get(JDWP_COMMAND_ID));
 103             log.display("referenceType = " + typeID);
 104             log.display("maxInstances = " + maxInstances);
 105 
 106             CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);
 107             command.addReferenceTypeID(typeID);
 108             command.addInt(maxInstances);
 109             command.setLength();
 110 
 111             log.display("Sending command packet:\n" + command);
 112             transport.write(command);
 113 
 114             ReplyPacket reply;
 115 
 116             reply = getReply(command, expectError, errorCode);
 117 
 118             if (expectError)
 119                 return;
 120 
 121             int instances = reply.getInt();
 122             log.display("instances = " + instances);
 123 
 124             // check that correct value of 'instances' was received
 125             if (instances != expectedInstances) {
 126                 setSuccess(false);
 127                 log.complain("Unexpected 'instances' value: " + instances + ", expected is " + expectedInstances);
 128             }
 129 
 130             for (int i = 0; i < instances; i++) {
 131                 JDWP.Value value = reply.getValue();
 132                 log.display("tagged-ObjectID = " + value);
 133             }
 134 
 135             if (!reply.isParsed()) {
 136                 setSuccess(false);
 137                 log.complain("Extra trailing bytes found in reply packet at: " + reply.currentPosition());
 138             }
 139         } catch (Exception e) {
 140             setSuccess(false);
 141             log.complain("Caught exception while testing JDWP command: " + e);
 142             e.printStackTrace(log.getOutStream());
 143         }
 144     }
 145 
 146     public void doTest() {
 147         // force GC in debuggee VM to avoid collection of weak references during test execution
 148         forceGC();
 149         pipe.println(instances001a.COMMAND_CREATE_TEST_INSTANCES);
 150 
 151         if (!isDebuggeeReady())
 152             return;
 153 
 154         int expectedInstances = instances001a.expectedCount;
 155 
 156         String testClassName = nsk.jdwp.ReferenceType.Instances.instances001.TestClass.class.getName();
 157 
 158         long typeID = debuggee.getReferenceTypeID(createTypeSignature(testClassName));
 159 
 160         // Note! This test is broken, in the sense that it incorrectly assumes
 161         // that no GC can happen before it walks the heap. In practice, it seems
 162         // to only affect this test when using ZGC. However, this test will also
 163         // fail when using other GCs if an explicit GC is done here.
 164 
 165         // create command with maxInstances=1, only 1 instance should be returned
 166         testClass(typeID, 1, 1, false, 0);
 167         // create command with maxInstances=0, all instances should be returned
 168         testClass(typeID, 0, expectedInstances, false, 0);
 169         // create command with maxInstances=Integer.MAX_VALUE, all instances should be returned
 170         testClass(typeID, Integer.MAX_VALUE, expectedInstances, false, 0);
 171 
 172         // create command with maxInstances < 0, expect ILLEGAL_ARGUMENT error
 173         testClass(typeID, -1, expectedInstances, true, JDWP.Error.ILLEGAL_ARGUMENT);
 174 
 175         // create command with typeID = 1, expect INVALID_OBJECT error
 176         testClass(-1, Integer.MAX_VALUE, expectedInstances, true, JDWP.Error.INVALID_OBJECT);
 177 
 178         // create command with threadID instead of referenceTypeID, expect INVALID_CLASS error
 179         testClass(debuggee.getThreadID("main"), Integer.MAX_VALUE, expectedInstances, true, JDWP.Error.INVALID_CLASS);
 180 
 181         // if GC occurs during test the results should be ignored
 182         resetStatusIfGC();
 183     }
 184 }