annotate agent/src/share/classes/sun/jvm/hotspot/tools/soql/SOQL.java @ 17524:89152779163c

Merge with jdk8-b132
author Gilles Duboscq <duboscq@ssw.jku.at>
date Wed, 15 Oct 2014 11:59:32 +0200
parents 4ca6dc0799b6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
17524
89152779163c Merge with jdk8-b132
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 14909
diff changeset
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.tools.soql;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.io.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import java.util.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import sun.jvm.hotspot.debugger.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import sun.jvm.hotspot.oops.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import sun.jvm.hotspot.runtime.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import sun.jvm.hotspot.tools.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
33 import sun.jvm.hotspot.utilities.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
34 import sun.jvm.hotspot.utilities.soql.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
37 This is command line SOQL (Simple Object Query Language) interpreter.
a61af66fc99e Initial load
duke
parents:
diff changeset
38 */
a61af66fc99e Initial load
duke
parents:
diff changeset
39
a61af66fc99e Initial load
duke
parents:
diff changeset
40 public class SOQL extends Tool {
a61af66fc99e Initial load
duke
parents:
diff changeset
41 public static void main(String[] args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
42 SOQL soql = new SOQL();
12932
7fe6ef09d242 8025638: jmap returns 0 instead of 1 when it fails.
farvidsson
parents: 11054
diff changeset
43 soql.execute(args);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
44 }
a61af66fc99e Initial load
duke
parents:
diff changeset
45
11054
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 6725
diff changeset
46 public SOQL() {
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 6725
diff changeset
47 super();
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 6725
diff changeset
48 }
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 6725
diff changeset
49
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 6725
diff changeset
50 public SOQL(JVMDebugger d) {
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 6725
diff changeset
51 super(d);
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 6725
diff changeset
52 }
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 6725
diff changeset
53
0
a61af66fc99e Initial load
duke
parents:
diff changeset
54 protected SOQLEngine soqlEngine;
a61af66fc99e Initial load
duke
parents:
diff changeset
55 protected BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
a61af66fc99e Initial load
duke
parents:
diff changeset
56 protected PrintStream out = System.out;
a61af66fc99e Initial load
duke
parents:
diff changeset
57 static protected String prompt = "soql> ";
a61af66fc99e Initial load
duke
parents:
diff changeset
58 static protected String secondPrompt = "> ";
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
61 soqlEngine = SOQLEngine.getEngine();
a61af66fc99e Initial load
duke
parents:
diff changeset
62 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
63 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
64 out.print(prompt);
a61af66fc99e Initial load
duke
parents:
diff changeset
65 String line = in.readLine();
a61af66fc99e Initial load
duke
parents:
diff changeset
66 if (line == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
67 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
68 }
a61af66fc99e Initial load
duke
parents:
diff changeset
69 StringTokenizer st = new StringTokenizer(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
70 if (st.hasMoreTokens()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
71 String cmd = st.nextToken();
a61af66fc99e Initial load
duke
parents:
diff changeset
72 if (cmd.equals("select")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
73 handleSelect(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
74 } else if (cmd.equals("classes")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
75 handleClasses(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
76 } else if (cmd.equals("class")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
77 handleClass(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
78 } else if (cmd.equals("object")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
79 handleObject(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
80 } else if (cmd.equals("quit")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
81 out.println("Bye!");
a61af66fc99e Initial load
duke
parents:
diff changeset
82 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
83 } else if (cmd.equals("")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
84 // do nothing ...
a61af66fc99e Initial load
duke
parents:
diff changeset
85 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
86 handleUnknown(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
87 }
a61af66fc99e Initial load
duke
parents:
diff changeset
88 }
a61af66fc99e Initial load
duke
parents:
diff changeset
89 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
90 e.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
91 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
92 }
a61af66fc99e Initial load
duke
parents:
diff changeset
93 }
a61af66fc99e Initial load
duke
parents:
diff changeset
94 }
a61af66fc99e Initial load
duke
parents:
diff changeset
95
a61af66fc99e Initial load
duke
parents:
diff changeset
96 protected void handleSelect(String query) {
a61af66fc99e Initial load
duke
parents:
diff changeset
97 StringBuffer buf = new StringBuffer(query);
a61af66fc99e Initial load
duke
parents:
diff changeset
98 String tmp = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
99 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
100 out.print(secondPrompt);
a61af66fc99e Initial load
duke
parents:
diff changeset
101 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
102 tmp = in.readLine();
a61af66fc99e Initial load
duke
parents:
diff changeset
103 } catch (IOException ioe) {
a61af66fc99e Initial load
duke
parents:
diff changeset
104 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
105 }
a61af66fc99e Initial load
duke
parents:
diff changeset
106 if (tmp.equals("") || tmp.equals("go"))
a61af66fc99e Initial load
duke
parents:
diff changeset
107 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
108 buf.append('\n');
a61af66fc99e Initial load
duke
parents:
diff changeset
109 buf.append(tmp);
a61af66fc99e Initial load
duke
parents:
diff changeset
110 }
a61af66fc99e Initial load
duke
parents:
diff changeset
111 query = buf.toString();
a61af66fc99e Initial load
duke
parents:
diff changeset
112
a61af66fc99e Initial load
duke
parents:
diff changeset
113 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 soqlEngine.executeQuery(query,
a61af66fc99e Initial load
duke
parents:
diff changeset
115 new ObjectVisitor() {
a61af66fc99e Initial load
duke
parents:
diff changeset
116 public void visit(Object o) {
a61af66fc99e Initial load
duke
parents:
diff changeset
117 if (o != null && o instanceof JSJavaObject) {
a61af66fc99e Initial load
duke
parents:
diff changeset
118 String oopAddr = ((JSJavaObject)o).getOop().getHandle().toString();
a61af66fc99e Initial load
duke
parents:
diff changeset
119 out.println(oopAddr);
a61af66fc99e Initial load
duke
parents:
diff changeset
120 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
121 out.println((o == null)? "null" : o.toString());
a61af66fc99e Initial load
duke
parents:
diff changeset
122 }
a61af66fc99e Initial load
duke
parents:
diff changeset
123 }
a61af66fc99e Initial load
duke
parents:
diff changeset
124 });
a61af66fc99e Initial load
duke
parents:
diff changeset
125 } catch (SOQLException se) {
a61af66fc99e Initial load
duke
parents:
diff changeset
126 se.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
127 }
a61af66fc99e Initial load
duke
parents:
diff changeset
128 }
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 protected void handleClasses(String line) {
a61af66fc99e Initial load
duke
parents:
diff changeset
131 // just list all InstanceKlasses
a61af66fc99e Initial load
duke
parents:
diff changeset
132 InstanceKlass[] klasses = SystemDictionaryHelper.getAllInstanceKlasses();
a61af66fc99e Initial load
duke
parents:
diff changeset
133 for (int i = 0; i < klasses.length; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
134 out.print(klasses[i].getName().asString().replace('/', '.'));
a61af66fc99e Initial load
duke
parents:
diff changeset
135 out.print(" @");
6725
da91efe96a93 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 3938
diff changeset
136 out.println(klasses[i].getAddress());
0
a61af66fc99e Initial load
duke
parents:
diff changeset
137 }
a61af66fc99e Initial load
duke
parents:
diff changeset
138 }
a61af66fc99e Initial load
duke
parents:
diff changeset
139
a61af66fc99e Initial load
duke
parents:
diff changeset
140 protected void handleClass(String line) {
a61af66fc99e Initial load
duke
parents:
diff changeset
141 StringTokenizer st = new StringTokenizer(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
142 st.nextToken(); // ignore "class"
a61af66fc99e Initial load
duke
parents:
diff changeset
143 if (st.hasMoreTokens()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
144 String className = st.nextToken();
a61af66fc99e Initial load
duke
parents:
diff changeset
145 InstanceKlass klass = SystemDictionaryHelper.findInstanceKlass(className);
a61af66fc99e Initial load
duke
parents:
diff changeset
146 if (klass == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 out.println("class " + className + " not found");
a61af66fc99e Initial load
duke
parents:
diff changeset
148 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
149 // klass.iterate(new OopPrinter(out), true);
a61af66fc99e Initial load
duke
parents:
diff changeset
150
a61af66fc99e Initial load
duke
parents:
diff changeset
151 // base class
a61af66fc99e Initial load
duke
parents:
diff changeset
152 InstanceKlass base = (InstanceKlass) klass.getSuper();
a61af66fc99e Initial load
duke
parents:
diff changeset
153 if (base != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
154 out.println("super");
a61af66fc99e Initial load
duke
parents:
diff changeset
155 out.print("\t");
a61af66fc99e Initial load
duke
parents:
diff changeset
156 out.println(base.getName().asString().replace('/', '.'));
a61af66fc99e Initial load
duke
parents:
diff changeset
157 }
a61af66fc99e Initial load
duke
parents:
diff changeset
158
a61af66fc99e Initial load
duke
parents:
diff changeset
159 // list immediate fields only
6725
da91efe96a93 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 3938
diff changeset
160 U2Array fields = klass.getFields();
da91efe96a93 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 3938
diff changeset
161 int numFields = (int) fields.length();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
162 ConstantPool cp = klass.getConstants();
a61af66fc99e Initial load
duke
parents:
diff changeset
163 out.println("fields");
a61af66fc99e Initial load
duke
parents:
diff changeset
164 if (numFields != 0) {
3938
e6b1331a51d2 7086585: make Java field injection more flexible
never
parents: 1552
diff changeset
165 for (int f = 0; f < numFields; f++){
e6b1331a51d2 7086585: make Java field injection more flexible
never
parents: 1552
diff changeset
166 Symbol f_name = klass.getFieldName(f);
e6b1331a51d2 7086585: make Java field injection more flexible
never
parents: 1552
diff changeset
167 Symbol f_sig = klass.getFieldSignature(f);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
168 StringBuffer sigBuf = new StringBuffer();
a61af66fc99e Initial load
duke
parents:
diff changeset
169 new SignatureConverter(f_sig, sigBuf).dispatchField();
a61af66fc99e Initial load
duke
parents:
diff changeset
170 out.print('\t');
a61af66fc99e Initial load
duke
parents:
diff changeset
171 out.print(sigBuf.toString().replace('/', '.'));
a61af66fc99e Initial load
duke
parents:
diff changeset
172 out.print(' ');
a61af66fc99e Initial load
duke
parents:
diff changeset
173 out.println(f_name.asString());
a61af66fc99e Initial load
duke
parents:
diff changeset
174 }
a61af66fc99e Initial load
duke
parents:
diff changeset
175 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
176 out.println("\tno fields in this class");
a61af66fc99e Initial load
duke
parents:
diff changeset
177 }
a61af66fc99e Initial load
duke
parents:
diff changeset
178 }
a61af66fc99e Initial load
duke
parents:
diff changeset
179 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
180 out.println("usage: class <name of the class>");
a61af66fc99e Initial load
duke
parents:
diff changeset
181 }
a61af66fc99e Initial load
duke
parents:
diff changeset
182 }
a61af66fc99e Initial load
duke
parents:
diff changeset
183
a61af66fc99e Initial load
duke
parents:
diff changeset
184 protected Oop getOopAtAddress(Address addr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
185 OopHandle oopHandle = addr.addOffsetToAsOopHandle(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
186 return VM.getVM().getObjectHeap().newOop(oopHandle);
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188
a61af66fc99e Initial load
duke
parents:
diff changeset
189 protected void handleObject(String line) {
a61af66fc99e Initial load
duke
parents:
diff changeset
190 StringTokenizer st = new StringTokenizer(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
191 st.nextToken(); // ignore "object"
a61af66fc99e Initial load
duke
parents:
diff changeset
192 if (st.hasMoreTokens()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
193 String addrStr = st.nextToken();
a61af66fc99e Initial load
duke
parents:
diff changeset
194 Address addr = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
195 Debugger dbg = VM.getVM().getDebugger();
a61af66fc99e Initial load
duke
parents:
diff changeset
196 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
197 addr = dbg.parseAddress(addrStr);
a61af66fc99e Initial load
duke
parents:
diff changeset
198 } catch (Exception e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
199 out.println("invalid address : " + e.getMessage());
a61af66fc99e Initial load
duke
parents:
diff changeset
200 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
201 }
a61af66fc99e Initial load
duke
parents:
diff changeset
202
a61af66fc99e Initial load
duke
parents:
diff changeset
203 Oop oop = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
204 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 oop = getOopAtAddress(addr);
a61af66fc99e Initial load
duke
parents:
diff changeset
206 } catch (Exception e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
207 out.println("invalid object : " + e.getMessage());
a61af66fc99e Initial load
duke
parents:
diff changeset
208 }
a61af66fc99e Initial load
duke
parents:
diff changeset
209
a61af66fc99e Initial load
duke
parents:
diff changeset
210 if (oop != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
211 oop.iterate(new OopPrinter(out), true);
a61af66fc99e Initial load
duke
parents:
diff changeset
212 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
213 out.println("null object!");
a61af66fc99e Initial load
duke
parents:
diff changeset
214 }
a61af66fc99e Initial load
duke
parents:
diff changeset
215 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
216 out.println("usage: object <address>");
a61af66fc99e Initial load
duke
parents:
diff changeset
217 }
a61af66fc99e Initial load
duke
parents:
diff changeset
218 }
a61af66fc99e Initial load
duke
parents:
diff changeset
219
a61af66fc99e Initial load
duke
parents:
diff changeset
220 protected void handleUnknown(String line) {
a61af66fc99e Initial load
duke
parents:
diff changeset
221 out.println("Unknown command!");
a61af66fc99e Initial load
duke
parents:
diff changeset
222 }
a61af66fc99e Initial load
duke
parents:
diff changeset
223 }