hotspot/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/ThreadStackTrace.java
changeset 45371 5d0b68ea07c3
equal deleted inserted replaced
45321:b0f2b8ff25a2 45371:5d0b68ea07c3
       
     1 /*
       
     2  * Copyright (c) 2017, 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 package sun.jvm.hotspot.runtime;
       
    26 
       
    27 import java.util.*;
       
    28 
       
    29 public class ThreadStackTrace {
       
    30     private JavaThread                        thread;
       
    31     private int                               depth;  // number of stack frames added
       
    32     private ArrayList<StackFrameInfo>         frames;
       
    33 
       
    34     public ThreadStackTrace(JavaThread t) {
       
    35         this.thread = t;
       
    36         this.depth = 0;
       
    37         this.frames = new ArrayList<StackFrameInfo>();
       
    38     }
       
    39 
       
    40     public int getStackDepth() {
       
    41         return depth;
       
    42     }
       
    43 
       
    44     public StackFrameInfo stackFrameAt(int index) {
       
    45         return frames.get(index);
       
    46     }
       
    47 
       
    48     public void dumpStack(int maxDepth) {
       
    49         if (!thread.isJavaThread()) {
       
    50             System.out.println("dumpStack: not java Thread returning");
       
    51             return;
       
    52         }
       
    53         try {
       
    54             for (JavaVFrame vf = thread.getLastJavaVFrameDbg(); vf != null; vf = vf.javaSender()) {
       
    55                 StackFrameInfo frame = new StackFrameInfo(vf);
       
    56                 frames.add(frame);
       
    57                 depth++;
       
    58 
       
    59                 if (maxDepth > 0 && depth == maxDepth) {
       
    60                     // Skip frames if more than maxDepth
       
    61                     break;
       
    62                 }
       
    63             }
       
    64         } catch (Exception e) {
       
    65             System.out.println("Error occurred during stack walking:");
       
    66             e.printStackTrace();
       
    67         }
       
    68     }
       
    69 }