hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/StackTrace.java
author kevinw
Wed, 26 Jun 2013 00:01:20 +0100
changeset 18481 fa7f6ad24216
parent 13728 882756847a04
child 21065 b8281f54308b
permissions -rw-r--r--
8010278: SA: provide mechanism for using an alternative SA debugger back-end. Reviewed-by: sla, dsamersoff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 5547
diff changeset
     2
 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot.tools;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import sun.jvm.hotspot.debugger.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import sun.jvm.hotspot.runtime.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import sun.jvm.hotspot.oops.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
/** Traverses and prints the stack traces for all Java threads in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
 * remote VM */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
public class StackTrace extends Tool {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 5547
diff changeset
    34
    // in non-verbose mode pc, sp and Method* are not printed
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
    public StackTrace(boolean v, boolean concurrentLocks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
        this.verbose = v;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
        this.concurrentLocks = concurrentLocks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
    public StackTrace() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
        this(true, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
    public void run() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
        run(System.out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
18481
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 13728
diff changeset
    48
    public StackTrace(JVMDebugger d) {
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 13728
diff changeset
    49
        super(d);
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 13728
diff changeset
    50
    }
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 13728
diff changeset
    51
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 13728
diff changeset
    52
    public StackTrace(JVMDebugger d, boolean v, boolean concurrentLocks) {
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 13728
diff changeset
    53
        super(d);
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 13728
diff changeset
    54
        this.verbose = v;
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 13728
diff changeset
    55
        this.concurrentLocks = concurrentLocks;
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 13728
diff changeset
    56
    }
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 13728
diff changeset
    57
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    public void run(java.io.PrintStream tty) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
        // Ready to go with the database...
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
            // print deadlock information before stack trace
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
            DeadlockDetector.print(tty);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
        } catch (Exception exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
            exp.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
            tty.println("Can't print deadlocks:" + exp.getMessage());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
            ConcurrentLocksPrinter concLocksPrinter = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
            if (concurrentLocks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
                concLocksPrinter = new ConcurrentLocksPrinter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
            Threads threads = VM.getVM().getThreads();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
            int i = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
            for (JavaThread cur = threads.first(); cur != null; cur = cur.next(), i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
                if (cur.isJavaThread()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
                    Address sp = cur.getLastJavaSP();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
                    tty.print("Thread ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
                    cur.printThreadIDOn(tty);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
                    tty.print(": (state = " + cur.getThreadState());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
                    if (verbose) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
                        tty.println(", current Java SP = " + sp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
                    tty.println(')');
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
                    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
                        for (JavaVFrame vf = cur.getLastJavaVFrameDbg(); vf != null; vf = vf.javaSender()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
                            Method method = vf.getMethod();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
                            tty.print(" - " + method.externalNameAndSignature() +
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
                            " @bci=" + vf.getBCI());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
                            int lineNumber = method.getLineNumberFromBCI(vf.getBCI());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
                            if (lineNumber != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
                                tty.print(", line=" + lineNumber);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
                            if (verbose) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
                                Address pc = vf.getFrame().getPC();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
                                if (pc != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
                                    tty.print(", pc=" + pc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
                                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 5547
diff changeset
   102
                                tty.print(", Method*=" + method.getAddress());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
                            if (vf.isCompiledFrame()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
                                tty.print(" (Compiled frame");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
                                if (vf.isDeoptimized()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
                                  tty.print(" [deoptimized]");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
                                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
                            if (vf.isInterpretedFrame()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
                                tty.print(" (Interpreted frame");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
                            if (vf.mayBeImpreciseDbg()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
                                tty.print("; information may be imprecise");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
                            tty.println(")");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
                    } catch (Exception e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
                        tty.println("Error occurred during stack walking:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
                        e.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
                    tty.println();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
                    if (concurrentLocks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
                        concLocksPrinter.print(cur, tty);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
                    tty.println();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
              }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
      catch (AddressException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
        System.err.println("Error accessing address 0x" + Long.toHexString(e.getAddress()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
        e.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
   public static void main(String[] args) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
      StackTrace st = new StackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
      st.start(args);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
      st.stop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
   private boolean verbose;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
   private boolean concurrentLocks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
}