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/VirtualMachine/InstanceCounts/instanceCounts001.
  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: VirtualMachine
  33  *         command: InstanceCounts
  34  *     Test checks that debuggee accept the command packet and
  35  *     replies with correct reply packet.
  36  *     Test consists of two compoments:
  37  *         debugger: instanceCounts001
  38  *         debuggee: instanceCounts001a
  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  *     Debuggee during startup creates instances of 'nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.TestClass1'
  44  *     and 'nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.TestClass2' reachable via references with types
  45  *     which should be supported by command VirtualMachine.InstanceCounts:
  46  *         - strong reference
  47  *         - soft reference
  48  *         - weak reference
  49  *         - phantom reference
  50  *         - JNI global reference
  51  *         - JNI local reference
  52  *     First, debugger obtains referenceTypeID for 'nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.TestClass1'.
  53  *     Then, debugger creates command packet for InstanceCounts command with the found referenceTypeID
  54  *     as an argument, writes packet to the transport      channel, and waits for a reply packet.
  55  *     When reply packet is received, debugger parses the packet structure, extracts number of instances
  56  *     and checks that received value is correct.
  57  *     Then debugger repeat described above actions, but created command packet contains 2 referenceTypeIDs for
  58  *     'nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.TestClass1' and 'nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.TestClass2'.
  59  *         Also, test performs check for case when incorrect data is sent in command:
  60  *                 - create command with refTypesCount < 0, expect ILLEGAL_ARGUMENT error
  61  *     Finally, debugger sends debuggee signal to quit, waits for it exits
  62  *     and exits too with the proper exit code.
  63  *
  64  * @library /vmTestbase /test/hotspot/jtreg/vmTestbase
  65  *          /test/lib
  66  * @run driver jdk.test.lib.FileInstaller . .
  67  * @build nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.instanceCounts001
  68  *        nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.instanceCounts001a
  69  * @run main/othervm/native PropertyResolvingWrapper
  70  *      nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.instanceCounts001
  71  *      -arch=${os.family}-${os.simpleArch}
  72  *      -verbose
  73  *      -waittime=5
  74  *      -debugee.vmkind=java
  75  *      -transport.address=dynamic
  76  *      -debugee.vmkeys="-Xmx128M ${test.vm.opts} ${test.java.opts}"
  77  */
  78 
  79 package nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001;
  80 
  81 import java.io.*;
  82 import nsk.share.Consts;
  83 import nsk.share.jdwp.*;
  84 import nsk.share.jpda.AbstractDebuggeeTest;
  85 
  86 public class instanceCounts001 extends TestDebuggerType1 {
  87     protected String getDebugeeClassName() {
  88         return "nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.instanceCounts001a";
  89     }
  90 
  91     public static void main(String argv[]) {
  92         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
  93     }
  94 
  95     public static int run(String argv[], PrintStream out) {
  96         return new instanceCounts001().runIt(argv, out);
  97     }
  98 
  99     private void testCommand(long typeIDs[], int refTypesCount, int[] expectedInstanceCounts, boolean expectError, int errorCode) {
 100         try {
 101             int JDWP_COMMAND_ID = JDWP.Command.VirtualMachine.InstanceCounts;
 102 
 103             log.display("Create command: " + JDWP.commandNames.get(JDWP_COMMAND_ID));
 104             log.display("refTypesCount = " + refTypesCount);
 105             for (long typeID : typeIDs)
 106                 log.display("refType = " + typeID);
 107 
 108             CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);
 109             command.addInt(refTypesCount);
 110 
 111             for (int i = 0; i < refTypesCount; i++)
 112                 command.addReferenceTypeID(typeIDs[i]);
 113 
 114             command.setLength();
 115 
 116             log.display("Sending command packet:\n" + command);
 117             transport.write(command);
 118 
 119             ReplyPacket reply;
 120 
 121             reply = getReply(command, expectError, errorCode);
 122 
 123             if (expectError)
 124                 return;
 125 
 126             int counts = reply.getInt();
 127             log.display("counts = " + counts);
 128 
 129             // check received 'counts' value
 130             if (counts != refTypesCount) {
 131                 setSuccess(false);
 132                 log.complain("Invalid 'counts' value, expected is: " + refTypesCount);
 133             }
 134 
 135             // check received 'instanceCount' value for all classes
 136             for (int i = 0; i < counts; i++) {
 137                 long instanceCount = reply.getLong();
 138                 log.display("instanceCount = " + instanceCount);
 139 
 140                 if (instanceCount != expectedInstanceCounts[i]) {
 141                     setSuccess(false);
 142                     log.complain("Unexpected value 'instanceCount': " + instanceCount + ", expected is " + expectedInstanceCounts[i]);
 143                 }
 144             }
 145 
 146             if (!reply.isParsed()) {
 147                 setSuccess(false);
 148                 log.complain("Extra trailing bytes found in reply packet at: " + reply.currentPosition());
 149             }
 150         } catch (Exception e) {
 151             setSuccess(false);
 152             log.complain("Caught exception while testing JDWP command: " + e);
 153             e.printStackTrace(log.getOutStream());
 154         }
 155     }
 156 
 157     public void doTest() {
 158         String testClass1 = nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.TestClass1.class.getName();
 159         String testClass2 = nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.TestClass2.class.getName();
 160 
 161         forceGC();
 162         pipe.println(instanceCounts001a.COMMAND_CREATE_TEST_INSTANCES);
 163 
 164         if (!isDebuggeeReady())
 165             return;
 166 
 167         int expectedCount = instanceCounts001a.expectedCount;
 168 
 169         String classNames[];
 170 
 171         classNames = new String[] { createTypeSignature(testClass1) };
 172 
 173         long typeIDs[];
 174 
 175         typeIDs = new long[classNames.length];
 176 
 177         // create valid command for 1 class
 178         for (int i = 0; i < classNames.length; i++)
 179             typeIDs[i] = debuggee.getReferenceTypeID(classNames[i]);
 180 
 181         testCommand(typeIDs, typeIDs.length, new int[] { expectedCount }, false, 0);
 182 
 183         classNames = new String[] { createTypeSignature(testClass1), createTypeSignature(testClass2) };
 184 
 185         typeIDs = new long[classNames.length];
 186 
 187         // create valid command for 2 classes
 188         for (int i = 0; i < classNames.length; i++)
 189             typeIDs[i] = debuggee.getReferenceTypeID(classNames[i]);
 190 
 191         testCommand(typeIDs, typeIDs.length, new int[] { expectedCount, expectedCount }, false, 0);
 192 
 193         // create command with refTypesCount < 0, expect ILLEGAL_ARGUMENT error
 194         testCommand(typeIDs, -1, new int[] { expectedCount }, true, JDWP.Error.ILLEGAL_ARGUMENT);
 195         resetStatusIfGC();
 196     }
 197 }