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/jdi/ReferenceType/instances/instances002.
  28  * VM Testbase keywords: [quick, jpda, jdi, feature_jdk6_jpda, vm6]
  29  * VM Testbase readme:
  30  * DESCRIPTION
  31  *      The test scenario is following:
  32  *       - Debugger VM
  33  *         - initiate creation a number of instances in debuggee VM using ClassType.newInstance
  34  *         - check the number of instances is correct
  35  *         - initiate creation a number of instances in debuggee VM using ArrayType.newInstance
  36  *         - check the number of instances is correct
  37  *
  38  * @library /vmTestbase
  39  *          /test/lib
  40  * @run driver jdk.test.lib.FileInstaller . .
  41  * @build nsk.jdi.ReferenceType.instances.instances002.instances002
  42  *        nsk.jdi.ReferenceType.instances.instances002.instances002a
  43  *        nsk.share.jdi.TestClass1
  44  *        nsk.share.jdi.TestInterfaceImplementer1
  45  * @run main/othervm/native PropertyResolvingWrapper
  46  *      nsk.jdi.ReferenceType.instances.instances002.instances002
  47  *      -verbose
  48  *      -arch=${os.family}-${os.simpleArch}
  49  *      -waittime=5
  50  *      -debugee.vmkind=java
  51  *      -transport.address=dynamic
  52  *      "-debugee.vmkeys=${test.vm.opts} ${test.java.opts}"
  53  */
  54 
  55 package nsk.jdi.ReferenceType.instances.instances002;
  56 
  57 import java.io.*;
  58 import java.util.*;
  59 import com.sun.jdi.*;
  60 import com.sun.jdi.event.*;
  61 import nsk.share.Consts;
  62 import nsk.share.ObjectInstancesManager;
  63 import nsk.share.jdi.HeapwalkingDebuggee;
  64 import nsk.share.jdi.HeapwalkingDebugger;
  65 import nsk.share.jpda.AbstractDebuggeeTest;
  66 
  67 public class instances002 extends HeapwalkingDebugger {
  68 
  69     public static void main(String argv[]) {
  70         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
  71     }
  72 
  73     public static int run(String argv[], PrintStream out) {
  74         return new instances002().runIt(argv, out);
  75     }
  76 
  77     protected boolean canRunTest() {
  78         return super.canRunTest() || (!vm.canBeModified());
  79     }
  80 
  81     protected String debuggeeClassName() {
  82         return nsk.jdi.ReferenceType.instances.instances002.instances002a.class.getName();
  83     }
  84 
  85     // create instance of ReferenceType
  86     public ReferenceType prepareReferenceType(String className, int instanceCount) {
  87         String referrerType = ObjectInstancesManager.STRONG_REFERENCE;
  88 
  89         pipe.println(HeapwalkingDebuggee.COMMAND_CREATE_INSTANCES + ":" + className + ":" + instanceCount + ":" + "1" + ":" + referrerType);
  90 
  91         if (!isDebuggeeReady())
  92             return null;
  93 
  94         return debuggee.classByName(className);
  95     }
  96 
  97     // test method ArrayType.newInstance
  98     public void testArrayType(String className) {
  99         // create some instances in target VM, just to get ReferenceType object
 100         int baseInstances = 10;
 101 
 102         ReferenceType referenceType = prepareReferenceType(className, baseInstances);
 103 
 104         if (referenceType == null)
 105             return;
 106 
 107         if (!(referenceType instanceof ArrayType)) {
 108             setSuccess(false);
 109             log.complain("Unexpected reference type: " + referenceType.getClass().getName() + ", expected is ArrayType");
 110             return;
 111         }
 112         // There are potentially other non-test Java threads allocating objects and triggering GC's.
 113         debuggee.suspend();
 114 
 115         List<ObjectReference> baseReferences = new LinkedList<>();
 116         // We need to call disableCollection() on each object returned by referenceType.instances()
 117         // to deal with the case when GC was triggered before the suspend. Otherwise, these objects can
 118         // be potentially collected.
 119         for (ObjectReference objRef : referenceType.instances(0)) {
 120             try {
 121                 objRef.disableCollection();
 122                 baseReferences.add(objRef);
 123             } catch (ObjectCollectedException e) {
 124                 // skip this reference
 125             }
 126         }
 127         baseInstances = baseReferences.size();
 128 
 129         int createInstanceCount = 100;
 130         int arraySize = 1;
 131 
 132         ArrayType arrayType = (ArrayType) referenceType;
 133         List<ArrayReference> objectReferences = new ArrayList<ArrayReference>();
 134 
 135         for (int i = 0; i < createInstanceCount; i++) {
 136             // instances created in this way aren't reachable for the purposes of garbage collection,
 137             // to make it reachable call disableCollection() for this objects
 138             ArrayReference arrayReference = arrayType.newInstance(arraySize);
 139             arrayReference.disableCollection();
 140 
 141             objectReferences.add(arrayReference);
 142         }
 143 
 144         checkDebugeeAnswer_instances(className, createInstanceCount + baseInstances);
 145 
 146         for (ArrayReference arrayReference : objectReferences) {
 147             arrayReference.enableCollection();
 148         }
 149 
 150         for (ObjectReference baseRef : baseReferences) {
 151             baseRef.enableCollection();
 152         }
 153 
 154         debuggee.resume();
 155     }
 156 
 157     // test method ClassType.newInstance
 158     public void testClassType(String className) {
 159         // create some instances in target VM, just to get ReferenceType object
 160         int baseInstances = 10;
 161 
 162         ReferenceType referenceType = prepareReferenceType(className, baseInstances);
 163 
 164         if (referenceType == null)
 165             return;
 166 
 167         if (!(referenceType instanceof ClassType)) {
 168             setSuccess(false);
 169             log.display("Unexpected reference type: " + referenceType.getClass().getName() + ", expected is ClassType");
 170             return;
 171         }
 172 
 173         baseInstances = referenceType.instances(0).size();
 174 
 175         ClassType classType = (ClassType) referenceType;
 176 
 177         pipe.println(AbstractDebuggeeTest.COMMAND_FORCE_BREAKPOINT);
 178 
 179         BreakpointEvent breakpointEvent = waitForBreakpoint(defaultBreakpointRequest);
 180 
 181         List<Method> methods = referenceType.allMethods();
 182         List<ObjectReference> objectReferences = new ArrayList<ObjectReference>();
 183 
 184         int createInstanceCount = 100;
 185 
 186         try {
 187             for (Method method : methods) {
 188                 if (method.isConstructor()) {
 189                     for (int i = 0; i < createInstanceCount; i++) {
 190                         objectReferences.add(classType.newInstance(breakpointEvent.thread(), method, new ArrayList<Value>(), 0));
 191                     }
 192 
 193                     checkDebugeeAnswer_instances(className, baseInstances);
 194 
 195                     break;
 196                 }
 197             }
 198         } catch (Exception e) {
 199             setSuccess(false);
 200             log.display("Unexpected exception: ");
 201             e.printStackTrace(log.getOutStream());
 202         }
 203 
 204         debuggee.resume();
 205 
 206         // wait response for command 'COMMAND_FORCE_BREAKPOINT'
 207         if (!isDebuggeeReady())
 208             return;
 209 
 210         pipe.println(HeapwalkingDebuggee.COMMAND_DELETE_INSTANCES + ":" + className + ":" + baseInstances);
 211 
 212         if (!isDebuggeeReady())
 213             return;
 214 
 215         checkDebugeeAnswer_instances(className, 0);
 216     }
 217 
 218     protected void doTest() {
 219         initDefaultBreakpoint();
 220 
 221         String[] testClasses = { "nsk.share.jdi.TestClass1", "nsk.share.jdi.TestInterfaceImplementer1" };
 222 
 223         for (String className : testClasses)
 224             testClassType(className);
 225 
 226         for (String className : ObjectInstancesManager.primitiveArrayClassNames)
 227             testArrayType(className);
 228     }
 229 
 230 }