comparison agent/src/share/classes/sun/jvm/hotspot/tools/soql/SOQL.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children c18cbe5936b8
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2003-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 package sun.jvm.hotspot.tools.soql;
26
27 import java.io.*;
28 import java.util.*;
29 import sun.jvm.hotspot.debugger.*;
30 import sun.jvm.hotspot.oops.*;
31 import sun.jvm.hotspot.runtime.*;
32 import sun.jvm.hotspot.tools.*;
33 import sun.jvm.hotspot.utilities.*;
34 import sun.jvm.hotspot.utilities.soql.*;
35
36 /**
37 This is command line SOQL (Simple Object Query Language) interpreter.
38 */
39
40 public class SOQL extends Tool {
41 public static void main(String[] args) {
42 SOQL soql = new SOQL();
43 soql.start(args);
44 soql.stop();
45 }
46
47 protected SOQLEngine soqlEngine;
48 protected BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
49 protected PrintStream out = System.out;
50 static protected String prompt = "soql> ";
51 static protected String secondPrompt = "> ";
52
53 public void run() {
54 soqlEngine = SOQLEngine.getEngine();
55 while (true) {
56 try {
57 out.print(prompt);
58 String line = in.readLine();
59 if (line == null) {
60 return;
61 }
62 StringTokenizer st = new StringTokenizer(line);
63 if (st.hasMoreTokens()) {
64 String cmd = st.nextToken();
65 if (cmd.equals("select")) {
66 handleSelect(line);
67 } else if (cmd.equals("classes")) {
68 handleClasses(line);
69 } else if (cmd.equals("class")) {
70 handleClass(line);
71 } else if (cmd.equals("object")) {
72 handleObject(line);
73 } else if (cmd.equals("quit")) {
74 out.println("Bye!");
75 return;
76 } else if (cmd.equals("")) {
77 // do nothing ...
78 } else {
79 handleUnknown(line);
80 }
81 }
82 } catch (IOException e) {
83 e.printStackTrace();
84 return;
85 }
86 }
87 }
88
89 protected void handleSelect(String query) {
90 StringBuffer buf = new StringBuffer(query);
91 String tmp = null;
92 while (true) {
93 out.print(secondPrompt);
94 try {
95 tmp = in.readLine();
96 } catch (IOException ioe) {
97 break;
98 }
99 if (tmp.equals("") || tmp.equals("go"))
100 break;
101 buf.append('\n');
102 buf.append(tmp);
103 }
104 query = buf.toString();
105
106 try {
107 soqlEngine.executeQuery(query,
108 new ObjectVisitor() {
109 public void visit(Object o) {
110 if (o != null && o instanceof JSJavaObject) {
111 String oopAddr = ((JSJavaObject)o).getOop().getHandle().toString();
112 out.println(oopAddr);
113 } else {
114 out.println((o == null)? "null" : o.toString());
115 }
116 }
117 });
118 } catch (SOQLException se) {
119 se.printStackTrace();
120 }
121 }
122
123 protected void handleClasses(String line) {
124 // just list all InstanceKlasses
125 InstanceKlass[] klasses = SystemDictionaryHelper.getAllInstanceKlasses();
126 for (int i = 0; i < klasses.length; i++) {
127 out.print(klasses[i].getName().asString().replace('/', '.'));
128 out.print(" @");
129 out.println(klasses[i].getHandle());
130 }
131 }
132
133 protected void handleClass(String line) {
134 StringTokenizer st = new StringTokenizer(line);
135 st.nextToken(); // ignore "class"
136 if (st.hasMoreTokens()) {
137 String className = st.nextToken();
138 InstanceKlass klass = SystemDictionaryHelper.findInstanceKlass(className);
139 if (klass == null) {
140 out.println("class " + className + " not found");
141 } else {
142 // klass.iterate(new OopPrinter(out), true);
143
144 // base class
145 InstanceKlass base = (InstanceKlass) klass.getSuper();
146 if (base != null) {
147 out.println("super");
148 out.print("\t");
149 out.println(base.getName().asString().replace('/', '.'));
150 }
151
152 // list immediate fields only
153 TypeArray fields = klass.getFields();
154 int numFields = (int) fields.getLength();
155 ConstantPool cp = klass.getConstants();
156 out.println("fields");
157 if (numFields != 0) {
158 for (int f = 0; f < numFields; f += InstanceKlass.NEXT_OFFSET) {
159 int nameIndex = fields.getShortAt(f + InstanceKlass.NAME_INDEX_OFFSET);
160 int sigIndex = fields.getShortAt(f + InstanceKlass.SIGNATURE_INDEX_OFFSET);
161 Symbol f_name = cp.getSymbolAt(nameIndex);
162 Symbol f_sig = cp.getSymbolAt(sigIndex);
163 StringBuffer sigBuf = new StringBuffer();
164 new SignatureConverter(f_sig, sigBuf).dispatchField();
165 out.print('\t');
166 out.print(sigBuf.toString().replace('/', '.'));
167 out.print(' ');
168 out.println(f_name.asString());
169 }
170 } else {
171 out.println("\tno fields in this class");
172 }
173 }
174 } else {
175 out.println("usage: class <name of the class>");
176 }
177 }
178
179 protected Oop getOopAtAddress(Address addr) {
180 OopHandle oopHandle = addr.addOffsetToAsOopHandle(0);
181 return VM.getVM().getObjectHeap().newOop(oopHandle);
182 }
183
184 protected void handleObject(String line) {
185 StringTokenizer st = new StringTokenizer(line);
186 st.nextToken(); // ignore "object"
187 if (st.hasMoreTokens()) {
188 String addrStr = st.nextToken();
189 Address addr = null;
190 Debugger dbg = VM.getVM().getDebugger();
191 try {
192 addr = dbg.parseAddress(addrStr);
193 } catch (Exception e) {
194 out.println("invalid address : " + e.getMessage());
195 return;
196 }
197
198 Oop oop = null;
199 try {
200 oop = getOopAtAddress(addr);
201 } catch (Exception e) {
202 out.println("invalid object : " + e.getMessage());
203 }
204
205 if (oop != null) {
206 oop.iterate(new OopPrinter(out), true);
207 } else {
208 out.println("null object!");
209 }
210 } else {
211 out.println("usage: object <address>");
212 }
213 }
214
215 protected void handleUnknown(String line) {
216 out.println("Unknown command!");
217 }
218 }