aboutsummaryrefslogtreecommitdiff
path: root/agent/src/share/classes/sun/jvm/hotspot/tools/soql/SOQL.java
blob: 67f5ed1e9209dc0d3ebde96ca5f292ba14deec87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

package sun.jvm.hotspot.tools.soql;

import java.io.*;
import java.util.*;
import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.tools.*;
import sun.jvm.hotspot.utilities.*;
import sun.jvm.hotspot.utilities.soql.*;

/**
  This is command line SOQL (Simple Object Query Language) interpreter.
*/

public class SOQL extends Tool {
   public static void main(String[] args) {
      SOQL soql = new SOQL();
      soql.execute(args);
   }

   public SOQL() {
      super();
   }

   public SOQL(JVMDebugger d) {
      super(d);
   }

   protected SOQLEngine soqlEngine;
   protected BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
   protected PrintStream out       = System.out;
   static protected String prompt       = "soql> ";
   static protected String secondPrompt = "> ";

   public void run() {
      soqlEngine = SOQLEngine.getEngine();
      while (true) {
         try {
            out.print(prompt);
            String line = in.readLine();
            if (line == null) {
               return;
            }
            StringTokenizer st = new StringTokenizer(line);
            if (st.hasMoreTokens()) {
               String cmd = st.nextToken();
               if (cmd.equals("select")) {
                  handleSelect(line);
               } else if (cmd.equals("classes")) {
                  handleClasses(line);
               } else if (cmd.equals("class")) {
                  handleClass(line);
               } else if (cmd.equals("object")) {
                  handleObject(line);
               } else if (cmd.equals("quit")) {
                  out.println("Bye!");
                  return;
               } else if (cmd.equals("")) {
                  // do nothing ...
               } else {
                  handleUnknown(line);
               }
            }
         } catch (IOException e) {
            e.printStackTrace();
            return;
         }
      }
   }

   protected void handleSelect(String query) {
      StringBuffer buf = new StringBuffer(query);
      String tmp = null;
      while (true) {
         out.print(secondPrompt);
         try {
            tmp = in.readLine();
         } catch (IOException ioe) {
            break;
         }
         if (tmp.equals("") || tmp.equals("go"))
            break;
         buf.append('\n');
         buf.append(tmp);
      }
      query = buf.toString();

      try {
         soqlEngine.executeQuery(query,
                           new ObjectVisitor() {
                              public void visit(Object o) {
                                 if (o != null && o instanceof JSJavaObject) {
                                     String oopAddr = ((JSJavaObject)o).getOop().getHandle().toString();
                                     out.println(oopAddr);
                                 } else {
                                     out.println((o == null)? "null" : o.toString());
                                 }
                              }
                           });
      } catch (SOQLException se) {
         se.printStackTrace();
      }
   }

   protected void handleClasses(String line) {
      // just list all InstanceKlasses
      InstanceKlass[] klasses = SystemDictionaryHelper.getAllInstanceKlasses();
      for (int i = 0; i < klasses.length; i++) {
         out.print(klasses[i].getName().asString().replace('/', '.'));
         out.print(" @");
         out.println(klasses[i].getAddress());
      }
   }

   protected void handleClass(String line) {
      StringTokenizer st = new StringTokenizer(line);
      st.nextToken(); // ignore "class"
      if (st.hasMoreTokens()) {
         String className = st.nextToken();
         InstanceKlass klass = SystemDictionaryHelper.findInstanceKlass(className);
         if (klass == null) {
            out.println("class " + className + " not found");
         } else {
            // klass.iterate(new OopPrinter(out), true);

            // base class
            InstanceKlass base = (InstanceKlass) klass.getSuper();
            if (base != null) {
               out.println("super");
               out.print("\t");
               out.println(base.getName().asString().replace('/', '.'));
            }

            // list immediate fields only
            U2Array fields = klass.getFields();
            int numFields = (int) fields.length();
            ConstantPool cp = klass.getConstants();
            out.println("fields");
            if (numFields != 0) {
              for (int f = 0; f < numFields; f++){
                 Symbol f_name = klass.getFieldName(f);
                 Symbol f_sig  = klass.getFieldSignature(f);
                 StringBuffer sigBuf = new StringBuffer();
                 new SignatureConverter(f_sig, sigBuf).dispatchField();
                 out.print('\t');
                 out.print(sigBuf.toString().replace('/', '.'));
                 out.print(' ');
                 out.println(f_name.asString());
               }
            } else {
               out.println("\tno fields in this class");
            }
         }
      } else {
         out.println("usage: class <name of the class>");
      }
   }

   protected Oop getOopAtAddress(Address addr) {
      OopHandle oopHandle = addr.addOffsetToAsOopHandle(0);
      return VM.getVM().getObjectHeap().newOop(oopHandle);
   }

   protected void handleObject(String line) {
      StringTokenizer st = new StringTokenizer(line);
      st.nextToken(); // ignore "object"
      if (st.hasMoreTokens()) {
         String addrStr = st.nextToken();
         Address addr = null;
         Debugger dbg = VM.getVM().getDebugger();
         try {
            addr = dbg.parseAddress(addrStr);
         } catch (Exception e) {
            out.println("invalid address : " + e.getMessage());
            return;
         }

         Oop oop = null;
         try {
            oop = getOopAtAddress(addr);
         } catch (Exception e) {
            out.println("invalid object : " + e.getMessage());
         }

         if (oop != null) {
            oop.iterate(new OopPrinter(out), true);
         } else {
            out.println("null object!");
         }
      } else {
         out.println("usage: object <address>");
      }
   }

   protected void handleUnknown(String line) {
      out.println("Unknown command!");
   }
}