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