annotate agent/test/libproc/LibprocClient.java @ 18408:2c3666f44855

Truffle: initial commit of object API implementation
author Andreas Woess <andreas.woess@jku.at>
date Tue, 18 Nov 2014 23:19:43 +0100
parents c18cbe5936b8
children
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, 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 import sun.jvm.hotspot.oops.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
26 import sun.jvm.hotspot.runtime.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import sun.jvm.hotspot.tools.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import sun.jvm.hotspot.utilities.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29
a61af66fc99e Initial load
duke
parents:
diff changeset
30 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
31 We don't run any of the "standard" SA command line tools for sanity
a61af66fc99e Initial load
duke
parents:
diff changeset
32 check. This is because the standard tools print addresses in hex
a61af66fc99e Initial load
duke
parents:
diff changeset
33 which could change legally. Also, textual comparison of output may
a61af66fc99e Initial load
duke
parents:
diff changeset
34 not match because of other reasons as well. This tool checks
a61af66fc99e Initial load
duke
parents:
diff changeset
35 validity of threads and frames logically. This class has reference
a61af66fc99e Initial load
duke
parents:
diff changeset
36 frame names from "known" threads. The debuggee is assumed to run
a61af66fc99e Initial load
duke
parents:
diff changeset
37 "LibprocTest.java".
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 LibprocClient extends Tool {
a61af66fc99e Initial load
duke
parents:
diff changeset
41
a61af66fc99e Initial load
duke
parents:
diff changeset
42 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
43 // try to get VM version and check
a61af66fc99e Initial load
duke
parents:
diff changeset
44 String version = VM.getVM().getVMRelease();
a61af66fc99e Initial load
duke
parents:
diff changeset
45 Assert.that(version.startsWith("1.5"), "1.5 expected");
a61af66fc99e Initial load
duke
parents:
diff changeset
46
a61af66fc99e Initial load
duke
parents:
diff changeset
47 // try getting threads
a61af66fc99e Initial load
duke
parents:
diff changeset
48 Threads threads = VM.getVM().getThreads();
a61af66fc99e Initial load
duke
parents:
diff changeset
49 boolean mainTested = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
50
a61af66fc99e Initial load
duke
parents:
diff changeset
51 // check frames of each thread
a61af66fc99e Initial load
duke
parents:
diff changeset
52 for (JavaThread cur = threads.first(); cur != null; cur = cur.next()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
53 if (cur.isJavaThread()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
54 String name = cur.getThreadName();
a61af66fc99e Initial load
duke
parents:
diff changeset
55 // testing of basic frame walking for all threads
a61af66fc99e Initial load
duke
parents:
diff changeset
56 for (JavaVFrame vf = getLastJavaVFrame(cur); vf != null; vf = vf.javaSender()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
57 checkFrame(vf);
a61af66fc99e Initial load
duke
parents:
diff changeset
58 }
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 // special testing for "known" threads. For now, only "main" thread.
a61af66fc99e Initial load
duke
parents:
diff changeset
61 if (name.equals("main")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
62 checkMainThread(cur);
a61af66fc99e Initial load
duke
parents:
diff changeset
63 mainTested = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
64 }
a61af66fc99e Initial load
duke
parents:
diff changeset
65 }
a61af66fc99e Initial load
duke
parents:
diff changeset
66 }
a61af66fc99e Initial load
duke
parents:
diff changeset
67 Assert.that(mainTested, "main thread missing");
a61af66fc99e Initial load
duke
parents:
diff changeset
68 }
a61af66fc99e Initial load
duke
parents:
diff changeset
69
a61af66fc99e Initial load
duke
parents:
diff changeset
70 public static void main(String[] args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
71 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
72 LibprocClient lc = new LibprocClient();
a61af66fc99e Initial load
duke
parents:
diff changeset
73 lc.start(args);
a61af66fc99e Initial load
duke
parents:
diff changeset
74 lc.getAgent().detach();
a61af66fc99e Initial load
duke
parents:
diff changeset
75 System.out.println("\nPASSED\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
76 } catch (Exception exp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
77 System.out.println("\nFAILED\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
78 exp.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
79 }
a61af66fc99e Initial load
duke
parents:
diff changeset
80 }
a61af66fc99e Initial load
duke
parents:
diff changeset
81
a61af66fc99e Initial load
duke
parents:
diff changeset
82 // -- Internals only below this point
a61af66fc99e Initial load
duke
parents:
diff changeset
83 private static JavaVFrame getLastJavaVFrame(JavaThread cur) {
a61af66fc99e Initial load
duke
parents:
diff changeset
84 RegisterMap regMap = cur.newRegisterMap(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
85 Frame f = cur.getCurrentFrameGuess();
a61af66fc99e Initial load
duke
parents:
diff changeset
86 if (f == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
87 System.err.println(" (Unable to get a top most frame)");
a61af66fc99e Initial load
duke
parents:
diff changeset
88 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
89 }
a61af66fc99e Initial load
duke
parents:
diff changeset
90 VFrame vf = VFrame.newVFrame(f, regMap, cur, true, true);
a61af66fc99e Initial load
duke
parents:
diff changeset
91 if (vf == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
92 System.err.println(" (Unable to create vframe for topmost frame guess)");
a61af66fc99e Initial load
duke
parents:
diff changeset
93 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
94 }
a61af66fc99e Initial load
duke
parents:
diff changeset
95 if (vf.isJavaFrame()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
96 return (JavaVFrame) vf;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 }
a61af66fc99e Initial load
duke
parents:
diff changeset
98 return (JavaVFrame) vf.javaSender();
a61af66fc99e Initial load
duke
parents:
diff changeset
99 }
a61af66fc99e Initial load
duke
parents:
diff changeset
100
a61af66fc99e Initial load
duke
parents:
diff changeset
101 private void checkMethodSignature(Symbol sig) {
a61af66fc99e Initial load
duke
parents:
diff changeset
102 SignatureIterator itr = new SignatureIterator(sig) {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 public void doBool () {}
a61af66fc99e Initial load
duke
parents:
diff changeset
104 public void doChar () {}
a61af66fc99e Initial load
duke
parents:
diff changeset
105 public void doFloat () {}
a61af66fc99e Initial load
duke
parents:
diff changeset
106 public void doDouble() {}
a61af66fc99e Initial load
duke
parents:
diff changeset
107 public void doByte () {}
a61af66fc99e Initial load
duke
parents:
diff changeset
108 public void doShort () {}
a61af66fc99e Initial load
duke
parents:
diff changeset
109 public void doInt () {}
a61af66fc99e Initial load
duke
parents:
diff changeset
110 public void doLong () {}
a61af66fc99e Initial load
duke
parents:
diff changeset
111 public void doVoid () {}
a61af66fc99e Initial load
duke
parents:
diff changeset
112 public void doObject(int begin, int end) {}
a61af66fc99e Initial load
duke
parents:
diff changeset
113 public void doArray (int begin, int end) {}
a61af66fc99e Initial load
duke
parents:
diff changeset
114 };
a61af66fc99e Initial load
duke
parents:
diff changeset
115 // this will throw RuntimeException for any invalid item in signature.
a61af66fc99e Initial load
duke
parents:
diff changeset
116 itr.iterate();
a61af66fc99e Initial load
duke
parents:
diff changeset
117 }
a61af66fc99e Initial load
duke
parents:
diff changeset
118
a61af66fc99e Initial load
duke
parents:
diff changeset
119 private void checkBCI(Method m, int bci) {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 if (! m.isNative()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
121 byte[] buf = m.getByteCode();
a61af66fc99e Initial load
duke
parents:
diff changeset
122 Assert.that(bci >= 0 && bci < buf.length, "invalid bci, not in code range");
a61af66fc99e Initial load
duke
parents:
diff changeset
123 if (m.hasLineNumberTable()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
124 int lineNum = m.getLineNumberFromBCI(bci);
a61af66fc99e Initial load
duke
parents:
diff changeset
125 Assert.that(lineNum >= 0, "expecting non-negative line number");
a61af66fc99e Initial load
duke
parents:
diff changeset
126 }
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 private void checkMethodHolder(Method method) {
a61af66fc99e Initial load
duke
parents:
diff changeset
131 Klass klass = method.getMethodHolder();
a61af66fc99e Initial load
duke
parents:
diff changeset
132 Assert.that(klass != null, "expecting non-null instance klass");
a61af66fc99e Initial load
duke
parents:
diff changeset
133 }
a61af66fc99e Initial load
duke
parents:
diff changeset
134
a61af66fc99e Initial load
duke
parents:
diff changeset
135 private void checkFrame(JavaVFrame vf) {
a61af66fc99e Initial load
duke
parents:
diff changeset
136 Method method = vf.getMethod();
a61af66fc99e Initial load
duke
parents:
diff changeset
137 Assert.that(method != null, "expecting a non-null method here");
a61af66fc99e Initial load
duke
parents:
diff changeset
138 Assert.that(method.getName() != null, "expecting non-null method name");
a61af66fc99e Initial load
duke
parents:
diff changeset
139 checkMethodHolder(method);
a61af66fc99e Initial load
duke
parents:
diff changeset
140 checkMethodSignature(method.getSignature());
a61af66fc99e Initial load
duke
parents:
diff changeset
141 checkBCI(method, vf.getBCI());
a61af66fc99e Initial load
duke
parents:
diff changeset
142 }
a61af66fc99e Initial load
duke
parents:
diff changeset
143
a61af66fc99e Initial load
duke
parents:
diff changeset
144 // from the test case LibprocTest.java - in the main thread we
a61af66fc99e Initial load
duke
parents:
diff changeset
145 // should see frames as below
a61af66fc99e Initial load
duke
parents:
diff changeset
146 private static String[] mainThreadMethods = new String[] {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 "java.lang.Object.wait(long)",
a61af66fc99e Initial load
duke
parents:
diff changeset
148 "java.lang.Object.wait()",
a61af66fc99e Initial load
duke
parents:
diff changeset
149 "LibprocTest.main(java.lang.String[])"
a61af66fc99e Initial load
duke
parents:
diff changeset
150 };
a61af66fc99e Initial load
duke
parents:
diff changeset
151
a61af66fc99e Initial load
duke
parents:
diff changeset
152 private void checkMainThread(JavaThread thread) {
a61af66fc99e Initial load
duke
parents:
diff changeset
153 checkFrames(thread, mainThreadMethods);
a61af66fc99e Initial load
duke
parents:
diff changeset
154 }
a61af66fc99e Initial load
duke
parents:
diff changeset
155
a61af66fc99e Initial load
duke
parents:
diff changeset
156 private void checkFrames(JavaThread thread, String[] expectedMethodNames) {
a61af66fc99e Initial load
duke
parents:
diff changeset
157 int i = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
158 for (JavaVFrame vf = getLastJavaVFrame(thread); vf != null; vf = vf.javaSender(), i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
159 Method m = vf.getMethod();
a61af66fc99e Initial load
duke
parents:
diff changeset
160 Assert.that(m.externalNameAndSignature().equals(expectedMethodNames[i]),
a61af66fc99e Initial load
duke
parents:
diff changeset
161 "expected frame missing");
a61af66fc99e Initial load
duke
parents:
diff changeset
162 }
a61af66fc99e Initial load
duke
parents:
diff changeset
163 }
a61af66fc99e Initial load
duke
parents:
diff changeset
164 }