src/java.base/share/classes/java/lang/StackStreamFactory.java
author darcy
Mon, 20 May 2019 17:29:44 -0700
changeset 54952 a978d86ac389
parent 47216 71c04702a3d5
permissions -rw-r--r--
8224175: Fix inconsistencies in @jls and @jvms tags Reviewed-by: jjg, rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     1
/*
38784
c0a88deb692a 8152893: StackWalker#getCallerClass is not filtering hidden/ reflection frames when walker is configured to show hidden /reflection frames
bchristi
parents: 37819
diff changeset
     2
 * Copyright (c) 2015, 2016, 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
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34882
diff changeset
    27
import jdk.internal.reflect.MethodAccessor;
43694
fcc6fff17bfa 8173898: StackWalker.walk throws InternalError if called from a constructor invoked through reflection.
dfuchs
parents: 41117
diff changeset
    28
import jdk.internal.reflect.ConstructorAccessor;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    29
import java.lang.StackWalker.Option;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    30
import java.lang.StackWalker.StackFrame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    31
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    32
import java.lang.annotation.Native;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    33
import java.lang.reflect.Method;
43694
fcc6fff17bfa 8173898: StackWalker.walk throws InternalError if called from a constructor invoked through reflection.
dfuchs
parents: 41117
diff changeset
    34
import java.lang.reflect.Constructor;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    35
import java.util.HashSet;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    36
import java.util.NoSuchElementException;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    37
import java.util.Objects;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    38
import java.util.Set;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    39
import java.util.Spliterator;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    40
import java.util.function.Consumer;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    41
import java.util.function.Function;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    42
import java.util.stream.Stream;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    43
import java.util.stream.StreamSupport;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37534
diff changeset
    44
import sun.security.action.GetPropertyAction;
34362
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
import static java.lang.StackStreamFactory.WalkerState.*;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    47
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
 * StackStreamFactory class provides static factory methods
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    50
 * to get different kinds of stack walker/traverser.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    51
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    52
 * AbstractStackWalker provides the basic stack walking support
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    53
 * fetching stack frames from VM in batches.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    54
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    55
 * AbstractStackWalker subclass is specialized for a specific kind of stack traversal
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    56
 * to avoid overhead of Stream/Lambda
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    57
 * 1. Support traversing Stream<StackFrame>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    58
 * 2. StackWalker::getCallerClass
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
    59
 * 3. AccessControlContext getting ProtectionDomain
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    60
 */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    61
