annotate agent/src/share/classes/sun/jvm/hotspot/tools/soql/SOQL.java @ 1552:c18cbe5936b8

6941466: Oracle rebranding changes for Hotspot repositories Summary: Change all the Sun copyrights to Oracle copyright Reviewed-by: ohair
author trims
date Thu, 27 May 2010 19:08:38 -0700
parents a61af66fc99e
children e6b1331a51d2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
2 * Copyright (c) 2003, 2007, 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();
a61af66fc99e Initial load
duke
parents:
diff changeset
43 soql.start(args);
a61af66fc99e Initial load
duke
parents:
diff changeset
44 soql.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
45 }
a61af66fc99e Initial load
duke
parents:
diff changeset
46
a61af66fc99e Initial load
duke
parents:
diff changeset
47 protected SOQLEngine soqlEngine;
a61af66fc99e Initial load
duke
parents:
diff changeset
48 protected BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
a61af66fc99e Initial load
duke
parents:
diff changeset
49 protected PrintStream out = System.out;
a61af66fc99e Initial load
duke
parents:
diff changeset
50 static protected String prompt = "soql> ";
a61af66fc99e Initial load
duke
parents:
diff changeset
51 static protected String secondPrompt = "> ";
a61af66fc99e Initial load
duke
parents:
diff changeset
52
a61af66fc99e Initial load
duke
parents:
diff changeset
53 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
54 soqlEngine = SOQLEngine.getEngine();
a61af66fc99e Initial load
duke
parents:
diff changeset
55 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
56 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
57 out.print(prompt);
a61af66fc99e Initial load
duke
parents:
diff changeset
58 String line = in.readLine();
a61af66fc99e Initial load
duke
parents:
diff changeset
59 if (line == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
60 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
61 }
a61af66fc99e Initial load
duke
parents:
diff changeset
62 StringTokenizer st = new StringTokenizer(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
63 if (st.hasMoreTokens()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
64 String cmd = st.nextToken();
a61af66fc99e Initial load
duke
parents:
diff changeset
65 if (cmd.equals("select")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
66 handleSelect(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
67 } else if (cmd.equals("classes")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
68 handleClasses(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
69 } else if (cmd.equals("class")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
70 handleClass(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
71 } else if (cmd.equals("object")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
72 handleObject(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
73 } else if (cmd.equals("quit")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 out.println("Bye!");
a61af66fc99e Initial load
duke
parents:
diff changeset
75 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
76 } else if (cmd.equals("")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
77 // do nothing ...
a61af66fc99e Initial load
duke
parents:
diff changeset
78 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
79 handleUnknown(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
80 }
a61af66fc99e Initial load
duke
parents:
diff changeset
81 }
a61af66fc99e Initial load
duke
parents:
diff changeset
82 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
83 e.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
84 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
85 }
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
a61af66fc99e Initial load
duke
parents:
diff changeset
87 }
a61af66fc99e Initial load
duke
parents:
diff changeset
88
a61af66fc99e Initial load
duke
parents:
diff changeset
89 protected void handleSelect(String query) {
a61af66fc99e Initial load
duke
parents:
diff changeset
90 StringBuffer buf = new StringBuffer(query);
a61af66fc99e Initial load
duke
parents:
diff changeset
91 String tmp = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
92 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
93 out.print(secondPrompt);
a61af66fc99e Initial load
duke
parents:
diff changeset
94 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 tmp = in.readLine();
a61af66fc99e Initial load
duke
parents:
diff changeset
96 } catch (IOException ioe) {
a61af66fc99e Initial load
duke
parents:
diff changeset
97 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
98 }
a61af66fc99e Initial load
duke
parents:
diff changeset
99 if (tmp.equals("") || tmp.equals("go"))
a61af66fc99e Initial load
duke
parents:
diff changeset
100 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
101 buf.append('\n');
a61af66fc99e Initial load
duke
parents:
diff changeset
102 buf.append(tmp);
a61af66fc99e Initial load
duke
parents:
diff changeset
103 }
a61af66fc99e Initial load
duke
parents:
diff changeset
104 query = buf.toString();
a61af66fc99e Initial load
duke
parents:
diff changeset
105
a61af66fc99e Initial load
duke
parents:
diff changeset
106 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
107 soqlEngine.executeQuery(query,
a61af66fc99e Initial load
duke
parents:
diff changeset
108 new ObjectVisitor() {
a61af66fc99e Initial load
duke
parents:
diff changeset
109 public void visit(Object o) {
a61af66fc99e Initial load
duke
parents:
diff changeset
110 if (o != null && o instanceof JSJavaObject) {
a61af66fc99e Initial load
duke
parents:
diff changeset
111 String oopAddr = ((JSJavaObject)o).getOop().getHandle().toString();
a61af66fc99e Initial load
duke
parents:
diff changeset
112 out.println(oopAddr);
a61af66fc99e Initial load
duke
parents:
diff changeset
113 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 out.println((o == null)? "null" : o.toString());
a61af66fc99e Initial load
duke
parents:
diff changeset
115 }
a61af66fc99e Initial load
duke
parents:
diff changeset
116 }
a61af66fc99e Initial load
duke
parents:
diff changeset
117 });
a61af66fc99e Initial load
duke
parents:
diff changeset
118 } catch (SOQLException se) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 se.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
120 }
a61af66fc99e Initial load
duke
parents:
diff changeset
121 }
a61af66fc99e Initial load
duke
parents:
diff changeset
122
a61af66fc99e Initial load
duke
parents:
diff changeset
123 protected void handleClasses(String line) {
a61af66fc99e Initial load
duke
parents:
diff changeset
124 // just list all InstanceKlasses
a61af66fc99e Initial load
duke
parents:
diff changeset
125 InstanceKlass[] klasses = SystemDictionaryHelper.getAllInstanceKlasses();
a61af66fc99e Initial load
duke
parents:
diff changeset
126 for (int i = 0; i < klasses.length; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
127 out.print(klasses[i].getName().asString().replace('/', '.'));
a61af66fc99e Initial load
duke
parents:
diff changeset
128 out.print(" @");
a61af66fc99e Initial load
duke
parents:
diff changeset
129 out.println(klasses[i].getHandle());
a61af66fc99e Initial load
duke
parents:
diff changeset
130 }
a61af66fc99e Initial load
duke
parents:
diff changeset
131 }
a61af66fc99e Initial load
duke
parents:
diff changeset
132
a61af66fc99e Initial load
duke
parents:
diff changeset
133 protected void handleClass(String line) {
a61af66fc99e Initial load
duke
parents:
diff changeset
134 StringTokenizer st = new StringTokenizer(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
135 st.nextToken(); // ignore "class"
a61af66fc99e Initial load
duke
parents:
diff changeset
136 if (st.hasMoreTokens()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
137 String className = st.nextToken();
a61af66fc99e Initial load
duke
parents:
diff changeset
138 InstanceKlass klass = SystemDictionaryHelper.findInstanceKlass(className);
a61af66fc99e Initial load
duke
parents:
diff changeset
139 if (klass == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
140 out.println("class " + className + " not found");
a61af66fc99e Initial load
duke
parents:
diff changeset
141 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
142 // klass.iterate(new OopPrinter(out), true);
a61af66fc99e Initial load
duke
parents:
diff changeset
143
a61af66fc99e Initial load
duke
parents:
diff changeset
144 // base class
a61af66fc99e Initial load
duke
parents:
diff changeset
145 InstanceKlass base = (InstanceKlass) klass.getSuper();
a61af66fc99e Initial load
duke
parents:
diff changeset
146 if (base != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 out.println("super");
a61af66fc99e Initial load
duke
parents:
diff changeset
148 out.print("\t");
a61af66fc99e Initial load
duke
parents:
diff changeset
149 out.println(base.getName().asString().replace('/', '.'));
a61af66fc99e Initial load
duke
parents:
diff changeset
150 }
a61af66fc99e Initial load
duke
parents:
diff changeset
151
a61af66fc99e Initial load
duke
parents:
diff changeset
152 // list immediate fields only
a61af66fc99e Initial load
duke
parents:
diff changeset
153 TypeArray fields = klass.getFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
154 int numFields = (int) fields.getLength();
a61af66fc99e Initial load
duke
parents:
diff changeset
155 ConstantPool cp = klass.getConstants();
a61af66fc99e Initial load
duke
parents:
diff changeset
156 out.println("fields");
a61af66fc99e Initial load
duke
parents:
diff changeset
157 if (numFields != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
158 for (int f = 0; f < numFields; f += InstanceKlass.NEXT_OFFSET) {
a61af66fc99e Initial load
duke
parents:
diff changeset
159 int nameIndex = fields.getShortAt(f + InstanceKlass.NAME_INDEX_OFFSET);
a61af66fc99e Initial load
duke
parents:
diff changeset
160 int sigIndex = fields.getShortAt(f + InstanceKlass.SIGNATURE_INDEX_OFFSET);
a61af66fc99e Initial load
duke
parents:
diff changeset
161 Symbol f_name = cp.getSymbolAt(nameIndex);
a61af66fc99e Initial load
duke
parents:
diff changeset
162 Symbol f_sig = cp.getSymbolAt(sigIndex);
a61af66fc99e Initial load
duke
parents:
diff changeset
163 StringBuffer sigBuf = new StringBuffer();
a61af66fc99e Initial load
duke
parents:
diff changeset
164 new SignatureConverter(f_sig, sigBuf).dispatchField();
a61af66fc99e Initial load
duke
parents:
diff changeset
165 out.print('\t');
a61af66fc99e Initial load
duke
parents:
diff changeset
166 out.print(sigBuf.toString().replace('/', '.'));
a61af66fc99e Initial load
duke
parents:
diff changeset
167 out.print(' ');
a61af66fc99e Initial load
duke
parents:
diff changeset
168 out.println(f_name.asString());
a61af66fc99e Initial load
duke
parents:
diff changeset
169 }
a61af66fc99e Initial load
duke
parents:
diff changeset
170 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
171 out.println("\tno fields in this class");
a61af66fc99e Initial load
duke
parents:
diff changeset
172 }
a61af66fc99e Initial load
duke
parents:
diff changeset
173 }
a61af66fc99e Initial load
duke
parents:
diff changeset
174 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
175 out.println("usage: class <name of the class>");
a61af66fc99e Initial load
duke
parents:
diff changeset
176 }
a61af66fc99e Initial load
duke
parents:
diff changeset
177 }
a61af66fc99e Initial load
duke
parents:
diff changeset
178
a61af66fc99e Initial load
duke
parents:
diff changeset
179 protected Oop getOopAtAddress(Address addr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
180 OopHandle oopHandle = addr.addOffsetToAsOopHandle(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
181 return VM.getVM().getObjectHeap().newOop(oopHandle);
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 void handleObject(String line) {
a61af66fc99e Initial load
duke
parents:
diff changeset
185 StringTokenizer st = new StringTokenizer(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
186 st.nextToken(); // ignore "object"
a61af66fc99e Initial load
duke
parents:
diff changeset
187 if (st.hasMoreTokens()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
188 String addrStr = st.nextToken();
a61af66fc99e Initial load
duke
parents:
diff changeset
189 Address addr = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
190 Debugger dbg = VM.getVM().getDebugger();
a61af66fc99e Initial load
duke
parents:
diff changeset
191 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
192 addr = dbg.parseAddress(addrStr);
a61af66fc99e Initial load
duke
parents:
diff changeset
193 } catch (Exception e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
194 out.println("invalid address : " + e.getMessage());
a61af66fc99e Initial load
duke
parents:
diff changeset
195 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
196 }
a61af66fc99e Initial load
duke
parents:
diff changeset
197
a61af66fc99e Initial load
duke
parents:
diff changeset
198 Oop oop = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
199 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
200 oop = getOopAtAddress(addr);
a61af66fc99e Initial load
duke
parents:
diff changeset
201 } catch (Exception e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
202 out.println("invalid object : " + e.getMessage());
a61af66fc99e Initial load
duke
parents:
diff changeset
203 }
a61af66fc99e Initial load
duke
parents:
diff changeset
204
a61af66fc99e Initial load
duke
parents:
diff changeset
205 if (oop != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
206 oop.iterate(new OopPrinter(out), true);
a61af66fc99e Initial load
duke
parents:
diff changeset
207 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
208 out.println("null object!");
a61af66fc99e Initial load
duke
parents:
diff changeset
209 }
a61af66fc99e Initial load
duke
parents:
diff changeset
210 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
211 out.println("usage: object <address>");
a61af66fc99e Initial load
duke
parents:
diff changeset
212 }
a61af66fc99e Initial load
duke
parents:
diff changeset
213 }
a61af66fc99e Initial load
duke
parents:
diff changeset
214
a61af66fc99e Initial load
duke
parents:
diff changeset
215 protected void handleUnknown(String line) {
a61af66fc99e Initial load
duke
parents:
diff changeset
216 out.println("Unknown command!");
a61af66fc99e Initial load
duke
parents:
diff changeset
217 }
a61af66fc99e Initial load
duke
parents:
diff changeset
218 }