Path: blob/master/src/jdk.hotspot.agent/test/libproc/LibprocClient.java
41145 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324import sun.jvm.hotspot.oops.*;25import sun.jvm.hotspot.runtime.*;26import sun.jvm.hotspot.tools.*;27import sun.jvm.hotspot.utilities.*;2829/**30We don't run any of the "standard" SA command line tools for sanity31check. This is because the standard tools print addresses in hex32which could change legally. Also, textual comparison of output may33not match because of other reasons as well. This tool checks34validity of threads and frames logically. This class has reference35frame names from "known" threads. The debuggee is assumed to run36"LibprocTest.java".37*/3839public class LibprocClient extends Tool {4041public void run() {42// try to get VM version and check43String version = VM.getVM().getVMRelease();44Assert.that(version.startsWith("1.5"), "1.5 expected");4546// try getting threads47Threads threads = VM.getVM().getThreads();48boolean mainTested = false;4950// check frames of each thread51for (JavaThread cur = threads.first(); cur != null; cur = cur.next()) {52if (cur.isJavaThread()) {53String name = cur.getThreadName();54// testing of basic frame walking for all threads55for (JavaVFrame vf = getLastJavaVFrame(cur); vf != null; vf = vf.javaSender()) {56checkFrame(vf);57}5859// special testing for "known" threads. For now, only "main" thread.60if (name.equals("main")) {61checkMainThread(cur);62mainTested = true;63}64}65}66Assert.that(mainTested, "main thread missing");67}6869public static void main(String[] args) {70try {71LibprocClient lc = new LibprocClient();72lc.start(args);73lc.getAgent().detach();74System.out.println("\nPASSED\n");75} catch (Exception exp) {76System.out.println("\nFAILED\n");77exp.printStackTrace();78}79}8081// -- Internals only below this point82private static JavaVFrame getLastJavaVFrame(JavaThread cur) {83RegisterMap regMap = cur.newRegisterMap(true);84Frame f = cur.getCurrentFrameGuess();85if (f == null) {86System.err.println(" (Unable to get a top most frame)");87return null;88}89VFrame vf = VFrame.newVFrame(f, regMap, cur, true, true);90if (vf == null) {91System.err.println(" (Unable to create vframe for topmost frame guess)");92return null;93}94if (vf.isJavaFrame()) {95return (JavaVFrame) vf;96}97return (JavaVFrame) vf.javaSender();98}99100private void checkMethodSignature(Symbol sig) {101SignatureIterator itr = new SignatureIterator(sig) {102public void doBool () {}103public void doChar () {}104public void doFloat () {}105public void doDouble() {}106public void doByte () {}107public void doShort () {}108public void doInt () {}109public void doLong () {}110public void doVoid () {}111public void doObject(int begin, int end) {}112public void doArray (int begin, int end) {}113};114// this will throw RuntimeException for any invalid item in signature.115itr.iterate();116}117118private void checkBCI(Method m, int bci) {119if (! m.isNative()) {120byte[] buf = m.getByteCode();121Assert.that(bci >= 0 && bci < buf.length, "invalid bci, not in code range");122if (m.hasLineNumberTable()) {123int lineNum = m.getLineNumberFromBCI(bci);124Assert.that(lineNum >= 0, "expecting non-negative line number");125}126}127}128129private void checkMethodHolder(Method method) {130Klass klass = method.getMethodHolder();131Assert.that(klass != null, "expecting non-null instance klass");132}133134private void checkFrame(JavaVFrame vf) {135Method method = vf.getMethod();136Assert.that(method != null, "expecting a non-null method here");137Assert.that(method.getName() != null, "expecting non-null method name");138checkMethodHolder(method);139checkMethodSignature(method.getSignature());140checkBCI(method, vf.getBCI());141}142143// from the test case LibprocTest.java - in the main thread we144// should see frames as below145private static String[] mainThreadMethods = new String[] {146"java.lang.Object.wait(long)",147"java.lang.Object.wait()",148"LibprocTest.main(java.lang.String[])"149};150151private void checkMainThread(JavaThread thread) {152checkFrames(thread, mainThreadMethods);153}154155private void checkFrames(JavaThread thread, String[] expectedMethodNames) {156int i = 0;157for (JavaVFrame vf = getLastJavaVFrame(thread); vf != null; vf = vf.javaSender(), i++) {158Method m = vf.getMethod();159Assert.that(m.externalNameAndSignature().equals(expectedMethodNames[i]),160"expected frame missing");161}162}163}164165166