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