src/java.base/share/classes/java/lang/StackFrameInfo.java
author jiangli
Sat, 03 Nov 2018 15:40:19 -0400
changeset 52402 72d4e10305b9
parent 47818 2f6ab27efb60
child 52427 3c6aa484536c
permissions -rw-r--r--
8212995: Placing the Integer.IntegerCache and cached Integer objects in the closed archive heap region. Summary: Support shareable archive object subgraphs in closed archive heap regions. Reviewed-by: iklam, ccheung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     1
/*
47818
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     4
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    10
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    15
 * accompanied this code).
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    16
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    20
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    23
 * questions.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    24
 */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    25
package java.lang;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    26
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    27
import jdk.internal.misc.JavaLangInvokeAccess;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    28
import jdk.internal.misc.SharedSecrets;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    29
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    30
import static java.lang.StackWalker.Option.*;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    31
import java.lang.StackWalker.StackFrame;
47294
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47216
diff changeset
    32
import java.lang.invoke.MethodType;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    33
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    34
class StackFrameInfo implements StackFrame {
47818
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
    35
    private final byte RETAIN_CLASS_REF = 0x01;
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
    36
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    37
    private final static JavaLangInvokeAccess JLIA =
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    38
        SharedSecrets.getJavaLangInvokeAccess();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    39
47818
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
    40
    private final byte flags;
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    41
    private final Object memberName;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    42
    private final short bci;
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 36511
diff changeset
    43
    private volatile StackTraceElement ste;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    44
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    45
    /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    46
     * Create StackFrameInfo for StackFrameTraverser and LiveStackFrameTraverser
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    47
     * to use
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    48
     */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    49
    StackFrameInfo(StackWalker walker) {
47818
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
    50
        this.flags = walker.retainClassRef ? RETAIN_CLASS_REF : 0;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    51
        this.bci = -1;
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    52
        this.memberName = JLIA.newMemberName();
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    53
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    54
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    55
    // package-private called by StackStreamFactory to skip
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    56
    // the capability check
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    57
    Class<?> declaringClass() {
47818
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
    58
        return JLIA.getDeclaringClass(memberName);
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    59
    }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    60
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    61
    // ----- implementation of StackFrame methods
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    62
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    63
    @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    64
    public String getClassName() {
47818
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
    65
        return declaringClass().getName();
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    66
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    67
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    68
    @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    69
    public Class<?> getDeclaringClass() {
47818
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
    70
        ensureRetainClassRefEnabled();
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
    71
        return declaringClass();
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    72
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    73
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    74
    @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    75
    public String getMethodName() {
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    76
        return JLIA.getName(memberName);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    77
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    78
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    79
    @Override
47294
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47216
diff changeset
    80
    public MethodType getMethodType() {
47818
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
    81
        ensureRetainClassRefEnabled();
47294
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47216
diff changeset
    82
        return JLIA.getMethodType(memberName);
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47216
diff changeset
    83
    }
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47216
diff changeset
    84
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47216
diff changeset
    85
    @Override
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47216
diff changeset
    86
    public String getDescriptor() {
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47216
diff changeset
    87
        return JLIA.getMethodDescriptor(memberName);
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47216
diff changeset
    88
    }
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47216
diff changeset
    89
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47216
diff changeset
    90
    @Override
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    91
    public int getByteCodeIndex() {
38779
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents: 37819
diff changeset
    92
        // bci not available for native methods
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents: 37819
diff changeset
    93
        if (isNativeMethod())
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents: 37819
diff changeset
    94
            return -1;
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents: 37819
diff changeset
    95
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
    96
        return bci;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    97
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    98
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    99
    @Override
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   100
    public String getFileName() {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   101
        return toStackTraceElement().getFileName();
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   102
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   103
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   104
    @Override
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   105
    public int getLineNumber() {
38779
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents: 37819
diff changeset
   106
        // line number not available for native methods
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   107
        if (isNativeMethod())
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   108
            return -2;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   109
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   110
        return toStackTraceElement().getLineNumber();
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   111
    }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   112
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   113
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   114
    @Override
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   115
    public boolean isNativeMethod() {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   116
        return JLIA.isNative(memberName);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   117
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   118
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   119
    @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   120
    public String toString() {
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37526
diff changeset
   121
        return toStackTraceElement().toString();
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   122
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   123
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34362
diff changeset
   124
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34362
diff changeset
   125
    public StackTraceElement toStackTraceElement() {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 36511
diff changeset
   126
        StackTraceElement s = ste;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 36511
diff changeset
   127
        if (s == null) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 36511
diff changeset
   128
            synchronized (this) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 36511
diff changeset
   129
                s = ste;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 36511
diff changeset
   130
                if (s == null) {
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 38779
diff changeset
   131
                    ste = s = StackTraceElement.of(this);
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 36511
diff changeset
   132
                }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 36511
diff changeset
   133
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34362
diff changeset
   134
        }
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 36511
diff changeset
   135
        return s;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34362
diff changeset
   136
    }
47818
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
   137
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
   138
    private void ensureRetainClassRefEnabled() {
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
   139
        if ((flags & RETAIN_CLASS_REF) == 0) {
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
   140
            throw new UnsupportedOperationException("No access to RETAIN_CLASS_REFERENCE");
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
   141
        }
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47294
diff changeset
   142
    }
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   143
}