Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/StackTrace.java
41161 views
/*1* Copyright (c) 2002, 2019, 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*/2324package sun.jvm.hotspot.tools;2526import sun.jvm.hotspot.debugger.*;27import sun.jvm.hotspot.runtime.*;28import sun.jvm.hotspot.oops.*;2930/** Traverses and prints the stack traces for all Java threads in the31* remote VM */32public class StackTrace extends Tool {33// in non-verbose mode pc, sp and Method* are not printed34public StackTrace(boolean v, boolean concurrentLocks) {35this.verbose = v;36this.concurrentLocks = concurrentLocks;37}3839public StackTrace() {40this(true, true);41}4243public void run() {44run(System.out);45}4647public StackTrace(JVMDebugger d) {48super(d);49}5051public StackTrace(JVMDebugger d, boolean v, boolean concurrentLocks) {52super(d);53this.verbose = v;54this.concurrentLocks = concurrentLocks;55}5657public void run(java.io.PrintStream tty) {58// Ready to go with the database...59try {60// print deadlock information before stack trace61DeadlockDetector.print(tty);62} catch (Exception exp) {63exp.printStackTrace();64tty.println("Can't print deadlocks:" + exp.getMessage());65}6667try {68ConcurrentLocksPrinter concLocksPrinter = null;69if (concurrentLocks) {70concLocksPrinter = new ConcurrentLocksPrinter();71}72Threads threads = VM.getVM().getThreads();73for (int i = 0; i < threads.getNumberOfThreads(); i++) {74JavaThread cur = threads.getJavaThreadAt(i);75if (cur.isJavaThread()) {76cur.printThreadInfoOn(tty);77try {78int count = 0;7980for (JavaVFrame vf = cur.getLastJavaVFrameDbg(); vf != null; vf = vf.javaSender()) {81Method method = vf.getMethod();82tty.print(" - " + method.externalNameAndSignature() +83" @bci=" + vf.getBCI());8485int lineNumber = method.getLineNumberFromBCI(vf.getBCI());86if (lineNumber != -1) {87tty.print(", line=" + lineNumber);88}8990if (verbose) {91Address pc = vf.getFrame().getPC();92if (pc != null) {93tty.print(", pc=" + pc);94}9596tty.print(", Method*=" + method.getAddress());97}9899if (vf.isCompiledFrame()) {100tty.print(" (Compiled frame");101if (vf.isDeoptimized()) {102tty.print(" [deoptimized]");103}104}105if (vf.isInterpretedFrame()) {106tty.print(" (Interpreted frame");107}108if (vf.mayBeImpreciseDbg()) {109tty.print("; information may be imprecise");110}111112tty.println(")");113vf.printLockInfo(tty, count++);114}115} catch (Exception e) {116tty.println("Error occurred during stack walking:");117e.printStackTrace();118}119tty.println();120if (concurrentLocks) {121concLocksPrinter.print(cur, tty);122}123tty.println();124}125}126}127catch (AddressException e) {128System.err.println("Error accessing address 0x" + Long.toHexString(e.getAddress()));129e.printStackTrace();130}131}132133public static void main(String[] args) {134StackTrace st = new StackTrace();135st.execute(args);136}137138private boolean verbose;139private boolean concurrentLocks;140}141142143