1 /* 2 * Copyright (c) 2007, 2020, 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 * @requires !vm.graal.enabled 65 * @library /vmTestbase /test/hotspot/jtreg/vmTestbase 66 * /test/lib 67 * @run main/othervm/native 68 * nsk.jdwp.ReferenceType.Instances.instances001.instances001 69 * -arch=${os.family}-${os.simpleArch} 70 * -verbose 71 * -waittime=5 72 * -debugee.vmkind=java 73 * -transport.address=dynamic 74 * -debugee.vmkeys="-Xmx128M ${test.vm.opts} ${test.java.opts}" 75 */ 76 77 package nsk.jdwp.ReferenceType.Instances.instances001; 78 79 import nsk.share.Consts; 80 import nsk.share.jdwp.CommandPacket; 81 import nsk.share.jdwp.JDWP; 82 import nsk.share.jdwp.ReplyPacket; 83 import nsk.share.jdwp.TestDebuggerType1; 84 85 import java.io.PrintStream; 86 87 public class instances001 extends TestDebuggerType1 { getDebugeeClassName()88 protected String getDebugeeClassName() { 89 return nsk.jdwp.ReferenceType.Instances.instances001.instances001a.class.getName(); 90 } 91 main(String[] argv)92 public static void main(String[] argv) { 93 System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE); 94 } 95 run(String[] argv, PrintStream out)96 public static int run(String[] argv, PrintStream out) { 97 return new instances001().runIt(argv, out); 98 } 99 testClass(long typeID, int maxInstances, int expectedInstances, boolean expectError, int errorCode)100 private void testClass(long typeID, int maxInstances, int expectedInstances, boolean expectError, int errorCode) { 101 try { 102 int JDWP_COMMAND_ID = JDWP.Command.ReferenceType.Instances; 103 104 log.display("Create command: " + JDWP.commandNames.get(JDWP_COMMAND_ID)); 105 log.display("referenceType = " + typeID); 106 log.display("maxInstances = " + maxInstances); 107 108 CommandPacket command = new CommandPacket(JDWP_COMMAND_ID); 109 command.addReferenceTypeID(typeID); 110 command.addInt(maxInstances); 111 command.setLength(); 112 113 log.display("Sending command packet:\n" + command); 114 transport.write(command); 115 116 ReplyPacket reply; 117 118 reply = getReply(command, expectError, errorCode); 119 120 if (expectError) 121 return; 122 123 int instances = reply.getInt(); 124 log.display("instances = " + instances); 125 126 // check that correct value of 'instances' was received 127 if (instances != expectedInstances) { 128 setSuccess(false); 129 log.complain("Unexpected 'instances' value: " + instances + ", expected is " + expectedInstances); 130 } 131 132 for (int i = 0; i < instances; i++) { 133 JDWP.Value value = reply.getValue(); 134 log.display("tagged-ObjectID = " + value); 135 } 136 137 if (!reply.isParsed()) { 138 setSuccess(false); 139 log.complain("Extra trailing bytes found in reply packet at: " + reply.currentPosition()); 140 } 141 } catch (Exception e) { 142 setSuccess(false); 143 log.complain("Caught exception while testing JDWP command: " + e); 144 e.printStackTrace(log.getOutStream()); 145 } 146 } 147 doTest()148 public void doTest() { 149 // force GC in debuggee VM to avoid collection of weak references during test execution 150 forceGC(); 151 pipe.println(instances001a.COMMAND_CREATE_TEST_INSTANCES); 152 153 if (!isDebuggeeReady()) 154 return; 155 156 int expectedInstances = instances001a.expectedCount; 157 158 String testClassName = nsk.jdwp.ReferenceType.Instances.instances001.TestClass.class.getName(); 159 160 long typeID = debuggee.getReferenceTypeID(createTypeSignature(testClassName)); 161 162 // Note! This test is broken, in the sense that it incorrectly assumes 163 // that no GC can happen before it walks the heap. In practice, it seems 164 // to only affect this test when using ZGC. However, this test will also 165 // fail when using other GCs if an explicit GC is done here. 166 167 // create command with maxInstances=1, only 1 instance should be returned 168 testClass(typeID, 1, 1, false, 0); 169 // create command with maxInstances=0, all instances should be returned 170 testClass(typeID, 0, expectedInstances, false, 0); 171 // create command with maxInstances=Integer.MAX_VALUE, all instances should be returned 172 testClass(typeID, Integer.MAX_VALUE, expectedInstances, false, 0); 173 174 // create command with maxInstances < 0, expect ILLEGAL_ARGUMENT error 175 testClass(typeID, -1, expectedInstances, true, JDWP.Error.ILLEGAL_ARGUMENT); 176 177 // create command with typeID = 1, expect INVALID_OBJECT error 178 testClass(-1, Integer.MAX_VALUE, expectedInstances, true, JDWP.Error.INVALID_OBJECT); 179 180 // create command with threadID instead of referenceTypeID, expect INVALID_CLASS error 181 testClass(debuggee.getThreadID("main"), Integer.MAX_VALUE, expectedInstances, true, JDWP.Error.INVALID_CLASS); 182 183 // if GC occurs during test the results should be ignored 184 resetStatusIfGC(); 185 } 186 } 187