1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/agent/test/jdi/sagdoit.java Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,329 @@ 1.4 +/* 1.5 + * Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +import com.sun.jdi.*; 1.29 +import java.util.*; 1.30 + 1.31 + 1.32 +// This just contains a bunch of methods that call various JDI methods. 1.33 +// It is called from the sagtest.java jtreg test to get this info for the standard 1.34 +// JDI and from the sagclient.java test to get this info for the SA JDI. 1.35 + 1.36 +class comparator implements Comparator { 1.37 + 1.38 + public int compare(Object o1, Object o2) { 1.39 + ReferenceType rt1 = (ReferenceType)o1; 1.40 + ReferenceType rt2 = (ReferenceType)o2; 1.41 + return rt1.signature().compareTo(rt2.signature()); 1.42 + } 1.43 + 1.44 + public boolean equals(Object oo) { 1.45 + return false; 1.46 + } 1.47 +} 1.48 + 1.49 +public class sagdoit { 1.50 + 1.51 + VirtualMachine myVm; 1.52 + int margin = 0; 1.53 + static String blanks = " "; 1.54 + static int nblanks = blanks.length(); 1.55 + 1.56 + sagdoit(VirtualMachine vm) { 1.57 + super(); 1.58 + myVm = vm; 1.59 + } 1.60 + 1.61 + void indent(int count) { 1.62 + margin += count; 1.63 + } 1.64 + 1.65 + void pp(String msg) { 1.66 + System.out.println(blanks.substring(nblanks - margin) + msg); 1.67 + } 1.68 + 1.69 + public void doAll() { 1.70 + doThreadGroups(); 1.71 + //System.out.println("NOTE: dumping of class info is disabled in sagdoit.java"); 1.72 + //System.out.println(" just to keep the output small while working on objects"); 1.73 + doClasses(); //fixme jj: uncomment this to see all class info 1.74 + 1.75 + } 1.76 + public void doThreadGroups() { 1.77 + doThreadGroupList(myVm.topLevelThreadGroups()); 1.78 + } 1.79 + 1.80 + private void doThreadGroupList(List groups) { 1.81 + // sort; need a comparator 1.82 + if (groups == null) { 1.83 + return; 1.84 + } 1.85 + 1.86 + Iterator myIter = groups.iterator(); 1.87 + while(myIter.hasNext()) { 1.88 + ThreadGroupReference aGroup = (ThreadGroupReference)myIter.next(); 1.89 + doOneThreadGroup(aGroup); 1.90 + } 1.91 + 1.92 + } 1.93 + 1.94 + public void doOneThreadGroup(ThreadGroupReference xx) { 1.95 + pp("threadGroup:" + xx.name()); 1.96 + indent(4); 1.97 + pp("parent() = " + xx.parent()); 1.98 + pp("threads:"); 1.99 + indent(4); 1.100 + doThreadList(xx.threads()); 1.101 + indent(-4); 1.102 + pp("threadGroups:"); 1.103 + indent(4); 1.104 + doThreadGroupList(xx.threadGroups()); 1.105 + indent(-4); 1.106 + indent(-4); 1.107 + } 1.108 + 1.109 + public void doThreads() { 1.110 + doThreadList(myVm.allThreads()); 1.111 + } 1.112 + 1.113 + public void doThreadList(List threads) { 1.114 + if (threads == null) { 1.115 + return; 1.116 + } 1.117 + Iterator myIter = threads.iterator(); 1.118 + while(myIter.hasNext()) { 1.119 + ThreadReference aThread = (ThreadReference)myIter.next(); 1.120 + doOneThread(aThread); 1.121 + } 1.122 + } 1.123 + 1.124 + public void doOneThread(ThreadReference xx) { 1.125 + pp("Thread: " + xx.name()); 1.126 + indent(4); 1.127 + pp("suspendCount() = " + xx.suspendCount()); 1.128 + 1.129 + //void stop(ObjectReference throwable) throws InvalidTypeException; 1.130 + //void interrupt(); 1.131 + pp("status() = " + xx.status()); 1.132 + pp("isSuspended() = " + xx.isSuspended()); 1.133 + pp("isAtBreakpoint() = " + xx.isAtBreakpoint()); 1.134 + 1.135 + pp("threadGroup() = " + xx.threadGroup()); 1.136 + indent(-4); 1.137 + 1.138 + indent(4); 1.139 + try { 1.140 + List allFrames = xx.frames(); 1.141 + for (int ii = 0; ii < xx.frameCount(); ii++) { 1.142 + StackFrame oneFrame = xx.frame(ii); 1.143 + pp("frame(" + ii + ") = " + oneFrame); 1.144 + doOneFrame(oneFrame); 1.145 + } 1.146 + //List frames(int start, int length) throws IncompatibleThreadStateException; 1.147 + // unsupported List allMonitors = xx.ownedMonitors(); 1.148 + // unsupported pp("currentContendedMonitor() = " + xx.currentContendedMonitor()); 1.149 + } catch (IncompatibleThreadStateException ee) { 1.150 + pp("GOT IncompatibleThreadStateException: " + ee); 1.151 + } 1.152 + indent(-4); 1.153 + } 1.154 + 1.155 + public void doOneFrame(StackFrame frame) { 1.156 + 1.157 + List localVars = null; 1.158 + try { 1.159 + localVars = frame.visibleVariables(); 1.160 + } catch (AbsentInformationException ee) { 1.161 + // we compile with -g so this shouldn't happen 1.162 + return; 1.163 + } 1.164 + indent(4); 1.165 + for (Iterator it = localVars.iterator(); it.hasNext();) { 1.166 + LocalVariable lv = (LocalVariable) it.next(); 1.167 + pp("lv name = " + lv.name() + 1.168 + ", type = " + lv.typeName() + 1.169 + ", sig = " + lv.signature() + 1.170 + ", gsig = " + lv.genericSignature() + 1.171 + ", isVis = " + lv.isVisible(frame) + 1.172 + ", isArg = " + lv.isArgument()); 1.173 + } 1.174 + indent(-4); 1.175 + } 1.176 + 1.177 + public void doClasses() { 1.178 + List myClasses = myVm.allClasses(); 1.179 + myClasses = new ArrayList(myClasses); 1.180 + Collections.sort(myClasses, new comparator()); 1.181 + for (int ii = 0; ii < myClasses.size(); ii++) { 1.182 + // Spec says each is a ReferenceType 1.183 + //System.out.println("class " + (ii + 1) + " is " + myClasses.get(ii)); 1.184 + ReferenceType aClass = (ReferenceType)myClasses.get(ii); 1.185 + System.out.println("class " + (ii + 1) + " is " + aClass.signature()); 1.186 + doOneClass(aClass); 1.187 + // Uncomment this to just do a few classes. 1.188 + //if ( ii > 4) break; 1.189 + } 1.190 + } 1.191 + 1.192 + public void doOneClass(ReferenceType xx) { 1.193 + indent(5); 1.194 + // inherited from Mirror 1.195 + pp("toString() = " + xx.toString()); 1.196 + pp("virtualMachine() = " + xx.virtualMachine()); 1.197 + 1.198 + // inherited from Type 1.199 + pp("name() = " + xx.name()); 1.200 + pp("signature() = " + xx.signature()); 1.201 + 1.202 + // ReferenceType fields 1.203 + doReferenceTypeFields(xx); 1.204 + 1.205 + 1.206 + 1.207 + 1.208 + 1.209 + String className = xx.getClass().getName(); 1.210 + pp("subclass = " + className); 1.211 + 1.212 + Class referenceType = null; 1.213 + Class arrayType = null; 1.214 + Class classType = null; 1.215 + Class interfaceType = null; 1.216 + 1.217 + try { 1.218 + referenceType = Class.forName("com.sun.jdi.ReferenceType"); 1.219 + arrayType = Class.forName("com.sun.jdi.ArrayType"); 1.220 + interfaceType = Class.forName("com.sun.jdi.InterfaceType"); 1.221 + classType = Class.forName("com.sun.jdi.ClassType"); 1.222 + } catch (ClassNotFoundException ee) { 1.223 + } 1.224 + 1.225 + 1.226 + if (referenceType.isInstance(xx)) { 1.227 + pp("ReferenceType fields"); 1.228 + ReferenceType rr = (ReferenceType)xx; 1.229 + 1.230 + if (arrayType.isInstance(xx)) { 1.231 + pp("ArrayType fields"); 1.232 + } 1.233 + 1.234 + if (classType.isInstance(xx)) { 1.235 + pp("ClassType fields"); 1.236 + } 1.237 + 1.238 + if (interfaceType.isInstance(xx)) { 1.239 + pp("InterfaceType fields"); 1.240 + } 1.241 + } 1.242 + indent(-5); 1.243 + 1.244 + } 1.245 + 1.246 + 1.247 + public void doReferenceTypeFields(ReferenceType xx) { 1.248 + Object zz; 1.249 + pp("classLoader() = " + xx.classLoader()); 1.250 + try {zz =xx.sourceName();} catch(AbsentInformationException ee) { zz = ee;} pp("sourceName() = " + zz); 1.251 + try {zz =xx.sourceNames("stratum");} catch(AbsentInformationException ee) { zz = ee;} pp("sourceNames() = " + zz); 1.252 + try {zz =xx.sourcePaths("stratum");} catch(AbsentInformationException ee) { zz = ee;} pp("sourcePaths() = " + zz); 1.253 + //try {zz =xx.sourceDebugExtension();} catch(AbsentInformationException ee) { zz = ee;} pp("sourceDebugExtension() = " + zz); 1.254 + //fixme: jj; should sourceDebugExtension throw UnsupportedOperationException? 1.255 + try {zz =xx.sourceDebugExtension();} catch(Exception ee) { zz = ee;} pp("sourceDebugExtension() = " + zz); 1.256 + // If xx is an array, this can cause a ClassNotLoadedException on the 1.257 + // component type. Is that a JDI bug? 1.258 + pp("isStatic() = " + xx.isStatic()); 1.259 + pp("isAbstract() = " + xx.isAbstract()); 1.260 + pp("isFinal() = " + xx.isFinal()); 1.261 + pp("isPrepared() = " + xx.isPrepared()); 1.262 + pp("isVerified() = " + xx.isVerified()); 1.263 + pp("isInitialized() = " + xx.isInitialized()); 1.264 + pp("failedToInitialize() = " + xx.failedToInitialize()); 1.265 + pp("fields() = " + xx.fields()); 1.266 + pp("visibleFields() = " + xx.visibleFields()); 1.267 + pp("allFields() = " + xx.allFields()); 1.268 + pp("fieldByName(String fieldName) = " + xx.fieldByName("fieldName")); 1.269 + pp("methods() = " + xx.methods()); 1.270 + 1.271 + 1.272 + List meths = xx.methods(); 1.273 + Iterator iter = meths.iterator(); 1.274 + while (iter.hasNext()) { 1.275 + Method mm = (Method)iter.next(); 1.276 + pp(" name/sig:" + mm.name() + "/" + mm.signature()); 1.277 + } 1.278 + 1.279 + pp(" visibleMethods() = " + xx.visibleMethods()); 1.280 + 1.281 + //if (1 == 1) return; 1.282 + 1.283 + pp("allMethods() = " + xx.allMethods()); 1.284 + 1.285 + 1.286 + pp("methodsByName(String name) = " + xx.methodsByName("name")); 1.287 + pp("methodsByName(String name, String signature) = " + xx.methodsByName("name", "signature")); 1.288 + pp("nestedTypes() = " + xx.nestedTypes()); 1.289 + //pp("getValue(Field field) = " + xx.getValue("field")); 1.290 + pp("getValue(Field field) = " + "fixme: jjh"); 1.291 + //pp("getValues(List fields) = " + xx.getValues(new List[] = {"fields"})); 1.292 + pp("getValues(List fields) = " + "fixme: jjh"); 1.293 + pp("classObject() = " + xx.classObject()); 1.294 + //x pp("allLineLocations() = " + xx.allLineLocations()); 1.295 + //x pp("allLineLocations(String stratum, String sourceName) = " + xx.allLineLocations("stratum", "sourceName")); 1.296 + //x pp("locationsOfLine(int lineNumber) = " + xx.locationsOfLine(89)); 1.297 + //x pp("locationsOfLine(String stratum, String sourceName, int lineNumber) = " + xx.locationsOfLine("stratum", "sourceName", 89)); 1.298 + pp("availableStrata() = " + xx.availableStrata()); 1.299 + pp("defaultStratum() = " + xx.defaultStratum()); 1.300 + pp("equals(Object obj) = " + xx.equals(xx)); 1.301 + pp("hashCode() = " + xx.hashCode()); 1.302 + } 1.303 + 1.304 +} 1.305 + 1.306 +// try { 1.307 +// ReferenceType rr = (ReferenceType)xx; 1.308 +// pp("ReferenceType fields"); 1.309 + 1.310 +// try { 1.311 +// ArrayType ff = (ArrayType)xx; 1.312 +// pp("ArrayType fields"); 1.313 + 1.314 +// } catch(ClassCastException ee) { 1.315 +// } 1.316 + 1.317 +// try { 1.318 +// ClassType ff = (ClassType)xx; 1.319 +// pp("ClassType fields"); 1.320 + 1.321 +// } catch(ClassCastException ee) { 1.322 +// } 1.323 + 1.324 +// try { 1.325 +// InterfaceType ff = (InterfaceType)xx; 1.326 +// pp("InterfaceType fields"); 1.327 + 1.328 +// } catch(ClassCastException ee) { 1.329 +// } 1.330 + 1.331 +// } catch(ClassCastException ee) { 1.332 +// }