final class StackStreamFactory {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    62
    private StackStreamFactory() {}
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    63
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    64
    // Stack walk implementation classes to be excluded during stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    65
    // lazily add subclasses when they are loaded.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    66
    private final static Set<Class<?>> stackWalkImplClasses = init();
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
    private static final int SMALL_BATCH       = 8;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    69
    private static final int BATCH_SIZE        = 32;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    70
    private static final int LARGE_BATCH_SIZE  = 256;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    71
    private static final int MIN_BATCH_SIZE    = SMALL_BATCH;
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
    // These flags must match the values maintained in the VM
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    74
    @Native private static final int DEFAULT_MODE              = 0x0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    75
    @Native private static final int FILL_CLASS_REFS_ONLY      = 0x2;
41117
57810b6b669e 8157464: Disallow StackWalker.getCallerClass() be called by caller-sensitive method
mchung
parents: 38784
diff changeset
    76
    @Native private static final int GET_CALLER_CLASS          = 0x4;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    77
    @Native private static final int SHOW_HIDDEN_FRAMES        = 0x20;  // LambdaForms are hidden by the VM
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    78
    @Native private static final int FILL_LIVE_STACK_FRAMES    = 0x100;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    79
    /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    80
     * For Throwable to use StackWalker, set useNewThrowable to true.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    81
     * Performance work and extensive testing is needed to replace the
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    82
     * VM built-in backtrace filled in Throwable with the StackWalker.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    83
     */
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    84
    final static boolean isDebug =
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    85
            "true".equals(GetPropertyAction.privilegedGetProperty("stackwalk.debug"));
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    86
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    87
    static <T> StackFrameTraverser<T>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    88
        makeStackTraverser(StackWalker walker, Function<? super Stream<StackFrame>, ? extends T> function)
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    89
    {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    90
        if (walker.hasLocalsOperandsOption())
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
    91
            return new LiveStackInfoTraverser<>(walker, function);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    92
        else
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
    93
            return new StackFrameTraverser<>(walker, function);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    94
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    95
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    96
    /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    97
     * Gets a stack stream to find caller class.
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
    static CallerClassFinder makeCallerFinder(StackWalker walker) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   100
        return new CallerClassFinder(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   101
    }
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
    enum WalkerState {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   104
        NEW,     // the stream is new and stack walking has not started
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   105
        OPEN,    // the stream is open when it is being traversed.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   106
        CLOSED;  // the stream is closed when the stack walking is done
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   107
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   108
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   109
    /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   110
     * Subclass of AbstractStackWalker implements a specific stack walking logic.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   111
     * It needs to set up the frame buffer and stack walking mode.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   112
     *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   113
     * It initiates the VM stack walking via the callStackWalk method that serves
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   114
     * as the anchored frame and VM will call up to AbstractStackWalker::doStackWalk.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   115
     *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   116
     * @param <R> the type of the result returned from stack walking
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   117
     * @param <T> the type of the data gathered for each frame.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   118
     *            For example, StackFrameInfo for StackWalker::walk or
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   119
     *            Class<?> for StackWalker::getCallerClass
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   120
     */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   121
    static abstract class AbstractStackWalker<R, T> {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   122
        protected final StackWalker walker;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   123
        protected final Thread thread;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   124
        protected final int maxDepth;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   125
        protected final long mode;
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   126
        protected int depth;    // traversed stack depth
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   127
        protected FrameBuffer<? extends T> frameBuffer;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   128
        protected long anchor;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   129
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   130
        // buffers to fill in stack frame information
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   131
        protected AbstractStackWalker(StackWalker walker, int mode) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   132
            this(walker, mode, Integer.MAX_VALUE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   133
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   134
        protected AbstractStackWalker(StackWalker walker, int mode, int maxDepth) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   135
            this.thread = Thread.currentThread();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   136
            this.mode = toStackWalkMode(walker, mode);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   137
            this.walker = walker;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   138
            this.maxDepth = maxDepth;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   139
            this.depth = 0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   140
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   141
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   142
        private int toStackWalkMode(StackWalker walker, int mode) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   143
            int newMode = mode;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   144
            if (walker.hasOption(Option.SHOW_HIDDEN_FRAMES) &&
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   145
                    (mode & FILL_CLASS_REFS_ONLY) != FILL_CLASS_REFS_ONLY)
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   146
                newMode |= SHOW_HIDDEN_FRAMES;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   147
            if (walker.hasLocalsOperandsOption())
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   148
                newMode |= FILL_LIVE_STACK_FRAMES;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   149
            return newMode;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   150
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   151
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   152
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   153
         * A callback method to consume the stack frames.  This method is invoked
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   154
         * once stack walking begins (i.e. it is only invoked when walkFrames is called).
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   155
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   156
         * Each specialized AbstractStackWalker subclass implements the consumeFrames method
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   157
         * to control the following:
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   158
         * 1. fetch the subsequent batches of stack frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   159
         * 2. reuse or expand the allocated buffers
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   160
         * 3. create specialized StackFrame objects
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   161
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   162
         * @return the number of consumed frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   163
         */
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   164
         protected abstract R consumeFrames();
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   165
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   166
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   167
         * Initialize FrameBuffer.  Subclass should implement this method to
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   168
         * create its custom frame buffers.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   169
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   170
         protected abstract void initFrameBuffer();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   171
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   172
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   173
         * Returns the suggested next batch size.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   174
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   175
         * Subclass should override this method to change the batch size
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   176
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   177
         * @param lastBatchFrameCount number of frames in the last batch; or zero
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   178
         * @return suggested batch size
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   179
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   180
        protected abstract int batchSize(int lastBatchFrameCount);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   181
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   182
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   183
         * Returns the next batch size, always >= minimum batch size (32)
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   184
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   185
         * Subclass may override this method if the minimum batch size is different.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   186
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   187
        protected int getNextBatchSize() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   188
            int lastBatchSize = depth == 0 ? 0 : frameBuffer.curBatchFrameCount();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   189
            int nextBatchSize = batchSize(lastBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   190
            if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   191
                System.err.println("last batch size = " + lastBatchSize +
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   192
                                   " next batch size = " + nextBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   193
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   194
            return nextBatchSize >= MIN_BATCH_SIZE ? nextBatchSize : MIN_BATCH_SIZE;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   195
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   196
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   197
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   198
         * Checks if this stream is in the given state. Otherwise, throws IllegalStateException.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   199
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   200
         * VM also validates this stream if it's anchored for stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   201
         * when stack frames are fetched for each batch.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   202
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   203
        final void checkState(WalkerState state) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   204
            if (thread != Thread.currentThread()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   205
                throw new IllegalStateException("Invalid thread walking this stack stream: " +
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   206
                        Thread.currentThread().getName() + " " + thread.getName());
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   207
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   208
            switch (state) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   209
                case NEW:
34373
a551d53250dd 8143967: The stream state check should handle unsigned anchor value
mchung
parents: 34362
diff changeset
   210
                    if (anchor != 0) {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   211
                        throw new IllegalStateException("This stack stream is being reused.");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   212
                    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   213
                    break;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   214
                case OPEN:
34373
a551d53250dd 8143967: The stream state check should handle unsigned anchor value
mchung
parents: 34362
diff changeset
   215
                    if (anchor == 0 || anchor == -1L) {
a551d53250dd 8143967: The stream state check should handle unsigned anchor value
mchung
parents: 34362
diff changeset
   216
                        throw new IllegalStateException("This stack stream is not valid for walking.");
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   217
                    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   218
                    break;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   219
                case CLOSED:
34373
a551d53250dd 8143967: The stream state check should handle unsigned anchor value
mchung
parents: 34362
diff changeset
   220
                    if (anchor != -1L) {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   221
                        throw new IllegalStateException("This stack stream is not closed.");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   222
                    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   223
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   224
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   225
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   226
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   227
         * Close this stream.  This stream becomes invalid to walk.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   228
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   229
        private void close() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   230
            this.anchor = -1L;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   231
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   232
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   233
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   234
         * Walks stack frames until {@link #consumeFrames} is done consuming
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   235
         * the frames it is interested in.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   236
         */
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   237
        final R walk() {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   238
            checkState(NEW);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   239
            try {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   240
                // VM will need to stablize the stack before walking.  It will invoke
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   241
                // the AbstractStackWalker::doStackWalk method once it fetches the first batch.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   242
                // the callback will be invoked within the scope of the callStackWalk frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   243
                return beginStackWalk();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   244
            } finally {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   245
                close();  // done traversal; close the stream
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   246
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   247
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   248
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   249
        private boolean skipReflectionFrames() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   250
            return !walker.hasOption(Option.SHOW_REFLECT_FRAMES) &&
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   251
                       !walker.hasOption(Option.SHOW_HIDDEN_FRAMES);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   252
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   253
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   254
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   255
         * Returns {@code Class} object at the current frame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   256
         * or {@code null} if no more frame. If advanceToNextBatch is true,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   257
         * it will only fetch the next batch.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   258
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   259
        final Class<?> peekFrame() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   260
            while (frameBuffer.isActive() && depth < maxDepth) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   261
                if (frameBuffer.isEmpty()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   262
                    // fetch another batch of stack frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   263
                    getNextBatch();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   264
                } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   265
                    Class<?> c = frameBuffer.get();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   266
                    if (skipReflectionFrames() && isReflectionFrame(c)) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   267
                        if (isDebug)
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   268
                            System.err.println("  skip: frame " + frameBuffer.getIndex() + " " + c);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   269
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   270
                        frameBuffer.next();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   271
                        depth++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   272
                        continue;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   273
                    } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   274
                        return c;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   275
                    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   276
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   277
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   278
            return null;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   279
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   280
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   281
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   282
         * This method is only invoked by VM.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   283
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   284
         * It will invoke the consumeFrames method to start the stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   285
         * with the first batch of stack frames.  Each specialized AbstractStackWalker
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   286
         * subclass implements the consumeFrames method to control the following:
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   287
         * 1. fetch the subsequent batches of stack frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   288
         * 2. reuse or expand the allocated buffers
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   289
         * 3. create specialized StackFrame objects
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   290
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   291
        private Object doStackWalk(long anchor, int skipFrames, int batchSize,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   292
                                                int bufStartIndex, int bufEndIndex) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   293
            checkState(NEW);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   294
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   295
            frameBuffer.check(skipFrames);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   296
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   297
            if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   298
                System.err.format("doStackWalk: skip %d start %d end %d%n",
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   299
                        skipFrames, bufStartIndex, bufEndIndex);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   300
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   301
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   302
            this.anchor = anchor;  // set anchor for this bulk stack frame traversal
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   303
            frameBuffer.setBatch(depth, bufStartIndex, bufEndIndex);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   304
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   305
            // traverse all frames and perform the action on the stack frames, if specified
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   306
            return consumeFrames();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   307
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   308
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   309
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   310
         * Get next batch of stack frames.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   311
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   312
        private int getNextBatch() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   313
            int nextBatchSize = Math.min(maxDepth - depth, getNextBatchSize());
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   314
            if (!frameBuffer.isActive() || nextBatchSize <= 0) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   315
                if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   316
                    System.out.format("  more stack walk done%n");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   317
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   318
                frameBuffer.freeze();   // stack walk done
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   319
                return 0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   320
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   321
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   322
            return fetchStackFrames(nextBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   323
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   324
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   325
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   326
         * This method traverses the next stack frame and returns the Class
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   327
         * invoking that stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   328
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   329
         * This method can only be called during the walk method.  This is intended
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   330
         * to be used to walk the stack frames in one single invocation and
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   331
         * this stack stream will be invalidated once walk is done.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   332
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   333
         * @see #tryNextFrame
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   334
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   335
        final Class<?> nextFrame() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   336
            if (!hasNext()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   337
                return null;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   338
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   339
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   340
            Class<?> c = frameBuffer.next();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   341
            depth++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   342
            return c;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   343
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   344
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   345
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   346
         * Returns true if there is next frame to be traversed.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   347
         * This skips hidden frames unless this StackWalker has
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   348
         * {@link Option#SHOW_REFLECT_FRAMES}
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   349
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   350
        final boolean hasNext() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   351
            return peekFrame() != null;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   352
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   353
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   354
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   355
         * Begin stack walking - pass the allocated arrays to the VM to fill in
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   356
         * stack frame information.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   357
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   358
         * VM first anchors the frame of the current thread.  A traversable stream
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   359
         * on this thread's stack will be opened.  The VM will fetch the first batch
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   360
         * of stack frames and call AbstractStackWalker::doStackWalk to invoke the
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   361
         * stack walking function on each stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   362
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   363
         * If all fetched stack frames are traversed, AbstractStackWalker::fetchStackFrames will
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   364
         * fetch the next batch of stack frames to continue.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   365
         */
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   366
        private R beginStackWalk() {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   367
            // initialize buffers for VM to fill the stack frame info
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   368
            initFrameBuffer();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   369
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   370
            return callStackWalk(mode, 0,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   371
                                 frameBuffer.curBatchFrameCount(),
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   372
                                 frameBuffer.startIndex(),
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   373
                                 frameBuffer.frames());
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   374
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   375
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   376
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   377
         * Fetches stack frames.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   378
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   379
         * @params batchSize number of elements of the frame  buffers for this batch
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   380
         * @returns number of frames fetched in this batch
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   381
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   382
        private int fetchStackFrames(int batchSize) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   383
            int startIndex = frameBuffer.startIndex();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   384
            frameBuffer.resize(startIndex, batchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   385
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   386
            int endIndex = fetchStackFrames(mode, anchor, batchSize,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   387
                                            startIndex,
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   388
                                            frameBuffer.frames());
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   389
            if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   390
                System.out.format("  more stack walk requesting %d got %d to %d frames%n",
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   391
                                  batchSize, frameBuffer.startIndex(), endIndex);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   392
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   393
            int numFrames = endIndex - startIndex;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   394
            if (numFrames == 0) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   395
                frameBuffer.freeze(); // done stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   396
            } else {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   397
                frameBuffer.setBatch(depth, startIndex, endIndex);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   398
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   399
            return numFrames;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   400
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   401
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   402
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   403
         * Begins stack walking.  This method anchors this frame and invokes
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   404
         * AbstractStackWalker::doStackWalk after fetching the first batch of stack frames.
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   405
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   406
         * @param mode        mode of stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   407
         * @param skipframes  number of frames to be skipped before filling the frame buffer.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   408
         * @param batchSize   the batch size, max. number of elements to be filled in the frame buffers.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   409
         * @param startIndex  start index of the frame buffers to be filled.
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   410
         * @param frames      Either a Class<?> array, if mode is {@link #FILL_CLASS_REFS_ONLY}
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   411
         *                    or a {@link StackFrameInfo} (or derivative) array otherwise.
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   412
         * @return            Result of AbstractStackWalker::doStackWalk
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   413
         */
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   414
        private native R callStackWalk(long mode, int skipframes,
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   415
                                       int batchSize, int startIndex,
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   416
                                       T[] frames);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   417
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   418
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   419
         * Fetch the next batch of stack frames.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   420
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   421
         * @param mode        mode of stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   422
         * @param anchor
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   423
         * @param batchSize   the batch size, max. number of elements to be filled in the frame buffers.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   424
         * @param startIndex  start index of the frame buffers to be filled.
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   425
         * @param frames      Either a Class<?> array, if mode is {@link #FILL_CLASS_REFS_ONLY}
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   426
         *                    or a {@link StackFrameInfo} (or derivative) array otherwise.
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   427
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   428
         * @return the end index to the frame buffers
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   429
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   430
        private native int fetchStackFrames(long mode, long anchor,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   431
                                            int batchSize, int startIndex,
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   432
                                            T[] frames);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   433
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   434
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   435
    /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   436
     * This StackFrameTraverser supports {@link Stream} traversal.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   437
     *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   438
     * This class implements Spliterator::forEachRemaining and Spliterator::tryAdvance.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   439
     */
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   440
    static class StackFrameTraverser<T> extends AbstractStackWalker<T, StackFrameInfo>
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   441
            implements Spliterator<StackFrame>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   442
    {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   443
        static {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   444
            stackWalkImplClasses.add(StackFrameTraverser.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   445
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   446
        private static final int CHARACTERISTICS = Spliterator.ORDERED | Spliterator.IMMUTABLE;
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   447
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   448
        final class StackFrameBuffer extends FrameBuffer<StackFrameInfo> {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   449
            private StackFrameInfo[] stackFrames;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   450
            StackFrameBuffer(int initialBatchSize) {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   451
                super(initialBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   452
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   453
                this.stackFrames = new StackFrameInfo[initialBatchSize];
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   454
                for (int i = START_POS; i < initialBatchSize; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   455
                    stackFrames[i] = new StackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   456
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   457
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   458
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   459
            @Override
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   460
            StackFrameInfo[] frames() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   461
                return stackFrames;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   462
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   463
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   464
            @Override
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   465
            void resize(int startIndex, int elements) {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   466
                if (!isActive())
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   467
                    throw new IllegalStateException("inactive frame buffer can't be resized");
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   468
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   469
                assert startIndex == START_POS :
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   470
                       "bad start index " + startIndex + " expected " + START_POS;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   471
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   472
                int size = startIndex+elements;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   473
                if (stackFrames.length < size) {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   474
                    StackFrameInfo[] newFrames = new StackFrameInfo[size];
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   475
                    // copy initial magic...
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   476
                    System.arraycopy(stackFrames, 0, newFrames, 0, startIndex);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   477
                    stackFrames = newFrames;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   478
                }
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   479
                for (int i = startIndex; i < size; i++) {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   480
                    stackFrames[i] = new StackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   481
                }
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   482
                currentBatchSize = size;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   483
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   484
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   485
            @Override
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   486
            StackFrameInfo nextStackFrame() {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   487
                if (isEmpty()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   488
                    throw new NoSuchElementException("origin=" + origin + " fence=" + fence);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   489
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   490
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   491
                StackFrameInfo frame = stackFrames[origin];
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   492
                origin++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   493
                return frame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   494
            }
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   495
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   496
            @Override
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   497
            final Class<?> at(int index) {
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37781
diff changeset
   498
                return stackFrames[index].declaringClass();
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   499
            }
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   500
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   501
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   502
        final Function<? super Stream<StackFrame>, ? extends T> function;  // callback
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   503
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   504
        StackFrameTraverser(StackWalker walker,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   505
                            Function<? super Stream<StackFrame>, ? extends T> function) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   506
            this(walker, function, DEFAULT_MODE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   507
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   508
        StackFrameTraverser(StackWalker walker,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   509
                            Function<? super Stream<StackFrame>, ? extends T> function,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   510
                            int mode) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   511
            super(walker, mode);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   512
            this.function = function;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   513
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   514
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   515
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   516
         * Returns next StackFrame object in the current batch of stack frames;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   517
         * or null if no more stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   518
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   519
        StackFrame nextStackFrame() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   520
            if (!hasNext()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   521
                return null;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   522
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   523
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   524
            StackFrameInfo frame = frameBuffer.nextStackFrame();
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   525
            depth++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   526
            return frame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   527
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   528
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   529
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   530
        protected T consumeFrames() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   531
            checkState(OPEN);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   532
            Stream<StackFrame> stream = StreamSupport.stream(this, false);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   533
            if (function != null) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   534
                return function.apply(stream);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   535
            } else
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   536
                throw new UnsupportedOperationException();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   537
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   538
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   539
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   540
        protected void initFrameBuffer() {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   541
            this.frameBuffer = new StackFrameBuffer(getNextBatchSize());
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   542
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   543
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   544
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   545
        protected int batchSize(int lastBatchFrameCount) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   546
            if (lastBatchFrameCount == 0) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   547
                // First batch, use estimateDepth if not exceed the large batch size
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   548
                // and not too small
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   549
                int initialBatchSize = Math.max(walker.estimateDepth(), SMALL_BATCH);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   550
                return Math.min(initialBatchSize, LARGE_BATCH_SIZE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   551
            } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   552
                if (lastBatchFrameCount > BATCH_SIZE) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   553
                    return lastBatchFrameCount;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   554
                } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   555
                    return Math.min(lastBatchFrameCount*2, BATCH_SIZE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   556
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   557
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   558
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   559
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   560
        // ------- Implementation of Spliterator
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   561
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   562
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   563
        public Spliterator<StackFrame> trySplit() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   564
            return null;   // ordered stream and do not allow to split
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   565
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   566
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   567
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   568
        public long estimateSize() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   569
            return maxDepth;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   570
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   571
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   572
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   573
        public int characteristics() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   574
            return CHARACTERISTICS;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   575
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   576
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   577
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   578
        public void forEachRemaining(Consumer<? super StackFrame> action) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   579
            checkState(OPEN);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   580
            for (int n = 0; n < maxDepth; n++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   581
                StackFrame frame = nextStackFrame();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   582
                if (frame == null) break;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   583
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   584
                action.accept(frame);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   585
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   586
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   587
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   588
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   589
        public boolean tryAdvance(Consumer<? super StackFrame> action) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   590
            checkState(OPEN);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   591
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   592
            int index = frameBuffer.getIndex();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   593
            if (hasNext()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   594
                StackFrame frame = nextStackFrame();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   595
                action.accept(frame);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   596
                if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   597
                    System.err.println("tryAdvance: " + index + " " + frame);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   598
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   599
                return true;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   600
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   601
            if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   602
                System.err.println("tryAdvance: " + index + " NO element");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   603
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   604
            return false;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   605
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   606
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   607
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   608
    /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   609
     * CallerClassFinder is specialized to return Class<?> for each stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   610
     * StackFrame is not requested.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   611
     */
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   612
    static final class CallerClassFinder extends AbstractStackWalker<Integer, Class<?>> {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   613
        static {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   614
            stackWalkImplClasses.add(CallerClassFinder.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   615
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   616
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   617
        private Class<?> caller;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   618
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   619
        CallerClassFinder(StackWalker walker) {
41117
57810b6b669e 8157464: Disallow StackWalker.getCallerClass() be called by caller-sensitive method
mchung
parents: 38784
diff changeset
   620
            super(walker, FILL_CLASS_REFS_ONLY|GET_CALLER_CLASS);
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   621
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   622
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   623
        final class ClassBuffer extends FrameBuffer<Class<?>> {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   624
            Class<?>[] classes;      // caller class for fast path
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   625
            ClassBuffer(int batchSize) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   626
                super(batchSize);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   627
                classes = new Class<?>[batchSize];
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   628
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   629
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   630
            @Override
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   631
            Class<?>[] frames() { return classes;}
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   632
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   633
            @Override
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   634
            final Class<?> at(int index) { return classes[index];}
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   635
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   636
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   637
            // ------ subclass may override the following methods -------
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   638
            /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   639
             * Resizes the buffers for VM to fill in the next batch of stack frames.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   640
             * The next batch will start at the given startIndex with the maximum number
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   641
             * of elements.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   642
             *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   643
             * <p> Subclass may override this method to manage the allocated buffers.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   644
             *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   645
             * @param startIndex the start index for the first frame of the next batch to fill in.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   646
             * @param elements the number of elements for the next batch to fill in.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   647
             *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   648
             */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   649
            @Override
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   650
            void resize(int startIndex, int elements) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   651
                if (!isActive())
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   652
                    throw new IllegalStateException("inactive frame buffer can't be resized");
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   653
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   654
                assert startIndex == START_POS :
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   655
                       "bad start index " + startIndex + " expected " + START_POS;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   656
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   657
                int size = startIndex+elements;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   658
                if (classes.length < size) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   659
                    // copy the elements in classes array to the newly allocated one.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   660
                    // classes[0] is a Thread object
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   661
                    Class<?>[] prev = classes;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   662
                    classes = new Class<?>[size];
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   663
                    System.arraycopy(prev, 0, classes, 0, startIndex);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   664
                }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   665
                currentBatchSize = size;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   666
            }
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   667
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   668
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   669
        Class<?> findCaller() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   670
            walk();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   671
            return caller;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   672
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   673
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   674
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   675
        protected Integer consumeFrames() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   676
            checkState(OPEN);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   677
            int n = 0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   678
            Class<?>[] frames = new Class<?>[2];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   679
            // skip the API calling this getCallerClass method
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   680
            // 0: StackWalker::getCallerClass
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   681
            // 1: caller-sensitive method
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   682
            // 2: caller class
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   683
            while (n < 2 && (caller = nextFrame()) != null) {
38784
c0a88deb692a 8152893: StackWalker#getCallerClass is not filtering hidden/ reflection frames when walker is configured to show hidden /reflection frames
bchristi
parents: 37819
diff changeset
   684
                if (isMethodHandleFrame(caller)) { continue; }
c0a88deb692a 8152893: StackWalker#getCallerClass is not filtering hidden/ reflection frames when walker is configured to show hidden /reflection frames
bchristi
parents: 37819
diff changeset
   685
                if (isReflectionFrame(caller)) { continue; }
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   686
                frames[n++] = caller;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   687
            }
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   688
            if (frames[1] == null) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 41117
diff changeset
   689
                throw new IllegalCallerException("no caller frame");
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   690
            }
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   691
            return n;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   692
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   693
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   694
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   695
        protected void initFrameBuffer() {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   696
            this.frameBuffer = new ClassBuffer(getNextBatchSize());
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   697
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   698
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   699
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   700
        protected int batchSize(int lastBatchFrameCount) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   701
            return MIN_BATCH_SIZE;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   702
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   703
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   704
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   705
        protected int getNextBatchSize() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   706
            return MIN_BATCH_SIZE;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   707
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   708
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   709
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   710
    static final class LiveStackInfoTraverser<T> extends StackFrameTraverser<T> {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   711
        static {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   712
            stackWalkImplClasses.add(LiveStackInfoTraverser.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   713
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   714
        // VM will fill in all method info and live stack info directly in StackFrameInfo
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   715
        final class LiveStackFrameBuffer extends FrameBuffer<LiveStackFrameInfo> {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   716
            private LiveStackFrameInfo[] stackFrames;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   717
            LiveStackFrameBuffer(int initialBatchSize) {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   718
                super(initialBatchSize);
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   719
                this.stackFrames = new LiveStackFrameInfo[initialBatchSize];
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   720
                for (int i = START_POS; i < initialBatchSize; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   721
                    stackFrames[i] = new LiveStackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   722
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   723
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   724
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   725
            @Override
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   726
            LiveStackFrameInfo[] frames() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   727
                return stackFrames;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   728
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   729
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   730
            @Override
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   731
            void resize(int startIndex, int elements) {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   732
                if (!isActive()) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   733
                    throw new IllegalStateException("inactive frame buffer can't be resized");
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   734
                }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   735
                assert startIndex == START_POS :
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   736
                       "bad start index " + startIndex + " expected " + START_POS;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   737
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   738
                int size = startIndex + elements;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   739
                if (stackFrames.length < size) {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   740
                    LiveStackFrameInfo[] newFrames = new LiveStackFrameInfo[size];
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   741
                    // copy initial magic...
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   742
                    System.arraycopy(stackFrames, 0, newFrames, 0, startIndex);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   743
                    stackFrames = newFrames;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   744
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   745
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   746
                for (int i = startIndex(); i < size; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   747
                    stackFrames[i] = new LiveStackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   748
                }
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   749
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   750
                currentBatchSize = size;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   751
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   752
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   753
            @Override
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   754
            LiveStackFrameInfo nextStackFrame() {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   755
                if (isEmpty()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   756
                    throw new NoSuchElementException("origin=" + origin + " fence=" + fence);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   757
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   758
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   759
                LiveStackFrameInfo frame = stackFrames[origin];
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   760
                origin++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   761
                return frame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   762
            }
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   763
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   764
            @Override
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   765
            final Class<?> at(int index) {
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37781
diff changeset
   766
                return stackFrames[index].declaringClass();
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   767
            }
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   768
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   769
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   770
        LiveStackInfoTraverser(StackWalker walker,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   771
                               Function<? super Stream<StackFrame>, ? extends T> function) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   772
            super(walker, function, DEFAULT_MODE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   773
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   774
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   775
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   776
        protected void initFrameBuffer() {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   777
            this.frameBuffer = new LiveStackFrameBuffer(getNextBatchSize());
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   778
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   779
    }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   780
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   781
    /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   782
     * Frame buffer
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   783
     *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   784
     * Each specialized AbstractStackWalker subclass may subclass the FrameBuffer.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   785
     */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   786
    static abstract class FrameBuffer<F> {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   787
        static final int START_POS = 2;     // 0th and 1st elements are reserved
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   788
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   789
        // buffers for VM to fill stack frame info
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   790
        int currentBatchSize;    // current batch size
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   791
        int origin;         // index to the current traversed stack frame
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   792
        int fence;          // index to the last frame in the current batch
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   793
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   794
        FrameBuffer(int initialBatchSize) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   795
            if (initialBatchSize < MIN_BATCH_SIZE) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   796
                throw new IllegalArgumentException(initialBatchSize +
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   797
                        " < minimum batch size: " + MIN_BATCH_SIZE);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   798
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   799
            this.origin = START_POS;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   800
            this.fence = 0;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   801
            this.currentBatchSize = initialBatchSize;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   802
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   803
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   804
        /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   805
         * Returns an array of frames that may be used to store frame objects
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   806
         * when walking the stack.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   807
         *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   808
         * May be an array of {@code Class<?>} if the {@code AbstractStackWalker}
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   809
         * mode is {@link #FILL_CLASS_REFS_ONLY}, or an array of
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   810
         * {@link StackFrameInfo} (or derivative) array otherwise.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   811
         *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   812
         * @return An array of frames that may be used to store frame objects
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   813
         * when walking the stack. Must not be null.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   814
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   815
        abstract F[] frames(); // must not return null
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   816
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   817
        /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   818
         * Resizes the buffers for VM to fill in the next batch of stack frames.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   819
         * The next batch will start at the given startIndex with the maximum number
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   820
         * of elements.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   821
         *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   822
         * <p> Subclass may override this method to manage the allocated buffers.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   823
         *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   824
         * @param startIndex the start index for the first frame of the next batch to fill in.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   825
         * @param elements the number of elements for the next batch to fill in.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   826
         *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   827
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   828
        abstract void resize(int startIndex, int elements);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   829
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   830
        /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   831
         * Return the class at the given position in the current batch.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   832
         * @param index the position of the frame.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   833
         * @return the class at the given position in the current batch.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   834
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   835
        abstract Class<?> at(int index);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   836
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   837
        // ------ subclass may override the following methods -------
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   838
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   839
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   840
         * Returns the start index for this frame buffer is refilled.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   841
         *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   842
         * This implementation reuses the allocated buffer for the next batch
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   843
         * of stack frames.  For subclass to retain the fetched stack frames,
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   844
         * it should override this method to return the index at which the frame
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   845
         * should be filled in for the next batch.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   846
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   847
        int startIndex() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   848
            return START_POS;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   849
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   850
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   851
        /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   852
         * Returns next StackFrame object in the current batch of stack frames
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   853
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   854
        F nextStackFrame() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   855
            throw new InternalError("should not reach here");
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   856
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   857
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   858
        // ------ FrameBuffer implementation ------
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   859
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   860
        final int curBatchFrameCount() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   861
            return currentBatchSize-START_POS;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   862
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   863
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   864
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   865
         * Tests if this frame buffer is empty.  All frames are fetched.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   866
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   867
        final boolean isEmpty() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   868
            return origin >= fence || (origin == START_POS && fence == 0);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   869
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   870
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   871
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   872
         * Freezes this frame buffer.  The stack stream source is done fetching.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   873
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   874
        final void freeze() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   875
            origin = 0;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   876
            fence = 0;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   877
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   878
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   879
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   880
         * Tests if this frame buffer is active.  It is inactive when
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   881
         * it is done for traversal.  All stack frames have been traversed.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   882
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   883
        final boolean isActive() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   884
            return origin > 0 && (fence == 0 || origin < fence || fence == currentBatchSize);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   885
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   886
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   887
        /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   888
         * Gets the class at the current frame and move to the next frame.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   889
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   890
        final Class<?> next() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   891
            if (isEmpty()) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   892
                throw new NoSuchElementException("origin=" + origin + " fence=" + fence);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   893
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   894
            Class<?> c = at(origin);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   895
            origin++;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   896
            if (isDebug) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   897
                int index = origin-1;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   898
                System.out.format("  next frame at %d: %s (origin %d fence %d)%n", index,
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   899
                        Objects.toString(c), index, fence);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   900
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   901
            return c;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   902
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   903
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   904
        /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   905
         * Gets the class at the current frame.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   906
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   907
        final Class<?> get() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   908
            if (isEmpty()) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   909
                throw new NoSuchElementException("origin=" + origin + " fence=" + fence);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   910
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   911
            return at(origin);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   912
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   913
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   914
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   915
         * Returns the index of the current frame.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   916
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   917
        final int getIndex() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   918
            return origin;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   919
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   920
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   921
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   922
         * Set the start and end index of a new batch of stack frames that have
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   923
         * been filled in this frame buffer.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   924
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   925
        final void setBatch(int depth, int startIndex, int endIndex) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   926
            if (startIndex <= 0 || endIndex <= 0)
43694
fcc6fff17bfa 8173898: StackWalker.walk throws InternalError if called from a constructor invoked through reflection.
dfuchs
parents: 41117
diff changeset
   927
                throw new IllegalArgumentException("startIndex=" + startIndex
fcc6fff17bfa 8173898: StackWalker.walk throws InternalError if called from a constructor invoked through reflection.
dfuchs
parents: 41117
diff changeset
   928
                        + " endIndex=" + endIndex);
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   929
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   930
            this.origin = startIndex;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   931
            this.fence = endIndex;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   932
            if (depth == 0 && fence > 0) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   933
                // filter the frames due to the stack stream implementation
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   934
                for (int i = START_POS; i < fence; i++) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   935
                    Class<?> c = at(i);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   936
                    if (isDebug) System.err.format("  frame %d: %s%n", i, c);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   937
                    if (filterStackWalkImpl(c)) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   938
                        origin++;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   939
                    } else {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   940
                        break;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   941
                    }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   942
                }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   943
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   944
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   945
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   946
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   947
         * Checks if the origin is the expected start index.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   948
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   949
        final void check(int skipFrames) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   950
            int index = skipFrames + START_POS;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   951
            if (origin != index) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   952
                // stack walk must continue with the previous frame depth
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   953
                throw new IllegalStateException("origin " + origin + " != " + index);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   954
            }
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   955
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   956
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   957
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   958
    private static native boolean checkStackWalkModes();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   959
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   960
    // avoid loading other subclasses as they may not be used
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   961
    private static Set<Class<?>> init() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   962
        if (!checkStackWalkModes()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   963
            throw new InternalError("StackWalker mode values do not match with JVM");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   964
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   965
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   966
        Set<Class<?>> classes = new HashSet<>();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   967
        classes.add(StackWalker.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   968
        classes.add(StackStreamFactory.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   969
        classes.add(AbstractStackWalker.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   970
        return classes;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   971
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   972
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   973
    private static boolean filterStackWalkImpl(Class<?> c) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   974
        return stackWalkImplClasses.contains(c) ||
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   975
                c.getName().startsWith("java.util.stream.");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   976
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   977
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   978
    // MethodHandle frames are not hidden and CallerClassFinder has
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   979
    // to filter them out
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   980
    private static boolean isMethodHandleFrame(Class<?> c) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   981
        return c.getName().startsWith("java.lang.invoke.");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   982
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   983
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   984
    private static boolean isReflectionFrame(Class<?> c) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   985
        // ## should filter all @Hidden frames?
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   986
        return c == Method.class ||
43694
fcc6fff17bfa 8173898: StackWalker.walk throws InternalError if called from a constructor invoked through reflection.
dfuchs
parents: 41117
diff changeset
   987
               c == Constructor.class ||
fcc6fff17bfa 8173898: StackWalker.walk throws InternalError if called from a constructor invoked through reflection.
dfuchs
parents: 41117
diff changeset
   988
               MethodAccessor.class.isAssignableFrom(c) ||
fcc6fff17bfa 8173898: StackWalker.walk throws InternalError if called from a constructor invoked through reflection.
dfuchs
parents: 41117
diff changeset
   989
               ConstructorAccessor.class.isAssignableFrom(c) ||
fcc6fff17bfa 8173898: StackWalker.walk throws InternalError if called from a constructor invoked through reflection.
dfuchs
parents: 41117
diff changeset
   990
               c.getName().startsWith("java.lang.invoke.LambdaForm");
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   991
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   992
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   993
}