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 * @requires vm.gc != "Z" 65 * @library /vmTestbase /test/hotspot/jtreg/vmTestbase 66 * /test/lib 67 * @run driver jdk.test.lib.FileInstaller . . 68 * @build nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.instanceCounts001 69 * nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.instanceCounts001a 70 * @run main/othervm/native PropertyResolvingWrapper 71 * nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.instanceCounts001 72 * -arch=${os.family}-${os.simpleArch} 73 * -verbose 74 * -waittime=5 75 * -debugee.vmkind=java 76 * -transport.address=dynamic 77 * -debugee.vmkeys="-Xmx128M ${test.vm.opts} ${test.java.opts}" 78 */ 79 80 package nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001; 81 82 import java.io.*; 83 import nsk.share.Consts; 84 import nsk.share.jdwp.*; 85 import nsk.share.jpda.AbstractDebuggeeTest; 86 87 public class instanceCounts001 extends TestDebuggerType1 { 88 protected String getDebugeeClassName() { 89 return "nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.instanceCounts001a"; 90 } 91 92 public static void main(String argv[]) { 93 System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE); 94 } 95 96 public static int run(String argv[], PrintStream out) { 97 return new instanceCounts001().runIt(argv, out); 98 } 99 100 private void testCommand(long typeIDs[], int refTypesCount, int[] expectedInstanceCounts, boolean expectError, int errorCode) { 101 try { 102 int JDWP_COMMAND_ID = JDWP.Command.VirtualMachine.InstanceCounts; 103 104 log.display("Create command: " + JDWP.commandNames.get(JDWP_COMMAND_ID)); 105 log.display("refTypesCount = " + refTypesCount); 106 for (long typeID : typeIDs) 107 log.display("refType = " + typeID); 108 109 CommandPacket command = new CommandPacket(JDWP_COMMAND_ID); 110 command.addInt(refTypesCount); 111 112 for (int i = 0; i < refTypesCount; i++) 113 command.addReferenceTypeID(typeIDs[i]); 114 115 command.setLength(); 116 117 log.display("Sending command packet:\n" + command); 118 transport.write(command); 119 120 ReplyPacket reply; 121 122 reply = getReply(command, expectError, errorCode); 123 124 if (expectError) 125 return; 126 127 int counts = reply.getInt(); 128 log.display("counts = " + counts); 129 130 // check received 'counts' value 131 if (counts != refTypesCount) { 132 setSuccess(false); 133 log.complain("Invalid 'counts' value, expected is: " + refTypesCount); 134 } 135 136 // check received 'instanceCount' value for all classes 137 for (int i = 0; i < counts; i++) { 138 long instanceCount = reply.getLong(); 139 log.display("instanceCount = " + instanceCount); 140 141 if (instanceCount != expectedInstanceCounts[i]) { 142 setSuccess(false); 143 log.complain("Unexpected value 'instanceCount': " + instanceCount + ", expected is " + expectedInstanceCounts[i]); 144 } 145 } 146 147 if (!reply.isParsed()) { 148 setSuccess(false); 149 log.complain("Extra trailing bytes found in reply packet at: " + reply.currentPosition()); 150 } 151 } catch (Exception e) { 152 setSuccess(false); 153 log.complain("Caught exception while testing JDWP command: " + e); 154 e.printStackTrace(log.getOutStream()); 155 } 156 } 157 158 public void doTest() { 159 String testClass1 = nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.TestClass1.class.getName(); 160 String testClass2 = nsk.jdwp.VirtualMachine.InstanceCounts.instanceCounts001.TestClass2.class.getName(); 161 162 forceGC(); 163 pipe.println(instanceCounts001a.COMMAND_CREATE_TEST_INSTANCES); 164 165 if (!isDebuggeeReady()) 166 return; 167 168 // Note! This test is broken, in the sense that it incorrectly assumes 169 // that no GC can happen before it walks the heap. In practice, it seems 170 // to only affect this test when using ZGC. However, this test will also 171 // fail when using other GCs if an explicit GC is done here. 172 173 int expectedCount = instanceCounts001a.expectedCount; 174 175 String classNames[]; 176 177 classNames = new String[] { createTypeSignature(testClass1) }; 178 179 long typeIDs[]; 180 181 typeIDs = new long[classNames.length]; 182 183 // create valid command for 1 class 184 for (int i = 0; i < classNames.length; i++) 185 typeIDs[i] = debuggee.getReferenceTypeID(classNames[i]); 186 187 testCommand(typeIDs, typeIDs.length, new int[] { expectedCount }, false, 0); 188 189 classNames = new String[] { createTypeSignature(testClass1), createTypeSignature(testClass2) }; 190 191 typeIDs = new long[classNames.length]; 192 193 // create valid command for 2 classes 194 for (int i = 0; i < classNames.length; i++) 195 typeIDs[i] = debuggee.getReferenceTypeID(classNames[i]); 196 197 testCommand(typeIDs, typeIDs.length, new int[] { expectedCount, expectedCount }, false, 0); 198 199 // create command with refTypesCount < 0, expect ILLEGAL_ARGUMENT error 200 testCommand(typeIDs, -1, new int[] { expectedCount }, true, JDWP.Error.ILLEGAL_ARGUMENT); 201 resetStatusIfGC(); 202 } 203 }